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/freezer.h>
133 #include <linux/delay.h>
134 #include <linux/timer.h>
135 #include <linux/list.h>
136 #include <linux/init.h>
137 #include <linux/skbuff.h>
138 #include <linux/netdevice.h>
139 #include <linux/inet.h>
140 #include <linux/inetdevice.h>
141 #include <linux/rtnetlink.h>
142 #include <linux/if_arp.h>
143 #include <linux/if_vlan.h>
144 #include <linux/in.h>
145 #include <linux/ip.h>
146 #include <linux/ipv6.h>
147 #include <linux/udp.h>
148 #include <linux/proc_fs.h>
149 #include <linux/seq_file.h>
150 #include <linux/wait.h>
151 #include <linux/etherdevice.h>
152 #include <linux/kthread.h>
153 #include <net/checksum.h>
154 #include <net/ipv6.h>
155 #include <net/addrconf.h>
156 #include <asm/byteorder.h>
157 #include <linux/rcupdate.h>
158 #include <asm/bitops.h>
161 #include <asm/uaccess.h>
162 #include <asm/div64.h> /* do_div */
163 #include <asm/timex.h>
165 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
167 /* The buckets are exponential in 'width' */
168 #define LAT_BUCKETS_MAX 32
169 #define IP_NAME_SZ 32
170 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
171 #define MPLS_STACK_BOTTOM htonl(0x00000100)
173 /* Device flag bits */
174 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
175 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
176 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
177 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
178 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
179 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
180 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
181 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
182 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
183 #define F_VID_RND (1<<9) /* Random VLAN ID */
184 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
186 /* Thread control flag bits */
187 #define T_TERMINATE (1<<0)
188 #define T_STOP (1<<1) /* Stop run */
189 #define T_RUN (1<<2) /* Start run */
190 #define T_REMDEVALL (1<<3) /* Remove all devs */
191 #define T_REMDEV (1<<4) /* Remove one dev */
193 /* If lock -- can be removed after some work */
194 #define if_lock(t) spin_lock(&(t->if_lock));
195 #define if_unlock(t) spin_unlock(&(t->if_lock));
197 /* Used to help with determining the pkts on receive */
198 #define PKTGEN_MAGIC 0xbe9be955
199 #define PG_PROC_DIR "pktgen"
200 #define PGCTRL "pgctrl"
201 static struct proc_dir_entry *pg_proc_dir = NULL;
203 #define MAX_CFLOWS 65536
205 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
206 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
216 * Try to keep frequent/infrequent used vars. separated.
219 char ifname[IFNAMSIZ];
222 struct pktgen_thread *pg_thread; /* the owner */
223 struct list_head list; /* Used for chaining in the thread's run-queue */
225 int running; /* if this changes to false, the test will stop */
227 /* If min != max, then we will either do a linear iteration, or
228 * we will do a random selection from within the range.
231 int removal_mark; /* non-zero => the device is marked for
232 * removal by worker thread */
234 int min_pkt_size; /* = ETH_ZLEN; */
235 int max_pkt_size; /* = ETH_ZLEN; */
237 __u32 delay_us; /* Default delay */
239 __u64 count; /* Default No packets to send */
240 __u64 sofar; /* How many pkts we've sent so far */
241 __u64 tx_bytes; /* How many bytes we've transmitted */
242 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
244 /* runtime counters relating to clone_skb */
245 __u64 next_tx_us; /* timestamp of when to tx next */
248 __u64 allocated_skbs;
250 int last_ok; /* Was last skb sent?
251 * Or a failed transmit of some sort? This will keep
252 * sequence numbers in order, for example.
254 __u64 started_at; /* micro-seconds */
255 __u64 stopped_at; /* micro-seconds */
256 __u64 idle_acc; /* micro-seconds */
259 int clone_skb; /* Use multiple SKBs during packet gen. If this number
260 * is greater than 1, then that many copies of the same
261 * packet will be sent before a new packet is allocated.
262 * For instance, if you want to send 1024 identical packets
263 * before creating a new packet, set clone_skb to 1024.
266 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
267 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
268 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
269 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
271 struct in6_addr in6_saddr;
272 struct in6_addr in6_daddr;
273 struct in6_addr cur_in6_daddr;
274 struct in6_addr cur_in6_saddr;
276 struct in6_addr min_in6_daddr;
277 struct in6_addr max_in6_daddr;
278 struct in6_addr min_in6_saddr;
279 struct in6_addr max_in6_saddr;
281 /* If we're doing ranges, random or incremental, then this
282 * defines the min/max for those ranges.
284 __be32 saddr_min; /* inclusive, source IP address */
285 __be32 saddr_max; /* exclusive, source IP address */
286 __be32 daddr_min; /* inclusive, dest IP address */
287 __be32 daddr_max; /* exclusive, dest IP address */
289 __u16 udp_src_min; /* inclusive, source UDP port */
290 __u16 udp_src_max; /* exclusive, source UDP port */
291 __u16 udp_dst_min; /* inclusive, dest UDP port */
292 __u16 udp_dst_max; /* exclusive, dest UDP port */
295 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
296 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
299 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
300 __be32 labels[MAX_MPLS_LABELS];
302 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
305 __u16 vlan_id; /* 0xffff means no vlan tag */
309 __u16 svlan_id; /* 0xffff means no svlan tag */
311 __u32 src_mac_count; /* How many MACs to iterate through */
312 __u32 dst_mac_count; /* How many MACs to iterate through */
314 unsigned char dst_mac[ETH_ALEN];
315 unsigned char src_mac[ETH_ALEN];
317 __u32 cur_dst_mac_offset;
318 __u32 cur_src_mac_offset;
327 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
329 We fill in SRC address later
330 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
334 __u16 pad; /* pad out the hh struct to an even 16 bytes */
336 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
337 * are transmitting the same one multiple times
339 struct net_device *odev; /* The out-going device. Note that the device should
340 * have it's pg_info pointer pointing back to this
341 * device. This will be set when the user specifies
342 * the out-going device name (not when the inject is
343 * started as it used to do.)
345 struct flow_state *flows;
346 unsigned cflows; /* Concurrent flows (config) */
347 unsigned lflow; /* Flow length (config) */
348 unsigned nflows; /* accumulated flows (stats) */
358 struct pktgen_thread {
360 struct list_head if_list; /* All device here */
361 struct list_head th_list;
362 struct task_struct *tsk;
364 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
366 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
372 wait_queue_head_t queue;
378 /* This code works around the fact that do_div cannot handle two 64-bit
379 numbers, and regular 64-bit division doesn't work on x86 kernels.
385 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
386 * Function copied/adapted/optimized from:
388 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
390 * Copyright 1994, University of Cambridge Computer Laboratory
391 * All Rights Reserved.
394 static inline s64 divremdi3(s64 x, s64 y, int type)
396 u64 a = (x < 0) ? -x : x;
397 u64 b = (y < 0) ? -y : y;
417 if (PG_DIV == type) {
418 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
420 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
424 /* End of hacks to deal with 64-bit math on x86 */
426 /** Convert to milliseconds */
427 static inline __u64 tv_to_ms(const struct timeval *tv)
429 __u64 ms = tv->tv_usec / 1000;
430 ms += (__u64) tv->tv_sec * (__u64) 1000;
434 /** Convert to micro-seconds */
435 static inline __u64 tv_to_us(const struct timeval *tv)
437 __u64 us = tv->tv_usec;
438 us += (__u64) tv->tv_sec * (__u64) 1000000;
442 static inline __u64 pg_div(__u64 n, __u32 base)
446 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
451 static inline __u64 pg_div64(__u64 n, __u64 base)
455 * How do we know if the architecture we are running on
456 * supports division with 64 bit base?
459 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
463 tmp = divremdi3(n, base, PG_DIV);
468 static inline __u64 getCurMs(void)
471 do_gettimeofday(&tv);
472 return tv_to_ms(&tv);
475 static inline __u64 getCurUs(void)
478 do_gettimeofday(&tv);
479 return tv_to_us(&tv);
482 static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
484 return tv_to_us(a) - tv_to_us(b);
487 /* old include end */
489 static char version[] __initdata = VERSION;
491 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
492 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
493 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
495 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
496 static void pktgen_run_all_threads(void);
497 static void pktgen_stop_all_threads_ifs(void);
498 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
499 static void pktgen_stop(struct pktgen_thread *t);
500 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
501 static int pktgen_mark_device(const char *ifname);
502 static unsigned int scan_ip6(const char *s, char ip[16]);
503 static unsigned int fmt_ip6(char *s, const char ip[16]);
505 /* Module parameters, defaults. */
506 static int pg_count_d = 1000; /* 1000 pkts by default */
507 static int pg_delay_d;
508 static int pg_clone_skb_d;
511 static DEFINE_MUTEX(pktgen_thread_lock);
512 static LIST_HEAD(pktgen_threads);
514 static struct notifier_block pktgen_notifier_block = {
515 .notifier_call = pktgen_device_event,
519 * /proc handling functions
523 static int pgctrl_show(struct seq_file *seq, void *v)
525 seq_puts(seq, VERSION);
529 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
530 size_t count, loff_t * ppos)
535 if (!capable(CAP_NET_ADMIN)) {
540 if (count > sizeof(data))
541 count = sizeof(data);
543 if (copy_from_user(data, buf, count)) {
547 data[count - 1] = 0; /* Make string */
549 if (!strcmp(data, "stop"))
550 pktgen_stop_all_threads_ifs();
552 else if (!strcmp(data, "start"))
553 pktgen_run_all_threads();
556 printk("pktgen: Unknown command: %s\n", data);
564 static int pgctrl_open(struct inode *inode, struct file *file)
566 return single_open(file, pgctrl_show, PDE(inode)->data);
569 static const struct file_operations pktgen_fops = {
570 .owner = THIS_MODULE,
574 .write = pgctrl_write,
575 .release = single_release,
578 static int pktgen_if_show(struct seq_file *seq, void *v)
581 struct pktgen_dev *pkt_dev = seq->private;
584 __u64 now = getCurUs();
587 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
588 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
589 pkt_dev->max_pkt_size);
592 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
594 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
595 pkt_dev->clone_skb, pkt_dev->ifname);
597 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
600 if (pkt_dev->flags & F_IPV6) {
601 char b1[128], b2[128], b3[128];
602 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
603 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
604 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
606 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
609 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
610 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
611 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
613 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
618 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
619 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
622 seq_puts(seq, " src_mac: ");
624 if (is_zero_ether_addr(pkt_dev->src_mac))
625 for (i = 0; i < 6; i++)
626 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
629 for (i = 0; i < 6; i++)
630 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
633 seq_printf(seq, "dst_mac: ");
634 for (i = 0; i < 6; i++)
635 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
636 i == 5 ? "\n" : ":");
639 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
640 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
641 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
644 " src_mac_count: %d dst_mac_count: %d\n",
645 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
647 if (pkt_dev->nr_labels) {
649 seq_printf(seq, " mpls: ");
650 for(i = 0; i < pkt_dev->nr_labels; i++)
651 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
652 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
655 if (pkt_dev->vlan_id != 0xffff) {
656 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
657 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
660 if (pkt_dev->svlan_id != 0xffff) {
661 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
662 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
666 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
669 if (pkt_dev->traffic_class) {
670 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
673 seq_printf(seq, " Flags: ");
675 if (pkt_dev->flags & F_IPV6)
676 seq_printf(seq, "IPV6 ");
678 if (pkt_dev->flags & F_IPSRC_RND)
679 seq_printf(seq, "IPSRC_RND ");
681 if (pkt_dev->flags & F_IPDST_RND)
682 seq_printf(seq, "IPDST_RND ");
684 if (pkt_dev->flags & F_TXSIZE_RND)
685 seq_printf(seq, "TXSIZE_RND ");
687 if (pkt_dev->flags & F_UDPSRC_RND)
688 seq_printf(seq, "UDPSRC_RND ");
690 if (pkt_dev->flags & F_UDPDST_RND)
691 seq_printf(seq, "UDPDST_RND ");
693 if (pkt_dev->flags & F_MPLS_RND)
694 seq_printf(seq, "MPLS_RND ");
696 if (pkt_dev->flags & F_MACSRC_RND)
697 seq_printf(seq, "MACSRC_RND ");
699 if (pkt_dev->flags & F_MACDST_RND)
700 seq_printf(seq, "MACDST_RND ");
702 if (pkt_dev->flags & F_VID_RND)
703 seq_printf(seq, "VID_RND ");
705 if (pkt_dev->flags & F_SVID_RND)
706 seq_printf(seq, "SVID_RND ");
710 sa = pkt_dev->started_at;
711 stopped = pkt_dev->stopped_at;
712 if (pkt_dev->running)
713 stopped = now; /* not really stopped, more like last-running-at */
716 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
717 (unsigned long long)pkt_dev->sofar,
718 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
719 (unsigned long long)stopped,
720 (unsigned long long)pkt_dev->idle_acc);
723 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
724 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
725 pkt_dev->cur_src_mac_offset);
727 if (pkt_dev->flags & F_IPV6) {
728 char b1[128], b2[128];
729 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
730 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
731 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
733 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
734 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
736 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
737 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
739 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
741 if (pkt_dev->result[0])
742 seq_printf(seq, "Result: %s\n", pkt_dev->result);
744 seq_printf(seq, "Result: Idle\n");
750 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
755 for(; i < maxlen; i++) {
758 if (get_user(c, &user_buffer[i]))
760 if ((c >= '0') && (c <= '9'))
762 else if ((c >= 'a') && (c <= 'f'))
763 *num |= c - 'a' + 10;
764 else if ((c >= 'A') && (c <= 'F'))
765 *num |= c - 'A' + 10;
772 static int count_trail_chars(const char __user * user_buffer,
777 for (i = 0; i < maxlen; i++) {
779 if (get_user(c, &user_buffer[i]))
797 static unsigned long num_arg(const char __user * user_buffer,
798 unsigned long maxlen, unsigned long *num)
803 for (; i < maxlen; i++) {
805 if (get_user(c, &user_buffer[i]))
807 if ((c >= '0') && (c <= '9')) {
816 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
820 for (; i < maxlen; i++) {
822 if (get_user(c, &user_buffer[i]))
840 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
847 pkt_dev->nr_labels = 0;
850 len = hex32_arg(&buffer[i], 8, &tmp);
853 pkt_dev->labels[n] = htonl(tmp);
854 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
855 pkt_dev->flags |= F_MPLS_RND;
857 if (get_user(c, &buffer[i]))
861 if (n >= MAX_MPLS_LABELS)
865 pkt_dev->nr_labels = n;
869 static ssize_t pktgen_if_write(struct file *file,
870 const char __user * user_buffer, size_t count,
873 struct seq_file *seq = (struct seq_file *)file->private_data;
874 struct pktgen_dev *pkt_dev = seq->private;
876 char name[16], valstr[32];
877 unsigned long value = 0;
878 char *pg_result = NULL;
882 pg_result = &(pkt_dev->result[0]);
885 printk("pktgen: wrong command format\n");
890 tmp = count_trail_chars(&user_buffer[i], max);
892 printk("pktgen: illegal format\n");
897 /* Read variable name */
899 len = strn_len(&user_buffer[i], sizeof(name) - 1);
903 memset(name, 0, sizeof(name));
904 if (copy_from_user(name, &user_buffer[i], len))
909 len = count_trail_chars(&user_buffer[i], max);
917 if (copy_from_user(tb, user_buffer, count))
920 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
921 (unsigned long)count, tb);
924 if (!strcmp(name, "min_pkt_size")) {
925 len = num_arg(&user_buffer[i], 10, &value);
930 if (value < 14 + 20 + 8)
932 if (value != pkt_dev->min_pkt_size) {
933 pkt_dev->min_pkt_size = value;
934 pkt_dev->cur_pkt_size = value;
936 sprintf(pg_result, "OK: min_pkt_size=%u",
937 pkt_dev->min_pkt_size);
941 if (!strcmp(name, "max_pkt_size")) {
942 len = num_arg(&user_buffer[i], 10, &value);
947 if (value < 14 + 20 + 8)
949 if (value != pkt_dev->max_pkt_size) {
950 pkt_dev->max_pkt_size = value;
951 pkt_dev->cur_pkt_size = value;
953 sprintf(pg_result, "OK: max_pkt_size=%u",
954 pkt_dev->max_pkt_size);
958 /* Shortcut for min = max */
960 if (!strcmp(name, "pkt_size")) {
961 len = num_arg(&user_buffer[i], 10, &value);
966 if (value < 14 + 20 + 8)
968 if (value != pkt_dev->min_pkt_size) {
969 pkt_dev->min_pkt_size = value;
970 pkt_dev->max_pkt_size = value;
971 pkt_dev->cur_pkt_size = value;
973 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
977 if (!strcmp(name, "debug")) {
978 len = num_arg(&user_buffer[i], 10, &value);
984 sprintf(pg_result, "OK: debug=%u", debug);
988 if (!strcmp(name, "frags")) {
989 len = num_arg(&user_buffer[i], 10, &value);
994 pkt_dev->nfrags = value;
995 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
998 if (!strcmp(name, "delay")) {
999 len = num_arg(&user_buffer[i], 10, &value);
1004 if (value == 0x7FFFFFFF) {
1005 pkt_dev->delay_us = 0x7FFFFFFF;
1006 pkt_dev->delay_ns = 0;
1008 pkt_dev->delay_us = value / 1000;
1009 pkt_dev->delay_ns = value % 1000;
1011 sprintf(pg_result, "OK: delay=%u",
1012 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
1015 if (!strcmp(name, "udp_src_min")) {
1016 len = num_arg(&user_buffer[i], 10, &value);
1021 if (value != pkt_dev->udp_src_min) {
1022 pkt_dev->udp_src_min = value;
1023 pkt_dev->cur_udp_src = value;
1025 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1028 if (!strcmp(name, "udp_dst_min")) {
1029 len = num_arg(&user_buffer[i], 10, &value);
1034 if (value != pkt_dev->udp_dst_min) {
1035 pkt_dev->udp_dst_min = value;
1036 pkt_dev->cur_udp_dst = value;
1038 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1041 if (!strcmp(name, "udp_src_max")) {
1042 len = num_arg(&user_buffer[i], 10, &value);
1047 if (value != pkt_dev->udp_src_max) {
1048 pkt_dev->udp_src_max = value;
1049 pkt_dev->cur_udp_src = value;
1051 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1054 if (!strcmp(name, "udp_dst_max")) {
1055 len = num_arg(&user_buffer[i], 10, &value);
1060 if (value != pkt_dev->udp_dst_max) {
1061 pkt_dev->udp_dst_max = value;
1062 pkt_dev->cur_udp_dst = value;
1064 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1067 if (!strcmp(name, "clone_skb")) {
1068 len = num_arg(&user_buffer[i], 10, &value);
1073 pkt_dev->clone_skb = value;
1075 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1078 if (!strcmp(name, "count")) {
1079 len = num_arg(&user_buffer[i], 10, &value);
1084 pkt_dev->count = value;
1085 sprintf(pg_result, "OK: count=%llu",
1086 (unsigned long long)pkt_dev->count);
1089 if (!strcmp(name, "src_mac_count")) {
1090 len = num_arg(&user_buffer[i], 10, &value);
1095 if (pkt_dev->src_mac_count != value) {
1096 pkt_dev->src_mac_count = value;
1097 pkt_dev->cur_src_mac_offset = 0;
1099 sprintf(pg_result, "OK: src_mac_count=%d",
1100 pkt_dev->src_mac_count);
1103 if (!strcmp(name, "dst_mac_count")) {
1104 len = num_arg(&user_buffer[i], 10, &value);
1109 if (pkt_dev->dst_mac_count != value) {
1110 pkt_dev->dst_mac_count = value;
1111 pkt_dev->cur_dst_mac_offset = 0;
1113 sprintf(pg_result, "OK: dst_mac_count=%d",
1114 pkt_dev->dst_mac_count);
1117 if (!strcmp(name, "flag")) {
1120 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1124 if (copy_from_user(f, &user_buffer[i], len))
1127 if (strcmp(f, "IPSRC_RND") == 0)
1128 pkt_dev->flags |= F_IPSRC_RND;
1130 else if (strcmp(f, "!IPSRC_RND") == 0)
1131 pkt_dev->flags &= ~F_IPSRC_RND;
1133 else if (strcmp(f, "TXSIZE_RND") == 0)
1134 pkt_dev->flags |= F_TXSIZE_RND;
1136 else if (strcmp(f, "!TXSIZE_RND") == 0)
1137 pkt_dev->flags &= ~F_TXSIZE_RND;
1139 else if (strcmp(f, "IPDST_RND") == 0)
1140 pkt_dev->flags |= F_IPDST_RND;
1142 else if (strcmp(f, "!IPDST_RND") == 0)
1143 pkt_dev->flags &= ~F_IPDST_RND;
1145 else if (strcmp(f, "UDPSRC_RND") == 0)
1146 pkt_dev->flags |= F_UDPSRC_RND;
1148 else if (strcmp(f, "!UDPSRC_RND") == 0)
1149 pkt_dev->flags &= ~F_UDPSRC_RND;
1151 else if (strcmp(f, "UDPDST_RND") == 0)
1152 pkt_dev->flags |= F_UDPDST_RND;
1154 else if (strcmp(f, "!UDPDST_RND") == 0)
1155 pkt_dev->flags &= ~F_UDPDST_RND;
1157 else if (strcmp(f, "MACSRC_RND") == 0)
1158 pkt_dev->flags |= F_MACSRC_RND;
1160 else if (strcmp(f, "!MACSRC_RND") == 0)
1161 pkt_dev->flags &= ~F_MACSRC_RND;
1163 else if (strcmp(f, "MACDST_RND") == 0)
1164 pkt_dev->flags |= F_MACDST_RND;
1166 else if (strcmp(f, "!MACDST_RND") == 0)
1167 pkt_dev->flags &= ~F_MACDST_RND;
1169 else if (strcmp(f, "MPLS_RND") == 0)
1170 pkt_dev->flags |= F_MPLS_RND;
1172 else if (strcmp(f, "!MPLS_RND") == 0)
1173 pkt_dev->flags &= ~F_MPLS_RND;
1175 else if (strcmp(f, "VID_RND") == 0)
1176 pkt_dev->flags |= F_VID_RND;
1178 else if (strcmp(f, "!VID_RND") == 0)
1179 pkt_dev->flags &= ~F_VID_RND;
1181 else if (strcmp(f, "SVID_RND") == 0)
1182 pkt_dev->flags |= F_SVID_RND;
1184 else if (strcmp(f, "!SVID_RND") == 0)
1185 pkt_dev->flags &= ~F_SVID_RND;
1187 else if (strcmp(f, "!IPV6") == 0)
1188 pkt_dev->flags &= ~F_IPV6;
1192 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1194 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1195 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
1198 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1201 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1202 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1207 if (copy_from_user(buf, &user_buffer[i], len))
1210 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1211 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1212 strncpy(pkt_dev->dst_min, buf, len);
1213 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1214 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1217 printk("pktgen: dst_min set to: %s\n",
1220 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1223 if (!strcmp(name, "dst_max")) {
1224 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1229 if (copy_from_user(buf, &user_buffer[i], len))
1233 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1234 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1235 strncpy(pkt_dev->dst_max, buf, len);
1236 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1237 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1240 printk("pktgen: dst_max set to: %s\n",
1243 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1246 if (!strcmp(name, "dst6")) {
1247 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1251 pkt_dev->flags |= F_IPV6;
1253 if (copy_from_user(buf, &user_buffer[i], len))
1257 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1258 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1260 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1263 printk("pktgen: dst6 set to: %s\n", buf);
1266 sprintf(pg_result, "OK: dst6=%s", buf);
1269 if (!strcmp(name, "dst6_min")) {
1270 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1274 pkt_dev->flags |= F_IPV6;
1276 if (copy_from_user(buf, &user_buffer[i], len))
1280 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1281 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1283 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1284 &pkt_dev->min_in6_daddr);
1286 printk("pktgen: dst6_min set to: %s\n", buf);
1289 sprintf(pg_result, "OK: dst6_min=%s", buf);
1292 if (!strcmp(name, "dst6_max")) {
1293 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1297 pkt_dev->flags |= F_IPV6;
1299 if (copy_from_user(buf, &user_buffer[i], len))
1303 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1304 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1307 printk("pktgen: dst6_max set to: %s\n", buf);
1310 sprintf(pg_result, "OK: dst6_max=%s", buf);
1313 if (!strcmp(name, "src6")) {
1314 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1318 pkt_dev->flags |= F_IPV6;
1320 if (copy_from_user(buf, &user_buffer[i], len))
1324 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1325 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1327 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1330 printk("pktgen: src6 set to: %s\n", buf);
1333 sprintf(pg_result, "OK: src6=%s", buf);
1336 if (!strcmp(name, "src_min")) {
1337 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1341 if (copy_from_user(buf, &user_buffer[i], len))
1344 if (strcmp(buf, pkt_dev->src_min) != 0) {
1345 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1346 strncpy(pkt_dev->src_min, buf, len);
1347 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1348 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1351 printk("pktgen: src_min set to: %s\n",
1354 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1357 if (!strcmp(name, "src_max")) {
1358 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1362 if (copy_from_user(buf, &user_buffer[i], len))
1365 if (strcmp(buf, pkt_dev->src_max) != 0) {
1366 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1367 strncpy(pkt_dev->src_max, buf, len);
1368 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1369 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1372 printk("pktgen: src_max set to: %s\n",
1375 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1378 if (!strcmp(name, "dst_mac")) {
1380 unsigned char old_dmac[ETH_ALEN];
1381 unsigned char *m = pkt_dev->dst_mac;
1382 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1384 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1388 memset(valstr, 0, sizeof(valstr));
1389 if (copy_from_user(valstr, &user_buffer[i], len))
1393 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1394 if (*v >= '0' && *v <= '9') {
1398 if (*v >= 'A' && *v <= 'F') {
1400 *m += *v - 'A' + 10;
1402 if (*v >= 'a' && *v <= 'f') {
1404 *m += *v - 'a' + 10;
1412 /* Set up Dest MAC */
1413 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1414 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1416 sprintf(pg_result, "OK: dstmac");
1419 if (!strcmp(name, "src_mac")) {
1421 unsigned char *m = pkt_dev->src_mac;
1423 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1427 memset(valstr, 0, sizeof(valstr));
1428 if (copy_from_user(valstr, &user_buffer[i], len))
1432 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1433 if (*v >= '0' && *v <= '9') {
1437 if (*v >= 'A' && *v <= 'F') {
1439 *m += *v - 'A' + 10;
1441 if (*v >= 'a' && *v <= 'f') {
1443 *m += *v - 'a' + 10;
1451 sprintf(pg_result, "OK: srcmac");
1455 if (!strcmp(name, "clear_counters")) {
1456 pktgen_clear_counters(pkt_dev);
1457 sprintf(pg_result, "OK: Clearing counters.\n");
1461 if (!strcmp(name, "flows")) {
1462 len = num_arg(&user_buffer[i], 10, &value);
1467 if (value > MAX_CFLOWS)
1470 pkt_dev->cflows = value;
1471 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1475 if (!strcmp(name, "flowlen")) {
1476 len = num_arg(&user_buffer[i], 10, &value);
1481 pkt_dev->lflow = value;
1482 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1486 if (!strcmp(name, "mpls")) {
1488 len = get_labels(&user_buffer[i], pkt_dev);
1489 if (len < 0) { return len; }
1491 offset = sprintf(pg_result, "OK: mpls=");
1492 for(n = 0; n < pkt_dev->nr_labels; n++)
1493 offset += sprintf(pg_result + offset,
1494 "%08x%s", ntohl(pkt_dev->labels[n]),
1495 n == pkt_dev->nr_labels-1 ? "" : ",");
1497 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1498 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1499 pkt_dev->svlan_id = 0xffff;
1502 printk("pktgen: VLAN/SVLAN auto turned off\n");
1507 if (!strcmp(name, "vlan_id")) {
1508 len = num_arg(&user_buffer[i], 4, &value);
1513 if (value <= 4095) {
1514 pkt_dev->vlan_id = value; /* turn on VLAN */
1517 printk("pktgen: VLAN turned on\n");
1519 if (debug && pkt_dev->nr_labels)
1520 printk("pktgen: MPLS auto turned off\n");
1522 pkt_dev->nr_labels = 0; /* turn off MPLS */
1523 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1525 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1526 pkt_dev->svlan_id = 0xffff;
1529 printk("pktgen: VLAN/SVLAN turned off\n");
1534 if (!strcmp(name, "vlan_p")) {
1535 len = num_arg(&user_buffer[i], 1, &value);
1540 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1541 pkt_dev->vlan_p = value;
1542 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1544 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1549 if (!strcmp(name, "vlan_cfi")) {
1550 len = num_arg(&user_buffer[i], 1, &value);
1555 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1556 pkt_dev->vlan_cfi = value;
1557 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1559 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1564 if (!strcmp(name, "svlan_id")) {
1565 len = num_arg(&user_buffer[i], 4, &value);
1570 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1571 pkt_dev->svlan_id = value; /* turn on SVLAN */
1574 printk("pktgen: SVLAN turned on\n");
1576 if (debug && pkt_dev->nr_labels)
1577 printk("pktgen: MPLS auto turned off\n");
1579 pkt_dev->nr_labels = 0; /* turn off MPLS */
1580 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1582 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1583 pkt_dev->svlan_id = 0xffff;
1586 printk("pktgen: VLAN/SVLAN turned off\n");
1591 if (!strcmp(name, "svlan_p")) {
1592 len = num_arg(&user_buffer[i], 1, &value);
1597 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1598 pkt_dev->svlan_p = value;
1599 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1601 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1606 if (!strcmp(name, "svlan_cfi")) {
1607 len = num_arg(&user_buffer[i], 1, &value);
1612 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1613 pkt_dev->svlan_cfi = value;
1614 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1616 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1621 if (!strcmp(name, "tos")) {
1622 __u32 tmp_value = 0;
1623 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1629 pkt_dev->tos = tmp_value;
1630 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1632 sprintf(pg_result, "ERROR: tos must be 00-ff");
1637 if (!strcmp(name, "traffic_class")) {
1638 __u32 tmp_value = 0;
1639 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1645 pkt_dev->traffic_class = tmp_value;
1646 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1648 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1653 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1657 static int pktgen_if_open(struct inode *inode, struct file *file)
1659 return single_open(file, pktgen_if_show, PDE(inode)->data);
1662 static const struct file_operations pktgen_if_fops = {
1663 .owner = THIS_MODULE,
1664 .open = pktgen_if_open,
1666 .llseek = seq_lseek,
1667 .write = pktgen_if_write,
1668 .release = single_release,
1671 static int pktgen_thread_show(struct seq_file *seq, void *v)
1673 struct pktgen_thread *t = seq->private;
1674 struct pktgen_dev *pkt_dev;
1678 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1679 t->tsk->comm, t->max_before_softirq);
1681 seq_printf(seq, "Running: ");
1684 list_for_each_entry(pkt_dev, &t->if_list, list)
1685 if (pkt_dev->running)
1686 seq_printf(seq, "%s ", pkt_dev->ifname);
1688 seq_printf(seq, "\nStopped: ");
1690 list_for_each_entry(pkt_dev, &t->if_list, list)
1691 if (!pkt_dev->running)
1692 seq_printf(seq, "%s ", pkt_dev->ifname);
1695 seq_printf(seq, "\nResult: %s\n", t->result);
1697 seq_printf(seq, "\nResult: NA\n");
1704 static ssize_t pktgen_thread_write(struct file *file,
1705 const char __user * user_buffer,
1706 size_t count, loff_t * offset)
1708 struct seq_file *seq = (struct seq_file *)file->private_data;
1709 struct pktgen_thread *t = seq->private;
1710 int i = 0, max, len, ret;
1713 unsigned long value = 0;
1716 // sprintf(pg_result, "Wrong command format");
1721 len = count_trail_chars(&user_buffer[i], max);
1727 /* Read variable name */
1729 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1733 memset(name, 0, sizeof(name));
1734 if (copy_from_user(name, &user_buffer[i], len))
1739 len = count_trail_chars(&user_buffer[i], max);
1746 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
1749 printk("pktgen: ERROR: No thread\n");
1754 pg_result = &(t->result[0]);
1756 if (!strcmp(name, "add_device")) {
1759 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1764 if (copy_from_user(f, &user_buffer[i], len))
1767 mutex_lock(&pktgen_thread_lock);
1768 pktgen_add_device(t, f);
1769 mutex_unlock(&pktgen_thread_lock);
1771 sprintf(pg_result, "OK: add_device=%s", f);
1775 if (!strcmp(name, "rem_device_all")) {
1776 mutex_lock(&pktgen_thread_lock);
1777 t->control |= T_REMDEVALL;
1778 mutex_unlock(&pktgen_thread_lock);
1779 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1781 sprintf(pg_result, "OK: rem_device_all");
1785 if (!strcmp(name, "max_before_softirq")) {
1786 len = num_arg(&user_buffer[i], 10, &value);
1787 mutex_lock(&pktgen_thread_lock);
1788 t->max_before_softirq = value;
1789 mutex_unlock(&pktgen_thread_lock);
1791 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
1800 static int pktgen_thread_open(struct inode *inode, struct file *file)
1802 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1805 static const struct file_operations pktgen_thread_fops = {
1806 .owner = THIS_MODULE,
1807 .open = pktgen_thread_open,
1809 .llseek = seq_lseek,
1810 .write = pktgen_thread_write,
1811 .release = single_release,
1814 /* Think find or remove for NN */
1815 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1817 struct pktgen_thread *t;
1818 struct pktgen_dev *pkt_dev = NULL;
1820 list_for_each_entry(t, &pktgen_threads, th_list) {
1821 pkt_dev = pktgen_find_dev(t, ifname);
1825 pkt_dev->removal_mark = 1;
1826 t->control |= T_REMDEV;
1836 * mark a device for removal
1838 static int pktgen_mark_device(const char *ifname)
1840 struct pktgen_dev *pkt_dev = NULL;
1841 const int max_tries = 10, msec_per_try = 125;
1845 mutex_lock(&pktgen_thread_lock);
1846 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1850 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1851 if (pkt_dev == NULL)
1852 break; /* success */
1854 mutex_unlock(&pktgen_thread_lock);
1855 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1856 "to disappear....\n", ifname);
1857 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1858 mutex_lock(&pktgen_thread_lock);
1860 if (++i >= max_tries) {
1861 printk("pktgen_mark_device: timed out after waiting "
1862 "%d msec for device %s to be removed\n",
1863 msec_per_try * i, ifname);
1870 mutex_unlock(&pktgen_thread_lock);
1875 static int pktgen_device_event(struct notifier_block *unused,
1876 unsigned long event, void *ptr)
1878 struct net_device *dev = (struct net_device *)(ptr);
1880 /* It is OK that we do not hold the group lock right now,
1881 * as we run under the RTNL lock.
1885 case NETDEV_CHANGEADDR:
1886 case NETDEV_GOING_DOWN:
1889 /* Ignore for now */
1892 case NETDEV_UNREGISTER:
1893 pktgen_mark_device(dev->name);
1900 /* Associate pktgen_dev with a device. */
1902 static struct net_device *pktgen_setup_dev(struct pktgen_dev *pkt_dev)
1904 struct net_device *odev;
1906 /* Clean old setups */
1908 if (pkt_dev->odev) {
1909 dev_put(pkt_dev->odev);
1910 pkt_dev->odev = NULL;
1913 odev = dev_get_by_name(pkt_dev->ifname);
1916 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1919 if (odev->type != ARPHRD_ETHER) {
1920 printk("pktgen: not an ethernet device: \"%s\"\n",
1924 if (!netif_running(odev)) {
1925 printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1928 pkt_dev->odev = odev;
1930 return pkt_dev->odev;
1939 /* Read pkt_dev from the interface and set up internal pktgen_dev
1940 * structure to have the right information to create/send packets
1942 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1944 /* Try once more, just in case it works now. */
1946 pktgen_setup_dev(pkt_dev);
1948 if (!pkt_dev->odev) {
1949 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1950 sprintf(pkt_dev->result,
1951 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1955 /* Default to the interface's mac if not explicitly set. */
1957 if (is_zero_ether_addr(pkt_dev->src_mac))
1958 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1960 /* Set up Dest MAC */
1961 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1963 /* Set up pkt size */
1964 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1966 if (pkt_dev->flags & F_IPV6) {
1968 * Skip this automatic address setting until locks or functions
1973 int i, set = 0, err = 1;
1974 struct inet6_dev *idev;
1976 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1977 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1985 * Use linklevel address if unconfigured.
1987 * use ipv6_get_lladdr if/when it's get exported
1991 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1992 struct inet6_ifaddr *ifp;
1994 read_lock_bh(&idev->lock);
1995 for (ifp = idev->addr_list; ifp;
1996 ifp = ifp->if_next) {
1997 if (ifp->scope == IFA_LINK
1999 flags & IFA_F_TENTATIVE)) {
2000 ipv6_addr_copy(&pkt_dev->
2007 read_unlock_bh(&idev->lock);
2011 printk("pktgen: ERROR: IPv6 link address not availble.\n");
2015 pkt_dev->saddr_min = 0;
2016 pkt_dev->saddr_max = 0;
2017 if (strlen(pkt_dev->src_min) == 0) {
2019 struct in_device *in_dev;
2022 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2024 if (in_dev->ifa_list) {
2025 pkt_dev->saddr_min =
2026 in_dev->ifa_list->ifa_address;
2027 pkt_dev->saddr_max = pkt_dev->saddr_min;
2032 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2033 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2036 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2037 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2039 /* Initialize current values. */
2040 pkt_dev->cur_dst_mac_offset = 0;
2041 pkt_dev->cur_src_mac_offset = 0;
2042 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2043 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2044 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2045 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2046 pkt_dev->nflows = 0;
2049 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2054 start = now = getCurUs();
2055 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2056 while (now < spin_until_us) {
2057 /* TODO: optimize sleeping behavior */
2058 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2059 schedule_timeout_interruptible(1);
2060 else if (spin_until_us - now > 100) {
2062 if (!pkt_dev->running)
2071 pkt_dev->idle_acc += now - start;
2074 /* Increment/randomize headers according to flags and current values
2075 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2077 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2083 if (pkt_dev->cflows) {
2084 flow = random32() % pkt_dev->cflows;
2086 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2087 pkt_dev->flows[flow].count = 0;
2090 /* Deal with source MAC */
2091 if (pkt_dev->src_mac_count > 1) {
2095 if (pkt_dev->flags & F_MACSRC_RND)
2096 mc = random32() % pkt_dev->src_mac_count;
2098 mc = pkt_dev->cur_src_mac_offset++;
2099 if (pkt_dev->cur_src_mac_offset >
2100 pkt_dev->src_mac_count)
2101 pkt_dev->cur_src_mac_offset = 0;
2104 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2105 pkt_dev->hh[11] = tmp;
2106 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2107 pkt_dev->hh[10] = tmp;
2108 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2109 pkt_dev->hh[9] = tmp;
2110 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2111 pkt_dev->hh[8] = tmp;
2112 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2113 pkt_dev->hh[7] = tmp;
2116 /* Deal with Destination MAC */
2117 if (pkt_dev->dst_mac_count > 1) {
2121 if (pkt_dev->flags & F_MACDST_RND)
2122 mc = random32() % pkt_dev->dst_mac_count;
2125 mc = pkt_dev->cur_dst_mac_offset++;
2126 if (pkt_dev->cur_dst_mac_offset >
2127 pkt_dev->dst_mac_count) {
2128 pkt_dev->cur_dst_mac_offset = 0;
2132 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2133 pkt_dev->hh[5] = tmp;
2134 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2135 pkt_dev->hh[4] = tmp;
2136 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2137 pkt_dev->hh[3] = tmp;
2138 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2139 pkt_dev->hh[2] = tmp;
2140 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2141 pkt_dev->hh[1] = tmp;
2144 if (pkt_dev->flags & F_MPLS_RND) {
2146 for(i = 0; i < pkt_dev->nr_labels; i++)
2147 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2148 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2149 ((__force __be32)random32() &
2153 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2154 pkt_dev->vlan_id = random32() & (4096-1);
2157 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2158 pkt_dev->svlan_id = random32() & (4096 - 1);
2161 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2162 if (pkt_dev->flags & F_UDPSRC_RND)
2163 pkt_dev->cur_udp_src = random32() %
2164 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2165 + pkt_dev->udp_src_min;
2168 pkt_dev->cur_udp_src++;
2169 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2170 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2174 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2175 if (pkt_dev->flags & F_UDPDST_RND) {
2176 pkt_dev->cur_udp_dst = random32() %
2177 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2178 + pkt_dev->udp_dst_min;
2180 pkt_dev->cur_udp_dst++;
2181 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2182 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2186 if (!(pkt_dev->flags & F_IPV6)) {
2188 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2192 if (pkt_dev->flags & F_IPSRC_RND)
2193 t = random32() % (imx - imn) + imn;
2195 t = ntohl(pkt_dev->cur_saddr);
2201 pkt_dev->cur_saddr = htonl(t);
2204 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
2205 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2207 imn = ntohl(pkt_dev->daddr_min);
2208 imx = ntohl(pkt_dev->daddr_max);
2212 if (pkt_dev->flags & F_IPDST_RND) {
2214 t = random32() % (imx - imn) + imn;
2217 while (LOOPBACK(s) || MULTICAST(s)
2218 || BADCLASS(s) || ZERONET(s)
2219 || LOCAL_MCAST(s)) {
2220 t = random32() % (imx - imn) + imn;
2223 pkt_dev->cur_daddr = s;
2225 t = ntohl(pkt_dev->cur_daddr);
2230 pkt_dev->cur_daddr = htonl(t);
2233 if (pkt_dev->cflows) {
2234 pkt_dev->flows[flow].cur_daddr =
2239 } else { /* IPV6 * */
2241 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2242 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2243 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2244 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2248 /* Only random destinations yet */
2250 for (i = 0; i < 4; i++) {
2251 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2252 (((__force __be32)random32() |
2253 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2254 pkt_dev->max_in6_daddr.s6_addr32[i]);
2259 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2261 if (pkt_dev->flags & F_TXSIZE_RND) {
2263 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2264 + pkt_dev->min_pkt_size;
2266 t = pkt_dev->cur_pkt_size + 1;
2267 if (t > pkt_dev->max_pkt_size)
2268 t = pkt_dev->min_pkt_size;
2270 pkt_dev->cur_pkt_size = t;
2273 pkt_dev->flows[flow].count++;
2276 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2279 for(i = 0; i < pkt_dev->nr_labels; i++) {
2280 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2283 *mpls |= MPLS_STACK_BOTTOM;
2286 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2289 return htons(id | (cfi << 12) | (prio << 13));
2292 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2293 struct pktgen_dev *pkt_dev)
2295 struct sk_buff *skb = NULL;
2297 struct udphdr *udph;
2300 struct pktgen_hdr *pgh = NULL;
2301 __be16 protocol = htons(ETH_P_IP);
2303 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2304 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2305 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2306 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2309 if (pkt_dev->nr_labels)
2310 protocol = htons(ETH_P_MPLS_UC);
2312 if (pkt_dev->vlan_id != 0xffff)
2313 protocol = htons(ETH_P_8021Q);
2315 /* Update any of the values, used when we're incrementing various
2318 mod_cur_headers(pkt_dev);
2320 datalen = (odev->hard_header_len + 16) & ~0xf;
2321 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2322 pkt_dev->nr_labels*sizeof(u32) +
2323 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2326 sprintf(pkt_dev->result, "No memory");
2330 skb_reserve(skb, datalen);
2332 /* Reserve for ethernet and IP header */
2333 eth = (__u8 *) skb_push(skb, 14);
2334 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2335 if (pkt_dev->nr_labels)
2336 mpls_push(mpls, pkt_dev);
2338 if (pkt_dev->vlan_id != 0xffff) {
2339 if(pkt_dev->svlan_id != 0xffff) {
2340 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2341 *svlan_tci = build_tci(pkt_dev->svlan_id,
2344 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2345 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2347 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2348 *vlan_tci = build_tci(pkt_dev->vlan_id,
2351 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2352 *vlan_encapsulated_proto = htons(ETH_P_IP);
2355 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
2356 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2358 memcpy(eth, pkt_dev->hh, 12);
2359 *(__be16 *) & eth[12] = protocol;
2361 /* Eth + IPh + UDPh + mpls */
2362 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2363 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2364 if (datalen < sizeof(struct pktgen_hdr))
2365 datalen = sizeof(struct pktgen_hdr);
2367 udph->source = htons(pkt_dev->cur_udp_src);
2368 udph->dest = htons(pkt_dev->cur_udp_dst);
2369 udph->len = htons(datalen + 8); /* DATA + udphdr */
2370 udph->check = 0; /* No checksum */
2375 iph->tos = pkt_dev->tos;
2376 iph->protocol = IPPROTO_UDP; /* UDP */
2377 iph->saddr = pkt_dev->cur_saddr;
2378 iph->daddr = pkt_dev->cur_daddr;
2380 iplen = 20 + 8 + datalen;
2381 iph->tot_len = htons(iplen);
2383 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2384 skb->protocol = protocol;
2385 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
2386 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2388 skb->pkt_type = PACKET_HOST;
2392 if (pkt_dev->nfrags <= 0)
2393 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2395 int frags = pkt_dev->nfrags;
2398 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2400 if (frags > MAX_SKB_FRAGS)
2401 frags = MAX_SKB_FRAGS;
2402 if (datalen > frags * PAGE_SIZE) {
2403 skb_put(skb, datalen - frags * PAGE_SIZE);
2404 datalen = frags * PAGE_SIZE;
2408 while (datalen > 0) {
2409 struct page *page = alloc_pages(GFP_KERNEL, 0);
2410 skb_shinfo(skb)->frags[i].page = page;
2411 skb_shinfo(skb)->frags[i].page_offset = 0;
2412 skb_shinfo(skb)->frags[i].size =
2413 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2414 datalen -= skb_shinfo(skb)->frags[i].size;
2415 skb->len += skb_shinfo(skb)->frags[i].size;
2416 skb->data_len += skb_shinfo(skb)->frags[i].size;
2418 skb_shinfo(skb)->nr_frags = i;
2427 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2431 skb_shinfo(skb)->frags[i - 1].size -= rem;
2433 skb_shinfo(skb)->frags[i] =
2434 skb_shinfo(skb)->frags[i - 1];
2435 get_page(skb_shinfo(skb)->frags[i].page);
2436 skb_shinfo(skb)->frags[i].page =
2437 skb_shinfo(skb)->frags[i - 1].page;
2438 skb_shinfo(skb)->frags[i].page_offset +=
2439 skb_shinfo(skb)->frags[i - 1].size;
2440 skb_shinfo(skb)->frags[i].size = rem;
2442 skb_shinfo(skb)->nr_frags = i;
2446 /* Stamp the time, and sequence number, convert them to network byte order */
2449 struct timeval timestamp;
2451 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2452 pgh->seq_num = htonl(pkt_dev->seq_num);
2454 do_gettimeofday(×tamp);
2455 pgh->tv_sec = htonl(timestamp.tv_sec);
2456 pgh->tv_usec = htonl(timestamp.tv_usec);
2463 * scan_ip6, fmt_ip taken from dietlibc-0.21
2464 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2466 * Slightly modified for kernel.
2467 * Should be candidate for net/ipv4/utils.c
2471 static unsigned int scan_ip6(const char *s, char ip[16])
2474 unsigned int len = 0;
2477 unsigned int prefixlen = 0;
2478 unsigned int suffixlen = 0;
2481 for (i = 0; i < 16; i++)
2487 if (s[1] == ':') { /* Found "::", skip to part 2 */
2496 u = simple_strtoul(s, &tmp, 16);
2502 if (prefixlen == 12 && s[i] == '.') {
2504 /* the last 4 bytes may be written as IPv4 address */
2507 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2510 ip[prefixlen++] = (u >> 8);
2511 ip[prefixlen++] = (u & 255);
2514 if (prefixlen == 16)
2518 /* part 2, after "::" */
2525 } else if (suffixlen != 0)
2529 u = simple_strtol(s, &tmp, 16);
2537 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2539 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2545 suffix[suffixlen++] = (u >> 8);
2546 suffix[suffixlen++] = (u & 255);
2549 if (prefixlen + suffixlen == 16)
2552 for (i = 0; i < suffixlen; i++)
2553 ip[16 - suffixlen + i] = suffix[i];
2557 static char tohex(char hexdigit)
2559 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2562 static int fmt_xlong(char *s, unsigned int i)
2565 *s = tohex((i >> 12) & 0xf);
2566 if (s != bak || *s != '0')
2568 *s = tohex((i >> 8) & 0xf);
2569 if (s != bak || *s != '0')
2571 *s = tohex((i >> 4) & 0xf);
2572 if (s != bak || *s != '0')
2574 *s = tohex(i & 0xf);
2578 static unsigned int fmt_ip6(char *s, const char ip[16])
2583 unsigned int compressing;
2588 for (j = 0; j < 16; j += 2) {
2590 #ifdef V4MAPPEDPREFIX
2591 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2592 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2597 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2598 (unsigned long)(unsigned char)ip[j + 1];
2613 i = fmt_xlong(s, temp);
2630 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2631 struct pktgen_dev *pkt_dev)
2633 struct sk_buff *skb = NULL;
2635 struct udphdr *udph;
2637 struct ipv6hdr *iph;
2638 struct pktgen_hdr *pgh = NULL;
2639 __be16 protocol = htons(ETH_P_IPV6);
2641 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2642 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2643 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2644 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2646 if (pkt_dev->nr_labels)
2647 protocol = htons(ETH_P_MPLS_UC);
2649 if (pkt_dev->vlan_id != 0xffff)
2650 protocol = htons(ETH_P_8021Q);
2652 /* Update any of the values, used when we're incrementing various
2655 mod_cur_headers(pkt_dev);
2657 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2658 pkt_dev->nr_labels*sizeof(u32) +
2659 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2662 sprintf(pkt_dev->result, "No memory");
2666 skb_reserve(skb, 16);
2668 /* Reserve for ethernet and IP header */
2669 eth = (__u8 *) skb_push(skb, 14);
2670 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2671 if (pkt_dev->nr_labels)
2672 mpls_push(mpls, pkt_dev);
2674 if (pkt_dev->vlan_id != 0xffff) {
2675 if(pkt_dev->svlan_id != 0xffff) {
2676 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2677 *svlan_tci = build_tci(pkt_dev->svlan_id,
2680 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2681 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2683 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2684 *vlan_tci = build_tci(pkt_dev->vlan_id,
2687 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2688 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2691 iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
2692 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2694 memcpy(eth, pkt_dev->hh, 12);
2695 *(__be16 *) & eth[12] = protocol;
2697 /* Eth + IPh + UDPh + mpls */
2698 datalen = pkt_dev->cur_pkt_size - 14 -
2699 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2700 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2702 if (datalen < sizeof(struct pktgen_hdr)) {
2703 datalen = sizeof(struct pktgen_hdr);
2704 if (net_ratelimit())
2705 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2709 udph->source = htons(pkt_dev->cur_udp_src);
2710 udph->dest = htons(pkt_dev->cur_udp_dst);
2711 udph->len = htons(datalen + sizeof(struct udphdr));
2712 udph->check = 0; /* No checksum */
2714 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2716 if (pkt_dev->traffic_class) {
2717 /* Version + traffic class + flow (0) */
2718 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2721 iph->hop_limit = 32;
2723 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2724 iph->nexthdr = IPPROTO_UDP;
2726 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2727 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2729 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
2730 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
2731 skb->protocol = protocol;
2733 skb->pkt_type = PACKET_HOST;
2734 skb->nh.ipv6h = iph;
2737 if (pkt_dev->nfrags <= 0)
2738 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2740 int frags = pkt_dev->nfrags;
2743 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2745 if (frags > MAX_SKB_FRAGS)
2746 frags = MAX_SKB_FRAGS;
2747 if (datalen > frags * PAGE_SIZE) {
2748 skb_put(skb, datalen - frags * PAGE_SIZE);
2749 datalen = frags * PAGE_SIZE;
2753 while (datalen > 0) {
2754 struct page *page = alloc_pages(GFP_KERNEL, 0);
2755 skb_shinfo(skb)->frags[i].page = page;
2756 skb_shinfo(skb)->frags[i].page_offset = 0;
2757 skb_shinfo(skb)->frags[i].size =
2758 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2759 datalen -= skb_shinfo(skb)->frags[i].size;
2760 skb->len += skb_shinfo(skb)->frags[i].size;
2761 skb->data_len += skb_shinfo(skb)->frags[i].size;
2763 skb_shinfo(skb)->nr_frags = i;
2772 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2776 skb_shinfo(skb)->frags[i - 1].size -= rem;
2778 skb_shinfo(skb)->frags[i] =
2779 skb_shinfo(skb)->frags[i - 1];
2780 get_page(skb_shinfo(skb)->frags[i].page);
2781 skb_shinfo(skb)->frags[i].page =
2782 skb_shinfo(skb)->frags[i - 1].page;
2783 skb_shinfo(skb)->frags[i].page_offset +=
2784 skb_shinfo(skb)->frags[i - 1].size;
2785 skb_shinfo(skb)->frags[i].size = rem;
2787 skb_shinfo(skb)->nr_frags = i;
2791 /* Stamp the time, and sequence number, convert them to network byte order */
2792 /* should we update cloned packets too ? */
2794 struct timeval timestamp;
2796 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2797 pgh->seq_num = htonl(pkt_dev->seq_num);
2799 do_gettimeofday(×tamp);
2800 pgh->tv_sec = htonl(timestamp.tv_sec);
2801 pgh->tv_usec = htonl(timestamp.tv_usec);
2803 /* pkt_dev->seq_num++; FF: you really mean this? */
2808 static inline struct sk_buff *fill_packet(struct net_device *odev,
2809 struct pktgen_dev *pkt_dev)
2811 if (pkt_dev->flags & F_IPV6)
2812 return fill_packet_ipv6(odev, pkt_dev);
2814 return fill_packet_ipv4(odev, pkt_dev);
2817 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2819 pkt_dev->seq_num = 1;
2820 pkt_dev->idle_acc = 0;
2822 pkt_dev->tx_bytes = 0;
2823 pkt_dev->errors = 0;
2826 /* Set up structure for sending pkts, clear counters */
2828 static void pktgen_run(struct pktgen_thread *t)
2830 struct pktgen_dev *pkt_dev;
2833 pr_debug("pktgen: entering pktgen_run. %p\n", t);
2836 list_for_each_entry(pkt_dev, &t->if_list, list) {
2839 * setup odev and create initial packet.
2841 pktgen_setup_inject(pkt_dev);
2843 if (pkt_dev->odev) {
2844 pktgen_clear_counters(pkt_dev);
2845 pkt_dev->running = 1; /* Cranke yeself! */
2846 pkt_dev->skb = NULL;
2847 pkt_dev->started_at = getCurUs();
2848 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
2849 pkt_dev->next_tx_ns = 0;
2851 strcpy(pkt_dev->result, "Starting");
2854 strcpy(pkt_dev->result, "Error starting");
2858 t->control &= ~(T_STOP);
2861 static void pktgen_stop_all_threads_ifs(void)
2863 struct pktgen_thread *t;
2865 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
2867 mutex_lock(&pktgen_thread_lock);
2869 list_for_each_entry(t, &pktgen_threads, th_list)
2870 t->control |= T_STOP;
2872 mutex_unlock(&pktgen_thread_lock);
2875 static int thread_is_running(struct pktgen_thread *t)
2877 struct pktgen_dev *pkt_dev;
2880 list_for_each_entry(pkt_dev, &t->if_list, list)
2881 if (pkt_dev->running) {
2888 static int pktgen_wait_thread_run(struct pktgen_thread *t)
2892 while (thread_is_running(t)) {
2896 msleep_interruptible(100);
2898 if (signal_pending(current))
2908 static int pktgen_wait_all_threads_run(void)
2910 struct pktgen_thread *t;
2913 mutex_lock(&pktgen_thread_lock);
2915 list_for_each_entry(t, &pktgen_threads, th_list) {
2916 sig = pktgen_wait_thread_run(t);
2922 list_for_each_entry(t, &pktgen_threads, th_list)
2923 t->control |= (T_STOP);
2925 mutex_unlock(&pktgen_thread_lock);
2929 static void pktgen_run_all_threads(void)
2931 struct pktgen_thread *t;
2933 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
2935 mutex_lock(&pktgen_thread_lock);
2937 list_for_each_entry(t, &pktgen_threads, th_list)
2938 t->control |= (T_RUN);
2940 mutex_unlock(&pktgen_thread_lock);
2942 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2944 pktgen_wait_all_threads_run();
2947 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2949 __u64 total_us, bps, mbps, pps, idle;
2950 char *p = pkt_dev->result;
2952 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
2954 idle = pkt_dev->idle_acc;
2956 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2957 (unsigned long long)total_us,
2958 (unsigned long long)(total_us - idle),
2959 (unsigned long long)idle,
2960 (unsigned long long)pkt_dev->sofar,
2961 pkt_dev->cur_pkt_size, nr_frags);
2963 pps = pkt_dev->sofar * USEC_PER_SEC;
2965 while ((total_us >> 32) != 0) {
2970 do_div(pps, total_us);
2972 bps = pps * 8 * pkt_dev->cur_pkt_size;
2975 do_div(mbps, 1000000);
2976 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2977 (unsigned long long)pps,
2978 (unsigned long long)mbps,
2979 (unsigned long long)bps,
2980 (unsigned long long)pkt_dev->errors);
2983 /* Set stopped-at timer, remove from running list, do counters & statistics */
2985 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
2987 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
2989 if (!pkt_dev->running) {
2990 printk("pktgen: interface: %s is already stopped\n",
2995 pkt_dev->stopped_at = getCurUs();
2996 pkt_dev->running = 0;
2998 show_results(pkt_dev, nr_frags);
3003 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3005 struct pktgen_dev *pkt_dev, *best = NULL;
3009 list_for_each_entry(pkt_dev, &t->if_list, list) {
3010 if (!pkt_dev->running)
3014 else if (pkt_dev->next_tx_us < best->next_tx_us)
3021 static void pktgen_stop(struct pktgen_thread *t)
3023 struct pktgen_dev *pkt_dev;
3025 pr_debug("pktgen: entering pktgen_stop\n");
3029 list_for_each_entry(pkt_dev, &t->if_list, list) {
3030 pktgen_stop_device(pkt_dev);
3032 kfree_skb(pkt_dev->skb);
3034 pkt_dev->skb = NULL;
3041 * one of our devices needs to be removed - find it
3044 static void pktgen_rem_one_if(struct pktgen_thread *t)
3046 struct list_head *q, *n;
3047 struct pktgen_dev *cur;
3049 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3053 list_for_each_safe(q, n, &t->if_list) {
3054 cur = list_entry(q, struct pktgen_dev, list);
3056 if (!cur->removal_mark)
3060 kfree_skb(cur->skb);
3063 pktgen_remove_device(t, cur);
3071 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3073 struct list_head *q, *n;
3074 struct pktgen_dev *cur;
3076 /* Remove all devices, free mem */
3078 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3081 list_for_each_safe(q, n, &t->if_list) {
3082 cur = list_entry(q, struct pktgen_dev, list);
3085 kfree_skb(cur->skb);
3088 pktgen_remove_device(t, cur);
3094 static void pktgen_rem_thread(struct pktgen_thread *t)
3096 /* Remove from the thread list */
3098 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3100 mutex_lock(&pktgen_thread_lock);
3102 list_del(&t->th_list);
3104 mutex_unlock(&pktgen_thread_lock);
3107 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3109 struct net_device *odev = NULL;
3110 __u64 idle_start = 0;
3113 odev = pkt_dev->odev;
3115 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3119 if (now < pkt_dev->next_tx_us)
3120 spin(pkt_dev, pkt_dev->next_tx_us);
3122 /* This is max DELAY, this has special meaning of
3125 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3126 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3127 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3132 if (netif_queue_stopped(odev) || need_resched()) {
3133 idle_start = getCurUs();
3135 if (!netif_running(odev)) {
3136 pktgen_stop_device(pkt_dev);
3138 kfree_skb(pkt_dev->skb);
3139 pkt_dev->skb = NULL;
3145 pkt_dev->idle_acc += getCurUs() - idle_start;
3147 if (netif_queue_stopped(odev)) {
3148 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3149 pkt_dev->next_tx_ns = 0;
3150 goto out; /* Try the next interface */
3154 if (pkt_dev->last_ok || !pkt_dev->skb) {
3155 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3156 || (!pkt_dev->skb)) {
3157 /* build a new pkt */
3159 kfree_skb(pkt_dev->skb);
3161 pkt_dev->skb = fill_packet(odev, pkt_dev);
3162 if (pkt_dev->skb == NULL) {
3163 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3165 pkt_dev->clone_count--; /* back out increment, OOM */
3168 pkt_dev->allocated_skbs++;
3169 pkt_dev->clone_count = 0; /* reset counter */
3173 netif_tx_lock_bh(odev);
3174 if (!netif_queue_stopped(odev)) {
3176 atomic_inc(&(pkt_dev->skb->users));
3178 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3179 if (likely(ret == NETDEV_TX_OK)) {
3180 pkt_dev->last_ok = 1;
3183 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3185 } else if (ret == NETDEV_TX_LOCKED
3186 && (odev->features & NETIF_F_LLTX)) {
3189 } else { /* Retry it next time */
3191 atomic_dec(&(pkt_dev->skb->users));
3193 if (debug && net_ratelimit())
3194 printk(KERN_INFO "pktgen: Hard xmit error\n");
3197 pkt_dev->last_ok = 0;
3200 pkt_dev->next_tx_us = getCurUs();
3201 pkt_dev->next_tx_ns = 0;
3203 pkt_dev->next_tx_us += pkt_dev->delay_us;
3204 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3206 if (pkt_dev->next_tx_ns > 1000) {
3207 pkt_dev->next_tx_us++;
3208 pkt_dev->next_tx_ns -= 1000;
3212 else { /* Retry it next time */
3213 pkt_dev->last_ok = 0;
3214 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3215 pkt_dev->next_tx_ns = 0;
3218 netif_tx_unlock_bh(odev);
3220 /* If pkt_dev->count is zero, then run forever */
3221 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3222 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3223 idle_start = getCurUs();
3224 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3225 if (signal_pending(current)) {
3230 pkt_dev->idle_acc += getCurUs() - idle_start;
3233 /* Done with this */
3234 pktgen_stop_device(pkt_dev);
3236 kfree_skb(pkt_dev->skb);
3237 pkt_dev->skb = NULL;
3243 * Main loop of the thread goes here
3246 static int pktgen_thread_worker(void *arg)
3249 struct pktgen_thread *t = arg;
3250 struct pktgen_dev *pkt_dev = NULL;
3252 u32 max_before_softirq;
3253 u32 tx_since_softirq = 0;
3255 BUG_ON(smp_processor_id() != cpu);
3257 init_waitqueue_head(&t->queue);
3259 t->pid = current->pid;
3261 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
3263 max_before_softirq = t->max_before_softirq;
3265 set_current_state(TASK_INTERRUPTIBLE);
3267 while (!kthread_should_stop()) {
3268 pkt_dev = next_to_run(t);
3271 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3273 prepare_to_wait(&(t->queue), &wait,
3274 TASK_INTERRUPTIBLE);
3275 schedule_timeout(HZ / 10);
3276 finish_wait(&(t->queue), &wait);
3279 __set_current_state(TASK_RUNNING);
3283 pktgen_xmit(pkt_dev);
3286 * We like to stay RUNNING but must also give
3287 * others fair share.
3290 tx_since_softirq += pkt_dev->last_ok;
3292 if (tx_since_softirq > max_before_softirq) {
3293 if (local_softirq_pending())
3295 tx_since_softirq = 0;
3299 if (t->control & T_STOP) {
3301 t->control &= ~(T_STOP);
3304 if (t->control & T_RUN) {
3306 t->control &= ~(T_RUN);
3309 if (t->control & T_REMDEVALL) {
3310 pktgen_rem_all_ifs(t);
3311 t->control &= ~(T_REMDEVALL);
3314 if (t->control & T_REMDEV) {
3315 pktgen_rem_one_if(t);
3316 t->control &= ~(T_REMDEV);
3321 set_current_state(TASK_INTERRUPTIBLE);
3324 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3327 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3328 pktgen_rem_all_ifs(t);
3330 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3331 pktgen_rem_thread(t);
3336 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3339 struct pktgen_dev *p, *pkt_dev = NULL;
3342 list_for_each_entry(p, &t->if_list, list)
3343 if (strncmp(p->ifname, ifname, IFNAMSIZ) == 0) {
3349 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3354 * Adds a dev at front of if_list.
3357 static int add_dev_to_thread(struct pktgen_thread *t,
3358 struct pktgen_dev *pkt_dev)
3364 if (pkt_dev->pg_thread) {
3365 printk("pktgen: ERROR: already assigned to a thread.\n");
3370 list_add(&pkt_dev->list, &t->if_list);
3371 pkt_dev->pg_thread = t;
3372 pkt_dev->running = 0;
3379 /* Called under thread lock */
3381 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3383 struct pktgen_dev *pkt_dev;
3384 struct proc_dir_entry *pe;
3386 /* We don't allow a device to be on several threads */
3388 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3390 printk("pktgen: ERROR: interface already used.\n");
3394 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3398 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3399 if (pkt_dev->flows == NULL) {
3403 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3405 pkt_dev->removal_mark = 0;
3406 pkt_dev->min_pkt_size = ETH_ZLEN;
3407 pkt_dev->max_pkt_size = ETH_ZLEN;
3408 pkt_dev->nfrags = 0;
3409 pkt_dev->clone_skb = pg_clone_skb_d;
3410 pkt_dev->delay_us = pg_delay_d / 1000;
3411 pkt_dev->delay_ns = pg_delay_d % 1000;
3412 pkt_dev->count = pg_count_d;
3414 pkt_dev->udp_src_min = 9; /* sink port */
3415 pkt_dev->udp_src_max = 9;
3416 pkt_dev->udp_dst_min = 9;
3417 pkt_dev->udp_dst_max = 9;
3419 pkt_dev->vlan_p = 0;
3420 pkt_dev->vlan_cfi = 0;
3421 pkt_dev->vlan_id = 0xffff;
3422 pkt_dev->svlan_p = 0;
3423 pkt_dev->svlan_cfi = 0;
3424 pkt_dev->svlan_id = 0xffff;
3426 strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
3428 if (!pktgen_setup_dev(pkt_dev)) {
3429 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
3431 vfree(pkt_dev->flows);
3436 pe = create_proc_entry(ifname, 0600, pg_proc_dir);
3438 printk("pktgen: cannot create %s/%s procfs entry.\n",
3439 PG_PROC_DIR, ifname);
3441 vfree(pkt_dev->flows);
3445 pe->proc_fops = &pktgen_if_fops;
3448 return add_dev_to_thread(t, pkt_dev);
3451 static int __init pktgen_create_thread(int cpu)
3453 struct pktgen_thread *t;
3454 struct proc_dir_entry *pe;
3455 struct task_struct *p;
3457 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3459 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3463 spin_lock_init(&t->if_lock);
3466 INIT_LIST_HEAD(&t->if_list);
3468 list_add_tail(&t->th_list, &pktgen_threads);
3470 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3472 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3473 list_del(&t->th_list);
3477 kthread_bind(p, cpu);
3480 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3482 printk("pktgen: cannot create %s/%s procfs entry.\n",
3483 PG_PROC_DIR, t->tsk->comm);
3485 list_del(&t->th_list);
3490 pe->proc_fops = &pktgen_thread_fops;
3499 * Removes a device from the thread if_list.
3501 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3502 struct pktgen_dev *pkt_dev)
3504 struct list_head *q, *n;
3505 struct pktgen_dev *p;
3507 list_for_each_safe(q, n, &t->if_list) {
3508 p = list_entry(q, struct pktgen_dev, list);
3514 static int pktgen_remove_device(struct pktgen_thread *t,
3515 struct pktgen_dev *pkt_dev)
3518 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3520 if (pkt_dev->running) {
3521 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3522 pktgen_stop_device(pkt_dev);
3525 /* Dis-associate from the interface */
3527 if (pkt_dev->odev) {
3528 dev_put(pkt_dev->odev);
3529 pkt_dev->odev = NULL;
3532 /* And update the thread if_list */
3534 _rem_dev_from_if_list(t, pkt_dev);
3536 /* Clean up proc file system */
3538 remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
3541 vfree(pkt_dev->flows);
3546 static int __init pg_init(void)
3549 struct proc_dir_entry *pe;
3553 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3556 pg_proc_dir->owner = THIS_MODULE;
3558 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3560 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3562 proc_net_remove(PG_PROC_DIR);
3566 pe->proc_fops = &pktgen_fops;
3569 /* Register us to receive netdevice events */
3570 register_netdevice_notifier(&pktgen_notifier_block);
3572 for_each_online_cpu(cpu) {
3575 err = pktgen_create_thread(cpu);
3577 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3581 if (list_empty(&pktgen_threads)) {
3582 printk("pktgen: ERROR: Initialization failed for all threads\n");
3583 unregister_netdevice_notifier(&pktgen_notifier_block);
3584 remove_proc_entry(PGCTRL, pg_proc_dir);
3585 proc_net_remove(PG_PROC_DIR);
3592 static void __exit pg_cleanup(void)
3594 struct pktgen_thread *t;
3595 struct list_head *q, *n;
3596 wait_queue_head_t queue;
3597 init_waitqueue_head(&queue);
3599 /* Stop all interfaces & threads */
3601 list_for_each_safe(q, n, &pktgen_threads) {
3602 t = list_entry(q, struct pktgen_thread, th_list);
3603 kthread_stop(t->tsk);
3607 /* Un-register us from receiving netdevice events */
3608 unregister_netdevice_notifier(&pktgen_notifier_block);
3610 /* Clean up proc file system */
3611 remove_proc_entry(PGCTRL, pg_proc_dir);
3612 proc_net_remove(PG_PROC_DIR);
3615 module_init(pg_init);
3616 module_exit(pg_cleanup);
3618 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3619 MODULE_DESCRIPTION("Packet Generator tool");
3620 MODULE_LICENSE("GPL");
3621 module_param(pg_count_d, int, 0);
3622 module_param(pg_delay_d, int, 0);
3623 module_param(pg_clone_skb_d, int, 0);
3624 module_param(debug, int, 0);