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