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