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