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/list.h>
129 #include <linux/init.h>
130 #include <linux/skbuff.h>
131 #include <linux/netdevice.h>
132 #include <linux/inet.h>
133 #include <linux/inetdevice.h>
134 #include <linux/rtnetlink.h>
135 #include <linux/if_arp.h>
136 #include <linux/in.h>
137 #include <linux/ip.h>
138 #include <linux/ipv6.h>
139 #include <linux/udp.h>
140 #include <linux/proc_fs.h>
141 #include <linux/seq_file.h>
142 #include <linux/wait.h>
143 #include <linux/etherdevice.h>
144 #include <net/checksum.h>
145 #include <net/ipv6.h>
146 #include <net/addrconf.h>
147 #include <asm/byteorder.h>
148 #include <linux/rcupdate.h>
149 #include <asm/bitops.h>
152 #include <asm/uaccess.h>
153 #include <asm/div64.h> /* do_div */
154 #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
206 * Try to keep frequent/infrequent used vars. separated.
209 char ifname[IFNAMSIZ];
212 struct pktgen_thread *pg_thread; /* the owner */
213 struct list_head list; /* Used for chaining in the thread's run-queue */
215 int running; /* if this changes to false, the test will stop */
217 /* If min != max, then we will either do a linear iteration, or
218 * we will do a random selection from within the range.
221 int removal_mark; /* non-zero => the device is marked for
222 * removal by worker thread */
224 int min_pkt_size; /* = ETH_ZLEN; */
225 int max_pkt_size; /* = ETH_ZLEN; */
227 __u32 delay_us; /* Default delay */
229 __u64 count; /* Default No packets to send */
230 __u64 sofar; /* How many pkts we've sent so far */
231 __u64 tx_bytes; /* How many bytes we've transmitted */
232 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
234 /* runtime counters relating to clone_skb */
235 __u64 next_tx_us; /* timestamp of when to tx next */
238 __u64 allocated_skbs;
240 int last_ok; /* Was last skb sent?
241 * Or a failed transmit of some sort? This will keep
242 * sequence numbers in order, for example.
244 __u64 started_at; /* micro-seconds */
245 __u64 stopped_at; /* micro-seconds */
246 __u64 idle_acc; /* micro-seconds */
249 int clone_skb; /* Use multiple SKBs during packet gen. If this number
250 * is greater than 1, then that many copies of the same
251 * packet will be sent before a new packet is allocated.
252 * For instance, if you want to send 1024 identical packets
253 * before creating a new packet, set clone_skb to 1024.
256 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
257 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
258 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
259 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
261 struct in6_addr in6_saddr;
262 struct in6_addr in6_daddr;
263 struct in6_addr cur_in6_daddr;
264 struct in6_addr cur_in6_saddr;
266 struct in6_addr min_in6_daddr;
267 struct in6_addr max_in6_daddr;
268 struct in6_addr min_in6_saddr;
269 struct in6_addr max_in6_saddr;
271 /* If we're doing ranges, random or incremental, then this
272 * defines the min/max for those ranges.
274 __u32 saddr_min; /* inclusive, source IP address */
275 __u32 saddr_max; /* exclusive, source IP address */
276 __u32 daddr_min; /* inclusive, dest IP address */
277 __u32 daddr_max; /* exclusive, dest IP address */
279 __u16 udp_src_min; /* inclusive, source UDP port */
280 __u16 udp_src_max; /* exclusive, source UDP port */
281 __u16 udp_dst_min; /* inclusive, dest UDP port */
282 __u16 udp_dst_max; /* exclusive, dest UDP port */
284 __u32 src_mac_count; /* How many MACs to iterate through */
285 __u32 dst_mac_count; /* How many MACs to iterate through */
287 unsigned char dst_mac[ETH_ALEN];
288 unsigned char src_mac[ETH_ALEN];
290 __u32 cur_dst_mac_offset;
291 __u32 cur_src_mac_offset;
300 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
302 We fill in SRC address later
303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 __u16 pad; /* pad out the hh struct to an even 16 bytes */
309 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
310 * are transmitting the same one multiple times
312 struct net_device *odev; /* The out-going device. Note that the device should
313 * have it's pg_info pointer pointing back to this
314 * device. This will be set when the user specifies
315 * the out-going device name (not when the inject is
316 * started as it used to do.)
318 struct flow_state *flows;
319 unsigned cflows; /* Concurrent flows (config) */
320 unsigned lflow; /* Flow length (config) */
321 unsigned nflows; /* accumulated flows (stats) */
331 struct pktgen_thread {
333 struct list_head if_list; /* All device here */
334 struct list_head th_list;
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;
394 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
398 /* End of hacks to deal with 64-bit math on x86 */
400 /** Convert to milliseconds */
401 static inline __u64 tv_to_ms(const struct timeval *tv)
403 __u64 ms = tv->tv_usec / 1000;
404 ms += (__u64) tv->tv_sec * (__u64) 1000;
408 /** Convert to micro-seconds */
409 static inline __u64 tv_to_us(const struct timeval *tv)
411 __u64 us = tv->tv_usec;
412 us += (__u64) tv->tv_sec * (__u64) 1000000;
416 static inline __u64 pg_div(__u64 n, __u32 base)
420 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
425 static inline __u64 pg_div64(__u64 n, __u64 base)
429 * How do we know if the architecture we are running on
430 * supports division with 64 bit base?
433 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
437 tmp = divremdi3(n, base, PG_DIV);
442 static inline u32 pktgen_random(void)
446 get_random_bytes(&n, 4);
453 static inline __u64 getCurMs(void)
456 do_gettimeofday(&tv);
457 return tv_to_ms(&tv);
460 static inline __u64 getCurUs(void)
463 do_gettimeofday(&tv);
464 return tv_to_us(&tv);
467 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
469 return tv_to_us(a) - tv_to_us(b);
472 /* old include end */
474 static char version[] __initdata = VERSION;
476 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
477 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
478 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
480 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
481 static void pktgen_run_all_threads(void);
482 static void pktgen_stop_all_threads_ifs(void);
483 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
484 static void pktgen_stop(struct pktgen_thread *t);
485 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
486 static int pktgen_mark_device(const char *ifname);
487 static unsigned int scan_ip6(const char *s, char ip[16]);
488 static unsigned int fmt_ip6(char *s, const char ip[16]);
490 /* Module parameters, defaults. */
491 static int pg_count_d = 1000; /* 1000 pkts by default */
492 static int pg_delay_d;
493 static int pg_clone_skb_d;
496 static DECLARE_MUTEX(pktgen_sem);
497 static LIST_HEAD(pktgen_threads);
499 static struct notifier_block pktgen_notifier_block = {
500 .notifier_call = pktgen_device_event,
504 * /proc handling functions
508 static int pgctrl_show(struct seq_file *seq, void *v)
510 seq_puts(seq, VERSION);
514 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
515 size_t count, loff_t * ppos)
520 if (!capable(CAP_NET_ADMIN)) {
525 if (count > sizeof(data))
526 count = sizeof(data);
528 if (copy_from_user(data, buf, count)) {
532 data[count - 1] = 0; /* Make string */
534 if (!strcmp(data, "stop"))
535 pktgen_stop_all_threads_ifs();
537 else if (!strcmp(data, "start"))
538 pktgen_run_all_threads();
541 printk("pktgen: Unknown command: %s\n", data);
549 static int pgctrl_open(struct inode *inode, struct file *file)
551 return single_open(file, pgctrl_show, PDE(inode)->data);
554 static struct file_operations pktgen_fops = {
555 .owner = THIS_MODULE,
559 .write = pgctrl_write,
560 .release = single_release,
563 static int pktgen_if_show(struct seq_file *seq, void *v)
566 struct pktgen_dev *pkt_dev = seq->private;
569 __u64 now = getCurUs();
572 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
573 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
574 pkt_dev->max_pkt_size);
577 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
579 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
580 pkt_dev->clone_skb, pkt_dev->ifname);
582 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
585 if (pkt_dev->flags & F_IPV6) {
586 char b1[128], b2[128], b3[128];
587 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
588 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
589 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
591 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
594 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
595 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
596 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
598 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
603 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
604 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
607 seq_puts(seq, " src_mac: ");
609 if (is_zero_ether_addr(pkt_dev->src_mac))
610 for (i = 0; i < 6; i++)
611 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
614 for (i = 0; i < 6; i++)
615 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
618 seq_printf(seq, "dst_mac: ");
619 for (i = 0; i < 6; i++)
620 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
621 i == 5 ? "\n" : ":");
624 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
625 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
626 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
629 " src_mac_count: %d dst_mac_count: %d \n Flags: ",
630 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
632 if (pkt_dev->flags & F_IPV6)
633 seq_printf(seq, "IPV6 ");
635 if (pkt_dev->flags & F_IPSRC_RND)
636 seq_printf(seq, "IPSRC_RND ");
638 if (pkt_dev->flags & F_IPDST_RND)
639 seq_printf(seq, "IPDST_RND ");
641 if (pkt_dev->flags & F_TXSIZE_RND)
642 seq_printf(seq, "TXSIZE_RND ");
644 if (pkt_dev->flags & F_UDPSRC_RND)
645 seq_printf(seq, "UDPSRC_RND ");
647 if (pkt_dev->flags & F_UDPDST_RND)
648 seq_printf(seq, "UDPDST_RND ");
650 if (pkt_dev->flags & F_MACSRC_RND)
651 seq_printf(seq, "MACSRC_RND ");
653 if (pkt_dev->flags & F_MACDST_RND)
654 seq_printf(seq, "MACDST_RND ");
658 sa = pkt_dev->started_at;
659 stopped = pkt_dev->stopped_at;
660 if (pkt_dev->running)
661 stopped = now; /* not really stopped, more like last-running-at */
664 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
665 (unsigned long long)pkt_dev->sofar,
666 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
667 (unsigned long long)stopped,
668 (unsigned long long)pkt_dev->idle_acc);
671 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
672 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
673 pkt_dev->cur_src_mac_offset);
675 if (pkt_dev->flags & F_IPV6) {
676 char b1[128], b2[128];
677 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
678 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
679 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
681 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
682 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
684 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
685 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
687 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
689 if (pkt_dev->result[0])
690 seq_printf(seq, "Result: %s\n", pkt_dev->result);
692 seq_printf(seq, "Result: Idle\n");
697 static int count_trail_chars(const char __user * user_buffer,
702 for (i = 0; i < maxlen; i++) {
704 if (get_user(c, &user_buffer[i]))
722 static unsigned long num_arg(const char __user * user_buffer,
723 unsigned long maxlen, unsigned long *num)
728 for (; i < maxlen; i++) {
730 if (get_user(c, &user_buffer[i]))
732 if ((c >= '0') && (c <= '9')) {
741 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
745 for (; i < maxlen; i++) {
747 if (get_user(c, &user_buffer[i]))
765 static ssize_t pktgen_if_write(struct file *file,
766 const char __user * user_buffer, size_t count,
769 struct seq_file *seq = (struct seq_file *)file->private_data;
770 struct pktgen_dev *pkt_dev = seq->private;
772 char name[16], valstr[32];
773 unsigned long value = 0;
774 char *pg_result = NULL;
778 pg_result = &(pkt_dev->result[0]);
781 printk("pktgen: wrong command format\n");
786 tmp = count_trail_chars(&user_buffer[i], max);
788 printk("pktgen: illegal format\n");
793 /* Read variable name */
795 len = strn_len(&user_buffer[i], sizeof(name) - 1);
799 memset(name, 0, sizeof(name));
800 if (copy_from_user(name, &user_buffer[i], len))
805 len = count_trail_chars(&user_buffer[i], max);
813 if (copy_from_user(tb, user_buffer, count))
816 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
817 (unsigned long)count, tb);
820 if (!strcmp(name, "min_pkt_size")) {
821 len = num_arg(&user_buffer[i], 10, &value);
826 if (value < 14 + 20 + 8)
828 if (value != pkt_dev->min_pkt_size) {
829 pkt_dev->min_pkt_size = value;
830 pkt_dev->cur_pkt_size = value;
832 sprintf(pg_result, "OK: min_pkt_size=%u",
833 pkt_dev->min_pkt_size);
837 if (!strcmp(name, "max_pkt_size")) {
838 len = num_arg(&user_buffer[i], 10, &value);
843 if (value < 14 + 20 + 8)
845 if (value != pkt_dev->max_pkt_size) {
846 pkt_dev->max_pkt_size = value;
847 pkt_dev->cur_pkt_size = value;
849 sprintf(pg_result, "OK: max_pkt_size=%u",
850 pkt_dev->max_pkt_size);
854 /* Shortcut for min = max */
856 if (!strcmp(name, "pkt_size")) {
857 len = num_arg(&user_buffer[i], 10, &value);
862 if (value < 14 + 20 + 8)
864 if (value != pkt_dev->min_pkt_size) {
865 pkt_dev->min_pkt_size = value;
866 pkt_dev->max_pkt_size = value;
867 pkt_dev->cur_pkt_size = value;
869 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
873 if (!strcmp(name, "debug")) {
874 len = num_arg(&user_buffer[i], 10, &value);
880 sprintf(pg_result, "OK: debug=%u", debug);
884 if (!strcmp(name, "frags")) {
885 len = num_arg(&user_buffer[i], 10, &value);
890 pkt_dev->nfrags = value;
891 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
894 if (!strcmp(name, "delay")) {
895 len = num_arg(&user_buffer[i], 10, &value);
900 if (value == 0x7FFFFFFF) {
901 pkt_dev->delay_us = 0x7FFFFFFF;
902 pkt_dev->delay_ns = 0;
904 pkt_dev->delay_us = value / 1000;
905 pkt_dev->delay_ns = value % 1000;
907 sprintf(pg_result, "OK: delay=%u",
908 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
911 if (!strcmp(name, "udp_src_min")) {
912 len = num_arg(&user_buffer[i], 10, &value);
917 if (value != pkt_dev->udp_src_min) {
918 pkt_dev->udp_src_min = value;
919 pkt_dev->cur_udp_src = value;
921 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
924 if (!strcmp(name, "udp_dst_min")) {
925 len = num_arg(&user_buffer[i], 10, &value);
930 if (value != pkt_dev->udp_dst_min) {
931 pkt_dev->udp_dst_min = value;
932 pkt_dev->cur_udp_dst = value;
934 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
937 if (!strcmp(name, "udp_src_max")) {
938 len = num_arg(&user_buffer[i], 10, &value);
943 if (value != pkt_dev->udp_src_max) {
944 pkt_dev->udp_src_max = value;
945 pkt_dev->cur_udp_src = value;
947 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
950 if (!strcmp(name, "udp_dst_max")) {
951 len = num_arg(&user_buffer[i], 10, &value);
956 if (value != pkt_dev->udp_dst_max) {
957 pkt_dev->udp_dst_max = value;
958 pkt_dev->cur_udp_dst = value;
960 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
963 if (!strcmp(name, "clone_skb")) {
964 len = num_arg(&user_buffer[i], 10, &value);
969 pkt_dev->clone_skb = value;
971 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
974 if (!strcmp(name, "count")) {
975 len = num_arg(&user_buffer[i], 10, &value);
980 pkt_dev->count = value;
981 sprintf(pg_result, "OK: count=%llu",
982 (unsigned long long)pkt_dev->count);
985 if (!strcmp(name, "src_mac_count")) {
986 len = num_arg(&user_buffer[i], 10, &value);
991 if (pkt_dev->src_mac_count != value) {
992 pkt_dev->src_mac_count = value;
993 pkt_dev->cur_src_mac_offset = 0;
995 sprintf(pg_result, "OK: src_mac_count=%d",
996 pkt_dev->src_mac_count);
999 if (!strcmp(name, "dst_mac_count")) {
1000 len = num_arg(&user_buffer[i], 10, &value);
1005 if (pkt_dev->dst_mac_count != value) {
1006 pkt_dev->dst_mac_count = value;
1007 pkt_dev->cur_dst_mac_offset = 0;
1009 sprintf(pg_result, "OK: dst_mac_count=%d",
1010 pkt_dev->dst_mac_count);
1013 if (!strcmp(name, "flag")) {
1016 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1020 if (copy_from_user(f, &user_buffer[i], len))
1023 if (strcmp(f, "IPSRC_RND") == 0)
1024 pkt_dev->flags |= F_IPSRC_RND;
1026 else if (strcmp(f, "!IPSRC_RND") == 0)
1027 pkt_dev->flags &= ~F_IPSRC_RND;
1029 else if (strcmp(f, "TXSIZE_RND") == 0)
1030 pkt_dev->flags |= F_TXSIZE_RND;
1032 else if (strcmp(f, "!TXSIZE_RND") == 0)
1033 pkt_dev->flags &= ~F_TXSIZE_RND;
1035 else if (strcmp(f, "IPDST_RND") == 0)
1036 pkt_dev->flags |= F_IPDST_RND;
1038 else if (strcmp(f, "!IPDST_RND") == 0)
1039 pkt_dev->flags &= ~F_IPDST_RND;
1041 else if (strcmp(f, "UDPSRC_RND") == 0)
1042 pkt_dev->flags |= F_UDPSRC_RND;
1044 else if (strcmp(f, "!UDPSRC_RND") == 0)
1045 pkt_dev->flags &= ~F_UDPSRC_RND;
1047 else if (strcmp(f, "UDPDST_RND") == 0)
1048 pkt_dev->flags |= F_UDPDST_RND;
1050 else if (strcmp(f, "!UDPDST_RND") == 0)
1051 pkt_dev->flags &= ~F_UDPDST_RND;
1053 else if (strcmp(f, "MACSRC_RND") == 0)
1054 pkt_dev->flags |= F_MACSRC_RND;
1056 else if (strcmp(f, "!MACSRC_RND") == 0)
1057 pkt_dev->flags &= ~F_MACSRC_RND;
1059 else if (strcmp(f, "MACDST_RND") == 0)
1060 pkt_dev->flags |= F_MACDST_RND;
1062 else if (strcmp(f, "!MACDST_RND") == 0)
1063 pkt_dev->flags &= ~F_MACDST_RND;
1067 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1069 "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n");
1072 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1075 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1076 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1081 if (copy_from_user(buf, &user_buffer[i], len))
1084 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1085 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1086 strncpy(pkt_dev->dst_min, buf, len);
1087 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1088 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1091 printk("pktgen: dst_min set to: %s\n",
1094 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1097 if (!strcmp(name, "dst_max")) {
1098 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1103 if (copy_from_user(buf, &user_buffer[i], len))
1107 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1108 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1109 strncpy(pkt_dev->dst_max, buf, len);
1110 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1111 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1114 printk("pktgen: dst_max set to: %s\n",
1117 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1120 if (!strcmp(name, "dst6")) {
1121 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1125 pkt_dev->flags |= F_IPV6;
1127 if (copy_from_user(buf, &user_buffer[i], len))
1131 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1132 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1134 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1137 printk("pktgen: dst6 set to: %s\n", buf);
1140 sprintf(pg_result, "OK: dst6=%s", buf);
1143 if (!strcmp(name, "dst6_min")) {
1144 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1148 pkt_dev->flags |= F_IPV6;
1150 if (copy_from_user(buf, &user_buffer[i], len))
1154 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1155 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1157 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1158 &pkt_dev->min_in6_daddr);
1160 printk("pktgen: dst6_min set to: %s\n", buf);
1163 sprintf(pg_result, "OK: dst6_min=%s", buf);
1166 if (!strcmp(name, "dst6_max")) {
1167 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1171 pkt_dev->flags |= F_IPV6;
1173 if (copy_from_user(buf, &user_buffer[i], len))
1177 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1178 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1181 printk("pktgen: dst6_max set to: %s\n", buf);
1184 sprintf(pg_result, "OK: dst6_max=%s", buf);
1187 if (!strcmp(name, "src6")) {
1188 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1192 pkt_dev->flags |= F_IPV6;
1194 if (copy_from_user(buf, &user_buffer[i], len))
1198 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1199 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1201 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1204 printk("pktgen: src6 set to: %s\n", buf);
1207 sprintf(pg_result, "OK: src6=%s", buf);
1210 if (!strcmp(name, "src_min")) {
1211 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1215 if (copy_from_user(buf, &user_buffer[i], len))
1218 if (strcmp(buf, pkt_dev->src_min) != 0) {
1219 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1220 strncpy(pkt_dev->src_min, buf, len);
1221 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1222 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1225 printk("pktgen: src_min set to: %s\n",
1228 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1231 if (!strcmp(name, "src_max")) {
1232 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1236 if (copy_from_user(buf, &user_buffer[i], len))
1239 if (strcmp(buf, pkt_dev->src_max) != 0) {
1240 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1241 strncpy(pkt_dev->src_max, buf, len);
1242 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1243 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1246 printk("pktgen: src_max set to: %s\n",
1249 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1252 if (!strcmp(name, "dst_mac")) {
1254 unsigned char old_dmac[ETH_ALEN];
1255 unsigned char *m = pkt_dev->dst_mac;
1256 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1258 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1262 memset(valstr, 0, sizeof(valstr));
1263 if (copy_from_user(valstr, &user_buffer[i], len))
1267 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1268 if (*v >= '0' && *v <= '9') {
1272 if (*v >= 'A' && *v <= 'F') {
1274 *m += *v - 'A' + 10;
1276 if (*v >= 'a' && *v <= 'f') {
1278 *m += *v - 'a' + 10;
1286 /* Set up Dest MAC */
1287 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1288 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1290 sprintf(pg_result, "OK: dstmac");
1293 if (!strcmp(name, "src_mac")) {
1295 unsigned char *m = pkt_dev->src_mac;
1297 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1301 memset(valstr, 0, sizeof(valstr));
1302 if (copy_from_user(valstr, &user_buffer[i], len))
1306 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1307 if (*v >= '0' && *v <= '9') {
1311 if (*v >= 'A' && *v <= 'F') {
1313 *m += *v - 'A' + 10;
1315 if (*v >= 'a' && *v <= 'f') {
1317 *m += *v - 'a' + 10;
1325 sprintf(pg_result, "OK: srcmac");
1329 if (!strcmp(name, "clear_counters")) {
1330 pktgen_clear_counters(pkt_dev);
1331 sprintf(pg_result, "OK: Clearing counters.\n");
1335 if (!strcmp(name, "flows")) {
1336 len = num_arg(&user_buffer[i], 10, &value);
1341 if (value > MAX_CFLOWS)
1344 pkt_dev->cflows = value;
1345 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1349 if (!strcmp(name, "flowlen")) {
1350 len = num_arg(&user_buffer[i], 10, &value);
1355 pkt_dev->lflow = value;
1356 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1360 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1364 static int pktgen_if_open(struct inode *inode, struct file *file)
1366 return single_open(file, pktgen_if_show, PDE(inode)->data);
1369 static struct file_operations pktgen_if_fops = {
1370 .owner = THIS_MODULE,
1371 .open = pktgen_if_open,
1373 .llseek = seq_lseek,
1374 .write = pktgen_if_write,
1375 .release = single_release,
1378 static int pktgen_thread_show(struct seq_file *seq, void *v)
1380 struct pktgen_thread *t = seq->private;
1381 struct pktgen_dev *pkt_dev;
1385 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1386 t->name, t->max_before_softirq);
1388 seq_printf(seq, "Running: ");
1391 list_for_each_entry(pkt_dev, &t->if_list, list)
1392 if (pkt_dev->running)
1393 seq_printf(seq, "%s ", pkt_dev->ifname);
1395 seq_printf(seq, "\nStopped: ");
1397 list_for_each_entry(pkt_dev, &t->if_list, list)
1398 if (!pkt_dev->running)
1399 seq_printf(seq, "%s ", pkt_dev->ifname);
1402 seq_printf(seq, "\nResult: %s\n", t->result);
1404 seq_printf(seq, "\nResult: NA\n");
1411 static ssize_t pktgen_thread_write(struct file *file,
1412 const char __user * user_buffer,
1413 size_t count, loff_t * offset)
1415 struct seq_file *seq = (struct seq_file *)file->private_data;
1416 struct pktgen_thread *t = seq->private;
1417 int i = 0, max, len, ret;
1420 unsigned long value = 0;
1423 // sprintf(pg_result, "Wrong command format");
1428 len = count_trail_chars(&user_buffer[i], max);
1434 /* Read variable name */
1436 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1440 memset(name, 0, sizeof(name));
1441 if (copy_from_user(name, &user_buffer[i], len))
1446 len = count_trail_chars(&user_buffer[i], max);
1453 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
1456 printk("pktgen: ERROR: No thread\n");
1461 pg_result = &(t->result[0]);
1463 if (!strcmp(name, "add_device")) {
1466 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1471 if (copy_from_user(f, &user_buffer[i], len))
1475 pktgen_add_device(t, f);
1478 sprintf(pg_result, "OK: add_device=%s", f);
1482 if (!strcmp(name, "rem_device_all")) {
1484 t->control |= T_REMDEVALL;
1486 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1488 sprintf(pg_result, "OK: rem_device_all");
1492 if (!strcmp(name, "max_before_softirq")) {
1493 len = num_arg(&user_buffer[i], 10, &value);
1495 t->max_before_softirq = value;
1498 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1507 static int pktgen_thread_open(struct inode *inode, struct file *file)
1509 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1512 static struct file_operations pktgen_thread_fops = {
1513 .owner = THIS_MODULE,
1514 .open = pktgen_thread_open,
1516 .llseek = seq_lseek,
1517 .write = pktgen_thread_write,
1518 .release = single_release,
1521 /* Think find or remove for NN */
1522 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1524 struct pktgen_thread *t;
1525 struct pktgen_dev *pkt_dev = NULL;
1527 list_for_each_entry(t, &pktgen_threads, th_list) {
1528 pkt_dev = pktgen_find_dev(t, ifname);
1532 pkt_dev->removal_mark = 1;
1533 t->control |= T_REMDEV;
1543 * mark a device for removal
1545 static int pktgen_mark_device(const char *ifname)
1547 struct pktgen_dev *pkt_dev = NULL;
1548 const int max_tries = 10, msec_per_try = 125;
1553 PG_DEBUG(printk("pktgen: pktgen_mark_device marking %s for removal\n",
1558 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1559 if (pkt_dev == NULL)
1560 break; /* success */
1563 PG_DEBUG(printk("pktgen: pktgen_mark_device waiting for %s "
1564 "to disappear....\n", ifname));
1565 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1568 if (++i >= max_tries) {
1569 printk("pktgen_mark_device: timed out after waiting "
1570 "%d msec for device %s to be removed\n",
1571 msec_per_try * i, ifname);
1583 static int pktgen_device_event(struct notifier_block *unused,
1584 unsigned long event, void *ptr)
1586 struct net_device *dev = (struct net_device *)(ptr);
1588 /* It is OK that we do not hold the group lock right now,
1589 * as we run under the RTNL lock.
1593 case NETDEV_CHANGEADDR:
1594 case NETDEV_GOING_DOWN:
1597 /* Ignore for now */
1600 case NETDEV_UNREGISTER:
1601 pktgen_mark_device(dev->name);
1608 /* Associate pktgen_dev with a device. */
1610 static struct net_device *pktgen_setup_dev(struct pktgen_dev *pkt_dev)
1612 struct net_device *odev;
1614 /* Clean old setups */
1616 if (pkt_dev->odev) {
1617 dev_put(pkt_dev->odev);
1618 pkt_dev->odev = NULL;
1621 odev = dev_get_by_name(pkt_dev->ifname);
1624 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1627 if (odev->type != ARPHRD_ETHER) {
1628 printk("pktgen: not an ethernet device: \"%s\"\n",
1632 if (!netif_running(odev)) {
1633 printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1636 pkt_dev->odev = odev;
1638 return pkt_dev->odev;
1647 /* Read pkt_dev from the interface and set up internal pktgen_dev
1648 * structure to have the right information to create/send packets
1650 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1652 /* Try once more, just in case it works now. */
1654 pktgen_setup_dev(pkt_dev);
1656 if (!pkt_dev->odev) {
1657 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1658 sprintf(pkt_dev->result,
1659 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1663 /* Default to the interface's mac if not explicitly set. */
1665 if (is_zero_ether_addr(pkt_dev->src_mac))
1666 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1668 /* Set up Dest MAC */
1669 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1671 /* Set up pkt size */
1672 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1674 if (pkt_dev->flags & F_IPV6) {
1676 * Skip this automatic address setting until locks or functions
1681 int i, set = 0, err = 1;
1682 struct inet6_dev *idev;
1684 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1685 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1693 * Use linklevel address if unconfigured.
1695 * use ipv6_get_lladdr if/when it's get exported
1698 read_lock(&addrconf_lock);
1699 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1700 struct inet6_ifaddr *ifp;
1702 read_lock_bh(&idev->lock);
1703 for (ifp = idev->addr_list; ifp;
1704 ifp = ifp->if_next) {
1705 if (ifp->scope == IFA_LINK
1707 flags & IFA_F_TENTATIVE)) {
1708 ipv6_addr_copy(&pkt_dev->
1715 read_unlock_bh(&idev->lock);
1717 read_unlock(&addrconf_lock);
1719 printk("pktgen: ERROR: IPv6 link address not availble.\n");
1723 pkt_dev->saddr_min = 0;
1724 pkt_dev->saddr_max = 0;
1725 if (strlen(pkt_dev->src_min) == 0) {
1727 struct in_device *in_dev;
1730 in_dev = __in_dev_get_rcu(pkt_dev->odev);
1732 if (in_dev->ifa_list) {
1733 pkt_dev->saddr_min =
1734 in_dev->ifa_list->ifa_address;
1735 pkt_dev->saddr_max = pkt_dev->saddr_min;
1740 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1741 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1744 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1745 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1747 /* Initialize current values. */
1748 pkt_dev->cur_dst_mac_offset = 0;
1749 pkt_dev->cur_src_mac_offset = 0;
1750 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1751 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1752 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
1753 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
1754 pkt_dev->nflows = 0;
1757 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
1762 start = now = getCurUs();
1763 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
1764 while (now < spin_until_us) {
1765 /* TODO: optimize sleeping behavior */
1766 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
1767 schedule_timeout_interruptible(1);
1768 else if (spin_until_us - now > 100) {
1770 if (!pkt_dev->running)
1779 pkt_dev->idle_acc += now - start;
1782 /* Increment/randomize headers according to flags and current values
1783 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
1785 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
1791 if (pkt_dev->cflows) {
1792 flow = pktgen_random() % pkt_dev->cflows;
1794 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
1795 pkt_dev->flows[flow].count = 0;
1798 /* Deal with source MAC */
1799 if (pkt_dev->src_mac_count > 1) {
1803 if (pkt_dev->flags & F_MACSRC_RND)
1804 mc = pktgen_random() % (pkt_dev->src_mac_count);
1806 mc = pkt_dev->cur_src_mac_offset++;
1807 if (pkt_dev->cur_src_mac_offset >
1808 pkt_dev->src_mac_count)
1809 pkt_dev->cur_src_mac_offset = 0;
1812 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
1813 pkt_dev->hh[11] = tmp;
1814 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1815 pkt_dev->hh[10] = tmp;
1816 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1817 pkt_dev->hh[9] = tmp;
1818 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1819 pkt_dev->hh[8] = tmp;
1820 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
1821 pkt_dev->hh[7] = tmp;
1824 /* Deal with Destination MAC */
1825 if (pkt_dev->dst_mac_count > 1) {
1829 if (pkt_dev->flags & F_MACDST_RND)
1830 mc = pktgen_random() % (pkt_dev->dst_mac_count);
1833 mc = pkt_dev->cur_dst_mac_offset++;
1834 if (pkt_dev->cur_dst_mac_offset >
1835 pkt_dev->dst_mac_count) {
1836 pkt_dev->cur_dst_mac_offset = 0;
1840 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
1841 pkt_dev->hh[5] = tmp;
1842 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1843 pkt_dev->hh[4] = tmp;
1844 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1845 pkt_dev->hh[3] = tmp;
1846 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1847 pkt_dev->hh[2] = tmp;
1848 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
1849 pkt_dev->hh[1] = tmp;
1852 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
1853 if (pkt_dev->flags & F_UDPSRC_RND)
1854 pkt_dev->cur_udp_src =
1856 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) +
1857 pkt_dev->udp_src_min);
1860 pkt_dev->cur_udp_src++;
1861 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
1862 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
1866 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
1867 if (pkt_dev->flags & F_UDPDST_RND) {
1868 pkt_dev->cur_udp_dst =
1870 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) +
1871 pkt_dev->udp_dst_min);
1873 pkt_dev->cur_udp_dst++;
1874 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
1875 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
1879 if (!(pkt_dev->flags & F_IPV6)) {
1881 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
1885 if (pkt_dev->flags & F_IPSRC_RND)
1886 t = ((pktgen_random() % (imx - imn)) + imn);
1888 t = ntohl(pkt_dev->cur_saddr);
1894 pkt_dev->cur_saddr = htonl(t);
1897 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
1898 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
1901 if ((imn = ntohl(pkt_dev->daddr_min)) < (imx =
1906 if (pkt_dev->flags & F_IPDST_RND) {
1908 t = ((pktgen_random() % (imx - imn)) +
1912 while (LOOPBACK(t) || MULTICAST(t)
1913 || BADCLASS(t) || ZERONET(t)
1914 || LOCAL_MCAST(t)) {
1915 t = ((pktgen_random() %
1916 (imx - imn)) + imn);
1919 pkt_dev->cur_daddr = t;
1923 t = ntohl(pkt_dev->cur_daddr);
1928 pkt_dev->cur_daddr = htonl(t);
1931 if (pkt_dev->cflows) {
1932 pkt_dev->flows[flow].cur_daddr =
1937 } else { /* IPV6 * */
1939 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
1940 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
1941 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
1942 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
1946 /* Only random destinations yet */
1948 for (i = 0; i < 4; i++) {
1949 pkt_dev->cur_in6_daddr.s6_addr32[i] =
1951 pkt_dev->min_in6_daddr.s6_addr32[i]) &
1952 pkt_dev->max_in6_daddr.s6_addr32[i]);
1957 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
1959 if (pkt_dev->flags & F_TXSIZE_RND) {
1960 t = ((pktgen_random() %
1961 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size))
1962 + pkt_dev->min_pkt_size);
1964 t = pkt_dev->cur_pkt_size + 1;
1965 if (t > pkt_dev->max_pkt_size)
1966 t = pkt_dev->min_pkt_size;
1968 pkt_dev->cur_pkt_size = t;
1971 pkt_dev->flows[flow].count++;
1974 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
1975 struct pktgen_dev *pkt_dev)
1977 struct sk_buff *skb = NULL;
1979 struct udphdr *udph;
1982 struct pktgen_hdr *pgh = NULL;
1984 /* Update any of the values, used when we're incrementing various
1987 mod_cur_headers(pkt_dev);
1989 datalen = (odev->hard_header_len + 16) & ~0xf;
1990 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen, GFP_ATOMIC);
1992 sprintf(pkt_dev->result, "No memory");
1996 skb_reserve(skb, datalen);
1998 /* Reserve for ethernet and IP header */
1999 eth = (__u8 *) skb_push(skb, 14);
2000 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
2001 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2003 memcpy(eth, pkt_dev->hh, 12);
2004 *(u16 *) & eth[12] = __constant_htons(ETH_P_IP);
2006 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8; /* Eth + IPh + UDPh */
2007 if (datalen < sizeof(struct pktgen_hdr))
2008 datalen = sizeof(struct pktgen_hdr);
2010 udph->source = htons(pkt_dev->cur_udp_src);
2011 udph->dest = htons(pkt_dev->cur_udp_dst);
2012 udph->len = htons(datalen + 8); /* DATA + udphdr */
2013 udph->check = 0; /* No checksum */
2019 iph->protocol = IPPROTO_UDP; /* UDP */
2020 iph->saddr = pkt_dev->cur_saddr;
2021 iph->daddr = pkt_dev->cur_daddr;
2023 iplen = 20 + 8 + datalen;
2024 iph->tot_len = htons(iplen);
2026 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2027 skb->protocol = __constant_htons(ETH_P_IP);
2028 skb->mac.raw = ((u8 *) iph) - 14;
2030 skb->pkt_type = PACKET_HOST;
2032 if (pkt_dev->nfrags <= 0)
2033 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2035 int frags = pkt_dev->nfrags;
2038 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2040 if (frags > MAX_SKB_FRAGS)
2041 frags = MAX_SKB_FRAGS;
2042 if (datalen > frags * PAGE_SIZE) {
2043 skb_put(skb, datalen - frags * PAGE_SIZE);
2044 datalen = frags * PAGE_SIZE;
2048 while (datalen > 0) {
2049 struct page *page = alloc_pages(GFP_KERNEL, 0);
2050 skb_shinfo(skb)->frags[i].page = page;
2051 skb_shinfo(skb)->frags[i].page_offset = 0;
2052 skb_shinfo(skb)->frags[i].size =
2053 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2054 datalen -= skb_shinfo(skb)->frags[i].size;
2055 skb->len += skb_shinfo(skb)->frags[i].size;
2056 skb->data_len += skb_shinfo(skb)->frags[i].size;
2058 skb_shinfo(skb)->nr_frags = i;
2067 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2071 skb_shinfo(skb)->frags[i - 1].size -= rem;
2073 skb_shinfo(skb)->frags[i] =
2074 skb_shinfo(skb)->frags[i - 1];
2075 get_page(skb_shinfo(skb)->frags[i].page);
2076 skb_shinfo(skb)->frags[i].page =
2077 skb_shinfo(skb)->frags[i - 1].page;
2078 skb_shinfo(skb)->frags[i].page_offset +=
2079 skb_shinfo(skb)->frags[i - 1].size;
2080 skb_shinfo(skb)->frags[i].size = rem;
2082 skb_shinfo(skb)->nr_frags = i;
2086 /* Stamp the time, and sequence number, convert them to network byte order */
2089 struct timeval timestamp;
2091 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2092 pgh->seq_num = htonl(pkt_dev->seq_num);
2094 do_gettimeofday(×tamp);
2095 pgh->tv_sec = htonl(timestamp.tv_sec);
2096 pgh->tv_usec = htonl(timestamp.tv_usec);
2104 * scan_ip6, fmt_ip taken from dietlibc-0.21
2105 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2107 * Slightly modified for kernel.
2108 * Should be candidate for net/ipv4/utils.c
2112 static unsigned int scan_ip6(const char *s, char ip[16])
2115 unsigned int len = 0;
2118 unsigned int prefixlen = 0;
2119 unsigned int suffixlen = 0;
2122 for (i = 0; i < 16; i++)
2128 if (s[1] == ':') { /* Found "::", skip to part 2 */
2137 u = simple_strtoul(s, &tmp, 16);
2143 if (prefixlen == 12 && s[i] == '.') {
2145 /* the last 4 bytes may be written as IPv4 address */
2148 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2151 ip[prefixlen++] = (u >> 8);
2152 ip[prefixlen++] = (u & 255);
2155 if (prefixlen == 16)
2159 /* part 2, after "::" */
2166 } else if (suffixlen != 0)
2170 u = simple_strtol(s, &tmp, 16);
2178 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2180 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2186 suffix[suffixlen++] = (u >> 8);
2187 suffix[suffixlen++] = (u & 255);
2190 if (prefixlen + suffixlen == 16)
2193 for (i = 0; i < suffixlen; i++)
2194 ip[16 - suffixlen + i] = suffix[i];
2198 static char tohex(char hexdigit)
2200 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2203 static int fmt_xlong(char *s, unsigned int i)
2206 *s = tohex((i >> 12) & 0xf);
2207 if (s != bak || *s != '0')
2209 *s = tohex((i >> 8) & 0xf);
2210 if (s != bak || *s != '0')
2212 *s = tohex((i >> 4) & 0xf);
2213 if (s != bak || *s != '0')
2215 *s = tohex(i & 0xf);
2219 static unsigned int fmt_ip6(char *s, const char ip[16])
2224 unsigned int compressing;
2229 for (j = 0; j < 16; j += 2) {
2231 #ifdef V4MAPPEDPREFIX
2232 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2233 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2238 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2239 (unsigned long)(unsigned char)ip[j + 1];
2254 i = fmt_xlong(s, temp);
2271 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2272 struct pktgen_dev *pkt_dev)
2274 struct sk_buff *skb = NULL;
2276 struct udphdr *udph;
2278 struct ipv6hdr *iph;
2279 struct pktgen_hdr *pgh = NULL;
2281 /* Update any of the values, used when we're incrementing various
2284 mod_cur_headers(pkt_dev);
2286 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC);
2288 sprintf(pkt_dev->result, "No memory");
2292 skb_reserve(skb, 16);
2294 /* Reserve for ethernet and IP header */
2295 eth = (__u8 *) skb_push(skb, 14);
2296 iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
2297 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2299 memcpy(eth, pkt_dev->hh, 12);
2300 *(u16 *) & eth[12] = __constant_htons(ETH_P_IPV6);
2302 datalen = pkt_dev->cur_pkt_size - 14 - sizeof(struct ipv6hdr) - sizeof(struct udphdr); /* Eth + IPh + UDPh */
2304 if (datalen < sizeof(struct pktgen_hdr)) {
2305 datalen = sizeof(struct pktgen_hdr);
2306 if (net_ratelimit())
2307 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2311 udph->source = htons(pkt_dev->cur_udp_src);
2312 udph->dest = htons(pkt_dev->cur_udp_dst);
2313 udph->len = htons(datalen + sizeof(struct udphdr));
2314 udph->check = 0; /* No checksum */
2316 *(u32 *) iph = __constant_htonl(0x60000000); /* Version + flow */
2318 iph->hop_limit = 32;
2320 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2321 iph->nexthdr = IPPROTO_UDP;
2323 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2324 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2326 skb->mac.raw = ((u8 *) iph) - 14;
2327 skb->protocol = __constant_htons(ETH_P_IPV6);
2329 skb->pkt_type = PACKET_HOST;
2331 if (pkt_dev->nfrags <= 0)
2332 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2334 int frags = pkt_dev->nfrags;
2337 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2339 if (frags > MAX_SKB_FRAGS)
2340 frags = MAX_SKB_FRAGS;
2341 if (datalen > frags * PAGE_SIZE) {
2342 skb_put(skb, datalen - frags * PAGE_SIZE);
2343 datalen = frags * PAGE_SIZE;
2347 while (datalen > 0) {
2348 struct page *page = alloc_pages(GFP_KERNEL, 0);
2349 skb_shinfo(skb)->frags[i].page = page;
2350 skb_shinfo(skb)->frags[i].page_offset = 0;
2351 skb_shinfo(skb)->frags[i].size =
2352 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2353 datalen -= skb_shinfo(skb)->frags[i].size;
2354 skb->len += skb_shinfo(skb)->frags[i].size;
2355 skb->data_len += skb_shinfo(skb)->frags[i].size;
2357 skb_shinfo(skb)->nr_frags = i;
2366 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2370 skb_shinfo(skb)->frags[i - 1].size -= rem;
2372 skb_shinfo(skb)->frags[i] =
2373 skb_shinfo(skb)->frags[i - 1];
2374 get_page(skb_shinfo(skb)->frags[i].page);
2375 skb_shinfo(skb)->frags[i].page =
2376 skb_shinfo(skb)->frags[i - 1].page;
2377 skb_shinfo(skb)->frags[i].page_offset +=
2378 skb_shinfo(skb)->frags[i - 1].size;
2379 skb_shinfo(skb)->frags[i].size = rem;
2381 skb_shinfo(skb)->nr_frags = i;
2385 /* Stamp the time, and sequence number, convert them to network byte order */
2386 /* should we update cloned packets too ? */
2388 struct timeval timestamp;
2390 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2391 pgh->seq_num = htonl(pkt_dev->seq_num);
2393 do_gettimeofday(×tamp);
2394 pgh->tv_sec = htonl(timestamp.tv_sec);
2395 pgh->tv_usec = htonl(timestamp.tv_usec);
2402 static inline struct sk_buff *fill_packet(struct net_device *odev,
2403 struct pktgen_dev *pkt_dev)
2405 if (pkt_dev->flags & F_IPV6)
2406 return fill_packet_ipv6(odev, pkt_dev);
2408 return fill_packet_ipv4(odev, pkt_dev);
2411 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2413 pkt_dev->seq_num = 1;
2414 pkt_dev->idle_acc = 0;
2416 pkt_dev->tx_bytes = 0;
2417 pkt_dev->errors = 0;
2420 /* Set up structure for sending pkts, clear counters */
2422 static void pktgen_run(struct pktgen_thread *t)
2424 struct pktgen_dev *pkt_dev;
2427 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
2430 list_for_each_entry(pkt_dev, &t->if_list, list) {
2433 * setup odev and create initial packet.
2435 pktgen_setup_inject(pkt_dev);
2437 if (pkt_dev->odev) {
2438 pktgen_clear_counters(pkt_dev);
2439 pkt_dev->running = 1; /* Cranke yeself! */
2440 pkt_dev->skb = NULL;
2441 pkt_dev->started_at = getCurUs();
2442 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
2443 pkt_dev->next_tx_ns = 0;
2445 strcpy(pkt_dev->result, "Starting");
2448 strcpy(pkt_dev->result, "Error starting");
2452 t->control &= ~(T_STOP);
2455 static void pktgen_stop_all_threads_ifs(void)
2457 struct pktgen_thread *t;
2459 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads_ifs.\n"));
2463 list_for_each_entry(t, &pktgen_threads, th_list)
2464 t->control |= T_STOP;
2469 static int thread_is_running(struct pktgen_thread *t)
2471 struct pktgen_dev *pkt_dev;
2474 list_for_each_entry(pkt_dev, &t->if_list, list)
2475 if (pkt_dev->running) {
2482 static int pktgen_wait_thread_run(struct pktgen_thread *t)
2486 while (thread_is_running(t)) {
2490 msleep_interruptible(100);
2492 if (signal_pending(current))
2502 static int pktgen_wait_all_threads_run(void)
2504 struct pktgen_thread *t;
2509 list_for_each_entry(t, &pktgen_threads, th_list) {
2510 sig = pktgen_wait_thread_run(t);
2516 list_for_each_entry(t, &pktgen_threads, th_list)
2517 t->control |= (T_STOP);
2523 static void pktgen_run_all_threads(void)
2525 struct pktgen_thread *t;
2527 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2531 list_for_each_entry(t, &pktgen_threads, th_list)
2532 t->control |= (T_RUN);
2536 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2538 pktgen_wait_all_threads_run();
2541 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2543 __u64 total_us, bps, mbps, pps, idle;
2544 char *p = pkt_dev->result;
2546 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
2548 idle = pkt_dev->idle_acc;
2550 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2551 (unsigned long long)total_us,
2552 (unsigned long long)(total_us - idle),
2553 (unsigned long long)idle,
2554 (unsigned long long)pkt_dev->sofar,
2555 pkt_dev->cur_pkt_size, nr_frags);
2557 pps = pkt_dev->sofar * USEC_PER_SEC;
2559 while ((total_us >> 32) != 0) {
2564 do_div(pps, total_us);
2566 bps = pps * 8 * pkt_dev->cur_pkt_size;
2569 do_div(mbps, 1000000);
2570 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2571 (unsigned long long)pps,
2572 (unsigned long long)mbps,
2573 (unsigned long long)bps,
2574 (unsigned long long)pkt_dev->errors);
2577 /* Set stopped-at timer, remove from running list, do counters & statistics */
2579 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
2581 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
2583 if (!pkt_dev->running) {
2584 printk("pktgen: interface: %s is already stopped\n",
2589 pkt_dev->stopped_at = getCurUs();
2590 pkt_dev->running = 0;
2592 show_results(pkt_dev, nr_frags);
2597 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
2599 struct pktgen_dev *pkt_dev, *best = NULL;
2603 list_for_each_entry(pkt_dev, &t->if_list, list) {
2604 if (!pkt_dev->running)
2608 else if (pkt_dev->next_tx_us < best->next_tx_us)
2615 static void pktgen_stop(struct pktgen_thread *t)
2617 struct pktgen_dev *pkt_dev;
2619 PG_DEBUG(printk("pktgen: entering pktgen_stop\n"));
2623 list_for_each_entry(pkt_dev, &t->if_list, list) {
2624 pktgen_stop_device(pkt_dev);
2626 kfree_skb(pkt_dev->skb);
2628 pkt_dev->skb = NULL;
2635 * one of our devices needs to be removed - find it
2638 static void pktgen_rem_one_if(struct pktgen_thread *t)
2640 struct list_head *q, *n;
2641 struct pktgen_dev *cur;
2643 PG_DEBUG(printk("pktgen: entering pktgen_rem_one_if\n"));
2647 list_for_each_safe(q, n, &t->if_list) {
2648 cur = list_entry(q, struct pktgen_dev, list);
2650 if (!cur->removal_mark)
2654 kfree_skb(cur->skb);
2657 pktgen_remove_device(t, cur);
2665 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
2667 struct list_head *q, *n;
2668 struct pktgen_dev *cur;
2670 /* Remove all devices, free mem */
2672 PG_DEBUG(printk("pktgen: entering pktgen_rem_all_ifs\n"));
2675 list_for_each_safe(q, n, &t->if_list) {
2676 cur = list_entry(q, struct pktgen_dev, list);
2679 kfree_skb(cur->skb);
2682 pktgen_remove_device(t, cur);
2688 static void pktgen_rem_thread(struct pktgen_thread *t)
2690 /* Remove from the thread list */
2692 remove_proc_entry(t->name, pg_proc_dir);
2696 list_del(&t->th_list);
2701 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
2703 struct net_device *odev = NULL;
2704 __u64 idle_start = 0;
2707 odev = pkt_dev->odev;
2709 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
2713 if (now < pkt_dev->next_tx_us)
2714 spin(pkt_dev, pkt_dev->next_tx_us);
2716 /* This is max DELAY, this has special meaning of
2719 if (pkt_dev->delay_us == 0x7FFFFFFF) {
2720 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
2721 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
2726 if (netif_queue_stopped(odev) || need_resched()) {
2727 idle_start = getCurUs();
2729 if (!netif_running(odev)) {
2730 pktgen_stop_device(pkt_dev);
2732 kfree_skb(pkt_dev->skb);
2733 pkt_dev->skb = NULL;
2739 pkt_dev->idle_acc += getCurUs() - idle_start;
2741 if (netif_queue_stopped(odev)) {
2742 pkt_dev->next_tx_us = getCurUs(); /* TODO */
2743 pkt_dev->next_tx_ns = 0;
2744 goto out; /* Try the next interface */
2748 if (pkt_dev->last_ok || !pkt_dev->skb) {
2749 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
2750 || (!pkt_dev->skb)) {
2751 /* build a new pkt */
2753 kfree_skb(pkt_dev->skb);
2755 pkt_dev->skb = fill_packet(odev, pkt_dev);
2756 if (pkt_dev->skb == NULL) {
2757 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
2759 pkt_dev->clone_count--; /* back out increment, OOM */
2762 pkt_dev->allocated_skbs++;
2763 pkt_dev->clone_count = 0; /* reset counter */
2767 spin_lock_bh(&odev->xmit_lock);
2768 if (!netif_queue_stopped(odev)) {
2770 atomic_inc(&(pkt_dev->skb->users));
2772 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
2773 if (likely(ret == NETDEV_TX_OK)) {
2774 pkt_dev->last_ok = 1;
2777 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
2779 } else if (ret == NETDEV_TX_LOCKED
2780 && (odev->features & NETIF_F_LLTX)) {
2783 } else { /* Retry it next time */
2785 atomic_dec(&(pkt_dev->skb->users));
2787 if (debug && net_ratelimit())
2788 printk(KERN_INFO "pktgen: Hard xmit error\n");
2791 pkt_dev->last_ok = 0;
2794 pkt_dev->next_tx_us = getCurUs();
2795 pkt_dev->next_tx_ns = 0;
2797 pkt_dev->next_tx_us += pkt_dev->delay_us;
2798 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
2800 if (pkt_dev->next_tx_ns > 1000) {
2801 pkt_dev->next_tx_us++;
2802 pkt_dev->next_tx_ns -= 1000;
2806 else { /* Retry it next time */
2807 pkt_dev->last_ok = 0;
2808 pkt_dev->next_tx_us = getCurUs(); /* TODO */
2809 pkt_dev->next_tx_ns = 0;
2812 spin_unlock_bh(&odev->xmit_lock);
2814 /* If pkt_dev->count is zero, then run forever */
2815 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
2816 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
2817 idle_start = getCurUs();
2818 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
2819 if (signal_pending(current)) {
2824 pkt_dev->idle_acc += getCurUs() - idle_start;
2827 /* Done with this */
2828 pktgen_stop_device(pkt_dev);
2830 kfree_skb(pkt_dev->skb);
2831 pkt_dev->skb = NULL;
2837 * Main loop of the thread goes here
2840 static void pktgen_thread_worker(struct pktgen_thread *t)
2843 struct pktgen_dev *pkt_dev = NULL;
2846 u32 max_before_softirq;
2847 u32 tx_since_softirq = 0;
2849 daemonize("pktgen/%d", cpu);
2851 /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
2853 spin_lock_irq(¤t->sighand->siglock);
2854 tmpsig = current->blocked;
2855 siginitsetinv(¤t->blocked,
2856 sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGTERM));
2858 recalc_sigpending();
2859 spin_unlock_irq(¤t->sighand->siglock);
2861 /* Migrate to the right CPU */
2862 set_cpus_allowed(current, cpumask_of_cpu(cpu));
2863 if (smp_processor_id() != cpu)
2866 init_waitqueue_head(&t->queue);
2868 t->control &= ~(T_TERMINATE);
2869 t->control &= ~(T_RUN);
2870 t->control &= ~(T_STOP);
2871 t->control &= ~(T_REMDEVALL);
2872 t->control &= ~(T_REMDEV);
2874 t->pid = current->pid;
2876 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
2878 max_before_softirq = t->max_before_softirq;
2880 __set_current_state(TASK_INTERRUPTIBLE);
2885 __set_current_state(TASK_RUNNING);
2888 * Get next dev to xmit -- if any.
2891 pkt_dev = next_to_run(t);
2895 pktgen_xmit(pkt_dev);
2898 * We like to stay RUNNING but must also give
2899 * others fair share.
2902 tx_since_softirq += pkt_dev->last_ok;
2904 if (tx_since_softirq > max_before_softirq) {
2905 if (local_softirq_pending())
2907 tx_since_softirq = 0;
2910 prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
2911 schedule_timeout(HZ / 10);
2912 finish_wait(&(t->queue), &wait);
2916 * Back from sleep, either due to the timeout or signal.
2917 * We check if we have any "posted" work for us.
2920 if (t->control & T_TERMINATE || signal_pending(current))
2921 /* we received a request to terminate ourself */
2924 if (t->control & T_STOP) {
2926 t->control &= ~(T_STOP);
2929 if (t->control & T_RUN) {
2931 t->control &= ~(T_RUN);
2934 if (t->control & T_REMDEVALL) {
2935 pktgen_rem_all_ifs(t);
2936 t->control &= ~(T_REMDEVALL);
2939 if (t->control & T_REMDEV) {
2940 pktgen_rem_one_if(t);
2941 t->control &= ~(T_REMDEV);
2948 PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
2951 PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
2952 pktgen_rem_all_ifs(t);
2954 PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
2955 pktgen_rem_thread(t);
2960 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
2963 struct pktgen_dev *p, *pkt_dev = NULL;
2966 list_for_each_entry(p, &t->if_list, list)
2967 if (strncmp(p->ifname, ifname, IFNAMSIZ) == 0) {
2973 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev));
2978 * Adds a dev at front of if_list.
2981 static int add_dev_to_thread(struct pktgen_thread *t,
2982 struct pktgen_dev *pkt_dev)
2988 if (pkt_dev->pg_thread) {
2989 printk("pktgen: ERROR: already assigned to a thread.\n");
2994 list_add(&pkt_dev->list, &t->if_list);
2995 pkt_dev->pg_thread = t;
2996 pkt_dev->running = 0;
3003 /* Called under thread lock */
3005 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3007 struct pktgen_dev *pkt_dev;
3008 struct proc_dir_entry *pe;
3010 /* We don't allow a device to be on several threads */
3012 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3014 printk("pktgen: ERROR: interface already used.\n");
3018 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3022 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3023 if (pkt_dev->flows == NULL) {
3027 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3029 pkt_dev->removal_mark = 0;
3030 pkt_dev->min_pkt_size = ETH_ZLEN;
3031 pkt_dev->max_pkt_size = ETH_ZLEN;
3032 pkt_dev->nfrags = 0;
3033 pkt_dev->clone_skb = pg_clone_skb_d;
3034 pkt_dev->delay_us = pg_delay_d / 1000;
3035 pkt_dev->delay_ns = pg_delay_d % 1000;
3036 pkt_dev->count = pg_count_d;
3038 pkt_dev->udp_src_min = 9; /* sink port */
3039 pkt_dev->udp_src_max = 9;
3040 pkt_dev->udp_dst_min = 9;
3041 pkt_dev->udp_dst_max = 9;
3043 strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
3045 if (!pktgen_setup_dev(pkt_dev)) {
3046 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
3048 vfree(pkt_dev->flows);
3053 pe = create_proc_entry(ifname, 0600, pg_proc_dir);
3055 printk("pktgen: cannot create %s/%s procfs entry.\n",
3056 PG_PROC_DIR, ifname);
3058 vfree(pkt_dev->flows);
3062 pe->proc_fops = &pktgen_if_fops;
3065 return add_dev_to_thread(t, pkt_dev);
3068 static struct pktgen_thread *__init pktgen_find_thread(const char *name)
3070 struct pktgen_thread *t;
3074 list_for_each_entry(t, &pktgen_threads, th_list)
3075 if (strcmp(t->name, name) == 0) {
3084 static int __init pktgen_create_thread(const char *name, int cpu)
3087 struct pktgen_thread *t = NULL;
3088 struct proc_dir_entry *pe;
3090 if (strlen(name) > 31) {
3091 printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
3095 if (pktgen_find_thread(name)) {
3096 printk("pktgen: ERROR: thread: %s already exists\n", name);
3100 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3102 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3106 strcpy(t->name, name);
3107 spin_lock_init(&t->if_lock);
3110 pe = create_proc_entry(t->name, 0600, pg_proc_dir);
3112 printk("pktgen: cannot create %s/%s procfs entry.\n",
3113 PG_PROC_DIR, t->name);
3118 pe->proc_fops = &pktgen_thread_fops;
3121 INIT_LIST_HEAD(&t->if_list);
3123 list_add_tail(&t->th_list, &pktgen_threads);
3127 err = kernel_thread((void *)pktgen_thread_worker, (void *)t,
3128 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
3130 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3131 remove_proc_entry(t->name, pg_proc_dir);
3132 list_del(&t->th_list);
3141 * Removes a device from the thread if_list.
3143 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3144 struct pktgen_dev *pkt_dev)
3146 struct list_head *q, *n;
3147 struct pktgen_dev *p;
3149 list_for_each_safe(q, n, &t->if_list) {
3150 p = list_entry(q, struct pktgen_dev, list);
3156 static int pktgen_remove_device(struct pktgen_thread *t,
3157 struct pktgen_dev *pkt_dev)
3160 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
3162 if (pkt_dev->running) {
3163 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3164 pktgen_stop_device(pkt_dev);
3167 /* Dis-associate from the interface */
3169 if (pkt_dev->odev) {
3170 dev_put(pkt_dev->odev);
3171 pkt_dev->odev = NULL;
3174 /* And update the thread if_list */
3176 _rem_dev_from_if_list(t, pkt_dev);
3178 /* Clean up proc file system */
3180 remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
3183 vfree(pkt_dev->flows);
3188 static int __init pg_init(void)
3191 struct proc_dir_entry *pe;
3195 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3198 pg_proc_dir->owner = THIS_MODULE;
3200 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3202 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3204 proc_net_remove(PG_PROC_DIR);
3208 pe->proc_fops = &pktgen_fops;
3211 /* Register us to receive netdevice events */
3212 register_netdevice_notifier(&pktgen_notifier_block);
3214 for_each_online_cpu(cpu) {
3218 sprintf(buf, "kpktgend_%i", cpu);
3219 err = pktgen_create_thread(buf, cpu);
3221 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3225 if (list_empty(&pktgen_threads)) {
3226 printk("pktgen: ERROR: Initialization failed for all threads\n");
3227 unregister_netdevice_notifier(&pktgen_notifier_block);
3228 remove_proc_entry(PGCTRL, pg_proc_dir);
3229 proc_net_remove(PG_PROC_DIR);
3236 static void __exit pg_cleanup(void)
3238 struct pktgen_thread *t;
3239 struct list_head *q, *n;
3240 wait_queue_head_t queue;
3241 init_waitqueue_head(&queue);
3243 /* Stop all interfaces & threads */
3245 list_for_each_safe(q, n, &pktgen_threads) {
3246 t = list_entry(q, struct pktgen_thread, th_list);
3247 t->control |= (T_TERMINATE);
3249 wait_event_interruptible_timeout(queue, (t->removed == 1), HZ);
3252 /* Un-register us from receiving netdevice events */
3253 unregister_netdevice_notifier(&pktgen_notifier_block);
3255 /* Clean up proc file system */
3256 remove_proc_entry(PGCTRL, pg_proc_dir);
3257 proc_net_remove(PG_PROC_DIR);
3260 module_init(pg_init);
3261 module_exit(pg_cleanup);
3263 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3264 MODULE_DESCRIPTION("Packet Generator tool");
3265 MODULE_LICENSE("GPL");
3266 module_param(pg_count_d, int, 0);
3267 module_param(pg_delay_d, int, 0);
3268 module_param(pg_clone_skb_d, int, 0);
3269 module_param(debug, int, 0);