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>
156 #include <net/xfrm.h>
158 #include <asm/byteorder.h>
159 #include <linux/rcupdate.h>
160 #include <asm/bitops.h>
163 #include <asm/uaccess.h>
164 #include <asm/div64.h> /* do_div */
165 #include <asm/timex.h>
167 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
169 /* The buckets are exponential in 'width' */
170 #define LAT_BUCKETS_MAX 32
171 #define IP_NAME_SZ 32
172 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
173 #define MPLS_STACK_BOTTOM htonl(0x00000100)
175 /* Device flag bits */
176 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
177 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
178 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
179 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
180 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
181 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
182 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
183 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
184 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
185 #define F_VID_RND (1<<9) /* Random VLAN ID */
186 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
187 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
188 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
190 /* Thread control flag bits */
191 #define T_TERMINATE (1<<0)
192 #define T_STOP (1<<1) /* Stop run */
193 #define T_RUN (1<<2) /* Start run */
194 #define T_REMDEVALL (1<<3) /* Remove all devs */
195 #define T_REMDEV (1<<4) /* Remove one dev */
197 /* If lock -- can be removed after some work */
198 #define if_lock(t) spin_lock(&(t->if_lock));
199 #define if_unlock(t) spin_unlock(&(t->if_lock));
201 /* Used to help with determining the pkts on receive */
202 #define PKTGEN_MAGIC 0xbe9be955
203 #define PG_PROC_DIR "pktgen"
204 #define PGCTRL "pgctrl"
205 static struct proc_dir_entry *pg_proc_dir = NULL;
207 #define MAX_CFLOWS 65536
209 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
210 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
216 struct xfrm_state *x;
222 #define F_INIT (1<<0) /* flow has been initialized */
226 * Try to keep frequent/infrequent used vars. separated.
228 struct proc_dir_entry *entry; /* proc file */
229 struct pktgen_thread *pg_thread;/* the owner */
230 struct list_head list; /* Used for chaining in the thread's run-queue */
232 int running; /* if this changes to false, the test will stop */
234 /* If min != max, then we will either do a linear iteration, or
235 * we will do a random selection from within the range.
238 int removal_mark; /* non-zero => the device is marked for
239 * removal by worker thread */
241 int min_pkt_size; /* = ETH_ZLEN; */
242 int max_pkt_size; /* = ETH_ZLEN; */
243 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
245 __u32 delay_us; /* Default delay */
247 __u64 count; /* Default No packets to send */
248 __u64 sofar; /* How many pkts we've sent so far */
249 __u64 tx_bytes; /* How many bytes we've transmitted */
250 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
252 /* runtime counters relating to clone_skb */
253 __u64 next_tx_us; /* timestamp of when to tx next */
256 __u64 allocated_skbs;
258 int last_ok; /* Was last skb sent?
259 * Or a failed transmit of some sort? This will keep
260 * sequence numbers in order, for example.
262 __u64 started_at; /* micro-seconds */
263 __u64 stopped_at; /* micro-seconds */
264 __u64 idle_acc; /* micro-seconds */
267 int clone_skb; /* Use multiple SKBs during packet gen. If this number
268 * is greater than 1, then that many copies of the same
269 * packet will be sent before a new packet is allocated.
270 * For instance, if you want to send 1024 identical packets
271 * before creating a new packet, set clone_skb to 1024.
274 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
275 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
276 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
277 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 struct in6_addr in6_saddr;
280 struct in6_addr in6_daddr;
281 struct in6_addr cur_in6_daddr;
282 struct in6_addr cur_in6_saddr;
284 struct in6_addr min_in6_daddr;
285 struct in6_addr max_in6_daddr;
286 struct in6_addr min_in6_saddr;
287 struct in6_addr max_in6_saddr;
289 /* If we're doing ranges, random or incremental, then this
290 * defines the min/max for those ranges.
292 __be32 saddr_min; /* inclusive, source IP address */
293 __be32 saddr_max; /* exclusive, source IP address */
294 __be32 daddr_min; /* inclusive, dest IP address */
295 __be32 daddr_max; /* exclusive, dest IP address */
297 __u16 udp_src_min; /* inclusive, source UDP port */
298 __u16 udp_src_max; /* exclusive, source UDP port */
299 __u16 udp_dst_min; /* inclusive, dest UDP port */
300 __u16 udp_dst_max; /* exclusive, dest UDP port */
303 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
304 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
307 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
308 __be32 labels[MAX_MPLS_LABELS];
310 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
313 __u16 vlan_id; /* 0xffff means no vlan tag */
317 __u16 svlan_id; /* 0xffff means no svlan tag */
319 __u32 src_mac_count; /* How many MACs to iterate through */
320 __u32 dst_mac_count; /* How many MACs to iterate through */
322 unsigned char dst_mac[ETH_ALEN];
323 unsigned char src_mac[ETH_ALEN];
325 __u32 cur_dst_mac_offset;
326 __u32 cur_src_mac_offset;
335 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
337 We fill in SRC address later
338 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
342 __u16 pad; /* pad out the hh struct to an even 16 bytes */
344 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
345 * are transmitting the same one multiple times
347 struct net_device *odev; /* The out-going device. Note that the device should
348 * have it's pg_info pointer pointing back to this
349 * device. This will be set when the user specifies
350 * the out-going device name (not when the inject is
351 * started as it used to do.)
353 struct flow_state *flows;
354 unsigned cflows; /* Concurrent flows (config) */
355 unsigned lflow; /* Flow length (config) */
356 unsigned nflows; /* accumulated flows (stats) */
357 unsigned curfl; /* current sequenced flow (state)*/
359 __u8 ipsmode; /* IPSEC mode (config) */
360 __u8 ipsproto; /* IPSEC type (config) */
372 struct pktgen_thread {
374 struct list_head if_list; /* All device here */
375 struct list_head th_list;
376 struct task_struct *tsk;
378 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
380 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
385 wait_queue_head_t queue;
391 /* This code works around the fact that do_div cannot handle two 64-bit
392 numbers, and regular 64-bit division doesn't work on x86 kernels.
398 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
399 * Function copied/adapted/optimized from:
401 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
403 * Copyright 1994, University of Cambridge Computer Laboratory
404 * All Rights Reserved.
407 static inline s64 divremdi3(s64 x, s64 y, int type)
409 u64 a = (x < 0) ? -x : x;
410 u64 b = (y < 0) ? -y : y;
430 if (PG_DIV == type) {
431 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
433 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
437 /* End of hacks to deal with 64-bit math on x86 */
439 /** Convert to milliseconds */
440 static inline __u64 tv_to_ms(const struct timeval *tv)
442 __u64 ms = tv->tv_usec / 1000;
443 ms += (__u64) tv->tv_sec * (__u64) 1000;
447 /** Convert to micro-seconds */
448 static inline __u64 tv_to_us(const struct timeval *tv)
450 __u64 us = tv->tv_usec;
451 us += (__u64) tv->tv_sec * (__u64) 1000000;
455 static inline __u64 pg_div(__u64 n, __u32 base)
459 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
464 static inline __u64 pg_div64(__u64 n, __u64 base)
468 * How do we know if the architecture we are running on
469 * supports division with 64 bit base?
472 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
476 tmp = divremdi3(n, base, PG_DIV);
481 static inline __u64 getCurMs(void)
484 do_gettimeofday(&tv);
485 return tv_to_ms(&tv);
488 static inline __u64 getCurUs(void)
491 do_gettimeofday(&tv);
492 return tv_to_us(&tv);
495 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
497 return tv_to_us(a) - tv_to_us(b);
500 /* old include end */
502 static char version[] __initdata = VERSION;
504 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
505 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
506 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
508 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
509 static void pktgen_run_all_threads(void);
510 static void pktgen_stop_all_threads_ifs(void);
511 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
512 static void pktgen_stop(struct pktgen_thread *t);
513 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
515 static unsigned int scan_ip6(const char *s, char ip[16]);
516 static unsigned int fmt_ip6(char *s, const char ip[16]);
518 /* Module parameters, defaults. */
519 static int pg_count_d = 1000; /* 1000 pkts by default */
520 static int pg_delay_d;
521 static int pg_clone_skb_d;
524 static DEFINE_MUTEX(pktgen_thread_lock);
525 static LIST_HEAD(pktgen_threads);
527 static struct notifier_block pktgen_notifier_block = {
528 .notifier_call = pktgen_device_event,
532 * /proc handling functions
536 static int pgctrl_show(struct seq_file *seq, void *v)
538 seq_puts(seq, VERSION);
542 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
543 size_t count, loff_t * ppos)
548 if (!capable(CAP_NET_ADMIN)) {
553 if (count > sizeof(data))
554 count = sizeof(data);
556 if (copy_from_user(data, buf, count)) {
560 data[count - 1] = 0; /* Make string */
562 if (!strcmp(data, "stop"))
563 pktgen_stop_all_threads_ifs();
565 else if (!strcmp(data, "start"))
566 pktgen_run_all_threads();
569 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
577 static int pgctrl_open(struct inode *inode, struct file *file)
579 return single_open(file, pgctrl_show, PDE(inode)->data);
582 static const struct file_operations pktgen_fops = {
583 .owner = THIS_MODULE,
587 .write = pgctrl_write,
588 .release = single_release,
591 static int pktgen_if_show(struct seq_file *seq, void *v)
594 struct pktgen_dev *pkt_dev = seq->private;
597 __u64 now = getCurUs();
600 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
601 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
602 pkt_dev->max_pkt_size);
605 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
607 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
608 pkt_dev->clone_skb, pkt_dev->odev->name);
610 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
613 if (pkt_dev->flags & F_IPV6) {
614 char b1[128], b2[128], b3[128];
615 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
616 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
617 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
619 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
622 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
623 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
624 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
626 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
631 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
632 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
635 seq_puts(seq, " src_mac: ");
637 if (is_zero_ether_addr(pkt_dev->src_mac))
638 for (i = 0; i < 6; i++)
639 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
642 for (i = 0; i < 6; i++)
643 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
646 seq_printf(seq, "dst_mac: ");
647 for (i = 0; i < 6; i++)
648 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
649 i == 5 ? "\n" : ":");
652 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
653 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
654 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
657 " src_mac_count: %d dst_mac_count: %d\n",
658 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
660 if (pkt_dev->nr_labels) {
662 seq_printf(seq, " mpls: ");
663 for (i = 0; i < pkt_dev->nr_labels; i++)
664 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
665 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
668 if (pkt_dev->vlan_id != 0xffff) {
669 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
670 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
673 if (pkt_dev->svlan_id != 0xffff) {
674 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
675 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
679 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
682 if (pkt_dev->traffic_class) {
683 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
686 seq_printf(seq, " Flags: ");
688 if (pkt_dev->flags & F_IPV6)
689 seq_printf(seq, "IPV6 ");
691 if (pkt_dev->flags & F_IPSRC_RND)
692 seq_printf(seq, "IPSRC_RND ");
694 if (pkt_dev->flags & F_IPDST_RND)
695 seq_printf(seq, "IPDST_RND ");
697 if (pkt_dev->flags & F_TXSIZE_RND)
698 seq_printf(seq, "TXSIZE_RND ");
700 if (pkt_dev->flags & F_UDPSRC_RND)
701 seq_printf(seq, "UDPSRC_RND ");
703 if (pkt_dev->flags & F_UDPDST_RND)
704 seq_printf(seq, "UDPDST_RND ");
706 if (pkt_dev->flags & F_MPLS_RND)
707 seq_printf(seq, "MPLS_RND ");
709 if (pkt_dev->cflows) {
710 if (pkt_dev->flags & F_FLOW_SEQ)
711 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
713 seq_printf(seq, "FLOW_RND ");
717 if (pkt_dev->flags & F_IPSEC_ON)
718 seq_printf(seq, "IPSEC ");
721 if (pkt_dev->flags & F_MACSRC_RND)
722 seq_printf(seq, "MACSRC_RND ");
724 if (pkt_dev->flags & F_MACDST_RND)
725 seq_printf(seq, "MACDST_RND ");
727 if (pkt_dev->flags & F_VID_RND)
728 seq_printf(seq, "VID_RND ");
730 if (pkt_dev->flags & F_SVID_RND)
731 seq_printf(seq, "SVID_RND ");
735 sa = pkt_dev->started_at;
736 stopped = pkt_dev->stopped_at;
737 if (pkt_dev->running)
738 stopped = now; /* not really stopped, more like last-running-at */
741 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
742 (unsigned long long)pkt_dev->sofar,
743 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
744 (unsigned long long)stopped,
745 (unsigned long long)pkt_dev->idle_acc);
748 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
749 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
750 pkt_dev->cur_src_mac_offset);
752 if (pkt_dev->flags & F_IPV6) {
753 char b1[128], b2[128];
754 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
755 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
756 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
758 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
759 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
761 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
762 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
764 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
766 if (pkt_dev->result[0])
767 seq_printf(seq, "Result: %s\n", pkt_dev->result);
769 seq_printf(seq, "Result: Idle\n");
775 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
780 for (; i < maxlen; i++) {
783 if (get_user(c, &user_buffer[i]))
785 if ((c >= '0') && (c <= '9'))
787 else if ((c >= 'a') && (c <= 'f'))
788 *num |= c - 'a' + 10;
789 else if ((c >= 'A') && (c <= 'F'))
790 *num |= c - 'A' + 10;
797 static int count_trail_chars(const char __user * user_buffer,
802 for (i = 0; i < maxlen; i++) {
804 if (get_user(c, &user_buffer[i]))
822 static unsigned long num_arg(const char __user * user_buffer,
823 unsigned long maxlen, unsigned long *num)
828 for (; i < maxlen; i++) {
830 if (get_user(c, &user_buffer[i]))
832 if ((c >= '0') && (c <= '9')) {
841 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
845 for (; i < maxlen; i++) {
847 if (get_user(c, &user_buffer[i]))
865 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
872 pkt_dev->nr_labels = 0;
875 len = hex32_arg(&buffer[i], 8, &tmp);
878 pkt_dev->labels[n] = htonl(tmp);
879 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
880 pkt_dev->flags |= F_MPLS_RND;
882 if (get_user(c, &buffer[i]))
886 if (n >= MAX_MPLS_LABELS)
890 pkt_dev->nr_labels = n;
894 static ssize_t pktgen_if_write(struct file *file,
895 const char __user * user_buffer, size_t count,
898 struct seq_file *seq = (struct seq_file *)file->private_data;
899 struct pktgen_dev *pkt_dev = seq->private;
901 char name[16], valstr[32];
902 unsigned long value = 0;
903 char *pg_result = NULL;
907 pg_result = &(pkt_dev->result[0]);
910 printk(KERN_WARNING "pktgen: wrong command format\n");
915 tmp = count_trail_chars(&user_buffer[i], max);
917 printk(KERN_WARNING "pktgen: illegal format\n");
922 /* Read variable name */
924 len = strn_len(&user_buffer[i], sizeof(name) - 1);
928 memset(name, 0, sizeof(name));
929 if (copy_from_user(name, &user_buffer[i], len))
934 len = count_trail_chars(&user_buffer[i], max);
942 if (copy_from_user(tb, user_buffer, count))
945 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
946 (unsigned long)count, tb);
949 if (!strcmp(name, "min_pkt_size")) {
950 len = num_arg(&user_buffer[i], 10, &value);
955 if (value < 14 + 20 + 8)
957 if (value != pkt_dev->min_pkt_size) {
958 pkt_dev->min_pkt_size = value;
959 pkt_dev->cur_pkt_size = value;
961 sprintf(pg_result, "OK: min_pkt_size=%u",
962 pkt_dev->min_pkt_size);
966 if (!strcmp(name, "max_pkt_size")) {
967 len = num_arg(&user_buffer[i], 10, &value);
972 if (value < 14 + 20 + 8)
974 if (value != pkt_dev->max_pkt_size) {
975 pkt_dev->max_pkt_size = value;
976 pkt_dev->cur_pkt_size = value;
978 sprintf(pg_result, "OK: max_pkt_size=%u",
979 pkt_dev->max_pkt_size);
983 /* Shortcut for min = max */
985 if (!strcmp(name, "pkt_size")) {
986 len = num_arg(&user_buffer[i], 10, &value);
991 if (value < 14 + 20 + 8)
993 if (value != pkt_dev->min_pkt_size) {
994 pkt_dev->min_pkt_size = value;
995 pkt_dev->max_pkt_size = value;
996 pkt_dev->cur_pkt_size = value;
998 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
1002 if (!strcmp(name, "debug")) {
1003 len = num_arg(&user_buffer[i], 10, &value);
1009 sprintf(pg_result, "OK: debug=%u", debug);
1013 if (!strcmp(name, "frags")) {
1014 len = num_arg(&user_buffer[i], 10, &value);
1019 pkt_dev->nfrags = value;
1020 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1023 if (!strcmp(name, "delay")) {
1024 len = num_arg(&user_buffer[i], 10, &value);
1029 if (value == 0x7FFFFFFF) {
1030 pkt_dev->delay_us = 0x7FFFFFFF;
1031 pkt_dev->delay_ns = 0;
1033 pkt_dev->delay_us = value / 1000;
1034 pkt_dev->delay_ns = value % 1000;
1036 sprintf(pg_result, "OK: delay=%u",
1037 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1040 if (!strcmp(name, "udp_src_min")) {
1041 len = num_arg(&user_buffer[i], 10, &value);
1046 if (value != pkt_dev->udp_src_min) {
1047 pkt_dev->udp_src_min = value;
1048 pkt_dev->cur_udp_src = value;
1050 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1053 if (!strcmp(name, "udp_dst_min")) {
1054 len = num_arg(&user_buffer[i], 10, &value);
1059 if (value != pkt_dev->udp_dst_min) {
1060 pkt_dev->udp_dst_min = value;
1061 pkt_dev->cur_udp_dst = value;
1063 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1066 if (!strcmp(name, "udp_src_max")) {
1067 len = num_arg(&user_buffer[i], 10, &value);
1072 if (value != pkt_dev->udp_src_max) {
1073 pkt_dev->udp_src_max = value;
1074 pkt_dev->cur_udp_src = value;
1076 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1079 if (!strcmp(name, "udp_dst_max")) {
1080 len = num_arg(&user_buffer[i], 10, &value);
1085 if (value != pkt_dev->udp_dst_max) {
1086 pkt_dev->udp_dst_max = value;
1087 pkt_dev->cur_udp_dst = value;
1089 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1092 if (!strcmp(name, "clone_skb")) {
1093 len = num_arg(&user_buffer[i], 10, &value);
1098 pkt_dev->clone_skb = value;
1100 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1103 if (!strcmp(name, "count")) {
1104 len = num_arg(&user_buffer[i], 10, &value);
1109 pkt_dev->count = value;
1110 sprintf(pg_result, "OK: count=%llu",
1111 (unsigned long long)pkt_dev->count);
1114 if (!strcmp(name, "src_mac_count")) {
1115 len = num_arg(&user_buffer[i], 10, &value);
1120 if (pkt_dev->src_mac_count != value) {
1121 pkt_dev->src_mac_count = value;
1122 pkt_dev->cur_src_mac_offset = 0;
1124 sprintf(pg_result, "OK: src_mac_count=%d",
1125 pkt_dev->src_mac_count);
1128 if (!strcmp(name, "dst_mac_count")) {
1129 len = num_arg(&user_buffer[i], 10, &value);
1134 if (pkt_dev->dst_mac_count != value) {
1135 pkt_dev->dst_mac_count = value;
1136 pkt_dev->cur_dst_mac_offset = 0;
1138 sprintf(pg_result, "OK: dst_mac_count=%d",
1139 pkt_dev->dst_mac_count);
1142 if (!strcmp(name, "flag")) {
1145 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1149 if (copy_from_user(f, &user_buffer[i], len))
1152 if (strcmp(f, "IPSRC_RND") == 0)
1153 pkt_dev->flags |= F_IPSRC_RND;
1155 else if (strcmp(f, "!IPSRC_RND") == 0)
1156 pkt_dev->flags &= ~F_IPSRC_RND;
1158 else if (strcmp(f, "TXSIZE_RND") == 0)
1159 pkt_dev->flags |= F_TXSIZE_RND;
1161 else if (strcmp(f, "!TXSIZE_RND") == 0)
1162 pkt_dev->flags &= ~F_TXSIZE_RND;
1164 else if (strcmp(f, "IPDST_RND") == 0)
1165 pkt_dev->flags |= F_IPDST_RND;
1167 else if (strcmp(f, "!IPDST_RND") == 0)
1168 pkt_dev->flags &= ~F_IPDST_RND;
1170 else if (strcmp(f, "UDPSRC_RND") == 0)
1171 pkt_dev->flags |= F_UDPSRC_RND;
1173 else if (strcmp(f, "!UDPSRC_RND") == 0)
1174 pkt_dev->flags &= ~F_UDPSRC_RND;
1176 else if (strcmp(f, "UDPDST_RND") == 0)
1177 pkt_dev->flags |= F_UDPDST_RND;
1179 else if (strcmp(f, "!UDPDST_RND") == 0)
1180 pkt_dev->flags &= ~F_UDPDST_RND;
1182 else if (strcmp(f, "MACSRC_RND") == 0)
1183 pkt_dev->flags |= F_MACSRC_RND;
1185 else if (strcmp(f, "!MACSRC_RND") == 0)
1186 pkt_dev->flags &= ~F_MACSRC_RND;
1188 else if (strcmp(f, "MACDST_RND") == 0)
1189 pkt_dev->flags |= F_MACDST_RND;
1191 else if (strcmp(f, "!MACDST_RND") == 0)
1192 pkt_dev->flags &= ~F_MACDST_RND;
1194 else if (strcmp(f, "MPLS_RND") == 0)
1195 pkt_dev->flags |= F_MPLS_RND;
1197 else if (strcmp(f, "!MPLS_RND") == 0)
1198 pkt_dev->flags &= ~F_MPLS_RND;
1200 else if (strcmp(f, "VID_RND") == 0)
1201 pkt_dev->flags |= F_VID_RND;
1203 else if (strcmp(f, "!VID_RND") == 0)
1204 pkt_dev->flags &= ~F_VID_RND;
1206 else if (strcmp(f, "SVID_RND") == 0)
1207 pkt_dev->flags |= F_SVID_RND;
1209 else if (strcmp(f, "!SVID_RND") == 0)
1210 pkt_dev->flags &= ~F_SVID_RND;
1212 else if (strcmp(f, "FLOW_SEQ") == 0)
1213 pkt_dev->flags |= F_FLOW_SEQ;
1216 else if (strcmp(f, "IPSEC") == 0)
1217 pkt_dev->flags |= F_IPSEC_ON;
1220 else if (strcmp(f, "!IPV6") == 0)
1221 pkt_dev->flags &= ~F_IPV6;
1225 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1227 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1228 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1231 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1234 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1235 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1240 if (copy_from_user(buf, &user_buffer[i], len))
1243 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1244 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1245 strncpy(pkt_dev->dst_min, buf, len);
1246 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1247 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1250 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1253 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1256 if (!strcmp(name, "dst_max")) {
1257 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1262 if (copy_from_user(buf, &user_buffer[i], len))
1266 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1267 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1268 strncpy(pkt_dev->dst_max, buf, len);
1269 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1270 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1273 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1276 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1279 if (!strcmp(name, "dst6")) {
1280 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1284 pkt_dev->flags |= F_IPV6;
1286 if (copy_from_user(buf, &user_buffer[i], len))
1290 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1291 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1293 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1296 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1299 sprintf(pg_result, "OK: dst6=%s", buf);
1302 if (!strcmp(name, "dst6_min")) {
1303 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1307 pkt_dev->flags |= F_IPV6;
1309 if (copy_from_user(buf, &user_buffer[i], len))
1313 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1314 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1316 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1317 &pkt_dev->min_in6_daddr);
1319 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1322 sprintf(pg_result, "OK: dst6_min=%s", buf);
1325 if (!strcmp(name, "dst6_max")) {
1326 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1330 pkt_dev->flags |= F_IPV6;
1332 if (copy_from_user(buf, &user_buffer[i], len))
1336 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1337 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1340 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1343 sprintf(pg_result, "OK: dst6_max=%s", buf);
1346 if (!strcmp(name, "src6")) {
1347 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1351 pkt_dev->flags |= F_IPV6;
1353 if (copy_from_user(buf, &user_buffer[i], len))
1357 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1358 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1360 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1363 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1366 sprintf(pg_result, "OK: src6=%s", buf);
1369 if (!strcmp(name, "src_min")) {
1370 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1374 if (copy_from_user(buf, &user_buffer[i], len))
1377 if (strcmp(buf, pkt_dev->src_min) != 0) {
1378 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1379 strncpy(pkt_dev->src_min, buf, len);
1380 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1381 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1384 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1387 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1390 if (!strcmp(name, "src_max")) {
1391 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1395 if (copy_from_user(buf, &user_buffer[i], len))
1398 if (strcmp(buf, pkt_dev->src_max) != 0) {
1399 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1400 strncpy(pkt_dev->src_max, buf, len);
1401 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1402 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1405 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1408 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1411 if (!strcmp(name, "dst_mac")) {
1413 unsigned char old_dmac[ETH_ALEN];
1414 unsigned char *m = pkt_dev->dst_mac;
1415 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1417 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1421 memset(valstr, 0, sizeof(valstr));
1422 if (copy_from_user(valstr, &user_buffer[i], len))
1426 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1427 if (*v >= '0' && *v <= '9') {
1431 if (*v >= 'A' && *v <= 'F') {
1433 *m += *v - 'A' + 10;
1435 if (*v >= 'a' && *v <= 'f') {
1437 *m += *v - 'a' + 10;
1445 /* Set up Dest MAC */
1446 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1447 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1449 sprintf(pg_result, "OK: dstmac");
1452 if (!strcmp(name, "src_mac")) {
1454 unsigned char *m = pkt_dev->src_mac;
1456 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1460 memset(valstr, 0, sizeof(valstr));
1461 if (copy_from_user(valstr, &user_buffer[i], len))
1465 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1466 if (*v >= '0' && *v <= '9') {
1470 if (*v >= 'A' && *v <= 'F') {
1472 *m += *v - 'A' + 10;
1474 if (*v >= 'a' && *v <= 'f') {
1476 *m += *v - 'a' + 10;
1484 sprintf(pg_result, "OK: srcmac");
1488 if (!strcmp(name, "clear_counters")) {
1489 pktgen_clear_counters(pkt_dev);
1490 sprintf(pg_result, "OK: Clearing counters.\n");
1494 if (!strcmp(name, "flows")) {
1495 len = num_arg(&user_buffer[i], 10, &value);
1500 if (value > MAX_CFLOWS)
1503 pkt_dev->cflows = value;
1504 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1508 if (!strcmp(name, "flowlen")) {
1509 len = num_arg(&user_buffer[i], 10, &value);
1514 pkt_dev->lflow = value;
1515 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1519 if (!strcmp(name, "mpls")) {
1521 len = get_labels(&user_buffer[i], pkt_dev);
1522 if (len < 0) { return len; }
1524 offset = sprintf(pg_result, "OK: mpls=");
1525 for (n = 0; n < pkt_dev->nr_labels; n++)
1526 offset += sprintf(pg_result + offset,
1527 "%08x%s", ntohl(pkt_dev->labels[n]),
1528 n == pkt_dev->nr_labels-1 ? "" : ",");
1530 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1531 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1532 pkt_dev->svlan_id = 0xffff;
1535 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1540 if (!strcmp(name, "vlan_id")) {
1541 len = num_arg(&user_buffer[i], 4, &value);
1546 if (value <= 4095) {
1547 pkt_dev->vlan_id = value; /* turn on VLAN */
1550 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1552 if (debug && pkt_dev->nr_labels)
1553 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1555 pkt_dev->nr_labels = 0; /* turn off MPLS */
1556 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1558 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1559 pkt_dev->svlan_id = 0xffff;
1562 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1567 if (!strcmp(name, "vlan_p")) {
1568 len = num_arg(&user_buffer[i], 1, &value);
1573 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1574 pkt_dev->vlan_p = value;
1575 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1577 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1582 if (!strcmp(name, "vlan_cfi")) {
1583 len = num_arg(&user_buffer[i], 1, &value);
1588 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1589 pkt_dev->vlan_cfi = value;
1590 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1592 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1597 if (!strcmp(name, "svlan_id")) {
1598 len = num_arg(&user_buffer[i], 4, &value);
1603 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1604 pkt_dev->svlan_id = value; /* turn on SVLAN */
1607 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1609 if (debug && pkt_dev->nr_labels)
1610 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1612 pkt_dev->nr_labels = 0; /* turn off MPLS */
1613 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1615 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1616 pkt_dev->svlan_id = 0xffff;
1619 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1624 if (!strcmp(name, "svlan_p")) {
1625 len = num_arg(&user_buffer[i], 1, &value);
1630 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1631 pkt_dev->svlan_p = value;
1632 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1634 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1639 if (!strcmp(name, "svlan_cfi")) {
1640 len = num_arg(&user_buffer[i], 1, &value);
1645 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1646 pkt_dev->svlan_cfi = value;
1647 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1649 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1654 if (!strcmp(name, "tos")) {
1655 __u32 tmp_value = 0;
1656 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1662 pkt_dev->tos = tmp_value;
1663 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1665 sprintf(pg_result, "ERROR: tos must be 00-ff");
1670 if (!strcmp(name, "traffic_class")) {
1671 __u32 tmp_value = 0;
1672 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1678 pkt_dev->traffic_class = tmp_value;
1679 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1681 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1686 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1690 static int pktgen_if_open(struct inode *inode, struct file *file)
1692 return single_open(file, pktgen_if_show, PDE(inode)->data);
1695 static const struct file_operations pktgen_if_fops = {
1696 .owner = THIS_MODULE,
1697 .open = pktgen_if_open,
1699 .llseek = seq_lseek,
1700 .write = pktgen_if_write,
1701 .release = single_release,
1704 static int pktgen_thread_show(struct seq_file *seq, void *v)
1706 struct pktgen_thread *t = seq->private;
1707 struct pktgen_dev *pkt_dev;
1711 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1712 t->tsk->comm, t->max_before_softirq);
1714 seq_printf(seq, "Running: ");
1717 list_for_each_entry(pkt_dev, &t->if_list, list)
1718 if (pkt_dev->running)
1719 seq_printf(seq, "%s ", pkt_dev->odev->name);
1721 seq_printf(seq, "\nStopped: ");
1723 list_for_each_entry(pkt_dev, &t->if_list, list)
1724 if (!pkt_dev->running)
1725 seq_printf(seq, "%s ", pkt_dev->odev->name);
1728 seq_printf(seq, "\nResult: %s\n", t->result);
1730 seq_printf(seq, "\nResult: NA\n");
1737 static ssize_t pktgen_thread_write(struct file *file,
1738 const char __user * user_buffer,
1739 size_t count, loff_t * offset)
1741 struct seq_file *seq = (struct seq_file *)file->private_data;
1742 struct pktgen_thread *t = seq->private;
1743 int i = 0, max, len, ret;
1746 unsigned long value = 0;
1749 // sprintf(pg_result, "Wrong command format");
1754 len = count_trail_chars(&user_buffer[i], max);
1760 /* Read variable name */
1762 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1766 memset(name, 0, sizeof(name));
1767 if (copy_from_user(name, &user_buffer[i], len))
1772 len = count_trail_chars(&user_buffer[i], max);
1779 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1780 name, (unsigned long)count);
1783 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1788 pg_result = &(t->result[0]);
1790 if (!strcmp(name, "add_device")) {
1793 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1798 if (copy_from_user(f, &user_buffer[i], len))
1801 mutex_lock(&pktgen_thread_lock);
1802 pktgen_add_device(t, f);
1803 mutex_unlock(&pktgen_thread_lock);
1805 sprintf(pg_result, "OK: add_device=%s", f);
1809 if (!strcmp(name, "rem_device_all")) {
1810 mutex_lock(&pktgen_thread_lock);
1811 t->control |= T_REMDEVALL;
1812 mutex_unlock(&pktgen_thread_lock);
1813 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1815 sprintf(pg_result, "OK: rem_device_all");
1819 if (!strcmp(name, "max_before_softirq")) {
1820 len = num_arg(&user_buffer[i], 10, &value);
1821 mutex_lock(&pktgen_thread_lock);
1822 t->max_before_softirq = value;
1823 mutex_unlock(&pktgen_thread_lock);
1825 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1834 static int pktgen_thread_open(struct inode *inode, struct file *file)
1836 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1839 static const struct file_operations pktgen_thread_fops = {
1840 .owner = THIS_MODULE,
1841 .open = pktgen_thread_open,
1843 .llseek = seq_lseek,
1844 .write = pktgen_thread_write,
1845 .release = single_release,
1848 /* Think find or remove for NN */
1849 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1851 struct pktgen_thread *t;
1852 struct pktgen_dev *pkt_dev = NULL;
1854 list_for_each_entry(t, &pktgen_threads, th_list) {
1855 pkt_dev = pktgen_find_dev(t, ifname);
1859 pkt_dev->removal_mark = 1;
1860 t->control |= T_REMDEV;
1870 * mark a device for removal
1872 static void pktgen_mark_device(const char *ifname)
1874 struct pktgen_dev *pkt_dev = NULL;
1875 const int max_tries = 10, msec_per_try = 125;
1878 mutex_lock(&pktgen_thread_lock);
1879 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1883 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1884 if (pkt_dev == NULL)
1885 break; /* success */
1887 mutex_unlock(&pktgen_thread_lock);
1888 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1889 "to disappear....\n", ifname);
1890 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1891 mutex_lock(&pktgen_thread_lock);
1893 if (++i >= max_tries) {
1894 printk(KERN_ERR "pktgen_mark_device: timed out after "
1895 "waiting %d msec for device %s to be removed\n",
1896 msec_per_try * i, ifname);
1902 mutex_unlock(&pktgen_thread_lock);
1905 static void pktgen_change_name(struct net_device *dev)
1907 struct pktgen_thread *t;
1909 list_for_each_entry(t, &pktgen_threads, th_list) {
1910 struct pktgen_dev *pkt_dev;
1912 list_for_each_entry(pkt_dev, &t->if_list, list) {
1913 if (pkt_dev->odev != dev)
1916 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1918 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1920 if (!pkt_dev->entry)
1921 printk(KERN_ERR "pktgen: can't move proc "
1922 " entry for '%s'\n", dev->name);
1928 static int pktgen_device_event(struct notifier_block *unused,
1929 unsigned long event, void *ptr)
1931 struct net_device *dev = ptr;
1933 /* It is OK that we do not hold the group lock right now,
1934 * as we run under the RTNL lock.
1938 case NETDEV_CHANGENAME:
1939 pktgen_change_name(dev);
1942 case NETDEV_UNREGISTER:
1943 pktgen_mark_device(dev->name);
1950 /* Associate pktgen_dev with a device. */
1952 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1954 struct net_device *odev;
1957 /* Clean old setups */
1958 if (pkt_dev->odev) {
1959 dev_put(pkt_dev->odev);
1960 pkt_dev->odev = NULL;
1963 odev = dev_get_by_name(ifname);
1965 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1969 if (odev->type != ARPHRD_ETHER) {
1970 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1972 } else if (!netif_running(odev)) {
1973 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1976 pkt_dev->odev = odev;
1984 /* Read pkt_dev from the interface and set up internal pktgen_dev
1985 * structure to have the right information to create/send packets
1987 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1989 if (!pkt_dev->odev) {
1990 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1992 sprintf(pkt_dev->result,
1993 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1997 /* Default to the interface's mac if not explicitly set. */
1999 if (is_zero_ether_addr(pkt_dev->src_mac))
2000 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2002 /* Set up Dest MAC */
2003 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2005 /* Set up pkt size */
2006 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2008 if (pkt_dev->flags & F_IPV6) {
2010 * Skip this automatic address setting until locks or functions
2015 int i, set = 0, err = 1;
2016 struct inet6_dev *idev;
2018 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2019 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2027 * Use linklevel address if unconfigured.
2029 * use ipv6_get_lladdr if/when it's get exported
2033 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2034 struct inet6_ifaddr *ifp;
2036 read_lock_bh(&idev->lock);
2037 for (ifp = idev->addr_list; ifp;
2038 ifp = ifp->if_next) {
2039 if (ifp->scope == IFA_LINK
2041 flags & IFA_F_TENTATIVE)) {
2042 ipv6_addr_copy(&pkt_dev->
2049 read_unlock_bh(&idev->lock);
2053 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2054 "address not availble.\n");
2058 pkt_dev->saddr_min = 0;
2059 pkt_dev->saddr_max = 0;
2060 if (strlen(pkt_dev->src_min) == 0) {
2062 struct in_device *in_dev;
2065 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2067 if (in_dev->ifa_list) {
2068 pkt_dev->saddr_min =
2069 in_dev->ifa_list->ifa_address;
2070 pkt_dev->saddr_max = pkt_dev->saddr_min;
2075 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2076 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2079 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2080 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2082 /* Initialize current values. */
2083 pkt_dev->cur_dst_mac_offset = 0;
2084 pkt_dev->cur_src_mac_offset = 0;
2085 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2086 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2087 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2088 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2089 pkt_dev->nflows = 0;
2092 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2097 start = now = getCurUs();
2098 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2099 while (now < spin_until_us) {
2100 /* TODO: optimize sleeping behavior */
2101 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2102 schedule_timeout_interruptible(1);
2103 else if (spin_until_us - now > 100) {
2105 if (!pkt_dev->running)
2114 pkt_dev->idle_acc += now - start;
2117 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2119 pkt_dev->pkt_overhead = 0;
2120 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2121 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2122 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2125 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2128 if (pkt_dev->flows[flow].flags & F_INIT)
2134 static inline int f_pick(struct pktgen_dev *pkt_dev)
2136 int flow = pkt_dev->curfl;
2138 if (pkt_dev->flags & F_FLOW_SEQ) {
2139 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2141 pkt_dev->flows[flow].count = 0;
2142 pkt_dev->curfl += 1;
2143 if (pkt_dev->curfl >= pkt_dev->cflows)
2144 pkt_dev->curfl = 0; /*reset */
2147 flow = random32() % pkt_dev->cflows;
2149 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2150 pkt_dev->flows[flow].count = 0;
2153 return pkt_dev->curfl;
2158 /* If there was already an IPSEC SA, we keep it as is, else
2159 * we go look for it ...
2161 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2163 struct xfrm_state *x = pkt_dev->flows[flow].x;
2165 /*slow path: we dont already have xfrm_state*/
2166 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2167 (xfrm_address_t *)&pkt_dev->cur_saddr,
2170 pkt_dev->ipsproto, 0);
2172 pkt_dev->flows[flow].x = x;
2173 set_pkt_overhead(pkt_dev);
2174 pkt_dev->pkt_overhead+=x->props.header_len;
2180 /* Increment/randomize headers according to flags and current values
2181 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2183 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2189 if (pkt_dev->cflows)
2190 flow = f_pick(pkt_dev);
2192 /* Deal with source MAC */
2193 if (pkt_dev->src_mac_count > 1) {
2197 if (pkt_dev->flags & F_MACSRC_RND)
2198 mc = random32() % pkt_dev->src_mac_count;
2200 mc = pkt_dev->cur_src_mac_offset++;
2201 if (pkt_dev->cur_src_mac_offset >
2202 pkt_dev->src_mac_count)
2203 pkt_dev->cur_src_mac_offset = 0;
2206 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2207 pkt_dev->hh[11] = tmp;
2208 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2209 pkt_dev->hh[10] = tmp;
2210 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2211 pkt_dev->hh[9] = tmp;
2212 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2213 pkt_dev->hh[8] = tmp;
2214 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2215 pkt_dev->hh[7] = tmp;
2218 /* Deal with Destination MAC */
2219 if (pkt_dev->dst_mac_count > 1) {
2223 if (pkt_dev->flags & F_MACDST_RND)
2224 mc = random32() % pkt_dev->dst_mac_count;
2227 mc = pkt_dev->cur_dst_mac_offset++;
2228 if (pkt_dev->cur_dst_mac_offset >
2229 pkt_dev->dst_mac_count) {
2230 pkt_dev->cur_dst_mac_offset = 0;
2234 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2235 pkt_dev->hh[5] = tmp;
2236 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2237 pkt_dev->hh[4] = tmp;
2238 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2239 pkt_dev->hh[3] = tmp;
2240 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2241 pkt_dev->hh[2] = tmp;
2242 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2243 pkt_dev->hh[1] = tmp;
2246 if (pkt_dev->flags & F_MPLS_RND) {
2248 for (i = 0; i < pkt_dev->nr_labels; i++)
2249 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2250 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2251 ((__force __be32)random32() &
2255 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2256 pkt_dev->vlan_id = random32() & (4096-1);
2259 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2260 pkt_dev->svlan_id = random32() & (4096 - 1);
2263 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2264 if (pkt_dev->flags & F_UDPSRC_RND)
2265 pkt_dev->cur_udp_src = random32() %
2266 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2267 + pkt_dev->udp_src_min;
2270 pkt_dev->cur_udp_src++;
2271 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2272 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2276 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2277 if (pkt_dev->flags & F_UDPDST_RND) {
2278 pkt_dev->cur_udp_dst = random32() %
2279 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2280 + pkt_dev->udp_dst_min;
2282 pkt_dev->cur_udp_dst++;
2283 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2284 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2288 if (!(pkt_dev->flags & F_IPV6)) {
2290 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2294 if (pkt_dev->flags & F_IPSRC_RND)
2295 t = random32() % (imx - imn) + imn;
2297 t = ntohl(pkt_dev->cur_saddr);
2303 pkt_dev->cur_saddr = htonl(t);
2306 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2307 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2309 imn = ntohl(pkt_dev->daddr_min);
2310 imx = ntohl(pkt_dev->daddr_max);
2314 if (pkt_dev->flags & F_IPDST_RND) {
2316 t = random32() % (imx - imn) + imn;
2319 while (LOOPBACK(s) || MULTICAST(s)
2320 || BADCLASS(s) || ZERONET(s)
2321 || LOCAL_MCAST(s)) {
2322 t = random32() % (imx - imn) + imn;
2325 pkt_dev->cur_daddr = s;
2327 t = ntohl(pkt_dev->cur_daddr);
2332 pkt_dev->cur_daddr = htonl(t);
2335 if (pkt_dev->cflows) {
2336 pkt_dev->flows[flow].flags |= F_INIT;
2337 pkt_dev->flows[flow].cur_daddr =
2340 if (pkt_dev->flags & F_IPSEC_ON)
2341 get_ipsec_sa(pkt_dev, flow);
2346 } else { /* IPV6 * */
2348 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2349 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2350 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2351 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2355 /* Only random destinations yet */
2357 for (i = 0; i < 4; i++) {
2358 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2359 (((__force __be32)random32() |
2360 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2361 pkt_dev->max_in6_daddr.s6_addr32[i]);
2366 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2368 if (pkt_dev->flags & F_TXSIZE_RND) {
2370 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2371 + pkt_dev->min_pkt_size;
2373 t = pkt_dev->cur_pkt_size + 1;
2374 if (t > pkt_dev->max_pkt_size)
2375 t = pkt_dev->min_pkt_size;
2377 pkt_dev->cur_pkt_size = t;
2380 pkt_dev->flows[flow].count++;
2385 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2387 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2393 /* XXX: we dont support tunnel mode for now until
2394 * we resolve the dst issue */
2395 if (x->props.mode != XFRM_MODE_TRANSPORT)
2398 spin_lock(&x->lock);
2401 err = x->mode->output(x, skb);
2404 err = x->type->output(x, skb);
2408 x->curlft.bytes +=skb->len;
2409 x->curlft.packets++;
2410 spin_unlock(&x->lock);
2413 spin_unlock(&x->lock);
2417 static inline void free_SAs(struct pktgen_dev *pkt_dev)
2419 if (pkt_dev->cflows) {
2420 /* let go of the SAs if we have them */
2422 for (; i < pkt_dev->nflows; i++){
2423 struct xfrm_state *x = pkt_dev->flows[i].x;
2426 pkt_dev->flows[i].x = NULL;
2432 static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2433 struct sk_buff *skb, __be16 protocol)
2435 if (pkt_dev->flags & F_IPSEC_ON) {
2436 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2441 nhead = x->props.header_len - skb_headroom(skb);
2443 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2445 printk(KERN_ERR "Error expanding "
2446 "ipsec packet %d\n",ret);
2451 /* ipsec is not expecting ll header */
2452 skb_pull(skb, ETH_HLEN);
2453 ret = pktgen_output_ipsec(skb, pkt_dev);
2455 printk(KERN_ERR "Error creating ipsec "
2461 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2462 memcpy(eth, pkt_dev->hh, 12);
2463 *(u16 *) & eth[12] = protocol;
2470 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2473 for (i = 0; i < pkt_dev->nr_labels; i++) {
2474 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2477 *mpls |= MPLS_STACK_BOTTOM;
2480 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2483 return htons(id | (cfi << 12) | (prio << 13));
2486 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2487 struct pktgen_dev *pkt_dev)
2489 struct sk_buff *skb = NULL;
2491 struct udphdr *udph;
2494 struct pktgen_hdr *pgh = NULL;
2495 __be16 protocol = htons(ETH_P_IP);
2497 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2498 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2499 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2500 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2503 if (pkt_dev->nr_labels)
2504 protocol = htons(ETH_P_MPLS_UC);
2506 if (pkt_dev->vlan_id != 0xffff)
2507 protocol = htons(ETH_P_8021Q);
2509 /* Update any of the values, used when we're incrementing various
2512 mod_cur_headers(pkt_dev);
2514 datalen = (odev->hard_header_len + 16) & ~0xf;
2515 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2516 pkt_dev->pkt_overhead, GFP_ATOMIC);
2518 sprintf(pkt_dev->result, "No memory");
2522 skb_reserve(skb, datalen);
2524 /* Reserve for ethernet and IP header */
2525 eth = (__u8 *) skb_push(skb, 14);
2526 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2527 if (pkt_dev->nr_labels)
2528 mpls_push(mpls, pkt_dev);
2530 if (pkt_dev->vlan_id != 0xffff) {
2531 if (pkt_dev->svlan_id != 0xffff) {
2532 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2533 *svlan_tci = build_tci(pkt_dev->svlan_id,
2536 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2537 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2539 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2540 *vlan_tci = build_tci(pkt_dev->vlan_id,
2543 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2544 *vlan_encapsulated_proto = htons(ETH_P_IP);
2547 skb->network_header = skb->tail;
2548 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2549 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2552 udph = udp_hdr(skb);
2554 memcpy(eth, pkt_dev->hh, 12);
2555 *(__be16 *) & eth[12] = protocol;
2557 /* Eth + IPh + UDPh + mpls */
2558 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2559 pkt_dev->pkt_overhead;
2560 if (datalen < sizeof(struct pktgen_hdr))
2561 datalen = sizeof(struct pktgen_hdr);
2563 udph->source = htons(pkt_dev->cur_udp_src);
2564 udph->dest = htons(pkt_dev->cur_udp_dst);
2565 udph->len = htons(datalen + 8); /* DATA + udphdr */
2566 udph->check = 0; /* No checksum */
2571 iph->tos = pkt_dev->tos;
2572 iph->protocol = IPPROTO_UDP; /* UDP */
2573 iph->saddr = pkt_dev->cur_saddr;
2574 iph->daddr = pkt_dev->cur_daddr;
2576 iplen = 20 + 8 + datalen;
2577 iph->tot_len = htons(iplen);
2579 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2580 skb->protocol = protocol;
2581 skb->mac_header = (skb->network_header - ETH_HLEN -
2582 pkt_dev->pkt_overhead);
2584 skb->pkt_type = PACKET_HOST;
2586 if (pkt_dev->nfrags <= 0)
2587 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2589 int frags = pkt_dev->nfrags;
2592 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2594 if (frags > MAX_SKB_FRAGS)
2595 frags = MAX_SKB_FRAGS;
2596 if (datalen > frags * PAGE_SIZE) {
2597 skb_put(skb, datalen - frags * PAGE_SIZE);
2598 datalen = frags * PAGE_SIZE;
2602 while (datalen > 0) {
2603 struct page *page = alloc_pages(GFP_KERNEL, 0);
2604 skb_shinfo(skb)->frags[i].page = page;
2605 skb_shinfo(skb)->frags[i].page_offset = 0;
2606 skb_shinfo(skb)->frags[i].size =
2607 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2608 datalen -= skb_shinfo(skb)->frags[i].size;
2609 skb->len += skb_shinfo(skb)->frags[i].size;
2610 skb->data_len += skb_shinfo(skb)->frags[i].size;
2612 skb_shinfo(skb)->nr_frags = i;
2621 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2625 skb_shinfo(skb)->frags[i - 1].size -= rem;
2627 skb_shinfo(skb)->frags[i] =
2628 skb_shinfo(skb)->frags[i - 1];
2629 get_page(skb_shinfo(skb)->frags[i].page);
2630 skb_shinfo(skb)->frags[i].page =
2631 skb_shinfo(skb)->frags[i - 1].page;
2632 skb_shinfo(skb)->frags[i].page_offset +=
2633 skb_shinfo(skb)->frags[i - 1].size;
2634 skb_shinfo(skb)->frags[i].size = rem;
2636 skb_shinfo(skb)->nr_frags = i;
2640 /* Stamp the time, and sequence number, convert them to network byte order */
2643 struct timeval timestamp;
2645 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2646 pgh->seq_num = htonl(pkt_dev->seq_num);
2648 do_gettimeofday(×tamp);
2649 pgh->tv_sec = htonl(timestamp.tv_sec);
2650 pgh->tv_usec = htonl(timestamp.tv_usec);
2654 if (!process_ipsec(pkt_dev, skb, protocol))
2662 * scan_ip6, fmt_ip taken from dietlibc-0.21
2663 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2665 * Slightly modified for kernel.
2666 * Should be candidate for net/ipv4/utils.c
2670 static unsigned int scan_ip6(const char *s, char ip[16])
2673 unsigned int len = 0;
2676 unsigned int prefixlen = 0;
2677 unsigned int suffixlen = 0;
2680 for (i = 0; i < 16; i++)
2686 if (s[1] == ':') { /* Found "::", skip to part 2 */
2695 u = simple_strtoul(s, &tmp, 16);
2701 if (prefixlen == 12 && s[i] == '.') {
2703 /* the last 4 bytes may be written as IPv4 address */
2706 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2709 ip[prefixlen++] = (u >> 8);
2710 ip[prefixlen++] = (u & 255);
2713 if (prefixlen == 16)
2717 /* part 2, after "::" */
2724 } else if (suffixlen != 0)
2728 u = simple_strtol(s, &tmp, 16);
2736 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2738 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2744 suffix[suffixlen++] = (u >> 8);
2745 suffix[suffixlen++] = (u & 255);
2748 if (prefixlen + suffixlen == 16)
2751 for (i = 0; i < suffixlen; i++)
2752 ip[16 - suffixlen + i] = suffix[i];
2756 static char tohex(char hexdigit)
2758 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2761 static int fmt_xlong(char *s, unsigned int i)
2764 *s = tohex((i >> 12) & 0xf);
2765 if (s != bak || *s != '0')
2767 *s = tohex((i >> 8) & 0xf);
2768 if (s != bak || *s != '0')
2770 *s = tohex((i >> 4) & 0xf);
2771 if (s != bak || *s != '0')
2773 *s = tohex(i & 0xf);
2777 static unsigned int fmt_ip6(char *s, const char ip[16])
2782 unsigned int compressing;
2787 for (j = 0; j < 16; j += 2) {
2789 #ifdef V4MAPPEDPREFIX
2790 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2791 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2796 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2797 (unsigned long)(unsigned char)ip[j + 1];
2812 i = fmt_xlong(s, temp);
2829 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2830 struct pktgen_dev *pkt_dev)
2832 struct sk_buff *skb = NULL;
2834 struct udphdr *udph;
2836 struct ipv6hdr *iph;
2837 struct pktgen_hdr *pgh = NULL;
2838 __be16 protocol = htons(ETH_P_IPV6);
2840 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2841 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2842 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2843 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2845 if (pkt_dev->nr_labels)
2846 protocol = htons(ETH_P_MPLS_UC);
2848 if (pkt_dev->vlan_id != 0xffff)
2849 protocol = htons(ETH_P_8021Q);
2851 /* Update any of the values, used when we're incrementing various
2854 mod_cur_headers(pkt_dev);
2856 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2857 pkt_dev->pkt_overhead, GFP_ATOMIC);
2859 sprintf(pkt_dev->result, "No memory");
2863 skb_reserve(skb, 16);
2865 /* Reserve for ethernet and IP header */
2866 eth = (__u8 *) skb_push(skb, 14);
2867 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2868 if (pkt_dev->nr_labels)
2869 mpls_push(mpls, pkt_dev);
2871 if (pkt_dev->vlan_id != 0xffff) {
2872 if (pkt_dev->svlan_id != 0xffff) {
2873 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2874 *svlan_tci = build_tci(pkt_dev->svlan_id,
2877 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2878 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2880 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2881 *vlan_tci = build_tci(pkt_dev->vlan_id,
2884 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2885 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2888 skb->network_header = skb->tail;
2889 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2890 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2892 iph = ipv6_hdr(skb);
2893 udph = udp_hdr(skb);
2895 memcpy(eth, pkt_dev->hh, 12);
2896 *(__be16 *) & eth[12] = protocol;
2898 /* Eth + IPh + UDPh + mpls */
2899 datalen = pkt_dev->cur_pkt_size - 14 -
2900 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2901 pkt_dev->pkt_overhead;
2903 if (datalen < sizeof(struct pktgen_hdr)) {
2904 datalen = sizeof(struct pktgen_hdr);
2905 if (net_ratelimit())
2906 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2910 udph->source = htons(pkt_dev->cur_udp_src);
2911 udph->dest = htons(pkt_dev->cur_udp_dst);
2912 udph->len = htons(datalen + sizeof(struct udphdr));
2913 udph->check = 0; /* No checksum */
2915 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2917 if (pkt_dev->traffic_class) {
2918 /* Version + traffic class + flow (0) */
2919 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2922 iph->hop_limit = 32;
2924 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2925 iph->nexthdr = IPPROTO_UDP;
2927 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2928 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2930 skb->mac_header = (skb->network_header - ETH_HLEN -
2931 pkt_dev->pkt_overhead);
2932 skb->protocol = protocol;
2934 skb->pkt_type = PACKET_HOST;
2936 if (pkt_dev->nfrags <= 0)
2937 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2939 int frags = pkt_dev->nfrags;
2942 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2944 if (frags > MAX_SKB_FRAGS)
2945 frags = MAX_SKB_FRAGS;
2946 if (datalen > frags * PAGE_SIZE) {
2947 skb_put(skb, datalen - frags * PAGE_SIZE);
2948 datalen = frags * PAGE_SIZE;
2952 while (datalen > 0) {
2953 struct page *page = alloc_pages(GFP_KERNEL, 0);
2954 skb_shinfo(skb)->frags[i].page = page;
2955 skb_shinfo(skb)->frags[i].page_offset = 0;
2956 skb_shinfo(skb)->frags[i].size =
2957 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2958 datalen -= skb_shinfo(skb)->frags[i].size;
2959 skb->len += skb_shinfo(skb)->frags[i].size;
2960 skb->data_len += skb_shinfo(skb)->frags[i].size;
2962 skb_shinfo(skb)->nr_frags = i;
2971 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2975 skb_shinfo(skb)->frags[i - 1].size -= rem;
2977 skb_shinfo(skb)->frags[i] =
2978 skb_shinfo(skb)->frags[i - 1];
2979 get_page(skb_shinfo(skb)->frags[i].page);
2980 skb_shinfo(skb)->frags[i].page =
2981 skb_shinfo(skb)->frags[i - 1].page;
2982 skb_shinfo(skb)->frags[i].page_offset +=
2983 skb_shinfo(skb)->frags[i - 1].size;
2984 skb_shinfo(skb)->frags[i].size = rem;
2986 skb_shinfo(skb)->nr_frags = i;
2990 /* Stamp the time, and sequence number, convert them to network byte order */
2991 /* should we update cloned packets too ? */
2993 struct timeval timestamp;
2995 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2996 pgh->seq_num = htonl(pkt_dev->seq_num);
2998 do_gettimeofday(×tamp);
2999 pgh->tv_sec = htonl(timestamp.tv_sec);
3000 pgh->tv_usec = htonl(timestamp.tv_usec);
3002 /* pkt_dev->seq_num++; FF: you really mean this? */
3007 static inline struct sk_buff *fill_packet(struct net_device *odev,
3008 struct pktgen_dev *pkt_dev)
3010 if (pkt_dev->flags & F_IPV6)
3011 return fill_packet_ipv6(odev, pkt_dev);
3013 return fill_packet_ipv4(odev, pkt_dev);
3016 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3018 pkt_dev->seq_num = 1;
3019 pkt_dev->idle_acc = 0;
3021 pkt_dev->tx_bytes = 0;
3022 pkt_dev->errors = 0;
3025 /* Set up structure for sending pkts, clear counters */
3027 static void pktgen_run(struct pktgen_thread *t)
3029 struct pktgen_dev *pkt_dev;
3032 pr_debug("pktgen: entering pktgen_run. %p\n", t);
3035 list_for_each_entry(pkt_dev, &t->if_list, list) {
3038 * setup odev and create initial packet.
3040 pktgen_setup_inject(pkt_dev);
3042 if (pkt_dev->odev) {
3043 pktgen_clear_counters(pkt_dev);
3044 pkt_dev->running = 1; /* Cranke yeself! */
3045 pkt_dev->skb = NULL;
3046 pkt_dev->started_at = getCurUs();
3047 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
3048 pkt_dev->next_tx_ns = 0;
3049 set_pkt_overhead(pkt_dev);
3051 strcpy(pkt_dev->result, "Starting");
3054 strcpy(pkt_dev->result, "Error starting");
3058 t->control &= ~(T_STOP);
3061 static void pktgen_stop_all_threads_ifs(void)
3063 struct pktgen_thread *t;
3065 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3067 mutex_lock(&pktgen_thread_lock);
3069 list_for_each_entry(t, &pktgen_threads, th_list)
3070 t->control |= T_STOP;
3072 mutex_unlock(&pktgen_thread_lock);
3075 static int thread_is_running(struct pktgen_thread *t)
3077 struct pktgen_dev *pkt_dev;
3080 list_for_each_entry(pkt_dev, &t->if_list, list)
3081 if (pkt_dev->running) {
3088 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3092 while (thread_is_running(t)) {
3096 msleep_interruptible(100);
3098 if (signal_pending(current))
3108 static int pktgen_wait_all_threads_run(void)
3110 struct pktgen_thread *t;
3113 mutex_lock(&pktgen_thread_lock);
3115 list_for_each_entry(t, &pktgen_threads, th_list) {
3116 sig = pktgen_wait_thread_run(t);
3122 list_for_each_entry(t, &pktgen_threads, th_list)
3123 t->control |= (T_STOP);
3125 mutex_unlock(&pktgen_thread_lock);
3129 static void pktgen_run_all_threads(void)
3131 struct pktgen_thread *t;
3133 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3135 mutex_lock(&pktgen_thread_lock);
3137 list_for_each_entry(t, &pktgen_threads, th_list)
3138 t->control |= (T_RUN);
3140 mutex_unlock(&pktgen_thread_lock);
3142 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3144 pktgen_wait_all_threads_run();
3147 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3149 __u64 total_us, bps, mbps, pps, idle;
3150 char *p = pkt_dev->result;
3152 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3154 idle = pkt_dev->idle_acc;
3156 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3157 (unsigned long long)total_us,
3158 (unsigned long long)(total_us - idle),
3159 (unsigned long long)idle,
3160 (unsigned long long)pkt_dev->sofar,
3161 pkt_dev->cur_pkt_size, nr_frags);
3163 pps = pkt_dev->sofar * USEC_PER_SEC;
3165 while ((total_us >> 32) != 0) {
3170 do_div(pps, total_us);
3172 bps = pps * 8 * pkt_dev->cur_pkt_size;
3175 do_div(mbps, 1000000);
3176 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3177 (unsigned long long)pps,
3178 (unsigned long long)mbps,
3179 (unsigned long long)bps,
3180 (unsigned long long)pkt_dev->errors);
3183 /* Set stopped-at timer, remove from running list, do counters & statistics */
3185 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3187 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3189 if (!pkt_dev->running) {
3190 printk(KERN_WARNING "pktgen: interface: %s is already "
3191 "stopped\n", pkt_dev->odev->name);
3195 pkt_dev->stopped_at = getCurUs();
3196 pkt_dev->running = 0;
3198 show_results(pkt_dev, nr_frags);
3203 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3205 struct pktgen_dev *pkt_dev, *best = NULL;
3209 list_for_each_entry(pkt_dev, &t->if_list, list) {
3210 if (!pkt_dev->running)
3214 else if (pkt_dev->next_tx_us < best->next_tx_us)
3221 static void pktgen_stop(struct pktgen_thread *t)
3223 struct pktgen_dev *pkt_dev;
3225 pr_debug("pktgen: entering pktgen_stop\n");
3229 list_for_each_entry(pkt_dev, &t->if_list, list) {
3230 pktgen_stop_device(pkt_dev);
3232 kfree_skb(pkt_dev->skb);
3234 pkt_dev->skb = NULL;
3241 * one of our devices needs to be removed - find it
3244 static void pktgen_rem_one_if(struct pktgen_thread *t)
3246 struct list_head *q, *n;
3247 struct pktgen_dev *cur;
3249 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3253 list_for_each_safe(q, n, &t->if_list) {
3254 cur = list_entry(q, struct pktgen_dev, list);
3256 if (!cur->removal_mark)
3260 kfree_skb(cur->skb);
3263 pktgen_remove_device(t, cur);
3271 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3273 struct list_head *q, *n;
3274 struct pktgen_dev *cur;
3276 /* Remove all devices, free mem */
3278 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3281 list_for_each_safe(q, n, &t->if_list) {
3282 cur = list_entry(q, struct pktgen_dev, list);
3285 kfree_skb(cur->skb);
3288 pktgen_remove_device(t, cur);
3294 static void pktgen_rem_thread(struct pktgen_thread *t)
3296 /* Remove from the thread list */
3298 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3300 mutex_lock(&pktgen_thread_lock);
3302 list_del(&t->th_list);
3304 mutex_unlock(&pktgen_thread_lock);
3307 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3309 struct net_device *odev = NULL;
3310 __u64 idle_start = 0;
3313 odev = pkt_dev->odev;
3315 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3319 if (now < pkt_dev->next_tx_us)
3320 spin(pkt_dev, pkt_dev->next_tx_us);
3322 /* This is max DELAY, this has special meaning of
3325 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3326 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3327 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3332 if ((netif_queue_stopped(odev) ||
3334 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping))) ||
3336 idle_start = getCurUs();
3338 if (!netif_running(odev)) {
3339 pktgen_stop_device(pkt_dev);
3341 kfree_skb(pkt_dev->skb);
3342 pkt_dev->skb = NULL;
3348 pkt_dev->idle_acc += getCurUs() - idle_start;
3350 if (netif_queue_stopped(odev) ||
3351 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3352 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3353 pkt_dev->next_tx_ns = 0;
3354 goto out; /* Try the next interface */
3358 if (pkt_dev->last_ok || !pkt_dev->skb) {
3359 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3360 || (!pkt_dev->skb)) {
3361 /* build a new pkt */
3363 kfree_skb(pkt_dev->skb);
3365 pkt_dev->skb = fill_packet(odev, pkt_dev);
3366 if (pkt_dev->skb == NULL) {
3367 printk(KERN_ERR "pktgen: ERROR: couldn't "
3368 "allocate skb in fill_packet.\n");
3370 pkt_dev->clone_count--; /* back out increment, OOM */
3373 pkt_dev->allocated_skbs++;
3374 pkt_dev->clone_count = 0; /* reset counter */
3378 netif_tx_lock_bh(odev);
3379 if (!netif_queue_stopped(odev) &&
3380 !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
3382 atomic_inc(&(pkt_dev->skb->users));
3384 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3385 if (likely(ret == NETDEV_TX_OK)) {
3386 pkt_dev->last_ok = 1;
3389 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3391 } else if (ret == NETDEV_TX_LOCKED
3392 && (odev->features & NETIF_F_LLTX)) {
3395 } else { /* Retry it next time */
3397 atomic_dec(&(pkt_dev->skb->users));
3399 if (debug && net_ratelimit())
3400 printk(KERN_INFO "pktgen: Hard xmit error\n");
3403 pkt_dev->last_ok = 0;
3406 pkt_dev->next_tx_us = getCurUs();
3407 pkt_dev->next_tx_ns = 0;
3409 pkt_dev->next_tx_us += pkt_dev->delay_us;
3410 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3412 if (pkt_dev->next_tx_ns > 1000) {
3413 pkt_dev->next_tx_us++;
3414 pkt_dev->next_tx_ns -= 1000;
3418 else { /* Retry it next time */
3419 pkt_dev->last_ok = 0;
3420 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3421 pkt_dev->next_tx_ns = 0;
3424 netif_tx_unlock_bh(odev);
3426 /* If pkt_dev->count is zero, then run forever */
3427 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3428 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3429 idle_start = getCurUs();
3430 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3431 if (signal_pending(current)) {
3436 pkt_dev->idle_acc += getCurUs() - idle_start;
3439 /* Done with this */
3440 pktgen_stop_device(pkt_dev);
3442 kfree_skb(pkt_dev->skb);
3443 pkt_dev->skb = NULL;
3449 * Main loop of the thread goes here
3452 static int pktgen_thread_worker(void *arg)
3455 struct pktgen_thread *t = arg;
3456 struct pktgen_dev *pkt_dev = NULL;
3458 u32 max_before_softirq;
3459 u32 tx_since_softirq = 0;
3461 BUG_ON(smp_processor_id() != cpu);
3463 init_waitqueue_head(&t->queue);
3465 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
3467 max_before_softirq = t->max_before_softirq;
3469 set_current_state(TASK_INTERRUPTIBLE);
3473 while (!kthread_should_stop()) {
3474 pkt_dev = next_to_run(t);
3477 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3479 prepare_to_wait(&(t->queue), &wait,
3480 TASK_INTERRUPTIBLE);
3481 schedule_timeout(HZ / 10);
3482 finish_wait(&(t->queue), &wait);
3485 __set_current_state(TASK_RUNNING);
3489 pktgen_xmit(pkt_dev);
3492 * We like to stay RUNNING but must also give
3493 * others fair share.
3496 tx_since_softirq += pkt_dev->last_ok;
3498 if (tx_since_softirq > max_before_softirq) {
3499 if (local_softirq_pending())
3501 tx_since_softirq = 0;
3505 if (t->control & T_STOP) {
3507 t->control &= ~(T_STOP);
3510 if (t->control & T_RUN) {
3512 t->control &= ~(T_RUN);
3515 if (t->control & T_REMDEVALL) {
3516 pktgen_rem_all_ifs(t);
3517 t->control &= ~(T_REMDEVALL);
3520 if (t->control & T_REMDEV) {
3521 pktgen_rem_one_if(t);
3522 t->control &= ~(T_REMDEV);
3527 set_current_state(TASK_INTERRUPTIBLE);
3530 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3533 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3534 pktgen_rem_all_ifs(t);
3536 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3537 pktgen_rem_thread(t);
3542 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3545 struct pktgen_dev *p, *pkt_dev = NULL;
3548 list_for_each_entry(p, &t->if_list, list)
3549 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3555 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3560 * Adds a dev at front of if_list.
3563 static int add_dev_to_thread(struct pktgen_thread *t,
3564 struct pktgen_dev *pkt_dev)
3570 if (pkt_dev->pg_thread) {
3571 printk(KERN_ERR "pktgen: ERROR: already assigned "
3577 list_add(&pkt_dev->list, &t->if_list);
3578 pkt_dev->pg_thread = t;
3579 pkt_dev->running = 0;
3586 /* Called under thread lock */
3588 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3590 struct pktgen_dev *pkt_dev;
3593 /* We don't allow a device to be on several threads */
3595 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3597 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3601 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3605 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3606 if (pkt_dev->flows == NULL) {
3610 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3612 pkt_dev->removal_mark = 0;
3613 pkt_dev->min_pkt_size = ETH_ZLEN;
3614 pkt_dev->max_pkt_size = ETH_ZLEN;
3615 pkt_dev->nfrags = 0;
3616 pkt_dev->clone_skb = pg_clone_skb_d;
3617 pkt_dev->delay_us = pg_delay_d / 1000;
3618 pkt_dev->delay_ns = pg_delay_d % 1000;
3619 pkt_dev->count = pg_count_d;
3621 pkt_dev->udp_src_min = 9; /* sink port */
3622 pkt_dev->udp_src_max = 9;
3623 pkt_dev->udp_dst_min = 9;
3624 pkt_dev->udp_dst_max = 9;
3626 pkt_dev->vlan_p = 0;
3627 pkt_dev->vlan_cfi = 0;
3628 pkt_dev->vlan_id = 0xffff;
3629 pkt_dev->svlan_p = 0;
3630 pkt_dev->svlan_cfi = 0;
3631 pkt_dev->svlan_id = 0xffff;
3633 err = pktgen_setup_dev(pkt_dev, ifname);
3637 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3638 if (!pkt_dev->entry) {
3639 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3640 PG_PROC_DIR, ifname);
3644 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3645 pkt_dev->entry->data = pkt_dev;
3647 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3648 pkt_dev->ipsproto = IPPROTO_ESP;
3651 return add_dev_to_thread(t, pkt_dev);
3653 dev_put(pkt_dev->odev);
3659 vfree(pkt_dev->flows);
3664 static int __init pktgen_create_thread(int cpu)
3666 struct pktgen_thread *t;
3667 struct proc_dir_entry *pe;
3668 struct task_struct *p;
3670 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3672 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3673 "create new thread.\n");
3677 spin_lock_init(&t->if_lock);
3680 INIT_LIST_HEAD(&t->if_list);
3682 list_add_tail(&t->th_list, &pktgen_threads);
3684 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3686 printk(KERN_ERR "pktgen: kernel_thread() failed "
3687 "for cpu %d\n", t->cpu);
3688 list_del(&t->th_list);
3692 kthread_bind(p, cpu);
3695 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3697 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3698 PG_PROC_DIR, t->tsk->comm);
3700 list_del(&t->th_list);
3705 pe->proc_fops = &pktgen_thread_fops;
3714 * Removes a device from the thread if_list.
3716 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3717 struct pktgen_dev *pkt_dev)
3719 struct list_head *q, *n;
3720 struct pktgen_dev *p;
3722 list_for_each_safe(q, n, &t->if_list) {
3723 p = list_entry(q, struct pktgen_dev, list);
3729 static int pktgen_remove_device(struct pktgen_thread *t,
3730 struct pktgen_dev *pkt_dev)
3733 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3735 if (pkt_dev->running) {
3736 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3737 "running interface, stopping it now.\n");
3738 pktgen_stop_device(pkt_dev);
3741 /* Dis-associate from the interface */
3743 if (pkt_dev->odev) {
3744 dev_put(pkt_dev->odev);
3745 pkt_dev->odev = NULL;
3748 /* And update the thread if_list */
3750 _rem_dev_from_if_list(t, pkt_dev);
3753 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3759 vfree(pkt_dev->flows);
3764 static int __init pg_init(void)
3767 struct proc_dir_entry *pe;
3769 printk(KERN_INFO "%s", version);
3771 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3774 pg_proc_dir->owner = THIS_MODULE;
3776 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3778 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3779 "procfs entry.\n", PGCTRL);
3780 proc_net_remove(PG_PROC_DIR);
3784 pe->proc_fops = &pktgen_fops;
3787 /* Register us to receive netdevice events */
3788 register_netdevice_notifier(&pktgen_notifier_block);
3790 for_each_online_cpu(cpu) {
3793 err = pktgen_create_thread(cpu);
3795 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3796 "thread for cpu %d (%d)\n", cpu, err);
3799 if (list_empty(&pktgen_threads)) {
3800 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3802 unregister_netdevice_notifier(&pktgen_notifier_block);
3803 remove_proc_entry(PGCTRL, pg_proc_dir);
3804 proc_net_remove(PG_PROC_DIR);
3811 static void __exit pg_cleanup(void)
3813 struct pktgen_thread *t;
3814 struct list_head *q, *n;
3815 wait_queue_head_t queue;
3816 init_waitqueue_head(&queue);
3818 /* Stop all interfaces & threads */
3820 list_for_each_safe(q, n, &pktgen_threads) {
3821 t = list_entry(q, struct pktgen_thread, th_list);
3822 kthread_stop(t->tsk);
3826 /* Un-register us from receiving netdevice events */
3827 unregister_netdevice_notifier(&pktgen_notifier_block);
3829 /* Clean up proc file system */
3830 remove_proc_entry(PGCTRL, pg_proc_dir);
3831 proc_net_remove(PG_PROC_DIR);
3834 module_init(pg_init);
3835 module_exit(pg_cleanup);
3837 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3838 MODULE_DESCRIPTION("Packet Generator tool");
3839 MODULE_LICENSE("GPL");
3840 module_param(pg_count_d, int, 0);
3841 module_param(pg_delay_d, int, 0);
3842 module_param(pg_clone_skb_d, int, 0);
3843 module_param(debug, int, 0);