e1000: Add device IDs of blade version of the 82571 quad port
[pv_ops_mirror.git] / net / core / pktgen.c
blob803d0c8826af006047c19e198e7d242192d02b81
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/checksum.h>
156 #include <net/ipv6.h>
157 #include <net/addrconf.h>
158 #ifdef CONFIG_XFRM
159 #include <net/xfrm.h>
160 #endif
161 #include <asm/byteorder.h>
162 #include <linux/rcupdate.h>
163 #include <asm/bitops.h>
164 #include <asm/io.h>
165 #include <asm/dma.h>
166 #include <asm/uaccess.h>
167 #include <asm/div64.h> /* do_div */
168 #include <asm/timex.h>
170 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
172 /* The buckets are exponential in 'width' */
173 #define LAT_BUCKETS_MAX 32
174 #define IP_NAME_SZ 32
175 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
176 #define MPLS_STACK_BOTTOM htonl(0x00000100)
178 /* Device flag bits */
179 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
180 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
181 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
182 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
183 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
184 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
185 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
186 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
187 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
188 #define F_VID_RND (1<<9) /* Random VLAN ID */
189 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
190 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
191 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
193 /* Thread control flag bits */
194 #define T_TERMINATE (1<<0)
195 #define T_STOP (1<<1) /* Stop run */
196 #define T_RUN (1<<2) /* Start run */
197 #define T_REMDEVALL (1<<3) /* Remove all devs */
198 #define T_REMDEV (1<<4) /* Remove one dev */
200 /* If lock -- can be removed after some work */
201 #define if_lock(t) spin_lock(&(t->if_lock));
202 #define if_unlock(t) spin_unlock(&(t->if_lock));
204 /* Used to help with determining the pkts on receive */
205 #define PKTGEN_MAGIC 0xbe9be955
206 #define PG_PROC_DIR "pktgen"
207 #define PGCTRL "pgctrl"
208 static struct proc_dir_entry *pg_proc_dir = NULL;
210 #define MAX_CFLOWS 65536
212 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
213 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
215 struct flow_state {
216 __be32 cur_daddr;
217 int count;
218 #ifdef CONFIG_XFRM
219 struct xfrm_state *x;
220 #endif
221 __u32 flags;
224 /* flow flag bits */
225 #define F_INIT (1<<0) /* flow has been initialized */
227 struct pktgen_dev {
229 * Try to keep frequent/infrequent used vars. separated.
231 struct proc_dir_entry *entry; /* proc file */
232 struct pktgen_thread *pg_thread;/* the owner */
233 struct list_head list; /* Used for chaining in the thread's run-queue */
235 int running; /* if this changes to false, the test will stop */
237 /* If min != max, then we will either do a linear iteration, or
238 * we will do a random selection from within the range.
240 __u32 flags;
241 int removal_mark; /* non-zero => the device is marked for
242 * removal by worker thread */
244 int min_pkt_size; /* = ETH_ZLEN; */
245 int max_pkt_size; /* = ETH_ZLEN; */
246 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
247 int nfrags;
248 __u32 delay_us; /* Default delay */
249 __u32 delay_ns;
250 __u64 count; /* Default No packets to send */
251 __u64 sofar; /* How many pkts we've sent so far */
252 __u64 tx_bytes; /* How many bytes we've transmitted */
253 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
255 /* runtime counters relating to clone_skb */
256 __u64 next_tx_us; /* timestamp of when to tx next */
257 __u32 next_tx_ns;
259 __u64 allocated_skbs;
260 __u32 clone_count;
261 int last_ok; /* Was last skb sent?
262 * Or a failed transmit of some sort? This will keep
263 * sequence numbers in order, for example.
265 __u64 started_at; /* micro-seconds */
266 __u64 stopped_at; /* micro-seconds */
267 __u64 idle_acc; /* micro-seconds */
268 __u32 seq_num;
270 int clone_skb; /* Use multiple SKBs during packet gen. If this number
271 * is greater than 1, then that many copies of the same
272 * packet will be sent before a new packet is allocated.
273 * For instance, if you want to send 1024 identical packets
274 * before creating a new packet, set clone_skb to 1024.
277 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
278 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 struct in6_addr in6_saddr;
283 struct in6_addr in6_daddr;
284 struct in6_addr cur_in6_daddr;
285 struct in6_addr cur_in6_saddr;
286 /* For ranges */
287 struct in6_addr min_in6_daddr;
288 struct in6_addr max_in6_daddr;
289 struct in6_addr min_in6_saddr;
290 struct in6_addr max_in6_saddr;
292 /* If we're doing ranges, random or incremental, then this
293 * defines the min/max for those ranges.
295 __be32 saddr_min; /* inclusive, source IP address */
296 __be32 saddr_max; /* exclusive, source IP address */
297 __be32 daddr_min; /* inclusive, dest IP address */
298 __be32 daddr_max; /* exclusive, dest IP address */
300 __u16 udp_src_min; /* inclusive, source UDP port */
301 __u16 udp_src_max; /* exclusive, source UDP port */
302 __u16 udp_dst_min; /* inclusive, dest UDP port */
303 __u16 udp_dst_max; /* exclusive, dest UDP port */
305 /* DSCP + ECN */
306 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
307 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
309 /* MPLS */
310 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
311 __be32 labels[MAX_MPLS_LABELS];
313 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
314 __u8 vlan_p;
315 __u8 vlan_cfi;
316 __u16 vlan_id; /* 0xffff means no vlan tag */
318 __u8 svlan_p;
319 __u8 svlan_cfi;
320 __u16 svlan_id; /* 0xffff means no svlan tag */
322 __u32 src_mac_count; /* How many MACs to iterate through */
323 __u32 dst_mac_count; /* How many MACs to iterate through */
325 unsigned char dst_mac[ETH_ALEN];
326 unsigned char src_mac[ETH_ALEN];
328 __u32 cur_dst_mac_offset;
329 __u32 cur_src_mac_offset;
330 __be32 cur_saddr;
331 __be32 cur_daddr;
332 __u16 cur_udp_dst;
333 __u16 cur_udp_src;
334 __u32 cur_pkt_size;
336 __u8 hh[14];
337 /* = {
338 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
340 We fill in SRC address later
341 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
342 0x08, 0x00
345 __u16 pad; /* pad out the hh struct to an even 16 bytes */
347 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
348 * are transmitting the same one multiple times
350 struct net_device *odev; /* The out-going device. Note that the device should
351 * have it's pg_info pointer pointing back to this
352 * device. This will be set when the user specifies
353 * the out-going device name (not when the inject is
354 * started as it used to do.)
356 struct flow_state *flows;
357 unsigned cflows; /* Concurrent flows (config) */
358 unsigned lflow; /* Flow length (config) */
359 unsigned nflows; /* accumulated flows (stats) */
360 unsigned curfl; /* current sequenced flow (state)*/
361 #ifdef CONFIG_XFRM
362 __u8 ipsmode; /* IPSEC mode (config) */
363 __u8 ipsproto; /* IPSEC type (config) */
364 #endif
365 char result[512];
368 struct pktgen_hdr {
369 __be32 pgh_magic;
370 __be32 seq_num;
371 __be32 tv_sec;
372 __be32 tv_usec;
375 struct pktgen_thread {
376 spinlock_t if_lock;
377 struct list_head if_list; /* All device here */
378 struct list_head th_list;
379 struct task_struct *tsk;
380 char result[512];
381 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
383 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
385 u32 control;
386 int cpu;
388 wait_queue_head_t queue;
391 #define REMOVE 1
392 #define FIND 0
394 /* This code works around the fact that do_div cannot handle two 64-bit
395 numbers, and regular 64-bit division doesn't work on x86 kernels.
396 --Ben
399 #define PG_DIV 0
401 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
402 * Function copied/adapted/optimized from:
404 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
406 * Copyright 1994, University of Cambridge Computer Laboratory
407 * All Rights Reserved.
410 static inline s64 divremdi3(s64 x, s64 y, int type)
412 u64 a = (x < 0) ? -x : x;
413 u64 b = (y < 0) ? -y : y;
414 u64 res = 0, d = 1;
416 if (b > 0) {
417 while (b < a) {
418 b <<= 1;
419 d <<= 1;
423 do {
424 if (a >= b) {
425 a -= b;
426 res += d;
428 b >>= 1;
429 d >>= 1;
431 while (d);
433 if (PG_DIV == type) {
434 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
435 } else {
436 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
440 /* End of hacks to deal with 64-bit math on x86 */
442 /** Convert to milliseconds */
443 static inline __u64 tv_to_ms(const struct timeval *tv)
445 __u64 ms = tv->tv_usec / 1000;
446 ms += (__u64) tv->tv_sec * (__u64) 1000;
447 return ms;
450 /** Convert to micro-seconds */
451 static inline __u64 tv_to_us(const struct timeval *tv)
453 __u64 us = tv->tv_usec;
454 us += (__u64) tv->tv_sec * (__u64) 1000000;
455 return us;
458 static inline __u64 pg_div(__u64 n, __u32 base)
460 __u64 tmp = n;
461 do_div(tmp, base);
462 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
463 n, base, tmp); */
464 return tmp;
467 static inline __u64 pg_div64(__u64 n, __u64 base)
469 __u64 tmp = n;
471 * How do we know if the architecture we are running on
472 * supports division with 64 bit base?
475 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
477 do_div(tmp, base);
478 #else
479 tmp = divremdi3(n, base, PG_DIV);
480 #endif
481 return tmp;
484 static inline __u64 getCurMs(void)
486 struct timeval tv;
487 do_gettimeofday(&tv);
488 return tv_to_ms(&tv);
491 static inline __u64 getCurUs(void)
493 struct timeval tv;
494 do_gettimeofday(&tv);
495 return tv_to_us(&tv);
498 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
500 return tv_to_us(a) - tv_to_us(b);
503 /* old include end */
505 static char version[] __initdata = VERSION;
507 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
508 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
509 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
510 const char *ifname);
511 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
512 static void pktgen_run_all_threads(void);
513 static void pktgen_stop_all_threads_ifs(void);
514 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
515 static void pktgen_stop(struct pktgen_thread *t);
516 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
518 static unsigned int scan_ip6(const char *s, char ip[16]);
519 static unsigned int fmt_ip6(char *s, const char ip[16]);
521 /* Module parameters, defaults. */
522 static int pg_count_d = 1000; /* 1000 pkts by default */
523 static int pg_delay_d;
524 static int pg_clone_skb_d;
525 static int debug;
527 static DEFINE_MUTEX(pktgen_thread_lock);
528 static LIST_HEAD(pktgen_threads);
530 static struct notifier_block pktgen_notifier_block = {
531 .notifier_call = pktgen_device_event,
535 * /proc handling functions
539 static int pgctrl_show(struct seq_file *seq, void *v)
541 seq_puts(seq, VERSION);
542 return 0;
545 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
546 size_t count, loff_t * ppos)
548 int err = 0;
549 char data[128];
551 if (!capable(CAP_NET_ADMIN)) {
552 err = -EPERM;
553 goto out;
556 if (count > sizeof(data))
557 count = sizeof(data);
559 if (copy_from_user(data, buf, count)) {
560 err = -EFAULT;
561 goto out;
563 data[count - 1] = 0; /* Make string */
565 if (!strcmp(data, "stop"))
566 pktgen_stop_all_threads_ifs();
568 else if (!strcmp(data, "start"))
569 pktgen_run_all_threads();
571 else
572 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
574 err = count;
576 out:
577 return err;
580 static int pgctrl_open(struct inode *inode, struct file *file)
582 return single_open(file, pgctrl_show, PDE(inode)->data);
585 static const struct file_operations pktgen_fops = {
586 .owner = THIS_MODULE,
587 .open = pgctrl_open,
588 .read = seq_read,
589 .llseek = seq_lseek,
590 .write = pgctrl_write,
591 .release = single_release,
594 static int pktgen_if_show(struct seq_file *seq, void *v)
596 int i;
597 struct pktgen_dev *pkt_dev = seq->private;
598 __u64 sa;
599 __u64 stopped;
600 __u64 now = getCurUs();
602 seq_printf(seq,
603 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
604 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
605 pkt_dev->max_pkt_size);
607 seq_printf(seq,
608 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
609 pkt_dev->nfrags,
610 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
611 pkt_dev->clone_skb, pkt_dev->odev->name);
613 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
614 pkt_dev->lflow);
616 if (pkt_dev->flags & F_IPV6) {
617 char b1[128], b2[128], b3[128];
618 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
619 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
620 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
621 seq_printf(seq,
622 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
623 b2, b3);
625 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
626 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
627 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
628 seq_printf(seq,
629 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
630 b2, b3);
632 } else
633 seq_printf(seq,
634 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
635 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
636 pkt_dev->src_max);
638 seq_puts(seq, " src_mac: ");
640 if (is_zero_ether_addr(pkt_dev->src_mac))
641 for (i = 0; i < 6; i++)
642 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
643 i == 5 ? " " : ":");
644 else
645 for (i = 0; i < 6; i++)
646 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
647 i == 5 ? " " : ":");
649 seq_printf(seq, "dst_mac: ");
650 for (i = 0; i < 6; i++)
651 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
652 i == 5 ? "\n" : ":");
654 seq_printf(seq,
655 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
656 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
657 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
659 seq_printf(seq,
660 " src_mac_count: %d dst_mac_count: %d\n",
661 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
663 if (pkt_dev->nr_labels) {
664 unsigned i;
665 seq_printf(seq, " mpls: ");
666 for (i = 0; i < pkt_dev->nr_labels; i++)
667 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
668 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
671 if (pkt_dev->vlan_id != 0xffff) {
672 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
673 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
676 if (pkt_dev->svlan_id != 0xffff) {
677 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
678 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
681 if (pkt_dev->tos) {
682 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
685 if (pkt_dev->traffic_class) {
686 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
689 seq_printf(seq, " Flags: ");
691 if (pkt_dev->flags & F_IPV6)
692 seq_printf(seq, "IPV6 ");
694 if (pkt_dev->flags & F_IPSRC_RND)
695 seq_printf(seq, "IPSRC_RND ");
697 if (pkt_dev->flags & F_IPDST_RND)
698 seq_printf(seq, "IPDST_RND ");
700 if (pkt_dev->flags & F_TXSIZE_RND)
701 seq_printf(seq, "TXSIZE_RND ");
703 if (pkt_dev->flags & F_UDPSRC_RND)
704 seq_printf(seq, "UDPSRC_RND ");
706 if (pkt_dev->flags & F_UDPDST_RND)
707 seq_printf(seq, "UDPDST_RND ");
709 if (pkt_dev->flags & F_MPLS_RND)
710 seq_printf(seq, "MPLS_RND ");
712 if (pkt_dev->cflows) {
713 if (pkt_dev->flags & F_FLOW_SEQ)
714 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
715 else
716 seq_printf(seq, "FLOW_RND ");
719 #ifdef CONFIG_XFRM
720 if (pkt_dev->flags & F_IPSEC_ON)
721 seq_printf(seq, "IPSEC ");
722 #endif
724 if (pkt_dev->flags & F_MACSRC_RND)
725 seq_printf(seq, "MACSRC_RND ");
727 if (pkt_dev->flags & F_MACDST_RND)
728 seq_printf(seq, "MACDST_RND ");
730 if (pkt_dev->flags & F_VID_RND)
731 seq_printf(seq, "VID_RND ");
733 if (pkt_dev->flags & F_SVID_RND)
734 seq_printf(seq, "SVID_RND ");
736 seq_puts(seq, "\n");
738 sa = pkt_dev->started_at;
739 stopped = pkt_dev->stopped_at;
740 if (pkt_dev->running)
741 stopped = now; /* not really stopped, more like last-running-at */
743 seq_printf(seq,
744 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
745 (unsigned long long)pkt_dev->sofar,
746 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
747 (unsigned long long)stopped,
748 (unsigned long long)pkt_dev->idle_acc);
750 seq_printf(seq,
751 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
752 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
753 pkt_dev->cur_src_mac_offset);
755 if (pkt_dev->flags & F_IPV6) {
756 char b1[128], b2[128];
757 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
758 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
759 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
760 } else
761 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
762 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
764 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
765 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
767 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
769 if (pkt_dev->result[0])
770 seq_printf(seq, "Result: %s\n", pkt_dev->result);
771 else
772 seq_printf(seq, "Result: Idle\n");
774 return 0;
778 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
780 int i = 0;
781 *num = 0;
783 for (; i < maxlen; i++) {
784 char c;
785 *num <<= 4;
786 if (get_user(c, &user_buffer[i]))
787 return -EFAULT;
788 if ((c >= '0') && (c <= '9'))
789 *num |= c - '0';
790 else if ((c >= 'a') && (c <= 'f'))
791 *num |= c - 'a' + 10;
792 else if ((c >= 'A') && (c <= 'F'))
793 *num |= c - 'A' + 10;
794 else
795 break;
797 return i;
800 static int count_trail_chars(const char __user * user_buffer,
801 unsigned int maxlen)
803 int i;
805 for (i = 0; i < maxlen; i++) {
806 char c;
807 if (get_user(c, &user_buffer[i]))
808 return -EFAULT;
809 switch (c) {
810 case '\"':
811 case '\n':
812 case '\r':
813 case '\t':
814 case ' ':
815 case '=':
816 break;
817 default:
818 goto done;
821 done:
822 return i;
825 static unsigned long num_arg(const char __user * user_buffer,
826 unsigned long maxlen, unsigned long *num)
828 int i = 0;
829 *num = 0;
831 for (; i < maxlen; i++) {
832 char c;
833 if (get_user(c, &user_buffer[i]))
834 return -EFAULT;
835 if ((c >= '0') && (c <= '9')) {
836 *num *= 10;
837 *num += c - '0';
838 } else
839 break;
841 return i;
844 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
846 int i = 0;
848 for (; i < maxlen; i++) {
849 char c;
850 if (get_user(c, &user_buffer[i]))
851 return -EFAULT;
852 switch (c) {
853 case '\"':
854 case '\n':
855 case '\r':
856 case '\t':
857 case ' ':
858 goto done_str;
859 break;
860 default:
861 break;
864 done_str:
865 return i;
868 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
870 unsigned n = 0;
871 char c;
872 ssize_t i = 0;
873 int len;
875 pkt_dev->nr_labels = 0;
876 do {
877 __u32 tmp;
878 len = hex32_arg(&buffer[i], 8, &tmp);
879 if (len <= 0)
880 return len;
881 pkt_dev->labels[n] = htonl(tmp);
882 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
883 pkt_dev->flags |= F_MPLS_RND;
884 i += len;
885 if (get_user(c, &buffer[i]))
886 return -EFAULT;
887 i++;
888 n++;
889 if (n >= MAX_MPLS_LABELS)
890 return -E2BIG;
891 } while (c == ',');
893 pkt_dev->nr_labels = n;
894 return i;
897 static ssize_t pktgen_if_write(struct file *file,
898 const char __user * user_buffer, size_t count,
899 loff_t * offset)
901 struct seq_file *seq = (struct seq_file *)file->private_data;
902 struct pktgen_dev *pkt_dev = seq->private;
903 int i = 0, max, len;
904 char name[16], valstr[32];
905 unsigned long value = 0;
906 char *pg_result = NULL;
907 int tmp = 0;
908 char buf[128];
910 pg_result = &(pkt_dev->result[0]);
912 if (count < 1) {
913 printk(KERN_WARNING "pktgen: wrong command format\n");
914 return -EINVAL;
917 max = count - i;
918 tmp = count_trail_chars(&user_buffer[i], max);
919 if (tmp < 0) {
920 printk(KERN_WARNING "pktgen: illegal format\n");
921 return tmp;
923 i += tmp;
925 /* Read variable name */
927 len = strn_len(&user_buffer[i], sizeof(name) - 1);
928 if (len < 0) {
929 return len;
931 memset(name, 0, sizeof(name));
932 if (copy_from_user(name, &user_buffer[i], len))
933 return -EFAULT;
934 i += len;
936 max = count - i;
937 len = count_trail_chars(&user_buffer[i], max);
938 if (len < 0)
939 return len;
941 i += len;
943 if (debug) {
944 char tb[count + 1];
945 if (copy_from_user(tb, user_buffer, count))
946 return -EFAULT;
947 tb[count] = 0;
948 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
949 (unsigned long)count, tb);
952 if (!strcmp(name, "min_pkt_size")) {
953 len = num_arg(&user_buffer[i], 10, &value);
954 if (len < 0) {
955 return len;
957 i += len;
958 if (value < 14 + 20 + 8)
959 value = 14 + 20 + 8;
960 if (value != pkt_dev->min_pkt_size) {
961 pkt_dev->min_pkt_size = value;
962 pkt_dev->cur_pkt_size = value;
964 sprintf(pg_result, "OK: min_pkt_size=%u",
965 pkt_dev->min_pkt_size);
966 return count;
969 if (!strcmp(name, "max_pkt_size")) {
970 len = num_arg(&user_buffer[i], 10, &value);
971 if (len < 0) {
972 return len;
974 i += len;
975 if (value < 14 + 20 + 8)
976 value = 14 + 20 + 8;
977 if (value != pkt_dev->max_pkt_size) {
978 pkt_dev->max_pkt_size = value;
979 pkt_dev->cur_pkt_size = value;
981 sprintf(pg_result, "OK: max_pkt_size=%u",
982 pkt_dev->max_pkt_size);
983 return count;
986 /* Shortcut for min = max */
988 if (!strcmp(name, "pkt_size")) {
989 len = num_arg(&user_buffer[i], 10, &value);
990 if (len < 0) {
991 return len;
993 i += len;
994 if (value < 14 + 20 + 8)
995 value = 14 + 20 + 8;
996 if (value != pkt_dev->min_pkt_size) {
997 pkt_dev->min_pkt_size = value;
998 pkt_dev->max_pkt_size = value;
999 pkt_dev->cur_pkt_size = value;
1001 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
1002 return count;
1005 if (!strcmp(name, "debug")) {
1006 len = num_arg(&user_buffer[i], 10, &value);
1007 if (len < 0) {
1008 return len;
1010 i += len;
1011 debug = value;
1012 sprintf(pg_result, "OK: debug=%u", debug);
1013 return count;
1016 if (!strcmp(name, "frags")) {
1017 len = num_arg(&user_buffer[i], 10, &value);
1018 if (len < 0) {
1019 return len;
1021 i += len;
1022 pkt_dev->nfrags = value;
1023 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1024 return count;
1026 if (!strcmp(name, "delay")) {
1027 len = num_arg(&user_buffer[i], 10, &value);
1028 if (len < 0) {
1029 return len;
1031 i += len;
1032 if (value == 0x7FFFFFFF) {
1033 pkt_dev->delay_us = 0x7FFFFFFF;
1034 pkt_dev->delay_ns = 0;
1035 } else {
1036 pkt_dev->delay_us = value / 1000;
1037 pkt_dev->delay_ns = value % 1000;
1039 sprintf(pg_result, "OK: delay=%u",
1040 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1041 return count;
1043 if (!strcmp(name, "udp_src_min")) {
1044 len = num_arg(&user_buffer[i], 10, &value);
1045 if (len < 0) {
1046 return len;
1048 i += len;
1049 if (value != pkt_dev->udp_src_min) {
1050 pkt_dev->udp_src_min = value;
1051 pkt_dev->cur_udp_src = value;
1053 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1054 return count;
1056 if (!strcmp(name, "udp_dst_min")) {
1057 len = num_arg(&user_buffer[i], 10, &value);
1058 if (len < 0) {
1059 return len;
1061 i += len;
1062 if (value != pkt_dev->udp_dst_min) {
1063 pkt_dev->udp_dst_min = value;
1064 pkt_dev->cur_udp_dst = value;
1066 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1067 return count;
1069 if (!strcmp(name, "udp_src_max")) {
1070 len = num_arg(&user_buffer[i], 10, &value);
1071 if (len < 0) {
1072 return len;
1074 i += len;
1075 if (value != pkt_dev->udp_src_max) {
1076 pkt_dev->udp_src_max = value;
1077 pkt_dev->cur_udp_src = value;
1079 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1080 return count;
1082 if (!strcmp(name, "udp_dst_max")) {
1083 len = num_arg(&user_buffer[i], 10, &value);
1084 if (len < 0) {
1085 return len;
1087 i += len;
1088 if (value != pkt_dev->udp_dst_max) {
1089 pkt_dev->udp_dst_max = value;
1090 pkt_dev->cur_udp_dst = value;
1092 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1093 return count;
1095 if (!strcmp(name, "clone_skb")) {
1096 len = num_arg(&user_buffer[i], 10, &value);
1097 if (len < 0) {
1098 return len;
1100 i += len;
1101 pkt_dev->clone_skb = value;
1103 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1104 return count;
1106 if (!strcmp(name, "count")) {
1107 len = num_arg(&user_buffer[i], 10, &value);
1108 if (len < 0) {
1109 return len;
1111 i += len;
1112 pkt_dev->count = value;
1113 sprintf(pg_result, "OK: count=%llu",
1114 (unsigned long long)pkt_dev->count);
1115 return count;
1117 if (!strcmp(name, "src_mac_count")) {
1118 len = num_arg(&user_buffer[i], 10, &value);
1119 if (len < 0) {
1120 return len;
1122 i += len;
1123 if (pkt_dev->src_mac_count != value) {
1124 pkt_dev->src_mac_count = value;
1125 pkt_dev->cur_src_mac_offset = 0;
1127 sprintf(pg_result, "OK: src_mac_count=%d",
1128 pkt_dev->src_mac_count);
1129 return count;
1131 if (!strcmp(name, "dst_mac_count")) {
1132 len = num_arg(&user_buffer[i], 10, &value);
1133 if (len < 0) {
1134 return len;
1136 i += len;
1137 if (pkt_dev->dst_mac_count != value) {
1138 pkt_dev->dst_mac_count = value;
1139 pkt_dev->cur_dst_mac_offset = 0;
1141 sprintf(pg_result, "OK: dst_mac_count=%d",
1142 pkt_dev->dst_mac_count);
1143 return count;
1145 if (!strcmp(name, "flag")) {
1146 char f[32];
1147 memset(f, 0, 32);
1148 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1149 if (len < 0) {
1150 return len;
1152 if (copy_from_user(f, &user_buffer[i], len))
1153 return -EFAULT;
1154 i += len;
1155 if (strcmp(f, "IPSRC_RND") == 0)
1156 pkt_dev->flags |= F_IPSRC_RND;
1158 else if (strcmp(f, "!IPSRC_RND") == 0)
1159 pkt_dev->flags &= ~F_IPSRC_RND;
1161 else if (strcmp(f, "TXSIZE_RND") == 0)
1162 pkt_dev->flags |= F_TXSIZE_RND;
1164 else if (strcmp(f, "!TXSIZE_RND") == 0)
1165 pkt_dev->flags &= ~F_TXSIZE_RND;
1167 else if (strcmp(f, "IPDST_RND") == 0)
1168 pkt_dev->flags |= F_IPDST_RND;
1170 else if (strcmp(f, "!IPDST_RND") == 0)
1171 pkt_dev->flags &= ~F_IPDST_RND;
1173 else if (strcmp(f, "UDPSRC_RND") == 0)
1174 pkt_dev->flags |= F_UDPSRC_RND;
1176 else if (strcmp(f, "!UDPSRC_RND") == 0)
1177 pkt_dev->flags &= ~F_UDPSRC_RND;
1179 else if (strcmp(f, "UDPDST_RND") == 0)
1180 pkt_dev->flags |= F_UDPDST_RND;
1182 else if (strcmp(f, "!UDPDST_RND") == 0)
1183 pkt_dev->flags &= ~F_UDPDST_RND;
1185 else if (strcmp(f, "MACSRC_RND") == 0)
1186 pkt_dev->flags |= F_MACSRC_RND;
1188 else if (strcmp(f, "!MACSRC_RND") == 0)
1189 pkt_dev->flags &= ~F_MACSRC_RND;
1191 else if (strcmp(f, "MACDST_RND") == 0)
1192 pkt_dev->flags |= F_MACDST_RND;
1194 else if (strcmp(f, "!MACDST_RND") == 0)
1195 pkt_dev->flags &= ~F_MACDST_RND;
1197 else if (strcmp(f, "MPLS_RND") == 0)
1198 pkt_dev->flags |= F_MPLS_RND;
1200 else if (strcmp(f, "!MPLS_RND") == 0)
1201 pkt_dev->flags &= ~F_MPLS_RND;
1203 else if (strcmp(f, "VID_RND") == 0)
1204 pkt_dev->flags |= F_VID_RND;
1206 else if (strcmp(f, "!VID_RND") == 0)
1207 pkt_dev->flags &= ~F_VID_RND;
1209 else if (strcmp(f, "SVID_RND") == 0)
1210 pkt_dev->flags |= F_SVID_RND;
1212 else if (strcmp(f, "!SVID_RND") == 0)
1213 pkt_dev->flags &= ~F_SVID_RND;
1215 else if (strcmp(f, "FLOW_SEQ") == 0)
1216 pkt_dev->flags |= F_FLOW_SEQ;
1218 #ifdef CONFIG_XFRM
1219 else if (strcmp(f, "IPSEC") == 0)
1220 pkt_dev->flags |= F_IPSEC_ON;
1221 #endif
1223 else if (strcmp(f, "!IPV6") == 0)
1224 pkt_dev->flags &= ~F_IPV6;
1226 else {
1227 sprintf(pg_result,
1228 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1230 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1231 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1232 return count;
1234 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1235 return count;
1237 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1238 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1239 if (len < 0) {
1240 return len;
1243 if (copy_from_user(buf, &user_buffer[i], len))
1244 return -EFAULT;
1245 buf[len] = 0;
1246 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1247 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1248 strncpy(pkt_dev->dst_min, buf, len);
1249 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1250 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1252 if (debug)
1253 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1254 pkt_dev->dst_min);
1255 i += len;
1256 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1257 return count;
1259 if (!strcmp(name, "dst_max")) {
1260 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1261 if (len < 0) {
1262 return len;
1265 if (copy_from_user(buf, &user_buffer[i], len))
1266 return -EFAULT;
1268 buf[len] = 0;
1269 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1270 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1271 strncpy(pkt_dev->dst_max, buf, len);
1272 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1273 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1275 if (debug)
1276 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1277 pkt_dev->dst_max);
1278 i += len;
1279 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1280 return count;
1282 if (!strcmp(name, "dst6")) {
1283 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1284 if (len < 0)
1285 return len;
1287 pkt_dev->flags |= F_IPV6;
1289 if (copy_from_user(buf, &user_buffer[i], len))
1290 return -EFAULT;
1291 buf[len] = 0;
1293 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1294 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1296 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1298 if (debug)
1299 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1301 i += len;
1302 sprintf(pg_result, "OK: dst6=%s", buf);
1303 return count;
1305 if (!strcmp(name, "dst6_min")) {
1306 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1307 if (len < 0)
1308 return len;
1310 pkt_dev->flags |= F_IPV6;
1312 if (copy_from_user(buf, &user_buffer[i], len))
1313 return -EFAULT;
1314 buf[len] = 0;
1316 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1317 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1319 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1320 &pkt_dev->min_in6_daddr);
1321 if (debug)
1322 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1324 i += len;
1325 sprintf(pg_result, "OK: dst6_min=%s", buf);
1326 return count;
1328 if (!strcmp(name, "dst6_max")) {
1329 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1330 if (len < 0)
1331 return len;
1333 pkt_dev->flags |= F_IPV6;
1335 if (copy_from_user(buf, &user_buffer[i], len))
1336 return -EFAULT;
1337 buf[len] = 0;
1339 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1340 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1342 if (debug)
1343 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1345 i += len;
1346 sprintf(pg_result, "OK: dst6_max=%s", buf);
1347 return count;
1349 if (!strcmp(name, "src6")) {
1350 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1351 if (len < 0)
1352 return len;
1354 pkt_dev->flags |= F_IPV6;
1356 if (copy_from_user(buf, &user_buffer[i], len))
1357 return -EFAULT;
1358 buf[len] = 0;
1360 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1361 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1363 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1365 if (debug)
1366 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1368 i += len;
1369 sprintf(pg_result, "OK: src6=%s", buf);
1370 return count;
1372 if (!strcmp(name, "src_min")) {
1373 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1374 if (len < 0) {
1375 return len;
1377 if (copy_from_user(buf, &user_buffer[i], len))
1378 return -EFAULT;
1379 buf[len] = 0;
1380 if (strcmp(buf, pkt_dev->src_min) != 0) {
1381 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1382 strncpy(pkt_dev->src_min, buf, len);
1383 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1384 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1386 if (debug)
1387 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1388 pkt_dev->src_min);
1389 i += len;
1390 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1391 return count;
1393 if (!strcmp(name, "src_max")) {
1394 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1395 if (len < 0) {
1396 return len;
1398 if (copy_from_user(buf, &user_buffer[i], len))
1399 return -EFAULT;
1400 buf[len] = 0;
1401 if (strcmp(buf, pkt_dev->src_max) != 0) {
1402 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1403 strncpy(pkt_dev->src_max, buf, len);
1404 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1405 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1407 if (debug)
1408 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1409 pkt_dev->src_max);
1410 i += len;
1411 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1412 return count;
1414 if (!strcmp(name, "dst_mac")) {
1415 char *v = valstr;
1416 unsigned char old_dmac[ETH_ALEN];
1417 unsigned char *m = pkt_dev->dst_mac;
1418 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1420 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1421 if (len < 0) {
1422 return len;
1424 memset(valstr, 0, sizeof(valstr));
1425 if (copy_from_user(valstr, &user_buffer[i], len))
1426 return -EFAULT;
1427 i += len;
1429 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1430 if (*v >= '0' && *v <= '9') {
1431 *m *= 16;
1432 *m += *v - '0';
1434 if (*v >= 'A' && *v <= 'F') {
1435 *m *= 16;
1436 *m += *v - 'A' + 10;
1438 if (*v >= 'a' && *v <= 'f') {
1439 *m *= 16;
1440 *m += *v - 'a' + 10;
1442 if (*v == ':') {
1443 m++;
1444 *m = 0;
1448 /* Set up Dest MAC */
1449 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1450 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1452 sprintf(pg_result, "OK: dstmac");
1453 return count;
1455 if (!strcmp(name, "src_mac")) {
1456 char *v = valstr;
1457 unsigned char old_smac[ETH_ALEN];
1458 unsigned char *m = pkt_dev->src_mac;
1460 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1462 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1463 if (len < 0) {
1464 return len;
1466 memset(valstr, 0, sizeof(valstr));
1467 if (copy_from_user(valstr, &user_buffer[i], len))
1468 return -EFAULT;
1469 i += len;
1471 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1472 if (*v >= '0' && *v <= '9') {
1473 *m *= 16;
1474 *m += *v - '0';
1476 if (*v >= 'A' && *v <= 'F') {
1477 *m *= 16;
1478 *m += *v - 'A' + 10;
1480 if (*v >= 'a' && *v <= 'f') {
1481 *m *= 16;
1482 *m += *v - 'a' + 10;
1484 if (*v == ':') {
1485 m++;
1486 *m = 0;
1490 /* Set up Src MAC */
1491 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1492 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1494 sprintf(pg_result, "OK: srcmac");
1495 return count;
1498 if (!strcmp(name, "clear_counters")) {
1499 pktgen_clear_counters(pkt_dev);
1500 sprintf(pg_result, "OK: Clearing counters.\n");
1501 return count;
1504 if (!strcmp(name, "flows")) {
1505 len = num_arg(&user_buffer[i], 10, &value);
1506 if (len < 0) {
1507 return len;
1509 i += len;
1510 if (value > MAX_CFLOWS)
1511 value = MAX_CFLOWS;
1513 pkt_dev->cflows = value;
1514 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1515 return count;
1518 if (!strcmp(name, "flowlen")) {
1519 len = num_arg(&user_buffer[i], 10, &value);
1520 if (len < 0) {
1521 return len;
1523 i += len;
1524 pkt_dev->lflow = value;
1525 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1526 return count;
1529 if (!strcmp(name, "mpls")) {
1530 unsigned n, offset;
1531 len = get_labels(&user_buffer[i], pkt_dev);
1532 if (len < 0) { return len; }
1533 i += len;
1534 offset = sprintf(pg_result, "OK: mpls=");
1535 for (n = 0; n < pkt_dev->nr_labels; n++)
1536 offset += sprintf(pg_result + offset,
1537 "%08x%s", ntohl(pkt_dev->labels[n]),
1538 n == pkt_dev->nr_labels-1 ? "" : ",");
1540 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1541 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1542 pkt_dev->svlan_id = 0xffff;
1544 if (debug)
1545 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1547 return count;
1550 if (!strcmp(name, "vlan_id")) {
1551 len = num_arg(&user_buffer[i], 4, &value);
1552 if (len < 0) {
1553 return len;
1555 i += len;
1556 if (value <= 4095) {
1557 pkt_dev->vlan_id = value; /* turn on VLAN */
1559 if (debug)
1560 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1562 if (debug && pkt_dev->nr_labels)
1563 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1565 pkt_dev->nr_labels = 0; /* turn off MPLS */
1566 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1567 } else {
1568 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1569 pkt_dev->svlan_id = 0xffff;
1571 if (debug)
1572 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1574 return count;
1577 if (!strcmp(name, "vlan_p")) {
1578 len = num_arg(&user_buffer[i], 1, &value);
1579 if (len < 0) {
1580 return len;
1582 i += len;
1583 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1584 pkt_dev->vlan_p = value;
1585 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1586 } else {
1587 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1589 return count;
1592 if (!strcmp(name, "vlan_cfi")) {
1593 len = num_arg(&user_buffer[i], 1, &value);
1594 if (len < 0) {
1595 return len;
1597 i += len;
1598 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1599 pkt_dev->vlan_cfi = value;
1600 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1601 } else {
1602 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1604 return count;
1607 if (!strcmp(name, "svlan_id")) {
1608 len = num_arg(&user_buffer[i], 4, &value);
1609 if (len < 0) {
1610 return len;
1612 i += len;
1613 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1614 pkt_dev->svlan_id = value; /* turn on SVLAN */
1616 if (debug)
1617 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1619 if (debug && pkt_dev->nr_labels)
1620 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1622 pkt_dev->nr_labels = 0; /* turn off MPLS */
1623 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1624 } else {
1625 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1626 pkt_dev->svlan_id = 0xffff;
1628 if (debug)
1629 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1631 return count;
1634 if (!strcmp(name, "svlan_p")) {
1635 len = num_arg(&user_buffer[i], 1, &value);
1636 if (len < 0) {
1637 return len;
1639 i += len;
1640 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1641 pkt_dev->svlan_p = value;
1642 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1643 } else {
1644 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1646 return count;
1649 if (!strcmp(name, "svlan_cfi")) {
1650 len = num_arg(&user_buffer[i], 1, &value);
1651 if (len < 0) {
1652 return len;
1654 i += len;
1655 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1656 pkt_dev->svlan_cfi = value;
1657 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1658 } else {
1659 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1661 return count;
1664 if (!strcmp(name, "tos")) {
1665 __u32 tmp_value = 0;
1666 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1667 if (len < 0) {
1668 return len;
1670 i += len;
1671 if (len == 2) {
1672 pkt_dev->tos = tmp_value;
1673 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1674 } else {
1675 sprintf(pg_result, "ERROR: tos must be 00-ff");
1677 return count;
1680 if (!strcmp(name, "traffic_class")) {
1681 __u32 tmp_value = 0;
1682 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1683 if (len < 0) {
1684 return len;
1686 i += len;
1687 if (len == 2) {
1688 pkt_dev->traffic_class = tmp_value;
1689 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1690 } else {
1691 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1693 return count;
1696 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1697 return -EINVAL;
1700 static int pktgen_if_open(struct inode *inode, struct file *file)
1702 return single_open(file, pktgen_if_show, PDE(inode)->data);
1705 static const struct file_operations pktgen_if_fops = {
1706 .owner = THIS_MODULE,
1707 .open = pktgen_if_open,
1708 .read = seq_read,
1709 .llseek = seq_lseek,
1710 .write = pktgen_if_write,
1711 .release = single_release,
1714 static int pktgen_thread_show(struct seq_file *seq, void *v)
1716 struct pktgen_thread *t = seq->private;
1717 struct pktgen_dev *pkt_dev;
1719 BUG_ON(!t);
1721 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1722 t->tsk->comm, t->max_before_softirq);
1724 seq_printf(seq, "Running: ");
1726 if_lock(t);
1727 list_for_each_entry(pkt_dev, &t->if_list, list)
1728 if (pkt_dev->running)
1729 seq_printf(seq, "%s ", pkt_dev->odev->name);
1731 seq_printf(seq, "\nStopped: ");
1733 list_for_each_entry(pkt_dev, &t->if_list, list)
1734 if (!pkt_dev->running)
1735 seq_printf(seq, "%s ", pkt_dev->odev->name);
1737 if (t->result[0])
1738 seq_printf(seq, "\nResult: %s\n", t->result);
1739 else
1740 seq_printf(seq, "\nResult: NA\n");
1742 if_unlock(t);
1744 return 0;
1747 static ssize_t pktgen_thread_write(struct file *file,
1748 const char __user * user_buffer,
1749 size_t count, loff_t * offset)
1751 struct seq_file *seq = (struct seq_file *)file->private_data;
1752 struct pktgen_thread *t = seq->private;
1753 int i = 0, max, len, ret;
1754 char name[40];
1755 char *pg_result;
1756 unsigned long value = 0;
1758 if (count < 1) {
1759 // sprintf(pg_result, "Wrong command format");
1760 return -EINVAL;
1763 max = count - i;
1764 len = count_trail_chars(&user_buffer[i], max);
1765 if (len < 0)
1766 return len;
1768 i += len;
1770 /* Read variable name */
1772 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1773 if (len < 0)
1774 return len;
1776 memset(name, 0, sizeof(name));
1777 if (copy_from_user(name, &user_buffer[i], len))
1778 return -EFAULT;
1779 i += len;
1781 max = count - i;
1782 len = count_trail_chars(&user_buffer[i], max);
1783 if (len < 0)
1784 return len;
1786 i += len;
1788 if (debug)
1789 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1790 name, (unsigned long)count);
1792 if (!t) {
1793 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1794 ret = -EINVAL;
1795 goto out;
1798 pg_result = &(t->result[0]);
1800 if (!strcmp(name, "add_device")) {
1801 char f[32];
1802 memset(f, 0, 32);
1803 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1804 if (len < 0) {
1805 ret = len;
1806 goto out;
1808 if (copy_from_user(f, &user_buffer[i], len))
1809 return -EFAULT;
1810 i += len;
1811 mutex_lock(&pktgen_thread_lock);
1812 pktgen_add_device(t, f);
1813 mutex_unlock(&pktgen_thread_lock);
1814 ret = count;
1815 sprintf(pg_result, "OK: add_device=%s", f);
1816 goto out;
1819 if (!strcmp(name, "rem_device_all")) {
1820 mutex_lock(&pktgen_thread_lock);
1821 t->control |= T_REMDEVALL;
1822 mutex_unlock(&pktgen_thread_lock);
1823 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1824 ret = count;
1825 sprintf(pg_result, "OK: rem_device_all");
1826 goto out;
1829 if (!strcmp(name, "max_before_softirq")) {
1830 len = num_arg(&user_buffer[i], 10, &value);
1831 mutex_lock(&pktgen_thread_lock);
1832 t->max_before_softirq = value;
1833 mutex_unlock(&pktgen_thread_lock);
1834 ret = count;
1835 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1836 goto out;
1839 ret = -EINVAL;
1840 out:
1841 return ret;
1844 static int pktgen_thread_open(struct inode *inode, struct file *file)
1846 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1849 static const struct file_operations pktgen_thread_fops = {
1850 .owner = THIS_MODULE,
1851 .open = pktgen_thread_open,
1852 .read = seq_read,
1853 .llseek = seq_lseek,
1854 .write = pktgen_thread_write,
1855 .release = single_release,
1858 /* Think find or remove for NN */
1859 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1861 struct pktgen_thread *t;
1862 struct pktgen_dev *pkt_dev = NULL;
1864 list_for_each_entry(t, &pktgen_threads, th_list) {
1865 pkt_dev = pktgen_find_dev(t, ifname);
1866 if (pkt_dev) {
1867 if (remove) {
1868 if_lock(t);
1869 pkt_dev->removal_mark = 1;
1870 t->control |= T_REMDEV;
1871 if_unlock(t);
1873 break;
1876 return pkt_dev;
1880 * mark a device for removal
1882 static void pktgen_mark_device(const char *ifname)
1884 struct pktgen_dev *pkt_dev = NULL;
1885 const int max_tries = 10, msec_per_try = 125;
1886 int i = 0;
1888 mutex_lock(&pktgen_thread_lock);
1889 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1891 while (1) {
1893 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1894 if (pkt_dev == NULL)
1895 break; /* success */
1897 mutex_unlock(&pktgen_thread_lock);
1898 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1899 "to disappear....\n", ifname);
1900 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1901 mutex_lock(&pktgen_thread_lock);
1903 if (++i >= max_tries) {
1904 printk(KERN_ERR "pktgen_mark_device: timed out after "
1905 "waiting %d msec for device %s to be removed\n",
1906 msec_per_try * i, ifname);
1907 break;
1912 mutex_unlock(&pktgen_thread_lock);
1915 static void pktgen_change_name(struct net_device *dev)
1917 struct pktgen_thread *t;
1919 list_for_each_entry(t, &pktgen_threads, th_list) {
1920 struct pktgen_dev *pkt_dev;
1922 list_for_each_entry(pkt_dev, &t->if_list, list) {
1923 if (pkt_dev->odev != dev)
1924 continue;
1926 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1928 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1929 pg_proc_dir);
1930 if (!pkt_dev->entry)
1931 printk(KERN_ERR "pktgen: can't move proc "
1932 " entry for '%s'\n", dev->name);
1933 break;
1938 static int pktgen_device_event(struct notifier_block *unused,
1939 unsigned long event, void *ptr)
1941 struct net_device *dev = ptr;
1943 /* It is OK that we do not hold the group lock right now,
1944 * as we run under the RTNL lock.
1947 switch (event) {
1948 case NETDEV_CHANGENAME:
1949 pktgen_change_name(dev);
1950 break;
1952 case NETDEV_UNREGISTER:
1953 pktgen_mark_device(dev->name);
1954 break;
1957 return NOTIFY_DONE;
1960 /* Associate pktgen_dev with a device. */
1962 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1964 struct net_device *odev;
1965 int err;
1967 /* Clean old setups */
1968 if (pkt_dev->odev) {
1969 dev_put(pkt_dev->odev);
1970 pkt_dev->odev = NULL;
1973 odev = dev_get_by_name(ifname);
1974 if (!odev) {
1975 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1976 return -ENODEV;
1979 if (odev->type != ARPHRD_ETHER) {
1980 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1981 err = -EINVAL;
1982 } else if (!netif_running(odev)) {
1983 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1984 err = -ENETDOWN;
1985 } else {
1986 pkt_dev->odev = odev;
1987 return 0;
1990 dev_put(odev);
1991 return err;
1994 /* Read pkt_dev from the interface and set up internal pktgen_dev
1995 * structure to have the right information to create/send packets
1997 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1999 if (!pkt_dev->odev) {
2000 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
2001 "setup_inject.\n");
2002 sprintf(pkt_dev->result,
2003 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2004 return;
2007 /* Default to the interface's mac if not explicitly set. */
2009 if (is_zero_ether_addr(pkt_dev->src_mac))
2010 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2012 /* Set up Dest MAC */
2013 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2015 /* Set up pkt size */
2016 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2018 if (pkt_dev->flags & F_IPV6) {
2020 * Skip this automatic address setting until locks or functions
2021 * gets exported
2024 #ifdef NOTNOW
2025 int i, set = 0, err = 1;
2026 struct inet6_dev *idev;
2028 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2029 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2030 set = 1;
2031 break;
2034 if (!set) {
2037 * Use linklevel address if unconfigured.
2039 * use ipv6_get_lladdr if/when it's get exported
2042 rcu_read_lock();
2043 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2044 struct inet6_ifaddr *ifp;
2046 read_lock_bh(&idev->lock);
2047 for (ifp = idev->addr_list; ifp;
2048 ifp = ifp->if_next) {
2049 if (ifp->scope == IFA_LINK
2050 && !(ifp->
2051 flags & IFA_F_TENTATIVE)) {
2052 ipv6_addr_copy(&pkt_dev->
2053 cur_in6_saddr,
2054 &ifp->addr);
2055 err = 0;
2056 break;
2059 read_unlock_bh(&idev->lock);
2061 rcu_read_unlock();
2062 if (err)
2063 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2064 "address not availble.\n");
2066 #endif
2067 } else {
2068 pkt_dev->saddr_min = 0;
2069 pkt_dev->saddr_max = 0;
2070 if (strlen(pkt_dev->src_min) == 0) {
2072 struct in_device *in_dev;
2074 rcu_read_lock();
2075 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2076 if (in_dev) {
2077 if (in_dev->ifa_list) {
2078 pkt_dev->saddr_min =
2079 in_dev->ifa_list->ifa_address;
2080 pkt_dev->saddr_max = pkt_dev->saddr_min;
2083 rcu_read_unlock();
2084 } else {
2085 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2086 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2089 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2090 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2092 /* Initialize current values. */
2093 pkt_dev->cur_dst_mac_offset = 0;
2094 pkt_dev->cur_src_mac_offset = 0;
2095 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2096 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2097 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2098 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2099 pkt_dev->nflows = 0;
2102 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2104 __u64 start;
2105 __u64 now;
2107 start = now = getCurUs();
2108 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2109 while (now < spin_until_us) {
2110 /* TODO: optimize sleeping behavior */
2111 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2112 schedule_timeout_interruptible(1);
2113 else if (spin_until_us - now > 100) {
2114 do_softirq();
2115 if (!pkt_dev->running)
2116 return;
2117 if (need_resched())
2118 schedule();
2121 now = getCurUs();
2124 pkt_dev->idle_acc += now - start;
2127 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2129 pkt_dev->pkt_overhead = 0;
2130 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2131 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2132 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2135 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2138 if (pkt_dev->flows[flow].flags & F_INIT)
2139 return 1;
2140 else
2141 return 0;
2144 static inline int f_pick(struct pktgen_dev *pkt_dev)
2146 int flow = pkt_dev->curfl;
2148 if (pkt_dev->flags & F_FLOW_SEQ) {
2149 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2150 /* reset time */
2151 pkt_dev->flows[flow].count = 0;
2152 pkt_dev->curfl += 1;
2153 if (pkt_dev->curfl >= pkt_dev->cflows)
2154 pkt_dev->curfl = 0; /*reset */
2156 } else {
2157 flow = random32() % pkt_dev->cflows;
2159 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2160 pkt_dev->flows[flow].count = 0;
2163 return pkt_dev->curfl;
2167 #ifdef CONFIG_XFRM
2168 /* If there was already an IPSEC SA, we keep it as is, else
2169 * we go look for it ...
2171 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2173 struct xfrm_state *x = pkt_dev->flows[flow].x;
2174 if (!x) {
2175 /*slow path: we dont already have xfrm_state*/
2176 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2177 (xfrm_address_t *)&pkt_dev->cur_saddr,
2178 AF_INET,
2179 pkt_dev->ipsmode,
2180 pkt_dev->ipsproto, 0);
2181 if (x) {
2182 pkt_dev->flows[flow].x = x;
2183 set_pkt_overhead(pkt_dev);
2184 pkt_dev->pkt_overhead+=x->props.header_len;
2189 #endif
2190 /* Increment/randomize headers according to flags and current values
2191 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2193 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2195 __u32 imn;
2196 __u32 imx;
2197 int flow = 0;
2199 if (pkt_dev->cflows)
2200 flow = f_pick(pkt_dev);
2202 /* Deal with source MAC */
2203 if (pkt_dev->src_mac_count > 1) {
2204 __u32 mc;
2205 __u32 tmp;
2207 if (pkt_dev->flags & F_MACSRC_RND)
2208 mc = random32() % pkt_dev->src_mac_count;
2209 else {
2210 mc = pkt_dev->cur_src_mac_offset++;
2211 if (pkt_dev->cur_src_mac_offset >
2212 pkt_dev->src_mac_count)
2213 pkt_dev->cur_src_mac_offset = 0;
2216 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2217 pkt_dev->hh[11] = tmp;
2218 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2219 pkt_dev->hh[10] = tmp;
2220 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2221 pkt_dev->hh[9] = tmp;
2222 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2223 pkt_dev->hh[8] = tmp;
2224 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2225 pkt_dev->hh[7] = tmp;
2228 /* Deal with Destination MAC */
2229 if (pkt_dev->dst_mac_count > 1) {
2230 __u32 mc;
2231 __u32 tmp;
2233 if (pkt_dev->flags & F_MACDST_RND)
2234 mc = random32() % pkt_dev->dst_mac_count;
2236 else {
2237 mc = pkt_dev->cur_dst_mac_offset++;
2238 if (pkt_dev->cur_dst_mac_offset >
2239 pkt_dev->dst_mac_count) {
2240 pkt_dev->cur_dst_mac_offset = 0;
2244 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2245 pkt_dev->hh[5] = tmp;
2246 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2247 pkt_dev->hh[4] = tmp;
2248 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2249 pkt_dev->hh[3] = tmp;
2250 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2251 pkt_dev->hh[2] = tmp;
2252 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2253 pkt_dev->hh[1] = tmp;
2256 if (pkt_dev->flags & F_MPLS_RND) {
2257 unsigned i;
2258 for (i = 0; i < pkt_dev->nr_labels; i++)
2259 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2260 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2261 ((__force __be32)random32() &
2262 htonl(0x000fffff));
2265 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2266 pkt_dev->vlan_id = random32() & (4096-1);
2269 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2270 pkt_dev->svlan_id = random32() & (4096 - 1);
2273 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2274 if (pkt_dev->flags & F_UDPSRC_RND)
2275 pkt_dev->cur_udp_src = random32() %
2276 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2277 + pkt_dev->udp_src_min;
2279 else {
2280 pkt_dev->cur_udp_src++;
2281 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2282 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2286 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2287 if (pkt_dev->flags & F_UDPDST_RND) {
2288 pkt_dev->cur_udp_dst = random32() %
2289 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2290 + pkt_dev->udp_dst_min;
2291 } else {
2292 pkt_dev->cur_udp_dst++;
2293 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2294 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2298 if (!(pkt_dev->flags & F_IPV6)) {
2300 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2301 ntohl(pkt_dev->
2302 saddr_max))) {
2303 __u32 t;
2304 if (pkt_dev->flags & F_IPSRC_RND)
2305 t = random32() % (imx - imn) + imn;
2306 else {
2307 t = ntohl(pkt_dev->cur_saddr);
2308 t++;
2309 if (t > imx) {
2310 t = imn;
2313 pkt_dev->cur_saddr = htonl(t);
2316 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2317 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2318 } else {
2319 imn = ntohl(pkt_dev->daddr_min);
2320 imx = ntohl(pkt_dev->daddr_max);
2321 if (imn < imx) {
2322 __u32 t;
2323 __be32 s;
2324 if (pkt_dev->flags & F_IPDST_RND) {
2326 t = random32() % (imx - imn) + imn;
2327 s = htonl(t);
2329 while (LOOPBACK(s) || MULTICAST(s)
2330 || BADCLASS(s) || ZERONET(s)
2331 || LOCAL_MCAST(s)) {
2332 t = random32() % (imx - imn) + imn;
2333 s = htonl(t);
2335 pkt_dev->cur_daddr = s;
2336 } else {
2337 t = ntohl(pkt_dev->cur_daddr);
2338 t++;
2339 if (t > imx) {
2340 t = imn;
2342 pkt_dev->cur_daddr = htonl(t);
2345 if (pkt_dev->cflows) {
2346 pkt_dev->flows[flow].flags |= F_INIT;
2347 pkt_dev->flows[flow].cur_daddr =
2348 pkt_dev->cur_daddr;
2349 #ifdef CONFIG_XFRM
2350 if (pkt_dev->flags & F_IPSEC_ON)
2351 get_ipsec_sa(pkt_dev, flow);
2352 #endif
2353 pkt_dev->nflows++;
2356 } else { /* IPV6 * */
2358 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2359 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2360 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2361 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2362 else {
2363 int i;
2365 /* Only random destinations yet */
2367 for (i = 0; i < 4; i++) {
2368 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2369 (((__force __be32)random32() |
2370 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2371 pkt_dev->max_in6_daddr.s6_addr32[i]);
2376 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2377 __u32 t;
2378 if (pkt_dev->flags & F_TXSIZE_RND) {
2379 t = random32() %
2380 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2381 + pkt_dev->min_pkt_size;
2382 } else {
2383 t = pkt_dev->cur_pkt_size + 1;
2384 if (t > pkt_dev->max_pkt_size)
2385 t = pkt_dev->min_pkt_size;
2387 pkt_dev->cur_pkt_size = t;
2390 pkt_dev->flows[flow].count++;
2394 #ifdef CONFIG_XFRM
2395 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2397 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2398 int err = 0;
2399 struct iphdr *iph;
2401 if (!x)
2402 return 0;
2403 /* XXX: we dont support tunnel mode for now until
2404 * we resolve the dst issue */
2405 if (x->props.mode != XFRM_MODE_TRANSPORT)
2406 return 0;
2408 spin_lock(&x->lock);
2409 iph = ip_hdr(skb);
2411 err = x->mode->output(x, skb);
2412 if (err)
2413 goto error;
2414 err = x->type->output(x, skb);
2415 if (err)
2416 goto error;
2418 x->curlft.bytes +=skb->len;
2419 x->curlft.packets++;
2420 spin_unlock(&x->lock);
2422 error:
2423 spin_unlock(&x->lock);
2424 return err;
2427 static inline void free_SAs(struct pktgen_dev *pkt_dev)
2429 if (pkt_dev->cflows) {
2430 /* let go of the SAs if we have them */
2431 int i = 0;
2432 for (; i < pkt_dev->nflows; i++){
2433 struct xfrm_state *x = pkt_dev->flows[i].x;
2434 if (x) {
2435 xfrm_state_put(x);
2436 pkt_dev->flows[i].x = NULL;
2442 static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2443 struct sk_buff *skb, __be16 protocol)
2445 if (pkt_dev->flags & F_IPSEC_ON) {
2446 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2447 int nhead = 0;
2448 if (x) {
2449 int ret;
2450 __u8 *eth;
2451 nhead = x->props.header_len - skb_headroom(skb);
2452 if (nhead >0) {
2453 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2454 if (ret < 0) {
2455 printk(KERN_ERR "Error expanding "
2456 "ipsec packet %d\n",ret);
2457 return 0;
2461 /* ipsec is not expecting ll header */
2462 skb_pull(skb, ETH_HLEN);
2463 ret = pktgen_output_ipsec(skb, pkt_dev);
2464 if (ret) {
2465 printk(KERN_ERR "Error creating ipsec "
2466 "packet %d\n",ret);
2467 kfree_skb(skb);
2468 return 0;
2470 /* restore ll */
2471 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2472 memcpy(eth, pkt_dev->hh, 12);
2473 *(u16 *) & eth[12] = protocol;
2476 return 1;
2478 #endif
2480 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2482 unsigned i;
2483 for (i = 0; i < pkt_dev->nr_labels; i++) {
2484 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2486 mpls--;
2487 *mpls |= MPLS_STACK_BOTTOM;
2490 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2491 unsigned int prio)
2493 return htons(id | (cfi << 12) | (prio << 13));
2496 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2497 struct pktgen_dev *pkt_dev)
2499 struct sk_buff *skb = NULL;
2500 __u8 *eth;
2501 struct udphdr *udph;
2502 int datalen, iplen;
2503 struct iphdr *iph;
2504 struct pktgen_hdr *pgh = NULL;
2505 __be16 protocol = htons(ETH_P_IP);
2506 __be32 *mpls;
2507 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2508 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2509 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2510 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2513 if (pkt_dev->nr_labels)
2514 protocol = htons(ETH_P_MPLS_UC);
2516 if (pkt_dev->vlan_id != 0xffff)
2517 protocol = htons(ETH_P_8021Q);
2519 /* Update any of the values, used when we're incrementing various
2520 * fields.
2522 mod_cur_headers(pkt_dev);
2524 datalen = (odev->hard_header_len + 16) & ~0xf;
2525 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2526 pkt_dev->pkt_overhead, GFP_ATOMIC);
2527 if (!skb) {
2528 sprintf(pkt_dev->result, "No memory");
2529 return NULL;
2532 skb_reserve(skb, datalen);
2534 /* Reserve for ethernet and IP header */
2535 eth = (__u8 *) skb_push(skb, 14);
2536 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2537 if (pkt_dev->nr_labels)
2538 mpls_push(mpls, pkt_dev);
2540 if (pkt_dev->vlan_id != 0xffff) {
2541 if (pkt_dev->svlan_id != 0xffff) {
2542 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2543 *svlan_tci = build_tci(pkt_dev->svlan_id,
2544 pkt_dev->svlan_cfi,
2545 pkt_dev->svlan_p);
2546 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2547 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2549 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2550 *vlan_tci = build_tci(pkt_dev->vlan_id,
2551 pkt_dev->vlan_cfi,
2552 pkt_dev->vlan_p);
2553 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2554 *vlan_encapsulated_proto = htons(ETH_P_IP);
2557 skb->network_header = skb->tail;
2558 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2559 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2561 iph = ip_hdr(skb);
2562 udph = udp_hdr(skb);
2564 memcpy(eth, pkt_dev->hh, 12);
2565 *(__be16 *) & eth[12] = protocol;
2567 /* Eth + IPh + UDPh + mpls */
2568 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2569 pkt_dev->pkt_overhead;
2570 if (datalen < sizeof(struct pktgen_hdr))
2571 datalen = sizeof(struct pktgen_hdr);
2573 udph->source = htons(pkt_dev->cur_udp_src);
2574 udph->dest = htons(pkt_dev->cur_udp_dst);
2575 udph->len = htons(datalen + 8); /* DATA + udphdr */
2576 udph->check = 0; /* No checksum */
2578 iph->ihl = 5;
2579 iph->version = 4;
2580 iph->ttl = 32;
2581 iph->tos = pkt_dev->tos;
2582 iph->protocol = IPPROTO_UDP; /* UDP */
2583 iph->saddr = pkt_dev->cur_saddr;
2584 iph->daddr = pkt_dev->cur_daddr;
2585 iph->frag_off = 0;
2586 iplen = 20 + 8 + datalen;
2587 iph->tot_len = htons(iplen);
2588 iph->check = 0;
2589 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2590 skb->protocol = protocol;
2591 skb->mac_header = (skb->network_header - ETH_HLEN -
2592 pkt_dev->pkt_overhead);
2593 skb->dev = odev;
2594 skb->pkt_type = PACKET_HOST;
2596 if (pkt_dev->nfrags <= 0)
2597 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2598 else {
2599 int frags = pkt_dev->nfrags;
2600 int i;
2602 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2604 if (frags > MAX_SKB_FRAGS)
2605 frags = MAX_SKB_FRAGS;
2606 if (datalen > frags * PAGE_SIZE) {
2607 skb_put(skb, datalen - frags * PAGE_SIZE);
2608 datalen = frags * PAGE_SIZE;
2611 i = 0;
2612 while (datalen > 0) {
2613 struct page *page = alloc_pages(GFP_KERNEL, 0);
2614 skb_shinfo(skb)->frags[i].page = page;
2615 skb_shinfo(skb)->frags[i].page_offset = 0;
2616 skb_shinfo(skb)->frags[i].size =
2617 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2618 datalen -= skb_shinfo(skb)->frags[i].size;
2619 skb->len += skb_shinfo(skb)->frags[i].size;
2620 skb->data_len += skb_shinfo(skb)->frags[i].size;
2621 i++;
2622 skb_shinfo(skb)->nr_frags = i;
2625 while (i < frags) {
2626 int rem;
2628 if (i == 0)
2629 break;
2631 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2632 if (rem == 0)
2633 break;
2635 skb_shinfo(skb)->frags[i - 1].size -= rem;
2637 skb_shinfo(skb)->frags[i] =
2638 skb_shinfo(skb)->frags[i - 1];
2639 get_page(skb_shinfo(skb)->frags[i].page);
2640 skb_shinfo(skb)->frags[i].page =
2641 skb_shinfo(skb)->frags[i - 1].page;
2642 skb_shinfo(skb)->frags[i].page_offset +=
2643 skb_shinfo(skb)->frags[i - 1].size;
2644 skb_shinfo(skb)->frags[i].size = rem;
2645 i++;
2646 skb_shinfo(skb)->nr_frags = i;
2650 /* Stamp the time, and sequence number, convert them to network byte order */
2652 if (pgh) {
2653 struct timeval timestamp;
2655 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2656 pgh->seq_num = htonl(pkt_dev->seq_num);
2658 do_gettimeofday(&timestamp);
2659 pgh->tv_sec = htonl(timestamp.tv_sec);
2660 pgh->tv_usec = htonl(timestamp.tv_usec);
2663 #ifdef CONFIG_XFRM
2664 if (!process_ipsec(pkt_dev, skb, protocol))
2665 return NULL;
2666 #endif
2668 return skb;
2672 * scan_ip6, fmt_ip taken from dietlibc-0.21
2673 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2675 * Slightly modified for kernel.
2676 * Should be candidate for net/ipv4/utils.c
2677 * --ro
2680 static unsigned int scan_ip6(const char *s, char ip[16])
2682 unsigned int i;
2683 unsigned int len = 0;
2684 unsigned long u;
2685 char suffix[16];
2686 unsigned int prefixlen = 0;
2687 unsigned int suffixlen = 0;
2688 __be32 tmp;
2690 for (i = 0; i < 16; i++)
2691 ip[i] = 0;
2693 for (;;) {
2694 if (*s == ':') {
2695 len++;
2696 if (s[1] == ':') { /* Found "::", skip to part 2 */
2697 s += 2;
2698 len++;
2699 break;
2701 s++;
2704 char *tmp;
2705 u = simple_strtoul(s, &tmp, 16);
2706 i = tmp - s;
2709 if (!i)
2710 return 0;
2711 if (prefixlen == 12 && s[i] == '.') {
2713 /* the last 4 bytes may be written as IPv4 address */
2715 tmp = in_aton(s);
2716 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2717 return i + len;
2719 ip[prefixlen++] = (u >> 8);
2720 ip[prefixlen++] = (u & 255);
2721 s += i;
2722 len += i;
2723 if (prefixlen == 16)
2724 return len;
2727 /* part 2, after "::" */
2728 for (;;) {
2729 if (*s == ':') {
2730 if (suffixlen == 0)
2731 break;
2732 s++;
2733 len++;
2734 } else if (suffixlen != 0)
2735 break;
2737 char *tmp;
2738 u = simple_strtol(s, &tmp, 16);
2739 i = tmp - s;
2741 if (!i) {
2742 if (*s)
2743 len--;
2744 break;
2746 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2747 tmp = in_aton(s);
2748 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2749 sizeof(tmp));
2750 suffixlen += 4;
2751 len += strlen(s);
2752 break;
2754 suffix[suffixlen++] = (u >> 8);
2755 suffix[suffixlen++] = (u & 255);
2756 s += i;
2757 len += i;
2758 if (prefixlen + suffixlen == 16)
2759 break;
2761 for (i = 0; i < suffixlen; i++)
2762 ip[16 - suffixlen + i] = suffix[i];
2763 return len;
2766 static char tohex(char hexdigit)
2768 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2771 static int fmt_xlong(char *s, unsigned int i)
2773 char *bak = s;
2774 *s = tohex((i >> 12) & 0xf);
2775 if (s != bak || *s != '0')
2776 ++s;
2777 *s = tohex((i >> 8) & 0xf);
2778 if (s != bak || *s != '0')
2779 ++s;
2780 *s = tohex((i >> 4) & 0xf);
2781 if (s != bak || *s != '0')
2782 ++s;
2783 *s = tohex(i & 0xf);
2784 return s - bak + 1;
2787 static unsigned int fmt_ip6(char *s, const char ip[16])
2789 unsigned int len;
2790 unsigned int i;
2791 unsigned int temp;
2792 unsigned int compressing;
2793 int j;
2795 len = 0;
2796 compressing = 0;
2797 for (j = 0; j < 16; j += 2) {
2799 #ifdef V4MAPPEDPREFIX
2800 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2801 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2802 temp = strlen(s);
2803 return len + temp;
2805 #endif
2806 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2807 (unsigned long)(unsigned char)ip[j + 1];
2808 if (temp == 0) {
2809 if (!compressing) {
2810 compressing = 1;
2811 if (j == 0) {
2812 *s++ = ':';
2813 ++len;
2816 } else {
2817 if (compressing) {
2818 compressing = 0;
2819 *s++ = ':';
2820 ++len;
2822 i = fmt_xlong(s, temp);
2823 len += i;
2824 s += i;
2825 if (j < 14) {
2826 *s++ = ':';
2827 ++len;
2831 if (compressing) {
2832 *s++ = ':';
2833 ++len;
2835 *s = 0;
2836 return len;
2839 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2840 struct pktgen_dev *pkt_dev)
2842 struct sk_buff *skb = NULL;
2843 __u8 *eth;
2844 struct udphdr *udph;
2845 int datalen;
2846 struct ipv6hdr *iph;
2847 struct pktgen_hdr *pgh = NULL;
2848 __be16 protocol = htons(ETH_P_IPV6);
2849 __be32 *mpls;
2850 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2851 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2852 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2853 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2855 if (pkt_dev->nr_labels)
2856 protocol = htons(ETH_P_MPLS_UC);
2858 if (pkt_dev->vlan_id != 0xffff)
2859 protocol = htons(ETH_P_8021Q);
2861 /* Update any of the values, used when we're incrementing various
2862 * fields.
2864 mod_cur_headers(pkt_dev);
2866 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2867 pkt_dev->pkt_overhead, GFP_ATOMIC);
2868 if (!skb) {
2869 sprintf(pkt_dev->result, "No memory");
2870 return NULL;
2873 skb_reserve(skb, 16);
2875 /* Reserve for ethernet and IP header */
2876 eth = (__u8 *) skb_push(skb, 14);
2877 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2878 if (pkt_dev->nr_labels)
2879 mpls_push(mpls, pkt_dev);
2881 if (pkt_dev->vlan_id != 0xffff) {
2882 if (pkt_dev->svlan_id != 0xffff) {
2883 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2884 *svlan_tci = build_tci(pkt_dev->svlan_id,
2885 pkt_dev->svlan_cfi,
2886 pkt_dev->svlan_p);
2887 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2888 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2890 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2891 *vlan_tci = build_tci(pkt_dev->vlan_id,
2892 pkt_dev->vlan_cfi,
2893 pkt_dev->vlan_p);
2894 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2895 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2898 skb->network_header = skb->tail;
2899 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2900 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2902 iph = ipv6_hdr(skb);
2903 udph = udp_hdr(skb);
2905 memcpy(eth, pkt_dev->hh, 12);
2906 *(__be16 *) & eth[12] = protocol;
2908 /* Eth + IPh + UDPh + mpls */
2909 datalen = pkt_dev->cur_pkt_size - 14 -
2910 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2911 pkt_dev->pkt_overhead;
2913 if (datalen < sizeof(struct pktgen_hdr)) {
2914 datalen = sizeof(struct pktgen_hdr);
2915 if (net_ratelimit())
2916 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2917 datalen);
2920 udph->source = htons(pkt_dev->cur_udp_src);
2921 udph->dest = htons(pkt_dev->cur_udp_dst);
2922 udph->len = htons(datalen + sizeof(struct udphdr));
2923 udph->check = 0; /* No checksum */
2925 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2927 if (pkt_dev->traffic_class) {
2928 /* Version + traffic class + flow (0) */
2929 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2932 iph->hop_limit = 32;
2934 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2935 iph->nexthdr = IPPROTO_UDP;
2937 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2938 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2940 skb->mac_header = (skb->network_header - ETH_HLEN -
2941 pkt_dev->pkt_overhead);
2942 skb->protocol = protocol;
2943 skb->dev = odev;
2944 skb->pkt_type = PACKET_HOST;
2946 if (pkt_dev->nfrags <= 0)
2947 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2948 else {
2949 int frags = pkt_dev->nfrags;
2950 int i;
2952 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2954 if (frags > MAX_SKB_FRAGS)
2955 frags = MAX_SKB_FRAGS;
2956 if (datalen > frags * PAGE_SIZE) {
2957 skb_put(skb, datalen - frags * PAGE_SIZE);
2958 datalen = frags * PAGE_SIZE;
2961 i = 0;
2962 while (datalen > 0) {
2963 struct page *page = alloc_pages(GFP_KERNEL, 0);
2964 skb_shinfo(skb)->frags[i].page = page;
2965 skb_shinfo(skb)->frags[i].page_offset = 0;
2966 skb_shinfo(skb)->frags[i].size =
2967 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2968 datalen -= skb_shinfo(skb)->frags[i].size;
2969 skb->len += skb_shinfo(skb)->frags[i].size;
2970 skb->data_len += skb_shinfo(skb)->frags[i].size;
2971 i++;
2972 skb_shinfo(skb)->nr_frags = i;
2975 while (i < frags) {
2976 int rem;
2978 if (i == 0)
2979 break;
2981 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2982 if (rem == 0)
2983 break;
2985 skb_shinfo(skb)->frags[i - 1].size -= rem;
2987 skb_shinfo(skb)->frags[i] =
2988 skb_shinfo(skb)->frags[i - 1];
2989 get_page(skb_shinfo(skb)->frags[i].page);
2990 skb_shinfo(skb)->frags[i].page =
2991 skb_shinfo(skb)->frags[i - 1].page;
2992 skb_shinfo(skb)->frags[i].page_offset +=
2993 skb_shinfo(skb)->frags[i - 1].size;
2994 skb_shinfo(skb)->frags[i].size = rem;
2995 i++;
2996 skb_shinfo(skb)->nr_frags = i;
3000 /* Stamp the time, and sequence number, convert them to network byte order */
3001 /* should we update cloned packets too ? */
3002 if (pgh) {
3003 struct timeval timestamp;
3005 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3006 pgh->seq_num = htonl(pkt_dev->seq_num);
3008 do_gettimeofday(&timestamp);
3009 pgh->tv_sec = htonl(timestamp.tv_sec);
3010 pgh->tv_usec = htonl(timestamp.tv_usec);
3012 /* pkt_dev->seq_num++; FF: you really mean this? */
3014 return skb;
3017 static inline struct sk_buff *fill_packet(struct net_device *odev,
3018 struct pktgen_dev *pkt_dev)
3020 if (pkt_dev->flags & F_IPV6)
3021 return fill_packet_ipv6(odev, pkt_dev);
3022 else
3023 return fill_packet_ipv4(odev, pkt_dev);
3026 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3028 pkt_dev->seq_num = 1;
3029 pkt_dev->idle_acc = 0;
3030 pkt_dev->sofar = 0;
3031 pkt_dev->tx_bytes = 0;
3032 pkt_dev->errors = 0;
3035 /* Set up structure for sending pkts, clear counters */
3037 static void pktgen_run(struct pktgen_thread *t)
3039 struct pktgen_dev *pkt_dev;
3040 int started = 0;
3042 pr_debug("pktgen: entering pktgen_run. %p\n", t);
3044 if_lock(t);
3045 list_for_each_entry(pkt_dev, &t->if_list, list) {
3048 * setup odev and create initial packet.
3050 pktgen_setup_inject(pkt_dev);
3052 if (pkt_dev->odev) {
3053 pktgen_clear_counters(pkt_dev);
3054 pkt_dev->running = 1; /* Cranke yeself! */
3055 pkt_dev->skb = NULL;
3056 pkt_dev->started_at = getCurUs();
3057 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
3058 pkt_dev->next_tx_ns = 0;
3059 set_pkt_overhead(pkt_dev);
3061 strcpy(pkt_dev->result, "Starting");
3062 started++;
3063 } else
3064 strcpy(pkt_dev->result, "Error starting");
3066 if_unlock(t);
3067 if (started)
3068 t->control &= ~(T_STOP);
3071 static void pktgen_stop_all_threads_ifs(void)
3073 struct pktgen_thread *t;
3075 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3077 mutex_lock(&pktgen_thread_lock);
3079 list_for_each_entry(t, &pktgen_threads, th_list)
3080 t->control |= T_STOP;
3082 mutex_unlock(&pktgen_thread_lock);
3085 static int thread_is_running(struct pktgen_thread *t)
3087 struct pktgen_dev *pkt_dev;
3088 int res = 0;
3090 list_for_each_entry(pkt_dev, &t->if_list, list)
3091 if (pkt_dev->running) {
3092 res = 1;
3093 break;
3095 return res;
3098 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3100 if_lock(t);
3102 while (thread_is_running(t)) {
3104 if_unlock(t);
3106 msleep_interruptible(100);
3108 if (signal_pending(current))
3109 goto signal;
3110 if_lock(t);
3112 if_unlock(t);
3113 return 1;
3114 signal:
3115 return 0;
3118 static int pktgen_wait_all_threads_run(void)
3120 struct pktgen_thread *t;
3121 int sig = 1;
3123 mutex_lock(&pktgen_thread_lock);
3125 list_for_each_entry(t, &pktgen_threads, th_list) {
3126 sig = pktgen_wait_thread_run(t);
3127 if (sig == 0)
3128 break;
3131 if (sig == 0)
3132 list_for_each_entry(t, &pktgen_threads, th_list)
3133 t->control |= (T_STOP);
3135 mutex_unlock(&pktgen_thread_lock);
3136 return sig;
3139 static void pktgen_run_all_threads(void)
3141 struct pktgen_thread *t;
3143 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3145 mutex_lock(&pktgen_thread_lock);
3147 list_for_each_entry(t, &pktgen_threads, th_list)
3148 t->control |= (T_RUN);
3150 mutex_unlock(&pktgen_thread_lock);
3152 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3154 pktgen_wait_all_threads_run();
3157 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3159 __u64 total_us, bps, mbps, pps, idle;
3160 char *p = pkt_dev->result;
3162 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3164 idle = pkt_dev->idle_acc;
3166 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3167 (unsigned long long)total_us,
3168 (unsigned long long)(total_us - idle),
3169 (unsigned long long)idle,
3170 (unsigned long long)pkt_dev->sofar,
3171 pkt_dev->cur_pkt_size, nr_frags);
3173 pps = pkt_dev->sofar * USEC_PER_SEC;
3175 while ((total_us >> 32) != 0) {
3176 pps >>= 1;
3177 total_us >>= 1;
3180 do_div(pps, total_us);
3182 bps = pps * 8 * pkt_dev->cur_pkt_size;
3184 mbps = bps;
3185 do_div(mbps, 1000000);
3186 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3187 (unsigned long long)pps,
3188 (unsigned long long)mbps,
3189 (unsigned long long)bps,
3190 (unsigned long long)pkt_dev->errors);
3193 /* Set stopped-at timer, remove from running list, do counters & statistics */
3195 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3197 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3199 if (!pkt_dev->running) {
3200 printk(KERN_WARNING "pktgen: interface: %s is already "
3201 "stopped\n", pkt_dev->odev->name);
3202 return -EINVAL;
3205 pkt_dev->stopped_at = getCurUs();
3206 pkt_dev->running = 0;
3208 show_results(pkt_dev, nr_frags);
3210 return 0;
3213 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3215 struct pktgen_dev *pkt_dev, *best = NULL;
3217 if_lock(t);
3219 list_for_each_entry(pkt_dev, &t->if_list, list) {
3220 if (!pkt_dev->running)
3221 continue;
3222 if (best == NULL)
3223 best = pkt_dev;
3224 else if (pkt_dev->next_tx_us < best->next_tx_us)
3225 best = pkt_dev;
3227 if_unlock(t);
3228 return best;
3231 static void pktgen_stop(struct pktgen_thread *t)
3233 struct pktgen_dev *pkt_dev;
3235 pr_debug("pktgen: entering pktgen_stop\n");
3237 if_lock(t);
3239 list_for_each_entry(pkt_dev, &t->if_list, list) {
3240 pktgen_stop_device(pkt_dev);
3241 if (pkt_dev->skb)
3242 kfree_skb(pkt_dev->skb);
3244 pkt_dev->skb = NULL;
3247 if_unlock(t);
3251 * one of our devices needs to be removed - find it
3252 * and remove it
3254 static void pktgen_rem_one_if(struct pktgen_thread *t)
3256 struct list_head *q, *n;
3257 struct pktgen_dev *cur;
3259 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3261 if_lock(t);
3263 list_for_each_safe(q, n, &t->if_list) {
3264 cur = list_entry(q, struct pktgen_dev, list);
3266 if (!cur->removal_mark)
3267 continue;
3269 if (cur->skb)
3270 kfree_skb(cur->skb);
3271 cur->skb = NULL;
3273 pktgen_remove_device(t, cur);
3275 break;
3278 if_unlock(t);
3281 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3283 struct list_head *q, *n;
3284 struct pktgen_dev *cur;
3286 /* Remove all devices, free mem */
3288 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3289 if_lock(t);
3291 list_for_each_safe(q, n, &t->if_list) {
3292 cur = list_entry(q, struct pktgen_dev, list);
3294 if (cur->skb)
3295 kfree_skb(cur->skb);
3296 cur->skb = NULL;
3298 pktgen_remove_device(t, cur);
3301 if_unlock(t);
3304 static void pktgen_rem_thread(struct pktgen_thread *t)
3306 /* Remove from the thread list */
3308 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3310 mutex_lock(&pktgen_thread_lock);
3312 list_del(&t->th_list);
3314 mutex_unlock(&pktgen_thread_lock);
3317 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3319 struct net_device *odev = NULL;
3320 __u64 idle_start = 0;
3321 int ret;
3323 odev = pkt_dev->odev;
3325 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3326 u64 now;
3328 now = getCurUs();
3329 if (now < pkt_dev->next_tx_us)
3330 spin(pkt_dev, pkt_dev->next_tx_us);
3332 /* This is max DELAY, this has special meaning of
3333 * "never transmit"
3335 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3336 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3337 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3338 goto out;
3342 if ((netif_queue_stopped(odev) ||
3343 (pkt_dev->skb &&
3344 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping))) ||
3345 need_resched()) {
3346 idle_start = getCurUs();
3348 if (!netif_running(odev)) {
3349 pktgen_stop_device(pkt_dev);
3350 if (pkt_dev->skb)
3351 kfree_skb(pkt_dev->skb);
3352 pkt_dev->skb = NULL;
3353 goto out;
3355 if (need_resched())
3356 schedule();
3358 pkt_dev->idle_acc += getCurUs() - idle_start;
3360 if (netif_queue_stopped(odev) ||
3361 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3362 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3363 pkt_dev->next_tx_ns = 0;
3364 goto out; /* Try the next interface */
3368 if (pkt_dev->last_ok || !pkt_dev->skb) {
3369 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3370 || (!pkt_dev->skb)) {
3371 /* build a new pkt */
3372 if (pkt_dev->skb)
3373 kfree_skb(pkt_dev->skb);
3375 pkt_dev->skb = fill_packet(odev, pkt_dev);
3376 if (pkt_dev->skb == NULL) {
3377 printk(KERN_ERR "pktgen: ERROR: couldn't "
3378 "allocate skb in fill_packet.\n");
3379 schedule();
3380 pkt_dev->clone_count--; /* back out increment, OOM */
3381 goto out;
3383 pkt_dev->allocated_skbs++;
3384 pkt_dev->clone_count = 0; /* reset counter */
3388 netif_tx_lock_bh(odev);
3389 if (!netif_queue_stopped(odev) &&
3390 !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3392 atomic_inc(&(pkt_dev->skb->users));
3393 retry_now:
3394 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3395 if (likely(ret == NETDEV_TX_OK)) {
3396 pkt_dev->last_ok = 1;
3397 pkt_dev->sofar++;
3398 pkt_dev->seq_num++;
3399 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3401 } else if (ret == NETDEV_TX_LOCKED
3402 && (odev->features & NETIF_F_LLTX)) {
3403 cpu_relax();
3404 goto retry_now;
3405 } else { /* Retry it next time */
3407 atomic_dec(&(pkt_dev->skb->users));
3409 if (debug && net_ratelimit())
3410 printk(KERN_INFO "pktgen: Hard xmit error\n");
3412 pkt_dev->errors++;
3413 pkt_dev->last_ok = 0;
3416 pkt_dev->next_tx_us = getCurUs();
3417 pkt_dev->next_tx_ns = 0;
3419 pkt_dev->next_tx_us += pkt_dev->delay_us;
3420 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3422 if (pkt_dev->next_tx_ns > 1000) {
3423 pkt_dev->next_tx_us++;
3424 pkt_dev->next_tx_ns -= 1000;
3428 else { /* Retry it next time */
3429 pkt_dev->last_ok = 0;
3430 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3431 pkt_dev->next_tx_ns = 0;
3434 netif_tx_unlock_bh(odev);
3436 /* If pkt_dev->count is zero, then run forever */
3437 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3438 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3439 idle_start = getCurUs();
3440 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3441 if (signal_pending(current)) {
3442 break;
3444 schedule();
3446 pkt_dev->idle_acc += getCurUs() - idle_start;
3449 /* Done with this */
3450 pktgen_stop_device(pkt_dev);
3451 if (pkt_dev->skb)
3452 kfree_skb(pkt_dev->skb);
3453 pkt_dev->skb = NULL;
3455 out:;
3459 * Main loop of the thread goes here
3462 static int pktgen_thread_worker(void *arg)
3464 DEFINE_WAIT(wait);
3465 struct pktgen_thread *t = arg;
3466 struct pktgen_dev *pkt_dev = NULL;
3467 int cpu = t->cpu;
3468 u32 max_before_softirq;
3469 u32 tx_since_softirq = 0;
3471 BUG_ON(smp_processor_id() != cpu);
3473 init_waitqueue_head(&t->queue);
3475 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
3477 max_before_softirq = t->max_before_softirq;
3479 set_current_state(TASK_INTERRUPTIBLE);
3481 set_freezable();
3483 while (!kthread_should_stop()) {
3484 pkt_dev = next_to_run(t);
3486 if (!pkt_dev &&
3487 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3488 == 0) {
3489 prepare_to_wait(&(t->queue), &wait,
3490 TASK_INTERRUPTIBLE);
3491 schedule_timeout(HZ / 10);
3492 finish_wait(&(t->queue), &wait);
3495 __set_current_state(TASK_RUNNING);
3497 if (pkt_dev) {
3499 pktgen_xmit(pkt_dev);
3502 * We like to stay RUNNING but must also give
3503 * others fair share.
3506 tx_since_softirq += pkt_dev->last_ok;
3508 if (tx_since_softirq > max_before_softirq) {
3509 if (local_softirq_pending())
3510 do_softirq();
3511 tx_since_softirq = 0;
3515 if (t->control & T_STOP) {
3516 pktgen_stop(t);
3517 t->control &= ~(T_STOP);
3520 if (t->control & T_RUN) {
3521 pktgen_run(t);
3522 t->control &= ~(T_RUN);
3525 if (t->control & T_REMDEVALL) {
3526 pktgen_rem_all_ifs(t);
3527 t->control &= ~(T_REMDEVALL);
3530 if (t->control & T_REMDEV) {
3531 pktgen_rem_one_if(t);
3532 t->control &= ~(T_REMDEV);
3535 try_to_freeze();
3537 set_current_state(TASK_INTERRUPTIBLE);
3540 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3541 pktgen_stop(t);
3543 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3544 pktgen_rem_all_ifs(t);
3546 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3547 pktgen_rem_thread(t);
3549 return 0;
3552 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3553 const char *ifname)
3555 struct pktgen_dev *p, *pkt_dev = NULL;
3556 if_lock(t);
3558 list_for_each_entry(p, &t->if_list, list)
3559 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3560 pkt_dev = p;
3561 break;
3564 if_unlock(t);
3565 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3566 return pkt_dev;
3570 * Adds a dev at front of if_list.
3573 static int add_dev_to_thread(struct pktgen_thread *t,
3574 struct pktgen_dev *pkt_dev)
3576 int rv = 0;
3578 if_lock(t);
3580 if (pkt_dev->pg_thread) {
3581 printk(KERN_ERR "pktgen: ERROR: already assigned "
3582 "to a thread.\n");
3583 rv = -EBUSY;
3584 goto out;
3587 list_add(&pkt_dev->list, &t->if_list);
3588 pkt_dev->pg_thread = t;
3589 pkt_dev->running = 0;
3591 out:
3592 if_unlock(t);
3593 return rv;
3596 /* Called under thread lock */
3598 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3600 struct pktgen_dev *pkt_dev;
3601 int err;
3603 /* We don't allow a device to be on several threads */
3605 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3606 if (pkt_dev) {
3607 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3608 return -EBUSY;
3611 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3612 if (!pkt_dev)
3613 return -ENOMEM;
3615 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3616 if (pkt_dev->flows == NULL) {
3617 kfree(pkt_dev);
3618 return -ENOMEM;
3620 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3622 pkt_dev->removal_mark = 0;
3623 pkt_dev->min_pkt_size = ETH_ZLEN;
3624 pkt_dev->max_pkt_size = ETH_ZLEN;
3625 pkt_dev->nfrags = 0;
3626 pkt_dev->clone_skb = pg_clone_skb_d;
3627 pkt_dev->delay_us = pg_delay_d / 1000;
3628 pkt_dev->delay_ns = pg_delay_d % 1000;
3629 pkt_dev->count = pg_count_d;
3630 pkt_dev->sofar = 0;
3631 pkt_dev->udp_src_min = 9; /* sink port */
3632 pkt_dev->udp_src_max = 9;
3633 pkt_dev->udp_dst_min = 9;
3634 pkt_dev->udp_dst_max = 9;
3636 pkt_dev->vlan_p = 0;
3637 pkt_dev->vlan_cfi = 0;
3638 pkt_dev->vlan_id = 0xffff;
3639 pkt_dev->svlan_p = 0;
3640 pkt_dev->svlan_cfi = 0;
3641 pkt_dev->svlan_id = 0xffff;
3643 err = pktgen_setup_dev(pkt_dev, ifname);
3644 if (err)
3645 goto out1;
3647 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3648 if (!pkt_dev->entry) {
3649 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3650 PG_PROC_DIR, ifname);
3651 err = -EINVAL;
3652 goto out2;
3654 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3655 pkt_dev->entry->data = pkt_dev;
3656 #ifdef CONFIG_XFRM
3657 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3658 pkt_dev->ipsproto = IPPROTO_ESP;
3659 #endif
3661 return add_dev_to_thread(t, pkt_dev);
3662 out2:
3663 dev_put(pkt_dev->odev);
3664 out1:
3665 #ifdef CONFIG_XFRM
3666 free_SAs(pkt_dev);
3667 #endif
3668 if (pkt_dev->flows)
3669 vfree(pkt_dev->flows);
3670 kfree(pkt_dev);
3671 return err;
3674 static int __init pktgen_create_thread(int cpu)
3676 struct pktgen_thread *t;
3677 struct proc_dir_entry *pe;
3678 struct task_struct *p;
3680 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3681 if (!t) {
3682 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3683 "create new thread.\n");
3684 return -ENOMEM;
3687 spin_lock_init(&t->if_lock);
3688 t->cpu = cpu;
3690 INIT_LIST_HEAD(&t->if_list);
3692 list_add_tail(&t->th_list, &pktgen_threads);
3694 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3695 if (IS_ERR(p)) {
3696 printk(KERN_ERR "pktgen: kernel_thread() failed "
3697 "for cpu %d\n", t->cpu);
3698 list_del(&t->th_list);
3699 kfree(t);
3700 return PTR_ERR(p);
3702 kthread_bind(p, cpu);
3703 t->tsk = p;
3705 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3706 if (!pe) {
3707 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3708 PG_PROC_DIR, t->tsk->comm);
3709 kthread_stop(p);
3710 list_del(&t->th_list);
3711 kfree(t);
3712 return -EINVAL;
3715 pe->proc_fops = &pktgen_thread_fops;
3716 pe->data = t;
3718 wake_up_process(p);
3720 return 0;
3724 * Removes a device from the thread if_list.
3726 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3727 struct pktgen_dev *pkt_dev)
3729 struct list_head *q, *n;
3730 struct pktgen_dev *p;
3732 list_for_each_safe(q, n, &t->if_list) {
3733 p = list_entry(q, struct pktgen_dev, list);
3734 if (p == pkt_dev)
3735 list_del(&p->list);
3739 static int pktgen_remove_device(struct pktgen_thread *t,
3740 struct pktgen_dev *pkt_dev)
3743 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3745 if (pkt_dev->running) {
3746 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3747 "running interface, stopping it now.\n");
3748 pktgen_stop_device(pkt_dev);
3751 /* Dis-associate from the interface */
3753 if (pkt_dev->odev) {
3754 dev_put(pkt_dev->odev);
3755 pkt_dev->odev = NULL;
3758 /* And update the thread if_list */
3760 _rem_dev_from_if_list(t, pkt_dev);
3762 if (pkt_dev->entry)
3763 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3765 #ifdef CONFIG_XFRM
3766 free_SAs(pkt_dev);
3767 #endif
3768 if (pkt_dev->flows)
3769 vfree(pkt_dev->flows);
3770 kfree(pkt_dev);
3771 return 0;
3774 static int __init pg_init(void)
3776 int cpu;
3777 struct proc_dir_entry *pe;
3779 printk(KERN_INFO "%s", version);
3781 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3782 if (!pg_proc_dir)
3783 return -ENODEV;
3784 pg_proc_dir->owner = THIS_MODULE;
3786 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3787 if (pe == NULL) {
3788 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3789 "procfs entry.\n", PGCTRL);
3790 proc_net_remove(PG_PROC_DIR);
3791 return -EINVAL;
3794 pe->proc_fops = &pktgen_fops;
3795 pe->data = NULL;
3797 /* Register us to receive netdevice events */
3798 register_netdevice_notifier(&pktgen_notifier_block);
3800 for_each_online_cpu(cpu) {
3801 int err;
3803 err = pktgen_create_thread(cpu);
3804 if (err)
3805 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3806 "thread for cpu %d (%d)\n", cpu, err);
3809 if (list_empty(&pktgen_threads)) {
3810 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3811 "all threads\n");
3812 unregister_netdevice_notifier(&pktgen_notifier_block);
3813 remove_proc_entry(PGCTRL, pg_proc_dir);
3814 proc_net_remove(PG_PROC_DIR);
3815 return -ENODEV;
3818 return 0;
3821 static void __exit pg_cleanup(void)
3823 struct pktgen_thread *t;
3824 struct list_head *q, *n;
3825 wait_queue_head_t queue;
3826 init_waitqueue_head(&queue);
3828 /* Stop all interfaces & threads */
3830 list_for_each_safe(q, n, &pktgen_threads) {
3831 t = list_entry(q, struct pktgen_thread, th_list);
3832 kthread_stop(t->tsk);
3833 kfree(t);
3836 /* Un-register us from receiving netdevice events */
3837 unregister_netdevice_notifier(&pktgen_notifier_block);
3839 /* Clean up proc file system */
3840 remove_proc_entry(PGCTRL, pg_proc_dir);
3841 proc_net_remove(PG_PROC_DIR);
3844 module_init(pg_init);
3845 module_exit(pg_cleanup);
3847 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3848 MODULE_DESCRIPTION("Packet Generator tool");
3849 MODULE_LICENSE("GPL");
3850 module_param(pg_count_d, int, 0);
3851 module_param(pg_delay_d, int, 0);
3852 module_param(pg_clone_skb_d, int, 0);
3853 module_param(debug, int, 0);