blob: 092fa3d75b32a8cef12ee6112343cd44f098618a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
Jan Engelhardt96de0e22007-10-19 23:21:04 +02009 * Jens Låås <jens.laas@data.slu.se>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090047 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090068 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +020072 * way.
73 * The if_list is RCU protected, and the if_lock remains to protect updating
74 * of if_list, from "add_device" as it invoked from userspace (via proc write).
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090076 * By design there should only be *one* "controlling" process. In practice
77 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070079 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090081 * Note when adding devices to a specific CPU there good idea to also assign
82 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * --ro
84 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090085 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * memleak 030710- KJP
87 *
88 * First "ranges" functionality for ipv6 030726 --ro
89 *
90 * Included flow support. 030802 ANK.
91 *
92 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090093 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
95 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090097 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * <shemminger@osdl.org> 040923
99 *
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -0500100 * Randy Dunlap fixed u64 printk compiler warning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
102 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
103 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900105 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900108 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800110 *
111 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700113 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700115 * Fixed src_mac command to set source mac of packet to value specified in
116 * command by Adit Ranadive <adit.262@gmail.com>
117 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
Joe Perchesf9467ea2010-06-21 12:29:14 +0000119
120#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <linux/sys.h>
123#include <linux/types.h>
124#include <linux/module.h>
125#include <linux/moduleparam.h>
126#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800127#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#include <linux/sched.h>
129#include <linux/slab.h>
130#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#include <linux/unistd.h>
132#include <linux/string.h>
133#include <linux/ptrace.h>
134#include <linux/errno.h>
135#include <linux/ioport.h>
136#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800137#include <linux/capability.h>
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700138#include <linux/hrtimer.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700139#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#include <linux/delay.h>
141#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800142#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#include <linux/init.h>
144#include <linux/skbuff.h>
145#include <linux/netdevice.h>
146#include <linux/inet.h>
147#include <linux/inetdevice.h>
148#include <linux/rtnetlink.h>
149#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700150#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#include <linux/in.h>
152#include <linux/ip.h>
153#include <linux/ipv6.h>
154#include <linux/udp.h>
155#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700156#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800158#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800159#include <linux/kthread.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700160#include <linux/prefetch.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200161#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#include <net/checksum.h>
163#include <net/ipv6.h>
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200164#include <net/udp.h>
Stephen Rothwell73d94e92013-07-29 16:21:26 +1000165#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700167#ifdef CONFIG_XFRM
168#include <net/xfrm.h>
169#endif
Cong Wang4e58a022013-01-28 19:55:53 +0000170#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#include <asm/byteorder.h>
172#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700173#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000174#include <linux/io.h>
175#include <linux/timex.h>
176#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800178#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Jesper Dangaard Brouer40207262015-05-21 12:16:56 +0200180#define VERSION "2.75"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800182#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800183#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Joe Perchesf9467ea2010-06-21 12:29:14 +0000185#define func_enter() pr_debug("entering %s\n", __func__);
186
Dmitry Safonov6f107c72018-01-18 18:31:35 +0000187#define PKT_FLAGS \
188 pf(IPV6) /* Interface in IPV6 Mode */ \
189 pf(IPSRC_RND) /* IP-Src Random */ \
190 pf(IPDST_RND) /* IP-Dst Random */ \
191 pf(TXSIZE_RND) /* Transmit size is random */ \
192 pf(UDPSRC_RND) /* UDP-Src Random */ \
193 pf(UDPDST_RND) /* UDP-Dst Random */ \
194 pf(UDPCSUM) /* Include UDP checksum */ \
195 pf(NO_TIMESTAMP) /* Don't timestamp packets (default TS) */ \
196 pf(MPLS_RND) /* Random MPLS labels */ \
197 pf(QUEUE_MAP_RND) /* queue map Random */ \
198 pf(QUEUE_MAP_CPU) /* queue map mirrors smp_processor_id() */ \
199 pf(FLOW_SEQ) /* Sequential flows */ \
200 pf(IPSEC) /* ipsec on for flows */ \
201 pf(MACSRC_RND) /* MAC-Src Random */ \
202 pf(MACDST_RND) /* MAC-Dst Random */ \
203 pf(VID_RND) /* Random VLAN ID */ \
204 pf(SVID_RND) /* Random SVLAN ID */ \
205 pf(NODE) /* Node memory alloc*/ \
206
207#define pf(flag) flag##_SHIFT,
208enum pkt_flags {
209 PKT_FLAGS
210};
211#undef pf
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/* Device flag bits */
Dmitry Safonov6f107c72018-01-18 18:31:35 +0000214#define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
215PKT_FLAGS
216#undef pf
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000218#define pf(flag) __stringify(flag),
219static char *pkt_flag_names[] = {
220 PKT_FLAGS
221};
222#undef pf
223
224#define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000227#define T_STOP (1<<0) /* Stop run */
228#define T_RUN (1<<1) /* Start run */
229#define T_REMDEVALL (1<<2) /* Remove all devs */
230#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200232/* Xmit modes */
233#define M_START_XMIT 0 /* Default normal TX */
234#define M_NETIF_RECEIVE 1 /* Inject packets into stack */
John Fastabend0967f242016-07-02 14:12:54 -0700235#define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200236
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200237/* If lock -- protects updating of if_list */
Eric Dumazet9a0b1e82016-10-15 17:50:49 +0200238#define if_lock(t) mutex_lock(&(t->if_lock));
239#define if_unlock(t) mutex_unlock(&(t->if_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/* Used to help with determining the pkts on receive */
242#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700243#define PG_PROC_DIR "pktgen"
244#define PGCTRL "pgctrl"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246#define MAX_CFLOWS 65536
247
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700248#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
249#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
250
Luiz Capitulino222f1802006-03-20 22:16:13 -0800251struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800252 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800253 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700254#ifdef CONFIG_XFRM
255 struct xfrm_state *x;
256#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700257 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700260/* flow flag bits */
261#define F_INIT (1<<0) /* flow has been initialized */
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /*
265 * Try to keep frequent/infrequent used vars. separated.
266 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800267 struct proc_dir_entry *entry; /* proc file */
268 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000269 struct list_head list; /* chaining in the thread's run-queue */
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200270 struct rcu_head rcu; /* freed by RCU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000272 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800273
274 /* If min != max, then we will either do a linear iteration, or
275 * we will do a random selection from within the range.
276 */
277 __u32 flags;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200278 int xmit_mode;
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000279 int min_pkt_size;
280 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700281 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800282 int nfrags;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200283 int removal_mark; /* non-zero => the device is marked for
284 * removal by worker thread */
285
Eric Dumazet26ad7872011-01-25 13:26:05 -0800286 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000287 u64 delay; /* nano-seconds */
288
Luiz Capitulino222f1802006-03-20 22:16:13 -0800289 __u64 count; /* Default No packets to send */
290 __u64 sofar; /* How many pkts we've sent so far */
291 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800292 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Luiz Capitulino222f1802006-03-20 22:16:13 -0800294 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Luiz Capitulino222f1802006-03-20 22:16:13 -0800296 __u32 clone_count;
297 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000298 * Or a failed transmit of some sort?
299 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800300 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000301 ktime_t next_tx;
302 ktime_t started_at;
303 ktime_t stopped_at;
304 u64 idle_acc; /* nano-seconds */
305
Luiz Capitulino222f1802006-03-20 22:16:13 -0800306 __u32 seq_num;
307
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000308 int clone_skb; /*
309 * Use multiple SKBs during packet gen.
310 * If this number is greater than 1, then
311 * that many copies of the same packet will be
312 * sent before a new packet is allocated.
313 * If you want to send 1024 identical packets
314 * before creating a new packet,
315 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800316 */
317
318 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
319 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
320 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
321 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
322
323 struct in6_addr in6_saddr;
324 struct in6_addr in6_daddr;
325 struct in6_addr cur_in6_daddr;
326 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800328 struct in6_addr min_in6_daddr;
329 struct in6_addr max_in6_daddr;
330 struct in6_addr min_in6_saddr;
331 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Luiz Capitulino222f1802006-03-20 22:16:13 -0800333 /* If we're doing ranges, random or incremental, then this
334 * defines the min/max for those ranges.
335 */
Al Viro252e33462006-11-14 20:48:11 -0800336 __be32 saddr_min; /* inclusive, source IP address */
337 __be32 saddr_max; /* exclusive, source IP address */
338 __be32 daddr_min; /* inclusive, dest IP address */
339 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Luiz Capitulino222f1802006-03-20 22:16:13 -0800341 __u16 udp_src_min; /* inclusive, source UDP port */
342 __u16 udp_src_max; /* exclusive, source UDP port */
343 __u16 udp_dst_min; /* inclusive, dest UDP port */
344 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700346 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000347 __u8 tos; /* six MSB of (former) IPv4 TOS
348 are for dscp codepoint */
349 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
350 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700351
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800352 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000353 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800354 __be32 labels[MAX_MPLS_LABELS];
355
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700356 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
357 __u8 vlan_p;
358 __u8 vlan_cfi;
359 __u16 vlan_id; /* 0xffff means no vlan tag */
360
361 __u8 svlan_p;
362 __u8 svlan_cfi;
363 __u16 svlan_id; /* 0xffff means no svlan tag */
364
Luiz Capitulino222f1802006-03-20 22:16:13 -0800365 __u32 src_mac_count; /* How many MACs to iterate through */
366 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Luiz Capitulino222f1802006-03-20 22:16:13 -0800368 unsigned char dst_mac[ETH_ALEN];
369 unsigned char src_mac[ETH_ALEN];
370
371 __u32 cur_dst_mac_offset;
372 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800373 __be32 cur_saddr;
374 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700375 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800376 __u16 cur_udp_dst;
377 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700378 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800379 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800380 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800381
382 __u8 hh[14];
383 /* = {
384 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
385
386 We fill in SRC address later
387 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
388 0x08, 0x00
389 };
390 */
391 __u16 pad; /* pad out the hh struct to an even 16 bytes */
392
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000393 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800394 * are transmitting the same one multiple times
395 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000396 struct net_device *odev; /* The out-going device.
397 * Note that the device should have it's
398 * pg_info pointer pointing back to this
399 * device.
400 * Set when the user specifies the out-going
401 * device name (not when the inject is
402 * started as it used to do.)
403 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000404 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000406 unsigned int cflows; /* Concurrent flows (config) */
407 unsigned int lflow; /* Flow length (config) */
408 unsigned int nflows; /* accumulated flows (stats) */
409 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700410
411 u16 queue_map_min;
412 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000413 __u32 skb_priority; /* skb priority field */
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -0700414 unsigned int burst; /* number of duplicated packets to burst */
Robert Olssone99b99b2010-03-18 22:44:30 +0000415 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700416
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700417#ifdef CONFIG_XFRM
418 __u8 ipsmode; /* IPSEC mode (config) */
419 __u8 ipsproto; /* IPSEC type (config) */
Fan Dude4aee72014-01-03 11:18:30 +0800420 __u32 spi;
David Millerb6ca8bd2017-11-28 15:45:44 -0500421 struct xfrm_dst xdst;
Fan Ducf93d47e2014-01-03 11:18:31 +0800422 struct dst_ops dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700423#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800424 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425};
426
427struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800428 __be32 pgh_magic;
429 __be32 seq_num;
430 __be32 tv_sec;
431 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432};
433
Cong Wang4e58a022013-01-28 19:55:53 +0000434
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300435static unsigned int pg_net_id __read_mostly;
Cong Wang4e58a022013-01-28 19:55:53 +0000436
437struct pktgen_net {
438 struct net *net;
439 struct proc_dir_entry *proc_dir;
440 struct list_head pktgen_threads;
441 bool pktgen_exiting;
442};
Eric Dumazet551eaff2010-11-21 10:26:44 -0800443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444struct pktgen_thread {
Eric Dumazet9a0b1e82016-10-15 17:50:49 +0200445 struct mutex if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800446 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800447 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800448 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800449 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000451 /* Field for thread to receive "posted" events terminate,
452 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800453
454 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int cpu;
456
Luiz Capitulino222f1802006-03-20 22:16:13 -0800457 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700458 struct completion start_done;
Cong Wang4e58a022013-01-28 19:55:53 +0000459 struct pktgen_net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460};
461
462#define REMOVE 1
463#define FIND 0
464
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000465static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000466 "Packet Generator for packet performance testing. "
467 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Luiz Capitulino222f1802006-03-20 22:16:13 -0800469static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
470static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
471static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800472 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
Cong Wang4e58a022013-01-28 19:55:53 +0000474static void pktgen_run_all_threads(struct pktgen_net *pn);
475static void pktgen_reset_all_threads(struct pktgen_net *pn);
476static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000477
Luiz Capitulino222f1802006-03-20 22:16:13 -0800478static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000482static int pg_count_d __read_mostly = 1000;
483static int pg_delay_d __read_mostly;
484static int pg_clone_skb_d __read_mostly;
485static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Luiz Capitulino222fa072006-03-20 22:24:27 -0800487static DEFINE_MUTEX(pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static struct notifier_block pktgen_notifier_block = {
490 .notifier_call = pktgen_device_event,
491};
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900494 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 *
496 */
497
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700498static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800499{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000500 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000504static ssize_t pgctrl_write(struct file *file, const char __user *buf,
505 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700507 char data[128];
Cong Wang4e58a022013-01-28 19:55:53 +0000508 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Mathias Krause09455742014-02-21 21:38:35 +0100510 if (!capable(CAP_NET_ADMIN))
511 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Mathias Krause20b0c712014-02-21 21:38:34 +0100513 if (count == 0)
514 return -EINVAL;
515
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700516 if (count > sizeof(data))
517 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Mathias Krause09455742014-02-21 21:38:35 +0100519 if (copy_from_user(data, buf, count))
520 return -EFAULT;
521
Mathias Krause20b0c712014-02-21 21:38:34 +0100522 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Luiz Capitulino222f1802006-03-20 22:16:13 -0800524 if (!strcmp(data, "stop"))
Cong Wang4e58a022013-01-28 19:55:53 +0000525 pktgen_stop_all_threads_ifs(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Luiz Capitulino222f1802006-03-20 22:16:13 -0800527 else if (!strcmp(data, "start"))
Cong Wang4e58a022013-01-28 19:55:53 +0000528 pktgen_run_all_threads(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800530 else if (!strcmp(data, "reset"))
Cong Wang4e58a022013-01-28 19:55:53 +0000531 pktgen_reset_all_threads(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800532
Luiz Capitulino222f1802006-03-20 22:16:13 -0800533 else
Jesper Dangaard Brouer40207262015-05-21 12:16:56 +0200534 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Mathias Krause09455742014-02-21 21:38:35 +0100536 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700539static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Al Virod9dda782013-03-31 18:16:14 -0400541 return single_open(file, pgctrl_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700542}
543
Arjan van de Ven9a321442007-02-12 00:55:35 -0800544static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800545 .open = pgctrl_open,
546 .read = seq_read,
547 .llseek = seq_lseek,
548 .write = pgctrl_write,
549 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700550};
551
552static int pktgen_if_show(struct seq_file *seq, void *v)
553{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000554 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000555 ktime_t stopped;
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000556 unsigned int i;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000557 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Luiz Capitulino222f1802006-03-20 22:16:13 -0800559 seq_printf(seq,
560 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
561 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
562 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Luiz Capitulino222f1802006-03-20 22:16:13 -0800564 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000565 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
566 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000567 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Luiz Capitulino222f1802006-03-20 22:16:13 -0800569 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
570 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Robert Olsson45b270f2007-08-28 15:45:55 -0700572 seq_printf(seq,
573 " queue_map_min: %u queue_map_max: %u\n",
574 pkt_dev->queue_map_min,
575 pkt_dev->queue_map_max);
576
John Fastabend9e50e3a2010-11-16 19:12:28 +0000577 if (pkt_dev->skb_priority)
578 seq_printf(seq, " skb_priority: %u\n",
579 pkt_dev->skb_priority);
580
Luiz Capitulino222f1802006-03-20 22:16:13 -0800581 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800582 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000583 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
584 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
585 &pkt_dev->in6_saddr,
586 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
587 &pkt_dev->in6_daddr,
588 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000589 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800590 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000591 " dst_min: %s dst_max: %s\n",
592 pkt_dev->dst_min, pkt_dev->dst_max);
593 seq_printf(seq,
Jesper Dangaard Brouerd079abd2015-05-21 12:16:11 +0200594 " src_min: %s src_max: %s\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000595 pkt_dev->src_min, pkt_dev->src_max);
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700598 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Johannes Berge1749612008-10-27 15:59:26 -0700600 seq_printf(seq, "%pM ",
601 is_zero_ether_addr(pkt_dev->src_mac) ?
602 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Thomas Graf97dc48e2014-05-16 23:28:54 +0200604 seq_puts(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700605 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Luiz Capitulino222f1802006-03-20 22:16:13 -0800607 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000608 " udp_src_min: %d udp_src_max: %d"
609 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800610 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
611 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Luiz Capitulino222f1802006-03-20 22:16:13 -0800613 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800614 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700615 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800617 if (pkt_dev->nr_labels) {
Thomas Graf97dc48e2014-05-16 23:28:54 +0200618 seq_puts(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700619 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800620 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
621 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
622 }
623
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000624 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700625 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000626 pkt_dev->vlan_id, pkt_dev->vlan_p,
627 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700628
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000629 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700630 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000631 pkt_dev->svlan_id, pkt_dev->svlan_p,
632 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700633
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000634 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700635 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700636
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000637 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700638 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700639
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -0700640 if (pkt_dev->burst > 1)
641 seq_printf(seq, " burst: %d\n", pkt_dev->burst);
642
Robert Olssone99b99b2010-03-18 22:44:30 +0000643 if (pkt_dev->node >= 0)
644 seq_printf(seq, " node: %d\n", pkt_dev->node);
645
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200646 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
647 seq_puts(seq, " xmit_mode: netif_receive\n");
John Fastabend0967f242016-07-02 14:12:54 -0700648 else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
649 seq_puts(seq, " xmit_mode: xmit_queue\n");
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200650
Thomas Graf97dc48e2014-05-16 23:28:54 +0200651 seq_puts(seq, " Flags: ");
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800652
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000653 for (i = 0; i < NR_PKT_FLAGS; i++) {
654 if (i == F_FLOW_SEQ)
655 if (!pkt_dev->cflows)
656 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000658 if (pkt_dev->flags & (1 << i))
659 seq_printf(seq, "%s ", pkt_flag_names[i]);
660 else if (i == F_FLOW_SEQ)
661 seq_puts(seq, "FLOW_RND ");
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700662
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700663#ifdef CONFIG_XFRM
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000664 if (i == F_IPSEC && pkt_dev->spi)
Fan Du81013282014-01-03 11:18:33 +0800665 seq_printf(seq, "spi:%u", pkt_dev->spi);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700666#endif
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000667 }
Robert Olssone99b99b2010-03-18 22:44:30 +0000668
Luiz Capitulino222f1802006-03-20 22:16:13 -0800669 seq_puts(seq, "\n");
670
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000671 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000672 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000673 idle = pkt_dev->idle_acc;
674 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800675
676 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000677 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800678 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000679 (unsigned long long)pkt_dev->errors);
680
681 seq_printf(seq,
682 " started: %lluus stopped: %lluus idle: %lluus\n",
683 (unsigned long long) ktime_to_us(pkt_dev->started_at),
684 (unsigned long long) ktime_to_us(stopped),
685 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800686
687 seq_printf(seq,
688 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700689 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
690 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Luiz Capitulino222f1802006-03-20 22:16:13 -0800692 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000693 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
694 &pkt_dev->cur_in6_saddr,
695 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800696 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000697 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
698 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Luiz Capitulino222f1802006-03-20 22:16:13 -0800700 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700701 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Robert Olsson45b270f2007-08-28 15:45:55 -0700703 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
704
Luiz Capitulino222f1802006-03-20 22:16:13 -0800705 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800708 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 else
Thomas Graf97dc48e2014-05-16 23:28:54 +0200710 seq_puts(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800715
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000716static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
717 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800718{
719 int i = 0;
720 *num = 0;
721
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700722 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000723 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800724 char c;
725 *num <<= 4;
726 if (get_user(c, &user_buffer[i]))
727 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000728 value = hex_to_bin(c);
729 if (value >= 0)
730 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800731 else
732 break;
733 }
734 return i;
735}
736
Luiz Capitulino222f1802006-03-20 22:16:13 -0800737static int count_trail_chars(const char __user * user_buffer,
738 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 int i;
741
742 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800743 char c;
744 if (get_user(c, &user_buffer[i]))
745 return -EFAULT;
746 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 case '\"':
748 case '\n':
749 case '\r':
750 case '\t':
751 case ' ':
752 case '=':
753 break;
754 default:
755 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758done:
759 return i;
760}
761
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000762static long num_arg(const char __user *user_buffer, unsigned long maxlen,
763 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000765 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800767
Paul Gortmakerd6182222010-10-18 12:14:44 +0000768 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800769 char c;
770 if (get_user(c, &user_buffer[i]))
771 return -EFAULT;
772 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800774 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 } else
776 break;
777 }
778 return i;
779}
780
Luiz Capitulino222f1802006-03-20 22:16:13 -0800781static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000783 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Paul Gortmakerd6182222010-10-18 12:14:44 +0000785 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800786 char c;
787 if (get_user(c, &user_buffer[i]))
788 return -EFAULT;
789 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case '\"':
791 case '\n':
792 case '\r':
793 case '\t':
794 case ' ':
795 goto done_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 default:
797 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return i;
802}
803
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800804static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
805{
Eric Dumazet95c96172012-04-15 05:58:06 +0000806 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800807 char c;
808 ssize_t i = 0;
809 int len;
810
811 pkt_dev->nr_labels = 0;
812 do {
813 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700814 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800815 if (len <= 0)
816 return len;
817 pkt_dev->labels[n] = htonl(tmp);
818 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
819 pkt_dev->flags |= F_MPLS_RND;
820 i += len;
821 if (get_user(c, &buffer[i]))
822 return -EFAULT;
823 i++;
824 n++;
825 if (n >= MAX_MPLS_LABELS)
826 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700827 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800828
829 pkt_dev->nr_labels = n;
830 return i;
831}
832
Dmitry Safonov52e12d52018-01-18 18:31:37 +0000833static __u32 pktgen_read_flag(const char *f, bool *disable)
834{
835 __u32 i;
836
837 if (f[0] == '!') {
838 *disable = true;
839 f++;
840 }
841
842 for (i = 0; i < NR_PKT_FLAGS; i++) {
843 if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
844 continue;
845
846 /* allow only disabling ipv6 flag */
847 if (!*disable && i == IPV6_SHIFT)
848 continue;
849
850 if (strcmp(f, pkt_flag_names[i]) == 0)
851 return 1 << i;
852 }
853
854 if (strcmp(f, "FLOW_RND") == 0) {
855 *disable = !*disable;
856 return F_FLOW_SEQ;
857 }
858
859 return 0;
860}
861
Luiz Capitulino222f1802006-03-20 22:16:13 -0800862static ssize_t pktgen_if_write(struct file *file,
863 const char __user * user_buffer, size_t count,
864 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Joe Perches8a994a72010-07-12 10:50:23 +0000866 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000868 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 char name[16], valstr[32];
870 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800871 char *pg_result = NULL;
872 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800874
875 pg_result = &(pkt_dev->result[0]);
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (count < 1) {
Joe Perches294a0b72014-09-09 21:17:30 -0700878 pr_warn("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -EINVAL;
880 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800881
Paul Gortmakerd6182222010-10-18 12:14:44 +0000882 max = count;
883 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800884 if (tmp < 0) {
Joe Perches294a0b72014-09-09 21:17:30 -0700885 pr_warn("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000888 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* Read variable name */
891
892 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000893 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800894 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800897 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -EFAULT;
899 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800900
901 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800903 if (len < 0)
904 return len;
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 i += len;
907
908 if (debug) {
Arnd Bergmanna870a022018-03-13 21:58:39 +0100909 size_t copy = min_t(size_t, count + 1, 1024);
910 char *tp = strndup_user(user_buffer, copy);
911
912 if (IS_ERR(tp))
913 return PTR_ERR(tp);
914
915 pr_debug("%s,%zu buffer -:%s:-\n", name, count, tp);
Gustavo A. R. Silva29d1df72018-03-14 03:07:27 -0500916 kfree(tp);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 if (!strcmp(name, "min_pkt_size")) {
920 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000921 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800922 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800925 if (value < 14 + 20 + 8)
926 value = 14 + 20 + 8;
927 if (value != pkt_dev->min_pkt_size) {
928 pkt_dev->min_pkt_size = value;
929 pkt_dev->cur_pkt_size = value;
930 }
931 sprintf(pg_result, "OK: min_pkt_size=%u",
932 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return count;
934 }
935
Luiz Capitulino222f1802006-03-20 22:16:13 -0800936 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000938 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800939 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800942 if (value < 14 + 20 + 8)
943 value = 14 + 20 + 8;
944 if (value != pkt_dev->max_pkt_size) {
945 pkt_dev->max_pkt_size = value;
946 pkt_dev->cur_pkt_size = value;
947 }
948 sprintf(pg_result, "OK: max_pkt_size=%u",
949 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return count;
951 }
952
Luiz Capitulino222f1802006-03-20 22:16:13 -0800953 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 if (!strcmp(name, "pkt_size")) {
956 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000957 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800958 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800961 if (value < 14 + 20 + 8)
962 value = 14 + 20 + 8;
963 if (value != pkt_dev->min_pkt_size) {
964 pkt_dev->min_pkt_size = value;
965 pkt_dev->max_pkt_size = value;
966 pkt_dev->cur_pkt_size = value;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
969 return count;
970 }
971
Luiz Capitulino222f1802006-03-20 22:16:13 -0800972 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000974 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800978 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sprintf(pg_result, "OK: debug=%u", debug);
980 return count;
981 }
982
Luiz Capitulino222f1802006-03-20 22:16:13 -0800983 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000985 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800986 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 i += len;
989 pkt_dev->nfrags = value;
990 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
991 return count;
992 }
993 if (!strcmp(name, "delay")) {
994 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000995 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800996 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000999 if (value == 0x7FFFFFFF)
1000 pkt_dev->delay = ULLONG_MAX;
1001 else
Eric Dumazet9240d712009-10-03 01:39:18 +00001002 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00001003
1004 sprintf(pg_result, "OK: delay=%llu",
1005 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return count;
1007 }
Daniel Turull43d28b62010-06-09 22:49:57 +00001008 if (!strcmp(name, "rate")) {
1009 len = num_arg(&user_buffer[i], 10, &value);
1010 if (len < 0)
1011 return len;
1012
1013 i += len;
1014 if (!value)
1015 return len;
1016 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1017 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001018 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001019
1020 sprintf(pg_result, "OK: rate=%lu", value);
1021 return count;
1022 }
1023 if (!strcmp(name, "ratep")) {
1024 len = num_arg(&user_buffer[i], 10, &value);
1025 if (len < 0)
1026 return len;
1027
1028 i += len;
1029 if (!value)
1030 return len;
1031 pkt_dev->delay = NSEC_PER_SEC/value;
1032 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001033 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001034
1035 sprintf(pg_result, "OK: rate=%lu", value);
1036 return count;
1037 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001038 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001040 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001041 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001044 if (value != pkt_dev->udp_src_min) {
1045 pkt_dev->udp_src_min = value;
1046 pkt_dev->cur_udp_src = value;
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1049 return count;
1050 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001051 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001053 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001057 if (value != pkt_dev->udp_dst_min) {
1058 pkt_dev->udp_dst_min = value;
1059 pkt_dev->cur_udp_dst = value;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1062 return count;
1063 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001064 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001066 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001067 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001070 if (value != pkt_dev->udp_src_max) {
1071 pkt_dev->udp_src_max = value;
1072 pkt_dev->cur_udp_src = value;
1073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1075 return count;
1076 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001077 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001079 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001080 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 if (value != pkt_dev->udp_dst_max) {
1084 pkt_dev->udp_dst_max = value;
1085 pkt_dev->cur_udp_dst = value;
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1088 return count;
1089 }
1090 if (!strcmp(name, "clone_skb")) {
1091 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001092 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001093 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001094 if ((value > 0) &&
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001095 ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
1096 !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
Neil Hormand8873312011-07-26 06:05:37 +00001097 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001099 pkt_dev->clone_skb = value;
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1102 return count;
1103 }
1104 if (!strcmp(name, "count")) {
1105 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001106 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001107 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 i += len;
1110 pkt_dev->count = value;
1111 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001112 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return count;
1114 }
1115 if (!strcmp(name, "src_mac_count")) {
1116 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001117 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001118 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 i += len;
1121 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001122 pkt_dev->src_mac_count = value;
1123 pkt_dev->cur_src_mac_offset = 0;
1124 }
1125 sprintf(pg_result, "OK: src_mac_count=%d",
1126 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return count;
1128 }
1129 if (!strcmp(name, "dst_mac_count")) {
1130 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001131 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001132 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 i += len;
1135 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001136 pkt_dev->dst_mac_count = value;
1137 pkt_dev->cur_dst_mac_offset = 0;
1138 }
1139 sprintf(pg_result, "OK: dst_mac_count=%d",
1140 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return count;
1142 }
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07001143 if (!strcmp(name, "burst")) {
1144 len = num_arg(&user_buffer[i], 10, &value);
1145 if (len < 0)
1146 return len;
1147
1148 i += len;
John Fastabend0967f242016-07-02 14:12:54 -07001149 if ((value > 1) &&
1150 ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
1151 ((pkt_dev->xmit_mode == M_START_XMIT) &&
1152 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
Eric Dumazet52d6c8c2015-02-22 17:03:41 -08001153 return -ENOTSUPP;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07001154 pkt_dev->burst = value < 1 ? 1 : value;
1155 sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
1156 return count;
1157 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001158 if (!strcmp(name, "node")) {
1159 len = num_arg(&user_buffer[i], 10, &value);
1160 if (len < 0)
1161 return len;
1162
1163 i += len;
1164
1165 if (node_possible(value)) {
1166 pkt_dev->node = value;
1167 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001168 if (pkt_dev->page) {
1169 put_page(pkt_dev->page);
1170 pkt_dev->page = NULL;
1171 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001172 }
1173 else
1174 sprintf(pg_result, "ERROR: node not possible");
1175 return count;
1176 }
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001177 if (!strcmp(name, "xmit_mode")) {
1178 char f[32];
1179
1180 memset(f, 0, 32);
1181 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1182 if (len < 0)
1183 return len;
1184
1185 if (copy_from_user(f, &user_buffer[i], len))
1186 return -EFAULT;
1187 i += len;
1188
1189 if (strcmp(f, "start_xmit") == 0) {
1190 pkt_dev->xmit_mode = M_START_XMIT;
1191 } else if (strcmp(f, "netif_receive") == 0) {
1192 /* clone_skb set earlier, not supported in this mode */
1193 if (pkt_dev->clone_skb > 0)
1194 return -ENOTSUPP;
1195
1196 pkt_dev->xmit_mode = M_NETIF_RECEIVE;
Alexei Starovoitov9eea9222015-05-11 15:19:48 -07001197
1198 /* make sure new packet is allocated every time
1199 * pktgen_xmit() is called
1200 */
1201 pkt_dev->last_ok = 1;
1202
1203 /* override clone_skb if user passed default value
1204 * at module loading time
1205 */
1206 pkt_dev->clone_skb = 0;
John Fastabend0967f242016-07-02 14:12:54 -07001207 } else if (strcmp(f, "queue_xmit") == 0) {
1208 pkt_dev->xmit_mode = M_QUEUE_XMIT;
1209 pkt_dev->last_ok = 1;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001210 } else {
1211 sprintf(pg_result,
1212 "xmit_mode -:%s:- unknown\nAvailable modes: %s",
1213 f, "start_xmit, netif_receive\n");
1214 return count;
1215 }
1216 sprintf(pg_result, "OK: xmit_mode=%s", f);
1217 return count;
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (!strcmp(name, "flag")) {
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001220 __u32 flag;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001221 char f[32];
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001222 bool disable = false;
1223
Luiz Capitulino222f1802006-03-20 22:16:13 -08001224 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001226 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001227 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (copy_from_user(f, &user_buffer[i], len))
1230 return -EFAULT;
1231 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001232
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001233 flag = pktgen_read_flag(f, &disable);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001234
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001235 if (flag) {
1236 if (disable)
1237 pkt_dev->flags &= ~flag;
1238 else
1239 pkt_dev->flags |= flag;
1240 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001241 sprintf(pg_result,
1242 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1243 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001244 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001245 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1246 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1247 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02001248 "NO_TIMESTAMP, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001249#ifdef CONFIG_XFRM
1250 "IPSEC, "
1251#endif
1252 "NODE_ALLOC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001253 return count;
1254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1256 return count;
1257 }
1258 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1259 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001260 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001261 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Luiz Capitulino222f1802006-03-20 22:16:13 -08001263 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001265 buf[len] = 0;
1266 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1267 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001268 strcpy(pkt_dev->dst_min, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001269 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1270 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1271 }
1272 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001273 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001274 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1276 return count;
1277 }
1278 if (!strcmp(name, "dst_max")) {
1279 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001280 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001281 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001282
Luiz Capitulino222f1802006-03-20 22:16:13 -08001283 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001285 buf[len] = 0;
1286 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1287 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001288 strcpy(pkt_dev->dst_max, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001289 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1290 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1291 }
1292 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001293 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 i += len;
1295 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1296 return count;
1297 }
1298 if (!strcmp(name, "dst6")) {
1299 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001300 if (len < 0)
1301 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 pkt_dev->flags |= F_IPV6;
1304
Luiz Capitulino222f1802006-03-20 22:16:13 -08001305 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Amerigo Wangc468fb12012-10-09 17:48:20 +00001309 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001310 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001312 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Luiz Capitulino222f1802006-03-20 22:16:13 -08001314 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001315 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 sprintf(pg_result, "OK: dst6=%s", buf);
1319 return count;
1320 }
1321 if (!strcmp(name, "dst6_min")) {
1322 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001323 if (len < 0)
1324 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 pkt_dev->flags |= F_IPV6;
1327
Luiz Capitulino222f1802006-03-20 22:16:13 -08001328 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001330 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Amerigo Wangc468fb12012-10-09 17:48:20 +00001332 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001333 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001335 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001336 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001337 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Luiz Capitulino222f1802006-03-20 22:16:13 -08001339 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 sprintf(pg_result, "OK: dst6_min=%s", buf);
1341 return count;
1342 }
1343 if (!strcmp(name, "dst6_max")) {
1344 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001345 if (len < 0)
1346 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 pkt_dev->flags |= F_IPV6;
1349
Luiz Capitulino222f1802006-03-20 22:16:13 -08001350 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001352 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Amerigo Wangc468fb12012-10-09 17:48:20 +00001354 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001355 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Luiz Capitulino222f1802006-03-20 22:16:13 -08001357 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001358 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Luiz Capitulino222f1802006-03-20 22:16:13 -08001360 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 sprintf(pg_result, "OK: dst6_max=%s", buf);
1362 return count;
1363 }
1364 if (!strcmp(name, "src6")) {
1365 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001366 if (len < 0)
1367 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 pkt_dev->flags |= F_IPV6;
1370
Luiz Capitulino222f1802006-03-20 22:16:13 -08001371 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001373 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Amerigo Wangc468fb12012-10-09 17:48:20 +00001375 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001376 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001378 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Luiz Capitulino222f1802006-03-20 22:16:13 -08001380 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001381 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001382
1383 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 sprintf(pg_result, "OK: src6=%s", buf);
1385 return count;
1386 }
1387 if (!strcmp(name, "src_min")) {
1388 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001389 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001390 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001391
Luiz Capitulino222f1802006-03-20 22:16:13 -08001392 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001394 buf[len] = 0;
1395 if (strcmp(buf, pkt_dev->src_min) != 0) {
1396 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001397 strcpy(pkt_dev->src_min, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001398 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1399 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1400 }
1401 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001402 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 i += len;
1404 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1405 return count;
1406 }
1407 if (!strcmp(name, "src_max")) {
1408 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001409 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001410 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001411
Luiz Capitulino222f1802006-03-20 22:16:13 -08001412 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001414 buf[len] = 0;
1415 if (strcmp(buf, pkt_dev->src_max) != 0) {
1416 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001417 strcpy(pkt_dev->src_max, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001418 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1419 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1420 }
1421 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001422 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 i += len;
1424 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1425 return count;
1426 }
1427 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001429 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001430 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001433 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001436 if (!mac_pton(valstr, pkt_dev->dst_mac))
1437 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001439 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001440
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001441 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return count;
1443 }
1444 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001446 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001447 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001450 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001453 if (!mac_pton(valstr, pkt_dev->src_mac))
1454 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001455 /* Set up Src MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001456 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001457
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001458 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return count;
1460 }
1461
Luiz Capitulino222f1802006-03-20 22:16:13 -08001462 if (!strcmp(name, "clear_counters")) {
1463 pktgen_clear_counters(pkt_dev);
1464 sprintf(pg_result, "OK: Clearing counters.\n");
1465 return count;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 if (!strcmp(name, "flows")) {
1469 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001470 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001471 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 i += len;
1474 if (value > MAX_CFLOWS)
1475 value = MAX_CFLOWS;
1476
1477 pkt_dev->cflows = value;
1478 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1479 return count;
1480 }
Fan Du6bae9192014-01-10 14:39:13 +08001481#ifdef CONFIG_XFRM
Fan Dude4aee72014-01-03 11:18:30 +08001482 if (!strcmp(name, "spi")) {
1483 len = num_arg(&user_buffer[i], 10, &value);
1484 if (len < 0)
1485 return len;
1486
1487 i += len;
1488 pkt_dev->spi = value;
1489 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1490 return count;
1491 }
Fan Du6bae9192014-01-10 14:39:13 +08001492#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!strcmp(name, "flowlen")) {
1494 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001495 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001496 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 i += len;
1499 pkt_dev->lflow = value;
1500 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1501 return count;
1502 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001503
Robert Olsson45b270f2007-08-28 15:45:55 -07001504 if (!strcmp(name, "queue_map_min")) {
1505 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001506 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001507 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001508
Robert Olsson45b270f2007-08-28 15:45:55 -07001509 i += len;
1510 pkt_dev->queue_map_min = value;
1511 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1512 return count;
1513 }
1514
1515 if (!strcmp(name, "queue_map_max")) {
1516 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001517 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001518 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001519
Robert Olsson45b270f2007-08-28 15:45:55 -07001520 i += len;
1521 pkt_dev->queue_map_max = value;
1522 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1523 return count;
1524 }
1525
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001526 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001527 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001528
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001529 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001530 if (len < 0)
1531 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001532 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001533 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001534 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001535 cnt += sprintf(pg_result + cnt,
1536 "%08x%s", ntohl(pkt_dev->labels[n]),
1537 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001538
1539 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1540 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1541 pkt_dev->svlan_id = 0xffff;
1542
1543 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001544 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001545 }
1546 return count;
1547 }
1548
1549 if (!strcmp(name, "vlan_id")) {
1550 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001551 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001552 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001553
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001554 i += len;
1555 if (value <= 4095) {
1556 pkt_dev->vlan_id = value; /* turn on VLAN */
1557
1558 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001559 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001560
1561 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001562 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001563
1564 pkt_dev->nr_labels = 0; /* turn off MPLS */
1565 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1566 } else {
1567 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1568 pkt_dev->svlan_id = 0xffff;
1569
1570 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001571 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001572 }
1573 return count;
1574 }
1575
1576 if (!strcmp(name, "vlan_p")) {
1577 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001578 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001579 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001580
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001581 i += len;
1582 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1583 pkt_dev->vlan_p = value;
1584 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1585 } else {
1586 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1587 }
1588 return count;
1589 }
1590
1591 if (!strcmp(name, "vlan_cfi")) {
1592 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001593 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001594 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001595
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001596 i += len;
1597 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1598 pkt_dev->vlan_cfi = value;
1599 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1600 } else {
1601 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1602 }
1603 return count;
1604 }
1605
1606 if (!strcmp(name, "svlan_id")) {
1607 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001608 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001609 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001610
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001611 i += len;
1612 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1613 pkt_dev->svlan_id = value; /* turn on SVLAN */
1614
1615 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001616 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001617
1618 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001619 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001620
1621 pkt_dev->nr_labels = 0; /* turn off MPLS */
1622 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1623 } else {
1624 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1625 pkt_dev->svlan_id = 0xffff;
1626
1627 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001628 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001629 }
1630 return count;
1631 }
1632
1633 if (!strcmp(name, "svlan_p")) {
1634 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001635 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001636 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001637
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001638 i += len;
1639 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1640 pkt_dev->svlan_p = value;
1641 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1642 } else {
1643 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1644 }
1645 return count;
1646 }
1647
1648 if (!strcmp(name, "svlan_cfi")) {
1649 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001650 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001651 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001652
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001653 i += len;
1654 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1655 pkt_dev->svlan_cfi = value;
1656 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1657 } else {
1658 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1659 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001660 return count;
1661 }
1662
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001663 if (!strcmp(name, "tos")) {
1664 __u32 tmp_value = 0;
1665 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001666 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001667 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001668
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001669 i += len;
1670 if (len == 2) {
1671 pkt_dev->tos = tmp_value;
1672 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1673 } else {
1674 sprintf(pg_result, "ERROR: tos must be 00-ff");
1675 }
1676 return count;
1677 }
1678
1679 if (!strcmp(name, "traffic_class")) {
1680 __u32 tmp_value = 0;
1681 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001682 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001683 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001684
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001685 i += len;
1686 if (len == 2) {
1687 pkt_dev->traffic_class = tmp_value;
1688 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1689 } else {
1690 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1691 }
1692 return count;
1693 }
1694
John Fastabend9e50e3a2010-11-16 19:12:28 +00001695 if (!strcmp(name, "skb_priority")) {
1696 len = num_arg(&user_buffer[i], 9, &value);
1697 if (len < 0)
1698 return len;
1699
1700 i += len;
1701 pkt_dev->skb_priority = value;
1702 sprintf(pg_result, "OK: skb_priority=%i",
1703 pkt_dev->skb_priority);
1704 return count;
1705 }
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1708 return -EINVAL;
1709}
1710
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001711static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Al Virod9dda782013-03-31 18:16:14 -04001713 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001714}
1715
Arjan van de Ven9a321442007-02-12 00:55:35 -08001716static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001717 .open = pktgen_if_open,
1718 .read = seq_read,
1719 .llseek = seq_lseek,
1720 .write = pktgen_if_write,
1721 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001722};
1723
1724static int pktgen_thread_show(struct seq_file *seq, void *v)
1725{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001726 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001727 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001729 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Thomas Graf97dc48e2014-05-16 23:28:54 +02001731 seq_puts(seq, "Running: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001732
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001733 rcu_read_lock();
1734 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001736 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Thomas Graf97dc48e2014-05-16 23:28:54 +02001738 seq_puts(seq, "\nStopped: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001739
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001740 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001741 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001742 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001745 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 else
Thomas Graf97dc48e2014-05-16 23:28:54 +02001747 seq_puts(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001749 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001754static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001755 const char __user * user_buffer,
1756 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Joe Perches8a994a72010-07-12 10:50:23 +00001758 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001759 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001760 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001765 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return -EINVAL;
1767 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001768
Paul Gortmakerd6182222010-10-18 12:14:44 +00001769 max = count;
1770 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001771 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001772 return len;
1773
Paul Gortmakerd6182222010-10-18 12:14:44 +00001774 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /* Read variable name */
1777
1778 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001779 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001780 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 memset(name, 0, sizeof(name));
1783 if (copy_from_user(name, &user_buffer[i], len))
1784 return -EFAULT;
1785 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001786
Luiz Capitulino222f1802006-03-20 22:16:13 -08001787 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001789 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001790 return len;
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 i += len;
1793
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001794 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001795 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Luiz Capitulino222f1802006-03-20 22:16:13 -08001797 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001798 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 ret = -EINVAL;
1800 goto out;
1801 }
1802
1803 pg_result = &(t->result[0]);
1804
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805 if (!strcmp(name, "add_device")) {
1806 char f[32];
1807 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001809 if (len < 0) {
1810 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto out;
1812 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return -EFAULT;
1815 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001816 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001817 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001818 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001819 if (!ret) {
1820 ret = count;
1821 sprintf(pg_result, "OK: add_device=%s", f);
1822 } else
1823 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 goto out;
1825 }
1826
Luiz Capitulino222f1802006-03-20 22:16:13 -08001827 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001828 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001829 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001830 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001831 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001833 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 goto out;
1835 }
1836
Luiz Capitulino222f1802006-03-20 22:16:13 -08001837 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001838 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001839 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto out;
1841 }
1842
1843 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001844out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return ret;
1846}
1847
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001848static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Al Virod9dda782013-03-31 18:16:14 -04001850 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Arjan van de Ven9a321442007-02-12 00:55:35 -08001853static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001854 .open = pktgen_thread_open,
1855 .read = seq_read,
1856 .llseek = seq_lseek,
1857 .write = pktgen_thread_write,
1858 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001859};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001862static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1863 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 struct pktgen_thread *t;
1866 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001867 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Cong Wang4e58a022013-01-28 19:55:53 +00001869 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001870 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001872 if (remove) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001873 pkt_dev->removal_mark = 1;
1874 t->control |= T_REMDEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 break;
1877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001879 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001882/*
1883 * mark a device for removal
1884 */
Cong Wang4e58a022013-01-28 19:55:53 +00001885static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001888 const int max_tries = 10, msec_per_try = 125;
1889 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001890
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001891 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001892 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001893
Luiz Capitulino222f1802006-03-20 22:16:13 -08001894 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001895
Cong Wang4e58a022013-01-28 19:55:53 +00001896 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001897 if (pkt_dev == NULL)
1898 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001899
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001900 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001901 pr_debug("%s: waiting for %s to disappear....\n",
1902 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001903 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001904 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001905
1906 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001907 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1908 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001909 break;
1910 }
1911
1912 }
1913
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001914 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001915}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001916
Cong Wang4e58a022013-01-28 19:55:53 +00001917static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001918{
1919 struct pktgen_thread *t;
1920
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001921 mutex_lock(&pktgen_thread_lock);
1922
Cong Wang4e58a022013-01-28 19:55:53 +00001923 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001924 struct pktgen_dev *pkt_dev;
1925
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001926 if_lock(t);
1927 list_for_each_entry(pkt_dev, &t->if_list, list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001928 if (pkt_dev->odev != dev)
1929 continue;
1930
David Howellsa8ca16e2013-04-12 17:27:28 +01001931 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001932
Alexey Dobriyan29753152009-08-28 23:34:43 -07001933 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00001934 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07001935 &pktgen_if_fops,
1936 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001937 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001938 pr_err("can't move proc entry for '%s'\n",
1939 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001940 break;
1941 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001942 if_unlock(t);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001943 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001944 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Luiz Capitulino222f1802006-03-20 22:16:13 -08001947static int pktgen_device_event(struct notifier_block *unused,
1948 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Jiri Pirko351638e2013-05-28 01:30:21 +00001950 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00001951 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Cong Wang4e58a022013-01-28 19:55:53 +00001953 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001954 return NOTIFY_DONE;
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /* It is OK that we do not hold the group lock right now,
1957 * as we run under the RTNL lock.
1958 */
1959
1960 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001961 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00001962 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00001966 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 return NOTIFY_DONE;
1971}
1972
Cong Wang4e58a022013-01-28 19:55:53 +00001973static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1974 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001975 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001976{
1977 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001978 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001979
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001980 for (i = 0; ifname[i] != '@'; i++) {
1981 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001982 break;
1983
1984 b[i] = ifname[i];
1985 }
1986 b[i] = 0;
1987
Cong Wang4e58a022013-01-28 19:55:53 +00001988 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07001989}
1990
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/* Associate pktgen_dev with a device. */
1993
Cong Wang4e58a022013-01-28 19:55:53 +00001994static int pktgen_setup_dev(const struct pktgen_net *pn,
1995 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001996{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001998 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (pkt_dev->odev) {
2002 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002003 pkt_dev->odev = NULL;
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Cong Wang4e58a022013-01-28 19:55:53 +00002006 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002008 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002009 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002013 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002014 err = -EINVAL;
2015 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002016 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002017 err = -ENETDOWN;
2018 } else {
2019 pkt_dev->odev = odev;
2020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002024 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027/* Read pkt_dev from the interface and set up internal pktgen_dev
2028 * structure to have the right information to create/send packets
2029 */
2030static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2031{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002032 int ntxq;
2033
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002035 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002036 sprintf(pkt_dev->result,
2037 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2038 return;
2039 }
2040
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002041 /* make sure that we don't pick a non-existing transmit queue */
2042 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002043
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002044 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perches294a0b72014-09-09 21:17:30 -07002045 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2046 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2047 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002048 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002049 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002050 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perches294a0b72014-09-09 21:17:30 -07002051 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2052 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2053 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002054 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002055 }
2056
Luiz Capitulino222f1802006-03-20 22:16:13 -08002057 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002059 if (is_zero_ether_addr(pkt_dev->src_mac))
Joe Perches9ea08b12014-01-20 09:52:19 -08002060 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Luiz Capitulino222f1802006-03-20 22:16:13 -08002062 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08002063 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Luiz Capitulino222f1802006-03-20 22:16:13 -08002065 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002066 int i, set = 0, err = 1;
2067 struct inet6_dev *idev;
2068
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002069 if (pkt_dev->min_pkt_size == 0) {
2070 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2071 + sizeof(struct udphdr)
2072 + sizeof(struct pktgen_hdr)
2073 + pkt_dev->pkt_overhead;
2074 }
2075
Eric Dumazetdf7e8e22017-11-04 08:27:14 -07002076 for (i = 0; i < sizeof(struct in6_addr); i++)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002077 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 set = 1;
2079 break;
2080 }
2081
Luiz Capitulino222f1802006-03-20 22:16:13 -08002082 if (!set) {
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * Use linklevel address if unconfigured.
2086 *
2087 * use ipv6_get_lladdr if/when it's get exported
2088 */
2089
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002090 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002091 idev = __in6_dev_get(pkt_dev->odev);
2092 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 struct inet6_ifaddr *ifp;
2094
2095 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002096 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2097 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002098 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002099 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 err = 0;
2101 break;
2102 }
2103 }
2104 read_unlock_bh(&idev->lock);
2105 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002106 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002107 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002108 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002110 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002111 if (pkt_dev->min_pkt_size == 0) {
2112 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2113 + sizeof(struct udphdr)
2114 + sizeof(struct pktgen_hdr)
2115 + pkt_dev->pkt_overhead;
2116 }
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 pkt_dev->saddr_min = 0;
2119 pkt_dev->saddr_max = 0;
2120 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002121
2122 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002125 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (in_dev) {
2127 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002128 pkt_dev->saddr_min =
2129 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 pkt_dev->saddr_max = pkt_dev->saddr_min;
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002134 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2136 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2137 }
2138
2139 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2140 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2141 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002142 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002143 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2144 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2145 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2146
Luiz Capitulino222f1802006-03-20 22:16:13 -08002147 pkt_dev->cur_dst_mac_offset = 0;
2148 pkt_dev->cur_src_mac_offset = 0;
2149 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2150 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2151 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2152 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 pkt_dev->nflows = 0;
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002157static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2158{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002159 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002160 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002161 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002162
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002163 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2164 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002165
Daniel Turull43d28b62010-06-09 22:49:57 +00002166 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002167 if (remaining <= 0)
2168 goto out;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002169
Daniel Borkmann398f3822012-10-28 08:27:19 +00002170 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002171 if (remaining < 100000) {
2172 /* for small delays (<100us), just loop until limit is reached */
2173 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002174 end_time = ktime_get();
2175 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002176 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002177 /* see do_nanosleep */
2178 hrtimer_init_sleeper(&t, current);
2179 do {
2180 set_current_state(TASK_INTERRUPTIBLE);
2181 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002182
2183 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002186 hrtimer_cancel(&t.timer);
2187 } while (t.task && pkt_dev->running && !signal_pending(current));
2188 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002189 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002191
2192 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002193out:
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002194 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002195 destroy_hrtimer_on_stack(&t.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002198static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2199{
Paolo Abeni63d75462016-09-30 16:56:45 +02002200 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002201 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2202 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2203 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2204}
2205
Stephen Hemminger648fda72009-08-27 13:55:07 +00002206static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002207{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002208 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002209}
2210
2211static inline int f_pick(struct pktgen_dev *pkt_dev)
2212{
2213 int flow = pkt_dev->curfl;
2214
2215 if (pkt_dev->flags & F_FLOW_SEQ) {
2216 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2217 /* reset time */
2218 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002219 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002220 pkt_dev->curfl += 1;
2221 if (pkt_dev->curfl >= pkt_dev->cflows)
2222 pkt_dev->curfl = 0; /*reset */
2223 }
2224 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002225 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002226 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002227
Robert Olsson1211a642008-08-05 18:44:26 -07002228 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002229 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002230 pkt_dev->flows[flow].flags = 0;
2231 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002232 }
2233
2234 return pkt_dev->curfl;
2235}
2236
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002237
2238#ifdef CONFIG_XFRM
2239/* If there was already an IPSEC SA, we keep it as is, else
2240 * we go look for it ...
2241*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002242#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002243static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002244{
2245 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002246 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002247 if (!x) {
Fan Duc4549972014-01-03 11:18:32 +08002248
2249 if (pkt_dev->spi) {
2250 /* We need as quick as possible to find the right SA
2251 * Searching with minimum criteria to archieve this.
2252 */
2253 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2254 } else {
2255 /* slow path: we dont already have xfrm_state */
Steffen Klassert7e652642018-06-12 14:07:07 +02002256 x = xfrm_stateonly_find(pn->net, DUMMY_MARK, 0,
Fan Duc4549972014-01-03 11:18:32 +08002257 (xfrm_address_t *)&pkt_dev->cur_daddr,
2258 (xfrm_address_t *)&pkt_dev->cur_saddr,
2259 AF_INET,
2260 pkt_dev->ipsmode,
2261 pkt_dev->ipsproto, 0);
2262 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002263 if (x) {
2264 pkt_dev->flows[flow].x = x;
2265 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002266 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002267 }
2268
2269 }
2270}
2271#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002272static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2273{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002274
2275 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2276 pkt_dev->cur_queue_map = smp_processor_id();
2277
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002278 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002279 __u16 t;
2280 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002281 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002282 (pkt_dev->queue_map_max -
2283 pkt_dev->queue_map_min + 1)
2284 + pkt_dev->queue_map_min;
2285 } else {
2286 t = pkt_dev->cur_queue_map + 1;
2287 if (t > pkt_dev->queue_map_max)
2288 t = pkt_dev->queue_map_min;
2289 }
2290 pkt_dev->cur_queue_map = t;
2291 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002292 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 -07002293}
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295/* Increment/randomize headers according to flags and current values
2296 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2297 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002298static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2299{
2300 __u32 imn;
2301 __u32 imx;
2302 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002304 if (pkt_dev->cflows)
2305 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002308 if (pkt_dev->src_mac_count > 1) {
2309 __u32 mc;
2310 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Luiz Capitulino222f1802006-03-20 22:16:13 -08002312 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002313 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002314 else {
2315 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002316 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002317 pkt_dev->src_mac_count)
2318 pkt_dev->cur_src_mac_offset = 0;
2319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Luiz Capitulino222f1802006-03-20 22:16:13 -08002321 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2322 pkt_dev->hh[11] = tmp;
2323 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2324 pkt_dev->hh[10] = tmp;
2325 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2326 pkt_dev->hh[9] = tmp;
2327 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2328 pkt_dev->hh[8] = tmp;
2329 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2330 pkt_dev->hh[7] = tmp;
2331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Luiz Capitulino222f1802006-03-20 22:16:13 -08002333 /* Deal with Destination MAC */
2334 if (pkt_dev->dst_mac_count > 1) {
2335 __u32 mc;
2336 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Luiz Capitulino222f1802006-03-20 22:16:13 -08002338 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002339 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Luiz Capitulino222f1802006-03-20 22:16:13 -08002341 else {
2342 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002343 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002344 pkt_dev->dst_mac_count) {
2345 pkt_dev->cur_dst_mac_offset = 0;
2346 }
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Luiz Capitulino222f1802006-03-20 22:16:13 -08002349 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2350 pkt_dev->hh[5] = tmp;
2351 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2352 pkt_dev->hh[4] = tmp;
2353 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2354 pkt_dev->hh[3] = tmp;
2355 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2356 pkt_dev->hh[2] = tmp;
2357 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2358 pkt_dev->hh[1] = tmp;
2359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002361 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002362 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002363 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002364 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2365 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002366 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002367 htonl(0x000fffff));
2368 }
2369
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002370 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002371 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002372 }
2373
2374 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002375 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002376 }
2377
Luiz Capitulino222f1802006-03-20 22:16:13 -08002378 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2379 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002380 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002381 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2382 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Luiz Capitulino222f1802006-03-20 22:16:13 -08002384 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 pkt_dev->cur_udp_src++;
2386 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2387 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002388 }
2389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Luiz Capitulino222f1802006-03-20 22:16:13 -08002391 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2392 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002393 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002394 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2395 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002396 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002398 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 }
2401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 if (!(pkt_dev->flags & F_IPV6)) {
2404
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002405 imn = ntohl(pkt_dev->saddr_min);
2406 imx = ntohl(pkt_dev->saddr_max);
2407 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002409 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002410 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 else {
2412 t = ntohl(pkt_dev->cur_saddr);
2413 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002414 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 pkt_dev->cur_saddr = htonl(t);
2419 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002420
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002421 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2423 } else {
Al Viro252e33462006-11-14 20:48:11 -08002424 imn = ntohl(pkt_dev->daddr_min);
2425 imx = ntohl(pkt_dev->daddr_max);
2426 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002428 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (pkt_dev->flags & F_IPDST_RND) {
2430
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002431 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002432 t = prandom_u32() %
2433 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002434 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002435 } while (ipv4_is_loopback(s) ||
2436 ipv4_is_multicast(s) ||
2437 ipv4_is_lbcast(s) ||
2438 ipv4_is_zeronet(s) ||
2439 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002440 pkt_dev->cur_daddr = s;
2441 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 t = ntohl(pkt_dev->cur_daddr);
2443 t++;
2444 if (t > imx) {
2445 t = imn;
2446 }
2447 pkt_dev->cur_daddr = htonl(t);
2448 }
2449 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002450 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002451 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002452 pkt_dev->flows[flow].cur_daddr =
2453 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002454#ifdef CONFIG_XFRM
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002455 if (pkt_dev->flags & F_IPSEC)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002456 get_ipsec_sa(pkt_dev, flow);
2457#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 pkt_dev->nflows++;
2459 }
2460 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002461 } else { /* IPV6 * */
2462
Joe Perches06e30412012-10-18 17:55:31 +00002463 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 int i;
2465
2466 /* Only random destinations yet */
2467
Luiz Capitulino222f1802006-03-20 22:16:13 -08002468 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002470 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002471 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2472 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
Luiz Capitulino222f1802006-03-20 22:16:13 -08002477 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2478 __u32 t;
2479 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002480 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002481 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2482 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002483 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002485 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002487 }
2488 pkt_dev->cur_pkt_size = t;
2489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002491 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 pkt_dev->flows[flow].count++;
2494}
2495
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002496
2497#ifdef CONFIG_XFRM
Fengguang Wu5537a052014-01-06 11:00:07 +01002498static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
Fan Ducf93d47e2014-01-03 11:18:31 +08002499
2500 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2501};
2502
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002503static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2504{
2505 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2506 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002507 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002508
2509 if (!x)
2510 return 0;
2511 /* XXX: we dont support tunnel mode for now until
2512 * we resolve the dst issue */
Fan Ducf93d47e2014-01-03 11:18:31 +08002513 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002514 return 0;
2515
Fan Ducf93d47e2014-01-03 11:18:31 +08002516 /* But when user specify an valid SPI, transformation
2517 * supports both transport/tunnel mode + ESP/AH type.
2518 */
2519 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
David Millerb6ca8bd2017-11-28 15:45:44 -05002520 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
Fan Ducf93d47e2014-01-03 11:18:31 +08002521
2522 rcu_read_lock_bh();
Herbert Xu13996372007-10-17 21:35:51 -07002523 err = x->outer_mode->output(x, skb);
Fan Ducf93d47e2014-01-03 11:18:31 +08002524 rcu_read_unlock_bh();
Fan Du6de9ace2014-01-03 11:18:28 +08002525 if (err) {
2526 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002527 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002528 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002529 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002530 if (err) {
2531 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002532 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002533 }
Fan Du0af0a412014-01-03 11:18:27 +08002534 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002535 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002536 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002537 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002538error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002539 return err;
2540}
2541
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002542static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002543{
2544 if (pkt_dev->cflows) {
2545 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002546 int i;
2547 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002548 struct xfrm_state *x = pkt_dev->flows[i].x;
2549 if (x) {
2550 xfrm_state_put(x);
2551 pkt_dev->flows[i].x = NULL;
2552 }
2553 }
2554 }
2555}
2556
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002557static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002558 struct sk_buff *skb, __be16 protocol)
2559{
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002560 if (pkt_dev->flags & F_IPSEC) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002561 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2562 int nhead = 0;
2563 if (x) {
Eric Dumazetd4969582015-05-25 16:06:37 -07002564 struct ethhdr *eth;
fan.du38682042013-12-01 16:28:48 +08002565 struct iphdr *iph;
Eric Dumazetd4969582015-05-25 16:06:37 -07002566 int ret;
fan.du38682042013-12-01 16:28:48 +08002567
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002568 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002569 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002570 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2571 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002572 pr_err("Error expanding ipsec packet %d\n",
2573 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002574 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002575 }
2576 }
2577
2578 /* ipsec is not expecting ll header */
2579 skb_pull(skb, ETH_HLEN);
2580 ret = pktgen_output_ipsec(skb, pkt_dev);
2581 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002582 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002583 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002584 }
2585 /* restore ll */
Johannes Bergd58ff352017-06-16 14:29:23 +02002586 eth = skb_push(skb, ETH_HLEN);
Eric Dumazetd4969582015-05-25 16:06:37 -07002587 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2588 eth->h_proto = protocol;
fan.du38682042013-12-01 16:28:48 +08002589
2590 /* Update IPv4 header len as well as checksum value */
2591 iph = ip_hdr(skb);
2592 iph->tot_len = htons(skb->len - ETH_HLEN);
2593 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002594 }
2595 }
2596 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002597err:
2598 kfree_skb(skb);
2599 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002600}
2601#endif
2602
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002603static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2604{
Eric Dumazet95c96172012-04-15 05:58:06 +00002605 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002606 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002607 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002608
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002609 mpls--;
2610 *mpls |= MPLS_STACK_BOTTOM;
2611}
2612
Al Viro0f37c602006-11-03 03:49:56 -08002613static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2614 unsigned int prio)
2615{
2616 return htons(id | (cfi << 12) | (prio << 13));
2617}
2618
Eric Dumazet26ad7872011-01-25 13:26:05 -08002619static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2620 int datalen)
2621{
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002622 struct timespec64 timestamp;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002623 struct pktgen_hdr *pgh;
2624
Johannes Berg4df864c2017-06-16 14:29:21 +02002625 pgh = skb_put(skb, sizeof(*pgh));
Eric Dumazet26ad7872011-01-25 13:26:05 -08002626 datalen -= sizeof(*pgh);
2627
2628 if (pkt_dev->nfrags <= 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002629 skb_put_zero(skb, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002630 } else {
2631 int frags = pkt_dev->nfrags;
2632 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002633 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002634
2635
2636 if (frags > MAX_SKB_FRAGS)
2637 frags = MAX_SKB_FRAGS;
2638 len = datalen - frags * PAGE_SIZE;
2639 if (len > 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002640 skb_put_zero(skb, len);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002641 datalen = frags * PAGE_SIZE;
2642 }
2643
2644 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002645 frag_len = (datalen/frags) < PAGE_SIZE ?
2646 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002647 while (datalen > 0) {
2648 if (unlikely(!pkt_dev->page)) {
2649 int node = numa_node_id();
2650
2651 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2652 node = pkt_dev->node;
2653 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2654 if (!pkt_dev->page)
2655 break;
2656 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002657 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002658 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002659 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002660 /*last fragment, fill rest of data*/
2661 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002662 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2663 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002664 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002665 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2666 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2667 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2668 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002669 i++;
2670 skb_shinfo(skb)->nr_frags = i;
2671 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002672 }
2673
2674 /* Stamp the time, and sequence number,
2675 * convert them to network byte order
2676 */
2677 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2678 pgh->seq_num = htonl(pkt_dev->seq_num);
2679
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002680 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2681 pgh->tv_sec = 0;
2682 pgh->tv_usec = 0;
2683 } else {
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002684 /*
2685 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2686 * as done by wireshark, or y2038 when interpreted as signed.
2687 * This is probably harmless, but if anyone wants to improve
2688 * it, we could introduce a variant that puts 64-bit nanoseconds
2689 * into the respective header bytes.
2690 * This would also be slightly faster to read.
2691 */
2692 ktime_get_real_ts64(&timestamp);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002693 pgh->tv_sec = htonl(timestamp.tv_sec);
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002694 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002695 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002696}
2697
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002698static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
Paolo Abeni63d75462016-09-30 16:56:45 +02002699 struct pktgen_dev *pkt_dev)
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002700{
Paolo Abeni63d75462016-09-30 16:56:45 +02002701 unsigned int extralen = LL_RESERVED_SPACE(dev);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002702 struct sk_buff *skb = NULL;
Paolo Abeni63d75462016-09-30 16:56:45 +02002703 unsigned int size;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002704
Paolo Abeni63d75462016-09-30 16:56:45 +02002705 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002706 if (pkt_dev->flags & F_NODE) {
2707 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2708
2709 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2710 if (likely(skb)) {
2711 skb_reserve(skb, NET_SKB_PAD);
2712 skb->dev = dev;
2713 }
2714 } else {
2715 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2716 }
John Fastabend3de03592016-01-10 21:38:44 -08002717
Paolo Abeni63d75462016-09-30 16:56:45 +02002718 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
John Fastabend3de03592016-01-10 21:38:44 -08002719 if (likely(skb))
Paolo Abeni63d75462016-09-30 16:56:45 +02002720 skb_reserve(skb, extralen - 16);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002721
2722 return skb;
2723}
2724
Luiz Capitulino222f1802006-03-20 22:16:13 -08002725static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2726 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
2728 struct sk_buff *skb = NULL;
2729 __u8 *eth;
2730 struct udphdr *udph;
2731 int datalen, iplen;
2732 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002733 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002734 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002735 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2736 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2737 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2738 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002739 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002740
2741 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002742 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002743
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002744 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002745 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002746
Robert Olsson64053be2005-06-26 15:27:10 -07002747 /* Update any of the values, used when we're incrementing various
2748 * fields.
2749 */
2750 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002751 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002752
Paolo Abeni63d75462016-09-30 16:56:45 +02002753 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (!skb) {
2755 sprintf(pkt_dev->result, "No memory");
2756 return NULL;
2757 }
2758
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002759 prefetchw(skb->data);
Paolo Abeni63d75462016-09-30 16:56:45 +02002760 skb_reserve(skb, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02002763 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02002764 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002765 if (pkt_dev->nr_labels)
2766 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002767
2768 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002769 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02002770 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002771 *svlan_tci = build_tci(pkt_dev->svlan_id,
2772 pkt_dev->svlan_cfi,
2773 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002774 svlan_encapsulated_proto = skb_put(skb,
2775 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002776 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002777 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002778 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002779 *vlan_tci = build_tci(pkt_dev->vlan_id,
2780 pkt_dev->vlan_cfi,
2781 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002782 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002783 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002784 }
2785
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00002786 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00002787 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002788 iph = skb_put(skb, sizeof(struct iphdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00002789
2790 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002791 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002792 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002793 skb->priority = pkt_dev->skb_priority;
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002796 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002798 /* Eth + IPh + UDPh + mpls */
2799 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002800 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002801 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 udph->source = htons(pkt_dev->cur_udp_src);
2805 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002806 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002807 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 iph->ihl = 5;
2810 iph->version = 4;
2811 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002812 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002813 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 iph->saddr = pkt_dev->cur_saddr;
2815 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002816 iph->id = htons(pkt_dev->ip_id);
2817 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 iph->frag_off = 0;
2819 iplen = 20 + 8 + datalen;
2820 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002821 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002822 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 skb->dev = odev;
2824 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002825
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002826 pktgen_finalize_skb(pkt_dev, skb, datalen);
2827
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002828 if (!(pkt_dev->flags & F_UDPCSUM)) {
2829 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08002830 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002831 skb->ip_summed = CHECKSUM_PARTIAL;
2832 skb->csum = 0;
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002833 udp4_hwcsum(skb, iph->saddr, iph->daddr);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002834 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002835 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002836
2837 /* add protocol-dependent pseudo-header */
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002838 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002839 datalen + 8, IPPROTO_UDP, csum);
2840
2841 if (udph->check == 0)
2842 udph->check = CSUM_MANGLED_0;
2843 }
2844
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002845#ifdef CONFIG_XFRM
2846 if (!process_ipsec(pkt_dev, skb, protocol))
2847 return NULL;
2848#endif
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return skb;
2851}
2852
Luiz Capitulino222f1802006-03-20 22:16:13 -08002853static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2854 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 struct sk_buff *skb = NULL;
2857 __u8 *eth;
2858 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002859 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002861 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002862 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002863 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2864 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2865 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2866 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002867 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002868
2869 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002870 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002871
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002872 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002873 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002874
Robert Olsson64053be2005-06-26 15:27:10 -07002875 /* Update any of the values, used when we're incrementing various
2876 * fields.
2877 */
2878 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002879 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002880
Paolo Abeni63d75462016-09-30 16:56:45 +02002881 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (!skb) {
2883 sprintf(pkt_dev->result, "No memory");
2884 return NULL;
2885 }
2886
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002887 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 skb_reserve(skb, 16);
2889
2890 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02002891 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02002892 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002893 if (pkt_dev->nr_labels)
2894 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002895
2896 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002897 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02002898 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002899 *svlan_tci = build_tci(pkt_dev->svlan_id,
2900 pkt_dev->svlan_cfi,
2901 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002902 svlan_encapsulated_proto = skb_put(skb,
2903 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002904 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002905 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002906 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002907 *vlan_tci = build_tci(pkt_dev->vlan_id,
2908 pkt_dev->vlan_cfi,
2909 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002910 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002911 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002912 }
2913
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00002914 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00002915 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002916 iph = skb_put(skb, sizeof(struct ipv6hdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00002917
2918 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002919 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002920 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002921 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002924 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002925
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002926 /* Eth + IPh + UDPh + mpls */
2927 datalen = pkt_dev->cur_pkt_size - 14 -
2928 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002929 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002931 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002933 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
2935
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002936 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 udph->source = htons(pkt_dev->cur_udp_src);
2938 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002939 udph->len = htons(udplen);
2940 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002942 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002944 if (pkt_dev->traffic_class) {
2945 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002946 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002947 }
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 iph->hop_limit = 32;
2950
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002951 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 iph->nexthdr = IPPROTO_UDP;
2953
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002954 iph->daddr = pkt_dev->cur_in6_daddr;
2955 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002957 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 skb->dev = odev;
2959 skb->pkt_type = PACKET_HOST;
2960
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002961 pktgen_finalize_skb(pkt_dev, skb, datalen);
2962
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002963 if (!(pkt_dev->flags & F_UDPCSUM)) {
2964 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08002965 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002966 skb->ip_summed = CHECKSUM_PARTIAL;
2967 skb->csum_start = skb_transport_header(skb) - skb->head;
2968 skb->csum_offset = offsetof(struct udphdr, check);
2969 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2970 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002971 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002972
2973 /* add protocol-dependent pseudo-header */
2974 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2975
2976 if (udph->check == 0)
2977 udph->check = CSUM_MANGLED_0;
2978 }
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return skb;
2981}
2982
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002983static struct sk_buff *fill_packet(struct net_device *odev,
2984 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002986 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return fill_packet_ipv6(odev, pkt_dev);
2988 else
2989 return fill_packet_ipv4(odev, pkt_dev);
2990}
2991
Luiz Capitulino222f1802006-03-20 22:16:13 -08002992static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002994 pkt_dev->seq_num = 1;
2995 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002997 pkt_dev->tx_bytes = 0;
2998 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
3001/* Set up structure for sending pkts, clear counters */
3002
3003static void pktgen_run(struct pktgen_thread *t)
3004{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003005 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 int started = 0;
3007
Joe Perchesf9467ea2010-06-21 12:29:14 +00003008 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003010 rcu_read_lock();
3011 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 /*
3014 * setup odev and create initial packet.
3015 */
3016 pktgen_setup_inject(pkt_dev);
3017
Luiz Capitulino222f1802006-03-20 22:16:13 -08003018 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 pktgen_clear_counters(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003021 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003022
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003023 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 strcpy(pkt_dev->result, "Starting");
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003026 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003028 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 strcpy(pkt_dev->result, "Error starting");
3030 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003031 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003032 if (started)
3033 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
Cong Wang4e58a022013-01-28 19:55:53 +00003036static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003038 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Joe Perchesf9467ea2010-06-21 12:29:14 +00003040 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003042 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003043
Cong Wang4e58a022013-01-28 19:55:53 +00003044 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003045 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003046
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003047 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
Stephen Hemminger648fda72009-08-27 13:55:07 +00003050static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003052 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003054 rcu_read_lock();
3055 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3056 if (pkt_dev->running) {
3057 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003058 return 1;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003059 }
3060 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Luiz Capitulino222f1802006-03-20 22:16:13 -08003064static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003066 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Paolo Abeni396244b2019-06-06 15:45:03 +02003068 /* note: 't' will still be around even after the unlock/lock
3069 * cycle because pktgen_thread threads are only cleared at
3070 * net exit
3071 */
3072 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003073 msleep_interruptible(100);
Paolo Abeni396244b2019-06-06 15:45:03 +02003074 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Luiz Capitulino222f1802006-03-20 22:16:13 -08003076 if (signal_pending(current))
3077 goto signal;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003078 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003079 return 1;
3080signal:
3081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
Cong Wang4e58a022013-01-28 19:55:53 +00003084static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003086 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003088
Paolo Abeni396244b2019-06-06 15:45:03 +02003089 /* prevent from racing with rmmod */
3090 if (!try_module_get(THIS_MODULE))
3091 return sig;
3092
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003093 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003094
Cong Wang4e58a022013-01-28 19:55:53 +00003095 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003097 if (sig == 0)
3098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003100
3101 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003102 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003104
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003105 mutex_unlock(&pktgen_thread_lock);
Paolo Abeni396244b2019-06-06 15:45:03 +02003106 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return sig;
3108}
3109
Cong Wang4e58a022013-01-28 19:55:53 +00003110static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003112 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Joe Perchesf9467ea2010-06-21 12:29:14 +00003114 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003116 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Cong Wang4e58a022013-01-28 19:55:53 +00003118 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003120
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003121 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003123 /* Propagate thread->control */
3124 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003125
Cong Wang4e58a022013-01-28 19:55:53 +00003126 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Cong Wang4e58a022013-01-28 19:55:53 +00003129static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003130{
3131 struct pktgen_thread *t;
3132
Joe Perchesf9467ea2010-06-21 12:29:14 +00003133 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003134
3135 mutex_lock(&pktgen_thread_lock);
3136
Cong Wang4e58a022013-01-28 19:55:53 +00003137 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003138 t->control |= (T_REMDEVALL);
3139
3140 mutex_unlock(&pktgen_thread_lock);
3141
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003142 /* Propagate thread->control */
3143 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003144
Cong Wang4e58a022013-01-28 19:55:53 +00003145 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003146}
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3149{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003150 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003152 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3153 pkt_dev->started_at);
3154 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Daniel Turull03a14ab2011-03-09 14:11:00 -08003156 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003157 (unsigned long long)ktime_to_us(elapsed),
3158 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3159 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003160 (unsigned long long)pkt_dev->sofar,
3161 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003163 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3164 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 bps = pps * 8 * pkt_dev->cur_pkt_size;
3167
3168 mbps = bps;
3169 do_div(mbps, 1000000);
3170 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3171 (unsigned long long)pps,
3172 (unsigned long long)mbps,
3173 (unsigned long long)bps,
3174 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003178static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003180 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003181
Luiz Capitulino222f1802006-03-20 22:16:13 -08003182 if (!pkt_dev->running) {
Joe Perches294a0b72014-09-09 21:17:30 -07003183 pr_warn("interface: %s is already stopped\n",
3184 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003185 return -EINVAL;
3186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003188 pkt_dev->running = 0;
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003189 kfree_skb(pkt_dev->skb);
3190 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003191 pkt_dev->stopped_at = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003193 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Luiz Capitulino222f1802006-03-20 22:16:13 -08003198static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003200 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003201
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003202 rcu_read_lock();
3203 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003204 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205 continue;
3206 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003207 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003208 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003209 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003211 rcu_read_unlock();
3212
Luiz Capitulino222f1802006-03-20 22:16:13 -08003213 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214}
3215
Luiz Capitulino222f1802006-03-20 22:16:13 -08003216static void pktgen_stop(struct pktgen_thread *t)
3217{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003218 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Joe Perchesf9467ea2010-06-21 12:29:14 +00003220 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003222 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003224 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003225 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003228 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003231/*
3232 * one of our devices needs to be removed - find it
3233 * and remove it
3234 */
3235static void pktgen_rem_one_if(struct pktgen_thread *t)
3236{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003237 struct list_head *q, *n;
3238 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003239
Joe Perchesf9467ea2010-06-21 12:29:14 +00003240 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003241
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003242 list_for_each_safe(q, n, &t->if_list) {
3243 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003244
Luiz Capitulino222f1802006-03-20 22:16:13 -08003245 if (!cur->removal_mark)
3246 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003247
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003248 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003249 cur->skb = NULL;
3250
3251 pktgen_remove_device(t, cur);
3252
3253 break;
3254 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003255}
3256
Luiz Capitulino222f1802006-03-20 22:16:13 -08003257static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003259 struct list_head *q, *n;
3260 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003261
Joe Perchesf9467ea2010-06-21 12:29:14 +00003262 func_enter();
3263
Luiz Capitulino222f1802006-03-20 22:16:13 -08003264 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003265
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003266 list_for_each_safe(q, n, &t->if_list) {
3267 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003268
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003269 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003270 cur->skb = NULL;
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 pktgen_remove_device(t, cur);
3273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
Luiz Capitulino222f1802006-03-20 22:16:13 -08003276static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003278 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003279 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281
Stephen Hemmingeref879792009-09-22 19:41:43 +00003282static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003283{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003284 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003285 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003286 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003287}
3288
Stephen Hemmingeref879792009-09-22 19:41:43 +00003289static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3290{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003291 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003292
Reshetova, Elena63354792017-06-30 13:07:58 +03003293 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003294 if (signal_pending(current))
3295 break;
3296
3297 if (need_resched())
3298 pktgen_resched(pkt_dev);
3299 else
3300 cpu_relax();
3301 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003302 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003303}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003304
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003305static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
Mark Rutland6aa7de02017-10-23 14:07:29 -07003307 unsigned int burst = READ_ONCE(pkt_dev->burst);
Stephen Hemminger00829822008-11-20 20:14:53 -08003308 struct net_device *odev = pkt_dev->odev;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003309 struct netdev_queue *txq;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003310 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 int ret;
3312
Stephen Hemmingeref879792009-09-22 19:41:43 +00003313 /* If device is offline, then don't send */
3314 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3315 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003316 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003318
Stephen Hemmingeref879792009-09-22 19:41:43 +00003319 /* This is max DELAY, this has special meaning of
3320 * "never transmit"
3321 */
3322 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003323 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003324 return;
3325 }
3326
3327 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003328 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3329 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3330 /* build a new pkt */
3331 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003332
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003333 pkt_dev->skb = fill_packet(odev, pkt_dev);
3334 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003335 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003336 schedule();
3337 pkt_dev->clone_count--; /* back out increment, OOM */
3338 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003340 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003341 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003343
Stephen Hemmingeref879792009-09-22 19:41:43 +00003344 if (pkt_dev->delay && pkt_dev->last_ok)
3345 spin(pkt_dev, pkt_dev->next_tx);
3346
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003347 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3348 skb = pkt_dev->skb;
3349 skb->protocol = eth_type_trans(skb, skb->dev);
Reshetova, Elena63354792017-06-30 13:07:58 +03003350 refcount_add(burst, &skb->users);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003351 local_bh_disable();
3352 do {
3353 ret = netif_receive_skb(skb);
3354 if (ret == NET_RX_DROP)
3355 pkt_dev->errors++;
3356 pkt_dev->sofar++;
3357 pkt_dev->seq_num++;
Reshetova, Elena63354792017-06-30 13:07:58 +03003358 if (refcount_read(&skb->users) != burst) {
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003359 /* skb was queued by rps/rfs or taps,
3360 * so cannot reuse this skb
3361 */
Reshetova, Elena63354792017-06-30 13:07:58 +03003362 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003363 /* get out of the loop and wait
3364 * until skb is consumed
3365 */
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003366 break;
3367 }
3368 /* skb was 'freed' by stack, so clean few
3369 * bits and reuse it
3370 */
Willem de Bruijna5135bc2017-01-07 17:06:36 -05003371 skb_reset_tc(skb);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003372 } while (--burst > 0);
3373 goto out; /* Skips xmit_mode M_START_XMIT */
John Fastabend0967f242016-07-02 14:12:54 -07003374 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3375 local_bh_disable();
Reshetova, Elena63354792017-06-30 13:07:58 +03003376 refcount_inc(&pkt_dev->skb->users);
John Fastabend0967f242016-07-02 14:12:54 -07003377
3378 ret = dev_queue_xmit(pkt_dev->skb);
3379 switch (ret) {
3380 case NET_XMIT_SUCCESS:
3381 pkt_dev->sofar++;
3382 pkt_dev->seq_num++;
3383 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3384 break;
3385 case NET_XMIT_DROP:
3386 case NET_XMIT_CN:
3387 /* These are all valid return codes for a qdisc but
3388 * indicate packets are being dropped or will likely
3389 * be dropped soon.
3390 */
3391 case NETDEV_TX_BUSY:
3392 /* qdisc may call dev_hard_start_xmit directly in cases
3393 * where no queues exist e.g. loopback device, virtual
3394 * devices, etc. In this case we need to handle
3395 * NETDEV_TX_ codes.
3396 */
3397 default:
3398 pkt_dev->errors++;
3399 net_info_ratelimited("%s xmit error: %d\n",
3400 pkt_dev->odevname, ret);
3401 break;
3402 }
3403 goto out;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003404 }
3405
Daniel Borkmann10c51b56232014-08-27 11:11:27 +02003406 txq = skb_get_tx_queue(odev, pkt_dev->skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003407
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003408 local_bh_disable();
3409
3410 HARD_TX_LOCK(odev, txq, smp_processor_id());
Stephen Hemmingeref879792009-09-22 19:41:43 +00003411
Daniel Borkmann6f25cd42014-04-07 17:18:30 +02003412 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003413 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003414 pkt_dev->last_ok = 0;
3415 goto unlock;
3416 }
Reshetova, Elena63354792017-06-30 13:07:58 +03003417 refcount_add(burst, &pkt_dev->skb->users);
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003418
3419xmit_more:
3420 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Stephen Hemmingeref879792009-09-22 19:41:43 +00003422 switch (ret) {
3423 case NETDEV_TX_OK:
Stephen Hemmingeref879792009-09-22 19:41:43 +00003424 pkt_dev->last_ok = 1;
3425 pkt_dev->sofar++;
3426 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003427 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003428 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3429 goto xmit_more;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003430 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003431 case NET_XMIT_DROP:
3432 case NET_XMIT_CN:
John Fastabendf466dba2009-12-23 22:02:57 -08003433 /* skb has been consumed */
3434 pkt_dev->errors++;
3435 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003436 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003437 net_info_ratelimited("%s xmit error: %d\n",
3438 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003439 pkt_dev->errors++;
3440 /* fallthru */
Stephen Hemmingeref879792009-09-22 19:41:43 +00003441 case NETDEV_TX_BUSY:
3442 /* Retry it next time */
Reshetova, Elena63354792017-06-30 13:07:58 +03003443 refcount_dec(&(pkt_dev->skb->users));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003444 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003445 }
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003446 if (unlikely(burst))
Reshetova, Elena63354792017-06-30 13:07:58 +03003447 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
Eric Dumazet0835acf2009-09-30 13:03:33 +00003448unlock:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003449 HARD_TX_UNLOCK(odev, txq);
3450
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003451out:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003452 local_bh_enable();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 /* If pkt_dev->count is zero, then run forever */
3455 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003456 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 /* Done with this */
3459 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003460 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003463/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 * Main loop of the thread goes here
3465 */
3466
David S. Milleree74baa2007-01-01 20:51:53 -08003467static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
3469 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003470 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003471 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
David S. Milleree74baa2007-01-01 20:51:53 -08003474 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003477 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Joe Perchesf9467ea2010-06-21 12:29:14 +00003479 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003481 set_freezable();
3482
David S. Milleree74baa2007-01-01 20:51:53 -08003483 while (!kthread_should_stop()) {
3484 pkt_dev = next_to_run(t);
3485
Stephen Hemmingeref879792009-09-22 19:41:43 +00003486 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003487 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003488 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003489 wait_event_interruptible_timeout(t->queue,
3490 t->control != 0,
3491 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003492 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003493 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003494 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003495
Stephen Hemmingeref879792009-09-22 19:41:43 +00003496 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 pktgen_xmit(pkt_dev);
3498
Stephen Hemmingeref879792009-09-22 19:41:43 +00003499 if (need_resched())
3500 pktgen_resched(pkt_dev);
3501 else
3502 cpu_relax();
3503 }
3504
Luiz Capitulino222f1802006-03-20 22:16:13 -08003505 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 pktgen_stop(t);
3507 t->control &= ~(T_STOP);
3508 }
3509
Luiz Capitulino222f1802006-03-20 22:16:13 -08003510 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 pktgen_run(t);
3512 t->control &= ~(T_RUN);
3513 }
3514
Luiz Capitulino222f1802006-03-20 22:16:13 -08003515 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003517 t->control &= ~(T_REMDEVALL);
3518 }
3519
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003521 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 t->control &= ~(T_REMDEV);
3523 }
3524
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003525 try_to_freeze();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Joe Perchesf9467ea2010-06-21 12:29:14 +00003528 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003529 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Joe Perchesf9467ea2010-06-21 12:29:14 +00003531 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003532 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Joe Perchesf9467ea2010-06-21 12:29:14 +00003534 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003535 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003536
David S. Milleree74baa2007-01-01 20:51:53 -08003537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538}
3539
Luiz Capitulino222f1802006-03-20 22:16:13 -08003540static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003541 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003543 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003544 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003546 rcu_read_lock();
3547 list_for_each_entry_rcu(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003548 if (strncmp(p->odevname, ifname, len) == 0) {
3549 if (p->odevname[len]) {
3550 if (exact || p->odevname[len] != '@')
3551 continue;
3552 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003553 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003554 break;
3555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003557 rcu_read_unlock();
Joe Perchesf9467ea2010-06-21 12:29:14 +00003558 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003559 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003562/*
3563 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 */
3565
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566static int add_dev_to_thread(struct pktgen_thread *t,
3567 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
3569 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003571 /* This function cannot be called concurrently, as its called
3572 * under pktgen_thread_lock mutex, but it can run from
3573 * userspace on another CPU than the kthread. The if_lock()
3574 * is used here to sync with concurrent instances of
3575 * _rem_dev_from_if_list() invoked via kthread, which is also
3576 * updating the if_list */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003577 if_lock(t);
3578
3579 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003580 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003581 rv = -EBUSY;
3582 goto out;
3583 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 pkt_dev->running = 0;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003586 pkt_dev->pg_thread = t;
3587 list_add_rcu(&pkt_dev->list, &t->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Luiz Capitulino222f1802006-03-20 22:16:13 -08003589out:
3590 if_unlock(t);
3591 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
3593
3594/* Called under thread lock */
3595
Luiz Capitulino222f1802006-03-20 22:16:13 -08003596static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003598 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003599 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003600 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 /* We don't allow a device to be on several threads */
3603
Cong Wang4e58a022013-01-28 19:55:53 +00003604 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003605 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003606 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003607 return -EBUSY;
3608 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003609
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003610 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003611 if (!pkt_dev)
3612 return -ENOMEM;
3613
Eric Dumazet593f63b2009-11-23 01:44:37 +00003614 strcpy(pkt_dev->odevname, ifname);
Kees Cookfd7bece2018-06-12 14:27:52 -07003615 pkt_dev->flows = vzalloc_node(array_size(MAX_CFLOWS,
3616 sizeof(struct flow_state)),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003617 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003618 if (pkt_dev->flows == NULL) {
3619 kfree(pkt_dev);
3620 return -ENOMEM;
3621 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003622
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003623 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003624 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003625 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003626 pkt_dev->count = pg_count_d;
3627 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003628 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003629 pkt_dev->udp_src_max = 9;
3630 pkt_dev->udp_dst_min = 9;
3631 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003632 pkt_dev->vlan_p = 0;
3633 pkt_dev->vlan_cfi = 0;
3634 pkt_dev->vlan_id = 0xffff;
3635 pkt_dev->svlan_p = 0;
3636 pkt_dev->svlan_cfi = 0;
3637 pkt_dev->svlan_id = 0xffff;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003638 pkt_dev->burst = 1;
Robert Olssone99b99b2010-03-18 22:44:30 +00003639 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003640
Cong Wang4e58a022013-01-28 19:55:53 +00003641 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003642 if (err)
3643 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003644 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3645 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003646
Cong Wang4e58a022013-01-28 19:55:53 +00003647 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003648 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003649 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003650 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003651 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003652 err = -EINVAL;
3653 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003654 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003655#ifdef CONFIG_XFRM
3656 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3657 pkt_dev->ipsproto = IPPROTO_ESP;
Fan Ducf93d47e2014-01-03 11:18:31 +08003658
3659 /* xfrm tunnel mode needs additional dst to extract outter
3660 * ip header protocol/ttl/id field, here creat a phony one.
3661 * instead of looking for a valid rt, which definitely hurting
3662 * performance under such circumstance.
3663 */
3664 pkt_dev->dstops.family = AF_INET;
David Millerb6ca8bd2017-11-28 15:45:44 -05003665 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3666 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3667 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3668 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003669#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003670
3671 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003672out2:
3673 dev_put(pkt_dev->odev);
3674out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003675#ifdef CONFIG_XFRM
3676 free_SAs(pkt_dev);
3677#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003678 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003679 kfree(pkt_dev);
3680 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
Cong Wang4e58a022013-01-28 19:55:53 +00003683static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003685 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003686 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003687 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003689 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3690 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003691 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003692 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003693 return -ENOMEM;
3694 }
3695
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02003696 mutex_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003698
David S. Milleree74baa2007-01-01 20:51:53 -08003699 INIT_LIST_HEAD(&t->if_list);
3700
Cong Wang4e58a022013-01-28 19:55:53 +00003701 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003702 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003703
Eric Dumazet94dcf292011-03-22 16:30:45 -07003704 p = kthread_create_on_node(pktgen_thread_worker,
3705 t,
3706 cpu_to_node(cpu),
3707 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003708 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003709 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003710 list_del(&t->th_list);
3711 kfree(t);
3712 return PTR_ERR(p);
3713 }
3714 kthread_bind(p, cpu);
3715 t->tsk = p;
3716
Cong Wang4e58a022013-01-28 19:55:53 +00003717 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003718 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003719 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003720 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003721 PG_PROC_DIR, t->tsk->comm);
3722 kthread_stop(p);
3723 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003724 kfree(t);
3725 return -EINVAL;
3726 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003727
Cong Wang4e58a022013-01-28 19:55:53 +00003728 t->net = pn;
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003729 get_task_struct(p);
David S. Milleree74baa2007-01-01 20:51:53 -08003730 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003731 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
3733 return 0;
3734}
3735
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003736/*
3737 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003739static void _rem_dev_from_if_list(struct pktgen_thread *t,
3740 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003742 struct list_head *q, *n;
3743 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003745 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003746 list_for_each_safe(q, n, &t->if_list) {
3747 p = list_entry(q, struct pktgen_dev, list);
3748 if (p == pkt_dev)
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003749 list_del_rcu(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003751 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752}
3753
Luiz Capitulino222f1802006-03-20 22:16:13 -08003754static int pktgen_remove_device(struct pktgen_thread *t,
3755 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003757 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Luiz Capitulino222f1802006-03-20 22:16:13 -08003759 if (pkt_dev->running) {
Joe Perches294a0b72014-09-09 21:17:30 -07003760 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003761 pktgen_stop_device(pkt_dev);
3762 }
3763
3764 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 if (pkt_dev->odev) {
3767 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003768 pkt_dev->odev = NULL;
3769 }
3770
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003771 /* Remove proc before if_list entry, because add_device uses
3772 * list to determine if interface already exist, avoid race
3773 * with proc_create_data() */
Markus Elfringef87c5d2014-11-18 20:10:34 +01003774 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003776 /* And update the thread if_list */
3777 _rem_dev_from_if_list(t, pkt_dev);
3778
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003779#ifdef CONFIG_XFRM
3780 free_SAs(pkt_dev);
3781#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003782 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003783 if (pkt_dev->page)
3784 put_page(pkt_dev->page);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003785 kfree_rcu(pkt_dev, rcu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
Cong Wang4e58a022013-01-28 19:55:53 +00003789static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
Cong Wang4e58a022013-01-28 19:55:53 +00003791 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003792 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003793 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003794
Cong Wang4e58a022013-01-28 19:55:53 +00003795 pn->net = net;
3796 INIT_LIST_HEAD(&pn->pktgen_threads);
3797 pn->pktgen_exiting = false;
3798 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3799 if (!pn->proc_dir) {
3800 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003801 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003802 }
Cong Wang4e58a022013-01-28 19:55:53 +00003803 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3804 if (pe == NULL) {
3805 pr_err("cannot create %s procfs entry\n", PGCTRL);
3806 ret = -EINVAL;
3807 goto remove;
3808 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003809
John Hawkes670c02c2005-10-13 09:30:31 -07003810 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003811 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Cong Wang4e58a022013-01-28 19:55:53 +00003813 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003814 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003815 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003816 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003817 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003818
Cong Wang4e58a022013-01-28 19:55:53 +00003819 if (list_empty(&pn->pktgen_threads)) {
3820 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003821 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003822 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003823 }
3824
Luiz Capitulino222f1802006-03-20 22:16:13 -08003825 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003826
Cong Wang4e58a022013-01-28 19:55:53 +00003827remove_entry:
3828 remove_proc_entry(PGCTRL, pn->proc_dir);
3829remove:
Gao fengece31ff2013-02-18 01:34:56 +00003830 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003831 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832}
3833
Cong Wang4e58a022013-01-28 19:55:53 +00003834static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
Cong Wang4e58a022013-01-28 19:55:53 +00003836 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003837 struct pktgen_thread *t;
3838 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003839 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Luiz Capitulino222f1802006-03-20 22:16:13 -08003841 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003842 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Eric Dumazetc57b5462012-05-09 13:29:51 +00003844 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003845 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003846 mutex_unlock(&pktgen_thread_lock);
3847
3848 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003849 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003850 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003851 kthread_stop(t->tsk);
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003852 put_task_struct(t->tsk);
David S. Milleree74baa2007-01-01 20:51:53 -08003853 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Cong Wang4e58a022013-01-28 19:55:53 +00003856 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003857 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003858}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Cong Wang4e58a022013-01-28 19:55:53 +00003860static struct pernet_operations pg_net_ops = {
3861 .init = pg_net_init,
3862 .exit = pg_net_exit,
3863 .id = &pg_net_id,
3864 .size = sizeof(struct pktgen_net),
3865};
3866
3867static int __init pg_init(void)
3868{
3869 int ret = 0;
3870
3871 pr_info("%s", version);
3872 ret = register_pernet_subsys(&pg_net_ops);
3873 if (ret)
3874 return ret;
3875 ret = register_netdevice_notifier(&pktgen_notifier_block);
3876 if (ret)
3877 unregister_pernet_subsys(&pg_net_ops);
3878
3879 return ret;
3880}
3881
3882static void __exit pg_cleanup(void)
3883{
3884 unregister_netdevice_notifier(&pktgen_notifier_block);
3885 unregister_pernet_subsys(&pg_net_ops);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003886 /* Don't need rcu_barrier() due to use of kfree_rcu() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887}
3888
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889module_init(pg_init);
3890module_exit(pg_cleanup);
3891
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003892MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893MODULE_DESCRIPTION("Packet Generator tool");
3894MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003895MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003897MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003899MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003901MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003903MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");