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