1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
5 * Uppsala University and
6 * Swedish University of Agricultural Sciences
8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * Ben Greear <greearb@candelatech.com>
10 * Jens Låås <jens.laas@data.slu.se>
12 * A tool for loading the network with preconfigurated packets.
13 * The tool is implemented as a linux module. Parameters are output
14 * device, delay (to hard_xmit), number of packets, and whether
15 * to use multiple SKBs or just the same one.
16 * pktgen uses the installed interface's output routine.
18 * Additional hacking by:
20 * Jens.Laas@data.slu.se
21 * Improved by ANK. 010120.
22 * Improved by ANK even more. 010212.
23 * MAC address typo fixed. 010417 --ro
24 * Integrated. 020301 --DaveM
25 * Added multiskb option 020301 --DaveM
26 * Scaling of results. 020417--sigurdur@linpro.no
27 * Significant re-work of the module:
28 * * Convert to threaded model to more efficiently be able to transmit
29 * and receive on multiple interfaces at once.
30 * * Converted many counters to __u64 to allow longer runs.
31 * * Allow configuration of ranges, like min/max IP address, MACs,
32 * and UDP-ports, for both source and destination, and can
33 * set to use a random distribution or sequentially walk the range.
34 * * Can now change most values after starting.
35 * * Place 12-byte packet in UDP payload with magic number,
36 * sequence number, and timestamp.
37 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
38 * latencies (with micro-second) precision.
39 * * Add IOCTL interface to easily get counters & configuration.
40 * --Ben Greear <greearb@candelatech.com>
42 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
43 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
44 * as a "fastpath" with a configurable number of clones after alloc's.
45 * clone_skb=0 means all packets are allocated this also means ranges time
46 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
49 * Also moved to /proc/net/pktgen/
52 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
53 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
54 * --Ben Greear <greearb@candelatech.com>
56 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
58 * 021124 Finished major redesign and rewrite for new functionality.
59 * See Documentation/networking/pktgen.rst for how to use this.
62 * For each CPU one thread/process is created at start. This process checks
63 * for running devices in the if_list and sends packets until count is 0 it
64 * also the thread checks the thread->control which is used for inter-process
65 * communication. controlling process "posts" operations to the threads this
67 * The if_list is RCU protected, and the if_lock remains to protect updating
68 * of if_list, from "add_device" as it invoked from userspace (via proc write).
70 * By design there should only be *one* "controlling" process. In practice
71 * multiple write accesses gives unpredictable result. Understood by "write"
72 * to /proc gives result code thats should be read be the "writer".
73 * For practical use this should be no problem.
75 * Note when adding devices to a specific CPU there good idea to also assign
76 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
79 * Fix refcount off by one if first packet fails, potential null deref,
82 * First "ranges" functionality for ipv6 030726 --ro
84 * Included flow support. 030802 ANK.
86 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
88 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
89 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
91 * New xmit() return, do_div and misc clean up by Stephen Hemminger
92 * <shemminger@osdl.org> 040923
94 * Randy Dunlap fixed u64 printk compiler warning
96 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
97 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
99 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
100 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
102 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
105 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
107 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
109 * Fixed src_mac command to set source mac of packet to value specified in
110 * command by Adit Ranadive <adit.262@gmail.com>
113 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
115 #include <linux/sys.h>
116 #include <linux/types.h>
117 #include <linux/module.h>
118 #include <linux/moduleparam.h>
119 #include <linux/kernel.h>
120 #include <linux/mutex.h>
121 #include <linux/sched.h>
122 #include <linux/slab.h>
123 #include <linux/vmalloc.h>
124 #include <linux/unistd.h>
125 #include <linux/string.h>
126 #include <linux/ptrace.h>
127 #include <linux/errno.h>
128 #include <linux/ioport.h>
129 #include <linux/interrupt.h>
130 #include <linux/capability.h>
131 #include <linux/hrtimer.h>
132 #include <linux/freezer.h>
133 #include <linux/delay.h>
134 #include <linux/timer.h>
135 #include <linux/list.h>
136 #include <linux/init.h>
137 #include <linux/skbuff.h>
138 #include <linux/netdevice.h>
139 #include <linux/inet.h>
140 #include <linux/inetdevice.h>
141 #include <linux/rtnetlink.h>
142 #include <linux/if_arp.h>
143 #include <linux/if_vlan.h>
144 #include <linux/in.h>
145 #include <linux/ip.h>
146 #include <linux/ipv6.h>
147 #include <linux/udp.h>
148 #include <linux/proc_fs.h>
149 #include <linux/seq_file.h>
150 #include <linux/wait.h>
151 #include <linux/etherdevice.h>
152 #include <linux/kthread.h>
153 #include <linux/prefetch.h>
154 #include <linux/mmzone.h>
155 #include <net/net_namespace.h>
156 #include <net/checksum.h>
157 #include <net/ipv6.h>
159 #include <net/ip6_checksum.h>
160 #include <net/addrconf.h>
162 #include <net/xfrm.h>
164 #include <net/netns/generic.h>
165 #include <asm/byteorder.h>
166 #include <linux/rcupdate.h>
167 #include <linux/bitops.h>
168 #include <linux/io.h>
169 #include <linux/timex.h>
170 #include <linux/uaccess.h>
172 #include <asm/div64.h> /* do_div */
174 #define VERSION "2.75"
175 #define IP_NAME_SZ 32
176 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
177 #define MPLS_STACK_BOTTOM htonl(0x00000100)
178 /* Max number of internet mix entries that can be specified in imix_weights. */
179 #define MAX_IMIX_ENTRIES 20
180 #define IMIX_PRECISION 100 /* Precision of IMIX distribution */
182 #define func_enter() pr_debug("entering %s\n", __func__);
185 pf(IPV6) /* Interface in IPV6 Mode */ \
186 pf(IPSRC_RND) /* IP-Src Random */ \
187 pf(IPDST_RND) /* IP-Dst Random */ \
188 pf(TXSIZE_RND) /* Transmit size is random */ \
189 pf(UDPSRC_RND) /* UDP-Src Random */ \
190 pf(UDPDST_RND) /* UDP-Dst Random */ \
191 pf(UDPCSUM) /* Include UDP checksum */ \
192 pf(NO_TIMESTAMP) /* Don't timestamp packets (default TS) */ \
193 pf(MPLS_RND) /* Random MPLS labels */ \
194 pf(QUEUE_MAP_RND) /* queue map Random */ \
195 pf(QUEUE_MAP_CPU) /* queue map mirrors smp_processor_id() */ \
196 pf(FLOW_SEQ) /* Sequential flows */ \
197 pf(IPSEC) /* ipsec on for flows */ \
198 pf(MACSRC_RND) /* MAC-Src Random */ \
199 pf(MACDST_RND) /* MAC-Dst Random */ \
200 pf(VID_RND) /* Random VLAN ID */ \
201 pf(SVID_RND) /* Random SVLAN ID */ \
202 pf(NODE) /* Node memory alloc*/ \
204 #define pf(flag) flag##_SHIFT,
210 /* Device flag bits */
211 #define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
215 #define pf(flag) __stringify(flag),
216 static char *pkt_flag_names[] = {
221 #define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
223 /* Thread control flag bits */
224 #define T_STOP (1<<0) /* Stop run */
225 #define T_RUN (1<<1) /* Start run */
226 #define T_REMDEVALL (1<<2) /* Remove all devs */
227 #define T_REMDEV (1<<3) /* Remove one dev */
230 #define M_START_XMIT 0 /* Default normal TX */
231 #define M_NETIF_RECEIVE 1 /* Inject packets into stack */
232 #define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
234 /* If lock -- protects updating of if_list */
235 #define if_lock(t) mutex_lock(&(t->if_lock));
236 #define if_unlock(t) mutex_unlock(&(t->if_lock));
238 /* Used to help with determining the pkts on receive */
239 #define PKTGEN_MAGIC 0xbe9be955
240 #define PG_PROC_DIR "pktgen"
241 #define PGCTRL "pgctrl"
243 #define MAX_CFLOWS 65536
245 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
246 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
258 struct xfrm_state *x;
264 #define F_INIT (1<<0) /* flow has been initialized */
268 * Try to keep frequent/infrequent used vars. separated.
270 struct proc_dir_entry *entry; /* proc file */
271 struct pktgen_thread *pg_thread;/* the owner */
272 struct list_head list; /* chaining in the thread's run-queue */
273 struct rcu_head rcu; /* freed by RCU */
275 int running; /* if false, the test will stop */
277 /* If min != max, then we will either do a linear iteration, or
278 * we will do a random selection from within the range.
284 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
286 int removal_mark; /* non-zero => the device is marked for
287 * removal by worker thread */
290 u64 delay; /* nano-seconds */
292 __u64 count; /* Default No packets to send */
293 __u64 sofar; /* How many pkts we've sent so far */
294 __u64 tx_bytes; /* How many bytes we've transmitted */
295 __u64 errors; /* Errors when trying to transmit, */
297 /* runtime counters relating to clone_skb */
300 int last_ok; /* Was last skb sent?
301 * Or a failed transmit of some sort?
302 * This will keep sequence numbers in order
307 u64 idle_acc; /* nano-seconds */
312 * Use multiple SKBs during packet gen.
313 * If this number is greater than 1, then
314 * that many copies of the same packet will be
315 * sent before a new packet is allocated.
316 * If you want to send 1024 identical packets
317 * before creating a new packet,
318 * set clone_skb to 1024.
321 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
322 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
323 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
324 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
326 struct in6_addr in6_saddr;
327 struct in6_addr in6_daddr;
328 struct in6_addr cur_in6_daddr;
329 struct in6_addr cur_in6_saddr;
331 struct in6_addr min_in6_daddr;
332 struct in6_addr max_in6_daddr;
333 struct in6_addr min_in6_saddr;
334 struct in6_addr max_in6_saddr;
336 /* If we're doing ranges, random or incremental, then this
337 * defines the min/max for those ranges.
339 __be32 saddr_min; /* inclusive, source IP address */
340 __be32 saddr_max; /* exclusive, source IP address */
341 __be32 daddr_min; /* inclusive, dest IP address */
342 __be32 daddr_max; /* exclusive, dest IP address */
344 __u16 udp_src_min; /* inclusive, source UDP port */
345 __u16 udp_src_max; /* exclusive, source UDP port */
346 __u16 udp_dst_min; /* inclusive, dest UDP port */
347 __u16 udp_dst_max; /* exclusive, dest UDP port */
350 __u8 tos; /* six MSB of (former) IPv4 TOS
351 are for dscp codepoint */
352 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
353 (see RFC 3260, sec. 4) */
356 unsigned int n_imix_entries;
357 struct imix_pkt imix_entries[MAX_IMIX_ENTRIES];
358 /* Maps 0-IMIX_PRECISION range to imix_entry based on probability*/
359 __u8 imix_distribution[IMIX_PRECISION];
362 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
363 __be32 labels[MAX_MPLS_LABELS];
365 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
368 __u16 vlan_id; /* 0xffff means no vlan tag */
372 __u16 svlan_id; /* 0xffff means no svlan tag */
374 __u32 src_mac_count; /* How many MACs to iterate through */
375 __u32 dst_mac_count; /* How many MACs to iterate through */
377 unsigned char dst_mac[ETH_ALEN];
378 unsigned char src_mac[ETH_ALEN];
380 __u32 cur_dst_mac_offset;
381 __u32 cur_src_mac_offset;
393 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
395 We fill in SRC address later
396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
400 __u16 pad; /* pad out the hh struct to an even 16 bytes */
402 struct sk_buff *skb; /* skb we are to transmit next, used for when we
403 * are transmitting the same one multiple times
405 struct net_device *odev; /* The out-going device.
406 * Note that the device should have it's
407 * pg_info pointer pointing back to this
409 * Set when the user specifies the out-going
410 * device name (not when the inject is
411 * started as it used to do.)
414 struct flow_state *flows;
415 unsigned int cflows; /* Concurrent flows (config) */
416 unsigned int lflow; /* Flow length (config) */
417 unsigned int nflows; /* accumulated flows (stats) */
418 unsigned int curfl; /* current sequenced flow (state)*/
422 __u32 skb_priority; /* skb priority field */
423 unsigned int burst; /* number of duplicated packets to burst */
424 int node; /* Memory node */
427 __u8 ipsmode; /* IPSEC mode (config) */
428 __u8 ipsproto; /* IPSEC type (config) */
430 struct xfrm_dst xdst;
431 struct dst_ops dstops;
444 static unsigned int pg_net_id __read_mostly;
448 struct proc_dir_entry *proc_dir;
449 struct list_head pktgen_threads;
453 struct pktgen_thread {
454 struct mutex if_lock; /* for list of devices */
455 struct list_head if_list; /* All device here */
456 struct list_head th_list;
457 struct task_struct *tsk;
460 /* Field for thread to receive "posted" events terminate,
466 wait_queue_head_t queue;
467 struct completion start_done;
468 struct pktgen_net *net;
474 static const char version[] =
475 "Packet Generator for packet performance testing. "
476 "Version: " VERSION "\n";
478 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
479 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
480 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
481 const char *ifname, bool exact);
482 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
483 static void pktgen_run_all_threads(struct pktgen_net *pn);
484 static void pktgen_reset_all_threads(struct pktgen_net *pn);
485 static void pktgen_stop_all_threads(struct pktgen_net *pn);
487 static void pktgen_stop(struct pktgen_thread *t);
488 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
489 static void fill_imix_distribution(struct pktgen_dev *pkt_dev);
491 /* Module parameters, defaults. */
492 static int pg_count_d __read_mostly = 1000;
493 static int pg_delay_d __read_mostly;
494 static int pg_clone_skb_d __read_mostly;
495 static int debug __read_mostly;
497 static DEFINE_MUTEX(pktgen_thread_lock);
499 static struct notifier_block pktgen_notifier_block = {
500 .notifier_call = pktgen_device_event,
504 * /proc handling functions
508 static int pgctrl_show(struct seq_file *seq, void *v)
510 seq_puts(seq, version);
514 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
515 size_t count, loff_t *ppos)
518 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
520 if (!capable(CAP_NET_ADMIN))
526 if (count > sizeof(data))
527 count = sizeof(data);
529 if (copy_from_user(data, buf, count))
532 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
534 if (!strcmp(data, "stop"))
535 pktgen_stop_all_threads(pn);
536 else if (!strcmp(data, "start"))
537 pktgen_run_all_threads(pn);
538 else if (!strcmp(data, "reset"))
539 pktgen_reset_all_threads(pn);
546 static int pgctrl_open(struct inode *inode, struct file *file)
548 return single_open(file, pgctrl_show, PDE_DATA(inode));
551 static const struct proc_ops pktgen_proc_ops = {
552 .proc_open = pgctrl_open,
553 .proc_read = seq_read,
554 .proc_lseek = seq_lseek,
555 .proc_write = pgctrl_write,
556 .proc_release = single_release,
559 static int pktgen_if_show(struct seq_file *seq, void *v)
561 const struct pktgen_dev *pkt_dev = seq->private;
567 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
568 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
569 pkt_dev->max_pkt_size);
571 if (pkt_dev->n_imix_entries > 0) {
572 seq_puts(seq, " imix_weights: ");
573 for (i = 0; i < pkt_dev->n_imix_entries; i++) {
574 seq_printf(seq, "%llu,%llu ",
575 pkt_dev->imix_entries[i].size,
576 pkt_dev->imix_entries[i].weight);
582 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
583 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
584 pkt_dev->clone_skb, pkt_dev->odevname);
586 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
590 " queue_map_min: %u queue_map_max: %u\n",
591 pkt_dev->queue_map_min,
592 pkt_dev->queue_map_max);
594 if (pkt_dev->skb_priority)
595 seq_printf(seq, " skb_priority: %u\n",
596 pkt_dev->skb_priority);
598 if (pkt_dev->flags & F_IPV6) {
600 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
601 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
603 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
605 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
608 " dst_min: %s dst_max: %s\n",
609 pkt_dev->dst_min, pkt_dev->dst_max);
611 " src_min: %s src_max: %s\n",
612 pkt_dev->src_min, pkt_dev->src_max);
615 seq_puts(seq, " src_mac: ");
617 seq_printf(seq, "%pM ",
618 is_zero_ether_addr(pkt_dev->src_mac) ?
619 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
621 seq_puts(seq, "dst_mac: ");
622 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
625 " udp_src_min: %d udp_src_max: %d"
626 " udp_dst_min: %d udp_dst_max: %d\n",
627 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
628 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
631 " src_mac_count: %d dst_mac_count: %d\n",
632 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
634 if (pkt_dev->nr_labels) {
635 seq_puts(seq, " mpls: ");
636 for (i = 0; i < pkt_dev->nr_labels; i++)
637 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
638 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
641 if (pkt_dev->vlan_id != 0xffff)
642 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
643 pkt_dev->vlan_id, pkt_dev->vlan_p,
646 if (pkt_dev->svlan_id != 0xffff)
647 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
648 pkt_dev->svlan_id, pkt_dev->svlan_p,
652 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
654 if (pkt_dev->traffic_class)
655 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
657 if (pkt_dev->burst > 1)
658 seq_printf(seq, " burst: %d\n", pkt_dev->burst);
660 if (pkt_dev->node >= 0)
661 seq_printf(seq, " node: %d\n", pkt_dev->node);
663 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
664 seq_puts(seq, " xmit_mode: netif_receive\n");
665 else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
666 seq_puts(seq, " xmit_mode: xmit_queue\n");
668 seq_puts(seq, " Flags: ");
670 for (i = 0; i < NR_PKT_FLAGS; i++) {
672 if (!pkt_dev->cflows)
675 if (pkt_dev->flags & (1 << i))
676 seq_printf(seq, "%s ", pkt_flag_names[i]);
677 else if (i == F_FLOW_SEQ)
678 seq_puts(seq, "FLOW_RND ");
681 if (i == F_IPSEC && pkt_dev->spi)
682 seq_printf(seq, "spi:%u", pkt_dev->spi);
688 /* not really stopped, more like last-running-at */
689 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
690 idle = pkt_dev->idle_acc;
691 do_div(idle, NSEC_PER_USEC);
694 "Current:\n pkts-sofar: %llu errors: %llu\n",
695 (unsigned long long)pkt_dev->sofar,
696 (unsigned long long)pkt_dev->errors);
698 if (pkt_dev->n_imix_entries > 0) {
701 seq_puts(seq, " imix_size_counts: ");
702 for (i = 0; i < pkt_dev->n_imix_entries; i++) {
703 seq_printf(seq, "%llu,%llu ",
704 pkt_dev->imix_entries[i].size,
705 pkt_dev->imix_entries[i].count_so_far);
711 " started: %lluus stopped: %lluus idle: %lluus\n",
712 (unsigned long long) ktime_to_us(pkt_dev->started_at),
713 (unsigned long long) ktime_to_us(stopped),
714 (unsigned long long) idle);
717 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
718 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
719 pkt_dev->cur_src_mac_offset);
721 if (pkt_dev->flags & F_IPV6) {
722 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
723 &pkt_dev->cur_in6_saddr,
724 &pkt_dev->cur_in6_daddr);
726 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
727 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
729 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
730 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
732 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
734 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
736 if (pkt_dev->result[0])
737 seq_printf(seq, "Result: %s\n", pkt_dev->result);
739 seq_puts(seq, "Result: Idle\n");
745 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
751 for (; i < maxlen; i++) {
755 if (get_user(c, &user_buffer[i]))
757 value = hex_to_bin(c);
766 static int count_trail_chars(const char __user * user_buffer,
771 for (i = 0; i < maxlen; i++) {
773 if (get_user(c, &user_buffer[i]))
791 static long num_arg(const char __user *user_buffer, unsigned long maxlen,
797 for (i = 0; i < maxlen; i++) {
799 if (get_user(c, &user_buffer[i]))
801 if ((c >= '0') && (c <= '9')) {
810 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
814 for (i = 0; i < maxlen; i++) {
816 if (get_user(c, &user_buffer[i]))
833 /* Parses imix entries from user buffer.
834 * The user buffer should consist of imix entries separated by spaces
835 * where each entry consists of size and weight delimited by commas.
836 * "size1,weight_1 size2,weight_2 ... size_n,weight_n" for example.
838 static ssize_t get_imix_entries(const char __user *buffer,
839 struct pktgen_dev *pkt_dev)
841 const int max_digits = 10;
846 pkt_dev->n_imix_entries = 0;
849 unsigned long weight;
852 len = num_arg(&buffer[i], max_digits, &size);
856 if (get_user(c, &buffer[i]))
858 /* Check for comma between size_i and weight_i */
863 if (size < 14 + 20 + 8)
866 len = num_arg(&buffer[i], max_digits, &weight);
872 pkt_dev->imix_entries[pkt_dev->n_imix_entries].size = size;
873 pkt_dev->imix_entries[pkt_dev->n_imix_entries].weight = weight;
876 if (get_user(c, &buffer[i]))
880 pkt_dev->n_imix_entries++;
882 if (pkt_dev->n_imix_entries > MAX_IMIX_ENTRIES)
889 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
896 pkt_dev->nr_labels = 0;
899 len = hex32_arg(&buffer[i], 8, &tmp);
902 pkt_dev->labels[n] = htonl(tmp);
903 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
904 pkt_dev->flags |= F_MPLS_RND;
906 if (get_user(c, &buffer[i]))
910 if (n >= MAX_MPLS_LABELS)
914 pkt_dev->nr_labels = n;
918 static __u32 pktgen_read_flag(const char *f, bool *disable)
927 for (i = 0; i < NR_PKT_FLAGS; i++) {
928 if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
931 /* allow only disabling ipv6 flag */
932 if (!*disable && i == IPV6_SHIFT)
935 if (strcmp(f, pkt_flag_names[i]) == 0)
939 if (strcmp(f, "FLOW_RND") == 0) {
940 *disable = !*disable;
947 static ssize_t pktgen_if_write(struct file *file,
948 const char __user * user_buffer, size_t count,
951 struct seq_file *seq = file->private_data;
952 struct pktgen_dev *pkt_dev = seq->private;
954 char name[16], valstr[32];
955 unsigned long value = 0;
956 char *pg_result = NULL;
960 pg_result = &(pkt_dev->result[0]);
963 pr_warn("wrong command format\n");
968 tmp = count_trail_chars(user_buffer, max);
970 pr_warn("illegal format\n");
975 /* Read variable name */
977 len = strn_len(&user_buffer[i], sizeof(name) - 1);
981 memset(name, 0, sizeof(name));
982 if (copy_from_user(name, &user_buffer[i], len))
987 len = count_trail_chars(&user_buffer[i], max);
994 size_t copy = min_t(size_t, count + 1, 1024);
995 char *tp = strndup_user(user_buffer, copy);
1000 pr_debug("%s,%zu buffer -:%s:-\n", name, count, tp);
1004 if (!strcmp(name, "min_pkt_size")) {
1005 len = num_arg(&user_buffer[i], 10, &value);
1010 if (value < 14 + 20 + 8)
1011 value = 14 + 20 + 8;
1012 if (value != pkt_dev->min_pkt_size) {
1013 pkt_dev->min_pkt_size = value;
1014 pkt_dev->cur_pkt_size = value;
1016 sprintf(pg_result, "OK: min_pkt_size=%d",
1017 pkt_dev->min_pkt_size);
1021 if (!strcmp(name, "max_pkt_size")) {
1022 len = num_arg(&user_buffer[i], 10, &value);
1027 if (value < 14 + 20 + 8)
1028 value = 14 + 20 + 8;
1029 if (value != pkt_dev->max_pkt_size) {
1030 pkt_dev->max_pkt_size = value;
1031 pkt_dev->cur_pkt_size = value;
1033 sprintf(pg_result, "OK: max_pkt_size=%d",
1034 pkt_dev->max_pkt_size);
1038 /* Shortcut for min = max */
1040 if (!strcmp(name, "pkt_size")) {
1041 len = num_arg(&user_buffer[i], 10, &value);
1046 if (value < 14 + 20 + 8)
1047 value = 14 + 20 + 8;
1048 if (value != pkt_dev->min_pkt_size) {
1049 pkt_dev->min_pkt_size = value;
1050 pkt_dev->max_pkt_size = value;
1051 pkt_dev->cur_pkt_size = value;
1053 sprintf(pg_result, "OK: pkt_size=%d", pkt_dev->min_pkt_size);
1057 if (!strcmp(name, "imix_weights")) {
1058 if (pkt_dev->clone_skb > 0)
1061 len = get_imix_entries(&user_buffer[i], pkt_dev);
1065 fill_imix_distribution(pkt_dev);
1071 if (!strcmp(name, "debug")) {
1072 len = num_arg(&user_buffer[i], 10, &value);
1078 sprintf(pg_result, "OK: debug=%u", debug);
1082 if (!strcmp(name, "frags")) {
1083 len = num_arg(&user_buffer[i], 10, &value);
1088 pkt_dev->nfrags = value;
1089 sprintf(pg_result, "OK: frags=%d", pkt_dev->nfrags);
1092 if (!strcmp(name, "delay")) {
1093 len = num_arg(&user_buffer[i], 10, &value);
1098 if (value == 0x7FFFFFFF)
1099 pkt_dev->delay = ULLONG_MAX;
1101 pkt_dev->delay = (u64)value;
1103 sprintf(pg_result, "OK: delay=%llu",
1104 (unsigned long long) pkt_dev->delay);
1107 if (!strcmp(name, "rate")) {
1108 len = num_arg(&user_buffer[i], 10, &value);
1115 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1117 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1119 sprintf(pg_result, "OK: rate=%lu", value);
1122 if (!strcmp(name, "ratep")) {
1123 len = num_arg(&user_buffer[i], 10, &value);
1130 pkt_dev->delay = NSEC_PER_SEC/value;
1132 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1134 sprintf(pg_result, "OK: rate=%lu", value);
1137 if (!strcmp(name, "udp_src_min")) {
1138 len = num_arg(&user_buffer[i], 10, &value);
1143 if (value != pkt_dev->udp_src_min) {
1144 pkt_dev->udp_src_min = value;
1145 pkt_dev->cur_udp_src = value;
1147 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1150 if (!strcmp(name, "udp_dst_min")) {
1151 len = num_arg(&user_buffer[i], 10, &value);
1156 if (value != pkt_dev->udp_dst_min) {
1157 pkt_dev->udp_dst_min = value;
1158 pkt_dev->cur_udp_dst = value;
1160 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1163 if (!strcmp(name, "udp_src_max")) {
1164 len = num_arg(&user_buffer[i], 10, &value);
1169 if (value != pkt_dev->udp_src_max) {
1170 pkt_dev->udp_src_max = value;
1171 pkt_dev->cur_udp_src = value;
1173 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1176 if (!strcmp(name, "udp_dst_max")) {
1177 len = num_arg(&user_buffer[i], 10, &value);
1182 if (value != pkt_dev->udp_dst_max) {
1183 pkt_dev->udp_dst_max = value;
1184 pkt_dev->cur_udp_dst = value;
1186 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1189 if (!strcmp(name, "clone_skb")) {
1190 len = num_arg(&user_buffer[i], 10, &value);
1193 /* clone_skb is not supported for netif_receive xmit_mode and
1197 ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
1198 !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1200 if (value > 0 && pkt_dev->n_imix_entries > 0)
1204 pkt_dev->clone_skb = value;
1206 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1209 if (!strcmp(name, "count")) {
1210 len = num_arg(&user_buffer[i], 10, &value);
1215 pkt_dev->count = value;
1216 sprintf(pg_result, "OK: count=%llu",
1217 (unsigned long long)pkt_dev->count);
1220 if (!strcmp(name, "src_mac_count")) {
1221 len = num_arg(&user_buffer[i], 10, &value);
1226 if (pkt_dev->src_mac_count != value) {
1227 pkt_dev->src_mac_count = value;
1228 pkt_dev->cur_src_mac_offset = 0;
1230 sprintf(pg_result, "OK: src_mac_count=%d",
1231 pkt_dev->src_mac_count);
1234 if (!strcmp(name, "dst_mac_count")) {
1235 len = num_arg(&user_buffer[i], 10, &value);
1240 if (pkt_dev->dst_mac_count != value) {
1241 pkt_dev->dst_mac_count = value;
1242 pkt_dev->cur_dst_mac_offset = 0;
1244 sprintf(pg_result, "OK: dst_mac_count=%d",
1245 pkt_dev->dst_mac_count);
1248 if (!strcmp(name, "burst")) {
1249 len = num_arg(&user_buffer[i], 10, &value);
1255 ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
1256 ((pkt_dev->xmit_mode == M_START_XMIT) &&
1257 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
1259 pkt_dev->burst = value < 1 ? 1 : value;
1260 sprintf(pg_result, "OK: burst=%u", pkt_dev->burst);
1263 if (!strcmp(name, "node")) {
1264 len = num_arg(&user_buffer[i], 10, &value);
1270 if (node_possible(value)) {
1271 pkt_dev->node = value;
1272 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1273 if (pkt_dev->page) {
1274 put_page(pkt_dev->page);
1275 pkt_dev->page = NULL;
1279 sprintf(pg_result, "ERROR: node not possible");
1282 if (!strcmp(name, "xmit_mode")) {
1286 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1290 if (copy_from_user(f, &user_buffer[i], len))
1294 if (strcmp(f, "start_xmit") == 0) {
1295 pkt_dev->xmit_mode = M_START_XMIT;
1296 } else if (strcmp(f, "netif_receive") == 0) {
1297 /* clone_skb set earlier, not supported in this mode */
1298 if (pkt_dev->clone_skb > 0)
1301 pkt_dev->xmit_mode = M_NETIF_RECEIVE;
1303 /* make sure new packet is allocated every time
1304 * pktgen_xmit() is called
1306 pkt_dev->last_ok = 1;
1307 } else if (strcmp(f, "queue_xmit") == 0) {
1308 pkt_dev->xmit_mode = M_QUEUE_XMIT;
1309 pkt_dev->last_ok = 1;
1312 "xmit_mode -:%s:- unknown\nAvailable modes: %s",
1313 f, "start_xmit, netif_receive\n");
1316 sprintf(pg_result, "OK: xmit_mode=%s", f);
1319 if (!strcmp(name, "flag")) {
1322 bool disable = false;
1325 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1329 if (copy_from_user(f, &user_buffer[i], len))
1333 flag = pktgen_read_flag(f, &disable);
1337 pkt_dev->flags &= ~flag;
1339 pkt_dev->flags |= flag;
1342 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1344 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1345 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1346 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1347 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
1355 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1358 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1359 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1363 if (copy_from_user(buf, &user_buffer[i], len))
1366 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1367 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1368 strcpy(pkt_dev->dst_min, buf);
1369 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1370 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1373 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
1375 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1378 if (!strcmp(name, "dst_max")) {
1379 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1383 if (copy_from_user(buf, &user_buffer[i], len))
1386 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1387 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1388 strcpy(pkt_dev->dst_max, buf);
1389 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1390 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1393 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
1395 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1398 if (!strcmp(name, "dst6")) {
1399 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1403 pkt_dev->flags |= F_IPV6;
1405 if (copy_from_user(buf, &user_buffer[i], len))
1409 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
1410 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
1412 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
1415 pr_debug("dst6 set to: %s\n", buf);
1418 sprintf(pg_result, "OK: dst6=%s", buf);
1421 if (!strcmp(name, "dst6_min")) {
1422 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1426 pkt_dev->flags |= F_IPV6;
1428 if (copy_from_user(buf, &user_buffer[i], len))
1432 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
1433 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
1435 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
1437 pr_debug("dst6_min set to: %s\n", buf);
1440 sprintf(pg_result, "OK: dst6_min=%s", buf);
1443 if (!strcmp(name, "dst6_max")) {
1444 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1448 pkt_dev->flags |= F_IPV6;
1450 if (copy_from_user(buf, &user_buffer[i], len))
1454 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
1455 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
1458 pr_debug("dst6_max set to: %s\n", buf);
1461 sprintf(pg_result, "OK: dst6_max=%s", buf);
1464 if (!strcmp(name, "src6")) {
1465 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1469 pkt_dev->flags |= F_IPV6;
1471 if (copy_from_user(buf, &user_buffer[i], len))
1475 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
1476 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
1478 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
1481 pr_debug("src6 set to: %s\n", buf);
1484 sprintf(pg_result, "OK: src6=%s", buf);
1487 if (!strcmp(name, "src_min")) {
1488 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1492 if (copy_from_user(buf, &user_buffer[i], len))
1495 if (strcmp(buf, pkt_dev->src_min) != 0) {
1496 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1497 strcpy(pkt_dev->src_min, buf);
1498 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1499 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1502 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
1504 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1507 if (!strcmp(name, "src_max")) {
1508 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1512 if (copy_from_user(buf, &user_buffer[i], len))
1515 if (strcmp(buf, pkt_dev->src_max) != 0) {
1516 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1517 strcpy(pkt_dev->src_max, buf);
1518 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1519 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1522 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
1524 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1527 if (!strcmp(name, "dst_mac")) {
1528 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1532 memset(valstr, 0, sizeof(valstr));
1533 if (copy_from_user(valstr, &user_buffer[i], len))
1536 if (!mac_pton(valstr, pkt_dev->dst_mac))
1538 /* Set up Dest MAC */
1539 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
1541 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
1544 if (!strcmp(name, "src_mac")) {
1545 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1549 memset(valstr, 0, sizeof(valstr));
1550 if (copy_from_user(valstr, &user_buffer[i], len))
1553 if (!mac_pton(valstr, pkt_dev->src_mac))
1555 /* Set up Src MAC */
1556 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
1558 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
1562 if (!strcmp(name, "clear_counters")) {
1563 pktgen_clear_counters(pkt_dev);
1564 sprintf(pg_result, "OK: Clearing counters.\n");
1568 if (!strcmp(name, "flows")) {
1569 len = num_arg(&user_buffer[i], 10, &value);
1574 if (value > MAX_CFLOWS)
1577 pkt_dev->cflows = value;
1578 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1582 if (!strcmp(name, "spi")) {
1583 len = num_arg(&user_buffer[i], 10, &value);
1588 pkt_dev->spi = value;
1589 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1593 if (!strcmp(name, "flowlen")) {
1594 len = num_arg(&user_buffer[i], 10, &value);
1599 pkt_dev->lflow = value;
1600 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1604 if (!strcmp(name, "queue_map_min")) {
1605 len = num_arg(&user_buffer[i], 5, &value);
1610 pkt_dev->queue_map_min = value;
1611 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1615 if (!strcmp(name, "queue_map_max")) {
1616 len = num_arg(&user_buffer[i], 5, &value);
1621 pkt_dev->queue_map_max = value;
1622 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1626 if (!strcmp(name, "mpls")) {
1627 unsigned int n, cnt;
1629 len = get_labels(&user_buffer[i], pkt_dev);
1633 cnt = sprintf(pg_result, "OK: mpls=");
1634 for (n = 0; n < pkt_dev->nr_labels; n++)
1635 cnt += sprintf(pg_result + cnt,
1636 "%08x%s", ntohl(pkt_dev->labels[n]),
1637 n == pkt_dev->nr_labels-1 ? "" : ",");
1639 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1640 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1641 pkt_dev->svlan_id = 0xffff;
1644 pr_debug("VLAN/SVLAN auto turned off\n");
1649 if (!strcmp(name, "vlan_id")) {
1650 len = num_arg(&user_buffer[i], 4, &value);
1655 if (value <= 4095) {
1656 pkt_dev->vlan_id = value; /* turn on VLAN */
1659 pr_debug("VLAN turned on\n");
1661 if (debug && pkt_dev->nr_labels)
1662 pr_debug("MPLS auto turned off\n");
1664 pkt_dev->nr_labels = 0; /* turn off MPLS */
1665 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1667 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1668 pkt_dev->svlan_id = 0xffff;
1671 pr_debug("VLAN/SVLAN turned off\n");
1676 if (!strcmp(name, "vlan_p")) {
1677 len = num_arg(&user_buffer[i], 1, &value);
1682 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1683 pkt_dev->vlan_p = value;
1684 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1686 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1691 if (!strcmp(name, "vlan_cfi")) {
1692 len = num_arg(&user_buffer[i], 1, &value);
1697 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1698 pkt_dev->vlan_cfi = value;
1699 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1701 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1706 if (!strcmp(name, "svlan_id")) {
1707 len = num_arg(&user_buffer[i], 4, &value);
1712 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1713 pkt_dev->svlan_id = value; /* turn on SVLAN */
1716 pr_debug("SVLAN turned on\n");
1718 if (debug && pkt_dev->nr_labels)
1719 pr_debug("MPLS auto turned off\n");
1721 pkt_dev->nr_labels = 0; /* turn off MPLS */
1722 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1724 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1725 pkt_dev->svlan_id = 0xffff;
1728 pr_debug("VLAN/SVLAN turned off\n");
1733 if (!strcmp(name, "svlan_p")) {
1734 len = num_arg(&user_buffer[i], 1, &value);
1739 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1740 pkt_dev->svlan_p = value;
1741 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1743 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1748 if (!strcmp(name, "svlan_cfi")) {
1749 len = num_arg(&user_buffer[i], 1, &value);
1754 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1755 pkt_dev->svlan_cfi = value;
1756 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1758 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1763 if (!strcmp(name, "tos")) {
1764 __u32 tmp_value = 0;
1765 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1771 pkt_dev->tos = tmp_value;
1772 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1774 sprintf(pg_result, "ERROR: tos must be 00-ff");
1779 if (!strcmp(name, "traffic_class")) {
1780 __u32 tmp_value = 0;
1781 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1787 pkt_dev->traffic_class = tmp_value;
1788 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1790 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1795 if (!strcmp(name, "skb_priority")) {
1796 len = num_arg(&user_buffer[i], 9, &value);
1801 pkt_dev->skb_priority = value;
1802 sprintf(pg_result, "OK: skb_priority=%i",
1803 pkt_dev->skb_priority);
1807 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1811 static int pktgen_if_open(struct inode *inode, struct file *file)
1813 return single_open(file, pktgen_if_show, PDE_DATA(inode));
1816 static const struct proc_ops pktgen_if_proc_ops = {
1817 .proc_open = pktgen_if_open,
1818 .proc_read = seq_read,
1819 .proc_lseek = seq_lseek,
1820 .proc_write = pktgen_if_write,
1821 .proc_release = single_release,
1824 static int pktgen_thread_show(struct seq_file *seq, void *v)
1826 struct pktgen_thread *t = seq->private;
1827 const struct pktgen_dev *pkt_dev;
1831 seq_puts(seq, "Running: ");
1834 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1835 if (pkt_dev->running)
1836 seq_printf(seq, "%s ", pkt_dev->odevname);
1838 seq_puts(seq, "\nStopped: ");
1840 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1841 if (!pkt_dev->running)
1842 seq_printf(seq, "%s ", pkt_dev->odevname);
1845 seq_printf(seq, "\nResult: %s\n", t->result);
1847 seq_puts(seq, "\nResult: NA\n");
1854 static ssize_t pktgen_thread_write(struct file *file,
1855 const char __user * user_buffer,
1856 size_t count, loff_t * offset)
1858 struct seq_file *seq = file->private_data;
1859 struct pktgen_thread *t = seq->private;
1860 int i, max, len, ret;
1865 // sprintf(pg_result, "Wrong command format");
1870 len = count_trail_chars(user_buffer, max);
1876 /* Read variable name */
1878 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1882 memset(name, 0, sizeof(name));
1883 if (copy_from_user(name, &user_buffer[i], len))
1888 len = count_trail_chars(&user_buffer[i], max);
1895 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
1898 pr_err("ERROR: No thread\n");
1903 pg_result = &(t->result[0]);
1905 if (!strcmp(name, "add_device")) {
1908 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1913 if (copy_from_user(f, &user_buffer[i], len))
1916 mutex_lock(&pktgen_thread_lock);
1917 ret = pktgen_add_device(t, f);
1918 mutex_unlock(&pktgen_thread_lock);
1921 sprintf(pg_result, "OK: add_device=%s", f);
1923 sprintf(pg_result, "ERROR: can not add device %s", f);
1927 if (!strcmp(name, "rem_device_all")) {
1928 mutex_lock(&pktgen_thread_lock);
1929 t->control |= T_REMDEVALL;
1930 mutex_unlock(&pktgen_thread_lock);
1931 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1933 sprintf(pg_result, "OK: rem_device_all");
1937 if (!strcmp(name, "max_before_softirq")) {
1938 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1948 static int pktgen_thread_open(struct inode *inode, struct file *file)
1950 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
1953 static const struct proc_ops pktgen_thread_proc_ops = {
1954 .proc_open = pktgen_thread_open,
1955 .proc_read = seq_read,
1956 .proc_lseek = seq_lseek,
1957 .proc_write = pktgen_thread_write,
1958 .proc_release = single_release,
1961 /* Think find or remove for NN */
1962 static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1963 const char *ifname, int remove)
1965 struct pktgen_thread *t;
1966 struct pktgen_dev *pkt_dev = NULL;
1967 bool exact = (remove == FIND);
1969 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
1970 pkt_dev = pktgen_find_dev(t, ifname, exact);
1973 pkt_dev->removal_mark = 1;
1974 t->control |= T_REMDEV;
1983 * mark a device for removal
1985 static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
1987 struct pktgen_dev *pkt_dev = NULL;
1988 const int max_tries = 10, msec_per_try = 125;
1991 mutex_lock(&pktgen_thread_lock);
1992 pr_debug("%s: marking %s for removal\n", __func__, ifname);
1996 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1997 if (pkt_dev == NULL)
1998 break; /* success */
2000 mutex_unlock(&pktgen_thread_lock);
2001 pr_debug("%s: waiting for %s to disappear....\n",
2003 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
2004 mutex_lock(&pktgen_thread_lock);
2006 if (++i >= max_tries) {
2007 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
2008 __func__, msec_per_try * i, ifname);
2014 mutex_unlock(&pktgen_thread_lock);
2017 static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
2019 struct pktgen_thread *t;
2021 mutex_lock(&pktgen_thread_lock);
2023 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
2024 struct pktgen_dev *pkt_dev;
2027 list_for_each_entry(pkt_dev, &t->if_list, list) {
2028 if (pkt_dev->odev != dev)
2031 proc_remove(pkt_dev->entry);
2033 pkt_dev->entry = proc_create_data(dev->name, 0600,
2035 &pktgen_if_proc_ops,
2037 if (!pkt_dev->entry)
2038 pr_err("can't move proc entry for '%s'\n",
2044 mutex_unlock(&pktgen_thread_lock);
2047 static int pktgen_device_event(struct notifier_block *unused,
2048 unsigned long event, void *ptr)
2050 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2051 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
2053 if (pn->pktgen_exiting)
2056 /* It is OK that we do not hold the group lock right now,
2057 * as we run under the RTNL lock.
2061 case NETDEV_CHANGENAME:
2062 pktgen_change_name(pn, dev);
2065 case NETDEV_UNREGISTER:
2066 pktgen_mark_device(pn, dev->name);
2073 static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
2074 struct pktgen_dev *pkt_dev,
2080 for (i = 0; ifname[i] != '@'; i++) {
2088 return dev_get_by_name(pn->net, b);
2092 /* Associate pktgen_dev with a device. */
2094 static int pktgen_setup_dev(const struct pktgen_net *pn,
2095 struct pktgen_dev *pkt_dev, const char *ifname)
2097 struct net_device *odev;
2100 /* Clean old setups */
2101 if (pkt_dev->odev) {
2102 dev_put(pkt_dev->odev);
2103 pkt_dev->odev = NULL;
2106 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
2108 pr_err("no such netdevice: \"%s\"\n", ifname);
2112 if (odev->type != ARPHRD_ETHER && odev->type != ARPHRD_LOOPBACK) {
2113 pr_err("not an ethernet or loopback device: \"%s\"\n", ifname);
2115 } else if (!netif_running(odev)) {
2116 pr_err("device is down: \"%s\"\n", ifname);
2119 pkt_dev->odev = odev;
2127 /* Read pkt_dev from the interface and set up internal pktgen_dev
2128 * structure to have the right information to create/send packets
2130 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2134 if (!pkt_dev->odev) {
2135 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2136 sprintf(pkt_dev->result,
2137 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2141 /* make sure that we don't pick a non-existing transmit queue */
2142 ntxq = pkt_dev->odev->real_num_tx_queues;
2144 if (ntxq <= pkt_dev->queue_map_min) {
2145 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2146 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2148 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
2150 if (pkt_dev->queue_map_max >= ntxq) {
2151 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2152 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2154 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
2157 /* Default to the interface's mac if not explicitly set. */
2159 if (is_zero_ether_addr(pkt_dev->src_mac))
2160 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
2162 /* Set up Dest MAC */
2163 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
2165 if (pkt_dev->flags & F_IPV6) {
2166 int i, set = 0, err = 1;
2167 struct inet6_dev *idev;
2169 if (pkt_dev->min_pkt_size == 0) {
2170 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2171 + sizeof(struct udphdr)
2172 + sizeof(struct pktgen_hdr)
2173 + pkt_dev->pkt_overhead;
2176 for (i = 0; i < sizeof(struct in6_addr); i++)
2177 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2185 * Use linklevel address if unconfigured.
2187 * use ipv6_get_lladdr if/when it's get exported
2191 idev = __in6_dev_get(pkt_dev->odev);
2193 struct inet6_ifaddr *ifp;
2195 read_lock_bh(&idev->lock);
2196 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2197 if ((ifp->scope & IFA_LINK) &&
2198 !(ifp->flags & IFA_F_TENTATIVE)) {
2199 pkt_dev->cur_in6_saddr = ifp->addr;
2204 read_unlock_bh(&idev->lock);
2208 pr_err("ERROR: IPv6 link address not available\n");
2211 if (pkt_dev->min_pkt_size == 0) {
2212 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2213 + sizeof(struct udphdr)
2214 + sizeof(struct pktgen_hdr)
2215 + pkt_dev->pkt_overhead;
2218 pkt_dev->saddr_min = 0;
2219 pkt_dev->saddr_max = 0;
2220 if (strlen(pkt_dev->src_min) == 0) {
2222 struct in_device *in_dev;
2225 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2227 const struct in_ifaddr *ifa;
2229 ifa = rcu_dereference(in_dev->ifa_list);
2231 pkt_dev->saddr_min = ifa->ifa_address;
2232 pkt_dev->saddr_max = pkt_dev->saddr_min;
2237 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2238 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2241 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2242 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2244 /* Initialize current values. */
2245 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2246 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2247 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2249 pkt_dev->cur_dst_mac_offset = 0;
2250 pkt_dev->cur_src_mac_offset = 0;
2251 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2252 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2253 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2254 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2255 pkt_dev->nflows = 0;
2259 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2261 ktime_t start_time, end_time;
2263 struct hrtimer_sleeper t;
2265 hrtimer_init_sleeper_on_stack(&t, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2266 hrtimer_set_expires(&t.timer, spin_until);
2268 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2272 start_time = ktime_get();
2273 if (remaining < 100000) {
2274 /* for small delays (<100us), just loop until limit is reached */
2276 end_time = ktime_get();
2277 } while (ktime_compare(end_time, spin_until) < 0);
2280 set_current_state(TASK_INTERRUPTIBLE);
2281 hrtimer_sleeper_start_expires(&t, HRTIMER_MODE_ABS);
2286 hrtimer_cancel(&t.timer);
2287 } while (t.task && pkt_dev->running && !signal_pending(current));
2288 __set_current_state(TASK_RUNNING);
2289 end_time = ktime_get();
2292 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2294 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2295 destroy_hrtimer_on_stack(&t.timer);
2298 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2300 pkt_dev->pkt_overhead = 0;
2301 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2302 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2303 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2306 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2308 return !!(pkt_dev->flows[flow].flags & F_INIT);
2311 static inline int f_pick(struct pktgen_dev *pkt_dev)
2313 int flow = pkt_dev->curfl;
2315 if (pkt_dev->flags & F_FLOW_SEQ) {
2316 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2318 pkt_dev->flows[flow].count = 0;
2319 pkt_dev->flows[flow].flags = 0;
2320 pkt_dev->curfl += 1;
2321 if (pkt_dev->curfl >= pkt_dev->cflows)
2322 pkt_dev->curfl = 0; /*reset */
2325 flow = prandom_u32() % pkt_dev->cflows;
2326 pkt_dev->curfl = flow;
2328 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2329 pkt_dev->flows[flow].count = 0;
2330 pkt_dev->flows[flow].flags = 0;
2334 return pkt_dev->curfl;
2339 /* If there was already an IPSEC SA, we keep it as is, else
2340 * we go look for it ...
2342 #define DUMMY_MARK 0
2343 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2345 struct xfrm_state *x = pkt_dev->flows[flow].x;
2346 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2350 /* We need as quick as possible to find the right SA
2351 * Searching with minimum criteria to archieve this.
2353 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2355 /* slow path: we dont already have xfrm_state */
2356 x = xfrm_stateonly_find(pn->net, DUMMY_MARK, 0,
2357 (xfrm_address_t *)&pkt_dev->cur_daddr,
2358 (xfrm_address_t *)&pkt_dev->cur_saddr,
2361 pkt_dev->ipsproto, 0);
2364 pkt_dev->flows[flow].x = x;
2365 set_pkt_overhead(pkt_dev);
2366 pkt_dev->pkt_overhead += x->props.header_len;
2372 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2375 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2376 pkt_dev->cur_queue_map = smp_processor_id();
2378 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2380 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2382 (pkt_dev->queue_map_max -
2383 pkt_dev->queue_map_min + 1)
2384 + pkt_dev->queue_map_min;
2386 t = pkt_dev->cur_queue_map + 1;
2387 if (t > pkt_dev->queue_map_max)
2388 t = pkt_dev->queue_map_min;
2390 pkt_dev->cur_queue_map = t;
2392 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2395 /* Increment/randomize headers according to flags and current values
2396 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2398 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2404 if (pkt_dev->cflows)
2405 flow = f_pick(pkt_dev);
2407 /* Deal with source MAC */
2408 if (pkt_dev->src_mac_count > 1) {
2412 if (pkt_dev->flags & F_MACSRC_RND)
2413 mc = prandom_u32() % pkt_dev->src_mac_count;
2415 mc = pkt_dev->cur_src_mac_offset++;
2416 if (pkt_dev->cur_src_mac_offset >=
2417 pkt_dev->src_mac_count)
2418 pkt_dev->cur_src_mac_offset = 0;
2421 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2422 pkt_dev->hh[11] = tmp;
2423 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2424 pkt_dev->hh[10] = tmp;
2425 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2426 pkt_dev->hh[9] = tmp;
2427 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2428 pkt_dev->hh[8] = tmp;
2429 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2430 pkt_dev->hh[7] = tmp;
2433 /* Deal with Destination MAC */
2434 if (pkt_dev->dst_mac_count > 1) {
2438 if (pkt_dev->flags & F_MACDST_RND)
2439 mc = prandom_u32() % pkt_dev->dst_mac_count;
2442 mc = pkt_dev->cur_dst_mac_offset++;
2443 if (pkt_dev->cur_dst_mac_offset >=
2444 pkt_dev->dst_mac_count) {
2445 pkt_dev->cur_dst_mac_offset = 0;
2449 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2450 pkt_dev->hh[5] = tmp;
2451 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2452 pkt_dev->hh[4] = tmp;
2453 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2454 pkt_dev->hh[3] = tmp;
2455 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2456 pkt_dev->hh[2] = tmp;
2457 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2458 pkt_dev->hh[1] = tmp;
2461 if (pkt_dev->flags & F_MPLS_RND) {
2463 for (i = 0; i < pkt_dev->nr_labels; i++)
2464 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2465 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2466 ((__force __be32)prandom_u32() &
2470 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2471 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
2474 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2475 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
2478 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2479 if (pkt_dev->flags & F_UDPSRC_RND)
2480 pkt_dev->cur_udp_src = prandom_u32() %
2481 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2482 + pkt_dev->udp_src_min;
2485 pkt_dev->cur_udp_src++;
2486 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2487 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2491 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2492 if (pkt_dev->flags & F_UDPDST_RND) {
2493 pkt_dev->cur_udp_dst = prandom_u32() %
2494 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2495 + pkt_dev->udp_dst_min;
2497 pkt_dev->cur_udp_dst++;
2498 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2499 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2503 if (!(pkt_dev->flags & F_IPV6)) {
2505 imn = ntohl(pkt_dev->saddr_min);
2506 imx = ntohl(pkt_dev->saddr_max);
2509 if (pkt_dev->flags & F_IPSRC_RND)
2510 t = prandom_u32() % (imx - imn) + imn;
2512 t = ntohl(pkt_dev->cur_saddr);
2518 pkt_dev->cur_saddr = htonl(t);
2521 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2522 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2524 imn = ntohl(pkt_dev->daddr_min);
2525 imx = ntohl(pkt_dev->daddr_max);
2529 if (pkt_dev->flags & F_IPDST_RND) {
2535 } while (ipv4_is_loopback(s) ||
2536 ipv4_is_multicast(s) ||
2537 ipv4_is_lbcast(s) ||
2538 ipv4_is_zeronet(s) ||
2539 ipv4_is_local_multicast(s));
2540 pkt_dev->cur_daddr = s;
2542 t = ntohl(pkt_dev->cur_daddr);
2547 pkt_dev->cur_daddr = htonl(t);
2550 if (pkt_dev->cflows) {
2551 pkt_dev->flows[flow].flags |= F_INIT;
2552 pkt_dev->flows[flow].cur_daddr =
2555 if (pkt_dev->flags & F_IPSEC)
2556 get_ipsec_sa(pkt_dev, flow);
2561 } else { /* IPV6 * */
2563 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
2566 /* Only random destinations yet */
2568 for (i = 0; i < 4; i++) {
2569 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2570 (((__force __be32)prandom_u32() |
2571 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2572 pkt_dev->max_in6_daddr.s6_addr32[i]);
2577 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2579 if (pkt_dev->flags & F_TXSIZE_RND) {
2581 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2582 + pkt_dev->min_pkt_size;
2584 t = pkt_dev->cur_pkt_size + 1;
2585 if (t > pkt_dev->max_pkt_size)
2586 t = pkt_dev->min_pkt_size;
2588 pkt_dev->cur_pkt_size = t;
2589 } else if (pkt_dev->n_imix_entries > 0) {
2590 struct imix_pkt *entry;
2591 __u32 t = prandom_u32() % IMIX_PRECISION;
2592 __u8 entry_index = pkt_dev->imix_distribution[t];
2594 entry = &pkt_dev->imix_entries[entry_index];
2595 entry->count_so_far++;
2596 pkt_dev->cur_pkt_size = entry->size;
2599 set_cur_queue_map(pkt_dev);
2601 pkt_dev->flows[flow].count++;
2604 static void fill_imix_distribution(struct pktgen_dev *pkt_dev)
2606 int cumulative_probabilites[MAX_IMIX_ENTRIES];
2608 __u64 cumulative_prob = 0;
2609 __u64 total_weight = 0;
2612 for (i = 0; i < pkt_dev->n_imix_entries; i++)
2613 total_weight += pkt_dev->imix_entries[i].weight;
2615 /* Fill cumulative_probabilites with sum of normalized probabilities */
2616 for (i = 0; i < pkt_dev->n_imix_entries - 1; i++) {
2617 cumulative_prob += div64_u64(pkt_dev->imix_entries[i].weight *
2620 cumulative_probabilites[i] = cumulative_prob;
2622 cumulative_probabilites[pkt_dev->n_imix_entries - 1] = 100;
2624 for (i = 0; i < IMIX_PRECISION; i++) {
2625 if (i == cumulative_probabilites[j])
2627 pkt_dev->imix_distribution[i] = j;
2632 static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2634 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2637 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2639 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2641 struct net *net = dev_net(pkt_dev->odev);
2645 /* XXX: we dont support tunnel mode for now until
2646 * we resolve the dst issue */
2647 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2650 /* But when user specify an valid SPI, transformation
2651 * supports both transport/tunnel mode + ESP/AH type.
2653 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2654 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
2657 err = pktgen_xfrm_outer_mode_output(x, skb);
2658 rcu_read_unlock_bh();
2660 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2663 err = x->type->output(x, skb);
2665 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2668 spin_lock_bh(&x->lock);
2669 x->curlft.bytes += skb->len;
2670 x->curlft.packets++;
2671 spin_unlock_bh(&x->lock);
2676 static void free_SAs(struct pktgen_dev *pkt_dev)
2678 if (pkt_dev->cflows) {
2679 /* let go of the SAs if we have them */
2681 for (i = 0; i < pkt_dev->cflows; i++) {
2682 struct xfrm_state *x = pkt_dev->flows[i].x;
2685 pkt_dev->flows[i].x = NULL;
2691 static int process_ipsec(struct pktgen_dev *pkt_dev,
2692 struct sk_buff *skb, __be16 protocol)
2694 if (pkt_dev->flags & F_IPSEC) {
2695 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2702 nhead = x->props.header_len - skb_headroom(skb);
2704 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2706 pr_err("Error expanding ipsec packet %d\n",
2712 /* ipsec is not expecting ll header */
2713 skb_pull(skb, ETH_HLEN);
2714 ret = pktgen_output_ipsec(skb, pkt_dev);
2716 pr_err("Error creating ipsec packet %d\n", ret);
2720 eth = skb_push(skb, ETH_HLEN);
2721 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2722 eth->h_proto = protocol;
2724 /* Update IPv4 header len as well as checksum value */
2726 iph->tot_len = htons(skb->len - ETH_HLEN);
2737 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2740 for (i = 0; i < pkt_dev->nr_labels; i++)
2741 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2744 *mpls |= MPLS_STACK_BOTTOM;
2747 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2750 return htons(id | (cfi << 12) | (prio << 13));
2753 static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2756 struct timespec64 timestamp;
2757 struct pktgen_hdr *pgh;
2759 pgh = skb_put(skb, sizeof(*pgh));
2760 datalen -= sizeof(*pgh);
2762 if (pkt_dev->nfrags <= 0) {
2763 skb_put_zero(skb, datalen);
2765 int frags = pkt_dev->nfrags;
2770 if (frags > MAX_SKB_FRAGS)
2771 frags = MAX_SKB_FRAGS;
2772 len = datalen - frags * PAGE_SIZE;
2774 skb_put_zero(skb, len);
2775 datalen = frags * PAGE_SIZE;
2779 frag_len = (datalen/frags) < PAGE_SIZE ?
2780 (datalen/frags) : PAGE_SIZE;
2781 while (datalen > 0) {
2782 if (unlikely(!pkt_dev->page)) {
2783 int node = numa_node_id();
2785 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2786 node = pkt_dev->node;
2787 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2791 get_page(pkt_dev->page);
2792 skb_frag_set_page(skb, i, pkt_dev->page);
2793 skb_frag_off_set(&skb_shinfo(skb)->frags[i], 0);
2794 /*last fragment, fill rest of data*/
2795 if (i == (frags - 1))
2796 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2797 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
2799 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2800 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2801 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2802 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2804 skb_shinfo(skb)->nr_frags = i;
2808 /* Stamp the time, and sequence number,
2809 * convert them to network byte order
2811 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2812 pgh->seq_num = htonl(pkt_dev->seq_num);
2814 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2819 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2820 * as done by wireshark, or y2038 when interpreted as signed.
2821 * This is probably harmless, but if anyone wants to improve
2822 * it, we could introduce a variant that puts 64-bit nanoseconds
2823 * into the respective header bytes.
2824 * This would also be slightly faster to read.
2826 ktime_get_real_ts64(×tamp);
2827 pgh->tv_sec = htonl(timestamp.tv_sec);
2828 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
2832 static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2833 struct pktgen_dev *pkt_dev)
2835 unsigned int extralen = LL_RESERVED_SPACE(dev);
2836 struct sk_buff *skb = NULL;
2839 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
2840 if (pkt_dev->flags & F_NODE) {
2841 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2843 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2845 skb_reserve(skb, NET_SKB_PAD);
2849 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2852 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
2854 skb_reserve(skb, extralen - 16);
2859 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2860 struct pktgen_dev *pkt_dev)
2862 struct sk_buff *skb = NULL;
2864 struct udphdr *udph;
2867 __be16 protocol = htons(ETH_P_IP);
2869 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2870 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2871 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2872 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2875 if (pkt_dev->nr_labels)
2876 protocol = htons(ETH_P_MPLS_UC);
2878 if (pkt_dev->vlan_id != 0xffff)
2879 protocol = htons(ETH_P_8021Q);
2881 /* Update any of the values, used when we're incrementing various
2884 mod_cur_headers(pkt_dev);
2885 queue_map = pkt_dev->cur_queue_map;
2887 skb = pktgen_alloc_skb(odev, pkt_dev);
2889 sprintf(pkt_dev->result, "No memory");
2893 prefetchw(skb->data);
2894 skb_reserve(skb, 16);
2896 /* Reserve for ethernet and IP header */
2897 eth = skb_push(skb, 14);
2898 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2899 if (pkt_dev->nr_labels)
2900 mpls_push(mpls, pkt_dev);
2902 if (pkt_dev->vlan_id != 0xffff) {
2903 if (pkt_dev->svlan_id != 0xffff) {
2904 svlan_tci = skb_put(skb, sizeof(__be16));
2905 *svlan_tci = build_tci(pkt_dev->svlan_id,
2908 svlan_encapsulated_proto = skb_put(skb,
2910 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2912 vlan_tci = skb_put(skb, sizeof(__be16));
2913 *vlan_tci = build_tci(pkt_dev->vlan_id,
2916 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2917 *vlan_encapsulated_proto = htons(ETH_P_IP);
2920 skb_reset_mac_header(skb);
2921 skb_set_network_header(skb, skb->len);
2922 iph = skb_put(skb, sizeof(struct iphdr));
2924 skb_set_transport_header(skb, skb->len);
2925 udph = skb_put(skb, sizeof(struct udphdr));
2926 skb_set_queue_mapping(skb, queue_map);
2927 skb->priority = pkt_dev->skb_priority;
2929 memcpy(eth, pkt_dev->hh, 12);
2930 *(__be16 *) & eth[12] = protocol;
2932 /* Eth + IPh + UDPh + mpls */
2933 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2934 pkt_dev->pkt_overhead;
2935 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
2936 datalen = sizeof(struct pktgen_hdr);
2938 udph->source = htons(pkt_dev->cur_udp_src);
2939 udph->dest = htons(pkt_dev->cur_udp_dst);
2940 udph->len = htons(datalen + 8); /* DATA + udphdr */
2946 iph->tos = pkt_dev->tos;
2947 iph->protocol = IPPROTO_UDP; /* UDP */
2948 iph->saddr = pkt_dev->cur_saddr;
2949 iph->daddr = pkt_dev->cur_daddr;
2950 iph->id = htons(pkt_dev->ip_id);
2953 iplen = 20 + 8 + datalen;
2954 iph->tot_len = htons(iplen);
2956 skb->protocol = protocol;
2958 skb->pkt_type = PACKET_HOST;
2960 pktgen_finalize_skb(pkt_dev, skb, datalen);
2962 if (!(pkt_dev->flags & F_UDPCSUM)) {
2963 skb->ip_summed = CHECKSUM_NONE;
2964 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
2965 skb->ip_summed = CHECKSUM_PARTIAL;
2967 udp4_hwcsum(skb, iph->saddr, iph->daddr);
2969 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
2971 /* add protocol-dependent pseudo-header */
2972 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
2973 datalen + 8, IPPROTO_UDP, csum);
2975 if (udph->check == 0)
2976 udph->check = CSUM_MANGLED_0;
2980 if (!process_ipsec(pkt_dev, skb, protocol))
2987 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2988 struct pktgen_dev *pkt_dev)
2990 struct sk_buff *skb = NULL;
2992 struct udphdr *udph;
2993 int datalen, udplen;
2994 struct ipv6hdr *iph;
2995 __be16 protocol = htons(ETH_P_IPV6);
2997 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2998 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2999 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
3000 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
3003 if (pkt_dev->nr_labels)
3004 protocol = htons(ETH_P_MPLS_UC);
3006 if (pkt_dev->vlan_id != 0xffff)
3007 protocol = htons(ETH_P_8021Q);
3009 /* Update any of the values, used when we're incrementing various
3012 mod_cur_headers(pkt_dev);
3013 queue_map = pkt_dev->cur_queue_map;
3015 skb = pktgen_alloc_skb(odev, pkt_dev);
3017 sprintf(pkt_dev->result, "No memory");
3021 prefetchw(skb->data);
3022 skb_reserve(skb, 16);
3024 /* Reserve for ethernet and IP header */
3025 eth = skb_push(skb, 14);
3026 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
3027 if (pkt_dev->nr_labels)
3028 mpls_push(mpls, pkt_dev);
3030 if (pkt_dev->vlan_id != 0xffff) {
3031 if (pkt_dev->svlan_id != 0xffff) {
3032 svlan_tci = skb_put(skb, sizeof(__be16));
3033 *svlan_tci = build_tci(pkt_dev->svlan_id,
3036 svlan_encapsulated_proto = skb_put(skb,
3038 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
3040 vlan_tci = skb_put(skb, sizeof(__be16));
3041 *vlan_tci = build_tci(pkt_dev->vlan_id,
3044 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
3045 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
3048 skb_reset_mac_header(skb);
3049 skb_set_network_header(skb, skb->len);
3050 iph = skb_put(skb, sizeof(struct ipv6hdr));
3052 skb_set_transport_header(skb, skb->len);
3053 udph = skb_put(skb, sizeof(struct udphdr));
3054 skb_set_queue_mapping(skb, queue_map);
3055 skb->priority = pkt_dev->skb_priority;
3057 memcpy(eth, pkt_dev->hh, 12);
3058 *(__be16 *) ð[12] = protocol;
3060 /* Eth + IPh + UDPh + mpls */
3061 datalen = pkt_dev->cur_pkt_size - 14 -
3062 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
3063 pkt_dev->pkt_overhead;
3065 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
3066 datalen = sizeof(struct pktgen_hdr);
3067 net_info_ratelimited("increased datalen to %d\n", datalen);
3070 udplen = datalen + sizeof(struct udphdr);
3071 udph->source = htons(pkt_dev->cur_udp_src);
3072 udph->dest = htons(pkt_dev->cur_udp_dst);
3073 udph->len = htons(udplen);
3076 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
3078 if (pkt_dev->traffic_class) {
3079 /* Version + traffic class + flow (0) */
3080 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
3083 iph->hop_limit = 32;
3085 iph->payload_len = htons(udplen);
3086 iph->nexthdr = IPPROTO_UDP;
3088 iph->daddr = pkt_dev->cur_in6_daddr;
3089 iph->saddr = pkt_dev->cur_in6_saddr;
3091 skb->protocol = protocol;
3093 skb->pkt_type = PACKET_HOST;
3095 pktgen_finalize_skb(pkt_dev, skb, datalen);
3097 if (!(pkt_dev->flags & F_UDPCSUM)) {
3098 skb->ip_summed = CHECKSUM_NONE;
3099 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
3100 skb->ip_summed = CHECKSUM_PARTIAL;
3101 skb->csum_start = skb_transport_header(skb) - skb->head;
3102 skb->csum_offset = offsetof(struct udphdr, check);
3103 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
3105 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
3107 /* add protocol-dependent pseudo-header */
3108 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
3110 if (udph->check == 0)
3111 udph->check = CSUM_MANGLED_0;
3117 static struct sk_buff *fill_packet(struct net_device *odev,
3118 struct pktgen_dev *pkt_dev)
3120 if (pkt_dev->flags & F_IPV6)
3121 return fill_packet_ipv6(odev, pkt_dev);
3123 return fill_packet_ipv4(odev, pkt_dev);
3126 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3128 pkt_dev->seq_num = 1;
3129 pkt_dev->idle_acc = 0;
3131 pkt_dev->tx_bytes = 0;
3132 pkt_dev->errors = 0;
3135 /* Set up structure for sending pkts, clear counters */
3137 static void pktgen_run(struct pktgen_thread *t)
3139 struct pktgen_dev *pkt_dev;
3145 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3148 * setup odev and create initial packet.
3150 pktgen_setup_inject(pkt_dev);
3152 if (pkt_dev->odev) {
3153 pktgen_clear_counters(pkt_dev);
3154 pkt_dev->skb = NULL;
3155 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3157 set_pkt_overhead(pkt_dev);
3159 strcpy(pkt_dev->result, "Starting");
3160 pkt_dev->running = 1; /* Cranke yeself! */
3163 strcpy(pkt_dev->result, "Error starting");
3167 t->control &= ~(T_STOP);
3170 static void pktgen_handle_all_threads(struct pktgen_net *pn, u32 flags)
3172 struct pktgen_thread *t;
3174 mutex_lock(&pktgen_thread_lock);
3176 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3177 t->control |= (flags);
3179 mutex_unlock(&pktgen_thread_lock);
3182 static void pktgen_stop_all_threads(struct pktgen_net *pn)
3186 pktgen_handle_all_threads(pn, T_STOP);
3189 static int thread_is_running(const struct pktgen_thread *t)
3191 const struct pktgen_dev *pkt_dev;
3194 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3195 if (pkt_dev->running) {
3203 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3205 while (thread_is_running(t)) {
3207 /* note: 't' will still be around even after the unlock/lock
3208 * cycle because pktgen_thread threads are only cleared at
3211 mutex_unlock(&pktgen_thread_lock);
3212 msleep_interruptible(100);
3213 mutex_lock(&pktgen_thread_lock);
3215 if (signal_pending(current))
3223 static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
3225 struct pktgen_thread *t;
3228 /* prevent from racing with rmmod */
3229 if (!try_module_get(THIS_MODULE))
3232 mutex_lock(&pktgen_thread_lock);
3234 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
3235 sig = pktgen_wait_thread_run(t);
3241 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3242 t->control |= (T_STOP);
3244 mutex_unlock(&pktgen_thread_lock);
3245 module_put(THIS_MODULE);
3249 static void pktgen_run_all_threads(struct pktgen_net *pn)
3253 pktgen_handle_all_threads(pn, T_RUN);
3255 /* Propagate thread->control */
3256 schedule_timeout_interruptible(msecs_to_jiffies(125));
3258 pktgen_wait_all_threads_run(pn);
3261 static void pktgen_reset_all_threads(struct pktgen_net *pn)
3265 pktgen_handle_all_threads(pn, T_REMDEVALL);
3267 /* Propagate thread->control */
3268 schedule_timeout_interruptible(msecs_to_jiffies(125));
3270 pktgen_wait_all_threads_run(pn);
3273 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3275 __u64 bps, mbps, pps;
3276 char *p = pkt_dev->result;
3277 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3278 pkt_dev->started_at);
3279 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3281 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3282 (unsigned long long)ktime_to_us(elapsed),
3283 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3284 (unsigned long long)ktime_to_us(idle),
3285 (unsigned long long)pkt_dev->sofar,
3286 pkt_dev->cur_pkt_size, nr_frags);
3288 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3289 ktime_to_ns(elapsed));
3291 if (pkt_dev->n_imix_entries > 0) {
3293 struct imix_pkt *entry;
3296 for (i = 0; i < pkt_dev->n_imix_entries; i++) {
3297 entry = &pkt_dev->imix_entries[i];
3298 bps += entry->size * entry->count_so_far;
3300 bps = div64_u64(bps * 8 * NSEC_PER_SEC, ktime_to_ns(elapsed));
3302 bps = pps * 8 * pkt_dev->cur_pkt_size;
3306 do_div(mbps, 1000000);
3307 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3308 (unsigned long long)pps,
3309 (unsigned long long)mbps,
3310 (unsigned long long)bps,
3311 (unsigned long long)pkt_dev->errors);
3314 /* Set stopped-at timer, remove from running list, do counters & statistics */
3315 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3317 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3319 if (!pkt_dev->running) {
3320 pr_warn("interface: %s is already stopped\n",
3325 pkt_dev->running = 0;
3326 kfree_skb(pkt_dev->skb);
3327 pkt_dev->skb = NULL;
3328 pkt_dev->stopped_at = ktime_get();
3330 show_results(pkt_dev, nr_frags);
3335 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3337 struct pktgen_dev *pkt_dev, *best = NULL;
3340 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3341 if (!pkt_dev->running)
3345 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3353 static void pktgen_stop(struct pktgen_thread *t)
3355 struct pktgen_dev *pkt_dev;
3361 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3362 pktgen_stop_device(pkt_dev);
3369 * one of our devices needs to be removed - find it
3372 static void pktgen_rem_one_if(struct pktgen_thread *t)
3374 struct list_head *q, *n;
3375 struct pktgen_dev *cur;
3379 list_for_each_safe(q, n, &t->if_list) {
3380 cur = list_entry(q, struct pktgen_dev, list);
3382 if (!cur->removal_mark)
3385 kfree_skb(cur->skb);
3388 pktgen_remove_device(t, cur);
3394 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3396 struct list_head *q, *n;
3397 struct pktgen_dev *cur;
3401 /* Remove all devices, free mem */
3403 list_for_each_safe(q, n, &t->if_list) {
3404 cur = list_entry(q, struct pktgen_dev, list);
3406 kfree_skb(cur->skb);
3409 pktgen_remove_device(t, cur);
3413 static void pktgen_rem_thread(struct pktgen_thread *t)
3415 /* Remove from the thread list */
3416 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
3419 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3421 ktime_t idle_start = ktime_get();
3423 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3426 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3428 ktime_t idle_start = ktime_get();
3430 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
3431 if (signal_pending(current))
3435 pktgen_resched(pkt_dev);
3439 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3442 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3444 unsigned int burst = READ_ONCE(pkt_dev->burst);
3445 struct net_device *odev = pkt_dev->odev;
3446 struct netdev_queue *txq;
3447 struct sk_buff *skb;
3450 /* If device is offline, then don't send */
3451 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3452 pktgen_stop_device(pkt_dev);
3456 /* This is max DELAY, this has special meaning of
3459 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3460 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3464 /* If no skb or clone count exhausted then get new one */
3465 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3466 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3467 /* build a new pkt */
3468 kfree_skb(pkt_dev->skb);
3470 pkt_dev->skb = fill_packet(odev, pkt_dev);
3471 if (pkt_dev->skb == NULL) {
3472 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
3474 pkt_dev->clone_count--; /* back out increment, OOM */
3477 pkt_dev->last_pkt_size = pkt_dev->skb->len;
3478 pkt_dev->clone_count = 0; /* reset counter */
3481 if (pkt_dev->delay && pkt_dev->last_ok)
3482 spin(pkt_dev, pkt_dev->next_tx);
3484 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3486 skb->protocol = eth_type_trans(skb, skb->dev);
3487 refcount_add(burst, &skb->users);
3490 ret = netif_receive_skb(skb);
3491 if (ret == NET_RX_DROP)
3495 if (refcount_read(&skb->users) != burst) {
3496 /* skb was queued by rps/rfs or taps,
3497 * so cannot reuse this skb
3499 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
3500 /* get out of the loop and wait
3501 * until skb is consumed
3505 /* skb was 'freed' by stack, so clean few
3508 skb_reset_redirect(skb);
3509 } while (--burst > 0);
3510 goto out; /* Skips xmit_mode M_START_XMIT */
3511 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3513 refcount_inc(&pkt_dev->skb->users);
3515 ret = dev_queue_xmit(pkt_dev->skb);
3517 case NET_XMIT_SUCCESS:
3520 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3524 /* These are all valid return codes for a qdisc but
3525 * indicate packets are being dropped or will likely
3528 case NETDEV_TX_BUSY:
3529 /* qdisc may call dev_hard_start_xmit directly in cases
3530 * where no queues exist e.g. loopback device, virtual
3531 * devices, etc. In this case we need to handle
3536 net_info_ratelimited("%s xmit error: %d\n",
3537 pkt_dev->odevname, ret);
3543 txq = skb_get_tx_queue(odev, pkt_dev->skb);
3547 HARD_TX_LOCK(odev, txq, smp_processor_id());
3549 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3550 pkt_dev->last_ok = 0;
3553 refcount_add(burst, &pkt_dev->skb->users);
3556 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
3560 pkt_dev->last_ok = 1;
3563 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3564 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3569 /* skb has been consumed */
3572 default: /* Drivers are not supposed to return other values! */
3573 net_info_ratelimited("%s xmit error: %d\n",
3574 pkt_dev->odevname, ret);
3577 case NETDEV_TX_BUSY:
3578 /* Retry it next time */
3579 refcount_dec(&(pkt_dev->skb->users));
3580 pkt_dev->last_ok = 0;
3582 if (unlikely(burst))
3583 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
3585 HARD_TX_UNLOCK(odev, txq);
3590 /* If pkt_dev->count is zero, then run forever */
3591 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3592 pktgen_wait_for_skb(pkt_dev);
3594 /* Done with this */
3595 pktgen_stop_device(pkt_dev);
3600 * Main loop of the thread goes here
3603 static int pktgen_thread_worker(void *arg)
3606 struct pktgen_thread *t = arg;
3607 struct pktgen_dev *pkt_dev = NULL;
3610 WARN_ON(smp_processor_id() != cpu);
3612 init_waitqueue_head(&t->queue);
3613 complete(&t->start_done);
3615 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3619 while (!kthread_should_stop()) {
3620 pkt_dev = next_to_run(t);
3622 if (unlikely(!pkt_dev && t->control == 0)) {
3623 if (t->net->pktgen_exiting)
3625 wait_event_interruptible_timeout(t->queue,
3632 if (likely(pkt_dev)) {
3633 pktgen_xmit(pkt_dev);
3636 pktgen_resched(pkt_dev);
3641 if (t->control & T_STOP) {
3643 t->control &= ~(T_STOP);
3646 if (t->control & T_RUN) {
3648 t->control &= ~(T_RUN);
3651 if (t->control & T_REMDEVALL) {
3652 pktgen_rem_all_ifs(t);
3653 t->control &= ~(T_REMDEVALL);
3656 if (t->control & T_REMDEV) {
3657 pktgen_rem_one_if(t);
3658 t->control &= ~(T_REMDEV);
3664 pr_debug("%s stopping all device\n", t->tsk->comm);
3667 pr_debug("%s removing all device\n", t->tsk->comm);
3668 pktgen_rem_all_ifs(t);
3670 pr_debug("%s removing thread\n", t->tsk->comm);
3671 pktgen_rem_thread(t);
3676 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3677 const char *ifname, bool exact)
3679 struct pktgen_dev *p, *pkt_dev = NULL;
3680 size_t len = strlen(ifname);
3683 list_for_each_entry_rcu(p, &t->if_list, list)
3684 if (strncmp(p->odevname, ifname, len) == 0) {
3685 if (p->odevname[len]) {
3686 if (exact || p->odevname[len] != '@')
3694 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
3699 * Adds a dev at front of if_list.
3702 static int add_dev_to_thread(struct pktgen_thread *t,
3703 struct pktgen_dev *pkt_dev)
3707 /* This function cannot be called concurrently, as its called
3708 * under pktgen_thread_lock mutex, but it can run from
3709 * userspace on another CPU than the kthread. The if_lock()
3710 * is used here to sync with concurrent instances of
3711 * _rem_dev_from_if_list() invoked via kthread, which is also
3712 * updating the if_list */
3715 if (pkt_dev->pg_thread) {
3716 pr_err("ERROR: already assigned to a thread\n");
3721 pkt_dev->running = 0;
3722 pkt_dev->pg_thread = t;
3723 list_add_rcu(&pkt_dev->list, &t->if_list);
3730 /* Called under thread lock */
3732 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3734 struct pktgen_dev *pkt_dev;
3736 int node = cpu_to_node(t->cpu);
3738 /* We don't allow a device to be on several threads */
3740 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3742 pr_err("ERROR: interface already used\n");
3746 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3750 strcpy(pkt_dev->odevname, ifname);
3751 pkt_dev->flows = vzalloc_node(array_size(MAX_CFLOWS,
3752 sizeof(struct flow_state)),
3754 if (pkt_dev->flows == NULL) {
3759 pkt_dev->removal_mark = 0;
3760 pkt_dev->nfrags = 0;
3761 pkt_dev->delay = pg_delay_d;
3762 pkt_dev->count = pg_count_d;
3764 pkt_dev->udp_src_min = 9; /* sink port */
3765 pkt_dev->udp_src_max = 9;
3766 pkt_dev->udp_dst_min = 9;
3767 pkt_dev->udp_dst_max = 9;
3768 pkt_dev->vlan_p = 0;
3769 pkt_dev->vlan_cfi = 0;
3770 pkt_dev->vlan_id = 0xffff;
3771 pkt_dev->svlan_p = 0;
3772 pkt_dev->svlan_cfi = 0;
3773 pkt_dev->svlan_id = 0xffff;
3775 pkt_dev->node = NUMA_NO_NODE;
3777 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
3780 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3781 pkt_dev->clone_skb = pg_clone_skb_d;
3783 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
3784 &pktgen_if_proc_ops, pkt_dev);
3785 if (!pkt_dev->entry) {
3786 pr_err("cannot create %s/%s procfs entry\n",
3787 PG_PROC_DIR, ifname);
3792 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3793 pkt_dev->ipsproto = IPPROTO_ESP;
3795 /* xfrm tunnel mode needs additional dst to extract outter
3796 * ip header protocol/ttl/id field, here creat a phony one.
3797 * instead of looking for a valid rt, which definitely hurting
3798 * performance under such circumstance.
3800 pkt_dev->dstops.family = AF_INET;
3801 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3802 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3803 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3804 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
3807 return add_dev_to_thread(t, pkt_dev);
3809 dev_put(pkt_dev->odev);
3814 vfree(pkt_dev->flows);
3819 static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
3821 struct pktgen_thread *t;
3822 struct proc_dir_entry *pe;
3823 struct task_struct *p;
3825 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3828 pr_err("ERROR: out of memory, can't create new thread\n");
3832 mutex_init(&t->if_lock);
3835 INIT_LIST_HEAD(&t->if_list);
3837 list_add_tail(&t->th_list, &pn->pktgen_threads);
3838 init_completion(&t->start_done);
3840 p = kthread_create_on_node(pktgen_thread_worker,
3843 "kpktgend_%d", cpu);
3845 pr_err("kthread_create_on_node() failed for cpu %d\n", t->cpu);
3846 list_del(&t->th_list);
3850 kthread_bind(p, cpu);
3853 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
3854 &pktgen_thread_proc_ops, t);
3856 pr_err("cannot create %s/%s procfs entry\n",
3857 PG_PROC_DIR, t->tsk->comm);
3859 list_del(&t->th_list);
3867 wait_for_completion(&t->start_done);
3873 * Removes a device from the thread if_list.
3875 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3876 struct pktgen_dev *pkt_dev)
3878 struct list_head *q, *n;
3879 struct pktgen_dev *p;
3882 list_for_each_safe(q, n, &t->if_list) {
3883 p = list_entry(q, struct pktgen_dev, list);
3885 list_del_rcu(&p->list);
3890 static int pktgen_remove_device(struct pktgen_thread *t,
3891 struct pktgen_dev *pkt_dev)
3893 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
3895 if (pkt_dev->running) {
3896 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
3897 pktgen_stop_device(pkt_dev);
3900 /* Dis-associate from the interface */
3902 if (pkt_dev->odev) {
3903 dev_put(pkt_dev->odev);
3904 pkt_dev->odev = NULL;
3907 /* Remove proc before if_list entry, because add_device uses
3908 * list to determine if interface already exist, avoid race
3909 * with proc_create_data() */
3910 proc_remove(pkt_dev->entry);
3912 /* And update the thread if_list */
3913 _rem_dev_from_if_list(t, pkt_dev);
3918 vfree(pkt_dev->flows);
3920 put_page(pkt_dev->page);
3921 kfree_rcu(pkt_dev, rcu);
3925 static int __net_init pg_net_init(struct net *net)
3927 struct pktgen_net *pn = net_generic(net, pg_net_id);
3928 struct proc_dir_entry *pe;
3932 INIT_LIST_HEAD(&pn->pktgen_threads);
3933 pn->pktgen_exiting = false;
3934 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3935 if (!pn->proc_dir) {
3936 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3939 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_proc_ops);
3941 pr_err("cannot create %s procfs entry\n", PGCTRL);
3946 for_each_online_cpu(cpu) {
3949 err = pktgen_create_thread(cpu, pn);
3951 pr_warn("Cannot create thread for cpu %d (%d)\n",
3955 if (list_empty(&pn->pktgen_threads)) {
3956 pr_err("Initialization failed for all threads\n");
3964 remove_proc_entry(PGCTRL, pn->proc_dir);
3966 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3970 static void __net_exit pg_net_exit(struct net *net)
3972 struct pktgen_net *pn = net_generic(net, pg_net_id);
3973 struct pktgen_thread *t;
3974 struct list_head *q, *n;
3977 /* Stop all interfaces & threads */
3978 pn->pktgen_exiting = true;
3980 mutex_lock(&pktgen_thread_lock);
3981 list_splice_init(&pn->pktgen_threads, &list);
3982 mutex_unlock(&pktgen_thread_lock);
3984 list_for_each_safe(q, n, &list) {
3985 t = list_entry(q, struct pktgen_thread, th_list);
3986 list_del(&t->th_list);
3987 kthread_stop(t->tsk);
3988 put_task_struct(t->tsk);
3992 remove_proc_entry(PGCTRL, pn->proc_dir);
3993 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3996 static struct pernet_operations pg_net_ops = {
3997 .init = pg_net_init,
3998 .exit = pg_net_exit,
4000 .size = sizeof(struct pktgen_net),
4003 static int __init pg_init(void)
4007 pr_info("%s", version);
4008 ret = register_pernet_subsys(&pg_net_ops);
4011 ret = register_netdevice_notifier(&pktgen_notifier_block);
4013 unregister_pernet_subsys(&pg_net_ops);
4018 static void __exit pg_cleanup(void)
4020 unregister_netdevice_notifier(&pktgen_notifier_block);
4021 unregister_pernet_subsys(&pg_net_ops);
4022 /* Don't need rcu_barrier() due to use of kfree_rcu() */
4025 module_init(pg_init);
4026 module_exit(pg_cleanup);
4028 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
4029 MODULE_DESCRIPTION("Packet Generator tool");
4030 MODULE_LICENSE("GPL");
4031 MODULE_VERSION(VERSION);
4032 module_param(pg_count_d, int, 0);
4033 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
4034 module_param(pg_delay_d, int, 0);
4035 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
4036 module_param(pg_clone_skb_d, int, 0);
4037 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
4038 module_param(debug, int, 0);
4039 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");