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>
119 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
121 #include <linux/sys.h>
122 #include <linux/types.h>
123 #include <linux/module.h>
124 #include <linux/moduleparam.h>
125 #include <linux/kernel.h>
126 #include <linux/mutex.h>
127 #include <linux/sched.h>
128 #include <linux/slab.h>
129 #include <linux/vmalloc.h>
130 #include <linux/unistd.h>
131 #include <linux/string.h>
132 #include <linux/ptrace.h>
133 #include <linux/errno.h>
134 #include <linux/ioport.h>
135 #include <linux/interrupt.h>
136 #include <linux/capability.h>
137 #include <linux/hrtimer.h>
138 #include <linux/freezer.h>
139 #include <linux/delay.h>
140 #include <linux/timer.h>
141 #include <linux/list.h>
142 #include <linux/init.h>
143 #include <linux/skbuff.h>
144 #include <linux/netdevice.h>
145 #include <linux/inet.h>
146 #include <linux/inetdevice.h>
147 #include <linux/rtnetlink.h>
148 #include <linux/if_arp.h>
149 #include <linux/if_vlan.h>
150 #include <linux/in.h>
151 #include <linux/ip.h>
152 #include <linux/ipv6.h>
153 #include <linux/udp.h>
154 #include <linux/proc_fs.h>
155 #include <linux/seq_file.h>
156 #include <linux/wait.h>
157 #include <linux/etherdevice.h>
158 #include <linux/kthread.h>
159 #include <net/net_namespace.h>
160 #include <net/checksum.h>
161 #include <net/ipv6.h>
162 #include <net/addrconf.h>
164 #include <net/xfrm.h>
166 #include <asm/byteorder.h>
167 #include <linux/rcupdate.h>
168 #include <linux/bitops.h>
169 #include <linux/io.h>
170 #include <linux/timex.h>
171 #include <linux/uaccess.h>
173 #include <asm/div64.h> /* do_div */
175 #define VERSION "2.74"
176 #define IP_NAME_SZ 32
177 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
178 #define MPLS_STACK_BOTTOM htonl(0x00000100)
180 #define func_enter() pr_debug("entering %s\n", __func__);
182 /* Device flag bits */
183 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
184 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
185 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
186 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
187 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
188 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
189 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
190 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
191 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
192 #define F_VID_RND (1<<9) /* Random VLAN ID */
193 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
194 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
195 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
196 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
197 #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
198 #define F_NODE (1<<15) /* Node memory alloc*/
200 /* Thread control flag bits */
201 #define T_STOP (1<<0) /* Stop run */
202 #define T_RUN (1<<1) /* Start run */
203 #define T_REMDEVALL (1<<2) /* Remove all devs */
204 #define T_REMDEV (1<<3) /* Remove one dev */
206 /* If lock -- can be removed after some work */
207 #define if_lock(t) spin_lock(&(t->if_lock));
208 #define if_unlock(t) spin_unlock(&(t->if_lock));
210 /* Used to help with determining the pkts on receive */
211 #define PKTGEN_MAGIC 0xbe9be955
212 #define PG_PROC_DIR "pktgen"
213 #define PGCTRL "pgctrl"
214 static struct proc_dir_entry *pg_proc_dir;
216 #define MAX_CFLOWS 65536
218 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
219 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
225 struct xfrm_state *x;
231 #define F_INIT (1<<0) /* flow has been initialized */
235 * Try to keep frequent/infrequent used vars. separated.
237 struct proc_dir_entry *entry; /* proc file */
238 struct pktgen_thread *pg_thread;/* the owner */
239 struct list_head list; /* chaining in the thread's run-queue */
241 int running; /* if false, the test will stop */
243 /* If min != max, then we will either do a linear iteration, or
244 * we will do a random selection from within the range.
247 int removal_mark; /* non-zero => the device is marked for
248 * removal by worker thread */
250 int min_pkt_size; /* = ETH_ZLEN; */
251 int max_pkt_size; /* = ETH_ZLEN; */
252 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
254 u64 delay; /* nano-seconds */
256 __u64 count; /* Default No packets to send */
257 __u64 sofar; /* How many pkts we've sent so far */
258 __u64 tx_bytes; /* How many bytes we've transmitted */
259 __u64 errors; /* Errors when trying to transmit, */
261 /* runtime counters relating to clone_skb */
263 __u64 allocated_skbs;
265 int last_ok; /* Was last skb sent?
266 * Or a failed transmit of some sort?
267 * This will keep sequence numbers in order
272 u64 idle_acc; /* nano-seconds */
277 * Use multiple SKBs during packet gen.
278 * If this number is greater than 1, then
279 * that many copies of the same packet will be
280 * sent before a new packet is allocated.
281 * If you want to send 1024 identical packets
282 * before creating a new packet,
283 * set clone_skb to 1024.
286 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
287 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
288 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
289 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
291 struct in6_addr in6_saddr;
292 struct in6_addr in6_daddr;
293 struct in6_addr cur_in6_daddr;
294 struct in6_addr cur_in6_saddr;
296 struct in6_addr min_in6_daddr;
297 struct in6_addr max_in6_daddr;
298 struct in6_addr min_in6_saddr;
299 struct in6_addr max_in6_saddr;
301 /* If we're doing ranges, random or incremental, then this
302 * defines the min/max for those ranges.
304 __be32 saddr_min; /* inclusive, source IP address */
305 __be32 saddr_max; /* exclusive, source IP address */
306 __be32 daddr_min; /* inclusive, dest IP address */
307 __be32 daddr_max; /* exclusive, dest IP address */
309 __u16 udp_src_min; /* inclusive, source UDP port */
310 __u16 udp_src_max; /* exclusive, source UDP port */
311 __u16 udp_dst_min; /* inclusive, dest UDP port */
312 __u16 udp_dst_max; /* exclusive, dest UDP port */
315 __u8 tos; /* six MSB of (former) IPv4 TOS
316 are for dscp codepoint */
317 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
318 (see RFC 3260, sec. 4) */
321 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
322 __be32 labels[MAX_MPLS_LABELS];
324 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
327 __u16 vlan_id; /* 0xffff means no vlan tag */
331 __u16 svlan_id; /* 0xffff means no svlan tag */
333 __u32 src_mac_count; /* How many MACs to iterate through */
334 __u32 dst_mac_count; /* How many MACs to iterate through */
336 unsigned char dst_mac[ETH_ALEN];
337 unsigned char src_mac[ETH_ALEN];
339 __u32 cur_dst_mac_offset;
340 __u32 cur_src_mac_offset;
352 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
354 We fill in SRC address later
355 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359 __u16 pad; /* pad out the hh struct to an even 16 bytes */
361 struct sk_buff *skb; /* skb we are to transmit next, used for when we
362 * are transmitting the same one multiple times
364 struct net_device *odev; /* The out-going device.
365 * Note that the device should have it's
366 * pg_info pointer pointing back to this
368 * Set when the user specifies the out-going
369 * device name (not when the inject is
370 * started as it used to do.)
373 struct flow_state *flows;
374 unsigned cflows; /* Concurrent flows (config) */
375 unsigned lflow; /* Flow length (config) */
376 unsigned nflows; /* accumulated flows (stats) */
377 unsigned curfl; /* current sequenced flow (state)*/
381 __u32 skb_priority; /* skb priority field */
382 int node; /* Memory node */
385 __u8 ipsmode; /* IPSEC mode (config) */
386 __u8 ipsproto; /* IPSEC type (config) */
398 struct pktgen_thread {
399 spinlock_t if_lock; /* for list of devices */
400 struct list_head if_list; /* All device here */
401 struct list_head th_list;
402 struct task_struct *tsk;
405 /* Field for thread to receive "posted" events terminate,
411 wait_queue_head_t queue;
412 struct completion start_done;
418 static inline ktime_t ktime_now(void)
423 return timespec_to_ktime(ts);
426 /* This works even if 32 bit because of careful byte order choice */
427 static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
429 return cmp1.tv64 < cmp2.tv64;
432 static const char version[] =
433 "Packet Generator for packet performance testing. "
434 "Version: " VERSION "\n";
436 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
437 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
438 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
439 const char *ifname, bool exact);
440 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
441 static void pktgen_run_all_threads(void);
442 static void pktgen_reset_all_threads(void);
443 static void pktgen_stop_all_threads_ifs(void);
445 static void pktgen_stop(struct pktgen_thread *t);
446 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
448 static unsigned int scan_ip6(const char *s, char ip[16]);
449 static unsigned int fmt_ip6(char *s, const char ip[16]);
451 /* Module parameters, defaults. */
452 static int pg_count_d __read_mostly = 1000;
453 static int pg_delay_d __read_mostly;
454 static int pg_clone_skb_d __read_mostly;
455 static int debug __read_mostly;
457 static DEFINE_MUTEX(pktgen_thread_lock);
458 static LIST_HEAD(pktgen_threads);
460 static struct notifier_block pktgen_notifier_block = {
461 .notifier_call = pktgen_device_event,
465 * /proc handling functions
469 static int pgctrl_show(struct seq_file *seq, void *v)
471 seq_puts(seq, version);
475 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
476 size_t count, loff_t *ppos)
481 if (!capable(CAP_NET_ADMIN)) {
486 if (count > sizeof(data))
487 count = sizeof(data);
489 if (copy_from_user(data, buf, count)) {
493 data[count - 1] = 0; /* Make string */
495 if (!strcmp(data, "stop"))
496 pktgen_stop_all_threads_ifs();
498 else if (!strcmp(data, "start"))
499 pktgen_run_all_threads();
501 else if (!strcmp(data, "reset"))
502 pktgen_reset_all_threads();
505 pr_warning("Unknown command: %s\n", data);
513 static int pgctrl_open(struct inode *inode, struct file *file)
515 return single_open(file, pgctrl_show, PDE(inode)->data);
518 static const struct file_operations pktgen_fops = {
519 .owner = THIS_MODULE,
523 .write = pgctrl_write,
524 .release = single_release,
527 static int pktgen_if_show(struct seq_file *seq, void *v)
529 const struct pktgen_dev *pkt_dev = seq->private;
534 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
535 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
536 pkt_dev->max_pkt_size);
539 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
540 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
541 pkt_dev->clone_skb, pkt_dev->odevname);
543 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
547 " queue_map_min: %u queue_map_max: %u\n",
548 pkt_dev->queue_map_min,
549 pkt_dev->queue_map_max);
551 if (pkt_dev->skb_priority)
552 seq_printf(seq, " skb_priority: %u\n",
553 pkt_dev->skb_priority);
555 if (pkt_dev->flags & F_IPV6) {
556 char b1[128], b2[128], b3[128];
557 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
558 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
559 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
561 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
564 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
565 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
566 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
568 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
573 " dst_min: %s dst_max: %s\n",
574 pkt_dev->dst_min, pkt_dev->dst_max);
576 " src_min: %s src_max: %s\n",
577 pkt_dev->src_min, pkt_dev->src_max);
580 seq_puts(seq, " src_mac: ");
582 seq_printf(seq, "%pM ",
583 is_zero_ether_addr(pkt_dev->src_mac) ?
584 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
586 seq_printf(seq, "dst_mac: ");
587 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
590 " udp_src_min: %d udp_src_max: %d"
591 " udp_dst_min: %d udp_dst_max: %d\n",
592 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
593 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
596 " src_mac_count: %d dst_mac_count: %d\n",
597 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
599 if (pkt_dev->nr_labels) {
601 seq_printf(seq, " mpls: ");
602 for (i = 0; i < pkt_dev->nr_labels; i++)
603 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
604 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
607 if (pkt_dev->vlan_id != 0xffff)
608 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
609 pkt_dev->vlan_id, pkt_dev->vlan_p,
612 if (pkt_dev->svlan_id != 0xffff)
613 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
614 pkt_dev->svlan_id, pkt_dev->svlan_p,
618 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
620 if (pkt_dev->traffic_class)
621 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
623 if (pkt_dev->node >= 0)
624 seq_printf(seq, " node: %d\n", pkt_dev->node);
626 seq_printf(seq, " Flags: ");
628 if (pkt_dev->flags & F_IPV6)
629 seq_printf(seq, "IPV6 ");
631 if (pkt_dev->flags & F_IPSRC_RND)
632 seq_printf(seq, "IPSRC_RND ");
634 if (pkt_dev->flags & F_IPDST_RND)
635 seq_printf(seq, "IPDST_RND ");
637 if (pkt_dev->flags & F_TXSIZE_RND)
638 seq_printf(seq, "TXSIZE_RND ");
640 if (pkt_dev->flags & F_UDPSRC_RND)
641 seq_printf(seq, "UDPSRC_RND ");
643 if (pkt_dev->flags & F_UDPDST_RND)
644 seq_printf(seq, "UDPDST_RND ");
646 if (pkt_dev->flags & F_MPLS_RND)
647 seq_printf(seq, "MPLS_RND ");
649 if (pkt_dev->flags & F_QUEUE_MAP_RND)
650 seq_printf(seq, "QUEUE_MAP_RND ");
652 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
653 seq_printf(seq, "QUEUE_MAP_CPU ");
655 if (pkt_dev->cflows) {
656 if (pkt_dev->flags & F_FLOW_SEQ)
657 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
659 seq_printf(seq, "FLOW_RND ");
663 if (pkt_dev->flags & F_IPSEC_ON)
664 seq_printf(seq, "IPSEC ");
667 if (pkt_dev->flags & F_MACSRC_RND)
668 seq_printf(seq, "MACSRC_RND ");
670 if (pkt_dev->flags & F_MACDST_RND)
671 seq_printf(seq, "MACDST_RND ");
673 if (pkt_dev->flags & F_VID_RND)
674 seq_printf(seq, "VID_RND ");
676 if (pkt_dev->flags & F_SVID_RND)
677 seq_printf(seq, "SVID_RND ");
679 if (pkt_dev->flags & F_NODE)
680 seq_printf(seq, "NODE_ALLOC ");
684 /* not really stopped, more like last-running-at */
685 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
686 idle = pkt_dev->idle_acc;
687 do_div(idle, NSEC_PER_USEC);
690 "Current:\n pkts-sofar: %llu errors: %llu\n",
691 (unsigned long long)pkt_dev->sofar,
692 (unsigned long long)pkt_dev->errors);
695 " started: %lluus stopped: %lluus idle: %lluus\n",
696 (unsigned long long) ktime_to_us(pkt_dev->started_at),
697 (unsigned long long) ktime_to_us(stopped),
698 (unsigned long long) idle);
701 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
702 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
703 pkt_dev->cur_src_mac_offset);
705 if (pkt_dev->flags & F_IPV6) {
706 char b1[128], b2[128];
707 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
708 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
709 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
711 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
712 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
714 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
715 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
717 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
719 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
721 if (pkt_dev->result[0])
722 seq_printf(seq, "Result: %s\n", pkt_dev->result);
724 seq_printf(seq, "Result: Idle\n");
730 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
736 for (; i < maxlen; i++) {
740 if (get_user(c, &user_buffer[i]))
742 value = hex_to_bin(c);
751 static int count_trail_chars(const char __user * user_buffer,
756 for (i = 0; i < maxlen; i++) {
758 if (get_user(c, &user_buffer[i]))
776 static unsigned long num_arg(const char __user * user_buffer,
777 unsigned long maxlen, unsigned long *num)
782 for (i = 0; i < maxlen; i++) {
784 if (get_user(c, &user_buffer[i]))
786 if ((c >= '0') && (c <= '9')) {
795 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
799 for (i = 0; i < maxlen; i++) {
801 if (get_user(c, &user_buffer[i]))
819 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
826 pkt_dev->nr_labels = 0;
829 len = hex32_arg(&buffer[i], 8, &tmp);
832 pkt_dev->labels[n] = htonl(tmp);
833 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
834 pkt_dev->flags |= F_MPLS_RND;
836 if (get_user(c, &buffer[i]))
840 if (n >= MAX_MPLS_LABELS)
844 pkt_dev->nr_labels = n;
848 static ssize_t pktgen_if_write(struct file *file,
849 const char __user * user_buffer, size_t count,
852 struct seq_file *seq = file->private_data;
853 struct pktgen_dev *pkt_dev = seq->private;
855 char name[16], valstr[32];
856 unsigned long value = 0;
857 char *pg_result = NULL;
861 pg_result = &(pkt_dev->result[0]);
864 pr_warning("wrong command format\n");
869 tmp = count_trail_chars(user_buffer, max);
871 pr_warning("illegal format\n");
876 /* Read variable name */
878 len = strn_len(&user_buffer[i], sizeof(name) - 1);
882 memset(name, 0, sizeof(name));
883 if (copy_from_user(name, &user_buffer[i], len))
888 len = count_trail_chars(&user_buffer[i], max);
895 size_t copy = min_t(size_t, count, 1023);
897 if (copy_from_user(tb, user_buffer, copy))
900 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
901 (unsigned long)count, tb);
904 if (!strcmp(name, "min_pkt_size")) {
905 len = num_arg(&user_buffer[i], 10, &value);
910 if (value < 14 + 20 + 8)
912 if (value != pkt_dev->min_pkt_size) {
913 pkt_dev->min_pkt_size = value;
914 pkt_dev->cur_pkt_size = value;
916 sprintf(pg_result, "OK: min_pkt_size=%u",
917 pkt_dev->min_pkt_size);
921 if (!strcmp(name, "max_pkt_size")) {
922 len = num_arg(&user_buffer[i], 10, &value);
927 if (value < 14 + 20 + 8)
929 if (value != pkt_dev->max_pkt_size) {
930 pkt_dev->max_pkt_size = value;
931 pkt_dev->cur_pkt_size = value;
933 sprintf(pg_result, "OK: max_pkt_size=%u",
934 pkt_dev->max_pkt_size);
938 /* Shortcut for min = max */
940 if (!strcmp(name, "pkt_size")) {
941 len = num_arg(&user_buffer[i], 10, &value);
946 if (value < 14 + 20 + 8)
948 if (value != pkt_dev->min_pkt_size) {
949 pkt_dev->min_pkt_size = value;
950 pkt_dev->max_pkt_size = value;
951 pkt_dev->cur_pkt_size = value;
953 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
957 if (!strcmp(name, "debug")) {
958 len = num_arg(&user_buffer[i], 10, &value);
964 sprintf(pg_result, "OK: debug=%u", debug);
968 if (!strcmp(name, "frags")) {
969 len = num_arg(&user_buffer[i], 10, &value);
974 pkt_dev->nfrags = value;
975 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
978 if (!strcmp(name, "delay")) {
979 len = num_arg(&user_buffer[i], 10, &value);
984 if (value == 0x7FFFFFFF)
985 pkt_dev->delay = ULLONG_MAX;
987 pkt_dev->delay = (u64)value;
989 sprintf(pg_result, "OK: delay=%llu",
990 (unsigned long long) pkt_dev->delay);
993 if (!strcmp(name, "rate")) {
994 len = num_arg(&user_buffer[i], 10, &value);
1001 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1003 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1005 sprintf(pg_result, "OK: rate=%lu", value);
1008 if (!strcmp(name, "ratep")) {
1009 len = num_arg(&user_buffer[i], 10, &value);
1016 pkt_dev->delay = NSEC_PER_SEC/value;
1018 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1020 sprintf(pg_result, "OK: rate=%lu", value);
1023 if (!strcmp(name, "udp_src_min")) {
1024 len = num_arg(&user_buffer[i], 10, &value);
1029 if (value != pkt_dev->udp_src_min) {
1030 pkt_dev->udp_src_min = value;
1031 pkt_dev->cur_udp_src = value;
1033 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1036 if (!strcmp(name, "udp_dst_min")) {
1037 len = num_arg(&user_buffer[i], 10, &value);
1042 if (value != pkt_dev->udp_dst_min) {
1043 pkt_dev->udp_dst_min = value;
1044 pkt_dev->cur_udp_dst = value;
1046 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1049 if (!strcmp(name, "udp_src_max")) {
1050 len = num_arg(&user_buffer[i], 10, &value);
1055 if (value != pkt_dev->udp_src_max) {
1056 pkt_dev->udp_src_max = value;
1057 pkt_dev->cur_udp_src = value;
1059 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1062 if (!strcmp(name, "udp_dst_max")) {
1063 len = num_arg(&user_buffer[i], 10, &value);
1068 if (value != pkt_dev->udp_dst_max) {
1069 pkt_dev->udp_dst_max = value;
1070 pkt_dev->cur_udp_dst = value;
1072 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1075 if (!strcmp(name, "clone_skb")) {
1076 len = num_arg(&user_buffer[i], 10, &value);
1081 pkt_dev->clone_skb = value;
1083 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1086 if (!strcmp(name, "count")) {
1087 len = num_arg(&user_buffer[i], 10, &value);
1092 pkt_dev->count = value;
1093 sprintf(pg_result, "OK: count=%llu",
1094 (unsigned long long)pkt_dev->count);
1097 if (!strcmp(name, "src_mac_count")) {
1098 len = num_arg(&user_buffer[i], 10, &value);
1103 if (pkt_dev->src_mac_count != value) {
1104 pkt_dev->src_mac_count = value;
1105 pkt_dev->cur_src_mac_offset = 0;
1107 sprintf(pg_result, "OK: src_mac_count=%d",
1108 pkt_dev->src_mac_count);
1111 if (!strcmp(name, "dst_mac_count")) {
1112 len = num_arg(&user_buffer[i], 10, &value);
1117 if (pkt_dev->dst_mac_count != value) {
1118 pkt_dev->dst_mac_count = value;
1119 pkt_dev->cur_dst_mac_offset = 0;
1121 sprintf(pg_result, "OK: dst_mac_count=%d",
1122 pkt_dev->dst_mac_count);
1125 if (!strcmp(name, "node")) {
1126 len = num_arg(&user_buffer[i], 10, &value);
1132 if (node_possible(value)) {
1133 pkt_dev->node = value;
1134 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1137 sprintf(pg_result, "ERROR: node not possible");
1140 if (!strcmp(name, "flag")) {
1143 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1147 if (copy_from_user(f, &user_buffer[i], len))
1150 if (strcmp(f, "IPSRC_RND") == 0)
1151 pkt_dev->flags |= F_IPSRC_RND;
1153 else if (strcmp(f, "!IPSRC_RND") == 0)
1154 pkt_dev->flags &= ~F_IPSRC_RND;
1156 else if (strcmp(f, "TXSIZE_RND") == 0)
1157 pkt_dev->flags |= F_TXSIZE_RND;
1159 else if (strcmp(f, "!TXSIZE_RND") == 0)
1160 pkt_dev->flags &= ~F_TXSIZE_RND;
1162 else if (strcmp(f, "IPDST_RND") == 0)
1163 pkt_dev->flags |= F_IPDST_RND;
1165 else if (strcmp(f, "!IPDST_RND") == 0)
1166 pkt_dev->flags &= ~F_IPDST_RND;
1168 else if (strcmp(f, "UDPSRC_RND") == 0)
1169 pkt_dev->flags |= F_UDPSRC_RND;
1171 else if (strcmp(f, "!UDPSRC_RND") == 0)
1172 pkt_dev->flags &= ~F_UDPSRC_RND;
1174 else if (strcmp(f, "UDPDST_RND") == 0)
1175 pkt_dev->flags |= F_UDPDST_RND;
1177 else if (strcmp(f, "!UDPDST_RND") == 0)
1178 pkt_dev->flags &= ~F_UDPDST_RND;
1180 else if (strcmp(f, "MACSRC_RND") == 0)
1181 pkt_dev->flags |= F_MACSRC_RND;
1183 else if (strcmp(f, "!MACSRC_RND") == 0)
1184 pkt_dev->flags &= ~F_MACSRC_RND;
1186 else if (strcmp(f, "MACDST_RND") == 0)
1187 pkt_dev->flags |= F_MACDST_RND;
1189 else if (strcmp(f, "!MACDST_RND") == 0)
1190 pkt_dev->flags &= ~F_MACDST_RND;
1192 else if (strcmp(f, "MPLS_RND") == 0)
1193 pkt_dev->flags |= F_MPLS_RND;
1195 else if (strcmp(f, "!MPLS_RND") == 0)
1196 pkt_dev->flags &= ~F_MPLS_RND;
1198 else if (strcmp(f, "VID_RND") == 0)
1199 pkt_dev->flags |= F_VID_RND;
1201 else if (strcmp(f, "!VID_RND") == 0)
1202 pkt_dev->flags &= ~F_VID_RND;
1204 else if (strcmp(f, "SVID_RND") == 0)
1205 pkt_dev->flags |= F_SVID_RND;
1207 else if (strcmp(f, "!SVID_RND") == 0)
1208 pkt_dev->flags &= ~F_SVID_RND;
1210 else if (strcmp(f, "FLOW_SEQ") == 0)
1211 pkt_dev->flags |= F_FLOW_SEQ;
1213 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1214 pkt_dev->flags |= F_QUEUE_MAP_RND;
1216 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1217 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1219 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1220 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1222 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1223 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1225 else if (strcmp(f, "IPSEC") == 0)
1226 pkt_dev->flags |= F_IPSEC_ON;
1229 else if (strcmp(f, "!IPV6") == 0)
1230 pkt_dev->flags &= ~F_IPV6;
1232 else if (strcmp(f, "NODE_ALLOC") == 0)
1233 pkt_dev->flags |= F_NODE;
1235 else if (strcmp(f, "!NODE_ALLOC") == 0)
1236 pkt_dev->flags &= ~F_NODE;
1240 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1242 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1243 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
1246 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1249 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1250 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1254 if (copy_from_user(buf, &user_buffer[i], len))
1257 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1258 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1259 strncpy(pkt_dev->dst_min, buf, len);
1260 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1261 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1264 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1267 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1270 if (!strcmp(name, "dst_max")) {
1271 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1276 if (copy_from_user(buf, &user_buffer[i], len))
1280 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1281 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1282 strncpy(pkt_dev->dst_max, buf, len);
1283 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1284 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1287 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1290 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1293 if (!strcmp(name, "dst6")) {
1294 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1298 pkt_dev->flags |= F_IPV6;
1300 if (copy_from_user(buf, &user_buffer[i], len))
1304 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1305 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1307 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1310 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1313 sprintf(pg_result, "OK: dst6=%s", buf);
1316 if (!strcmp(name, "dst6_min")) {
1317 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1321 pkt_dev->flags |= F_IPV6;
1323 if (copy_from_user(buf, &user_buffer[i], len))
1327 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1328 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1330 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1331 &pkt_dev->min_in6_daddr);
1333 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1336 sprintf(pg_result, "OK: dst6_min=%s", buf);
1339 if (!strcmp(name, "dst6_max")) {
1340 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1344 pkt_dev->flags |= F_IPV6;
1346 if (copy_from_user(buf, &user_buffer[i], len))
1350 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1351 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1354 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1357 sprintf(pg_result, "OK: dst6_max=%s", buf);
1360 if (!strcmp(name, "src6")) {
1361 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1365 pkt_dev->flags |= F_IPV6;
1367 if (copy_from_user(buf, &user_buffer[i], len))
1371 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1372 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1374 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1377 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1380 sprintf(pg_result, "OK: src6=%s", buf);
1383 if (!strcmp(name, "src_min")) {
1384 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1388 if (copy_from_user(buf, &user_buffer[i], len))
1391 if (strcmp(buf, pkt_dev->src_min) != 0) {
1392 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1393 strncpy(pkt_dev->src_min, buf, len);
1394 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1395 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1398 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1401 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1404 if (!strcmp(name, "src_max")) {
1405 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1409 if (copy_from_user(buf, &user_buffer[i], len))
1412 if (strcmp(buf, pkt_dev->src_max) != 0) {
1413 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1414 strncpy(pkt_dev->src_max, buf, len);
1415 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1416 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1419 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1422 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1425 if (!strcmp(name, "dst_mac")) {
1427 unsigned char old_dmac[ETH_ALEN];
1428 unsigned char *m = pkt_dev->dst_mac;
1429 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1431 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1435 memset(valstr, 0, sizeof(valstr));
1436 if (copy_from_user(valstr, &user_buffer[i], len))
1440 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1443 value = hex_to_bin(*v);
1445 *m = *m * 16 + value;
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++) {
1479 value = hex_to_bin(*v);
1481 *m = *m * 16 + value;
1489 /* Set up Src MAC */
1490 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1491 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1493 sprintf(pg_result, "OK: srcmac");
1497 if (!strcmp(name, "clear_counters")) {
1498 pktgen_clear_counters(pkt_dev);
1499 sprintf(pg_result, "OK: Clearing counters.\n");
1503 if (!strcmp(name, "flows")) {
1504 len = num_arg(&user_buffer[i], 10, &value);
1509 if (value > MAX_CFLOWS)
1512 pkt_dev->cflows = value;
1513 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1517 if (!strcmp(name, "flowlen")) {
1518 len = num_arg(&user_buffer[i], 10, &value);
1523 pkt_dev->lflow = value;
1524 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1528 if (!strcmp(name, "queue_map_min")) {
1529 len = num_arg(&user_buffer[i], 5, &value);
1534 pkt_dev->queue_map_min = value;
1535 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1539 if (!strcmp(name, "queue_map_max")) {
1540 len = num_arg(&user_buffer[i], 5, &value);
1545 pkt_dev->queue_map_max = value;
1546 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1550 if (!strcmp(name, "mpls")) {
1553 len = get_labels(&user_buffer[i], pkt_dev);
1557 cnt = sprintf(pg_result, "OK: mpls=");
1558 for (n = 0; n < pkt_dev->nr_labels; n++)
1559 cnt += sprintf(pg_result + cnt,
1560 "%08x%s", ntohl(pkt_dev->labels[n]),
1561 n == pkt_dev->nr_labels-1 ? "" : ",");
1563 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1564 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1565 pkt_dev->svlan_id = 0xffff;
1568 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1573 if (!strcmp(name, "vlan_id")) {
1574 len = num_arg(&user_buffer[i], 4, &value);
1579 if (value <= 4095) {
1580 pkt_dev->vlan_id = value; /* turn on VLAN */
1583 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1585 if (debug && pkt_dev->nr_labels)
1586 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1588 pkt_dev->nr_labels = 0; /* turn off MPLS */
1589 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1591 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1592 pkt_dev->svlan_id = 0xffff;
1595 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1600 if (!strcmp(name, "vlan_p")) {
1601 len = num_arg(&user_buffer[i], 1, &value);
1606 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1607 pkt_dev->vlan_p = value;
1608 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1610 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1615 if (!strcmp(name, "vlan_cfi")) {
1616 len = num_arg(&user_buffer[i], 1, &value);
1621 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1622 pkt_dev->vlan_cfi = value;
1623 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1625 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1630 if (!strcmp(name, "svlan_id")) {
1631 len = num_arg(&user_buffer[i], 4, &value);
1636 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1637 pkt_dev->svlan_id = value; /* turn on SVLAN */
1640 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1642 if (debug && pkt_dev->nr_labels)
1643 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1645 pkt_dev->nr_labels = 0; /* turn off MPLS */
1646 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1648 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1649 pkt_dev->svlan_id = 0xffff;
1652 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1657 if (!strcmp(name, "svlan_p")) {
1658 len = num_arg(&user_buffer[i], 1, &value);
1663 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1664 pkt_dev->svlan_p = value;
1665 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1667 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1672 if (!strcmp(name, "svlan_cfi")) {
1673 len = num_arg(&user_buffer[i], 1, &value);
1678 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1679 pkt_dev->svlan_cfi = value;
1680 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1682 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1687 if (!strcmp(name, "tos")) {
1688 __u32 tmp_value = 0;
1689 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1695 pkt_dev->tos = tmp_value;
1696 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1698 sprintf(pg_result, "ERROR: tos must be 00-ff");
1703 if (!strcmp(name, "traffic_class")) {
1704 __u32 tmp_value = 0;
1705 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1711 pkt_dev->traffic_class = tmp_value;
1712 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1714 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1719 if (!strcmp(name, "skb_priority")) {
1720 len = num_arg(&user_buffer[i], 9, &value);
1725 pkt_dev->skb_priority = value;
1726 sprintf(pg_result, "OK: skb_priority=%i",
1727 pkt_dev->skb_priority);
1731 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1735 static int pktgen_if_open(struct inode *inode, struct file *file)
1737 return single_open(file, pktgen_if_show, PDE(inode)->data);
1740 static const struct file_operations pktgen_if_fops = {
1741 .owner = THIS_MODULE,
1742 .open = pktgen_if_open,
1744 .llseek = seq_lseek,
1745 .write = pktgen_if_write,
1746 .release = single_release,
1749 static int pktgen_thread_show(struct seq_file *seq, void *v)
1751 struct pktgen_thread *t = seq->private;
1752 const struct pktgen_dev *pkt_dev;
1756 seq_printf(seq, "Running: ");
1759 list_for_each_entry(pkt_dev, &t->if_list, list)
1760 if (pkt_dev->running)
1761 seq_printf(seq, "%s ", pkt_dev->odevname);
1763 seq_printf(seq, "\nStopped: ");
1765 list_for_each_entry(pkt_dev, &t->if_list, list)
1766 if (!pkt_dev->running)
1767 seq_printf(seq, "%s ", pkt_dev->odevname);
1770 seq_printf(seq, "\nResult: %s\n", t->result);
1772 seq_printf(seq, "\nResult: NA\n");
1779 static ssize_t pktgen_thread_write(struct file *file,
1780 const char __user * user_buffer,
1781 size_t count, loff_t * offset)
1783 struct seq_file *seq = file->private_data;
1784 struct pktgen_thread *t = seq->private;
1785 int i, max, len, ret;
1790 // sprintf(pg_result, "Wrong command format");
1795 len = count_trail_chars(user_buffer, max);
1801 /* Read variable name */
1803 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1807 memset(name, 0, sizeof(name));
1808 if (copy_from_user(name, &user_buffer[i], len))
1813 len = count_trail_chars(&user_buffer[i], max);
1820 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1821 name, (unsigned long)count);
1824 pr_err("ERROR: No thread\n");
1829 pg_result = &(t->result[0]);
1831 if (!strcmp(name, "add_device")) {
1834 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1839 if (copy_from_user(f, &user_buffer[i], len))
1842 mutex_lock(&pktgen_thread_lock);
1843 pktgen_add_device(t, f);
1844 mutex_unlock(&pktgen_thread_lock);
1846 sprintf(pg_result, "OK: add_device=%s", f);
1850 if (!strcmp(name, "rem_device_all")) {
1851 mutex_lock(&pktgen_thread_lock);
1852 t->control |= T_REMDEVALL;
1853 mutex_unlock(&pktgen_thread_lock);
1854 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1856 sprintf(pg_result, "OK: rem_device_all");
1860 if (!strcmp(name, "max_before_softirq")) {
1861 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1871 static int pktgen_thread_open(struct inode *inode, struct file *file)
1873 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1876 static const struct file_operations pktgen_thread_fops = {
1877 .owner = THIS_MODULE,
1878 .open = pktgen_thread_open,
1880 .llseek = seq_lseek,
1881 .write = pktgen_thread_write,
1882 .release = single_release,
1885 /* Think find or remove for NN */
1886 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1888 struct pktgen_thread *t;
1889 struct pktgen_dev *pkt_dev = NULL;
1890 bool exact = (remove == FIND);
1892 list_for_each_entry(t, &pktgen_threads, th_list) {
1893 pkt_dev = pktgen_find_dev(t, ifname, exact);
1897 pkt_dev->removal_mark = 1;
1898 t->control |= T_REMDEV;
1908 * mark a device for removal
1910 static void pktgen_mark_device(const char *ifname)
1912 struct pktgen_dev *pkt_dev = NULL;
1913 const int max_tries = 10, msec_per_try = 125;
1916 mutex_lock(&pktgen_thread_lock);
1917 pr_debug("%s: marking %s for removal\n", __func__, ifname);
1921 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1922 if (pkt_dev == NULL)
1923 break; /* success */
1925 mutex_unlock(&pktgen_thread_lock);
1926 pr_debug("%s: waiting for %s to disappear....\n",
1928 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1929 mutex_lock(&pktgen_thread_lock);
1931 if (++i >= max_tries) {
1932 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1933 __func__, msec_per_try * i, ifname);
1939 mutex_unlock(&pktgen_thread_lock);
1942 static void pktgen_change_name(struct net_device *dev)
1944 struct pktgen_thread *t;
1946 list_for_each_entry(t, &pktgen_threads, th_list) {
1947 struct pktgen_dev *pkt_dev;
1949 list_for_each_entry(pkt_dev, &t->if_list, list) {
1950 if (pkt_dev->odev != dev)
1953 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1955 pkt_dev->entry = proc_create_data(dev->name, 0600,
1959 if (!pkt_dev->entry)
1960 pr_err("can't move proc entry for '%s'\n",
1967 static int pktgen_device_event(struct notifier_block *unused,
1968 unsigned long event, void *ptr)
1970 struct net_device *dev = ptr;
1972 if (!net_eq(dev_net(dev), &init_net))
1975 /* It is OK that we do not hold the group lock right now,
1976 * as we run under the RTNL lock.
1980 case NETDEV_CHANGENAME:
1981 pktgen_change_name(dev);
1984 case NETDEV_UNREGISTER:
1985 pktgen_mark_device(dev->name);
1992 static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1998 for (i = 0; ifname[i] != '@'; i++) {
2006 return dev_get_by_name(&init_net, b);
2010 /* Associate pktgen_dev with a device. */
2012 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
2014 struct net_device *odev;
2017 /* Clean old setups */
2018 if (pkt_dev->odev) {
2019 dev_put(pkt_dev->odev);
2020 pkt_dev->odev = NULL;
2023 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
2025 pr_err("no such netdevice: \"%s\"\n", ifname);
2029 if (odev->type != ARPHRD_ETHER) {
2030 pr_err("not an ethernet device: \"%s\"\n", ifname);
2032 } else if (!netif_running(odev)) {
2033 pr_err("device is down: \"%s\"\n", ifname);
2036 pkt_dev->odev = odev;
2044 /* Read pkt_dev from the interface and set up internal pktgen_dev
2045 * structure to have the right information to create/send packets
2047 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2051 if (!pkt_dev->odev) {
2052 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2053 sprintf(pkt_dev->result,
2054 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2058 /* make sure that we don't pick a non-existing transmit queue */
2059 ntxq = pkt_dev->odev->real_num_tx_queues;
2061 if (ntxq <= pkt_dev->queue_map_min) {
2062 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2063 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2065 pkt_dev->queue_map_min = ntxq - 1;
2067 if (pkt_dev->queue_map_max >= ntxq) {
2068 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [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 pr_err("ERROR: IPv6 link address not available\n");
2134 pkt_dev->saddr_min = 0;
2135 pkt_dev->saddr_max = 0;
2136 if (strlen(pkt_dev->src_min) == 0) {
2138 struct in_device *in_dev;
2141 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2143 if (in_dev->ifa_list) {
2144 pkt_dev->saddr_min =
2145 in_dev->ifa_list->ifa_address;
2146 pkt_dev->saddr_max = pkt_dev->saddr_min;
2151 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2152 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2155 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2156 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2158 /* Initialize current values. */
2159 pkt_dev->cur_dst_mac_offset = 0;
2160 pkt_dev->cur_src_mac_offset = 0;
2161 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2162 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2163 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2164 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2165 pkt_dev->nflows = 0;
2169 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2171 ktime_t start_time, end_time;
2173 struct hrtimer_sleeper t;
2175 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2176 hrtimer_set_expires(&t.timer, spin_until);
2178 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2179 if (remaining <= 0) {
2180 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2184 start_time = ktime_now();
2185 if (remaining < 100000)
2186 ndelay(remaining); /* really small just spin */
2188 /* see do_nanosleep */
2189 hrtimer_init_sleeper(&t, current);
2191 set_current_state(TASK_INTERRUPTIBLE);
2192 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2193 if (!hrtimer_active(&t.timer))
2199 hrtimer_cancel(&t.timer);
2200 } while (t.task && pkt_dev->running && !signal_pending(current));
2201 __set_current_state(TASK_RUNNING);
2203 end_time = ktime_now();
2205 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2206 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2209 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2211 pkt_dev->pkt_overhead = 0;
2212 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2213 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2214 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2217 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2219 return !!(pkt_dev->flows[flow].flags & F_INIT);
2222 static inline int f_pick(struct pktgen_dev *pkt_dev)
2224 int flow = pkt_dev->curfl;
2226 if (pkt_dev->flags & F_FLOW_SEQ) {
2227 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2229 pkt_dev->flows[flow].count = 0;
2230 pkt_dev->flows[flow].flags = 0;
2231 pkt_dev->curfl += 1;
2232 if (pkt_dev->curfl >= pkt_dev->cflows)
2233 pkt_dev->curfl = 0; /*reset */
2236 flow = random32() % pkt_dev->cflows;
2237 pkt_dev->curfl = flow;
2239 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2240 pkt_dev->flows[flow].count = 0;
2241 pkt_dev->flows[flow].flags = 0;
2245 return pkt_dev->curfl;
2250 /* If there was already an IPSEC SA, we keep it as is, else
2251 * we go look for it ...
2253 #define DUMMY_MARK 0
2254 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2256 struct xfrm_state *x = pkt_dev->flows[flow].x;
2258 /*slow path: we dont already have xfrm_state*/
2259 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
2260 (xfrm_address_t *)&pkt_dev->cur_daddr,
2261 (xfrm_address_t *)&pkt_dev->cur_saddr,
2264 pkt_dev->ipsproto, 0);
2266 pkt_dev->flows[flow].x = x;
2267 set_pkt_overhead(pkt_dev);
2268 pkt_dev->pkt_overhead += x->props.header_len;
2274 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2277 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2278 pkt_dev->cur_queue_map = smp_processor_id();
2280 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2282 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2284 (pkt_dev->queue_map_max -
2285 pkt_dev->queue_map_min + 1)
2286 + pkt_dev->queue_map_min;
2288 t = pkt_dev->cur_queue_map + 1;
2289 if (t > pkt_dev->queue_map_max)
2290 t = pkt_dev->queue_map_min;
2292 pkt_dev->cur_queue_map = t;
2294 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2297 /* Increment/randomize headers according to flags and current values
2298 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2300 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2306 if (pkt_dev->cflows)
2307 flow = f_pick(pkt_dev);
2309 /* Deal with source MAC */
2310 if (pkt_dev->src_mac_count > 1) {
2314 if (pkt_dev->flags & F_MACSRC_RND)
2315 mc = random32() % pkt_dev->src_mac_count;
2317 mc = pkt_dev->cur_src_mac_offset++;
2318 if (pkt_dev->cur_src_mac_offset >=
2319 pkt_dev->src_mac_count)
2320 pkt_dev->cur_src_mac_offset = 0;
2323 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2324 pkt_dev->hh[11] = tmp;
2325 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2326 pkt_dev->hh[10] = tmp;
2327 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2328 pkt_dev->hh[9] = tmp;
2329 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2330 pkt_dev->hh[8] = tmp;
2331 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2332 pkt_dev->hh[7] = tmp;
2335 /* Deal with Destination MAC */
2336 if (pkt_dev->dst_mac_count > 1) {
2340 if (pkt_dev->flags & F_MACDST_RND)
2341 mc = random32() % pkt_dev->dst_mac_count;
2344 mc = pkt_dev->cur_dst_mac_offset++;
2345 if (pkt_dev->cur_dst_mac_offset >=
2346 pkt_dev->dst_mac_count) {
2347 pkt_dev->cur_dst_mac_offset = 0;
2351 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2352 pkt_dev->hh[5] = tmp;
2353 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2354 pkt_dev->hh[4] = tmp;
2355 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2356 pkt_dev->hh[3] = tmp;
2357 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2358 pkt_dev->hh[2] = tmp;
2359 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2360 pkt_dev->hh[1] = tmp;
2363 if (pkt_dev->flags & F_MPLS_RND) {
2365 for (i = 0; i < pkt_dev->nr_labels; i++)
2366 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2367 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2368 ((__force __be32)random32() &
2372 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2373 pkt_dev->vlan_id = random32() & (4096-1);
2376 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2377 pkt_dev->svlan_id = random32() & (4096 - 1);
2380 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2381 if (pkt_dev->flags & F_UDPSRC_RND)
2382 pkt_dev->cur_udp_src = random32() %
2383 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2384 + pkt_dev->udp_src_min;
2387 pkt_dev->cur_udp_src++;
2388 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2389 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2393 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2394 if (pkt_dev->flags & F_UDPDST_RND) {
2395 pkt_dev->cur_udp_dst = random32() %
2396 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2397 + pkt_dev->udp_dst_min;
2399 pkt_dev->cur_udp_dst++;
2400 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2401 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2405 if (!(pkt_dev->flags & F_IPV6)) {
2407 imn = ntohl(pkt_dev->saddr_min);
2408 imx = ntohl(pkt_dev->saddr_max);
2411 if (pkt_dev->flags & F_IPSRC_RND)
2412 t = random32() % (imx - imn) + imn;
2414 t = ntohl(pkt_dev->cur_saddr);
2420 pkt_dev->cur_saddr = htonl(t);
2423 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2424 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2426 imn = ntohl(pkt_dev->daddr_min);
2427 imx = ntohl(pkt_dev->daddr_max);
2431 if (pkt_dev->flags & F_IPDST_RND) {
2433 t = random32() % (imx - imn) + imn;
2436 while (ipv4_is_loopback(s) ||
2437 ipv4_is_multicast(s) ||
2438 ipv4_is_lbcast(s) ||
2439 ipv4_is_zeronet(s) ||
2440 ipv4_is_local_multicast(s)) {
2441 t = random32() % (imx - imn) + imn;
2444 pkt_dev->cur_daddr = s;
2446 t = ntohl(pkt_dev->cur_daddr);
2451 pkt_dev->cur_daddr = htonl(t);
2454 if (pkt_dev->cflows) {
2455 pkt_dev->flows[flow].flags |= F_INIT;
2456 pkt_dev->flows[flow].cur_daddr =
2459 if (pkt_dev->flags & F_IPSEC_ON)
2460 get_ipsec_sa(pkt_dev, flow);
2465 } else { /* IPV6 * */
2467 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2468 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2469 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2470 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2474 /* Only random destinations yet */
2476 for (i = 0; i < 4; i++) {
2477 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2478 (((__force __be32)random32() |
2479 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2480 pkt_dev->max_in6_daddr.s6_addr32[i]);
2485 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2487 if (pkt_dev->flags & F_TXSIZE_RND) {
2489 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2490 + pkt_dev->min_pkt_size;
2492 t = pkt_dev->cur_pkt_size + 1;
2493 if (t > pkt_dev->max_pkt_size)
2494 t = pkt_dev->min_pkt_size;
2496 pkt_dev->cur_pkt_size = t;
2499 set_cur_queue_map(pkt_dev);
2501 pkt_dev->flows[flow].count++;
2506 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2508 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2514 /* XXX: we dont support tunnel mode for now until
2515 * we resolve the dst issue */
2516 if (x->props.mode != XFRM_MODE_TRANSPORT)
2519 spin_lock(&x->lock);
2522 err = x->outer_mode->output(x, skb);
2525 err = x->type->output(x, skb);
2529 x->curlft.bytes += skb->len;
2530 x->curlft.packets++;
2532 spin_unlock(&x->lock);
2536 static void free_SAs(struct pktgen_dev *pkt_dev)
2538 if (pkt_dev->cflows) {
2539 /* let go of the SAs if we have them */
2541 for (i = 0; i < pkt_dev->cflows; i++) {
2542 struct xfrm_state *x = pkt_dev->flows[i].x;
2545 pkt_dev->flows[i].x = NULL;
2551 static int process_ipsec(struct pktgen_dev *pkt_dev,
2552 struct sk_buff *skb, __be16 protocol)
2554 if (pkt_dev->flags & F_IPSEC_ON) {
2555 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2560 nhead = x->props.header_len - skb_headroom(skb);
2562 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2564 pr_err("Error expanding ipsec packet %d\n",
2570 /* ipsec is not expecting ll header */
2571 skb_pull(skb, ETH_HLEN);
2572 ret = pktgen_output_ipsec(skb, pkt_dev);
2574 pr_err("Error creating ipsec packet %d\n", ret);
2578 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2579 memcpy(eth, pkt_dev->hh, 12);
2580 *(u16 *) ð[12] = protocol;
2590 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2593 for (i = 0; i < pkt_dev->nr_labels; i++)
2594 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2597 *mpls |= MPLS_STACK_BOTTOM;
2600 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2603 return htons(id | (cfi << 12) | (prio << 13));
2606 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2607 struct pktgen_dev *pkt_dev)
2609 struct sk_buff *skb = NULL;
2611 struct udphdr *udph;
2614 struct pktgen_hdr *pgh = NULL;
2615 __be16 protocol = htons(ETH_P_IP);
2617 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2618 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2619 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2620 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2623 if (pkt_dev->nr_labels)
2624 protocol = htons(ETH_P_MPLS_UC);
2626 if (pkt_dev->vlan_id != 0xffff)
2627 protocol = htons(ETH_P_8021Q);
2629 /* Update any of the values, used when we're incrementing various
2632 mod_cur_headers(pkt_dev);
2633 queue_map = pkt_dev->cur_queue_map;
2635 datalen = (odev->hard_header_len + 16) & ~0xf;
2637 if (pkt_dev->flags & F_NODE) {
2640 if (pkt_dev->node >= 0)
2641 node = pkt_dev->node;
2643 node = numa_node_id();
2645 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2646 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2648 skb_reserve(skb, NET_SKB_PAD);
2653 skb = __netdev_alloc_skb(odev,
2654 pkt_dev->cur_pkt_size + 64
2655 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2658 sprintf(pkt_dev->result, "No memory");
2662 skb_reserve(skb, datalen);
2664 /* Reserve for ethernet and IP header */
2665 eth = (__u8 *) skb_push(skb, 14);
2666 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2667 if (pkt_dev->nr_labels)
2668 mpls_push(mpls, pkt_dev);
2670 if (pkt_dev->vlan_id != 0xffff) {
2671 if (pkt_dev->svlan_id != 0xffff) {
2672 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2673 *svlan_tci = build_tci(pkt_dev->svlan_id,
2676 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2677 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2679 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2680 *vlan_tci = build_tci(pkt_dev->vlan_id,
2683 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2684 *vlan_encapsulated_proto = htons(ETH_P_IP);
2687 skb->network_header = skb->tail;
2688 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2689 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2690 skb_set_queue_mapping(skb, queue_map);
2691 skb->priority = pkt_dev->skb_priority;
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 mod_cur_headers(pkt_dev);
2999 queue_map = pkt_dev->cur_queue_map;
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 skb->priority = pkt_dev->skb_priority;
3039 iph = ipv6_hdr(skb);
3040 udph = udp_hdr(skb);
3042 memcpy(eth, pkt_dev->hh, 12);
3043 *(__be16 *) ð[12] = protocol;
3045 /* Eth + IPh + UDPh + mpls */
3046 datalen = pkt_dev->cur_pkt_size - 14 -
3047 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
3048 pkt_dev->pkt_overhead;
3050 if (datalen < sizeof(struct pktgen_hdr)) {
3051 datalen = sizeof(struct pktgen_hdr);
3052 if (net_ratelimit())
3053 pr_info("increased datalen to %d\n", datalen);
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;
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;
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;
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;
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 pr_warning("interface: %s is already stopped\n",
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;
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;
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;
3432 /* Remove all devices, free mem */
3436 list_for_each_safe(q, n, &t->if_list) {
3437 cur = list_entry(q, struct pktgen_dev, list);
3439 kfree_skb(cur->skb);
3442 pktgen_remove_device(t, cur);
3448 static void pktgen_rem_thread(struct pktgen_thread *t)
3450 /* Remove from the thread list */
3452 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3454 mutex_lock(&pktgen_thread_lock);
3456 list_del(&t->th_list);
3458 mutex_unlock(&pktgen_thread_lock);
3461 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3463 ktime_t idle_start = ktime_now();
3465 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3468 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3470 ktime_t idle_start = ktime_now();
3472 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3473 if (signal_pending(current))
3477 pktgen_resched(pkt_dev);
3481 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3484 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3486 struct net_device *odev = pkt_dev->odev;
3487 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
3488 = odev->netdev_ops->ndo_start_xmit;
3489 struct netdev_queue *txq;
3493 /* If device is offline, then don't send */
3494 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3495 pktgen_stop_device(pkt_dev);
3499 /* This is max DELAY, this has special meaning of
3502 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3503 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3507 /* If no skb or clone count exhausted then get new one */
3508 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3509 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3510 /* build a new pkt */
3511 kfree_skb(pkt_dev->skb);
3513 pkt_dev->skb = fill_packet(odev, pkt_dev);
3514 if (pkt_dev->skb == NULL) {
3515 pr_err("ERROR: couldn't 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("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3597 set_current_state(TASK_INTERRUPTIBLE);
3601 while (!kthread_should_stop()) {
3602 pkt_dev = next_to_run(t);
3604 if (unlikely(!pkt_dev && t->control == 0)) {
3605 wait_event_interruptible_timeout(t->queue,
3612 __set_current_state(TASK_RUNNING);
3614 if (likely(pkt_dev)) {
3615 pktgen_xmit(pkt_dev);
3618 pktgen_resched(pkt_dev);
3623 if (t->control & T_STOP) {
3625 t->control &= ~(T_STOP);
3628 if (t->control & T_RUN) {
3630 t->control &= ~(T_RUN);
3633 if (t->control & T_REMDEVALL) {
3634 pktgen_rem_all_ifs(t);
3635 t->control &= ~(T_REMDEVALL);
3638 if (t->control & T_REMDEV) {
3639 pktgen_rem_one_if(t);
3640 t->control &= ~(T_REMDEV);
3645 set_current_state(TASK_INTERRUPTIBLE);
3648 pr_debug("%s stopping all device\n", t->tsk->comm);
3651 pr_debug("%s removing all device\n", t->tsk->comm);
3652 pktgen_rem_all_ifs(t);
3654 pr_debug("%s removing thread\n", t->tsk->comm);
3655 pktgen_rem_thread(t);
3660 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3661 const char *ifname, bool exact)
3663 struct pktgen_dev *p, *pkt_dev = NULL;
3664 size_t len = strlen(ifname);
3667 list_for_each_entry(p, &t->if_list, list)
3668 if (strncmp(p->odevname, ifname, len) == 0) {
3669 if (p->odevname[len]) {
3670 if (exact || p->odevname[len] != '@')
3678 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
3683 * Adds a dev at front of if_list.
3686 static int add_dev_to_thread(struct pktgen_thread *t,
3687 struct pktgen_dev *pkt_dev)
3693 if (pkt_dev->pg_thread) {
3694 pr_err("ERROR: already assigned to a thread\n");
3699 list_add(&pkt_dev->list, &t->if_list);
3700 pkt_dev->pg_thread = t;
3701 pkt_dev->running = 0;
3708 /* Called under thread lock */
3710 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3712 struct pktgen_dev *pkt_dev;
3714 int node = cpu_to_node(t->cpu);
3716 /* We don't allow a device to be on several threads */
3718 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3720 pr_err("ERROR: interface already used\n");
3724 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3728 strcpy(pkt_dev->odevname, ifname);
3729 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3731 if (pkt_dev->flows == NULL) {
3735 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3737 pkt_dev->removal_mark = 0;
3738 pkt_dev->min_pkt_size = ETH_ZLEN;
3739 pkt_dev->max_pkt_size = ETH_ZLEN;
3740 pkt_dev->nfrags = 0;
3741 pkt_dev->clone_skb = pg_clone_skb_d;
3742 pkt_dev->delay = pg_delay_d;
3743 pkt_dev->count = pg_count_d;
3745 pkt_dev->udp_src_min = 9; /* sink port */
3746 pkt_dev->udp_src_max = 9;
3747 pkt_dev->udp_dst_min = 9;
3748 pkt_dev->udp_dst_max = 9;
3750 pkt_dev->vlan_p = 0;
3751 pkt_dev->vlan_cfi = 0;
3752 pkt_dev->vlan_id = 0xffff;
3753 pkt_dev->svlan_p = 0;
3754 pkt_dev->svlan_cfi = 0;
3755 pkt_dev->svlan_id = 0xffff;
3758 err = pktgen_setup_dev(pkt_dev, ifname);
3762 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3763 &pktgen_if_fops, pkt_dev);
3764 if (!pkt_dev->entry) {
3765 pr_err("cannot create %s/%s procfs entry\n",
3766 PG_PROC_DIR, ifname);
3771 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3772 pkt_dev->ipsproto = IPPROTO_ESP;
3775 return add_dev_to_thread(t, pkt_dev);
3777 dev_put(pkt_dev->odev);
3782 vfree(pkt_dev->flows);
3787 static int __init pktgen_create_thread(int cpu)
3789 struct pktgen_thread *t;
3790 struct proc_dir_entry *pe;
3791 struct task_struct *p;
3793 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3796 pr_err("ERROR: out of memory, can't create new thread\n");
3800 spin_lock_init(&t->if_lock);
3803 INIT_LIST_HEAD(&t->if_list);
3805 list_add_tail(&t->th_list, &pktgen_threads);
3806 init_completion(&t->start_done);
3808 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3810 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3811 list_del(&t->th_list);
3815 kthread_bind(p, cpu);
3818 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3819 &pktgen_thread_fops, t);
3821 pr_err("cannot create %s/%s procfs entry\n",
3822 PG_PROC_DIR, t->tsk->comm);
3824 list_del(&t->th_list);
3830 wait_for_completion(&t->start_done);
3836 * Removes a device from the thread if_list.
3838 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3839 struct pktgen_dev *pkt_dev)
3841 struct list_head *q, *n;
3842 struct pktgen_dev *p;
3844 list_for_each_safe(q, n, &t->if_list) {
3845 p = list_entry(q, struct pktgen_dev, list);
3851 static int pktgen_remove_device(struct pktgen_thread *t,
3852 struct pktgen_dev *pkt_dev)
3855 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
3857 if (pkt_dev->running) {
3858 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
3859 pktgen_stop_device(pkt_dev);
3862 /* Dis-associate from the interface */
3864 if (pkt_dev->odev) {
3865 dev_put(pkt_dev->odev);
3866 pkt_dev->odev = NULL;
3869 /* And update the thread if_list */
3871 _rem_dev_from_if_list(t, pkt_dev);
3874 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3879 vfree(pkt_dev->flows);
3884 static int __init pg_init(void)
3887 struct proc_dir_entry *pe;
3889 pr_info("%s", version);
3891 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3895 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3897 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
3898 proc_net_remove(&init_net, PG_PROC_DIR);
3902 /* Register us to receive netdevice events */
3903 register_netdevice_notifier(&pktgen_notifier_block);
3905 for_each_online_cpu(cpu) {
3908 err = pktgen_create_thread(cpu);
3910 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3914 if (list_empty(&pktgen_threads)) {
3915 pr_err("ERROR: Initialization failed for all threads\n");
3916 unregister_netdevice_notifier(&pktgen_notifier_block);
3917 remove_proc_entry(PGCTRL, pg_proc_dir);
3918 proc_net_remove(&init_net, PG_PROC_DIR);
3925 static void __exit pg_cleanup(void)
3927 struct pktgen_thread *t;
3928 struct list_head *q, *n;
3930 /* Stop all interfaces & threads */
3932 list_for_each_safe(q, n, &pktgen_threads) {
3933 t = list_entry(q, struct pktgen_thread, th_list);
3934 kthread_stop(t->tsk);
3938 /* Un-register us from receiving netdevice events */
3939 unregister_netdevice_notifier(&pktgen_notifier_block);
3941 /* Clean up proc file system */
3942 remove_proc_entry(PGCTRL, pg_proc_dir);
3943 proc_net_remove(&init_net, PG_PROC_DIR);
3946 module_init(pg_init);
3947 module_exit(pg_cleanup);
3949 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3950 MODULE_DESCRIPTION("Packet Generator tool");
3951 MODULE_LICENSE("GPL");
3952 MODULE_VERSION(VERSION);
3953 module_param(pg_count_d, int, 0);
3954 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
3955 module_param(pg_delay_d, int, 0);
3956 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
3957 module_param(pg_clone_skb_d, int, 0);
3958 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
3959 module_param(debug, int, 0);
3960 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");