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 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
115 #include <linux/sys.h>
116 #include <linux/types.h>
117 #include <linux/module.h>
118 #include <linux/moduleparam.h>
119 #include <linux/kernel.h>
120 #include <linux/mutex.h>
121 #include <linux/sched.h>
122 #include <linux/slab.h>
123 #include <linux/vmalloc.h>
124 #include <linux/unistd.h>
125 #include <linux/string.h>
126 #include <linux/ptrace.h>
127 #include <linux/errno.h>
128 #include <linux/ioport.h>
129 #include <linux/interrupt.h>
130 #include <linux/capability.h>
131 #include <linux/freezer.h>
132 #include <linux/delay.h>
133 #include <linux/timer.h>
134 #include <linux/list.h>
135 #include <linux/init.h>
136 #include <linux/skbuff.h>
137 #include <linux/netdevice.h>
138 #include <linux/inet.h>
139 #include <linux/inetdevice.h>
140 #include <linux/rtnetlink.h>
141 #include <linux/if_arp.h>
142 #include <linux/if_vlan.h>
143 #include <linux/in.h>
144 #include <linux/ip.h>
145 #include <linux/ipv6.h>
146 #include <linux/udp.h>
147 #include <linux/proc_fs.h>
148 #include <linux/seq_file.h>
149 #include <linux/wait.h>
150 #include <linux/etherdevice.h>
151 #include <linux/kthread.h>
152 #include <net/checksum.h>
153 #include <net/ipv6.h>
154 #include <net/addrconf.h>
155 #include <asm/byteorder.h>
156 #include <linux/rcupdate.h>
157 #include <asm/bitops.h>
160 #include <asm/uaccess.h>
161 #include <asm/div64.h> /* do_div */
162 #include <asm/timex.h>
164 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
166 /* The buckets are exponential in 'width' */
167 #define LAT_BUCKETS_MAX 32
168 #define IP_NAME_SZ 32
169 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
170 #define MPLS_STACK_BOTTOM htonl(0x00000100)
172 /* Device flag bits */
173 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
174 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
175 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
176 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
177 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
178 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
179 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
180 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
181 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
182 #define F_VID_RND (1<<9) /* Random VLAN ID */
183 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
184 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
186 /* Thread control flag bits */
187 #define T_TERMINATE (1<<0)
188 #define T_STOP (1<<1) /* Stop run */
189 #define T_RUN (1<<2) /* Start run */
190 #define T_REMDEVALL (1<<3) /* Remove all devs */
191 #define T_REMDEV (1<<4) /* Remove one dev */
193 /* If lock -- can be removed after some work */
194 #define if_lock(t) spin_lock(&(t->if_lock));
195 #define if_unlock(t) spin_unlock(&(t->if_lock));
197 /* Used to help with determining the pkts on receive */
198 #define PKTGEN_MAGIC 0xbe9be955
199 #define PG_PROC_DIR "pktgen"
200 #define PGCTRL "pgctrl"
201 static struct proc_dir_entry *pg_proc_dir = NULL;
203 #define MAX_CFLOWS 65536
205 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
206 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
215 #define F_INIT (1<<0) /* flow has been initialized */
219 * Try to keep frequent/infrequent used vars. separated.
221 struct proc_dir_entry *entry; /* proc file */
222 struct pktgen_thread *pg_thread;/* the owner */
223 struct list_head list; /* Used for chaining in the thread's run-queue */
225 int running; /* if this changes to false, the test will stop */
227 /* If min != max, then we will either do a linear iteration, or
228 * we will do a random selection from within the range.
231 int removal_mark; /* non-zero => the device is marked for
232 * removal by worker thread */
234 int min_pkt_size; /* = ETH_ZLEN; */
235 int max_pkt_size; /* = ETH_ZLEN; */
236 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
238 __u32 delay_us; /* Default delay */
240 __u64 count; /* Default No packets to send */
241 __u64 sofar; /* How many pkts we've sent so far */
242 __u64 tx_bytes; /* How many bytes we've transmitted */
243 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
245 /* runtime counters relating to clone_skb */
246 __u64 next_tx_us; /* timestamp of when to tx next */
249 __u64 allocated_skbs;
251 int last_ok; /* Was last skb sent?
252 * Or a failed transmit of some sort? This will keep
253 * sequence numbers in order, for example.
255 __u64 started_at; /* micro-seconds */
256 __u64 stopped_at; /* micro-seconds */
257 __u64 idle_acc; /* micro-seconds */
260 int clone_skb; /* Use multiple SKBs during packet gen. If this number
261 * is greater than 1, then that many copies of the same
262 * packet will be sent before a new packet is allocated.
263 * For instance, if you want to send 1024 identical packets
264 * before creating a new packet, set clone_skb to 1024.
267 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
268 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
269 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
270 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
272 struct in6_addr in6_saddr;
273 struct in6_addr in6_daddr;
274 struct in6_addr cur_in6_daddr;
275 struct in6_addr cur_in6_saddr;
277 struct in6_addr min_in6_daddr;
278 struct in6_addr max_in6_daddr;
279 struct in6_addr min_in6_saddr;
280 struct in6_addr max_in6_saddr;
282 /* If we're doing ranges, random or incremental, then this
283 * defines the min/max for those ranges.
285 __be32 saddr_min; /* inclusive, source IP address */
286 __be32 saddr_max; /* exclusive, source IP address */
287 __be32 daddr_min; /* inclusive, dest IP address */
288 __be32 daddr_max; /* exclusive, dest IP address */
290 __u16 udp_src_min; /* inclusive, source UDP port */
291 __u16 udp_src_max; /* exclusive, source UDP port */
292 __u16 udp_dst_min; /* inclusive, dest UDP port */
293 __u16 udp_dst_max; /* exclusive, dest UDP port */
296 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
297 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
300 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
301 __be32 labels[MAX_MPLS_LABELS];
303 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
306 __u16 vlan_id; /* 0xffff means no vlan tag */
310 __u16 svlan_id; /* 0xffff means no svlan tag */
312 __u32 src_mac_count; /* How many MACs to iterate through */
313 __u32 dst_mac_count; /* How many MACs to iterate through */
315 unsigned char dst_mac[ETH_ALEN];
316 unsigned char src_mac[ETH_ALEN];
318 __u32 cur_dst_mac_offset;
319 __u32 cur_src_mac_offset;
328 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
330 We fill in SRC address later
331 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
335 __u16 pad; /* pad out the hh struct to an even 16 bytes */
337 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
338 * are transmitting the same one multiple times
340 struct net_device *odev; /* The out-going device. Note that the device should
341 * have it's pg_info pointer pointing back to this
342 * device. This will be set when the user specifies
343 * the out-going device name (not when the inject is
344 * started as it used to do.)
346 struct flow_state *flows;
347 unsigned cflows; /* Concurrent flows (config) */
348 unsigned lflow; /* Flow length (config) */
349 unsigned nflows; /* accumulated flows (stats) */
350 unsigned curfl; /* current sequenced flow (state)*/
362 struct pktgen_thread {
364 struct list_head if_list; /* All device here */
365 struct list_head th_list;
366 struct task_struct *tsk;
368 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
370 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
376 wait_queue_head_t queue;
382 /* This code works around the fact that do_div cannot handle two 64-bit
383 numbers, and regular 64-bit division doesn't work on x86 kernels.
389 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
390 * Function copied/adapted/optimized from:
392 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
394 * Copyright 1994, University of Cambridge Computer Laboratory
395 * All Rights Reserved.
398 static inline s64 divremdi3(s64 x, s64 y, int type)
400 u64 a = (x < 0) ? -x : x;
401 u64 b = (y < 0) ? -y : y;
421 if (PG_DIV == type) {
422 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
424 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
428 /* End of hacks to deal with 64-bit math on x86 */
430 /** Convert to milliseconds */
431 static inline __u64 tv_to_ms(const struct timeval *tv)
433 __u64 ms = tv->tv_usec / 1000;
434 ms += (__u64) tv->tv_sec * (__u64) 1000;
438 /** Convert to micro-seconds */
439 static inline __u64 tv_to_us(const struct timeval *tv)
441 __u64 us = tv->tv_usec;
442 us += (__u64) tv->tv_sec * (__u64) 1000000;
446 static inline __u64 pg_div(__u64 n, __u32 base)
450 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
455 static inline __u64 pg_div64(__u64 n, __u64 base)
459 * How do we know if the architecture we are running on
460 * supports division with 64 bit base?
463 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
467 tmp = divremdi3(n, base, PG_DIV);
472 static inline __u64 getCurMs(void)
475 do_gettimeofday(&tv);
476 return tv_to_ms(&tv);
479 static inline __u64 getCurUs(void)
482 do_gettimeofday(&tv);
483 return tv_to_us(&tv);
486 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
488 return tv_to_us(a) - tv_to_us(b);
491 /* old include end */
493 static char version[] __initdata = VERSION;
495 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
496 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
497 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
499 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
500 static void pktgen_run_all_threads(void);
501 static void pktgen_stop_all_threads_ifs(void);
502 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
503 static void pktgen_stop(struct pktgen_thread *t);
504 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
506 static unsigned int scan_ip6(const char *s, char ip[16]);
507 static unsigned int fmt_ip6(char *s, const char ip[16]);
509 /* Module parameters, defaults. */
510 static int pg_count_d = 1000; /* 1000 pkts by default */
511 static int pg_delay_d;
512 static int pg_clone_skb_d;
515 static DEFINE_MUTEX(pktgen_thread_lock);
516 static LIST_HEAD(pktgen_threads);
518 static struct notifier_block pktgen_notifier_block = {
519 .notifier_call = pktgen_device_event,
523 * /proc handling functions
527 static int pgctrl_show(struct seq_file *seq, void *v)
529 seq_puts(seq, VERSION);
533 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
534 size_t count, loff_t * ppos)
539 if (!capable(CAP_NET_ADMIN)) {
544 if (count > sizeof(data))
545 count = sizeof(data);
547 if (copy_from_user(data, buf, count)) {
551 data[count - 1] = 0; /* Make string */
553 if (!strcmp(data, "stop"))
554 pktgen_stop_all_threads_ifs();
556 else if (!strcmp(data, "start"))
557 pktgen_run_all_threads();
560 printk("pktgen: Unknown command: %s\n", data);
568 static int pgctrl_open(struct inode *inode, struct file *file)
570 return single_open(file, pgctrl_show, PDE(inode)->data);
573 static const struct file_operations pktgen_fops = {
574 .owner = THIS_MODULE,
578 .write = pgctrl_write,
579 .release = single_release,
582 static int pktgen_if_show(struct seq_file *seq, void *v)
585 struct pktgen_dev *pkt_dev = seq->private;
588 __u64 now = getCurUs();
591 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
592 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
593 pkt_dev->max_pkt_size);
596 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
598 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
599 pkt_dev->clone_skb, pkt_dev->odev->name);
601 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
604 if (pkt_dev->flags & F_IPV6) {
605 char b1[128], b2[128], b3[128];
606 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
607 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
608 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
610 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
613 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
614 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
615 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
617 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
622 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
623 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
626 seq_puts(seq, " src_mac: ");
628 if (is_zero_ether_addr(pkt_dev->src_mac))
629 for (i = 0; i < 6; i++)
630 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
633 for (i = 0; i < 6; i++)
634 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
637 seq_printf(seq, "dst_mac: ");
638 for (i = 0; i < 6; i++)
639 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
640 i == 5 ? "\n" : ":");
643 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
644 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
645 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
648 " src_mac_count: %d dst_mac_count: %d\n",
649 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
651 if (pkt_dev->nr_labels) {
653 seq_printf(seq, " mpls: ");
654 for (i = 0; i < pkt_dev->nr_labels; i++)
655 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
656 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
659 if (pkt_dev->vlan_id != 0xffff) {
660 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
661 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
664 if (pkt_dev->svlan_id != 0xffff) {
665 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
666 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
670 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
673 if (pkt_dev->traffic_class) {
674 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
677 seq_printf(seq, " Flags: ");
679 if (pkt_dev->flags & F_IPV6)
680 seq_printf(seq, "IPV6 ");
682 if (pkt_dev->flags & F_IPSRC_RND)
683 seq_printf(seq, "IPSRC_RND ");
685 if (pkt_dev->flags & F_IPDST_RND)
686 seq_printf(seq, "IPDST_RND ");
688 if (pkt_dev->flags & F_TXSIZE_RND)
689 seq_printf(seq, "TXSIZE_RND ");
691 if (pkt_dev->flags & F_UDPSRC_RND)
692 seq_printf(seq, "UDPSRC_RND ");
694 if (pkt_dev->flags & F_UDPDST_RND)
695 seq_printf(seq, "UDPDST_RND ");
697 if (pkt_dev->flags & F_MPLS_RND)
698 seq_printf(seq, "MPLS_RND ");
700 if (pkt_dev->cflows) {
701 if (pkt_dev->flags & F_FLOW_SEQ)
702 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
704 seq_printf(seq, "FLOW_RND ");
707 if (pkt_dev->flags & F_MACSRC_RND)
708 seq_printf(seq, "MACSRC_RND ");
710 if (pkt_dev->flags & F_MACDST_RND)
711 seq_printf(seq, "MACDST_RND ");
713 if (pkt_dev->flags & F_VID_RND)
714 seq_printf(seq, "VID_RND ");
716 if (pkt_dev->flags & F_SVID_RND)
717 seq_printf(seq, "SVID_RND ");
721 sa = pkt_dev->started_at;
722 stopped = pkt_dev->stopped_at;
723 if (pkt_dev->running)
724 stopped = now; /* not really stopped, more like last-running-at */
727 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
728 (unsigned long long)pkt_dev->sofar,
729 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
730 (unsigned long long)stopped,
731 (unsigned long long)pkt_dev->idle_acc);
734 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
735 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
736 pkt_dev->cur_src_mac_offset);
738 if (pkt_dev->flags & F_IPV6) {
739 char b1[128], b2[128];
740 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
741 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
742 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
744 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
745 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
747 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
748 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
750 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
752 if (pkt_dev->result[0])
753 seq_printf(seq, "Result: %s\n", pkt_dev->result);
755 seq_printf(seq, "Result: Idle\n");
761 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
766 for (; i < maxlen; i++) {
769 if (get_user(c, &user_buffer[i]))
771 if ((c >= '0') && (c <= '9'))
773 else if ((c >= 'a') && (c <= 'f'))
774 *num |= c - 'a' + 10;
775 else if ((c >= 'A') && (c <= 'F'))
776 *num |= c - 'A' + 10;
783 static int count_trail_chars(const char __user * user_buffer,
788 for (i = 0; i < maxlen; i++) {
790 if (get_user(c, &user_buffer[i]))
808 static unsigned long num_arg(const char __user * user_buffer,
809 unsigned long maxlen, unsigned long *num)
814 for (; i < maxlen; i++) {
816 if (get_user(c, &user_buffer[i]))
818 if ((c >= '0') && (c <= '9')) {
827 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
831 for (; i < maxlen; i++) {
833 if (get_user(c, &user_buffer[i]))
851 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
858 pkt_dev->nr_labels = 0;
861 len = hex32_arg(&buffer[i], 8, &tmp);
864 pkt_dev->labels[n] = htonl(tmp);
865 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
866 pkt_dev->flags |= F_MPLS_RND;
868 if (get_user(c, &buffer[i]))
872 if (n >= MAX_MPLS_LABELS)
876 pkt_dev->nr_labels = n;
880 static ssize_t pktgen_if_write(struct file *file,
881 const char __user * user_buffer, size_t count,
884 struct seq_file *seq = (struct seq_file *)file->private_data;
885 struct pktgen_dev *pkt_dev = seq->private;
887 char name[16], valstr[32];
888 unsigned long value = 0;
889 char *pg_result = NULL;
893 pg_result = &(pkt_dev->result[0]);
896 printk("pktgen: wrong command format\n");
901 tmp = count_trail_chars(&user_buffer[i], max);
903 printk("pktgen: illegal format\n");
908 /* Read variable name */
910 len = strn_len(&user_buffer[i], sizeof(name) - 1);
914 memset(name, 0, sizeof(name));
915 if (copy_from_user(name, &user_buffer[i], len))
920 len = count_trail_chars(&user_buffer[i], max);
928 if (copy_from_user(tb, user_buffer, count))
931 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
932 (unsigned long)count, tb);
935 if (!strcmp(name, "min_pkt_size")) {
936 len = num_arg(&user_buffer[i], 10, &value);
941 if (value < 14 + 20 + 8)
943 if (value != pkt_dev->min_pkt_size) {
944 pkt_dev->min_pkt_size = value;
945 pkt_dev->cur_pkt_size = value;
947 sprintf(pg_result, "OK: min_pkt_size=%u",
948 pkt_dev->min_pkt_size);
952 if (!strcmp(name, "max_pkt_size")) {
953 len = num_arg(&user_buffer[i], 10, &value);
958 if (value < 14 + 20 + 8)
960 if (value != pkt_dev->max_pkt_size) {
961 pkt_dev->max_pkt_size = value;
962 pkt_dev->cur_pkt_size = value;
964 sprintf(pg_result, "OK: max_pkt_size=%u",
965 pkt_dev->max_pkt_size);
969 /* Shortcut for min = max */
971 if (!strcmp(name, "pkt_size")) {
972 len = num_arg(&user_buffer[i], 10, &value);
977 if (value < 14 + 20 + 8)
979 if (value != pkt_dev->min_pkt_size) {
980 pkt_dev->min_pkt_size = value;
981 pkt_dev->max_pkt_size = value;
982 pkt_dev->cur_pkt_size = value;
984 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
988 if (!strcmp(name, "debug")) {
989 len = num_arg(&user_buffer[i], 10, &value);
995 sprintf(pg_result, "OK: debug=%u", debug);
999 if (!strcmp(name, "frags")) {
1000 len = num_arg(&user_buffer[i], 10, &value);
1005 pkt_dev->nfrags = value;
1006 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1009 if (!strcmp(name, "delay")) {
1010 len = num_arg(&user_buffer[i], 10, &value);
1015 if (value == 0x7FFFFFFF) {
1016 pkt_dev->delay_us = 0x7FFFFFFF;
1017 pkt_dev->delay_ns = 0;
1019 pkt_dev->delay_us = value / 1000;
1020 pkt_dev->delay_ns = value % 1000;
1022 sprintf(pg_result, "OK: delay=%u",
1023 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1026 if (!strcmp(name, "udp_src_min")) {
1027 len = num_arg(&user_buffer[i], 10, &value);
1032 if (value != pkt_dev->udp_src_min) {
1033 pkt_dev->udp_src_min = value;
1034 pkt_dev->cur_udp_src = value;
1036 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1039 if (!strcmp(name, "udp_dst_min")) {
1040 len = num_arg(&user_buffer[i], 10, &value);
1045 if (value != pkt_dev->udp_dst_min) {
1046 pkt_dev->udp_dst_min = value;
1047 pkt_dev->cur_udp_dst = value;
1049 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1052 if (!strcmp(name, "udp_src_max")) {
1053 len = num_arg(&user_buffer[i], 10, &value);
1058 if (value != pkt_dev->udp_src_max) {
1059 pkt_dev->udp_src_max = value;
1060 pkt_dev->cur_udp_src = value;
1062 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1065 if (!strcmp(name, "udp_dst_max")) {
1066 len = num_arg(&user_buffer[i], 10, &value);
1071 if (value != pkt_dev->udp_dst_max) {
1072 pkt_dev->udp_dst_max = value;
1073 pkt_dev->cur_udp_dst = value;
1075 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1078 if (!strcmp(name, "clone_skb")) {
1079 len = num_arg(&user_buffer[i], 10, &value);
1084 pkt_dev->clone_skb = value;
1086 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1089 if (!strcmp(name, "count")) {
1090 len = num_arg(&user_buffer[i], 10, &value);
1095 pkt_dev->count = value;
1096 sprintf(pg_result, "OK: count=%llu",
1097 (unsigned long long)pkt_dev->count);
1100 if (!strcmp(name, "src_mac_count")) {
1101 len = num_arg(&user_buffer[i], 10, &value);
1106 if (pkt_dev->src_mac_count != value) {
1107 pkt_dev->src_mac_count = value;
1108 pkt_dev->cur_src_mac_offset = 0;
1110 sprintf(pg_result, "OK: src_mac_count=%d",
1111 pkt_dev->src_mac_count);
1114 if (!strcmp(name, "dst_mac_count")) {
1115 len = num_arg(&user_buffer[i], 10, &value);
1120 if (pkt_dev->dst_mac_count != value) {
1121 pkt_dev->dst_mac_count = value;
1122 pkt_dev->cur_dst_mac_offset = 0;
1124 sprintf(pg_result, "OK: dst_mac_count=%d",
1125 pkt_dev->dst_mac_count);
1128 if (!strcmp(name, "flag")) {
1131 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1135 if (copy_from_user(f, &user_buffer[i], len))
1138 if (strcmp(f, "IPSRC_RND") == 0)
1139 pkt_dev->flags |= F_IPSRC_RND;
1141 else if (strcmp(f, "!IPSRC_RND") == 0)
1142 pkt_dev->flags &= ~F_IPSRC_RND;
1144 else if (strcmp(f, "TXSIZE_RND") == 0)
1145 pkt_dev->flags |= F_TXSIZE_RND;
1147 else if (strcmp(f, "!TXSIZE_RND") == 0)
1148 pkt_dev->flags &= ~F_TXSIZE_RND;
1150 else if (strcmp(f, "IPDST_RND") == 0)
1151 pkt_dev->flags |= F_IPDST_RND;
1153 else if (strcmp(f, "!IPDST_RND") == 0)
1154 pkt_dev->flags &= ~F_IPDST_RND;
1156 else if (strcmp(f, "UDPSRC_RND") == 0)
1157 pkt_dev->flags |= F_UDPSRC_RND;
1159 else if (strcmp(f, "!UDPSRC_RND") == 0)
1160 pkt_dev->flags &= ~F_UDPSRC_RND;
1162 else if (strcmp(f, "UDPDST_RND") == 0)
1163 pkt_dev->flags |= F_UDPDST_RND;
1165 else if (strcmp(f, "!UDPDST_RND") == 0)
1166 pkt_dev->flags &= ~F_UDPDST_RND;
1168 else if (strcmp(f, "MACSRC_RND") == 0)
1169 pkt_dev->flags |= F_MACSRC_RND;
1171 else if (strcmp(f, "!MACSRC_RND") == 0)
1172 pkt_dev->flags &= ~F_MACSRC_RND;
1174 else if (strcmp(f, "MACDST_RND") == 0)
1175 pkt_dev->flags |= F_MACDST_RND;
1177 else if (strcmp(f, "!MACDST_RND") == 0)
1178 pkt_dev->flags &= ~F_MACDST_RND;
1180 else if (strcmp(f, "MPLS_RND") == 0)
1181 pkt_dev->flags |= F_MPLS_RND;
1183 else if (strcmp(f, "!MPLS_RND") == 0)
1184 pkt_dev->flags &= ~F_MPLS_RND;
1186 else if (strcmp(f, "VID_RND") == 0)
1187 pkt_dev->flags |= F_VID_RND;
1189 else if (strcmp(f, "!VID_RND") == 0)
1190 pkt_dev->flags &= ~F_VID_RND;
1192 else if (strcmp(f, "SVID_RND") == 0)
1193 pkt_dev->flags |= F_SVID_RND;
1195 else if (strcmp(f, "!SVID_RND") == 0)
1196 pkt_dev->flags &= ~F_SVID_RND;
1198 else if (strcmp(f, "FLOW_SEQ") == 0)
1199 pkt_dev->flags |= F_FLOW_SEQ;
1201 else if (strcmp(f, "!IPV6") == 0)
1202 pkt_dev->flags &= ~F_IPV6;
1206 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1208 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1209 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ\n");
1212 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1215 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1216 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1221 if (copy_from_user(buf, &user_buffer[i], len))
1224 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1225 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1226 strncpy(pkt_dev->dst_min, buf, len);
1227 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1228 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1231 printk("pktgen: dst_min set to: %s\n",
1234 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1237 if (!strcmp(name, "dst_max")) {
1238 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1243 if (copy_from_user(buf, &user_buffer[i], len))
1247 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1248 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1249 strncpy(pkt_dev->dst_max, buf, len);
1250 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1251 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1254 printk("pktgen: dst_max set to: %s\n",
1257 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1260 if (!strcmp(name, "dst6")) {
1261 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1265 pkt_dev->flags |= F_IPV6;
1267 if (copy_from_user(buf, &user_buffer[i], len))
1271 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1272 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1274 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1277 printk("pktgen: dst6 set to: %s\n", buf);
1280 sprintf(pg_result, "OK: dst6=%s", buf);
1283 if (!strcmp(name, "dst6_min")) {
1284 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1288 pkt_dev->flags |= F_IPV6;
1290 if (copy_from_user(buf, &user_buffer[i], len))
1294 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1295 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1297 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1298 &pkt_dev->min_in6_daddr);
1300 printk("pktgen: dst6_min set to: %s\n", buf);
1303 sprintf(pg_result, "OK: dst6_min=%s", buf);
1306 if (!strcmp(name, "dst6_max")) {
1307 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1311 pkt_dev->flags |= F_IPV6;
1313 if (copy_from_user(buf, &user_buffer[i], len))
1317 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1318 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1321 printk("pktgen: dst6_max set to: %s\n", buf);
1324 sprintf(pg_result, "OK: dst6_max=%s", buf);
1327 if (!strcmp(name, "src6")) {
1328 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1332 pkt_dev->flags |= F_IPV6;
1334 if (copy_from_user(buf, &user_buffer[i], len))
1338 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1339 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1341 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1344 printk("pktgen: src6 set to: %s\n", buf);
1347 sprintf(pg_result, "OK: src6=%s", buf);
1350 if (!strcmp(name, "src_min")) {
1351 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1355 if (copy_from_user(buf, &user_buffer[i], len))
1358 if (strcmp(buf, pkt_dev->src_min) != 0) {
1359 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1360 strncpy(pkt_dev->src_min, buf, len);
1361 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1362 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1365 printk("pktgen: src_min set to: %s\n",
1368 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1371 if (!strcmp(name, "src_max")) {
1372 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1376 if (copy_from_user(buf, &user_buffer[i], len))
1379 if (strcmp(buf, pkt_dev->src_max) != 0) {
1380 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1381 strncpy(pkt_dev->src_max, buf, len);
1382 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1383 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1386 printk("pktgen: src_max set to: %s\n",
1389 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1392 if (!strcmp(name, "dst_mac")) {
1394 unsigned char old_dmac[ETH_ALEN];
1395 unsigned char *m = pkt_dev->dst_mac;
1396 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1398 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1402 memset(valstr, 0, sizeof(valstr));
1403 if (copy_from_user(valstr, &user_buffer[i], len))
1407 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1408 if (*v >= '0' && *v <= '9') {
1412 if (*v >= 'A' && *v <= 'F') {
1414 *m += *v - 'A' + 10;
1416 if (*v >= 'a' && *v <= 'f') {
1418 *m += *v - 'a' + 10;
1426 /* Set up Dest MAC */
1427 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1428 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1430 sprintf(pg_result, "OK: dstmac");
1433 if (!strcmp(name, "src_mac")) {
1435 unsigned char *m = pkt_dev->src_mac;
1437 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1441 memset(valstr, 0, sizeof(valstr));
1442 if (copy_from_user(valstr, &user_buffer[i], len))
1446 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1447 if (*v >= '0' && *v <= '9') {
1451 if (*v >= 'A' && *v <= 'F') {
1453 *m += *v - 'A' + 10;
1455 if (*v >= 'a' && *v <= 'f') {
1457 *m += *v - 'a' + 10;
1465 sprintf(pg_result, "OK: srcmac");
1469 if (!strcmp(name, "clear_counters")) {
1470 pktgen_clear_counters(pkt_dev);
1471 sprintf(pg_result, "OK: Clearing counters.\n");
1475 if (!strcmp(name, "flows")) {
1476 len = num_arg(&user_buffer[i], 10, &value);
1481 if (value > MAX_CFLOWS)
1484 pkt_dev->cflows = value;
1485 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1489 if (!strcmp(name, "flowlen")) {
1490 len = num_arg(&user_buffer[i], 10, &value);
1495 pkt_dev->lflow = value;
1496 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1500 if (!strcmp(name, "mpls")) {
1502 len = get_labels(&user_buffer[i], pkt_dev);
1503 if (len < 0) { return len; }
1505 offset = sprintf(pg_result, "OK: mpls=");
1506 for (n = 0; n < pkt_dev->nr_labels; n++)
1507 offset += sprintf(pg_result + offset,
1508 "%08x%s", ntohl(pkt_dev->labels[n]),
1509 n == pkt_dev->nr_labels-1 ? "" : ",");
1511 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1512 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1513 pkt_dev->svlan_id = 0xffff;
1516 printk("pktgen: VLAN/SVLAN auto turned off\n");
1521 if (!strcmp(name, "vlan_id")) {
1522 len = num_arg(&user_buffer[i], 4, &value);
1527 if (value <= 4095) {
1528 pkt_dev->vlan_id = value; /* turn on VLAN */
1531 printk("pktgen: VLAN turned on\n");
1533 if (debug && pkt_dev->nr_labels)
1534 printk("pktgen: MPLS auto turned off\n");
1536 pkt_dev->nr_labels = 0; /* turn off MPLS */
1537 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1539 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1540 pkt_dev->svlan_id = 0xffff;
1543 printk("pktgen: VLAN/SVLAN turned off\n");
1548 if (!strcmp(name, "vlan_p")) {
1549 len = num_arg(&user_buffer[i], 1, &value);
1554 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1555 pkt_dev->vlan_p = value;
1556 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1558 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1563 if (!strcmp(name, "vlan_cfi")) {
1564 len = num_arg(&user_buffer[i], 1, &value);
1569 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1570 pkt_dev->vlan_cfi = value;
1571 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1573 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1578 if (!strcmp(name, "svlan_id")) {
1579 len = num_arg(&user_buffer[i], 4, &value);
1584 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1585 pkt_dev->svlan_id = value; /* turn on SVLAN */
1588 printk("pktgen: SVLAN turned on\n");
1590 if (debug && pkt_dev->nr_labels)
1591 printk("pktgen: MPLS auto turned off\n");
1593 pkt_dev->nr_labels = 0; /* turn off MPLS */
1594 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1596 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1597 pkt_dev->svlan_id = 0xffff;
1600 printk("pktgen: VLAN/SVLAN turned off\n");
1605 if (!strcmp(name, "svlan_p")) {
1606 len = num_arg(&user_buffer[i], 1, &value);
1611 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1612 pkt_dev->svlan_p = value;
1613 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1615 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1620 if (!strcmp(name, "svlan_cfi")) {
1621 len = num_arg(&user_buffer[i], 1, &value);
1626 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1627 pkt_dev->svlan_cfi = value;
1628 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1630 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1635 if (!strcmp(name, "tos")) {
1636 __u32 tmp_value = 0;
1637 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1643 pkt_dev->tos = tmp_value;
1644 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1646 sprintf(pg_result, "ERROR: tos must be 00-ff");
1651 if (!strcmp(name, "traffic_class")) {
1652 __u32 tmp_value = 0;
1653 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1659 pkt_dev->traffic_class = tmp_value;
1660 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1662 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1667 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1671 static int pktgen_if_open(struct inode *inode, struct file *file)
1673 return single_open(file, pktgen_if_show, PDE(inode)->data);
1676 static const struct file_operations pktgen_if_fops = {
1677 .owner = THIS_MODULE,
1678 .open = pktgen_if_open,
1680 .llseek = seq_lseek,
1681 .write = pktgen_if_write,
1682 .release = single_release,
1685 static int pktgen_thread_show(struct seq_file *seq, void *v)
1687 struct pktgen_thread *t = seq->private;
1688 struct pktgen_dev *pkt_dev;
1692 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1693 t->tsk->comm, t->max_before_softirq);
1695 seq_printf(seq, "Running: ");
1698 list_for_each_entry(pkt_dev, &t->if_list, list)
1699 if (pkt_dev->running)
1700 seq_printf(seq, "%s ", pkt_dev->odev->name);
1702 seq_printf(seq, "\nStopped: ");
1704 list_for_each_entry(pkt_dev, &t->if_list, list)
1705 if (!pkt_dev->running)
1706 seq_printf(seq, "%s ", pkt_dev->odev->name);
1709 seq_printf(seq, "\nResult: %s\n", t->result);
1711 seq_printf(seq, "\nResult: NA\n");
1718 static ssize_t pktgen_thread_write(struct file *file,
1719 const char __user * user_buffer,
1720 size_t count, loff_t * offset)
1722 struct seq_file *seq = (struct seq_file *)file->private_data;
1723 struct pktgen_thread *t = seq->private;
1724 int i = 0, max, len, ret;
1727 unsigned long value = 0;
1730 // sprintf(pg_result, "Wrong command format");
1735 len = count_trail_chars(&user_buffer[i], max);
1741 /* Read variable name */
1743 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1747 memset(name, 0, sizeof(name));
1748 if (copy_from_user(name, &user_buffer[i], len))
1753 len = count_trail_chars(&user_buffer[i], max);
1760 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
1763 printk("pktgen: ERROR: No thread\n");
1768 pg_result = &(t->result[0]);
1770 if (!strcmp(name, "add_device")) {
1773 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1778 if (copy_from_user(f, &user_buffer[i], len))
1781 mutex_lock(&pktgen_thread_lock);
1782 pktgen_add_device(t, f);
1783 mutex_unlock(&pktgen_thread_lock);
1785 sprintf(pg_result, "OK: add_device=%s", f);
1789 if (!strcmp(name, "rem_device_all")) {
1790 mutex_lock(&pktgen_thread_lock);
1791 t->control |= T_REMDEVALL;
1792 mutex_unlock(&pktgen_thread_lock);
1793 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1795 sprintf(pg_result, "OK: rem_device_all");
1799 if (!strcmp(name, "max_before_softirq")) {
1800 len = num_arg(&user_buffer[i], 10, &value);
1801 mutex_lock(&pktgen_thread_lock);
1802 t->max_before_softirq = value;
1803 mutex_unlock(&pktgen_thread_lock);
1805 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1814 static int pktgen_thread_open(struct inode *inode, struct file *file)
1816 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1819 static const struct file_operations pktgen_thread_fops = {
1820 .owner = THIS_MODULE,
1821 .open = pktgen_thread_open,
1823 .llseek = seq_lseek,
1824 .write = pktgen_thread_write,
1825 .release = single_release,
1828 /* Think find or remove for NN */
1829 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1831 struct pktgen_thread *t;
1832 struct pktgen_dev *pkt_dev = NULL;
1834 list_for_each_entry(t, &pktgen_threads, th_list) {
1835 pkt_dev = pktgen_find_dev(t, ifname);
1839 pkt_dev->removal_mark = 1;
1840 t->control |= T_REMDEV;
1850 * mark a device for removal
1852 static void pktgen_mark_device(const char *ifname)
1854 struct pktgen_dev *pkt_dev = NULL;
1855 const int max_tries = 10, msec_per_try = 125;
1858 mutex_lock(&pktgen_thread_lock);
1859 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1863 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1864 if (pkt_dev == NULL)
1865 break; /* success */
1867 mutex_unlock(&pktgen_thread_lock);
1868 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1869 "to disappear....\n", ifname);
1870 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1871 mutex_lock(&pktgen_thread_lock);
1873 if (++i >= max_tries) {
1874 printk("pktgen_mark_device: timed out after waiting "
1875 "%d msec for device %s to be removed\n",
1876 msec_per_try * i, ifname);
1882 mutex_unlock(&pktgen_thread_lock);
1885 static void pktgen_change_name(struct net_device *dev)
1887 struct pktgen_thread *t;
1889 list_for_each_entry(t, &pktgen_threads, th_list) {
1890 struct pktgen_dev *pkt_dev;
1892 list_for_each_entry(pkt_dev, &t->if_list, list) {
1893 if (pkt_dev->odev != dev)
1896 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1898 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1900 if (!pkt_dev->entry)
1901 printk(KERN_ERR "pktgen: can't move proc "
1902 " entry for '%s'\n", dev->name);
1908 static int pktgen_device_event(struct notifier_block *unused,
1909 unsigned long event, void *ptr)
1911 struct net_device *dev = ptr;
1913 /* It is OK that we do not hold the group lock right now,
1914 * as we run under the RTNL lock.
1918 case NETDEV_CHANGENAME:
1919 pktgen_change_name(dev);
1922 case NETDEV_UNREGISTER:
1923 pktgen_mark_device(dev->name);
1930 /* Associate pktgen_dev with a device. */
1932 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1934 struct net_device *odev;
1937 /* Clean old setups */
1938 if (pkt_dev->odev) {
1939 dev_put(pkt_dev->odev);
1940 pkt_dev->odev = NULL;
1943 odev = dev_get_by_name(ifname);
1945 printk("pktgen: no such netdevice: \"%s\"\n", ifname);
1949 if (odev->type != ARPHRD_ETHER) {
1950 printk("pktgen: not an ethernet device: \"%s\"\n", ifname);
1952 } else if (!netif_running(odev)) {
1953 printk("pktgen: device is down: \"%s\"\n", ifname);
1956 pkt_dev->odev = odev;
1964 /* Read pkt_dev from the interface and set up internal pktgen_dev
1965 * structure to have the right information to create/send packets
1967 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1969 if (!pkt_dev->odev) {
1970 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1971 sprintf(pkt_dev->result,
1972 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1976 /* Default to the interface's mac if not explicitly set. */
1978 if (is_zero_ether_addr(pkt_dev->src_mac))
1979 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1981 /* Set up Dest MAC */
1982 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1984 /* Set up pkt size */
1985 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1987 if (pkt_dev->flags & F_IPV6) {
1989 * Skip this automatic address setting until locks or functions
1994 int i, set = 0, err = 1;
1995 struct inet6_dev *idev;
1997 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1998 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2006 * Use linklevel address if unconfigured.
2008 * use ipv6_get_lladdr if/when it's get exported
2012 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2013 struct inet6_ifaddr *ifp;
2015 read_lock_bh(&idev->lock);
2016 for (ifp = idev->addr_list; ifp;
2017 ifp = ifp->if_next) {
2018 if (ifp->scope == IFA_LINK
2020 flags & IFA_F_TENTATIVE)) {
2021 ipv6_addr_copy(&pkt_dev->
2028 read_unlock_bh(&idev->lock);
2032 printk("pktgen: ERROR: IPv6 link address not availble.\n");
2036 pkt_dev->saddr_min = 0;
2037 pkt_dev->saddr_max = 0;
2038 if (strlen(pkt_dev->src_min) == 0) {
2040 struct in_device *in_dev;
2043 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2045 if (in_dev->ifa_list) {
2046 pkt_dev->saddr_min =
2047 in_dev->ifa_list->ifa_address;
2048 pkt_dev->saddr_max = pkt_dev->saddr_min;
2053 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2054 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2057 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2058 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2060 /* Initialize current values. */
2061 pkt_dev->cur_dst_mac_offset = 0;
2062 pkt_dev->cur_src_mac_offset = 0;
2063 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2064 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2065 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2066 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2067 pkt_dev->nflows = 0;
2070 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2075 start = now = getCurUs();
2076 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2077 while (now < spin_until_us) {
2078 /* TODO: optimize sleeping behavior */
2079 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2080 schedule_timeout_interruptible(1);
2081 else if (spin_until_us - now > 100) {
2083 if (!pkt_dev->running)
2092 pkt_dev->idle_acc += now - start;
2095 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2097 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2098 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2099 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2102 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2105 if (pkt_dev->flows[flow].flags & F_INIT)
2111 static inline int f_pick(struct pktgen_dev *pkt_dev)
2113 int flow = pkt_dev->curfl;
2115 if (pkt_dev->flags & F_FLOW_SEQ) {
2116 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2118 pkt_dev->flows[flow].count = 0;
2119 pkt_dev->curfl += 1;
2120 if (pkt_dev->curfl >= pkt_dev->cflows)
2121 pkt_dev->curfl = 0; /*reset */
2124 flow = random32() % pkt_dev->cflows;
2126 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2127 pkt_dev->flows[flow].count = 0;
2130 return pkt_dev->curfl;
2133 /* Increment/randomize headers according to flags and current values
2134 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2136 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2142 if (pkt_dev->cflows)
2143 flow = f_pick(pkt_dev);
2145 /* Deal with source MAC */
2146 if (pkt_dev->src_mac_count > 1) {
2150 if (pkt_dev->flags & F_MACSRC_RND)
2151 mc = random32() % pkt_dev->src_mac_count;
2153 mc = pkt_dev->cur_src_mac_offset++;
2154 if (pkt_dev->cur_src_mac_offset >
2155 pkt_dev->src_mac_count)
2156 pkt_dev->cur_src_mac_offset = 0;
2159 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2160 pkt_dev->hh[11] = tmp;
2161 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2162 pkt_dev->hh[10] = tmp;
2163 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2164 pkt_dev->hh[9] = tmp;
2165 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2166 pkt_dev->hh[8] = tmp;
2167 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2168 pkt_dev->hh[7] = tmp;
2171 /* Deal with Destination MAC */
2172 if (pkt_dev->dst_mac_count > 1) {
2176 if (pkt_dev->flags & F_MACDST_RND)
2177 mc = random32() % pkt_dev->dst_mac_count;
2180 mc = pkt_dev->cur_dst_mac_offset++;
2181 if (pkt_dev->cur_dst_mac_offset >
2182 pkt_dev->dst_mac_count) {
2183 pkt_dev->cur_dst_mac_offset = 0;
2187 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2188 pkt_dev->hh[5] = tmp;
2189 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2190 pkt_dev->hh[4] = tmp;
2191 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2192 pkt_dev->hh[3] = tmp;
2193 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2194 pkt_dev->hh[2] = tmp;
2195 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2196 pkt_dev->hh[1] = tmp;
2199 if (pkt_dev->flags & F_MPLS_RND) {
2201 for (i = 0; i < pkt_dev->nr_labels; i++)
2202 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2203 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2204 ((__force __be32)random32() &
2208 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2209 pkt_dev->vlan_id = random32() & (4096-1);
2212 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2213 pkt_dev->svlan_id = random32() & (4096 - 1);
2216 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2217 if (pkt_dev->flags & F_UDPSRC_RND)
2218 pkt_dev->cur_udp_src = random32() %
2219 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2220 + pkt_dev->udp_src_min;
2223 pkt_dev->cur_udp_src++;
2224 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2225 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2229 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2230 if (pkt_dev->flags & F_UDPDST_RND) {
2231 pkt_dev->cur_udp_dst = random32() %
2232 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2233 + pkt_dev->udp_dst_min;
2235 pkt_dev->cur_udp_dst++;
2236 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2237 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2241 if (!(pkt_dev->flags & F_IPV6)) {
2243 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2247 if (pkt_dev->flags & F_IPSRC_RND)
2248 t = random32() % (imx - imn) + imn;
2250 t = ntohl(pkt_dev->cur_saddr);
2256 pkt_dev->cur_saddr = htonl(t);
2259 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2260 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2262 imn = ntohl(pkt_dev->daddr_min);
2263 imx = ntohl(pkt_dev->daddr_max);
2267 if (pkt_dev->flags & F_IPDST_RND) {
2269 t = random32() % (imx - imn) + imn;
2272 while (LOOPBACK(s) || MULTICAST(s)
2273 || BADCLASS(s) || ZERONET(s)
2274 || LOCAL_MCAST(s)) {
2275 t = random32() % (imx - imn) + imn;
2278 pkt_dev->cur_daddr = s;
2280 t = ntohl(pkt_dev->cur_daddr);
2285 pkt_dev->cur_daddr = htonl(t);
2288 if (pkt_dev->cflows) {
2289 pkt_dev->flows[flow].flags |= F_INIT;
2290 pkt_dev->flows[flow].cur_daddr =
2295 } else { /* IPV6 * */
2297 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2298 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2299 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2300 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2304 /* Only random destinations yet */
2306 for (i = 0; i < 4; i++) {
2307 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2308 (((__force __be32)random32() |
2309 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2310 pkt_dev->max_in6_daddr.s6_addr32[i]);
2315 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2317 if (pkt_dev->flags & F_TXSIZE_RND) {
2319 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2320 + pkt_dev->min_pkt_size;
2322 t = pkt_dev->cur_pkt_size + 1;
2323 if (t > pkt_dev->max_pkt_size)
2324 t = pkt_dev->min_pkt_size;
2326 pkt_dev->cur_pkt_size = t;
2329 pkt_dev->flows[flow].count++;
2332 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2335 for (i = 0; i < pkt_dev->nr_labels; i++) {
2336 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2339 *mpls |= MPLS_STACK_BOTTOM;
2342 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2345 return htons(id | (cfi << 12) | (prio << 13));
2348 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2349 struct pktgen_dev *pkt_dev)
2351 struct sk_buff *skb = NULL;
2353 struct udphdr *udph;
2356 struct pktgen_hdr *pgh = NULL;
2357 __be16 protocol = htons(ETH_P_IP);
2359 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2360 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2361 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2362 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2365 if (pkt_dev->nr_labels)
2366 protocol = htons(ETH_P_MPLS_UC);
2368 if (pkt_dev->vlan_id != 0xffff)
2369 protocol = htons(ETH_P_8021Q);
2371 /* Update any of the values, used when we're incrementing various
2374 mod_cur_headers(pkt_dev);
2376 datalen = (odev->hard_header_len + 16) & ~0xf;
2377 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2378 pkt_dev->pkt_overhead, GFP_ATOMIC);
2380 sprintf(pkt_dev->result, "No memory");
2384 skb_reserve(skb, datalen);
2386 /* Reserve for ethernet and IP header */
2387 eth = (__u8 *) skb_push(skb, 14);
2388 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2389 if (pkt_dev->nr_labels)
2390 mpls_push(mpls, pkt_dev);
2392 if (pkt_dev->vlan_id != 0xffff) {
2393 if (pkt_dev->svlan_id != 0xffff) {
2394 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2395 *svlan_tci = build_tci(pkt_dev->svlan_id,
2398 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2399 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2401 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2402 *vlan_tci = build_tci(pkt_dev->vlan_id,
2405 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2406 *vlan_encapsulated_proto = htons(ETH_P_IP);
2409 skb->network_header = skb->tail;
2410 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2411 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2414 udph = udp_hdr(skb);
2416 memcpy(eth, pkt_dev->hh, 12);
2417 *(__be16 *) & eth[12] = protocol;
2419 /* Eth + IPh + UDPh + mpls */
2420 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2421 pkt_dev->pkt_overhead;
2422 if (datalen < sizeof(struct pktgen_hdr))
2423 datalen = sizeof(struct pktgen_hdr);
2425 udph->source = htons(pkt_dev->cur_udp_src);
2426 udph->dest = htons(pkt_dev->cur_udp_dst);
2427 udph->len = htons(datalen + 8); /* DATA + udphdr */
2428 udph->check = 0; /* No checksum */
2433 iph->tos = pkt_dev->tos;
2434 iph->protocol = IPPROTO_UDP; /* UDP */
2435 iph->saddr = pkt_dev->cur_saddr;
2436 iph->daddr = pkt_dev->cur_daddr;
2438 iplen = 20 + 8 + datalen;
2439 iph->tot_len = htons(iplen);
2441 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2442 skb->protocol = protocol;
2443 skb->mac_header = (skb->network_header - ETH_HLEN -
2444 pkt_dev->pkt_overhead);
2446 skb->pkt_type = PACKET_HOST;
2448 if (pkt_dev->nfrags <= 0)
2449 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2451 int frags = pkt_dev->nfrags;
2454 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2456 if (frags > MAX_SKB_FRAGS)
2457 frags = MAX_SKB_FRAGS;
2458 if (datalen > frags * PAGE_SIZE) {
2459 skb_put(skb, datalen - frags * PAGE_SIZE);
2460 datalen = frags * PAGE_SIZE;
2464 while (datalen > 0) {
2465 struct page *page = alloc_pages(GFP_KERNEL, 0);
2466 skb_shinfo(skb)->frags[i].page = page;
2467 skb_shinfo(skb)->frags[i].page_offset = 0;
2468 skb_shinfo(skb)->frags[i].size =
2469 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2470 datalen -= skb_shinfo(skb)->frags[i].size;
2471 skb->len += skb_shinfo(skb)->frags[i].size;
2472 skb->data_len += skb_shinfo(skb)->frags[i].size;
2474 skb_shinfo(skb)->nr_frags = i;
2483 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2487 skb_shinfo(skb)->frags[i - 1].size -= rem;
2489 skb_shinfo(skb)->frags[i] =
2490 skb_shinfo(skb)->frags[i - 1];
2491 get_page(skb_shinfo(skb)->frags[i].page);
2492 skb_shinfo(skb)->frags[i].page =
2493 skb_shinfo(skb)->frags[i - 1].page;
2494 skb_shinfo(skb)->frags[i].page_offset +=
2495 skb_shinfo(skb)->frags[i - 1].size;
2496 skb_shinfo(skb)->frags[i].size = rem;
2498 skb_shinfo(skb)->nr_frags = i;
2502 /* Stamp the time, and sequence number, convert them to network byte order */
2505 struct timeval timestamp;
2507 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2508 pgh->seq_num = htonl(pkt_dev->seq_num);
2510 do_gettimeofday(×tamp);
2511 pgh->tv_sec = htonl(timestamp.tv_sec);
2512 pgh->tv_usec = htonl(timestamp.tv_usec);
2519 * scan_ip6, fmt_ip taken from dietlibc-0.21
2520 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2522 * Slightly modified for kernel.
2523 * Should be candidate for net/ipv4/utils.c
2527 static unsigned int scan_ip6(const char *s, char ip[16])
2530 unsigned int len = 0;
2533 unsigned int prefixlen = 0;
2534 unsigned int suffixlen = 0;
2537 for (i = 0; i < 16; i++)
2543 if (s[1] == ':') { /* Found "::", skip to part 2 */
2552 u = simple_strtoul(s, &tmp, 16);
2558 if (prefixlen == 12 && s[i] == '.') {
2560 /* the last 4 bytes may be written as IPv4 address */
2563 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2566 ip[prefixlen++] = (u >> 8);
2567 ip[prefixlen++] = (u & 255);
2570 if (prefixlen == 16)
2574 /* part 2, after "::" */
2581 } else if (suffixlen != 0)
2585 u = simple_strtol(s, &tmp, 16);
2593 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2595 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2601 suffix[suffixlen++] = (u >> 8);
2602 suffix[suffixlen++] = (u & 255);
2605 if (prefixlen + suffixlen == 16)
2608 for (i = 0; i < suffixlen; i++)
2609 ip[16 - suffixlen + i] = suffix[i];
2613 static char tohex(char hexdigit)
2615 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2618 static int fmt_xlong(char *s, unsigned int i)
2621 *s = tohex((i >> 12) & 0xf);
2622 if (s != bak || *s != '0')
2624 *s = tohex((i >> 8) & 0xf);
2625 if (s != bak || *s != '0')
2627 *s = tohex((i >> 4) & 0xf);
2628 if (s != bak || *s != '0')
2630 *s = tohex(i & 0xf);
2634 static unsigned int fmt_ip6(char *s, const char ip[16])
2639 unsigned int compressing;
2644 for (j = 0; j < 16; j += 2) {
2646 #ifdef V4MAPPEDPREFIX
2647 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2648 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2653 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2654 (unsigned long)(unsigned char)ip[j + 1];
2669 i = fmt_xlong(s, temp);
2686 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2687 struct pktgen_dev *pkt_dev)
2689 struct sk_buff *skb = NULL;
2691 struct udphdr *udph;
2693 struct ipv6hdr *iph;
2694 struct pktgen_hdr *pgh = NULL;
2695 __be16 protocol = htons(ETH_P_IPV6);
2697 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2698 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2699 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2700 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2702 if (pkt_dev->nr_labels)
2703 protocol = htons(ETH_P_MPLS_UC);
2705 if (pkt_dev->vlan_id != 0xffff)
2706 protocol = htons(ETH_P_8021Q);
2708 /* Update any of the values, used when we're incrementing various
2711 mod_cur_headers(pkt_dev);
2713 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2714 pkt_dev->pkt_overhead, GFP_ATOMIC);
2716 sprintf(pkt_dev->result, "No memory");
2720 skb_reserve(skb, 16);
2722 /* Reserve for ethernet and IP header */
2723 eth = (__u8 *) skb_push(skb, 14);
2724 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2725 if (pkt_dev->nr_labels)
2726 mpls_push(mpls, pkt_dev);
2728 if (pkt_dev->vlan_id != 0xffff) {
2729 if (pkt_dev->svlan_id != 0xffff) {
2730 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2731 *svlan_tci = build_tci(pkt_dev->svlan_id,
2734 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2735 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2737 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2738 *vlan_tci = build_tci(pkt_dev->vlan_id,
2741 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2742 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2745 skb->network_header = skb->tail;
2746 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2747 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2749 iph = ipv6_hdr(skb);
2750 udph = udp_hdr(skb);
2752 memcpy(eth, pkt_dev->hh, 12);
2753 *(__be16 *) & eth[12] = protocol;
2755 /* Eth + IPh + UDPh + mpls */
2756 datalen = pkt_dev->cur_pkt_size - 14 -
2757 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2758 pkt_dev->pkt_overhead;
2760 if (datalen < sizeof(struct pktgen_hdr)) {
2761 datalen = sizeof(struct pktgen_hdr);
2762 if (net_ratelimit())
2763 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2767 udph->source = htons(pkt_dev->cur_udp_src);
2768 udph->dest = htons(pkt_dev->cur_udp_dst);
2769 udph->len = htons(datalen + sizeof(struct udphdr));
2770 udph->check = 0; /* No checksum */
2772 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2774 if (pkt_dev->traffic_class) {
2775 /* Version + traffic class + flow (0) */
2776 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2779 iph->hop_limit = 32;
2781 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2782 iph->nexthdr = IPPROTO_UDP;
2784 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2785 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2787 skb->mac_header = (skb->network_header - ETH_HLEN -
2788 pkt_dev->pkt_overhead);
2789 skb->protocol = protocol;
2791 skb->pkt_type = PACKET_HOST;
2793 if (pkt_dev->nfrags <= 0)
2794 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2796 int frags = pkt_dev->nfrags;
2799 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2801 if (frags > MAX_SKB_FRAGS)
2802 frags = MAX_SKB_FRAGS;
2803 if (datalen > frags * PAGE_SIZE) {
2804 skb_put(skb, datalen - frags * PAGE_SIZE);
2805 datalen = frags * PAGE_SIZE;
2809 while (datalen > 0) {
2810 struct page *page = alloc_pages(GFP_KERNEL, 0);
2811 skb_shinfo(skb)->frags[i].page = page;
2812 skb_shinfo(skb)->frags[i].page_offset = 0;
2813 skb_shinfo(skb)->frags[i].size =
2814 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2815 datalen -= skb_shinfo(skb)->frags[i].size;
2816 skb->len += skb_shinfo(skb)->frags[i].size;
2817 skb->data_len += skb_shinfo(skb)->frags[i].size;
2819 skb_shinfo(skb)->nr_frags = i;
2828 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2832 skb_shinfo(skb)->frags[i - 1].size -= rem;
2834 skb_shinfo(skb)->frags[i] =
2835 skb_shinfo(skb)->frags[i - 1];
2836 get_page(skb_shinfo(skb)->frags[i].page);
2837 skb_shinfo(skb)->frags[i].page =
2838 skb_shinfo(skb)->frags[i - 1].page;
2839 skb_shinfo(skb)->frags[i].page_offset +=
2840 skb_shinfo(skb)->frags[i - 1].size;
2841 skb_shinfo(skb)->frags[i].size = rem;
2843 skb_shinfo(skb)->nr_frags = i;
2847 /* Stamp the time, and sequence number, convert them to network byte order */
2848 /* should we update cloned packets too ? */
2850 struct timeval timestamp;
2852 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2853 pgh->seq_num = htonl(pkt_dev->seq_num);
2855 do_gettimeofday(×tamp);
2856 pgh->tv_sec = htonl(timestamp.tv_sec);
2857 pgh->tv_usec = htonl(timestamp.tv_usec);
2859 /* pkt_dev->seq_num++; FF: you really mean this? */
2864 static inline struct sk_buff *fill_packet(struct net_device *odev,
2865 struct pktgen_dev *pkt_dev)
2867 if (pkt_dev->flags & F_IPV6)
2868 return fill_packet_ipv6(odev, pkt_dev);
2870 return fill_packet_ipv4(odev, pkt_dev);
2873 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2875 pkt_dev->seq_num = 1;
2876 pkt_dev->idle_acc = 0;
2878 pkt_dev->tx_bytes = 0;
2879 pkt_dev->errors = 0;
2882 /* Set up structure for sending pkts, clear counters */
2884 static void pktgen_run(struct pktgen_thread *t)
2886 struct pktgen_dev *pkt_dev;
2889 pr_debug("pktgen: entering pktgen_run. %p\n", t);
2892 list_for_each_entry(pkt_dev, &t->if_list, list) {
2895 * setup odev and create initial packet.
2897 pktgen_setup_inject(pkt_dev);
2899 if (pkt_dev->odev) {
2900 pktgen_clear_counters(pkt_dev);
2901 pkt_dev->running = 1; /* Cranke yeself! */
2902 pkt_dev->skb = NULL;
2903 pkt_dev->started_at = getCurUs();
2904 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
2905 pkt_dev->next_tx_ns = 0;
2906 set_pkt_overhead(pkt_dev);
2908 strcpy(pkt_dev->result, "Starting");
2911 strcpy(pkt_dev->result, "Error starting");
2915 t->control &= ~(T_STOP);
2918 static void pktgen_stop_all_threads_ifs(void)
2920 struct pktgen_thread *t;
2922 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
2924 mutex_lock(&pktgen_thread_lock);
2926 list_for_each_entry(t, &pktgen_threads, th_list)
2927 t->control |= T_STOP;
2929 mutex_unlock(&pktgen_thread_lock);
2932 static int thread_is_running(struct pktgen_thread *t)
2934 struct pktgen_dev *pkt_dev;
2937 list_for_each_entry(pkt_dev, &t->if_list, list)
2938 if (pkt_dev->running) {
2945 static int pktgen_wait_thread_run(struct pktgen_thread *t)
2949 while (thread_is_running(t)) {
2953 msleep_interruptible(100);
2955 if (signal_pending(current))
2965 static int pktgen_wait_all_threads_run(void)
2967 struct pktgen_thread *t;
2970 mutex_lock(&pktgen_thread_lock);
2972 list_for_each_entry(t, &pktgen_threads, th_list) {
2973 sig = pktgen_wait_thread_run(t);
2979 list_for_each_entry(t, &pktgen_threads, th_list)
2980 t->control |= (T_STOP);
2982 mutex_unlock(&pktgen_thread_lock);
2986 static void pktgen_run_all_threads(void)
2988 struct pktgen_thread *t;
2990 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
2992 mutex_lock(&pktgen_thread_lock);
2994 list_for_each_entry(t, &pktgen_threads, th_list)
2995 t->control |= (T_RUN);
2997 mutex_unlock(&pktgen_thread_lock);
2999 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3001 pktgen_wait_all_threads_run();
3004 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3006 __u64 total_us, bps, mbps, pps, idle;
3007 char *p = pkt_dev->result;
3009 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3011 idle = pkt_dev->idle_acc;
3013 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3014 (unsigned long long)total_us,
3015 (unsigned long long)(total_us - idle),
3016 (unsigned long long)idle,
3017 (unsigned long long)pkt_dev->sofar,
3018 pkt_dev->cur_pkt_size, nr_frags);
3020 pps = pkt_dev->sofar * USEC_PER_SEC;
3022 while ((total_us >> 32) != 0) {
3027 do_div(pps, total_us);
3029 bps = pps * 8 * pkt_dev->cur_pkt_size;
3032 do_div(mbps, 1000000);
3033 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3034 (unsigned long long)pps,
3035 (unsigned long long)mbps,
3036 (unsigned long long)bps,
3037 (unsigned long long)pkt_dev->errors);
3040 /* Set stopped-at timer, remove from running list, do counters & statistics */
3042 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3044 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3046 if (!pkt_dev->running) {
3047 printk("pktgen: interface: %s is already stopped\n",
3048 pkt_dev->odev->name);
3052 pkt_dev->stopped_at = getCurUs();
3053 pkt_dev->running = 0;
3055 show_results(pkt_dev, nr_frags);
3060 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3062 struct pktgen_dev *pkt_dev, *best = NULL;
3066 list_for_each_entry(pkt_dev, &t->if_list, list) {
3067 if (!pkt_dev->running)
3071 else if (pkt_dev->next_tx_us < best->next_tx_us)
3078 static void pktgen_stop(struct pktgen_thread *t)
3080 struct pktgen_dev *pkt_dev;
3082 pr_debug("pktgen: entering pktgen_stop\n");
3086 list_for_each_entry(pkt_dev, &t->if_list, list) {
3087 pktgen_stop_device(pkt_dev);
3089 kfree_skb(pkt_dev->skb);
3091 pkt_dev->skb = NULL;
3098 * one of our devices needs to be removed - find it
3101 static void pktgen_rem_one_if(struct pktgen_thread *t)
3103 struct list_head *q, *n;
3104 struct pktgen_dev *cur;
3106 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3110 list_for_each_safe(q, n, &t->if_list) {
3111 cur = list_entry(q, struct pktgen_dev, list);
3113 if (!cur->removal_mark)
3117 kfree_skb(cur->skb);
3120 pktgen_remove_device(t, cur);
3128 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3130 struct list_head *q, *n;
3131 struct pktgen_dev *cur;
3133 /* Remove all devices, free mem */
3135 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3138 list_for_each_safe(q, n, &t->if_list) {
3139 cur = list_entry(q, struct pktgen_dev, list);
3142 kfree_skb(cur->skb);
3145 pktgen_remove_device(t, cur);
3151 static void pktgen_rem_thread(struct pktgen_thread *t)
3153 /* Remove from the thread list */
3155 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3157 mutex_lock(&pktgen_thread_lock);
3159 list_del(&t->th_list);
3161 mutex_unlock(&pktgen_thread_lock);
3164 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3166 struct net_device *odev = NULL;
3167 __u64 idle_start = 0;
3170 odev = pkt_dev->odev;
3172 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3176 if (now < pkt_dev->next_tx_us)
3177 spin(pkt_dev, pkt_dev->next_tx_us);
3179 /* This is max DELAY, this has special meaning of
3182 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3183 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3184 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3189 if ((netif_queue_stopped(odev) ||
3190 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) ||
3192 idle_start = getCurUs();
3194 if (!netif_running(odev)) {
3195 pktgen_stop_device(pkt_dev);
3197 kfree_skb(pkt_dev->skb);
3198 pkt_dev->skb = NULL;
3204 pkt_dev->idle_acc += getCurUs() - idle_start;
3206 if (netif_queue_stopped(odev) ||
3207 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3208 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3209 pkt_dev->next_tx_ns = 0;
3210 goto out; /* Try the next interface */
3214 if (pkt_dev->last_ok || !pkt_dev->skb) {
3215 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3216 || (!pkt_dev->skb)) {
3217 /* build a new pkt */
3219 kfree_skb(pkt_dev->skb);
3221 pkt_dev->skb = fill_packet(odev, pkt_dev);
3222 if (pkt_dev->skb == NULL) {
3223 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3225 pkt_dev->clone_count--; /* back out increment, OOM */
3228 pkt_dev->allocated_skbs++;
3229 pkt_dev->clone_count = 0; /* reset counter */
3233 netif_tx_lock_bh(odev);
3234 if (!netif_queue_stopped(odev) &&
3235 !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3237 atomic_inc(&(pkt_dev->skb->users));
3239 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3240 if (likely(ret == NETDEV_TX_OK)) {
3241 pkt_dev->last_ok = 1;
3244 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3246 } else if (ret == NETDEV_TX_LOCKED
3247 && (odev->features & NETIF_F_LLTX)) {
3250 } else { /* Retry it next time */
3252 atomic_dec(&(pkt_dev->skb->users));
3254 if (debug && net_ratelimit())
3255 printk(KERN_INFO "pktgen: Hard xmit error\n");
3258 pkt_dev->last_ok = 0;
3261 pkt_dev->next_tx_us = getCurUs();
3262 pkt_dev->next_tx_ns = 0;
3264 pkt_dev->next_tx_us += pkt_dev->delay_us;
3265 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3267 if (pkt_dev->next_tx_ns > 1000) {
3268 pkt_dev->next_tx_us++;
3269 pkt_dev->next_tx_ns -= 1000;
3273 else { /* Retry it next time */
3274 pkt_dev->last_ok = 0;
3275 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3276 pkt_dev->next_tx_ns = 0;
3279 netif_tx_unlock_bh(odev);
3281 /* If pkt_dev->count is zero, then run forever */
3282 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3283 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3284 idle_start = getCurUs();
3285 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3286 if (signal_pending(current)) {
3291 pkt_dev->idle_acc += getCurUs() - idle_start;
3294 /* Done with this */
3295 pktgen_stop_device(pkt_dev);
3297 kfree_skb(pkt_dev->skb);
3298 pkt_dev->skb = NULL;
3304 * Main loop of the thread goes here
3307 static int pktgen_thread_worker(void *arg)
3310 struct pktgen_thread *t = arg;
3311 struct pktgen_dev *pkt_dev = NULL;
3313 u32 max_before_softirq;
3314 u32 tx_since_softirq = 0;
3316 BUG_ON(smp_processor_id() != cpu);
3318 init_waitqueue_head(&t->queue);
3320 t->pid = current->pid;
3322 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
3324 max_before_softirq = t->max_before_softirq;
3326 set_current_state(TASK_INTERRUPTIBLE);
3328 while (!kthread_should_stop()) {
3329 pkt_dev = next_to_run(t);
3332 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3334 prepare_to_wait(&(t->queue), &wait,
3335 TASK_INTERRUPTIBLE);
3336 schedule_timeout(HZ / 10);
3337 finish_wait(&(t->queue), &wait);
3340 __set_current_state(TASK_RUNNING);
3344 pktgen_xmit(pkt_dev);
3347 * We like to stay RUNNING but must also give
3348 * others fair share.
3351 tx_since_softirq += pkt_dev->last_ok;
3353 if (tx_since_softirq > max_before_softirq) {
3354 if (local_softirq_pending())
3356 tx_since_softirq = 0;
3360 if (t->control & T_STOP) {
3362 t->control &= ~(T_STOP);
3365 if (t->control & T_RUN) {
3367 t->control &= ~(T_RUN);
3370 if (t->control & T_REMDEVALL) {
3371 pktgen_rem_all_ifs(t);
3372 t->control &= ~(T_REMDEVALL);
3375 if (t->control & T_REMDEV) {
3376 pktgen_rem_one_if(t);
3377 t->control &= ~(T_REMDEV);
3382 set_current_state(TASK_INTERRUPTIBLE);
3385 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3388 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3389 pktgen_rem_all_ifs(t);
3391 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3392 pktgen_rem_thread(t);
3397 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3400 struct pktgen_dev *p, *pkt_dev = NULL;
3403 list_for_each_entry(p, &t->if_list, list)
3404 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3410 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3415 * Adds a dev at front of if_list.
3418 static int add_dev_to_thread(struct pktgen_thread *t,
3419 struct pktgen_dev *pkt_dev)
3425 if (pkt_dev->pg_thread) {
3426 printk("pktgen: ERROR: already assigned to a thread.\n");
3431 list_add(&pkt_dev->list, &t->if_list);
3432 pkt_dev->pg_thread = t;
3433 pkt_dev->running = 0;
3440 /* Called under thread lock */
3442 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3444 struct pktgen_dev *pkt_dev;
3447 /* We don't allow a device to be on several threads */
3449 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3451 printk("pktgen: ERROR: interface already used.\n");
3455 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3459 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3460 if (pkt_dev->flows == NULL) {
3464 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3466 pkt_dev->removal_mark = 0;
3467 pkt_dev->min_pkt_size = ETH_ZLEN;
3468 pkt_dev->max_pkt_size = ETH_ZLEN;
3469 pkt_dev->nfrags = 0;
3470 pkt_dev->clone_skb = pg_clone_skb_d;
3471 pkt_dev->delay_us = pg_delay_d / 1000;
3472 pkt_dev->delay_ns = pg_delay_d % 1000;
3473 pkt_dev->count = pg_count_d;
3475 pkt_dev->udp_src_min = 9; /* sink port */
3476 pkt_dev->udp_src_max = 9;
3477 pkt_dev->udp_dst_min = 9;
3478 pkt_dev->udp_dst_max = 9;
3480 pkt_dev->vlan_p = 0;
3481 pkt_dev->vlan_cfi = 0;
3482 pkt_dev->vlan_id = 0xffff;
3483 pkt_dev->svlan_p = 0;
3484 pkt_dev->svlan_cfi = 0;
3485 pkt_dev->svlan_id = 0xffff;
3487 err = pktgen_setup_dev(pkt_dev, ifname);
3491 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3492 if (!pkt_dev->entry) {
3493 printk("pktgen: cannot create %s/%s procfs entry.\n",
3494 PG_PROC_DIR, ifname);
3498 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3499 pkt_dev->entry->data = pkt_dev;
3501 return add_dev_to_thread(t, pkt_dev);
3503 dev_put(pkt_dev->odev);
3506 vfree(pkt_dev->flows);
3511 static int __init pktgen_create_thread(int cpu)
3513 struct pktgen_thread *t;
3514 struct proc_dir_entry *pe;
3515 struct task_struct *p;
3517 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3519 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3523 spin_lock_init(&t->if_lock);
3526 INIT_LIST_HEAD(&t->if_list);
3528 list_add_tail(&t->th_list, &pktgen_threads);
3530 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3532 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3533 list_del(&t->th_list);
3537 kthread_bind(p, cpu);
3540 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3542 printk("pktgen: cannot create %s/%s procfs entry.\n",
3543 PG_PROC_DIR, t->tsk->comm);
3545 list_del(&t->th_list);
3550 pe->proc_fops = &pktgen_thread_fops;
3559 * Removes a device from the thread if_list.
3561 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3562 struct pktgen_dev *pkt_dev)
3564 struct list_head *q, *n;
3565 struct pktgen_dev *p;
3567 list_for_each_safe(q, n, &t->if_list) {
3568 p = list_entry(q, struct pktgen_dev, list);
3574 static int pktgen_remove_device(struct pktgen_thread *t,
3575 struct pktgen_dev *pkt_dev)
3578 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3580 if (pkt_dev->running) {
3581 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3582 pktgen_stop_device(pkt_dev);
3585 /* Dis-associate from the interface */
3587 if (pkt_dev->odev) {
3588 dev_put(pkt_dev->odev);
3589 pkt_dev->odev = NULL;
3592 /* And update the thread if_list */
3594 _rem_dev_from_if_list(t, pkt_dev);
3597 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3600 vfree(pkt_dev->flows);
3605 static int __init pg_init(void)
3608 struct proc_dir_entry *pe;
3612 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3615 pg_proc_dir->owner = THIS_MODULE;
3617 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3619 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3621 proc_net_remove(PG_PROC_DIR);
3625 pe->proc_fops = &pktgen_fops;
3628 /* Register us to receive netdevice events */
3629 register_netdevice_notifier(&pktgen_notifier_block);
3631 for_each_online_cpu(cpu) {
3634 err = pktgen_create_thread(cpu);
3636 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3640 if (list_empty(&pktgen_threads)) {
3641 printk("pktgen: ERROR: Initialization failed for all threads\n");
3642 unregister_netdevice_notifier(&pktgen_notifier_block);
3643 remove_proc_entry(PGCTRL, pg_proc_dir);
3644 proc_net_remove(PG_PROC_DIR);
3651 static void __exit pg_cleanup(void)
3653 struct pktgen_thread *t;
3654 struct list_head *q, *n;
3655 wait_queue_head_t queue;
3656 init_waitqueue_head(&queue);
3658 /* Stop all interfaces & threads */
3660 list_for_each_safe(q, n, &pktgen_threads) {
3661 t = list_entry(q, struct pktgen_thread, th_list);
3662 kthread_stop(t->tsk);
3666 /* Un-register us from receiving netdevice events */
3667 unregister_netdevice_notifier(&pktgen_notifier_block);
3669 /* Clean up proc file system */
3670 remove_proc_entry(PGCTRL, pg_proc_dir);
3671 proc_net_remove(PG_PROC_DIR);
3674 module_init(pg_init);
3675 module_exit(pg_cleanup);
3677 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3678 MODULE_DESCRIPTION("Packet Generator tool");
3679 MODULE_LICENSE("GPL");
3680 module_param(pg_count_d, int, 0);
3681 module_param(pg_delay_d, int, 0);
3682 module_param(pg_clone_skb_d, int, 0);
3683 module_param(debug, int, 0);