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