blob: 84b62cd7bc57a96c04622e99c8a48cc5b03d1b66 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Authors:
4 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
5 * Uppsala University and
6 * Swedish University of Agricultural Sciences
7 *
8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * Ben Greear <greearb@candelatech.com>
Jan Engelhardt96de0e22007-10-19 23:21:04 +020010 * Jens Låås <jens.laas@data.slu.se>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090013 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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.
17 *
18 * Additional hacking by:
19 *
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>
41 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090042 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090045 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * clones.
48 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090049 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * --ro
51 *
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>
55 *
56 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
57 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * 021124 Finished major redesign and rewrite for new functionality.
Mauro Carvalho Chehabc1e4535f2020-04-30 18:04:13 +020059 * See Documentation/networking/pktgen.rst for how to use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
61 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090062 * 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
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +020066 * way.
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).
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090070 * By design there should only be *one* "controlling" process. In practice
71 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070073 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * --ro
78 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090079 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * memleak 030710- KJP
81 *
82 * First "ranges" functionality for ipv6 030726 --ro
83 *
84 * Included flow support. 030802 ANK.
85 *
86 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090087 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
89 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
90 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090091 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * <shemminger@osdl.org> 040923
93 *
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -050094 * Randy Dunlap fixed u64 printk compiler warning
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
96 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
97 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
101 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900102 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800104 *
105 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
106 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700107 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
108 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700109 * Fixed src_mac command to set source mac of packet to value specified in
110 * command by Adit Ranadive <adit.262@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Joe Perchesf9467ea2010-06-21 12:29:14 +0000112
113#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#include <linux/sys.h>
116#include <linux/types.h>
117#include <linux/module.h>
118#include <linux/moduleparam.h>
119#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800120#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <linux/sched.h>
122#include <linux/slab.h>
123#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#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>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800130#include <linux/capability.h>
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700131#include <linux/hrtimer.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700132#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#include <linux/delay.h>
134#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800135#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#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>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700143#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#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>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700149#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800151#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800152#include <linux/kthread.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700153#include <linux/prefetch.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800154#include <linux/mmzone.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200155#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#include <net/checksum.h>
157#include <net/ipv6.h>
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200158#include <net/udp.h>
Stephen Rothwell73d94e92013-07-29 16:21:26 +1000159#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700161#ifdef CONFIG_XFRM
162#include <net/xfrm.h>
163#endif
Cong Wang4e58a022013-01-28 19:55:53 +0000164#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#include <asm/byteorder.h>
166#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700167#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000168#include <linux/io.h>
169#include <linux/timex.h>
170#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800172#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jesper Dangaard Brouer40207262015-05-21 12:16:56 +0200174#define VERSION "2.75"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800176#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800177#define MPLS_STACK_BOTTOM htonl(0x00000100)
Nick Richardson52a62f82021-08-10 19:01:53 +0000178/* Max number of internet mix entries that can be specified in imix_weights. */
179#define MAX_IMIX_ENTRIES 20
Nick Richardson90149032021-08-10 19:01:54 +0000180#define IMIX_PRECISION 100 /* Precision of IMIX distribution */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Joe Perchesf9467ea2010-06-21 12:29:14 +0000182#define func_enter() pr_debug("entering %s\n", __func__);
183
Dmitry Safonov6f107c72018-01-18 18:31:35 +0000184#define PKT_FLAGS \
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*/ \
203
204#define pf(flag) flag##_SHIFT,
205enum pkt_flags {
206 PKT_FLAGS
207};
208#undef pf
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/* Device flag bits */
Dmitry Safonov6f107c72018-01-18 18:31:35 +0000211#define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
212PKT_FLAGS
213#undef pf
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000215#define pf(flag) __stringify(flag),
216static char *pkt_flag_names[] = {
217 PKT_FLAGS
218};
219#undef pf
220
221#define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000224#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200229/* Xmit modes */
230#define M_START_XMIT 0 /* Default normal TX */
231#define M_NETIF_RECEIVE 1 /* Inject packets into stack */
John Fastabend0967f242016-07-02 14:12:54 -0700232#define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200233
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200234/* If lock -- protects updating of if_list */
Eric Dumazet9a0b1e82016-10-15 17:50:49 +0200235#define if_lock(t) mutex_lock(&(t->if_lock));
236#define if_unlock(t) mutex_unlock(&(t->if_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238/* Used to help with determining the pkts on receive */
239#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700240#define PG_PROC_DIR "pktgen"
241#define PGCTRL "pgctrl"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243#define MAX_CFLOWS 65536
244
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700245#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
246#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
247
Nick Richardson52a62f82021-08-10 19:01:53 +0000248struct imix_pkt {
249 u64 size;
250 u64 weight;
251 u64 count_so_far;
252};
253
Luiz Capitulino222f1802006-03-20 22:16:13 -0800254struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800255 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800256 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700257#ifdef CONFIG_XFRM
258 struct xfrm_state *x;
259#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700260 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700263/* flow flag bits */
264#define F_INIT (1<<0) /* flow has been initialized */
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /*
268 * Try to keep frequent/infrequent used vars. separated.
269 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800270 struct proc_dir_entry *entry; /* proc file */
271 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000272 struct list_head list; /* chaining in the thread's run-queue */
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200273 struct rcu_head rcu; /* freed by RCU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000275 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800276
277 /* If min != max, then we will either do a linear iteration, or
278 * we will do a random selection from within the range.
279 */
280 __u32 flags;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200281 int xmit_mode;
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000282 int min_pkt_size;
283 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700284 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800285 int nfrags;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200286 int removal_mark; /* non-zero => the device is marked for
287 * removal by worker thread */
288
Eric Dumazet26ad7872011-01-25 13:26:05 -0800289 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000290 u64 delay; /* nano-seconds */
291
Luiz Capitulino222f1802006-03-20 22:16:13 -0800292 __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 */
John Fastabendf466dba2009-12-23 22:02:57 -0800295 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Luiz Capitulino222f1802006-03-20 22:16:13 -0800297 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Luiz Capitulino222f1802006-03-20 22:16:13 -0800299 __u32 clone_count;
300 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000301 * Or a failed transmit of some sort?
302 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800303 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000304 ktime_t next_tx;
305 ktime_t started_at;
306 ktime_t stopped_at;
307 u64 idle_acc; /* nano-seconds */
308
Luiz Capitulino222f1802006-03-20 22:16:13 -0800309 __u32 seq_num;
310
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000311 int clone_skb; /*
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.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800319 */
320
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 */
325
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800331 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Luiz Capitulino222f1802006-03-20 22:16:13 -0800336 /* If we're doing ranges, random or incremental, then this
337 * defines the min/max for those ranges.
338 */
Al Viro252e33462006-11-14 20:48:11 -0800339 __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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Luiz Capitulino222f1802006-03-20 22:16:13 -0800344 __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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700349 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000350 __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) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700354
Nick Richardson52a62f82021-08-10 19:01:53 +0000355 /* IMIX */
356 unsigned int n_imix_entries;
357 struct imix_pkt imix_entries[MAX_IMIX_ENTRIES];
Nick Richardson90149032021-08-10 19:01:54 +0000358 /* Maps 0-IMIX_PRECISION range to imix_entry based on probability*/
359 __u8 imix_distribution[IMIX_PRECISION];
Nick Richardson52a62f82021-08-10 19:01:53 +0000360
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800361 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000362 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800363 __be32 labels[MAX_MPLS_LABELS];
364
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700365 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
366 __u8 vlan_p;
367 __u8 vlan_cfi;
368 __u16 vlan_id; /* 0xffff means no vlan tag */
369
370 __u8 svlan_p;
371 __u8 svlan_cfi;
372 __u16 svlan_id; /* 0xffff means no svlan tag */
373
Luiz Capitulino222f1802006-03-20 22:16:13 -0800374 __u32 src_mac_count; /* How many MACs to iterate through */
375 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Luiz Capitulino222f1802006-03-20 22:16:13 -0800377 unsigned char dst_mac[ETH_ALEN];
378 unsigned char src_mac[ETH_ALEN];
379
380 __u32 cur_dst_mac_offset;
381 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800382 __be32 cur_saddr;
383 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700384 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800385 __u16 cur_udp_dst;
386 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700387 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800388 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800389 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800390
391 __u8 hh[14];
392 /* = {
393 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
394
395 We fill in SRC address later
396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397 0x08, 0x00
398 };
399 */
400 __u16 pad; /* pad out the hh struct to an even 16 bytes */
401
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000402 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800403 * are transmitting the same one multiple times
404 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000405 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
408 * device.
409 * Set when the user specifies the out-going
410 * device name (not when the inject is
411 * started as it used to do.)
412 */
Eric Dumazet035f1f22021-12-06 17:30:35 -0800413 netdevice_tracker dev_tracker;
Eric Dumazet593f63b2009-11-23 01:44:37 +0000414 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000416 unsigned int cflows; /* Concurrent flows (config) */
417 unsigned int lflow; /* Flow length (config) */
418 unsigned int nflows; /* accumulated flows (stats) */
419 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700420
421 u16 queue_map_min;
422 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000423 __u32 skb_priority; /* skb priority field */
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -0700424 unsigned int burst; /* number of duplicated packets to burst */
Robert Olssone99b99b2010-03-18 22:44:30 +0000425 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700426
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700427#ifdef CONFIG_XFRM
428 __u8 ipsmode; /* IPSEC mode (config) */
429 __u8 ipsproto; /* IPSEC type (config) */
Fan Dude4aee72014-01-03 11:18:30 +0800430 __u32 spi;
David Millerb6ca8bd2017-11-28 15:45:44 -0500431 struct xfrm_dst xdst;
Fan Ducf93d47e2014-01-03 11:18:31 +0800432 struct dst_ops dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700433#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800434 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435};
436
437struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800438 __be32 pgh_magic;
439 __be32 seq_num;
440 __be32 tv_sec;
441 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442};
443
Cong Wang4e58a022013-01-28 19:55:53 +0000444
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300445static unsigned int pg_net_id __read_mostly;
Cong Wang4e58a022013-01-28 19:55:53 +0000446
447struct pktgen_net {
448 struct net *net;
449 struct proc_dir_entry *proc_dir;
450 struct list_head pktgen_threads;
451 bool pktgen_exiting;
452};
Eric Dumazet551eaff2010-11-21 10:26:44 -0800453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454struct pktgen_thread {
Eric Dumazet9a0b1e82016-10-15 17:50:49 +0200455 struct mutex if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800456 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800457 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800458 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800459 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000461 /* Field for thread to receive "posted" events terminate,
462 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800463
464 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int cpu;
466
Luiz Capitulino222f1802006-03-20 22:16:13 -0800467 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700468 struct completion start_done;
Cong Wang4e58a022013-01-28 19:55:53 +0000469 struct pktgen_net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470};
471
472#define REMOVE 1
473#define FIND 0
474
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000475static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000476 "Packet Generator for packet performance testing. "
477 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Luiz Capitulino222f1802006-03-20 22:16:13 -0800479static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
480static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
481static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800482 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
Cong Wang4e58a022013-01-28 19:55:53 +0000484static void pktgen_run_all_threads(struct pktgen_net *pn);
485static void pktgen_reset_all_threads(struct pktgen_net *pn);
Yejune Dengcda9de02021-06-07 10:37:41 +0800486static void pktgen_stop_all_threads(struct pktgen_net *pn);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000487
Luiz Capitulino222f1802006-03-20 22:16:13 -0800488static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Nick Richardson90149032021-08-10 19:01:54 +0000490static void fill_imix_distribution(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000493static int pg_count_d __read_mostly = 1000;
494static int pg_delay_d __read_mostly;
495static int pg_clone_skb_d __read_mostly;
496static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Luiz Capitulino222fa072006-03-20 22:24:27 -0800498static DEFINE_MUTEX(pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500static struct notifier_block pktgen_notifier_block = {
501 .notifier_call = pktgen_device_event,
502};
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900505 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *
507 */
508
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700509static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800510{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000511 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000515static ssize_t pgctrl_write(struct file *file, const char __user *buf,
516 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700518 char data[128];
Cong Wang4e58a022013-01-28 19:55:53 +0000519 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Mathias Krause09455742014-02-21 21:38:35 +0100521 if (!capable(CAP_NET_ADMIN))
522 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Mathias Krause20b0c712014-02-21 21:38:34 +0100524 if (count == 0)
525 return -EINVAL;
526
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700527 if (count > sizeof(data))
528 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Mathias Krause09455742014-02-21 21:38:35 +0100530 if (copy_from_user(data, buf, count))
531 return -EFAULT;
532
Mathias Krause20b0c712014-02-21 21:38:34 +0100533 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Luiz Capitulino222f1802006-03-20 22:16:13 -0800535 if (!strcmp(data, "stop"))
Yejune Dengcda9de02021-06-07 10:37:41 +0800536 pktgen_stop_all_threads(pn);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800537 else if (!strcmp(data, "start"))
Cong Wang4e58a022013-01-28 19:55:53 +0000538 pktgen_run_all_threads(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800539 else if (!strcmp(data, "reset"))
Cong Wang4e58a022013-01-28 19:55:53 +0000540 pktgen_reset_all_threads(pn);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800541 else
Jesper Dangaard Brouer40207262015-05-21 12:16:56 +0200542 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Mathias Krause09455742014-02-21 21:38:35 +0100544 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700547static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Muchun Song359745d2022-01-21 22:14:23 -0800549 return single_open(file, pgctrl_show, pde_data(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700550}
551
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800552static const struct proc_ops pktgen_proc_ops = {
553 .proc_open = pgctrl_open,
554 .proc_read = seq_read,
555 .proc_lseek = seq_lseek,
556 .proc_write = pgctrl_write,
557 .proc_release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700558};
559
560static int pktgen_if_show(struct seq_file *seq, void *v)
561{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000562 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000563 ktime_t stopped;
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000564 unsigned int i;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000565 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Luiz Capitulino222f1802006-03-20 22:16:13 -0800567 seq_printf(seq,
568 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
569 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
570 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Nick Richardson52a62f82021-08-10 19:01:53 +0000572 if (pkt_dev->n_imix_entries > 0) {
573 seq_puts(seq, " imix_weights: ");
574 for (i = 0; i < pkt_dev->n_imix_entries; i++) {
575 seq_printf(seq, "%llu,%llu ",
576 pkt_dev->imix_entries[i].size,
577 pkt_dev->imix_entries[i].weight);
578 }
579 seq_puts(seq, "\n");
580 }
581
Luiz Capitulino222f1802006-03-20 22:16:13 -0800582 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000583 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
584 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000585 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Luiz Capitulino222f1802006-03-20 22:16:13 -0800587 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
588 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Robert Olsson45b270f2007-08-28 15:45:55 -0700590 seq_printf(seq,
591 " queue_map_min: %u queue_map_max: %u\n",
592 pkt_dev->queue_map_min,
593 pkt_dev->queue_map_max);
594
John Fastabend9e50e3a2010-11-16 19:12:28 +0000595 if (pkt_dev->skb_priority)
596 seq_printf(seq, " skb_priority: %u\n",
597 pkt_dev->skb_priority);
598
Luiz Capitulino222f1802006-03-20 22:16:13 -0800599 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800600 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000601 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
602 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
603 &pkt_dev->in6_saddr,
604 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
605 &pkt_dev->in6_daddr,
606 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000607 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800608 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000609 " dst_min: %s dst_max: %s\n",
610 pkt_dev->dst_min, pkt_dev->dst_max);
611 seq_printf(seq,
Jesper Dangaard Brouerd079abd2015-05-21 12:16:11 +0200612 " src_min: %s src_max: %s\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000613 pkt_dev->src_min, pkt_dev->src_max);
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700616 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Johannes Berge1749612008-10-27 15:59:26 -0700618 seq_printf(seq, "%pM ",
619 is_zero_ether_addr(pkt_dev->src_mac) ?
620 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Thomas Graf97dc48e2014-05-16 23:28:54 +0200622 seq_puts(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700623 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Luiz Capitulino222f1802006-03-20 22:16:13 -0800625 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000626 " udp_src_min: %d udp_src_max: %d"
627 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800628 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
629 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Luiz Capitulino222f1802006-03-20 22:16:13 -0800631 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800632 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700633 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800635 if (pkt_dev->nr_labels) {
Thomas Graf97dc48e2014-05-16 23:28:54 +0200636 seq_puts(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700637 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800638 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
639 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
640 }
641
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000642 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700643 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000644 pkt_dev->vlan_id, pkt_dev->vlan_p,
645 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700646
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000647 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700648 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000649 pkt_dev->svlan_id, pkt_dev->svlan_p,
650 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700651
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000652 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700653 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700654
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000655 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700656 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700657
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -0700658 if (pkt_dev->burst > 1)
659 seq_printf(seq, " burst: %d\n", pkt_dev->burst);
660
Robert Olssone99b99b2010-03-18 22:44:30 +0000661 if (pkt_dev->node >= 0)
662 seq_printf(seq, " node: %d\n", pkt_dev->node);
663
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200664 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
665 seq_puts(seq, " xmit_mode: netif_receive\n");
John Fastabend0967f242016-07-02 14:12:54 -0700666 else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
667 seq_puts(seq, " xmit_mode: xmit_queue\n");
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200668
Thomas Graf97dc48e2014-05-16 23:28:54 +0200669 seq_puts(seq, " Flags: ");
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800670
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000671 for (i = 0; i < NR_PKT_FLAGS; i++) {
672 if (i == F_FLOW_SEQ)
673 if (!pkt_dev->cflows)
674 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000676 if (pkt_dev->flags & (1 << i))
677 seq_printf(seq, "%s ", pkt_flag_names[i]);
678 else if (i == F_FLOW_SEQ)
679 seq_puts(seq, "FLOW_RND ");
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700680
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700681#ifdef CONFIG_XFRM
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000682 if (i == F_IPSEC && pkt_dev->spi)
Fan Du81013282014-01-03 11:18:33 +0800683 seq_printf(seq, "spi:%u", pkt_dev->spi);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700684#endif
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000685 }
Robert Olssone99b99b2010-03-18 22:44:30 +0000686
Luiz Capitulino222f1802006-03-20 22:16:13 -0800687 seq_puts(seq, "\n");
688
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000689 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000690 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000691 idle = pkt_dev->idle_acc;
692 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800693
694 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000695 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800696 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000697 (unsigned long long)pkt_dev->errors);
698
Nick Richardson769afb32021-08-10 19:01:55 +0000699 if (pkt_dev->n_imix_entries > 0) {
700 int i;
701
702 seq_puts(seq, " imix_size_counts: ");
703 for (i = 0; i < pkt_dev->n_imix_entries; i++) {
704 seq_printf(seq, "%llu,%llu ",
705 pkt_dev->imix_entries[i].size,
706 pkt_dev->imix_entries[i].count_so_far);
707 }
708 seq_puts(seq, "\n");
709 }
710
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000711 seq_printf(seq,
712 " started: %lluus stopped: %lluus idle: %lluus\n",
713 (unsigned long long) ktime_to_us(pkt_dev->started_at),
714 (unsigned long long) ktime_to_us(stopped),
715 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800716
717 seq_printf(seq,
718 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700719 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
720 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Luiz Capitulino222f1802006-03-20 22:16:13 -0800722 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000723 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
724 &pkt_dev->cur_in6_saddr,
725 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800726 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000727 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
728 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Luiz Capitulino222f1802006-03-20 22:16:13 -0800730 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700731 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Robert Olsson45b270f2007-08-28 15:45:55 -0700733 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
734
Luiz Capitulino222f1802006-03-20 22:16:13 -0800735 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800738 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 else
Thomas Graf97dc48e2014-05-16 23:28:54 +0200740 seq_puts(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800745
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000746static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
747 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800748{
749 int i = 0;
750 *num = 0;
751
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700752 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000753 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800754 char c;
755 *num <<= 4;
756 if (get_user(c, &user_buffer[i]))
757 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000758 value = hex_to_bin(c);
759 if (value >= 0)
760 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800761 else
762 break;
763 }
764 return i;
765}
766
Luiz Capitulino222f1802006-03-20 22:16:13 -0800767static int count_trail_chars(const char __user * user_buffer,
768 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 int i;
771
772 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800773 char c;
774 if (get_user(c, &user_buffer[i]))
775 return -EFAULT;
776 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 case '\"':
778 case '\n':
779 case '\r':
780 case '\t':
781 case ' ':
782 case '=':
783 break;
784 default:
785 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788done:
789 return i;
790}
791
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000792static long num_arg(const char __user *user_buffer, unsigned long maxlen,
793 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000795 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800797
Paul Gortmakerd6182222010-10-18 12:14:44 +0000798 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800799 char c;
800 if (get_user(c, &user_buffer[i]))
801 return -EFAULT;
802 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800804 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 } else
806 break;
807 }
808 return i;
809}
810
Luiz Capitulino222f1802006-03-20 22:16:13 -0800811static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000813 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Paul Gortmakerd6182222010-10-18 12:14:44 +0000815 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800816 char c;
817 if (get_user(c, &user_buffer[i]))
818 return -EFAULT;
819 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 case '\"':
821 case '\n':
822 case '\r':
823 case '\t':
824 case ' ':
825 goto done_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 default:
827 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return i;
832}
833
Nick Richardson52a62f82021-08-10 19:01:53 +0000834/* Parses imix entries from user buffer.
835 * The user buffer should consist of imix entries separated by spaces
836 * where each entry consists of size and weight delimited by commas.
837 * "size1,weight_1 size2,weight_2 ... size_n,weight_n" for example.
838 */
839static ssize_t get_imix_entries(const char __user *buffer,
840 struct pktgen_dev *pkt_dev)
841{
842 const int max_digits = 10;
843 int i = 0;
844 long len;
845 char c;
846
847 pkt_dev->n_imix_entries = 0;
848
849 do {
850 unsigned long weight;
851 unsigned long size;
852
853 len = num_arg(&buffer[i], max_digits, &size);
854 if (len < 0)
855 return len;
856 i += len;
857 if (get_user(c, &buffer[i]))
858 return -EFAULT;
859 /* Check for comma between size_i and weight_i */
860 if (c != ',')
861 return -EINVAL;
862 i++;
863
864 if (size < 14 + 20 + 8)
865 size = 14 + 20 + 8;
866
867 len = num_arg(&buffer[i], max_digits, &weight);
868 if (len < 0)
869 return len;
870 if (weight <= 0)
871 return -EINVAL;
872
873 pkt_dev->imix_entries[pkt_dev->n_imix_entries].size = size;
874 pkt_dev->imix_entries[pkt_dev->n_imix_entries].weight = weight;
875
876 i += len;
877 if (get_user(c, &buffer[i]))
878 return -EFAULT;
879
880 i++;
881 pkt_dev->n_imix_entries++;
882
883 if (pkt_dev->n_imix_entries > MAX_IMIX_ENTRIES)
884 return -E2BIG;
885 } while (c == ' ');
886
887 return i;
888}
889
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800890static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
891{
Eric Dumazet95c96172012-04-15 05:58:06 +0000892 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800893 char c;
894 ssize_t i = 0;
895 int len;
896
897 pkt_dev->nr_labels = 0;
898 do {
899 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700900 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800901 if (len <= 0)
902 return len;
903 pkt_dev->labels[n] = htonl(tmp);
904 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
905 pkt_dev->flags |= F_MPLS_RND;
906 i += len;
907 if (get_user(c, &buffer[i]))
908 return -EFAULT;
909 i++;
910 n++;
911 if (n >= MAX_MPLS_LABELS)
912 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700913 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800914
915 pkt_dev->nr_labels = n;
916 return i;
917}
918
Dmitry Safonov52e12d52018-01-18 18:31:37 +0000919static __u32 pktgen_read_flag(const char *f, bool *disable)
920{
921 __u32 i;
922
923 if (f[0] == '!') {
924 *disable = true;
925 f++;
926 }
927
928 for (i = 0; i < NR_PKT_FLAGS; i++) {
929 if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
930 continue;
931
932 /* allow only disabling ipv6 flag */
933 if (!*disable && i == IPV6_SHIFT)
934 continue;
935
936 if (strcmp(f, pkt_flag_names[i]) == 0)
937 return 1 << i;
938 }
939
940 if (strcmp(f, "FLOW_RND") == 0) {
941 *disable = !*disable;
942 return F_FLOW_SEQ;
943 }
944
945 return 0;
946}
947
Luiz Capitulino222f1802006-03-20 22:16:13 -0800948static ssize_t pktgen_if_write(struct file *file,
949 const char __user * user_buffer, size_t count,
950 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Joe Perches8a994a72010-07-12 10:50:23 +0000952 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800953 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000954 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 char name[16], valstr[32];
956 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800957 char *pg_result = NULL;
958 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960
961 pg_result = &(pkt_dev->result[0]);
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (count < 1) {
Joe Perches294a0b7f2014-09-09 21:17:30 -0700964 pr_warn("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -EINVAL;
966 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800967
Paul Gortmakerd6182222010-10-18 12:14:44 +0000968 max = count;
969 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800970 if (tmp < 0) {
Joe Perches294a0b7f2014-09-09 21:17:30 -0700971 pr_warn("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800972 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000974 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Read variable name */
977
978 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000979 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800980 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800983 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -EFAULT;
985 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800986
987 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800989 if (len < 0)
990 return len;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 i += len;
993
994 if (debug) {
Arnd Bergmanna870a022018-03-13 21:58:39 +0100995 size_t copy = min_t(size_t, count + 1, 1024);
996 char *tp = strndup_user(user_buffer, copy);
997
998 if (IS_ERR(tp))
999 return PTR_ERR(tp);
1000
1001 pr_debug("%s,%zu buffer -:%s:-\n", name, count, tp);
Gustavo A. R. Silva29d1df72018-03-14 03:07:27 -05001002 kfree(tp);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if (!strcmp(name, "min_pkt_size")) {
1006 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001007 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001008 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001011 if (value < 14 + 20 + 8)
1012 value = 14 + 20 + 8;
1013 if (value != pkt_dev->min_pkt_size) {
1014 pkt_dev->min_pkt_size = value;
1015 pkt_dev->cur_pkt_size = value;
1016 }
Ye Bin32be4252020-09-30 09:08:37 +08001017 sprintf(pg_result, "OK: min_pkt_size=%d",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001018 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return count;
1020 }
1021
Luiz Capitulino222f1802006-03-20 22:16:13 -08001022 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001024 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001025 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001028 if (value < 14 + 20 + 8)
1029 value = 14 + 20 + 8;
1030 if (value != pkt_dev->max_pkt_size) {
1031 pkt_dev->max_pkt_size = value;
1032 pkt_dev->cur_pkt_size = value;
1033 }
Ye Bin32be4252020-09-30 09:08:37 +08001034 sprintf(pg_result, "OK: max_pkt_size=%d",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001035 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return count;
1037 }
1038
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 if (!strcmp(name, "pkt_size")) {
1042 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001043 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001044 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001047 if (value < 14 + 20 + 8)
1048 value = 14 + 20 + 8;
1049 if (value != pkt_dev->min_pkt_size) {
1050 pkt_dev->min_pkt_size = value;
1051 pkt_dev->max_pkt_size = value;
1052 pkt_dev->cur_pkt_size = value;
1053 }
Ye Bin32be4252020-09-30 09:08:37 +08001054 sprintf(pg_result, "OK: pkt_size=%d", pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return count;
1056 }
1057
Nick Richardson52a62f82021-08-10 19:01:53 +00001058 if (!strcmp(name, "imix_weights")) {
1059 if (pkt_dev->clone_skb > 0)
1060 return -EINVAL;
1061
1062 len = get_imix_entries(&user_buffer[i], pkt_dev);
1063 if (len < 0)
1064 return len;
1065
Nick Richardson90149032021-08-10 19:01:54 +00001066 fill_imix_distribution(pkt_dev);
1067
Nick Richardson52a62f82021-08-10 19:01:53 +00001068 i += len;
1069 return count;
1070 }
1071
Luiz Capitulino222f1802006-03-20 22:16:13 -08001072 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001074 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001075 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001078 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 sprintf(pg_result, "OK: debug=%u", debug);
1080 return count;
1081 }
1082
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001085 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001086 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 i += len;
1089 pkt_dev->nfrags = value;
Ye Bin32be4252020-09-30 09:08:37 +08001090 sprintf(pg_result, "OK: frags=%d", pkt_dev->nfrags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return count;
1092 }
1093 if (!strcmp(name, "delay")) {
1094 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001095 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001096 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00001099 if (value == 0x7FFFFFFF)
1100 pkt_dev->delay = ULLONG_MAX;
1101 else
Eric Dumazet9240d712009-10-03 01:39:18 +00001102 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00001103
1104 sprintf(pg_result, "OK: delay=%llu",
1105 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return count;
1107 }
Daniel Turull43d28b62010-06-09 22:49:57 +00001108 if (!strcmp(name, "rate")) {
1109 len = num_arg(&user_buffer[i], 10, &value);
1110 if (len < 0)
1111 return len;
1112
1113 i += len;
1114 if (!value)
1115 return len;
1116 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1117 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001118 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001119
1120 sprintf(pg_result, "OK: rate=%lu", value);
1121 return count;
1122 }
1123 if (!strcmp(name, "ratep")) {
1124 len = num_arg(&user_buffer[i], 10, &value);
1125 if (len < 0)
1126 return len;
1127
1128 i += len;
1129 if (!value)
1130 return len;
1131 pkt_dev->delay = NSEC_PER_SEC/value;
1132 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001133 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001134
1135 sprintf(pg_result, "OK: rate=%lu", value);
1136 return count;
1137 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001138 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001140 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001141 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001144 if (value != pkt_dev->udp_src_min) {
1145 pkt_dev->udp_src_min = value;
1146 pkt_dev->cur_udp_src = value;
1147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1149 return count;
1150 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001151 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001153 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001154 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001157 if (value != pkt_dev->udp_dst_min) {
1158 pkt_dev->udp_dst_min = value;
1159 pkt_dev->cur_udp_dst = value;
1160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1162 return count;
1163 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001164 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001166 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001167 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001170 if (value != pkt_dev->udp_src_max) {
1171 pkt_dev->udp_src_max = value;
1172 pkt_dev->cur_udp_src = value;
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1175 return count;
1176 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001177 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001179 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001180 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001183 if (value != pkt_dev->udp_dst_max) {
1184 pkt_dev->udp_dst_max = value;
1185 pkt_dev->cur_udp_dst = value;
1186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1188 return count;
1189 }
1190 if (!strcmp(name, "clone_skb")) {
1191 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001192 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001193 return len;
Nick Richardson52a62f82021-08-10 19:01:53 +00001194 /* clone_skb is not supported for netif_receive xmit_mode and
1195 * IMIX mode.
1196 */
Neil Hormand8873312011-07-26 06:05:37 +00001197 if ((value > 0) &&
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001198 ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
1199 !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
Neil Hormand8873312011-07-26 06:05:37 +00001200 return -ENOTSUPP;
Nick Richardson52a62f82021-08-10 19:01:53 +00001201 if (value > 0 && pkt_dev->n_imix_entries > 0)
1202 return -EINVAL;
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001205 pkt_dev->clone_skb = value;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1208 return count;
1209 }
1210 if (!strcmp(name, "count")) {
1211 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001212 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001213 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 i += len;
1216 pkt_dev->count = value;
1217 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001218 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return count;
1220 }
1221 if (!strcmp(name, "src_mac_count")) {
1222 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001223 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001224 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 i += len;
1227 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001228 pkt_dev->src_mac_count = value;
1229 pkt_dev->cur_src_mac_offset = 0;
1230 }
1231 sprintf(pg_result, "OK: src_mac_count=%d",
1232 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return count;
1234 }
1235 if (!strcmp(name, "dst_mac_count")) {
1236 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001237 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001238 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 i += len;
1241 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001242 pkt_dev->dst_mac_count = value;
1243 pkt_dev->cur_dst_mac_offset = 0;
1244 }
1245 sprintf(pg_result, "OK: dst_mac_count=%d",
1246 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return count;
1248 }
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07001249 if (!strcmp(name, "burst")) {
1250 len = num_arg(&user_buffer[i], 10, &value);
1251 if (len < 0)
1252 return len;
1253
1254 i += len;
John Fastabend0967f242016-07-02 14:12:54 -07001255 if ((value > 1) &&
1256 ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
1257 ((pkt_dev->xmit_mode == M_START_XMIT) &&
1258 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
Eric Dumazet52d6c8c2015-02-22 17:03:41 -08001259 return -ENOTSUPP;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07001260 pkt_dev->burst = value < 1 ? 1 : value;
Ye Bin32be4252020-09-30 09:08:37 +08001261 sprintf(pg_result, "OK: burst=%u", pkt_dev->burst);
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07001262 return count;
1263 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001264 if (!strcmp(name, "node")) {
1265 len = num_arg(&user_buffer[i], 10, &value);
1266 if (len < 0)
1267 return len;
1268
1269 i += len;
1270
1271 if (node_possible(value)) {
1272 pkt_dev->node = value;
1273 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001274 if (pkt_dev->page) {
1275 put_page(pkt_dev->page);
1276 pkt_dev->page = NULL;
1277 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001278 }
1279 else
1280 sprintf(pg_result, "ERROR: node not possible");
1281 return count;
1282 }
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001283 if (!strcmp(name, "xmit_mode")) {
1284 char f[32];
1285
1286 memset(f, 0, 32);
1287 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1288 if (len < 0)
1289 return len;
1290
1291 if (copy_from_user(f, &user_buffer[i], len))
1292 return -EFAULT;
1293 i += len;
1294
1295 if (strcmp(f, "start_xmit") == 0) {
1296 pkt_dev->xmit_mode = M_START_XMIT;
1297 } else if (strcmp(f, "netif_receive") == 0) {
1298 /* clone_skb set earlier, not supported in this mode */
1299 if (pkt_dev->clone_skb > 0)
1300 return -ENOTSUPP;
1301
1302 pkt_dev->xmit_mode = M_NETIF_RECEIVE;
Alexei Starovoitov9eea9222015-05-11 15:19:48 -07001303
1304 /* make sure new packet is allocated every time
1305 * pktgen_xmit() is called
1306 */
1307 pkt_dev->last_ok = 1;
John Fastabend0967f242016-07-02 14:12:54 -07001308 } else if (strcmp(f, "queue_xmit") == 0) {
1309 pkt_dev->xmit_mode = M_QUEUE_XMIT;
1310 pkt_dev->last_ok = 1;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001311 } else {
1312 sprintf(pg_result,
1313 "xmit_mode -:%s:- unknown\nAvailable modes: %s",
1314 f, "start_xmit, netif_receive\n");
1315 return count;
1316 }
1317 sprintf(pg_result, "OK: xmit_mode=%s", f);
1318 return count;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (!strcmp(name, "flag")) {
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001321 __u32 flag;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 char f[32];
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001323 bool disable = false;
1324
Luiz Capitulino222f1802006-03-20 22:16:13 -08001325 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001327 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001328 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (copy_from_user(f, &user_buffer[i], len))
1331 return -EFAULT;
1332 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001333
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001334 flag = pktgen_read_flag(f, &disable);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001336 if (flag) {
1337 if (disable)
1338 pkt_dev->flags &= ~flag;
1339 else
1340 pkt_dev->flags |= flag;
1341 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001342 sprintf(pg_result,
1343 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1344 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001345 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001346 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1347 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1348 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02001349 "NO_TIMESTAMP, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001350#ifdef CONFIG_XFRM
1351 "IPSEC, "
1352#endif
1353 "NODE_ALLOC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001354 return count;
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1357 return count;
1358 }
1359 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1360 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001361 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001362 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Luiz Capitulino222f1802006-03-20 22:16:13 -08001364 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001366 buf[len] = 0;
1367 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1368 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001369 strcpy(pkt_dev->dst_min, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001370 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1371 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1372 }
1373 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001374 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001375 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1377 return count;
1378 }
1379 if (!strcmp(name, "dst_max")) {
1380 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001381 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001382 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001383
Luiz Capitulino222f1802006-03-20 22:16:13 -08001384 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001386 buf[len] = 0;
1387 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1388 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001389 strcpy(pkt_dev->dst_max, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001390 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1391 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1392 }
1393 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001394 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 i += len;
1396 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1397 return count;
1398 }
1399 if (!strcmp(name, "dst6")) {
1400 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001401 if (len < 0)
1402 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 pkt_dev->flags |= F_IPV6;
1405
Luiz Capitulino222f1802006-03-20 22:16:13 -08001406 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001408 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Amerigo Wangc468fb12012-10-09 17:48:20 +00001410 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001411 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001413 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Luiz Capitulino222f1802006-03-20 22:16:13 -08001415 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001416 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Luiz Capitulino222f1802006-03-20 22:16:13 -08001418 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 sprintf(pg_result, "OK: dst6=%s", buf);
1420 return count;
1421 }
1422 if (!strcmp(name, "dst6_min")) {
1423 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001424 if (len < 0)
1425 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 pkt_dev->flags |= F_IPV6;
1428
Luiz Capitulino222f1802006-03-20 22:16:13 -08001429 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001431 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Amerigo Wangc468fb12012-10-09 17:48:20 +00001433 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001434 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001436 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001437 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001438 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Luiz Capitulino222f1802006-03-20 22:16:13 -08001440 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 sprintf(pg_result, "OK: dst6_min=%s", buf);
1442 return count;
1443 }
1444 if (!strcmp(name, "dst6_max")) {
1445 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001446 if (len < 0)
1447 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 pkt_dev->flags |= F_IPV6;
1450
Luiz Capitulino222f1802006-03-20 22:16:13 -08001451 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001453 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Amerigo Wangc468fb12012-10-09 17:48:20 +00001455 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001456 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Luiz Capitulino222f1802006-03-20 22:16:13 -08001458 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001459 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Luiz Capitulino222f1802006-03-20 22:16:13 -08001461 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 sprintf(pg_result, "OK: dst6_max=%s", buf);
1463 return count;
1464 }
1465 if (!strcmp(name, "src6")) {
1466 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001467 if (len < 0)
1468 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 pkt_dev->flags |= F_IPV6;
1471
Luiz Capitulino222f1802006-03-20 22:16:13 -08001472 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001474 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Amerigo Wangc468fb12012-10-09 17:48:20 +00001476 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001477 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001479 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Luiz Capitulino222f1802006-03-20 22:16:13 -08001481 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001482 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001483
1484 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 sprintf(pg_result, "OK: src6=%s", buf);
1486 return count;
1487 }
1488 if (!strcmp(name, "src_min")) {
1489 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001490 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001491 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001492
Luiz Capitulino222f1802006-03-20 22:16:13 -08001493 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001495 buf[len] = 0;
1496 if (strcmp(buf, pkt_dev->src_min) != 0) {
1497 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001498 strcpy(pkt_dev->src_min, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001499 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1500 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1501 }
1502 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001503 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 i += len;
1505 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1506 return count;
1507 }
1508 if (!strcmp(name, "src_max")) {
1509 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001510 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001511 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001512
Luiz Capitulino222f1802006-03-20 22:16:13 -08001513 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001515 buf[len] = 0;
1516 if (strcmp(buf, pkt_dev->src_max) != 0) {
1517 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001518 strcpy(pkt_dev->src_max, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001519 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1520 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1521 }
1522 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001523 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 i += len;
1525 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1526 return count;
1527 }
1528 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001530 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001531 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001534 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001537 if (!mac_pton(valstr, pkt_dev->dst_mac))
1538 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001540 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001541
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001542 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return count;
1544 }
1545 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001547 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001548 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001551 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001554 if (!mac_pton(valstr, pkt_dev->src_mac))
1555 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001556 /* Set up Src MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001557 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001558
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001559 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return count;
1561 }
1562
Luiz Capitulino222f1802006-03-20 22:16:13 -08001563 if (!strcmp(name, "clear_counters")) {
1564 pktgen_clear_counters(pkt_dev);
1565 sprintf(pg_result, "OK: Clearing counters.\n");
1566 return count;
1567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 if (!strcmp(name, "flows")) {
1570 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001571 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001572 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 i += len;
1575 if (value > MAX_CFLOWS)
1576 value = MAX_CFLOWS;
1577
1578 pkt_dev->cflows = value;
1579 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1580 return count;
1581 }
Fan Du6bae9192014-01-10 14:39:13 +08001582#ifdef CONFIG_XFRM
Fan Dude4aee72014-01-03 11:18:30 +08001583 if (!strcmp(name, "spi")) {
1584 len = num_arg(&user_buffer[i], 10, &value);
1585 if (len < 0)
1586 return len;
1587
1588 i += len;
1589 pkt_dev->spi = value;
1590 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1591 return count;
1592 }
Fan Du6bae9192014-01-10 14:39:13 +08001593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!strcmp(name, "flowlen")) {
1595 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001596 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001597 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 i += len;
1600 pkt_dev->lflow = value;
1601 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1602 return count;
1603 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001604
Robert Olsson45b270f2007-08-28 15:45:55 -07001605 if (!strcmp(name, "queue_map_min")) {
1606 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001607 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001608 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001609
Robert Olsson45b270f2007-08-28 15:45:55 -07001610 i += len;
1611 pkt_dev->queue_map_min = value;
1612 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1613 return count;
1614 }
1615
1616 if (!strcmp(name, "queue_map_max")) {
1617 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001618 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001619 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001620
Robert Olsson45b270f2007-08-28 15:45:55 -07001621 i += len;
1622 pkt_dev->queue_map_max = value;
1623 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1624 return count;
1625 }
1626
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001627 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001628 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001629
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001630 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001631 if (len < 0)
1632 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001633 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001634 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001635 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001636 cnt += sprintf(pg_result + cnt,
1637 "%08x%s", ntohl(pkt_dev->labels[n]),
1638 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001639
1640 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1641 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1642 pkt_dev->svlan_id = 0xffff;
1643
1644 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001645 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001646 }
1647 return count;
1648 }
1649
1650 if (!strcmp(name, "vlan_id")) {
1651 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001652 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001653 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001654
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001655 i += len;
1656 if (value <= 4095) {
1657 pkt_dev->vlan_id = value; /* turn on VLAN */
1658
1659 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001660 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001661
1662 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001663 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001664
1665 pkt_dev->nr_labels = 0; /* turn off MPLS */
1666 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1667 } else {
1668 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1669 pkt_dev->svlan_id = 0xffff;
1670
1671 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001672 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001673 }
1674 return count;
1675 }
1676
1677 if (!strcmp(name, "vlan_p")) {
1678 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001679 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001680 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001681
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001682 i += len;
1683 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1684 pkt_dev->vlan_p = value;
1685 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1686 } else {
1687 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1688 }
1689 return count;
1690 }
1691
1692 if (!strcmp(name, "vlan_cfi")) {
1693 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001694 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001695 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001696
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001697 i += len;
1698 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1699 pkt_dev->vlan_cfi = value;
1700 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1701 } else {
1702 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1703 }
1704 return count;
1705 }
1706
1707 if (!strcmp(name, "svlan_id")) {
1708 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001709 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001710 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001711
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001712 i += len;
1713 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1714 pkt_dev->svlan_id = value; /* turn on SVLAN */
1715
1716 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001717 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001718
1719 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001720 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001721
1722 pkt_dev->nr_labels = 0; /* turn off MPLS */
1723 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1724 } else {
1725 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1726 pkt_dev->svlan_id = 0xffff;
1727
1728 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001729 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001730 }
1731 return count;
1732 }
1733
1734 if (!strcmp(name, "svlan_p")) {
1735 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001736 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001737 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001738
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001739 i += len;
1740 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1741 pkt_dev->svlan_p = value;
1742 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1743 } else {
1744 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1745 }
1746 return count;
1747 }
1748
1749 if (!strcmp(name, "svlan_cfi")) {
1750 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001751 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001752 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001753
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001754 i += len;
1755 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1756 pkt_dev->svlan_cfi = value;
1757 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1758 } else {
1759 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1760 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001761 return count;
1762 }
1763
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001764 if (!strcmp(name, "tos")) {
1765 __u32 tmp_value = 0;
1766 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001767 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001768 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001769
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001770 i += len;
1771 if (len == 2) {
1772 pkt_dev->tos = tmp_value;
1773 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1774 } else {
1775 sprintf(pg_result, "ERROR: tos must be 00-ff");
1776 }
1777 return count;
1778 }
1779
1780 if (!strcmp(name, "traffic_class")) {
1781 __u32 tmp_value = 0;
1782 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001783 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001784 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001785
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001786 i += len;
1787 if (len == 2) {
1788 pkt_dev->traffic_class = tmp_value;
1789 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1790 } else {
1791 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1792 }
1793 return count;
1794 }
1795
John Fastabend9e50e3a2010-11-16 19:12:28 +00001796 if (!strcmp(name, "skb_priority")) {
1797 len = num_arg(&user_buffer[i], 9, &value);
1798 if (len < 0)
1799 return len;
1800
1801 i += len;
1802 pkt_dev->skb_priority = value;
1803 sprintf(pg_result, "OK: skb_priority=%i",
1804 pkt_dev->skb_priority);
1805 return count;
1806 }
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1809 return -EINVAL;
1810}
1811
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001812static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Muchun Song359745d2022-01-21 22:14:23 -08001814 return single_open(file, pktgen_if_show, pde_data(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001815}
1816
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001817static const struct proc_ops pktgen_if_proc_ops = {
1818 .proc_open = pktgen_if_open,
1819 .proc_read = seq_read,
1820 .proc_lseek = seq_lseek,
1821 .proc_write = pktgen_if_write,
1822 .proc_release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001823};
1824
1825static int pktgen_thread_show(struct seq_file *seq, void *v)
1826{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001827 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001828 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001830 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Thomas Graf97dc48e2014-05-16 23:28:54 +02001832 seq_puts(seq, "Running: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001833
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001834 rcu_read_lock();
1835 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001836 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001837 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Thomas Graf97dc48e2014-05-16 23:28:54 +02001839 seq_puts(seq, "\nStopped: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001840
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001841 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001842 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001843 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001846 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 else
Thomas Graf97dc48e2014-05-16 23:28:54 +02001848 seq_puts(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001850 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001855static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001856 const char __user * user_buffer,
1857 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Joe Perches8a994a72010-07-12 10:50:23 +00001859 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001860 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001861 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001863 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001866 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return -EINVAL;
1868 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001869
Paul Gortmakerd6182222010-10-18 12:14:44 +00001870 max = count;
1871 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001872 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001873 return len;
1874
Paul Gortmakerd6182222010-10-18 12:14:44 +00001875 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* Read variable name */
1878
1879 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001880 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001881 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 memset(name, 0, sizeof(name));
1884 if (copy_from_user(name, &user_buffer[i], len))
1885 return -EFAULT;
1886 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001887
Luiz Capitulino222f1802006-03-20 22:16:13 -08001888 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001890 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001891 return len;
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 i += len;
1894
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001895 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001896 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Luiz Capitulino222f1802006-03-20 22:16:13 -08001898 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001899 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 ret = -EINVAL;
1901 goto out;
1902 }
1903
1904 pg_result = &(t->result[0]);
1905
Luiz Capitulino222f1802006-03-20 22:16:13 -08001906 if (!strcmp(name, "add_device")) {
1907 char f[32];
1908 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001910 if (len < 0) {
1911 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto out;
1913 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001914 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return -EFAULT;
1916 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001917 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001918 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001919 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001920 if (!ret) {
1921 ret = count;
1922 sprintf(pg_result, "OK: add_device=%s", f);
1923 } else
1924 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 goto out;
1926 }
1927
Luiz Capitulino222f1802006-03-20 22:16:13 -08001928 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001929 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001930 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001931 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001932 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001934 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto out;
1936 }
1937
Luiz Capitulino222f1802006-03-20 22:16:13 -08001938 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001939 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001940 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto out;
1942 }
1943
1944 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001945out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return ret;
1947}
1948
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001949static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Muchun Song359745d2022-01-21 22:14:23 -08001951 return single_open(file, pktgen_thread_show, pde_data(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001954static const struct proc_ops pktgen_thread_proc_ops = {
1955 .proc_open = pktgen_thread_open,
1956 .proc_read = seq_read,
1957 .proc_lseek = seq_lseek,
1958 .proc_write = pktgen_thread_write,
1959 .proc_release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001960};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001963static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1964 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct pktgen_thread *t;
1967 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001968 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Cong Wang4e58a022013-01-28 19:55:53 +00001970 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001971 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001973 if (remove) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001974 pkt_dev->removal_mark = 1;
1975 t->control |= T_REMDEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
1978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001980 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001983/*
1984 * mark a device for removal
1985 */
Cong Wang4e58a022013-01-28 19:55:53 +00001986static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001989 const int max_tries = 10, msec_per_try = 125;
1990 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001991
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001992 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001993 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001994
Luiz Capitulino222f1802006-03-20 22:16:13 -08001995 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001996
Cong Wang4e58a022013-01-28 19:55:53 +00001997 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001998 if (pkt_dev == NULL)
1999 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002000
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002001 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00002002 pr_debug("%s: waiting for %s to disappear....\n",
2003 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002004 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002005 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002006
2007 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002008 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
2009 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002010 break;
2011 }
2012
2013 }
2014
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002015 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002016}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002017
Cong Wang4e58a022013-01-28 19:55:53 +00002018static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08002019{
2020 struct pktgen_thread *t;
2021
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02002022 mutex_lock(&pktgen_thread_lock);
2023
Cong Wang4e58a022013-01-28 19:55:53 +00002024 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08002025 struct pktgen_dev *pkt_dev;
2026
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02002027 if_lock(t);
2028 list_for_each_entry(pkt_dev, &t->if_list, list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08002029 if (pkt_dev->odev != dev)
2030 continue;
2031
David Howellsa8ca16e2013-04-12 17:27:28 +01002032 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002033
Alexey Dobriyan29753152009-08-28 23:34:43 -07002034 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00002035 pn->proc_dir,
Alexey Dobriyan97a32532020-02-03 17:37:17 -08002036 &pktgen_if_proc_ops,
Alexey Dobriyan29753152009-08-28 23:34:43 -07002037 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002038 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002039 pr_err("can't move proc entry for '%s'\n",
2040 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002041 break;
2042 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02002043 if_unlock(t);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002044 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02002045 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Luiz Capitulino222f1802006-03-20 22:16:13 -08002048static int pktgen_device_event(struct notifier_block *unused,
2049 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Jiri Pirko351638e2013-05-28 01:30:21 +00002051 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00002052 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Cong Wang4e58a022013-01-28 19:55:53 +00002054 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002055 return NOTIFY_DONE;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 /* It is OK that we do not hold the group lock right now,
2058 * as we run under the RTNL lock.
2059 */
2060
2061 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08002062 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00002063 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00002067 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 return NOTIFY_DONE;
2072}
2073
Cong Wang4e58a022013-01-28 19:55:53 +00002074static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
2075 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002076 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002077{
2078 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00002079 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07002080
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002081 for (i = 0; ifname[i] != '@'; i++) {
2082 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002083 break;
2084
2085 b[i] = ifname[i];
2086 }
2087 b[i] = 0;
2088
Cong Wang4e58a022013-01-28 19:55:53 +00002089 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07002090}
2091
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093/* Associate pktgen_dev with a device. */
2094
Cong Wang4e58a022013-01-28 19:55:53 +00002095static int pktgen_setup_dev(const struct pktgen_net *pn,
2096 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002099 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (pkt_dev->odev) {
Eric Dumazet035f1f22021-12-06 17:30:35 -08002103 dev_put_track(pkt_dev->odev, &pkt_dev->dev_tracker);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002104 pkt_dev->odev = NULL;
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Cong Wang4e58a022013-01-28 19:55:53 +00002107 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002109 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002110 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002112
Lukas Wunner1e09e582020-03-10 11:49:46 +01002113 if (odev->type != ARPHRD_ETHER && odev->type != ARPHRD_LOOPBACK) {
2114 pr_err("not an ethernet or loopback device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002115 err = -EINVAL;
2116 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002117 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002118 err = -ENETDOWN;
2119 } else {
2120 pkt_dev->odev = odev;
Eric Dumazet035f1f22021-12-06 17:30:35 -08002121 netdev_tracker_alloc(odev, &pkt_dev->dev_tracker, GFP_KERNEL);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002126 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129/* Read pkt_dev from the interface and set up internal pktgen_dev
2130 * structure to have the right information to create/send packets
2131 */
2132static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2133{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002134 int ntxq;
2135
Luiz Capitulino222f1802006-03-20 22:16:13 -08002136 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002137 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002138 sprintf(pkt_dev->result,
2139 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2140 return;
2141 }
2142
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002143 /* make sure that we don't pick a non-existing transmit queue */
2144 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002145
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002146 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07002147 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2148 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2149 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002150 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002151 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002152 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07002153 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2154 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2155 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002156 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002157 }
2158
Luiz Capitulino222f1802006-03-20 22:16:13 -08002159 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002161 if (is_zero_ether_addr(pkt_dev->src_mac))
Joe Perches9ea08b12014-01-20 09:52:19 -08002162 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Luiz Capitulino222f1802006-03-20 22:16:13 -08002164 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08002165 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Luiz Capitulino222f1802006-03-20 22:16:13 -08002167 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002168 int i, set = 0, err = 1;
2169 struct inet6_dev *idev;
2170
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002171 if (pkt_dev->min_pkt_size == 0) {
2172 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2173 + sizeof(struct udphdr)
2174 + sizeof(struct pktgen_hdr)
2175 + pkt_dev->pkt_overhead;
2176 }
2177
Eric Dumazetdf7e8e22017-11-04 08:27:14 -07002178 for (i = 0; i < sizeof(struct in6_addr); i++)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002179 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 set = 1;
2181 break;
2182 }
2183
Luiz Capitulino222f1802006-03-20 22:16:13 -08002184 if (!set) {
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /*
2187 * Use linklevel address if unconfigured.
2188 *
2189 * use ipv6_get_lladdr if/when it's get exported
2190 */
2191
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002192 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002193 idev = __in6_dev_get(pkt_dev->odev);
2194 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 struct inet6_ifaddr *ifp;
2196
2197 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002198 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2199 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002200 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002201 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 err = 0;
2203 break;
2204 }
2205 }
2206 read_unlock_bh(&idev->lock);
2207 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002208 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002209 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002210 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002212 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002213 if (pkt_dev->min_pkt_size == 0) {
2214 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2215 + sizeof(struct udphdr)
2216 + sizeof(struct pktgen_hdr)
2217 + pkt_dev->pkt_overhead;
2218 }
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 pkt_dev->saddr_min = 0;
2221 pkt_dev->saddr_max = 0;
2222 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002223
2224 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002227 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (in_dev) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +02002229 const struct in_ifaddr *ifa;
2230
2231 ifa = rcu_dereference(in_dev->ifa_list);
2232 if (ifa) {
2233 pkt_dev->saddr_min = ifa->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 pkt_dev->saddr_max = pkt_dev->saddr_min;
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002238 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2240 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2241 }
2242
2243 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2244 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2245 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002246 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002247 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2248 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2249 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2250
Luiz Capitulino222f1802006-03-20 22:16:13 -08002251 pkt_dev->cur_dst_mac_offset = 0;
2252 pkt_dev->cur_src_mac_offset = 0;
2253 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2254 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2255 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2256 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 pkt_dev->nflows = 0;
2258}
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002261static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2262{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002263 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002264 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002265 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002266
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02002267 hrtimer_init_sleeper_on_stack(&t, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002268 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002269
Daniel Turull43d28b62010-06-09 22:49:57 +00002270 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002271 if (remaining <= 0)
2272 goto out;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002273
Daniel Borkmann398f3822012-10-28 08:27:19 +00002274 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002275 if (remaining < 100000) {
2276 /* for small delays (<100us), just loop until limit is reached */
2277 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002278 end_time = ktime_get();
2279 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002280 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002281 do {
2282 set_current_state(TASK_INTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02002283 hrtimer_sleeper_start_expires(&t, HRTIMER_MODE_ABS);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002284
2285 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002288 hrtimer_cancel(&t.timer);
2289 } while (t.task && pkt_dev->running && !signal_pending(current));
2290 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002291 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002293
2294 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002295out:
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002296 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002297 destroy_hrtimer_on_stack(&t.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002300static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2301{
Paolo Abeni63d75462016-09-30 16:56:45 +02002302 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002303 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2304 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2305 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2306}
2307
Stephen Hemminger648fda72009-08-27 13:55:07 +00002308static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002309{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002310 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002311}
2312
2313static inline int f_pick(struct pktgen_dev *pkt_dev)
2314{
2315 int flow = pkt_dev->curfl;
2316
2317 if (pkt_dev->flags & F_FLOW_SEQ) {
2318 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2319 /* reset time */
2320 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002321 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002322 pkt_dev->curfl += 1;
2323 if (pkt_dev->curfl >= pkt_dev->cflows)
2324 pkt_dev->curfl = 0; /*reset */
2325 }
2326 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002327 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002328 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002329
Robert Olsson1211a642008-08-05 18:44:26 -07002330 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002331 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002332 pkt_dev->flows[flow].flags = 0;
2333 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002334 }
2335
2336 return pkt_dev->curfl;
2337}
2338
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002339
2340#ifdef CONFIG_XFRM
2341/* If there was already an IPSEC SA, we keep it as is, else
2342 * we go look for it ...
2343*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002344#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002345static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002346{
2347 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002348 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002349 if (!x) {
Fan Duc4549972014-01-03 11:18:32 +08002350
2351 if (pkt_dev->spi) {
2352 /* We need as quick as possible to find the right SA
2353 * Searching with minimum criteria to archieve this.
2354 */
2355 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2356 } else {
2357 /* slow path: we dont already have xfrm_state */
Steffen Klassert7e652642018-06-12 14:07:07 +02002358 x = xfrm_stateonly_find(pn->net, DUMMY_MARK, 0,
Fan Duc4549972014-01-03 11:18:32 +08002359 (xfrm_address_t *)&pkt_dev->cur_daddr,
2360 (xfrm_address_t *)&pkt_dev->cur_saddr,
2361 AF_INET,
2362 pkt_dev->ipsmode,
2363 pkt_dev->ipsproto, 0);
2364 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002365 if (x) {
2366 pkt_dev->flows[flow].x = x;
2367 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002368 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002369 }
2370
2371 }
2372}
2373#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002374static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2375{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002376
2377 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2378 pkt_dev->cur_queue_map = smp_processor_id();
2379
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002380 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002381 __u16 t;
2382 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002383 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002384 (pkt_dev->queue_map_max -
2385 pkt_dev->queue_map_min + 1)
2386 + pkt_dev->queue_map_min;
2387 } else {
2388 t = pkt_dev->cur_queue_map + 1;
2389 if (t > pkt_dev->queue_map_max)
2390 t = pkt_dev->queue_map_min;
2391 }
2392 pkt_dev->cur_queue_map = t;
2393 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002394 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002395}
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397/* Increment/randomize headers according to flags and current values
2398 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2399 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2401{
2402 __u32 imn;
2403 __u32 imx;
2404 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002406 if (pkt_dev->cflows)
2407 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002410 if (pkt_dev->src_mac_count > 1) {
2411 __u32 mc;
2412 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Luiz Capitulino222f1802006-03-20 22:16:13 -08002414 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002415 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002416 else {
2417 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002418 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002419 pkt_dev->src_mac_count)
2420 pkt_dev->cur_src_mac_offset = 0;
2421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Luiz Capitulino222f1802006-03-20 22:16:13 -08002423 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2424 pkt_dev->hh[11] = tmp;
2425 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2426 pkt_dev->hh[10] = tmp;
2427 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2428 pkt_dev->hh[9] = tmp;
2429 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2430 pkt_dev->hh[8] = tmp;
2431 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2432 pkt_dev->hh[7] = tmp;
2433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Luiz Capitulino222f1802006-03-20 22:16:13 -08002435 /* Deal with Destination MAC */
2436 if (pkt_dev->dst_mac_count > 1) {
2437 __u32 mc;
2438 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Luiz Capitulino222f1802006-03-20 22:16:13 -08002440 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002441 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Luiz Capitulino222f1802006-03-20 22:16:13 -08002443 else {
2444 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002445 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002446 pkt_dev->dst_mac_count) {
2447 pkt_dev->cur_dst_mac_offset = 0;
2448 }
2449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Luiz Capitulino222f1802006-03-20 22:16:13 -08002451 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2452 pkt_dev->hh[5] = tmp;
2453 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2454 pkt_dev->hh[4] = tmp;
2455 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2456 pkt_dev->hh[3] = tmp;
2457 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2458 pkt_dev->hh[2] = tmp;
2459 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2460 pkt_dev->hh[1] = tmp;
2461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002463 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002464 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002465 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002466 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2467 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002468 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002469 htonl(0x000fffff));
2470 }
2471
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002472 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002473 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002474 }
2475
2476 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002477 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002478 }
2479
Luiz Capitulino222f1802006-03-20 22:16:13 -08002480 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2481 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002482 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002483 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2484 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Luiz Capitulino222f1802006-03-20 22:16:13 -08002486 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 pkt_dev->cur_udp_src++;
2488 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2489 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002490 }
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Luiz Capitulino222f1802006-03-20 22:16:13 -08002493 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2494 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002495 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002496 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2497 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002498 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002500 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002502 }
2503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 if (!(pkt_dev->flags & F_IPV6)) {
2506
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002507 imn = ntohl(pkt_dev->saddr_min);
2508 imx = ntohl(pkt_dev->saddr_max);
2509 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002511 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002512 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 else {
2514 t = ntohl(pkt_dev->cur_saddr);
2515 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002516 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520 pkt_dev->cur_saddr = htonl(t);
2521 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002522
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002523 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2525 } else {
Al Viro252e33462006-11-14 20:48:11 -08002526 imn = ntohl(pkt_dev->daddr_min);
2527 imx = ntohl(pkt_dev->daddr_max);
2528 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002530 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (pkt_dev->flags & F_IPDST_RND) {
2532
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002533 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002534 t = prandom_u32() %
2535 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002536 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002537 } while (ipv4_is_loopback(s) ||
2538 ipv4_is_multicast(s) ||
2539 ipv4_is_lbcast(s) ||
2540 ipv4_is_zeronet(s) ||
2541 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002542 pkt_dev->cur_daddr = s;
2543 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 t = ntohl(pkt_dev->cur_daddr);
2545 t++;
2546 if (t > imx) {
2547 t = imn;
2548 }
2549 pkt_dev->cur_daddr = htonl(t);
2550 }
2551 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002552 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002553 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002554 pkt_dev->flows[flow].cur_daddr =
2555 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002556#ifdef CONFIG_XFRM
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002557 if (pkt_dev->flags & F_IPSEC)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002558 get_ipsec_sa(pkt_dev, flow);
2559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 pkt_dev->nflows++;
2561 }
2562 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002563 } else { /* IPV6 * */
2564
Joe Perches06e30412012-10-18 17:55:31 +00002565 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 int i;
2567
2568 /* Only random destinations yet */
2569
Luiz Capitulino222f1802006-03-20 22:16:13 -08002570 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002572 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002573 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2574 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578
Luiz Capitulino222f1802006-03-20 22:16:13 -08002579 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2580 __u32 t;
2581 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002582 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002583 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2584 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002585 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002587 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002589 }
2590 pkt_dev->cur_pkt_size = t;
Nick Richardson90149032021-08-10 19:01:54 +00002591 } else if (pkt_dev->n_imix_entries > 0) {
2592 struct imix_pkt *entry;
2593 __u32 t = prandom_u32() % IMIX_PRECISION;
2594 __u8 entry_index = pkt_dev->imix_distribution[t];
2595
2596 entry = &pkt_dev->imix_entries[entry_index];
2597 entry->count_so_far++;
2598 pkt_dev->cur_pkt_size = entry->size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002601 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 pkt_dev->flows[flow].count++;
2604}
2605
Nick Richardson7e5a3ef2021-08-18 01:31:26 +00002606static void fill_imix_distribution(struct pktgen_dev *pkt_dev)
2607{
2608 int cumulative_probabilites[MAX_IMIX_ENTRIES];
2609 int j = 0;
2610 __u64 cumulative_prob = 0;
2611 __u64 total_weight = 0;
2612 int i = 0;
2613
2614 for (i = 0; i < pkt_dev->n_imix_entries; i++)
2615 total_weight += pkt_dev->imix_entries[i].weight;
2616
2617 /* Fill cumulative_probabilites with sum of normalized probabilities */
2618 for (i = 0; i < pkt_dev->n_imix_entries - 1; i++) {
2619 cumulative_prob += div64_u64(pkt_dev->imix_entries[i].weight *
2620 IMIX_PRECISION,
2621 total_weight);
2622 cumulative_probabilites[i] = cumulative_prob;
2623 }
2624 cumulative_probabilites[pkt_dev->n_imix_entries - 1] = 100;
2625
2626 for (i = 0; i < IMIX_PRECISION; i++) {
2627 if (i == cumulative_probabilites[j])
2628 j++;
2629 pkt_dev->imix_distribution[i] = j;
2630 }
2631}
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002632
2633#ifdef CONFIG_XFRM
Fengguang Wu5537a052014-01-06 11:00:07 +01002634static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
Fan Ducf93d47e2014-01-03 11:18:31 +08002635
2636 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2637};
2638
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002639static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2640{
2641 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2642 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002643 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002644
2645 if (!x)
2646 return 0;
2647 /* XXX: we dont support tunnel mode for now until
2648 * we resolve the dst issue */
Fan Ducf93d47e2014-01-03 11:18:31 +08002649 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002650 return 0;
2651
Fan Ducf93d47e2014-01-03 11:18:31 +08002652 /* But when user specify an valid SPI, transformation
2653 * supports both transport/tunnel mode + ESP/AH type.
2654 */
2655 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
David Millerb6ca8bd2017-11-28 15:45:44 -05002656 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
Fan Ducf93d47e2014-01-03 11:18:31 +08002657
2658 rcu_read_lock_bh();
Florian Westphal0c620e92019-03-29 21:16:25 +01002659 err = pktgen_xfrm_outer_mode_output(x, skb);
Fan Ducf93d47e2014-01-03 11:18:31 +08002660 rcu_read_unlock_bh();
Fan Du6de9ace2014-01-03 11:18:28 +08002661 if (err) {
2662 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002663 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002664 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002665 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002666 if (err) {
2667 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002668 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002669 }
Fan Du0af0a412014-01-03 11:18:27 +08002670 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002671 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002672 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002673 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002674error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002675 return err;
2676}
2677
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002678static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002679{
2680 if (pkt_dev->cflows) {
2681 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002682 int i;
2683 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002684 struct xfrm_state *x = pkt_dev->flows[i].x;
2685 if (x) {
2686 xfrm_state_put(x);
2687 pkt_dev->flows[i].x = NULL;
2688 }
2689 }
2690 }
2691}
2692
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002693static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002694 struct sk_buff *skb, __be16 protocol)
2695{
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002696 if (pkt_dev->flags & F_IPSEC) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002697 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2698 int nhead = 0;
2699 if (x) {
Eric Dumazetd4969582015-05-25 16:06:37 -07002700 struct ethhdr *eth;
fan.du38682042013-12-01 16:28:48 +08002701 struct iphdr *iph;
Eric Dumazetd4969582015-05-25 16:06:37 -07002702 int ret;
fan.du38682042013-12-01 16:28:48 +08002703
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002704 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002705 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002706 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2707 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002708 pr_err("Error expanding ipsec packet %d\n",
2709 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002710 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002711 }
2712 }
2713
2714 /* ipsec is not expecting ll header */
2715 skb_pull(skb, ETH_HLEN);
2716 ret = pktgen_output_ipsec(skb, pkt_dev);
2717 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002718 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002719 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002720 }
2721 /* restore ll */
Johannes Bergd58ff352017-06-16 14:29:23 +02002722 eth = skb_push(skb, ETH_HLEN);
Eric Dumazetd4969582015-05-25 16:06:37 -07002723 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2724 eth->h_proto = protocol;
fan.du38682042013-12-01 16:28:48 +08002725
2726 /* Update IPv4 header len as well as checksum value */
2727 iph = ip_hdr(skb);
2728 iph->tot_len = htons(skb->len - ETH_HLEN);
2729 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002730 }
2731 }
2732 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002733err:
2734 kfree_skb(skb);
2735 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002736}
2737#endif
2738
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002739static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2740{
Eric Dumazet95c96172012-04-15 05:58:06 +00002741 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002742 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002743 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002744
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002745 mpls--;
2746 *mpls |= MPLS_STACK_BOTTOM;
2747}
2748
Al Viro0f37c602006-11-03 03:49:56 -08002749static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2750 unsigned int prio)
2751{
2752 return htons(id | (cfi << 12) | (prio << 13));
2753}
2754
Eric Dumazet26ad7872011-01-25 13:26:05 -08002755static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2756 int datalen)
2757{
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002758 struct timespec64 timestamp;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002759 struct pktgen_hdr *pgh;
2760
Johannes Berg4df864c2017-06-16 14:29:21 +02002761 pgh = skb_put(skb, sizeof(*pgh));
Eric Dumazet26ad7872011-01-25 13:26:05 -08002762 datalen -= sizeof(*pgh);
2763
2764 if (pkt_dev->nfrags <= 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002765 skb_put_zero(skb, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002766 } else {
2767 int frags = pkt_dev->nfrags;
2768 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002769 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002770
2771
2772 if (frags > MAX_SKB_FRAGS)
2773 frags = MAX_SKB_FRAGS;
2774 len = datalen - frags * PAGE_SIZE;
2775 if (len > 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002776 skb_put_zero(skb, len);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002777 datalen = frags * PAGE_SIZE;
2778 }
2779
2780 i = 0;
David S. Millerdc91e3b2021-12-14 12:53:41 +00002781 frag_len = (datalen/frags) < PAGE_SIZE ?
2782 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002783 while (datalen > 0) {
2784 if (unlikely(!pkt_dev->page)) {
2785 int node = numa_node_id();
2786
2787 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2788 node = pkt_dev->node;
2789 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2790 if (!pkt_dev->page)
2791 break;
2792 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002793 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002794 skb_frag_set_page(skb, i, pkt_dev->page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002795 skb_frag_off_set(&skb_shinfo(skb)->frags[i], 0);
amit salecha7d36a992011-04-22 16:22:20 +00002796 /*last fragment, fill rest of data*/
2797 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002798 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
David S. Millerdc91e3b2021-12-14 12:53:41 +00002799 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002800 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002801 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2802 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2803 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2804 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002805 i++;
2806 skb_shinfo(skb)->nr_frags = i;
2807 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002808 }
2809
2810 /* Stamp the time, and sequence number,
2811 * convert them to network byte order
2812 */
2813 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2814 pgh->seq_num = htonl(pkt_dev->seq_num);
2815
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002816 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2817 pgh->tv_sec = 0;
2818 pgh->tv_usec = 0;
2819 } else {
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002820 /*
2821 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2822 * as done by wireshark, or y2038 when interpreted as signed.
2823 * This is probably harmless, but if anyone wants to improve
2824 * it, we could introduce a variant that puts 64-bit nanoseconds
2825 * into the respective header bytes.
2826 * This would also be slightly faster to read.
2827 */
2828 ktime_get_real_ts64(&timestamp);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002829 pgh->tv_sec = htonl(timestamp.tv_sec);
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002830 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002831 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002832}
2833
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002834static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
Paolo Abeni63d75462016-09-30 16:56:45 +02002835 struct pktgen_dev *pkt_dev)
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002836{
Paolo Abeni63d75462016-09-30 16:56:45 +02002837 unsigned int extralen = LL_RESERVED_SPACE(dev);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002838 struct sk_buff *skb = NULL;
Paolo Abeni63d75462016-09-30 16:56:45 +02002839 unsigned int size;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002840
Paolo Abeni63d75462016-09-30 16:56:45 +02002841 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002842 if (pkt_dev->flags & F_NODE) {
2843 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2844
2845 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2846 if (likely(skb)) {
2847 skb_reserve(skb, NET_SKB_PAD);
2848 skb->dev = dev;
2849 }
2850 } else {
2851 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2852 }
John Fastabend3de03592016-01-10 21:38:44 -08002853
Paolo Abeni63d75462016-09-30 16:56:45 +02002854 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
John Fastabend3de03592016-01-10 21:38:44 -08002855 if (likely(skb))
Paolo Abeni63d75462016-09-30 16:56:45 +02002856 skb_reserve(skb, extralen - 16);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002857
2858 return skb;
2859}
2860
Luiz Capitulino222f1802006-03-20 22:16:13 -08002861static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2862 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct sk_buff *skb = NULL;
2865 __u8 *eth;
2866 struct udphdr *udph;
2867 int datalen, iplen;
2868 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002869 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002870 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002871 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2872 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2873 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2874 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002875 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002876
2877 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002878 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002879
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002880 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002881 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002882
Robert Olsson64053be2005-06-26 15:27:10 -07002883 /* Update any of the values, used when we're incrementing various
2884 * fields.
2885 */
2886 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002887 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002888
Paolo Abeni63d75462016-09-30 16:56:45 +02002889 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (!skb) {
2891 sprintf(pkt_dev->result, "No memory");
2892 return NULL;
2893 }
2894
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002895 prefetchw(skb->data);
Paolo Abeni63d75462016-09-30 16:56:45 +02002896 skb_reserve(skb, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02002899 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02002900 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002901 if (pkt_dev->nr_labels)
2902 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002903
2904 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002905 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02002906 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002907 *svlan_tci = build_tci(pkt_dev->svlan_id,
2908 pkt_dev->svlan_cfi,
2909 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002910 svlan_encapsulated_proto = skb_put(skb,
2911 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002912 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002913 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002914 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002915 *vlan_tci = build_tci(pkt_dev->vlan_id,
2916 pkt_dev->vlan_cfi,
2917 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002918 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002919 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002920 }
2921
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00002922 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00002923 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002924 iph = skb_put(skb, sizeof(struct iphdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00002925
2926 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002927 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002928 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002929 skb->priority = pkt_dev->skb_priority;
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002932 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002934 /* Eth + IPh + UDPh + mpls */
2935 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002936 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002937 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 udph->source = htons(pkt_dev->cur_udp_src);
2941 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002942 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002943 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 iph->ihl = 5;
2946 iph->version = 4;
2947 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002948 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002949 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 iph->saddr = pkt_dev->cur_saddr;
2951 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002952 iph->id = htons(pkt_dev->ip_id);
2953 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 iph->frag_off = 0;
2955 iplen = 20 + 8 + datalen;
2956 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002957 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002958 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 skb->dev = odev;
2960 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002961
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002962 pktgen_finalize_skb(pkt_dev, skb, datalen);
2963
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002964 if (!(pkt_dev->flags & F_UDPCSUM)) {
2965 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08002966 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002967 skb->ip_summed = CHECKSUM_PARTIAL;
2968 skb->csum = 0;
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002969 udp4_hwcsum(skb, iph->saddr, iph->daddr);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002970 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002971 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002972
2973 /* add protocol-dependent pseudo-header */
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002974 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002975 datalen + 8, IPPROTO_UDP, csum);
2976
2977 if (udph->check == 0)
2978 udph->check = CSUM_MANGLED_0;
2979 }
2980
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002981#ifdef CONFIG_XFRM
2982 if (!process_ipsec(pkt_dev, skb, protocol))
2983 return NULL;
2984#endif
2985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return skb;
2987}
2988
Luiz Capitulino222f1802006-03-20 22:16:13 -08002989static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2990 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
2992 struct sk_buff *skb = NULL;
2993 __u8 *eth;
2994 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002995 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002997 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002998 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002999 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
3000 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
3001 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
3002 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003003 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003004
3005 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003006 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07003007
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003008 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003009 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003010
Robert Olsson64053be2005-06-26 15:27:10 -07003011 /* Update any of the values, used when we're incrementing various
3012 * fields.
3013 */
3014 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00003015 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07003016
Paolo Abeni63d75462016-09-30 16:56:45 +02003017 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if (!skb) {
3019 sprintf(pkt_dev->result, "No memory");
3020 return NULL;
3021 }
3022
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02003023 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 skb_reserve(skb, 16);
3025
3026 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02003027 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02003028 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003029 if (pkt_dev->nr_labels)
3030 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003031
3032 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07003033 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02003034 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003035 *svlan_tci = build_tci(pkt_dev->svlan_id,
3036 pkt_dev->svlan_cfi,
3037 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02003038 svlan_encapsulated_proto = skb_put(skb,
3039 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003040 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003041 }
Johannes Berg4df864c2017-06-16 14:29:21 +02003042 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003043 *vlan_tci = build_tci(pkt_dev->vlan_id,
3044 pkt_dev->vlan_cfi,
3045 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02003046 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003047 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003048 }
3049
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00003050 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00003051 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02003052 iph = skb_put(skb, sizeof(struct ipv6hdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00003053
3054 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02003055 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003056 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00003057 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003060 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07003061
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003062 /* Eth + IPh + UDPh + mpls */
3063 datalen = pkt_dev->cur_pkt_size - 14 -
3064 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003065 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Amerigo Wang5aa8b572012-10-09 17:48:16 +00003067 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00003069 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
3071
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003072 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 udph->source = htons(pkt_dev->cur_udp_src);
3074 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003075 udph->len = htons(udplen);
3076 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003078 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003080 if (pkt_dev->traffic_class) {
3081 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08003082 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003083 }
3084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 iph->hop_limit = 32;
3086
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003087 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 iph->nexthdr = IPPROTO_UDP;
3089
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003090 iph->daddr = pkt_dev->cur_in6_daddr;
3091 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003093 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 skb->dev = odev;
3095 skb->pkt_type = PACKET_HOST;
3096
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01003097 pktgen_finalize_skb(pkt_dev, skb, datalen);
3098
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003099 if (!(pkt_dev->flags & F_UDPCSUM)) {
3100 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08003101 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003102 skb->ip_summed = CHECKSUM_PARTIAL;
3103 skb->csum_start = skb_transport_header(skb) - skb->head;
3104 skb->csum_offset = offsetof(struct udphdr, check);
3105 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
3106 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01003107 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003108
3109 /* add protocol-dependent pseudo-header */
3110 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
3111
3112 if (udph->check == 0)
3113 udph->check = CSUM_MANGLED_0;
3114 }
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return skb;
3117}
3118
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003119static struct sk_buff *fill_packet(struct net_device *odev,
3120 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003122 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return fill_packet_ipv6(odev, pkt_dev);
3124 else
3125 return fill_packet_ipv4(odev, pkt_dev);
3126}
3127
Luiz Capitulino222f1802006-03-20 22:16:13 -08003128static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003130 pkt_dev->seq_num = 1;
3131 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003133 pkt_dev->tx_bytes = 0;
3134 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
3137/* Set up structure for sending pkts, clear counters */
3138
3139static void pktgen_run(struct pktgen_thread *t)
3140{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003141 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 int started = 0;
3143
Joe Perchesf9467ea2010-06-21 12:29:14 +00003144 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003146 rcu_read_lock();
3147 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149 /*
3150 * setup odev and create initial packet.
3151 */
3152 pktgen_setup_inject(pkt_dev);
3153
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 pktgen_clear_counters(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003157 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003158
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003159 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 strcpy(pkt_dev->result, "Starting");
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003162 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003164 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 strcpy(pkt_dev->result, "Error starting");
3166 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003167 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003168 if (started)
3169 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Yejune Dengcda9de02021-06-07 10:37:41 +08003172static void pktgen_handle_all_threads(struct pktgen_net *pn, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003174 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003176 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003177
Cong Wang4e58a022013-01-28 19:55:53 +00003178 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Yejune Dengcda9de02021-06-07 10:37:41 +08003179 t->control |= (flags);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003180
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003181 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
Yejune Dengcda9de02021-06-07 10:37:41 +08003184static void pktgen_stop_all_threads(struct pktgen_net *pn)
3185{
3186 func_enter();
3187
3188 pktgen_handle_all_threads(pn, T_STOP);
3189}
3190
Stephen Hemminger648fda72009-08-27 13:55:07 +00003191static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003193 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003195 rcu_read_lock();
3196 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3197 if (pkt_dev->running) {
3198 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003199 return 1;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003200 }
3201 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003207 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Paolo Abeni720f1de2019-06-06 15:45:03 +02003209 /* note: 't' will still be around even after the unlock/lock
3210 * cycle because pktgen_thread threads are only cleared at
3211 * net exit
3212 */
3213 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003214 msleep_interruptible(100);
Paolo Abeni720f1de2019-06-06 15:45:03 +02003215 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Luiz Capitulino222f1802006-03-20 22:16:13 -08003217 if (signal_pending(current))
3218 goto signal;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003219 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003220 return 1;
3221signal:
3222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223}
3224
Cong Wang4e58a022013-01-28 19:55:53 +00003225static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003227 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003229
Paolo Abeni720f1de2019-06-06 15:45:03 +02003230 /* prevent from racing with rmmod */
3231 if (!try_module_get(THIS_MODULE))
3232 return sig;
3233
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003234 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003235
Cong Wang4e58a022013-01-28 19:55:53 +00003236 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003238 if (sig == 0)
3239 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003241
3242 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003243 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003245
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003246 mutex_unlock(&pktgen_thread_lock);
Paolo Abeni720f1de2019-06-06 15:45:03 +02003247 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return sig;
3249}
3250
Cong Wang4e58a022013-01-28 19:55:53 +00003251static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003253 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Yejune Dengcda9de02021-06-07 10:37:41 +08003255 pktgen_handle_all_threads(pn, T_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003257 /* Propagate thread->control */
3258 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003259
Cong Wang4e58a022013-01-28 19:55:53 +00003260 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Cong Wang4e58a022013-01-28 19:55:53 +00003263static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003264{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003265 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003266
Yejune Dengcda9de02021-06-07 10:37:41 +08003267 pktgen_handle_all_threads(pn, T_REMDEVALL);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003268
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003269 /* Propagate thread->control */
3270 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003271
Cong Wang4e58a022013-01-28 19:55:53 +00003272 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003273}
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3276{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003277 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003278 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003279 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3280 pkt_dev->started_at);
3281 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Daniel Turull03a14ab2011-03-09 14:11:00 -08003283 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003284 (unsigned long long)ktime_to_us(elapsed),
3285 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3286 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003287 (unsigned long long)pkt_dev->sofar,
3288 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003290 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3291 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Nick Richardson769afb32021-08-10 19:01:55 +00003293 if (pkt_dev->n_imix_entries > 0) {
3294 int i;
3295 struct imix_pkt *entry;
3296
3297 bps = 0;
3298 for (i = 0; i < pkt_dev->n_imix_entries; i++) {
3299 entry = &pkt_dev->imix_entries[i];
3300 bps += entry->size * entry->count_so_far;
3301 }
3302 bps = div64_u64(bps * 8 * NSEC_PER_SEC, ktime_to_ns(elapsed));
3303 } else {
3304 bps = pps * 8 * pkt_dev->cur_pkt_size;
3305 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003306
3307 mbps = bps;
3308 do_div(mbps, 1000000);
3309 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3310 (unsigned long long)pps,
3311 (unsigned long long)mbps,
3312 (unsigned long long)bps,
3313 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003317static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003319 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003320
Luiz Capitulino222f1802006-03-20 22:16:13 -08003321 if (!pkt_dev->running) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07003322 pr_warn("interface: %s is already stopped\n",
3323 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003324 return -EINVAL;
3325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003327 pkt_dev->running = 0;
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003328 kfree_skb(pkt_dev->skb);
3329 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003330 pkt_dev->stopped_at = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003332 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Luiz Capitulino222f1802006-03-20 22:16:13 -08003334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
Luiz Capitulino222f1802006-03-20 22:16:13 -08003337static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003339 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003340
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003341 rcu_read_lock();
3342 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003343 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003344 continue;
3345 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003346 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003347 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003348 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003350 rcu_read_unlock();
3351
Luiz Capitulino222f1802006-03-20 22:16:13 -08003352 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Luiz Capitulino222f1802006-03-20 22:16:13 -08003355static void pktgen_stop(struct pktgen_thread *t)
3356{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003357 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Joe Perchesf9467ea2010-06-21 12:29:14 +00003359 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003361 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003363 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003364 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003367 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003370/*
3371 * one of our devices needs to be removed - find it
3372 * and remove it
3373 */
3374static void pktgen_rem_one_if(struct pktgen_thread *t)
3375{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003376 struct list_head *q, *n;
3377 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003378
Joe Perchesf9467ea2010-06-21 12:29:14 +00003379 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003380
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003381 list_for_each_safe(q, n, &t->if_list) {
3382 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003383
Luiz Capitulino222f1802006-03-20 22:16:13 -08003384 if (!cur->removal_mark)
3385 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003386
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003387 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003388 cur->skb = NULL;
3389
3390 pktgen_remove_device(t, cur);
3391
3392 break;
3393 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003394}
3395
Luiz Capitulino222f1802006-03-20 22:16:13 -08003396static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003398 struct list_head *q, *n;
3399 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003400
Joe Perchesf9467ea2010-06-21 12:29:14 +00003401 func_enter();
3402
Luiz Capitulino222f1802006-03-20 22:16:13 -08003403 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003404
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003405 list_for_each_safe(q, n, &t->if_list) {
3406 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003407
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003408 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003409 cur->skb = NULL;
3410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 pktgen_remove_device(t, cur);
3412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Luiz Capitulino222f1802006-03-20 22:16:13 -08003415static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003417 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003418 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419}
3420
Stephen Hemmingeref879792009-09-22 19:41:43 +00003421static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003422{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003423 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003424 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003425 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003426}
3427
Stephen Hemmingeref879792009-09-22 19:41:43 +00003428static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3429{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003430 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003431
Reshetova, Elena63354792017-06-30 13:07:58 +03003432 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003433 if (signal_pending(current))
3434 break;
3435
3436 if (need_resched())
3437 pktgen_resched(pkt_dev);
3438 else
3439 cpu_relax();
3440 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003441 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003442}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003443
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003444static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
Mark Rutland6aa7de02017-10-23 14:07:29 -07003446 unsigned int burst = READ_ONCE(pkt_dev->burst);
Stephen Hemminger00829822008-11-20 20:14:53 -08003447 struct net_device *odev = pkt_dev->odev;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003448 struct netdev_queue *txq;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003449 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 int ret;
3451
Stephen Hemmingeref879792009-09-22 19:41:43 +00003452 /* If device is offline, then don't send */
3453 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3454 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003455 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003457
Stephen Hemmingeref879792009-09-22 19:41:43 +00003458 /* This is max DELAY, this has special meaning of
3459 * "never transmit"
3460 */
3461 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003462 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003463 return;
3464 }
3465
3466 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003467 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3468 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3469 /* build a new pkt */
3470 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003471
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003472 pkt_dev->skb = fill_packet(odev, pkt_dev);
3473 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003474 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003475 schedule();
3476 pkt_dev->clone_count--; /* back out increment, OOM */
3477 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003479 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003480 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003482
Stephen Hemmingeref879792009-09-22 19:41:43 +00003483 if (pkt_dev->delay && pkt_dev->last_ok)
3484 spin(pkt_dev, pkt_dev->next_tx);
3485
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003486 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3487 skb = pkt_dev->skb;
3488 skb->protocol = eth_type_trans(skb, skb->dev);
Reshetova, Elena63354792017-06-30 13:07:58 +03003489 refcount_add(burst, &skb->users);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003490 local_bh_disable();
3491 do {
3492 ret = netif_receive_skb(skb);
3493 if (ret == NET_RX_DROP)
3494 pkt_dev->errors++;
3495 pkt_dev->sofar++;
3496 pkt_dev->seq_num++;
Reshetova, Elena63354792017-06-30 13:07:58 +03003497 if (refcount_read(&skb->users) != burst) {
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003498 /* skb was queued by rps/rfs or taps,
3499 * so cannot reuse this skb
3500 */
Reshetova, Elena63354792017-06-30 13:07:58 +03003501 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003502 /* get out of the loop and wait
3503 * until skb is consumed
3504 */
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003505 break;
3506 }
3507 /* skb was 'freed' by stack, so clean few
3508 * bits and reuse it
3509 */
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +01003510 skb_reset_redirect(skb);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003511 } while (--burst > 0);
3512 goto out; /* Skips xmit_mode M_START_XMIT */
John Fastabend0967f242016-07-02 14:12:54 -07003513 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3514 local_bh_disable();
Reshetova, Elena63354792017-06-30 13:07:58 +03003515 refcount_inc(&pkt_dev->skb->users);
John Fastabend0967f242016-07-02 14:12:54 -07003516
3517 ret = dev_queue_xmit(pkt_dev->skb);
3518 switch (ret) {
3519 case NET_XMIT_SUCCESS:
3520 pkt_dev->sofar++;
3521 pkt_dev->seq_num++;
3522 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3523 break;
3524 case NET_XMIT_DROP:
3525 case NET_XMIT_CN:
3526 /* These are all valid return codes for a qdisc but
3527 * indicate packets are being dropped or will likely
3528 * be dropped soon.
3529 */
3530 case NETDEV_TX_BUSY:
3531 /* qdisc may call dev_hard_start_xmit directly in cases
3532 * where no queues exist e.g. loopback device, virtual
3533 * devices, etc. In this case we need to handle
3534 * NETDEV_TX_ codes.
3535 */
3536 default:
3537 pkt_dev->errors++;
3538 net_info_ratelimited("%s xmit error: %d\n",
3539 pkt_dev->odevname, ret);
3540 break;
3541 }
3542 goto out;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003543 }
3544
Daniel Borkmann10c51b56232014-08-27 11:11:27 +02003545 txq = skb_get_tx_queue(odev, pkt_dev->skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003546
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003547 local_bh_disable();
3548
3549 HARD_TX_LOCK(odev, txq, smp_processor_id());
Stephen Hemmingeref879792009-09-22 19:41:43 +00003550
Daniel Borkmann6f25cd42014-04-07 17:18:30 +02003551 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
Eric Dumazet0835acf2009-09-30 13:03:33 +00003552 pkt_dev->last_ok = 0;
3553 goto unlock;
3554 }
Reshetova, Elena63354792017-06-30 13:07:58 +03003555 refcount_add(burst, &pkt_dev->skb->users);
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003556
3557xmit_more:
3558 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Stephen Hemmingeref879792009-09-22 19:41:43 +00003560 switch (ret) {
3561 case NETDEV_TX_OK:
Stephen Hemmingeref879792009-09-22 19:41:43 +00003562 pkt_dev->last_ok = 1;
3563 pkt_dev->sofar++;
3564 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003565 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003566 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3567 goto xmit_more;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003568 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003569 case NET_XMIT_DROP:
3570 case NET_XMIT_CN:
John Fastabendf466dba2009-12-23 22:02:57 -08003571 /* skb has been consumed */
3572 pkt_dev->errors++;
3573 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003574 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003575 net_info_ratelimited("%s xmit error: %d\n",
3576 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003577 pkt_dev->errors++;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003578 fallthrough;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003579 case NETDEV_TX_BUSY:
3580 /* Retry it next time */
Reshetova, Elena63354792017-06-30 13:07:58 +03003581 refcount_dec(&(pkt_dev->skb->users));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003582 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003583 }
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003584 if (unlikely(burst))
Reshetova, Elena63354792017-06-30 13:07:58 +03003585 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
Eric Dumazet0835acf2009-09-30 13:03:33 +00003586unlock:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003587 HARD_TX_UNLOCK(odev, txq);
3588
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003589out:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003590 local_bh_enable();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 /* If pkt_dev->count is zero, then run forever */
3593 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003594 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 /* Done with this */
3597 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003598 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003601/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 * Main loop of the thread goes here
3603 */
3604
David S. Milleree74baa2007-01-01 20:51:53 -08003605static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
David S. Milleree74baa2007-01-01 20:51:53 -08003607 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003608 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Di Zhu275b1e82021-01-25 20:42:29 +08003611 WARN_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
3613 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003614 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Joe Perchesf9467ea2010-06-21 12:29:14 +00003616 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003618 set_freezable();
3619
David S. Milleree74baa2007-01-01 20:51:53 -08003620 while (!kthread_should_stop()) {
3621 pkt_dev = next_to_run(t);
3622
Stephen Hemmingeref879792009-09-22 19:41:43 +00003623 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003624 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003625 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003626 wait_event_interruptible_timeout(t->queue,
3627 t->control != 0,
3628 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003629 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003630 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003631 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003632
Stephen Hemmingeref879792009-09-22 19:41:43 +00003633 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 pktgen_xmit(pkt_dev);
3635
Stephen Hemmingeref879792009-09-22 19:41:43 +00003636 if (need_resched())
3637 pktgen_resched(pkt_dev);
3638 else
3639 cpu_relax();
3640 }
3641
Luiz Capitulino222f1802006-03-20 22:16:13 -08003642 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 pktgen_stop(t);
3644 t->control &= ~(T_STOP);
3645 }
3646
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 pktgen_run(t);
3649 t->control &= ~(T_RUN);
3650 }
3651
Luiz Capitulino222f1802006-03-20 22:16:13 -08003652 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003654 t->control &= ~(T_REMDEVALL);
3655 }
3656
Luiz Capitulino222f1802006-03-20 22:16:13 -08003657 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003658 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 t->control &= ~(T_REMDEV);
3660 }
3661
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003662 try_to_freeze();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Joe Perchesf9467ea2010-06-21 12:29:14 +00003665 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003666 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Joe Perchesf9467ea2010-06-21 12:29:14 +00003668 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003669 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Joe Perchesf9467ea2010-06-21 12:29:14 +00003671 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003672 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003673
David S. Milleree74baa2007-01-01 20:51:53 -08003674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675}
3676
Luiz Capitulino222f1802006-03-20 22:16:13 -08003677static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003678 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003680 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003681 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003683 rcu_read_lock();
3684 list_for_each_entry_rcu(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003685 if (strncmp(p->odevname, ifname, len) == 0) {
3686 if (p->odevname[len]) {
3687 if (exact || p->odevname[len] != '@')
3688 continue;
3689 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003690 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003691 break;
3692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003694 rcu_read_unlock();
Joe Perchesf9467ea2010-06-21 12:29:14 +00003695 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003696 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
3698
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003699/*
3700 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 */
3702
Luiz Capitulino222f1802006-03-20 22:16:13 -08003703static int add_dev_to_thread(struct pktgen_thread *t,
3704 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
3706 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003708 /* This function cannot be called concurrently, as its called
3709 * under pktgen_thread_lock mutex, but it can run from
3710 * userspace on another CPU than the kthread. The if_lock()
3711 * is used here to sync with concurrent instances of
3712 * _rem_dev_from_if_list() invoked via kthread, which is also
3713 * updating the if_list */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003714 if_lock(t);
3715
3716 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003717 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003718 rv = -EBUSY;
3719 goto out;
3720 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003721
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 pkt_dev->running = 0;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003723 pkt_dev->pg_thread = t;
3724 list_add_rcu(&pkt_dev->list, &t->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Luiz Capitulino222f1802006-03-20 22:16:13 -08003726out:
3727 if_unlock(t);
3728 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729}
3730
3731/* Called under thread lock */
3732
Luiz Capitulino222f1802006-03-20 22:16:13 -08003733static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003735 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003736 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003737 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 /* We don't allow a device to be on several threads */
3740
Cong Wang4e58a022013-01-28 19:55:53 +00003741 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003742 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003743 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003744 return -EBUSY;
3745 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003746
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003747 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003748 if (!pkt_dev)
3749 return -ENOMEM;
3750
Eric Dumazet593f63b2009-11-23 01:44:37 +00003751 strcpy(pkt_dev->odevname, ifname);
Kees Cookfd7bece2018-06-12 14:27:52 -07003752 pkt_dev->flows = vzalloc_node(array_size(MAX_CFLOWS,
3753 sizeof(struct flow_state)),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003754 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003755 if (pkt_dev->flows == NULL) {
3756 kfree(pkt_dev);
3757 return -ENOMEM;
3758 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003759
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003760 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003761 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003762 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003763 pkt_dev->count = pg_count_d;
3764 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003765 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003766 pkt_dev->udp_src_max = 9;
3767 pkt_dev->udp_dst_min = 9;
3768 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003769 pkt_dev->vlan_p = 0;
3770 pkt_dev->vlan_cfi = 0;
3771 pkt_dev->vlan_id = 0xffff;
3772 pkt_dev->svlan_p = 0;
3773 pkt_dev->svlan_cfi = 0;
3774 pkt_dev->svlan_id = 0xffff;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003775 pkt_dev->burst = 1;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08003776 pkt_dev->node = NUMA_NO_NODE;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003777
Cong Wang4e58a022013-01-28 19:55:53 +00003778 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003779 if (err)
3780 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003781 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3782 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003783
Cong Wang4e58a022013-01-28 19:55:53 +00003784 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Alexey Dobriyan97a32532020-02-03 17:37:17 -08003785 &pktgen_if_proc_ops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003786 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003787 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003788 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003789 err = -EINVAL;
3790 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003791 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003792#ifdef CONFIG_XFRM
3793 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3794 pkt_dev->ipsproto = IPPROTO_ESP;
Fan Ducf93d47e2014-01-03 11:18:31 +08003795
3796 /* xfrm tunnel mode needs additional dst to extract outter
3797 * ip header protocol/ttl/id field, here creat a phony one.
3798 * instead of looking for a valid rt, which definitely hurting
3799 * performance under such circumstance.
3800 */
3801 pkt_dev->dstops.family = AF_INET;
David Millerb6ca8bd2017-11-28 15:45:44 -05003802 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3803 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3804 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3805 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003806#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003807
3808 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003809out2:
Eric Dumazet035f1f22021-12-06 17:30:35 -08003810 dev_put_track(pkt_dev->odev, &pkt_dev->dev_tracker);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003811out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003812#ifdef CONFIG_XFRM
3813 free_SAs(pkt_dev);
3814#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003815 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003816 kfree(pkt_dev);
3817 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818}
3819
Cong Wang4e58a022013-01-28 19:55:53 +00003820static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003822 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003823 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003824 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003826 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3827 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003828 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003829 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003830 return -ENOMEM;
3831 }
3832
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02003833 mutex_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003835
David S. Milleree74baa2007-01-01 20:51:53 -08003836 INIT_LIST_HEAD(&t->if_list);
3837
Cong Wang4e58a022013-01-28 19:55:53 +00003838 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003839 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003840
Eric Dumazet94dcf292011-03-22 16:30:45 -07003841 p = kthread_create_on_node(pktgen_thread_worker,
3842 t,
3843 cpu_to_node(cpu),
3844 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003845 if (IS_ERR(p)) {
Leesoo Ahn355db392020-09-01 22:04:47 +09003846 pr_err("kthread_create_on_node() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003847 list_del(&t->th_list);
3848 kfree(t);
3849 return PTR_ERR(p);
3850 }
3851 kthread_bind(p, cpu);
3852 t->tsk = p;
3853
Cong Wang4e58a022013-01-28 19:55:53 +00003854 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Alexey Dobriyan97a32532020-02-03 17:37:17 -08003855 &pktgen_thread_proc_ops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003856 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003857 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003858 PG_PROC_DIR, t->tsk->comm);
3859 kthread_stop(p);
3860 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003861 kfree(t);
3862 return -EINVAL;
3863 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003864
Cong Wang4e58a022013-01-28 19:55:53 +00003865 t->net = pn;
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003866 get_task_struct(p);
David S. Milleree74baa2007-01-01 20:51:53 -08003867 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003868 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
3870 return 0;
3871}
3872
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003873/*
3874 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003876static void _rem_dev_from_if_list(struct pktgen_thread *t,
3877 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003879 struct list_head *q, *n;
3880 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003882 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003883 list_for_each_safe(q, n, &t->if_list) {
3884 p = list_entry(q, struct pktgen_dev, list);
3885 if (p == pkt_dev)
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003886 list_del_rcu(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003888 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889}
3890
Luiz Capitulino222f1802006-03-20 22:16:13 -08003891static int pktgen_remove_device(struct pktgen_thread *t,
3892 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003894 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Luiz Capitulino222f1802006-03-20 22:16:13 -08003896 if (pkt_dev->running) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07003897 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003898 pktgen_stop_device(pkt_dev);
3899 }
3900
3901 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
3903 if (pkt_dev->odev) {
Eric Dumazet035f1f22021-12-06 17:30:35 -08003904 dev_put_track(pkt_dev->odev, &pkt_dev->dev_tracker);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003905 pkt_dev->odev = NULL;
3906 }
3907
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003908 /* Remove proc before if_list entry, because add_device uses
3909 * list to determine if interface already exist, avoid race
3910 * with proc_create_data() */
Markus Elfringef87c5d2014-11-18 20:10:34 +01003911 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003913 /* And update the thread if_list */
3914 _rem_dev_from_if_list(t, pkt_dev);
3915
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003916#ifdef CONFIG_XFRM
3917 free_SAs(pkt_dev);
3918#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003919 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003920 if (pkt_dev->page)
3921 put_page(pkt_dev->page);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003922 kfree_rcu(pkt_dev, rcu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924}
3925
Cong Wang4e58a022013-01-28 19:55:53 +00003926static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
Cong Wang4e58a022013-01-28 19:55:53 +00003928 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003929 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003930 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003931
Cong Wang4e58a022013-01-28 19:55:53 +00003932 pn->net = net;
3933 INIT_LIST_HEAD(&pn->pktgen_threads);
3934 pn->pktgen_exiting = false;
3935 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3936 if (!pn->proc_dir) {
3937 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003938 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003939 }
Alexey Dobriyan97a32532020-02-03 17:37:17 -08003940 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_proc_ops);
Cong Wang4e58a022013-01-28 19:55:53 +00003941 if (pe == NULL) {
3942 pr_err("cannot create %s procfs entry\n", PGCTRL);
3943 ret = -EINVAL;
3944 goto remove;
3945 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003946
John Hawkes670c02c2005-10-13 09:30:31 -07003947 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003948 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Cong Wang4e58a022013-01-28 19:55:53 +00003950 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003951 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003952 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003953 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003954 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003955
Cong Wang4e58a022013-01-28 19:55:53 +00003956 if (list_empty(&pn->pktgen_threads)) {
3957 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003958 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003959 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003960 }
3961
Luiz Capitulino222f1802006-03-20 22:16:13 -08003962 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003963
Cong Wang4e58a022013-01-28 19:55:53 +00003964remove_entry:
3965 remove_proc_entry(PGCTRL, pn->proc_dir);
3966remove:
Gao fengece31ff2013-02-18 01:34:56 +00003967 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003968 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969}
3970
Cong Wang4e58a022013-01-28 19:55:53 +00003971static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
Cong Wang4e58a022013-01-28 19:55:53 +00003973 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003974 struct pktgen_thread *t;
3975 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003976 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Luiz Capitulino222f1802006-03-20 22:16:13 -08003978 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003979 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Eric Dumazetc57b5462012-05-09 13:29:51 +00003981 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003982 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003983 mutex_unlock(&pktgen_thread_lock);
3984
3985 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003986 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003987 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003988 kthread_stop(t->tsk);
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003989 put_task_struct(t->tsk);
David S. Milleree74baa2007-01-01 20:51:53 -08003990 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Cong Wang4e58a022013-01-28 19:55:53 +00003993 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003994 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Cong Wang4e58a022013-01-28 19:55:53 +00003997static struct pernet_operations pg_net_ops = {
3998 .init = pg_net_init,
3999 .exit = pg_net_exit,
4000 .id = &pg_net_id,
4001 .size = sizeof(struct pktgen_net),
4002};
4003
4004static int __init pg_init(void)
4005{
4006 int ret = 0;
4007
4008 pr_info("%s", version);
4009 ret = register_pernet_subsys(&pg_net_ops);
4010 if (ret)
4011 return ret;
4012 ret = register_netdevice_notifier(&pktgen_notifier_block);
4013 if (ret)
4014 unregister_pernet_subsys(&pg_net_ops);
4015
4016 return ret;
4017}
4018
4019static void __exit pg_cleanup(void)
4020{
4021 unregister_netdevice_notifier(&pktgen_notifier_block);
4022 unregister_pernet_subsys(&pg_net_ops);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02004023 /* Don't need rcu_barrier() due to use of kfree_rcu() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026module_init(pg_init);
4027module_exit(pg_cleanup);
4028
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00004029MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030MODULE_DESCRIPTION("Packet Generator tool");
4031MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00004032MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00004034MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00004036MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00004038MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00004040MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");