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