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