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