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>
114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
118 #include <linux/sys.h>
119 #include <linux/types.h>
120 #include <linux/module.h>
121 #include <linux/moduleparam.h>
122 #include <linux/kernel.h>
123 #include <linux/mutex.h>
124 #include <linux/sched.h>
125 #include <linux/slab.h>
126 #include <linux/vmalloc.h>
127 #include <linux/unistd.h>
128 #include <linux/string.h>
129 #include <linux/ptrace.h>
130 #include <linux/errno.h>
131 #include <linux/ioport.h>
132 #include <linux/interrupt.h>
133 #include <linux/capability.h>
134 #include <linux/freezer.h>
135 #include <linux/delay.h>
136 #include <linux/timer.h>
137 #include <linux/list.h>
138 #include <linux/init.h>
139 #include <linux/skbuff.h>
140 #include <linux/netdevice.h>
141 #include <linux/inet.h>
142 #include <linux/inetdevice.h>
143 #include <linux/rtnetlink.h>
144 #include <linux/if_arp.h>
145 #include <linux/if_vlan.h>
146 #include <linux/in.h>
147 #include <linux/ip.h>
148 #include <linux/ipv6.h>
149 #include <linux/udp.h>
150 #include <linux/proc_fs.h>
151 #include <linux/seq_file.h>
152 #include <linux/wait.h>
153 #include <linux/etherdevice.h>
154 #include <linux/kthread.h>
155 #include <net/net_namespace.h>
156 #include <net/checksum.h>
157 #include <net/ipv6.h>
158 #include <net/addrconf.h>
160 #include <net/xfrm.h>
162 #include <asm/byteorder.h>
163 #include <linux/rcupdate.h>
164 #include <linux/bitops.h>
167 #include <asm/uaccess.h>
168 #include <asm/div64.h> /* do_div */
169 #include <asm/timex.h>
171 #define VERSION "pktgen v2.69: Packet Generator for packet performance testing.\n"
173 #define IP_NAME_SZ 32
174 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
175 #define MPLS_STACK_BOTTOM htonl(0x00000100)
177 /* Device flag bits */
178 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
179 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
180 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
186 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
187 #define F_VID_RND (1<<9) /* Random VLAN ID */
188 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
189 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
190 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
191 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
193 /* Thread control flag bits */
194 #define T_TERMINATE (1<<0)
195 #define T_STOP (1<<1) /* Stop run */
196 #define T_RUN (1<<2) /* Start run */
197 #define T_REMDEVALL (1<<3) /* Remove all devs */
198 #define T_REMDEV (1<<4) /* Remove one dev */
200 /* If lock -- can be removed after some work */
201 #define if_lock(t) spin_lock(&(t->if_lock));
202 #define if_unlock(t) spin_unlock(&(t->if_lock));
204 /* Used to help with determining the pkts on receive */
205 #define PKTGEN_MAGIC 0xbe9be955
206 #define PG_PROC_DIR "pktgen"
207 #define PGCTRL "pgctrl"
208 static struct proc_dir_entry *pg_proc_dir = NULL;
210 #define MAX_CFLOWS 65536
212 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
213 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
219 struct xfrm_state *x;
225 #define F_INIT (1<<0) /* flow has been initialized */
229 * Try to keep frequent/infrequent used vars. separated.
231 struct proc_dir_entry *entry; /* proc file */
232 struct pktgen_thread *pg_thread;/* the owner */
233 struct list_head list; /* Used for chaining in the thread's run-queue */
235 int running; /* if this changes to false, the test will stop */
237 /* If min != max, then we will either do a linear iteration, or
238 * we will do a random selection from within the range.
241 int removal_mark; /* non-zero => the device is marked for
242 * removal by worker thread */
244 int min_pkt_size; /* = ETH_ZLEN; */
245 int max_pkt_size; /* = ETH_ZLEN; */
246 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
248 __u32 delay_us; /* Default delay */
250 __u64 count; /* Default No packets to send */
251 __u64 sofar; /* How many pkts we've sent so far */
252 __u64 tx_bytes; /* How many bytes we've transmitted */
253 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
255 /* runtime counters relating to clone_skb */
256 __u64 next_tx_us; /* timestamp of when to tx next */
259 __u64 allocated_skbs;
261 int last_ok; /* Was last skb sent?
262 * Or a failed transmit of some sort? This will keep
263 * sequence numbers in order, for example.
265 __u64 started_at; /* micro-seconds */
266 __u64 stopped_at; /* micro-seconds */
267 __u64 idle_acc; /* micro-seconds */
270 int clone_skb; /* Use multiple SKBs during packet gen. If this number
271 * is greater than 1, then that many copies of the same
272 * packet will be sent before a new packet is allocated.
273 * For instance, if you want to send 1024 identical packets
274 * before creating a new packet, set clone_skb to 1024.
277 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
278 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 struct in6_addr in6_saddr;
283 struct in6_addr in6_daddr;
284 struct in6_addr cur_in6_daddr;
285 struct in6_addr cur_in6_saddr;
287 struct in6_addr min_in6_daddr;
288 struct in6_addr max_in6_daddr;
289 struct in6_addr min_in6_saddr;
290 struct in6_addr max_in6_saddr;
292 /* If we're doing ranges, random or incremental, then this
293 * defines the min/max for those ranges.
295 __be32 saddr_min; /* inclusive, source IP address */
296 __be32 saddr_max; /* exclusive, source IP address */
297 __be32 daddr_min; /* inclusive, dest IP address */
298 __be32 daddr_max; /* exclusive, dest IP address */
300 __u16 udp_src_min; /* inclusive, source UDP port */
301 __u16 udp_src_max; /* exclusive, source UDP port */
302 __u16 udp_dst_min; /* inclusive, dest UDP port */
303 __u16 udp_dst_max; /* exclusive, dest UDP port */
306 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
307 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
310 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
311 __be32 labels[MAX_MPLS_LABELS];
313 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
316 __u16 vlan_id; /* 0xffff means no vlan tag */
320 __u16 svlan_id; /* 0xffff means no svlan tag */
322 __u32 src_mac_count; /* How many MACs to iterate through */
323 __u32 dst_mac_count; /* How many MACs to iterate through */
325 unsigned char dst_mac[ETH_ALEN];
326 unsigned char src_mac[ETH_ALEN];
328 __u32 cur_dst_mac_offset;
329 __u32 cur_src_mac_offset;
339 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
341 We fill in SRC address later
342 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346 __u16 pad; /* pad out the hh struct to an even 16 bytes */
348 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
349 * are transmitting the same one multiple times
351 struct net_device *odev; /* The out-going device. Note that the device should
352 * have it's pg_info pointer pointing back to this
353 * device. This will be set when the user specifies
354 * the out-going device name (not when the inject is
355 * started as it used to do.)
357 struct flow_state *flows;
358 unsigned cflows; /* Concurrent flows (config) */
359 unsigned lflow; /* Flow length (config) */
360 unsigned nflows; /* accumulated flows (stats) */
361 unsigned curfl; /* current sequenced flow (state)*/
367 __u8 ipsmode; /* IPSEC mode (config) */
368 __u8 ipsproto; /* IPSEC type (config) */
380 struct pktgen_thread {
382 struct list_head if_list; /* All device here */
383 struct list_head th_list;
384 struct task_struct *tsk;
387 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
392 wait_queue_head_t queue;
393 struct completion start_done;
399 /** Convert to micro-seconds */
400 static inline __u64 tv_to_us(const struct timeval *tv)
402 __u64 us = tv->tv_usec;
403 us += (__u64) tv->tv_sec * (__u64) 1000000;
407 static __u64 getCurUs(void)
410 do_gettimeofday(&tv);
411 return tv_to_us(&tv);
414 /* old include end */
416 static char version[] __initdata = VERSION;
418 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
419 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
420 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
422 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
423 static void pktgen_run_all_threads(void);
424 static void pktgen_stop_all_threads_ifs(void);
425 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
426 static void pktgen_stop(struct pktgen_thread *t);
427 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
429 static unsigned int scan_ip6(const char *s, char ip[16]);
430 static unsigned int fmt_ip6(char *s, const char ip[16]);
432 /* Module parameters, defaults. */
433 static int pg_count_d = 1000; /* 1000 pkts by default */
434 static int pg_delay_d;
435 static int pg_clone_skb_d;
438 static DEFINE_MUTEX(pktgen_thread_lock);
439 static LIST_HEAD(pktgen_threads);
441 static struct notifier_block pktgen_notifier_block = {
442 .notifier_call = pktgen_device_event,
446 * /proc handling functions
450 static int pgctrl_show(struct seq_file *seq, void *v)
452 seq_puts(seq, VERSION);
456 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
457 size_t count, loff_t * ppos)
462 if (!capable(CAP_NET_ADMIN)) {
467 if (count > sizeof(data))
468 count = sizeof(data);
470 if (copy_from_user(data, buf, count)) {
474 data[count - 1] = 0; /* Make string */
476 if (!strcmp(data, "stop"))
477 pktgen_stop_all_threads_ifs();
479 else if (!strcmp(data, "start"))
480 pktgen_run_all_threads();
483 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
491 static int pgctrl_open(struct inode *inode, struct file *file)
493 return single_open(file, pgctrl_show, PDE(inode)->data);
496 static const struct file_operations pktgen_fops = {
497 .owner = THIS_MODULE,
501 .write = pgctrl_write,
502 .release = single_release,
505 static int pktgen_if_show(struct seq_file *seq, void *v)
507 struct pktgen_dev *pkt_dev = seq->private;
510 __u64 now = getCurUs();
511 DECLARE_MAC_BUF(mac);
514 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
515 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
516 pkt_dev->max_pkt_size);
519 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
521 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
522 pkt_dev->clone_skb, pkt_dev->odev->name);
524 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
528 " queue_map_min: %u queue_map_max: %u\n",
529 pkt_dev->queue_map_min,
530 pkt_dev->queue_map_max);
532 if (pkt_dev->flags & F_IPV6) {
533 char b1[128], b2[128], b3[128];
534 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
535 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
536 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
538 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
541 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
542 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
543 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
545 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
550 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
551 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
554 seq_puts(seq, " src_mac: ");
556 seq_printf(seq, "%s ",
557 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
558 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
560 seq_printf(seq, "dst_mac: ");
561 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
564 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
565 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
566 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
569 " src_mac_count: %d dst_mac_count: %d\n",
570 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
572 if (pkt_dev->nr_labels) {
574 seq_printf(seq, " mpls: ");
575 for (i = 0; i < pkt_dev->nr_labels; i++)
576 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
577 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
580 if (pkt_dev->vlan_id != 0xffff) {
581 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
582 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
585 if (pkt_dev->svlan_id != 0xffff) {
586 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
587 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
591 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
594 if (pkt_dev->traffic_class) {
595 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
598 seq_printf(seq, " Flags: ");
600 if (pkt_dev->flags & F_IPV6)
601 seq_printf(seq, "IPV6 ");
603 if (pkt_dev->flags & F_IPSRC_RND)
604 seq_printf(seq, "IPSRC_RND ");
606 if (pkt_dev->flags & F_IPDST_RND)
607 seq_printf(seq, "IPDST_RND ");
609 if (pkt_dev->flags & F_TXSIZE_RND)
610 seq_printf(seq, "TXSIZE_RND ");
612 if (pkt_dev->flags & F_UDPSRC_RND)
613 seq_printf(seq, "UDPSRC_RND ");
615 if (pkt_dev->flags & F_UDPDST_RND)
616 seq_printf(seq, "UDPDST_RND ");
618 if (pkt_dev->flags & F_MPLS_RND)
619 seq_printf(seq, "MPLS_RND ");
621 if (pkt_dev->flags & F_QUEUE_MAP_RND)
622 seq_printf(seq, "QUEUE_MAP_RND ");
624 if (pkt_dev->cflows) {
625 if (pkt_dev->flags & F_FLOW_SEQ)
626 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
628 seq_printf(seq, "FLOW_RND ");
632 if (pkt_dev->flags & F_IPSEC_ON)
633 seq_printf(seq, "IPSEC ");
636 if (pkt_dev->flags & F_MACSRC_RND)
637 seq_printf(seq, "MACSRC_RND ");
639 if (pkt_dev->flags & F_MACDST_RND)
640 seq_printf(seq, "MACDST_RND ");
642 if (pkt_dev->flags & F_VID_RND)
643 seq_printf(seq, "VID_RND ");
645 if (pkt_dev->flags & F_SVID_RND)
646 seq_printf(seq, "SVID_RND ");
650 sa = pkt_dev->started_at;
651 stopped = pkt_dev->stopped_at;
652 if (pkt_dev->running)
653 stopped = now; /* not really stopped, more like last-running-at */
656 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
657 (unsigned long long)pkt_dev->sofar,
658 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
659 (unsigned long long)stopped,
660 (unsigned long long)pkt_dev->idle_acc);
663 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
664 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
665 pkt_dev->cur_src_mac_offset);
667 if (pkt_dev->flags & F_IPV6) {
668 char b1[128], b2[128];
669 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
670 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
671 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
673 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
674 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
676 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
677 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
679 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
681 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
683 if (pkt_dev->result[0])
684 seq_printf(seq, "Result: %s\n", pkt_dev->result);
686 seq_printf(seq, "Result: Idle\n");
692 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
697 for (; i < maxlen; i++) {
700 if (get_user(c, &user_buffer[i]))
702 if ((c >= '0') && (c <= '9'))
704 else if ((c >= 'a') && (c <= 'f'))
705 *num |= c - 'a' + 10;
706 else if ((c >= 'A') && (c <= 'F'))
707 *num |= c - 'A' + 10;
714 static int count_trail_chars(const char __user * user_buffer,
719 for (i = 0; i < maxlen; i++) {
721 if (get_user(c, &user_buffer[i]))
739 static unsigned long num_arg(const char __user * user_buffer,
740 unsigned long maxlen, unsigned long *num)
745 for (; i < maxlen; i++) {
747 if (get_user(c, &user_buffer[i]))
749 if ((c >= '0') && (c <= '9')) {
758 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
762 for (; i < maxlen; i++) {
764 if (get_user(c, &user_buffer[i]))
782 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
789 pkt_dev->nr_labels = 0;
792 len = hex32_arg(&buffer[i], 8, &tmp);
795 pkt_dev->labels[n] = htonl(tmp);
796 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
797 pkt_dev->flags |= F_MPLS_RND;
799 if (get_user(c, &buffer[i]))
803 if (n >= MAX_MPLS_LABELS)
807 pkt_dev->nr_labels = n;
811 static ssize_t pktgen_if_write(struct file *file,
812 const char __user * user_buffer, size_t count,
815 struct seq_file *seq = (struct seq_file *)file->private_data;
816 struct pktgen_dev *pkt_dev = seq->private;
818 char name[16], valstr[32];
819 unsigned long value = 0;
820 char *pg_result = NULL;
824 pg_result = &(pkt_dev->result[0]);
827 printk(KERN_WARNING "pktgen: wrong command format\n");
832 tmp = count_trail_chars(&user_buffer[i], max);
834 printk(KERN_WARNING "pktgen: illegal format\n");
839 /* Read variable name */
841 len = strn_len(&user_buffer[i], sizeof(name) - 1);
845 memset(name, 0, sizeof(name));
846 if (copy_from_user(name, &user_buffer[i], len))
851 len = count_trail_chars(&user_buffer[i], max);
859 if (copy_from_user(tb, user_buffer, count))
862 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
863 (unsigned long)count, tb);
866 if (!strcmp(name, "min_pkt_size")) {
867 len = num_arg(&user_buffer[i], 10, &value);
872 if (value < 14 + 20 + 8)
874 if (value != pkt_dev->min_pkt_size) {
875 pkt_dev->min_pkt_size = value;
876 pkt_dev->cur_pkt_size = value;
878 sprintf(pg_result, "OK: min_pkt_size=%u",
879 pkt_dev->min_pkt_size);
883 if (!strcmp(name, "max_pkt_size")) {
884 len = num_arg(&user_buffer[i], 10, &value);
889 if (value < 14 + 20 + 8)
891 if (value != pkt_dev->max_pkt_size) {
892 pkt_dev->max_pkt_size = value;
893 pkt_dev->cur_pkt_size = value;
895 sprintf(pg_result, "OK: max_pkt_size=%u",
896 pkt_dev->max_pkt_size);
900 /* Shortcut for min = max */
902 if (!strcmp(name, "pkt_size")) {
903 len = num_arg(&user_buffer[i], 10, &value);
908 if (value < 14 + 20 + 8)
910 if (value != pkt_dev->min_pkt_size) {
911 pkt_dev->min_pkt_size = value;
912 pkt_dev->max_pkt_size = value;
913 pkt_dev->cur_pkt_size = value;
915 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
919 if (!strcmp(name, "debug")) {
920 len = num_arg(&user_buffer[i], 10, &value);
926 sprintf(pg_result, "OK: debug=%u", debug);
930 if (!strcmp(name, "frags")) {
931 len = num_arg(&user_buffer[i], 10, &value);
936 pkt_dev->nfrags = value;
937 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
940 if (!strcmp(name, "delay")) {
941 len = num_arg(&user_buffer[i], 10, &value);
946 if (value == 0x7FFFFFFF) {
947 pkt_dev->delay_us = 0x7FFFFFFF;
948 pkt_dev->delay_ns = 0;
950 pkt_dev->delay_us = value / 1000;
951 pkt_dev->delay_ns = value % 1000;
953 sprintf(pg_result, "OK: delay=%u",
954 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
957 if (!strcmp(name, "udp_src_min")) {
958 len = num_arg(&user_buffer[i], 10, &value);
963 if (value != pkt_dev->udp_src_min) {
964 pkt_dev->udp_src_min = value;
965 pkt_dev->cur_udp_src = value;
967 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
970 if (!strcmp(name, "udp_dst_min")) {
971 len = num_arg(&user_buffer[i], 10, &value);
976 if (value != pkt_dev->udp_dst_min) {
977 pkt_dev->udp_dst_min = value;
978 pkt_dev->cur_udp_dst = value;
980 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
983 if (!strcmp(name, "udp_src_max")) {
984 len = num_arg(&user_buffer[i], 10, &value);
989 if (value != pkt_dev->udp_src_max) {
990 pkt_dev->udp_src_max = value;
991 pkt_dev->cur_udp_src = value;
993 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
996 if (!strcmp(name, "udp_dst_max")) {
997 len = num_arg(&user_buffer[i], 10, &value);
1002 if (value != pkt_dev->udp_dst_max) {
1003 pkt_dev->udp_dst_max = value;
1004 pkt_dev->cur_udp_dst = value;
1006 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1009 if (!strcmp(name, "clone_skb")) {
1010 len = num_arg(&user_buffer[i], 10, &value);
1015 pkt_dev->clone_skb = value;
1017 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1020 if (!strcmp(name, "count")) {
1021 len = num_arg(&user_buffer[i], 10, &value);
1026 pkt_dev->count = value;
1027 sprintf(pg_result, "OK: count=%llu",
1028 (unsigned long long)pkt_dev->count);
1031 if (!strcmp(name, "src_mac_count")) {
1032 len = num_arg(&user_buffer[i], 10, &value);
1037 if (pkt_dev->src_mac_count != value) {
1038 pkt_dev->src_mac_count = value;
1039 pkt_dev->cur_src_mac_offset = 0;
1041 sprintf(pg_result, "OK: src_mac_count=%d",
1042 pkt_dev->src_mac_count);
1045 if (!strcmp(name, "dst_mac_count")) {
1046 len = num_arg(&user_buffer[i], 10, &value);
1051 if (pkt_dev->dst_mac_count != value) {
1052 pkt_dev->dst_mac_count = value;
1053 pkt_dev->cur_dst_mac_offset = 0;
1055 sprintf(pg_result, "OK: dst_mac_count=%d",
1056 pkt_dev->dst_mac_count);
1059 if (!strcmp(name, "flag")) {
1062 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1066 if (copy_from_user(f, &user_buffer[i], len))
1069 if (strcmp(f, "IPSRC_RND") == 0)
1070 pkt_dev->flags |= F_IPSRC_RND;
1072 else if (strcmp(f, "!IPSRC_RND") == 0)
1073 pkt_dev->flags &= ~F_IPSRC_RND;
1075 else if (strcmp(f, "TXSIZE_RND") == 0)
1076 pkt_dev->flags |= F_TXSIZE_RND;
1078 else if (strcmp(f, "!TXSIZE_RND") == 0)
1079 pkt_dev->flags &= ~F_TXSIZE_RND;
1081 else if (strcmp(f, "IPDST_RND") == 0)
1082 pkt_dev->flags |= F_IPDST_RND;
1084 else if (strcmp(f, "!IPDST_RND") == 0)
1085 pkt_dev->flags &= ~F_IPDST_RND;
1087 else if (strcmp(f, "UDPSRC_RND") == 0)
1088 pkt_dev->flags |= F_UDPSRC_RND;
1090 else if (strcmp(f, "!UDPSRC_RND") == 0)
1091 pkt_dev->flags &= ~F_UDPSRC_RND;
1093 else if (strcmp(f, "UDPDST_RND") == 0)
1094 pkt_dev->flags |= F_UDPDST_RND;
1096 else if (strcmp(f, "!UDPDST_RND") == 0)
1097 pkt_dev->flags &= ~F_UDPDST_RND;
1099 else if (strcmp(f, "MACSRC_RND") == 0)
1100 pkt_dev->flags |= F_MACSRC_RND;
1102 else if (strcmp(f, "!MACSRC_RND") == 0)
1103 pkt_dev->flags &= ~F_MACSRC_RND;
1105 else if (strcmp(f, "MACDST_RND") == 0)
1106 pkt_dev->flags |= F_MACDST_RND;
1108 else if (strcmp(f, "!MACDST_RND") == 0)
1109 pkt_dev->flags &= ~F_MACDST_RND;
1111 else if (strcmp(f, "MPLS_RND") == 0)
1112 pkt_dev->flags |= F_MPLS_RND;
1114 else if (strcmp(f, "!MPLS_RND") == 0)
1115 pkt_dev->flags &= ~F_MPLS_RND;
1117 else if (strcmp(f, "VID_RND") == 0)
1118 pkt_dev->flags |= F_VID_RND;
1120 else if (strcmp(f, "!VID_RND") == 0)
1121 pkt_dev->flags &= ~F_VID_RND;
1123 else if (strcmp(f, "SVID_RND") == 0)
1124 pkt_dev->flags |= F_SVID_RND;
1126 else if (strcmp(f, "!SVID_RND") == 0)
1127 pkt_dev->flags &= ~F_SVID_RND;
1129 else if (strcmp(f, "FLOW_SEQ") == 0)
1130 pkt_dev->flags |= F_FLOW_SEQ;
1132 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1133 pkt_dev->flags |= F_QUEUE_MAP_RND;
1135 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1136 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1138 else if (strcmp(f, "IPSEC") == 0)
1139 pkt_dev->flags |= F_IPSEC_ON;
1142 else if (strcmp(f, "!IPV6") == 0)
1143 pkt_dev->flags &= ~F_IPV6;
1147 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1149 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1150 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1153 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1156 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1157 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1162 if (copy_from_user(buf, &user_buffer[i], len))
1165 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1166 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1167 strncpy(pkt_dev->dst_min, buf, len);
1168 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1169 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1172 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1175 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1178 if (!strcmp(name, "dst_max")) {
1179 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1184 if (copy_from_user(buf, &user_buffer[i], len))
1188 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1189 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1190 strncpy(pkt_dev->dst_max, buf, len);
1191 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1192 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1195 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1198 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1201 if (!strcmp(name, "dst6")) {
1202 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1206 pkt_dev->flags |= F_IPV6;
1208 if (copy_from_user(buf, &user_buffer[i], len))
1212 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1213 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1215 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1218 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1221 sprintf(pg_result, "OK: dst6=%s", buf);
1224 if (!strcmp(name, "dst6_min")) {
1225 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1229 pkt_dev->flags |= F_IPV6;
1231 if (copy_from_user(buf, &user_buffer[i], len))
1235 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1236 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1238 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1239 &pkt_dev->min_in6_daddr);
1241 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1244 sprintf(pg_result, "OK: dst6_min=%s", buf);
1247 if (!strcmp(name, "dst6_max")) {
1248 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1252 pkt_dev->flags |= F_IPV6;
1254 if (copy_from_user(buf, &user_buffer[i], len))
1258 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1259 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1262 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1265 sprintf(pg_result, "OK: dst6_max=%s", buf);
1268 if (!strcmp(name, "src6")) {
1269 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1273 pkt_dev->flags |= F_IPV6;
1275 if (copy_from_user(buf, &user_buffer[i], len))
1279 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1280 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1282 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1285 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1288 sprintf(pg_result, "OK: src6=%s", buf);
1291 if (!strcmp(name, "src_min")) {
1292 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1296 if (copy_from_user(buf, &user_buffer[i], len))
1299 if (strcmp(buf, pkt_dev->src_min) != 0) {
1300 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1301 strncpy(pkt_dev->src_min, buf, len);
1302 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1303 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1306 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1309 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1312 if (!strcmp(name, "src_max")) {
1313 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1317 if (copy_from_user(buf, &user_buffer[i], len))
1320 if (strcmp(buf, pkt_dev->src_max) != 0) {
1321 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1322 strncpy(pkt_dev->src_max, buf, len);
1323 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1324 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1327 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1330 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1333 if (!strcmp(name, "dst_mac")) {
1335 unsigned char old_dmac[ETH_ALEN];
1336 unsigned char *m = pkt_dev->dst_mac;
1337 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1339 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1343 memset(valstr, 0, sizeof(valstr));
1344 if (copy_from_user(valstr, &user_buffer[i], len))
1348 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1349 if (*v >= '0' && *v <= '9') {
1353 if (*v >= 'A' && *v <= 'F') {
1355 *m += *v - 'A' + 10;
1357 if (*v >= 'a' && *v <= 'f') {
1359 *m += *v - 'a' + 10;
1367 /* Set up Dest MAC */
1368 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1369 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1371 sprintf(pg_result, "OK: dstmac");
1374 if (!strcmp(name, "src_mac")) {
1376 unsigned char old_smac[ETH_ALEN];
1377 unsigned char *m = pkt_dev->src_mac;
1379 memcpy(old_smac, pkt_dev->src_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->src_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 Src MAC */
1410 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1411 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1413 sprintf(pg_result, "OK: srcmac");
1417 if (!strcmp(name, "clear_counters")) {
1418 pktgen_clear_counters(pkt_dev);
1419 sprintf(pg_result, "OK: Clearing counters.\n");
1423 if (!strcmp(name, "flows")) {
1424 len = num_arg(&user_buffer[i], 10, &value);
1429 if (value > MAX_CFLOWS)
1432 pkt_dev->cflows = value;
1433 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1437 if (!strcmp(name, "flowlen")) {
1438 len = num_arg(&user_buffer[i], 10, &value);
1443 pkt_dev->lflow = value;
1444 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1448 if (!strcmp(name, "queue_map_min")) {
1449 len = num_arg(&user_buffer[i], 5, &value);
1454 pkt_dev->queue_map_min = value;
1455 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1459 if (!strcmp(name, "queue_map_max")) {
1460 len = num_arg(&user_buffer[i], 5, &value);
1465 pkt_dev->queue_map_max = value;
1466 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1470 if (!strcmp(name, "mpls")) {
1473 len = get_labels(&user_buffer[i], pkt_dev);
1477 cnt = sprintf(pg_result, "OK: mpls=");
1478 for (n = 0; n < pkt_dev->nr_labels; n++)
1479 cnt += sprintf(pg_result + cnt,
1480 "%08x%s", ntohl(pkt_dev->labels[n]),
1481 n == pkt_dev->nr_labels-1 ? "" : ",");
1483 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1484 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1485 pkt_dev->svlan_id = 0xffff;
1488 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1493 if (!strcmp(name, "vlan_id")) {
1494 len = num_arg(&user_buffer[i], 4, &value);
1499 if (value <= 4095) {
1500 pkt_dev->vlan_id = value; /* turn on VLAN */
1503 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1505 if (debug && pkt_dev->nr_labels)
1506 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1508 pkt_dev->nr_labels = 0; /* turn off MPLS */
1509 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1511 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1512 pkt_dev->svlan_id = 0xffff;
1515 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1520 if (!strcmp(name, "vlan_p")) {
1521 len = num_arg(&user_buffer[i], 1, &value);
1526 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1527 pkt_dev->vlan_p = value;
1528 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1530 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1535 if (!strcmp(name, "vlan_cfi")) {
1536 len = num_arg(&user_buffer[i], 1, &value);
1541 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1542 pkt_dev->vlan_cfi = value;
1543 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1545 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1550 if (!strcmp(name, "svlan_id")) {
1551 len = num_arg(&user_buffer[i], 4, &value);
1556 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1557 pkt_dev->svlan_id = value; /* turn on SVLAN */
1560 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1562 if (debug && pkt_dev->nr_labels)
1563 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1565 pkt_dev->nr_labels = 0; /* turn off MPLS */
1566 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1568 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1569 pkt_dev->svlan_id = 0xffff;
1572 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1577 if (!strcmp(name, "svlan_p")) {
1578 len = num_arg(&user_buffer[i], 1, &value);
1583 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1584 pkt_dev->svlan_p = value;
1585 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1587 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1592 if (!strcmp(name, "svlan_cfi")) {
1593 len = num_arg(&user_buffer[i], 1, &value);
1598 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1599 pkt_dev->svlan_cfi = value;
1600 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1602 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1607 if (!strcmp(name, "tos")) {
1608 __u32 tmp_value = 0;
1609 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1615 pkt_dev->tos = tmp_value;
1616 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1618 sprintf(pg_result, "ERROR: tos must be 00-ff");
1623 if (!strcmp(name, "traffic_class")) {
1624 __u32 tmp_value = 0;
1625 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1631 pkt_dev->traffic_class = tmp_value;
1632 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1634 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1639 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1643 static int pktgen_if_open(struct inode *inode, struct file *file)
1645 return single_open(file, pktgen_if_show, PDE(inode)->data);
1648 static const struct file_operations pktgen_if_fops = {
1649 .owner = THIS_MODULE,
1650 .open = pktgen_if_open,
1652 .llseek = seq_lseek,
1653 .write = pktgen_if_write,
1654 .release = single_release,
1657 static int pktgen_thread_show(struct seq_file *seq, void *v)
1659 struct pktgen_thread *t = seq->private;
1660 struct pktgen_dev *pkt_dev;
1664 seq_printf(seq, "Running: ");
1667 list_for_each_entry(pkt_dev, &t->if_list, list)
1668 if (pkt_dev->running)
1669 seq_printf(seq, "%s ", pkt_dev->odev->name);
1671 seq_printf(seq, "\nStopped: ");
1673 list_for_each_entry(pkt_dev, &t->if_list, list)
1674 if (!pkt_dev->running)
1675 seq_printf(seq, "%s ", pkt_dev->odev->name);
1678 seq_printf(seq, "\nResult: %s\n", t->result);
1680 seq_printf(seq, "\nResult: NA\n");
1687 static ssize_t pktgen_thread_write(struct file *file,
1688 const char __user * user_buffer,
1689 size_t count, loff_t * offset)
1691 struct seq_file *seq = (struct seq_file *)file->private_data;
1692 struct pktgen_thread *t = seq->private;
1693 int i = 0, max, len, ret;
1698 // sprintf(pg_result, "Wrong command format");
1703 len = count_trail_chars(&user_buffer[i], max);
1709 /* Read variable name */
1711 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1715 memset(name, 0, sizeof(name));
1716 if (copy_from_user(name, &user_buffer[i], len))
1721 len = count_trail_chars(&user_buffer[i], max);
1728 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1729 name, (unsigned long)count);
1732 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1737 pg_result = &(t->result[0]);
1739 if (!strcmp(name, "add_device")) {
1742 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1747 if (copy_from_user(f, &user_buffer[i], len))
1750 mutex_lock(&pktgen_thread_lock);
1751 pktgen_add_device(t, f);
1752 mutex_unlock(&pktgen_thread_lock);
1754 sprintf(pg_result, "OK: add_device=%s", f);
1758 if (!strcmp(name, "rem_device_all")) {
1759 mutex_lock(&pktgen_thread_lock);
1760 t->control |= T_REMDEVALL;
1761 mutex_unlock(&pktgen_thread_lock);
1762 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1764 sprintf(pg_result, "OK: rem_device_all");
1768 if (!strcmp(name, "max_before_softirq")) {
1769 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1779 static int pktgen_thread_open(struct inode *inode, struct file *file)
1781 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1784 static const struct file_operations pktgen_thread_fops = {
1785 .owner = THIS_MODULE,
1786 .open = pktgen_thread_open,
1788 .llseek = seq_lseek,
1789 .write = pktgen_thread_write,
1790 .release = single_release,
1793 /* Think find or remove for NN */
1794 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1796 struct pktgen_thread *t;
1797 struct pktgen_dev *pkt_dev = NULL;
1799 list_for_each_entry(t, &pktgen_threads, th_list) {
1800 pkt_dev = pktgen_find_dev(t, ifname);
1804 pkt_dev->removal_mark = 1;
1805 t->control |= T_REMDEV;
1815 * mark a device for removal
1817 static void pktgen_mark_device(const char *ifname)
1819 struct pktgen_dev *pkt_dev = NULL;
1820 const int max_tries = 10, msec_per_try = 125;
1823 mutex_lock(&pktgen_thread_lock);
1824 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1828 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1829 if (pkt_dev == NULL)
1830 break; /* success */
1832 mutex_unlock(&pktgen_thread_lock);
1833 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1834 "to disappear....\n", ifname);
1835 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1836 mutex_lock(&pktgen_thread_lock);
1838 if (++i >= max_tries) {
1839 printk(KERN_ERR "pktgen_mark_device: timed out after "
1840 "waiting %d msec for device %s to be removed\n",
1841 msec_per_try * i, ifname);
1847 mutex_unlock(&pktgen_thread_lock);
1850 static void pktgen_change_name(struct net_device *dev)
1852 struct pktgen_thread *t;
1854 list_for_each_entry(t, &pktgen_threads, th_list) {
1855 struct pktgen_dev *pkt_dev;
1857 list_for_each_entry(pkt_dev, &t->if_list, list) {
1858 if (pkt_dev->odev != dev)
1861 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1863 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1865 if (!pkt_dev->entry)
1866 printk(KERN_ERR "pktgen: can't move proc "
1867 " entry for '%s'\n", dev->name);
1873 static int pktgen_device_event(struct notifier_block *unused,
1874 unsigned long event, void *ptr)
1876 struct net_device *dev = ptr;
1878 if (!net_eq(dev_net(dev), &init_net))
1881 /* It is OK that we do not hold the group lock right now,
1882 * as we run under the RTNL lock.
1886 case NETDEV_CHANGENAME:
1887 pktgen_change_name(dev);
1890 case NETDEV_UNREGISTER:
1891 pktgen_mark_device(dev->name);
1898 /* Associate pktgen_dev with a device. */
1900 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1902 struct net_device *odev;
1905 /* Clean old setups */
1906 if (pkt_dev->odev) {
1907 dev_put(pkt_dev->odev);
1908 pkt_dev->odev = NULL;
1911 odev = dev_get_by_name(&init_net, ifname);
1913 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1917 if (odev->type != ARPHRD_ETHER) {
1918 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1920 } else if (!netif_running(odev)) {
1921 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1924 pkt_dev->odev = odev;
1932 /* Read pkt_dev from the interface and set up internal pktgen_dev
1933 * structure to have the right information to create/send packets
1935 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1937 if (!pkt_dev->odev) {
1938 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1940 sprintf(pkt_dev->result,
1941 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1945 /* Default to the interface's mac if not explicitly set. */
1947 if (is_zero_ether_addr(pkt_dev->src_mac))
1948 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1950 /* Set up Dest MAC */
1951 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1953 /* Set up pkt size */
1954 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1956 if (pkt_dev->flags & F_IPV6) {
1958 * Skip this automatic address setting until locks or functions
1963 int i, set = 0, err = 1;
1964 struct inet6_dev *idev;
1966 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1967 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1975 * Use linklevel address if unconfigured.
1977 * use ipv6_get_lladdr if/when it's get exported
1981 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1982 struct inet6_ifaddr *ifp;
1984 read_lock_bh(&idev->lock);
1985 for (ifp = idev->addr_list; ifp;
1986 ifp = ifp->if_next) {
1987 if (ifp->scope == IFA_LINK
1989 flags & IFA_F_TENTATIVE)) {
1990 ipv6_addr_copy(&pkt_dev->
1997 read_unlock_bh(&idev->lock);
2001 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2002 "address not availble.\n");
2006 pkt_dev->saddr_min = 0;
2007 pkt_dev->saddr_max = 0;
2008 if (strlen(pkt_dev->src_min) == 0) {
2010 struct in_device *in_dev;
2013 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2015 if (in_dev->ifa_list) {
2016 pkt_dev->saddr_min =
2017 in_dev->ifa_list->ifa_address;
2018 pkt_dev->saddr_max = pkt_dev->saddr_min;
2023 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2024 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2027 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2028 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2030 /* Initialize current values. */
2031 pkt_dev->cur_dst_mac_offset = 0;
2032 pkt_dev->cur_src_mac_offset = 0;
2033 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2034 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2035 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2036 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2037 pkt_dev->nflows = 0;
2040 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2045 start = now = getCurUs();
2046 while (now < spin_until_us) {
2047 /* TODO: optimize sleeping behavior */
2048 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2049 schedule_timeout_interruptible(1);
2050 else if (spin_until_us - now > 100) {
2051 if (!pkt_dev->running)
2060 pkt_dev->idle_acc += now - start;
2063 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2065 pkt_dev->pkt_overhead = 0;
2066 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2067 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2068 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2071 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2074 if (pkt_dev->flows[flow].flags & F_INIT)
2080 static inline int f_pick(struct pktgen_dev *pkt_dev)
2082 int flow = pkt_dev->curfl;
2084 if (pkt_dev->flags & F_FLOW_SEQ) {
2085 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2087 pkt_dev->flows[flow].count = 0;
2088 pkt_dev->flows[flow].flags = 0;
2089 pkt_dev->curfl += 1;
2090 if (pkt_dev->curfl >= pkt_dev->cflows)
2091 pkt_dev->curfl = 0; /*reset */
2094 flow = random32() % pkt_dev->cflows;
2095 pkt_dev->curfl = flow;
2097 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2098 pkt_dev->flows[flow].count = 0;
2099 pkt_dev->flows[flow].flags = 0;
2103 return pkt_dev->curfl;
2108 /* If there was already an IPSEC SA, we keep it as is, else
2109 * we go look for it ...
2111 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2113 struct xfrm_state *x = pkt_dev->flows[flow].x;
2115 /*slow path: we dont already have xfrm_state*/
2116 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2117 (xfrm_address_t *)&pkt_dev->cur_saddr,
2120 pkt_dev->ipsproto, 0);
2122 pkt_dev->flows[flow].x = x;
2123 set_pkt_overhead(pkt_dev);
2124 pkt_dev->pkt_overhead+=x->props.header_len;
2130 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2132 if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2134 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2136 (pkt_dev->queue_map_max -
2137 pkt_dev->queue_map_min + 1)
2138 + pkt_dev->queue_map_min;
2140 t = pkt_dev->cur_queue_map + 1;
2141 if (t > pkt_dev->queue_map_max)
2142 t = pkt_dev->queue_map_min;
2144 pkt_dev->cur_queue_map = t;
2148 /* Increment/randomize headers according to flags and current values
2149 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2151 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2157 if (pkt_dev->cflows)
2158 flow = f_pick(pkt_dev);
2160 /* Deal with source MAC */
2161 if (pkt_dev->src_mac_count > 1) {
2165 if (pkt_dev->flags & F_MACSRC_RND)
2166 mc = random32() % pkt_dev->src_mac_count;
2168 mc = pkt_dev->cur_src_mac_offset++;
2169 if (pkt_dev->cur_src_mac_offset >=
2170 pkt_dev->src_mac_count)
2171 pkt_dev->cur_src_mac_offset = 0;
2174 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2175 pkt_dev->hh[11] = tmp;
2176 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2177 pkt_dev->hh[10] = tmp;
2178 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2179 pkt_dev->hh[9] = tmp;
2180 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2181 pkt_dev->hh[8] = tmp;
2182 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2183 pkt_dev->hh[7] = tmp;
2186 /* Deal with Destination MAC */
2187 if (pkt_dev->dst_mac_count > 1) {
2191 if (pkt_dev->flags & F_MACDST_RND)
2192 mc = random32() % pkt_dev->dst_mac_count;
2195 mc = pkt_dev->cur_dst_mac_offset++;
2196 if (pkt_dev->cur_dst_mac_offset >=
2197 pkt_dev->dst_mac_count) {
2198 pkt_dev->cur_dst_mac_offset = 0;
2202 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2203 pkt_dev->hh[5] = tmp;
2204 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2205 pkt_dev->hh[4] = tmp;
2206 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2207 pkt_dev->hh[3] = tmp;
2208 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2209 pkt_dev->hh[2] = tmp;
2210 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2211 pkt_dev->hh[1] = tmp;
2214 if (pkt_dev->flags & F_MPLS_RND) {
2216 for (i = 0; i < pkt_dev->nr_labels; i++)
2217 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2218 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2219 ((__force __be32)random32() &
2223 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2224 pkt_dev->vlan_id = random32() & (4096-1);
2227 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2228 pkt_dev->svlan_id = random32() & (4096 - 1);
2231 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2232 if (pkt_dev->flags & F_UDPSRC_RND)
2233 pkt_dev->cur_udp_src = random32() %
2234 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2235 + pkt_dev->udp_src_min;
2238 pkt_dev->cur_udp_src++;
2239 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2240 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2244 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2245 if (pkt_dev->flags & F_UDPDST_RND) {
2246 pkt_dev->cur_udp_dst = random32() %
2247 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2248 + pkt_dev->udp_dst_min;
2250 pkt_dev->cur_udp_dst++;
2251 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2252 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2256 if (!(pkt_dev->flags & F_IPV6)) {
2258 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2262 if (pkt_dev->flags & F_IPSRC_RND)
2263 t = random32() % (imx - imn) + imn;
2265 t = ntohl(pkt_dev->cur_saddr);
2271 pkt_dev->cur_saddr = htonl(t);
2274 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2275 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2277 imn = ntohl(pkt_dev->daddr_min);
2278 imx = ntohl(pkt_dev->daddr_max);
2282 if (pkt_dev->flags & F_IPDST_RND) {
2284 t = random32() % (imx - imn) + imn;
2287 while (ipv4_is_loopback(s) ||
2288 ipv4_is_multicast(s) ||
2289 ipv4_is_lbcast(s) ||
2290 ipv4_is_zeronet(s) ||
2291 ipv4_is_local_multicast(s)) {
2292 t = random32() % (imx - imn) + imn;
2295 pkt_dev->cur_daddr = s;
2297 t = ntohl(pkt_dev->cur_daddr);
2302 pkt_dev->cur_daddr = htonl(t);
2305 if (pkt_dev->cflows) {
2306 pkt_dev->flows[flow].flags |= F_INIT;
2307 pkt_dev->flows[flow].cur_daddr =
2310 if (pkt_dev->flags & F_IPSEC_ON)
2311 get_ipsec_sa(pkt_dev, flow);
2316 } else { /* IPV6 * */
2318 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2319 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2320 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2321 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2325 /* Only random destinations yet */
2327 for (i = 0; i < 4; i++) {
2328 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2329 (((__force __be32)random32() |
2330 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2331 pkt_dev->max_in6_daddr.s6_addr32[i]);
2336 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2338 if (pkt_dev->flags & F_TXSIZE_RND) {
2340 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2341 + pkt_dev->min_pkt_size;
2343 t = pkt_dev->cur_pkt_size + 1;
2344 if (t > pkt_dev->max_pkt_size)
2345 t = pkt_dev->min_pkt_size;
2347 pkt_dev->cur_pkt_size = t;
2350 set_cur_queue_map(pkt_dev);
2352 pkt_dev->flows[flow].count++;
2357 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2359 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2365 /* XXX: we dont support tunnel mode for now until
2366 * we resolve the dst issue */
2367 if (x->props.mode != XFRM_MODE_TRANSPORT)
2370 spin_lock(&x->lock);
2373 err = x->outer_mode->output(x, skb);
2376 err = x->type->output(x, skb);
2380 x->curlft.bytes +=skb->len;
2381 x->curlft.packets++;
2383 spin_unlock(&x->lock);
2387 static inline void free_SAs(struct pktgen_dev *pkt_dev)
2389 if (pkt_dev->cflows) {
2390 /* let go of the SAs if we have them */
2392 for (; i < pkt_dev->nflows; i++){
2393 struct xfrm_state *x = pkt_dev->flows[i].x;
2396 pkt_dev->flows[i].x = NULL;
2402 static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2403 struct sk_buff *skb, __be16 protocol)
2405 if (pkt_dev->flags & F_IPSEC_ON) {
2406 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2411 nhead = x->props.header_len - skb_headroom(skb);
2413 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2415 printk(KERN_ERR "Error expanding "
2416 "ipsec packet %d\n",ret);
2421 /* ipsec is not expecting ll header */
2422 skb_pull(skb, ETH_HLEN);
2423 ret = pktgen_output_ipsec(skb, pkt_dev);
2425 printk(KERN_ERR "Error creating ipsec "
2431 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2432 memcpy(eth, pkt_dev->hh, 12);
2433 *(u16 *) & eth[12] = protocol;
2440 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2443 for (i = 0; i < pkt_dev->nr_labels; i++) {
2444 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2447 *mpls |= MPLS_STACK_BOTTOM;
2450 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2453 return htons(id | (cfi << 12) | (prio << 13));
2456 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2457 struct pktgen_dev *pkt_dev)
2459 struct sk_buff *skb = NULL;
2461 struct udphdr *udph;
2464 struct pktgen_hdr *pgh = NULL;
2465 __be16 protocol = htons(ETH_P_IP);
2467 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2468 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2469 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2470 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2473 if (pkt_dev->nr_labels)
2474 protocol = htons(ETH_P_MPLS_UC);
2476 if (pkt_dev->vlan_id != 0xffff)
2477 protocol = htons(ETH_P_8021Q);
2479 /* Update any of the values, used when we're incrementing various
2482 queue_map = pkt_dev->cur_queue_map;
2483 mod_cur_headers(pkt_dev);
2485 datalen = (odev->hard_header_len + 16) & ~0xf;
2486 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2487 pkt_dev->pkt_overhead, GFP_ATOMIC);
2489 sprintf(pkt_dev->result, "No memory");
2493 skb_reserve(skb, datalen);
2495 /* Reserve for ethernet and IP header */
2496 eth = (__u8 *) skb_push(skb, 14);
2497 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2498 if (pkt_dev->nr_labels)
2499 mpls_push(mpls, pkt_dev);
2501 if (pkt_dev->vlan_id != 0xffff) {
2502 if (pkt_dev->svlan_id != 0xffff) {
2503 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2504 *svlan_tci = build_tci(pkt_dev->svlan_id,
2507 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2508 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2510 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2511 *vlan_tci = build_tci(pkt_dev->vlan_id,
2514 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2515 *vlan_encapsulated_proto = htons(ETH_P_IP);
2518 skb->network_header = skb->tail;
2519 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2520 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2521 skb_set_queue_mapping(skb, queue_map);
2523 udph = udp_hdr(skb);
2525 memcpy(eth, pkt_dev->hh, 12);
2526 *(__be16 *) & eth[12] = protocol;
2528 /* Eth + IPh + UDPh + mpls */
2529 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2530 pkt_dev->pkt_overhead;
2531 if (datalen < sizeof(struct pktgen_hdr))
2532 datalen = sizeof(struct pktgen_hdr);
2534 udph->source = htons(pkt_dev->cur_udp_src);
2535 udph->dest = htons(pkt_dev->cur_udp_dst);
2536 udph->len = htons(datalen + 8); /* DATA + udphdr */
2537 udph->check = 0; /* No checksum */
2542 iph->tos = pkt_dev->tos;
2543 iph->protocol = IPPROTO_UDP; /* UDP */
2544 iph->saddr = pkt_dev->cur_saddr;
2545 iph->daddr = pkt_dev->cur_daddr;
2547 iplen = 20 + 8 + datalen;
2548 iph->tot_len = htons(iplen);
2550 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2551 skb->protocol = protocol;
2552 skb->mac_header = (skb->network_header - ETH_HLEN -
2553 pkt_dev->pkt_overhead);
2555 skb->pkt_type = PACKET_HOST;
2557 if (pkt_dev->nfrags <= 0)
2558 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2560 int frags = pkt_dev->nfrags;
2563 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2565 if (frags > MAX_SKB_FRAGS)
2566 frags = MAX_SKB_FRAGS;
2567 if (datalen > frags * PAGE_SIZE) {
2568 skb_put(skb, datalen - frags * PAGE_SIZE);
2569 datalen = frags * PAGE_SIZE;
2573 while (datalen > 0) {
2574 struct page *page = alloc_pages(GFP_KERNEL, 0);
2575 skb_shinfo(skb)->frags[i].page = page;
2576 skb_shinfo(skb)->frags[i].page_offset = 0;
2577 skb_shinfo(skb)->frags[i].size =
2578 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2579 datalen -= skb_shinfo(skb)->frags[i].size;
2580 skb->len += skb_shinfo(skb)->frags[i].size;
2581 skb->data_len += skb_shinfo(skb)->frags[i].size;
2583 skb_shinfo(skb)->nr_frags = i;
2592 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2596 skb_shinfo(skb)->frags[i - 1].size -= rem;
2598 skb_shinfo(skb)->frags[i] =
2599 skb_shinfo(skb)->frags[i - 1];
2600 get_page(skb_shinfo(skb)->frags[i].page);
2601 skb_shinfo(skb)->frags[i].page =
2602 skb_shinfo(skb)->frags[i - 1].page;
2603 skb_shinfo(skb)->frags[i].page_offset +=
2604 skb_shinfo(skb)->frags[i - 1].size;
2605 skb_shinfo(skb)->frags[i].size = rem;
2607 skb_shinfo(skb)->nr_frags = i;
2611 /* Stamp the time, and sequence number, convert them to network byte order */
2614 struct timeval timestamp;
2616 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2617 pgh->seq_num = htonl(pkt_dev->seq_num);
2619 do_gettimeofday(×tamp);
2620 pgh->tv_sec = htonl(timestamp.tv_sec);
2621 pgh->tv_usec = htonl(timestamp.tv_usec);
2625 if (!process_ipsec(pkt_dev, skb, protocol))
2633 * scan_ip6, fmt_ip taken from dietlibc-0.21
2634 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2636 * Slightly modified for kernel.
2637 * Should be candidate for net/ipv4/utils.c
2641 static unsigned int scan_ip6(const char *s, char ip[16])
2644 unsigned int len = 0;
2647 unsigned int prefixlen = 0;
2648 unsigned int suffixlen = 0;
2652 for (i = 0; i < 16; i++)
2658 if (s[1] == ':') { /* Found "::", skip to part 2 */
2666 u = simple_strtoul(s, &pos, 16);
2670 if (prefixlen == 12 && s[i] == '.') {
2672 /* the last 4 bytes may be written as IPv4 address */
2675 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2678 ip[prefixlen++] = (u >> 8);
2679 ip[prefixlen++] = (u & 255);
2682 if (prefixlen == 16)
2686 /* part 2, after "::" */
2693 } else if (suffixlen != 0)
2696 u = simple_strtol(s, &pos, 16);
2703 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2705 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2711 suffix[suffixlen++] = (u >> 8);
2712 suffix[suffixlen++] = (u & 255);
2715 if (prefixlen + suffixlen == 16)
2718 for (i = 0; i < suffixlen; i++)
2719 ip[16 - suffixlen + i] = suffix[i];
2723 static char tohex(char hexdigit)
2725 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2728 static int fmt_xlong(char *s, unsigned int i)
2731 *s = tohex((i >> 12) & 0xf);
2732 if (s != bak || *s != '0')
2734 *s = tohex((i >> 8) & 0xf);
2735 if (s != bak || *s != '0')
2737 *s = tohex((i >> 4) & 0xf);
2738 if (s != bak || *s != '0')
2740 *s = tohex(i & 0xf);
2744 static unsigned int fmt_ip6(char *s, const char ip[16])
2749 unsigned int compressing;
2754 for (j = 0; j < 16; j += 2) {
2756 #ifdef V4MAPPEDPREFIX
2757 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2758 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2763 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2764 (unsigned long)(unsigned char)ip[j + 1];
2779 i = fmt_xlong(s, temp);
2796 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2797 struct pktgen_dev *pkt_dev)
2799 struct sk_buff *skb = NULL;
2801 struct udphdr *udph;
2803 struct ipv6hdr *iph;
2804 struct pktgen_hdr *pgh = NULL;
2805 __be16 protocol = htons(ETH_P_IPV6);
2807 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2808 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2809 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2810 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2813 if (pkt_dev->nr_labels)
2814 protocol = htons(ETH_P_MPLS_UC);
2816 if (pkt_dev->vlan_id != 0xffff)
2817 protocol = htons(ETH_P_8021Q);
2819 /* Update any of the values, used when we're incrementing various
2822 queue_map = pkt_dev->cur_queue_map;
2823 mod_cur_headers(pkt_dev);
2825 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2826 pkt_dev->pkt_overhead, GFP_ATOMIC);
2828 sprintf(pkt_dev->result, "No memory");
2832 skb_reserve(skb, 16);
2834 /* Reserve for ethernet and IP header */
2835 eth = (__u8 *) skb_push(skb, 14);
2836 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2837 if (pkt_dev->nr_labels)
2838 mpls_push(mpls, pkt_dev);
2840 if (pkt_dev->vlan_id != 0xffff) {
2841 if (pkt_dev->svlan_id != 0xffff) {
2842 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2843 *svlan_tci = build_tci(pkt_dev->svlan_id,
2846 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2847 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2849 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2850 *vlan_tci = build_tci(pkt_dev->vlan_id,
2853 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2854 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2857 skb->network_header = skb->tail;
2858 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2859 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2860 skb_set_queue_mapping(skb, queue_map);
2861 iph = ipv6_hdr(skb);
2862 udph = udp_hdr(skb);
2864 memcpy(eth, pkt_dev->hh, 12);
2865 *(__be16 *) & eth[12] = protocol;
2867 /* Eth + IPh + UDPh + mpls */
2868 datalen = pkt_dev->cur_pkt_size - 14 -
2869 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2870 pkt_dev->pkt_overhead;
2872 if (datalen < sizeof(struct pktgen_hdr)) {
2873 datalen = sizeof(struct pktgen_hdr);
2874 if (net_ratelimit())
2875 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2879 udph->source = htons(pkt_dev->cur_udp_src);
2880 udph->dest = htons(pkt_dev->cur_udp_dst);
2881 udph->len = htons(datalen + sizeof(struct udphdr));
2882 udph->check = 0; /* No checksum */
2884 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2886 if (pkt_dev->traffic_class) {
2887 /* Version + traffic class + flow (0) */
2888 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2891 iph->hop_limit = 32;
2893 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2894 iph->nexthdr = IPPROTO_UDP;
2896 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2897 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2899 skb->mac_header = (skb->network_header - ETH_HLEN -
2900 pkt_dev->pkt_overhead);
2901 skb->protocol = protocol;
2903 skb->pkt_type = PACKET_HOST;
2905 if (pkt_dev->nfrags <= 0)
2906 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2908 int frags = pkt_dev->nfrags;
2911 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2913 if (frags > MAX_SKB_FRAGS)
2914 frags = MAX_SKB_FRAGS;
2915 if (datalen > frags * PAGE_SIZE) {
2916 skb_put(skb, datalen - frags * PAGE_SIZE);
2917 datalen = frags * PAGE_SIZE;
2921 while (datalen > 0) {
2922 struct page *page = alloc_pages(GFP_KERNEL, 0);
2923 skb_shinfo(skb)->frags[i].page = page;
2924 skb_shinfo(skb)->frags[i].page_offset = 0;
2925 skb_shinfo(skb)->frags[i].size =
2926 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2927 datalen -= skb_shinfo(skb)->frags[i].size;
2928 skb->len += skb_shinfo(skb)->frags[i].size;
2929 skb->data_len += skb_shinfo(skb)->frags[i].size;
2931 skb_shinfo(skb)->nr_frags = i;
2940 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2944 skb_shinfo(skb)->frags[i - 1].size -= rem;
2946 skb_shinfo(skb)->frags[i] =
2947 skb_shinfo(skb)->frags[i - 1];
2948 get_page(skb_shinfo(skb)->frags[i].page);
2949 skb_shinfo(skb)->frags[i].page =
2950 skb_shinfo(skb)->frags[i - 1].page;
2951 skb_shinfo(skb)->frags[i].page_offset +=
2952 skb_shinfo(skb)->frags[i - 1].size;
2953 skb_shinfo(skb)->frags[i].size = rem;
2955 skb_shinfo(skb)->nr_frags = i;
2959 /* Stamp the time, and sequence number, convert them to network byte order */
2960 /* should we update cloned packets too ? */
2962 struct timeval timestamp;
2964 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2965 pgh->seq_num = htonl(pkt_dev->seq_num);
2967 do_gettimeofday(×tamp);
2968 pgh->tv_sec = htonl(timestamp.tv_sec);
2969 pgh->tv_usec = htonl(timestamp.tv_usec);
2971 /* pkt_dev->seq_num++; FF: you really mean this? */
2976 static inline struct sk_buff *fill_packet(struct net_device *odev,
2977 struct pktgen_dev *pkt_dev)
2979 if (pkt_dev->flags & F_IPV6)
2980 return fill_packet_ipv6(odev, pkt_dev);
2982 return fill_packet_ipv4(odev, pkt_dev);
2985 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2987 pkt_dev->seq_num = 1;
2988 pkt_dev->idle_acc = 0;
2990 pkt_dev->tx_bytes = 0;
2991 pkt_dev->errors = 0;
2994 /* Set up structure for sending pkts, clear counters */
2996 static void pktgen_run(struct pktgen_thread *t)
2998 struct pktgen_dev *pkt_dev;
3001 pr_debug("pktgen: entering pktgen_run. %p\n", t);
3004 list_for_each_entry(pkt_dev, &t->if_list, list) {
3007 * setup odev and create initial packet.
3009 pktgen_setup_inject(pkt_dev);
3011 if (pkt_dev->odev) {
3012 pktgen_clear_counters(pkt_dev);
3013 pkt_dev->running = 1; /* Cranke yeself! */
3014 pkt_dev->skb = NULL;
3015 pkt_dev->started_at = getCurUs();
3016 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
3017 pkt_dev->next_tx_ns = 0;
3018 set_pkt_overhead(pkt_dev);
3020 strcpy(pkt_dev->result, "Starting");
3023 strcpy(pkt_dev->result, "Error starting");
3027 t->control &= ~(T_STOP);
3030 static void pktgen_stop_all_threads_ifs(void)
3032 struct pktgen_thread *t;
3034 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3036 mutex_lock(&pktgen_thread_lock);
3038 list_for_each_entry(t, &pktgen_threads, th_list)
3039 t->control |= T_STOP;
3041 mutex_unlock(&pktgen_thread_lock);
3044 static int thread_is_running(struct pktgen_thread *t)
3046 struct pktgen_dev *pkt_dev;
3049 list_for_each_entry(pkt_dev, &t->if_list, list)
3050 if (pkt_dev->running) {
3057 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3061 while (thread_is_running(t)) {
3065 msleep_interruptible(100);
3067 if (signal_pending(current))
3077 static int pktgen_wait_all_threads_run(void)
3079 struct pktgen_thread *t;
3082 mutex_lock(&pktgen_thread_lock);
3084 list_for_each_entry(t, &pktgen_threads, th_list) {
3085 sig = pktgen_wait_thread_run(t);
3091 list_for_each_entry(t, &pktgen_threads, th_list)
3092 t->control |= (T_STOP);
3094 mutex_unlock(&pktgen_thread_lock);
3098 static void pktgen_run_all_threads(void)
3100 struct pktgen_thread *t;
3102 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3104 mutex_lock(&pktgen_thread_lock);
3106 list_for_each_entry(t, &pktgen_threads, th_list)
3107 t->control |= (T_RUN);
3109 mutex_unlock(&pktgen_thread_lock);
3111 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3113 pktgen_wait_all_threads_run();
3116 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3118 __u64 total_us, bps, mbps, pps, idle;
3119 char *p = pkt_dev->result;
3121 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3123 idle = pkt_dev->idle_acc;
3125 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3126 (unsigned long long)total_us,
3127 (unsigned long long)(total_us - idle),
3128 (unsigned long long)idle,
3129 (unsigned long long)pkt_dev->sofar,
3130 pkt_dev->cur_pkt_size, nr_frags);
3132 pps = pkt_dev->sofar * USEC_PER_SEC;
3134 while ((total_us >> 32) != 0) {
3139 do_div(pps, total_us);
3141 bps = pps * 8 * pkt_dev->cur_pkt_size;
3144 do_div(mbps, 1000000);
3145 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3146 (unsigned long long)pps,
3147 (unsigned long long)mbps,
3148 (unsigned long long)bps,
3149 (unsigned long long)pkt_dev->errors);
3152 /* Set stopped-at timer, remove from running list, do counters & statistics */
3154 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3156 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3158 if (!pkt_dev->running) {
3159 printk(KERN_WARNING "pktgen: interface: %s is already "
3160 "stopped\n", pkt_dev->odev->name);
3164 pkt_dev->stopped_at = getCurUs();
3165 pkt_dev->running = 0;
3167 show_results(pkt_dev, nr_frags);
3172 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3174 struct pktgen_dev *pkt_dev, *best = NULL;
3178 list_for_each_entry(pkt_dev, &t->if_list, list) {
3179 if (!pkt_dev->running)
3183 else if (pkt_dev->next_tx_us < best->next_tx_us)
3190 static void pktgen_stop(struct pktgen_thread *t)
3192 struct pktgen_dev *pkt_dev;
3194 pr_debug("pktgen: entering pktgen_stop\n");
3198 list_for_each_entry(pkt_dev, &t->if_list, list) {
3199 pktgen_stop_device(pkt_dev);
3201 kfree_skb(pkt_dev->skb);
3203 pkt_dev->skb = NULL;
3210 * one of our devices needs to be removed - find it
3213 static void pktgen_rem_one_if(struct pktgen_thread *t)
3215 struct list_head *q, *n;
3216 struct pktgen_dev *cur;
3218 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3222 list_for_each_safe(q, n, &t->if_list) {
3223 cur = list_entry(q, struct pktgen_dev, list);
3225 if (!cur->removal_mark)
3229 kfree_skb(cur->skb);
3232 pktgen_remove_device(t, cur);
3240 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3242 struct list_head *q, *n;
3243 struct pktgen_dev *cur;
3245 /* Remove all devices, free mem */
3247 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3250 list_for_each_safe(q, n, &t->if_list) {
3251 cur = list_entry(q, struct pktgen_dev, list);
3254 kfree_skb(cur->skb);
3257 pktgen_remove_device(t, cur);
3263 static void pktgen_rem_thread(struct pktgen_thread *t)
3265 /* Remove from the thread list */
3267 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3269 mutex_lock(&pktgen_thread_lock);
3271 list_del(&t->th_list);
3273 mutex_unlock(&pktgen_thread_lock);
3276 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3278 struct net_device *odev = NULL;
3279 struct netdev_queue *txq;
3280 __u64 idle_start = 0;
3284 odev = pkt_dev->odev;
3286 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3290 if (now < pkt_dev->next_tx_us)
3291 spin(pkt_dev, pkt_dev->next_tx_us);
3293 /* This is max DELAY, this has special meaning of
3296 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3297 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3298 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3303 if (!pkt_dev->skb) {
3304 set_cur_queue_map(pkt_dev);
3305 queue_map = pkt_dev->cur_queue_map;
3307 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3310 txq = netdev_get_tx_queue(odev, queue_map);
3311 if (netif_tx_queue_stopped(txq) ||
3312 netif_tx_queue_frozen(txq) ||
3314 idle_start = getCurUs();
3316 if (!netif_running(odev)) {
3317 pktgen_stop_device(pkt_dev);
3319 kfree_skb(pkt_dev->skb);
3320 pkt_dev->skb = NULL;
3326 pkt_dev->idle_acc += getCurUs() - idle_start;
3328 if (netif_tx_queue_stopped(txq) ||
3329 netif_tx_queue_frozen(txq)) {
3330 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3331 pkt_dev->next_tx_ns = 0;
3332 goto out; /* Try the next interface */
3336 if (pkt_dev->last_ok || !pkt_dev->skb) {
3337 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3338 || (!pkt_dev->skb)) {
3339 /* build a new pkt */
3341 kfree_skb(pkt_dev->skb);
3343 pkt_dev->skb = fill_packet(odev, pkt_dev);
3344 if (pkt_dev->skb == NULL) {
3345 printk(KERN_ERR "pktgen: ERROR: couldn't "
3346 "allocate skb in fill_packet.\n");
3348 pkt_dev->clone_count--; /* back out increment, OOM */
3351 pkt_dev->allocated_skbs++;
3352 pkt_dev->clone_count = 0; /* reset counter */
3356 /* fill_packet() might have changed the queue */
3357 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3358 txq = netdev_get_tx_queue(odev, queue_map);
3360 __netif_tx_lock_bh(txq);
3361 if (!netif_tx_queue_stopped(txq) &&
3362 !netif_tx_queue_frozen(txq)) {
3364 atomic_inc(&(pkt_dev->skb->users));
3366 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3367 if (likely(ret == NETDEV_TX_OK)) {
3368 pkt_dev->last_ok = 1;
3371 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3373 } else if (ret == NETDEV_TX_LOCKED
3374 && (odev->features & NETIF_F_LLTX)) {
3377 } else { /* Retry it next time */
3379 atomic_dec(&(pkt_dev->skb->users));
3381 if (debug && net_ratelimit())
3382 printk(KERN_INFO "pktgen: Hard xmit error\n");
3385 pkt_dev->last_ok = 0;
3388 pkt_dev->next_tx_us = getCurUs();
3389 pkt_dev->next_tx_ns = 0;
3391 pkt_dev->next_tx_us += pkt_dev->delay_us;
3392 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3394 if (pkt_dev->next_tx_ns > 1000) {
3395 pkt_dev->next_tx_us++;
3396 pkt_dev->next_tx_ns -= 1000;
3400 else { /* Retry it next time */
3401 pkt_dev->last_ok = 0;
3402 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3403 pkt_dev->next_tx_ns = 0;
3406 __netif_tx_unlock_bh(txq);
3408 /* If pkt_dev->count is zero, then run forever */
3409 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3410 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3411 idle_start = getCurUs();
3412 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3413 if (signal_pending(current)) {
3418 pkt_dev->idle_acc += getCurUs() - idle_start;
3421 /* Done with this */
3422 pktgen_stop_device(pkt_dev);
3424 kfree_skb(pkt_dev->skb);
3425 pkt_dev->skb = NULL;
3431 * Main loop of the thread goes here
3434 static int pktgen_thread_worker(void *arg)
3437 struct pktgen_thread *t = arg;
3438 struct pktgen_dev *pkt_dev = NULL;
3441 BUG_ON(smp_processor_id() != cpu);
3443 init_waitqueue_head(&t->queue);
3444 complete(&t->start_done);
3446 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3448 set_current_state(TASK_INTERRUPTIBLE);
3452 while (!kthread_should_stop()) {
3453 pkt_dev = next_to_run(t);
3456 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3458 prepare_to_wait(&(t->queue), &wait,
3459 TASK_INTERRUPTIBLE);
3460 schedule_timeout(HZ / 10);
3461 finish_wait(&(t->queue), &wait);
3464 __set_current_state(TASK_RUNNING);
3467 pktgen_xmit(pkt_dev);
3469 if (t->control & T_STOP) {
3471 t->control &= ~(T_STOP);
3474 if (t->control & T_RUN) {
3476 t->control &= ~(T_RUN);
3479 if (t->control & T_REMDEVALL) {
3480 pktgen_rem_all_ifs(t);
3481 t->control &= ~(T_REMDEVALL);
3484 if (t->control & T_REMDEV) {
3485 pktgen_rem_one_if(t);
3486 t->control &= ~(T_REMDEV);
3491 set_current_state(TASK_INTERRUPTIBLE);
3494 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3497 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3498 pktgen_rem_all_ifs(t);
3500 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3501 pktgen_rem_thread(t);
3506 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3509 struct pktgen_dev *p, *pkt_dev = NULL;
3512 list_for_each_entry(p, &t->if_list, list)
3513 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3519 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3524 * Adds a dev at front of if_list.
3527 static int add_dev_to_thread(struct pktgen_thread *t,
3528 struct pktgen_dev *pkt_dev)
3534 if (pkt_dev->pg_thread) {
3535 printk(KERN_ERR "pktgen: ERROR: already assigned "
3541 list_add(&pkt_dev->list, &t->if_list);
3542 pkt_dev->pg_thread = t;
3543 pkt_dev->running = 0;
3550 /* Called under thread lock */
3552 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3554 struct pktgen_dev *pkt_dev;
3557 /* We don't allow a device to be on several threads */
3559 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3561 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3565 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3569 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3570 if (pkt_dev->flows == NULL) {
3574 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3576 pkt_dev->removal_mark = 0;
3577 pkt_dev->min_pkt_size = ETH_ZLEN;
3578 pkt_dev->max_pkt_size = ETH_ZLEN;
3579 pkt_dev->nfrags = 0;
3580 pkt_dev->clone_skb = pg_clone_skb_d;
3581 pkt_dev->delay_us = pg_delay_d / 1000;
3582 pkt_dev->delay_ns = pg_delay_d % 1000;
3583 pkt_dev->count = pg_count_d;
3585 pkt_dev->udp_src_min = 9; /* sink port */
3586 pkt_dev->udp_src_max = 9;
3587 pkt_dev->udp_dst_min = 9;
3588 pkt_dev->udp_dst_max = 9;
3590 pkt_dev->vlan_p = 0;
3591 pkt_dev->vlan_cfi = 0;
3592 pkt_dev->vlan_id = 0xffff;
3593 pkt_dev->svlan_p = 0;
3594 pkt_dev->svlan_cfi = 0;
3595 pkt_dev->svlan_id = 0xffff;
3597 err = pktgen_setup_dev(pkt_dev, ifname);
3601 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3602 &pktgen_if_fops, pkt_dev);
3603 if (!pkt_dev->entry) {
3604 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3605 PG_PROC_DIR, ifname);
3610 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3611 pkt_dev->ipsproto = IPPROTO_ESP;
3614 return add_dev_to_thread(t, pkt_dev);
3616 dev_put(pkt_dev->odev);
3622 vfree(pkt_dev->flows);
3627 static int __init pktgen_create_thread(int cpu)
3629 struct pktgen_thread *t;
3630 struct proc_dir_entry *pe;
3631 struct task_struct *p;
3633 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3635 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3636 "create new thread.\n");
3640 spin_lock_init(&t->if_lock);
3643 INIT_LIST_HEAD(&t->if_list);
3645 list_add_tail(&t->th_list, &pktgen_threads);
3646 init_completion(&t->start_done);
3648 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3650 printk(KERN_ERR "pktgen: kernel_thread() failed "
3651 "for cpu %d\n", t->cpu);
3652 list_del(&t->th_list);
3656 kthread_bind(p, cpu);
3659 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3660 &pktgen_thread_fops, t);
3662 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3663 PG_PROC_DIR, t->tsk->comm);
3665 list_del(&t->th_list);
3671 wait_for_completion(&t->start_done);
3677 * Removes a device from the thread if_list.
3679 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3680 struct pktgen_dev *pkt_dev)
3682 struct list_head *q, *n;
3683 struct pktgen_dev *p;
3685 list_for_each_safe(q, n, &t->if_list) {
3686 p = list_entry(q, struct pktgen_dev, list);
3692 static int pktgen_remove_device(struct pktgen_thread *t,
3693 struct pktgen_dev *pkt_dev)
3696 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3698 if (pkt_dev->running) {
3699 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3700 "running interface, stopping it now.\n");
3701 pktgen_stop_device(pkt_dev);
3704 /* Dis-associate from the interface */
3706 if (pkt_dev->odev) {
3707 dev_put(pkt_dev->odev);
3708 pkt_dev->odev = NULL;
3711 /* And update the thread if_list */
3713 _rem_dev_from_if_list(t, pkt_dev);
3716 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3722 vfree(pkt_dev->flows);
3727 static int __init pg_init(void)
3730 struct proc_dir_entry *pe;
3732 printk(KERN_INFO "%s", version);
3734 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3737 pg_proc_dir->owner = THIS_MODULE;
3739 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3741 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3742 "procfs entry.\n", PGCTRL);
3743 proc_net_remove(&init_net, PG_PROC_DIR);
3747 /* Register us to receive netdevice events */
3748 register_netdevice_notifier(&pktgen_notifier_block);
3750 for_each_online_cpu(cpu) {
3753 err = pktgen_create_thread(cpu);
3755 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3756 "thread for cpu %d (%d)\n", cpu, err);
3759 if (list_empty(&pktgen_threads)) {
3760 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3762 unregister_netdevice_notifier(&pktgen_notifier_block);
3763 remove_proc_entry(PGCTRL, pg_proc_dir);
3764 proc_net_remove(&init_net, PG_PROC_DIR);
3771 static void __exit pg_cleanup(void)
3773 struct pktgen_thread *t;
3774 struct list_head *q, *n;
3775 wait_queue_head_t queue;
3776 init_waitqueue_head(&queue);
3778 /* Stop all interfaces & threads */
3780 list_for_each_safe(q, n, &pktgen_threads) {
3781 t = list_entry(q, struct pktgen_thread, th_list);
3782 kthread_stop(t->tsk);
3786 /* Un-register us from receiving netdevice events */
3787 unregister_netdevice_notifier(&pktgen_notifier_block);
3789 /* Clean up proc file system */
3790 remove_proc_entry(PGCTRL, pg_proc_dir);
3791 proc_net_remove(&init_net, PG_PROC_DIR);
3794 module_init(pg_init);
3795 module_exit(pg_cleanup);
3797 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3798 MODULE_DESCRIPTION("Packet Generator tool");
3799 MODULE_LICENSE("GPL");
3800 module_param(pg_count_d, int, 0);
3801 module_param(pg_delay_d, int, 0);
3802 module_param(pg_clone_skb_d, int, 0);
3803 module_param(debug, int, 0);