xref: /openbmc/linux/net/core/pktgen.c (revision 34f5c1c5)
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.73"
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, "udp_src_min")) {
984 		len = num_arg(&user_buffer[i], 10, &value);
985 		if (len < 0)
986 			return len;
987 
988 		i += len;
989 		if (value != pkt_dev->udp_src_min) {
990 			pkt_dev->udp_src_min = value;
991 			pkt_dev->cur_udp_src = value;
992 		}
993 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
994 		return count;
995 	}
996 	if (!strcmp(name, "udp_dst_min")) {
997 		len = num_arg(&user_buffer[i], 10, &value);
998 		if (len < 0)
999 			return len;
1000 
1001 		i += len;
1002 		if (value != pkt_dev->udp_dst_min) {
1003 			pkt_dev->udp_dst_min = value;
1004 			pkt_dev->cur_udp_dst = value;
1005 		}
1006 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1007 		return count;
1008 	}
1009 	if (!strcmp(name, "udp_src_max")) {
1010 		len = num_arg(&user_buffer[i], 10, &value);
1011 		if (len < 0)
1012 			return len;
1013 
1014 		i += len;
1015 		if (value != pkt_dev->udp_src_max) {
1016 			pkt_dev->udp_src_max = value;
1017 			pkt_dev->cur_udp_src = value;
1018 		}
1019 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1020 		return count;
1021 	}
1022 	if (!strcmp(name, "udp_dst_max")) {
1023 		len = num_arg(&user_buffer[i], 10, &value);
1024 		if (len < 0)
1025 			return len;
1026 
1027 		i += len;
1028 		if (value != pkt_dev->udp_dst_max) {
1029 			pkt_dev->udp_dst_max = value;
1030 			pkt_dev->cur_udp_dst = value;
1031 		}
1032 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1033 		return count;
1034 	}
1035 	if (!strcmp(name, "clone_skb")) {
1036 		len = num_arg(&user_buffer[i], 10, &value);
1037 		if (len < 0)
1038 			return len;
1039 
1040 		i += len;
1041 		pkt_dev->clone_skb = value;
1042 
1043 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1044 		return count;
1045 	}
1046 	if (!strcmp(name, "count")) {
1047 		len = num_arg(&user_buffer[i], 10, &value);
1048 		if (len < 0)
1049 			return len;
1050 
1051 		i += len;
1052 		pkt_dev->count = value;
1053 		sprintf(pg_result, "OK: count=%llu",
1054 			(unsigned long long)pkt_dev->count);
1055 		return count;
1056 	}
1057 	if (!strcmp(name, "src_mac_count")) {
1058 		len = num_arg(&user_buffer[i], 10, &value);
1059 		if (len < 0)
1060 			return len;
1061 
1062 		i += len;
1063 		if (pkt_dev->src_mac_count != value) {
1064 			pkt_dev->src_mac_count = value;
1065 			pkt_dev->cur_src_mac_offset = 0;
1066 		}
1067 		sprintf(pg_result, "OK: src_mac_count=%d",
1068 			pkt_dev->src_mac_count);
1069 		return count;
1070 	}
1071 	if (!strcmp(name, "dst_mac_count")) {
1072 		len = num_arg(&user_buffer[i], 10, &value);
1073 		if (len < 0)
1074 			return len;
1075 
1076 		i += len;
1077 		if (pkt_dev->dst_mac_count != value) {
1078 			pkt_dev->dst_mac_count = value;
1079 			pkt_dev->cur_dst_mac_offset = 0;
1080 		}
1081 		sprintf(pg_result, "OK: dst_mac_count=%d",
1082 			pkt_dev->dst_mac_count);
1083 		return count;
1084 	}
1085 	if (!strcmp(name, "node")) {
1086 		len = num_arg(&user_buffer[i], 10, &value);
1087 		if (len < 0)
1088 			return len;
1089 
1090 		i += len;
1091 
1092 		if (node_possible(value)) {
1093 			pkt_dev->node = value;
1094 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1095 		}
1096 		else
1097 			sprintf(pg_result, "ERROR: node not possible");
1098 		return count;
1099 	}
1100 	if (!strcmp(name, "flag")) {
1101 		char f[32];
1102 		memset(f, 0, 32);
1103 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
1104 		if (len < 0)
1105 			return len;
1106 
1107 		if (copy_from_user(f, &user_buffer[i], len))
1108 			return -EFAULT;
1109 		i += len;
1110 		if (strcmp(f, "IPSRC_RND") == 0)
1111 			pkt_dev->flags |= F_IPSRC_RND;
1112 
1113 		else if (strcmp(f, "!IPSRC_RND") == 0)
1114 			pkt_dev->flags &= ~F_IPSRC_RND;
1115 
1116 		else if (strcmp(f, "TXSIZE_RND") == 0)
1117 			pkt_dev->flags |= F_TXSIZE_RND;
1118 
1119 		else if (strcmp(f, "!TXSIZE_RND") == 0)
1120 			pkt_dev->flags &= ~F_TXSIZE_RND;
1121 
1122 		else if (strcmp(f, "IPDST_RND") == 0)
1123 			pkt_dev->flags |= F_IPDST_RND;
1124 
1125 		else if (strcmp(f, "!IPDST_RND") == 0)
1126 			pkt_dev->flags &= ~F_IPDST_RND;
1127 
1128 		else if (strcmp(f, "UDPSRC_RND") == 0)
1129 			pkt_dev->flags |= F_UDPSRC_RND;
1130 
1131 		else if (strcmp(f, "!UDPSRC_RND") == 0)
1132 			pkt_dev->flags &= ~F_UDPSRC_RND;
1133 
1134 		else if (strcmp(f, "UDPDST_RND") == 0)
1135 			pkt_dev->flags |= F_UDPDST_RND;
1136 
1137 		else if (strcmp(f, "!UDPDST_RND") == 0)
1138 			pkt_dev->flags &= ~F_UDPDST_RND;
1139 
1140 		else if (strcmp(f, "MACSRC_RND") == 0)
1141 			pkt_dev->flags |= F_MACSRC_RND;
1142 
1143 		else if (strcmp(f, "!MACSRC_RND") == 0)
1144 			pkt_dev->flags &= ~F_MACSRC_RND;
1145 
1146 		else if (strcmp(f, "MACDST_RND") == 0)
1147 			pkt_dev->flags |= F_MACDST_RND;
1148 
1149 		else if (strcmp(f, "!MACDST_RND") == 0)
1150 			pkt_dev->flags &= ~F_MACDST_RND;
1151 
1152 		else if (strcmp(f, "MPLS_RND") == 0)
1153 			pkt_dev->flags |= F_MPLS_RND;
1154 
1155 		else if (strcmp(f, "!MPLS_RND") == 0)
1156 			pkt_dev->flags &= ~F_MPLS_RND;
1157 
1158 		else if (strcmp(f, "VID_RND") == 0)
1159 			pkt_dev->flags |= F_VID_RND;
1160 
1161 		else if (strcmp(f, "!VID_RND") == 0)
1162 			pkt_dev->flags &= ~F_VID_RND;
1163 
1164 		else if (strcmp(f, "SVID_RND") == 0)
1165 			pkt_dev->flags |= F_SVID_RND;
1166 
1167 		else if (strcmp(f, "!SVID_RND") == 0)
1168 			pkt_dev->flags &= ~F_SVID_RND;
1169 
1170 		else if (strcmp(f, "FLOW_SEQ") == 0)
1171 			pkt_dev->flags |= F_FLOW_SEQ;
1172 
1173 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1174 			pkt_dev->flags |= F_QUEUE_MAP_RND;
1175 
1176 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1177 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1178 
1179 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1180 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1181 
1182 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1183 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1184 #ifdef CONFIG_XFRM
1185 		else if (strcmp(f, "IPSEC") == 0)
1186 			pkt_dev->flags |= F_IPSEC_ON;
1187 #endif
1188 
1189 		else if (strcmp(f, "!IPV6") == 0)
1190 			pkt_dev->flags &= ~F_IPV6;
1191 
1192 		else if (strcmp(f, "NODE_ALLOC") == 0)
1193 			pkt_dev->flags |= F_NODE;
1194 
1195 		else if (strcmp(f, "!NODE_ALLOC") == 0)
1196 			pkt_dev->flags &= ~F_NODE;
1197 
1198 		else {
1199 			sprintf(pg_result,
1200 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1201 				f,
1202 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1203 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
1204 			return count;
1205 		}
1206 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1207 		return count;
1208 	}
1209 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1210 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1211 		if (len < 0)
1212 			return len;
1213 
1214 		if (copy_from_user(buf, &user_buffer[i], len))
1215 			return -EFAULT;
1216 		buf[len] = 0;
1217 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
1218 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1219 			strncpy(pkt_dev->dst_min, buf, len);
1220 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1221 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
1222 		}
1223 		if (debug)
1224 			printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1225 			       pkt_dev->dst_min);
1226 		i += len;
1227 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1228 		return count;
1229 	}
1230 	if (!strcmp(name, "dst_max")) {
1231 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1232 		if (len < 0)
1233 			return len;
1234 
1235 
1236 		if (copy_from_user(buf, &user_buffer[i], len))
1237 			return -EFAULT;
1238 
1239 		buf[len] = 0;
1240 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
1241 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1242 			strncpy(pkt_dev->dst_max, buf, len);
1243 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1244 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
1245 		}
1246 		if (debug)
1247 			printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1248 			       pkt_dev->dst_max);
1249 		i += len;
1250 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1251 		return count;
1252 	}
1253 	if (!strcmp(name, "dst6")) {
1254 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1255 		if (len < 0)
1256 			return len;
1257 
1258 		pkt_dev->flags |= F_IPV6;
1259 
1260 		if (copy_from_user(buf, &user_buffer[i], len))
1261 			return -EFAULT;
1262 		buf[len] = 0;
1263 
1264 		scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1265 		fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1266 
1267 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1268 
1269 		if (debug)
1270 			printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1271 
1272 		i += len;
1273 		sprintf(pg_result, "OK: dst6=%s", buf);
1274 		return count;
1275 	}
1276 	if (!strcmp(name, "dst6_min")) {
1277 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1278 		if (len < 0)
1279 			return len;
1280 
1281 		pkt_dev->flags |= F_IPV6;
1282 
1283 		if (copy_from_user(buf, &user_buffer[i], len))
1284 			return -EFAULT;
1285 		buf[len] = 0;
1286 
1287 		scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1288 		fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1289 
1290 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1291 			       &pkt_dev->min_in6_daddr);
1292 		if (debug)
1293 			printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1294 
1295 		i += len;
1296 		sprintf(pg_result, "OK: dst6_min=%s", buf);
1297 		return count;
1298 	}
1299 	if (!strcmp(name, "dst6_max")) {
1300 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1301 		if (len < 0)
1302 			return len;
1303 
1304 		pkt_dev->flags |= F_IPV6;
1305 
1306 		if (copy_from_user(buf, &user_buffer[i], len))
1307 			return -EFAULT;
1308 		buf[len] = 0;
1309 
1310 		scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1311 		fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1312 
1313 		if (debug)
1314 			printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1315 
1316 		i += len;
1317 		sprintf(pg_result, "OK: dst6_max=%s", buf);
1318 		return count;
1319 	}
1320 	if (!strcmp(name, "src6")) {
1321 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1322 		if (len < 0)
1323 			return len;
1324 
1325 		pkt_dev->flags |= F_IPV6;
1326 
1327 		if (copy_from_user(buf, &user_buffer[i], len))
1328 			return -EFAULT;
1329 		buf[len] = 0;
1330 
1331 		scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1332 		fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1333 
1334 		ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1335 
1336 		if (debug)
1337 			printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1338 
1339 		i += len;
1340 		sprintf(pg_result, "OK: src6=%s", buf);
1341 		return count;
1342 	}
1343 	if (!strcmp(name, "src_min")) {
1344 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1345 		if (len < 0)
1346 			return len;
1347 
1348 		if (copy_from_user(buf, &user_buffer[i], len))
1349 			return -EFAULT;
1350 		buf[len] = 0;
1351 		if (strcmp(buf, pkt_dev->src_min) != 0) {
1352 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1353 			strncpy(pkt_dev->src_min, buf, len);
1354 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1355 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
1356 		}
1357 		if (debug)
1358 			printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1359 			       pkt_dev->src_min);
1360 		i += len;
1361 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1362 		return count;
1363 	}
1364 	if (!strcmp(name, "src_max")) {
1365 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1366 		if (len < 0)
1367 			return len;
1368 
1369 		if (copy_from_user(buf, &user_buffer[i], len))
1370 			return -EFAULT;
1371 		buf[len] = 0;
1372 		if (strcmp(buf, pkt_dev->src_max) != 0) {
1373 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1374 			strncpy(pkt_dev->src_max, buf, len);
1375 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1376 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
1377 		}
1378 		if (debug)
1379 			printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1380 			       pkt_dev->src_max);
1381 		i += len;
1382 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1383 		return count;
1384 	}
1385 	if (!strcmp(name, "dst_mac")) {
1386 		char *v = valstr;
1387 		unsigned char old_dmac[ETH_ALEN];
1388 		unsigned char *m = pkt_dev->dst_mac;
1389 		memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1390 
1391 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1392 		if (len < 0)
1393 			return len;
1394 
1395 		memset(valstr, 0, sizeof(valstr));
1396 		if (copy_from_user(valstr, &user_buffer[i], len))
1397 			return -EFAULT;
1398 		i += len;
1399 
1400 		for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1401 			if (*v >= '0' && *v <= '9') {
1402 				*m *= 16;
1403 				*m += *v - '0';
1404 			}
1405 			if (*v >= 'A' && *v <= 'F') {
1406 				*m *= 16;
1407 				*m += *v - 'A' + 10;
1408 			}
1409 			if (*v >= 'a' && *v <= 'f') {
1410 				*m *= 16;
1411 				*m += *v - 'a' + 10;
1412 			}
1413 			if (*v == ':') {
1414 				m++;
1415 				*m = 0;
1416 			}
1417 		}
1418 
1419 		/* Set up Dest MAC */
1420 		if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1421 			memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1422 
1423 		sprintf(pg_result, "OK: dstmac");
1424 		return count;
1425 	}
1426 	if (!strcmp(name, "src_mac")) {
1427 		char *v = valstr;
1428 		unsigned char old_smac[ETH_ALEN];
1429 		unsigned char *m = pkt_dev->src_mac;
1430 
1431 		memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1432 
1433 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1434 		if (len < 0)
1435 			return len;
1436 
1437 		memset(valstr, 0, sizeof(valstr));
1438 		if (copy_from_user(valstr, &user_buffer[i], len))
1439 			return -EFAULT;
1440 		i += len;
1441 
1442 		for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1443 			if (*v >= '0' && *v <= '9') {
1444 				*m *= 16;
1445 				*m += *v - '0';
1446 			}
1447 			if (*v >= 'A' && *v <= 'F') {
1448 				*m *= 16;
1449 				*m += *v - 'A' + 10;
1450 			}
1451 			if (*v >= 'a' && *v <= 'f') {
1452 				*m *= 16;
1453 				*m += *v - 'a' + 10;
1454 			}
1455 			if (*v == ':') {
1456 				m++;
1457 				*m = 0;
1458 			}
1459 		}
1460 
1461 		/* Set up Src MAC */
1462 		if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1463 			memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1464 
1465 		sprintf(pg_result, "OK: srcmac");
1466 		return count;
1467 	}
1468 
1469 	if (!strcmp(name, "clear_counters")) {
1470 		pktgen_clear_counters(pkt_dev);
1471 		sprintf(pg_result, "OK: Clearing counters.\n");
1472 		return count;
1473 	}
1474 
1475 	if (!strcmp(name, "flows")) {
1476 		len = num_arg(&user_buffer[i], 10, &value);
1477 		if (len < 0)
1478 			return len;
1479 
1480 		i += len;
1481 		if (value > MAX_CFLOWS)
1482 			value = MAX_CFLOWS;
1483 
1484 		pkt_dev->cflows = value;
1485 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1486 		return count;
1487 	}
1488 
1489 	if (!strcmp(name, "flowlen")) {
1490 		len = num_arg(&user_buffer[i], 10, &value);
1491 		if (len < 0)
1492 			return len;
1493 
1494 		i += len;
1495 		pkt_dev->lflow = value;
1496 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1497 		return count;
1498 	}
1499 
1500 	if (!strcmp(name, "queue_map_min")) {
1501 		len = num_arg(&user_buffer[i], 5, &value);
1502 		if (len < 0)
1503 			return len;
1504 
1505 		i += len;
1506 		pkt_dev->queue_map_min = value;
1507 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1508 		return count;
1509 	}
1510 
1511 	if (!strcmp(name, "queue_map_max")) {
1512 		len = num_arg(&user_buffer[i], 5, &value);
1513 		if (len < 0)
1514 			return len;
1515 
1516 		i += len;
1517 		pkt_dev->queue_map_max = value;
1518 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1519 		return count;
1520 	}
1521 
1522 	if (!strcmp(name, "mpls")) {
1523 		unsigned n, cnt;
1524 
1525 		len = get_labels(&user_buffer[i], pkt_dev);
1526 		if (len < 0)
1527 			return len;
1528 		i += len;
1529 		cnt = sprintf(pg_result, "OK: mpls=");
1530 		for (n = 0; n < pkt_dev->nr_labels; n++)
1531 			cnt += sprintf(pg_result + cnt,
1532 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1533 				       n == pkt_dev->nr_labels-1 ? "" : ",");
1534 
1535 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1536 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1537 			pkt_dev->svlan_id = 0xffff;
1538 
1539 			if (debug)
1540 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1541 		}
1542 		return count;
1543 	}
1544 
1545 	if (!strcmp(name, "vlan_id")) {
1546 		len = num_arg(&user_buffer[i], 4, &value);
1547 		if (len < 0)
1548 			return len;
1549 
1550 		i += len;
1551 		if (value <= 4095) {
1552 			pkt_dev->vlan_id = value;  /* turn on VLAN */
1553 
1554 			if (debug)
1555 				printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1556 
1557 			if (debug && pkt_dev->nr_labels)
1558 				printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1559 
1560 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
1561 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1562 		} else {
1563 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1564 			pkt_dev->svlan_id = 0xffff;
1565 
1566 			if (debug)
1567 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1568 		}
1569 		return count;
1570 	}
1571 
1572 	if (!strcmp(name, "vlan_p")) {
1573 		len = num_arg(&user_buffer[i], 1, &value);
1574 		if (len < 0)
1575 			return len;
1576 
1577 		i += len;
1578 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1579 			pkt_dev->vlan_p = value;
1580 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1581 		} else {
1582 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1583 		}
1584 		return count;
1585 	}
1586 
1587 	if (!strcmp(name, "vlan_cfi")) {
1588 		len = num_arg(&user_buffer[i], 1, &value);
1589 		if (len < 0)
1590 			return len;
1591 
1592 		i += len;
1593 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1594 			pkt_dev->vlan_cfi = value;
1595 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1596 		} else {
1597 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1598 		}
1599 		return count;
1600 	}
1601 
1602 	if (!strcmp(name, "svlan_id")) {
1603 		len = num_arg(&user_buffer[i], 4, &value);
1604 		if (len < 0)
1605 			return len;
1606 
1607 		i += len;
1608 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1609 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
1610 
1611 			if (debug)
1612 				printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1613 
1614 			if (debug && pkt_dev->nr_labels)
1615 				printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1616 
1617 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
1618 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1619 		} else {
1620 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1621 			pkt_dev->svlan_id = 0xffff;
1622 
1623 			if (debug)
1624 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1625 		}
1626 		return count;
1627 	}
1628 
1629 	if (!strcmp(name, "svlan_p")) {
1630 		len = num_arg(&user_buffer[i], 1, &value);
1631 		if (len < 0)
1632 			return len;
1633 
1634 		i += len;
1635 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1636 			pkt_dev->svlan_p = value;
1637 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1638 		} else {
1639 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1640 		}
1641 		return count;
1642 	}
1643 
1644 	if (!strcmp(name, "svlan_cfi")) {
1645 		len = num_arg(&user_buffer[i], 1, &value);
1646 		if (len < 0)
1647 			return len;
1648 
1649 		i += len;
1650 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1651 			pkt_dev->svlan_cfi = value;
1652 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1653 		} else {
1654 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1655 		}
1656 		return count;
1657 	}
1658 
1659 	if (!strcmp(name, "tos")) {
1660 		__u32 tmp_value = 0;
1661 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1662 		if (len < 0)
1663 			return len;
1664 
1665 		i += len;
1666 		if (len == 2) {
1667 			pkt_dev->tos = tmp_value;
1668 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1669 		} else {
1670 			sprintf(pg_result, "ERROR: tos must be 00-ff");
1671 		}
1672 		return count;
1673 	}
1674 
1675 	if (!strcmp(name, "traffic_class")) {
1676 		__u32 tmp_value = 0;
1677 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1678 		if (len < 0)
1679 			return len;
1680 
1681 		i += len;
1682 		if (len == 2) {
1683 			pkt_dev->traffic_class = tmp_value;
1684 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1685 		} else {
1686 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1687 		}
1688 		return count;
1689 	}
1690 
1691 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1692 	return -EINVAL;
1693 }
1694 
1695 static int pktgen_if_open(struct inode *inode, struct file *file)
1696 {
1697 	return single_open(file, pktgen_if_show, PDE(inode)->data);
1698 }
1699 
1700 static const struct file_operations pktgen_if_fops = {
1701 	.owner   = THIS_MODULE,
1702 	.open    = pktgen_if_open,
1703 	.read    = seq_read,
1704 	.llseek  = seq_lseek,
1705 	.write   = pktgen_if_write,
1706 	.release = single_release,
1707 };
1708 
1709 static int pktgen_thread_show(struct seq_file *seq, void *v)
1710 {
1711 	struct pktgen_thread *t = seq->private;
1712 	const struct pktgen_dev *pkt_dev;
1713 
1714 	BUG_ON(!t);
1715 
1716 	seq_printf(seq, "Running: ");
1717 
1718 	if_lock(t);
1719 	list_for_each_entry(pkt_dev, &t->if_list, list)
1720 		if (pkt_dev->running)
1721 			seq_printf(seq, "%s ", pkt_dev->odevname);
1722 
1723 	seq_printf(seq, "\nStopped: ");
1724 
1725 	list_for_each_entry(pkt_dev, &t->if_list, list)
1726 		if (!pkt_dev->running)
1727 			seq_printf(seq, "%s ", pkt_dev->odevname);
1728 
1729 	if (t->result[0])
1730 		seq_printf(seq, "\nResult: %s\n", t->result);
1731 	else
1732 		seq_printf(seq, "\nResult: NA\n");
1733 
1734 	if_unlock(t);
1735 
1736 	return 0;
1737 }
1738 
1739 static ssize_t pktgen_thread_write(struct file *file,
1740 				   const char __user * user_buffer,
1741 				   size_t count, loff_t * offset)
1742 {
1743 	struct seq_file *seq = (struct seq_file *)file->private_data;
1744 	struct pktgen_thread *t = seq->private;
1745 	int i = 0, max, len, ret;
1746 	char name[40];
1747 	char *pg_result;
1748 
1749 	if (count < 1) {
1750 		//      sprintf(pg_result, "Wrong command format");
1751 		return -EINVAL;
1752 	}
1753 
1754 	max = count - i;
1755 	len = count_trail_chars(&user_buffer[i], max);
1756 	if (len < 0)
1757 		return len;
1758 
1759 	i += len;
1760 
1761 	/* Read variable name */
1762 
1763 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
1764 	if (len < 0)
1765 		return len;
1766 
1767 	memset(name, 0, sizeof(name));
1768 	if (copy_from_user(name, &user_buffer[i], len))
1769 		return -EFAULT;
1770 	i += len;
1771 
1772 	max = count - i;
1773 	len = count_trail_chars(&user_buffer[i], max);
1774 	if (len < 0)
1775 		return len;
1776 
1777 	i += len;
1778 
1779 	if (debug)
1780 		printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1781 		       name, (unsigned long)count);
1782 
1783 	if (!t) {
1784 		printk(KERN_ERR "pktgen: ERROR: No thread\n");
1785 		ret = -EINVAL;
1786 		goto out;
1787 	}
1788 
1789 	pg_result = &(t->result[0]);
1790 
1791 	if (!strcmp(name, "add_device")) {
1792 		char f[32];
1793 		memset(f, 0, 32);
1794 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
1795 		if (len < 0) {
1796 			ret = len;
1797 			goto out;
1798 		}
1799 		if (copy_from_user(f, &user_buffer[i], len))
1800 			return -EFAULT;
1801 		i += len;
1802 		mutex_lock(&pktgen_thread_lock);
1803 		pktgen_add_device(t, f);
1804 		mutex_unlock(&pktgen_thread_lock);
1805 		ret = count;
1806 		sprintf(pg_result, "OK: add_device=%s", f);
1807 		goto out;
1808 	}
1809 
1810 	if (!strcmp(name, "rem_device_all")) {
1811 		mutex_lock(&pktgen_thread_lock);
1812 		t->control |= T_REMDEVALL;
1813 		mutex_unlock(&pktgen_thread_lock);
1814 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
1815 		ret = count;
1816 		sprintf(pg_result, "OK: rem_device_all");
1817 		goto out;
1818 	}
1819 
1820 	if (!strcmp(name, "max_before_softirq")) {
1821 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1822 		ret = count;
1823 		goto out;
1824 	}
1825 
1826 	ret = -EINVAL;
1827 out:
1828 	return ret;
1829 }
1830 
1831 static int pktgen_thread_open(struct inode *inode, struct file *file)
1832 {
1833 	return single_open(file, pktgen_thread_show, PDE(inode)->data);
1834 }
1835 
1836 static const struct file_operations pktgen_thread_fops = {
1837 	.owner   = THIS_MODULE,
1838 	.open    = pktgen_thread_open,
1839 	.read    = seq_read,
1840 	.llseek  = seq_lseek,
1841 	.write   = pktgen_thread_write,
1842 	.release = single_release,
1843 };
1844 
1845 /* Think find or remove for NN */
1846 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1847 {
1848 	struct pktgen_thread *t;
1849 	struct pktgen_dev *pkt_dev = NULL;
1850 	bool exact = (remove == FIND);
1851 
1852 	list_for_each_entry(t, &pktgen_threads, th_list) {
1853 		pkt_dev = pktgen_find_dev(t, ifname, exact);
1854 		if (pkt_dev) {
1855 			if (remove) {
1856 				if_lock(t);
1857 				pkt_dev->removal_mark = 1;
1858 				t->control |= T_REMDEV;
1859 				if_unlock(t);
1860 			}
1861 			break;
1862 		}
1863 	}
1864 	return pkt_dev;
1865 }
1866 
1867 /*
1868  * mark a device for removal
1869  */
1870 static void pktgen_mark_device(const char *ifname)
1871 {
1872 	struct pktgen_dev *pkt_dev = NULL;
1873 	const int max_tries = 10, msec_per_try = 125;
1874 	int i = 0;
1875 
1876 	mutex_lock(&pktgen_thread_lock);
1877 	pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1878 
1879 	while (1) {
1880 
1881 		pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1882 		if (pkt_dev == NULL)
1883 			break;	/* success */
1884 
1885 		mutex_unlock(&pktgen_thread_lock);
1886 		pr_debug("pktgen: pktgen_mark_device waiting for %s "
1887 				"to disappear....\n", ifname);
1888 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1889 		mutex_lock(&pktgen_thread_lock);
1890 
1891 		if (++i >= max_tries) {
1892 			printk(KERN_ERR "pktgen_mark_device: timed out after "
1893 			       "waiting %d msec for device %s to be removed\n",
1894 			       msec_per_try * i, ifname);
1895 			break;
1896 		}
1897 
1898 	}
1899 
1900 	mutex_unlock(&pktgen_thread_lock);
1901 }
1902 
1903 static void pktgen_change_name(struct net_device *dev)
1904 {
1905 	struct pktgen_thread *t;
1906 
1907 	list_for_each_entry(t, &pktgen_threads, th_list) {
1908 		struct pktgen_dev *pkt_dev;
1909 
1910 		list_for_each_entry(pkt_dev, &t->if_list, list) {
1911 			if (pkt_dev->odev != dev)
1912 				continue;
1913 
1914 			remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1915 
1916 			pkt_dev->entry = proc_create_data(dev->name, 0600,
1917 							  pg_proc_dir,
1918 							  &pktgen_if_fops,
1919 							  pkt_dev);
1920 			if (!pkt_dev->entry)
1921 				printk(KERN_ERR "pktgen: can't move proc "
1922 				       " entry for '%s'\n", dev->name);
1923 			break;
1924 		}
1925 	}
1926 }
1927 
1928 static int pktgen_device_event(struct notifier_block *unused,
1929 			       unsigned long event, void *ptr)
1930 {
1931 	struct net_device *dev = ptr;
1932 
1933 	if (!net_eq(dev_net(dev), &init_net))
1934 		return NOTIFY_DONE;
1935 
1936 	/* It is OK that we do not hold the group lock right now,
1937 	 * as we run under the RTNL lock.
1938 	 */
1939 
1940 	switch (event) {
1941 	case NETDEV_CHANGENAME:
1942 		pktgen_change_name(dev);
1943 		break;
1944 
1945 	case NETDEV_UNREGISTER:
1946 		pktgen_mark_device(dev->name);
1947 		break;
1948 	}
1949 
1950 	return NOTIFY_DONE;
1951 }
1952 
1953 static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1954 						 const char *ifname)
1955 {
1956 	char b[IFNAMSIZ+5];
1957 	int i = 0;
1958 
1959 	for (i = 0; ifname[i] != '@'; i++) {
1960 		if (i == IFNAMSIZ)
1961 			break;
1962 
1963 		b[i] = ifname[i];
1964 	}
1965 	b[i] = 0;
1966 
1967 	return dev_get_by_name(&init_net, b);
1968 }
1969 
1970 
1971 /* Associate pktgen_dev with a device. */
1972 
1973 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1974 {
1975 	struct net_device *odev;
1976 	int err;
1977 
1978 	/* Clean old setups */
1979 	if (pkt_dev->odev) {
1980 		dev_put(pkt_dev->odev);
1981 		pkt_dev->odev = NULL;
1982 	}
1983 
1984 	odev = pktgen_dev_get_by_name(pkt_dev, ifname);
1985 	if (!odev) {
1986 		printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1987 		return -ENODEV;
1988 	}
1989 
1990 	if (odev->type != ARPHRD_ETHER) {
1991 		printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1992 		err = -EINVAL;
1993 	} else if (!netif_running(odev)) {
1994 		printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1995 		err = -ENETDOWN;
1996 	} else {
1997 		pkt_dev->odev = odev;
1998 		return 0;
1999 	}
2000 
2001 	dev_put(odev);
2002 	return err;
2003 }
2004 
2005 /* Read pkt_dev from the interface and set up internal pktgen_dev
2006  * structure to have the right information to create/send packets
2007  */
2008 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2009 {
2010 	int ntxq;
2011 
2012 	if (!pkt_dev->odev) {
2013 		printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
2014 		       "setup_inject.\n");
2015 		sprintf(pkt_dev->result,
2016 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2017 		return;
2018 	}
2019 
2020 	/* make sure that we don't pick a non-existing transmit queue */
2021 	ntxq = pkt_dev->odev->real_num_tx_queues;
2022 
2023 	if (ntxq <= pkt_dev->queue_map_min) {
2024 		printk(KERN_WARNING "pktgen: WARNING: Requested "
2025 		       "queue_map_min (zero-based) (%d) exceeds valid range "
2026 		       "[0 - %d] for (%d) queues on %s, resetting\n",
2027 		       pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2028 		       pkt_dev->odevname);
2029 		pkt_dev->queue_map_min = ntxq - 1;
2030 	}
2031 	if (pkt_dev->queue_map_max >= ntxq) {
2032 		printk(KERN_WARNING "pktgen: WARNING: Requested "
2033 		       "queue_map_max (zero-based) (%d) exceeds valid range "
2034 		       "[0 - %d] for (%d) queues on %s, resetting\n",
2035 		       pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2036 		       pkt_dev->odevname);
2037 		pkt_dev->queue_map_max = ntxq - 1;
2038 	}
2039 
2040 	/* Default to the interface's mac if not explicitly set. */
2041 
2042 	if (is_zero_ether_addr(pkt_dev->src_mac))
2043 		memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2044 
2045 	/* Set up Dest MAC */
2046 	memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2047 
2048 	/* Set up pkt size */
2049 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2050 
2051 	if (pkt_dev->flags & F_IPV6) {
2052 		/*
2053 		 * Skip this automatic address setting until locks or functions
2054 		 * gets exported
2055 		 */
2056 
2057 #ifdef NOTNOW
2058 		int i, set = 0, err = 1;
2059 		struct inet6_dev *idev;
2060 
2061 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
2062 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2063 				set = 1;
2064 				break;
2065 			}
2066 
2067 		if (!set) {
2068 
2069 			/*
2070 			 * Use linklevel address if unconfigured.
2071 			 *
2072 			 * use ipv6_get_lladdr if/when it's get exported
2073 			 */
2074 
2075 			rcu_read_lock();
2076 			idev = __in6_dev_get(pkt_dev->odev);
2077 			if (idev) {
2078 				struct inet6_ifaddr *ifp;
2079 
2080 				read_lock_bh(&idev->lock);
2081 				for (ifp = idev->addr_list; ifp;
2082 				     ifp = ifp->if_next) {
2083 					if (ifp->scope == IFA_LINK &&
2084 					    !(ifp->flags & IFA_F_TENTATIVE)) {
2085 						ipv6_addr_copy(&pkt_dev->
2086 							       cur_in6_saddr,
2087 							       &ifp->addr);
2088 						err = 0;
2089 						break;
2090 					}
2091 				}
2092 				read_unlock_bh(&idev->lock);
2093 			}
2094 			rcu_read_unlock();
2095 			if (err)
2096 				printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2097 				       "address not availble.\n");
2098 		}
2099 #endif
2100 	} else {
2101 		pkt_dev->saddr_min = 0;
2102 		pkt_dev->saddr_max = 0;
2103 		if (strlen(pkt_dev->src_min) == 0) {
2104 
2105 			struct in_device *in_dev;
2106 
2107 			rcu_read_lock();
2108 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
2109 			if (in_dev) {
2110 				if (in_dev->ifa_list) {
2111 					pkt_dev->saddr_min =
2112 					    in_dev->ifa_list->ifa_address;
2113 					pkt_dev->saddr_max = pkt_dev->saddr_min;
2114 				}
2115 			}
2116 			rcu_read_unlock();
2117 		} else {
2118 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2119 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2120 		}
2121 
2122 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2123 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2124 	}
2125 	/* Initialize current values. */
2126 	pkt_dev->cur_dst_mac_offset = 0;
2127 	pkt_dev->cur_src_mac_offset = 0;
2128 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
2129 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
2130 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2131 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2132 	pkt_dev->nflows = 0;
2133 }
2134 
2135 
2136 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2137 {
2138 	ktime_t start_time, end_time;
2139 	s64 remaining;
2140 	struct hrtimer_sleeper t;
2141 
2142 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2143 	hrtimer_set_expires(&t.timer, spin_until);
2144 
2145 	remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
2146 	if (remaining <= 0) {
2147 		pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2148 		return;
2149 	}
2150 
2151 	start_time = ktime_now();
2152 	if (remaining < 100)
2153 		udelay(remaining); 	/* really small just spin */
2154 	else {
2155 		/* see do_nanosleep */
2156 		hrtimer_init_sleeper(&t, current);
2157 		do {
2158 			set_current_state(TASK_INTERRUPTIBLE);
2159 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2160 			if (!hrtimer_active(&t.timer))
2161 				t.task = NULL;
2162 
2163 			if (likely(t.task))
2164 				schedule();
2165 
2166 			hrtimer_cancel(&t.timer);
2167 		} while (t.task && pkt_dev->running && !signal_pending(current));
2168 		__set_current_state(TASK_RUNNING);
2169 	}
2170 	end_time = ktime_now();
2171 
2172 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2173 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2174 }
2175 
2176 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2177 {
2178 	pkt_dev->pkt_overhead = 0;
2179 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2180 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2181 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2182 }
2183 
2184 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2185 {
2186 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2187 }
2188 
2189 static inline int f_pick(struct pktgen_dev *pkt_dev)
2190 {
2191 	int flow = pkt_dev->curfl;
2192 
2193 	if (pkt_dev->flags & F_FLOW_SEQ) {
2194 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2195 			/* reset time */
2196 			pkt_dev->flows[flow].count = 0;
2197 			pkt_dev->flows[flow].flags = 0;
2198 			pkt_dev->curfl += 1;
2199 			if (pkt_dev->curfl >= pkt_dev->cflows)
2200 				pkt_dev->curfl = 0; /*reset */
2201 		}
2202 	} else {
2203 		flow = random32() % pkt_dev->cflows;
2204 		pkt_dev->curfl = flow;
2205 
2206 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2207 			pkt_dev->flows[flow].count = 0;
2208 			pkt_dev->flows[flow].flags = 0;
2209 		}
2210 	}
2211 
2212 	return pkt_dev->curfl;
2213 }
2214 
2215 
2216 #ifdef CONFIG_XFRM
2217 /* If there was already an IPSEC SA, we keep it as is, else
2218  * we go look for it ...
2219 */
2220 #define DUMMY_MARK 0
2221 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2222 {
2223 	struct xfrm_state *x = pkt_dev->flows[flow].x;
2224 	if (!x) {
2225 		/*slow path: we dont already have xfrm_state*/
2226 		x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
2227 					(xfrm_address_t *)&pkt_dev->cur_daddr,
2228 					(xfrm_address_t *)&pkt_dev->cur_saddr,
2229 					AF_INET,
2230 					pkt_dev->ipsmode,
2231 					pkt_dev->ipsproto, 0);
2232 		if (x) {
2233 			pkt_dev->flows[flow].x = x;
2234 			set_pkt_overhead(pkt_dev);
2235 			pkt_dev->pkt_overhead += x->props.header_len;
2236 		}
2237 
2238 	}
2239 }
2240 #endif
2241 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2242 {
2243 
2244 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2245 		pkt_dev->cur_queue_map = smp_processor_id();
2246 
2247 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2248 		__u16 t;
2249 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2250 			t = random32() %
2251 				(pkt_dev->queue_map_max -
2252 				 pkt_dev->queue_map_min + 1)
2253 				+ pkt_dev->queue_map_min;
2254 		} else {
2255 			t = pkt_dev->cur_queue_map + 1;
2256 			if (t > pkt_dev->queue_map_max)
2257 				t = pkt_dev->queue_map_min;
2258 		}
2259 		pkt_dev->cur_queue_map = t;
2260 	}
2261 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2262 }
2263 
2264 /* Increment/randomize headers according to flags and current values
2265  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2266  */
2267 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2268 {
2269 	__u32 imn;
2270 	__u32 imx;
2271 	int flow = 0;
2272 
2273 	if (pkt_dev->cflows)
2274 		flow = f_pick(pkt_dev);
2275 
2276 	/*  Deal with source MAC */
2277 	if (pkt_dev->src_mac_count > 1) {
2278 		__u32 mc;
2279 		__u32 tmp;
2280 
2281 		if (pkt_dev->flags & F_MACSRC_RND)
2282 			mc = random32() % pkt_dev->src_mac_count;
2283 		else {
2284 			mc = pkt_dev->cur_src_mac_offset++;
2285 			if (pkt_dev->cur_src_mac_offset >=
2286 			    pkt_dev->src_mac_count)
2287 				pkt_dev->cur_src_mac_offset = 0;
2288 		}
2289 
2290 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2291 		pkt_dev->hh[11] = tmp;
2292 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2293 		pkt_dev->hh[10] = tmp;
2294 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2295 		pkt_dev->hh[9] = tmp;
2296 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2297 		pkt_dev->hh[8] = tmp;
2298 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2299 		pkt_dev->hh[7] = tmp;
2300 	}
2301 
2302 	/*  Deal with Destination MAC */
2303 	if (pkt_dev->dst_mac_count > 1) {
2304 		__u32 mc;
2305 		__u32 tmp;
2306 
2307 		if (pkt_dev->flags & F_MACDST_RND)
2308 			mc = random32() % pkt_dev->dst_mac_count;
2309 
2310 		else {
2311 			mc = pkt_dev->cur_dst_mac_offset++;
2312 			if (pkt_dev->cur_dst_mac_offset >=
2313 			    pkt_dev->dst_mac_count) {
2314 				pkt_dev->cur_dst_mac_offset = 0;
2315 			}
2316 		}
2317 
2318 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2319 		pkt_dev->hh[5] = tmp;
2320 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2321 		pkt_dev->hh[4] = tmp;
2322 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2323 		pkt_dev->hh[3] = tmp;
2324 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2325 		pkt_dev->hh[2] = tmp;
2326 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2327 		pkt_dev->hh[1] = tmp;
2328 	}
2329 
2330 	if (pkt_dev->flags & F_MPLS_RND) {
2331 		unsigned i;
2332 		for (i = 0; i < pkt_dev->nr_labels; i++)
2333 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2334 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2335 					     ((__force __be32)random32() &
2336 						      htonl(0x000fffff));
2337 	}
2338 
2339 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2340 		pkt_dev->vlan_id = random32() & (4096-1);
2341 	}
2342 
2343 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2344 		pkt_dev->svlan_id = random32() & (4096 - 1);
2345 	}
2346 
2347 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2348 		if (pkt_dev->flags & F_UDPSRC_RND)
2349 			pkt_dev->cur_udp_src = random32() %
2350 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2351 				+ pkt_dev->udp_src_min;
2352 
2353 		else {
2354 			pkt_dev->cur_udp_src++;
2355 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2356 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2357 		}
2358 	}
2359 
2360 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2361 		if (pkt_dev->flags & F_UDPDST_RND) {
2362 			pkt_dev->cur_udp_dst = random32() %
2363 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2364 				+ pkt_dev->udp_dst_min;
2365 		} else {
2366 			pkt_dev->cur_udp_dst++;
2367 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2368 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2369 		}
2370 	}
2371 
2372 	if (!(pkt_dev->flags & F_IPV6)) {
2373 
2374 		imn = ntohl(pkt_dev->saddr_min);
2375 		imx = ntohl(pkt_dev->saddr_max);
2376 		if (imn < imx) {
2377 			__u32 t;
2378 			if (pkt_dev->flags & F_IPSRC_RND)
2379 				t = random32() % (imx - imn) + imn;
2380 			else {
2381 				t = ntohl(pkt_dev->cur_saddr);
2382 				t++;
2383 				if (t > imx)
2384 					t = imn;
2385 
2386 			}
2387 			pkt_dev->cur_saddr = htonl(t);
2388 		}
2389 
2390 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2391 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2392 		} else {
2393 			imn = ntohl(pkt_dev->daddr_min);
2394 			imx = ntohl(pkt_dev->daddr_max);
2395 			if (imn < imx) {
2396 				__u32 t;
2397 				__be32 s;
2398 				if (pkt_dev->flags & F_IPDST_RND) {
2399 
2400 					t = random32() % (imx - imn) + imn;
2401 					s = htonl(t);
2402 
2403 					while (ipv4_is_loopback(s) ||
2404 					       ipv4_is_multicast(s) ||
2405 					       ipv4_is_lbcast(s) ||
2406 					       ipv4_is_zeronet(s) ||
2407 					       ipv4_is_local_multicast(s)) {
2408 						t = random32() % (imx - imn) + imn;
2409 						s = htonl(t);
2410 					}
2411 					pkt_dev->cur_daddr = s;
2412 				} else {
2413 					t = ntohl(pkt_dev->cur_daddr);
2414 					t++;
2415 					if (t > imx) {
2416 						t = imn;
2417 					}
2418 					pkt_dev->cur_daddr = htonl(t);
2419 				}
2420 			}
2421 			if (pkt_dev->cflows) {
2422 				pkt_dev->flows[flow].flags |= F_INIT;
2423 				pkt_dev->flows[flow].cur_daddr =
2424 				    pkt_dev->cur_daddr;
2425 #ifdef CONFIG_XFRM
2426 				if (pkt_dev->flags & F_IPSEC_ON)
2427 					get_ipsec_sa(pkt_dev, flow);
2428 #endif
2429 				pkt_dev->nflows++;
2430 			}
2431 		}
2432 	} else {		/* IPV6 * */
2433 
2434 		if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2435 		    pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2436 		    pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2437 		    pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2438 		else {
2439 			int i;
2440 
2441 			/* Only random destinations yet */
2442 
2443 			for (i = 0; i < 4; i++) {
2444 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
2445 				    (((__force __be32)random32() |
2446 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
2447 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
2448 			}
2449 		}
2450 	}
2451 
2452 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2453 		__u32 t;
2454 		if (pkt_dev->flags & F_TXSIZE_RND) {
2455 			t = random32() %
2456 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2457 				+ pkt_dev->min_pkt_size;
2458 		} else {
2459 			t = pkt_dev->cur_pkt_size + 1;
2460 			if (t > pkt_dev->max_pkt_size)
2461 				t = pkt_dev->min_pkt_size;
2462 		}
2463 		pkt_dev->cur_pkt_size = t;
2464 	}
2465 
2466 	set_cur_queue_map(pkt_dev);
2467 
2468 	pkt_dev->flows[flow].count++;
2469 }
2470 
2471 
2472 #ifdef CONFIG_XFRM
2473 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2474 {
2475 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2476 	int err = 0;
2477 	struct iphdr *iph;
2478 
2479 	if (!x)
2480 		return 0;
2481 	/* XXX: we dont support tunnel mode for now until
2482 	 * we resolve the dst issue */
2483 	if (x->props.mode != XFRM_MODE_TRANSPORT)
2484 		return 0;
2485 
2486 	spin_lock(&x->lock);
2487 	iph = ip_hdr(skb);
2488 
2489 	err = x->outer_mode->output(x, skb);
2490 	if (err)
2491 		goto error;
2492 	err = x->type->output(x, skb);
2493 	if (err)
2494 		goto error;
2495 
2496 	x->curlft.bytes += skb->len;
2497 	x->curlft.packets++;
2498 error:
2499 	spin_unlock(&x->lock);
2500 	return err;
2501 }
2502 
2503 static void free_SAs(struct pktgen_dev *pkt_dev)
2504 {
2505 	if (pkt_dev->cflows) {
2506 		/* let go of the SAs if we have them */
2507 		int i = 0;
2508 		for (;  i < pkt_dev->cflows; i++) {
2509 			struct xfrm_state *x = pkt_dev->flows[i].x;
2510 			if (x) {
2511 				xfrm_state_put(x);
2512 				pkt_dev->flows[i].x = NULL;
2513 			}
2514 		}
2515 	}
2516 }
2517 
2518 static int process_ipsec(struct pktgen_dev *pkt_dev,
2519 			      struct sk_buff *skb, __be16 protocol)
2520 {
2521 	if (pkt_dev->flags & F_IPSEC_ON) {
2522 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2523 		int nhead = 0;
2524 		if (x) {
2525 			int ret;
2526 			__u8 *eth;
2527 			nhead = x->props.header_len - skb_headroom(skb);
2528 			if (nhead > 0) {
2529 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2530 				if (ret < 0) {
2531 					printk(KERN_ERR "Error expanding "
2532 					       "ipsec packet %d\n", ret);
2533 					goto err;
2534 				}
2535 			}
2536 
2537 			/* ipsec is not expecting ll header */
2538 			skb_pull(skb, ETH_HLEN);
2539 			ret = pktgen_output_ipsec(skb, pkt_dev);
2540 			if (ret) {
2541 				printk(KERN_ERR "Error creating ipsec "
2542 				       "packet %d\n", ret);
2543 				goto err;
2544 			}
2545 			/* restore ll */
2546 			eth = (__u8 *) skb_push(skb, ETH_HLEN);
2547 			memcpy(eth, pkt_dev->hh, 12);
2548 			*(u16 *) &eth[12] = protocol;
2549 		}
2550 	}
2551 	return 1;
2552 err:
2553 	kfree_skb(skb);
2554 	return 0;
2555 }
2556 #endif
2557 
2558 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2559 {
2560 	unsigned i;
2561 	for (i = 0; i < pkt_dev->nr_labels; i++)
2562 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2563 
2564 	mpls--;
2565 	*mpls |= MPLS_STACK_BOTTOM;
2566 }
2567 
2568 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2569 			       unsigned int prio)
2570 {
2571 	return htons(id | (cfi << 12) | (prio << 13));
2572 }
2573 
2574 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2575 					struct pktgen_dev *pkt_dev)
2576 {
2577 	struct sk_buff *skb = NULL;
2578 	__u8 *eth;
2579 	struct udphdr *udph;
2580 	int datalen, iplen;
2581 	struct iphdr *iph;
2582 	struct pktgen_hdr *pgh = NULL;
2583 	__be16 protocol = htons(ETH_P_IP);
2584 	__be32 *mpls;
2585 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
2586 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
2587 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
2588 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2589 	u16 queue_map;
2590 
2591 	if (pkt_dev->nr_labels)
2592 		protocol = htons(ETH_P_MPLS_UC);
2593 
2594 	if (pkt_dev->vlan_id != 0xffff)
2595 		protocol = htons(ETH_P_8021Q);
2596 
2597 	/* Update any of the values, used when we're incrementing various
2598 	 * fields.
2599 	 */
2600 	queue_map = pkt_dev->cur_queue_map;
2601 	mod_cur_headers(pkt_dev);
2602 
2603 	datalen = (odev->hard_header_len + 16) & ~0xf;
2604 
2605 	if (pkt_dev->flags & F_NODE) {
2606 		int node;
2607 
2608 		if (pkt_dev->node >= 0)
2609 			node = pkt_dev->node;
2610 		else
2611 			node =  numa_node_id();
2612 
2613 		skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2614 				  + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2615 		if (likely(skb)) {
2616 			skb_reserve(skb, NET_SKB_PAD);
2617 			skb->dev = odev;
2618 		}
2619 	}
2620 	else
2621 	  skb = __netdev_alloc_skb(odev,
2622 				   pkt_dev->cur_pkt_size + 64
2623 				   + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2624 
2625 	if (!skb) {
2626 		sprintf(pkt_dev->result, "No memory");
2627 		return NULL;
2628 	}
2629 
2630 	skb_reserve(skb, datalen);
2631 
2632 	/*  Reserve for ethernet and IP header  */
2633 	eth = (__u8 *) skb_push(skb, 14);
2634 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2635 	if (pkt_dev->nr_labels)
2636 		mpls_push(mpls, pkt_dev);
2637 
2638 	if (pkt_dev->vlan_id != 0xffff) {
2639 		if (pkt_dev->svlan_id != 0xffff) {
2640 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2641 			*svlan_tci = build_tci(pkt_dev->svlan_id,
2642 					       pkt_dev->svlan_cfi,
2643 					       pkt_dev->svlan_p);
2644 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2645 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
2646 		}
2647 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2648 		*vlan_tci = build_tci(pkt_dev->vlan_id,
2649 				      pkt_dev->vlan_cfi,
2650 				      pkt_dev->vlan_p);
2651 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2652 		*vlan_encapsulated_proto = htons(ETH_P_IP);
2653 	}
2654 
2655 	skb->network_header = skb->tail;
2656 	skb->transport_header = skb->network_header + sizeof(struct iphdr);
2657 	skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2658 	skb_set_queue_mapping(skb, queue_map);
2659 	iph = ip_hdr(skb);
2660 	udph = udp_hdr(skb);
2661 
2662 	memcpy(eth, pkt_dev->hh, 12);
2663 	*(__be16 *) & eth[12] = protocol;
2664 
2665 	/* Eth + IPh + UDPh + mpls */
2666 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2667 		  pkt_dev->pkt_overhead;
2668 	if (datalen < sizeof(struct pktgen_hdr))
2669 		datalen = sizeof(struct pktgen_hdr);
2670 
2671 	udph->source = htons(pkt_dev->cur_udp_src);
2672 	udph->dest = htons(pkt_dev->cur_udp_dst);
2673 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
2674 	udph->check = 0;	/* No checksum */
2675 
2676 	iph->ihl = 5;
2677 	iph->version = 4;
2678 	iph->ttl = 32;
2679 	iph->tos = pkt_dev->tos;
2680 	iph->protocol = IPPROTO_UDP;	/* UDP */
2681 	iph->saddr = pkt_dev->cur_saddr;
2682 	iph->daddr = pkt_dev->cur_daddr;
2683 	iph->id = htons(pkt_dev->ip_id);
2684 	pkt_dev->ip_id++;
2685 	iph->frag_off = 0;
2686 	iplen = 20 + 8 + datalen;
2687 	iph->tot_len = htons(iplen);
2688 	iph->check = 0;
2689 	iph->check = ip_fast_csum((void *)iph, iph->ihl);
2690 	skb->protocol = protocol;
2691 	skb->mac_header = (skb->network_header - ETH_HLEN -
2692 			   pkt_dev->pkt_overhead);
2693 	skb->dev = odev;
2694 	skb->pkt_type = PACKET_HOST;
2695 
2696 	if (pkt_dev->nfrags <= 0) {
2697 		pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2698 		memset(pgh + 1, 0, datalen - sizeof(struct pktgen_hdr));
2699 	} else {
2700 		int frags = pkt_dev->nfrags;
2701 		int i, len;
2702 
2703 		pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2704 
2705 		if (frags > MAX_SKB_FRAGS)
2706 			frags = MAX_SKB_FRAGS;
2707 		if (datalen > frags * PAGE_SIZE) {
2708 			len = datalen - frags * PAGE_SIZE;
2709 			memset(skb_put(skb, len), 0, len);
2710 			datalen = frags * PAGE_SIZE;
2711 		}
2712 
2713 		i = 0;
2714 		while (datalen > 0) {
2715 			struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
2716 			skb_shinfo(skb)->frags[i].page = page;
2717 			skb_shinfo(skb)->frags[i].page_offset = 0;
2718 			skb_shinfo(skb)->frags[i].size =
2719 			    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2720 			datalen -= skb_shinfo(skb)->frags[i].size;
2721 			skb->len += skb_shinfo(skb)->frags[i].size;
2722 			skb->data_len += skb_shinfo(skb)->frags[i].size;
2723 			i++;
2724 			skb_shinfo(skb)->nr_frags = i;
2725 		}
2726 
2727 		while (i < frags) {
2728 			int rem;
2729 
2730 			if (i == 0)
2731 				break;
2732 
2733 			rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2734 			if (rem == 0)
2735 				break;
2736 
2737 			skb_shinfo(skb)->frags[i - 1].size -= rem;
2738 
2739 			skb_shinfo(skb)->frags[i] =
2740 			    skb_shinfo(skb)->frags[i - 1];
2741 			get_page(skb_shinfo(skb)->frags[i].page);
2742 			skb_shinfo(skb)->frags[i].page =
2743 			    skb_shinfo(skb)->frags[i - 1].page;
2744 			skb_shinfo(skb)->frags[i].page_offset +=
2745 			    skb_shinfo(skb)->frags[i - 1].size;
2746 			skb_shinfo(skb)->frags[i].size = rem;
2747 			i++;
2748 			skb_shinfo(skb)->nr_frags = i;
2749 		}
2750 	}
2751 
2752 	/* Stamp the time, and sequence number,
2753 	 * convert them to network byte order
2754 	 */
2755 	if (pgh) {
2756 		struct timeval timestamp;
2757 
2758 		pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2759 		pgh->seq_num = htonl(pkt_dev->seq_num);
2760 
2761 		do_gettimeofday(&timestamp);
2762 		pgh->tv_sec = htonl(timestamp.tv_sec);
2763 		pgh->tv_usec = htonl(timestamp.tv_usec);
2764 	}
2765 
2766 #ifdef CONFIG_XFRM
2767 	if (!process_ipsec(pkt_dev, skb, protocol))
2768 		return NULL;
2769 #endif
2770 
2771 	return skb;
2772 }
2773 
2774 /*
2775  * scan_ip6, fmt_ip taken from dietlibc-0.21
2776  * Author Felix von Leitner <felix-dietlibc@fefe.de>
2777  *
2778  * Slightly modified for kernel.
2779  * Should be candidate for net/ipv4/utils.c
2780  * --ro
2781  */
2782 
2783 static unsigned int scan_ip6(const char *s, char ip[16])
2784 {
2785 	unsigned int i;
2786 	unsigned int len = 0;
2787 	unsigned long u;
2788 	char suffix[16];
2789 	unsigned int prefixlen = 0;
2790 	unsigned int suffixlen = 0;
2791 	__be32 tmp;
2792 	char *pos;
2793 
2794 	for (i = 0; i < 16; i++)
2795 		ip[i] = 0;
2796 
2797 	for (;;) {
2798 		if (*s == ':') {
2799 			len++;
2800 			if (s[1] == ':') {	/* Found "::", skip to part 2 */
2801 				s += 2;
2802 				len++;
2803 				break;
2804 			}
2805 			s++;
2806 		}
2807 
2808 		u = simple_strtoul(s, &pos, 16);
2809 		i = pos - s;
2810 		if (!i)
2811 			return 0;
2812 		if (prefixlen == 12 && s[i] == '.') {
2813 
2814 			/* the last 4 bytes may be written as IPv4 address */
2815 
2816 			tmp = in_aton(s);
2817 			memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2818 			return i + len;
2819 		}
2820 		ip[prefixlen++] = (u >> 8);
2821 		ip[prefixlen++] = (u & 255);
2822 		s += i;
2823 		len += i;
2824 		if (prefixlen == 16)
2825 			return len;
2826 	}
2827 
2828 /* part 2, after "::" */
2829 	for (;;) {
2830 		if (*s == ':') {
2831 			if (suffixlen == 0)
2832 				break;
2833 			s++;
2834 			len++;
2835 		} else if (suffixlen != 0)
2836 			break;
2837 
2838 		u = simple_strtol(s, &pos, 16);
2839 		i = pos - s;
2840 		if (!i) {
2841 			if (*s)
2842 				len--;
2843 			break;
2844 		}
2845 		if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2846 			tmp = in_aton(s);
2847 			memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2848 			       sizeof(tmp));
2849 			suffixlen += 4;
2850 			len += strlen(s);
2851 			break;
2852 		}
2853 		suffix[suffixlen++] = (u >> 8);
2854 		suffix[suffixlen++] = (u & 255);
2855 		s += i;
2856 		len += i;
2857 		if (prefixlen + suffixlen == 16)
2858 			break;
2859 	}
2860 	for (i = 0; i < suffixlen; i++)
2861 		ip[16 - suffixlen + i] = suffix[i];
2862 	return len;
2863 }
2864 
2865 static char tohex(char hexdigit)
2866 {
2867 	return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2868 }
2869 
2870 static int fmt_xlong(char *s, unsigned int i)
2871 {
2872 	char *bak = s;
2873 	*s = tohex((i >> 12) & 0xf);
2874 	if (s != bak || *s != '0')
2875 		++s;
2876 	*s = tohex((i >> 8) & 0xf);
2877 	if (s != bak || *s != '0')
2878 		++s;
2879 	*s = tohex((i >> 4) & 0xf);
2880 	if (s != bak || *s != '0')
2881 		++s;
2882 	*s = tohex(i & 0xf);
2883 	return s - bak + 1;
2884 }
2885 
2886 static unsigned int fmt_ip6(char *s, const char ip[16])
2887 {
2888 	unsigned int len;
2889 	unsigned int i;
2890 	unsigned int temp;
2891 	unsigned int compressing;
2892 	int j;
2893 
2894 	len = 0;
2895 	compressing = 0;
2896 	for (j = 0; j < 16; j += 2) {
2897 
2898 #ifdef V4MAPPEDPREFIX
2899 		if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2900 			inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2901 			temp = strlen(s);
2902 			return len + temp;
2903 		}
2904 #endif
2905 		temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2906 		    (unsigned long)(unsigned char)ip[j + 1];
2907 		if (temp == 0) {
2908 			if (!compressing) {
2909 				compressing = 1;
2910 				if (j == 0) {
2911 					*s++ = ':';
2912 					++len;
2913 				}
2914 			}
2915 		} else {
2916 			if (compressing) {
2917 				compressing = 0;
2918 				*s++ = ':';
2919 				++len;
2920 			}
2921 			i = fmt_xlong(s, temp);
2922 			len += i;
2923 			s += i;
2924 			if (j < 14) {
2925 				*s++ = ':';
2926 				++len;
2927 			}
2928 		}
2929 	}
2930 	if (compressing) {
2931 		*s++ = ':';
2932 		++len;
2933 	}
2934 	*s = 0;
2935 	return len;
2936 }
2937 
2938 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2939 					struct pktgen_dev *pkt_dev)
2940 {
2941 	struct sk_buff *skb = NULL;
2942 	__u8 *eth;
2943 	struct udphdr *udph;
2944 	int datalen;
2945 	struct ipv6hdr *iph;
2946 	struct pktgen_hdr *pgh = NULL;
2947 	__be16 protocol = htons(ETH_P_IPV6);
2948 	__be32 *mpls;
2949 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
2950 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
2951 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
2952 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2953 	u16 queue_map;
2954 
2955 	if (pkt_dev->nr_labels)
2956 		protocol = htons(ETH_P_MPLS_UC);
2957 
2958 	if (pkt_dev->vlan_id != 0xffff)
2959 		protocol = htons(ETH_P_8021Q);
2960 
2961 	/* Update any of the values, used when we're incrementing various
2962 	 * fields.
2963 	 */
2964 	queue_map = pkt_dev->cur_queue_map;
2965 	mod_cur_headers(pkt_dev);
2966 
2967 	skb = __netdev_alloc_skb(odev,
2968 				 pkt_dev->cur_pkt_size + 64
2969 				 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
2970 	if (!skb) {
2971 		sprintf(pkt_dev->result, "No memory");
2972 		return NULL;
2973 	}
2974 
2975 	skb_reserve(skb, 16);
2976 
2977 	/*  Reserve for ethernet and IP header  */
2978 	eth = (__u8 *) skb_push(skb, 14);
2979 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2980 	if (pkt_dev->nr_labels)
2981 		mpls_push(mpls, pkt_dev);
2982 
2983 	if (pkt_dev->vlan_id != 0xffff) {
2984 		if (pkt_dev->svlan_id != 0xffff) {
2985 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2986 			*svlan_tci = build_tci(pkt_dev->svlan_id,
2987 					       pkt_dev->svlan_cfi,
2988 					       pkt_dev->svlan_p);
2989 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2990 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
2991 		}
2992 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2993 		*vlan_tci = build_tci(pkt_dev->vlan_id,
2994 				      pkt_dev->vlan_cfi,
2995 				      pkt_dev->vlan_p);
2996 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2997 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
2998 	}
2999 
3000 	skb->network_header = skb->tail;
3001 	skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
3002 	skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
3003 	skb_set_queue_mapping(skb, queue_map);
3004 	iph = ipv6_hdr(skb);
3005 	udph = udp_hdr(skb);
3006 
3007 	memcpy(eth, pkt_dev->hh, 12);
3008 	*(__be16 *) &eth[12] = protocol;
3009 
3010 	/* Eth + IPh + UDPh + mpls */
3011 	datalen = pkt_dev->cur_pkt_size - 14 -
3012 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
3013 		  pkt_dev->pkt_overhead;
3014 
3015 	if (datalen < sizeof(struct pktgen_hdr)) {
3016 		datalen = sizeof(struct pktgen_hdr);
3017 		if (net_ratelimit())
3018 			printk(KERN_INFO "pktgen: increased datalen to %d\n",
3019 			       datalen);
3020 	}
3021 
3022 	udph->source = htons(pkt_dev->cur_udp_src);
3023 	udph->dest = htons(pkt_dev->cur_udp_dst);
3024 	udph->len = htons(datalen + sizeof(struct udphdr));
3025 	udph->check = 0;	/* No checksum */
3026 
3027 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
3028 
3029 	if (pkt_dev->traffic_class) {
3030 		/* Version + traffic class + flow (0) */
3031 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
3032 	}
3033 
3034 	iph->hop_limit = 32;
3035 
3036 	iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3037 	iph->nexthdr = IPPROTO_UDP;
3038 
3039 	ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3040 	ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3041 
3042 	skb->mac_header = (skb->network_header - ETH_HLEN -
3043 			   pkt_dev->pkt_overhead);
3044 	skb->protocol = protocol;
3045 	skb->dev = odev;
3046 	skb->pkt_type = PACKET_HOST;
3047 
3048 	if (pkt_dev->nfrags <= 0)
3049 		pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
3050 	else {
3051 		int frags = pkt_dev->nfrags;
3052 		int i;
3053 
3054 		pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
3055 
3056 		if (frags > MAX_SKB_FRAGS)
3057 			frags = MAX_SKB_FRAGS;
3058 		if (datalen > frags * PAGE_SIZE) {
3059 			skb_put(skb, datalen - frags * PAGE_SIZE);
3060 			datalen = frags * PAGE_SIZE;
3061 		}
3062 
3063 		i = 0;
3064 		while (datalen > 0) {
3065 			struct page *page = alloc_pages(GFP_KERNEL, 0);
3066 			skb_shinfo(skb)->frags[i].page = page;
3067 			skb_shinfo(skb)->frags[i].page_offset = 0;
3068 			skb_shinfo(skb)->frags[i].size =
3069 			    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
3070 			datalen -= skb_shinfo(skb)->frags[i].size;
3071 			skb->len += skb_shinfo(skb)->frags[i].size;
3072 			skb->data_len += skb_shinfo(skb)->frags[i].size;
3073 			i++;
3074 			skb_shinfo(skb)->nr_frags = i;
3075 		}
3076 
3077 		while (i < frags) {
3078 			int rem;
3079 
3080 			if (i == 0)
3081 				break;
3082 
3083 			rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3084 			if (rem == 0)
3085 				break;
3086 
3087 			skb_shinfo(skb)->frags[i - 1].size -= rem;
3088 
3089 			skb_shinfo(skb)->frags[i] =
3090 			    skb_shinfo(skb)->frags[i - 1];
3091 			get_page(skb_shinfo(skb)->frags[i].page);
3092 			skb_shinfo(skb)->frags[i].page =
3093 			    skb_shinfo(skb)->frags[i - 1].page;
3094 			skb_shinfo(skb)->frags[i].page_offset +=
3095 			    skb_shinfo(skb)->frags[i - 1].size;
3096 			skb_shinfo(skb)->frags[i].size = rem;
3097 			i++;
3098 			skb_shinfo(skb)->nr_frags = i;
3099 		}
3100 	}
3101 
3102 	/* Stamp the time, and sequence number,
3103 	 * convert them to network byte order
3104 	 * should we update cloned packets too ?
3105 	 */
3106 	if (pgh) {
3107 		struct timeval timestamp;
3108 
3109 		pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3110 		pgh->seq_num = htonl(pkt_dev->seq_num);
3111 
3112 		do_gettimeofday(&timestamp);
3113 		pgh->tv_sec = htonl(timestamp.tv_sec);
3114 		pgh->tv_usec = htonl(timestamp.tv_usec);
3115 	}
3116 	/* pkt_dev->seq_num++; FF: you really mean this? */
3117 
3118 	return skb;
3119 }
3120 
3121 static struct sk_buff *fill_packet(struct net_device *odev,
3122 				   struct pktgen_dev *pkt_dev)
3123 {
3124 	if (pkt_dev->flags & F_IPV6)
3125 		return fill_packet_ipv6(odev, pkt_dev);
3126 	else
3127 		return fill_packet_ipv4(odev, pkt_dev);
3128 }
3129 
3130 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3131 {
3132 	pkt_dev->seq_num = 1;
3133 	pkt_dev->idle_acc = 0;
3134 	pkt_dev->sofar = 0;
3135 	pkt_dev->tx_bytes = 0;
3136 	pkt_dev->errors = 0;
3137 }
3138 
3139 /* Set up structure for sending pkts, clear counters */
3140 
3141 static void pktgen_run(struct pktgen_thread *t)
3142 {
3143 	struct pktgen_dev *pkt_dev;
3144 	int started = 0;
3145 
3146 	pr_debug("pktgen: entering pktgen_run. %p\n", t);
3147 
3148 	if_lock(t);
3149 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3150 
3151 		/*
3152 		 * setup odev and create initial packet.
3153 		 */
3154 		pktgen_setup_inject(pkt_dev);
3155 
3156 		if (pkt_dev->odev) {
3157 			pktgen_clear_counters(pkt_dev);
3158 			pkt_dev->running = 1;	/* Cranke yeself! */
3159 			pkt_dev->skb = NULL;
3160 			pkt_dev->started_at =
3161 				pkt_dev->next_tx = ktime_now();
3162 
3163 			set_pkt_overhead(pkt_dev);
3164 
3165 			strcpy(pkt_dev->result, "Starting");
3166 			started++;
3167 		} else
3168 			strcpy(pkt_dev->result, "Error starting");
3169 	}
3170 	if_unlock(t);
3171 	if (started)
3172 		t->control &= ~(T_STOP);
3173 }
3174 
3175 static void pktgen_stop_all_threads_ifs(void)
3176 {
3177 	struct pktgen_thread *t;
3178 
3179 	pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3180 
3181 	mutex_lock(&pktgen_thread_lock);
3182 
3183 	list_for_each_entry(t, &pktgen_threads, th_list)
3184 		t->control |= T_STOP;
3185 
3186 	mutex_unlock(&pktgen_thread_lock);
3187 }
3188 
3189 static int thread_is_running(const struct pktgen_thread *t)
3190 {
3191 	const struct pktgen_dev *pkt_dev;
3192 
3193 	list_for_each_entry(pkt_dev, &t->if_list, list)
3194 		if (pkt_dev->running)
3195 			return 1;
3196 	return 0;
3197 }
3198 
3199 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3200 {
3201 	if_lock(t);
3202 
3203 	while (thread_is_running(t)) {
3204 
3205 		if_unlock(t);
3206 
3207 		msleep_interruptible(100);
3208 
3209 		if (signal_pending(current))
3210 			goto signal;
3211 		if_lock(t);
3212 	}
3213 	if_unlock(t);
3214 	return 1;
3215 signal:
3216 	return 0;
3217 }
3218 
3219 static int pktgen_wait_all_threads_run(void)
3220 {
3221 	struct pktgen_thread *t;
3222 	int sig = 1;
3223 
3224 	mutex_lock(&pktgen_thread_lock);
3225 
3226 	list_for_each_entry(t, &pktgen_threads, th_list) {
3227 		sig = pktgen_wait_thread_run(t);
3228 		if (sig == 0)
3229 			break;
3230 	}
3231 
3232 	if (sig == 0)
3233 		list_for_each_entry(t, &pktgen_threads, th_list)
3234 			t->control |= (T_STOP);
3235 
3236 	mutex_unlock(&pktgen_thread_lock);
3237 	return sig;
3238 }
3239 
3240 static void pktgen_run_all_threads(void)
3241 {
3242 	struct pktgen_thread *t;
3243 
3244 	pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3245 
3246 	mutex_lock(&pktgen_thread_lock);
3247 
3248 	list_for_each_entry(t, &pktgen_threads, th_list)
3249 		t->control |= (T_RUN);
3250 
3251 	mutex_unlock(&pktgen_thread_lock);
3252 
3253 	/* Propagate thread->control  */
3254 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3255 
3256 	pktgen_wait_all_threads_run();
3257 }
3258 
3259 static void pktgen_reset_all_threads(void)
3260 {
3261 	struct pktgen_thread *t;
3262 
3263 	pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3264 
3265 	mutex_lock(&pktgen_thread_lock);
3266 
3267 	list_for_each_entry(t, &pktgen_threads, th_list)
3268 		t->control |= (T_REMDEVALL);
3269 
3270 	mutex_unlock(&pktgen_thread_lock);
3271 
3272 	/* Propagate thread->control  */
3273 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3274 
3275 	pktgen_wait_all_threads_run();
3276 }
3277 
3278 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3279 {
3280 	__u64 bps, mbps, pps;
3281 	char *p = pkt_dev->result;
3282 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3283 				    pkt_dev->started_at);
3284 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3285 
3286 	p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3287 		     (unsigned long long)ktime_to_us(elapsed),
3288 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3289 		     (unsigned long long)ktime_to_us(idle),
3290 		     (unsigned long long)pkt_dev->sofar,
3291 		     pkt_dev->cur_pkt_size, nr_frags);
3292 
3293 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3294 			ktime_to_ns(elapsed));
3295 
3296 	bps = pps * 8 * pkt_dev->cur_pkt_size;
3297 
3298 	mbps = bps;
3299 	do_div(mbps, 1000000);
3300 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
3301 		     (unsigned long long)pps,
3302 		     (unsigned long long)mbps,
3303 		     (unsigned long long)bps,
3304 		     (unsigned long long)pkt_dev->errors);
3305 }
3306 
3307 /* Set stopped-at timer, remove from running list, do counters & statistics */
3308 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3309 {
3310 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3311 
3312 	if (!pkt_dev->running) {
3313 		printk(KERN_WARNING "pktgen: interface: %s is already "
3314 		       "stopped\n", pkt_dev->odevname);
3315 		return -EINVAL;
3316 	}
3317 
3318 	kfree_skb(pkt_dev->skb);
3319 	pkt_dev->skb = NULL;
3320 	pkt_dev->stopped_at = ktime_now();
3321 	pkt_dev->running = 0;
3322 
3323 	show_results(pkt_dev, nr_frags);
3324 
3325 	return 0;
3326 }
3327 
3328 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3329 {
3330 	struct pktgen_dev *pkt_dev, *best = NULL;
3331 
3332 	if_lock(t);
3333 
3334 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3335 		if (!pkt_dev->running)
3336 			continue;
3337 		if (best == NULL)
3338 			best = pkt_dev;
3339 		else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
3340 			best = pkt_dev;
3341 	}
3342 	if_unlock(t);
3343 	return best;
3344 }
3345 
3346 static void pktgen_stop(struct pktgen_thread *t)
3347 {
3348 	struct pktgen_dev *pkt_dev;
3349 
3350 	pr_debug("pktgen: entering pktgen_stop\n");
3351 
3352 	if_lock(t);
3353 
3354 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3355 		pktgen_stop_device(pkt_dev);
3356 	}
3357 
3358 	if_unlock(t);
3359 }
3360 
3361 /*
3362  * one of our devices needs to be removed - find it
3363  * and remove it
3364  */
3365 static void pktgen_rem_one_if(struct pktgen_thread *t)
3366 {
3367 	struct list_head *q, *n;
3368 	struct pktgen_dev *cur;
3369 
3370 	pr_debug("pktgen: entering pktgen_rem_one_if\n");
3371 
3372 	if_lock(t);
3373 
3374 	list_for_each_safe(q, n, &t->if_list) {
3375 		cur = list_entry(q, struct pktgen_dev, list);
3376 
3377 		if (!cur->removal_mark)
3378 			continue;
3379 
3380 		kfree_skb(cur->skb);
3381 		cur->skb = NULL;
3382 
3383 		pktgen_remove_device(t, cur);
3384 
3385 		break;
3386 	}
3387 
3388 	if_unlock(t);
3389 }
3390 
3391 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3392 {
3393 	struct list_head *q, *n;
3394 	struct pktgen_dev *cur;
3395 
3396 	/* Remove all devices, free mem */
3397 
3398 	pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3399 	if_lock(t);
3400 
3401 	list_for_each_safe(q, n, &t->if_list) {
3402 		cur = list_entry(q, struct pktgen_dev, list);
3403 
3404 		kfree_skb(cur->skb);
3405 		cur->skb = NULL;
3406 
3407 		pktgen_remove_device(t, cur);
3408 	}
3409 
3410 	if_unlock(t);
3411 }
3412 
3413 static void pktgen_rem_thread(struct pktgen_thread *t)
3414 {
3415 	/* Remove from the thread list */
3416 
3417 	remove_proc_entry(t->tsk->comm, pg_proc_dir);
3418 
3419 	mutex_lock(&pktgen_thread_lock);
3420 
3421 	list_del(&t->th_list);
3422 
3423 	mutex_unlock(&pktgen_thread_lock);
3424 }
3425 
3426 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3427 {
3428 	ktime_t idle_start = ktime_now();
3429 	schedule();
3430 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3431 }
3432 
3433 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3434 {
3435 	ktime_t idle_start = ktime_now();
3436 
3437 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3438 		if (signal_pending(current))
3439 			break;
3440 
3441 		if (need_resched())
3442 			pktgen_resched(pkt_dev);
3443 		else
3444 			cpu_relax();
3445 	}
3446 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3447 }
3448 
3449 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3450 {
3451 	struct net_device *odev = pkt_dev->odev;
3452 	netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
3453 		= odev->netdev_ops->ndo_start_xmit;
3454 	struct netdev_queue *txq;
3455 	u16 queue_map;
3456 	int ret;
3457 
3458 	/* If device is offline, then don't send */
3459 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3460 		pktgen_stop_device(pkt_dev);
3461 		return;
3462 	}
3463 
3464 	/* This is max DELAY, this has special meaning of
3465 	 * "never transmit"
3466 	 */
3467 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3468 		pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3469 		return;
3470 	}
3471 
3472 	/* If no skb or clone count exhausted then get new one */
3473 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
3474 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3475 		/* build a new pkt */
3476 		kfree_skb(pkt_dev->skb);
3477 
3478 		pkt_dev->skb = fill_packet(odev, pkt_dev);
3479 		if (pkt_dev->skb == NULL) {
3480 			printk(KERN_ERR "pktgen: ERROR: couldn't "
3481 			       "allocate skb in fill_packet.\n");
3482 			schedule();
3483 			pkt_dev->clone_count--;	/* back out increment, OOM */
3484 			return;
3485 		}
3486 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
3487 		pkt_dev->allocated_skbs++;
3488 		pkt_dev->clone_count = 0;	/* reset counter */
3489 	}
3490 
3491 	if (pkt_dev->delay && pkt_dev->last_ok)
3492 		spin(pkt_dev, pkt_dev->next_tx);
3493 
3494 	queue_map = skb_get_queue_mapping(pkt_dev->skb);
3495 	txq = netdev_get_tx_queue(odev, queue_map);
3496 
3497 	__netif_tx_lock_bh(txq);
3498 
3499 	if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq))) {
3500 		ret = NETDEV_TX_BUSY;
3501 		pkt_dev->last_ok = 0;
3502 		goto unlock;
3503 	}
3504 	atomic_inc(&(pkt_dev->skb->users));
3505 	ret = (*xmit)(pkt_dev->skb, odev);
3506 
3507 	switch (ret) {
3508 	case NETDEV_TX_OK:
3509 		txq_trans_update(txq);
3510 		pkt_dev->last_ok = 1;
3511 		pkt_dev->sofar++;
3512 		pkt_dev->seq_num++;
3513 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3514 		break;
3515 	case NET_XMIT_DROP:
3516 	case NET_XMIT_CN:
3517 	case NET_XMIT_POLICED:
3518 		/* skb has been consumed */
3519 		pkt_dev->errors++;
3520 		break;
3521 	default: /* Drivers are not supposed to return other values! */
3522 		if (net_ratelimit())
3523 			pr_info("pktgen: %s xmit error: %d\n",
3524 				pkt_dev->odevname, ret);
3525 		pkt_dev->errors++;
3526 		/* fallthru */
3527 	case NETDEV_TX_LOCKED:
3528 	case NETDEV_TX_BUSY:
3529 		/* Retry it next time */
3530 		atomic_dec(&(pkt_dev->skb->users));
3531 		pkt_dev->last_ok = 0;
3532 	}
3533 unlock:
3534 	__netif_tx_unlock_bh(txq);
3535 
3536 	/* If pkt_dev->count is zero, then run forever */
3537 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3538 		pktgen_wait_for_skb(pkt_dev);
3539 
3540 		/* Done with this */
3541 		pktgen_stop_device(pkt_dev);
3542 	}
3543 }
3544 
3545 /*
3546  * Main loop of the thread goes here
3547  */
3548 
3549 static int pktgen_thread_worker(void *arg)
3550 {
3551 	DEFINE_WAIT(wait);
3552 	struct pktgen_thread *t = arg;
3553 	struct pktgen_dev *pkt_dev = NULL;
3554 	int cpu = t->cpu;
3555 
3556 	BUG_ON(smp_processor_id() != cpu);
3557 
3558 	init_waitqueue_head(&t->queue);
3559 	complete(&t->start_done);
3560 
3561 	pr_debug("pktgen: starting pktgen/%d:  pid=%d\n",
3562 		 cpu, task_pid_nr(current));
3563 
3564 	set_current_state(TASK_INTERRUPTIBLE);
3565 
3566 	set_freezable();
3567 
3568 	while (!kthread_should_stop()) {
3569 		pkt_dev = next_to_run(t);
3570 
3571 		if (unlikely(!pkt_dev && t->control == 0)) {
3572 			wait_event_interruptible_timeout(t->queue,
3573 							 t->control != 0,
3574 							 HZ/10);
3575 			try_to_freeze();
3576 			continue;
3577 		}
3578 
3579 		__set_current_state(TASK_RUNNING);
3580 
3581 		if (likely(pkt_dev)) {
3582 			pktgen_xmit(pkt_dev);
3583 
3584 			if (need_resched())
3585 				pktgen_resched(pkt_dev);
3586 			else
3587 				cpu_relax();
3588 		}
3589 
3590 		if (t->control & T_STOP) {
3591 			pktgen_stop(t);
3592 			t->control &= ~(T_STOP);
3593 		}
3594 
3595 		if (t->control & T_RUN) {
3596 			pktgen_run(t);
3597 			t->control &= ~(T_RUN);
3598 		}
3599 
3600 		if (t->control & T_REMDEVALL) {
3601 			pktgen_rem_all_ifs(t);
3602 			t->control &= ~(T_REMDEVALL);
3603 		}
3604 
3605 		if (t->control & T_REMDEV) {
3606 			pktgen_rem_one_if(t);
3607 			t->control &= ~(T_REMDEV);
3608 		}
3609 
3610 		try_to_freeze();
3611 
3612 		set_current_state(TASK_INTERRUPTIBLE);
3613 	}
3614 
3615 	pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3616 	pktgen_stop(t);
3617 
3618 	pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3619 	pktgen_rem_all_ifs(t);
3620 
3621 	pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3622 	pktgen_rem_thread(t);
3623 
3624 	return 0;
3625 }
3626 
3627 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3628 					  const char *ifname, bool exact)
3629 {
3630 	struct pktgen_dev *p, *pkt_dev = NULL;
3631 	size_t len = strlen(ifname);
3632 
3633 	if_lock(t);
3634 	list_for_each_entry(p, &t->if_list, list)
3635 		if (strncmp(p->odevname, ifname, len) == 0) {
3636 			if (p->odevname[len]) {
3637 				if (exact || p->odevname[len] != '@')
3638 					continue;
3639 			}
3640 			pkt_dev = p;
3641 			break;
3642 		}
3643 
3644 	if_unlock(t);
3645 	pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3646 	return pkt_dev;
3647 }
3648 
3649 /*
3650  * Adds a dev at front of if_list.
3651  */
3652 
3653 static int add_dev_to_thread(struct pktgen_thread *t,
3654 			     struct pktgen_dev *pkt_dev)
3655 {
3656 	int rv = 0;
3657 
3658 	if_lock(t);
3659 
3660 	if (pkt_dev->pg_thread) {
3661 		printk(KERN_ERR "pktgen: ERROR: already assigned "
3662 		       "to a thread.\n");
3663 		rv = -EBUSY;
3664 		goto out;
3665 	}
3666 
3667 	list_add(&pkt_dev->list, &t->if_list);
3668 	pkt_dev->pg_thread = t;
3669 	pkt_dev->running = 0;
3670 
3671 out:
3672 	if_unlock(t);
3673 	return rv;
3674 }
3675 
3676 /* Called under thread lock */
3677 
3678 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3679 {
3680 	struct pktgen_dev *pkt_dev;
3681 	int err;
3682 	int node = cpu_to_node(t->cpu);
3683 
3684 	/* We don't allow a device to be on several threads */
3685 
3686 	pkt_dev = __pktgen_NN_threads(ifname, FIND);
3687 	if (pkt_dev) {
3688 		printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3689 		return -EBUSY;
3690 	}
3691 
3692 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3693 	if (!pkt_dev)
3694 		return -ENOMEM;
3695 
3696 	strcpy(pkt_dev->odevname, ifname);
3697 	pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3698 				      node);
3699 	if (pkt_dev->flows == NULL) {
3700 		kfree(pkt_dev);
3701 		return -ENOMEM;
3702 	}
3703 	memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3704 
3705 	pkt_dev->removal_mark = 0;
3706 	pkt_dev->min_pkt_size = ETH_ZLEN;
3707 	pkt_dev->max_pkt_size = ETH_ZLEN;
3708 	pkt_dev->nfrags = 0;
3709 	pkt_dev->clone_skb = pg_clone_skb_d;
3710 	pkt_dev->delay = pg_delay_d;
3711 	pkt_dev->count = pg_count_d;
3712 	pkt_dev->sofar = 0;
3713 	pkt_dev->udp_src_min = 9;	/* sink port */
3714 	pkt_dev->udp_src_max = 9;
3715 	pkt_dev->udp_dst_min = 9;
3716 	pkt_dev->udp_dst_max = 9;
3717 
3718 	pkt_dev->vlan_p = 0;
3719 	pkt_dev->vlan_cfi = 0;
3720 	pkt_dev->vlan_id = 0xffff;
3721 	pkt_dev->svlan_p = 0;
3722 	pkt_dev->svlan_cfi = 0;
3723 	pkt_dev->svlan_id = 0xffff;
3724 	pkt_dev->node = -1;
3725 
3726 	err = pktgen_setup_dev(pkt_dev, ifname);
3727 	if (err)
3728 		goto out1;
3729 
3730 	pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3731 					  &pktgen_if_fops, pkt_dev);
3732 	if (!pkt_dev->entry) {
3733 		printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3734 		       PG_PROC_DIR, ifname);
3735 		err = -EINVAL;
3736 		goto out2;
3737 	}
3738 #ifdef CONFIG_XFRM
3739 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3740 	pkt_dev->ipsproto = IPPROTO_ESP;
3741 #endif
3742 
3743 	return add_dev_to_thread(t, pkt_dev);
3744 out2:
3745 	dev_put(pkt_dev->odev);
3746 out1:
3747 #ifdef CONFIG_XFRM
3748 	free_SAs(pkt_dev);
3749 #endif
3750 	vfree(pkt_dev->flows);
3751 	kfree(pkt_dev);
3752 	return err;
3753 }
3754 
3755 static int __init pktgen_create_thread(int cpu)
3756 {
3757 	struct pktgen_thread *t;
3758 	struct proc_dir_entry *pe;
3759 	struct task_struct *p;
3760 
3761 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3762 			 cpu_to_node(cpu));
3763 	if (!t) {
3764 		printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3765 		       "create new thread.\n");
3766 		return -ENOMEM;
3767 	}
3768 
3769 	spin_lock_init(&t->if_lock);
3770 	t->cpu = cpu;
3771 
3772 	INIT_LIST_HEAD(&t->if_list);
3773 
3774 	list_add_tail(&t->th_list, &pktgen_threads);
3775 	init_completion(&t->start_done);
3776 
3777 	p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3778 	if (IS_ERR(p)) {
3779 		printk(KERN_ERR "pktgen: kernel_thread() failed "
3780 		       "for cpu %d\n", t->cpu);
3781 		list_del(&t->th_list);
3782 		kfree(t);
3783 		return PTR_ERR(p);
3784 	}
3785 	kthread_bind(p, cpu);
3786 	t->tsk = p;
3787 
3788 	pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3789 			      &pktgen_thread_fops, t);
3790 	if (!pe) {
3791 		printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3792 		       PG_PROC_DIR, t->tsk->comm);
3793 		kthread_stop(p);
3794 		list_del(&t->th_list);
3795 		kfree(t);
3796 		return -EINVAL;
3797 	}
3798 
3799 	wake_up_process(p);
3800 	wait_for_completion(&t->start_done);
3801 
3802 	return 0;
3803 }
3804 
3805 /*
3806  * Removes a device from the thread if_list.
3807  */
3808 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3809 				  struct pktgen_dev *pkt_dev)
3810 {
3811 	struct list_head *q, *n;
3812 	struct pktgen_dev *p;
3813 
3814 	list_for_each_safe(q, n, &t->if_list) {
3815 		p = list_entry(q, struct pktgen_dev, list);
3816 		if (p == pkt_dev)
3817 			list_del(&p->list);
3818 	}
3819 }
3820 
3821 static int pktgen_remove_device(struct pktgen_thread *t,
3822 				struct pktgen_dev *pkt_dev)
3823 {
3824 
3825 	pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3826 
3827 	if (pkt_dev->running) {
3828 		printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3829 		       "running interface, stopping it now.\n");
3830 		pktgen_stop_device(pkt_dev);
3831 	}
3832 
3833 	/* Dis-associate from the interface */
3834 
3835 	if (pkt_dev->odev) {
3836 		dev_put(pkt_dev->odev);
3837 		pkt_dev->odev = NULL;
3838 	}
3839 
3840 	/* And update the thread if_list */
3841 
3842 	_rem_dev_from_if_list(t, pkt_dev);
3843 
3844 	if (pkt_dev->entry)
3845 		remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3846 
3847 #ifdef CONFIG_XFRM
3848 	free_SAs(pkt_dev);
3849 #endif
3850 	vfree(pkt_dev->flows);
3851 	kfree(pkt_dev);
3852 	return 0;
3853 }
3854 
3855 static int __init pg_init(void)
3856 {
3857 	int cpu;
3858 	struct proc_dir_entry *pe;
3859 
3860 	printk(KERN_INFO "%s", version);
3861 
3862 	pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3863 	if (!pg_proc_dir)
3864 		return -ENODEV;
3865 
3866 	pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3867 	if (pe == NULL) {
3868 		printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3869 		       "procfs entry.\n", PGCTRL);
3870 		proc_net_remove(&init_net, PG_PROC_DIR);
3871 		return -EINVAL;
3872 	}
3873 
3874 	/* Register us to receive netdevice events */
3875 	register_netdevice_notifier(&pktgen_notifier_block);
3876 
3877 	for_each_online_cpu(cpu) {
3878 		int err;
3879 
3880 		err = pktgen_create_thread(cpu);
3881 		if (err)
3882 			printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3883 			       "thread for cpu %d (%d)\n", cpu, err);
3884 	}
3885 
3886 	if (list_empty(&pktgen_threads)) {
3887 		printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3888 		       "all threads\n");
3889 		unregister_netdevice_notifier(&pktgen_notifier_block);
3890 		remove_proc_entry(PGCTRL, pg_proc_dir);
3891 		proc_net_remove(&init_net, PG_PROC_DIR);
3892 		return -ENODEV;
3893 	}
3894 
3895 	return 0;
3896 }
3897 
3898 static void __exit pg_cleanup(void)
3899 {
3900 	struct pktgen_thread *t;
3901 	struct list_head *q, *n;
3902 	wait_queue_head_t queue;
3903 	init_waitqueue_head(&queue);
3904 
3905 	/* Stop all interfaces & threads */
3906 
3907 	list_for_each_safe(q, n, &pktgen_threads) {
3908 		t = list_entry(q, struct pktgen_thread, th_list);
3909 		kthread_stop(t->tsk);
3910 		kfree(t);
3911 	}
3912 
3913 	/* Un-register us from receiving netdevice events */
3914 	unregister_netdevice_notifier(&pktgen_notifier_block);
3915 
3916 	/* Clean up proc file system */
3917 	remove_proc_entry(PGCTRL, pg_proc_dir);
3918 	proc_net_remove(&init_net, PG_PROC_DIR);
3919 }
3920 
3921 module_init(pg_init);
3922 module_exit(pg_cleanup);
3923 
3924 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3925 MODULE_DESCRIPTION("Packet Generator tool");
3926 MODULE_LICENSE("GPL");
3927 MODULE_VERSION(VERSION);
3928 module_param(pg_count_d, int, 0);
3929 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
3930 module_param(pg_delay_d, int, 0);
3931 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
3932 module_param(pg_clone_skb_d, int, 0);
3933 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
3934 module_param(debug, int, 0);
3935 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3936