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/hrtimer.h>
135 #include <linux/freezer.h>
136 #include <linux/delay.h>
137 #include <linux/timer.h>
138 #include <linux/list.h>
139 #include <linux/init.h>
140 #include <linux/skbuff.h>
141 #include <linux/netdevice.h>
142 #include <linux/inet.h>
143 #include <linux/inetdevice.h>
144 #include <linux/rtnetlink.h>
145 #include <linux/if_arp.h>
146 #include <linux/if_vlan.h>
147 #include <linux/in.h>
148 #include <linux/ip.h>
149 #include <linux/ipv6.h>
150 #include <linux/udp.h>
151 #include <linux/proc_fs.h>
152 #include <linux/seq_file.h>
153 #include <linux/wait.h>
154 #include <linux/etherdevice.h>
155 #include <linux/kthread.h>
156 #include <net/net_namespace.h>
157 #include <net/checksum.h>
158 #include <net/ipv6.h>
159 #include <net/addrconf.h>
161 #include <net/xfrm.h>
163 #include <asm/byteorder.h>
164 #include <linux/rcupdate.h>
165 #include <linux/bitops.h>
166 #include <linux/io.h>
167 #include <linux/timex.h>
168 #include <linux/uaccess.h>
170 #include <asm/div64.h> /* do_div */
172 #define VERSION "2.74"
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 */
192 #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
193 #define F_NODE (1<<15) /* Node memory alloc*/
195 /* Thread control flag bits */
196 #define T_STOP (1<<0) /* Stop run */
197 #define T_RUN (1<<1) /* Start run */
198 #define T_REMDEVALL (1<<2) /* Remove all devs */
199 #define T_REMDEV (1<<3) /* Remove one dev */
201 /* If lock -- can be removed after some work */
202 #define if_lock(t) spin_lock(&(t->if_lock));
203 #define if_unlock(t) spin_unlock(&(t->if_lock));
205 /* Used to help with determining the pkts on receive */
206 #define PKTGEN_MAGIC 0xbe9be955
207 #define PG_PROC_DIR "pktgen"
208 #define PGCTRL "pgctrl"
209 static struct proc_dir_entry *pg_proc_dir;
211 #define MAX_CFLOWS 65536
213 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
214 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
220 struct xfrm_state *x;
226 #define F_INIT (1<<0) /* flow has been initialized */
230 * Try to keep frequent/infrequent used vars. separated.
232 struct proc_dir_entry *entry; /* proc file */
233 struct pktgen_thread *pg_thread;/* the owner */
234 struct list_head list; /* chaining in the thread's run-queue */
236 int running; /* if false, the test will stop */
238 /* If min != max, then we will either do a linear iteration, or
239 * we will do a random selection from within the range.
242 int removal_mark; /* non-zero => the device is marked for
243 * removal by worker thread */
245 int min_pkt_size; /* = ETH_ZLEN; */
246 int max_pkt_size; /* = ETH_ZLEN; */
247 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
249 u64 delay; /* nano-seconds */
251 __u64 count; /* Default No packets to send */
252 __u64 sofar; /* How many pkts we've sent so far */
253 __u64 tx_bytes; /* How many bytes we've transmitted */
254 __u64 errors; /* Errors when trying to transmit, */
256 /* runtime counters relating to clone_skb */
258 __u64 allocated_skbs;
260 int last_ok; /* Was last skb sent?
261 * Or a failed transmit of some sort?
262 * This will keep sequence numbers in order
267 u64 idle_acc; /* nano-seconds */
272 * Use multiple SKBs during packet gen.
273 * If this number is greater than 1, then
274 * that many copies of the same packet will be
275 * sent before a new packet is allocated.
276 * If you want to send 1024 identical packets
277 * before creating a new packet,
278 * set clone_skb to 1024.
281 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
283 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
284 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
286 struct in6_addr in6_saddr;
287 struct in6_addr in6_daddr;
288 struct in6_addr cur_in6_daddr;
289 struct in6_addr cur_in6_saddr;
291 struct in6_addr min_in6_daddr;
292 struct in6_addr max_in6_daddr;
293 struct in6_addr min_in6_saddr;
294 struct in6_addr max_in6_saddr;
296 /* If we're doing ranges, random or incremental, then this
297 * defines the min/max for those ranges.
299 __be32 saddr_min; /* inclusive, source IP address */
300 __be32 saddr_max; /* exclusive, source IP address */
301 __be32 daddr_min; /* inclusive, dest IP address */
302 __be32 daddr_max; /* exclusive, dest IP address */
304 __u16 udp_src_min; /* inclusive, source UDP port */
305 __u16 udp_src_max; /* exclusive, source UDP port */
306 __u16 udp_dst_min; /* inclusive, dest UDP port */
307 __u16 udp_dst_max; /* exclusive, dest UDP port */
310 __u8 tos; /* six MSB of (former) IPv4 TOS
311 are for dscp codepoint */
312 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
313 (see RFC 3260, sec. 4) */
316 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
317 __be32 labels[MAX_MPLS_LABELS];
319 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
322 __u16 vlan_id; /* 0xffff means no vlan tag */
326 __u16 svlan_id; /* 0xffff means no svlan tag */
328 __u32 src_mac_count; /* How many MACs to iterate through */
329 __u32 dst_mac_count; /* How many MACs to iterate through */
331 unsigned char dst_mac[ETH_ALEN];
332 unsigned char src_mac[ETH_ALEN];
334 __u32 cur_dst_mac_offset;
335 __u32 cur_src_mac_offset;
347 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
349 We fill in SRC address later
350 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
354 __u16 pad; /* pad out the hh struct to an even 16 bytes */
356 struct sk_buff *skb; /* skb we are to transmit next, used for when we
357 * are transmitting the same one multiple times
359 struct net_device *odev; /* The out-going device.
360 * Note that the device should have it's
361 * pg_info pointer pointing back to this
363 * Set when the user specifies the out-going
364 * device name (not when the inject is
365 * started as it used to do.)
368 struct flow_state *flows;
369 unsigned cflows; /* Concurrent flows (config) */
370 unsigned lflow; /* Flow length (config) */
371 unsigned nflows; /* accumulated flows (stats) */
372 unsigned curfl; /* current sequenced flow (state)*/
376 int node; /* Memory node */
379 __u8 ipsmode; /* IPSEC mode (config) */
380 __u8 ipsproto; /* IPSEC type (config) */
392 struct pktgen_thread {
393 spinlock_t if_lock; /* for list of devices */
394 struct list_head if_list; /* All device here */
395 struct list_head th_list;
396 struct task_struct *tsk;
399 /* Field for thread to receive "posted" events terminate,
405 wait_queue_head_t queue;
406 struct completion start_done;
412 static inline ktime_t ktime_now(void)
417 return timespec_to_ktime(ts);
420 /* This works even if 32 bit because of careful byte order choice */
421 static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
423 return cmp1.tv64 < cmp2.tv64;
426 static const char version[] =
427 "pktgen " VERSION ": Packet Generator for packet performance testing.\n";
429 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
430 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
431 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
432 const char *ifname, bool exact);
433 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
434 static void pktgen_run_all_threads(void);
435 static void pktgen_reset_all_threads(void);
436 static void pktgen_stop_all_threads_ifs(void);
438 static void pktgen_stop(struct pktgen_thread *t);
439 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
441 static unsigned int scan_ip6(const char *s, char ip[16]);
442 static unsigned int fmt_ip6(char *s, const char ip[16]);
444 /* Module parameters, defaults. */
445 static int pg_count_d __read_mostly = 1000;
446 static int pg_delay_d __read_mostly;
447 static int pg_clone_skb_d __read_mostly;
448 static int debug __read_mostly;
450 static DEFINE_MUTEX(pktgen_thread_lock);
451 static LIST_HEAD(pktgen_threads);
453 static struct notifier_block pktgen_notifier_block = {
454 .notifier_call = pktgen_device_event,
458 * /proc handling functions
462 static int pgctrl_show(struct seq_file *seq, void *v)
464 seq_puts(seq, version);
468 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
469 size_t count, loff_t *ppos)
474 if (!capable(CAP_NET_ADMIN)) {
479 if (count > sizeof(data))
480 count = sizeof(data);
482 if (copy_from_user(data, buf, count)) {
486 data[count - 1] = 0; /* Make string */
488 if (!strcmp(data, "stop"))
489 pktgen_stop_all_threads_ifs();
491 else if (!strcmp(data, "start"))
492 pktgen_run_all_threads();
494 else if (!strcmp(data, "reset"))
495 pktgen_reset_all_threads();
498 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
506 static int pgctrl_open(struct inode *inode, struct file *file)
508 return single_open(file, pgctrl_show, PDE(inode)->data);
511 static const struct file_operations pktgen_fops = {
512 .owner = THIS_MODULE,
516 .write = pgctrl_write,
517 .release = single_release,
520 static int pktgen_if_show(struct seq_file *seq, void *v)
522 const struct pktgen_dev *pkt_dev = seq->private;
527 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
528 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
529 pkt_dev->max_pkt_size);
532 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
533 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
534 pkt_dev->clone_skb, pkt_dev->odevname);
536 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
540 " queue_map_min: %u queue_map_max: %u\n",
541 pkt_dev->queue_map_min,
542 pkt_dev->queue_map_max);
544 if (pkt_dev->flags & F_IPV6) {
545 char b1[128], b2[128], b3[128];
546 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
547 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
548 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
550 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
553 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
554 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
555 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
557 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
562 " dst_min: %s dst_max: %s\n",
563 pkt_dev->dst_min, pkt_dev->dst_max);
565 " src_min: %s src_max: %s\n",
566 pkt_dev->src_min, pkt_dev->src_max);
569 seq_puts(seq, " src_mac: ");
571 seq_printf(seq, "%pM ",
572 is_zero_ether_addr(pkt_dev->src_mac) ?
573 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
575 seq_printf(seq, "dst_mac: ");
576 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
579 " udp_src_min: %d udp_src_max: %d"
580 " udp_dst_min: %d udp_dst_max: %d\n",
581 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
582 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
585 " src_mac_count: %d dst_mac_count: %d\n",
586 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
588 if (pkt_dev->nr_labels) {
590 seq_printf(seq, " mpls: ");
591 for (i = 0; i < pkt_dev->nr_labels; i++)
592 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
593 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
596 if (pkt_dev->vlan_id != 0xffff)
597 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
598 pkt_dev->vlan_id, pkt_dev->vlan_p,
601 if (pkt_dev->svlan_id != 0xffff)
602 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
603 pkt_dev->svlan_id, pkt_dev->svlan_p,
607 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
609 if (pkt_dev->traffic_class)
610 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
612 if (pkt_dev->node >= 0)
613 seq_printf(seq, " node: %d\n", pkt_dev->node);
615 seq_printf(seq, " Flags: ");
617 if (pkt_dev->flags & F_IPV6)
618 seq_printf(seq, "IPV6 ");
620 if (pkt_dev->flags & F_IPSRC_RND)
621 seq_printf(seq, "IPSRC_RND ");
623 if (pkt_dev->flags & F_IPDST_RND)
624 seq_printf(seq, "IPDST_RND ");
626 if (pkt_dev->flags & F_TXSIZE_RND)
627 seq_printf(seq, "TXSIZE_RND ");
629 if (pkt_dev->flags & F_UDPSRC_RND)
630 seq_printf(seq, "UDPSRC_RND ");
632 if (pkt_dev->flags & F_UDPDST_RND)
633 seq_printf(seq, "UDPDST_RND ");
635 if (pkt_dev->flags & F_MPLS_RND)
636 seq_printf(seq, "MPLS_RND ");
638 if (pkt_dev->flags & F_QUEUE_MAP_RND)
639 seq_printf(seq, "QUEUE_MAP_RND ");
641 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
642 seq_printf(seq, "QUEUE_MAP_CPU ");
644 if (pkt_dev->cflows) {
645 if (pkt_dev->flags & F_FLOW_SEQ)
646 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
648 seq_printf(seq, "FLOW_RND ");
652 if (pkt_dev->flags & F_IPSEC_ON)
653 seq_printf(seq, "IPSEC ");
656 if (pkt_dev->flags & F_MACSRC_RND)
657 seq_printf(seq, "MACSRC_RND ");
659 if (pkt_dev->flags & F_MACDST_RND)
660 seq_printf(seq, "MACDST_RND ");
662 if (pkt_dev->flags & F_VID_RND)
663 seq_printf(seq, "VID_RND ");
665 if (pkt_dev->flags & F_SVID_RND)
666 seq_printf(seq, "SVID_RND ");
668 if (pkt_dev->flags & F_NODE)
669 seq_printf(seq, "NODE_ALLOC ");
673 /* not really stopped, more like last-running-at */
674 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
675 idle = pkt_dev->idle_acc;
676 do_div(idle, NSEC_PER_USEC);
679 "Current:\n pkts-sofar: %llu errors: %llu\n",
680 (unsigned long long)pkt_dev->sofar,
681 (unsigned long long)pkt_dev->errors);
684 " started: %lluus stopped: %lluus idle: %lluus\n",
685 (unsigned long long) ktime_to_us(pkt_dev->started_at),
686 (unsigned long long) ktime_to_us(stopped),
687 (unsigned long long) idle);
690 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
691 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
692 pkt_dev->cur_src_mac_offset);
694 if (pkt_dev->flags & F_IPV6) {
695 char b1[128], b2[128];
696 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
697 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
698 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
700 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
701 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
703 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
704 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
706 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
708 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
710 if (pkt_dev->result[0])
711 seq_printf(seq, "Result: %s\n", pkt_dev->result);
713 seq_printf(seq, "Result: Idle\n");
719 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
725 for (; i < maxlen; i++) {
728 if (get_user(c, &user_buffer[i]))
730 if ((c >= '0') && (c <= '9'))
732 else if ((c >= 'a') && (c <= 'f'))
733 *num |= c - 'a' + 10;
734 else if ((c >= 'A') && (c <= 'F'))
735 *num |= c - 'A' + 10;
742 static int count_trail_chars(const char __user * user_buffer,
747 for (i = 0; i < maxlen; i++) {
749 if (get_user(c, &user_buffer[i]))
767 static unsigned long num_arg(const char __user * user_buffer,
768 unsigned long maxlen, unsigned long *num)
773 for (; i < maxlen; i++) {
775 if (get_user(c, &user_buffer[i]))
777 if ((c >= '0') && (c <= '9')) {
786 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
790 for (; i < maxlen; i++) {
792 if (get_user(c, &user_buffer[i]))
810 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
817 pkt_dev->nr_labels = 0;
820 len = hex32_arg(&buffer[i], 8, &tmp);
823 pkt_dev->labels[n] = htonl(tmp);
824 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
825 pkt_dev->flags |= F_MPLS_RND;
827 if (get_user(c, &buffer[i]))
831 if (n >= MAX_MPLS_LABELS)
835 pkt_dev->nr_labels = n;
839 static ssize_t pktgen_if_write(struct file *file,
840 const char __user * user_buffer, size_t count,
843 struct seq_file *seq = (struct seq_file *)file->private_data;
844 struct pktgen_dev *pkt_dev = seq->private;
846 char name[16], valstr[32];
847 unsigned long value = 0;
848 char *pg_result = NULL;
852 pg_result = &(pkt_dev->result[0]);
855 printk(KERN_WARNING "pktgen: wrong command format\n");
860 tmp = count_trail_chars(&user_buffer[i], max);
862 printk(KERN_WARNING "pktgen: illegal format\n");
867 /* Read variable name */
869 len = strn_len(&user_buffer[i], sizeof(name) - 1);
873 memset(name, 0, sizeof(name));
874 if (copy_from_user(name, &user_buffer[i], len))
879 len = count_trail_chars(&user_buffer[i], max);
887 if (copy_from_user(tb, user_buffer, count))
890 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
891 (unsigned long)count, tb);
894 if (!strcmp(name, "min_pkt_size")) {
895 len = num_arg(&user_buffer[i], 10, &value);
900 if (value < 14 + 20 + 8)
902 if (value != pkt_dev->min_pkt_size) {
903 pkt_dev->min_pkt_size = value;
904 pkt_dev->cur_pkt_size = value;
906 sprintf(pg_result, "OK: min_pkt_size=%u",
907 pkt_dev->min_pkt_size);
911 if (!strcmp(name, "max_pkt_size")) {
912 len = num_arg(&user_buffer[i], 10, &value);
917 if (value < 14 + 20 + 8)
919 if (value != pkt_dev->max_pkt_size) {
920 pkt_dev->max_pkt_size = value;
921 pkt_dev->cur_pkt_size = value;
923 sprintf(pg_result, "OK: max_pkt_size=%u",
924 pkt_dev->max_pkt_size);
928 /* Shortcut for min = max */
930 if (!strcmp(name, "pkt_size")) {
931 len = num_arg(&user_buffer[i], 10, &value);
936 if (value < 14 + 20 + 8)
938 if (value != pkt_dev->min_pkt_size) {
939 pkt_dev->min_pkt_size = value;
940 pkt_dev->max_pkt_size = value;
941 pkt_dev->cur_pkt_size = value;
943 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
947 if (!strcmp(name, "debug")) {
948 len = num_arg(&user_buffer[i], 10, &value);
954 sprintf(pg_result, "OK: debug=%u", debug);
958 if (!strcmp(name, "frags")) {
959 len = num_arg(&user_buffer[i], 10, &value);
964 pkt_dev->nfrags = value;
965 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
968 if (!strcmp(name, "delay")) {
969 len = num_arg(&user_buffer[i], 10, &value);
974 if (value == 0x7FFFFFFF)
975 pkt_dev->delay = ULLONG_MAX;
977 pkt_dev->delay = (u64)value;
979 sprintf(pg_result, "OK: delay=%llu",
980 (unsigned long long) pkt_dev->delay);
983 if (!strcmp(name, "rate")) {
984 len = num_arg(&user_buffer[i], 10, &value);
991 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
994 "pktgen: Delay set at: %llu ns\n",
997 sprintf(pg_result, "OK: rate=%lu", value);
1000 if (!strcmp(name, "ratep")) {
1001 len = num_arg(&user_buffer[i], 10, &value);
1008 pkt_dev->delay = NSEC_PER_SEC/value;
1011 "pktgen: Delay set at: %llu ns\n",
1014 sprintf(pg_result, "OK: rate=%lu", value);
1017 if (!strcmp(name, "udp_src_min")) {
1018 len = num_arg(&user_buffer[i], 10, &value);
1023 if (value != pkt_dev->udp_src_min) {
1024 pkt_dev->udp_src_min = value;
1025 pkt_dev->cur_udp_src = value;
1027 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1030 if (!strcmp(name, "udp_dst_min")) {
1031 len = num_arg(&user_buffer[i], 10, &value);
1036 if (value != pkt_dev->udp_dst_min) {
1037 pkt_dev->udp_dst_min = value;
1038 pkt_dev->cur_udp_dst = value;
1040 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1043 if (!strcmp(name, "udp_src_max")) {
1044 len = num_arg(&user_buffer[i], 10, &value);
1049 if (value != pkt_dev->udp_src_max) {
1050 pkt_dev->udp_src_max = value;
1051 pkt_dev->cur_udp_src = value;
1053 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1056 if (!strcmp(name, "udp_dst_max")) {
1057 len = num_arg(&user_buffer[i], 10, &value);
1062 if (value != pkt_dev->udp_dst_max) {
1063 pkt_dev->udp_dst_max = value;
1064 pkt_dev->cur_udp_dst = value;
1066 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1069 if (!strcmp(name, "clone_skb")) {
1070 len = num_arg(&user_buffer[i], 10, &value);
1075 pkt_dev->clone_skb = value;
1077 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1080 if (!strcmp(name, "count")) {
1081 len = num_arg(&user_buffer[i], 10, &value);
1086 pkt_dev->count = value;
1087 sprintf(pg_result, "OK: count=%llu",
1088 (unsigned long long)pkt_dev->count);
1091 if (!strcmp(name, "src_mac_count")) {
1092 len = num_arg(&user_buffer[i], 10, &value);
1097 if (pkt_dev->src_mac_count != value) {
1098 pkt_dev->src_mac_count = value;
1099 pkt_dev->cur_src_mac_offset = 0;
1101 sprintf(pg_result, "OK: src_mac_count=%d",
1102 pkt_dev->src_mac_count);
1105 if (!strcmp(name, "dst_mac_count")) {
1106 len = num_arg(&user_buffer[i], 10, &value);
1111 if (pkt_dev->dst_mac_count != value) {
1112 pkt_dev->dst_mac_count = value;
1113 pkt_dev->cur_dst_mac_offset = 0;
1115 sprintf(pg_result, "OK: dst_mac_count=%d",
1116 pkt_dev->dst_mac_count);
1119 if (!strcmp(name, "node")) {
1120 len = num_arg(&user_buffer[i], 10, &value);
1126 if (node_possible(value)) {
1127 pkt_dev->node = value;
1128 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1131 sprintf(pg_result, "ERROR: node not possible");
1134 if (!strcmp(name, "flag")) {
1137 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1141 if (copy_from_user(f, &user_buffer[i], len))
1144 if (strcmp(f, "IPSRC_RND") == 0)
1145 pkt_dev->flags |= F_IPSRC_RND;
1147 else if (strcmp(f, "!IPSRC_RND") == 0)
1148 pkt_dev->flags &= ~F_IPSRC_RND;
1150 else if (strcmp(f, "TXSIZE_RND") == 0)
1151 pkt_dev->flags |= F_TXSIZE_RND;
1153 else if (strcmp(f, "!TXSIZE_RND") == 0)
1154 pkt_dev->flags &= ~F_TXSIZE_RND;
1156 else if (strcmp(f, "IPDST_RND") == 0)
1157 pkt_dev->flags |= F_IPDST_RND;
1159 else if (strcmp(f, "!IPDST_RND") == 0)
1160 pkt_dev->flags &= ~F_IPDST_RND;
1162 else if (strcmp(f, "UDPSRC_RND") == 0)
1163 pkt_dev->flags |= F_UDPSRC_RND;
1165 else if (strcmp(f, "!UDPSRC_RND") == 0)
1166 pkt_dev->flags &= ~F_UDPSRC_RND;
1168 else if (strcmp(f, "UDPDST_RND") == 0)
1169 pkt_dev->flags |= F_UDPDST_RND;
1171 else if (strcmp(f, "!UDPDST_RND") == 0)
1172 pkt_dev->flags &= ~F_UDPDST_RND;
1174 else if (strcmp(f, "MACSRC_RND") == 0)
1175 pkt_dev->flags |= F_MACSRC_RND;
1177 else if (strcmp(f, "!MACSRC_RND") == 0)
1178 pkt_dev->flags &= ~F_MACSRC_RND;
1180 else if (strcmp(f, "MACDST_RND") == 0)
1181 pkt_dev->flags |= F_MACDST_RND;
1183 else if (strcmp(f, "!MACDST_RND") == 0)
1184 pkt_dev->flags &= ~F_MACDST_RND;
1186 else if (strcmp(f, "MPLS_RND") == 0)
1187 pkt_dev->flags |= F_MPLS_RND;
1189 else if (strcmp(f, "!MPLS_RND") == 0)
1190 pkt_dev->flags &= ~F_MPLS_RND;
1192 else if (strcmp(f, "VID_RND") == 0)
1193 pkt_dev->flags |= F_VID_RND;
1195 else if (strcmp(f, "!VID_RND") == 0)
1196 pkt_dev->flags &= ~F_VID_RND;
1198 else if (strcmp(f, "SVID_RND") == 0)
1199 pkt_dev->flags |= F_SVID_RND;
1201 else if (strcmp(f, "!SVID_RND") == 0)
1202 pkt_dev->flags &= ~F_SVID_RND;
1204 else if (strcmp(f, "FLOW_SEQ") == 0)
1205 pkt_dev->flags |= F_FLOW_SEQ;
1207 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1208 pkt_dev->flags |= F_QUEUE_MAP_RND;
1210 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1211 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1213 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1214 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1216 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1217 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1219 else if (strcmp(f, "IPSEC") == 0)
1220 pkt_dev->flags |= F_IPSEC_ON;
1223 else if (strcmp(f, "!IPV6") == 0)
1224 pkt_dev->flags &= ~F_IPV6;
1226 else if (strcmp(f, "NODE_ALLOC") == 0)
1227 pkt_dev->flags |= F_NODE;
1229 else if (strcmp(f, "!NODE_ALLOC") == 0)
1230 pkt_dev->flags &= ~F_NODE;
1234 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1236 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1237 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
1240 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1243 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1244 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1248 if (copy_from_user(buf, &user_buffer[i], len))
1251 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1252 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1253 strncpy(pkt_dev->dst_min, buf, len);
1254 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1255 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1258 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1261 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1264 if (!strcmp(name, "dst_max")) {
1265 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1270 if (copy_from_user(buf, &user_buffer[i], len))
1274 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1275 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1276 strncpy(pkt_dev->dst_max, buf, len);
1277 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1278 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1281 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1284 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1287 if (!strcmp(name, "dst6")) {
1288 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1292 pkt_dev->flags |= F_IPV6;
1294 if (copy_from_user(buf, &user_buffer[i], len))
1298 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1299 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1301 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1304 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1307 sprintf(pg_result, "OK: dst6=%s", buf);
1310 if (!strcmp(name, "dst6_min")) {
1311 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1315 pkt_dev->flags |= F_IPV6;
1317 if (copy_from_user(buf, &user_buffer[i], len))
1321 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1322 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1324 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1325 &pkt_dev->min_in6_daddr);
1327 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1330 sprintf(pg_result, "OK: dst6_min=%s", buf);
1333 if (!strcmp(name, "dst6_max")) {
1334 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1338 pkt_dev->flags |= F_IPV6;
1340 if (copy_from_user(buf, &user_buffer[i], len))
1344 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1345 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1348 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1351 sprintf(pg_result, "OK: dst6_max=%s", buf);
1354 if (!strcmp(name, "src6")) {
1355 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1359 pkt_dev->flags |= F_IPV6;
1361 if (copy_from_user(buf, &user_buffer[i], len))
1365 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1366 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1368 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1371 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1374 sprintf(pg_result, "OK: src6=%s", buf);
1377 if (!strcmp(name, "src_min")) {
1378 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1382 if (copy_from_user(buf, &user_buffer[i], len))
1385 if (strcmp(buf, pkt_dev->src_min) != 0) {
1386 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1387 strncpy(pkt_dev->src_min, buf, len);
1388 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1389 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1392 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1395 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1398 if (!strcmp(name, "src_max")) {
1399 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1403 if (copy_from_user(buf, &user_buffer[i], len))
1406 if (strcmp(buf, pkt_dev->src_max) != 0) {
1407 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1408 strncpy(pkt_dev->src_max, buf, len);
1409 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1410 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1413 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1416 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1419 if (!strcmp(name, "dst_mac")) {
1421 unsigned char old_dmac[ETH_ALEN];
1422 unsigned char *m = pkt_dev->dst_mac;
1423 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1425 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1429 memset(valstr, 0, sizeof(valstr));
1430 if (copy_from_user(valstr, &user_buffer[i], len))
1434 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1435 if (*v >= '0' && *v <= '9') {
1439 if (*v >= 'A' && *v <= 'F') {
1441 *m += *v - 'A' + 10;
1443 if (*v >= 'a' && *v <= 'f') {
1445 *m += *v - 'a' + 10;
1453 /* Set up Dest MAC */
1454 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1455 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1457 sprintf(pg_result, "OK: dstmac");
1460 if (!strcmp(name, "src_mac")) {
1462 unsigned char old_smac[ETH_ALEN];
1463 unsigned char *m = pkt_dev->src_mac;
1465 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1467 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1471 memset(valstr, 0, sizeof(valstr));
1472 if (copy_from_user(valstr, &user_buffer[i], len))
1476 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1477 if (*v >= '0' && *v <= '9') {
1481 if (*v >= 'A' && *v <= 'F') {
1483 *m += *v - 'A' + 10;
1485 if (*v >= 'a' && *v <= 'f') {
1487 *m += *v - 'a' + 10;
1495 /* Set up Src MAC */
1496 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1497 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1499 sprintf(pg_result, "OK: srcmac");
1503 if (!strcmp(name, "clear_counters")) {
1504 pktgen_clear_counters(pkt_dev);
1505 sprintf(pg_result, "OK: Clearing counters.\n");
1509 if (!strcmp(name, "flows")) {
1510 len = num_arg(&user_buffer[i], 10, &value);
1515 if (value > MAX_CFLOWS)
1518 pkt_dev->cflows = value;
1519 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1523 if (!strcmp(name, "flowlen")) {
1524 len = num_arg(&user_buffer[i], 10, &value);
1529 pkt_dev->lflow = value;
1530 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1534 if (!strcmp(name, "queue_map_min")) {
1535 len = num_arg(&user_buffer[i], 5, &value);
1540 pkt_dev->queue_map_min = value;
1541 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1545 if (!strcmp(name, "queue_map_max")) {
1546 len = num_arg(&user_buffer[i], 5, &value);
1551 pkt_dev->queue_map_max = value;
1552 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1556 if (!strcmp(name, "mpls")) {
1559 len = get_labels(&user_buffer[i], pkt_dev);
1563 cnt = sprintf(pg_result, "OK: mpls=");
1564 for (n = 0; n < pkt_dev->nr_labels; n++)
1565 cnt += sprintf(pg_result + cnt,
1566 "%08x%s", ntohl(pkt_dev->labels[n]),
1567 n == pkt_dev->nr_labels-1 ? "" : ",");
1569 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1570 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1571 pkt_dev->svlan_id = 0xffff;
1574 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1579 if (!strcmp(name, "vlan_id")) {
1580 len = num_arg(&user_buffer[i], 4, &value);
1585 if (value <= 4095) {
1586 pkt_dev->vlan_id = value; /* turn on VLAN */
1589 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1591 if (debug && pkt_dev->nr_labels)
1592 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1594 pkt_dev->nr_labels = 0; /* turn off MPLS */
1595 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1597 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1598 pkt_dev->svlan_id = 0xffff;
1601 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1606 if (!strcmp(name, "vlan_p")) {
1607 len = num_arg(&user_buffer[i], 1, &value);
1612 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1613 pkt_dev->vlan_p = value;
1614 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1616 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1621 if (!strcmp(name, "vlan_cfi")) {
1622 len = num_arg(&user_buffer[i], 1, &value);
1627 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1628 pkt_dev->vlan_cfi = value;
1629 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1631 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1636 if (!strcmp(name, "svlan_id")) {
1637 len = num_arg(&user_buffer[i], 4, &value);
1642 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1643 pkt_dev->svlan_id = value; /* turn on SVLAN */
1646 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1648 if (debug && pkt_dev->nr_labels)
1649 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1651 pkt_dev->nr_labels = 0; /* turn off MPLS */
1652 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1654 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1655 pkt_dev->svlan_id = 0xffff;
1658 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1663 if (!strcmp(name, "svlan_p")) {
1664 len = num_arg(&user_buffer[i], 1, &value);
1669 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1670 pkt_dev->svlan_p = value;
1671 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1673 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1678 if (!strcmp(name, "svlan_cfi")) {
1679 len = num_arg(&user_buffer[i], 1, &value);
1684 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1685 pkt_dev->svlan_cfi = value;
1686 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1688 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1693 if (!strcmp(name, "tos")) {
1694 __u32 tmp_value = 0;
1695 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1701 pkt_dev->tos = tmp_value;
1702 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1704 sprintf(pg_result, "ERROR: tos must be 00-ff");
1709 if (!strcmp(name, "traffic_class")) {
1710 __u32 tmp_value = 0;
1711 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1717 pkt_dev->traffic_class = tmp_value;
1718 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1720 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1725 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1729 static int pktgen_if_open(struct inode *inode, struct file *file)
1731 return single_open(file, pktgen_if_show, PDE(inode)->data);
1734 static const struct file_operations pktgen_if_fops = {
1735 .owner = THIS_MODULE,
1736 .open = pktgen_if_open,
1738 .llseek = seq_lseek,
1739 .write = pktgen_if_write,
1740 .release = single_release,
1743 static int pktgen_thread_show(struct seq_file *seq, void *v)
1745 struct pktgen_thread *t = seq->private;
1746 const struct pktgen_dev *pkt_dev;
1750 seq_printf(seq, "Running: ");
1753 list_for_each_entry(pkt_dev, &t->if_list, list)
1754 if (pkt_dev->running)
1755 seq_printf(seq, "%s ", pkt_dev->odevname);
1757 seq_printf(seq, "\nStopped: ");
1759 list_for_each_entry(pkt_dev, &t->if_list, list)
1760 if (!pkt_dev->running)
1761 seq_printf(seq, "%s ", pkt_dev->odevname);
1764 seq_printf(seq, "\nResult: %s\n", t->result);
1766 seq_printf(seq, "\nResult: NA\n");
1773 static ssize_t pktgen_thread_write(struct file *file,
1774 const char __user * user_buffer,
1775 size_t count, loff_t * offset)
1777 struct seq_file *seq = (struct seq_file *)file->private_data;
1778 struct pktgen_thread *t = seq->private;
1779 int i = 0, max, len, ret;
1784 // sprintf(pg_result, "Wrong command format");
1789 len = count_trail_chars(&user_buffer[i], max);
1795 /* Read variable name */
1797 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1801 memset(name, 0, sizeof(name));
1802 if (copy_from_user(name, &user_buffer[i], len))
1807 len = count_trail_chars(&user_buffer[i], max);
1814 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1815 name, (unsigned long)count);
1818 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1823 pg_result = &(t->result[0]);
1825 if (!strcmp(name, "add_device")) {
1828 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1833 if (copy_from_user(f, &user_buffer[i], len))
1836 mutex_lock(&pktgen_thread_lock);
1837 pktgen_add_device(t, f);
1838 mutex_unlock(&pktgen_thread_lock);
1840 sprintf(pg_result, "OK: add_device=%s", f);
1844 if (!strcmp(name, "rem_device_all")) {
1845 mutex_lock(&pktgen_thread_lock);
1846 t->control |= T_REMDEVALL;
1847 mutex_unlock(&pktgen_thread_lock);
1848 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1850 sprintf(pg_result, "OK: rem_device_all");
1854 if (!strcmp(name, "max_before_softirq")) {
1855 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1865 static int pktgen_thread_open(struct inode *inode, struct file *file)
1867 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1870 static const struct file_operations pktgen_thread_fops = {
1871 .owner = THIS_MODULE,
1872 .open = pktgen_thread_open,
1874 .llseek = seq_lseek,
1875 .write = pktgen_thread_write,
1876 .release = single_release,
1879 /* Think find or remove for NN */
1880 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1882 struct pktgen_thread *t;
1883 struct pktgen_dev *pkt_dev = NULL;
1884 bool exact = (remove == FIND);
1886 list_for_each_entry(t, &pktgen_threads, th_list) {
1887 pkt_dev = pktgen_find_dev(t, ifname, exact);
1891 pkt_dev->removal_mark = 1;
1892 t->control |= T_REMDEV;
1902 * mark a device for removal
1904 static void pktgen_mark_device(const char *ifname)
1906 struct pktgen_dev *pkt_dev = NULL;
1907 const int max_tries = 10, msec_per_try = 125;
1910 mutex_lock(&pktgen_thread_lock);
1911 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1915 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1916 if (pkt_dev == NULL)
1917 break; /* success */
1919 mutex_unlock(&pktgen_thread_lock);
1920 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1921 "to disappear....\n", ifname);
1922 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1923 mutex_lock(&pktgen_thread_lock);
1925 if (++i >= max_tries) {
1926 printk(KERN_ERR "pktgen_mark_device: timed out after "
1927 "waiting %d msec for device %s to be removed\n",
1928 msec_per_try * i, ifname);
1934 mutex_unlock(&pktgen_thread_lock);
1937 static void pktgen_change_name(struct net_device *dev)
1939 struct pktgen_thread *t;
1941 list_for_each_entry(t, &pktgen_threads, th_list) {
1942 struct pktgen_dev *pkt_dev;
1944 list_for_each_entry(pkt_dev, &t->if_list, list) {
1945 if (pkt_dev->odev != dev)
1948 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1950 pkt_dev->entry = proc_create_data(dev->name, 0600,
1954 if (!pkt_dev->entry)
1955 printk(KERN_ERR "pktgen: can't move proc "
1956 " entry for '%s'\n", dev->name);
1962 static int pktgen_device_event(struct notifier_block *unused,
1963 unsigned long event, void *ptr)
1965 struct net_device *dev = ptr;
1967 if (!net_eq(dev_net(dev), &init_net))
1970 /* It is OK that we do not hold the group lock right now,
1971 * as we run under the RTNL lock.
1975 case NETDEV_CHANGENAME:
1976 pktgen_change_name(dev);
1979 case NETDEV_UNREGISTER:
1980 pktgen_mark_device(dev->name);
1987 static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1993 for (i = 0; ifname[i] != '@'; i++) {
2001 return dev_get_by_name(&init_net, b);
2005 /* Associate pktgen_dev with a device. */
2007 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
2009 struct net_device *odev;
2012 /* Clean old setups */
2013 if (pkt_dev->odev) {
2014 dev_put(pkt_dev->odev);
2015 pkt_dev->odev = NULL;
2018 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
2020 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
2024 if (odev->type != ARPHRD_ETHER) {
2025 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
2027 } else if (!netif_running(odev)) {
2028 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
2031 pkt_dev->odev = odev;
2039 /* Read pkt_dev from the interface and set up internal pktgen_dev
2040 * structure to have the right information to create/send packets
2042 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2046 if (!pkt_dev->odev) {
2047 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
2049 sprintf(pkt_dev->result,
2050 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2054 /* make sure that we don't pick a non-existing transmit queue */
2055 ntxq = pkt_dev->odev->real_num_tx_queues;
2057 if (ntxq <= pkt_dev->queue_map_min) {
2058 printk(KERN_WARNING "pktgen: WARNING: Requested "
2059 "queue_map_min (zero-based) (%d) exceeds valid range "
2060 "[0 - %d] for (%d) queues on %s, resetting\n",
2061 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2063 pkt_dev->queue_map_min = ntxq - 1;
2065 if (pkt_dev->queue_map_max >= ntxq) {
2066 printk(KERN_WARNING "pktgen: WARNING: Requested "
2067 "queue_map_max (zero-based) (%d) exceeds valid range "
2068 "[0 - %d] for (%d) queues on %s, resetting\n",
2069 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2071 pkt_dev->queue_map_max = ntxq - 1;
2074 /* Default to the interface's mac if not explicitly set. */
2076 if (is_zero_ether_addr(pkt_dev->src_mac))
2077 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2079 /* Set up Dest MAC */
2080 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2082 /* Set up pkt size */
2083 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2085 if (pkt_dev->flags & F_IPV6) {
2087 * Skip this automatic address setting until locks or functions
2092 int i, set = 0, err = 1;
2093 struct inet6_dev *idev;
2095 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2096 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2104 * Use linklevel address if unconfigured.
2106 * use ipv6_get_lladdr if/when it's get exported
2110 idev = __in6_dev_get(pkt_dev->odev);
2112 struct inet6_ifaddr *ifp;
2114 read_lock_bh(&idev->lock);
2115 for (ifp = idev->addr_list; ifp;
2116 ifp = ifp->if_next) {
2117 if (ifp->scope == IFA_LINK &&
2118 !(ifp->flags & IFA_F_TENTATIVE)) {
2119 ipv6_addr_copy(&pkt_dev->
2126 read_unlock_bh(&idev->lock);
2130 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2131 "address not availble.\n");
2135 pkt_dev->saddr_min = 0;
2136 pkt_dev->saddr_max = 0;
2137 if (strlen(pkt_dev->src_min) == 0) {
2139 struct in_device *in_dev;
2142 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2144 if (in_dev->ifa_list) {
2145 pkt_dev->saddr_min =
2146 in_dev->ifa_list->ifa_address;
2147 pkt_dev->saddr_max = pkt_dev->saddr_min;
2152 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2153 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2156 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2157 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2159 /* Initialize current values. */
2160 pkt_dev->cur_dst_mac_offset = 0;
2161 pkt_dev->cur_src_mac_offset = 0;
2162 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2163 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2164 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2165 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2166 pkt_dev->nflows = 0;
2170 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2172 ktime_t start_time, end_time;
2174 struct hrtimer_sleeper t;
2176 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2177 hrtimer_set_expires(&t.timer, spin_until);
2179 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2180 if (remaining <= 0) {
2181 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2185 start_time = ktime_now();
2186 if (remaining < 100000)
2187 ndelay(remaining); /* really small just spin */
2189 /* see do_nanosleep */
2190 hrtimer_init_sleeper(&t, current);
2192 set_current_state(TASK_INTERRUPTIBLE);
2193 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2194 if (!hrtimer_active(&t.timer))
2200 hrtimer_cancel(&t.timer);
2201 } while (t.task && pkt_dev->running && !signal_pending(current));
2202 __set_current_state(TASK_RUNNING);
2204 end_time = ktime_now();
2206 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2207 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2210 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2212 pkt_dev->pkt_overhead = 0;
2213 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2214 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2215 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2218 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2220 return !!(pkt_dev->flows[flow].flags & F_INIT);
2223 static inline int f_pick(struct pktgen_dev *pkt_dev)
2225 int flow = pkt_dev->curfl;
2227 if (pkt_dev->flags & F_FLOW_SEQ) {
2228 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2230 pkt_dev->flows[flow].count = 0;
2231 pkt_dev->flows[flow].flags = 0;
2232 pkt_dev->curfl += 1;
2233 if (pkt_dev->curfl >= pkt_dev->cflows)
2234 pkt_dev->curfl = 0; /*reset */
2237 flow = random32() % pkt_dev->cflows;
2238 pkt_dev->curfl = flow;
2240 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2241 pkt_dev->flows[flow].count = 0;
2242 pkt_dev->flows[flow].flags = 0;
2246 return pkt_dev->curfl;
2251 /* If there was already an IPSEC SA, we keep it as is, else
2252 * we go look for it ...
2254 #define DUMMY_MARK 0
2255 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2257 struct xfrm_state *x = pkt_dev->flows[flow].x;
2259 /*slow path: we dont already have xfrm_state*/
2260 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
2261 (xfrm_address_t *)&pkt_dev->cur_daddr,
2262 (xfrm_address_t *)&pkt_dev->cur_saddr,
2265 pkt_dev->ipsproto, 0);
2267 pkt_dev->flows[flow].x = x;
2268 set_pkt_overhead(pkt_dev);
2269 pkt_dev->pkt_overhead += x->props.header_len;
2275 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2278 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2279 pkt_dev->cur_queue_map = smp_processor_id();
2281 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2283 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2285 (pkt_dev->queue_map_max -
2286 pkt_dev->queue_map_min + 1)
2287 + pkt_dev->queue_map_min;
2289 t = pkt_dev->cur_queue_map + 1;
2290 if (t > pkt_dev->queue_map_max)
2291 t = pkt_dev->queue_map_min;
2293 pkt_dev->cur_queue_map = t;
2295 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2298 /* Increment/randomize headers according to flags and current values
2299 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2301 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2307 if (pkt_dev->cflows)
2308 flow = f_pick(pkt_dev);
2310 /* Deal with source MAC */
2311 if (pkt_dev->src_mac_count > 1) {
2315 if (pkt_dev->flags & F_MACSRC_RND)
2316 mc = random32() % pkt_dev->src_mac_count;
2318 mc = pkt_dev->cur_src_mac_offset++;
2319 if (pkt_dev->cur_src_mac_offset >=
2320 pkt_dev->src_mac_count)
2321 pkt_dev->cur_src_mac_offset = 0;
2324 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2325 pkt_dev->hh[11] = tmp;
2326 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2327 pkt_dev->hh[10] = tmp;
2328 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2329 pkt_dev->hh[9] = tmp;
2330 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2331 pkt_dev->hh[8] = tmp;
2332 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2333 pkt_dev->hh[7] = tmp;
2336 /* Deal with Destination MAC */
2337 if (pkt_dev->dst_mac_count > 1) {
2341 if (pkt_dev->flags & F_MACDST_RND)
2342 mc = random32() % pkt_dev->dst_mac_count;
2345 mc = pkt_dev->cur_dst_mac_offset++;
2346 if (pkt_dev->cur_dst_mac_offset >=
2347 pkt_dev->dst_mac_count) {
2348 pkt_dev->cur_dst_mac_offset = 0;
2352 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2353 pkt_dev->hh[5] = tmp;
2354 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2355 pkt_dev->hh[4] = tmp;
2356 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2357 pkt_dev->hh[3] = tmp;
2358 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2359 pkt_dev->hh[2] = tmp;
2360 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2361 pkt_dev->hh[1] = tmp;
2364 if (pkt_dev->flags & F_MPLS_RND) {
2366 for (i = 0; i < pkt_dev->nr_labels; i++)
2367 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2368 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2369 ((__force __be32)random32() &
2373 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2374 pkt_dev->vlan_id = random32() & (4096-1);
2377 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2378 pkt_dev->svlan_id = random32() & (4096 - 1);
2381 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2382 if (pkt_dev->flags & F_UDPSRC_RND)
2383 pkt_dev->cur_udp_src = random32() %
2384 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2385 + pkt_dev->udp_src_min;
2388 pkt_dev->cur_udp_src++;
2389 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2390 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2394 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2395 if (pkt_dev->flags & F_UDPDST_RND) {
2396 pkt_dev->cur_udp_dst = random32() %
2397 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2398 + pkt_dev->udp_dst_min;
2400 pkt_dev->cur_udp_dst++;
2401 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2402 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2406 if (!(pkt_dev->flags & F_IPV6)) {
2408 imn = ntohl(pkt_dev->saddr_min);
2409 imx = ntohl(pkt_dev->saddr_max);
2412 if (pkt_dev->flags & F_IPSRC_RND)
2413 t = random32() % (imx - imn) + imn;
2415 t = ntohl(pkt_dev->cur_saddr);
2421 pkt_dev->cur_saddr = htonl(t);
2424 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2425 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2427 imn = ntohl(pkt_dev->daddr_min);
2428 imx = ntohl(pkt_dev->daddr_max);
2432 if (pkt_dev->flags & F_IPDST_RND) {
2434 t = random32() % (imx - imn) + imn;
2437 while (ipv4_is_loopback(s) ||
2438 ipv4_is_multicast(s) ||
2439 ipv4_is_lbcast(s) ||
2440 ipv4_is_zeronet(s) ||
2441 ipv4_is_local_multicast(s)) {
2442 t = random32() % (imx - imn) + imn;
2445 pkt_dev->cur_daddr = s;
2447 t = ntohl(pkt_dev->cur_daddr);
2452 pkt_dev->cur_daddr = htonl(t);
2455 if (pkt_dev->cflows) {
2456 pkt_dev->flows[flow].flags |= F_INIT;
2457 pkt_dev->flows[flow].cur_daddr =
2460 if (pkt_dev->flags & F_IPSEC_ON)
2461 get_ipsec_sa(pkt_dev, flow);
2466 } else { /* IPV6 * */
2468 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2469 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2470 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2471 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2475 /* Only random destinations yet */
2477 for (i = 0; i < 4; i++) {
2478 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2479 (((__force __be32)random32() |
2480 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2481 pkt_dev->max_in6_daddr.s6_addr32[i]);
2486 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2488 if (pkt_dev->flags & F_TXSIZE_RND) {
2490 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2491 + pkt_dev->min_pkt_size;
2493 t = pkt_dev->cur_pkt_size + 1;
2494 if (t > pkt_dev->max_pkt_size)
2495 t = pkt_dev->min_pkt_size;
2497 pkt_dev->cur_pkt_size = t;
2500 set_cur_queue_map(pkt_dev);
2502 pkt_dev->flows[flow].count++;
2507 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2509 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2515 /* XXX: we dont support tunnel mode for now until
2516 * we resolve the dst issue */
2517 if (x->props.mode != XFRM_MODE_TRANSPORT)
2520 spin_lock(&x->lock);
2523 err = x->outer_mode->output(x, skb);
2526 err = x->type->output(x, skb);
2530 x->curlft.bytes += skb->len;
2531 x->curlft.packets++;
2533 spin_unlock(&x->lock);
2537 static void free_SAs(struct pktgen_dev *pkt_dev)
2539 if (pkt_dev->cflows) {
2540 /* let go of the SAs if we have them */
2542 for (; i < pkt_dev->cflows; i++) {
2543 struct xfrm_state *x = pkt_dev->flows[i].x;
2546 pkt_dev->flows[i].x = NULL;
2552 static int process_ipsec(struct pktgen_dev *pkt_dev,
2553 struct sk_buff *skb, __be16 protocol)
2555 if (pkt_dev->flags & F_IPSEC_ON) {
2556 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2561 nhead = x->props.header_len - skb_headroom(skb);
2563 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2565 printk(KERN_ERR "Error expanding "
2566 "ipsec packet %d\n", ret);
2571 /* ipsec is not expecting ll header */
2572 skb_pull(skb, ETH_HLEN);
2573 ret = pktgen_output_ipsec(skb, pkt_dev);
2575 printk(KERN_ERR "Error creating ipsec "
2576 "packet %d\n", ret);
2580 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2581 memcpy(eth, pkt_dev->hh, 12);
2582 *(u16 *) ð[12] = protocol;
2592 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2595 for (i = 0; i < pkt_dev->nr_labels; i++)
2596 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2599 *mpls |= MPLS_STACK_BOTTOM;
2602 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2605 return htons(id | (cfi << 12) | (prio << 13));
2608 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2609 struct pktgen_dev *pkt_dev)
2611 struct sk_buff *skb = NULL;
2613 struct udphdr *udph;
2616 struct pktgen_hdr *pgh = NULL;
2617 __be16 protocol = htons(ETH_P_IP);
2619 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2620 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2621 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2622 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2625 if (pkt_dev->nr_labels)
2626 protocol = htons(ETH_P_MPLS_UC);
2628 if (pkt_dev->vlan_id != 0xffff)
2629 protocol = htons(ETH_P_8021Q);
2631 /* Update any of the values, used when we're incrementing various
2634 queue_map = pkt_dev->cur_queue_map;
2635 mod_cur_headers(pkt_dev);
2637 datalen = (odev->hard_header_len + 16) & ~0xf;
2639 if (pkt_dev->flags & F_NODE) {
2642 if (pkt_dev->node >= 0)
2643 node = pkt_dev->node;
2645 node = numa_node_id();
2647 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2648 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2650 skb_reserve(skb, NET_SKB_PAD);
2655 skb = __netdev_alloc_skb(odev,
2656 pkt_dev->cur_pkt_size + 64
2657 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2660 sprintf(pkt_dev->result, "No memory");
2664 skb_reserve(skb, datalen);
2666 /* Reserve for ethernet and IP header */
2667 eth = (__u8 *) skb_push(skb, 14);
2668 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2669 if (pkt_dev->nr_labels)
2670 mpls_push(mpls, pkt_dev);
2672 if (pkt_dev->vlan_id != 0xffff) {
2673 if (pkt_dev->svlan_id != 0xffff) {
2674 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2675 *svlan_tci = build_tci(pkt_dev->svlan_id,
2678 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2679 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2681 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2682 *vlan_tci = build_tci(pkt_dev->vlan_id,
2685 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2686 *vlan_encapsulated_proto = htons(ETH_P_IP);
2689 skb->network_header = skb->tail;
2690 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2691 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2692 skb_set_queue_mapping(skb, queue_map);
2694 udph = udp_hdr(skb);
2696 memcpy(eth, pkt_dev->hh, 12);
2697 *(__be16 *) & eth[12] = protocol;
2699 /* Eth + IPh + UDPh + mpls */
2700 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2701 pkt_dev->pkt_overhead;
2702 if (datalen < sizeof(struct pktgen_hdr))
2703 datalen = sizeof(struct pktgen_hdr);
2705 udph->source = htons(pkt_dev->cur_udp_src);
2706 udph->dest = htons(pkt_dev->cur_udp_dst);
2707 udph->len = htons(datalen + 8); /* DATA + udphdr */
2708 udph->check = 0; /* No checksum */
2713 iph->tos = pkt_dev->tos;
2714 iph->protocol = IPPROTO_UDP; /* UDP */
2715 iph->saddr = pkt_dev->cur_saddr;
2716 iph->daddr = pkt_dev->cur_daddr;
2717 iph->id = htons(pkt_dev->ip_id);
2720 iplen = 20 + 8 + datalen;
2721 iph->tot_len = htons(iplen);
2723 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2724 skb->protocol = protocol;
2725 skb->mac_header = (skb->network_header - ETH_HLEN -
2726 pkt_dev->pkt_overhead);
2728 skb->pkt_type = PACKET_HOST;
2730 if (pkt_dev->nfrags <= 0) {
2731 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2732 memset(pgh + 1, 0, datalen - sizeof(struct pktgen_hdr));
2734 int frags = pkt_dev->nfrags;
2737 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2739 if (frags > MAX_SKB_FRAGS)
2740 frags = MAX_SKB_FRAGS;
2741 if (datalen > frags * PAGE_SIZE) {
2742 len = datalen - frags * PAGE_SIZE;
2743 memset(skb_put(skb, len), 0, len);
2744 datalen = frags * PAGE_SIZE;
2748 while (datalen > 0) {
2749 struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
2750 skb_shinfo(skb)->frags[i].page = page;
2751 skb_shinfo(skb)->frags[i].page_offset = 0;
2752 skb_shinfo(skb)->frags[i].size =
2753 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2754 datalen -= skb_shinfo(skb)->frags[i].size;
2755 skb->len += skb_shinfo(skb)->frags[i].size;
2756 skb->data_len += skb_shinfo(skb)->frags[i].size;
2758 skb_shinfo(skb)->nr_frags = i;
2767 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2771 skb_shinfo(skb)->frags[i - 1].size -= rem;
2773 skb_shinfo(skb)->frags[i] =
2774 skb_shinfo(skb)->frags[i - 1];
2775 get_page(skb_shinfo(skb)->frags[i].page);
2776 skb_shinfo(skb)->frags[i].page =
2777 skb_shinfo(skb)->frags[i - 1].page;
2778 skb_shinfo(skb)->frags[i].page_offset +=
2779 skb_shinfo(skb)->frags[i - 1].size;
2780 skb_shinfo(skb)->frags[i].size = rem;
2782 skb_shinfo(skb)->nr_frags = i;
2786 /* Stamp the time, and sequence number,
2787 * convert them to network byte order
2790 struct timeval timestamp;
2792 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2793 pgh->seq_num = htonl(pkt_dev->seq_num);
2795 do_gettimeofday(×tamp);
2796 pgh->tv_sec = htonl(timestamp.tv_sec);
2797 pgh->tv_usec = htonl(timestamp.tv_usec);
2801 if (!process_ipsec(pkt_dev, skb, protocol))
2809 * scan_ip6, fmt_ip taken from dietlibc-0.21
2810 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2812 * Slightly modified for kernel.
2813 * Should be candidate for net/ipv4/utils.c
2817 static unsigned int scan_ip6(const char *s, char ip[16])
2820 unsigned int len = 0;
2823 unsigned int prefixlen = 0;
2824 unsigned int suffixlen = 0;
2828 for (i = 0; i < 16; i++)
2834 if (s[1] == ':') { /* Found "::", skip to part 2 */
2842 u = simple_strtoul(s, &pos, 16);
2846 if (prefixlen == 12 && s[i] == '.') {
2848 /* the last 4 bytes may be written as IPv4 address */
2851 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2854 ip[prefixlen++] = (u >> 8);
2855 ip[prefixlen++] = (u & 255);
2858 if (prefixlen == 16)
2862 /* part 2, after "::" */
2869 } else if (suffixlen != 0)
2872 u = simple_strtol(s, &pos, 16);
2879 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2881 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2887 suffix[suffixlen++] = (u >> 8);
2888 suffix[suffixlen++] = (u & 255);
2891 if (prefixlen + suffixlen == 16)
2894 for (i = 0; i < suffixlen; i++)
2895 ip[16 - suffixlen + i] = suffix[i];
2899 static char tohex(char hexdigit)
2901 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2904 static int fmt_xlong(char *s, unsigned int i)
2907 *s = tohex((i >> 12) & 0xf);
2908 if (s != bak || *s != '0')
2910 *s = tohex((i >> 8) & 0xf);
2911 if (s != bak || *s != '0')
2913 *s = tohex((i >> 4) & 0xf);
2914 if (s != bak || *s != '0')
2916 *s = tohex(i & 0xf);
2920 static unsigned int fmt_ip6(char *s, const char ip[16])
2925 unsigned int compressing;
2930 for (j = 0; j < 16; j += 2) {
2932 #ifdef V4MAPPEDPREFIX
2933 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2934 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2939 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2940 (unsigned long)(unsigned char)ip[j + 1];
2955 i = fmt_xlong(s, temp);
2972 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2973 struct pktgen_dev *pkt_dev)
2975 struct sk_buff *skb = NULL;
2977 struct udphdr *udph;
2979 struct ipv6hdr *iph;
2980 struct pktgen_hdr *pgh = NULL;
2981 __be16 protocol = htons(ETH_P_IPV6);
2983 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2984 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2985 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2986 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2989 if (pkt_dev->nr_labels)
2990 protocol = htons(ETH_P_MPLS_UC);
2992 if (pkt_dev->vlan_id != 0xffff)
2993 protocol = htons(ETH_P_8021Q);
2995 /* Update any of the values, used when we're incrementing various
2998 queue_map = pkt_dev->cur_queue_map;
2999 mod_cur_headers(pkt_dev);
3001 skb = __netdev_alloc_skb(odev,
3002 pkt_dev->cur_pkt_size + 64
3003 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
3005 sprintf(pkt_dev->result, "No memory");
3009 skb_reserve(skb, 16);
3011 /* Reserve for ethernet and IP header */
3012 eth = (__u8 *) skb_push(skb, 14);
3013 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
3014 if (pkt_dev->nr_labels)
3015 mpls_push(mpls, pkt_dev);
3017 if (pkt_dev->vlan_id != 0xffff) {
3018 if (pkt_dev->svlan_id != 0xffff) {
3019 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
3020 *svlan_tci = build_tci(pkt_dev->svlan_id,
3023 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
3024 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
3026 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
3027 *vlan_tci = build_tci(pkt_dev->vlan_id,
3030 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
3031 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
3034 skb->network_header = skb->tail;
3035 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
3036 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
3037 skb_set_queue_mapping(skb, queue_map);
3038 iph = ipv6_hdr(skb);
3039 udph = udp_hdr(skb);
3041 memcpy(eth, pkt_dev->hh, 12);
3042 *(__be16 *) ð[12] = protocol;
3044 /* Eth + IPh + UDPh + mpls */
3045 datalen = pkt_dev->cur_pkt_size - 14 -
3046 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
3047 pkt_dev->pkt_overhead;
3049 if (datalen < sizeof(struct pktgen_hdr)) {
3050 datalen = sizeof(struct pktgen_hdr);
3051 if (net_ratelimit())
3052 printk(KERN_INFO "pktgen: increased datalen to %d\n",
3056 udph->source = htons(pkt_dev->cur_udp_src);
3057 udph->dest = htons(pkt_dev->cur_udp_dst);
3058 udph->len = htons(datalen + sizeof(struct udphdr));
3059 udph->check = 0; /* No checksum */
3061 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
3063 if (pkt_dev->traffic_class) {
3064 /* Version + traffic class + flow (0) */
3065 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
3068 iph->hop_limit = 32;
3070 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3071 iph->nexthdr = IPPROTO_UDP;
3073 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3074 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3076 skb->mac_header = (skb->network_header - ETH_HLEN -
3077 pkt_dev->pkt_overhead);
3078 skb->protocol = protocol;
3080 skb->pkt_type = PACKET_HOST;
3082 if (pkt_dev->nfrags <= 0)
3083 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
3085 int frags = pkt_dev->nfrags;
3088 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
3090 if (frags > MAX_SKB_FRAGS)
3091 frags = MAX_SKB_FRAGS;
3092 if (datalen > frags * PAGE_SIZE) {
3093 skb_put(skb, datalen - frags * PAGE_SIZE);
3094 datalen = frags * PAGE_SIZE;
3098 while (datalen > 0) {
3099 struct page *page = alloc_pages(GFP_KERNEL, 0);
3100 skb_shinfo(skb)->frags[i].page = page;
3101 skb_shinfo(skb)->frags[i].page_offset = 0;
3102 skb_shinfo(skb)->frags[i].size =
3103 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
3104 datalen -= skb_shinfo(skb)->frags[i].size;
3105 skb->len += skb_shinfo(skb)->frags[i].size;
3106 skb->data_len += skb_shinfo(skb)->frags[i].size;
3108 skb_shinfo(skb)->nr_frags = i;
3117 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3121 skb_shinfo(skb)->frags[i - 1].size -= rem;
3123 skb_shinfo(skb)->frags[i] =
3124 skb_shinfo(skb)->frags[i - 1];
3125 get_page(skb_shinfo(skb)->frags[i].page);
3126 skb_shinfo(skb)->frags[i].page =
3127 skb_shinfo(skb)->frags[i - 1].page;
3128 skb_shinfo(skb)->frags[i].page_offset +=
3129 skb_shinfo(skb)->frags[i - 1].size;
3130 skb_shinfo(skb)->frags[i].size = rem;
3132 skb_shinfo(skb)->nr_frags = i;
3136 /* Stamp the time, and sequence number,
3137 * convert them to network byte order
3138 * should we update cloned packets too ?
3141 struct timeval timestamp;
3143 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3144 pgh->seq_num = htonl(pkt_dev->seq_num);
3146 do_gettimeofday(×tamp);
3147 pgh->tv_sec = htonl(timestamp.tv_sec);
3148 pgh->tv_usec = htonl(timestamp.tv_usec);
3150 /* pkt_dev->seq_num++; FF: you really mean this? */
3155 static struct sk_buff *fill_packet(struct net_device *odev,
3156 struct pktgen_dev *pkt_dev)
3158 if (pkt_dev->flags & F_IPV6)
3159 return fill_packet_ipv6(odev, pkt_dev);
3161 return fill_packet_ipv4(odev, pkt_dev);
3164 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3166 pkt_dev->seq_num = 1;
3167 pkt_dev->idle_acc = 0;
3169 pkt_dev->tx_bytes = 0;
3170 pkt_dev->errors = 0;
3173 /* Set up structure for sending pkts, clear counters */
3175 static void pktgen_run(struct pktgen_thread *t)
3177 struct pktgen_dev *pkt_dev;
3180 pr_debug("pktgen: entering pktgen_run. %p\n", t);
3183 list_for_each_entry(pkt_dev, &t->if_list, list) {
3186 * setup odev and create initial packet.
3188 pktgen_setup_inject(pkt_dev);
3190 if (pkt_dev->odev) {
3191 pktgen_clear_counters(pkt_dev);
3192 pkt_dev->running = 1; /* Cranke yeself! */
3193 pkt_dev->skb = NULL;
3194 pkt_dev->started_at =
3195 pkt_dev->next_tx = ktime_now();
3197 set_pkt_overhead(pkt_dev);
3199 strcpy(pkt_dev->result, "Starting");
3202 strcpy(pkt_dev->result, "Error starting");
3206 t->control &= ~(T_STOP);
3209 static void pktgen_stop_all_threads_ifs(void)
3211 struct pktgen_thread *t;
3213 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3215 mutex_lock(&pktgen_thread_lock);
3217 list_for_each_entry(t, &pktgen_threads, th_list)
3218 t->control |= T_STOP;
3220 mutex_unlock(&pktgen_thread_lock);
3223 static int thread_is_running(const struct pktgen_thread *t)
3225 const struct pktgen_dev *pkt_dev;
3227 list_for_each_entry(pkt_dev, &t->if_list, list)
3228 if (pkt_dev->running)
3233 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3237 while (thread_is_running(t)) {
3241 msleep_interruptible(100);
3243 if (signal_pending(current))
3253 static int pktgen_wait_all_threads_run(void)
3255 struct pktgen_thread *t;
3258 mutex_lock(&pktgen_thread_lock);
3260 list_for_each_entry(t, &pktgen_threads, th_list) {
3261 sig = pktgen_wait_thread_run(t);
3267 list_for_each_entry(t, &pktgen_threads, th_list)
3268 t->control |= (T_STOP);
3270 mutex_unlock(&pktgen_thread_lock);
3274 static void pktgen_run_all_threads(void)
3276 struct pktgen_thread *t;
3278 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3280 mutex_lock(&pktgen_thread_lock);
3282 list_for_each_entry(t, &pktgen_threads, th_list)
3283 t->control |= (T_RUN);
3285 mutex_unlock(&pktgen_thread_lock);
3287 /* Propagate thread->control */
3288 schedule_timeout_interruptible(msecs_to_jiffies(125));
3290 pktgen_wait_all_threads_run();
3293 static void pktgen_reset_all_threads(void)
3295 struct pktgen_thread *t;
3297 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3299 mutex_lock(&pktgen_thread_lock);
3301 list_for_each_entry(t, &pktgen_threads, th_list)
3302 t->control |= (T_REMDEVALL);
3304 mutex_unlock(&pktgen_thread_lock);
3306 /* Propagate thread->control */
3307 schedule_timeout_interruptible(msecs_to_jiffies(125));
3309 pktgen_wait_all_threads_run();
3312 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3314 __u64 bps, mbps, pps;
3315 char *p = pkt_dev->result;
3316 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3317 pkt_dev->started_at);
3318 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3320 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3321 (unsigned long long)ktime_to_us(elapsed),
3322 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3323 (unsigned long long)ktime_to_us(idle),
3324 (unsigned long long)pkt_dev->sofar,
3325 pkt_dev->cur_pkt_size, nr_frags);
3327 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3328 ktime_to_ns(elapsed));
3330 bps = pps * 8 * pkt_dev->cur_pkt_size;
3333 do_div(mbps, 1000000);
3334 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3335 (unsigned long long)pps,
3336 (unsigned long long)mbps,
3337 (unsigned long long)bps,
3338 (unsigned long long)pkt_dev->errors);
3341 /* Set stopped-at timer, remove from running list, do counters & statistics */
3342 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3344 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3346 if (!pkt_dev->running) {
3347 printk(KERN_WARNING "pktgen: interface: %s is already "
3348 "stopped\n", pkt_dev->odevname);
3352 kfree_skb(pkt_dev->skb);
3353 pkt_dev->skb = NULL;
3354 pkt_dev->stopped_at = ktime_now();
3355 pkt_dev->running = 0;
3357 show_results(pkt_dev, nr_frags);
3362 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3364 struct pktgen_dev *pkt_dev, *best = NULL;
3368 list_for_each_entry(pkt_dev, &t->if_list, list) {
3369 if (!pkt_dev->running)
3373 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
3380 static void pktgen_stop(struct pktgen_thread *t)
3382 struct pktgen_dev *pkt_dev;
3384 pr_debug("pktgen: entering pktgen_stop\n");
3388 list_for_each_entry(pkt_dev, &t->if_list, list) {
3389 pktgen_stop_device(pkt_dev);
3396 * one of our devices needs to be removed - find it
3399 static void pktgen_rem_one_if(struct pktgen_thread *t)
3401 struct list_head *q, *n;
3402 struct pktgen_dev *cur;
3404 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3408 list_for_each_safe(q, n, &t->if_list) {
3409 cur = list_entry(q, struct pktgen_dev, list);
3411 if (!cur->removal_mark)
3414 kfree_skb(cur->skb);
3417 pktgen_remove_device(t, cur);
3425 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3427 struct list_head *q, *n;
3428 struct pktgen_dev *cur;
3430 /* Remove all devices, free mem */
3432 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3435 list_for_each_safe(q, n, &t->if_list) {
3436 cur = list_entry(q, struct pktgen_dev, list);
3438 kfree_skb(cur->skb);
3441 pktgen_remove_device(t, cur);
3447 static void pktgen_rem_thread(struct pktgen_thread *t)
3449 /* Remove from the thread list */
3451 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3453 mutex_lock(&pktgen_thread_lock);
3455 list_del(&t->th_list);
3457 mutex_unlock(&pktgen_thread_lock);
3460 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3462 ktime_t idle_start = ktime_now();
3464 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3467 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3469 ktime_t idle_start = ktime_now();
3471 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3472 if (signal_pending(current))
3476 pktgen_resched(pkt_dev);
3480 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3483 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3485 struct net_device *odev = pkt_dev->odev;
3486 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
3487 = odev->netdev_ops->ndo_start_xmit;
3488 struct netdev_queue *txq;
3492 /* If device is offline, then don't send */
3493 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3494 pktgen_stop_device(pkt_dev);
3498 /* This is max DELAY, this has special meaning of
3501 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3502 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3506 /* If no skb or clone count exhausted then get new one */
3507 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3508 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3509 /* build a new pkt */
3510 kfree_skb(pkt_dev->skb);
3512 pkt_dev->skb = fill_packet(odev, pkt_dev);
3513 if (pkt_dev->skb == NULL) {
3514 printk(KERN_ERR "pktgen: ERROR: couldn't "
3515 "allocate skb in fill_packet.\n");
3517 pkt_dev->clone_count--; /* back out increment, OOM */
3520 pkt_dev->last_pkt_size = pkt_dev->skb->len;
3521 pkt_dev->allocated_skbs++;
3522 pkt_dev->clone_count = 0; /* reset counter */
3525 if (pkt_dev->delay && pkt_dev->last_ok)
3526 spin(pkt_dev, pkt_dev->next_tx);
3528 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3529 txq = netdev_get_tx_queue(odev, queue_map);
3531 __netif_tx_lock_bh(txq);
3533 if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq))) {
3534 ret = NETDEV_TX_BUSY;
3535 pkt_dev->last_ok = 0;
3538 atomic_inc(&(pkt_dev->skb->users));
3539 ret = (*xmit)(pkt_dev->skb, odev);
3543 txq_trans_update(txq);
3544 pkt_dev->last_ok = 1;
3547 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3551 case NET_XMIT_POLICED:
3552 /* skb has been consumed */
3555 default: /* Drivers are not supposed to return other values! */
3556 if (net_ratelimit())
3557 pr_info("pktgen: %s xmit error: %d\n",
3558 pkt_dev->odevname, ret);
3561 case NETDEV_TX_LOCKED:
3562 case NETDEV_TX_BUSY:
3563 /* Retry it next time */
3564 atomic_dec(&(pkt_dev->skb->users));
3565 pkt_dev->last_ok = 0;
3568 __netif_tx_unlock_bh(txq);
3570 /* If pkt_dev->count is zero, then run forever */
3571 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3572 pktgen_wait_for_skb(pkt_dev);
3574 /* Done with this */
3575 pktgen_stop_device(pkt_dev);
3580 * Main loop of the thread goes here
3583 static int pktgen_thread_worker(void *arg)
3586 struct pktgen_thread *t = arg;
3587 struct pktgen_dev *pkt_dev = NULL;
3590 BUG_ON(smp_processor_id() != cpu);
3592 init_waitqueue_head(&t->queue);
3593 complete(&t->start_done);
3595 pr_debug("pktgen: starting pktgen/%d: pid=%d\n",
3596 cpu, task_pid_nr(current));
3598 set_current_state(TASK_INTERRUPTIBLE);
3602 while (!kthread_should_stop()) {
3603 pkt_dev = next_to_run(t);
3605 if (unlikely(!pkt_dev && t->control == 0)) {
3606 wait_event_interruptible_timeout(t->queue,
3613 __set_current_state(TASK_RUNNING);
3615 if (likely(pkt_dev)) {
3616 pktgen_xmit(pkt_dev);
3619 pktgen_resched(pkt_dev);
3624 if (t->control & T_STOP) {
3626 t->control &= ~(T_STOP);
3629 if (t->control & T_RUN) {
3631 t->control &= ~(T_RUN);
3634 if (t->control & T_REMDEVALL) {
3635 pktgen_rem_all_ifs(t);
3636 t->control &= ~(T_REMDEVALL);
3639 if (t->control & T_REMDEV) {
3640 pktgen_rem_one_if(t);
3641 t->control &= ~(T_REMDEV);
3646 set_current_state(TASK_INTERRUPTIBLE);
3649 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3652 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3653 pktgen_rem_all_ifs(t);
3655 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3656 pktgen_rem_thread(t);
3661 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3662 const char *ifname, bool exact)
3664 struct pktgen_dev *p, *pkt_dev = NULL;
3665 size_t len = strlen(ifname);
3668 list_for_each_entry(p, &t->if_list, list)
3669 if (strncmp(p->odevname, ifname, len) == 0) {
3670 if (p->odevname[len]) {
3671 if (exact || p->odevname[len] != '@')
3679 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3684 * Adds a dev at front of if_list.
3687 static int add_dev_to_thread(struct pktgen_thread *t,
3688 struct pktgen_dev *pkt_dev)
3694 if (pkt_dev->pg_thread) {
3695 printk(KERN_ERR "pktgen: ERROR: already assigned "
3701 list_add(&pkt_dev->list, &t->if_list);
3702 pkt_dev->pg_thread = t;
3703 pkt_dev->running = 0;
3710 /* Called under thread lock */
3712 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3714 struct pktgen_dev *pkt_dev;
3716 int node = cpu_to_node(t->cpu);
3718 /* We don't allow a device to be on several threads */
3720 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3722 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3726 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3730 strcpy(pkt_dev->odevname, ifname);
3731 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3733 if (pkt_dev->flows == NULL) {
3737 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3739 pkt_dev->removal_mark = 0;
3740 pkt_dev->min_pkt_size = ETH_ZLEN;
3741 pkt_dev->max_pkt_size = ETH_ZLEN;
3742 pkt_dev->nfrags = 0;
3743 pkt_dev->clone_skb = pg_clone_skb_d;
3744 pkt_dev->delay = pg_delay_d;
3745 pkt_dev->count = pg_count_d;
3747 pkt_dev->udp_src_min = 9; /* sink port */
3748 pkt_dev->udp_src_max = 9;
3749 pkt_dev->udp_dst_min = 9;
3750 pkt_dev->udp_dst_max = 9;
3752 pkt_dev->vlan_p = 0;
3753 pkt_dev->vlan_cfi = 0;
3754 pkt_dev->vlan_id = 0xffff;
3755 pkt_dev->svlan_p = 0;
3756 pkt_dev->svlan_cfi = 0;
3757 pkt_dev->svlan_id = 0xffff;
3760 err = pktgen_setup_dev(pkt_dev, ifname);
3764 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3765 &pktgen_if_fops, pkt_dev);
3766 if (!pkt_dev->entry) {
3767 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3768 PG_PROC_DIR, ifname);
3773 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3774 pkt_dev->ipsproto = IPPROTO_ESP;
3777 return add_dev_to_thread(t, pkt_dev);
3779 dev_put(pkt_dev->odev);
3784 vfree(pkt_dev->flows);
3789 static int __init pktgen_create_thread(int cpu)
3791 struct pktgen_thread *t;
3792 struct proc_dir_entry *pe;
3793 struct task_struct *p;
3795 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3798 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3799 "create new thread.\n");
3803 spin_lock_init(&t->if_lock);
3806 INIT_LIST_HEAD(&t->if_list);
3808 list_add_tail(&t->th_list, &pktgen_threads);
3809 init_completion(&t->start_done);
3811 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3813 printk(KERN_ERR "pktgen: kernel_thread() failed "
3814 "for cpu %d\n", t->cpu);
3815 list_del(&t->th_list);
3819 kthread_bind(p, cpu);
3822 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3823 &pktgen_thread_fops, t);
3825 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3826 PG_PROC_DIR, t->tsk->comm);
3828 list_del(&t->th_list);
3834 wait_for_completion(&t->start_done);
3840 * Removes a device from the thread if_list.
3842 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3843 struct pktgen_dev *pkt_dev)
3845 struct list_head *q, *n;
3846 struct pktgen_dev *p;
3848 list_for_each_safe(q, n, &t->if_list) {
3849 p = list_entry(q, struct pktgen_dev, list);
3855 static int pktgen_remove_device(struct pktgen_thread *t,
3856 struct pktgen_dev *pkt_dev)
3859 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3861 if (pkt_dev->running) {
3862 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3863 "running interface, stopping it now.\n");
3864 pktgen_stop_device(pkt_dev);
3867 /* Dis-associate from the interface */
3869 if (pkt_dev->odev) {
3870 dev_put(pkt_dev->odev);
3871 pkt_dev->odev = NULL;
3874 /* And update the thread if_list */
3876 _rem_dev_from_if_list(t, pkt_dev);
3879 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3884 vfree(pkt_dev->flows);
3889 static int __init pg_init(void)
3892 struct proc_dir_entry *pe;
3894 printk(KERN_INFO "%s", version);
3896 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3900 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3902 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3903 "procfs entry.\n", PGCTRL);
3904 proc_net_remove(&init_net, PG_PROC_DIR);
3908 /* Register us to receive netdevice events */
3909 register_netdevice_notifier(&pktgen_notifier_block);
3911 for_each_online_cpu(cpu) {
3914 err = pktgen_create_thread(cpu);
3916 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3917 "thread for cpu %d (%d)\n", cpu, err);
3920 if (list_empty(&pktgen_threads)) {
3921 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3923 unregister_netdevice_notifier(&pktgen_notifier_block);
3924 remove_proc_entry(PGCTRL, pg_proc_dir);
3925 proc_net_remove(&init_net, PG_PROC_DIR);
3932 static void __exit pg_cleanup(void)
3934 struct pktgen_thread *t;
3935 struct list_head *q, *n;
3936 wait_queue_head_t queue;
3937 init_waitqueue_head(&queue);
3939 /* Stop all interfaces & threads */
3941 list_for_each_safe(q, n, &pktgen_threads) {
3942 t = list_entry(q, struct pktgen_thread, th_list);
3943 kthread_stop(t->tsk);
3947 /* Un-register us from receiving netdevice events */
3948 unregister_netdevice_notifier(&pktgen_notifier_block);
3950 /* Clean up proc file system */
3951 remove_proc_entry(PGCTRL, pg_proc_dir);
3952 proc_net_remove(&init_net, PG_PROC_DIR);
3955 module_init(pg_init);
3956 module_exit(pg_cleanup);
3958 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3959 MODULE_DESCRIPTION("Packet Generator tool");
3960 MODULE_LICENSE("GPL");
3961 MODULE_VERSION(VERSION);
3962 module_param(pg_count_d, int, 0);
3963 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
3964 module_param(pg_delay_d, int, 0);
3965 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
3966 module_param(pg_clone_skb_d, int, 0);
3967 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
3968 module_param(debug, int, 0);
3969 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");