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