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/smp_lock.h>
121 #include <linux/mutex.h>
122 #include <linux/sched.h>
123 #include <linux/slab.h>
124 #include <linux/vmalloc.h>
125 #include <linux/unistd.h>
126 #include <linux/string.h>
127 #include <linux/ptrace.h>
128 #include <linux/errno.h>
129 #include <linux/ioport.h>
130 #include <linux/interrupt.h>
131 #include <linux/capability.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 <net/checksum.h>
152 #include <net/ipv6.h>
153 #include <net/addrconf.h>
154 #include <asm/byteorder.h>
155 #include <linux/rcupdate.h>
156 #include <asm/bitops.h>
159 #include <asm/uaccess.h>
160 #include <asm/div64.h> /* do_div */
161 #include <asm/timex.h>
163 #define VERSION "pktgen v2.67: Packet Generator for packet performance testing.\n"
165 /* #define PG_DEBUG(a) a */
168 /* The buckets are exponential in 'width' */
169 #define LAT_BUCKETS_MAX 32
170 #define IP_NAME_SZ 32
171 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
172 #define MPLS_STACK_BOTTOM __constant_htonl(0x00000100)
174 /* Device flag bits */
175 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
176 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
177 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
178 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
179 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
180 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
181 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
182 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
183 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
184 #define F_VID_RND (1<<9) /* Random VLAN ID */
185 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
187 /* Thread control flag bits */
188 #define T_TERMINATE (1<<0)
189 #define T_STOP (1<<1) /* Stop run */
190 #define T_RUN (1<<2) /* Start run */
191 #define T_REMDEVALL (1<<3) /* Remove all devs */
192 #define T_REMDEV (1<<4) /* Remove one dev */
194 /* If lock -- can be removed after some work */
195 #define if_lock(t) spin_lock(&(t->if_lock));
196 #define if_unlock(t) spin_unlock(&(t->if_lock));
198 /* Used to help with determining the pkts on receive */
199 #define PKTGEN_MAGIC 0xbe9be955
200 #define PG_PROC_DIR "pktgen"
201 #define PGCTRL "pgctrl"
202 static struct proc_dir_entry *pg_proc_dir = NULL;
204 #define MAX_CFLOWS 65536
206 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
207 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
217 * Try to keep frequent/infrequent used vars. separated.
220 char ifname[IFNAMSIZ];
223 struct pktgen_thread *pg_thread; /* the owner */
224 struct list_head list; /* Used for chaining in the thread's run-queue */
226 int running; /* if this changes to false, the test will stop */
228 /* If min != max, then we will either do a linear iteration, or
229 * we will do a random selection from within the range.
232 int removal_mark; /* non-zero => the device is marked for
233 * removal by worker thread */
235 int min_pkt_size; /* = ETH_ZLEN; */
236 int max_pkt_size; /* = ETH_ZLEN; */
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 __u32 saddr_min; /* inclusive, source IP address */
286 __u32 saddr_max; /* exclusive, source IP address */
287 __u32 daddr_min; /* inclusive, dest IP address */
288 __u32 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 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
297 __be32 labels[MAX_MPLS_LABELS];
299 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
302 __u16 vlan_id; /* 0xffff means no vlan tag */
306 __u16 svlan_id; /* 0xffff means no svlan tag */
308 __u32 src_mac_count; /* How many MACs to iterate through */
309 __u32 dst_mac_count; /* How many MACs to iterate through */
311 unsigned char dst_mac[ETH_ALEN];
312 unsigned char src_mac[ETH_ALEN];
314 __u32 cur_dst_mac_offset;
315 __u32 cur_src_mac_offset;
324 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
326 We fill in SRC address later
327 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
331 __u16 pad; /* pad out the hh struct to an even 16 bytes */
333 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
334 * are transmitting the same one multiple times
336 struct net_device *odev; /* The out-going device. Note that the device should
337 * have it's pg_info pointer pointing back to this
338 * device. This will be set when the user specifies
339 * the out-going device name (not when the inject is
340 * started as it used to do.)
342 struct flow_state *flows;
343 unsigned cflows; /* Concurrent flows (config) */
344 unsigned lflow; /* Flow length (config) */
345 unsigned nflows; /* accumulated flows (stats) */
355 struct pktgen_thread {
357 struct list_head if_list; /* All device here */
358 struct list_head th_list;
362 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
364 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
370 wait_queue_head_t queue;
376 /* This code works around the fact that do_div cannot handle two 64-bit
377 numbers, and regular 64-bit division doesn't work on x86 kernels.
383 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
384 * Function copied/adapted/optimized from:
386 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
388 * Copyright 1994, University of Cambridge Computer Laboratory
389 * All Rights Reserved.
392 static inline s64 divremdi3(s64 x, s64 y, int type)
394 u64 a = (x < 0) ? -x : x;
395 u64 b = (y < 0) ? -y : y;
415 if (PG_DIV == type) {
416 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
418 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
422 /* End of hacks to deal with 64-bit math on x86 */
424 /** Convert to milliseconds */
425 static inline __u64 tv_to_ms(const struct timeval *tv)
427 __u64 ms = tv->tv_usec / 1000;
428 ms += (__u64) tv->tv_sec * (__u64) 1000;
432 /** Convert to micro-seconds */
433 static inline __u64 tv_to_us(const struct timeval *tv)
435 __u64 us = tv->tv_usec;
436 us += (__u64) tv->tv_sec * (__u64) 1000000;
440 static inline __u64 pg_div(__u64 n, __u32 base)
444 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
449 static inline __u64 pg_div64(__u64 n, __u64 base)
453 * How do we know if the architecture we are running on
454 * supports division with 64 bit base?
457 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
461 tmp = divremdi3(n, base, PG_DIV);
466 static inline u32 pktgen_random(void)
470 get_random_bytes(&n, 4);
477 static inline __u64 getCurMs(void)
480 do_gettimeofday(&tv);
481 return tv_to_ms(&tv);
484 static inline __u64 getCurUs(void)
487 do_gettimeofday(&tv);
488 return tv_to_us(&tv);
491 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
493 return tv_to_us(a) - tv_to_us(b);
496 /* old include end */
498 static char version[] __initdata = VERSION;
500 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
501 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
502 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
504 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
505 static void pktgen_run_all_threads(void);
506 static void pktgen_stop_all_threads_ifs(void);
507 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
508 static void pktgen_stop(struct pktgen_thread *t);
509 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
510 static int pktgen_mark_device(const char *ifname);
511 static unsigned int scan_ip6(const char *s, char ip[16]);
512 static unsigned int fmt_ip6(char *s, const char ip[16]);
514 /* Module parameters, defaults. */
515 static int pg_count_d = 1000; /* 1000 pkts by default */
516 static int pg_delay_d;
517 static int pg_clone_skb_d;
520 static DEFINE_MUTEX(pktgen_thread_lock);
521 static LIST_HEAD(pktgen_threads);
523 static struct notifier_block pktgen_notifier_block = {
524 .notifier_call = pktgen_device_event,
528 * /proc handling functions
532 static int pgctrl_show(struct seq_file *seq, void *v)
534 seq_puts(seq, VERSION);
538 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
539 size_t count, loff_t * ppos)
544 if (!capable(CAP_NET_ADMIN)) {
549 if (count > sizeof(data))
550 count = sizeof(data);
552 if (copy_from_user(data, buf, count)) {
556 data[count - 1] = 0; /* Make string */
558 if (!strcmp(data, "stop"))
559 pktgen_stop_all_threads_ifs();
561 else if (!strcmp(data, "start"))
562 pktgen_run_all_threads();
565 printk("pktgen: Unknown command: %s\n", data);
573 static int pgctrl_open(struct inode *inode, struct file *file)
575 return single_open(file, pgctrl_show, PDE(inode)->data);
578 static struct file_operations pktgen_fops = {
579 .owner = THIS_MODULE,
583 .write = pgctrl_write,
584 .release = single_release,
587 static int pktgen_if_show(struct seq_file *seq, void *v)
590 struct pktgen_dev *pkt_dev = seq->private;
593 __u64 now = getCurUs();
596 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
597 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
598 pkt_dev->max_pkt_size);
601 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
603 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
604 pkt_dev->clone_skb, pkt_dev->ifname);
606 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
609 if (pkt_dev->flags & F_IPV6) {
610 char b1[128], b2[128], b3[128];
611 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
612 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
613 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
615 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
618 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
619 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
620 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
622 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
627 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
628 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
631 seq_puts(seq, " src_mac: ");
633 if (is_zero_ether_addr(pkt_dev->src_mac))
634 for (i = 0; i < 6; i++)
635 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
638 for (i = 0; i < 6; i++)
639 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
642 seq_printf(seq, "dst_mac: ");
643 for (i = 0; i < 6; i++)
644 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
645 i == 5 ? "\n" : ":");
648 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
649 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
650 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
653 " src_mac_count: %d dst_mac_count: %d\n",
654 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
656 if (pkt_dev->nr_labels) {
658 seq_printf(seq, " mpls: ");
659 for(i = 0; i < pkt_dev->nr_labels; i++)
660 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
661 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
664 if (pkt_dev->vlan_id != 0xffff) {
665 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
666 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
669 if (pkt_dev->svlan_id != 0xffff) {
670 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
671 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
674 seq_printf(seq, " Flags: ");
676 if (pkt_dev->flags & F_IPV6)
677 seq_printf(seq, "IPV6 ");
679 if (pkt_dev->flags & F_IPSRC_RND)
680 seq_printf(seq, "IPSRC_RND ");
682 if (pkt_dev->flags & F_IPDST_RND)
683 seq_printf(seq, "IPDST_RND ");
685 if (pkt_dev->flags & F_TXSIZE_RND)
686 seq_printf(seq, "TXSIZE_RND ");
688 if (pkt_dev->flags & F_UDPSRC_RND)
689 seq_printf(seq, "UDPSRC_RND ");
691 if (pkt_dev->flags & F_UDPDST_RND)
692 seq_printf(seq, "UDPDST_RND ");
694 if (pkt_dev->flags & F_MPLS_RND)
695 seq_printf(seq, "MPLS_RND ");
697 if (pkt_dev->flags & F_MACSRC_RND)
698 seq_printf(seq, "MACSRC_RND ");
700 if (pkt_dev->flags & F_MACDST_RND)
701 seq_printf(seq, "MACDST_RND ");
703 if (pkt_dev->flags & F_VID_RND)
704 seq_printf(seq, "VID_RND ");
706 if (pkt_dev->flags & F_SVID_RND)
707 seq_printf(seq, "SVID_RND ");
711 sa = pkt_dev->started_at;
712 stopped = pkt_dev->stopped_at;
713 if (pkt_dev->running)
714 stopped = now; /* not really stopped, more like last-running-at */
717 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
718 (unsigned long long)pkt_dev->sofar,
719 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
720 (unsigned long long)stopped,
721 (unsigned long long)pkt_dev->idle_acc);
724 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
725 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
726 pkt_dev->cur_src_mac_offset);
728 if (pkt_dev->flags & F_IPV6) {
729 char b1[128], b2[128];
730 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
731 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
732 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
734 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
735 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
737 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
738 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
740 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
742 if (pkt_dev->result[0])
743 seq_printf(seq, "Result: %s\n", pkt_dev->result);
745 seq_printf(seq, "Result: Idle\n");
751 static int hex32_arg(const char __user *user_buffer, __u32 *num)
759 if (get_user(c, &user_buffer[i]))
761 if ((c >= '0') && (c <= '9'))
763 else if ((c >= 'a') && (c <= 'f'))
764 *num |= c - 'a' + 10;
765 else if ((c >= 'A') && (c <= 'F'))
766 *num |= c - 'A' + 10;
773 static int count_trail_chars(const char __user * user_buffer,
778 for (i = 0; i < maxlen; i++) {
780 if (get_user(c, &user_buffer[i]))
798 static unsigned long num_arg(const char __user * user_buffer,
799 unsigned long maxlen, unsigned long *num)
804 for (; i < maxlen; i++) {
806 if (get_user(c, &user_buffer[i]))
808 if ((c >= '0') && (c <= '9')) {
817 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
821 for (; i < maxlen; i++) {
823 if (get_user(c, &user_buffer[i]))
841 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
848 pkt_dev->nr_labels = 0;
851 len = hex32_arg(&buffer[i], &tmp);
854 pkt_dev->labels[n] = htonl(tmp);
855 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
856 pkt_dev->flags |= F_MPLS_RND;
858 if (get_user(c, &buffer[i]))
862 if (n >= MAX_MPLS_LABELS)
866 pkt_dev->nr_labels = n;
870 static ssize_t pktgen_if_write(struct file *file,
871 const char __user * user_buffer, size_t count,
874 struct seq_file *seq = (struct seq_file *)file->private_data;
875 struct pktgen_dev *pkt_dev = seq->private;
877 char name[16], valstr[32];
878 unsigned long value = 0;
879 char *pg_result = NULL;
883 pg_result = &(pkt_dev->result[0]);
886 printk("pktgen: wrong command format\n");
891 tmp = count_trail_chars(&user_buffer[i], max);
893 printk("pktgen: illegal format\n");
898 /* Read variable name */
900 len = strn_len(&user_buffer[i], sizeof(name) - 1);
904 memset(name, 0, sizeof(name));
905 if (copy_from_user(name, &user_buffer[i], len))
910 len = count_trail_chars(&user_buffer[i], max);
918 if (copy_from_user(tb, user_buffer, count))
921 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
922 (unsigned long)count, tb);
925 if (!strcmp(name, "min_pkt_size")) {
926 len = num_arg(&user_buffer[i], 10, &value);
931 if (value < 14 + 20 + 8)
933 if (value != pkt_dev->min_pkt_size) {
934 pkt_dev->min_pkt_size = value;
935 pkt_dev->cur_pkt_size = value;
937 sprintf(pg_result, "OK: min_pkt_size=%u",
938 pkt_dev->min_pkt_size);
942 if (!strcmp(name, "max_pkt_size")) {
943 len = num_arg(&user_buffer[i], 10, &value);
948 if (value < 14 + 20 + 8)
950 if (value != pkt_dev->max_pkt_size) {
951 pkt_dev->max_pkt_size = value;
952 pkt_dev->cur_pkt_size = value;
954 sprintf(pg_result, "OK: max_pkt_size=%u",
955 pkt_dev->max_pkt_size);
959 /* Shortcut for min = max */
961 if (!strcmp(name, "pkt_size")) {
962 len = num_arg(&user_buffer[i], 10, &value);
967 if (value < 14 + 20 + 8)
969 if (value != pkt_dev->min_pkt_size) {
970 pkt_dev->min_pkt_size = value;
971 pkt_dev->max_pkt_size = value;
972 pkt_dev->cur_pkt_size = value;
974 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
978 if (!strcmp(name, "debug")) {
979 len = num_arg(&user_buffer[i], 10, &value);
985 sprintf(pg_result, "OK: debug=%u", debug);
989 if (!strcmp(name, "frags")) {
990 len = num_arg(&user_buffer[i], 10, &value);
995 pkt_dev->nfrags = value;
996 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
999 if (!strcmp(name, "delay")) {
1000 len = num_arg(&user_buffer[i], 10, &value);
1005 if (value == 0x7FFFFFFF) {
1006 pkt_dev->delay_us = 0x7FFFFFFF;
1007 pkt_dev->delay_ns = 0;
1009 pkt_dev->delay_us = value / 1000;
1010 pkt_dev->delay_ns = value % 1000;
1012 sprintf(pg_result, "OK: delay=%u",
1013 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1016 if (!strcmp(name, "udp_src_min")) {
1017 len = num_arg(&user_buffer[i], 10, &value);
1022 if (value != pkt_dev->udp_src_min) {
1023 pkt_dev->udp_src_min = value;
1024 pkt_dev->cur_udp_src = value;
1026 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1029 if (!strcmp(name, "udp_dst_min")) {
1030 len = num_arg(&user_buffer[i], 10, &value);
1035 if (value != pkt_dev->udp_dst_min) {
1036 pkt_dev->udp_dst_min = value;
1037 pkt_dev->cur_udp_dst = value;
1039 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1042 if (!strcmp(name, "udp_src_max")) {
1043 len = num_arg(&user_buffer[i], 10, &value);
1048 if (value != pkt_dev->udp_src_max) {
1049 pkt_dev->udp_src_max = value;
1050 pkt_dev->cur_udp_src = value;
1052 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1055 if (!strcmp(name, "udp_dst_max")) {
1056 len = num_arg(&user_buffer[i], 10, &value);
1061 if (value != pkt_dev->udp_dst_max) {
1062 pkt_dev->udp_dst_max = value;
1063 pkt_dev->cur_udp_dst = value;
1065 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1068 if (!strcmp(name, "clone_skb")) {
1069 len = num_arg(&user_buffer[i], 10, &value);
1074 pkt_dev->clone_skb = value;
1076 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1079 if (!strcmp(name, "count")) {
1080 len = num_arg(&user_buffer[i], 10, &value);
1085 pkt_dev->count = value;
1086 sprintf(pg_result, "OK: count=%llu",
1087 (unsigned long long)pkt_dev->count);
1090 if (!strcmp(name, "src_mac_count")) {
1091 len = num_arg(&user_buffer[i], 10, &value);
1096 if (pkt_dev->src_mac_count != value) {
1097 pkt_dev->src_mac_count = value;
1098 pkt_dev->cur_src_mac_offset = 0;
1100 sprintf(pg_result, "OK: src_mac_count=%d",
1101 pkt_dev->src_mac_count);
1104 if (!strcmp(name, "dst_mac_count")) {
1105 len = num_arg(&user_buffer[i], 10, &value);
1110 if (pkt_dev->dst_mac_count != value) {
1111 pkt_dev->dst_mac_count = value;
1112 pkt_dev->cur_dst_mac_offset = 0;
1114 sprintf(pg_result, "OK: dst_mac_count=%d",
1115 pkt_dev->dst_mac_count);
1118 if (!strcmp(name, "flag")) {
1121 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1125 if (copy_from_user(f, &user_buffer[i], len))
1128 if (strcmp(f, "IPSRC_RND") == 0)
1129 pkt_dev->flags |= F_IPSRC_RND;
1131 else if (strcmp(f, "!IPSRC_RND") == 0)
1132 pkt_dev->flags &= ~F_IPSRC_RND;
1134 else if (strcmp(f, "TXSIZE_RND") == 0)
1135 pkt_dev->flags |= F_TXSIZE_RND;
1137 else if (strcmp(f, "!TXSIZE_RND") == 0)
1138 pkt_dev->flags &= ~F_TXSIZE_RND;
1140 else if (strcmp(f, "IPDST_RND") == 0)
1141 pkt_dev->flags |= F_IPDST_RND;
1143 else if (strcmp(f, "!IPDST_RND") == 0)
1144 pkt_dev->flags &= ~F_IPDST_RND;
1146 else if (strcmp(f, "UDPSRC_RND") == 0)
1147 pkt_dev->flags |= F_UDPSRC_RND;
1149 else if (strcmp(f, "!UDPSRC_RND") == 0)
1150 pkt_dev->flags &= ~F_UDPSRC_RND;
1152 else if (strcmp(f, "UDPDST_RND") == 0)
1153 pkt_dev->flags |= F_UDPDST_RND;
1155 else if (strcmp(f, "!UDPDST_RND") == 0)
1156 pkt_dev->flags &= ~F_UDPDST_RND;
1158 else if (strcmp(f, "MACSRC_RND") == 0)
1159 pkt_dev->flags |= F_MACSRC_RND;
1161 else if (strcmp(f, "!MACSRC_RND") == 0)
1162 pkt_dev->flags &= ~F_MACSRC_RND;
1164 else if (strcmp(f, "MACDST_RND") == 0)
1165 pkt_dev->flags |= F_MACDST_RND;
1167 else if (strcmp(f, "!MACDST_RND") == 0)
1168 pkt_dev->flags &= ~F_MACDST_RND;
1170 else if (strcmp(f, "MPLS_RND") == 0)
1171 pkt_dev->flags |= F_MPLS_RND;
1173 else if (strcmp(f, "!MPLS_RND") == 0)
1174 pkt_dev->flags &= ~F_MPLS_RND;
1176 else if (strcmp(f, "VID_RND") == 0)
1177 pkt_dev->flags |= F_VID_RND;
1179 else if (strcmp(f, "!VID_RND") == 0)
1180 pkt_dev->flags &= ~F_VID_RND;
1182 else if (strcmp(f, "SVID_RND") == 0)
1183 pkt_dev->flags |= F_SVID_RND;
1185 else if (strcmp(f, "!SVID_RND") == 0)
1186 pkt_dev->flags &= ~F_SVID_RND;
1190 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1192 "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n");
1195 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1198 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1199 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1204 if (copy_from_user(buf, &user_buffer[i], len))
1207 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1208 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1209 strncpy(pkt_dev->dst_min, buf, len);
1210 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1211 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1214 printk("pktgen: dst_min set to: %s\n",
1217 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1220 if (!strcmp(name, "dst_max")) {
1221 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1226 if (copy_from_user(buf, &user_buffer[i], len))
1230 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1231 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1232 strncpy(pkt_dev->dst_max, buf, len);
1233 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1234 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1237 printk("pktgen: dst_max set to: %s\n",
1240 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1243 if (!strcmp(name, "dst6")) {
1244 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1248 pkt_dev->flags |= F_IPV6;
1250 if (copy_from_user(buf, &user_buffer[i], len))
1254 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1255 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1257 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1260 printk("pktgen: dst6 set to: %s\n", buf);
1263 sprintf(pg_result, "OK: dst6=%s", buf);
1266 if (!strcmp(name, "dst6_min")) {
1267 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1271 pkt_dev->flags |= F_IPV6;
1273 if (copy_from_user(buf, &user_buffer[i], len))
1277 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1278 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1280 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1281 &pkt_dev->min_in6_daddr);
1283 printk("pktgen: dst6_min set to: %s\n", buf);
1286 sprintf(pg_result, "OK: dst6_min=%s", buf);
1289 if (!strcmp(name, "dst6_max")) {
1290 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1294 pkt_dev->flags |= F_IPV6;
1296 if (copy_from_user(buf, &user_buffer[i], len))
1300 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1301 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1304 printk("pktgen: dst6_max set to: %s\n", buf);
1307 sprintf(pg_result, "OK: dst6_max=%s", buf);
1310 if (!strcmp(name, "src6")) {
1311 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1315 pkt_dev->flags |= F_IPV6;
1317 if (copy_from_user(buf, &user_buffer[i], len))
1321 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1322 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1324 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1327 printk("pktgen: src6 set to: %s\n", buf);
1330 sprintf(pg_result, "OK: src6=%s", buf);
1333 if (!strcmp(name, "src_min")) {
1334 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1338 if (copy_from_user(buf, &user_buffer[i], len))
1341 if (strcmp(buf, pkt_dev->src_min) != 0) {
1342 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1343 strncpy(pkt_dev->src_min, buf, len);
1344 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1345 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1348 printk("pktgen: src_min set to: %s\n",
1351 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1354 if (!strcmp(name, "src_max")) {
1355 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1359 if (copy_from_user(buf, &user_buffer[i], len))
1362 if (strcmp(buf, pkt_dev->src_max) != 0) {
1363 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1364 strncpy(pkt_dev->src_max, buf, len);
1365 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1366 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1369 printk("pktgen: src_max set to: %s\n",
1372 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1375 if (!strcmp(name, "dst_mac")) {
1377 unsigned char old_dmac[ETH_ALEN];
1378 unsigned char *m = pkt_dev->dst_mac;
1379 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1381 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1385 memset(valstr, 0, sizeof(valstr));
1386 if (copy_from_user(valstr, &user_buffer[i], len))
1390 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1391 if (*v >= '0' && *v <= '9') {
1395 if (*v >= 'A' && *v <= 'F') {
1397 *m += *v - 'A' + 10;
1399 if (*v >= 'a' && *v <= 'f') {
1401 *m += *v - 'a' + 10;
1409 /* Set up Dest MAC */
1410 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1411 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1413 sprintf(pg_result, "OK: dstmac");
1416 if (!strcmp(name, "src_mac")) {
1418 unsigned char *m = pkt_dev->src_mac;
1420 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1424 memset(valstr, 0, sizeof(valstr));
1425 if (copy_from_user(valstr, &user_buffer[i], len))
1429 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1430 if (*v >= '0' && *v <= '9') {
1434 if (*v >= 'A' && *v <= 'F') {
1436 *m += *v - 'A' + 10;
1438 if (*v >= 'a' && *v <= 'f') {
1440 *m += *v - 'a' + 10;
1448 sprintf(pg_result, "OK: srcmac");
1452 if (!strcmp(name, "clear_counters")) {
1453 pktgen_clear_counters(pkt_dev);
1454 sprintf(pg_result, "OK: Clearing counters.\n");
1458 if (!strcmp(name, "flows")) {
1459 len = num_arg(&user_buffer[i], 10, &value);
1464 if (value > MAX_CFLOWS)
1467 pkt_dev->cflows = value;
1468 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1472 if (!strcmp(name, "flowlen")) {
1473 len = num_arg(&user_buffer[i], 10, &value);
1478 pkt_dev->lflow = value;
1479 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1483 if (!strcmp(name, "mpls")) {
1485 len = get_labels(&user_buffer[i], pkt_dev);
1486 if (len < 0) { return len; }
1488 offset = sprintf(pg_result, "OK: mpls=");
1489 for(n = 0; n < pkt_dev->nr_labels; n++)
1490 offset += sprintf(pg_result + offset,
1491 "%08x%s", ntohl(pkt_dev->labels[n]),
1492 n == pkt_dev->nr_labels-1 ? "" : ",");
1494 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1495 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1496 pkt_dev->svlan_id = 0xffff;
1499 printk("pktgen: VLAN/SVLAN auto turned off\n");
1504 if (!strcmp(name, "vlan_id")) {
1505 len = num_arg(&user_buffer[i], 4, &value);
1510 if (value <= 4095) {
1511 pkt_dev->vlan_id = value; /* turn on VLAN */
1514 printk("pktgen: VLAN turned on\n");
1516 if (debug && pkt_dev->nr_labels)
1517 printk("pktgen: MPLS auto turned off\n");
1519 pkt_dev->nr_labels = 0; /* turn off MPLS */
1520 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1522 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1523 pkt_dev->svlan_id = 0xffff;
1526 printk("pktgen: VLAN/SVLAN turned off\n");
1531 if (!strcmp(name, "vlan_p")) {
1532 len = num_arg(&user_buffer[i], 1, &value);
1537 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1538 pkt_dev->vlan_p = value;
1539 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1541 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1546 if (!strcmp(name, "vlan_cfi")) {
1547 len = num_arg(&user_buffer[i], 1, &value);
1552 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1553 pkt_dev->vlan_cfi = value;
1554 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1556 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1561 if (!strcmp(name, "svlan_id")) {
1562 len = num_arg(&user_buffer[i], 4, &value);
1567 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1568 pkt_dev->svlan_id = value; /* turn on SVLAN */
1571 printk("pktgen: SVLAN turned on\n");
1573 if (debug && pkt_dev->nr_labels)
1574 printk("pktgen: MPLS auto turned off\n");
1576 pkt_dev->nr_labels = 0; /* turn off MPLS */
1577 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1579 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1580 pkt_dev->svlan_id = 0xffff;
1583 printk("pktgen: VLAN/SVLAN turned off\n");
1588 if (!strcmp(name, "svlan_p")) {
1589 len = num_arg(&user_buffer[i], 1, &value);
1594 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1595 pkt_dev->svlan_p = value;
1596 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1598 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1603 if (!strcmp(name, "svlan_cfi")) {
1604 len = num_arg(&user_buffer[i], 1, &value);
1609 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1610 pkt_dev->svlan_cfi = value;
1611 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1613 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1618 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1622 static int pktgen_if_open(struct inode *inode, struct file *file)
1624 return single_open(file, pktgen_if_show, PDE(inode)->data);
1627 static struct file_operations pktgen_if_fops = {
1628 .owner = THIS_MODULE,
1629 .open = pktgen_if_open,
1631 .llseek = seq_lseek,
1632 .write = pktgen_if_write,
1633 .release = single_release,
1636 static int pktgen_thread_show(struct seq_file *seq, void *v)
1638 struct pktgen_thread *t = seq->private;
1639 struct pktgen_dev *pkt_dev;
1643 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1644 t->name, t->max_before_softirq);
1646 seq_printf(seq, "Running: ");
1649 list_for_each_entry(pkt_dev, &t->if_list, list)
1650 if (pkt_dev->running)
1651 seq_printf(seq, "%s ", pkt_dev->ifname);
1653 seq_printf(seq, "\nStopped: ");
1655 list_for_each_entry(pkt_dev, &t->if_list, list)
1656 if (!pkt_dev->running)
1657 seq_printf(seq, "%s ", pkt_dev->ifname);
1660 seq_printf(seq, "\nResult: %s\n", t->result);
1662 seq_printf(seq, "\nResult: NA\n");
1669 static ssize_t pktgen_thread_write(struct file *file,
1670 const char __user * user_buffer,
1671 size_t count, loff_t * offset)
1673 struct seq_file *seq = (struct seq_file *)file->private_data;
1674 struct pktgen_thread *t = seq->private;
1675 int i = 0, max, len, ret;
1678 unsigned long value = 0;
1681 // sprintf(pg_result, "Wrong command format");
1686 len = count_trail_chars(&user_buffer[i], max);
1692 /* Read variable name */
1694 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1698 memset(name, 0, sizeof(name));
1699 if (copy_from_user(name, &user_buffer[i], len))
1704 len = count_trail_chars(&user_buffer[i], max);
1711 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
1714 printk("pktgen: ERROR: No thread\n");
1719 pg_result = &(t->result[0]);
1721 if (!strcmp(name, "add_device")) {
1724 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1729 if (copy_from_user(f, &user_buffer[i], len))
1732 mutex_lock(&pktgen_thread_lock);
1733 pktgen_add_device(t, f);
1734 mutex_unlock(&pktgen_thread_lock);
1736 sprintf(pg_result, "OK: add_device=%s", f);
1740 if (!strcmp(name, "rem_device_all")) {
1741 mutex_lock(&pktgen_thread_lock);
1742 t->control |= T_REMDEVALL;
1743 mutex_unlock(&pktgen_thread_lock);
1744 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1746 sprintf(pg_result, "OK: rem_device_all");
1750 if (!strcmp(name, "max_before_softirq")) {
1751 len = num_arg(&user_buffer[i], 10, &value);
1752 mutex_lock(&pktgen_thread_lock);
1753 t->max_before_softirq = value;
1754 mutex_unlock(&pktgen_thread_lock);
1756 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1765 static int pktgen_thread_open(struct inode *inode, struct file *file)
1767 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1770 static struct file_operations pktgen_thread_fops = {
1771 .owner = THIS_MODULE,
1772 .open = pktgen_thread_open,
1774 .llseek = seq_lseek,
1775 .write = pktgen_thread_write,
1776 .release = single_release,
1779 /* Think find or remove for NN */
1780 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1782 struct pktgen_thread *t;
1783 struct pktgen_dev *pkt_dev = NULL;
1785 list_for_each_entry(t, &pktgen_threads, th_list) {
1786 pkt_dev = pktgen_find_dev(t, ifname);
1790 pkt_dev->removal_mark = 1;
1791 t->control |= T_REMDEV;
1801 * mark a device for removal
1803 static int pktgen_mark_device(const char *ifname)
1805 struct pktgen_dev *pkt_dev = NULL;
1806 const int max_tries = 10, msec_per_try = 125;
1810 mutex_lock(&pktgen_thread_lock);
1811 PG_DEBUG(printk("pktgen: pktgen_mark_device marking %s for removal\n",
1816 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1817 if (pkt_dev == NULL)
1818 break; /* success */
1820 mutex_unlock(&pktgen_thread_lock);
1821 PG_DEBUG(printk("pktgen: pktgen_mark_device waiting for %s "
1822 "to disappear....\n", ifname));
1823 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1824 mutex_lock(&pktgen_thread_lock);
1826 if (++i >= max_tries) {
1827 printk("pktgen_mark_device: timed out after waiting "
1828 "%d msec for device %s to be removed\n",
1829 msec_per_try * i, ifname);
1836 mutex_unlock(&pktgen_thread_lock);
1841 static int pktgen_device_event(struct notifier_block *unused,
1842 unsigned long event, void *ptr)
1844 struct net_device *dev = (struct net_device *)(ptr);
1846 /* It is OK that we do not hold the group lock right now,
1847 * as we run under the RTNL lock.
1851 case NETDEV_CHANGEADDR:
1852 case NETDEV_GOING_DOWN:
1855 /* Ignore for now */
1858 case NETDEV_UNREGISTER:
1859 pktgen_mark_device(dev->name);
1866 /* Associate pktgen_dev with a device. */
1868 static struct net_device *pktgen_setup_dev(struct pktgen_dev *pkt_dev)
1870 struct net_device *odev;
1872 /* Clean old setups */
1874 if (pkt_dev->odev) {
1875 dev_put(pkt_dev->odev);
1876 pkt_dev->odev = NULL;
1879 odev = dev_get_by_name(pkt_dev->ifname);
1882 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1885 if (odev->type != ARPHRD_ETHER) {
1886 printk("pktgen: not an ethernet device: \"%s\"\n",
1890 if (!netif_running(odev)) {
1891 printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1894 pkt_dev->odev = odev;
1896 return pkt_dev->odev;
1905 /* Read pkt_dev from the interface and set up internal pktgen_dev
1906 * structure to have the right information to create/send packets
1908 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1910 /* Try once more, just in case it works now. */
1912 pktgen_setup_dev(pkt_dev);
1914 if (!pkt_dev->odev) {
1915 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1916 sprintf(pkt_dev->result,
1917 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1921 /* Default to the interface's mac if not explicitly set. */
1923 if (is_zero_ether_addr(pkt_dev->src_mac))
1924 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1926 /* Set up Dest MAC */
1927 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1929 /* Set up pkt size */
1930 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1932 if (pkt_dev->flags & F_IPV6) {
1934 * Skip this automatic address setting until locks or functions
1939 int i, set = 0, err = 1;
1940 struct inet6_dev *idev;
1942 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1943 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1951 * Use linklevel address if unconfigured.
1953 * use ipv6_get_lladdr if/when it's get exported
1957 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1958 struct inet6_ifaddr *ifp;
1960 read_lock_bh(&idev->lock);
1961 for (ifp = idev->addr_list; ifp;
1962 ifp = ifp->if_next) {
1963 if (ifp->scope == IFA_LINK
1965 flags & IFA_F_TENTATIVE)) {
1966 ipv6_addr_copy(&pkt_dev->
1973 read_unlock_bh(&idev->lock);
1977 printk("pktgen: ERROR: IPv6 link address not availble.\n");
1981 pkt_dev->saddr_min = 0;
1982 pkt_dev->saddr_max = 0;
1983 if (strlen(pkt_dev->src_min) == 0) {
1985 struct in_device *in_dev;
1988 in_dev = __in_dev_get_rcu(pkt_dev->odev);
1990 if (in_dev->ifa_list) {
1991 pkt_dev->saddr_min =
1992 in_dev->ifa_list->ifa_address;
1993 pkt_dev->saddr_max = pkt_dev->saddr_min;
1998 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1999 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2002 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2003 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2005 /* Initialize current values. */
2006 pkt_dev->cur_dst_mac_offset = 0;
2007 pkt_dev->cur_src_mac_offset = 0;
2008 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2009 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2010 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2011 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2012 pkt_dev->nflows = 0;
2015 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2020 start = now = getCurUs();
2021 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2022 while (now < spin_until_us) {
2023 /* TODO: optimize sleeping behavior */
2024 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2025 schedule_timeout_interruptible(1);
2026 else if (spin_until_us - now > 100) {
2028 if (!pkt_dev->running)
2037 pkt_dev->idle_acc += now - start;
2040 /* Increment/randomize headers according to flags and current values
2041 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2043 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2049 if (pkt_dev->cflows) {
2050 flow = pktgen_random() % pkt_dev->cflows;
2052 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2053 pkt_dev->flows[flow].count = 0;
2056 /* Deal with source MAC */
2057 if (pkt_dev->src_mac_count > 1) {
2061 if (pkt_dev->flags & F_MACSRC_RND)
2062 mc = pktgen_random() % (pkt_dev->src_mac_count);
2064 mc = pkt_dev->cur_src_mac_offset++;
2065 if (pkt_dev->cur_src_mac_offset >
2066 pkt_dev->src_mac_count)
2067 pkt_dev->cur_src_mac_offset = 0;
2070 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2071 pkt_dev->hh[11] = tmp;
2072 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2073 pkt_dev->hh[10] = tmp;
2074 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2075 pkt_dev->hh[9] = tmp;
2076 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2077 pkt_dev->hh[8] = tmp;
2078 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2079 pkt_dev->hh[7] = tmp;
2082 /* Deal with Destination MAC */
2083 if (pkt_dev->dst_mac_count > 1) {
2087 if (pkt_dev->flags & F_MACDST_RND)
2088 mc = pktgen_random() % (pkt_dev->dst_mac_count);
2091 mc = pkt_dev->cur_dst_mac_offset++;
2092 if (pkt_dev->cur_dst_mac_offset >
2093 pkt_dev->dst_mac_count) {
2094 pkt_dev->cur_dst_mac_offset = 0;
2098 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2099 pkt_dev->hh[5] = tmp;
2100 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2101 pkt_dev->hh[4] = tmp;
2102 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2103 pkt_dev->hh[3] = tmp;
2104 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2105 pkt_dev->hh[2] = tmp;
2106 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2107 pkt_dev->hh[1] = tmp;
2110 if (pkt_dev->flags & F_MPLS_RND) {
2112 for(i = 0; i < pkt_dev->nr_labels; i++)
2113 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2114 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2119 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2120 pkt_dev->vlan_id = pktgen_random() % 4096;
2123 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2124 pkt_dev->svlan_id = pktgen_random() % 4096;
2127 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2128 if (pkt_dev->flags & F_UDPSRC_RND)
2129 pkt_dev->cur_udp_src =
2131 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) +
2132 pkt_dev->udp_src_min);
2135 pkt_dev->cur_udp_src++;
2136 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2137 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2141 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2142 if (pkt_dev->flags & F_UDPDST_RND) {
2143 pkt_dev->cur_udp_dst =
2145 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) +
2146 pkt_dev->udp_dst_min);
2148 pkt_dev->cur_udp_dst++;
2149 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2150 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2154 if (!(pkt_dev->flags & F_IPV6)) {
2156 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2160 if (pkt_dev->flags & F_IPSRC_RND)
2161 t = ((pktgen_random() % (imx - imn)) + imn);
2163 t = ntohl(pkt_dev->cur_saddr);
2169 pkt_dev->cur_saddr = htonl(t);
2172 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
2173 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2176 if ((imn = ntohl(pkt_dev->daddr_min)) < (imx =
2181 if (pkt_dev->flags & F_IPDST_RND) {
2183 t = ((pktgen_random() % (imx - imn)) +
2187 while (LOOPBACK(t) || MULTICAST(t)
2188 || BADCLASS(t) || ZERONET(t)
2189 || LOCAL_MCAST(t)) {
2190 t = ((pktgen_random() %
2191 (imx - imn)) + imn);
2194 pkt_dev->cur_daddr = t;
2198 t = ntohl(pkt_dev->cur_daddr);
2203 pkt_dev->cur_daddr = htonl(t);
2206 if (pkt_dev->cflows) {
2207 pkt_dev->flows[flow].cur_daddr =
2212 } else { /* IPV6 * */
2214 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2215 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2216 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2217 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2221 /* Only random destinations yet */
2223 for (i = 0; i < 4; i++) {
2224 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2226 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2227 pkt_dev->max_in6_daddr.s6_addr32[i]);
2232 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2234 if (pkt_dev->flags & F_TXSIZE_RND) {
2235 t = ((pktgen_random() %
2236 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size))
2237 + pkt_dev->min_pkt_size);
2239 t = pkt_dev->cur_pkt_size + 1;
2240 if (t > pkt_dev->max_pkt_size)
2241 t = pkt_dev->min_pkt_size;
2243 pkt_dev->cur_pkt_size = t;
2246 pkt_dev->flows[flow].count++;
2249 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2252 for(i = 0; i < pkt_dev->nr_labels; i++) {
2253 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2256 *mpls |= MPLS_STACK_BOTTOM;
2259 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2260 struct pktgen_dev *pkt_dev)
2262 struct sk_buff *skb = NULL;
2264 struct udphdr *udph;
2267 struct pktgen_hdr *pgh = NULL;
2268 __be16 protocol = __constant_htons(ETH_P_IP);
2270 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2271 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2272 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2273 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2276 if (pkt_dev->nr_labels)
2277 protocol = __constant_htons(ETH_P_MPLS_UC);
2279 if (pkt_dev->vlan_id != 0xffff)
2280 protocol = __constant_htons(ETH_P_8021Q);
2282 /* Update any of the values, used when we're incrementing various
2285 mod_cur_headers(pkt_dev);
2287 datalen = (odev->hard_header_len + 16) & ~0xf;
2288 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2289 pkt_dev->nr_labels*sizeof(u32) +
2290 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2293 sprintf(pkt_dev->result, "No memory");
2297 skb_reserve(skb, datalen);
2299 /* Reserve for ethernet and IP header */
2300 eth = (__u8 *) skb_push(skb, 14);
2301 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2302 if (pkt_dev->nr_labels)
2303 mpls_push(mpls, pkt_dev);
2305 if (pkt_dev->vlan_id != 0xffff) {
2306 if(pkt_dev->svlan_id != 0xffff) {
2307 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2308 *svlan_tci = htons(pkt_dev->svlan_id);
2309 *svlan_tci |= pkt_dev->svlan_p << 5;
2310 *svlan_tci |= pkt_dev->svlan_cfi << 4;
2311 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2312 *svlan_encapsulated_proto = __constant_htons(ETH_P_8021Q);
2314 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2315 *vlan_tci = htons(pkt_dev->vlan_id);
2316 *vlan_tci |= pkt_dev->vlan_p << 5;
2317 *vlan_tci |= pkt_dev->vlan_cfi << 4;
2318 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2319 *vlan_encapsulated_proto = __constant_htons(ETH_P_IP);
2322 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
2323 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2325 memcpy(eth, pkt_dev->hh, 12);
2326 *(u16 *) & eth[12] = protocol;
2328 /* Eth + IPh + UDPh + mpls */
2329 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2330 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2331 if (datalen < sizeof(struct pktgen_hdr))
2332 datalen = sizeof(struct pktgen_hdr);
2334 udph->source = htons(pkt_dev->cur_udp_src);
2335 udph->dest = htons(pkt_dev->cur_udp_dst);
2336 udph->len = htons(datalen + 8); /* DATA + udphdr */
2337 udph->check = 0; /* No checksum */
2343 iph->protocol = IPPROTO_UDP; /* UDP */
2344 iph->saddr = pkt_dev->cur_saddr;
2345 iph->daddr = pkt_dev->cur_daddr;
2347 iplen = 20 + 8 + datalen;
2348 iph->tot_len = htons(iplen);
2350 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2351 skb->protocol = protocol;
2352 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
2353 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2355 skb->pkt_type = PACKET_HOST;
2359 if (pkt_dev->nfrags <= 0)
2360 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2362 int frags = pkt_dev->nfrags;
2365 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2367 if (frags > MAX_SKB_FRAGS)
2368 frags = MAX_SKB_FRAGS;
2369 if (datalen > frags * PAGE_SIZE) {
2370 skb_put(skb, datalen - frags * PAGE_SIZE);
2371 datalen = frags * PAGE_SIZE;
2375 while (datalen > 0) {
2376 struct page *page = alloc_pages(GFP_KERNEL, 0);
2377 skb_shinfo(skb)->frags[i].page = page;
2378 skb_shinfo(skb)->frags[i].page_offset = 0;
2379 skb_shinfo(skb)->frags[i].size =
2380 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2381 datalen -= skb_shinfo(skb)->frags[i].size;
2382 skb->len += skb_shinfo(skb)->frags[i].size;
2383 skb->data_len += skb_shinfo(skb)->frags[i].size;
2385 skb_shinfo(skb)->nr_frags = i;
2394 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2398 skb_shinfo(skb)->frags[i - 1].size -= rem;
2400 skb_shinfo(skb)->frags[i] =
2401 skb_shinfo(skb)->frags[i - 1];
2402 get_page(skb_shinfo(skb)->frags[i].page);
2403 skb_shinfo(skb)->frags[i].page =
2404 skb_shinfo(skb)->frags[i - 1].page;
2405 skb_shinfo(skb)->frags[i].page_offset +=
2406 skb_shinfo(skb)->frags[i - 1].size;
2407 skb_shinfo(skb)->frags[i].size = rem;
2409 skb_shinfo(skb)->nr_frags = i;
2413 /* Stamp the time, and sequence number, convert them to network byte order */
2416 struct timeval timestamp;
2418 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2419 pgh->seq_num = htonl(pkt_dev->seq_num);
2421 do_gettimeofday(×tamp);
2422 pgh->tv_sec = htonl(timestamp.tv_sec);
2423 pgh->tv_usec = htonl(timestamp.tv_usec);
2430 * scan_ip6, fmt_ip taken from dietlibc-0.21
2431 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2433 * Slightly modified for kernel.
2434 * Should be candidate for net/ipv4/utils.c
2438 static unsigned int scan_ip6(const char *s, char ip[16])
2441 unsigned int len = 0;
2444 unsigned int prefixlen = 0;
2445 unsigned int suffixlen = 0;
2448 for (i = 0; i < 16; i++)
2454 if (s[1] == ':') { /* Found "::", skip to part 2 */
2463 u = simple_strtoul(s, &tmp, 16);
2469 if (prefixlen == 12 && s[i] == '.') {
2471 /* the last 4 bytes may be written as IPv4 address */
2474 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2477 ip[prefixlen++] = (u >> 8);
2478 ip[prefixlen++] = (u & 255);
2481 if (prefixlen == 16)
2485 /* part 2, after "::" */
2492 } else if (suffixlen != 0)
2496 u = simple_strtol(s, &tmp, 16);
2504 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2506 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2512 suffix[suffixlen++] = (u >> 8);
2513 suffix[suffixlen++] = (u & 255);
2516 if (prefixlen + suffixlen == 16)
2519 for (i = 0; i < suffixlen; i++)
2520 ip[16 - suffixlen + i] = suffix[i];
2524 static char tohex(char hexdigit)
2526 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2529 static int fmt_xlong(char *s, unsigned int i)
2532 *s = tohex((i >> 12) & 0xf);
2533 if (s != bak || *s != '0')
2535 *s = tohex((i >> 8) & 0xf);
2536 if (s != bak || *s != '0')
2538 *s = tohex((i >> 4) & 0xf);
2539 if (s != bak || *s != '0')
2541 *s = tohex(i & 0xf);
2545 static unsigned int fmt_ip6(char *s, const char ip[16])
2550 unsigned int compressing;
2555 for (j = 0; j < 16; j += 2) {
2557 #ifdef V4MAPPEDPREFIX
2558 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2559 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2564 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2565 (unsigned long)(unsigned char)ip[j + 1];
2580 i = fmt_xlong(s, temp);
2597 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2598 struct pktgen_dev *pkt_dev)
2600 struct sk_buff *skb = NULL;
2602 struct udphdr *udph;
2604 struct ipv6hdr *iph;
2605 struct pktgen_hdr *pgh = NULL;
2606 __be16 protocol = __constant_htons(ETH_P_IPV6);
2608 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2609 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2610 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2611 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2613 if (pkt_dev->nr_labels)
2614 protocol = __constant_htons(ETH_P_MPLS_UC);
2616 if (pkt_dev->vlan_id != 0xffff)
2617 protocol = __constant_htons(ETH_P_8021Q);
2619 /* Update any of the values, used when we're incrementing various
2622 mod_cur_headers(pkt_dev);
2624 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2625 pkt_dev->nr_labels*sizeof(u32) +
2626 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2629 sprintf(pkt_dev->result, "No memory");
2633 skb_reserve(skb, 16);
2635 /* Reserve for ethernet and IP header */
2636 eth = (__u8 *) skb_push(skb, 14);
2637 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2638 if (pkt_dev->nr_labels)
2639 mpls_push(mpls, pkt_dev);
2641 if (pkt_dev->vlan_id != 0xffff) {
2642 if(pkt_dev->svlan_id != 0xffff) {
2643 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2644 *svlan_tci = htons(pkt_dev->svlan_id);
2645 *svlan_tci |= pkt_dev->svlan_p << 5;
2646 *svlan_tci |= pkt_dev->svlan_cfi << 4;
2647 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2648 *svlan_encapsulated_proto = __constant_htons(ETH_P_8021Q);
2650 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2651 *vlan_tci = htons(pkt_dev->vlan_id);
2652 *vlan_tci |= pkt_dev->vlan_p << 5;
2653 *vlan_tci |= pkt_dev->vlan_cfi << 4;
2654 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2655 *vlan_encapsulated_proto = __constant_htons(ETH_P_IPV6);
2658 iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
2659 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2661 memcpy(eth, pkt_dev->hh, 12);
2662 *(u16 *) & eth[12] = protocol;
2664 /* Eth + IPh + UDPh + mpls */
2665 datalen = pkt_dev->cur_pkt_size - 14 -
2666 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2667 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2669 if (datalen < sizeof(struct pktgen_hdr)) {
2670 datalen = sizeof(struct pktgen_hdr);
2671 if (net_ratelimit())
2672 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2676 udph->source = htons(pkt_dev->cur_udp_src);
2677 udph->dest = htons(pkt_dev->cur_udp_dst);
2678 udph->len = htons(datalen + sizeof(struct udphdr));
2679 udph->check = 0; /* No checksum */
2681 *(u32 *) iph = __constant_htonl(0x60000000); /* Version + flow */
2683 iph->hop_limit = 32;
2685 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2686 iph->nexthdr = IPPROTO_UDP;
2688 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2689 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2691 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
2692 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2693 skb->protocol = protocol;
2695 skb->pkt_type = PACKET_HOST;
2696 skb->nh.ipv6h = iph;
2699 if (pkt_dev->nfrags <= 0)
2700 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2702 int frags = pkt_dev->nfrags;
2705 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2707 if (frags > MAX_SKB_FRAGS)
2708 frags = MAX_SKB_FRAGS;
2709 if (datalen > frags * PAGE_SIZE) {
2710 skb_put(skb, datalen - frags * PAGE_SIZE);
2711 datalen = frags * PAGE_SIZE;
2715 while (datalen > 0) {
2716 struct page *page = alloc_pages(GFP_KERNEL, 0);
2717 skb_shinfo(skb)->frags[i].page = page;
2718 skb_shinfo(skb)->frags[i].page_offset = 0;
2719 skb_shinfo(skb)->frags[i].size =
2720 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2721 datalen -= skb_shinfo(skb)->frags[i].size;
2722 skb->len += skb_shinfo(skb)->frags[i].size;
2723 skb->data_len += skb_shinfo(skb)->frags[i].size;
2725 skb_shinfo(skb)->nr_frags = i;
2734 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2738 skb_shinfo(skb)->frags[i - 1].size -= rem;
2740 skb_shinfo(skb)->frags[i] =
2741 skb_shinfo(skb)->frags[i - 1];
2742 get_page(skb_shinfo(skb)->frags[i].page);
2743 skb_shinfo(skb)->frags[i].page =
2744 skb_shinfo(skb)->frags[i - 1].page;
2745 skb_shinfo(skb)->frags[i].page_offset +=
2746 skb_shinfo(skb)->frags[i - 1].size;
2747 skb_shinfo(skb)->frags[i].size = rem;
2749 skb_shinfo(skb)->nr_frags = i;
2753 /* Stamp the time, and sequence number, convert them to network byte order */
2754 /* should we update cloned packets too ? */
2756 struct timeval timestamp;
2758 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2759 pgh->seq_num = htonl(pkt_dev->seq_num);
2761 do_gettimeofday(×tamp);
2762 pgh->tv_sec = htonl(timestamp.tv_sec);
2763 pgh->tv_usec = htonl(timestamp.tv_usec);
2765 /* pkt_dev->seq_num++; FF: you really mean this? */
2770 static inline struct sk_buff *fill_packet(struct net_device *odev,
2771 struct pktgen_dev *pkt_dev)
2773 if (pkt_dev->flags & F_IPV6)
2774 return fill_packet_ipv6(odev, pkt_dev);
2776 return fill_packet_ipv4(odev, pkt_dev);
2779 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2781 pkt_dev->seq_num = 1;
2782 pkt_dev->idle_acc = 0;
2784 pkt_dev->tx_bytes = 0;
2785 pkt_dev->errors = 0;
2788 /* Set up structure for sending pkts, clear counters */
2790 static void pktgen_run(struct pktgen_thread *t)
2792 struct pktgen_dev *pkt_dev;
2795 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
2798 list_for_each_entry(pkt_dev, &t->if_list, list) {
2801 * setup odev and create initial packet.
2803 pktgen_setup_inject(pkt_dev);
2805 if (pkt_dev->odev) {
2806 pktgen_clear_counters(pkt_dev);
2807 pkt_dev->running = 1; /* Cranke yeself! */
2808 pkt_dev->skb = NULL;
2809 pkt_dev->started_at = getCurUs();
2810 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
2811 pkt_dev->next_tx_ns = 0;
2813 strcpy(pkt_dev->result, "Starting");
2816 strcpy(pkt_dev->result, "Error starting");
2820 t->control &= ~(T_STOP);
2823 static void pktgen_stop_all_threads_ifs(void)
2825 struct pktgen_thread *t;
2827 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads_ifs.\n"));
2829 mutex_lock(&pktgen_thread_lock);
2831 list_for_each_entry(t, &pktgen_threads, th_list)
2832 t->control |= T_STOP;
2834 mutex_unlock(&pktgen_thread_lock);
2837 static int thread_is_running(struct pktgen_thread *t)
2839 struct pktgen_dev *pkt_dev;
2842 list_for_each_entry(pkt_dev, &t->if_list, list)
2843 if (pkt_dev->running) {
2850 static int pktgen_wait_thread_run(struct pktgen_thread *t)
2854 while (thread_is_running(t)) {
2858 msleep_interruptible(100);
2860 if (signal_pending(current))
2870 static int pktgen_wait_all_threads_run(void)
2872 struct pktgen_thread *t;
2875 mutex_lock(&pktgen_thread_lock);
2877 list_for_each_entry(t, &pktgen_threads, th_list) {
2878 sig = pktgen_wait_thread_run(t);
2884 list_for_each_entry(t, &pktgen_threads, th_list)
2885 t->control |= (T_STOP);
2887 mutex_unlock(&pktgen_thread_lock);
2891 static void pktgen_run_all_threads(void)
2893 struct pktgen_thread *t;
2895 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2897 mutex_lock(&pktgen_thread_lock);
2899 list_for_each_entry(t, &pktgen_threads, th_list)
2900 t->control |= (T_RUN);
2902 mutex_unlock(&pktgen_thread_lock);
2904 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2906 pktgen_wait_all_threads_run();
2909 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2911 __u64 total_us, bps, mbps, pps, idle;
2912 char *p = pkt_dev->result;
2914 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
2916 idle = pkt_dev->idle_acc;
2918 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2919 (unsigned long long)total_us,
2920 (unsigned long long)(total_us - idle),
2921 (unsigned long long)idle,
2922 (unsigned long long)pkt_dev->sofar,
2923 pkt_dev->cur_pkt_size, nr_frags);
2925 pps = pkt_dev->sofar * USEC_PER_SEC;
2927 while ((total_us >> 32) != 0) {
2932 do_div(pps, total_us);
2934 bps = pps * 8 * pkt_dev->cur_pkt_size;
2937 do_div(mbps, 1000000);
2938 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2939 (unsigned long long)pps,
2940 (unsigned long long)mbps,
2941 (unsigned long long)bps,
2942 (unsigned long long)pkt_dev->errors);
2945 /* Set stopped-at timer, remove from running list, do counters & statistics */
2947 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
2949 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
2951 if (!pkt_dev->running) {
2952 printk("pktgen: interface: %s is already stopped\n",
2957 pkt_dev->stopped_at = getCurUs();
2958 pkt_dev->running = 0;
2960 show_results(pkt_dev, nr_frags);
2965 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
2967 struct pktgen_dev *pkt_dev, *best = NULL;
2971 list_for_each_entry(pkt_dev, &t->if_list, list) {
2972 if (!pkt_dev->running)
2976 else if (pkt_dev->next_tx_us < best->next_tx_us)
2983 static void pktgen_stop(struct pktgen_thread *t)
2985 struct pktgen_dev *pkt_dev;
2987 PG_DEBUG(printk("pktgen: entering pktgen_stop\n"));
2991 list_for_each_entry(pkt_dev, &t->if_list, list) {
2992 pktgen_stop_device(pkt_dev);
2994 kfree_skb(pkt_dev->skb);
2996 pkt_dev->skb = NULL;
3003 * one of our devices needs to be removed - find it
3006 static void pktgen_rem_one_if(struct pktgen_thread *t)
3008 struct list_head *q, *n;
3009 struct pktgen_dev *cur;
3011 PG_DEBUG(printk("pktgen: entering pktgen_rem_one_if\n"));
3015 list_for_each_safe(q, n, &t->if_list) {
3016 cur = list_entry(q, struct pktgen_dev, list);
3018 if (!cur->removal_mark)
3022 kfree_skb(cur->skb);
3025 pktgen_remove_device(t, cur);
3033 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3035 struct list_head *q, *n;
3036 struct pktgen_dev *cur;
3038 /* Remove all devices, free mem */
3040 PG_DEBUG(printk("pktgen: entering pktgen_rem_all_ifs\n"));
3043 list_for_each_safe(q, n, &t->if_list) {
3044 cur = list_entry(q, struct pktgen_dev, list);
3047 kfree_skb(cur->skb);
3050 pktgen_remove_device(t, cur);
3056 static void pktgen_rem_thread(struct pktgen_thread *t)
3058 /* Remove from the thread list */
3060 remove_proc_entry(t->name, pg_proc_dir);
3062 mutex_lock(&pktgen_thread_lock);
3064 list_del(&t->th_list);
3066 mutex_unlock(&pktgen_thread_lock);
3069 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3071 struct net_device *odev = NULL;
3072 __u64 idle_start = 0;
3075 odev = pkt_dev->odev;
3077 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3081 if (now < pkt_dev->next_tx_us)
3082 spin(pkt_dev, pkt_dev->next_tx_us);
3084 /* This is max DELAY, this has special meaning of
3087 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3088 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3089 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3094 if (netif_queue_stopped(odev) || need_resched()) {
3095 idle_start = getCurUs();
3097 if (!netif_running(odev)) {
3098 pktgen_stop_device(pkt_dev);
3100 kfree_skb(pkt_dev->skb);
3101 pkt_dev->skb = NULL;
3107 pkt_dev->idle_acc += getCurUs() - idle_start;
3109 if (netif_queue_stopped(odev)) {
3110 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3111 pkt_dev->next_tx_ns = 0;
3112 goto out; /* Try the next interface */
3116 if (pkt_dev->last_ok || !pkt_dev->skb) {
3117 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3118 || (!pkt_dev->skb)) {
3119 /* build a new pkt */
3121 kfree_skb(pkt_dev->skb);
3123 pkt_dev->skb = fill_packet(odev, pkt_dev);
3124 if (pkt_dev->skb == NULL) {
3125 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3127 pkt_dev->clone_count--; /* back out increment, OOM */
3130 pkt_dev->allocated_skbs++;
3131 pkt_dev->clone_count = 0; /* reset counter */
3135 netif_tx_lock_bh(odev);
3136 if (!netif_queue_stopped(odev)) {
3138 atomic_inc(&(pkt_dev->skb->users));
3140 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3141 if (likely(ret == NETDEV_TX_OK)) {
3142 pkt_dev->last_ok = 1;
3145 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3147 } else if (ret == NETDEV_TX_LOCKED
3148 && (odev->features & NETIF_F_LLTX)) {
3151 } else { /* Retry it next time */
3153 atomic_dec(&(pkt_dev->skb->users));
3155 if (debug && net_ratelimit())
3156 printk(KERN_INFO "pktgen: Hard xmit error\n");
3159 pkt_dev->last_ok = 0;
3162 pkt_dev->next_tx_us = getCurUs();
3163 pkt_dev->next_tx_ns = 0;
3165 pkt_dev->next_tx_us += pkt_dev->delay_us;
3166 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3168 if (pkt_dev->next_tx_ns > 1000) {
3169 pkt_dev->next_tx_us++;
3170 pkt_dev->next_tx_ns -= 1000;
3174 else { /* Retry it next time */
3175 pkt_dev->last_ok = 0;
3176 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3177 pkt_dev->next_tx_ns = 0;
3180 netif_tx_unlock_bh(odev);
3182 /* If pkt_dev->count is zero, then run forever */
3183 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3184 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3185 idle_start = getCurUs();
3186 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3187 if (signal_pending(current)) {
3192 pkt_dev->idle_acc += getCurUs() - idle_start;
3195 /* Done with this */
3196 pktgen_stop_device(pkt_dev);
3198 kfree_skb(pkt_dev->skb);
3199 pkt_dev->skb = NULL;
3205 * Main loop of the thread goes here
3208 static void pktgen_thread_worker(struct pktgen_thread *t)
3211 struct pktgen_dev *pkt_dev = NULL;
3214 u32 max_before_softirq;
3215 u32 tx_since_softirq = 0;
3217 daemonize("pktgen/%d", cpu);
3219 /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
3221 spin_lock_irq(¤t->sighand->siglock);
3222 tmpsig = current->blocked;
3223 siginitsetinv(¤t->blocked,
3224 sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGTERM));
3226 recalc_sigpending();
3227 spin_unlock_irq(¤t->sighand->siglock);
3229 /* Migrate to the right CPU */
3230 set_cpus_allowed(current, cpumask_of_cpu(cpu));
3231 if (smp_processor_id() != cpu)
3234 init_waitqueue_head(&t->queue);
3236 t->control &= ~(T_TERMINATE);
3237 t->control &= ~(T_RUN);
3238 t->control &= ~(T_STOP);
3239 t->control &= ~(T_REMDEVALL);
3240 t->control &= ~(T_REMDEV);
3242 t->pid = current->pid;
3244 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
3246 max_before_softirq = t->max_before_softirq;
3248 __set_current_state(TASK_INTERRUPTIBLE);
3253 __set_current_state(TASK_RUNNING);
3256 * Get next dev to xmit -- if any.
3259 pkt_dev = next_to_run(t);
3263 pktgen_xmit(pkt_dev);
3266 * We like to stay RUNNING but must also give
3267 * others fair share.
3270 tx_since_softirq += pkt_dev->last_ok;
3272 if (tx_since_softirq > max_before_softirq) {
3273 if (local_softirq_pending())
3275 tx_since_softirq = 0;
3278 prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
3279 schedule_timeout(HZ / 10);
3280 finish_wait(&(t->queue), &wait);
3284 * Back from sleep, either due to the timeout or signal.
3285 * We check if we have any "posted" work for us.
3288 if (t->control & T_TERMINATE || signal_pending(current))
3289 /* we received a request to terminate ourself */
3292 if (t->control & T_STOP) {
3294 t->control &= ~(T_STOP);
3297 if (t->control & T_RUN) {
3299 t->control &= ~(T_RUN);
3302 if (t->control & T_REMDEVALL) {
3303 pktgen_rem_all_ifs(t);
3304 t->control &= ~(T_REMDEVALL);
3307 if (t->control & T_REMDEV) {
3308 pktgen_rem_one_if(t);
3309 t->control &= ~(T_REMDEV);
3316 PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
3319 PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
3320 pktgen_rem_all_ifs(t);
3322 PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
3323 pktgen_rem_thread(t);
3328 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3331 struct pktgen_dev *p, *pkt_dev = NULL;
3334 list_for_each_entry(p, &t->if_list, list)
3335 if (strncmp(p->ifname, ifname, IFNAMSIZ) == 0) {
3341 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev));
3346 * Adds a dev at front of if_list.
3349 static int add_dev_to_thread(struct pktgen_thread *t,
3350 struct pktgen_dev *pkt_dev)
3356 if (pkt_dev->pg_thread) {
3357 printk("pktgen: ERROR: already assigned to a thread.\n");
3362 list_add(&pkt_dev->list, &t->if_list);
3363 pkt_dev->pg_thread = t;
3364 pkt_dev->running = 0;
3371 /* Called under thread lock */
3373 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3375 struct pktgen_dev *pkt_dev;
3376 struct proc_dir_entry *pe;
3378 /* We don't allow a device to be on several threads */
3380 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3382 printk("pktgen: ERROR: interface already used.\n");
3386 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3390 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3391 if (pkt_dev->flows == NULL) {
3395 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3397 pkt_dev->removal_mark = 0;
3398 pkt_dev->min_pkt_size = ETH_ZLEN;
3399 pkt_dev->max_pkt_size = ETH_ZLEN;
3400 pkt_dev->nfrags = 0;
3401 pkt_dev->clone_skb = pg_clone_skb_d;
3402 pkt_dev->delay_us = pg_delay_d / 1000;
3403 pkt_dev->delay_ns = pg_delay_d % 1000;
3404 pkt_dev->count = pg_count_d;
3406 pkt_dev->udp_src_min = 9; /* sink port */
3407 pkt_dev->udp_src_max = 9;
3408 pkt_dev->udp_dst_min = 9;
3409 pkt_dev->udp_dst_max = 9;
3411 pkt_dev->vlan_p = 0;
3412 pkt_dev->vlan_cfi = 0;
3413 pkt_dev->vlan_id = 0xffff;
3414 pkt_dev->svlan_p = 0;
3415 pkt_dev->svlan_cfi = 0;
3416 pkt_dev->svlan_id = 0xffff;
3418 strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
3420 if (!pktgen_setup_dev(pkt_dev)) {
3421 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
3423 vfree(pkt_dev->flows);
3428 pe = create_proc_entry(ifname, 0600, pg_proc_dir);
3430 printk("pktgen: cannot create %s/%s procfs entry.\n",
3431 PG_PROC_DIR, ifname);
3433 vfree(pkt_dev->flows);
3437 pe->proc_fops = &pktgen_if_fops;
3440 return add_dev_to_thread(t, pkt_dev);
3443 static struct pktgen_thread *__init pktgen_find_thread(const char *name)
3445 struct pktgen_thread *t;
3447 mutex_lock(&pktgen_thread_lock);
3449 list_for_each_entry(t, &pktgen_threads, th_list)
3450 if (strcmp(t->name, name) == 0) {
3451 mutex_unlock(&pktgen_thread_lock);
3455 mutex_unlock(&pktgen_thread_lock);
3459 static int __init pktgen_create_thread(const char *name, int cpu)
3462 struct pktgen_thread *t = NULL;
3463 struct proc_dir_entry *pe;
3465 if (strlen(name) > 31) {
3466 printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
3470 if (pktgen_find_thread(name)) {
3471 printk("pktgen: ERROR: thread: %s already exists\n", name);
3475 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3477 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3481 strcpy(t->name, name);
3482 spin_lock_init(&t->if_lock);
3485 pe = create_proc_entry(t->name, 0600, pg_proc_dir);
3487 printk("pktgen: cannot create %s/%s procfs entry.\n",
3488 PG_PROC_DIR, t->name);
3493 pe->proc_fops = &pktgen_thread_fops;
3496 INIT_LIST_HEAD(&t->if_list);
3498 list_add_tail(&t->th_list, &pktgen_threads);
3502 err = kernel_thread((void *)pktgen_thread_worker, (void *)t,
3503 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
3505 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3506 remove_proc_entry(t->name, pg_proc_dir);
3507 list_del(&t->th_list);
3516 * Removes a device from the thread if_list.
3518 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3519 struct pktgen_dev *pkt_dev)
3521 struct list_head *q, *n;
3522 struct pktgen_dev *p;
3524 list_for_each_safe(q, n, &t->if_list) {
3525 p = list_entry(q, struct pktgen_dev, list);
3531 static int pktgen_remove_device(struct pktgen_thread *t,
3532 struct pktgen_dev *pkt_dev)
3535 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
3537 if (pkt_dev->running) {
3538 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3539 pktgen_stop_device(pkt_dev);
3542 /* Dis-associate from the interface */
3544 if (pkt_dev->odev) {
3545 dev_put(pkt_dev->odev);
3546 pkt_dev->odev = NULL;
3549 /* And update the thread if_list */
3551 _rem_dev_from_if_list(t, pkt_dev);
3553 /* Clean up proc file system */
3555 remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
3558 vfree(pkt_dev->flows);
3563 static int __init pg_init(void)
3566 struct proc_dir_entry *pe;
3570 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3573 pg_proc_dir->owner = THIS_MODULE;
3575 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3577 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3579 proc_net_remove(PG_PROC_DIR);
3583 pe->proc_fops = &pktgen_fops;
3586 /* Register us to receive netdevice events */
3587 register_netdevice_notifier(&pktgen_notifier_block);
3589 for_each_online_cpu(cpu) {
3593 sprintf(buf, "kpktgend_%i", cpu);
3594 err = pktgen_create_thread(buf, cpu);
3596 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3600 if (list_empty(&pktgen_threads)) {
3601 printk("pktgen: ERROR: Initialization failed for all threads\n");
3602 unregister_netdevice_notifier(&pktgen_notifier_block);
3603 remove_proc_entry(PGCTRL, pg_proc_dir);
3604 proc_net_remove(PG_PROC_DIR);
3611 static void __exit pg_cleanup(void)
3613 struct pktgen_thread *t;
3614 struct list_head *q, *n;
3615 wait_queue_head_t queue;
3616 init_waitqueue_head(&queue);
3618 /* Stop all interfaces & threads */
3620 list_for_each_safe(q, n, &pktgen_threads) {
3621 t = list_entry(q, struct pktgen_thread, th_list);
3622 t->control |= (T_TERMINATE);
3624 wait_event_interruptible_timeout(queue, (t->removed == 1), HZ);
3627 /* Un-register us from receiving netdevice events */
3628 unregister_netdevice_notifier(&pktgen_notifier_block);
3630 /* Clean up proc file system */
3631 remove_proc_entry(PGCTRL, pg_proc_dir);
3632 proc_net_remove(PG_PROC_DIR);
3635 module_init(pg_init);
3636 module_exit(pg_cleanup);
3638 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3639 MODULE_DESCRIPTION("Packet Generator tool");
3640 MODULE_LICENSE("GPL");
3641 module_param(pg_count_d, int, 0);
3642 module_param(pg_delay_d, int, 0);
3643 module_param(pg_clone_skb_d, int, 0);
3644 module_param(debug, int, 0);