[PKTGEN]: Multiqueue support.
[safe/jmp/linux-2.6] / net / core / pktgen.c
1 /*
2  * Authors:
3  * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4  *                             Uppsala University and
5  *                             Swedish University of Agricultural Sciences
6  *
7  * Alexey Kuznetsov  <kuznet@ms2.inr.ac.ru>
8  * Ben Greear <greearb@candelatech.com>
9  * Jens Låås <jens.laas@data.slu.se>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version
14  * 2 of the License, or (at your option) any later version.
15  *
16  *
17  * A tool for loading the network with preconfigurated packets.
18  * The tool is implemented as a linux module.  Parameters are output
19  * device, delay (to hard_xmit), number of packets, and whether
20  * to use multiple SKBs or just the same one.
21  * pktgen uses the installed interface's output routine.
22  *
23  * Additional hacking by:
24  *
25  * Jens.Laas@data.slu.se
26  * Improved by ANK. 010120.
27  * Improved by ANK even more. 010212.
28  * MAC address typo fixed. 010417 --ro
29  * Integrated.  020301 --DaveM
30  * Added multiskb option 020301 --DaveM
31  * Scaling of results. 020417--sigurdur@linpro.no
32  * Significant re-work of the module:
33  *   *  Convert to threaded model to more efficiently be able to transmit
34  *       and receive on multiple interfaces at once.
35  *   *  Converted many counters to __u64 to allow longer runs.
36  *   *  Allow configuration of ranges, like min/max IP address, MACs,
37  *       and UDP-ports, for both source and destination, and can
38  *       set to use a random distribution or sequentially walk the range.
39  *   *  Can now change most values after starting.
40  *   *  Place 12-byte packet in UDP payload with magic number,
41  *       sequence number, and timestamp.
42  *   *  Add receiver code that detects dropped pkts, re-ordered pkts, and
43  *       latencies (with micro-second) precision.
44  *   *  Add IOCTL interface to easily get counters & configuration.
45  *   --Ben Greear <greearb@candelatech.com>
46  *
47  * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48  * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49  * as a "fastpath" with a configurable number of clones after alloc's.
50  * clone_skb=0 means all packets are allocated this also means ranges time
51  * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
52  * clones.
53  *
54  * Also moved to /proc/net/pktgen/
55  * --ro
56  *
57  * Sept 10:  Fixed threading/locking.  Lots of bone-headed and more clever
58  *    mistakes.  Also merged in DaveM's patch in the -pre6 patch.
59  * --Ben Greear <greearb@candelatech.com>
60  *
61  * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62  *
63  *
64  * 021124 Finished major redesign and rewrite for new functionality.
65  * See Documentation/networking/pktgen.txt for how to use this.
66  *
67  * The new operation:
68  * For each CPU one thread/process is created at start. This process checks
69  * for running devices in the if_list and sends packets until count is 0 it
70  * also the thread checks the thread->control which is used for inter-process
71  * communication. controlling process "posts" operations to the threads this
72  * way. The if_lock should be possible to remove when add/rem_device is merged
73  * into this too.
74  *
75  * By design there should only be *one* "controlling" process. In practice
76  * multiple write accesses gives unpredictable result. Understood by "write"
77  * to /proc gives result code thats should be read be the "writer".
78  * For practical use this should be no problem.
79  *
80  * Note when adding devices to a specific CPU there good idea to also assign
81  * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
82  * --ro
83  *
84  * Fix refcount off by one if first packet fails, potential null deref,
85  * memleak 030710- KJP
86  *
87  * First "ranges" functionality for ipv6 030726 --ro
88  *
89  * Included flow support. 030802 ANK.
90  *
91  * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
92  *
93  * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94  * ia64 compilation fix from  Aron Griffis <aron@hp.com> 040604
95  *
96  * New xmit() return, do_div and misc clean up by Stephen Hemminger
97  * <shemminger@osdl.org> 040923
98  *
99  * Randy Dunlap fixed u64 printk compiler waring
100  *
101  * Remove FCS from BW calculation.  Lennert Buytenhek <buytenh@wantstofly.org>
102  * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103  *
104  * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105  * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106  *
107  * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
108  * 050103
109  *
110  * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111  *
112  * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113  *
114  * Fixed src_mac command to set source mac of packet to value specified in
115  * command by Adit Ranadive <adit.262@gmail.com>
116  *
117  */
118 #include <linux/sys.h>
119 #include <linux/types.h>
120 #include <linux/module.h>
121 #include <linux/moduleparam.h>
122 #include <linux/kernel.h>
123 #include <linux/mutex.h>
124 #include <linux/sched.h>
125 #include <linux/slab.h>
126 #include <linux/vmalloc.h>
127 #include <linux/unistd.h>
128 #include <linux/string.h>
129 #include <linux/ptrace.h>
130 #include <linux/errno.h>
131 #include <linux/ioport.h>
132 #include <linux/interrupt.h>
133 #include <linux/capability.h>
134 #include <linux/freezer.h>
135 #include <linux/delay.h>
136 #include <linux/timer.h>
137 #include <linux/list.h>
138 #include <linux/init.h>
139 #include <linux/skbuff.h>
140 #include <linux/netdevice.h>
141 #include <linux/inet.h>
142 #include <linux/inetdevice.h>
143 #include <linux/rtnetlink.h>
144 #include <linux/if_arp.h>
145 #include <linux/if_vlan.h>
146 #include <linux/in.h>
147 #include <linux/ip.h>
148 #include <linux/ipv6.h>
149 #include <linux/udp.h>
150 #include <linux/proc_fs.h>
151 #include <linux/seq_file.h>
152 #include <linux/wait.h>
153 #include <linux/etherdevice.h>
154 #include <linux/kthread.h>
155 #include <net/checksum.h>
156 #include <net/ipv6.h>
157 #include <net/addrconf.h>
158 #ifdef CONFIG_XFRM
159 #include <net/xfrm.h>
160 #endif
161 #include <asm/byteorder.h>
162 #include <linux/rcupdate.h>
163 #include <asm/bitops.h>
164 #include <asm/io.h>
165 #include <asm/dma.h>
166 #include <asm/uaccess.h>
167 #include <asm/div64.h>          /* do_div */
168 #include <asm/timex.h>
169
170 #define VERSION  "pktgen v2.68: Packet Generator for packet performance testing.\n"
171
172 /* The buckets are exponential in 'width' */
173 #define LAT_BUCKETS_MAX 32
174 #define IP_NAME_SZ 32
175 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
176 #define MPLS_STACK_BOTTOM htonl(0x00000100)
177
178 /* Device flag bits */
179 #define F_IPSRC_RND   (1<<0)    /* IP-Src Random  */
180 #define F_IPDST_RND   (1<<1)    /* IP-Dst Random  */
181 #define F_UDPSRC_RND  (1<<2)    /* UDP-Src Random */
182 #define F_UDPDST_RND  (1<<3)    /* UDP-Dst Random */
183 #define F_MACSRC_RND  (1<<4)    /* MAC-Src Random */
184 #define F_MACDST_RND  (1<<5)    /* MAC-Dst Random */
185 #define F_TXSIZE_RND  (1<<6)    /* Transmit size is random */
186 #define F_IPV6        (1<<7)    /* Interface in IPV6 Mode */
187 #define F_MPLS_RND    (1<<8)    /* Random MPLS labels */
188 #define F_VID_RND     (1<<9)    /* Random VLAN ID */
189 #define F_SVID_RND    (1<<10)   /* Random SVLAN ID */
190 #define F_FLOW_SEQ    (1<<11)   /* Sequential flows */
191 #define F_IPSEC_ON    (1<<12)   /* ipsec on for flows */
192 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
193
194 /* Thread control flag bits */
195 #define T_TERMINATE   (1<<0)
196 #define T_STOP        (1<<1)    /* Stop run */
197 #define T_RUN         (1<<2)    /* Start run */
198 #define T_REMDEVALL   (1<<3)    /* Remove all devs */
199 #define T_REMDEV      (1<<4)    /* Remove one dev */
200
201 /* If lock -- can be removed after some work */
202 #define   if_lock(t)           spin_lock(&(t->if_lock));
203 #define   if_unlock(t)           spin_unlock(&(t->if_lock));
204
205 /* Used to help with determining the pkts on receive */
206 #define PKTGEN_MAGIC 0xbe9be955
207 #define PG_PROC_DIR "pktgen"
208 #define PGCTRL      "pgctrl"
209 static struct proc_dir_entry *pg_proc_dir = NULL;
210
211 #define MAX_CFLOWS  65536
212
213 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
214 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
215
216 struct flow_state {
217         __be32 cur_daddr;
218         int count;
219 #ifdef CONFIG_XFRM
220         struct xfrm_state *x;
221 #endif
222         __u32 flags;
223 };
224
225 /* flow flag bits */
226 #define F_INIT   (1<<0)         /* flow has been initialized */
227
228 struct pktgen_dev {
229         /*
230          * Try to keep frequent/infrequent used vars. separated.
231          */
232         struct proc_dir_entry *entry;   /* proc file */
233         struct pktgen_thread *pg_thread;/* the owner */
234         struct list_head list;          /* Used for chaining in the thread's run-queue */
235
236         int running;            /* if this changes to false, the test will stop */
237
238         /* If min != max, then we will either do a linear iteration, or
239          * we will do a random selection from within the range.
240          */
241         __u32 flags;
242         int removal_mark;       /* non-zero => the device is marked for
243                                  * removal by worker thread */
244
245         int min_pkt_size;       /* = ETH_ZLEN; */
246         int max_pkt_size;       /* = ETH_ZLEN; */
247         int pkt_overhead;       /* overhead for MPLS, VLANs, IPSEC etc */
248         int nfrags;
249         __u32 delay_us;         /* Default delay */
250         __u32 delay_ns;
251         __u64 count;            /* Default No packets to send */
252         __u64 sofar;            /* How many pkts we've sent so far */
253         __u64 tx_bytes;         /* How many bytes we've transmitted */
254         __u64 errors;           /* Errors when trying to transmit, pkts will be re-sent */
255
256         /* runtime counters relating to clone_skb */
257         __u64 next_tx_us;       /* timestamp of when to tx next */
258         __u32 next_tx_ns;
259
260         __u64 allocated_skbs;
261         __u32 clone_count;
262         int last_ok;            /* Was last skb sent?
263                                  * Or a failed transmit of some sort?  This will keep
264                                  * sequence numbers in order, for example.
265                                  */
266         __u64 started_at;       /* micro-seconds */
267         __u64 stopped_at;       /* micro-seconds */
268         __u64 idle_acc;         /* micro-seconds */
269         __u32 seq_num;
270
271         int clone_skb;          /* Use multiple SKBs during packet gen.  If this number
272                                  * is greater than 1, then that many copies of the same
273                                  * packet will be sent before a new packet is allocated.
274                                  * For instance, if you want to send 1024 identical packets
275                                  * before creating a new packet, set clone_skb to 1024.
276                                  */
277
278         char dst_min[IP_NAME_SZ];       /* IP, ie 1.2.3.4 */
279         char dst_max[IP_NAME_SZ];       /* IP, ie 1.2.3.4 */
280         char src_min[IP_NAME_SZ];       /* IP, ie 1.2.3.4 */
281         char src_max[IP_NAME_SZ];       /* IP, ie 1.2.3.4 */
282
283         struct in6_addr in6_saddr;
284         struct in6_addr in6_daddr;
285         struct in6_addr cur_in6_daddr;
286         struct in6_addr cur_in6_saddr;
287         /* For ranges */
288         struct in6_addr min_in6_daddr;
289         struct in6_addr max_in6_daddr;
290         struct in6_addr min_in6_saddr;
291         struct in6_addr max_in6_saddr;
292
293         /* If we're doing ranges, random or incremental, then this
294          * defines the min/max for those ranges.
295          */
296         __be32 saddr_min;       /* inclusive, source IP address */
297         __be32 saddr_max;       /* exclusive, source IP address */
298         __be32 daddr_min;       /* inclusive, dest IP address */
299         __be32 daddr_max;       /* exclusive, dest IP address */
300
301         __u16 udp_src_min;      /* inclusive, source UDP port */
302         __u16 udp_src_max;      /* exclusive, source UDP port */
303         __u16 udp_dst_min;      /* inclusive, dest UDP port */
304         __u16 udp_dst_max;      /* exclusive, dest UDP port */
305
306         /* DSCP + ECN */
307         __u8 tos;            /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
308         __u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
309
310         /* MPLS */
311         unsigned nr_labels;     /* Depth of stack, 0 = no MPLS */
312         __be32 labels[MAX_MPLS_LABELS];
313
314         /* VLAN/SVLAN (802.1Q/Q-in-Q) */
315         __u8  vlan_p;
316         __u8  vlan_cfi;
317         __u16 vlan_id;  /* 0xffff means no vlan tag */
318
319         __u8  svlan_p;
320         __u8  svlan_cfi;
321         __u16 svlan_id; /* 0xffff means no svlan tag */
322
323         __u32 src_mac_count;    /* How many MACs to iterate through */
324         __u32 dst_mac_count;    /* How many MACs to iterate through */
325
326         unsigned char dst_mac[ETH_ALEN];
327         unsigned char src_mac[ETH_ALEN];
328
329         __u32 cur_dst_mac_offset;
330         __u32 cur_src_mac_offset;
331         __be32 cur_saddr;
332         __be32 cur_daddr;
333         __u16 cur_udp_dst;
334         __u16 cur_udp_src;
335         __u16 cur_queue_map;
336         __u32 cur_pkt_size;
337
338         __u8 hh[14];
339         /* = {
340            0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
341
342            We fill in SRC address later
343            0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
344            0x08, 0x00
345            };
346          */
347         __u16 pad;              /* pad out the hh struct to an even 16 bytes */
348
349         struct sk_buff *skb;    /* skb we are to transmit next, mainly used for when we
350                                  * are transmitting the same one multiple times
351                                  */
352         struct net_device *odev;        /* The out-going device.  Note that the device should
353                                          * have it's pg_info pointer pointing back to this
354                                          * device.  This will be set when the user specifies
355                                          * the out-going device name (not when the inject is
356                                          * started as it used to do.)
357                                          */
358         struct flow_state *flows;
359         unsigned cflows;        /* Concurrent flows (config) */
360         unsigned lflow;         /* Flow length  (config) */
361         unsigned nflows;        /* accumulated flows (stats) */
362         unsigned curfl;         /* current sequenced flow (state)*/
363
364         u16 queue_map_min;
365         u16 queue_map_max;
366
367 #ifdef CONFIG_XFRM
368         __u8    ipsmode;                /* IPSEC mode (config) */
369         __u8    ipsproto;               /* IPSEC type (config) */
370 #endif
371         char result[512];
372 };
373
374 struct pktgen_hdr {
375         __be32 pgh_magic;
376         __be32 seq_num;
377         __be32 tv_sec;
378         __be32 tv_usec;
379 };
380
381 struct pktgen_thread {
382         spinlock_t if_lock;
383         struct list_head if_list;       /* All device here */
384         struct list_head th_list;
385         struct task_struct *tsk;
386         char result[512];
387         u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
388
389         /* Field for thread to receive "posted" events terminate, stop ifs etc. */
390
391         u32 control;
392         int cpu;
393
394         wait_queue_head_t queue;
395 };
396
397 #define REMOVE 1
398 #define FIND   0
399
400 /*  This code works around the fact that do_div cannot handle two 64-bit
401     numbers, and regular 64-bit division doesn't work on x86 kernels.
402     --Ben
403 */
404
405 #define PG_DIV 0
406
407 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
408  * Function copied/adapted/optimized from:
409  *
410  *  nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
411  *
412  * Copyright 1994, University of Cambridge Computer Laboratory
413  * All Rights Reserved.
414  *
415  */
416 static inline s64 divremdi3(s64 x, s64 y, int type)
417 {
418         u64 a = (x < 0) ? -x : x;
419         u64 b = (y < 0) ? -y : y;
420         u64 res = 0, d = 1;
421
422         if (b > 0) {
423                 while (b < a) {
424                         b <<= 1;
425                         d <<= 1;
426                 }
427         }
428
429         do {
430                 if (a >= b) {
431                         a -= b;
432                         res += d;
433                 }
434                 b >>= 1;
435                 d >>= 1;
436         }
437         while (d);
438
439         if (PG_DIV == type) {
440                 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
441         } else {
442                 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
443         }
444 }
445
446 /* End of hacks to deal with 64-bit math on x86 */
447
448 /** Convert to milliseconds */
449 static inline __u64 tv_to_ms(const struct timeval *tv)
450 {
451         __u64 ms = tv->tv_usec / 1000;
452         ms += (__u64) tv->tv_sec * (__u64) 1000;
453         return ms;
454 }
455
456 /** Convert to micro-seconds */
457 static inline __u64 tv_to_us(const struct timeval *tv)
458 {
459         __u64 us = tv->tv_usec;
460         us += (__u64) tv->tv_sec * (__u64) 1000000;
461         return us;
462 }
463
464 static inline __u64 pg_div(__u64 n, __u32 base)
465 {
466         __u64 tmp = n;
467         do_div(tmp, base);
468         /* printk("pktgen: pg_div, n: %llu  base: %d  rv: %llu\n",
469            n, base, tmp); */
470         return tmp;
471 }
472
473 static inline __u64 pg_div64(__u64 n, __u64 base)
474 {
475         __u64 tmp = n;
476 /*
477  * How do we know if the architecture we are running on
478  * supports division with 64 bit base?
479  *
480  */
481 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
482
483         do_div(tmp, base);
484 #else
485         tmp = divremdi3(n, base, PG_DIV);
486 #endif
487         return tmp;
488 }
489
490 static inline __u64 getCurMs(void)
491 {
492         struct timeval tv;
493         do_gettimeofday(&tv);
494         return tv_to_ms(&tv);
495 }
496
497 static inline __u64 getCurUs(void)
498 {
499         struct timeval tv;
500         do_gettimeofday(&tv);
501         return tv_to_us(&tv);
502 }
503
504 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
505 {
506         return tv_to_us(a) - tv_to_us(b);
507 }
508
509 /* old include end */
510
511 static char version[] __initdata = VERSION;
512
513 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
514 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
515 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
516                                           const char *ifname);
517 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
518 static void pktgen_run_all_threads(void);
519 static void pktgen_stop_all_threads_ifs(void);
520 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
521 static void pktgen_stop(struct pktgen_thread *t);
522 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
523
524 static unsigned int scan_ip6(const char *s, char ip[16]);
525 static unsigned int fmt_ip6(char *s, const char ip[16]);
526
527 /* Module parameters, defaults. */
528 static int pg_count_d = 1000;   /* 1000 pkts by default */
529 static int pg_delay_d;
530 static int pg_clone_skb_d;
531 static int debug;
532
533 static DEFINE_MUTEX(pktgen_thread_lock);
534 static LIST_HEAD(pktgen_threads);
535
536 static struct notifier_block pktgen_notifier_block = {
537         .notifier_call = pktgen_device_event,
538 };
539
540 /*
541  * /proc handling functions
542  *
543  */
544
545 static int pgctrl_show(struct seq_file *seq, void *v)
546 {
547         seq_puts(seq, VERSION);
548         return 0;
549 }
550
551 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
552                             size_t count, loff_t * ppos)
553 {
554         int err = 0;
555         char data[128];
556
557         if (!capable(CAP_NET_ADMIN)) {
558                 err = -EPERM;
559                 goto out;
560         }
561
562         if (count > sizeof(data))
563                 count = sizeof(data);
564
565         if (copy_from_user(data, buf, count)) {
566                 err = -EFAULT;
567                 goto out;
568         }
569         data[count - 1] = 0;    /* Make string */
570
571         if (!strcmp(data, "stop"))
572                 pktgen_stop_all_threads_ifs();
573
574         else if (!strcmp(data, "start"))
575                 pktgen_run_all_threads();
576
577         else
578                 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
579
580         err = count;
581
582 out:
583         return err;
584 }
585
586 static int pgctrl_open(struct inode *inode, struct file *file)
587 {
588         return single_open(file, pgctrl_show, PDE(inode)->data);
589 }
590
591 static const struct file_operations pktgen_fops = {
592         .owner   = THIS_MODULE,
593         .open    = pgctrl_open,
594         .read    = seq_read,
595         .llseek  = seq_lseek,
596         .write   = pgctrl_write,
597         .release = single_release,
598 };
599
600 static int pktgen_if_show(struct seq_file *seq, void *v)
601 {
602         int i;
603         struct pktgen_dev *pkt_dev = seq->private;
604         __u64 sa;
605         __u64 stopped;
606         __u64 now = getCurUs();
607
608         seq_printf(seq,
609                    "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
610                    (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
611                    pkt_dev->max_pkt_size);
612
613         seq_printf(seq,
614                    "     frags: %d  delay: %u  clone_skb: %d  ifname: %s\n",
615                    pkt_dev->nfrags,
616                    1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
617                    pkt_dev->clone_skb, pkt_dev->odev->name);
618
619         seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
620                    pkt_dev->lflow);
621
622         seq_printf(seq,
623                    "     queue_map_min: %u  queue_map_max: %u\n",
624                    pkt_dev->queue_map_min,
625                    pkt_dev->queue_map_max);
626
627         if (pkt_dev->flags & F_IPV6) {
628                 char b1[128], b2[128], b3[128];
629                 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
630                 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
631                 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
632                 seq_printf(seq,
633                            "     saddr: %s  min_saddr: %s  max_saddr: %s\n", b1,
634                            b2, b3);
635
636                 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
637                 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
638                 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
639                 seq_printf(seq,
640                            "     daddr: %s  min_daddr: %s  max_daddr: %s\n", b1,
641                            b2, b3);
642
643         } else
644                 seq_printf(seq,
645                            "     dst_min: %s  dst_max: %s\n     src_min: %s  src_max: %s\n",
646                            pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
647                            pkt_dev->src_max);
648
649         seq_puts(seq, "     src_mac: ");
650
651         if (is_zero_ether_addr(pkt_dev->src_mac))
652                 for (i = 0; i < 6; i++)
653                         seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
654                                    i == 5 ? "  " : ":");
655         else
656                 for (i = 0; i < 6; i++)
657                         seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
658                                    i == 5 ? "  " : ":");
659
660         seq_printf(seq, "dst_mac: ");
661         for (i = 0; i < 6; i++)
662                 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
663                            i == 5 ? "\n" : ":");
664
665         seq_printf(seq,
666                    "     udp_src_min: %d  udp_src_max: %d  udp_dst_min: %d  udp_dst_max: %d\n",
667                    pkt_dev->udp_src_min, pkt_dev->udp_src_max,
668                    pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
669
670         seq_printf(seq,
671                    "     src_mac_count: %d  dst_mac_count: %d\n",
672                    pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
673
674         if (pkt_dev->nr_labels) {
675                 unsigned i;
676                 seq_printf(seq, "     mpls: ");
677                 for (i = 0; i < pkt_dev->nr_labels; i++)
678                         seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
679                                    i == pkt_dev->nr_labels-1 ? "\n" : ", ");
680         }
681
682         if (pkt_dev->vlan_id != 0xffff) {
683                 seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
684                            pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
685         }
686
687         if (pkt_dev->svlan_id != 0xffff) {
688                 seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
689                            pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
690         }
691
692         if (pkt_dev->tos) {
693                 seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
694         }
695
696         if (pkt_dev->traffic_class) {
697                 seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
698         }
699
700         seq_printf(seq, "     Flags: ");
701
702         if (pkt_dev->flags & F_IPV6)
703                 seq_printf(seq, "IPV6  ");
704
705         if (pkt_dev->flags & F_IPSRC_RND)
706                 seq_printf(seq, "IPSRC_RND  ");
707
708         if (pkt_dev->flags & F_IPDST_RND)
709                 seq_printf(seq, "IPDST_RND  ");
710
711         if (pkt_dev->flags & F_TXSIZE_RND)
712                 seq_printf(seq, "TXSIZE_RND  ");
713
714         if (pkt_dev->flags & F_UDPSRC_RND)
715                 seq_printf(seq, "UDPSRC_RND  ");
716
717         if (pkt_dev->flags & F_UDPDST_RND)
718                 seq_printf(seq, "UDPDST_RND  ");
719
720         if (pkt_dev->flags & F_MPLS_RND)
721                 seq_printf(seq,  "MPLS_RND  ");
722
723         if (pkt_dev->flags & F_QUEUE_MAP_RND)
724                 seq_printf(seq,  "QUEUE_MAP_RND  ");
725
726         if (pkt_dev->cflows) {
727                 if (pkt_dev->flags & F_FLOW_SEQ)
728                         seq_printf(seq,  "FLOW_SEQ  "); /*in sequence flows*/
729                 else
730                         seq_printf(seq,  "FLOW_RND  ");
731         }
732
733 #ifdef CONFIG_XFRM
734         if (pkt_dev->flags & F_IPSEC_ON)
735                 seq_printf(seq,  "IPSEC  ");
736 #endif
737
738         if (pkt_dev->flags & F_MACSRC_RND)
739                 seq_printf(seq, "MACSRC_RND  ");
740
741         if (pkt_dev->flags & F_MACDST_RND)
742                 seq_printf(seq, "MACDST_RND  ");
743
744         if (pkt_dev->flags & F_VID_RND)
745                 seq_printf(seq, "VID_RND  ");
746
747         if (pkt_dev->flags & F_SVID_RND)
748                 seq_printf(seq, "SVID_RND  ");
749
750         seq_puts(seq, "\n");
751
752         sa = pkt_dev->started_at;
753         stopped = pkt_dev->stopped_at;
754         if (pkt_dev->running)
755                 stopped = now;  /* not really stopped, more like last-running-at */
756
757         seq_printf(seq,
758                    "Current:\n     pkts-sofar: %llu  errors: %llu\n     started: %lluus  stopped: %lluus idle: %lluus\n",
759                    (unsigned long long)pkt_dev->sofar,
760                    (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
761                    (unsigned long long)stopped,
762                    (unsigned long long)pkt_dev->idle_acc);
763
764         seq_printf(seq,
765                    "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
766                    pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
767                    pkt_dev->cur_src_mac_offset);
768
769         if (pkt_dev->flags & F_IPV6) {
770                 char b1[128], b2[128];
771                 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
772                 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
773                 seq_printf(seq, "     cur_saddr: %s  cur_daddr: %s\n", b2, b1);
774         } else
775                 seq_printf(seq, "     cur_saddr: 0x%x  cur_daddr: 0x%x\n",
776                            pkt_dev->cur_saddr, pkt_dev->cur_daddr);
777
778         seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
779                    pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
780
781         seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
782
783         seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
784
785         if (pkt_dev->result[0])
786                 seq_printf(seq, "Result: %s\n", pkt_dev->result);
787         else
788                 seq_printf(seq, "Result: Idle\n");
789
790         return 0;
791 }
792
793
794 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
795 {
796         int i = 0;
797         *num = 0;
798
799         for (; i < maxlen; i++) {
800                 char c;
801                 *num <<= 4;
802                 if (get_user(c, &user_buffer[i]))
803                         return -EFAULT;
804                 if ((c >= '0') && (c <= '9'))
805                         *num |= c - '0';
806                 else if ((c >= 'a') && (c <= 'f'))
807                         *num |= c - 'a' + 10;
808                 else if ((c >= 'A') && (c <= 'F'))
809                         *num |= c - 'A' + 10;
810                 else
811                         break;
812         }
813         return i;
814 }
815
816 static int count_trail_chars(const char __user * user_buffer,
817                              unsigned int maxlen)
818 {
819         int i;
820
821         for (i = 0; i < maxlen; i++) {
822                 char c;
823                 if (get_user(c, &user_buffer[i]))
824                         return -EFAULT;
825                 switch (c) {
826                 case '\"':
827                 case '\n':
828                 case '\r':
829                 case '\t':
830                 case ' ':
831                 case '=':
832                         break;
833                 default:
834                         goto done;
835                 }
836         }
837 done:
838         return i;
839 }
840
841 static unsigned long num_arg(const char __user * user_buffer,
842                              unsigned long maxlen, unsigned long *num)
843 {
844         int i = 0;
845         *num = 0;
846
847         for (; i < maxlen; i++) {
848                 char c;
849                 if (get_user(c, &user_buffer[i]))
850                         return -EFAULT;
851                 if ((c >= '0') && (c <= '9')) {
852                         *num *= 10;
853                         *num += c - '0';
854                 } else
855                         break;
856         }
857         return i;
858 }
859
860 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
861 {
862         int i = 0;
863
864         for (; i < maxlen; i++) {
865                 char c;
866                 if (get_user(c, &user_buffer[i]))
867                         return -EFAULT;
868                 switch (c) {
869                 case '\"':
870                 case '\n':
871                 case '\r':
872                 case '\t':
873                 case ' ':
874                         goto done_str;
875                         break;
876                 default:
877                         break;
878                 }
879         }
880 done_str:
881         return i;
882 }
883
884 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
885 {
886         unsigned n = 0;
887         char c;
888         ssize_t i = 0;
889         int len;
890
891         pkt_dev->nr_labels = 0;
892         do {
893                 __u32 tmp;
894                 len = hex32_arg(&buffer[i], 8, &tmp);
895                 if (len <= 0)
896                         return len;
897                 pkt_dev->labels[n] = htonl(tmp);
898                 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
899                         pkt_dev->flags |= F_MPLS_RND;
900                 i += len;
901                 if (get_user(c, &buffer[i]))
902                         return -EFAULT;
903                 i++;
904                 n++;
905                 if (n >= MAX_MPLS_LABELS)
906                         return -E2BIG;
907         } while (c == ',');
908
909         pkt_dev->nr_labels = n;
910         return i;
911 }
912
913 static ssize_t pktgen_if_write(struct file *file,
914                                const char __user * user_buffer, size_t count,
915                                loff_t * offset)
916 {
917         struct seq_file *seq = (struct seq_file *)file->private_data;
918         struct pktgen_dev *pkt_dev = seq->private;
919         int i = 0, max, len;
920         char name[16], valstr[32];
921         unsigned long value = 0;
922         char *pg_result = NULL;
923         int tmp = 0;
924         char buf[128];
925
926         pg_result = &(pkt_dev->result[0]);
927
928         if (count < 1) {
929                 printk(KERN_WARNING "pktgen: wrong command format\n");
930                 return -EINVAL;
931         }
932
933         max = count - i;
934         tmp = count_trail_chars(&user_buffer[i], max);
935         if (tmp < 0) {
936                 printk(KERN_WARNING "pktgen: illegal format\n");
937                 return tmp;
938         }
939         i += tmp;
940
941         /* Read variable name */
942
943         len = strn_len(&user_buffer[i], sizeof(name) - 1);
944         if (len < 0) {
945                 return len;
946         }
947         memset(name, 0, sizeof(name));
948         if (copy_from_user(name, &user_buffer[i], len))
949                 return -EFAULT;
950         i += len;
951
952         max = count - i;
953         len = count_trail_chars(&user_buffer[i], max);
954         if (len < 0)
955                 return len;
956
957         i += len;
958
959         if (debug) {
960                 char tb[count + 1];
961                 if (copy_from_user(tb, user_buffer, count))
962                         return -EFAULT;
963                 tb[count] = 0;
964                 printk(KERN_DEBUG "pktgen: %s,%lu  buffer -:%s:-\n", name,
965                        (unsigned long)count, tb);
966         }
967
968         if (!strcmp(name, "min_pkt_size")) {
969                 len = num_arg(&user_buffer[i], 10, &value);
970                 if (len < 0) {
971                         return len;
972                 }
973                 i += len;
974                 if (value < 14 + 20 + 8)
975                         value = 14 + 20 + 8;
976                 if (value != pkt_dev->min_pkt_size) {
977                         pkt_dev->min_pkt_size = value;
978                         pkt_dev->cur_pkt_size = value;
979                 }
980                 sprintf(pg_result, "OK: min_pkt_size=%u",
981                         pkt_dev->min_pkt_size);
982                 return count;
983         }
984
985         if (!strcmp(name, "max_pkt_size")) {
986                 len = num_arg(&user_buffer[i], 10, &value);
987                 if (len < 0) {
988                         return len;
989                 }
990                 i += len;
991                 if (value < 14 + 20 + 8)
992                         value = 14 + 20 + 8;
993                 if (value != pkt_dev->max_pkt_size) {
994                         pkt_dev->max_pkt_size = value;
995                         pkt_dev->cur_pkt_size = value;
996                 }
997                 sprintf(pg_result, "OK: max_pkt_size=%u",
998                         pkt_dev->max_pkt_size);
999                 return count;
1000         }
1001
1002         /* Shortcut for min = max */
1003
1004         if (!strcmp(name, "pkt_size")) {
1005                 len = num_arg(&user_buffer[i], 10, &value);
1006                 if (len < 0) {
1007                         return len;
1008                 }
1009                 i += len;
1010                 if (value < 14 + 20 + 8)
1011                         value = 14 + 20 + 8;
1012                 if (value != pkt_dev->min_pkt_size) {
1013                         pkt_dev->min_pkt_size = value;
1014                         pkt_dev->max_pkt_size = value;
1015                         pkt_dev->cur_pkt_size = value;
1016                 }
1017                 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
1018                 return count;
1019         }
1020
1021         if (!strcmp(name, "debug")) {
1022                 len = num_arg(&user_buffer[i], 10, &value);
1023                 if (len < 0) {
1024                         return len;
1025                 }
1026                 i += len;
1027                 debug = value;
1028                 sprintf(pg_result, "OK: debug=%u", debug);
1029                 return count;
1030         }
1031
1032         if (!strcmp(name, "frags")) {
1033                 len = num_arg(&user_buffer[i], 10, &value);
1034                 if (len < 0) {
1035                         return len;
1036                 }
1037                 i += len;
1038                 pkt_dev->nfrags = value;
1039                 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1040                 return count;
1041         }
1042         if (!strcmp(name, "delay")) {
1043                 len = num_arg(&user_buffer[i], 10, &value);
1044                 if (len < 0) {
1045                         return len;
1046                 }
1047                 i += len;
1048                 if (value == 0x7FFFFFFF) {
1049                         pkt_dev->delay_us = 0x7FFFFFFF;
1050                         pkt_dev->delay_ns = 0;
1051                 } else {
1052                         pkt_dev->delay_us = value / 1000;
1053                         pkt_dev->delay_ns = value % 1000;
1054                 }
1055                 sprintf(pg_result, "OK: delay=%u",
1056                         1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1057                 return count;
1058         }
1059         if (!strcmp(name, "udp_src_min")) {
1060                 len = num_arg(&user_buffer[i], 10, &value);
1061                 if (len < 0) {
1062                         return len;
1063                 }
1064                 i += len;
1065                 if (value != pkt_dev->udp_src_min) {
1066                         pkt_dev->udp_src_min = value;
1067                         pkt_dev->cur_udp_src = value;
1068                 }
1069                 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1070                 return count;
1071         }
1072         if (!strcmp(name, "udp_dst_min")) {
1073                 len = num_arg(&user_buffer[i], 10, &value);
1074                 if (len < 0) {
1075                         return len;
1076                 }
1077                 i += len;
1078                 if (value != pkt_dev->udp_dst_min) {
1079                         pkt_dev->udp_dst_min = value;
1080                         pkt_dev->cur_udp_dst = value;
1081                 }
1082                 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1083                 return count;
1084         }
1085         if (!strcmp(name, "udp_src_max")) {
1086                 len = num_arg(&user_buffer[i], 10, &value);
1087                 if (len < 0) {
1088                         return len;
1089                 }
1090                 i += len;
1091                 if (value != pkt_dev->udp_src_max) {
1092                         pkt_dev->udp_src_max = value;
1093                         pkt_dev->cur_udp_src = value;
1094                 }
1095                 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1096                 return count;
1097         }
1098         if (!strcmp(name, "udp_dst_max")) {
1099                 len = num_arg(&user_buffer[i], 10, &value);
1100                 if (len < 0) {
1101                         return len;
1102                 }
1103                 i += len;
1104                 if (value != pkt_dev->udp_dst_max) {
1105                         pkt_dev->udp_dst_max = value;
1106                         pkt_dev->cur_udp_dst = value;
1107                 }
1108                 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1109                 return count;
1110         }
1111         if (!strcmp(name, "clone_skb")) {
1112                 len = num_arg(&user_buffer[i], 10, &value);
1113                 if (len < 0) {
1114                         return len;
1115                 }
1116                 i += len;
1117                 pkt_dev->clone_skb = value;
1118
1119                 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1120                 return count;
1121         }
1122         if (!strcmp(name, "count")) {
1123                 len = num_arg(&user_buffer[i], 10, &value);
1124                 if (len < 0) {
1125                         return len;
1126                 }
1127                 i += len;
1128                 pkt_dev->count = value;
1129                 sprintf(pg_result, "OK: count=%llu",
1130                         (unsigned long long)pkt_dev->count);
1131                 return count;
1132         }
1133         if (!strcmp(name, "src_mac_count")) {
1134                 len = num_arg(&user_buffer[i], 10, &value);
1135                 if (len < 0) {
1136                         return len;
1137                 }
1138                 i += len;
1139                 if (pkt_dev->src_mac_count != value) {
1140                         pkt_dev->src_mac_count = value;
1141                         pkt_dev->cur_src_mac_offset = 0;
1142                 }
1143                 sprintf(pg_result, "OK: src_mac_count=%d",
1144                         pkt_dev->src_mac_count);
1145                 return count;
1146         }
1147         if (!strcmp(name, "dst_mac_count")) {
1148                 len = num_arg(&user_buffer[i], 10, &value);
1149                 if (len < 0) {
1150                         return len;
1151                 }
1152                 i += len;
1153                 if (pkt_dev->dst_mac_count != value) {
1154                         pkt_dev->dst_mac_count = value;
1155                         pkt_dev->cur_dst_mac_offset = 0;
1156                 }
1157                 sprintf(pg_result, "OK: dst_mac_count=%d",
1158                         pkt_dev->dst_mac_count);
1159                 return count;
1160         }
1161         if (!strcmp(name, "flag")) {
1162                 char f[32];
1163                 memset(f, 0, 32);
1164                 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1165                 if (len < 0) {
1166                         return len;
1167                 }
1168                 if (copy_from_user(f, &user_buffer[i], len))
1169                         return -EFAULT;
1170                 i += len;
1171                 if (strcmp(f, "IPSRC_RND") == 0)
1172                         pkt_dev->flags |= F_IPSRC_RND;
1173
1174                 else if (strcmp(f, "!IPSRC_RND") == 0)
1175                         pkt_dev->flags &= ~F_IPSRC_RND;
1176
1177                 else if (strcmp(f, "TXSIZE_RND") == 0)
1178                         pkt_dev->flags |= F_TXSIZE_RND;
1179
1180                 else if (strcmp(f, "!TXSIZE_RND") == 0)
1181                         pkt_dev->flags &= ~F_TXSIZE_RND;
1182
1183                 else if (strcmp(f, "IPDST_RND") == 0)
1184                         pkt_dev->flags |= F_IPDST_RND;
1185
1186                 else if (strcmp(f, "!IPDST_RND") == 0)
1187                         pkt_dev->flags &= ~F_IPDST_RND;
1188
1189                 else if (strcmp(f, "UDPSRC_RND") == 0)
1190                         pkt_dev->flags |= F_UDPSRC_RND;
1191
1192                 else if (strcmp(f, "!UDPSRC_RND") == 0)
1193                         pkt_dev->flags &= ~F_UDPSRC_RND;
1194
1195                 else if (strcmp(f, "UDPDST_RND") == 0)
1196                         pkt_dev->flags |= F_UDPDST_RND;
1197
1198                 else if (strcmp(f, "!UDPDST_RND") == 0)
1199                         pkt_dev->flags &= ~F_UDPDST_RND;
1200
1201                 else if (strcmp(f, "MACSRC_RND") == 0)
1202                         pkt_dev->flags |= F_MACSRC_RND;
1203
1204                 else if (strcmp(f, "!MACSRC_RND") == 0)
1205                         pkt_dev->flags &= ~F_MACSRC_RND;
1206
1207                 else if (strcmp(f, "MACDST_RND") == 0)
1208                         pkt_dev->flags |= F_MACDST_RND;
1209
1210                 else if (strcmp(f, "!MACDST_RND") == 0)
1211                         pkt_dev->flags &= ~F_MACDST_RND;
1212
1213                 else if (strcmp(f, "MPLS_RND") == 0)
1214                         pkt_dev->flags |= F_MPLS_RND;
1215
1216                 else if (strcmp(f, "!MPLS_RND") == 0)
1217                         pkt_dev->flags &= ~F_MPLS_RND;
1218
1219                 else if (strcmp(f, "VID_RND") == 0)
1220                         pkt_dev->flags |= F_VID_RND;
1221
1222                 else if (strcmp(f, "!VID_RND") == 0)
1223                         pkt_dev->flags &= ~F_VID_RND;
1224
1225                 else if (strcmp(f, "SVID_RND") == 0)
1226                         pkt_dev->flags |= F_SVID_RND;
1227
1228                 else if (strcmp(f, "!SVID_RND") == 0)
1229                         pkt_dev->flags &= ~F_SVID_RND;
1230
1231                 else if (strcmp(f, "FLOW_SEQ") == 0)
1232                         pkt_dev->flags |= F_FLOW_SEQ;
1233
1234                 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1235                         pkt_dev->flags |= F_QUEUE_MAP_RND;
1236
1237                 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1238                         pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1239 #ifdef CONFIG_XFRM
1240                 else if (strcmp(f, "IPSEC") == 0)
1241                         pkt_dev->flags |= F_IPSEC_ON;
1242 #endif
1243
1244                 else if (strcmp(f, "!IPV6") == 0)
1245                         pkt_dev->flags &= ~F_IPV6;
1246
1247                 else {
1248                         sprintf(pg_result,
1249                                 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1250                                 f,
1251                                 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1252                                 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1253                         return count;
1254                 }
1255                 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1256                 return count;
1257         }
1258         if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1259                 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1260                 if (len < 0) {
1261                         return len;
1262                 }
1263
1264                 if (copy_from_user(buf, &user_buffer[i], len))
1265                         return -EFAULT;
1266                 buf[len] = 0;
1267                 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1268                         memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1269                         strncpy(pkt_dev->dst_min, buf, len);
1270                         pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1271                         pkt_dev->cur_daddr = pkt_dev->daddr_min;
1272                 }
1273                 if (debug)
1274                         printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1275                                pkt_dev->dst_min);
1276                 i += len;
1277                 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1278                 return count;
1279         }
1280         if (!strcmp(name, "dst_max")) {
1281                 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1282                 if (len < 0) {
1283                         return len;
1284                 }
1285
1286                 if (copy_from_user(buf, &user_buffer[i], len))
1287                         return -EFAULT;
1288
1289                 buf[len] = 0;
1290                 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1291                         memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1292                         strncpy(pkt_dev->dst_max, buf, len);
1293                         pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1294                         pkt_dev->cur_daddr = pkt_dev->daddr_max;
1295                 }
1296                 if (debug)
1297                         printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1298                                pkt_dev->dst_max);
1299                 i += len;
1300                 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1301                 return count;
1302         }
1303         if (!strcmp(name, "dst6")) {
1304                 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1305                 if (len < 0)
1306                         return len;
1307
1308                 pkt_dev->flags |= F_IPV6;
1309
1310                 if (copy_from_user(buf, &user_buffer[i], len))
1311                         return -EFAULT;
1312                 buf[len] = 0;
1313
1314                 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1315                 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1316
1317                 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1318
1319                 if (debug)
1320                         printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1321
1322                 i += len;
1323                 sprintf(pg_result, "OK: dst6=%s", buf);
1324                 return count;
1325         }
1326         if (!strcmp(name, "dst6_min")) {
1327                 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1328                 if (len < 0)
1329                         return len;
1330
1331                 pkt_dev->flags |= F_IPV6;
1332
1333                 if (copy_from_user(buf, &user_buffer[i], len))
1334                         return -EFAULT;
1335                 buf[len] = 0;
1336
1337                 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1338                 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1339
1340                 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1341                                &pkt_dev->min_in6_daddr);
1342                 if (debug)
1343                         printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1344
1345                 i += len;
1346                 sprintf(pg_result, "OK: dst6_min=%s", buf);
1347                 return count;
1348         }
1349         if (!strcmp(name, "dst6_max")) {
1350                 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1351                 if (len < 0)
1352                         return len;
1353
1354                 pkt_dev->flags |= F_IPV6;
1355
1356                 if (copy_from_user(buf, &user_buffer[i], len))
1357                         return -EFAULT;
1358                 buf[len] = 0;
1359
1360                 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1361                 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1362
1363                 if (debug)
1364                         printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1365
1366                 i += len;
1367                 sprintf(pg_result, "OK: dst6_max=%s", buf);
1368                 return count;
1369         }
1370         if (!strcmp(name, "src6")) {
1371                 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1372                 if (len < 0)
1373                         return len;
1374
1375                 pkt_dev->flags |= F_IPV6;
1376
1377                 if (copy_from_user(buf, &user_buffer[i], len))
1378                         return -EFAULT;
1379                 buf[len] = 0;
1380
1381                 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1382                 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1383
1384                 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1385
1386                 if (debug)
1387                         printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1388
1389                 i += len;
1390                 sprintf(pg_result, "OK: src6=%s", buf);
1391                 return count;
1392         }
1393         if (!strcmp(name, "src_min")) {
1394                 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1395                 if (len < 0) {
1396                         return len;
1397                 }
1398                 if (copy_from_user(buf, &user_buffer[i], len))
1399                         return -EFAULT;
1400                 buf[len] = 0;
1401                 if (strcmp(buf, pkt_dev->src_min) != 0) {
1402                         memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1403                         strncpy(pkt_dev->src_min, buf, len);
1404                         pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1405                         pkt_dev->cur_saddr = pkt_dev->saddr_min;
1406                 }
1407                 if (debug)
1408                         printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1409                                pkt_dev->src_min);
1410                 i += len;
1411                 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1412                 return count;
1413         }
1414         if (!strcmp(name, "src_max")) {
1415                 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1416                 if (len < 0) {
1417                         return len;
1418                 }
1419                 if (copy_from_user(buf, &user_buffer[i], len))
1420                         return -EFAULT;
1421                 buf[len] = 0;
1422                 if (strcmp(buf, pkt_dev->src_max) != 0) {
1423                         memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1424                         strncpy(pkt_dev->src_max, buf, len);
1425                         pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1426                         pkt_dev->cur_saddr = pkt_dev->saddr_max;
1427                 }
1428                 if (debug)
1429                         printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1430                                pkt_dev->src_max);
1431                 i += len;
1432                 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1433                 return count;
1434         }
1435         if (!strcmp(name, "dst_mac")) {
1436                 char *v = valstr;
1437                 unsigned char old_dmac[ETH_ALEN];
1438                 unsigned char *m = pkt_dev->dst_mac;
1439                 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1440
1441                 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1442                 if (len < 0) {
1443                         return len;
1444                 }
1445                 memset(valstr, 0, sizeof(valstr));
1446                 if (copy_from_user(valstr, &user_buffer[i], len))
1447                         return -EFAULT;
1448                 i += len;
1449
1450                 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1451                         if (*v >= '0' && *v <= '9') {
1452                                 *m *= 16;
1453                                 *m += *v - '0';
1454                         }
1455                         if (*v >= 'A' && *v <= 'F') {
1456                                 *m *= 16;
1457                                 *m += *v - 'A' + 10;
1458                         }
1459                         if (*v >= 'a' && *v <= 'f') {
1460                                 *m *= 16;
1461                                 *m += *v - 'a' + 10;
1462                         }
1463                         if (*v == ':') {
1464                                 m++;
1465                                 *m = 0;
1466                         }
1467                 }
1468
1469                 /* Set up Dest MAC */
1470                 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1471                         memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1472
1473                 sprintf(pg_result, "OK: dstmac");
1474                 return count;
1475         }
1476         if (!strcmp(name, "src_mac")) {
1477                 char *v = valstr;
1478                 unsigned char old_smac[ETH_ALEN];
1479                 unsigned char *m = pkt_dev->src_mac;
1480
1481                 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1482
1483                 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1484                 if (len < 0) {
1485                         return len;
1486                 }
1487                 memset(valstr, 0, sizeof(valstr));
1488                 if (copy_from_user(valstr, &user_buffer[i], len))
1489                         return -EFAULT;
1490                 i += len;
1491
1492                 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1493                         if (*v >= '0' && *v <= '9') {
1494                                 *m *= 16;
1495                                 *m += *v - '0';
1496                         }
1497                         if (*v >= 'A' && *v <= 'F') {
1498                                 *m *= 16;
1499                                 *m += *v - 'A' + 10;
1500                         }
1501                         if (*v >= 'a' && *v <= 'f') {
1502                                 *m *= 16;
1503                                 *m += *v - 'a' + 10;
1504                         }
1505                         if (*v == ':') {
1506                                 m++;
1507                                 *m = 0;
1508                         }
1509                 }
1510
1511                 /* Set up Src MAC */
1512                 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1513                         memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1514
1515                 sprintf(pg_result, "OK: srcmac");
1516                 return count;
1517         }
1518
1519         if (!strcmp(name, "clear_counters")) {
1520                 pktgen_clear_counters(pkt_dev);
1521                 sprintf(pg_result, "OK: Clearing counters.\n");
1522                 return count;
1523         }
1524
1525         if (!strcmp(name, "flows")) {
1526                 len = num_arg(&user_buffer[i], 10, &value);
1527                 if (len < 0) {
1528                         return len;
1529                 }
1530                 i += len;
1531                 if (value > MAX_CFLOWS)
1532                         value = MAX_CFLOWS;
1533
1534                 pkt_dev->cflows = value;
1535                 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1536                 return count;
1537         }
1538
1539         if (!strcmp(name, "flowlen")) {
1540                 len = num_arg(&user_buffer[i], 10, &value);
1541                 if (len < 0) {
1542                         return len;
1543                 }
1544                 i += len;
1545                 pkt_dev->lflow = value;
1546                 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1547                 return count;
1548         }
1549
1550         if (!strcmp(name, "queue_map_min")) {
1551                 len = num_arg(&user_buffer[i], 5, &value);
1552                 if (len < 0) {
1553                         return len;
1554                 }
1555                 i += len;
1556                 pkt_dev->queue_map_min = value;
1557                 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1558                 return count;
1559         }
1560
1561         if (!strcmp(name, "queue_map_max")) {
1562                 len = num_arg(&user_buffer[i], 5, &value);
1563                 if (len < 0) {
1564                         return len;
1565                 }
1566                 i += len;
1567                 pkt_dev->queue_map_max = value;
1568                 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1569                 return count;
1570         }
1571
1572         if (!strcmp(name, "mpls")) {
1573                 unsigned n, offset;
1574                 len = get_labels(&user_buffer[i], pkt_dev);
1575                 if (len < 0) { return len; }
1576                 i += len;
1577                 offset = sprintf(pg_result, "OK: mpls=");
1578                 for (n = 0; n < pkt_dev->nr_labels; n++)
1579                         offset += sprintf(pg_result + offset,
1580                                           "%08x%s", ntohl(pkt_dev->labels[n]),
1581                                           n == pkt_dev->nr_labels-1 ? "" : ",");
1582
1583                 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1584                         pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1585                         pkt_dev->svlan_id = 0xffff;
1586
1587                         if (debug)
1588                                 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1589                 }
1590                 return count;
1591         }
1592
1593         if (!strcmp(name, "vlan_id")) {
1594                 len = num_arg(&user_buffer[i], 4, &value);
1595                 if (len < 0) {
1596                         return len;
1597                 }
1598                 i += len;
1599                 if (value <= 4095) {
1600                         pkt_dev->vlan_id = value;  /* turn on VLAN */
1601
1602                         if (debug)
1603                                 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1604
1605                         if (debug && pkt_dev->nr_labels)
1606                                 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1607
1608                         pkt_dev->nr_labels = 0;    /* turn off MPLS */
1609                         sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1610                 } else {
1611                         pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1612                         pkt_dev->svlan_id = 0xffff;
1613
1614                         if (debug)
1615                                 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1616                 }
1617                 return count;
1618         }
1619
1620         if (!strcmp(name, "vlan_p")) {
1621                 len = num_arg(&user_buffer[i], 1, &value);
1622                 if (len < 0) {
1623                         return len;
1624                 }
1625                 i += len;
1626                 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1627                         pkt_dev->vlan_p = value;
1628                         sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1629                 } else {
1630                         sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1631                 }
1632                 return count;
1633         }
1634
1635         if (!strcmp(name, "vlan_cfi")) {
1636                 len = num_arg(&user_buffer[i], 1, &value);
1637                 if (len < 0) {
1638                         return len;
1639                 }
1640                 i += len;
1641                 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1642                         pkt_dev->vlan_cfi = value;
1643                         sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1644                 } else {
1645                         sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1646                 }
1647                 return count;
1648         }
1649
1650         if (!strcmp(name, "svlan_id")) {
1651                 len = num_arg(&user_buffer[i], 4, &value);
1652                 if (len < 0) {
1653                         return len;
1654                 }
1655                 i += len;
1656                 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1657                         pkt_dev->svlan_id = value;  /* turn on SVLAN */
1658
1659                         if (debug)
1660                                 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1661
1662                         if (debug && pkt_dev->nr_labels)
1663                                 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1664
1665                         pkt_dev->nr_labels = 0;    /* turn off MPLS */
1666                         sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1667                 } else {
1668                         pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1669                         pkt_dev->svlan_id = 0xffff;
1670
1671                         if (debug)
1672                                 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1673                 }
1674                 return count;
1675         }
1676
1677         if (!strcmp(name, "svlan_p")) {
1678                 len = num_arg(&user_buffer[i], 1, &value);
1679                 if (len < 0) {
1680                         return len;
1681                 }
1682                 i += len;
1683                 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1684                         pkt_dev->svlan_p = value;
1685                         sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1686                 } else {
1687                         sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1688                 }
1689                 return count;
1690         }
1691
1692         if (!strcmp(name, "svlan_cfi")) {
1693                 len = num_arg(&user_buffer[i], 1, &value);
1694                 if (len < 0) {
1695                         return len;
1696                 }
1697                 i += len;
1698                 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1699                         pkt_dev->svlan_cfi = value;
1700                         sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1701                 } else {
1702                         sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1703                 }
1704                 return count;
1705         }
1706
1707         if (!strcmp(name, "tos")) {
1708                 __u32 tmp_value = 0;
1709                 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1710                 if (len < 0) {
1711                         return len;
1712                 }
1713                 i += len;
1714                 if (len == 2) {
1715                         pkt_dev->tos = tmp_value;
1716                         sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1717                 } else {
1718                         sprintf(pg_result, "ERROR: tos must be 00-ff");
1719                 }
1720                 return count;
1721         }
1722
1723         if (!strcmp(name, "traffic_class")) {
1724                 __u32 tmp_value = 0;
1725                 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1726                 if (len < 0) {
1727                         return len;
1728                 }
1729                 i += len;
1730                 if (len == 2) {
1731                         pkt_dev->traffic_class = tmp_value;
1732                         sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1733                 } else {
1734                         sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1735                 }
1736                 return count;
1737         }
1738
1739         sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1740         return -EINVAL;
1741 }
1742
1743 static int pktgen_if_open(struct inode *inode, struct file *file)
1744 {
1745         return single_open(file, pktgen_if_show, PDE(inode)->data);
1746 }
1747
1748 static const struct file_operations pktgen_if_fops = {
1749         .owner   = THIS_MODULE,
1750         .open    = pktgen_if_open,
1751         .read    = seq_read,
1752         .llseek  = seq_lseek,
1753         .write   = pktgen_if_write,
1754         .release = single_release,
1755 };
1756
1757 static int pktgen_thread_show(struct seq_file *seq, void *v)
1758 {
1759         struct pktgen_thread *t = seq->private;
1760         struct pktgen_dev *pkt_dev;
1761
1762         BUG_ON(!t);
1763
1764         seq_printf(seq, "Name: %s  max_before_softirq: %d\n",
1765                    t->tsk->comm, t->max_before_softirq);
1766
1767         seq_printf(seq, "Running: ");
1768
1769         if_lock(t);
1770         list_for_each_entry(pkt_dev, &t->if_list, list)
1771                 if (pkt_dev->running)
1772                         seq_printf(seq, "%s ", pkt_dev->odev->name);
1773
1774         seq_printf(seq, "\nStopped: ");
1775
1776         list_for_each_entry(pkt_dev, &t->if_list, list)
1777                 if (!pkt_dev->running)
1778                         seq_printf(seq, "%s ", pkt_dev->odev->name);
1779
1780         if (t->result[0])
1781                 seq_printf(seq, "\nResult: %s\n", t->result);
1782         else
1783                 seq_printf(seq, "\nResult: NA\n");
1784
1785         if_unlock(t);
1786
1787         return 0;
1788 }
1789
1790 static ssize_t pktgen_thread_write(struct file *file,
1791                                    const char __user * user_buffer,
1792                                    size_t count, loff_t * offset)
1793 {
1794         struct seq_file *seq = (struct seq_file *)file->private_data;
1795         struct pktgen_thread *t = seq->private;
1796         int i = 0, max, len, ret;
1797         char name[40];
1798         char *pg_result;
1799         unsigned long value = 0;
1800
1801         if (count < 1) {
1802                 //      sprintf(pg_result, "Wrong command format");
1803                 return -EINVAL;
1804         }
1805
1806         max = count - i;
1807         len = count_trail_chars(&user_buffer[i], max);
1808         if (len < 0)
1809                 return len;
1810
1811         i += len;
1812
1813         /* Read variable name */
1814
1815         len = strn_len(&user_buffer[i], sizeof(name) - 1);
1816         if (len < 0)
1817                 return len;
1818
1819         memset(name, 0, sizeof(name));
1820         if (copy_from_user(name, &user_buffer[i], len))
1821                 return -EFAULT;
1822         i += len;
1823
1824         max = count - i;
1825         len = count_trail_chars(&user_buffer[i], max);
1826         if (len < 0)
1827                 return len;
1828
1829         i += len;
1830
1831         if (debug)
1832                 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1833                        name, (unsigned long)count);
1834
1835         if (!t) {
1836                 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1837                 ret = -EINVAL;
1838                 goto out;
1839         }
1840
1841         pg_result = &(t->result[0]);
1842
1843         if (!strcmp(name, "add_device")) {
1844                 char f[32];
1845                 memset(f, 0, 32);
1846                 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1847                 if (len < 0) {
1848                         ret = len;
1849                         goto out;
1850                 }
1851                 if (copy_from_user(f, &user_buffer[i], len))
1852                         return -EFAULT;
1853                 i += len;
1854                 mutex_lock(&pktgen_thread_lock);
1855                 pktgen_add_device(t, f);
1856                 mutex_unlock(&pktgen_thread_lock);
1857                 ret = count;
1858                 sprintf(pg_result, "OK: add_device=%s", f);
1859                 goto out;
1860         }
1861
1862         if (!strcmp(name, "rem_device_all")) {
1863                 mutex_lock(&pktgen_thread_lock);
1864                 t->control |= T_REMDEVALL;
1865                 mutex_unlock(&pktgen_thread_lock);
1866                 schedule_timeout_interruptible(msecs_to_jiffies(125));  /* Propagate thread->control  */
1867                 ret = count;
1868                 sprintf(pg_result, "OK: rem_device_all");
1869                 goto out;
1870         }
1871
1872         if (!strcmp(name, "max_before_softirq")) {
1873                 len = num_arg(&user_buffer[i], 10, &value);
1874                 mutex_lock(&pktgen_thread_lock);
1875                 t->max_before_softirq = value;
1876                 mutex_unlock(&pktgen_thread_lock);
1877                 ret = count;
1878                 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1879                 goto out;
1880         }
1881
1882         ret = -EINVAL;
1883 out:
1884         return ret;
1885 }
1886
1887 static int pktgen_thread_open(struct inode *inode, struct file *file)
1888 {
1889         return single_open(file, pktgen_thread_show, PDE(inode)->data);
1890 }
1891
1892 static const struct file_operations pktgen_thread_fops = {
1893         .owner   = THIS_MODULE,
1894         .open    = pktgen_thread_open,
1895         .read    = seq_read,
1896         .llseek  = seq_lseek,
1897         .write   = pktgen_thread_write,
1898         .release = single_release,
1899 };
1900
1901 /* Think find or remove for NN */
1902 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1903 {
1904         struct pktgen_thread *t;
1905         struct pktgen_dev *pkt_dev = NULL;
1906
1907         list_for_each_entry(t, &pktgen_threads, th_list) {
1908                 pkt_dev = pktgen_find_dev(t, ifname);
1909                 if (pkt_dev) {
1910                         if (remove) {
1911                                 if_lock(t);
1912                                 pkt_dev->removal_mark = 1;
1913                                 t->control |= T_REMDEV;
1914                                 if_unlock(t);
1915                         }
1916                         break;
1917                 }
1918         }
1919         return pkt_dev;
1920 }
1921
1922 /*
1923  * mark a device for removal
1924  */
1925 static void pktgen_mark_device(const char *ifname)
1926 {
1927         struct pktgen_dev *pkt_dev = NULL;
1928         const int max_tries = 10, msec_per_try = 125;
1929         int i = 0;
1930
1931         mutex_lock(&pktgen_thread_lock);
1932         pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1933
1934         while (1) {
1935
1936                 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1937                 if (pkt_dev == NULL)
1938                         break;  /* success */
1939
1940                 mutex_unlock(&pktgen_thread_lock);
1941                 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1942                                 "to disappear....\n", ifname);
1943                 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1944                 mutex_lock(&pktgen_thread_lock);
1945
1946                 if (++i >= max_tries) {
1947                         printk(KERN_ERR "pktgen_mark_device: timed out after "
1948                                "waiting %d msec for device %s to be removed\n",
1949                                msec_per_try * i, ifname);
1950                         break;
1951                 }
1952
1953         }
1954
1955         mutex_unlock(&pktgen_thread_lock);
1956 }
1957
1958 static void pktgen_change_name(struct net_device *dev)
1959 {
1960         struct pktgen_thread *t;
1961
1962         list_for_each_entry(t, &pktgen_threads, th_list) {
1963                 struct pktgen_dev *pkt_dev;
1964
1965                 list_for_each_entry(pkt_dev, &t->if_list, list) {
1966                         if (pkt_dev->odev != dev)
1967                                 continue;
1968
1969                         remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1970
1971                         pkt_dev->entry = create_proc_entry(dev->name, 0600,
1972                                                            pg_proc_dir);
1973                         if (!pkt_dev->entry)
1974                                 printk(KERN_ERR "pktgen: can't move proc "
1975                                        " entry for '%s'\n", dev->name);
1976                         break;
1977                 }
1978         }
1979 }
1980
1981 static int pktgen_device_event(struct notifier_block *unused,
1982                                unsigned long event, void *ptr)
1983 {
1984         struct net_device *dev = ptr;
1985
1986         /* It is OK that we do not hold the group lock right now,
1987          * as we run under the RTNL lock.
1988          */
1989
1990         switch (event) {
1991         case NETDEV_CHANGENAME:
1992                 pktgen_change_name(dev);
1993                 break;
1994
1995         case NETDEV_UNREGISTER:
1996                 pktgen_mark_device(dev->name);
1997                 break;
1998         }
1999
2000         return NOTIFY_DONE;
2001 }
2002
2003 /* Associate pktgen_dev with a device. */
2004
2005 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
2006 {
2007         struct net_device *odev;
2008         int err;
2009
2010         /* Clean old setups */
2011         if (pkt_dev->odev) {
2012                 dev_put(pkt_dev->odev);
2013                 pkt_dev->odev = NULL;
2014         }
2015
2016         odev = dev_get_by_name(ifname);
2017         if (!odev) {
2018                 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
2019                 return -ENODEV;
2020         }
2021
2022         if (odev->type != ARPHRD_ETHER) {
2023                 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
2024                 err = -EINVAL;
2025         } else if (!netif_running(odev)) {
2026                 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
2027                 err = -ENETDOWN;
2028         } else {
2029                 pkt_dev->odev = odev;
2030                 return 0;
2031         }
2032
2033         dev_put(odev);
2034         return err;
2035 }
2036
2037 /* Read pkt_dev from the interface and set up internal pktgen_dev
2038  * structure to have the right information to create/send packets
2039  */
2040 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2041 {
2042         if (!pkt_dev->odev) {
2043                 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
2044                        "setup_inject.\n");
2045                 sprintf(pkt_dev->result,
2046                         "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2047                 return;
2048         }
2049
2050         /* Default to the interface's mac if not explicitly set. */
2051
2052         if (is_zero_ether_addr(pkt_dev->src_mac))
2053                 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2054
2055         /* Set up Dest MAC */
2056         memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2057
2058         /* Set up pkt size */
2059         pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2060
2061         if (pkt_dev->flags & F_IPV6) {
2062                 /*
2063                  * Skip this automatic address setting until locks or functions
2064                  * gets exported
2065                  */
2066
2067 #ifdef NOTNOW
2068                 int i, set = 0, err = 1;
2069                 struct inet6_dev *idev;
2070
2071                 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2072                         if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2073                                 set = 1;
2074                                 break;
2075                         }
2076
2077                 if (!set) {
2078
2079                         /*
2080                          * Use linklevel address if unconfigured.
2081                          *
2082                          * use ipv6_get_lladdr if/when it's get exported
2083                          */
2084
2085                         rcu_read_lock();
2086                         if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2087                                 struct inet6_ifaddr *ifp;
2088
2089                                 read_lock_bh(&idev->lock);
2090                                 for (ifp = idev->addr_list; ifp;
2091                                      ifp = ifp->if_next) {
2092                                         if (ifp->scope == IFA_LINK
2093                                             && !(ifp->
2094                                                  flags & IFA_F_TENTATIVE)) {
2095                                                 ipv6_addr_copy(&pkt_dev->
2096                                                                cur_in6_saddr,
2097                                                                &ifp->addr);
2098                                                 err = 0;
2099                                                 break;
2100                                         }
2101                                 }
2102                                 read_unlock_bh(&idev->lock);
2103                         }
2104                         rcu_read_unlock();
2105                         if (err)
2106                                 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2107                                        "address not availble.\n");
2108                 }
2109 #endif
2110         } else {
2111                 pkt_dev->saddr_min = 0;
2112                 pkt_dev->saddr_max = 0;
2113                 if (strlen(pkt_dev->src_min) == 0) {
2114
2115                         struct in_device *in_dev;
2116
2117                         rcu_read_lock();
2118                         in_dev = __in_dev_get_rcu(pkt_dev->odev);
2119                         if (in_dev) {
2120                                 if (in_dev->ifa_list) {
2121                                         pkt_dev->saddr_min =
2122                                             in_dev->ifa_list->ifa_address;
2123                                         pkt_dev->saddr_max = pkt_dev->saddr_min;
2124                                 }
2125                         }
2126                         rcu_read_unlock();
2127                 } else {
2128                         pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2129                         pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2130                 }
2131
2132                 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2133                 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2134         }
2135         /* Initialize current values. */
2136         pkt_dev->cur_dst_mac_offset = 0;
2137         pkt_dev->cur_src_mac_offset = 0;
2138         pkt_dev->cur_saddr = pkt_dev->saddr_min;
2139         pkt_dev->cur_daddr = pkt_dev->daddr_min;
2140         pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2141         pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2142         pkt_dev->nflows = 0;
2143 }
2144
2145 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2146 {
2147         __u64 start;
2148         __u64 now;
2149
2150         start = now = getCurUs();
2151         printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2152         while (now < spin_until_us) {
2153                 /* TODO: optimize sleeping behavior */
2154                 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2155                         schedule_timeout_interruptible(1);
2156                 else if (spin_until_us - now > 100) {
2157                         do_softirq();
2158                         if (!pkt_dev->running)
2159                                 return;
2160                         if (need_resched())
2161                                 schedule();
2162                 }
2163
2164                 now = getCurUs();
2165         }
2166
2167         pkt_dev->idle_acc += now - start;
2168 }
2169
2170 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2171 {
2172         pkt_dev->pkt_overhead = 0;
2173         pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2174         pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2175         pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2176 }
2177
2178 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2179 {
2180
2181         if (pkt_dev->flows[flow].flags & F_INIT)
2182                 return 1;
2183         else
2184                 return 0;
2185 }
2186
2187 static inline int f_pick(struct pktgen_dev *pkt_dev)
2188 {
2189         int flow = pkt_dev->curfl;
2190
2191         if (pkt_dev->flags & F_FLOW_SEQ) {
2192                 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2193                         /* reset time */
2194                         pkt_dev->flows[flow].count = 0;
2195                         pkt_dev->curfl += 1;
2196                         if (pkt_dev->curfl >= pkt_dev->cflows)
2197                                 pkt_dev->curfl = 0; /*reset */
2198                 }
2199         } else {
2200                 flow = random32() % pkt_dev->cflows;
2201
2202                 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2203                         pkt_dev->flows[flow].count = 0;
2204         }
2205
2206         return pkt_dev->curfl;
2207 }
2208
2209
2210 #ifdef CONFIG_XFRM
2211 /* If there was already an IPSEC SA, we keep it as is, else
2212  * we go look for it ...
2213 */
2214 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2215 {
2216         struct xfrm_state *x = pkt_dev->flows[flow].x;
2217         if (!x) {
2218                 /*slow path: we dont already have xfrm_state*/
2219                 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2220                                         (xfrm_address_t *)&pkt_dev->cur_saddr,
2221                                         AF_INET,
2222                                         pkt_dev->ipsmode,
2223                                         pkt_dev->ipsproto, 0);
2224                 if (x) {
2225                         pkt_dev->flows[flow].x = x;
2226                         set_pkt_overhead(pkt_dev);
2227                         pkt_dev->pkt_overhead+=x->props.header_len;
2228                 }
2229
2230         }
2231 }
2232 #endif
2233 /* Increment/randomize headers according to flags and current values
2234  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2235  */
2236 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2237 {
2238         __u32 imn;
2239         __u32 imx;
2240         int flow = 0;
2241
2242         if (pkt_dev->cflows)
2243                 flow = f_pick(pkt_dev);
2244
2245         /*  Deal with source MAC */
2246         if (pkt_dev->src_mac_count > 1) {
2247                 __u32 mc;
2248                 __u32 tmp;
2249
2250                 if (pkt_dev->flags & F_MACSRC_RND)
2251                         mc = random32() % pkt_dev->src_mac_count;
2252                 else {
2253                         mc = pkt_dev->cur_src_mac_offset++;
2254                         if (pkt_dev->cur_src_mac_offset >
2255                             pkt_dev->src_mac_count)
2256                                 pkt_dev->cur_src_mac_offset = 0;
2257                 }
2258
2259                 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2260                 pkt_dev->hh[11] = tmp;
2261                 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2262                 pkt_dev->hh[10] = tmp;
2263                 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2264                 pkt_dev->hh[9] = tmp;
2265                 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2266                 pkt_dev->hh[8] = tmp;
2267                 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2268                 pkt_dev->hh[7] = tmp;
2269         }
2270
2271         /*  Deal with Destination MAC */
2272         if (pkt_dev->dst_mac_count > 1) {
2273                 __u32 mc;
2274                 __u32 tmp;
2275
2276                 if (pkt_dev->flags & F_MACDST_RND)
2277                         mc = random32() % pkt_dev->dst_mac_count;
2278
2279                 else {
2280                         mc = pkt_dev->cur_dst_mac_offset++;
2281                         if (pkt_dev->cur_dst_mac_offset >
2282                             pkt_dev->dst_mac_count) {
2283                                 pkt_dev->cur_dst_mac_offset = 0;
2284                         }
2285                 }
2286
2287                 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2288                 pkt_dev->hh[5] = tmp;
2289                 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2290                 pkt_dev->hh[4] = tmp;
2291                 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2292                 pkt_dev->hh[3] = tmp;
2293                 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2294                 pkt_dev->hh[2] = tmp;
2295                 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2296                 pkt_dev->hh[1] = tmp;
2297         }
2298
2299         if (pkt_dev->flags & F_MPLS_RND) {
2300                 unsigned i;
2301                 for (i = 0; i < pkt_dev->nr_labels; i++)
2302                         if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2303                                 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2304                                              ((__force __be32)random32() &
2305                                                       htonl(0x000fffff));
2306         }
2307
2308         if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2309                 pkt_dev->vlan_id = random32() & (4096-1);
2310         }
2311
2312         if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2313                 pkt_dev->svlan_id = random32() & (4096 - 1);
2314         }
2315
2316         if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2317                 if (pkt_dev->flags & F_UDPSRC_RND)
2318                         pkt_dev->cur_udp_src = random32() %
2319                                 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2320                                 + pkt_dev->udp_src_min;
2321
2322                 else {
2323                         pkt_dev->cur_udp_src++;
2324                         if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2325                                 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2326                 }
2327         }
2328
2329         if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2330                 if (pkt_dev->flags & F_UDPDST_RND) {
2331                         pkt_dev->cur_udp_dst = random32() %
2332                                 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2333                                 + pkt_dev->udp_dst_min;
2334                 } else {
2335                         pkt_dev->cur_udp_dst++;
2336                         if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2337                                 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2338                 }
2339         }
2340
2341         if (!(pkt_dev->flags & F_IPV6)) {
2342
2343                 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2344                                                          ntohl(pkt_dev->
2345                                                                saddr_max))) {
2346                         __u32 t;
2347                         if (pkt_dev->flags & F_IPSRC_RND)
2348                                 t = random32() % (imx - imn) + imn;
2349                         else {
2350                                 t = ntohl(pkt_dev->cur_saddr);
2351                                 t++;
2352                                 if (t > imx) {
2353                                         t = imn;
2354                                 }
2355                         }
2356                         pkt_dev->cur_saddr = htonl(t);
2357                 }
2358
2359                 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2360                         pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2361                 } else {
2362                         imn = ntohl(pkt_dev->daddr_min);
2363                         imx = ntohl(pkt_dev->daddr_max);
2364                         if (imn < imx) {
2365                                 __u32 t;
2366                                 __be32 s;
2367                                 if (pkt_dev->flags & F_IPDST_RND) {
2368
2369                                         t = random32() % (imx - imn) + imn;
2370                                         s = htonl(t);
2371
2372                                         while (LOOPBACK(s) || MULTICAST(s)
2373                                                || BADCLASS(s) || ZERONET(s)
2374                                                || LOCAL_MCAST(s)) {
2375                                                 t = random32() % (imx - imn) + imn;
2376                                                 s = htonl(t);
2377                                         }
2378                                         pkt_dev->cur_daddr = s;
2379                                 } else {
2380                                         t = ntohl(pkt_dev->cur_daddr);
2381                                         t++;
2382                                         if (t > imx) {
2383                                                 t = imn;
2384                                         }
2385                                         pkt_dev->cur_daddr = htonl(t);
2386                                 }
2387                         }
2388                         if (pkt_dev->cflows) {
2389                                 pkt_dev->flows[flow].flags |= F_INIT;
2390                                 pkt_dev->flows[flow].cur_daddr =
2391                                     pkt_dev->cur_daddr;
2392 #ifdef CONFIG_XFRM
2393                                 if (pkt_dev->flags & F_IPSEC_ON)
2394                                         get_ipsec_sa(pkt_dev, flow);
2395 #endif
2396                                 pkt_dev->nflows++;
2397                         }
2398                 }
2399         } else {                /* IPV6 * */
2400
2401                 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2402                     pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2403                     pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2404                     pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2405                 else {
2406                         int i;
2407
2408                         /* Only random destinations yet */
2409
2410                         for (i = 0; i < 4; i++) {
2411                                 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2412                                     (((__force __be32)random32() |
2413                                       pkt_dev->min_in6_daddr.s6_addr32[i]) &
2414                                      pkt_dev->max_in6_daddr.s6_addr32[i]);
2415                         }
2416                 }
2417         }
2418
2419         if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2420                 __u32 t;
2421                 if (pkt_dev->flags & F_TXSIZE_RND) {
2422                         t = random32() %
2423                                 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2424                                 + pkt_dev->min_pkt_size;
2425                 } else {
2426                         t = pkt_dev->cur_pkt_size + 1;
2427                         if (t > pkt_dev->max_pkt_size)
2428                                 t = pkt_dev->min_pkt_size;
2429                 }
2430                 pkt_dev->cur_pkt_size = t;
2431         }
2432
2433         if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2434                 __u16 t;
2435                 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2436                         t = random32() %
2437                                 (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1)
2438                                 + pkt_dev->queue_map_min;
2439                 } else {
2440                         t = pkt_dev->cur_queue_map + 1;
2441                         if (t > pkt_dev->queue_map_max)
2442                                 t = pkt_dev->queue_map_min;
2443                 }
2444                 pkt_dev->cur_queue_map = t;
2445         }
2446
2447         pkt_dev->flows[flow].count++;
2448 }
2449
2450
2451 #ifdef CONFIG_XFRM
2452 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2453 {
2454         struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2455         int err = 0;
2456         struct iphdr *iph;
2457
2458         if (!x)
2459                 return 0;
2460         /* XXX: we dont support tunnel mode for now until
2461          * we resolve the dst issue */
2462         if (x->props.mode != XFRM_MODE_TRANSPORT)
2463                 return 0;
2464
2465         spin_lock(&x->lock);
2466         iph = ip_hdr(skb);
2467
2468         err = x->mode->output(x, skb);
2469         if (err)
2470                 goto error;
2471         err = x->type->output(x, skb);
2472         if (err)
2473                 goto error;
2474
2475         x->curlft.bytes +=skb->len;
2476         x->curlft.packets++;
2477         spin_unlock(&x->lock);
2478
2479 error:
2480         spin_unlock(&x->lock);
2481         return err;
2482 }
2483
2484 static inline void free_SAs(struct pktgen_dev *pkt_dev)
2485 {
2486         if (pkt_dev->cflows) {
2487                 /* let go of the SAs if we have them */
2488                 int i = 0;
2489                 for (;  i < pkt_dev->nflows; i++){
2490                         struct xfrm_state *x = pkt_dev->flows[i].x;
2491                         if (x) {
2492                                 xfrm_state_put(x);
2493                                 pkt_dev->flows[i].x = NULL;
2494                         }
2495                 }
2496         }
2497 }
2498
2499 static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2500                               struct sk_buff *skb, __be16 protocol)
2501 {
2502         if (pkt_dev->flags & F_IPSEC_ON) {
2503                 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2504                 int nhead = 0;
2505                 if (x) {
2506                         int ret;
2507                         __u8 *eth;
2508                         nhead = x->props.header_len - skb_headroom(skb);
2509                         if (nhead >0) {
2510                                 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2511                                 if (ret < 0) {
2512                                         printk(KERN_ERR "Error expanding "
2513                                                "ipsec packet %d\n",ret);
2514                                         return 0;
2515                                 }
2516                         }
2517
2518                         /* ipsec is not expecting ll header */
2519                         skb_pull(skb, ETH_HLEN);
2520                         ret = pktgen_output_ipsec(skb, pkt_dev);
2521                         if (ret) {
2522                                 printk(KERN_ERR "Error creating ipsec "
2523                                        "packet %d\n",ret);
2524                                 kfree_skb(skb);
2525                                 return 0;
2526                         }
2527                         /* restore ll */
2528                         eth = (__u8 *) skb_push(skb, ETH_HLEN);
2529                         memcpy(eth, pkt_dev->hh, 12);
2530                         *(u16 *) & eth[12] = protocol;
2531                 }
2532         }
2533         return 1;
2534 }
2535 #endif
2536
2537 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2538 {
2539         unsigned i;
2540         for (i = 0; i < pkt_dev->nr_labels; i++) {
2541                 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2542         }
2543         mpls--;
2544         *mpls |= MPLS_STACK_BOTTOM;
2545 }
2546
2547 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2548                                unsigned int prio)
2549 {
2550         return htons(id | (cfi << 12) | (prio << 13));
2551 }
2552
2553 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2554                                         struct pktgen_dev *pkt_dev)
2555 {
2556         struct sk_buff *skb = NULL;
2557         __u8 *eth;
2558         struct udphdr *udph;
2559         int datalen, iplen;
2560         struct iphdr *iph;
2561         struct pktgen_hdr *pgh = NULL;
2562         __be16 protocol = htons(ETH_P_IP);
2563         __be32 *mpls;
2564         __be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
2565         __be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
2566         __be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
2567         __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2568
2569
2570         if (pkt_dev->nr_labels)
2571                 protocol = htons(ETH_P_MPLS_UC);
2572
2573         if (pkt_dev->vlan_id != 0xffff)
2574                 protocol = htons(ETH_P_8021Q);
2575
2576         /* Update any of the values, used when we're incrementing various
2577          * fields.
2578          */
2579         mod_cur_headers(pkt_dev);
2580
2581         datalen = (odev->hard_header_len + 16) & ~0xf;
2582         skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2583                         pkt_dev->pkt_overhead, GFP_ATOMIC);
2584         if (!skb) {
2585                 sprintf(pkt_dev->result, "No memory");
2586                 return NULL;
2587         }
2588
2589         skb_reserve(skb, datalen);
2590
2591         /*  Reserve for ethernet and IP header  */
2592         eth = (__u8 *) skb_push(skb, 14);
2593         mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2594         if (pkt_dev->nr_labels)
2595                 mpls_push(mpls, pkt_dev);
2596
2597         if (pkt_dev->vlan_id != 0xffff) {
2598                 if (pkt_dev->svlan_id != 0xffff) {
2599                         svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2600                         *svlan_tci = build_tci(pkt_dev->svlan_id,
2601                                                pkt_dev->svlan_cfi,
2602                                                pkt_dev->svlan_p);
2603                         svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2604                         *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2605                 }
2606                 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2607                 *vlan_tci = build_tci(pkt_dev->vlan_id,
2608                                       pkt_dev->vlan_cfi,
2609                                       pkt_dev->vlan_p);
2610                 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2611                 *vlan_encapsulated_proto = htons(ETH_P_IP);
2612         }
2613
2614         skb->network_header = skb->tail;
2615         skb->transport_header = skb->network_header + sizeof(struct iphdr);
2616         skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2617         skb->queue_mapping = pkt_dev->cur_queue_map;
2618
2619         iph = ip_hdr(skb);
2620         udph = udp_hdr(skb);
2621
2622         memcpy(eth, pkt_dev->hh, 12);
2623         *(__be16 *) & eth[12] = protocol;
2624
2625         /* Eth + IPh + UDPh + mpls */
2626         datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2627                   pkt_dev->pkt_overhead;
2628         if (datalen < sizeof(struct pktgen_hdr))
2629                 datalen = sizeof(struct pktgen_hdr);
2630
2631         udph->source = htons(pkt_dev->cur_udp_src);
2632         udph->dest = htons(pkt_dev->cur_udp_dst);
2633         udph->len = htons(datalen + 8); /* DATA + udphdr */
2634         udph->check = 0;        /* No checksum */
2635
2636         iph->ihl = 5;
2637         iph->version = 4;
2638         iph->ttl = 32;
2639         iph->tos = pkt_dev->tos;
2640         iph->protocol = IPPROTO_UDP;    /* UDP */
2641         iph->saddr = pkt_dev->cur_saddr;
2642         iph->daddr = pkt_dev->cur_daddr;
2643         iph->frag_off = 0;
2644         iplen = 20 + 8 + datalen;
2645         iph->tot_len = htons(iplen);
2646         iph->check = 0;
2647         iph->check = ip_fast_csum((void *)iph, iph->ihl);
2648         skb->protocol = protocol;
2649         skb->mac_header = (skb->network_header - ETH_HLEN -
2650                            pkt_dev->pkt_overhead);
2651         skb->dev = odev;
2652         skb->pkt_type = PACKET_HOST;
2653
2654         if (pkt_dev->nfrags <= 0)
2655                 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2656         else {
2657                 int frags = pkt_dev->nfrags;
2658                 int i;
2659
2660                 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2661
2662                 if (frags > MAX_SKB_FRAGS)
2663                         frags = MAX_SKB_FRAGS;
2664                 if (datalen > frags * PAGE_SIZE) {
2665                         skb_put(skb, datalen - frags * PAGE_SIZE);
2666                         datalen = frags * PAGE_SIZE;
2667                 }
2668
2669                 i = 0;
2670                 while (datalen > 0) {
2671                         struct page *page = alloc_pages(GFP_KERNEL, 0);
2672                         skb_shinfo(skb)->frags[i].page = page;
2673                         skb_shinfo(skb)->frags[i].page_offset = 0;
2674                         skb_shinfo(skb)->frags[i].size =
2675                             (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2676                         datalen -= skb_shinfo(skb)->frags[i].size;
2677                         skb->len += skb_shinfo(skb)->frags[i].size;
2678                         skb->data_len += skb_shinfo(skb)->frags[i].size;
2679                         i++;
2680                         skb_shinfo(skb)->nr_frags = i;
2681                 }
2682
2683                 while (i < frags) {
2684                         int rem;
2685
2686                         if (i == 0)
2687                                 break;
2688
2689                         rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2690                         if (rem == 0)
2691                                 break;
2692
2693                         skb_shinfo(skb)->frags[i - 1].size -= rem;
2694
2695                         skb_shinfo(skb)->frags[i] =
2696                             skb_shinfo(skb)->frags[i - 1];
2697                         get_page(skb_shinfo(skb)->frags[i].page);
2698                         skb_shinfo(skb)->frags[i].page =
2699                             skb_shinfo(skb)->frags[i - 1].page;
2700                         skb_shinfo(skb)->frags[i].page_offset +=
2701                             skb_shinfo(skb)->frags[i - 1].size;
2702                         skb_shinfo(skb)->frags[i].size = rem;
2703                         i++;
2704                         skb_shinfo(skb)->nr_frags = i;
2705                 }
2706         }
2707
2708         /* Stamp the time, and sequence number, convert them to network byte order */
2709
2710         if (pgh) {
2711                 struct timeval timestamp;
2712
2713                 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2714                 pgh->seq_num = htonl(pkt_dev->seq_num);
2715
2716                 do_gettimeofday(&timestamp);
2717                 pgh->tv_sec = htonl(timestamp.tv_sec);
2718                 pgh->tv_usec = htonl(timestamp.tv_usec);
2719         }
2720
2721 #ifdef CONFIG_XFRM
2722         if (!process_ipsec(pkt_dev, skb, protocol))
2723                 return NULL;
2724 #endif
2725
2726         return skb;
2727 }
2728
2729 /*
2730  * scan_ip6, fmt_ip taken from dietlibc-0.21
2731  * Author Felix von Leitner <felix-dietlibc@fefe.de>
2732  *
2733  * Slightly modified for kernel.
2734  * Should be candidate for net/ipv4/utils.c
2735  * --ro
2736  */
2737
2738 static unsigned int scan_ip6(const char *s, char ip[16])
2739 {
2740         unsigned int i;
2741         unsigned int len = 0;
2742         unsigned long u;
2743         char suffix[16];
2744         unsigned int prefixlen = 0;
2745         unsigned int suffixlen = 0;
2746         __be32 tmp;
2747
2748         for (i = 0; i < 16; i++)
2749                 ip[i] = 0;
2750
2751         for (;;) {
2752                 if (*s == ':') {
2753                         len++;
2754                         if (s[1] == ':') {      /* Found "::", skip to part 2 */
2755                                 s += 2;
2756                                 len++;
2757                                 break;
2758                         }
2759                         s++;
2760                 }
2761                 {
2762                         char *tmp;
2763                         u = simple_strtoul(s, &tmp, 16);
2764                         i = tmp - s;
2765                 }
2766
2767                 if (!i)
2768                         return 0;
2769                 if (prefixlen == 12 && s[i] == '.') {
2770
2771                         /* the last 4 bytes may be written as IPv4 address */
2772
2773                         tmp = in_aton(s);
2774                         memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2775                         return i + len;
2776                 }
2777                 ip[prefixlen++] = (u >> 8);
2778                 ip[prefixlen++] = (u & 255);
2779                 s += i;
2780                 len += i;
2781                 if (prefixlen == 16)
2782                         return len;
2783         }
2784
2785 /* part 2, after "::" */
2786         for (;;) {
2787                 if (*s == ':') {
2788                         if (suffixlen == 0)
2789                                 break;
2790                         s++;
2791                         len++;
2792                 } else if (suffixlen != 0)
2793                         break;
2794                 {
2795                         char *tmp;
2796                         u = simple_strtol(s, &tmp, 16);
2797                         i = tmp - s;
2798                 }
2799                 if (!i) {
2800                         if (*s)
2801                                 len--;
2802                         break;
2803                 }
2804                 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2805                         tmp = in_aton(s);
2806                         memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2807                                sizeof(tmp));
2808                         suffixlen += 4;
2809                         len += strlen(s);
2810                         break;
2811                 }
2812                 suffix[suffixlen++] = (u >> 8);
2813                 suffix[suffixlen++] = (u & 255);
2814                 s += i;
2815                 len += i;
2816                 if (prefixlen + suffixlen == 16)
2817                         break;
2818         }
2819         for (i = 0; i < suffixlen; i++)
2820                 ip[16 - suffixlen + i] = suffix[i];
2821         return len;
2822 }
2823
2824 static char tohex(char hexdigit)
2825 {
2826         return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2827 }
2828
2829 static int fmt_xlong(char *s, unsigned int i)
2830 {
2831         char *bak = s;
2832         *s = tohex((i >> 12) & 0xf);
2833         if (s != bak || *s != '0')
2834                 ++s;
2835         *s = tohex((i >> 8) & 0xf);
2836         if (s != bak || *s != '0')
2837                 ++s;
2838         *s = tohex((i >> 4) & 0xf);
2839         if (s != bak || *s != '0')
2840                 ++s;
2841         *s = tohex(i & 0xf);
2842         return s - bak + 1;
2843 }
2844
2845 static unsigned int fmt_ip6(char *s, const char ip[16])
2846 {
2847         unsigned int len;
2848         unsigned int i;
2849         unsigned int temp;
2850         unsigned int compressing;
2851         int j;
2852
2853         len = 0;
2854         compressing = 0;
2855         for (j = 0; j < 16; j += 2) {
2856
2857 #ifdef V4MAPPEDPREFIX
2858                 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2859                         inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2860                         temp = strlen(s);
2861                         return len + temp;
2862                 }
2863 #endif
2864                 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2865                     (unsigned long)(unsigned char)ip[j + 1];
2866                 if (temp == 0) {
2867                         if (!compressing) {
2868                                 compressing = 1;
2869                                 if (j == 0) {
2870                                         *s++ = ':';
2871                                         ++len;
2872                                 }
2873                         }
2874                 } else {
2875                         if (compressing) {
2876                                 compressing = 0;
2877                                 *s++ = ':';
2878                                 ++len;
2879                         }
2880                         i = fmt_xlong(s, temp);
2881                         len += i;
2882                         s += i;
2883                         if (j < 14) {
2884                                 *s++ = ':';
2885                                 ++len;
2886                         }
2887                 }
2888         }
2889         if (compressing) {
2890                 *s++ = ':';
2891                 ++len;
2892         }
2893         *s = 0;
2894         return len;
2895 }
2896
2897 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2898                                         struct pktgen_dev *pkt_dev)
2899 {
2900         struct sk_buff *skb = NULL;
2901         __u8 *eth;
2902         struct udphdr *udph;
2903         int datalen;
2904         struct ipv6hdr *iph;
2905         struct pktgen_hdr *pgh = NULL;
2906         __be16 protocol = htons(ETH_P_IPV6);
2907         __be32 *mpls;
2908         __be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
2909         __be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
2910         __be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
2911         __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2912
2913         if (pkt_dev->nr_labels)
2914                 protocol = htons(ETH_P_MPLS_UC);
2915
2916         if (pkt_dev->vlan_id != 0xffff)
2917                 protocol = htons(ETH_P_8021Q);
2918
2919         /* Update any of the values, used when we're incrementing various
2920          * fields.
2921          */
2922         mod_cur_headers(pkt_dev);
2923
2924         skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2925                         pkt_dev->pkt_overhead, GFP_ATOMIC);
2926         if (!skb) {
2927                 sprintf(pkt_dev->result, "No memory");
2928                 return NULL;
2929         }
2930
2931         skb_reserve(skb, 16);
2932
2933         /*  Reserve for ethernet and IP header  */
2934         eth = (__u8 *) skb_push(skb, 14);
2935         mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2936         if (pkt_dev->nr_labels)
2937                 mpls_push(mpls, pkt_dev);
2938
2939         if (pkt_dev->vlan_id != 0xffff) {
2940                 if (pkt_dev->svlan_id != 0xffff) {
2941                         svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2942                         *svlan_tci = build_tci(pkt_dev->svlan_id,
2943                                                pkt_dev->svlan_cfi,
2944                                                pkt_dev->svlan_p);
2945                         svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2946                         *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2947                 }
2948                 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2949                 *vlan_tci = build_tci(pkt_dev->vlan_id,
2950                                       pkt_dev->vlan_cfi,
2951                                       pkt_dev->vlan_p);
2952                 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2953                 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2954         }
2955
2956         skb->network_header = skb->tail;
2957         skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2958         skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2959         skb->queue_mapping = pkt_dev->cur_queue_map;
2960
2961         iph = ipv6_hdr(skb);
2962         udph = udp_hdr(skb);
2963
2964         memcpy(eth, pkt_dev->hh, 12);
2965         *(__be16 *) & eth[12] = protocol;
2966
2967         /* Eth + IPh + UDPh + mpls */
2968         datalen = pkt_dev->cur_pkt_size - 14 -
2969                   sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2970                   pkt_dev->pkt_overhead;
2971
2972         if (datalen < sizeof(struct pktgen_hdr)) {
2973                 datalen = sizeof(struct pktgen_hdr);
2974                 if (net_ratelimit())
2975                         printk(KERN_INFO "pktgen: increased datalen to %d\n",
2976                                datalen);
2977         }
2978
2979         udph->source = htons(pkt_dev->cur_udp_src);
2980         udph->dest = htons(pkt_dev->cur_udp_dst);
2981         udph->len = htons(datalen + sizeof(struct udphdr));
2982         udph->check = 0;        /* No checksum */
2983
2984         *(__be32 *) iph = htonl(0x60000000);    /* Version + flow */
2985
2986         if (pkt_dev->traffic_class) {
2987                 /* Version + traffic class + flow (0) */
2988                 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2989         }
2990
2991         iph->hop_limit = 32;
2992
2993         iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2994         iph->nexthdr = IPPROTO_UDP;
2995
2996         ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2997         ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2998
2999         skb->mac_header = (skb->network_header - ETH_HLEN -
3000                            pkt_dev->pkt_overhead);
3001         skb->protocol = protocol;
3002         skb->dev = odev;
3003         skb->pkt_type = PACKET_HOST;
3004
3005         if (pkt_dev->nfrags <= 0)
3006                 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
3007         else {
3008                 int frags = pkt_dev->nfrags;
3009                 int i;
3010
3011                 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
3012
3013                 if (frags > MAX_SKB_FRAGS)
3014                         frags = MAX_SKB_FRAGS;
3015                 if (datalen > frags * PAGE_SIZE) {
3016                         skb_put(skb, datalen - frags * PAGE_SIZE);
3017                         datalen = frags * PAGE_SIZE;
3018                 }
3019
3020                 i = 0;
3021                 while (datalen > 0) {
3022                         struct page *page = alloc_pages(GFP_KERNEL, 0);
3023                         skb_shinfo(skb)->frags[i].page = page;
3024                         skb_shinfo(skb)->frags[i].page_offset = 0;
3025                         skb_shinfo(skb)->frags[i].size =
3026                             (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
3027                         datalen -= skb_shinfo(skb)->frags[i].size;
3028                         skb->len += skb_shinfo(skb)->frags[i].size;
3029                         skb->data_len += skb_shinfo(skb)->frags[i].size;
3030                         i++;
3031                         skb_shinfo(skb)->nr_frags = i;
3032                 }
3033
3034                 while (i < frags) {
3035                         int rem;
3036
3037                         if (i == 0)
3038                                 break;
3039
3040                         rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3041                         if (rem == 0)
3042                                 break;
3043
3044                         skb_shinfo(skb)->frags[i - 1].size -= rem;
3045
3046                         skb_shinfo(skb)->frags[i] =
3047                             skb_shinfo(skb)->frags[i - 1];
3048                         get_page(skb_shinfo(skb)->frags[i].page);
3049                         skb_shinfo(skb)->frags[i].page =
3050                             skb_shinfo(skb)->frags[i - 1].page;
3051                         skb_shinfo(skb)->frags[i].page_offset +=
3052                             skb_shinfo(skb)->frags[i - 1].size;
3053                         skb_shinfo(skb)->frags[i].size = rem;
3054                         i++;
3055                         skb_shinfo(skb)->nr_frags = i;
3056                 }
3057         }
3058
3059         /* Stamp the time, and sequence number, convert them to network byte order */
3060         /* should we update cloned packets too ? */
3061         if (pgh) {
3062                 struct timeval timestamp;
3063
3064                 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3065                 pgh->seq_num = htonl(pkt_dev->seq_num);
3066
3067                 do_gettimeofday(&timestamp);
3068                 pgh->tv_sec = htonl(timestamp.tv_sec);
3069                 pgh->tv_usec = htonl(timestamp.tv_usec);
3070         }
3071         /* pkt_dev->seq_num++; FF: you really mean this? */
3072
3073         return skb;
3074 }
3075
3076 static inline struct sk_buff *fill_packet(struct net_device *odev,
3077                                           struct pktgen_dev *pkt_dev)
3078 {
3079         if (pkt_dev->flags & F_IPV6)
3080                 return fill_packet_ipv6(odev, pkt_dev);
3081         else
3082                 return fill_packet_ipv4(odev, pkt_dev);
3083 }
3084
3085 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3086 {
3087         pkt_dev->seq_num = 1;
3088         pkt_dev->idle_acc = 0;
3089         pkt_dev->sofar = 0;
3090         pkt_dev->tx_bytes = 0;
3091         pkt_dev->errors = 0;
3092 }
3093
3094 /* Set up structure for sending pkts, clear counters */
3095
3096 static void pktgen_run(struct pktgen_thread *t)
3097 {
3098         struct pktgen_dev *pkt_dev;
3099         int started = 0;
3100
3101         pr_debug("pktgen: entering pktgen_run. %p\n", t);
3102
3103         if_lock(t);
3104         list_for_each_entry(pkt_dev, &t->if_list, list) {
3105
3106                 /*
3107                  * setup odev and create initial packet.
3108                  */
3109                 pktgen_setup_inject(pkt_dev);
3110
3111                 if (pkt_dev->odev) {
3112                         pktgen_clear_counters(pkt_dev);
3113                         pkt_dev->running = 1;   /* Cranke yeself! */
3114                         pkt_dev->skb = NULL;
3115                         pkt_dev->started_at = getCurUs();
3116                         pkt_dev->next_tx_us = getCurUs();       /* Transmit immediately */
3117                         pkt_dev->next_tx_ns = 0;
3118                         set_pkt_overhead(pkt_dev);
3119
3120                         strcpy(pkt_dev->result, "Starting");
3121                         started++;
3122                 } else
3123                         strcpy(pkt_dev->result, "Error starting");
3124         }
3125         if_unlock(t);
3126         if (started)
3127                 t->control &= ~(T_STOP);
3128 }
3129
3130 static void pktgen_stop_all_threads_ifs(void)
3131 {
3132         struct pktgen_thread *t;
3133
3134         pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3135
3136         mutex_lock(&pktgen_thread_lock);
3137
3138         list_for_each_entry(t, &pktgen_threads, th_list)
3139                 t->control |= T_STOP;
3140
3141         mutex_unlock(&pktgen_thread_lock);
3142 }
3143
3144 static int thread_is_running(struct pktgen_thread *t)
3145 {
3146         struct pktgen_dev *pkt_dev;
3147         int res = 0;
3148
3149         list_for_each_entry(pkt_dev, &t->if_list, list)
3150                 if (pkt_dev->running) {
3151                         res = 1;
3152                         break;
3153                 }
3154         return res;
3155 }
3156
3157 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3158 {
3159         if_lock(t);
3160
3161         while (thread_is_running(t)) {
3162
3163                 if_unlock(t);
3164
3165                 msleep_interruptible(100);
3166
3167                 if (signal_pending(current))
3168                         goto signal;
3169                 if_lock(t);
3170         }
3171         if_unlock(t);
3172         return 1;
3173 signal:
3174         return 0;
3175 }
3176
3177 static int pktgen_wait_all_threads_run(void)
3178 {
3179         struct pktgen_thread *t;
3180         int sig = 1;
3181
3182         mutex_lock(&pktgen_thread_lock);
3183
3184         list_for_each_entry(t, &pktgen_threads, th_list) {
3185                 sig = pktgen_wait_thread_run(t);
3186                 if (sig == 0)
3187                         break;
3188         }
3189
3190         if (sig == 0)
3191                 list_for_each_entry(t, &pktgen_threads, th_list)
3192                         t->control |= (T_STOP);
3193
3194         mutex_unlock(&pktgen_thread_lock);
3195         return sig;
3196 }
3197
3198 static void pktgen_run_all_threads(void)
3199 {
3200         struct pktgen_thread *t;
3201
3202         pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3203
3204         mutex_lock(&pktgen_thread_lock);
3205
3206         list_for_each_entry(t, &pktgen_threads, th_list)
3207                 t->control |= (T_RUN);
3208
3209         mutex_unlock(&pktgen_thread_lock);
3210
3211         schedule_timeout_interruptible(msecs_to_jiffies(125));  /* Propagate thread->control  */
3212
3213         pktgen_wait_all_threads_run();
3214 }
3215
3216 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3217 {
3218         __u64 total_us, bps, mbps, pps, idle;
3219         char *p = pkt_dev->result;
3220
3221         total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3222
3223         idle = pkt_dev->idle_acc;
3224
3225         p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3226                      (unsigned long long)total_us,
3227                      (unsigned long long)(total_us - idle),
3228                      (unsigned long long)idle,
3229                      (unsigned long long)pkt_dev->sofar,
3230                      pkt_dev->cur_pkt_size, nr_frags);
3231
3232         pps = pkt_dev->sofar * USEC_PER_SEC;
3233
3234         while ((total_us >> 32) != 0) {
3235                 pps >>= 1;
3236                 total_us >>= 1;
3237         }
3238
3239         do_div(pps, total_us);
3240
3241         bps = pps * 8 * pkt_dev->cur_pkt_size;
3242
3243         mbps = bps;
3244         do_div(mbps, 1000000);
3245         p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
3246                      (unsigned long long)pps,
3247                      (unsigned long long)mbps,
3248                      (unsigned long long)bps,
3249                      (unsigned long long)pkt_dev->errors);
3250 }
3251
3252 /* Set stopped-at timer, remove from running list, do counters & statistics */
3253
3254 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3255 {
3256         int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3257
3258         if (!pkt_dev->running) {
3259                 printk(KERN_WARNING "pktgen: interface: %s is already "
3260                        "stopped\n", pkt_dev->odev->name);
3261                 return -EINVAL;
3262         }
3263
3264         pkt_dev->stopped_at = getCurUs();
3265         pkt_dev->running = 0;
3266
3267         show_results(pkt_dev, nr_frags);
3268
3269         return 0;
3270 }
3271
3272 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3273 {
3274         struct pktgen_dev *pkt_dev, *best = NULL;
3275
3276         if_lock(t);
3277
3278         list_for_each_entry(pkt_dev, &t->if_list, list) {
3279                 if (!pkt_dev->running)
3280                         continue;
3281                 if (best == NULL)
3282                         best = pkt_dev;
3283                 else if (pkt_dev->next_tx_us < best->next_tx_us)
3284                         best = pkt_dev;
3285         }
3286         if_unlock(t);
3287         return best;
3288 }
3289
3290 static void pktgen_stop(struct pktgen_thread *t)
3291 {
3292         struct pktgen_dev *pkt_dev;
3293
3294         pr_debug("pktgen: entering pktgen_stop\n");
3295
3296         if_lock(t);
3297
3298         list_for_each_entry(pkt_dev, &t->if_list, list) {
3299                 pktgen_stop_device(pkt_dev);
3300                 if (pkt_dev->skb)
3301                         kfree_skb(pkt_dev->skb);
3302
3303                 pkt_dev->skb = NULL;
3304         }
3305
3306         if_unlock(t);
3307 }
3308
3309 /*
3310  * one of our devices needs to be removed - find it
3311  * and remove it
3312  */
3313 static void pktgen_rem_one_if(struct pktgen_thread *t)
3314 {
3315         struct list_head *q, *n;
3316         struct pktgen_dev *cur;
3317
3318         pr_debug("pktgen: entering pktgen_rem_one_if\n");
3319
3320         if_lock(t);
3321
3322         list_for_each_safe(q, n, &t->if_list) {
3323                 cur = list_entry(q, struct pktgen_dev, list);
3324
3325                 if (!cur->removal_mark)
3326                         continue;
3327
3328                 if (cur->skb)
3329                         kfree_skb(cur->skb);
3330                 cur->skb = NULL;
3331
3332                 pktgen_remove_device(t, cur);
3333
3334                 break;
3335         }
3336
3337         if_unlock(t);
3338 }
3339
3340 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3341 {
3342         struct list_head *q, *n;
3343         struct pktgen_dev *cur;
3344
3345         /* Remove all devices, free mem */
3346
3347         pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3348         if_lock(t);
3349
3350         list_for_each_safe(q, n, &t->if_list) {
3351                 cur = list_entry(q, struct pktgen_dev, list);
3352
3353                 if (cur->skb)
3354                         kfree_skb(cur->skb);
3355                 cur->skb = NULL;
3356
3357                 pktgen_remove_device(t, cur);
3358         }
3359
3360         if_unlock(t);
3361 }
3362
3363 static void pktgen_rem_thread(struct pktgen_thread *t)
3364 {
3365         /* Remove from the thread list */
3366
3367         remove_proc_entry(t->tsk->comm, pg_proc_dir);
3368
3369         mutex_lock(&pktgen_thread_lock);
3370
3371         list_del(&t->th_list);
3372
3373         mutex_unlock(&pktgen_thread_lock);
3374 }
3375
3376 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3377 {
3378         struct net_device *odev = NULL;
3379         __u64 idle_start = 0;
3380         int ret;
3381
3382         odev = pkt_dev->odev;
3383
3384         if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3385                 u64 now;
3386
3387                 now = getCurUs();
3388                 if (now < pkt_dev->next_tx_us)
3389                         spin(pkt_dev, pkt_dev->next_tx_us);
3390
3391                 /* This is max DELAY, this has special meaning of
3392                  * "never transmit"
3393                  */
3394                 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3395                         pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3396                         pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3397                         goto out;
3398                 }
3399         }
3400
3401         if ((netif_queue_stopped(odev) ||
3402              (pkt_dev->skb &&
3403               netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping))) ||
3404             need_resched()) {
3405                 idle_start = getCurUs();
3406
3407                 if (!netif_running(odev)) {
3408                         pktgen_stop_device(pkt_dev);
3409                         if (pkt_dev->skb)
3410                                 kfree_skb(pkt_dev->skb);
3411                         pkt_dev->skb = NULL;
3412                         goto out;
3413                 }
3414                 if (need_resched())
3415                         schedule();
3416
3417                 pkt_dev->idle_acc += getCurUs() - idle_start;
3418
3419                 if (netif_queue_stopped(odev) ||
3420                     netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3421                         pkt_dev->next_tx_us = getCurUs();       /* TODO */
3422                         pkt_dev->next_tx_ns = 0;
3423                         goto out;       /* Try the next interface */
3424                 }
3425         }
3426
3427         if (pkt_dev->last_ok || !pkt_dev->skb) {
3428                 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3429                     || (!pkt_dev->skb)) {
3430                         /* build a new pkt */
3431                         if (pkt_dev->skb)
3432                                 kfree_skb(pkt_dev->skb);
3433
3434                         pkt_dev->skb = fill_packet(odev, pkt_dev);
3435                         if (pkt_dev->skb == NULL) {
3436                                 printk(KERN_ERR "pktgen: ERROR: couldn't "
3437                                        "allocate skb in fill_packet.\n");
3438                                 schedule();
3439                                 pkt_dev->clone_count--; /* back out increment, OOM */
3440                                 goto out;
3441                         }
3442                         pkt_dev->allocated_skbs++;
3443                         pkt_dev->clone_count = 0;       /* reset counter */
3444                 }
3445         }
3446
3447         netif_tx_lock_bh(odev);
3448         if (!netif_queue_stopped(odev) &&
3449             !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3450
3451                 atomic_inc(&(pkt_dev->skb->users));
3452               retry_now:
3453                 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3454                 if (likely(ret == NETDEV_TX_OK)) {
3455                         pkt_dev->last_ok = 1;
3456                         pkt_dev->sofar++;
3457                         pkt_dev->seq_num++;
3458                         pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3459
3460                 } else if (ret == NETDEV_TX_LOCKED
3461                            && (odev->features & NETIF_F_LLTX)) {
3462                         cpu_relax();
3463                         goto retry_now;
3464                 } else {        /* Retry it next time */
3465
3466                         atomic_dec(&(pkt_dev->skb->users));
3467
3468                         if (debug && net_ratelimit())
3469                                 printk(KERN_INFO "pktgen: Hard xmit error\n");
3470
3471                         pkt_dev->errors++;
3472                         pkt_dev->last_ok = 0;
3473                 }
3474
3475                 pkt_dev->next_tx_us = getCurUs();
3476                 pkt_dev->next_tx_ns = 0;
3477
3478                 pkt_dev->next_tx_us += pkt_dev->delay_us;
3479                 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3480
3481                 if (pkt_dev->next_tx_ns > 1000) {
3482                         pkt_dev->next_tx_us++;
3483                         pkt_dev->next_tx_ns -= 1000;
3484                 }
3485         }
3486
3487         else {                  /* Retry it next time */
3488                 pkt_dev->last_ok = 0;
3489                 pkt_dev->next_tx_us = getCurUs();       /* TODO */
3490                 pkt_dev->next_tx_ns = 0;
3491         }
3492
3493         netif_tx_unlock_bh(odev);
3494
3495         /* If pkt_dev->count is zero, then run forever */
3496         if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3497                 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3498                         idle_start = getCurUs();
3499                         while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3500                                 if (signal_pending(current)) {
3501                                         break;
3502                                 }
3503                                 schedule();
3504                         }
3505                         pkt_dev->idle_acc += getCurUs() - idle_start;
3506                 }
3507
3508                 /* Done with this */
3509                 pktgen_stop_device(pkt_dev);
3510                 if (pkt_dev->skb)
3511                         kfree_skb(pkt_dev->skb);
3512                 pkt_dev->skb = NULL;
3513         }
3514 out:;
3515 }
3516
3517 /*
3518  * Main loop of the thread goes here
3519  */
3520
3521 static int pktgen_thread_worker(void *arg)
3522 {
3523         DEFINE_WAIT(wait);
3524         struct pktgen_thread *t = arg;
3525         struct pktgen_dev *pkt_dev = NULL;
3526         int cpu = t->cpu;
3527         u32 max_before_softirq;
3528         u32 tx_since_softirq = 0;
3529
3530         BUG_ON(smp_processor_id() != cpu);
3531
3532         init_waitqueue_head(&t->queue);
3533
3534         pr_debug("pktgen: starting pktgen/%d:  pid=%d\n", cpu, current->pid);
3535
3536         max_before_softirq = t->max_before_softirq;
3537
3538         set_current_state(TASK_INTERRUPTIBLE);
3539
3540         set_freezable();
3541
3542         while (!kthread_should_stop()) {
3543                 pkt_dev = next_to_run(t);
3544
3545                 if (!pkt_dev &&
3546                     (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3547                     == 0) {
3548                         prepare_to_wait(&(t->queue), &wait,
3549                                         TASK_INTERRUPTIBLE);
3550                         schedule_timeout(HZ / 10);
3551                         finish_wait(&(t->queue), &wait);
3552                 }
3553
3554                 __set_current_state(TASK_RUNNING);
3555
3556                 if (pkt_dev) {
3557
3558                         pktgen_xmit(pkt_dev);
3559
3560                         /*
3561                          * We like to stay RUNNING but must also give
3562                          * others fair share.
3563                          */
3564
3565                         tx_since_softirq += pkt_dev->last_ok;
3566
3567                         if (tx_since_softirq > max_before_softirq) {
3568                                 if (local_softirq_pending())
3569                                         do_softirq();
3570                                 tx_since_softirq = 0;
3571                         }
3572                 }
3573
3574                 if (t->control & T_STOP) {
3575                         pktgen_stop(t);
3576                         t->control &= ~(T_STOP);
3577                 }
3578
3579                 if (t->control & T_RUN) {
3580                         pktgen_run(t);
3581                         t->control &= ~(T_RUN);
3582                 }
3583
3584                 if (t->control & T_REMDEVALL) {
3585                         pktgen_rem_all_ifs(t);
3586                         t->control &= ~(T_REMDEVALL);
3587                 }
3588
3589                 if (t->control & T_REMDEV) {
3590                         pktgen_rem_one_if(t);
3591                         t->control &= ~(T_REMDEV);
3592                 }
3593
3594                 try_to_freeze();
3595
3596                 set_current_state(TASK_INTERRUPTIBLE);
3597         }
3598
3599         pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3600         pktgen_stop(t);
3601
3602         pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3603         pktgen_rem_all_ifs(t);
3604
3605         pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3606         pktgen_rem_thread(t);
3607
3608         return 0;
3609 }
3610
3611 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3612                                           const char *ifname)
3613 {
3614         struct pktgen_dev *p, *pkt_dev = NULL;
3615         if_lock(t);
3616
3617         list_for_each_entry(p, &t->if_list, list)
3618                 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3619                         pkt_dev = p;
3620                         break;
3621                 }
3622
3623         if_unlock(t);
3624         pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3625         return pkt_dev;
3626 }
3627
3628 /*
3629  * Adds a dev at front of if_list.
3630  */
3631
3632 static int add_dev_to_thread(struct pktgen_thread *t,
3633                              struct pktgen_dev *pkt_dev)
3634 {
3635         int rv = 0;
3636
3637         if_lock(t);
3638
3639         if (pkt_dev->pg_thread) {
3640                 printk(KERN_ERR "pktgen: ERROR: already assigned "
3641                        "to a thread.\n");
3642                 rv = -EBUSY;
3643                 goto out;
3644         }
3645
3646         list_add(&pkt_dev->list, &t->if_list);
3647         pkt_dev->pg_thread = t;
3648         pkt_dev->running = 0;
3649
3650 out:
3651         if_unlock(t);
3652         return rv;
3653 }
3654
3655 /* Called under thread lock */
3656
3657 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3658 {
3659         struct pktgen_dev *pkt_dev;
3660         int err;
3661
3662         /* We don't allow a device to be on several threads */
3663
3664         pkt_dev = __pktgen_NN_threads(ifname, FIND);
3665         if (pkt_dev) {
3666                 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3667                 return -EBUSY;
3668         }
3669
3670         pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3671         if (!pkt_dev)
3672                 return -ENOMEM;
3673
3674         pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3675         if (pkt_dev->flows == NULL) {
3676                 kfree(pkt_dev);
3677                 return -ENOMEM;
3678         }
3679         memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3680
3681         pkt_dev->removal_mark = 0;
3682         pkt_dev->min_pkt_size = ETH_ZLEN;
3683         pkt_dev->max_pkt_size = ETH_ZLEN;
3684         pkt_dev->nfrags = 0;
3685         pkt_dev->clone_skb = pg_clone_skb_d;
3686         pkt_dev->delay_us = pg_delay_d / 1000;
3687         pkt_dev->delay_ns = pg_delay_d % 1000;
3688         pkt_dev->count = pg_count_d;
3689         pkt_dev->sofar = 0;
3690         pkt_dev->udp_src_min = 9;       /* sink port */
3691         pkt_dev->udp_src_max = 9;
3692         pkt_dev->udp_dst_min = 9;
3693         pkt_dev->udp_dst_max = 9;
3694
3695         pkt_dev->vlan_p = 0;
3696         pkt_dev->vlan_cfi = 0;
3697         pkt_dev->vlan_id = 0xffff;
3698         pkt_dev->svlan_p = 0;
3699         pkt_dev->svlan_cfi = 0;
3700         pkt_dev->svlan_id = 0xffff;
3701
3702         err = pktgen_setup_dev(pkt_dev, ifname);
3703         if (err)
3704                 goto out1;
3705
3706         pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3707         if (!pkt_dev->entry) {
3708                 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3709                        PG_PROC_DIR, ifname);
3710                 err = -EINVAL;
3711                 goto out2;
3712         }
3713         pkt_dev->entry->proc_fops = &pktgen_if_fops;
3714         pkt_dev->entry->data = pkt_dev;
3715 #ifdef CONFIG_XFRM
3716         pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3717         pkt_dev->ipsproto = IPPROTO_ESP;
3718 #endif
3719
3720         return add_dev_to_thread(t, pkt_dev);
3721 out2:
3722         dev_put(pkt_dev->odev);
3723 out1:
3724 #ifdef CONFIG_XFRM
3725         free_SAs(pkt_dev);
3726 #endif
3727         if (pkt_dev->flows)
3728                 vfree(pkt_dev->flows);
3729         kfree(pkt_dev);
3730         return err;
3731 }
3732
3733 static int __init pktgen_create_thread(int cpu)
3734 {
3735         struct pktgen_thread *t;
3736         struct proc_dir_entry *pe;
3737         struct task_struct *p;
3738
3739         t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3740         if (!t) {
3741                 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3742                        "create new thread.\n");
3743                 return -ENOMEM;
3744         }
3745
3746         spin_lock_init(&t->if_lock);
3747         t->cpu = cpu;
3748
3749         INIT_LIST_HEAD(&t->if_list);
3750
3751         list_add_tail(&t->th_list, &pktgen_threads);
3752
3753         p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3754         if (IS_ERR(p)) {
3755                 printk(KERN_ERR "pktgen: kernel_thread() failed "
3756                        "for cpu %d\n", t->cpu);
3757                 list_del(&t->th_list);
3758                 kfree(t);
3759                 return PTR_ERR(p);
3760         }
3761         kthread_bind(p, cpu);
3762         t->tsk = p;
3763
3764         pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3765         if (!pe) {
3766                 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3767                        PG_PROC_DIR, t->tsk->comm);
3768                 kthread_stop(p);
3769                 list_del(&t->th_list);
3770                 kfree(t);
3771                 return -EINVAL;
3772         }
3773
3774         pe->proc_fops = &pktgen_thread_fops;
3775         pe->data = t;
3776
3777         wake_up_process(p);
3778
3779         return 0;
3780 }
3781
3782 /*
3783  * Removes a device from the thread if_list.
3784  */
3785 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3786                                   struct pktgen_dev *pkt_dev)
3787 {
3788         struct list_head *q, *n;
3789         struct pktgen_dev *p;
3790
3791         list_for_each_safe(q, n, &t->if_list) {
3792                 p = list_entry(q, struct pktgen_dev, list);
3793                 if (p == pkt_dev)
3794                         list_del(&p->list);
3795         }
3796 }
3797
3798 static int pktgen_remove_device(struct pktgen_thread *t,
3799                                 struct pktgen_dev *pkt_dev)
3800 {
3801
3802         pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3803
3804         if (pkt_dev->running) {
3805                 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3806                        "running interface, stopping it now.\n");
3807                 pktgen_stop_device(pkt_dev);
3808         }
3809
3810         /* Dis-associate from the interface */
3811
3812         if (pkt_dev->odev) {
3813                 dev_put(pkt_dev->odev);
3814                 pkt_dev->odev = NULL;
3815         }
3816
3817         /* And update the thread if_list */
3818
3819         _rem_dev_from_if_list(t, pkt_dev);
3820
3821         if (pkt_dev->entry)
3822                 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3823
3824 #ifdef CONFIG_XFRM
3825         free_SAs(pkt_dev);
3826 #endif
3827         if (pkt_dev->flows)
3828                 vfree(pkt_dev->flows);
3829         kfree(pkt_dev);
3830         return 0;
3831 }
3832
3833 static int __init pg_init(void)
3834 {
3835         int cpu;
3836         struct proc_dir_entry *pe;
3837
3838         printk(KERN_INFO "%s", version);
3839
3840         pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3841         if (!pg_proc_dir)
3842                 return -ENODEV;
3843         pg_proc_dir->owner = THIS_MODULE;
3844
3845         pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3846         if (pe == NULL) {
3847                 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3848                        "procfs entry.\n", PGCTRL);
3849                 proc_net_remove(PG_PROC_DIR);
3850                 return -EINVAL;
3851         }
3852
3853         pe->proc_fops = &pktgen_fops;
3854         pe->data = NULL;
3855
3856         /* Register us to receive netdevice events */
3857         register_netdevice_notifier(&pktgen_notifier_block);
3858
3859         for_each_online_cpu(cpu) {
3860                 int err;
3861
3862                 err = pktgen_create_thread(cpu);
3863                 if (err)
3864                         printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3865                                "thread for cpu %d (%d)\n", cpu, err);
3866         }
3867
3868         if (list_empty(&pktgen_threads)) {
3869                 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3870                        "all threads\n");
3871                 unregister_netdevice_notifier(&pktgen_notifier_block);
3872                 remove_proc_entry(PGCTRL, pg_proc_dir);
3873                 proc_net_remove(PG_PROC_DIR);
3874                 return -ENODEV;
3875         }
3876
3877         return 0;
3878 }
3879
3880 static void __exit pg_cleanup(void)
3881 {
3882         struct pktgen_thread *t;
3883         struct list_head *q, *n;
3884         wait_queue_head_t queue;
3885         init_waitqueue_head(&queue);
3886
3887         /* Stop all interfaces & threads */
3888
3889         list_for_each_safe(q, n, &pktgen_threads) {
3890                 t = list_entry(q, struct pktgen_thread, th_list);
3891                 kthread_stop(t->tsk);
3892                 kfree(t);
3893         }
3894
3895         /* Un-register us from receiving netdevice events */
3896         unregister_netdevice_notifier(&pktgen_notifier_block);
3897
3898         /* Clean up proc file system */
3899         remove_proc_entry(PGCTRL, pg_proc_dir);
3900         proc_net_remove(PG_PROC_DIR);
3901 }
3902
3903 module_init(pg_init);
3904 module_exit(pg_cleanup);
3905
3906 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3907 MODULE_DESCRIPTION("Packet Generator tool");
3908 MODULE_LICENSE("GPL");
3909 module_param(pg_count_d, int, 0);
3910 module_param(pg_delay_d, int, 0);
3911 module_param(pg_clone_skb_d, int, 0);
3912 module_param(debug, int, 0);