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