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