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 int node; /* Memory node */
384 __u8 ipsmode; /* IPSEC mode (config) */
385 __u8 ipsproto; /* IPSEC type (config) */
397 struct pktgen_thread {
398 spinlock_t if_lock; /* for list of devices */
399 struct list_head if_list; /* All device here */
400 struct list_head th_list;
401 struct task_struct *tsk;
404 /* Field for thread to receive "posted" events terminate,
410 wait_queue_head_t queue;
411 struct completion start_done;
417 static inline ktime_t ktime_now(void)
422 return timespec_to_ktime(ts);
425 /* This works even if 32 bit because of careful byte order choice */
426 static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
428 return cmp1.tv64 < cmp2.tv64;
431 static const char version[] =
432 "Packet Generator for packet performance testing. "
433 "Version: " VERSION "\n";
435 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
436 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
437 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
438 const char *ifname, bool exact);
439 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
440 static void pktgen_run_all_threads(void);
441 static void pktgen_reset_all_threads(void);
442 static void pktgen_stop_all_threads_ifs(void);
444 static void pktgen_stop(struct pktgen_thread *t);
445 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
447 static unsigned int scan_ip6(const char *s, char ip[16]);
448 static unsigned int fmt_ip6(char *s, const char ip[16]);
450 /* Module parameters, defaults. */
451 static int pg_count_d __read_mostly = 1000;
452 static int pg_delay_d __read_mostly;
453 static int pg_clone_skb_d __read_mostly;
454 static int debug __read_mostly;
456 static DEFINE_MUTEX(pktgen_thread_lock);
457 static LIST_HEAD(pktgen_threads);
459 static struct notifier_block pktgen_notifier_block = {
460 .notifier_call = pktgen_device_event,
464 * /proc handling functions
468 static int pgctrl_show(struct seq_file *seq, void *v)
470 seq_puts(seq, version);
474 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
475 size_t count, loff_t *ppos)
480 if (!capable(CAP_NET_ADMIN)) {
485 if (count > sizeof(data))
486 count = sizeof(data);
488 if (copy_from_user(data, buf, count)) {
492 data[count - 1] = 0; /* Make string */
494 if (!strcmp(data, "stop"))
495 pktgen_stop_all_threads_ifs();
497 else if (!strcmp(data, "start"))
498 pktgen_run_all_threads();
500 else if (!strcmp(data, "reset"))
501 pktgen_reset_all_threads();
504 pr_warning("Unknown command: %s\n", data);
512 static int pgctrl_open(struct inode *inode, struct file *file)
514 return single_open(file, pgctrl_show, PDE(inode)->data);
517 static const struct file_operations pktgen_fops = {
518 .owner = THIS_MODULE,
522 .write = pgctrl_write,
523 .release = single_release,
526 static int pktgen_if_show(struct seq_file *seq, void *v)
528 const struct pktgen_dev *pkt_dev = seq->private;
533 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
534 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
535 pkt_dev->max_pkt_size);
538 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
539 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
540 pkt_dev->clone_skb, pkt_dev->odevname);
542 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
546 " queue_map_min: %u queue_map_max: %u\n",
547 pkt_dev->queue_map_min,
548 pkt_dev->queue_map_max);
550 if (pkt_dev->flags & F_IPV6) {
551 char b1[128], b2[128], b3[128];
552 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
553 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
554 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
556 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
559 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
560 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
561 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
563 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
568 " dst_min: %s dst_max: %s\n",
569 pkt_dev->dst_min, pkt_dev->dst_max);
571 " src_min: %s src_max: %s\n",
572 pkt_dev->src_min, pkt_dev->src_max);
575 seq_puts(seq, " src_mac: ");
577 seq_printf(seq, "%pM ",
578 is_zero_ether_addr(pkt_dev->src_mac) ?
579 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
581 seq_printf(seq, "dst_mac: ");
582 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
585 " udp_src_min: %d udp_src_max: %d"
586 " udp_dst_min: %d udp_dst_max: %d\n",
587 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
588 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
591 " src_mac_count: %d dst_mac_count: %d\n",
592 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
594 if (pkt_dev->nr_labels) {
596 seq_printf(seq, " mpls: ");
597 for (i = 0; i < pkt_dev->nr_labels; i++)
598 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
599 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
602 if (pkt_dev->vlan_id != 0xffff)
603 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
604 pkt_dev->vlan_id, pkt_dev->vlan_p,
607 if (pkt_dev->svlan_id != 0xffff)
608 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
609 pkt_dev->svlan_id, pkt_dev->svlan_p,
613 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
615 if (pkt_dev->traffic_class)
616 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
618 if (pkt_dev->node >= 0)
619 seq_printf(seq, " node: %d\n", pkt_dev->node);
621 seq_printf(seq, " Flags: ");
623 if (pkt_dev->flags & F_IPV6)
624 seq_printf(seq, "IPV6 ");
626 if (pkt_dev->flags & F_IPSRC_RND)
627 seq_printf(seq, "IPSRC_RND ");
629 if (pkt_dev->flags & F_IPDST_RND)
630 seq_printf(seq, "IPDST_RND ");
632 if (pkt_dev->flags & F_TXSIZE_RND)
633 seq_printf(seq, "TXSIZE_RND ");
635 if (pkt_dev->flags & F_UDPSRC_RND)
636 seq_printf(seq, "UDPSRC_RND ");
638 if (pkt_dev->flags & F_UDPDST_RND)
639 seq_printf(seq, "UDPDST_RND ");
641 if (pkt_dev->flags & F_MPLS_RND)
642 seq_printf(seq, "MPLS_RND ");
644 if (pkt_dev->flags & F_QUEUE_MAP_RND)
645 seq_printf(seq, "QUEUE_MAP_RND ");
647 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
648 seq_printf(seq, "QUEUE_MAP_CPU ");
650 if (pkt_dev->cflows) {
651 if (pkt_dev->flags & F_FLOW_SEQ)
652 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
654 seq_printf(seq, "FLOW_RND ");
658 if (pkt_dev->flags & F_IPSEC_ON)
659 seq_printf(seq, "IPSEC ");
662 if (pkt_dev->flags & F_MACSRC_RND)
663 seq_printf(seq, "MACSRC_RND ");
665 if (pkt_dev->flags & F_MACDST_RND)
666 seq_printf(seq, "MACDST_RND ");
668 if (pkt_dev->flags & F_VID_RND)
669 seq_printf(seq, "VID_RND ");
671 if (pkt_dev->flags & F_SVID_RND)
672 seq_printf(seq, "SVID_RND ");
674 if (pkt_dev->flags & F_NODE)
675 seq_printf(seq, "NODE_ALLOC ");
679 /* not really stopped, more like last-running-at */
680 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
681 idle = pkt_dev->idle_acc;
682 do_div(idle, NSEC_PER_USEC);
685 "Current:\n pkts-sofar: %llu errors: %llu\n",
686 (unsigned long long)pkt_dev->sofar,
687 (unsigned long long)pkt_dev->errors);
690 " started: %lluus stopped: %lluus idle: %lluus\n",
691 (unsigned long long) ktime_to_us(pkt_dev->started_at),
692 (unsigned long long) ktime_to_us(stopped),
693 (unsigned long long) idle);
696 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
697 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
698 pkt_dev->cur_src_mac_offset);
700 if (pkt_dev->flags & F_IPV6) {
701 char b1[128], b2[128];
702 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
703 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
704 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
706 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
707 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
709 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
710 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
712 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
714 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
716 if (pkt_dev->result[0])
717 seq_printf(seq, "Result: %s\n", pkt_dev->result);
719 seq_printf(seq, "Result: Idle\n");
725 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
731 for (; i < maxlen; i++) {
734 if (get_user(c, &user_buffer[i]))
736 if ((c >= '0') && (c <= '9'))
738 else if ((c >= 'a') && (c <= 'f'))
739 *num |= c - 'a' + 10;
740 else if ((c >= 'A') && (c <= 'F'))
741 *num |= c - 'A' + 10;
748 static int count_trail_chars(const char __user * user_buffer,
753 for (i = 0; i < maxlen; i++) {
755 if (get_user(c, &user_buffer[i]))
773 static unsigned long num_arg(const char __user * user_buffer,
774 unsigned long maxlen, unsigned long *num)
779 for (; i < maxlen; i++) {
781 if (get_user(c, &user_buffer[i]))
783 if ((c >= '0') && (c <= '9')) {
792 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
796 for (; i < maxlen; i++) {
798 if (get_user(c, &user_buffer[i]))
816 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
823 pkt_dev->nr_labels = 0;
826 len = hex32_arg(&buffer[i], 8, &tmp);
829 pkt_dev->labels[n] = htonl(tmp);
830 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
831 pkt_dev->flags |= F_MPLS_RND;
833 if (get_user(c, &buffer[i]))
837 if (n >= MAX_MPLS_LABELS)
841 pkt_dev->nr_labels = n;
845 static ssize_t pktgen_if_write(struct file *file,
846 const char __user * user_buffer, size_t count,
849 struct seq_file *seq = file->private_data;
850 struct pktgen_dev *pkt_dev = seq->private;
852 char name[16], valstr[32];
853 unsigned long value = 0;
854 char *pg_result = NULL;
858 pg_result = &(pkt_dev->result[0]);
861 pr_warning("wrong command format\n");
866 tmp = count_trail_chars(&user_buffer[i], max);
868 pr_warning("illegal format\n");
873 /* Read variable name */
875 len = strn_len(&user_buffer[i], sizeof(name) - 1);
879 memset(name, 0, sizeof(name));
880 if (copy_from_user(name, &user_buffer[i], len))
885 len = count_trail_chars(&user_buffer[i], max);
893 if (copy_from_user(tb, user_buffer, count))
896 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
897 (unsigned long)count, tb);
900 if (!strcmp(name, "min_pkt_size")) {
901 len = num_arg(&user_buffer[i], 10, &value);
906 if (value < 14 + 20 + 8)
908 if (value != pkt_dev->min_pkt_size) {
909 pkt_dev->min_pkt_size = value;
910 pkt_dev->cur_pkt_size = value;
912 sprintf(pg_result, "OK: min_pkt_size=%u",
913 pkt_dev->min_pkt_size);
917 if (!strcmp(name, "max_pkt_size")) {
918 len = num_arg(&user_buffer[i], 10, &value);
923 if (value < 14 + 20 + 8)
925 if (value != pkt_dev->max_pkt_size) {
926 pkt_dev->max_pkt_size = value;
927 pkt_dev->cur_pkt_size = value;
929 sprintf(pg_result, "OK: max_pkt_size=%u",
930 pkt_dev->max_pkt_size);
934 /* Shortcut for min = max */
936 if (!strcmp(name, "pkt_size")) {
937 len = num_arg(&user_buffer[i], 10, &value);
942 if (value < 14 + 20 + 8)
944 if (value != pkt_dev->min_pkt_size) {
945 pkt_dev->min_pkt_size = value;
946 pkt_dev->max_pkt_size = value;
947 pkt_dev->cur_pkt_size = value;
949 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
953 if (!strcmp(name, "debug")) {
954 len = num_arg(&user_buffer[i], 10, &value);
960 sprintf(pg_result, "OK: debug=%u", debug);
964 if (!strcmp(name, "frags")) {
965 len = num_arg(&user_buffer[i], 10, &value);
970 pkt_dev->nfrags = value;
971 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
974 if (!strcmp(name, "delay")) {
975 len = num_arg(&user_buffer[i], 10, &value);
980 if (value == 0x7FFFFFFF)
981 pkt_dev->delay = ULLONG_MAX;
983 pkt_dev->delay = (u64)value;
985 sprintf(pg_result, "OK: delay=%llu",
986 (unsigned long long) pkt_dev->delay);
989 if (!strcmp(name, "rate")) {
990 len = num_arg(&user_buffer[i], 10, &value);
997 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
999 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1001 sprintf(pg_result, "OK: rate=%lu", value);
1004 if (!strcmp(name, "ratep")) {
1005 len = num_arg(&user_buffer[i], 10, &value);
1012 pkt_dev->delay = NSEC_PER_SEC/value;
1014 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1016 sprintf(pg_result, "OK: rate=%lu", value);
1019 if (!strcmp(name, "udp_src_min")) {
1020 len = num_arg(&user_buffer[i], 10, &value);
1025 if (value != pkt_dev->udp_src_min) {
1026 pkt_dev->udp_src_min = value;
1027 pkt_dev->cur_udp_src = value;
1029 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1032 if (!strcmp(name, "udp_dst_min")) {
1033 len = num_arg(&user_buffer[i], 10, &value);
1038 if (value != pkt_dev->udp_dst_min) {
1039 pkt_dev->udp_dst_min = value;
1040 pkt_dev->cur_udp_dst = value;
1042 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1045 if (!strcmp(name, "udp_src_max")) {
1046 len = num_arg(&user_buffer[i], 10, &value);
1051 if (value != pkt_dev->udp_src_max) {
1052 pkt_dev->udp_src_max = value;
1053 pkt_dev->cur_udp_src = value;
1055 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1058 if (!strcmp(name, "udp_dst_max")) {
1059 len = num_arg(&user_buffer[i], 10, &value);
1064 if (value != pkt_dev->udp_dst_max) {
1065 pkt_dev->udp_dst_max = value;
1066 pkt_dev->cur_udp_dst = value;
1068 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1071 if (!strcmp(name, "clone_skb")) {
1072 len = num_arg(&user_buffer[i], 10, &value);
1077 pkt_dev->clone_skb = value;
1079 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1082 if (!strcmp(name, "count")) {
1083 len = num_arg(&user_buffer[i], 10, &value);
1088 pkt_dev->count = value;
1089 sprintf(pg_result, "OK: count=%llu",
1090 (unsigned long long)pkt_dev->count);
1093 if (!strcmp(name, "src_mac_count")) {
1094 len = num_arg(&user_buffer[i], 10, &value);
1099 if (pkt_dev->src_mac_count != value) {
1100 pkt_dev->src_mac_count = value;
1101 pkt_dev->cur_src_mac_offset = 0;
1103 sprintf(pg_result, "OK: src_mac_count=%d",
1104 pkt_dev->src_mac_count);
1107 if (!strcmp(name, "dst_mac_count")) {
1108 len = num_arg(&user_buffer[i], 10, &value);
1113 if (pkt_dev->dst_mac_count != value) {
1114 pkt_dev->dst_mac_count = value;
1115 pkt_dev->cur_dst_mac_offset = 0;
1117 sprintf(pg_result, "OK: dst_mac_count=%d",
1118 pkt_dev->dst_mac_count);
1121 if (!strcmp(name, "node")) {
1122 len = num_arg(&user_buffer[i], 10, &value);
1128 if (node_possible(value)) {
1129 pkt_dev->node = value;
1130 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1133 sprintf(pg_result, "ERROR: node not possible");
1136 if (!strcmp(name, "flag")) {
1139 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1143 if (copy_from_user(f, &user_buffer[i], len))
1146 if (strcmp(f, "IPSRC_RND") == 0)
1147 pkt_dev->flags |= F_IPSRC_RND;
1149 else if (strcmp(f, "!IPSRC_RND") == 0)
1150 pkt_dev->flags &= ~F_IPSRC_RND;
1152 else if (strcmp(f, "TXSIZE_RND") == 0)
1153 pkt_dev->flags |= F_TXSIZE_RND;
1155 else if (strcmp(f, "!TXSIZE_RND") == 0)
1156 pkt_dev->flags &= ~F_TXSIZE_RND;
1158 else if (strcmp(f, "IPDST_RND") == 0)
1159 pkt_dev->flags |= F_IPDST_RND;
1161 else if (strcmp(f, "!IPDST_RND") == 0)
1162 pkt_dev->flags &= ~F_IPDST_RND;
1164 else if (strcmp(f, "UDPSRC_RND") == 0)
1165 pkt_dev->flags |= F_UDPSRC_RND;
1167 else if (strcmp(f, "!UDPSRC_RND") == 0)
1168 pkt_dev->flags &= ~F_UDPSRC_RND;
1170 else if (strcmp(f, "UDPDST_RND") == 0)
1171 pkt_dev->flags |= F_UDPDST_RND;
1173 else if (strcmp(f, "!UDPDST_RND") == 0)
1174 pkt_dev->flags &= ~F_UDPDST_RND;
1176 else if (strcmp(f, "MACSRC_RND") == 0)
1177 pkt_dev->flags |= F_MACSRC_RND;
1179 else if (strcmp(f, "!MACSRC_RND") == 0)
1180 pkt_dev->flags &= ~F_MACSRC_RND;
1182 else if (strcmp(f, "MACDST_RND") == 0)
1183 pkt_dev->flags |= F_MACDST_RND;
1185 else if (strcmp(f, "!MACDST_RND") == 0)
1186 pkt_dev->flags &= ~F_MACDST_RND;
1188 else if (strcmp(f, "MPLS_RND") == 0)
1189 pkt_dev->flags |= F_MPLS_RND;
1191 else if (strcmp(f, "!MPLS_RND") == 0)
1192 pkt_dev->flags &= ~F_MPLS_RND;
1194 else if (strcmp(f, "VID_RND") == 0)
1195 pkt_dev->flags |= F_VID_RND;
1197 else if (strcmp(f, "!VID_RND") == 0)
1198 pkt_dev->flags &= ~F_VID_RND;
1200 else if (strcmp(f, "SVID_RND") == 0)
1201 pkt_dev->flags |= F_SVID_RND;
1203 else if (strcmp(f, "!SVID_RND") == 0)
1204 pkt_dev->flags &= ~F_SVID_RND;
1206 else if (strcmp(f, "FLOW_SEQ") == 0)
1207 pkt_dev->flags |= F_FLOW_SEQ;
1209 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1210 pkt_dev->flags |= F_QUEUE_MAP_RND;
1212 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1213 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1215 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1216 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1218 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1219 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1221 else if (strcmp(f, "IPSEC") == 0)
1222 pkt_dev->flags |= F_IPSEC_ON;
1225 else if (strcmp(f, "!IPV6") == 0)
1226 pkt_dev->flags &= ~F_IPV6;
1228 else if (strcmp(f, "NODE_ALLOC") == 0)
1229 pkt_dev->flags |= F_NODE;
1231 else if (strcmp(f, "!NODE_ALLOC") == 0)
1232 pkt_dev->flags &= ~F_NODE;
1236 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1238 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1239 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
1242 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1245 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1246 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1250 if (copy_from_user(buf, &user_buffer[i], len))
1253 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1254 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1255 strncpy(pkt_dev->dst_min, buf, len);
1256 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1257 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1260 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1263 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1266 if (!strcmp(name, "dst_max")) {
1267 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1272 if (copy_from_user(buf, &user_buffer[i], len))
1276 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1277 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1278 strncpy(pkt_dev->dst_max, buf, len);
1279 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1280 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1283 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1286 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1289 if (!strcmp(name, "dst6")) {
1290 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1294 pkt_dev->flags |= F_IPV6;
1296 if (copy_from_user(buf, &user_buffer[i], len))
1300 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1301 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1303 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1306 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1309 sprintf(pg_result, "OK: dst6=%s", buf);
1312 if (!strcmp(name, "dst6_min")) {
1313 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1317 pkt_dev->flags |= F_IPV6;
1319 if (copy_from_user(buf, &user_buffer[i], len))
1323 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1324 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1326 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1327 &pkt_dev->min_in6_daddr);
1329 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1332 sprintf(pg_result, "OK: dst6_min=%s", buf);
1335 if (!strcmp(name, "dst6_max")) {
1336 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1340 pkt_dev->flags |= F_IPV6;
1342 if (copy_from_user(buf, &user_buffer[i], len))
1346 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1347 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1350 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1353 sprintf(pg_result, "OK: dst6_max=%s", buf);
1356 if (!strcmp(name, "src6")) {
1357 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1361 pkt_dev->flags |= F_IPV6;
1363 if (copy_from_user(buf, &user_buffer[i], len))
1367 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1368 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1370 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1373 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1376 sprintf(pg_result, "OK: src6=%s", buf);
1379 if (!strcmp(name, "src_min")) {
1380 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1384 if (copy_from_user(buf, &user_buffer[i], len))
1387 if (strcmp(buf, pkt_dev->src_min) != 0) {
1388 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1389 strncpy(pkt_dev->src_min, buf, len);
1390 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1391 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1394 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1397 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1400 if (!strcmp(name, "src_max")) {
1401 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1405 if (copy_from_user(buf, &user_buffer[i], len))
1408 if (strcmp(buf, pkt_dev->src_max) != 0) {
1409 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1410 strncpy(pkt_dev->src_max, buf, len);
1411 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1412 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1415 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1418 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1421 if (!strcmp(name, "dst_mac")) {
1423 unsigned char old_dmac[ETH_ALEN];
1424 unsigned char *m = pkt_dev->dst_mac;
1425 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1427 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1431 memset(valstr, 0, sizeof(valstr));
1432 if (copy_from_user(valstr, &user_buffer[i], len))
1436 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1437 if (*v >= '0' && *v <= '9') {
1441 if (*v >= 'A' && *v <= 'F') {
1443 *m += *v - 'A' + 10;
1445 if (*v >= 'a' && *v <= 'f') {
1447 *m += *v - 'a' + 10;
1455 /* Set up Dest MAC */
1456 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1457 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1459 sprintf(pg_result, "OK: dstmac");
1462 if (!strcmp(name, "src_mac")) {
1464 unsigned char old_smac[ETH_ALEN];
1465 unsigned char *m = pkt_dev->src_mac;
1467 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1469 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1473 memset(valstr, 0, sizeof(valstr));
1474 if (copy_from_user(valstr, &user_buffer[i], len))
1478 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1479 if (*v >= '0' && *v <= '9') {
1483 if (*v >= 'A' && *v <= 'F') {
1485 *m += *v - 'A' + 10;
1487 if (*v >= 'a' && *v <= 'f') {
1489 *m += *v - 'a' + 10;
1497 /* Set up Src MAC */
1498 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1499 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1501 sprintf(pg_result, "OK: srcmac");
1505 if (!strcmp(name, "clear_counters")) {
1506 pktgen_clear_counters(pkt_dev);
1507 sprintf(pg_result, "OK: Clearing counters.\n");
1511 if (!strcmp(name, "flows")) {
1512 len = num_arg(&user_buffer[i], 10, &value);
1517 if (value > MAX_CFLOWS)
1520 pkt_dev->cflows = value;
1521 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1525 if (!strcmp(name, "flowlen")) {
1526 len = num_arg(&user_buffer[i], 10, &value);
1531 pkt_dev->lflow = value;
1532 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1536 if (!strcmp(name, "queue_map_min")) {
1537 len = num_arg(&user_buffer[i], 5, &value);
1542 pkt_dev->queue_map_min = value;
1543 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1547 if (!strcmp(name, "queue_map_max")) {
1548 len = num_arg(&user_buffer[i], 5, &value);
1553 pkt_dev->queue_map_max = value;
1554 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1558 if (!strcmp(name, "mpls")) {
1561 len = get_labels(&user_buffer[i], pkt_dev);
1565 cnt = sprintf(pg_result, "OK: mpls=");
1566 for (n = 0; n < pkt_dev->nr_labels; n++)
1567 cnt += sprintf(pg_result + cnt,
1568 "%08x%s", ntohl(pkt_dev->labels[n]),
1569 n == pkt_dev->nr_labels-1 ? "" : ",");
1571 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1572 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1573 pkt_dev->svlan_id = 0xffff;
1576 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1581 if (!strcmp(name, "vlan_id")) {
1582 len = num_arg(&user_buffer[i], 4, &value);
1587 if (value <= 4095) {
1588 pkt_dev->vlan_id = value; /* turn on VLAN */
1591 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1593 if (debug && pkt_dev->nr_labels)
1594 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1596 pkt_dev->nr_labels = 0; /* turn off MPLS */
1597 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1599 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1600 pkt_dev->svlan_id = 0xffff;
1603 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1608 if (!strcmp(name, "vlan_p")) {
1609 len = num_arg(&user_buffer[i], 1, &value);
1614 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1615 pkt_dev->vlan_p = value;
1616 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1618 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1623 if (!strcmp(name, "vlan_cfi")) {
1624 len = num_arg(&user_buffer[i], 1, &value);
1629 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1630 pkt_dev->vlan_cfi = value;
1631 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1633 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1638 if (!strcmp(name, "svlan_id")) {
1639 len = num_arg(&user_buffer[i], 4, &value);
1644 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1645 pkt_dev->svlan_id = value; /* turn on SVLAN */
1648 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1650 if (debug && pkt_dev->nr_labels)
1651 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1653 pkt_dev->nr_labels = 0; /* turn off MPLS */
1654 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1656 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1657 pkt_dev->svlan_id = 0xffff;
1660 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1665 if (!strcmp(name, "svlan_p")) {
1666 len = num_arg(&user_buffer[i], 1, &value);
1671 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1672 pkt_dev->svlan_p = value;
1673 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1675 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1680 if (!strcmp(name, "svlan_cfi")) {
1681 len = num_arg(&user_buffer[i], 1, &value);
1686 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1687 pkt_dev->svlan_cfi = value;
1688 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1690 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1695 if (!strcmp(name, "tos")) {
1696 __u32 tmp_value = 0;
1697 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1703 pkt_dev->tos = tmp_value;
1704 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1706 sprintf(pg_result, "ERROR: tos must be 00-ff");
1711 if (!strcmp(name, "traffic_class")) {
1712 __u32 tmp_value = 0;
1713 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1719 pkt_dev->traffic_class = tmp_value;
1720 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1722 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1727 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1731 static int pktgen_if_open(struct inode *inode, struct file *file)
1733 return single_open(file, pktgen_if_show, PDE(inode)->data);
1736 static const struct file_operations pktgen_if_fops = {
1737 .owner = THIS_MODULE,
1738 .open = pktgen_if_open,
1740 .llseek = seq_lseek,
1741 .write = pktgen_if_write,
1742 .release = single_release,
1745 static int pktgen_thread_show(struct seq_file *seq, void *v)
1747 struct pktgen_thread *t = seq->private;
1748 const struct pktgen_dev *pkt_dev;
1752 seq_printf(seq, "Running: ");
1755 list_for_each_entry(pkt_dev, &t->if_list, list)
1756 if (pkt_dev->running)
1757 seq_printf(seq, "%s ", pkt_dev->odevname);
1759 seq_printf(seq, "\nStopped: ");
1761 list_for_each_entry(pkt_dev, &t->if_list, list)
1762 if (!pkt_dev->running)
1763 seq_printf(seq, "%s ", pkt_dev->odevname);
1766 seq_printf(seq, "\nResult: %s\n", t->result);
1768 seq_printf(seq, "\nResult: NA\n");
1775 static ssize_t pktgen_thread_write(struct file *file,
1776 const char __user * user_buffer,
1777 size_t count, loff_t * offset)
1779 struct seq_file *seq = file->private_data;
1780 struct pktgen_thread *t = seq->private;
1781 int i = 0, max, len, ret;
1786 // sprintf(pg_result, "Wrong command format");
1791 len = count_trail_chars(&user_buffer[i], max);
1797 /* Read variable name */
1799 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1803 memset(name, 0, sizeof(name));
1804 if (copy_from_user(name, &user_buffer[i], len))
1809 len = count_trail_chars(&user_buffer[i], max);
1816 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1817 name, (unsigned long)count);
1820 pr_err("ERROR: No thread\n");
1825 pg_result = &(t->result[0]);
1827 if (!strcmp(name, "add_device")) {
1830 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1835 if (copy_from_user(f, &user_buffer[i], len))
1838 mutex_lock(&pktgen_thread_lock);
1839 pktgen_add_device(t, f);
1840 mutex_unlock(&pktgen_thread_lock);
1842 sprintf(pg_result, "OK: add_device=%s", f);
1846 if (!strcmp(name, "rem_device_all")) {
1847 mutex_lock(&pktgen_thread_lock);
1848 t->control |= T_REMDEVALL;
1849 mutex_unlock(&pktgen_thread_lock);
1850 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1852 sprintf(pg_result, "OK: rem_device_all");
1856 if (!strcmp(name, "max_before_softirq")) {
1857 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1867 static int pktgen_thread_open(struct inode *inode, struct file *file)
1869 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1872 static const struct file_operations pktgen_thread_fops = {
1873 .owner = THIS_MODULE,
1874 .open = pktgen_thread_open,
1876 .llseek = seq_lseek,
1877 .write = pktgen_thread_write,
1878 .release = single_release,
1881 /* Think find or remove for NN */
1882 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1884 struct pktgen_thread *t;
1885 struct pktgen_dev *pkt_dev = NULL;
1886 bool exact = (remove == FIND);
1888 list_for_each_entry(t, &pktgen_threads, th_list) {
1889 pkt_dev = pktgen_find_dev(t, ifname, exact);
1893 pkt_dev->removal_mark = 1;
1894 t->control |= T_REMDEV;
1904 * mark a device for removal
1906 static void pktgen_mark_device(const char *ifname)
1908 struct pktgen_dev *pkt_dev = NULL;
1909 const int max_tries = 10, msec_per_try = 125;
1912 mutex_lock(&pktgen_thread_lock);
1913 pr_debug("%s: marking %s for removal\n", __func__, ifname);
1917 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1918 if (pkt_dev == NULL)
1919 break; /* success */
1921 mutex_unlock(&pktgen_thread_lock);
1922 pr_debug("%s: waiting for %s to disappear....\n",
1924 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1925 mutex_lock(&pktgen_thread_lock);
1927 if (++i >= max_tries) {
1928 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1929 __func__, msec_per_try * i, ifname);
1935 mutex_unlock(&pktgen_thread_lock);
1938 static void pktgen_change_name(struct net_device *dev)
1940 struct pktgen_thread *t;
1942 list_for_each_entry(t, &pktgen_threads, th_list) {
1943 struct pktgen_dev *pkt_dev;
1945 list_for_each_entry(pkt_dev, &t->if_list, list) {
1946 if (pkt_dev->odev != dev)
1949 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1951 pkt_dev->entry = proc_create_data(dev->name, 0600,
1955 if (!pkt_dev->entry)
1956 pr_err("can't move proc entry for '%s'\n",
1963 static int pktgen_device_event(struct notifier_block *unused,
1964 unsigned long event, void *ptr)
1966 struct net_device *dev = ptr;
1968 if (!net_eq(dev_net(dev), &init_net))
1971 /* It is OK that we do not hold the group lock right now,
1972 * as we run under the RTNL lock.
1976 case NETDEV_CHANGENAME:
1977 pktgen_change_name(dev);
1980 case NETDEV_UNREGISTER:
1981 pktgen_mark_device(dev->name);
1988 static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1994 for (i = 0; ifname[i] != '@'; i++) {
2002 return dev_get_by_name(&init_net, b);
2006 /* Associate pktgen_dev with a device. */
2008 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
2010 struct net_device *odev;
2013 /* Clean old setups */
2014 if (pkt_dev->odev) {
2015 dev_put(pkt_dev->odev);
2016 pkt_dev->odev = NULL;
2019 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
2021 pr_err("no such netdevice: \"%s\"\n", ifname);
2025 if (odev->type != ARPHRD_ETHER) {
2026 pr_err("not an ethernet device: \"%s\"\n", ifname);
2028 } else if (!netif_running(odev)) {
2029 pr_err("device is down: \"%s\"\n", ifname);
2032 pkt_dev->odev = odev;
2040 /* Read pkt_dev from the interface and set up internal pktgen_dev
2041 * structure to have the right information to create/send packets
2043 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2047 if (!pkt_dev->odev) {
2048 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
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 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2059 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2061 pkt_dev->queue_map_min = ntxq - 1;
2063 if (pkt_dev->queue_map_max >= ntxq) {
2064 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2065 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2067 pkt_dev->queue_map_max = ntxq - 1;
2070 /* Default to the interface's mac if not explicitly set. */
2072 if (is_zero_ether_addr(pkt_dev->src_mac))
2073 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2075 /* Set up Dest MAC */
2076 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2078 /* Set up pkt size */
2079 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2081 if (pkt_dev->flags & F_IPV6) {
2083 * Skip this automatic address setting until locks or functions
2088 int i, set = 0, err = 1;
2089 struct inet6_dev *idev;
2091 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2092 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2100 * Use linklevel address if unconfigured.
2102 * use ipv6_get_lladdr if/when it's get exported
2106 idev = __in6_dev_get(pkt_dev->odev);
2108 struct inet6_ifaddr *ifp;
2110 read_lock_bh(&idev->lock);
2111 for (ifp = idev->addr_list; ifp;
2112 ifp = ifp->if_next) {
2113 if (ifp->scope == IFA_LINK &&
2114 !(ifp->flags & IFA_F_TENTATIVE)) {
2115 ipv6_addr_copy(&pkt_dev->
2122 read_unlock_bh(&idev->lock);
2126 pr_err("ERROR: IPv6 link address not available\n");
2130 pkt_dev->saddr_min = 0;
2131 pkt_dev->saddr_max = 0;
2132 if (strlen(pkt_dev->src_min) == 0) {
2134 struct in_device *in_dev;
2137 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2139 if (in_dev->ifa_list) {
2140 pkt_dev->saddr_min =
2141 in_dev->ifa_list->ifa_address;
2142 pkt_dev->saddr_max = pkt_dev->saddr_min;
2147 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2148 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2151 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2152 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2154 /* Initialize current values. */
2155 pkt_dev->cur_dst_mac_offset = 0;
2156 pkt_dev->cur_src_mac_offset = 0;
2157 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2158 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2159 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2160 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2161 pkt_dev->nflows = 0;
2165 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2167 ktime_t start_time, end_time;
2169 struct hrtimer_sleeper t;
2171 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2172 hrtimer_set_expires(&t.timer, spin_until);
2174 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2175 if (remaining <= 0) {
2176 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2180 start_time = ktime_now();
2181 if (remaining < 100000)
2182 ndelay(remaining); /* really small just spin */
2184 /* see do_nanosleep */
2185 hrtimer_init_sleeper(&t, current);
2187 set_current_state(TASK_INTERRUPTIBLE);
2188 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2189 if (!hrtimer_active(&t.timer))
2195 hrtimer_cancel(&t.timer);
2196 } while (t.task && pkt_dev->running && !signal_pending(current));
2197 __set_current_state(TASK_RUNNING);
2199 end_time = ktime_now();
2201 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2202 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2205 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2207 pkt_dev->pkt_overhead = 0;
2208 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2209 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2210 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2213 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2215 return !!(pkt_dev->flows[flow].flags & F_INIT);
2218 static inline int f_pick(struct pktgen_dev *pkt_dev)
2220 int flow = pkt_dev->curfl;
2222 if (pkt_dev->flags & F_FLOW_SEQ) {
2223 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2225 pkt_dev->flows[flow].count = 0;
2226 pkt_dev->flows[flow].flags = 0;
2227 pkt_dev->curfl += 1;
2228 if (pkt_dev->curfl >= pkt_dev->cflows)
2229 pkt_dev->curfl = 0; /*reset */
2232 flow = random32() % pkt_dev->cflows;
2233 pkt_dev->curfl = flow;
2235 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2236 pkt_dev->flows[flow].count = 0;
2237 pkt_dev->flows[flow].flags = 0;
2241 return pkt_dev->curfl;
2246 /* If there was already an IPSEC SA, we keep it as is, else
2247 * we go look for it ...
2249 #define DUMMY_MARK 0
2250 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2252 struct xfrm_state *x = pkt_dev->flows[flow].x;
2254 /*slow path: we dont already have xfrm_state*/
2255 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
2256 (xfrm_address_t *)&pkt_dev->cur_daddr,
2257 (xfrm_address_t *)&pkt_dev->cur_saddr,
2260 pkt_dev->ipsproto, 0);
2262 pkt_dev->flows[flow].x = x;
2263 set_pkt_overhead(pkt_dev);
2264 pkt_dev->pkt_overhead += x->props.header_len;
2270 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2273 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2274 pkt_dev->cur_queue_map = smp_processor_id();
2276 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2278 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2280 (pkt_dev->queue_map_max -
2281 pkt_dev->queue_map_min + 1)
2282 + pkt_dev->queue_map_min;
2284 t = pkt_dev->cur_queue_map + 1;
2285 if (t > pkt_dev->queue_map_max)
2286 t = pkt_dev->queue_map_min;
2288 pkt_dev->cur_queue_map = t;
2290 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2293 /* Increment/randomize headers according to flags and current values
2294 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2296 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2302 if (pkt_dev->cflows)
2303 flow = f_pick(pkt_dev);
2305 /* Deal with source MAC */
2306 if (pkt_dev->src_mac_count > 1) {
2310 if (pkt_dev->flags & F_MACSRC_RND)
2311 mc = random32() % pkt_dev->src_mac_count;
2313 mc = pkt_dev->cur_src_mac_offset++;
2314 if (pkt_dev->cur_src_mac_offset >=
2315 pkt_dev->src_mac_count)
2316 pkt_dev->cur_src_mac_offset = 0;
2319 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2320 pkt_dev->hh[11] = tmp;
2321 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2322 pkt_dev->hh[10] = tmp;
2323 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2324 pkt_dev->hh[9] = tmp;
2325 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2326 pkt_dev->hh[8] = tmp;
2327 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2328 pkt_dev->hh[7] = tmp;
2331 /* Deal with Destination MAC */
2332 if (pkt_dev->dst_mac_count > 1) {
2336 if (pkt_dev->flags & F_MACDST_RND)
2337 mc = random32() % pkt_dev->dst_mac_count;
2340 mc = pkt_dev->cur_dst_mac_offset++;
2341 if (pkt_dev->cur_dst_mac_offset >=
2342 pkt_dev->dst_mac_count) {
2343 pkt_dev->cur_dst_mac_offset = 0;
2347 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2348 pkt_dev->hh[5] = tmp;
2349 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2350 pkt_dev->hh[4] = tmp;
2351 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2352 pkt_dev->hh[3] = tmp;
2353 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2354 pkt_dev->hh[2] = tmp;
2355 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2356 pkt_dev->hh[1] = tmp;
2359 if (pkt_dev->flags & F_MPLS_RND) {
2361 for (i = 0; i < pkt_dev->nr_labels; i++)
2362 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2363 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2364 ((__force __be32)random32() &
2368 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2369 pkt_dev->vlan_id = random32() & (4096-1);
2372 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2373 pkt_dev->svlan_id = random32() & (4096 - 1);
2376 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2377 if (pkt_dev->flags & F_UDPSRC_RND)
2378 pkt_dev->cur_udp_src = random32() %
2379 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2380 + pkt_dev->udp_src_min;
2383 pkt_dev->cur_udp_src++;
2384 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2385 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2389 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2390 if (pkt_dev->flags & F_UDPDST_RND) {
2391 pkt_dev->cur_udp_dst = random32() %
2392 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2393 + pkt_dev->udp_dst_min;
2395 pkt_dev->cur_udp_dst++;
2396 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2397 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2401 if (!(pkt_dev->flags & F_IPV6)) {
2403 imn = ntohl(pkt_dev->saddr_min);
2404 imx = ntohl(pkt_dev->saddr_max);
2407 if (pkt_dev->flags & F_IPSRC_RND)
2408 t = random32() % (imx - imn) + imn;
2410 t = ntohl(pkt_dev->cur_saddr);
2416 pkt_dev->cur_saddr = htonl(t);
2419 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2420 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2422 imn = ntohl(pkt_dev->daddr_min);
2423 imx = ntohl(pkt_dev->daddr_max);
2427 if (pkt_dev->flags & F_IPDST_RND) {
2429 t = random32() % (imx - imn) + imn;
2432 while (ipv4_is_loopback(s) ||
2433 ipv4_is_multicast(s) ||
2434 ipv4_is_lbcast(s) ||
2435 ipv4_is_zeronet(s) ||
2436 ipv4_is_local_multicast(s)) {
2437 t = random32() % (imx - imn) + imn;
2440 pkt_dev->cur_daddr = s;
2442 t = ntohl(pkt_dev->cur_daddr);
2447 pkt_dev->cur_daddr = htonl(t);
2450 if (pkt_dev->cflows) {
2451 pkt_dev->flows[flow].flags |= F_INIT;
2452 pkt_dev->flows[flow].cur_daddr =
2455 if (pkt_dev->flags & F_IPSEC_ON)
2456 get_ipsec_sa(pkt_dev, flow);
2461 } else { /* IPV6 * */
2463 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2464 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2465 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2466 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2470 /* Only random destinations yet */
2472 for (i = 0; i < 4; i++) {
2473 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2474 (((__force __be32)random32() |
2475 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2476 pkt_dev->max_in6_daddr.s6_addr32[i]);
2481 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2483 if (pkt_dev->flags & F_TXSIZE_RND) {
2485 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2486 + pkt_dev->min_pkt_size;
2488 t = pkt_dev->cur_pkt_size + 1;
2489 if (t > pkt_dev->max_pkt_size)
2490 t = pkt_dev->min_pkt_size;
2492 pkt_dev->cur_pkt_size = t;
2495 set_cur_queue_map(pkt_dev);
2497 pkt_dev->flows[flow].count++;
2502 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2504 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2510 /* XXX: we dont support tunnel mode for now until
2511 * we resolve the dst issue */
2512 if (x->props.mode != XFRM_MODE_TRANSPORT)
2515 spin_lock(&x->lock);
2518 err = x->outer_mode->output(x, skb);
2521 err = x->type->output(x, skb);
2525 x->curlft.bytes += skb->len;
2526 x->curlft.packets++;
2528 spin_unlock(&x->lock);
2532 static void free_SAs(struct pktgen_dev *pkt_dev)
2534 if (pkt_dev->cflows) {
2535 /* let go of the SAs if we have them */
2537 for (; i < pkt_dev->cflows; i++) {
2538 struct xfrm_state *x = pkt_dev->flows[i].x;
2541 pkt_dev->flows[i].x = NULL;
2547 static int process_ipsec(struct pktgen_dev *pkt_dev,
2548 struct sk_buff *skb, __be16 protocol)
2550 if (pkt_dev->flags & F_IPSEC_ON) {
2551 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2556 nhead = x->props.header_len - skb_headroom(skb);
2558 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2560 pr_err("Error expanding ipsec packet %d\n",
2566 /* ipsec is not expecting ll header */
2567 skb_pull(skb, ETH_HLEN);
2568 ret = pktgen_output_ipsec(skb, pkt_dev);
2570 pr_err("Error creating ipsec packet %d\n", ret);
2574 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2575 memcpy(eth, pkt_dev->hh, 12);
2576 *(u16 *) ð[12] = protocol;
2586 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2589 for (i = 0; i < pkt_dev->nr_labels; i++)
2590 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2593 *mpls |= MPLS_STACK_BOTTOM;
2596 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2599 return htons(id | (cfi << 12) | (prio << 13));
2602 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2603 struct pktgen_dev *pkt_dev)
2605 struct sk_buff *skb = NULL;
2607 struct udphdr *udph;
2610 struct pktgen_hdr *pgh = NULL;
2611 __be16 protocol = htons(ETH_P_IP);
2613 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2614 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2615 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2616 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2619 if (pkt_dev->nr_labels)
2620 protocol = htons(ETH_P_MPLS_UC);
2622 if (pkt_dev->vlan_id != 0xffff)
2623 protocol = htons(ETH_P_8021Q);
2625 /* Update any of the values, used when we're incrementing various
2628 queue_map = pkt_dev->cur_queue_map;
2629 mod_cur_headers(pkt_dev);
2631 datalen = (odev->hard_header_len + 16) & ~0xf;
2633 if (pkt_dev->flags & F_NODE) {
2636 if (pkt_dev->node >= 0)
2637 node = pkt_dev->node;
2639 node = numa_node_id();
2641 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2642 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2644 skb_reserve(skb, NET_SKB_PAD);
2649 skb = __netdev_alloc_skb(odev,
2650 pkt_dev->cur_pkt_size + 64
2651 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2654 sprintf(pkt_dev->result, "No memory");
2658 skb_reserve(skb, datalen);
2660 /* Reserve for ethernet and IP header */
2661 eth = (__u8 *) skb_push(skb, 14);
2662 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2663 if (pkt_dev->nr_labels)
2664 mpls_push(mpls, pkt_dev);
2666 if (pkt_dev->vlan_id != 0xffff) {
2667 if (pkt_dev->svlan_id != 0xffff) {
2668 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2669 *svlan_tci = build_tci(pkt_dev->svlan_id,
2672 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2673 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2675 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2676 *vlan_tci = build_tci(pkt_dev->vlan_id,
2679 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2680 *vlan_encapsulated_proto = htons(ETH_P_IP);
2683 skb->network_header = skb->tail;
2684 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2685 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2686 skb_set_queue_mapping(skb, queue_map);
2688 udph = udp_hdr(skb);
2690 memcpy(eth, pkt_dev->hh, 12);
2691 *(__be16 *) & eth[12] = protocol;
2693 /* Eth + IPh + UDPh + mpls */
2694 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2695 pkt_dev->pkt_overhead;
2696 if (datalen < sizeof(struct pktgen_hdr))
2697 datalen = sizeof(struct pktgen_hdr);
2699 udph->source = htons(pkt_dev->cur_udp_src);
2700 udph->dest = htons(pkt_dev->cur_udp_dst);
2701 udph->len = htons(datalen + 8); /* DATA + udphdr */
2702 udph->check = 0; /* No checksum */
2707 iph->tos = pkt_dev->tos;
2708 iph->protocol = IPPROTO_UDP; /* UDP */
2709 iph->saddr = pkt_dev->cur_saddr;
2710 iph->daddr = pkt_dev->cur_daddr;
2711 iph->id = htons(pkt_dev->ip_id);
2714 iplen = 20 + 8 + datalen;
2715 iph->tot_len = htons(iplen);
2717 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2718 skb->protocol = protocol;
2719 skb->mac_header = (skb->network_header - ETH_HLEN -
2720 pkt_dev->pkt_overhead);
2722 skb->pkt_type = PACKET_HOST;
2724 if (pkt_dev->nfrags <= 0) {
2725 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2726 memset(pgh + 1, 0, datalen - sizeof(struct pktgen_hdr));
2728 int frags = pkt_dev->nfrags;
2731 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2733 if (frags > MAX_SKB_FRAGS)
2734 frags = MAX_SKB_FRAGS;
2735 if (datalen > frags * PAGE_SIZE) {
2736 len = datalen - frags * PAGE_SIZE;
2737 memset(skb_put(skb, len), 0, len);
2738 datalen = frags * PAGE_SIZE;
2742 while (datalen > 0) {
2743 struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
2744 skb_shinfo(skb)->frags[i].page = page;
2745 skb_shinfo(skb)->frags[i].page_offset = 0;
2746 skb_shinfo(skb)->frags[i].size =
2747 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2748 datalen -= skb_shinfo(skb)->frags[i].size;
2749 skb->len += skb_shinfo(skb)->frags[i].size;
2750 skb->data_len += skb_shinfo(skb)->frags[i].size;
2752 skb_shinfo(skb)->nr_frags = i;
2761 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2765 skb_shinfo(skb)->frags[i - 1].size -= rem;
2767 skb_shinfo(skb)->frags[i] =
2768 skb_shinfo(skb)->frags[i - 1];
2769 get_page(skb_shinfo(skb)->frags[i].page);
2770 skb_shinfo(skb)->frags[i].page =
2771 skb_shinfo(skb)->frags[i - 1].page;
2772 skb_shinfo(skb)->frags[i].page_offset +=
2773 skb_shinfo(skb)->frags[i - 1].size;
2774 skb_shinfo(skb)->frags[i].size = rem;
2776 skb_shinfo(skb)->nr_frags = i;
2780 /* Stamp the time, and sequence number,
2781 * convert them to network byte order
2784 struct timeval timestamp;
2786 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2787 pgh->seq_num = htonl(pkt_dev->seq_num);
2789 do_gettimeofday(×tamp);
2790 pgh->tv_sec = htonl(timestamp.tv_sec);
2791 pgh->tv_usec = htonl(timestamp.tv_usec);
2795 if (!process_ipsec(pkt_dev, skb, protocol))
2803 * scan_ip6, fmt_ip taken from dietlibc-0.21
2804 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2806 * Slightly modified for kernel.
2807 * Should be candidate for net/ipv4/utils.c
2811 static unsigned int scan_ip6(const char *s, char ip[16])
2814 unsigned int len = 0;
2817 unsigned int prefixlen = 0;
2818 unsigned int suffixlen = 0;
2822 for (i = 0; i < 16; i++)
2828 if (s[1] == ':') { /* Found "::", skip to part 2 */
2836 u = simple_strtoul(s, &pos, 16);
2840 if (prefixlen == 12 && s[i] == '.') {
2842 /* the last 4 bytes may be written as IPv4 address */
2845 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2848 ip[prefixlen++] = (u >> 8);
2849 ip[prefixlen++] = (u & 255);
2852 if (prefixlen == 16)
2856 /* part 2, after "::" */
2863 } else if (suffixlen != 0)
2866 u = simple_strtol(s, &pos, 16);
2873 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2875 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2881 suffix[suffixlen++] = (u >> 8);
2882 suffix[suffixlen++] = (u & 255);
2885 if (prefixlen + suffixlen == 16)
2888 for (i = 0; i < suffixlen; i++)
2889 ip[16 - suffixlen + i] = suffix[i];
2893 static char tohex(char hexdigit)
2895 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2898 static int fmt_xlong(char *s, unsigned int i)
2901 *s = tohex((i >> 12) & 0xf);
2902 if (s != bak || *s != '0')
2904 *s = tohex((i >> 8) & 0xf);
2905 if (s != bak || *s != '0')
2907 *s = tohex((i >> 4) & 0xf);
2908 if (s != bak || *s != '0')
2910 *s = tohex(i & 0xf);
2914 static unsigned int fmt_ip6(char *s, const char ip[16])
2919 unsigned int compressing;
2924 for (j = 0; j < 16; j += 2) {
2926 #ifdef V4MAPPEDPREFIX
2927 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2928 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2933 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2934 (unsigned long)(unsigned char)ip[j + 1];
2949 i = fmt_xlong(s, temp);
2966 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2967 struct pktgen_dev *pkt_dev)
2969 struct sk_buff *skb = NULL;
2971 struct udphdr *udph;
2973 struct ipv6hdr *iph;
2974 struct pktgen_hdr *pgh = NULL;
2975 __be16 protocol = htons(ETH_P_IPV6);
2977 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2978 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2979 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2980 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2983 if (pkt_dev->nr_labels)
2984 protocol = htons(ETH_P_MPLS_UC);
2986 if (pkt_dev->vlan_id != 0xffff)
2987 protocol = htons(ETH_P_8021Q);
2989 /* Update any of the values, used when we're incrementing various
2992 queue_map = pkt_dev->cur_queue_map;
2993 mod_cur_headers(pkt_dev);
2995 skb = __netdev_alloc_skb(odev,
2996 pkt_dev->cur_pkt_size + 64
2997 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
2999 sprintf(pkt_dev->result, "No memory");
3003 skb_reserve(skb, 16);
3005 /* Reserve for ethernet and IP header */
3006 eth = (__u8 *) skb_push(skb, 14);
3007 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
3008 if (pkt_dev->nr_labels)
3009 mpls_push(mpls, pkt_dev);
3011 if (pkt_dev->vlan_id != 0xffff) {
3012 if (pkt_dev->svlan_id != 0xffff) {
3013 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
3014 *svlan_tci = build_tci(pkt_dev->svlan_id,
3017 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
3018 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
3020 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
3021 *vlan_tci = build_tci(pkt_dev->vlan_id,
3024 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
3025 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
3028 skb->network_header = skb->tail;
3029 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
3030 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
3031 skb_set_queue_mapping(skb, queue_map);
3032 iph = ipv6_hdr(skb);
3033 udph = udp_hdr(skb);
3035 memcpy(eth, pkt_dev->hh, 12);
3036 *(__be16 *) ð[12] = protocol;
3038 /* Eth + IPh + UDPh + mpls */
3039 datalen = pkt_dev->cur_pkt_size - 14 -
3040 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
3041 pkt_dev->pkt_overhead;
3043 if (datalen < sizeof(struct pktgen_hdr)) {
3044 datalen = sizeof(struct pktgen_hdr);
3045 if (net_ratelimit())
3046 pr_info("increased datalen to %d\n", datalen);
3049 udph->source = htons(pkt_dev->cur_udp_src);
3050 udph->dest = htons(pkt_dev->cur_udp_dst);
3051 udph->len = htons(datalen + sizeof(struct udphdr));
3052 udph->check = 0; /* No checksum */
3054 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
3056 if (pkt_dev->traffic_class) {
3057 /* Version + traffic class + flow (0) */
3058 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
3061 iph->hop_limit = 32;
3063 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3064 iph->nexthdr = IPPROTO_UDP;
3066 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3067 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3069 skb->mac_header = (skb->network_header - ETH_HLEN -
3070 pkt_dev->pkt_overhead);
3071 skb->protocol = protocol;
3073 skb->pkt_type = PACKET_HOST;
3075 if (pkt_dev->nfrags <= 0)
3076 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
3078 int frags = pkt_dev->nfrags;
3081 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
3083 if (frags > MAX_SKB_FRAGS)
3084 frags = MAX_SKB_FRAGS;
3085 if (datalen > frags * PAGE_SIZE) {
3086 skb_put(skb, datalen - frags * PAGE_SIZE);
3087 datalen = frags * PAGE_SIZE;
3091 while (datalen > 0) {
3092 struct page *page = alloc_pages(GFP_KERNEL, 0);
3093 skb_shinfo(skb)->frags[i].page = page;
3094 skb_shinfo(skb)->frags[i].page_offset = 0;
3095 skb_shinfo(skb)->frags[i].size =
3096 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
3097 datalen -= skb_shinfo(skb)->frags[i].size;
3098 skb->len += skb_shinfo(skb)->frags[i].size;
3099 skb->data_len += skb_shinfo(skb)->frags[i].size;
3101 skb_shinfo(skb)->nr_frags = i;
3110 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3114 skb_shinfo(skb)->frags[i - 1].size -= rem;
3116 skb_shinfo(skb)->frags[i] =
3117 skb_shinfo(skb)->frags[i - 1];
3118 get_page(skb_shinfo(skb)->frags[i].page);
3119 skb_shinfo(skb)->frags[i].page =
3120 skb_shinfo(skb)->frags[i - 1].page;
3121 skb_shinfo(skb)->frags[i].page_offset +=
3122 skb_shinfo(skb)->frags[i - 1].size;
3123 skb_shinfo(skb)->frags[i].size = rem;
3125 skb_shinfo(skb)->nr_frags = i;
3129 /* Stamp the time, and sequence number,
3130 * convert them to network byte order
3131 * should we update cloned packets too ?
3134 struct timeval timestamp;
3136 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3137 pgh->seq_num = htonl(pkt_dev->seq_num);
3139 do_gettimeofday(×tamp);
3140 pgh->tv_sec = htonl(timestamp.tv_sec);
3141 pgh->tv_usec = htonl(timestamp.tv_usec);
3143 /* pkt_dev->seq_num++; FF: you really mean this? */
3148 static struct sk_buff *fill_packet(struct net_device *odev,
3149 struct pktgen_dev *pkt_dev)
3151 if (pkt_dev->flags & F_IPV6)
3152 return fill_packet_ipv6(odev, pkt_dev);
3154 return fill_packet_ipv4(odev, pkt_dev);
3157 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3159 pkt_dev->seq_num = 1;
3160 pkt_dev->idle_acc = 0;
3162 pkt_dev->tx_bytes = 0;
3163 pkt_dev->errors = 0;
3166 /* Set up structure for sending pkts, clear counters */
3168 static void pktgen_run(struct pktgen_thread *t)
3170 struct pktgen_dev *pkt_dev;
3176 list_for_each_entry(pkt_dev, &t->if_list, list) {
3179 * setup odev and create initial packet.
3181 pktgen_setup_inject(pkt_dev);
3183 if (pkt_dev->odev) {
3184 pktgen_clear_counters(pkt_dev);
3185 pkt_dev->running = 1; /* Cranke yeself! */
3186 pkt_dev->skb = NULL;
3187 pkt_dev->started_at =
3188 pkt_dev->next_tx = ktime_now();
3190 set_pkt_overhead(pkt_dev);
3192 strcpy(pkt_dev->result, "Starting");
3195 strcpy(pkt_dev->result, "Error starting");
3199 t->control &= ~(T_STOP);
3202 static void pktgen_stop_all_threads_ifs(void)
3204 struct pktgen_thread *t;
3208 mutex_lock(&pktgen_thread_lock);
3210 list_for_each_entry(t, &pktgen_threads, th_list)
3211 t->control |= T_STOP;
3213 mutex_unlock(&pktgen_thread_lock);
3216 static int thread_is_running(const struct pktgen_thread *t)
3218 const struct pktgen_dev *pkt_dev;
3220 list_for_each_entry(pkt_dev, &t->if_list, list)
3221 if (pkt_dev->running)
3226 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3230 while (thread_is_running(t)) {
3234 msleep_interruptible(100);
3236 if (signal_pending(current))
3246 static int pktgen_wait_all_threads_run(void)
3248 struct pktgen_thread *t;
3251 mutex_lock(&pktgen_thread_lock);
3253 list_for_each_entry(t, &pktgen_threads, th_list) {
3254 sig = pktgen_wait_thread_run(t);
3260 list_for_each_entry(t, &pktgen_threads, th_list)
3261 t->control |= (T_STOP);
3263 mutex_unlock(&pktgen_thread_lock);
3267 static void pktgen_run_all_threads(void)
3269 struct pktgen_thread *t;
3273 mutex_lock(&pktgen_thread_lock);
3275 list_for_each_entry(t, &pktgen_threads, th_list)
3276 t->control |= (T_RUN);
3278 mutex_unlock(&pktgen_thread_lock);
3280 /* Propagate thread->control */
3281 schedule_timeout_interruptible(msecs_to_jiffies(125));
3283 pktgen_wait_all_threads_run();
3286 static void pktgen_reset_all_threads(void)
3288 struct pktgen_thread *t;
3292 mutex_lock(&pktgen_thread_lock);
3294 list_for_each_entry(t, &pktgen_threads, th_list)
3295 t->control |= (T_REMDEVALL);
3297 mutex_unlock(&pktgen_thread_lock);
3299 /* Propagate thread->control */
3300 schedule_timeout_interruptible(msecs_to_jiffies(125));
3302 pktgen_wait_all_threads_run();
3305 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3307 __u64 bps, mbps, pps;
3308 char *p = pkt_dev->result;
3309 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3310 pkt_dev->started_at);
3311 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3313 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3314 (unsigned long long)ktime_to_us(elapsed),
3315 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3316 (unsigned long long)ktime_to_us(idle),
3317 (unsigned long long)pkt_dev->sofar,
3318 pkt_dev->cur_pkt_size, nr_frags);
3320 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3321 ktime_to_ns(elapsed));
3323 bps = pps * 8 * pkt_dev->cur_pkt_size;
3326 do_div(mbps, 1000000);
3327 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3328 (unsigned long long)pps,
3329 (unsigned long long)mbps,
3330 (unsigned long long)bps,
3331 (unsigned long long)pkt_dev->errors);
3334 /* Set stopped-at timer, remove from running list, do counters & statistics */
3335 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3337 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3339 if (!pkt_dev->running) {
3340 pr_warning("interface: %s is already stopped\n",
3345 kfree_skb(pkt_dev->skb);
3346 pkt_dev->skb = NULL;
3347 pkt_dev->stopped_at = ktime_now();
3348 pkt_dev->running = 0;
3350 show_results(pkt_dev, nr_frags);
3355 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3357 struct pktgen_dev *pkt_dev, *best = NULL;
3361 list_for_each_entry(pkt_dev, &t->if_list, list) {
3362 if (!pkt_dev->running)
3366 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
3373 static void pktgen_stop(struct pktgen_thread *t)
3375 struct pktgen_dev *pkt_dev;
3381 list_for_each_entry(pkt_dev, &t->if_list, list) {
3382 pktgen_stop_device(pkt_dev);
3389 * one of our devices needs to be removed - find it
3392 static void pktgen_rem_one_if(struct pktgen_thread *t)
3394 struct list_head *q, *n;
3395 struct pktgen_dev *cur;
3401 list_for_each_safe(q, n, &t->if_list) {
3402 cur = list_entry(q, struct pktgen_dev, list);
3404 if (!cur->removal_mark)
3407 kfree_skb(cur->skb);
3410 pktgen_remove_device(t, cur);
3418 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3420 struct list_head *q, *n;
3421 struct pktgen_dev *cur;
3425 /* Remove all devices, free mem */
3429 list_for_each_safe(q, n, &t->if_list) {
3430 cur = list_entry(q, struct pktgen_dev, list);
3432 kfree_skb(cur->skb);
3435 pktgen_remove_device(t, cur);
3441 static void pktgen_rem_thread(struct pktgen_thread *t)
3443 /* Remove from the thread list */
3445 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3447 mutex_lock(&pktgen_thread_lock);
3449 list_del(&t->th_list);
3451 mutex_unlock(&pktgen_thread_lock);
3454 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3456 ktime_t idle_start = ktime_now();
3458 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3461 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3463 ktime_t idle_start = ktime_now();
3465 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3466 if (signal_pending(current))
3470 pktgen_resched(pkt_dev);
3474 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3477 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3479 struct net_device *odev = pkt_dev->odev;
3480 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
3481 = odev->netdev_ops->ndo_start_xmit;
3482 struct netdev_queue *txq;
3486 /* If device is offline, then don't send */
3487 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3488 pktgen_stop_device(pkt_dev);
3492 /* This is max DELAY, this has special meaning of
3495 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3496 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3500 /* If no skb or clone count exhausted then get new one */
3501 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3502 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3503 /* build a new pkt */
3504 kfree_skb(pkt_dev->skb);
3506 pkt_dev->skb = fill_packet(odev, pkt_dev);
3507 if (pkt_dev->skb == NULL) {
3508 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
3510 pkt_dev->clone_count--; /* back out increment, OOM */
3513 pkt_dev->last_pkt_size = pkt_dev->skb->len;
3514 pkt_dev->allocated_skbs++;
3515 pkt_dev->clone_count = 0; /* reset counter */
3518 if (pkt_dev->delay && pkt_dev->last_ok)
3519 spin(pkt_dev, pkt_dev->next_tx);
3521 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3522 txq = netdev_get_tx_queue(odev, queue_map);
3524 __netif_tx_lock_bh(txq);
3526 if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq))) {
3527 ret = NETDEV_TX_BUSY;
3528 pkt_dev->last_ok = 0;
3531 atomic_inc(&(pkt_dev->skb->users));
3532 ret = (*xmit)(pkt_dev->skb, odev);
3536 txq_trans_update(txq);
3537 pkt_dev->last_ok = 1;
3540 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3544 case NET_XMIT_POLICED:
3545 /* skb has been consumed */
3548 default: /* Drivers are not supposed to return other values! */
3549 if (net_ratelimit())
3550 pr_info("pktgen: %s xmit error: %d\n",
3551 pkt_dev->odevname, ret);
3554 case NETDEV_TX_LOCKED:
3555 case NETDEV_TX_BUSY:
3556 /* Retry it next time */
3557 atomic_dec(&(pkt_dev->skb->users));
3558 pkt_dev->last_ok = 0;
3561 __netif_tx_unlock_bh(txq);
3563 /* If pkt_dev->count is zero, then run forever */
3564 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3565 pktgen_wait_for_skb(pkt_dev);
3567 /* Done with this */
3568 pktgen_stop_device(pkt_dev);
3573 * Main loop of the thread goes here
3576 static int pktgen_thread_worker(void *arg)
3579 struct pktgen_thread *t = arg;
3580 struct pktgen_dev *pkt_dev = NULL;
3583 BUG_ON(smp_processor_id() != cpu);
3585 init_waitqueue_head(&t->queue);
3586 complete(&t->start_done);
3588 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3590 set_current_state(TASK_INTERRUPTIBLE);
3594 while (!kthread_should_stop()) {
3595 pkt_dev = next_to_run(t);
3597 if (unlikely(!pkt_dev && t->control == 0)) {
3598 wait_event_interruptible_timeout(t->queue,
3605 __set_current_state(TASK_RUNNING);
3607 if (likely(pkt_dev)) {
3608 pktgen_xmit(pkt_dev);
3611 pktgen_resched(pkt_dev);
3616 if (t->control & T_STOP) {
3618 t->control &= ~(T_STOP);
3621 if (t->control & T_RUN) {
3623 t->control &= ~(T_RUN);
3626 if (t->control & T_REMDEVALL) {
3627 pktgen_rem_all_ifs(t);
3628 t->control &= ~(T_REMDEVALL);
3631 if (t->control & T_REMDEV) {
3632 pktgen_rem_one_if(t);
3633 t->control &= ~(T_REMDEV);
3638 set_current_state(TASK_INTERRUPTIBLE);
3641 pr_debug("%s stopping all device\n", t->tsk->comm);
3644 pr_debug("%s removing all device\n", t->tsk->comm);
3645 pktgen_rem_all_ifs(t);
3647 pr_debug("%s removing thread\n", t->tsk->comm);
3648 pktgen_rem_thread(t);
3653 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3654 const char *ifname, bool exact)
3656 struct pktgen_dev *p, *pkt_dev = NULL;
3657 size_t len = strlen(ifname);
3660 list_for_each_entry(p, &t->if_list, list)
3661 if (strncmp(p->odevname, ifname, len) == 0) {
3662 if (p->odevname[len]) {
3663 if (exact || p->odevname[len] != '@')
3671 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
3676 * Adds a dev at front of if_list.
3679 static int add_dev_to_thread(struct pktgen_thread *t,
3680 struct pktgen_dev *pkt_dev)
3686 if (pkt_dev->pg_thread) {
3687 pr_err("ERROR: already assigned to a thread\n");
3692 list_add(&pkt_dev->list, &t->if_list);
3693 pkt_dev->pg_thread = t;
3694 pkt_dev->running = 0;
3701 /* Called under thread lock */
3703 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3705 struct pktgen_dev *pkt_dev;
3707 int node = cpu_to_node(t->cpu);
3709 /* We don't allow a device to be on several threads */
3711 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3713 pr_err("ERROR: interface already used\n");
3717 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3721 strcpy(pkt_dev->odevname, ifname);
3722 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3724 if (pkt_dev->flows == NULL) {
3728 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3730 pkt_dev->removal_mark = 0;
3731 pkt_dev->min_pkt_size = ETH_ZLEN;
3732 pkt_dev->max_pkt_size = ETH_ZLEN;
3733 pkt_dev->nfrags = 0;
3734 pkt_dev->clone_skb = pg_clone_skb_d;
3735 pkt_dev->delay = pg_delay_d;
3736 pkt_dev->count = pg_count_d;
3738 pkt_dev->udp_src_min = 9; /* sink port */
3739 pkt_dev->udp_src_max = 9;
3740 pkt_dev->udp_dst_min = 9;
3741 pkt_dev->udp_dst_max = 9;
3743 pkt_dev->vlan_p = 0;
3744 pkt_dev->vlan_cfi = 0;
3745 pkt_dev->vlan_id = 0xffff;
3746 pkt_dev->svlan_p = 0;
3747 pkt_dev->svlan_cfi = 0;
3748 pkt_dev->svlan_id = 0xffff;
3751 err = pktgen_setup_dev(pkt_dev, ifname);
3755 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3756 &pktgen_if_fops, pkt_dev);
3757 if (!pkt_dev->entry) {
3758 pr_err("cannot create %s/%s procfs entry\n",
3759 PG_PROC_DIR, ifname);
3764 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3765 pkt_dev->ipsproto = IPPROTO_ESP;
3768 return add_dev_to_thread(t, pkt_dev);
3770 dev_put(pkt_dev->odev);
3775 vfree(pkt_dev->flows);
3780 static int __init pktgen_create_thread(int cpu)
3782 struct pktgen_thread *t;
3783 struct proc_dir_entry *pe;
3784 struct task_struct *p;
3786 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3789 pr_err("ERROR: out of memory, can't create new thread\n");
3793 spin_lock_init(&t->if_lock);
3796 INIT_LIST_HEAD(&t->if_list);
3798 list_add_tail(&t->th_list, &pktgen_threads);
3799 init_completion(&t->start_done);
3801 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3803 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3804 list_del(&t->th_list);
3808 kthread_bind(p, cpu);
3811 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3812 &pktgen_thread_fops, t);
3814 pr_err("cannot create %s/%s procfs entry\n",
3815 PG_PROC_DIR, t->tsk->comm);
3817 list_del(&t->th_list);
3823 wait_for_completion(&t->start_done);
3829 * Removes a device from the thread if_list.
3831 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3832 struct pktgen_dev *pkt_dev)
3834 struct list_head *q, *n;
3835 struct pktgen_dev *p;
3837 list_for_each_safe(q, n, &t->if_list) {
3838 p = list_entry(q, struct pktgen_dev, list);
3844 static int pktgen_remove_device(struct pktgen_thread *t,
3845 struct pktgen_dev *pkt_dev)
3848 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
3850 if (pkt_dev->running) {
3851 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
3852 pktgen_stop_device(pkt_dev);
3855 /* Dis-associate from the interface */
3857 if (pkt_dev->odev) {
3858 dev_put(pkt_dev->odev);
3859 pkt_dev->odev = NULL;
3862 /* And update the thread if_list */
3864 _rem_dev_from_if_list(t, pkt_dev);
3867 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3872 vfree(pkt_dev->flows);
3877 static int __init pg_init(void)
3880 struct proc_dir_entry *pe;
3882 pr_info("%s", version);
3884 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3888 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3890 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
3891 proc_net_remove(&init_net, PG_PROC_DIR);
3895 /* Register us to receive netdevice events */
3896 register_netdevice_notifier(&pktgen_notifier_block);
3898 for_each_online_cpu(cpu) {
3901 err = pktgen_create_thread(cpu);
3903 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3907 if (list_empty(&pktgen_threads)) {
3908 pr_err("ERROR: Initialization failed for all threads\n");
3909 unregister_netdevice_notifier(&pktgen_notifier_block);
3910 remove_proc_entry(PGCTRL, pg_proc_dir);
3911 proc_net_remove(&init_net, PG_PROC_DIR);
3918 static void __exit pg_cleanup(void)
3920 struct pktgen_thread *t;
3921 struct list_head *q, *n;
3922 wait_queue_head_t queue;
3923 init_waitqueue_head(&queue);
3925 /* Stop all interfaces & threads */
3927 list_for_each_safe(q, n, &pktgen_threads) {
3928 t = list_entry(q, struct pktgen_thread, th_list);
3929 kthread_stop(t->tsk);
3933 /* Un-register us from receiving netdevice events */
3934 unregister_netdevice_notifier(&pktgen_notifier_block);
3936 /* Clean up proc file system */
3937 remove_proc_entry(PGCTRL, pg_proc_dir);
3938 proc_net_remove(&init_net, PG_PROC_DIR);
3941 module_init(pg_init);
3942 module_exit(pg_cleanup);
3944 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3945 MODULE_DESCRIPTION("Packet Generator tool");
3946 MODULE_LICENSE("GPL");
3947 MODULE_VERSION(VERSION);
3948 module_param(pg_count_d, int, 0);
3949 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
3950 module_param(pg_delay_d, int, 0);
3951 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
3952 module_param(pg_clone_skb_d, int, 0);
3953 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
3954 module_param(debug, int, 0);
3955 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");