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