[PKTGEN]: Fix double unlock of xfrm_state->lock
[linux-2.6/sactl.git] / net / core / pktgen.c
blob285ec3ed9b378179248b8d8900ad983c9ee0f210
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
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
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.
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.
23 * Additional hacking by:
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>
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.
54 * Also moved to /proc/net/pktgen/
55 * --ro
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>
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
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.
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.
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
84 * Fix refcount off by one if first packet fails, potential null deref,
85 * memleak 030710- KJP
87 * First "ranges" functionality for ipv6 030726 --ro
89 * Included flow support. 030802 ANK.
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
99 * Randy Dunlap fixed u64 printk compiler waring
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
108 * 050103
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
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>
171 #define VERSION "pktgen v2.69: Packet Generator for packet performance testing.\n"
173 /* The buckets are exponential in 'width' */
174 #define LAT_BUCKETS_MAX 32
175 #define IP_NAME_SZ 32
176 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
177 #define MPLS_STACK_BOTTOM htonl(0x00000100)
179 /* Device flag bits */
180 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
181 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
182 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
183 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
184 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
185 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
186 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
187 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
188 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
189 #define F_VID_RND (1<<9) /* Random VLAN ID */
190 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
191 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
192 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
193 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
195 /* Thread control flag bits */
196 #define T_TERMINATE (1<<0)
197 #define T_STOP (1<<1) /* Stop run */
198 #define T_RUN (1<<2) /* Start run */
199 #define T_REMDEVALL (1<<3) /* Remove all devs */
200 #define T_REMDEV (1<<4) /* Remove one dev */
202 /* If lock -- can be removed after some work */
203 #define if_lock(t) spin_lock(&(t->if_lock));
204 #define if_unlock(t) spin_unlock(&(t->if_lock));
206 /* Used to help with determining the pkts on receive */
207 #define PKTGEN_MAGIC 0xbe9be955
208 #define PG_PROC_DIR "pktgen"
209 #define PGCTRL "pgctrl"
210 static struct proc_dir_entry *pg_proc_dir = NULL;
212 #define MAX_CFLOWS 65536
214 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
215 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
217 struct flow_state {
218 __be32 cur_daddr;
219 int count;
220 #ifdef CONFIG_XFRM
221 struct xfrm_state *x;
222 #endif
223 __u32 flags;
226 /* flow flag bits */
227 #define F_INIT (1<<0) /* flow has been initialized */
229 struct pktgen_dev {
231 * Try to keep frequent/infrequent used vars. separated.
233 struct proc_dir_entry *entry; /* proc file */
234 struct pktgen_thread *pg_thread;/* the owner */
235 struct list_head list; /* Used for chaining in the thread's run-queue */
237 int running; /* if this changes to false, the test will stop */
239 /* If min != max, then we will either do a linear iteration, or
240 * we will do a random selection from within the range.
242 __u32 flags;
243 int removal_mark; /* non-zero => the device is marked for
244 * removal by worker thread */
246 int min_pkt_size; /* = ETH_ZLEN; */
247 int max_pkt_size; /* = ETH_ZLEN; */
248 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
249 int nfrags;
250 __u32 delay_us; /* Default delay */
251 __u32 delay_ns;
252 __u64 count; /* Default No packets to send */
253 __u64 sofar; /* How many pkts we've sent so far */
254 __u64 tx_bytes; /* How many bytes we've transmitted */
255 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
257 /* runtime counters relating to clone_skb */
258 __u64 next_tx_us; /* timestamp of when to tx next */
259 __u32 next_tx_ns;
261 __u64 allocated_skbs;
262 __u32 clone_count;
263 int last_ok; /* Was last skb sent?
264 * Or a failed transmit of some sort? This will keep
265 * sequence numbers in order, for example.
267 __u64 started_at; /* micro-seconds */
268 __u64 stopped_at; /* micro-seconds */
269 __u64 idle_acc; /* micro-seconds */
270 __u32 seq_num;
272 int clone_skb; /* Use multiple SKBs during packet gen. If this number
273 * is greater than 1, then that many copies of the same
274 * packet will be sent before a new packet is allocated.
275 * For instance, if you want to send 1024 identical packets
276 * before creating a new packet, set clone_skb to 1024.
279 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
284 struct in6_addr in6_saddr;
285 struct in6_addr in6_daddr;
286 struct in6_addr cur_in6_daddr;
287 struct in6_addr cur_in6_saddr;
288 /* For ranges */
289 struct in6_addr min_in6_daddr;
290 struct in6_addr max_in6_daddr;
291 struct in6_addr min_in6_saddr;
292 struct in6_addr max_in6_saddr;
294 /* If we're doing ranges, random or incremental, then this
295 * defines the min/max for those ranges.
297 __be32 saddr_min; /* inclusive, source IP address */
298 __be32 saddr_max; /* exclusive, source IP address */
299 __be32 daddr_min; /* inclusive, dest IP address */
300 __be32 daddr_max; /* exclusive, dest IP address */
302 __u16 udp_src_min; /* inclusive, source UDP port */
303 __u16 udp_src_max; /* exclusive, source UDP port */
304 __u16 udp_dst_min; /* inclusive, dest UDP port */
305 __u16 udp_dst_max; /* exclusive, dest UDP port */
307 /* DSCP + ECN */
308 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
309 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
311 /* MPLS */
312 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
313 __be32 labels[MAX_MPLS_LABELS];
315 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
316 __u8 vlan_p;
317 __u8 vlan_cfi;
318 __u16 vlan_id; /* 0xffff means no vlan tag */
320 __u8 svlan_p;
321 __u8 svlan_cfi;
322 __u16 svlan_id; /* 0xffff means no svlan tag */
324 __u32 src_mac_count; /* How many MACs to iterate through */
325 __u32 dst_mac_count; /* How many MACs to iterate through */
327 unsigned char dst_mac[ETH_ALEN];
328 unsigned char src_mac[ETH_ALEN];
330 __u32 cur_dst_mac_offset;
331 __u32 cur_src_mac_offset;
332 __be32 cur_saddr;
333 __be32 cur_daddr;
334 __u16 cur_udp_dst;
335 __u16 cur_udp_src;
336 __u16 cur_queue_map;
337 __u32 cur_pkt_size;
339 __u8 hh[14];
340 /* = {
341 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
343 We fill in SRC address later
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x08, 0x00
348 __u16 pad; /* pad out the hh struct to an even 16 bytes */
350 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
351 * are transmitting the same one multiple times
353 struct net_device *odev; /* The out-going device. Note that the device should
354 * have it's pg_info pointer pointing back to this
355 * device. This will be set when the user specifies
356 * the out-going device name (not when the inject is
357 * started as it used to do.)
359 struct flow_state *flows;
360 unsigned cflows; /* Concurrent flows (config) */
361 unsigned lflow; /* Flow length (config) */
362 unsigned nflows; /* accumulated flows (stats) */
363 unsigned curfl; /* current sequenced flow (state)*/
365 u16 queue_map_min;
366 u16 queue_map_max;
368 #ifdef CONFIG_XFRM
369 __u8 ipsmode; /* IPSEC mode (config) */
370 __u8 ipsproto; /* IPSEC type (config) */
371 #endif
372 char result[512];
375 struct pktgen_hdr {
376 __be32 pgh_magic;
377 __be32 seq_num;
378 __be32 tv_sec;
379 __be32 tv_usec;
382 struct pktgen_thread {
383 spinlock_t if_lock;
384 struct list_head if_list; /* All device here */
385 struct list_head th_list;
386 struct task_struct *tsk;
387 char result[512];
389 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
391 u32 control;
392 int cpu;
394 wait_queue_head_t queue;
397 #define REMOVE 1
398 #define FIND 0
400 /* This code works around the fact that do_div cannot handle two 64-bit
401 numbers, and regular 64-bit division doesn't work on x86 kernels.
402 --Ben
405 #define PG_DIV 0
407 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
408 * Function copied/adapted/optimized from:
410 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
412 * Copyright 1994, University of Cambridge Computer Laboratory
413 * All Rights Reserved.
416 static inline s64 divremdi3(s64 x, s64 y, int type)
418 u64 a = (x < 0) ? -x : x;
419 u64 b = (y < 0) ? -y : y;
420 u64 res = 0, d = 1;
422 if (b > 0) {
423 while (b < a) {
424 b <<= 1;
425 d <<= 1;
429 do {
430 if (a >= b) {
431 a -= b;
432 res += d;
434 b >>= 1;
435 d >>= 1;
437 while (d);
439 if (PG_DIV == type) {
440 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
441 } else {
442 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
446 /* End of hacks to deal with 64-bit math on x86 */
448 /** Convert to milliseconds */
449 static inline __u64 tv_to_ms(const struct timeval *tv)
451 __u64 ms = tv->tv_usec / 1000;
452 ms += (__u64) tv->tv_sec * (__u64) 1000;
453 return ms;
456 /** Convert to micro-seconds */
457 static inline __u64 tv_to_us(const struct timeval *tv)
459 __u64 us = tv->tv_usec;
460 us += (__u64) tv->tv_sec * (__u64) 1000000;
461 return us;
464 static inline __u64 pg_div(__u64 n, __u32 base)
466 __u64 tmp = n;
467 do_div(tmp, base);
468 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
469 n, base, tmp); */
470 return tmp;
473 static inline __u64 pg_div64(__u64 n, __u64 base)
475 __u64 tmp = n;
477 * How do we know if the architecture we are running on
478 * supports division with 64 bit base?
481 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
483 do_div(tmp, base);
484 #else
485 tmp = divremdi3(n, base, PG_DIV);
486 #endif
487 return tmp;
490 static inline __u64 getCurMs(void)
492 struct timeval tv;
493 do_gettimeofday(&tv);
494 return tv_to_ms(&tv);
497 static inline __u64 getCurUs(void)
499 struct timeval tv;
500 do_gettimeofday(&tv);
501 return tv_to_us(&tv);
504 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
506 return tv_to_us(a) - tv_to_us(b);
509 /* old include end */
511 static char version[] __initdata = VERSION;
513 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
514 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
515 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
516 const char *ifname);
517 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
518 static void pktgen_run_all_threads(void);
519 static void pktgen_stop_all_threads_ifs(void);
520 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
521 static void pktgen_stop(struct pktgen_thread *t);
522 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
524 static unsigned int scan_ip6(const char *s, char ip[16]);
525 static unsigned int fmt_ip6(char *s, const char ip[16]);
527 /* Module parameters, defaults. */
528 static int pg_count_d = 1000; /* 1000 pkts by default */
529 static int pg_delay_d;
530 static int pg_clone_skb_d;
531 static int debug;
533 static DEFINE_MUTEX(pktgen_thread_lock);
534 static LIST_HEAD(pktgen_threads);
536 static struct notifier_block pktgen_notifier_block = {
537 .notifier_call = pktgen_device_event,
541 * /proc handling functions
545 static int pgctrl_show(struct seq_file *seq, void *v)
547 seq_puts(seq, VERSION);
548 return 0;
551 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
552 size_t count, loff_t * ppos)
554 int err = 0;
555 char data[128];
557 if (!capable(CAP_NET_ADMIN)) {
558 err = -EPERM;
559 goto out;
562 if (count > sizeof(data))
563 count = sizeof(data);
565 if (copy_from_user(data, buf, count)) {
566 err = -EFAULT;
567 goto out;
569 data[count - 1] = 0; /* Make string */
571 if (!strcmp(data, "stop"))
572 pktgen_stop_all_threads_ifs();
574 else if (!strcmp(data, "start"))
575 pktgen_run_all_threads();
577 else
578 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
580 err = count;
582 out:
583 return err;
586 static int pgctrl_open(struct inode *inode, struct file *file)
588 return single_open(file, pgctrl_show, PDE(inode)->data);
591 static const struct file_operations pktgen_fops = {
592 .owner = THIS_MODULE,
593 .open = pgctrl_open,
594 .read = seq_read,
595 .llseek = seq_lseek,
596 .write = pgctrl_write,
597 .release = single_release,
600 static int pktgen_if_show(struct seq_file *seq, void *v)
602 struct pktgen_dev *pkt_dev = seq->private;
603 __u64 sa;
604 __u64 stopped;
605 __u64 now = getCurUs();
606 DECLARE_MAC_BUF(mac);
608 seq_printf(seq,
609 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
610 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
611 pkt_dev->max_pkt_size);
613 seq_printf(seq,
614 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
615 pkt_dev->nfrags,
616 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
617 pkt_dev->clone_skb, pkt_dev->odev->name);
619 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
620 pkt_dev->lflow);
622 seq_printf(seq,
623 " queue_map_min: %u queue_map_max: %u\n",
624 pkt_dev->queue_map_min,
625 pkt_dev->queue_map_max);
627 if (pkt_dev->flags & F_IPV6) {
628 char b1[128], b2[128], b3[128];
629 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
630 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
631 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
632 seq_printf(seq,
633 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
634 b2, b3);
636 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
637 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
638 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
639 seq_printf(seq,
640 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
641 b2, b3);
643 } else
644 seq_printf(seq,
645 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
646 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
647 pkt_dev->src_max);
649 seq_puts(seq, " src_mac: ");
651 seq_printf(seq, "%s ",
652 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
653 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
655 seq_printf(seq, "dst_mac: ");
656 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
658 seq_printf(seq,
659 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
660 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
661 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
663 seq_printf(seq,
664 " src_mac_count: %d dst_mac_count: %d\n",
665 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
667 if (pkt_dev->nr_labels) {
668 unsigned i;
669 seq_printf(seq, " mpls: ");
670 for (i = 0; i < pkt_dev->nr_labels; i++)
671 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
672 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
675 if (pkt_dev->vlan_id != 0xffff) {
676 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
677 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
680 if (pkt_dev->svlan_id != 0xffff) {
681 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
682 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
685 if (pkt_dev->tos) {
686 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
689 if (pkt_dev->traffic_class) {
690 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
693 seq_printf(seq, " Flags: ");
695 if (pkt_dev->flags & F_IPV6)
696 seq_printf(seq, "IPV6 ");
698 if (pkt_dev->flags & F_IPSRC_RND)
699 seq_printf(seq, "IPSRC_RND ");
701 if (pkt_dev->flags & F_IPDST_RND)
702 seq_printf(seq, "IPDST_RND ");
704 if (pkt_dev->flags & F_TXSIZE_RND)
705 seq_printf(seq, "TXSIZE_RND ");
707 if (pkt_dev->flags & F_UDPSRC_RND)
708 seq_printf(seq, "UDPSRC_RND ");
710 if (pkt_dev->flags & F_UDPDST_RND)
711 seq_printf(seq, "UDPDST_RND ");
713 if (pkt_dev->flags & F_MPLS_RND)
714 seq_printf(seq, "MPLS_RND ");
716 if (pkt_dev->flags & F_QUEUE_MAP_RND)
717 seq_printf(seq, "QUEUE_MAP_RND ");
719 if (pkt_dev->cflows) {
720 if (pkt_dev->flags & F_FLOW_SEQ)
721 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
722 else
723 seq_printf(seq, "FLOW_RND ");
726 #ifdef CONFIG_XFRM
727 if (pkt_dev->flags & F_IPSEC_ON)
728 seq_printf(seq, "IPSEC ");
729 #endif
731 if (pkt_dev->flags & F_MACSRC_RND)
732 seq_printf(seq, "MACSRC_RND ");
734 if (pkt_dev->flags & F_MACDST_RND)
735 seq_printf(seq, "MACDST_RND ");
737 if (pkt_dev->flags & F_VID_RND)
738 seq_printf(seq, "VID_RND ");
740 if (pkt_dev->flags & F_SVID_RND)
741 seq_printf(seq, "SVID_RND ");
743 seq_puts(seq, "\n");
745 sa = pkt_dev->started_at;
746 stopped = pkt_dev->stopped_at;
747 if (pkt_dev->running)
748 stopped = now; /* not really stopped, more like last-running-at */
750 seq_printf(seq,
751 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
752 (unsigned long long)pkt_dev->sofar,
753 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
754 (unsigned long long)stopped,
755 (unsigned long long)pkt_dev->idle_acc);
757 seq_printf(seq,
758 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
759 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
760 pkt_dev->cur_src_mac_offset);
762 if (pkt_dev->flags & F_IPV6) {
763 char b1[128], b2[128];
764 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
765 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
766 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
767 } else
768 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
769 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
771 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
772 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
774 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
776 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
778 if (pkt_dev->result[0])
779 seq_printf(seq, "Result: %s\n", pkt_dev->result);
780 else
781 seq_printf(seq, "Result: Idle\n");
783 return 0;
787 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
789 int i = 0;
790 *num = 0;
792 for (; i < maxlen; i++) {
793 char c;
794 *num <<= 4;
795 if (get_user(c, &user_buffer[i]))
796 return -EFAULT;
797 if ((c >= '0') && (c <= '9'))
798 *num |= c - '0';
799 else if ((c >= 'a') && (c <= 'f'))
800 *num |= c - 'a' + 10;
801 else if ((c >= 'A') && (c <= 'F'))
802 *num |= c - 'A' + 10;
803 else
804 break;
806 return i;
809 static int count_trail_chars(const char __user * user_buffer,
810 unsigned int maxlen)
812 int i;
814 for (i = 0; i < maxlen; i++) {
815 char c;
816 if (get_user(c, &user_buffer[i]))
817 return -EFAULT;
818 switch (c) {
819 case '\"':
820 case '\n':
821 case '\r':
822 case '\t':
823 case ' ':
824 case '=':
825 break;
826 default:
827 goto done;
830 done:
831 return i;
834 static unsigned long num_arg(const char __user * user_buffer,
835 unsigned long maxlen, unsigned long *num)
837 int i = 0;
838 *num = 0;
840 for (; i < maxlen; i++) {
841 char c;
842 if (get_user(c, &user_buffer[i]))
843 return -EFAULT;
844 if ((c >= '0') && (c <= '9')) {
845 *num *= 10;
846 *num += c - '0';
847 } else
848 break;
850 return i;
853 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
855 int i = 0;
857 for (; i < maxlen; i++) {
858 char c;
859 if (get_user(c, &user_buffer[i]))
860 return -EFAULT;
861 switch (c) {
862 case '\"':
863 case '\n':
864 case '\r':
865 case '\t':
866 case ' ':
867 goto done_str;
868 break;
869 default:
870 break;
873 done_str:
874 return i;
877 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
879 unsigned n = 0;
880 char c;
881 ssize_t i = 0;
882 int len;
884 pkt_dev->nr_labels = 0;
885 do {
886 __u32 tmp;
887 len = hex32_arg(&buffer[i], 8, &tmp);
888 if (len <= 0)
889 return len;
890 pkt_dev->labels[n] = htonl(tmp);
891 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
892 pkt_dev->flags |= F_MPLS_RND;
893 i += len;
894 if (get_user(c, &buffer[i]))
895 return -EFAULT;
896 i++;
897 n++;
898 if (n >= MAX_MPLS_LABELS)
899 return -E2BIG;
900 } while (c == ',');
902 pkt_dev->nr_labels = n;
903 return i;
906 static ssize_t pktgen_if_write(struct file *file,
907 const char __user * user_buffer, size_t count,
908 loff_t * offset)
910 struct seq_file *seq = (struct seq_file *)file->private_data;
911 struct pktgen_dev *pkt_dev = seq->private;
912 int i = 0, max, len;
913 char name[16], valstr[32];
914 unsigned long value = 0;
915 char *pg_result = NULL;
916 int tmp = 0;
917 char buf[128];
919 pg_result = &(pkt_dev->result[0]);
921 if (count < 1) {
922 printk(KERN_WARNING "pktgen: wrong command format\n");
923 return -EINVAL;
926 max = count - i;
927 tmp = count_trail_chars(&user_buffer[i], max);
928 if (tmp < 0) {
929 printk(KERN_WARNING "pktgen: illegal format\n");
930 return tmp;
932 i += tmp;
934 /* Read variable name */
936 len = strn_len(&user_buffer[i], sizeof(name) - 1);
937 if (len < 0) {
938 return len;
940 memset(name, 0, sizeof(name));
941 if (copy_from_user(name, &user_buffer[i], len))
942 return -EFAULT;
943 i += len;
945 max = count - i;
946 len = count_trail_chars(&user_buffer[i], max);
947 if (len < 0)
948 return len;
950 i += len;
952 if (debug) {
953 char tb[count + 1];
954 if (copy_from_user(tb, user_buffer, count))
955 return -EFAULT;
956 tb[count] = 0;
957 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
958 (unsigned long)count, tb);
961 if (!strcmp(name, "min_pkt_size")) {
962 len = num_arg(&user_buffer[i], 10, &value);
963 if (len < 0) {
964 return len;
966 i += len;
967 if (value < 14 + 20 + 8)
968 value = 14 + 20 + 8;
969 if (value != pkt_dev->min_pkt_size) {
970 pkt_dev->min_pkt_size = value;
971 pkt_dev->cur_pkt_size = value;
973 sprintf(pg_result, "OK: min_pkt_size=%u",
974 pkt_dev->min_pkt_size);
975 return count;
978 if (!strcmp(name, "max_pkt_size")) {
979 len = num_arg(&user_buffer[i], 10, &value);
980 if (len < 0) {
981 return len;
983 i += len;
984 if (value < 14 + 20 + 8)
985 value = 14 + 20 + 8;
986 if (value != pkt_dev->max_pkt_size) {
987 pkt_dev->max_pkt_size = value;
988 pkt_dev->cur_pkt_size = value;
990 sprintf(pg_result, "OK: max_pkt_size=%u",
991 pkt_dev->max_pkt_size);
992 return count;
995 /* Shortcut for min = max */
997 if (!strcmp(name, "pkt_size")) {
998 len = num_arg(&user_buffer[i], 10, &value);
999 if (len < 0) {
1000 return len;
1002 i += len;
1003 if (value < 14 + 20 + 8)
1004 value = 14 + 20 + 8;
1005 if (value != pkt_dev->min_pkt_size) {
1006 pkt_dev->min_pkt_size = value;
1007 pkt_dev->max_pkt_size = value;
1008 pkt_dev->cur_pkt_size = value;
1010 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
1011 return count;
1014 if (!strcmp(name, "debug")) {
1015 len = num_arg(&user_buffer[i], 10, &value);
1016 if (len < 0) {
1017 return len;
1019 i += len;
1020 debug = value;
1021 sprintf(pg_result, "OK: debug=%u", debug);
1022 return count;
1025 if (!strcmp(name, "frags")) {
1026 len = num_arg(&user_buffer[i], 10, &value);
1027 if (len < 0) {
1028 return len;
1030 i += len;
1031 pkt_dev->nfrags = value;
1032 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1033 return count;
1035 if (!strcmp(name, "delay")) {
1036 len = num_arg(&user_buffer[i], 10, &value);
1037 if (len < 0) {
1038 return len;
1040 i += len;
1041 if (value == 0x7FFFFFFF) {
1042 pkt_dev->delay_us = 0x7FFFFFFF;
1043 pkt_dev->delay_ns = 0;
1044 } else {
1045 pkt_dev->delay_us = value / 1000;
1046 pkt_dev->delay_ns = value % 1000;
1048 sprintf(pg_result, "OK: delay=%u",
1049 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1050 return count;
1052 if (!strcmp(name, "udp_src_min")) {
1053 len = num_arg(&user_buffer[i], 10, &value);
1054 if (len < 0) {
1055 return len;
1057 i += len;
1058 if (value != pkt_dev->udp_src_min) {
1059 pkt_dev->udp_src_min = value;
1060 pkt_dev->cur_udp_src = value;
1062 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1063 return count;
1065 if (!strcmp(name, "udp_dst_min")) {
1066 len = num_arg(&user_buffer[i], 10, &value);
1067 if (len < 0) {
1068 return len;
1070 i += len;
1071 if (value != pkt_dev->udp_dst_min) {
1072 pkt_dev->udp_dst_min = value;
1073 pkt_dev->cur_udp_dst = value;
1075 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1076 return count;
1078 if (!strcmp(name, "udp_src_max")) {
1079 len = num_arg(&user_buffer[i], 10, &value);
1080 if (len < 0) {
1081 return len;
1083 i += len;
1084 if (value != pkt_dev->udp_src_max) {
1085 pkt_dev->udp_src_max = value;
1086 pkt_dev->cur_udp_src = value;
1088 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1089 return count;
1091 if (!strcmp(name, "udp_dst_max")) {
1092 len = num_arg(&user_buffer[i], 10, &value);
1093 if (len < 0) {
1094 return len;
1096 i += len;
1097 if (value != pkt_dev->udp_dst_max) {
1098 pkt_dev->udp_dst_max = value;
1099 pkt_dev->cur_udp_dst = value;
1101 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1102 return count;
1104 if (!strcmp(name, "clone_skb")) {
1105 len = num_arg(&user_buffer[i], 10, &value);
1106 if (len < 0) {
1107 return len;
1109 i += len;
1110 pkt_dev->clone_skb = value;
1112 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1113 return count;
1115 if (!strcmp(name, "count")) {
1116 len = num_arg(&user_buffer[i], 10, &value);
1117 if (len < 0) {
1118 return len;
1120 i += len;
1121 pkt_dev->count = value;
1122 sprintf(pg_result, "OK: count=%llu",
1123 (unsigned long long)pkt_dev->count);
1124 return count;
1126 if (!strcmp(name, "src_mac_count")) {
1127 len = num_arg(&user_buffer[i], 10, &value);
1128 if (len < 0) {
1129 return len;
1131 i += len;
1132 if (pkt_dev->src_mac_count != value) {
1133 pkt_dev->src_mac_count = value;
1134 pkt_dev->cur_src_mac_offset = 0;
1136 sprintf(pg_result, "OK: src_mac_count=%d",
1137 pkt_dev->src_mac_count);
1138 return count;
1140 if (!strcmp(name, "dst_mac_count")) {
1141 len = num_arg(&user_buffer[i], 10, &value);
1142 if (len < 0) {
1143 return len;
1145 i += len;
1146 if (pkt_dev->dst_mac_count != value) {
1147 pkt_dev->dst_mac_count = value;
1148 pkt_dev->cur_dst_mac_offset = 0;
1150 sprintf(pg_result, "OK: dst_mac_count=%d",
1151 pkt_dev->dst_mac_count);
1152 return count;
1154 if (!strcmp(name, "flag")) {
1155 char f[32];
1156 memset(f, 0, 32);
1157 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1158 if (len < 0) {
1159 return len;
1161 if (copy_from_user(f, &user_buffer[i], len))
1162 return -EFAULT;
1163 i += len;
1164 if (strcmp(f, "IPSRC_RND") == 0)
1165 pkt_dev->flags |= F_IPSRC_RND;
1167 else if (strcmp(f, "!IPSRC_RND") == 0)
1168 pkt_dev->flags &= ~F_IPSRC_RND;
1170 else if (strcmp(f, "TXSIZE_RND") == 0)
1171 pkt_dev->flags |= F_TXSIZE_RND;
1173 else if (strcmp(f, "!TXSIZE_RND") == 0)
1174 pkt_dev->flags &= ~F_TXSIZE_RND;
1176 else if (strcmp(f, "IPDST_RND") == 0)
1177 pkt_dev->flags |= F_IPDST_RND;
1179 else if (strcmp(f, "!IPDST_RND") == 0)
1180 pkt_dev->flags &= ~F_IPDST_RND;
1182 else if (strcmp(f, "UDPSRC_RND") == 0)
1183 pkt_dev->flags |= F_UDPSRC_RND;
1185 else if (strcmp(f, "!UDPSRC_RND") == 0)
1186 pkt_dev->flags &= ~F_UDPSRC_RND;
1188 else if (strcmp(f, "UDPDST_RND") == 0)
1189 pkt_dev->flags |= F_UDPDST_RND;
1191 else if (strcmp(f, "!UDPDST_RND") == 0)
1192 pkt_dev->flags &= ~F_UDPDST_RND;
1194 else if (strcmp(f, "MACSRC_RND") == 0)
1195 pkt_dev->flags |= F_MACSRC_RND;
1197 else if (strcmp(f, "!MACSRC_RND") == 0)
1198 pkt_dev->flags &= ~F_MACSRC_RND;
1200 else if (strcmp(f, "MACDST_RND") == 0)
1201 pkt_dev->flags |= F_MACDST_RND;
1203 else if (strcmp(f, "!MACDST_RND") == 0)
1204 pkt_dev->flags &= ~F_MACDST_RND;
1206 else if (strcmp(f, "MPLS_RND") == 0)
1207 pkt_dev->flags |= F_MPLS_RND;
1209 else if (strcmp(f, "!MPLS_RND") == 0)
1210 pkt_dev->flags &= ~F_MPLS_RND;
1212 else if (strcmp(f, "VID_RND") == 0)
1213 pkt_dev->flags |= F_VID_RND;
1215 else if (strcmp(f, "!VID_RND") == 0)
1216 pkt_dev->flags &= ~F_VID_RND;
1218 else if (strcmp(f, "SVID_RND") == 0)
1219 pkt_dev->flags |= F_SVID_RND;
1221 else if (strcmp(f, "!SVID_RND") == 0)
1222 pkt_dev->flags &= ~F_SVID_RND;
1224 else if (strcmp(f, "FLOW_SEQ") == 0)
1225 pkt_dev->flags |= F_FLOW_SEQ;
1227 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1228 pkt_dev->flags |= F_QUEUE_MAP_RND;
1230 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1231 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1232 #ifdef CONFIG_XFRM
1233 else if (strcmp(f, "IPSEC") == 0)
1234 pkt_dev->flags |= F_IPSEC_ON;
1235 #endif
1237 else if (strcmp(f, "!IPV6") == 0)
1238 pkt_dev->flags &= ~F_IPV6;
1240 else {
1241 sprintf(pg_result,
1242 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1244 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1245 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1246 return count;
1248 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1249 return count;
1251 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1252 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1253 if (len < 0) {
1254 return len;
1257 if (copy_from_user(buf, &user_buffer[i], len))
1258 return -EFAULT;
1259 buf[len] = 0;
1260 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1261 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1262 strncpy(pkt_dev->dst_min, buf, len);
1263 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1264 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1266 if (debug)
1267 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1268 pkt_dev->dst_min);
1269 i += len;
1270 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1271 return count;
1273 if (!strcmp(name, "dst_max")) {
1274 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1275 if (len < 0) {
1276 return len;
1279 if (copy_from_user(buf, &user_buffer[i], len))
1280 return -EFAULT;
1282 buf[len] = 0;
1283 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1284 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1285 strncpy(pkt_dev->dst_max, buf, len);
1286 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1287 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1289 if (debug)
1290 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1291 pkt_dev->dst_max);
1292 i += len;
1293 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1294 return count;
1296 if (!strcmp(name, "dst6")) {
1297 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1298 if (len < 0)
1299 return len;
1301 pkt_dev->flags |= F_IPV6;
1303 if (copy_from_user(buf, &user_buffer[i], len))
1304 return -EFAULT;
1305 buf[len] = 0;
1307 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1308 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1310 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1312 if (debug)
1313 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1315 i += len;
1316 sprintf(pg_result, "OK: dst6=%s", buf);
1317 return count;
1319 if (!strcmp(name, "dst6_min")) {
1320 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1321 if (len < 0)
1322 return len;
1324 pkt_dev->flags |= F_IPV6;
1326 if (copy_from_user(buf, &user_buffer[i], len))
1327 return -EFAULT;
1328 buf[len] = 0;
1330 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1331 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1333 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1334 &pkt_dev->min_in6_daddr);
1335 if (debug)
1336 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1338 i += len;
1339 sprintf(pg_result, "OK: dst6_min=%s", buf);
1340 return count;
1342 if (!strcmp(name, "dst6_max")) {
1343 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1344 if (len < 0)
1345 return len;
1347 pkt_dev->flags |= F_IPV6;
1349 if (copy_from_user(buf, &user_buffer[i], len))
1350 return -EFAULT;
1351 buf[len] = 0;
1353 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1354 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1356 if (debug)
1357 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1359 i += len;
1360 sprintf(pg_result, "OK: dst6_max=%s", buf);
1361 return count;
1363 if (!strcmp(name, "src6")) {
1364 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1365 if (len < 0)
1366 return len;
1368 pkt_dev->flags |= F_IPV6;
1370 if (copy_from_user(buf, &user_buffer[i], len))
1371 return -EFAULT;
1372 buf[len] = 0;
1374 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1375 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1377 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1379 if (debug)
1380 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1382 i += len;
1383 sprintf(pg_result, "OK: src6=%s", buf);
1384 return count;
1386 if (!strcmp(name, "src_min")) {
1387 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1388 if (len < 0) {
1389 return len;
1391 if (copy_from_user(buf, &user_buffer[i], len))
1392 return -EFAULT;
1393 buf[len] = 0;
1394 if (strcmp(buf, pkt_dev->src_min) != 0) {
1395 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1396 strncpy(pkt_dev->src_min, buf, len);
1397 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1398 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1400 if (debug)
1401 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1402 pkt_dev->src_min);
1403 i += len;
1404 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1405 return count;
1407 if (!strcmp(name, "src_max")) {
1408 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1409 if (len < 0) {
1410 return len;
1412 if (copy_from_user(buf, &user_buffer[i], len))
1413 return -EFAULT;
1414 buf[len] = 0;
1415 if (strcmp(buf, pkt_dev->src_max) != 0) {
1416 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1417 strncpy(pkt_dev->src_max, buf, len);
1418 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1419 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1421 if (debug)
1422 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1423 pkt_dev->src_max);
1424 i += len;
1425 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1426 return count;
1428 if (!strcmp(name, "dst_mac")) {
1429 char *v = valstr;
1430 unsigned char old_dmac[ETH_ALEN];
1431 unsigned char *m = pkt_dev->dst_mac;
1432 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1434 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1435 if (len < 0) {
1436 return len;
1438 memset(valstr, 0, sizeof(valstr));
1439 if (copy_from_user(valstr, &user_buffer[i], len))
1440 return -EFAULT;
1441 i += len;
1443 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1444 if (*v >= '0' && *v <= '9') {
1445 *m *= 16;
1446 *m += *v - '0';
1448 if (*v >= 'A' && *v <= 'F') {
1449 *m *= 16;
1450 *m += *v - 'A' + 10;
1452 if (*v >= 'a' && *v <= 'f') {
1453 *m *= 16;
1454 *m += *v - 'a' + 10;
1456 if (*v == ':') {
1457 m++;
1458 *m = 0;
1462 /* Set up Dest MAC */
1463 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1464 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1466 sprintf(pg_result, "OK: dstmac");
1467 return count;
1469 if (!strcmp(name, "src_mac")) {
1470 char *v = valstr;
1471 unsigned char old_smac[ETH_ALEN];
1472 unsigned char *m = pkt_dev->src_mac;
1474 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1476 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1477 if (len < 0) {
1478 return len;
1480 memset(valstr, 0, sizeof(valstr));
1481 if (copy_from_user(valstr, &user_buffer[i], len))
1482 return -EFAULT;
1483 i += len;
1485 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1486 if (*v >= '0' && *v <= '9') {
1487 *m *= 16;
1488 *m += *v - '0';
1490 if (*v >= 'A' && *v <= 'F') {
1491 *m *= 16;
1492 *m += *v - 'A' + 10;
1494 if (*v >= 'a' && *v <= 'f') {
1495 *m *= 16;
1496 *m += *v - 'a' + 10;
1498 if (*v == ':') {
1499 m++;
1500 *m = 0;
1504 /* Set up Src MAC */
1505 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1506 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1508 sprintf(pg_result, "OK: srcmac");
1509 return count;
1512 if (!strcmp(name, "clear_counters")) {
1513 pktgen_clear_counters(pkt_dev);
1514 sprintf(pg_result, "OK: Clearing counters.\n");
1515 return count;
1518 if (!strcmp(name, "flows")) {
1519 len = num_arg(&user_buffer[i], 10, &value);
1520 if (len < 0) {
1521 return len;
1523 i += len;
1524 if (value > MAX_CFLOWS)
1525 value = MAX_CFLOWS;
1527 pkt_dev->cflows = value;
1528 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1529 return count;
1532 if (!strcmp(name, "flowlen")) {
1533 len = num_arg(&user_buffer[i], 10, &value);
1534 if (len < 0) {
1535 return len;
1537 i += len;
1538 pkt_dev->lflow = value;
1539 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1540 return count;
1543 if (!strcmp(name, "queue_map_min")) {
1544 len = num_arg(&user_buffer[i], 5, &value);
1545 if (len < 0) {
1546 return len;
1548 i += len;
1549 pkt_dev->queue_map_min = value;
1550 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1551 return count;
1554 if (!strcmp(name, "queue_map_max")) {
1555 len = num_arg(&user_buffer[i], 5, &value);
1556 if (len < 0) {
1557 return len;
1559 i += len;
1560 pkt_dev->queue_map_max = value;
1561 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1562 return count;
1565 if (!strcmp(name, "mpls")) {
1566 unsigned n, cnt;
1568 len = get_labels(&user_buffer[i], pkt_dev);
1569 if (len < 0)
1570 return len;
1571 i += len;
1572 cnt = sprintf(pg_result, "OK: mpls=");
1573 for (n = 0; n < pkt_dev->nr_labels; n++)
1574 cnt += sprintf(pg_result + cnt,
1575 "%08x%s", ntohl(pkt_dev->labels[n]),
1576 n == pkt_dev->nr_labels-1 ? "" : ",");
1578 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1579 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1580 pkt_dev->svlan_id = 0xffff;
1582 if (debug)
1583 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1585 return count;
1588 if (!strcmp(name, "vlan_id")) {
1589 len = num_arg(&user_buffer[i], 4, &value);
1590 if (len < 0) {
1591 return len;
1593 i += len;
1594 if (value <= 4095) {
1595 pkt_dev->vlan_id = value; /* turn on VLAN */
1597 if (debug)
1598 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1600 if (debug && pkt_dev->nr_labels)
1601 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1603 pkt_dev->nr_labels = 0; /* turn off MPLS */
1604 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1605 } else {
1606 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1607 pkt_dev->svlan_id = 0xffff;
1609 if (debug)
1610 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1612 return count;
1615 if (!strcmp(name, "vlan_p")) {
1616 len = num_arg(&user_buffer[i], 1, &value);
1617 if (len < 0) {
1618 return len;
1620 i += len;
1621 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1622 pkt_dev->vlan_p = value;
1623 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1624 } else {
1625 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1627 return count;
1630 if (!strcmp(name, "vlan_cfi")) {
1631 len = num_arg(&user_buffer[i], 1, &value);
1632 if (len < 0) {
1633 return len;
1635 i += len;
1636 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1637 pkt_dev->vlan_cfi = value;
1638 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1639 } else {
1640 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1642 return count;
1645 if (!strcmp(name, "svlan_id")) {
1646 len = num_arg(&user_buffer[i], 4, &value);
1647 if (len < 0) {
1648 return len;
1650 i += len;
1651 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1652 pkt_dev->svlan_id = value; /* turn on SVLAN */
1654 if (debug)
1655 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1657 if (debug && pkt_dev->nr_labels)
1658 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1660 pkt_dev->nr_labels = 0; /* turn off MPLS */
1661 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1662 } else {
1663 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1664 pkt_dev->svlan_id = 0xffff;
1666 if (debug)
1667 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1669 return count;
1672 if (!strcmp(name, "svlan_p")) {
1673 len = num_arg(&user_buffer[i], 1, &value);
1674 if (len < 0) {
1675 return len;
1677 i += len;
1678 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1679 pkt_dev->svlan_p = value;
1680 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1681 } else {
1682 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1684 return count;
1687 if (!strcmp(name, "svlan_cfi")) {
1688 len = num_arg(&user_buffer[i], 1, &value);
1689 if (len < 0) {
1690 return len;
1692 i += len;
1693 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1694 pkt_dev->svlan_cfi = value;
1695 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1696 } else {
1697 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1699 return count;
1702 if (!strcmp(name, "tos")) {
1703 __u32 tmp_value = 0;
1704 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1705 if (len < 0) {
1706 return len;
1708 i += len;
1709 if (len == 2) {
1710 pkt_dev->tos = tmp_value;
1711 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1712 } else {
1713 sprintf(pg_result, "ERROR: tos must be 00-ff");
1715 return count;
1718 if (!strcmp(name, "traffic_class")) {
1719 __u32 tmp_value = 0;
1720 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1721 if (len < 0) {
1722 return len;
1724 i += len;
1725 if (len == 2) {
1726 pkt_dev->traffic_class = tmp_value;
1727 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1728 } else {
1729 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1731 return count;
1734 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1735 return -EINVAL;
1738 static int pktgen_if_open(struct inode *inode, struct file *file)
1740 return single_open(file, pktgen_if_show, PDE(inode)->data);
1743 static const struct file_operations pktgen_if_fops = {
1744 .owner = THIS_MODULE,
1745 .open = pktgen_if_open,
1746 .read = seq_read,
1747 .llseek = seq_lseek,
1748 .write = pktgen_if_write,
1749 .release = single_release,
1752 static int pktgen_thread_show(struct seq_file *seq, void *v)
1754 struct pktgen_thread *t = seq->private;
1755 struct pktgen_dev *pkt_dev;
1757 BUG_ON(!t);
1759 seq_printf(seq, "Running: ");
1761 if_lock(t);
1762 list_for_each_entry(pkt_dev, &t->if_list, list)
1763 if (pkt_dev->running)
1764 seq_printf(seq, "%s ", pkt_dev->odev->name);
1766 seq_printf(seq, "\nStopped: ");
1768 list_for_each_entry(pkt_dev, &t->if_list, list)
1769 if (!pkt_dev->running)
1770 seq_printf(seq, "%s ", pkt_dev->odev->name);
1772 if (t->result[0])
1773 seq_printf(seq, "\nResult: %s\n", t->result);
1774 else
1775 seq_printf(seq, "\nResult: NA\n");
1777 if_unlock(t);
1779 return 0;
1782 static ssize_t pktgen_thread_write(struct file *file,
1783 const char __user * user_buffer,
1784 size_t count, loff_t * offset)
1786 struct seq_file *seq = (struct seq_file *)file->private_data;
1787 struct pktgen_thread *t = seq->private;
1788 int i = 0, max, len, ret;
1789 char name[40];
1790 char *pg_result;
1792 if (count < 1) {
1793 // sprintf(pg_result, "Wrong command format");
1794 return -EINVAL;
1797 max = count - i;
1798 len = count_trail_chars(&user_buffer[i], max);
1799 if (len < 0)
1800 return len;
1802 i += len;
1804 /* Read variable name */
1806 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1807 if (len < 0)
1808 return len;
1810 memset(name, 0, sizeof(name));
1811 if (copy_from_user(name, &user_buffer[i], len))
1812 return -EFAULT;
1813 i += len;
1815 max = count - i;
1816 len = count_trail_chars(&user_buffer[i], max);
1817 if (len < 0)
1818 return len;
1820 i += len;
1822 if (debug)
1823 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1824 name, (unsigned long)count);
1826 if (!t) {
1827 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1828 ret = -EINVAL;
1829 goto out;
1832 pg_result = &(t->result[0]);
1834 if (!strcmp(name, "add_device")) {
1835 char f[32];
1836 memset(f, 0, 32);
1837 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1838 if (len < 0) {
1839 ret = len;
1840 goto out;
1842 if (copy_from_user(f, &user_buffer[i], len))
1843 return -EFAULT;
1844 i += len;
1845 mutex_lock(&pktgen_thread_lock);
1846 pktgen_add_device(t, f);
1847 mutex_unlock(&pktgen_thread_lock);
1848 ret = count;
1849 sprintf(pg_result, "OK: add_device=%s", f);
1850 goto out;
1853 if (!strcmp(name, "rem_device_all")) {
1854 mutex_lock(&pktgen_thread_lock);
1855 t->control |= T_REMDEVALL;
1856 mutex_unlock(&pktgen_thread_lock);
1857 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1858 ret = count;
1859 sprintf(pg_result, "OK: rem_device_all");
1860 goto out;
1863 if (!strcmp(name, "max_before_softirq")) {
1864 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1865 ret = count;
1866 goto out;
1869 ret = -EINVAL;
1870 out:
1871 return ret;
1874 static int pktgen_thread_open(struct inode *inode, struct file *file)
1876 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1879 static const struct file_operations pktgen_thread_fops = {
1880 .owner = THIS_MODULE,
1881 .open = pktgen_thread_open,
1882 .read = seq_read,
1883 .llseek = seq_lseek,
1884 .write = pktgen_thread_write,
1885 .release = single_release,
1888 /* Think find or remove for NN */
1889 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1891 struct pktgen_thread *t;
1892 struct pktgen_dev *pkt_dev = NULL;
1894 list_for_each_entry(t, &pktgen_threads, th_list) {
1895 pkt_dev = pktgen_find_dev(t, ifname);
1896 if (pkt_dev) {
1897 if (remove) {
1898 if_lock(t);
1899 pkt_dev->removal_mark = 1;
1900 t->control |= T_REMDEV;
1901 if_unlock(t);
1903 break;
1906 return pkt_dev;
1910 * mark a device for removal
1912 static void pktgen_mark_device(const char *ifname)
1914 struct pktgen_dev *pkt_dev = NULL;
1915 const int max_tries = 10, msec_per_try = 125;
1916 int i = 0;
1918 mutex_lock(&pktgen_thread_lock);
1919 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1921 while (1) {
1923 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1924 if (pkt_dev == NULL)
1925 break; /* success */
1927 mutex_unlock(&pktgen_thread_lock);
1928 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1929 "to disappear....\n", ifname);
1930 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1931 mutex_lock(&pktgen_thread_lock);
1933 if (++i >= max_tries) {
1934 printk(KERN_ERR "pktgen_mark_device: timed out after "
1935 "waiting %d msec for device %s to be removed\n",
1936 msec_per_try * i, ifname);
1937 break;
1942 mutex_unlock(&pktgen_thread_lock);
1945 static void pktgen_change_name(struct net_device *dev)
1947 struct pktgen_thread *t;
1949 list_for_each_entry(t, &pktgen_threads, th_list) {
1950 struct pktgen_dev *pkt_dev;
1952 list_for_each_entry(pkt_dev, &t->if_list, list) {
1953 if (pkt_dev->odev != dev)
1954 continue;
1956 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1958 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1959 pg_proc_dir);
1960 if (!pkt_dev->entry)
1961 printk(KERN_ERR "pktgen: can't move proc "
1962 " entry for '%s'\n", dev->name);
1963 break;
1968 static int pktgen_device_event(struct notifier_block *unused,
1969 unsigned long event, void *ptr)
1971 struct net_device *dev = ptr;
1973 if (dev->nd_net != &init_net)
1974 return NOTIFY_DONE;
1976 /* It is OK that we do not hold the group lock right now,
1977 * as we run under the RTNL lock.
1980 switch (event) {
1981 case NETDEV_CHANGENAME:
1982 pktgen_change_name(dev);
1983 break;
1985 case NETDEV_UNREGISTER:
1986 pktgen_mark_device(dev->name);
1987 break;
1990 return NOTIFY_DONE;
1993 /* Associate pktgen_dev with a device. */
1995 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1997 struct net_device *odev;
1998 int err;
2000 /* Clean old setups */
2001 if (pkt_dev->odev) {
2002 dev_put(pkt_dev->odev);
2003 pkt_dev->odev = NULL;
2006 odev = dev_get_by_name(&init_net, ifname);
2007 if (!odev) {
2008 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
2009 return -ENODEV;
2012 if (odev->type != ARPHRD_ETHER) {
2013 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
2014 err = -EINVAL;
2015 } else if (!netif_running(odev)) {
2016 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
2017 err = -ENETDOWN;
2018 } else {
2019 pkt_dev->odev = odev;
2020 return 0;
2023 dev_put(odev);
2024 return err;
2027 /* Read pkt_dev from the interface and set up internal pktgen_dev
2028 * structure to have the right information to create/send packets
2030 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2032 if (!pkt_dev->odev) {
2033 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
2034 "setup_inject.\n");
2035 sprintf(pkt_dev->result,
2036 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2037 return;
2040 /* Default to the interface's mac if not explicitly set. */
2042 if (is_zero_ether_addr(pkt_dev->src_mac))
2043 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2045 /* Set up Dest MAC */
2046 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2048 /* Set up pkt size */
2049 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2051 if (pkt_dev->flags & F_IPV6) {
2053 * Skip this automatic address setting until locks or functions
2054 * gets exported
2057 #ifdef NOTNOW
2058 int i, set = 0, err = 1;
2059 struct inet6_dev *idev;
2061 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2062 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2063 set = 1;
2064 break;
2067 if (!set) {
2070 * Use linklevel address if unconfigured.
2072 * use ipv6_get_lladdr if/when it's get exported
2075 rcu_read_lock();
2076 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2077 struct inet6_ifaddr *ifp;
2079 read_lock_bh(&idev->lock);
2080 for (ifp = idev->addr_list; ifp;
2081 ifp = ifp->if_next) {
2082 if (ifp->scope == IFA_LINK
2083 && !(ifp->
2084 flags & IFA_F_TENTATIVE)) {
2085 ipv6_addr_copy(&pkt_dev->
2086 cur_in6_saddr,
2087 &ifp->addr);
2088 err = 0;
2089 break;
2092 read_unlock_bh(&idev->lock);
2094 rcu_read_unlock();
2095 if (err)
2096 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2097 "address not availble.\n");
2099 #endif
2100 } else {
2101 pkt_dev->saddr_min = 0;
2102 pkt_dev->saddr_max = 0;
2103 if (strlen(pkt_dev->src_min) == 0) {
2105 struct in_device *in_dev;
2107 rcu_read_lock();
2108 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2109 if (in_dev) {
2110 if (in_dev->ifa_list) {
2111 pkt_dev->saddr_min =
2112 in_dev->ifa_list->ifa_address;
2113 pkt_dev->saddr_max = pkt_dev->saddr_min;
2116 rcu_read_unlock();
2117 } else {
2118 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2119 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2122 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2123 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2125 /* Initialize current values. */
2126 pkt_dev->cur_dst_mac_offset = 0;
2127 pkt_dev->cur_src_mac_offset = 0;
2128 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2129 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2130 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2131 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2132 pkt_dev->nflows = 0;
2135 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2137 __u64 start;
2138 __u64 now;
2140 start = now = getCurUs();
2141 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2142 while (now < spin_until_us) {
2143 /* TODO: optimize sleeping behavior */
2144 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2145 schedule_timeout_interruptible(1);
2146 else if (spin_until_us - now > 100) {
2147 if (!pkt_dev->running)
2148 return;
2149 if (need_resched())
2150 schedule();
2153 now = getCurUs();
2156 pkt_dev->idle_acc += now - start;
2159 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2161 pkt_dev->pkt_overhead = 0;
2162 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2163 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2164 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2167 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2170 if (pkt_dev->flows[flow].flags & F_INIT)
2171 return 1;
2172 else
2173 return 0;
2176 static inline int f_pick(struct pktgen_dev *pkt_dev)
2178 int flow = pkt_dev->curfl;
2180 if (pkt_dev->flags & F_FLOW_SEQ) {
2181 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2182 /* reset time */
2183 pkt_dev->flows[flow].count = 0;
2184 pkt_dev->curfl += 1;
2185 if (pkt_dev->curfl >= pkt_dev->cflows)
2186 pkt_dev->curfl = 0; /*reset */
2188 } else {
2189 flow = random32() % pkt_dev->cflows;
2191 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2192 pkt_dev->flows[flow].count = 0;
2195 return pkt_dev->curfl;
2199 #ifdef CONFIG_XFRM
2200 /* If there was already an IPSEC SA, we keep it as is, else
2201 * we go look for it ...
2203 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2205 struct xfrm_state *x = pkt_dev->flows[flow].x;
2206 if (!x) {
2207 /*slow path: we dont already have xfrm_state*/
2208 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2209 (xfrm_address_t *)&pkt_dev->cur_saddr,
2210 AF_INET,
2211 pkt_dev->ipsmode,
2212 pkt_dev->ipsproto, 0);
2213 if (x) {
2214 pkt_dev->flows[flow].x = x;
2215 set_pkt_overhead(pkt_dev);
2216 pkt_dev->pkt_overhead+=x->props.header_len;
2221 #endif
2222 /* Increment/randomize headers according to flags and current values
2223 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2225 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2227 __u32 imn;
2228 __u32 imx;
2229 int flow = 0;
2231 if (pkt_dev->cflows)
2232 flow = f_pick(pkt_dev);
2234 /* Deal with source MAC */
2235 if (pkt_dev->src_mac_count > 1) {
2236 __u32 mc;
2237 __u32 tmp;
2239 if (pkt_dev->flags & F_MACSRC_RND)
2240 mc = random32() % pkt_dev->src_mac_count;
2241 else {
2242 mc = pkt_dev->cur_src_mac_offset++;
2243 if (pkt_dev->cur_src_mac_offset >
2244 pkt_dev->src_mac_count)
2245 pkt_dev->cur_src_mac_offset = 0;
2248 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2249 pkt_dev->hh[11] = tmp;
2250 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2251 pkt_dev->hh[10] = tmp;
2252 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2253 pkt_dev->hh[9] = tmp;
2254 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2255 pkt_dev->hh[8] = tmp;
2256 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2257 pkt_dev->hh[7] = tmp;
2260 /* Deal with Destination MAC */
2261 if (pkt_dev->dst_mac_count > 1) {
2262 __u32 mc;
2263 __u32 tmp;
2265 if (pkt_dev->flags & F_MACDST_RND)
2266 mc = random32() % pkt_dev->dst_mac_count;
2268 else {
2269 mc = pkt_dev->cur_dst_mac_offset++;
2270 if (pkt_dev->cur_dst_mac_offset >
2271 pkt_dev->dst_mac_count) {
2272 pkt_dev->cur_dst_mac_offset = 0;
2276 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2277 pkt_dev->hh[5] = tmp;
2278 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2279 pkt_dev->hh[4] = tmp;
2280 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2281 pkt_dev->hh[3] = tmp;
2282 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2283 pkt_dev->hh[2] = tmp;
2284 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2285 pkt_dev->hh[1] = tmp;
2288 if (pkt_dev->flags & F_MPLS_RND) {
2289 unsigned i;
2290 for (i = 0; i < pkt_dev->nr_labels; i++)
2291 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2292 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2293 ((__force __be32)random32() &
2294 htonl(0x000fffff));
2297 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2298 pkt_dev->vlan_id = random32() & (4096-1);
2301 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2302 pkt_dev->svlan_id = random32() & (4096 - 1);
2305 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2306 if (pkt_dev->flags & F_UDPSRC_RND)
2307 pkt_dev->cur_udp_src = random32() %
2308 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2309 + pkt_dev->udp_src_min;
2311 else {
2312 pkt_dev->cur_udp_src++;
2313 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2314 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2318 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2319 if (pkt_dev->flags & F_UDPDST_RND) {
2320 pkt_dev->cur_udp_dst = random32() %
2321 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2322 + pkt_dev->udp_dst_min;
2323 } else {
2324 pkt_dev->cur_udp_dst++;
2325 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2326 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2330 if (!(pkt_dev->flags & F_IPV6)) {
2332 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2333 ntohl(pkt_dev->
2334 saddr_max))) {
2335 __u32 t;
2336 if (pkt_dev->flags & F_IPSRC_RND)
2337 t = random32() % (imx - imn) + imn;
2338 else {
2339 t = ntohl(pkt_dev->cur_saddr);
2340 t++;
2341 if (t > imx) {
2342 t = imn;
2345 pkt_dev->cur_saddr = htonl(t);
2348 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2349 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2350 } else {
2351 imn = ntohl(pkt_dev->daddr_min);
2352 imx = ntohl(pkt_dev->daddr_max);
2353 if (imn < imx) {
2354 __u32 t;
2355 __be32 s;
2356 if (pkt_dev->flags & F_IPDST_RND) {
2358 t = random32() % (imx - imn) + imn;
2359 s = htonl(t);
2361 while (LOOPBACK(s) || MULTICAST(s)
2362 || BADCLASS(s) || ZERONET(s)
2363 || LOCAL_MCAST(s)) {
2364 t = random32() % (imx - imn) + imn;
2365 s = htonl(t);
2367 pkt_dev->cur_daddr = s;
2368 } else {
2369 t = ntohl(pkt_dev->cur_daddr);
2370 t++;
2371 if (t > imx) {
2372 t = imn;
2374 pkt_dev->cur_daddr = htonl(t);
2377 if (pkt_dev->cflows) {
2378 pkt_dev->flows[flow].flags |= F_INIT;
2379 pkt_dev->flows[flow].cur_daddr =
2380 pkt_dev->cur_daddr;
2381 #ifdef CONFIG_XFRM
2382 if (pkt_dev->flags & F_IPSEC_ON)
2383 get_ipsec_sa(pkt_dev, flow);
2384 #endif
2385 pkt_dev->nflows++;
2388 } else { /* IPV6 * */
2390 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2391 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2392 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2393 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2394 else {
2395 int i;
2397 /* Only random destinations yet */
2399 for (i = 0; i < 4; i++) {
2400 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2401 (((__force __be32)random32() |
2402 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2403 pkt_dev->max_in6_daddr.s6_addr32[i]);
2408 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2409 __u32 t;
2410 if (pkt_dev->flags & F_TXSIZE_RND) {
2411 t = random32() %
2412 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2413 + pkt_dev->min_pkt_size;
2414 } else {
2415 t = pkt_dev->cur_pkt_size + 1;
2416 if (t > pkt_dev->max_pkt_size)
2417 t = pkt_dev->min_pkt_size;
2419 pkt_dev->cur_pkt_size = t;
2422 if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2423 __u16 t;
2424 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2425 t = random32() %
2426 (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1)
2427 + pkt_dev->queue_map_min;
2428 } else {
2429 t = pkt_dev->cur_queue_map + 1;
2430 if (t > pkt_dev->queue_map_max)
2431 t = pkt_dev->queue_map_min;
2433 pkt_dev->cur_queue_map = t;
2436 pkt_dev->flows[flow].count++;
2440 #ifdef CONFIG_XFRM
2441 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2443 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2444 int err = 0;
2445 struct iphdr *iph;
2447 if (!x)
2448 return 0;
2449 /* XXX: we dont support tunnel mode for now until
2450 * we resolve the dst issue */
2451 if (x->props.mode != XFRM_MODE_TRANSPORT)
2452 return 0;
2454 spin_lock(&x->lock);
2455 iph = ip_hdr(skb);
2457 err = x->outer_mode->output(x, skb);
2458 if (err)
2459 goto error;
2460 err = x->type->output(x, skb);
2461 if (err)
2462 goto error;
2464 x->curlft.bytes +=skb->len;
2465 x->curlft.packets++;
2466 error:
2467 spin_unlock(&x->lock);
2468 return err;
2471 static inline void free_SAs(struct pktgen_dev *pkt_dev)
2473 if (pkt_dev->cflows) {
2474 /* let go of the SAs if we have them */
2475 int i = 0;
2476 for (; i < pkt_dev->nflows; i++){
2477 struct xfrm_state *x = pkt_dev->flows[i].x;
2478 if (x) {
2479 xfrm_state_put(x);
2480 pkt_dev->flows[i].x = NULL;
2486 static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2487 struct sk_buff *skb, __be16 protocol)
2489 if (pkt_dev->flags & F_IPSEC_ON) {
2490 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2491 int nhead = 0;
2492 if (x) {
2493 int ret;
2494 __u8 *eth;
2495 nhead = x->props.header_len - skb_headroom(skb);
2496 if (nhead >0) {
2497 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2498 if (ret < 0) {
2499 printk(KERN_ERR "Error expanding "
2500 "ipsec packet %d\n",ret);
2501 return 0;
2505 /* ipsec is not expecting ll header */
2506 skb_pull(skb, ETH_HLEN);
2507 ret = pktgen_output_ipsec(skb, pkt_dev);
2508 if (ret) {
2509 printk(KERN_ERR "Error creating ipsec "
2510 "packet %d\n",ret);
2511 kfree_skb(skb);
2512 return 0;
2514 /* restore ll */
2515 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2516 memcpy(eth, pkt_dev->hh, 12);
2517 *(u16 *) & eth[12] = protocol;
2520 return 1;
2522 #endif
2524 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2526 unsigned i;
2527 for (i = 0; i < pkt_dev->nr_labels; i++) {
2528 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2530 mpls--;
2531 *mpls |= MPLS_STACK_BOTTOM;
2534 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2535 unsigned int prio)
2537 return htons(id | (cfi << 12) | (prio << 13));
2540 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2541 struct pktgen_dev *pkt_dev)
2543 struct sk_buff *skb = NULL;
2544 __u8 *eth;
2545 struct udphdr *udph;
2546 int datalen, iplen;
2547 struct iphdr *iph;
2548 struct pktgen_hdr *pgh = NULL;
2549 __be16 protocol = htons(ETH_P_IP);
2550 __be32 *mpls;
2551 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2552 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2553 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2554 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2557 if (pkt_dev->nr_labels)
2558 protocol = htons(ETH_P_MPLS_UC);
2560 if (pkt_dev->vlan_id != 0xffff)
2561 protocol = htons(ETH_P_8021Q);
2563 /* Update any of the values, used when we're incrementing various
2564 * fields.
2566 mod_cur_headers(pkt_dev);
2568 datalen = (odev->hard_header_len + 16) & ~0xf;
2569 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2570 pkt_dev->pkt_overhead, GFP_ATOMIC);
2571 if (!skb) {
2572 sprintf(pkt_dev->result, "No memory");
2573 return NULL;
2576 skb_reserve(skb, datalen);
2578 /* Reserve for ethernet and IP header */
2579 eth = (__u8 *) skb_push(skb, 14);
2580 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2581 if (pkt_dev->nr_labels)
2582 mpls_push(mpls, pkt_dev);
2584 if (pkt_dev->vlan_id != 0xffff) {
2585 if (pkt_dev->svlan_id != 0xffff) {
2586 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2587 *svlan_tci = build_tci(pkt_dev->svlan_id,
2588 pkt_dev->svlan_cfi,
2589 pkt_dev->svlan_p);
2590 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2591 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2593 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2594 *vlan_tci = build_tci(pkt_dev->vlan_id,
2595 pkt_dev->vlan_cfi,
2596 pkt_dev->vlan_p);
2597 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2598 *vlan_encapsulated_proto = htons(ETH_P_IP);
2601 skb->network_header = skb->tail;
2602 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2603 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2604 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
2605 iph = ip_hdr(skb);
2606 udph = udp_hdr(skb);
2608 memcpy(eth, pkt_dev->hh, 12);
2609 *(__be16 *) & eth[12] = protocol;
2611 /* Eth + IPh + UDPh + mpls */
2612 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2613 pkt_dev->pkt_overhead;
2614 if (datalen < sizeof(struct pktgen_hdr))
2615 datalen = sizeof(struct pktgen_hdr);
2617 udph->source = htons(pkt_dev->cur_udp_src);
2618 udph->dest = htons(pkt_dev->cur_udp_dst);
2619 udph->len = htons(datalen + 8); /* DATA + udphdr */
2620 udph->check = 0; /* No checksum */
2622 iph->ihl = 5;
2623 iph->version = 4;
2624 iph->ttl = 32;
2625 iph->tos = pkt_dev->tos;
2626 iph->protocol = IPPROTO_UDP; /* UDP */
2627 iph->saddr = pkt_dev->cur_saddr;
2628 iph->daddr = pkt_dev->cur_daddr;
2629 iph->frag_off = 0;
2630 iplen = 20 + 8 + datalen;
2631 iph->tot_len = htons(iplen);
2632 iph->check = 0;
2633 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2634 skb->protocol = protocol;
2635 skb->mac_header = (skb->network_header - ETH_HLEN -
2636 pkt_dev->pkt_overhead);
2637 skb->dev = odev;
2638 skb->pkt_type = PACKET_HOST;
2640 if (pkt_dev->nfrags <= 0)
2641 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2642 else {
2643 int frags = pkt_dev->nfrags;
2644 int i;
2646 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2648 if (frags > MAX_SKB_FRAGS)
2649 frags = MAX_SKB_FRAGS;
2650 if (datalen > frags * PAGE_SIZE) {
2651 skb_put(skb, datalen - frags * PAGE_SIZE);
2652 datalen = frags * PAGE_SIZE;
2655 i = 0;
2656 while (datalen > 0) {
2657 struct page *page = alloc_pages(GFP_KERNEL, 0);
2658 skb_shinfo(skb)->frags[i].page = page;
2659 skb_shinfo(skb)->frags[i].page_offset = 0;
2660 skb_shinfo(skb)->frags[i].size =
2661 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2662 datalen -= skb_shinfo(skb)->frags[i].size;
2663 skb->len += skb_shinfo(skb)->frags[i].size;
2664 skb->data_len += skb_shinfo(skb)->frags[i].size;
2665 i++;
2666 skb_shinfo(skb)->nr_frags = i;
2669 while (i < frags) {
2670 int rem;
2672 if (i == 0)
2673 break;
2675 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2676 if (rem == 0)
2677 break;
2679 skb_shinfo(skb)->frags[i - 1].size -= rem;
2681 skb_shinfo(skb)->frags[i] =
2682 skb_shinfo(skb)->frags[i - 1];
2683 get_page(skb_shinfo(skb)->frags[i].page);
2684 skb_shinfo(skb)->frags[i].page =
2685 skb_shinfo(skb)->frags[i - 1].page;
2686 skb_shinfo(skb)->frags[i].page_offset +=
2687 skb_shinfo(skb)->frags[i - 1].size;
2688 skb_shinfo(skb)->frags[i].size = rem;
2689 i++;
2690 skb_shinfo(skb)->nr_frags = i;
2694 /* Stamp the time, and sequence number, convert them to network byte order */
2696 if (pgh) {
2697 struct timeval timestamp;
2699 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2700 pgh->seq_num = htonl(pkt_dev->seq_num);
2702 do_gettimeofday(&timestamp);
2703 pgh->tv_sec = htonl(timestamp.tv_sec);
2704 pgh->tv_usec = htonl(timestamp.tv_usec);
2707 #ifdef CONFIG_XFRM
2708 if (!process_ipsec(pkt_dev, skb, protocol))
2709 return NULL;
2710 #endif
2712 return skb;
2716 * scan_ip6, fmt_ip taken from dietlibc-0.21
2717 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2719 * Slightly modified for kernel.
2720 * Should be candidate for net/ipv4/utils.c
2721 * --ro
2724 static unsigned int scan_ip6(const char *s, char ip[16])
2726 unsigned int i;
2727 unsigned int len = 0;
2728 unsigned long u;
2729 char suffix[16];
2730 unsigned int prefixlen = 0;
2731 unsigned int suffixlen = 0;
2732 __be32 tmp;
2733 char *pos;
2735 for (i = 0; i < 16; i++)
2736 ip[i] = 0;
2738 for (;;) {
2739 if (*s == ':') {
2740 len++;
2741 if (s[1] == ':') { /* Found "::", skip to part 2 */
2742 s += 2;
2743 len++;
2744 break;
2746 s++;
2749 u = simple_strtoul(s, &pos, 16);
2750 i = pos - s;
2751 if (!i)
2752 return 0;
2753 if (prefixlen == 12 && s[i] == '.') {
2755 /* the last 4 bytes may be written as IPv4 address */
2757 tmp = in_aton(s);
2758 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2759 return i + len;
2761 ip[prefixlen++] = (u >> 8);
2762 ip[prefixlen++] = (u & 255);
2763 s += i;
2764 len += i;
2765 if (prefixlen == 16)
2766 return len;
2769 /* part 2, after "::" */
2770 for (;;) {
2771 if (*s == ':') {
2772 if (suffixlen == 0)
2773 break;
2774 s++;
2775 len++;
2776 } else if (suffixlen != 0)
2777 break;
2779 u = simple_strtol(s, &pos, 16);
2780 i = pos - s;
2781 if (!i) {
2782 if (*s)
2783 len--;
2784 break;
2786 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2787 tmp = in_aton(s);
2788 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2789 sizeof(tmp));
2790 suffixlen += 4;
2791 len += strlen(s);
2792 break;
2794 suffix[suffixlen++] = (u >> 8);
2795 suffix[suffixlen++] = (u & 255);
2796 s += i;
2797 len += i;
2798 if (prefixlen + suffixlen == 16)
2799 break;
2801 for (i = 0; i < suffixlen; i++)
2802 ip[16 - suffixlen + i] = suffix[i];
2803 return len;
2806 static char tohex(char hexdigit)
2808 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2811 static int fmt_xlong(char *s, unsigned int i)
2813 char *bak = s;
2814 *s = tohex((i >> 12) & 0xf);
2815 if (s != bak || *s != '0')
2816 ++s;
2817 *s = tohex((i >> 8) & 0xf);
2818 if (s != bak || *s != '0')
2819 ++s;
2820 *s = tohex((i >> 4) & 0xf);
2821 if (s != bak || *s != '0')
2822 ++s;
2823 *s = tohex(i & 0xf);
2824 return s - bak + 1;
2827 static unsigned int fmt_ip6(char *s, const char ip[16])
2829 unsigned int len;
2830 unsigned int i;
2831 unsigned int temp;
2832 unsigned int compressing;
2833 int j;
2835 len = 0;
2836 compressing = 0;
2837 for (j = 0; j < 16; j += 2) {
2839 #ifdef V4MAPPEDPREFIX
2840 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2841 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2842 temp = strlen(s);
2843 return len + temp;
2845 #endif
2846 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2847 (unsigned long)(unsigned char)ip[j + 1];
2848 if (temp == 0) {
2849 if (!compressing) {
2850 compressing = 1;
2851 if (j == 0) {
2852 *s++ = ':';
2853 ++len;
2856 } else {
2857 if (compressing) {
2858 compressing = 0;
2859 *s++ = ':';
2860 ++len;
2862 i = fmt_xlong(s, temp);
2863 len += i;
2864 s += i;
2865 if (j < 14) {
2866 *s++ = ':';
2867 ++len;
2871 if (compressing) {
2872 *s++ = ':';
2873 ++len;
2875 *s = 0;
2876 return len;
2879 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2880 struct pktgen_dev *pkt_dev)
2882 struct sk_buff *skb = NULL;
2883 __u8 *eth;
2884 struct udphdr *udph;
2885 int datalen;
2886 struct ipv6hdr *iph;
2887 struct pktgen_hdr *pgh = NULL;
2888 __be16 protocol = htons(ETH_P_IPV6);
2889 __be32 *mpls;
2890 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2891 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2892 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2893 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2895 if (pkt_dev->nr_labels)
2896 protocol = htons(ETH_P_MPLS_UC);
2898 if (pkt_dev->vlan_id != 0xffff)
2899 protocol = htons(ETH_P_8021Q);
2901 /* Update any of the values, used when we're incrementing various
2902 * fields.
2904 mod_cur_headers(pkt_dev);
2906 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2907 pkt_dev->pkt_overhead, GFP_ATOMIC);
2908 if (!skb) {
2909 sprintf(pkt_dev->result, "No memory");
2910 return NULL;
2913 skb_reserve(skb, 16);
2915 /* Reserve for ethernet and IP header */
2916 eth = (__u8 *) skb_push(skb, 14);
2917 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2918 if (pkt_dev->nr_labels)
2919 mpls_push(mpls, pkt_dev);
2921 if (pkt_dev->vlan_id != 0xffff) {
2922 if (pkt_dev->svlan_id != 0xffff) {
2923 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2924 *svlan_tci = build_tci(pkt_dev->svlan_id,
2925 pkt_dev->svlan_cfi,
2926 pkt_dev->svlan_p);
2927 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2928 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2930 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2931 *vlan_tci = build_tci(pkt_dev->vlan_id,
2932 pkt_dev->vlan_cfi,
2933 pkt_dev->vlan_p);
2934 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2935 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2938 skb->network_header = skb->tail;
2939 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2940 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2941 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
2942 iph = ipv6_hdr(skb);
2943 udph = udp_hdr(skb);
2945 memcpy(eth, pkt_dev->hh, 12);
2946 *(__be16 *) & eth[12] = protocol;
2948 /* Eth + IPh + UDPh + mpls */
2949 datalen = pkt_dev->cur_pkt_size - 14 -
2950 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2951 pkt_dev->pkt_overhead;
2953 if (datalen < sizeof(struct pktgen_hdr)) {
2954 datalen = sizeof(struct pktgen_hdr);
2955 if (net_ratelimit())
2956 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2957 datalen);
2960 udph->source = htons(pkt_dev->cur_udp_src);
2961 udph->dest = htons(pkt_dev->cur_udp_dst);
2962 udph->len = htons(datalen + sizeof(struct udphdr));
2963 udph->check = 0; /* No checksum */
2965 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2967 if (pkt_dev->traffic_class) {
2968 /* Version + traffic class + flow (0) */
2969 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2972 iph->hop_limit = 32;
2974 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2975 iph->nexthdr = IPPROTO_UDP;
2977 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2978 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2980 skb->mac_header = (skb->network_header - ETH_HLEN -
2981 pkt_dev->pkt_overhead);
2982 skb->protocol = protocol;
2983 skb->dev = odev;
2984 skb->pkt_type = PACKET_HOST;
2986 if (pkt_dev->nfrags <= 0)
2987 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2988 else {
2989 int frags = pkt_dev->nfrags;
2990 int i;
2992 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2994 if (frags > MAX_SKB_FRAGS)
2995 frags = MAX_SKB_FRAGS;
2996 if (datalen > frags * PAGE_SIZE) {
2997 skb_put(skb, datalen - frags * PAGE_SIZE);
2998 datalen = frags * PAGE_SIZE;
3001 i = 0;
3002 while (datalen > 0) {
3003 struct page *page = alloc_pages(GFP_KERNEL, 0);
3004 skb_shinfo(skb)->frags[i].page = page;
3005 skb_shinfo(skb)->frags[i].page_offset = 0;
3006 skb_shinfo(skb)->frags[i].size =
3007 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
3008 datalen -= skb_shinfo(skb)->frags[i].size;
3009 skb->len += skb_shinfo(skb)->frags[i].size;
3010 skb->data_len += skb_shinfo(skb)->frags[i].size;
3011 i++;
3012 skb_shinfo(skb)->nr_frags = i;
3015 while (i < frags) {
3016 int rem;
3018 if (i == 0)
3019 break;
3021 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3022 if (rem == 0)
3023 break;
3025 skb_shinfo(skb)->frags[i - 1].size -= rem;
3027 skb_shinfo(skb)->frags[i] =
3028 skb_shinfo(skb)->frags[i - 1];
3029 get_page(skb_shinfo(skb)->frags[i].page);
3030 skb_shinfo(skb)->frags[i].page =
3031 skb_shinfo(skb)->frags[i - 1].page;
3032 skb_shinfo(skb)->frags[i].page_offset +=
3033 skb_shinfo(skb)->frags[i - 1].size;
3034 skb_shinfo(skb)->frags[i].size = rem;
3035 i++;
3036 skb_shinfo(skb)->nr_frags = i;
3040 /* Stamp the time, and sequence number, convert them to network byte order */
3041 /* should we update cloned packets too ? */
3042 if (pgh) {
3043 struct timeval timestamp;
3045 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3046 pgh->seq_num = htonl(pkt_dev->seq_num);
3048 do_gettimeofday(&timestamp);
3049 pgh->tv_sec = htonl(timestamp.tv_sec);
3050 pgh->tv_usec = htonl(timestamp.tv_usec);
3052 /* pkt_dev->seq_num++; FF: you really mean this? */
3054 return skb;
3057 static inline struct sk_buff *fill_packet(struct net_device *odev,
3058 struct pktgen_dev *pkt_dev)
3060 if (pkt_dev->flags & F_IPV6)
3061 return fill_packet_ipv6(odev, pkt_dev);
3062 else
3063 return fill_packet_ipv4(odev, pkt_dev);
3066 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3068 pkt_dev->seq_num = 1;
3069 pkt_dev->idle_acc = 0;
3070 pkt_dev->sofar = 0;
3071 pkt_dev->tx_bytes = 0;
3072 pkt_dev->errors = 0;
3075 /* Set up structure for sending pkts, clear counters */
3077 static void pktgen_run(struct pktgen_thread *t)
3079 struct pktgen_dev *pkt_dev;
3080 int started = 0;
3082 pr_debug("pktgen: entering pktgen_run. %p\n", t);
3084 if_lock(t);
3085 list_for_each_entry(pkt_dev, &t->if_list, list) {
3088 * setup odev and create initial packet.
3090 pktgen_setup_inject(pkt_dev);
3092 if (pkt_dev->odev) {
3093 pktgen_clear_counters(pkt_dev);
3094 pkt_dev->running = 1; /* Cranke yeself! */
3095 pkt_dev->skb = NULL;
3096 pkt_dev->started_at = getCurUs();
3097 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
3098 pkt_dev->next_tx_ns = 0;
3099 set_pkt_overhead(pkt_dev);
3101 strcpy(pkt_dev->result, "Starting");
3102 started++;
3103 } else
3104 strcpy(pkt_dev->result, "Error starting");
3106 if_unlock(t);
3107 if (started)
3108 t->control &= ~(T_STOP);
3111 static void pktgen_stop_all_threads_ifs(void)
3113 struct pktgen_thread *t;
3115 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3117 mutex_lock(&pktgen_thread_lock);
3119 list_for_each_entry(t, &pktgen_threads, th_list)
3120 t->control |= T_STOP;
3122 mutex_unlock(&pktgen_thread_lock);
3125 static int thread_is_running(struct pktgen_thread *t)
3127 struct pktgen_dev *pkt_dev;
3128 int res = 0;
3130 list_for_each_entry(pkt_dev, &t->if_list, list)
3131 if (pkt_dev->running) {
3132 res = 1;
3133 break;
3135 return res;
3138 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3140 if_lock(t);
3142 while (thread_is_running(t)) {
3144 if_unlock(t);
3146 msleep_interruptible(100);
3148 if (signal_pending(current))
3149 goto signal;
3150 if_lock(t);
3152 if_unlock(t);
3153 return 1;
3154 signal:
3155 return 0;
3158 static int pktgen_wait_all_threads_run(void)
3160 struct pktgen_thread *t;
3161 int sig = 1;
3163 mutex_lock(&pktgen_thread_lock);
3165 list_for_each_entry(t, &pktgen_threads, th_list) {
3166 sig = pktgen_wait_thread_run(t);
3167 if (sig == 0)
3168 break;
3171 if (sig == 0)
3172 list_for_each_entry(t, &pktgen_threads, th_list)
3173 t->control |= (T_STOP);
3175 mutex_unlock(&pktgen_thread_lock);
3176 return sig;
3179 static void pktgen_run_all_threads(void)
3181 struct pktgen_thread *t;
3183 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3185 mutex_lock(&pktgen_thread_lock);
3187 list_for_each_entry(t, &pktgen_threads, th_list)
3188 t->control |= (T_RUN);
3190 mutex_unlock(&pktgen_thread_lock);
3192 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3194 pktgen_wait_all_threads_run();
3197 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3199 __u64 total_us, bps, mbps, pps, idle;
3200 char *p = pkt_dev->result;
3202 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3204 idle = pkt_dev->idle_acc;
3206 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3207 (unsigned long long)total_us,
3208 (unsigned long long)(total_us - idle),
3209 (unsigned long long)idle,
3210 (unsigned long long)pkt_dev->sofar,
3211 pkt_dev->cur_pkt_size, nr_frags);
3213 pps = pkt_dev->sofar * USEC_PER_SEC;
3215 while ((total_us >> 32) != 0) {
3216 pps >>= 1;
3217 total_us >>= 1;
3220 do_div(pps, total_us);
3222 bps = pps * 8 * pkt_dev->cur_pkt_size;
3224 mbps = bps;
3225 do_div(mbps, 1000000);
3226 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3227 (unsigned long long)pps,
3228 (unsigned long long)mbps,
3229 (unsigned long long)bps,
3230 (unsigned long long)pkt_dev->errors);
3233 /* Set stopped-at timer, remove from running list, do counters & statistics */
3235 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3237 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3239 if (!pkt_dev->running) {
3240 printk(KERN_WARNING "pktgen: interface: %s is already "
3241 "stopped\n", pkt_dev->odev->name);
3242 return -EINVAL;
3245 pkt_dev->stopped_at = getCurUs();
3246 pkt_dev->running = 0;
3248 show_results(pkt_dev, nr_frags);
3250 return 0;
3253 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3255 struct pktgen_dev *pkt_dev, *best = NULL;
3257 if_lock(t);
3259 list_for_each_entry(pkt_dev, &t->if_list, list) {
3260 if (!pkt_dev->running)
3261 continue;
3262 if (best == NULL)
3263 best = pkt_dev;
3264 else if (pkt_dev->next_tx_us < best->next_tx_us)
3265 best = pkt_dev;
3267 if_unlock(t);
3268 return best;
3271 static void pktgen_stop(struct pktgen_thread *t)
3273 struct pktgen_dev *pkt_dev;
3275 pr_debug("pktgen: entering pktgen_stop\n");
3277 if_lock(t);
3279 list_for_each_entry(pkt_dev, &t->if_list, list) {
3280 pktgen_stop_device(pkt_dev);
3281 if (pkt_dev->skb)
3282 kfree_skb(pkt_dev->skb);
3284 pkt_dev->skb = NULL;
3287 if_unlock(t);
3291 * one of our devices needs to be removed - find it
3292 * and remove it
3294 static void pktgen_rem_one_if(struct pktgen_thread *t)
3296 struct list_head *q, *n;
3297 struct pktgen_dev *cur;
3299 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3301 if_lock(t);
3303 list_for_each_safe(q, n, &t->if_list) {
3304 cur = list_entry(q, struct pktgen_dev, list);
3306 if (!cur->removal_mark)
3307 continue;
3309 if (cur->skb)
3310 kfree_skb(cur->skb);
3311 cur->skb = NULL;
3313 pktgen_remove_device(t, cur);
3315 break;
3318 if_unlock(t);
3321 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3323 struct list_head *q, *n;
3324 struct pktgen_dev *cur;
3326 /* Remove all devices, free mem */
3328 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3329 if_lock(t);
3331 list_for_each_safe(q, n, &t->if_list) {
3332 cur = list_entry(q, struct pktgen_dev, list);
3334 if (cur->skb)
3335 kfree_skb(cur->skb);
3336 cur->skb = NULL;
3338 pktgen_remove_device(t, cur);
3341 if_unlock(t);
3344 static void pktgen_rem_thread(struct pktgen_thread *t)
3346 /* Remove from the thread list */
3348 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3350 mutex_lock(&pktgen_thread_lock);
3352 list_del(&t->th_list);
3354 mutex_unlock(&pktgen_thread_lock);
3357 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3359 struct net_device *odev = NULL;
3360 __u64 idle_start = 0;
3361 int ret;
3363 odev = pkt_dev->odev;
3365 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3366 u64 now;
3368 now = getCurUs();
3369 if (now < pkt_dev->next_tx_us)
3370 spin(pkt_dev, pkt_dev->next_tx_us);
3372 /* This is max DELAY, this has special meaning of
3373 * "never transmit"
3375 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3376 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3377 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3378 goto out;
3382 if ((netif_queue_stopped(odev) ||
3383 (pkt_dev->skb &&
3384 netif_subqueue_stopped(odev, pkt_dev->skb))) ||
3385 need_resched()) {
3386 idle_start = getCurUs();
3388 if (!netif_running(odev)) {
3389 pktgen_stop_device(pkt_dev);
3390 if (pkt_dev->skb)
3391 kfree_skb(pkt_dev->skb);
3392 pkt_dev->skb = NULL;
3393 goto out;
3395 if (need_resched())
3396 schedule();
3398 pkt_dev->idle_acc += getCurUs() - idle_start;
3400 if (netif_queue_stopped(odev) ||
3401 netif_subqueue_stopped(odev, pkt_dev->skb)) {
3402 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3403 pkt_dev->next_tx_ns = 0;
3404 goto out; /* Try the next interface */
3408 if (pkt_dev->last_ok || !pkt_dev->skb) {
3409 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3410 || (!pkt_dev->skb)) {
3411 /* build a new pkt */
3412 if (pkt_dev->skb)
3413 kfree_skb(pkt_dev->skb);
3415 pkt_dev->skb = fill_packet(odev, pkt_dev);
3416 if (pkt_dev->skb == NULL) {
3417 printk(KERN_ERR "pktgen: ERROR: couldn't "
3418 "allocate skb in fill_packet.\n");
3419 schedule();
3420 pkt_dev->clone_count--; /* back out increment, OOM */
3421 goto out;
3423 pkt_dev->allocated_skbs++;
3424 pkt_dev->clone_count = 0; /* reset counter */
3428 netif_tx_lock_bh(odev);
3429 if (!netif_queue_stopped(odev) &&
3430 !netif_subqueue_stopped(odev, pkt_dev->skb)) {
3432 atomic_inc(&(pkt_dev->skb->users));
3433 retry_now:
3434 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3435 if (likely(ret == NETDEV_TX_OK)) {
3436 pkt_dev->last_ok = 1;
3437 pkt_dev->sofar++;
3438 pkt_dev->seq_num++;
3439 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3441 } else if (ret == NETDEV_TX_LOCKED
3442 && (odev->features & NETIF_F_LLTX)) {
3443 cpu_relax();
3444 goto retry_now;
3445 } else { /* Retry it next time */
3447 atomic_dec(&(pkt_dev->skb->users));
3449 if (debug && net_ratelimit())
3450 printk(KERN_INFO "pktgen: Hard xmit error\n");
3452 pkt_dev->errors++;
3453 pkt_dev->last_ok = 0;
3456 pkt_dev->next_tx_us = getCurUs();
3457 pkt_dev->next_tx_ns = 0;
3459 pkt_dev->next_tx_us += pkt_dev->delay_us;
3460 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3462 if (pkt_dev->next_tx_ns > 1000) {
3463 pkt_dev->next_tx_us++;
3464 pkt_dev->next_tx_ns -= 1000;
3468 else { /* Retry it next time */
3469 pkt_dev->last_ok = 0;
3470 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3471 pkt_dev->next_tx_ns = 0;
3474 netif_tx_unlock_bh(odev);
3476 /* If pkt_dev->count is zero, then run forever */
3477 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3478 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3479 idle_start = getCurUs();
3480 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3481 if (signal_pending(current)) {
3482 break;
3484 schedule();
3486 pkt_dev->idle_acc += getCurUs() - idle_start;
3489 /* Done with this */
3490 pktgen_stop_device(pkt_dev);
3491 if (pkt_dev->skb)
3492 kfree_skb(pkt_dev->skb);
3493 pkt_dev->skb = NULL;
3495 out:;
3499 * Main loop of the thread goes here
3502 static int pktgen_thread_worker(void *arg)
3504 DEFINE_WAIT(wait);
3505 struct pktgen_thread *t = arg;
3506 struct pktgen_dev *pkt_dev = NULL;
3507 int cpu = t->cpu;
3509 BUG_ON(smp_processor_id() != cpu);
3511 init_waitqueue_head(&t->queue);
3513 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3515 set_current_state(TASK_INTERRUPTIBLE);
3517 set_freezable();
3519 while (!kthread_should_stop()) {
3520 pkt_dev = next_to_run(t);
3522 if (!pkt_dev &&
3523 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3524 == 0) {
3525 prepare_to_wait(&(t->queue), &wait,
3526 TASK_INTERRUPTIBLE);
3527 schedule_timeout(HZ / 10);
3528 finish_wait(&(t->queue), &wait);
3531 __set_current_state(TASK_RUNNING);
3533 if (pkt_dev)
3534 pktgen_xmit(pkt_dev);
3536 if (t->control & T_STOP) {
3537 pktgen_stop(t);
3538 t->control &= ~(T_STOP);
3541 if (t->control & T_RUN) {
3542 pktgen_run(t);
3543 t->control &= ~(T_RUN);
3546 if (t->control & T_REMDEVALL) {
3547 pktgen_rem_all_ifs(t);
3548 t->control &= ~(T_REMDEVALL);
3551 if (t->control & T_REMDEV) {
3552 pktgen_rem_one_if(t);
3553 t->control &= ~(T_REMDEV);
3556 try_to_freeze();
3558 set_current_state(TASK_INTERRUPTIBLE);
3561 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3562 pktgen_stop(t);
3564 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3565 pktgen_rem_all_ifs(t);
3567 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3568 pktgen_rem_thread(t);
3570 return 0;
3573 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3574 const char *ifname)
3576 struct pktgen_dev *p, *pkt_dev = NULL;
3577 if_lock(t);
3579 list_for_each_entry(p, &t->if_list, list)
3580 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3581 pkt_dev = p;
3582 break;
3585 if_unlock(t);
3586 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3587 return pkt_dev;
3591 * Adds a dev at front of if_list.
3594 static int add_dev_to_thread(struct pktgen_thread *t,
3595 struct pktgen_dev *pkt_dev)
3597 int rv = 0;
3599 if_lock(t);
3601 if (pkt_dev->pg_thread) {
3602 printk(KERN_ERR "pktgen: ERROR: already assigned "
3603 "to a thread.\n");
3604 rv = -EBUSY;
3605 goto out;
3608 list_add(&pkt_dev->list, &t->if_list);
3609 pkt_dev->pg_thread = t;
3610 pkt_dev->running = 0;
3612 out:
3613 if_unlock(t);
3614 return rv;
3617 /* Called under thread lock */
3619 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3621 struct pktgen_dev *pkt_dev;
3622 int err;
3624 /* We don't allow a device to be on several threads */
3626 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3627 if (pkt_dev) {
3628 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3629 return -EBUSY;
3632 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3633 if (!pkt_dev)
3634 return -ENOMEM;
3636 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3637 if (pkt_dev->flows == NULL) {
3638 kfree(pkt_dev);
3639 return -ENOMEM;
3641 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3643 pkt_dev->removal_mark = 0;
3644 pkt_dev->min_pkt_size = ETH_ZLEN;
3645 pkt_dev->max_pkt_size = ETH_ZLEN;
3646 pkt_dev->nfrags = 0;
3647 pkt_dev->clone_skb = pg_clone_skb_d;
3648 pkt_dev->delay_us = pg_delay_d / 1000;
3649 pkt_dev->delay_ns = pg_delay_d % 1000;
3650 pkt_dev->count = pg_count_d;
3651 pkt_dev->sofar = 0;
3652 pkt_dev->udp_src_min = 9; /* sink port */
3653 pkt_dev->udp_src_max = 9;
3654 pkt_dev->udp_dst_min = 9;
3655 pkt_dev->udp_dst_max = 9;
3657 pkt_dev->vlan_p = 0;
3658 pkt_dev->vlan_cfi = 0;
3659 pkt_dev->vlan_id = 0xffff;
3660 pkt_dev->svlan_p = 0;
3661 pkt_dev->svlan_cfi = 0;
3662 pkt_dev->svlan_id = 0xffff;
3664 err = pktgen_setup_dev(pkt_dev, ifname);
3665 if (err)
3666 goto out1;
3668 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3669 if (!pkt_dev->entry) {
3670 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3671 PG_PROC_DIR, ifname);
3672 err = -EINVAL;
3673 goto out2;
3675 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3676 pkt_dev->entry->data = pkt_dev;
3677 #ifdef CONFIG_XFRM
3678 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3679 pkt_dev->ipsproto = IPPROTO_ESP;
3680 #endif
3682 return add_dev_to_thread(t, pkt_dev);
3683 out2:
3684 dev_put(pkt_dev->odev);
3685 out1:
3686 #ifdef CONFIG_XFRM
3687 free_SAs(pkt_dev);
3688 #endif
3689 if (pkt_dev->flows)
3690 vfree(pkt_dev->flows);
3691 kfree(pkt_dev);
3692 return err;
3695 static int __init pktgen_create_thread(int cpu)
3697 struct pktgen_thread *t;
3698 struct proc_dir_entry *pe;
3699 struct task_struct *p;
3701 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3702 if (!t) {
3703 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3704 "create new thread.\n");
3705 return -ENOMEM;
3708 spin_lock_init(&t->if_lock);
3709 t->cpu = cpu;
3711 INIT_LIST_HEAD(&t->if_list);
3713 list_add_tail(&t->th_list, &pktgen_threads);
3715 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3716 if (IS_ERR(p)) {
3717 printk(KERN_ERR "pktgen: kernel_thread() failed "
3718 "for cpu %d\n", t->cpu);
3719 list_del(&t->th_list);
3720 kfree(t);
3721 return PTR_ERR(p);
3723 kthread_bind(p, cpu);
3724 t->tsk = p;
3726 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3727 if (!pe) {
3728 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3729 PG_PROC_DIR, t->tsk->comm);
3730 kthread_stop(p);
3731 list_del(&t->th_list);
3732 kfree(t);
3733 return -EINVAL;
3736 pe->proc_fops = &pktgen_thread_fops;
3737 pe->data = t;
3739 wake_up_process(p);
3741 return 0;
3745 * Removes a device from the thread if_list.
3747 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3748 struct pktgen_dev *pkt_dev)
3750 struct list_head *q, *n;
3751 struct pktgen_dev *p;
3753 list_for_each_safe(q, n, &t->if_list) {
3754 p = list_entry(q, struct pktgen_dev, list);
3755 if (p == pkt_dev)
3756 list_del(&p->list);
3760 static int pktgen_remove_device(struct pktgen_thread *t,
3761 struct pktgen_dev *pkt_dev)
3764 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3766 if (pkt_dev->running) {
3767 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3768 "running interface, stopping it now.\n");
3769 pktgen_stop_device(pkt_dev);
3772 /* Dis-associate from the interface */
3774 if (pkt_dev->odev) {
3775 dev_put(pkt_dev->odev);
3776 pkt_dev->odev = NULL;
3779 /* And update the thread if_list */
3781 _rem_dev_from_if_list(t, pkt_dev);
3783 if (pkt_dev->entry)
3784 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3786 #ifdef CONFIG_XFRM
3787 free_SAs(pkt_dev);
3788 #endif
3789 if (pkt_dev->flows)
3790 vfree(pkt_dev->flows);
3791 kfree(pkt_dev);
3792 return 0;
3795 static int __init pg_init(void)
3797 int cpu;
3798 struct proc_dir_entry *pe;
3800 printk(KERN_INFO "%s", version);
3802 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3803 if (!pg_proc_dir)
3804 return -ENODEV;
3805 pg_proc_dir->owner = THIS_MODULE;
3807 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3808 if (pe == NULL) {
3809 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3810 "procfs entry.\n", PGCTRL);
3811 proc_net_remove(&init_net, PG_PROC_DIR);
3812 return -EINVAL;
3815 pe->proc_fops = &pktgen_fops;
3816 pe->data = NULL;
3818 /* Register us to receive netdevice events */
3819 register_netdevice_notifier(&pktgen_notifier_block);
3821 for_each_online_cpu(cpu) {
3822 int err;
3824 err = pktgen_create_thread(cpu);
3825 if (err)
3826 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3827 "thread for cpu %d (%d)\n", cpu, err);
3830 if (list_empty(&pktgen_threads)) {
3831 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3832 "all threads\n");
3833 unregister_netdevice_notifier(&pktgen_notifier_block);
3834 remove_proc_entry(PGCTRL, pg_proc_dir);
3835 proc_net_remove(&init_net, PG_PROC_DIR);
3836 return -ENODEV;
3839 return 0;
3842 static void __exit pg_cleanup(void)
3844 struct pktgen_thread *t;
3845 struct list_head *q, *n;
3846 wait_queue_head_t queue;
3847 init_waitqueue_head(&queue);
3849 /* Stop all interfaces & threads */
3851 list_for_each_safe(q, n, &pktgen_threads) {
3852 t = list_entry(q, struct pktgen_thread, th_list);
3853 kthread_stop(t->tsk);
3854 kfree(t);
3857 /* Un-register us from receiving netdevice events */
3858 unregister_netdevice_notifier(&pktgen_notifier_block);
3860 /* Clean up proc file system */
3861 remove_proc_entry(PGCTRL, pg_proc_dir);
3862 proc_net_remove(&init_net, PG_PROC_DIR);
3865 module_init(pg_init);
3866 module_exit(pg_cleanup);
3868 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3869 MODULE_DESCRIPTION("Packet Generator tool");
3870 MODULE_LICENSE("GPL");
3871 module_param(pg_count_d, int, 0);
3872 module_param(pg_delay_d, int, 0);
3873 module_param(pg_clone_skb_d, int, 0);
3874 module_param(debug, int, 0);