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
54 * Also moved to /proc/net/pktgen/
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.
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
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.
84 * Fix refcount off by one if first packet fails, potential null deref,
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>
110 #include <linux/sys.h>
111 #include <linux/types.h>
112 #include <linux/module.h>
113 #include <linux/moduleparam.h>
114 #include <linux/kernel.h>
115 #include <linux/smp_lock.h>
116 #include <linux/sched.h>
117 #include <linux/slab.h>
118 #include <linux/vmalloc.h>
119 #include <linux/unistd.h>
120 #include <linux/string.h>
121 #include <linux/ptrace.h>
122 #include <linux/errno.h>
123 #include <linux/ioport.h>
124 #include <linux/interrupt.h>
125 #include <linux/capability.h>
126 #include <linux/delay.h>
127 #include <linux/timer.h>
128 #include <linux/init.h>
129 #include <linux/skbuff.h>
130 #include <linux/netdevice.h>
131 #include <linux/inet.h>
132 #include <linux/inetdevice.h>
133 #include <linux/rtnetlink.h>
134 #include <linux/if_arp.h>
135 #include <linux/in.h>
136 #include <linux/ip.h>
137 #include <linux/ipv6.h>
138 #include <linux/udp.h>
139 #include <linux/proc_fs.h>
140 #include <linux/seq_file.h>
141 #include <linux/wait.h>
142 #include <linux/etherdevice.h>
143 #include <net/checksum.h>
144 #include <net/ipv6.h>
145 #include <net/addrconf.h>
146 #include <asm/byteorder.h>
147 #include <linux/rcupdate.h>
148 #include <asm/bitops.h>
151 #include <asm/uaccess.h>
152 #include <asm/div64.h> /* do_div */
153 #include <asm/timex.h>
156 #define VERSION "pktgen v2.64: Packet Generator for packet performance testing.\n"
158 /* #define PG_DEBUG(a) a */
161 /* The buckets are exponential in 'width' */
162 #define LAT_BUCKETS_MAX 32
163 #define IP_NAME_SZ 32
165 /* Device flag bits */
166 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
167 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
168 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
169 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
170 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
171 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
172 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
173 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
175 /* Thread control flag bits */
176 #define T_TERMINATE (1<<0)
177 #define T_STOP (1<<1) /* Stop run */
178 #define T_RUN (1<<2) /* Start run */
179 #define T_REMDEVALL (1<<3) /* Remove all devs */
180 #define T_REMDEV (1<<4) /* Remove one dev */
183 #define thread_lock() down(&pktgen_sem)
184 #define thread_unlock() up(&pktgen_sem)
186 /* If lock -- can be removed after some work */
187 #define if_lock(t) spin_lock(&(t->if_lock));
188 #define if_unlock(t) spin_unlock(&(t->if_lock));
190 /* Used to help with determining the pkts on receive */
191 #define PKTGEN_MAGIC 0xbe9be955
192 #define PG_PROC_DIR "pktgen"
193 #define PGCTRL "pgctrl"
194 static struct proc_dir_entry *pg_proc_dir = NULL;
196 #define MAX_CFLOWS 65536
207 * Try to keep frequent/infrequent used vars. separated.
210 char ifname[IFNAMSIZ];
213 struct pktgen_thread* pg_thread; /* the owner */
214 struct pktgen_dev *next; /* Used for chaining in the thread's run-queue */
216 int running; /* if this changes to false, the test will stop */
218 /* If min != max, then we will either do a linear iteration, or
219 * we will do a random selection from within the range.
222 int removal_mark; /* non-zero => the device is marked for
223 * removal by worker thread */
225 int min_pkt_size; /* = ETH_ZLEN; */
226 int max_pkt_size; /* = ETH_ZLEN; */
228 __u32 delay_us; /* Default delay */
230 __u64 count; /* Default No packets to send */
231 __u64 sofar; /* How many pkts we've sent so far */
232 __u64 tx_bytes; /* How many bytes we've transmitted */
233 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
235 /* runtime counters relating to clone_skb */
236 __u64 next_tx_us; /* timestamp of when to tx next */
239 __u64 allocated_skbs;
241 int last_ok; /* Was last skb sent?
242 * Or a failed transmit of some sort? This will keep
243 * sequence numbers in order, for example.
245 __u64 started_at; /* micro-seconds */
246 __u64 stopped_at; /* micro-seconds */
247 __u64 idle_acc; /* micro-seconds */
250 int clone_skb; /* Use multiple SKBs during packet gen. If this number
251 * is greater than 1, then that many copies of the same
252 * packet will be sent before a new packet is allocated.
253 * For instance, if you want to send 1024 identical packets
254 * before creating a new packet, set clone_skb to 1024.
257 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
258 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
259 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
260 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
262 struct in6_addr in6_saddr;
263 struct in6_addr in6_daddr;
264 struct in6_addr cur_in6_daddr;
265 struct in6_addr cur_in6_saddr;
267 struct in6_addr min_in6_daddr;
268 struct in6_addr max_in6_daddr;
269 struct in6_addr min_in6_saddr;
270 struct in6_addr max_in6_saddr;
272 /* If we're doing ranges, random or incremental, then this
273 * defines the min/max for those ranges.
275 __u32 saddr_min; /* inclusive, source IP address */
276 __u32 saddr_max; /* exclusive, source IP address */
277 __u32 daddr_min; /* inclusive, dest IP address */
278 __u32 daddr_max; /* exclusive, dest IP address */
280 __u16 udp_src_min; /* inclusive, source UDP port */
281 __u16 udp_src_max; /* exclusive, source UDP port */
282 __u16 udp_dst_min; /* inclusive, dest UDP port */
283 __u16 udp_dst_max; /* exclusive, dest UDP port */
285 __u32 src_mac_count; /* How many MACs to iterate through */
286 __u32 dst_mac_count; /* How many MACs to iterate through */
288 unsigned char dst_mac[ETH_ALEN];
289 unsigned char src_mac[ETH_ALEN];
291 __u32 cur_dst_mac_offset;
292 __u32 cur_src_mac_offset;
301 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
303 We fill in SRC address later
304 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 __u16 pad; /* pad out the hh struct to an even 16 bytes */
310 struct sk_buff* skb; /* skb we are to transmit next, mainly used for when we
311 * are transmitting the same one multiple times
313 struct net_device* odev; /* The out-going device. Note that the device should
314 * have it's pg_info pointer pointing back to this
315 * device. This will be set when the user specifies
316 * the out-going device name (not when the inject is
317 * started as it used to do.)
319 struct flow_state *flows;
320 unsigned cflows; /* Concurrent flows (config) */
321 unsigned lflow; /* Flow length (config) */
322 unsigned nflows; /* accumulated flows (stats) */
332 struct pktgen_thread {
334 struct pktgen_dev *if_list; /* All device here */
335 struct pktgen_thread* next;
338 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
340 /* Field for thread to receive "posted" events terminate, stop ifs etc.*/
346 wait_queue_head_t queue;
352 /* This code works around the fact that do_div cannot handle two 64-bit
353 numbers, and regular 64-bit division doesn't work on x86 kernels.
359 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
360 * Function copied/adapted/optimized from:
362 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
364 * Copyright 1994, University of Cambridge Computer Laboratory
365 * All Rights Reserved.
368 static inline s64 divremdi3(s64 x, s64 y, int type)
370 u64 a = (x < 0) ? -x : x;
371 u64 b = (y < 0) ? -y : y;
391 if (PG_DIV == type) {
392 return (((x ^ y) & (1ll<<63)) == 0) ? res : -(s64)res;
395 return ((x & (1ll<<63)) == 0) ? a : -(s64)a;
399 /* End of hacks to deal with 64-bit math on x86 */
401 /** Convert to milliseconds */
402 static inline __u64 tv_to_ms(const struct timeval* tv)
404 __u64 ms = tv->tv_usec / 1000;
405 ms += (__u64)tv->tv_sec * (__u64)1000;
410 /** Convert to micro-seconds */
411 static inline __u64 tv_to_us(const struct timeval* tv)
413 __u64 us = tv->tv_usec;
414 us += (__u64)tv->tv_sec * (__u64)1000000;
418 static inline __u64 pg_div(__u64 n, __u32 base) {
421 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
426 static inline __u64 pg_div64(__u64 n, __u64 base)
430 * How do we know if the architecture we are running on
431 * supports division with 64 bit base?
434 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
438 tmp = divremdi3(n, base, PG_DIV);
443 static inline u32 pktgen_random(void)
447 get_random_bytes(&n, 4);
454 static inline __u64 getCurMs(void)
457 do_gettimeofday(&tv);
458 return tv_to_ms(&tv);
461 static inline __u64 getCurUs(void)
464 do_gettimeofday(&tv);
465 return tv_to_us(&tv);
468 static inline __u64 tv_diff(const struct timeval* a, const struct timeval* b)
470 return tv_to_us(a) - tv_to_us(b);
474 /* old include end */
476 static char version[] __initdata = VERSION;
478 static int pktgen_remove_device(struct pktgen_thread* t, struct pktgen_dev *i);
479 static int pktgen_add_device(struct pktgen_thread* t, const char* ifname);
480 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread* t, const char* ifname);
481 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
482 static void pktgen_run_all_threads(void);
483 static void pktgen_stop_all_threads_ifs(void);
484 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
485 static void pktgen_stop(struct pktgen_thread* t);
486 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
487 static int pktgen_mark_device(const char* ifname);
488 static unsigned int scan_ip6(const char *s,char ip[16]);
489 static unsigned int fmt_ip6(char *s,const char ip[16]);
491 /* Module parameters, defaults. */
492 static int pg_count_d = 1000; /* 1000 pkts by default */
493 static int pg_delay_d;
494 static int pg_clone_skb_d;
497 static DECLARE_MUTEX(pktgen_sem);
498 static struct pktgen_thread *pktgen_threads = NULL;
500 static struct notifier_block pktgen_notifier_block = {
501 .notifier_call = pktgen_device_event,
505 * /proc handling functions
509 static int pgctrl_show(struct seq_file *seq, void *v)
511 seq_puts(seq, VERSION);
515 static ssize_t pgctrl_write(struct file* file,const char __user * buf,
516 size_t count, loff_t *ppos)
521 if (!capable(CAP_NET_ADMIN)){
526 if (count > sizeof(data))
527 count = sizeof(data);
529 if (copy_from_user(data, buf, count)) {
533 data[count-1] = 0; /* Make string */
535 if (!strcmp(data, "stop"))
536 pktgen_stop_all_threads_ifs();
538 else if (!strcmp(data, "start"))
539 pktgen_run_all_threads();
542 printk("pktgen: Unknown command: %s\n", data);
550 static int pgctrl_open(struct inode *inode, struct file *file)
552 return single_open(file, pgctrl_show, PDE(inode)->data);
555 static struct file_operations pktgen_fops = {
556 .owner = THIS_MODULE,
560 .write = pgctrl_write,
561 .release = single_release,
564 static int pktgen_if_show(struct seq_file *seq, void *v)
567 struct pktgen_dev *pkt_dev = seq->private;
570 __u64 now = getCurUs();
572 seq_printf(seq, "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
573 (unsigned long long) pkt_dev->count,
574 pkt_dev->min_pkt_size, pkt_dev->max_pkt_size);
576 seq_printf(seq, " frags: %d delay: %u clone_skb: %d ifname: %s\n",
577 pkt_dev->nfrags, 1000*pkt_dev->delay_us+pkt_dev->delay_ns, pkt_dev->clone_skb, pkt_dev->ifname);
579 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows, pkt_dev->lflow);
582 if(pkt_dev->flags & F_IPV6) {
583 char b1[128], b2[128], b3[128];
584 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
585 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
586 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
587 seq_printf(seq, " saddr: %s min_saddr: %s max_saddr: %s\n", b1, b2, b3);
589 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
590 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
591 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
592 seq_printf(seq, " daddr: %s min_daddr: %s max_daddr: %s\n", b1, b2, b3);
596 seq_printf(seq," dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
597 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min, pkt_dev->src_max);
599 seq_puts(seq, " src_mac: ");
601 if (is_zero_ether_addr(pkt_dev->src_mac))
602 for (i = 0; i < 6; i++)
603 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i], i == 5 ? " " : ":");
605 for (i = 0; i < 6; i++)
606 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i], i == 5 ? " " : ":");
608 seq_printf(seq, "dst_mac: ");
609 for (i = 0; i < 6; i++)
610 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i], i == 5 ? "\n" : ":");
612 seq_printf(seq, " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
613 pkt_dev->udp_src_min, pkt_dev->udp_src_max, pkt_dev->udp_dst_min,
614 pkt_dev->udp_dst_max);
616 seq_printf(seq, " src_mac_count: %d dst_mac_count: %d \n Flags: ",
617 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
620 if (pkt_dev->flags & F_IPV6)
621 seq_printf(seq, "IPV6 ");
623 if (pkt_dev->flags & F_IPSRC_RND)
624 seq_printf(seq, "IPSRC_RND ");
626 if (pkt_dev->flags & F_IPDST_RND)
627 seq_printf(seq, "IPDST_RND ");
629 if (pkt_dev->flags & F_TXSIZE_RND)
630 seq_printf(seq, "TXSIZE_RND ");
632 if (pkt_dev->flags & F_UDPSRC_RND)
633 seq_printf(seq, "UDPSRC_RND ");
635 if (pkt_dev->flags & F_UDPDST_RND)
636 seq_printf(seq, "UDPDST_RND ");
638 if (pkt_dev->flags & F_MACSRC_RND)
639 seq_printf(seq, "MACSRC_RND ");
641 if (pkt_dev->flags & F_MACDST_RND)
642 seq_printf(seq, "MACDST_RND ");
647 sa = pkt_dev->started_at;
648 stopped = pkt_dev->stopped_at;
649 if (pkt_dev->running)
650 stopped = now; /* not really stopped, more like last-running-at */
652 seq_printf(seq, "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
653 (unsigned long long) pkt_dev->sofar,
654 (unsigned long long) pkt_dev->errors,
655 (unsigned long long) sa,
656 (unsigned long long) stopped,
657 (unsigned long long) pkt_dev->idle_acc);
659 seq_printf(seq, " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
660 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
661 pkt_dev->cur_src_mac_offset);
663 if(pkt_dev->flags & F_IPV6) {
664 char b1[128], b2[128];
665 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
666 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
667 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
670 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
671 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
674 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
675 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
677 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
679 if (pkt_dev->result[0])
680 seq_printf(seq, "Result: %s\n", pkt_dev->result);
682 seq_printf(seq, "Result: Idle\n");
688 static int count_trail_chars(const char __user *user_buffer, unsigned int maxlen)
692 for (i = 0; i < maxlen; i++) {
694 if (get_user(c, &user_buffer[i]))
712 static unsigned long num_arg(const char __user *user_buffer, unsigned long maxlen,
718 for(; i < maxlen; i++) {
720 if (get_user(c, &user_buffer[i]))
722 if ((c >= '0') && (c <= '9')) {
731 static int strn_len(const char __user *user_buffer, unsigned int maxlen)
735 for(; i < maxlen; i++) {
737 if (get_user(c, &user_buffer[i]))
756 static ssize_t pktgen_if_write(struct file *file, const char __user *user_buffer,
757 size_t count, loff_t *offset)
759 struct seq_file *seq = (struct seq_file *) file->private_data;
760 struct pktgen_dev *pkt_dev = seq->private;
762 char name[16], valstr[32];
763 unsigned long value = 0;
764 char* pg_result = NULL;
768 pg_result = &(pkt_dev->result[0]);
771 printk("pktgen: wrong command format\n");
776 tmp = count_trail_chars(&user_buffer[i], max);
778 printk("pktgen: illegal format\n");
783 /* Read variable name */
785 len = strn_len(&user_buffer[i], sizeof(name) - 1);
786 if (len < 0) { return len; }
787 memset(name, 0, sizeof(name));
788 if (copy_from_user(name, &user_buffer[i], len) )
793 len = count_trail_chars(&user_buffer[i], max);
801 if (copy_from_user(tb, user_buffer, count))
804 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
805 (unsigned long) count, tb);
808 if (!strcmp(name, "min_pkt_size")) {
809 len = num_arg(&user_buffer[i], 10, &value);
810 if (len < 0) { return len; }
814 if (value != pkt_dev->min_pkt_size) {
815 pkt_dev->min_pkt_size = value;
816 pkt_dev->cur_pkt_size = value;
818 sprintf(pg_result, "OK: min_pkt_size=%u", pkt_dev->min_pkt_size);
822 if (!strcmp(name, "max_pkt_size")) {
823 len = num_arg(&user_buffer[i], 10, &value);
824 if (len < 0) { return len; }
828 if (value != pkt_dev->max_pkt_size) {
829 pkt_dev->max_pkt_size = value;
830 pkt_dev->cur_pkt_size = value;
832 sprintf(pg_result, "OK: max_pkt_size=%u", pkt_dev->max_pkt_size);
836 /* Shortcut for min = max */
838 if (!strcmp(name, "pkt_size")) {
839 len = num_arg(&user_buffer[i], 10, &value);
840 if (len < 0) { return len; }
844 if (value != pkt_dev->min_pkt_size) {
845 pkt_dev->min_pkt_size = value;
846 pkt_dev->max_pkt_size = value;
847 pkt_dev->cur_pkt_size = value;
849 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
853 if (!strcmp(name, "debug")) {
854 len = num_arg(&user_buffer[i], 10, &value);
855 if (len < 0) { return len; }
858 sprintf(pg_result, "OK: debug=%u", debug);
862 if (!strcmp(name, "frags")) {
863 len = num_arg(&user_buffer[i], 10, &value);
864 if (len < 0) { return len; }
866 pkt_dev->nfrags = value;
867 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
870 if (!strcmp(name, "delay")) {
871 len = num_arg(&user_buffer[i], 10, &value);
872 if (len < 0) { return len; }
874 if (value == 0x7FFFFFFF) {
875 pkt_dev->delay_us = 0x7FFFFFFF;
876 pkt_dev->delay_ns = 0;
878 pkt_dev->delay_us = value / 1000;
879 pkt_dev->delay_ns = value % 1000;
881 sprintf(pg_result, "OK: delay=%u", 1000*pkt_dev->delay_us+pkt_dev->delay_ns);
884 if (!strcmp(name, "udp_src_min")) {
885 len = num_arg(&user_buffer[i], 10, &value);
886 if (len < 0) { return len; }
888 if (value != pkt_dev->udp_src_min) {
889 pkt_dev->udp_src_min = value;
890 pkt_dev->cur_udp_src = value;
892 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
895 if (!strcmp(name, "udp_dst_min")) {
896 len = num_arg(&user_buffer[i], 10, &value);
897 if (len < 0) { return len; }
899 if (value != pkt_dev->udp_dst_min) {
900 pkt_dev->udp_dst_min = value;
901 pkt_dev->cur_udp_dst = value;
903 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
906 if (!strcmp(name, "udp_src_max")) {
907 len = num_arg(&user_buffer[i], 10, &value);
908 if (len < 0) { return len; }
910 if (value != pkt_dev->udp_src_max) {
911 pkt_dev->udp_src_max = value;
912 pkt_dev->cur_udp_src = value;
914 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
917 if (!strcmp(name, "udp_dst_max")) {
918 len = num_arg(&user_buffer[i], 10, &value);
919 if (len < 0) { return len; }
921 if (value != pkt_dev->udp_dst_max) {
922 pkt_dev->udp_dst_max = value;
923 pkt_dev->cur_udp_dst = value;
925 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
928 if (!strcmp(name, "clone_skb")) {
929 len = num_arg(&user_buffer[i], 10, &value);
930 if (len < 0) { return len; }
932 pkt_dev->clone_skb = value;
934 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
937 if (!strcmp(name, "count")) {
938 len = num_arg(&user_buffer[i], 10, &value);
939 if (len < 0) { return len; }
941 pkt_dev->count = value;
942 sprintf(pg_result, "OK: count=%llu",
943 (unsigned long long) pkt_dev->count);
946 if (!strcmp(name, "src_mac_count")) {
947 len = num_arg(&user_buffer[i], 10, &value);
948 if (len < 0) { return len; }
950 if (pkt_dev->src_mac_count != value) {
951 pkt_dev->src_mac_count = value;
952 pkt_dev->cur_src_mac_offset = 0;
954 sprintf(pg_result, "OK: src_mac_count=%d", pkt_dev->src_mac_count);
957 if (!strcmp(name, "dst_mac_count")) {
958 len = num_arg(&user_buffer[i], 10, &value);
959 if (len < 0) { return len; }
961 if (pkt_dev->dst_mac_count != value) {
962 pkt_dev->dst_mac_count = value;
963 pkt_dev->cur_dst_mac_offset = 0;
965 sprintf(pg_result, "OK: dst_mac_count=%d", pkt_dev->dst_mac_count);
968 if (!strcmp(name, "flag")) {
971 len = strn_len(&user_buffer[i], sizeof(f) - 1);
972 if (len < 0) { return len; }
973 if (copy_from_user(f, &user_buffer[i], len))
976 if (strcmp(f, "IPSRC_RND") == 0)
977 pkt_dev->flags |= F_IPSRC_RND;
979 else if (strcmp(f, "!IPSRC_RND") == 0)
980 pkt_dev->flags &= ~F_IPSRC_RND;
982 else if (strcmp(f, "TXSIZE_RND") == 0)
983 pkt_dev->flags |= F_TXSIZE_RND;
985 else if (strcmp(f, "!TXSIZE_RND") == 0)
986 pkt_dev->flags &= ~F_TXSIZE_RND;
988 else if (strcmp(f, "IPDST_RND") == 0)
989 pkt_dev->flags |= F_IPDST_RND;
991 else if (strcmp(f, "!IPDST_RND") == 0)
992 pkt_dev->flags &= ~F_IPDST_RND;
994 else if (strcmp(f, "UDPSRC_RND") == 0)
995 pkt_dev->flags |= F_UDPSRC_RND;
997 else if (strcmp(f, "!UDPSRC_RND") == 0)
998 pkt_dev->flags &= ~F_UDPSRC_RND;
1000 else if (strcmp(f, "UDPDST_RND") == 0)
1001 pkt_dev->flags |= F_UDPDST_RND;
1003 else if (strcmp(f, "!UDPDST_RND") == 0)
1004 pkt_dev->flags &= ~F_UDPDST_RND;
1006 else if (strcmp(f, "MACSRC_RND") == 0)
1007 pkt_dev->flags |= F_MACSRC_RND;
1009 else if (strcmp(f, "!MACSRC_RND") == 0)
1010 pkt_dev->flags &= ~F_MACSRC_RND;
1012 else if (strcmp(f, "MACDST_RND") == 0)
1013 pkt_dev->flags |= F_MACDST_RND;
1015 else if (strcmp(f, "!MACDST_RND") == 0)
1016 pkt_dev->flags &= ~F_MACDST_RND;
1019 sprintf(pg_result, "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1021 "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n");
1024 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1027 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1028 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1029 if (len < 0) { return len; }
1031 if (copy_from_user(buf, &user_buffer[i], len))
1034 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1035 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1036 strncpy(pkt_dev->dst_min, buf, len);
1037 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1038 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1041 printk("pktgen: dst_min set to: %s\n", pkt_dev->dst_min);
1043 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1046 if (!strcmp(name, "dst_max")) {
1047 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1048 if (len < 0) { return len; }
1050 if (copy_from_user(buf, &user_buffer[i], len))
1054 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1055 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1056 strncpy(pkt_dev->dst_max, buf, len);
1057 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1058 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1061 printk("pktgen: dst_max set to: %s\n", pkt_dev->dst_max);
1063 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1066 if (!strcmp(name, "dst6")) {
1067 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1068 if (len < 0) return len;
1070 pkt_dev->flags |= F_IPV6;
1072 if (copy_from_user(buf, &user_buffer[i], len))
1076 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1077 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1079 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1082 printk("pktgen: dst6 set to: %s\n", buf);
1085 sprintf(pg_result, "OK: dst6=%s", buf);
1088 if (!strcmp(name, "dst6_min")) {
1089 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1090 if (len < 0) return len;
1092 pkt_dev->flags |= F_IPV6;
1094 if (copy_from_user(buf, &user_buffer[i], len))
1098 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1099 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1101 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->min_in6_daddr);
1103 printk("pktgen: dst6_min set to: %s\n", buf);
1106 sprintf(pg_result, "OK: dst6_min=%s", buf);
1109 if (!strcmp(name, "dst6_max")) {
1110 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1111 if (len < 0) return len;
1113 pkt_dev->flags |= F_IPV6;
1115 if (copy_from_user(buf, &user_buffer[i], len))
1119 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1120 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1123 printk("pktgen: dst6_max set to: %s\n", buf);
1126 sprintf(pg_result, "OK: dst6_max=%s", buf);
1129 if (!strcmp(name, "src6")) {
1130 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1131 if (len < 0) return len;
1133 pkt_dev->flags |= F_IPV6;
1135 if (copy_from_user(buf, &user_buffer[i], len))
1139 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1140 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1142 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1145 printk("pktgen: src6 set to: %s\n", buf);
1148 sprintf(pg_result, "OK: src6=%s", buf);
1151 if (!strcmp(name, "src_min")) {
1152 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1153 if (len < 0) { return len; }
1154 if (copy_from_user(buf, &user_buffer[i], len))
1157 if (strcmp(buf, pkt_dev->src_min) != 0) {
1158 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1159 strncpy(pkt_dev->src_min, buf, len);
1160 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1161 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1164 printk("pktgen: src_min set to: %s\n", pkt_dev->src_min);
1166 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1169 if (!strcmp(name, "src_max")) {
1170 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1171 if (len < 0) { return len; }
1172 if (copy_from_user(buf, &user_buffer[i], len))
1175 if (strcmp(buf, pkt_dev->src_max) != 0) {
1176 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1177 strncpy(pkt_dev->src_max, buf, len);
1178 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1179 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1182 printk("pktgen: src_max set to: %s\n", pkt_dev->src_max);
1184 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1187 if (!strcmp(name, "dst_mac")) {
1189 unsigned char old_dmac[ETH_ALEN];
1190 unsigned char *m = pkt_dev->dst_mac;
1191 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1193 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1194 if (len < 0) { return len; }
1195 memset(valstr, 0, sizeof(valstr));
1196 if( copy_from_user(valstr, &user_buffer[i], len))
1200 for(*m = 0;*v && m < pkt_dev->dst_mac + 6; v++) {
1201 if (*v >= '0' && *v <= '9') {
1205 if (*v >= 'A' && *v <= 'F') {
1207 *m += *v - 'A' + 10;
1209 if (*v >= 'a' && *v <= 'f') {
1211 *m += *v - 'a' + 10;
1219 /* Set up Dest MAC */
1220 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1221 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1223 sprintf(pg_result, "OK: dstmac");
1226 if (!strcmp(name, "src_mac")) {
1228 unsigned char *m = pkt_dev->src_mac;
1230 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1231 if (len < 0) { return len; }
1232 memset(valstr, 0, sizeof(valstr));
1233 if( copy_from_user(valstr, &user_buffer[i], len))
1237 for(*m = 0;*v && m < pkt_dev->src_mac + 6; v++) {
1238 if (*v >= '0' && *v <= '9') {
1242 if (*v >= 'A' && *v <= 'F') {
1244 *m += *v - 'A' + 10;
1246 if (*v >= 'a' && *v <= 'f') {
1248 *m += *v - 'a' + 10;
1256 sprintf(pg_result, "OK: srcmac");
1260 if (!strcmp(name, "clear_counters")) {
1261 pktgen_clear_counters(pkt_dev);
1262 sprintf(pg_result, "OK: Clearing counters.\n");
1266 if (!strcmp(name, "flows")) {
1267 len = num_arg(&user_buffer[i], 10, &value);
1268 if (len < 0) { return len; }
1270 if (value > MAX_CFLOWS)
1273 pkt_dev->cflows = value;
1274 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1278 if (!strcmp(name, "flowlen")) {
1279 len = num_arg(&user_buffer[i], 10, &value);
1280 if (len < 0) { return len; }
1282 pkt_dev->lflow = value;
1283 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1287 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1291 static int pktgen_if_open(struct inode *inode, struct file *file)
1293 return single_open(file, pktgen_if_show, PDE(inode)->data);
1296 static struct file_operations pktgen_if_fops = {
1297 .owner = THIS_MODULE,
1298 .open = pktgen_if_open,
1300 .llseek = seq_lseek,
1301 .write = pktgen_if_write,
1302 .release = single_release,
1305 static int pktgen_thread_show(struct seq_file *seq, void *v)
1307 struct pktgen_thread *t = seq->private;
1308 struct pktgen_dev *pkt_dev = NULL;
1312 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1313 t->name, t->max_before_softirq);
1315 seq_printf(seq, "Running: ");
1318 for(pkt_dev = t->if_list;pkt_dev; pkt_dev = pkt_dev->next)
1319 if(pkt_dev->running)
1320 seq_printf(seq, "%s ", pkt_dev->ifname);
1322 seq_printf(seq, "\nStopped: ");
1324 for(pkt_dev = t->if_list;pkt_dev; pkt_dev = pkt_dev->next)
1325 if(!pkt_dev->running)
1326 seq_printf(seq, "%s ", pkt_dev->ifname);
1329 seq_printf(seq, "\nResult: %s\n", t->result);
1331 seq_printf(seq, "\nResult: NA\n");
1338 static ssize_t pktgen_thread_write(struct file *file,
1339 const char __user *user_buffer,
1340 size_t count, loff_t *offset)
1342 struct seq_file *seq = (struct seq_file *) file->private_data;
1343 struct pktgen_thread *t = seq->private;
1344 int i = 0, max, len, ret;
1347 unsigned long value = 0;
1350 // sprintf(pg_result, "Wrong command format");
1355 len = count_trail_chars(&user_buffer[i], max);
1361 /* Read variable name */
1363 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1367 memset(name, 0, sizeof(name));
1368 if (copy_from_user(name, &user_buffer[i], len))
1373 len = count_trail_chars(&user_buffer[i], max);
1380 printk("pktgen: t=%s, count=%lu\n", name,
1381 (unsigned long) count);
1384 printk("pktgen: ERROR: No thread\n");
1389 pg_result = &(t->result[0]);
1391 if (!strcmp(name, "add_device")) {
1394 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1399 if( copy_from_user(f, &user_buffer[i], len) )
1403 pktgen_add_device(t, f);
1406 sprintf(pg_result, "OK: add_device=%s", f);
1410 if (!strcmp(name, "rem_device_all")) {
1412 t->control |= T_REMDEVALL;
1414 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1416 sprintf(pg_result, "OK: rem_device_all");
1420 if (!strcmp(name, "max_before_softirq")) {
1421 len = num_arg(&user_buffer[i], 10, &value);
1423 t->max_before_softirq = value;
1426 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1436 static int pktgen_thread_open(struct inode *inode, struct file *file)
1438 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1441 static struct file_operations pktgen_thread_fops = {
1442 .owner = THIS_MODULE,
1443 .open = pktgen_thread_open,
1445 .llseek = seq_lseek,
1446 .write = pktgen_thread_write,
1447 .release = single_release,
1450 /* Think find or remove for NN */
1451 static struct pktgen_dev *__pktgen_NN_threads(const char* ifname, int remove)
1453 struct pktgen_thread *t;
1454 struct pktgen_dev *pkt_dev = NULL;
1459 pkt_dev = pktgen_find_dev(t, ifname);
1463 pkt_dev->removal_mark = 1;
1464 t->control |= T_REMDEV;
1475 * mark a device for removal
1477 static int pktgen_mark_device(const char* ifname)
1479 struct pktgen_dev *pkt_dev = NULL;
1480 const int max_tries = 10, msec_per_try = 125;
1485 PG_DEBUG(printk("pktgen: pktgen_mark_device marking %s for removal\n",
1490 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1491 if (pkt_dev == NULL) break; /* success */
1494 PG_DEBUG(printk("pktgen: pktgen_mark_device waiting for %s "
1495 "to disappear....\n", ifname));
1496 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1499 if (++i >= max_tries) {
1500 printk("pktgen_mark_device: timed out after waiting "
1501 "%d msec for device %s to be removed\n",
1502 msec_per_try*i, ifname);
1514 static int pktgen_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
1516 struct net_device *dev = (struct net_device *)(ptr);
1518 /* It is OK that we do not hold the group lock right now,
1519 * as we run under the RTNL lock.
1523 case NETDEV_CHANGEADDR:
1524 case NETDEV_GOING_DOWN:
1527 /* Ignore for now */
1530 case NETDEV_UNREGISTER:
1531 pktgen_mark_device(dev->name);
1538 /* Associate pktgen_dev with a device. */
1540 static struct net_device* pktgen_setup_dev(struct pktgen_dev *pkt_dev) {
1541 struct net_device *odev;
1543 /* Clean old setups */
1545 if (pkt_dev->odev) {
1546 dev_put(pkt_dev->odev);
1547 pkt_dev->odev = NULL;
1550 odev = dev_get_by_name(pkt_dev->ifname);
1553 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1556 if (odev->type != ARPHRD_ETHER) {
1557 printk("pktgen: not an ethernet device: \"%s\"\n", pkt_dev->ifname);
1560 if (!netif_running(odev)) {
1561 printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1564 pkt_dev->odev = odev;
1566 return pkt_dev->odev;
1575 /* Read pkt_dev from the interface and set up internal pktgen_dev
1576 * structure to have the right information to create/send packets
1578 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1580 /* Try once more, just in case it works now. */
1582 pktgen_setup_dev(pkt_dev);
1584 if (!pkt_dev->odev) {
1585 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1586 sprintf(pkt_dev->result, "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1590 /* Default to the interface's mac if not explicitly set. */
1592 if (is_zero_ether_addr(pkt_dev->src_mac))
1593 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1595 /* Set up Dest MAC */
1596 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1598 /* Set up pkt size */
1599 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1601 if(pkt_dev->flags & F_IPV6) {
1603 * Skip this automatic address setting until locks or functions
1608 int i, set = 0, err=1;
1609 struct inet6_dev *idev;
1611 for(i=0; i< IN6_ADDR_HSIZE; i++)
1612 if(pkt_dev->cur_in6_saddr.s6_addr[i]) {
1620 * Use linklevel address if unconfigured.
1622 * use ipv6_get_lladdr if/when it's get exported
1626 read_lock(&addrconf_lock);
1627 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1628 struct inet6_ifaddr *ifp;
1630 read_lock_bh(&idev->lock);
1631 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1632 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1633 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &ifp->addr);
1638 read_unlock_bh(&idev->lock);
1640 read_unlock(&addrconf_lock);
1641 if(err) printk("pktgen: ERROR: IPv6 link address not availble.\n");
1646 pkt_dev->saddr_min = 0;
1647 pkt_dev->saddr_max = 0;
1648 if (strlen(pkt_dev->src_min) == 0) {
1650 struct in_device *in_dev;
1653 in_dev = __in_dev_get_rcu(pkt_dev->odev);
1655 if (in_dev->ifa_list) {
1656 pkt_dev->saddr_min = in_dev->ifa_list->ifa_address;
1657 pkt_dev->saddr_max = pkt_dev->saddr_min;
1663 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1664 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1667 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1668 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1670 /* Initialize current values. */
1671 pkt_dev->cur_dst_mac_offset = 0;
1672 pkt_dev->cur_src_mac_offset = 0;
1673 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1674 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1675 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
1676 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
1677 pkt_dev->nflows = 0;
1680 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
1685 start = now = getCurUs();
1686 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
1687 while (now < spin_until_us) {
1688 /* TODO: optimize sleeping behavior */
1689 if (spin_until_us - now > jiffies_to_usecs(1)+1)
1690 schedule_timeout_interruptible(1);
1691 else if (spin_until_us - now > 100) {
1693 if (!pkt_dev->running)
1702 pkt_dev->idle_acc += now - start;
1706 /* Increment/randomize headers according to flags and current values
1707 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
1709 static void mod_cur_headers(struct pktgen_dev *pkt_dev) {
1714 if(pkt_dev->cflows) {
1715 flow = pktgen_random() % pkt_dev->cflows;
1717 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
1718 pkt_dev->flows[flow].count = 0;
1722 /* Deal with source MAC */
1723 if (pkt_dev->src_mac_count > 1) {
1727 if (pkt_dev->flags & F_MACSRC_RND)
1728 mc = pktgen_random() % (pkt_dev->src_mac_count);
1730 mc = pkt_dev->cur_src_mac_offset++;
1731 if (pkt_dev->cur_src_mac_offset > pkt_dev->src_mac_count)
1732 pkt_dev->cur_src_mac_offset = 0;
1735 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
1736 pkt_dev->hh[11] = tmp;
1737 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1738 pkt_dev->hh[10] = tmp;
1739 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1740 pkt_dev->hh[9] = tmp;
1741 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1742 pkt_dev->hh[8] = tmp;
1743 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
1744 pkt_dev->hh[7] = tmp;
1747 /* Deal with Destination MAC */
1748 if (pkt_dev->dst_mac_count > 1) {
1752 if (pkt_dev->flags & F_MACDST_RND)
1753 mc = pktgen_random() % (pkt_dev->dst_mac_count);
1756 mc = pkt_dev->cur_dst_mac_offset++;
1757 if (pkt_dev->cur_dst_mac_offset > pkt_dev->dst_mac_count) {
1758 pkt_dev->cur_dst_mac_offset = 0;
1762 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
1763 pkt_dev->hh[5] = tmp;
1764 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1765 pkt_dev->hh[4] = tmp;
1766 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1767 pkt_dev->hh[3] = tmp;
1768 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1769 pkt_dev->hh[2] = tmp;
1770 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
1771 pkt_dev->hh[1] = tmp;
1774 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
1775 if (pkt_dev->flags & F_UDPSRC_RND)
1776 pkt_dev->cur_udp_src = ((pktgen_random() % (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) + pkt_dev->udp_src_min);
1779 pkt_dev->cur_udp_src++;
1780 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
1781 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
1785 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
1786 if (pkt_dev->flags & F_UDPDST_RND) {
1787 pkt_dev->cur_udp_dst = ((pktgen_random() % (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) + pkt_dev->udp_dst_min);
1790 pkt_dev->cur_udp_dst++;
1791 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
1792 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
1796 if (!(pkt_dev->flags & F_IPV6)) {
1798 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx = ntohl(pkt_dev->saddr_max))) {
1800 if (pkt_dev->flags & F_IPSRC_RND)
1801 t = ((pktgen_random() % (imx - imn)) + imn);
1803 t = ntohl(pkt_dev->cur_saddr);
1809 pkt_dev->cur_saddr = htonl(t);
1812 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
1813 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
1816 if ((imn = ntohl(pkt_dev->daddr_min)) < (imx = ntohl(pkt_dev->daddr_max))) {
1818 if (pkt_dev->flags & F_IPDST_RND) {
1820 t = ((pktgen_random() % (imx - imn)) + imn);
1823 while( LOOPBACK(t) || MULTICAST(t) || BADCLASS(t) || ZERONET(t) || LOCAL_MCAST(t) ) {
1824 t = ((pktgen_random() % (imx - imn)) + imn);
1827 pkt_dev->cur_daddr = t;
1831 t = ntohl(pkt_dev->cur_daddr);
1836 pkt_dev->cur_daddr = htonl(t);
1839 if(pkt_dev->cflows) {
1840 pkt_dev->flows[flow].cur_daddr = pkt_dev->cur_daddr;
1847 if(pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
1848 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
1849 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
1850 pkt_dev->min_in6_daddr.s6_addr32[3] == 0);
1854 /* Only random destinations yet */
1856 for(i=0; i < 4; i++) {
1857 pkt_dev->cur_in6_daddr.s6_addr32[i] =
1859 pkt_dev->min_in6_daddr.s6_addr32[i]) &
1860 pkt_dev->max_in6_daddr.s6_addr32[i]);
1865 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
1867 if (pkt_dev->flags & F_TXSIZE_RND) {
1868 t = ((pktgen_random() % (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size))
1869 + pkt_dev->min_pkt_size);
1872 t = pkt_dev->cur_pkt_size + 1;
1873 if (t > pkt_dev->max_pkt_size)
1874 t = pkt_dev->min_pkt_size;
1876 pkt_dev->cur_pkt_size = t;
1879 pkt_dev->flows[flow].count++;
1883 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
1884 struct pktgen_dev *pkt_dev)
1886 struct sk_buff *skb = NULL;
1888 struct udphdr *udph;
1891 struct pktgen_hdr *pgh = NULL;
1893 /* Update any of the values, used when we're incrementing various
1896 mod_cur_headers(pkt_dev);
1898 datalen = (odev->hard_header_len + 16) & ~0xf;
1899 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen, GFP_ATOMIC);
1901 sprintf(pkt_dev->result, "No memory");
1905 skb_reserve(skb, datalen);
1907 /* Reserve for ethernet and IP header */
1908 eth = (__u8 *) skb_push(skb, 14);
1909 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
1910 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
1912 memcpy(eth, pkt_dev->hh, 12);
1913 *(u16*)ð[12] = __constant_htons(ETH_P_IP);
1915 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8; /* Eth + IPh + UDPh */
1916 if (datalen < sizeof(struct pktgen_hdr))
1917 datalen = sizeof(struct pktgen_hdr);
1919 udph->source = htons(pkt_dev->cur_udp_src);
1920 udph->dest = htons(pkt_dev->cur_udp_dst);
1921 udph->len = htons(datalen + 8); /* DATA + udphdr */
1922 udph->check = 0; /* No checksum */
1928 iph->protocol = IPPROTO_UDP; /* UDP */
1929 iph->saddr = pkt_dev->cur_saddr;
1930 iph->daddr = pkt_dev->cur_daddr;
1932 iplen = 20 + 8 + datalen;
1933 iph->tot_len = htons(iplen);
1935 iph->check = ip_fast_csum((void *) iph, iph->ihl);
1936 skb->protocol = __constant_htons(ETH_P_IP);
1937 skb->mac.raw = ((u8 *)iph) - 14;
1939 skb->pkt_type = PACKET_HOST;
1941 if (pkt_dev->nfrags <= 0)
1942 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
1944 int frags = pkt_dev->nfrags;
1947 pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8);
1949 if (frags > MAX_SKB_FRAGS)
1950 frags = MAX_SKB_FRAGS;
1951 if (datalen > frags*PAGE_SIZE) {
1952 skb_put(skb, datalen-frags*PAGE_SIZE);
1953 datalen = frags*PAGE_SIZE;
1957 while (datalen > 0) {
1958 struct page *page = alloc_pages(GFP_KERNEL, 0);
1959 skb_shinfo(skb)->frags[i].page = page;
1960 skb_shinfo(skb)->frags[i].page_offset = 0;
1961 skb_shinfo(skb)->frags[i].size =
1962 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
1963 datalen -= skb_shinfo(skb)->frags[i].size;
1964 skb->len += skb_shinfo(skb)->frags[i].size;
1965 skb->data_len += skb_shinfo(skb)->frags[i].size;
1967 skb_shinfo(skb)->nr_frags = i;
1976 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
1980 skb_shinfo(skb)->frags[i - 1].size -= rem;
1982 skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1];
1983 get_page(skb_shinfo(skb)->frags[i].page);
1984 skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page;
1985 skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size;
1986 skb_shinfo(skb)->frags[i].size = rem;
1988 skb_shinfo(skb)->nr_frags = i;
1992 /* Stamp the time, and sequence number, convert them to network byte order */
1995 struct timeval timestamp;
1997 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
1998 pgh->seq_num = htonl(pkt_dev->seq_num);
2000 do_gettimeofday(×tamp);
2001 pgh->tv_sec = htonl(timestamp.tv_sec);
2002 pgh->tv_usec = htonl(timestamp.tv_usec);
2010 * scan_ip6, fmt_ip taken from dietlibc-0.21
2011 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2013 * Slightly modified for kernel.
2014 * Should be candidate for net/ipv4/utils.c
2018 static unsigned int scan_ip6(const char *s,char ip[16])
2024 unsigned int prefixlen=0;
2025 unsigned int suffixlen=0;
2028 for (i=0; i<16; i++) ip[i]=0;
2033 if (s[1] == ':') { /* Found "::", skip to part 2 */
2042 u=simple_strtoul(s,&tmp,16);
2047 if (prefixlen==12 && s[i]=='.') {
2049 /* the last 4 bytes may be written as IPv4 address */
2052 memcpy((struct in_addr*)(ip+12), &tmp, sizeof(tmp));
2055 ip[prefixlen++] = (u >> 8);
2056 ip[prefixlen++] = (u & 255);
2062 /* part 2, after "::" */
2069 } else if (suffixlen!=0)
2073 u=simple_strtol(s,&tmp,16);
2080 if (suffixlen+prefixlen<=12 && s[i]=='.') {
2082 memcpy((struct in_addr*)(suffix+suffixlen), &tmp, sizeof(tmp));
2087 suffix[suffixlen++] = (u >> 8);
2088 suffix[suffixlen++] = (u & 255);
2090 if (prefixlen+suffixlen==16)
2093 for (i=0; i<suffixlen; i++)
2094 ip[16-suffixlen+i] = suffix[i];
2098 static char tohex(char hexdigit) {
2099 return hexdigit>9?hexdigit+'a'-10:hexdigit+'0';
2102 static int fmt_xlong(char* s,unsigned int i) {
2104 *s=tohex((i>>12)&0xf); if (s!=bak || *s!='0') ++s;
2105 *s=tohex((i>>8)&0xf); if (s!=bak || *s!='0') ++s;
2106 *s=tohex((i>>4)&0xf); if (s!=bak || *s!='0') ++s;
2111 static unsigned int fmt_ip6(char *s,const char ip[16]) {
2115 unsigned int compressing;
2118 len = 0; compressing = 0;
2119 for (j=0; j<16; j+=2) {
2121 #ifdef V4MAPPEDPREFIX
2122 if (j==12 && !memcmp(ip,V4mappedprefix,12)) {
2123 inet_ntoa_r(*(struct in_addr*)(ip+12),s);
2128 temp = ((unsigned long) (unsigned char) ip[j] << 8) +
2129 (unsigned long) (unsigned char) ip[j+1];
2142 i = fmt_xlong(s,temp); len += i; s += i;
2156 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2157 struct pktgen_dev *pkt_dev)
2159 struct sk_buff *skb = NULL;
2161 struct udphdr *udph;
2163 struct ipv6hdr *iph;
2164 struct pktgen_hdr *pgh = NULL;
2166 /* Update any of the values, used when we're incrementing various
2169 mod_cur_headers(pkt_dev);
2171 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC);
2173 sprintf(pkt_dev->result, "No memory");
2177 skb_reserve(skb, 16);
2179 /* Reserve for ethernet and IP header */
2180 eth = (__u8 *) skb_push(skb, 14);
2181 iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
2182 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2184 memcpy(eth, pkt_dev->hh, 12);
2185 *(u16*)ð[12] = __constant_htons(ETH_P_IPV6);
2187 datalen = pkt_dev->cur_pkt_size-14-
2188 sizeof(struct ipv6hdr)-sizeof(struct udphdr); /* Eth + IPh + UDPh */
2190 if (datalen < sizeof(struct pktgen_hdr)) {
2191 datalen = sizeof(struct pktgen_hdr);
2192 if (net_ratelimit())
2193 printk(KERN_INFO "pktgen: increased datalen to %d\n", datalen);
2196 udph->source = htons(pkt_dev->cur_udp_src);
2197 udph->dest = htons(pkt_dev->cur_udp_dst);
2198 udph->len = htons(datalen + sizeof(struct udphdr));
2199 udph->check = 0; /* No checksum */
2201 *(u32*)iph = __constant_htonl(0x60000000); /* Version + flow */
2203 iph->hop_limit = 32;
2205 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2206 iph->nexthdr = IPPROTO_UDP;
2208 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2209 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2211 skb->mac.raw = ((u8 *)iph) - 14;
2212 skb->protocol = __constant_htons(ETH_P_IPV6);
2214 skb->pkt_type = PACKET_HOST;
2216 if (pkt_dev->nfrags <= 0)
2217 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2219 int frags = pkt_dev->nfrags;
2222 pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8);
2224 if (frags > MAX_SKB_FRAGS)
2225 frags = MAX_SKB_FRAGS;
2226 if (datalen > frags*PAGE_SIZE) {
2227 skb_put(skb, datalen-frags*PAGE_SIZE);
2228 datalen = frags*PAGE_SIZE;
2232 while (datalen > 0) {
2233 struct page *page = alloc_pages(GFP_KERNEL, 0);
2234 skb_shinfo(skb)->frags[i].page = page;
2235 skb_shinfo(skb)->frags[i].page_offset = 0;
2236 skb_shinfo(skb)->frags[i].size =
2237 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2238 datalen -= skb_shinfo(skb)->frags[i].size;
2239 skb->len += skb_shinfo(skb)->frags[i].size;
2240 skb->data_len += skb_shinfo(skb)->frags[i].size;
2242 skb_shinfo(skb)->nr_frags = i;
2251 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2255 skb_shinfo(skb)->frags[i - 1].size -= rem;
2257 skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1];
2258 get_page(skb_shinfo(skb)->frags[i].page);
2259 skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page;
2260 skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size;
2261 skb_shinfo(skb)->frags[i].size = rem;
2263 skb_shinfo(skb)->nr_frags = i;
2267 /* Stamp the time, and sequence number, convert them to network byte order */
2268 /* should we update cloned packets too ? */
2270 struct timeval timestamp;
2272 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2273 pgh->seq_num = htonl(pkt_dev->seq_num);
2275 do_gettimeofday(×tamp);
2276 pgh->tv_sec = htonl(timestamp.tv_sec);
2277 pgh->tv_usec = htonl(timestamp.tv_usec);
2284 static inline struct sk_buff *fill_packet(struct net_device *odev,
2285 struct pktgen_dev *pkt_dev)
2287 if(pkt_dev->flags & F_IPV6)
2288 return fill_packet_ipv6(odev, pkt_dev);
2290 return fill_packet_ipv4(odev, pkt_dev);
2293 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2295 pkt_dev->seq_num = 1;
2296 pkt_dev->idle_acc = 0;
2298 pkt_dev->tx_bytes = 0;
2299 pkt_dev->errors = 0;
2302 /* Set up structure for sending pkts, clear counters */
2304 static void pktgen_run(struct pktgen_thread *t)
2306 struct pktgen_dev *pkt_dev = NULL;
2309 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
2312 for (pkt_dev = t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) {
2315 * setup odev and create initial packet.
2317 pktgen_setup_inject(pkt_dev);
2320 pktgen_clear_counters(pkt_dev);
2321 pkt_dev->running = 1; /* Cranke yeself! */
2322 pkt_dev->skb = NULL;
2323 pkt_dev->started_at = getCurUs();
2324 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
2325 pkt_dev->next_tx_ns = 0;
2327 strcpy(pkt_dev->result, "Starting");
2331 strcpy(pkt_dev->result, "Error starting");
2334 if(started) t->control &= ~(T_STOP);
2337 static void pktgen_stop_all_threads_ifs(void)
2339 struct pktgen_thread *t = pktgen_threads;
2341 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads_ifs.\n"));
2345 t->control |= T_STOP;
2351 static int thread_is_running(struct pktgen_thread *t )
2353 struct pktgen_dev *next;
2356 for(next=t->if_list; next; next=next->next) {
2365 static int pktgen_wait_thread_run(struct pktgen_thread *t )
2369 while(thread_is_running(t)) {
2373 msleep_interruptible(100);
2375 if (signal_pending(current))
2385 static int pktgen_wait_all_threads_run(void)
2387 struct pktgen_thread *t = pktgen_threads;
2391 sig = pktgen_wait_thread_run(t);
2392 if( sig == 0 ) break;
2400 t->control |= (T_STOP);
2408 static void pktgen_run_all_threads(void)
2410 struct pktgen_thread *t = pktgen_threads;
2412 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2417 t->control |= (T_RUN);
2422 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2424 pktgen_wait_all_threads_run();
2428 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2430 __u64 total_us, bps, mbps, pps, idle;
2431 char *p = pkt_dev->result;
2433 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
2435 idle = pkt_dev->idle_acc;
2437 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2438 (unsigned long long) total_us,
2439 (unsigned long long)(total_us - idle),
2440 (unsigned long long) idle,
2441 (unsigned long long) pkt_dev->sofar,
2442 pkt_dev->cur_pkt_size, nr_frags);
2444 pps = pkt_dev->sofar * USEC_PER_SEC;
2446 while ((total_us >> 32) != 0) {
2451 do_div(pps, total_us);
2453 bps = pps * 8 * pkt_dev->cur_pkt_size;
2456 do_div(mbps, 1000000);
2457 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2458 (unsigned long long) pps,
2459 (unsigned long long) mbps,
2460 (unsigned long long) bps,
2461 (unsigned long long) pkt_dev->errors);
2465 /* Set stopped-at timer, remove from running list, do counters & statistics */
2467 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
2469 int nr_frags = pkt_dev->skb ?
2470 skb_shinfo(pkt_dev->skb)->nr_frags: -1;
2472 if (!pkt_dev->running) {
2473 printk("pktgen: interface: %s is already stopped\n", pkt_dev->ifname);
2477 pkt_dev->stopped_at = getCurUs();
2478 pkt_dev->running = 0;
2480 show_results(pkt_dev, nr_frags);
2485 static struct pktgen_dev *next_to_run(struct pktgen_thread *t )
2487 struct pktgen_dev *next, *best = NULL;
2491 for(next=t->if_list; next ; next=next->next) {
2492 if(!next->running) continue;
2493 if(best == NULL) best=next;
2494 else if ( next->next_tx_us < best->next_tx_us)
2501 static void pktgen_stop(struct pktgen_thread *t) {
2502 struct pktgen_dev *next = NULL;
2504 PG_DEBUG(printk("pktgen: entering pktgen_stop\n"));
2508 for(next=t->if_list; next; next=next->next) {
2509 pktgen_stop_device(next);
2511 kfree_skb(next->skb);
2520 * one of our devices needs to be removed - find it
2523 static void pktgen_rem_one_if(struct pktgen_thread *t)
2525 struct pktgen_dev *cur, *next = NULL;
2527 PG_DEBUG(printk("pktgen: entering pktgen_rem_one_if\n"));
2531 for(cur=t->if_list; cur; cur=next) {
2534 if (!cur->removal_mark) continue;
2537 kfree_skb(cur->skb);
2540 pktgen_remove_device(t, cur);
2548 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
2550 struct pktgen_dev *cur, *next = NULL;
2552 /* Remove all devices, free mem */
2554 PG_DEBUG(printk("pktgen: entering pktgen_rem_all_ifs\n"));
2557 for(cur=t->if_list; cur; cur=next) {
2561 kfree_skb(cur->skb);
2564 pktgen_remove_device(t, cur);
2570 static void pktgen_rem_thread(struct pktgen_thread *t)
2572 /* Remove from the thread list */
2574 struct pktgen_thread *tmp = pktgen_threads;
2576 remove_proc_entry(t->name, pg_proc_dir);
2581 pktgen_threads = tmp->next;
2584 if (tmp->next == t) {
2585 tmp->next = t->next;
2595 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
2597 struct net_device *odev = NULL;
2598 __u64 idle_start = 0;
2601 odev = pkt_dev->odev;
2603 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
2607 if (now < pkt_dev->next_tx_us)
2608 spin(pkt_dev, pkt_dev->next_tx_us);
2610 /* This is max DELAY, this has special meaning of
2613 if (pkt_dev->delay_us == 0x7FFFFFFF) {
2614 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
2615 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
2620 if (netif_queue_stopped(odev) || need_resched()) {
2621 idle_start = getCurUs();
2623 if (!netif_running(odev)) {
2624 pktgen_stop_device(pkt_dev);
2626 kfree_skb(pkt_dev->skb);
2627 pkt_dev->skb = NULL;
2633 pkt_dev->idle_acc += getCurUs() - idle_start;
2635 if (netif_queue_stopped(odev)) {
2636 pkt_dev->next_tx_us = getCurUs(); /* TODO */
2637 pkt_dev->next_tx_ns = 0;
2638 goto out; /* Try the next interface */
2642 if (pkt_dev->last_ok || !pkt_dev->skb) {
2643 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb ) || (!pkt_dev->skb)) {
2644 /* build a new pkt */
2646 kfree_skb(pkt_dev->skb);
2648 pkt_dev->skb = fill_packet(odev, pkt_dev);
2649 if (pkt_dev->skb == NULL) {
2650 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
2652 pkt_dev->clone_count--; /* back out increment, OOM */
2655 pkt_dev->allocated_skbs++;
2656 pkt_dev->clone_count = 0; /* reset counter */
2660 spin_lock_bh(&odev->xmit_lock);
2661 if (!netif_queue_stopped(odev)) {
2663 atomic_inc(&(pkt_dev->skb->users));
2665 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
2666 if (likely(ret == NETDEV_TX_OK)) {
2667 pkt_dev->last_ok = 1;
2670 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
2672 } else if (ret == NETDEV_TX_LOCKED
2673 && (odev->features & NETIF_F_LLTX)) {
2676 } else { /* Retry it next time */
2678 atomic_dec(&(pkt_dev->skb->users));
2680 if (debug && net_ratelimit())
2681 printk(KERN_INFO "pktgen: Hard xmit error\n");
2684 pkt_dev->last_ok = 0;
2687 pkt_dev->next_tx_us = getCurUs();
2688 pkt_dev->next_tx_ns = 0;
2690 pkt_dev->next_tx_us += pkt_dev->delay_us;
2691 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
2693 if (pkt_dev->next_tx_ns > 1000) {
2694 pkt_dev->next_tx_us++;
2695 pkt_dev->next_tx_ns -= 1000;
2699 else { /* Retry it next time */
2700 pkt_dev->last_ok = 0;
2701 pkt_dev->next_tx_us = getCurUs(); /* TODO */
2702 pkt_dev->next_tx_ns = 0;
2705 spin_unlock_bh(&odev->xmit_lock);
2707 /* If pkt_dev->count is zero, then run forever */
2708 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
2709 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
2710 idle_start = getCurUs();
2711 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
2712 if (signal_pending(current)) {
2717 pkt_dev->idle_acc += getCurUs() - idle_start;
2720 /* Done with this */
2721 pktgen_stop_device(pkt_dev);
2723 kfree_skb(pkt_dev->skb);
2724 pkt_dev->skb = NULL;
2730 * Main loop of the thread goes here
2733 static void pktgen_thread_worker(struct pktgen_thread *t)
2736 struct pktgen_dev *pkt_dev = NULL;
2739 u32 max_before_softirq;
2740 u32 tx_since_softirq = 0;
2742 daemonize("pktgen/%d", cpu);
2744 /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
2746 spin_lock_irq(¤t->sighand->siglock);
2747 tmpsig = current->blocked;
2748 siginitsetinv(¤t->blocked,
2753 recalc_sigpending();
2754 spin_unlock_irq(¤t->sighand->siglock);
2756 /* Migrate to the right CPU */
2757 set_cpus_allowed(current, cpumask_of_cpu(cpu));
2758 if (smp_processor_id() != cpu)
2761 init_waitqueue_head(&t->queue);
2763 t->control &= ~(T_TERMINATE);
2764 t->control &= ~(T_RUN);
2765 t->control &= ~(T_STOP);
2766 t->control &= ~(T_REMDEVALL);
2767 t->control &= ~(T_REMDEV);
2769 t->pid = current->pid;
2771 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
2773 max_before_softirq = t->max_before_softirq;
2775 __set_current_state(TASK_INTERRUPTIBLE);
2780 __set_current_state(TASK_RUNNING);
2783 * Get next dev to xmit -- if any.
2786 pkt_dev = next_to_run(t);
2790 pktgen_xmit(pkt_dev);
2793 * We like to stay RUNNING but must also give
2794 * others fair share.
2797 tx_since_softirq += pkt_dev->last_ok;
2799 if (tx_since_softirq > max_before_softirq) {
2800 if (local_softirq_pending())
2802 tx_since_softirq = 0;
2805 prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
2806 schedule_timeout(HZ/10);
2807 finish_wait(&(t->queue), &wait);
2811 * Back from sleep, either due to the timeout or signal.
2812 * We check if we have any "posted" work for us.
2815 if (t->control & T_TERMINATE || signal_pending(current))
2816 /* we received a request to terminate ourself */
2820 if(t->control & T_STOP) {
2822 t->control &= ~(T_STOP);
2825 if(t->control & T_RUN) {
2827 t->control &= ~(T_RUN);
2830 if(t->control & T_REMDEVALL) {
2831 pktgen_rem_all_ifs(t);
2832 t->control &= ~(T_REMDEVALL);
2835 if(t->control & T_REMDEV) {
2836 pktgen_rem_one_if(t);
2837 t->control &= ~(T_REMDEV);
2844 PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
2847 PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
2848 pktgen_rem_all_ifs(t);
2850 PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
2851 pktgen_rem_thread(t);
2854 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, const char* ifname)
2856 struct pktgen_dev *pkt_dev = NULL;
2859 for(pkt_dev=t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) {
2860 if (strncmp(pkt_dev->ifname, ifname, IFNAMSIZ) == 0) {
2866 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname,pkt_dev));
2871 * Adds a dev at front of if_list.
2874 static int add_dev_to_thread(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
2880 if (pkt_dev->pg_thread) {
2881 printk("pktgen: ERROR: already assigned to a thread.\n");
2885 pkt_dev->next =t->if_list; t->if_list=pkt_dev;
2886 pkt_dev->pg_thread = t;
2887 pkt_dev->running = 0;
2894 /* Called under thread lock */
2896 static int pktgen_add_device(struct pktgen_thread *t, const char* ifname)
2898 struct pktgen_dev *pkt_dev;
2899 struct proc_dir_entry *pe;
2901 /* We don't allow a device to be on several threads */
2903 pkt_dev = __pktgen_NN_threads(ifname, FIND);
2905 printk("pktgen: ERROR: interface already used.\n");
2909 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
2913 pkt_dev->flows = vmalloc(MAX_CFLOWS*sizeof(struct flow_state));
2914 if (pkt_dev->flows == NULL) {
2918 memset(pkt_dev->flows, 0, MAX_CFLOWS*sizeof(struct flow_state));
2920 pkt_dev->removal_mark = 0;
2921 pkt_dev->min_pkt_size = ETH_ZLEN;
2922 pkt_dev->max_pkt_size = ETH_ZLEN;
2923 pkt_dev->nfrags = 0;
2924 pkt_dev->clone_skb = pg_clone_skb_d;
2925 pkt_dev->delay_us = pg_delay_d / 1000;
2926 pkt_dev->delay_ns = pg_delay_d % 1000;
2927 pkt_dev->count = pg_count_d;
2929 pkt_dev->udp_src_min = 9; /* sink port */
2930 pkt_dev->udp_src_max = 9;
2931 pkt_dev->udp_dst_min = 9;
2932 pkt_dev->udp_dst_max = 9;
2934 strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
2936 if (! pktgen_setup_dev(pkt_dev)) {
2937 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
2939 vfree(pkt_dev->flows);
2944 pe = create_proc_entry(ifname, 0600, pg_proc_dir);
2946 printk("pktgen: cannot create %s/%s procfs entry.\n",
2947 PG_PROC_DIR, ifname);
2949 vfree(pkt_dev->flows);
2953 pe->proc_fops = &pktgen_if_fops;
2956 return add_dev_to_thread(t, pkt_dev);
2959 static struct pktgen_thread * __init pktgen_find_thread(const char* name)
2961 struct pktgen_thread *t = NULL;
2967 if (strcmp(t->name, name) == 0)
2976 static int __init pktgen_create_thread(const char* name, int cpu)
2978 struct pktgen_thread *t = NULL;
2979 struct proc_dir_entry *pe;
2981 if (strlen(name) > 31) {
2982 printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
2986 if (pktgen_find_thread(name)) {
2987 printk("pktgen: ERROR: thread: %s already exists\n", name);
2991 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
2993 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
2997 strcpy(t->name, name);
2998 spin_lock_init(&t->if_lock);
3001 pe = create_proc_entry(t->name, 0600, pg_proc_dir);
3003 printk("pktgen: cannot create %s/%s procfs entry.\n",
3004 PG_PROC_DIR, t->name);
3009 pe->proc_fops = &pktgen_thread_fops;
3012 t->next = pktgen_threads;
3015 if (kernel_thread((void *) pktgen_thread_worker, (void *) t,
3016 CLONE_FS | CLONE_FILES | CLONE_SIGHAND) < 0)
3017 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3023 * Removes a device from the thread if_list.
3025 static void _rem_dev_from_if_list(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
3027 struct pktgen_dev *i, *prev = NULL;
3033 if(prev) prev->next = i->next;
3034 else t->if_list = NULL;
3042 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
3045 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
3047 if (pkt_dev->running) {
3048 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3049 pktgen_stop_device(pkt_dev);
3052 /* Dis-associate from the interface */
3054 if (pkt_dev->odev) {
3055 dev_put(pkt_dev->odev);
3056 pkt_dev->odev = NULL;
3059 /* And update the thread if_list */
3061 _rem_dev_from_if_list(t, pkt_dev);
3063 /* Clean up proc file system */
3065 remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
3068 vfree(pkt_dev->flows);
3073 static int __init pg_init(void)
3076 struct proc_dir_entry *pe;
3080 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3083 pg_proc_dir->owner = THIS_MODULE;
3085 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3087 printk("pktgen: ERROR: cannot create %s procfs entry.\n", PGCTRL);
3088 proc_net_remove(PG_PROC_DIR);
3092 pe->proc_fops = &pktgen_fops;
3095 /* Register us to receive netdevice events */
3096 register_netdevice_notifier(&pktgen_notifier_block);
3098 for_each_online_cpu(cpu) {
3101 sprintf(buf, "kpktgend_%i", cpu);
3102 pktgen_create_thread(buf, cpu);
3107 static void __exit pg_cleanup(void)
3109 wait_queue_head_t queue;
3110 init_waitqueue_head(&queue);
3112 /* Stop all interfaces & threads */
3114 while (pktgen_threads) {
3115 struct pktgen_thread *t = pktgen_threads;
3116 pktgen_threads->control |= (T_TERMINATE);
3118 wait_event_interruptible_timeout(queue, (t != pktgen_threads), HZ);
3121 /* Un-register us from receiving netdevice events */
3122 unregister_netdevice_notifier(&pktgen_notifier_block);
3124 /* Clean up proc file system */
3125 remove_proc_entry(PGCTRL, pg_proc_dir);
3126 proc_net_remove(PG_PROC_DIR);
3130 module_init(pg_init);
3131 module_exit(pg_cleanup);
3133 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3134 MODULE_DESCRIPTION("Packet Generator tool");
3135 MODULE_LICENSE("GPL");
3136 module_param(pg_count_d, int, 0);
3137 module_param(pg_delay_d, int, 0);
3138 module_param(pg_clone_skb_d, int, 0);
3139 module_param(debug, int, 0);