blob: 7e4ede34cc52d957a9b449247f0d71b7ef251a6a [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 Perches294a0b7f2014-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 Perches294a0b7f2014-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));
1268 strncpy(pkt_dev->dst_min, buf, len);
1269 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Luiz Capitulino222f1802006-03-20 22:16:13 -08001284 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return -EFAULT;
1286
Luiz Capitulino222f1802006-03-20 22:16:13 -08001287 buf[len] = 0;
1288 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1289 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1290 strncpy(pkt_dev->dst_max, buf, len);
1291 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1292 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1293 }
1294 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001295 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 i += len;
1297 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1298 return count;
1299 }
1300 if (!strcmp(name, "dst6")) {
1301 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001302 if (len < 0)
1303 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 pkt_dev->flags |= F_IPV6;
1306
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001309 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Amerigo Wangc468fb12012-10-09 17:48:20 +00001311 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001312 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001314 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Luiz Capitulino222f1802006-03-20 22:16:13 -08001316 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001317 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sprintf(pg_result, "OK: dst6=%s", buf);
1321 return count;
1322 }
1323 if (!strcmp(name, "dst6_min")) {
1324 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001325 if (len < 0)
1326 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 pkt_dev->flags |= F_IPV6;
1329
Luiz Capitulino222f1802006-03-20 22:16:13 -08001330 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001332 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Amerigo Wangc468fb12012-10-09 17:48:20 +00001334 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001335 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001337 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001339 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Luiz Capitulino222f1802006-03-20 22:16:13 -08001341 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 sprintf(pg_result, "OK: dst6_min=%s", buf);
1343 return count;
1344 }
1345 if (!strcmp(name, "dst6_max")) {
1346 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001347 if (len < 0)
1348 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 pkt_dev->flags |= F_IPV6;
1351
Luiz Capitulino222f1802006-03-20 22:16:13 -08001352 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001354 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Amerigo Wangc468fb12012-10-09 17:48:20 +00001356 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001357 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001360 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Luiz Capitulino222f1802006-03-20 22:16:13 -08001362 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 sprintf(pg_result, "OK: dst6_max=%s", buf);
1364 return count;
1365 }
1366 if (!strcmp(name, "src6")) {
1367 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001368 if (len < 0)
1369 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 pkt_dev->flags |= F_IPV6;
1372
Luiz Capitulino222f1802006-03-20 22:16:13 -08001373 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001375 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Amerigo Wangc468fb12012-10-09 17:48:20 +00001377 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001378 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001380 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Luiz Capitulino222f1802006-03-20 22:16:13 -08001382 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001383 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001384
1385 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 sprintf(pg_result, "OK: src6=%s", buf);
1387 return count;
1388 }
1389 if (!strcmp(name, "src_min")) {
1390 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001391 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001392 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001393
Luiz Capitulino222f1802006-03-20 22:16:13 -08001394 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001396 buf[len] = 0;
1397 if (strcmp(buf, pkt_dev->src_min) != 0) {
1398 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1399 strncpy(pkt_dev->src_min, buf, len);
1400 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1401 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1402 }
1403 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001404 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 i += len;
1406 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1407 return count;
1408 }
1409 if (!strcmp(name, "src_max")) {
1410 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001411 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001412 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001413
Luiz Capitulino222f1802006-03-20 22:16:13 -08001414 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001416 buf[len] = 0;
1417 if (strcmp(buf, pkt_dev->src_max) != 0) {
1418 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1419 strncpy(pkt_dev->src_max, buf, len);
1420 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1421 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1422 }
1423 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001424 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 i += len;
1426 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1427 return count;
1428 }
1429 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001431 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001432 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001435 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001438 if (!mac_pton(valstr, pkt_dev->dst_mac))
1439 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001441 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001442
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001443 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return count;
1445 }
1446 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001448 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001449 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001452 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001455 if (!mac_pton(valstr, pkt_dev->src_mac))
1456 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001457 /* Set up Src MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001458 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001459
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001460 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return count;
1462 }
1463
Luiz Capitulino222f1802006-03-20 22:16:13 -08001464 if (!strcmp(name, "clear_counters")) {
1465 pktgen_clear_counters(pkt_dev);
1466 sprintf(pg_result, "OK: Clearing counters.\n");
1467 return count;
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 if (!strcmp(name, "flows")) {
1471 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001472 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001473 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 i += len;
1476 if (value > MAX_CFLOWS)
1477 value = MAX_CFLOWS;
1478
1479 pkt_dev->cflows = value;
1480 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1481 return count;
1482 }
Fan Du6bae9192014-01-10 14:39:13 +08001483#ifdef CONFIG_XFRM
Fan Dude4aee72014-01-03 11:18:30 +08001484 if (!strcmp(name, "spi")) {
1485 len = num_arg(&user_buffer[i], 10, &value);
1486 if (len < 0)
1487 return len;
1488
1489 i += len;
1490 pkt_dev->spi = value;
1491 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1492 return count;
1493 }
Fan Du6bae9192014-01-10 14:39:13 +08001494#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (!strcmp(name, "flowlen")) {
1496 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001497 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001498 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 i += len;
1501 pkt_dev->lflow = value;
1502 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1503 return count;
1504 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001505
Robert Olsson45b270f2007-08-28 15:45:55 -07001506 if (!strcmp(name, "queue_map_min")) {
1507 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001508 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001509 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001510
Robert Olsson45b270f2007-08-28 15:45:55 -07001511 i += len;
1512 pkt_dev->queue_map_min = value;
1513 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1514 return count;
1515 }
1516
1517 if (!strcmp(name, "queue_map_max")) {
1518 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001519 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001520 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001521
Robert Olsson45b270f2007-08-28 15:45:55 -07001522 i += len;
1523 pkt_dev->queue_map_max = value;
1524 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1525 return count;
1526 }
1527
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001528 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001529 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001530
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001531 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001532 if (len < 0)
1533 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001534 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001535 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001536 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001537 cnt += sprintf(pg_result + cnt,
1538 "%08x%s", ntohl(pkt_dev->labels[n]),
1539 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001540
1541 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1542 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1543 pkt_dev->svlan_id = 0xffff;
1544
1545 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001546 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001547 }
1548 return count;
1549 }
1550
1551 if (!strcmp(name, "vlan_id")) {
1552 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001553 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001554 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001555
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001556 i += len;
1557 if (value <= 4095) {
1558 pkt_dev->vlan_id = value; /* turn on VLAN */
1559
1560 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001561 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001562
1563 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001564 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001565
1566 pkt_dev->nr_labels = 0; /* turn off MPLS */
1567 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1568 } else {
1569 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1570 pkt_dev->svlan_id = 0xffff;
1571
1572 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001573 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001574 }
1575 return count;
1576 }
1577
1578 if (!strcmp(name, "vlan_p")) {
1579 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001580 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001581 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001582
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001583 i += len;
1584 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1585 pkt_dev->vlan_p = value;
1586 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1587 } else {
1588 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1589 }
1590 return count;
1591 }
1592
1593 if (!strcmp(name, "vlan_cfi")) {
1594 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001595 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001596 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001597
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001598 i += len;
1599 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1600 pkt_dev->vlan_cfi = value;
1601 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1602 } else {
1603 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1604 }
1605 return count;
1606 }
1607
1608 if (!strcmp(name, "svlan_id")) {
1609 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001610 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001611 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001612
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001613 i += len;
1614 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1615 pkt_dev->svlan_id = value; /* turn on SVLAN */
1616
1617 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001618 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001619
1620 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001621 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001622
1623 pkt_dev->nr_labels = 0; /* turn off MPLS */
1624 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1625 } else {
1626 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1627 pkt_dev->svlan_id = 0xffff;
1628
1629 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001630 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001631 }
1632 return count;
1633 }
1634
1635 if (!strcmp(name, "svlan_p")) {
1636 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001637 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001638 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001639
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001640 i += len;
1641 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1642 pkt_dev->svlan_p = value;
1643 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1644 } else {
1645 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1646 }
1647 return count;
1648 }
1649
1650 if (!strcmp(name, "svlan_cfi")) {
1651 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001652 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001653 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001654
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001655 i += len;
1656 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1657 pkt_dev->svlan_cfi = value;
1658 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1659 } else {
1660 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1661 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001662 return count;
1663 }
1664
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001665 if (!strcmp(name, "tos")) {
1666 __u32 tmp_value = 0;
1667 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001668 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001669 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001670
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001671 i += len;
1672 if (len == 2) {
1673 pkt_dev->tos = tmp_value;
1674 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1675 } else {
1676 sprintf(pg_result, "ERROR: tos must be 00-ff");
1677 }
1678 return count;
1679 }
1680
1681 if (!strcmp(name, "traffic_class")) {
1682 __u32 tmp_value = 0;
1683 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001684 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001685 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001686
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001687 i += len;
1688 if (len == 2) {
1689 pkt_dev->traffic_class = tmp_value;
1690 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1691 } else {
1692 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1693 }
1694 return count;
1695 }
1696
John Fastabend9e50e3a2010-11-16 19:12:28 +00001697 if (!strcmp(name, "skb_priority")) {
1698 len = num_arg(&user_buffer[i], 9, &value);
1699 if (len < 0)
1700 return len;
1701
1702 i += len;
1703 pkt_dev->skb_priority = value;
1704 sprintf(pg_result, "OK: skb_priority=%i",
1705 pkt_dev->skb_priority);
1706 return count;
1707 }
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1710 return -EINVAL;
1711}
1712
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001713static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Al Virod9dda782013-03-31 18:16:14 -04001715 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001716}
1717
Arjan van de Ven9a321442007-02-12 00:55:35 -08001718static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001719 .open = pktgen_if_open,
1720 .read = seq_read,
1721 .llseek = seq_lseek,
1722 .write = pktgen_if_write,
1723 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001724};
1725
1726static int pktgen_thread_show(struct seq_file *seq, void *v)
1727{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001728 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001729 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001731 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Thomas Graf97dc48e2014-05-16 23:28:54 +02001733 seq_puts(seq, "Running: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001734
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001735 rcu_read_lock();
1736 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001737 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001738 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Thomas Graf97dc48e2014-05-16 23:28:54 +02001740 seq_puts(seq, "\nStopped: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001741
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001742 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001743 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001744 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001747 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 else
Thomas Graf97dc48e2014-05-16 23:28:54 +02001749 seq_puts(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001751 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001756static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 const char __user * user_buffer,
1758 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Joe Perches8a994a72010-07-12 10:50:23 +00001760 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001761 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001762 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001767 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return -EINVAL;
1769 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001770
Paul Gortmakerd6182222010-10-18 12:14:44 +00001771 max = count;
1772 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001774 return len;
1775
Paul Gortmakerd6182222010-10-18 12:14:44 +00001776 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* Read variable name */
1779
1780 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001782 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 memset(name, 0, sizeof(name));
1785 if (copy_from_user(name, &user_buffer[i], len))
1786 return -EFAULT;
1787 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001788
Luiz Capitulino222f1802006-03-20 22:16:13 -08001789 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001792 return len;
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 i += len;
1795
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001796 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001797 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Luiz Capitulino222f1802006-03-20 22:16:13 -08001799 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001800 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 ret = -EINVAL;
1802 goto out;
1803 }
1804
1805 pg_result = &(t->result[0]);
1806
Luiz Capitulino222f1802006-03-20 22:16:13 -08001807 if (!strcmp(name, "add_device")) {
1808 char f[32];
1809 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001811 if (len < 0) {
1812 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto out;
1814 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001815 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return -EFAULT;
1817 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001818 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001819 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001820 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001821 if (!ret) {
1822 ret = count;
1823 sprintf(pg_result, "OK: add_device=%s", f);
1824 } else
1825 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto out;
1827 }
1828
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001830 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001831 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001832 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001833 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001835 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto out;
1837 }
1838
Luiz Capitulino222f1802006-03-20 22:16:13 -08001839 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001840 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001841 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 goto out;
1843 }
1844
1845 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return ret;
1848}
1849
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001850static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Al Virod9dda782013-03-31 18:16:14 -04001852 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Arjan van de Ven9a321442007-02-12 00:55:35 -08001855static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001856 .open = pktgen_thread_open,
1857 .read = seq_read,
1858 .llseek = seq_lseek,
1859 .write = pktgen_thread_write,
1860 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001861};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001864static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1865 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 struct pktgen_thread *t;
1868 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001869 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Cong Wang4e58a022013-01-28 19:55:53 +00001871 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001872 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001874 if (remove) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001875 pkt_dev->removal_mark = 1;
1876 t->control |= T_REMDEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001881 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001884/*
1885 * mark a device for removal
1886 */
Cong Wang4e58a022013-01-28 19:55:53 +00001887static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001890 const int max_tries = 10, msec_per_try = 125;
1891 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001892
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001893 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001894 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001895
Luiz Capitulino222f1802006-03-20 22:16:13 -08001896 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001897
Cong Wang4e58a022013-01-28 19:55:53 +00001898 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001899 if (pkt_dev == NULL)
1900 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001901
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001902 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001903 pr_debug("%s: waiting for %s to disappear....\n",
1904 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001905 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001906 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001907
1908 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001909 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1910 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001911 break;
1912 }
1913
1914 }
1915
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001916 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001917}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001918
Cong Wang4e58a022013-01-28 19:55:53 +00001919static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001920{
1921 struct pktgen_thread *t;
1922
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001923 mutex_lock(&pktgen_thread_lock);
1924
Cong Wang4e58a022013-01-28 19:55:53 +00001925 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001926 struct pktgen_dev *pkt_dev;
1927
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001928 if_lock(t);
1929 list_for_each_entry(pkt_dev, &t->if_list, list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001930 if (pkt_dev->odev != dev)
1931 continue;
1932
David Howellsa8ca16e2013-04-12 17:27:28 +01001933 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001934
Alexey Dobriyan29753152009-08-28 23:34:43 -07001935 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00001936 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07001937 &pktgen_if_fops,
1938 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001939 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001940 pr_err("can't move proc entry for '%s'\n",
1941 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001942 break;
1943 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001944 if_unlock(t);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001945 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001946 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Luiz Capitulino222f1802006-03-20 22:16:13 -08001949static int pktgen_device_event(struct notifier_block *unused,
1950 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Jiri Pirko351638e2013-05-28 01:30:21 +00001952 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00001953 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Cong Wang4e58a022013-01-28 19:55:53 +00001955 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001956 return NOTIFY_DONE;
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /* It is OK that we do not hold the group lock right now,
1959 * as we run under the RTNL lock.
1960 */
1961
1962 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001963 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00001964 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00001968 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 return NOTIFY_DONE;
1973}
1974
Cong Wang4e58a022013-01-28 19:55:53 +00001975static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1976 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001977 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001978{
1979 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001980 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001981
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001982 for (i = 0; ifname[i] != '@'; i++) {
1983 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001984 break;
1985
1986 b[i] = ifname[i];
1987 }
1988 b[i] = 0;
1989
Cong Wang4e58a022013-01-28 19:55:53 +00001990 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07001991}
1992
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994/* Associate pktgen_dev with a device. */
1995
Cong Wang4e58a022013-01-28 19:55:53 +00001996static int pktgen_setup_dev(const struct pktgen_net *pn,
1997 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001998{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002000 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (pkt_dev->odev) {
2004 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002005 pkt_dev->odev = NULL;
2006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Cong Wang4e58a022013-01-28 19:55:53 +00002008 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002010 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002011 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002015 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002016 err = -EINVAL;
2017 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002018 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002019 err = -ENETDOWN;
2020 } else {
2021 pkt_dev->odev = odev;
2022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002026 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
2029/* Read pkt_dev from the interface and set up internal pktgen_dev
2030 * structure to have the right information to create/send packets
2031 */
2032static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2033{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002034 int ntxq;
2035
Luiz Capitulino222f1802006-03-20 22:16:13 -08002036 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002037 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002038 sprintf(pkt_dev->result,
2039 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2040 return;
2041 }
2042
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002043 /* make sure that we don't pick a non-existing transmit queue */
2044 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002045
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002046 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07002047 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2048 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2049 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002050 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002051 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002052 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07002053 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2054 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2055 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002056 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002057 }
2058
Luiz Capitulino222f1802006-03-20 22:16:13 -08002059 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002061 if (is_zero_ether_addr(pkt_dev->src_mac))
Joe Perches9ea08b12014-01-20 09:52:19 -08002062 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Luiz Capitulino222f1802006-03-20 22:16:13 -08002064 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08002065 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Luiz Capitulino222f1802006-03-20 22:16:13 -08002067 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002068 int i, set = 0, err = 1;
2069 struct inet6_dev *idev;
2070
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002071 if (pkt_dev->min_pkt_size == 0) {
2072 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2073 + sizeof(struct udphdr)
2074 + sizeof(struct pktgen_hdr)
2075 + pkt_dev->pkt_overhead;
2076 }
2077
Eric Dumazetdf7e8e22017-11-04 08:27:14 -07002078 for (i = 0; i < sizeof(struct in6_addr); i++)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002079 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 set = 1;
2081 break;
2082 }
2083
Luiz Capitulino222f1802006-03-20 22:16:13 -08002084 if (!set) {
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /*
2087 * Use linklevel address if unconfigured.
2088 *
2089 * use ipv6_get_lladdr if/when it's get exported
2090 */
2091
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002092 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002093 idev = __in6_dev_get(pkt_dev->odev);
2094 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 struct inet6_ifaddr *ifp;
2096
2097 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002098 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2099 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002100 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002101 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 err = 0;
2103 break;
2104 }
2105 }
2106 read_unlock_bh(&idev->lock);
2107 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002108 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002109 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002110 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002112 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002113 if (pkt_dev->min_pkt_size == 0) {
2114 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2115 + sizeof(struct udphdr)
2116 + sizeof(struct pktgen_hdr)
2117 + pkt_dev->pkt_overhead;
2118 }
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 pkt_dev->saddr_min = 0;
2121 pkt_dev->saddr_max = 0;
2122 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002123
2124 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002127 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (in_dev) {
2129 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002130 pkt_dev->saddr_min =
2131 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 pkt_dev->saddr_max = pkt_dev->saddr_min;
2133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002136 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2138 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2139 }
2140
2141 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2142 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2143 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002144 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002145 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2146 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2147 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2148
Luiz Capitulino222f1802006-03-20 22:16:13 -08002149 pkt_dev->cur_dst_mac_offset = 0;
2150 pkt_dev->cur_src_mac_offset = 0;
2151 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2152 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2153 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2154 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 pkt_dev->nflows = 0;
2156}
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002159static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2160{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002161 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002162 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002163 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002164
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002165 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2166 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002167
Daniel Turull43d28b62010-06-09 22:49:57 +00002168 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002169 if (remaining <= 0)
2170 goto out;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002171
Daniel Borkmann398f3822012-10-28 08:27:19 +00002172 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002173 if (remaining < 100000) {
2174 /* for small delays (<100us), just loop until limit is reached */
2175 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002176 end_time = ktime_get();
2177 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002178 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002179 /* see do_nanosleep */
2180 hrtimer_init_sleeper(&t, current);
2181 do {
2182 set_current_state(TASK_INTERRUPTIBLE);
2183 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002184
2185 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002188 hrtimer_cancel(&t.timer);
2189 } while (t.task && pkt_dev->running && !signal_pending(current));
2190 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002191 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002193
2194 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002195out:
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002196 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002197 destroy_hrtimer_on_stack(&t.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002200static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2201{
Paolo Abeni63d75462016-09-30 16:56:45 +02002202 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002203 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2204 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2205 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2206}
2207
Stephen Hemminger648fda72009-08-27 13:55:07 +00002208static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002209{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002210 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002211}
2212
2213static inline int f_pick(struct pktgen_dev *pkt_dev)
2214{
2215 int flow = pkt_dev->curfl;
2216
2217 if (pkt_dev->flags & F_FLOW_SEQ) {
2218 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2219 /* reset time */
2220 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002221 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002222 pkt_dev->curfl += 1;
2223 if (pkt_dev->curfl >= pkt_dev->cflows)
2224 pkt_dev->curfl = 0; /*reset */
2225 }
2226 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002227 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002228 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002229
Robert Olsson1211a642008-08-05 18:44:26 -07002230 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002231 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002232 pkt_dev->flows[flow].flags = 0;
2233 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002234 }
2235
2236 return pkt_dev->curfl;
2237}
2238
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002239
2240#ifdef CONFIG_XFRM
2241/* If there was already an IPSEC SA, we keep it as is, else
2242 * we go look for it ...
2243*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002244#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002245static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002246{
2247 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002248 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002249 if (!x) {
Fan Duc4549972014-01-03 11:18:32 +08002250
2251 if (pkt_dev->spi) {
2252 /* We need as quick as possible to find the right SA
2253 * Searching with minimum criteria to archieve this.
2254 */
2255 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2256 } else {
2257 /* slow path: we dont already have xfrm_state */
2258 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
2259 (xfrm_address_t *)&pkt_dev->cur_daddr,
2260 (xfrm_address_t *)&pkt_dev->cur_saddr,
2261 AF_INET,
2262 pkt_dev->ipsmode,
2263 pkt_dev->ipsproto, 0);
2264 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002265 if (x) {
2266 pkt_dev->flows[flow].x = x;
2267 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002268 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002269 }
2270
2271 }
2272}
2273#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002274static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2275{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002276
2277 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2278 pkt_dev->cur_queue_map = smp_processor_id();
2279
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002280 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002281 __u16 t;
2282 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002283 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002284 (pkt_dev->queue_map_max -
2285 pkt_dev->queue_map_min + 1)
2286 + pkt_dev->queue_map_min;
2287 } else {
2288 t = pkt_dev->cur_queue_map + 1;
2289 if (t > pkt_dev->queue_map_max)
2290 t = pkt_dev->queue_map_min;
2291 }
2292 pkt_dev->cur_queue_map = t;
2293 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002294 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 -07002295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297/* Increment/randomize headers according to flags and current values
2298 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2299 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002300static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2301{
2302 __u32 imn;
2303 __u32 imx;
2304 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002306 if (pkt_dev->cflows)
2307 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002310 if (pkt_dev->src_mac_count > 1) {
2311 __u32 mc;
2312 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Luiz Capitulino222f1802006-03-20 22:16:13 -08002314 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002315 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 else {
2317 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002318 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002319 pkt_dev->src_mac_count)
2320 pkt_dev->cur_src_mac_offset = 0;
2321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2324 pkt_dev->hh[11] = tmp;
2325 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2326 pkt_dev->hh[10] = tmp;
2327 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2328 pkt_dev->hh[9] = tmp;
2329 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2330 pkt_dev->hh[8] = tmp;
2331 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2332 pkt_dev->hh[7] = tmp;
2333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Luiz Capitulino222f1802006-03-20 22:16:13 -08002335 /* Deal with Destination MAC */
2336 if (pkt_dev->dst_mac_count > 1) {
2337 __u32 mc;
2338 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Luiz Capitulino222f1802006-03-20 22:16:13 -08002340 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002341 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Luiz Capitulino222f1802006-03-20 22:16:13 -08002343 else {
2344 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002345 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002346 pkt_dev->dst_mac_count) {
2347 pkt_dev->cur_dst_mac_offset = 0;
2348 }
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Luiz Capitulino222f1802006-03-20 22:16:13 -08002351 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2352 pkt_dev->hh[5] = tmp;
2353 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2354 pkt_dev->hh[4] = tmp;
2355 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2356 pkt_dev->hh[3] = tmp;
2357 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2358 pkt_dev->hh[2] = tmp;
2359 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2360 pkt_dev->hh[1] = tmp;
2361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002363 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002364 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002365 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002366 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2367 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002368 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002369 htonl(0x000fffff));
2370 }
2371
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002372 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002373 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002374 }
2375
2376 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002377 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002378 }
2379
Luiz Capitulino222f1802006-03-20 22:16:13 -08002380 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2381 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002382 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002383 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2384 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Luiz Capitulino222f1802006-03-20 22:16:13 -08002386 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 pkt_dev->cur_udp_src++;
2388 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2389 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002390 }
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Luiz Capitulino222f1802006-03-20 22:16:13 -08002393 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2394 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002395 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002396 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2397 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002398 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002402 }
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 if (!(pkt_dev->flags & F_IPV6)) {
2406
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002407 imn = ntohl(pkt_dev->saddr_min);
2408 imx = ntohl(pkt_dev->saddr_max);
2409 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002411 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002412 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 else {
2414 t = ntohl(pkt_dev->cur_saddr);
2415 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002416 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420 pkt_dev->cur_saddr = htonl(t);
2421 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002422
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002423 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2425 } else {
Al Viro252e33462006-11-14 20:48:11 -08002426 imn = ntohl(pkt_dev->daddr_min);
2427 imx = ntohl(pkt_dev->daddr_max);
2428 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002430 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (pkt_dev->flags & F_IPDST_RND) {
2432
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002433 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002434 t = prandom_u32() %
2435 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002436 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002437 } while (ipv4_is_loopback(s) ||
2438 ipv4_is_multicast(s) ||
2439 ipv4_is_lbcast(s) ||
2440 ipv4_is_zeronet(s) ||
2441 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002442 pkt_dev->cur_daddr = s;
2443 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 t = ntohl(pkt_dev->cur_daddr);
2445 t++;
2446 if (t > imx) {
2447 t = imn;
2448 }
2449 pkt_dev->cur_daddr = htonl(t);
2450 }
2451 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002452 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002453 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002454 pkt_dev->flows[flow].cur_daddr =
2455 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002456#ifdef CONFIG_XFRM
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002457 if (pkt_dev->flags & F_IPSEC)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002458 get_ipsec_sa(pkt_dev, flow);
2459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 pkt_dev->nflows++;
2461 }
2462 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002463 } else { /* IPV6 * */
2464
Joe Perches06e30412012-10-18 17:55:31 +00002465 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 int i;
2467
2468 /* Only random destinations yet */
2469
Luiz Capitulino222f1802006-03-20 22:16:13 -08002470 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002472 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002473 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2474 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
Luiz Capitulino222f1802006-03-20 22:16:13 -08002479 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2480 __u32 t;
2481 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002482 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002483 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2484 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002485 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002487 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002489 }
2490 pkt_dev->cur_pkt_size = t;
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002493 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 pkt_dev->flows[flow].count++;
2496}
2497
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002498
2499#ifdef CONFIG_XFRM
Fengguang Wu5537a052014-01-06 11:00:07 +01002500static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
Fan Ducf93d47e2014-01-03 11:18:31 +08002501
2502 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2503};
2504
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002505static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2506{
2507 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2508 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002509 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002510
2511 if (!x)
2512 return 0;
2513 /* XXX: we dont support tunnel mode for now until
2514 * we resolve the dst issue */
Fan Ducf93d47e2014-01-03 11:18:31 +08002515 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002516 return 0;
2517
Fan Ducf93d47e2014-01-03 11:18:31 +08002518 /* But when user specify an valid SPI, transformation
2519 * supports both transport/tunnel mode + ESP/AH type.
2520 */
2521 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
David Millerb6ca8bd2017-11-28 15:45:44 -05002522 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
Fan Ducf93d47e2014-01-03 11:18:31 +08002523
2524 rcu_read_lock_bh();
Herbert Xu13996372007-10-17 21:35:51 -07002525 err = x->outer_mode->output(x, skb);
Fan Ducf93d47e2014-01-03 11:18:31 +08002526 rcu_read_unlock_bh();
Fan Du6de9ace2014-01-03 11:18:28 +08002527 if (err) {
2528 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002529 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002530 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002531 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002532 if (err) {
2533 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002534 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002535 }
Fan Du0af0a412014-01-03 11:18:27 +08002536 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002537 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002538 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002539 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002540error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002541 return err;
2542}
2543
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002544static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002545{
2546 if (pkt_dev->cflows) {
2547 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002548 int i;
2549 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002550 struct xfrm_state *x = pkt_dev->flows[i].x;
2551 if (x) {
2552 xfrm_state_put(x);
2553 pkt_dev->flows[i].x = NULL;
2554 }
2555 }
2556 }
2557}
2558
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002559static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002560 struct sk_buff *skb, __be16 protocol)
2561{
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002562 if (pkt_dev->flags & F_IPSEC) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002563 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2564 int nhead = 0;
2565 if (x) {
Eric Dumazetd4969582015-05-25 16:06:37 -07002566 struct ethhdr *eth;
fan.du38682042013-12-01 16:28:48 +08002567 struct iphdr *iph;
Eric Dumazetd4969582015-05-25 16:06:37 -07002568 int ret;
fan.du38682042013-12-01 16:28:48 +08002569
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002570 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002571 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002572 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2573 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002574 pr_err("Error expanding ipsec packet %d\n",
2575 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002576 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002577 }
2578 }
2579
2580 /* ipsec is not expecting ll header */
2581 skb_pull(skb, ETH_HLEN);
2582 ret = pktgen_output_ipsec(skb, pkt_dev);
2583 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002584 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002585 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002586 }
2587 /* restore ll */
Johannes Bergd58ff352017-06-16 14:29:23 +02002588 eth = skb_push(skb, ETH_HLEN);
Eric Dumazetd4969582015-05-25 16:06:37 -07002589 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2590 eth->h_proto = protocol;
fan.du38682042013-12-01 16:28:48 +08002591
2592 /* Update IPv4 header len as well as checksum value */
2593 iph = ip_hdr(skb);
2594 iph->tot_len = htons(skb->len - ETH_HLEN);
2595 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002596 }
2597 }
2598 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002599err:
2600 kfree_skb(skb);
2601 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002602}
2603#endif
2604
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002605static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2606{
Eric Dumazet95c96172012-04-15 05:58:06 +00002607 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002608 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002609 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002610
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002611 mpls--;
2612 *mpls |= MPLS_STACK_BOTTOM;
2613}
2614
Al Viro0f37c602006-11-03 03:49:56 -08002615static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2616 unsigned int prio)
2617{
2618 return htons(id | (cfi << 12) | (prio << 13));
2619}
2620
Eric Dumazet26ad7872011-01-25 13:26:05 -08002621static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2622 int datalen)
2623{
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002624 struct timespec64 timestamp;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002625 struct pktgen_hdr *pgh;
2626
Johannes Berg4df864c2017-06-16 14:29:21 +02002627 pgh = skb_put(skb, sizeof(*pgh));
Eric Dumazet26ad7872011-01-25 13:26:05 -08002628 datalen -= sizeof(*pgh);
2629
2630 if (pkt_dev->nfrags <= 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002631 skb_put_zero(skb, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002632 } else {
2633 int frags = pkt_dev->nfrags;
2634 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002635 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002636
2637
2638 if (frags > MAX_SKB_FRAGS)
2639 frags = MAX_SKB_FRAGS;
2640 len = datalen - frags * PAGE_SIZE;
2641 if (len > 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002642 skb_put_zero(skb, len);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002643 datalen = frags * PAGE_SIZE;
2644 }
2645
2646 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002647 frag_len = (datalen/frags) < PAGE_SIZE ?
2648 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002649 while (datalen > 0) {
2650 if (unlikely(!pkt_dev->page)) {
2651 int node = numa_node_id();
2652
2653 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2654 node = pkt_dev->node;
2655 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2656 if (!pkt_dev->page)
2657 break;
2658 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002659 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002660 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002661 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002662 /*last fragment, fill rest of data*/
2663 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002664 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2665 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002666 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002667 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2668 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2669 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2670 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002671 i++;
2672 skb_shinfo(skb)->nr_frags = i;
2673 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002674 }
2675
2676 /* Stamp the time, and sequence number,
2677 * convert them to network byte order
2678 */
2679 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2680 pgh->seq_num = htonl(pkt_dev->seq_num);
2681
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002682 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2683 pgh->tv_sec = 0;
2684 pgh->tv_usec = 0;
2685 } else {
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002686 /*
2687 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2688 * as done by wireshark, or y2038 when interpreted as signed.
2689 * This is probably harmless, but if anyone wants to improve
2690 * it, we could introduce a variant that puts 64-bit nanoseconds
2691 * into the respective header bytes.
2692 * This would also be slightly faster to read.
2693 */
2694 ktime_get_real_ts64(&timestamp);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002695 pgh->tv_sec = htonl(timestamp.tv_sec);
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002696 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002697 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002698}
2699
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002700static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
Paolo Abeni63d75462016-09-30 16:56:45 +02002701 struct pktgen_dev *pkt_dev)
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002702{
Paolo Abeni63d75462016-09-30 16:56:45 +02002703 unsigned int extralen = LL_RESERVED_SPACE(dev);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002704 struct sk_buff *skb = NULL;
Paolo Abeni63d75462016-09-30 16:56:45 +02002705 unsigned int size;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002706
Paolo Abeni63d75462016-09-30 16:56:45 +02002707 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002708 if (pkt_dev->flags & F_NODE) {
2709 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2710
2711 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2712 if (likely(skb)) {
2713 skb_reserve(skb, NET_SKB_PAD);
2714 skb->dev = dev;
2715 }
2716 } else {
2717 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2718 }
John Fastabend3de03592016-01-10 21:38:44 -08002719
Paolo Abeni63d75462016-09-30 16:56:45 +02002720 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
John Fastabend3de03592016-01-10 21:38:44 -08002721 if (likely(skb))
Paolo Abeni63d75462016-09-30 16:56:45 +02002722 skb_reserve(skb, extralen - 16);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002723
2724 return skb;
2725}
2726
Luiz Capitulino222f1802006-03-20 22:16:13 -08002727static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2728 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
2730 struct sk_buff *skb = NULL;
2731 __u8 *eth;
2732 struct udphdr *udph;
2733 int datalen, iplen;
2734 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002735 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002736 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002737 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2738 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2739 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2740 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002741 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002742
2743 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002744 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002745
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002746 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002747 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002748
Robert Olsson64053be2005-06-26 15:27:10 -07002749 /* Update any of the values, used when we're incrementing various
2750 * fields.
2751 */
2752 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002753 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002754
Paolo Abeni63d75462016-09-30 16:56:45 +02002755 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (!skb) {
2757 sprintf(pkt_dev->result, "No memory");
2758 return NULL;
2759 }
2760
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002761 prefetchw(skb->data);
Paolo Abeni63d75462016-09-30 16:56:45 +02002762 skb_reserve(skb, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02002765 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02002766 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002767 if (pkt_dev->nr_labels)
2768 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002769
2770 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002771 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02002772 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002773 *svlan_tci = build_tci(pkt_dev->svlan_id,
2774 pkt_dev->svlan_cfi,
2775 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002776 svlan_encapsulated_proto = skb_put(skb,
2777 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002778 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002779 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002780 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002781 *vlan_tci = build_tci(pkt_dev->vlan_id,
2782 pkt_dev->vlan_cfi,
2783 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002784 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002785 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002786 }
2787
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00002788 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00002789 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002790 iph = skb_put(skb, sizeof(struct iphdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00002791
2792 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002793 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002794 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002795 skb->priority = pkt_dev->skb_priority;
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002798 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002800 /* Eth + IPh + UDPh + mpls */
2801 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002802 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002803 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 udph->source = htons(pkt_dev->cur_udp_src);
2807 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002808 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002809 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 iph->ihl = 5;
2812 iph->version = 4;
2813 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002814 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002815 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 iph->saddr = pkt_dev->cur_saddr;
2817 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002818 iph->id = htons(pkt_dev->ip_id);
2819 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 iph->frag_off = 0;
2821 iplen = 20 + 8 + datalen;
2822 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002823 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002824 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 skb->dev = odev;
2826 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002827
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002828 pktgen_finalize_skb(pkt_dev, skb, datalen);
2829
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002830 if (!(pkt_dev->flags & F_UDPCSUM)) {
2831 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08002832 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002833 skb->ip_summed = CHECKSUM_PARTIAL;
2834 skb->csum = 0;
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002835 udp4_hwcsum(skb, iph->saddr, iph->daddr);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002836 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002837 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002838
2839 /* add protocol-dependent pseudo-header */
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002840 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002841 datalen + 8, IPPROTO_UDP, csum);
2842
2843 if (udph->check == 0)
2844 udph->check = CSUM_MANGLED_0;
2845 }
2846
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002847#ifdef CONFIG_XFRM
2848 if (!process_ipsec(pkt_dev, skb, protocol))
2849 return NULL;
2850#endif
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return skb;
2853}
2854
Luiz Capitulino222f1802006-03-20 22:16:13 -08002855static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2856 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 struct sk_buff *skb = NULL;
2859 __u8 *eth;
2860 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002861 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002863 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002864 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002865 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2866 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2867 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2868 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002869 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002870
2871 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002872 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002873
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002874 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002875 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002876
Robert Olsson64053be2005-06-26 15:27:10 -07002877 /* Update any of the values, used when we're incrementing various
2878 * fields.
2879 */
2880 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002881 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002882
Paolo Abeni63d75462016-09-30 16:56:45 +02002883 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (!skb) {
2885 sprintf(pkt_dev->result, "No memory");
2886 return NULL;
2887 }
2888
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002889 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 skb_reserve(skb, 16);
2891
2892 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02002893 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02002894 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002895 if (pkt_dev->nr_labels)
2896 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002897
2898 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002899 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02002900 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002901 *svlan_tci = build_tci(pkt_dev->svlan_id,
2902 pkt_dev->svlan_cfi,
2903 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002904 svlan_encapsulated_proto = skb_put(skb,
2905 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002906 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002907 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002908 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002909 *vlan_tci = build_tci(pkt_dev->vlan_id,
2910 pkt_dev->vlan_cfi,
2911 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002912 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002913 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002914 }
2915
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00002916 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00002917 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002918 iph = skb_put(skb, sizeof(struct ipv6hdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00002919
2920 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002921 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002922 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002923 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002926 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002927
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002928 /* Eth + IPh + UDPh + mpls */
2929 datalen = pkt_dev->cur_pkt_size - 14 -
2930 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002931 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002933 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002935 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
2937
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002938 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 udph->source = htons(pkt_dev->cur_udp_src);
2940 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002941 udph->len = htons(udplen);
2942 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002944 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002946 if (pkt_dev->traffic_class) {
2947 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002948 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002949 }
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 iph->hop_limit = 32;
2952
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002953 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 iph->nexthdr = IPPROTO_UDP;
2955
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002956 iph->daddr = pkt_dev->cur_in6_daddr;
2957 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002959 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 skb->dev = odev;
2961 skb->pkt_type = PACKET_HOST;
2962
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002963 pktgen_finalize_skb(pkt_dev, skb, datalen);
2964
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002965 if (!(pkt_dev->flags & F_UDPCSUM)) {
2966 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08002967 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002968 skb->ip_summed = CHECKSUM_PARTIAL;
2969 skb->csum_start = skb_transport_header(skb) - skb->head;
2970 skb->csum_offset = offsetof(struct udphdr, check);
2971 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2972 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002973 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002974
2975 /* add protocol-dependent pseudo-header */
2976 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2977
2978 if (udph->check == 0)
2979 udph->check = CSUM_MANGLED_0;
2980 }
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return skb;
2983}
2984
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002985static struct sk_buff *fill_packet(struct net_device *odev,
2986 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002988 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return fill_packet_ipv6(odev, pkt_dev);
2990 else
2991 return fill_packet_ipv4(odev, pkt_dev);
2992}
2993
Luiz Capitulino222f1802006-03-20 22:16:13 -08002994static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002996 pkt_dev->seq_num = 1;
2997 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002999 pkt_dev->tx_bytes = 0;
3000 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
3003/* Set up structure for sending pkts, clear counters */
3004
3005static void pktgen_run(struct pktgen_thread *t)
3006{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003007 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 int started = 0;
3009
Joe Perchesf9467ea2010-06-21 12:29:14 +00003010 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003012 rcu_read_lock();
3013 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 /*
3016 * setup odev and create initial packet.
3017 */
3018 pktgen_setup_inject(pkt_dev);
3019
Luiz Capitulino222f1802006-03-20 22:16:13 -08003020 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 pktgen_clear_counters(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003023 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003024
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003025 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 strcpy(pkt_dev->result, "Starting");
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003028 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003030 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 strcpy(pkt_dev->result, "Error starting");
3032 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003033 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003034 if (started)
3035 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
Cong Wang4e58a022013-01-28 19:55:53 +00003038static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003040 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Joe Perchesf9467ea2010-06-21 12:29:14 +00003042 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003044 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003045
Cong Wang4e58a022013-01-28 19:55:53 +00003046 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003047 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003048
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003049 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Stephen Hemminger648fda72009-08-27 13:55:07 +00003052static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003054 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003056 rcu_read_lock();
3057 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3058 if (pkt_dev->running) {
3059 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003060 return 1;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003061 }
3062 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Luiz Capitulino222f1802006-03-20 22:16:13 -08003066static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003068 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Luiz Capitulino222f1802006-03-20 22:16:13 -08003070 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Luiz Capitulino222f1802006-03-20 22:16:13 -08003072 if (signal_pending(current))
3073 goto signal;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003075 return 1;
3076signal:
3077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
Cong Wang4e58a022013-01-28 19:55:53 +00003080static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003082 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003084
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003085 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003086
Cong Wang4e58a022013-01-28 19:55:53 +00003087 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003089 if (sig == 0)
3090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003092
3093 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003094 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003096
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003097 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return sig;
3099}
3100
Cong Wang4e58a022013-01-28 19:55:53 +00003101static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003103 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Joe Perchesf9467ea2010-06-21 12:29:14 +00003105 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003107 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Cong Wang4e58a022013-01-28 19:55:53 +00003109 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003111
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003112 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003114 /* Propagate thread->control */
3115 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003116
Cong Wang4e58a022013-01-28 19:55:53 +00003117 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Cong Wang4e58a022013-01-28 19:55:53 +00003120static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003121{
3122 struct pktgen_thread *t;
3123
Joe Perchesf9467ea2010-06-21 12:29:14 +00003124 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003125
3126 mutex_lock(&pktgen_thread_lock);
3127
Cong Wang4e58a022013-01-28 19:55:53 +00003128 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003129 t->control |= (T_REMDEVALL);
3130
3131 mutex_unlock(&pktgen_thread_lock);
3132
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003133 /* Propagate thread->control */
3134 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003135
Cong Wang4e58a022013-01-28 19:55:53 +00003136 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003137}
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3140{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003141 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003142 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003143 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3144 pkt_dev->started_at);
3145 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Daniel Turull03a14ab2011-03-09 14:11:00 -08003147 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003148 (unsigned long long)ktime_to_us(elapsed),
3149 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3150 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151 (unsigned long long)pkt_dev->sofar,
3152 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003154 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3155 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Luiz Capitulino222f1802006-03-20 22:16:13 -08003157 bps = pps * 8 * pkt_dev->cur_pkt_size;
3158
3159 mbps = bps;
3160 do_div(mbps, 1000000);
3161 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3162 (unsigned long long)pps,
3163 (unsigned long long)mbps,
3164 (unsigned long long)bps,
3165 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003172
Luiz Capitulino222f1802006-03-20 22:16:13 -08003173 if (!pkt_dev->running) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07003174 pr_warn("interface: %s is already stopped\n",
3175 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003176 return -EINVAL;
3177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003179 pkt_dev->running = 0;
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003180 kfree_skb(pkt_dev->skb);
3181 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003182 pkt_dev->stopped_at = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003184 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Luiz Capitulino222f1802006-03-20 22:16:13 -08003186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
Luiz Capitulino222f1802006-03-20 22:16:13 -08003189static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003191 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003192
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003193 rcu_read_lock();
3194 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003195 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003196 continue;
3197 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003198 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003199 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003200 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003202 rcu_read_unlock();
3203
Luiz Capitulino222f1802006-03-20 22:16:13 -08003204 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Luiz Capitulino222f1802006-03-20 22:16:13 -08003207static void pktgen_stop(struct pktgen_thread *t)
3208{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003209 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Joe Perchesf9467ea2010-06-21 12:29:14 +00003211 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003213 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003215 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003216 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003219 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003222/*
3223 * one of our devices needs to be removed - find it
3224 * and remove it
3225 */
3226static void pktgen_rem_one_if(struct pktgen_thread *t)
3227{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003228 struct list_head *q, *n;
3229 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003230
Joe Perchesf9467ea2010-06-21 12:29:14 +00003231 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003232
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003233 list_for_each_safe(q, n, &t->if_list) {
3234 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003235
Luiz Capitulino222f1802006-03-20 22:16:13 -08003236 if (!cur->removal_mark)
3237 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003238
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003239 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003240 cur->skb = NULL;
3241
3242 pktgen_remove_device(t, cur);
3243
3244 break;
3245 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003246}
3247
Luiz Capitulino222f1802006-03-20 22:16:13 -08003248static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003250 struct list_head *q, *n;
3251 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003252
Joe Perchesf9467ea2010-06-21 12:29:14 +00003253 func_enter();
3254
Luiz Capitulino222f1802006-03-20 22:16:13 -08003255 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003256
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003257 list_for_each_safe(q, n, &t->if_list) {
3258 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003259
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003260 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003261 cur->skb = NULL;
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 pktgen_remove_device(t, cur);
3264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
Luiz Capitulino222f1802006-03-20 22:16:13 -08003267static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003269 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003270 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
3272
Stephen Hemmingeref879792009-09-22 19:41:43 +00003273static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003274{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003275 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003276 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003277 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003278}
3279
Stephen Hemmingeref879792009-09-22 19:41:43 +00003280static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3281{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003282 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003283
Reshetova, Elena63354792017-06-30 13:07:58 +03003284 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003285 if (signal_pending(current))
3286 break;
3287
3288 if (need_resched())
3289 pktgen_resched(pkt_dev);
3290 else
3291 cpu_relax();
3292 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003293 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003294}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003295
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003296static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Mark Rutland6aa7de02017-10-23 14:07:29 -07003298 unsigned int burst = READ_ONCE(pkt_dev->burst);
Stephen Hemminger00829822008-11-20 20:14:53 -08003299 struct net_device *odev = pkt_dev->odev;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003300 struct netdev_queue *txq;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003301 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 int ret;
3303
Stephen Hemmingeref879792009-09-22 19:41:43 +00003304 /* If device is offline, then don't send */
3305 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3306 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003307 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003309
Stephen Hemmingeref879792009-09-22 19:41:43 +00003310 /* This is max DELAY, this has special meaning of
3311 * "never transmit"
3312 */
3313 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003314 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003315 return;
3316 }
3317
3318 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003319 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3320 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3321 /* build a new pkt */
3322 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003323
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003324 pkt_dev->skb = fill_packet(odev, pkt_dev);
3325 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003326 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003327 schedule();
3328 pkt_dev->clone_count--; /* back out increment, OOM */
3329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003331 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003332 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003334
Stephen Hemmingeref879792009-09-22 19:41:43 +00003335 if (pkt_dev->delay && pkt_dev->last_ok)
3336 spin(pkt_dev, pkt_dev->next_tx);
3337
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003338 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3339 skb = pkt_dev->skb;
3340 skb->protocol = eth_type_trans(skb, skb->dev);
Reshetova, Elena63354792017-06-30 13:07:58 +03003341 refcount_add(burst, &skb->users);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003342 local_bh_disable();
3343 do {
3344 ret = netif_receive_skb(skb);
3345 if (ret == NET_RX_DROP)
3346 pkt_dev->errors++;
3347 pkt_dev->sofar++;
3348 pkt_dev->seq_num++;
Reshetova, Elena63354792017-06-30 13:07:58 +03003349 if (refcount_read(&skb->users) != burst) {
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003350 /* skb was queued by rps/rfs or taps,
3351 * so cannot reuse this skb
3352 */
Reshetova, Elena63354792017-06-30 13:07:58 +03003353 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003354 /* get out of the loop and wait
3355 * until skb is consumed
3356 */
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003357 break;
3358 }
3359 /* skb was 'freed' by stack, so clean few
3360 * bits and reuse it
3361 */
Willem de Bruijna5135bc2017-01-07 17:06:36 -05003362 skb_reset_tc(skb);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003363 } while (--burst > 0);
3364 goto out; /* Skips xmit_mode M_START_XMIT */
John Fastabend0967f242016-07-02 14:12:54 -07003365 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3366 local_bh_disable();
Reshetova, Elena63354792017-06-30 13:07:58 +03003367 refcount_inc(&pkt_dev->skb->users);
John Fastabend0967f242016-07-02 14:12:54 -07003368
3369 ret = dev_queue_xmit(pkt_dev->skb);
3370 switch (ret) {
3371 case NET_XMIT_SUCCESS:
3372 pkt_dev->sofar++;
3373 pkt_dev->seq_num++;
3374 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3375 break;
3376 case NET_XMIT_DROP:
3377 case NET_XMIT_CN:
3378 /* These are all valid return codes for a qdisc but
3379 * indicate packets are being dropped or will likely
3380 * be dropped soon.
3381 */
3382 case NETDEV_TX_BUSY:
3383 /* qdisc may call dev_hard_start_xmit directly in cases
3384 * where no queues exist e.g. loopback device, virtual
3385 * devices, etc. In this case we need to handle
3386 * NETDEV_TX_ codes.
3387 */
3388 default:
3389 pkt_dev->errors++;
3390 net_info_ratelimited("%s xmit error: %d\n",
3391 pkt_dev->odevname, ret);
3392 break;
3393 }
3394 goto out;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003395 }
3396
Daniel Borkmann10c51b56232014-08-27 11:11:27 +02003397 txq = skb_get_tx_queue(odev, pkt_dev->skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003398
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003399 local_bh_disable();
3400
3401 HARD_TX_LOCK(odev, txq, smp_processor_id());
Stephen Hemmingeref879792009-09-22 19:41:43 +00003402
Daniel Borkmann6f25cd42014-04-07 17:18:30 +02003403 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003404 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003405 pkt_dev->last_ok = 0;
3406 goto unlock;
3407 }
Reshetova, Elena63354792017-06-30 13:07:58 +03003408 refcount_add(burst, &pkt_dev->skb->users);
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003409
3410xmit_more:
3411 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Stephen Hemmingeref879792009-09-22 19:41:43 +00003413 switch (ret) {
3414 case NETDEV_TX_OK:
Stephen Hemmingeref879792009-09-22 19:41:43 +00003415 pkt_dev->last_ok = 1;
3416 pkt_dev->sofar++;
3417 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003418 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003419 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3420 goto xmit_more;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003421 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003422 case NET_XMIT_DROP:
3423 case NET_XMIT_CN:
John Fastabendf466dba2009-12-23 22:02:57 -08003424 /* skb has been consumed */
3425 pkt_dev->errors++;
3426 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003427 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003428 net_info_ratelimited("%s xmit error: %d\n",
3429 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003430 pkt_dev->errors++;
3431 /* fallthru */
Stephen Hemmingeref879792009-09-22 19:41:43 +00003432 case NETDEV_TX_BUSY:
3433 /* Retry it next time */
Reshetova, Elena63354792017-06-30 13:07:58 +03003434 refcount_dec(&(pkt_dev->skb->users));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003435 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003436 }
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003437 if (unlikely(burst))
Reshetova, Elena63354792017-06-30 13:07:58 +03003438 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
Eric Dumazet0835acf2009-09-30 13:03:33 +00003439unlock:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003440 HARD_TX_UNLOCK(odev, txq);
3441
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003442out:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003443 local_bh_enable();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 /* If pkt_dev->count is zero, then run forever */
3446 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003447 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 /* Done with this */
3450 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003451 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 * Main loop of the thread goes here
3456 */
3457
David S. Milleree74baa2007-01-01 20:51:53 -08003458static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
3460 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003461 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003462 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
David S. Milleree74baa2007-01-01 20:51:53 -08003465 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003468 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Joe Perchesf9467ea2010-06-21 12:29:14 +00003470 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003472 set_freezable();
3473
David S. Milleree74baa2007-01-01 20:51:53 -08003474 while (!kthread_should_stop()) {
3475 pkt_dev = next_to_run(t);
3476
Stephen Hemmingeref879792009-09-22 19:41:43 +00003477 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003478 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003479 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003480 wait_event_interruptible_timeout(t->queue,
3481 t->control != 0,
3482 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003483 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003484 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003485 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003486
Stephen Hemmingeref879792009-09-22 19:41:43 +00003487 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 pktgen_xmit(pkt_dev);
3489
Stephen Hemmingeref879792009-09-22 19:41:43 +00003490 if (need_resched())
3491 pktgen_resched(pkt_dev);
3492 else
3493 cpu_relax();
3494 }
3495
Luiz Capitulino222f1802006-03-20 22:16:13 -08003496 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 pktgen_stop(t);
3498 t->control &= ~(T_STOP);
3499 }
3500
Luiz Capitulino222f1802006-03-20 22:16:13 -08003501 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 pktgen_run(t);
3503 t->control &= ~(T_RUN);
3504 }
3505
Luiz Capitulino222f1802006-03-20 22:16:13 -08003506 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003508 t->control &= ~(T_REMDEVALL);
3509 }
3510
Luiz Capitulino222f1802006-03-20 22:16:13 -08003511 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003512 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 t->control &= ~(T_REMDEV);
3514 }
3515
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003516 try_to_freeze();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Joe Perchesf9467ea2010-06-21 12:29:14 +00003519 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Joe Perchesf9467ea2010-06-21 12:29:14 +00003522 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003523 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Joe Perchesf9467ea2010-06-21 12:29:14 +00003525 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003526 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003527
David S. Milleree74baa2007-01-01 20:51:53 -08003528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529}
3530
Luiz Capitulino222f1802006-03-20 22:16:13 -08003531static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003532 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003534 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003535 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003537 rcu_read_lock();
3538 list_for_each_entry_rcu(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003539 if (strncmp(p->odevname, ifname, len) == 0) {
3540 if (p->odevname[len]) {
3541 if (exact || p->odevname[len] != '@')
3542 continue;
3543 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003544 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003545 break;
3546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003548 rcu_read_unlock();
Joe Perchesf9467ea2010-06-21 12:29:14 +00003549 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003550 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551}
3552
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003553/*
3554 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 */
3556
Luiz Capitulino222f1802006-03-20 22:16:13 -08003557static int add_dev_to_thread(struct pktgen_thread *t,
3558 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
3560 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003562 /* This function cannot be called concurrently, as its called
3563 * under pktgen_thread_lock mutex, but it can run from
3564 * userspace on another CPU than the kthread. The if_lock()
3565 * is used here to sync with concurrent instances of
3566 * _rem_dev_from_if_list() invoked via kthread, which is also
3567 * updating the if_list */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003568 if_lock(t);
3569
3570 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003571 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003572 rv = -EBUSY;
3573 goto out;
3574 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003575
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 pkt_dev->running = 0;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003577 pkt_dev->pg_thread = t;
3578 list_add_rcu(&pkt_dev->list, &t->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Luiz Capitulino222f1802006-03-20 22:16:13 -08003580out:
3581 if_unlock(t);
3582 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
3585/* Called under thread lock */
3586
Luiz Capitulino222f1802006-03-20 22:16:13 -08003587static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003589 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003590 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003591 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003592
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 /* We don't allow a device to be on several threads */
3594
Cong Wang4e58a022013-01-28 19:55:53 +00003595 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003596 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003597 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003598 return -EBUSY;
3599 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003600
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003601 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003602 if (!pkt_dev)
3603 return -ENOMEM;
3604
Eric Dumazet593f63b2009-11-23 01:44:37 +00003605 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003606 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003607 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003608 if (pkt_dev->flows == NULL) {
3609 kfree(pkt_dev);
3610 return -ENOMEM;
3611 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003612
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003613 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003614 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003615 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003616 pkt_dev->count = pg_count_d;
3617 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003618 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003619 pkt_dev->udp_src_max = 9;
3620 pkt_dev->udp_dst_min = 9;
3621 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003622 pkt_dev->vlan_p = 0;
3623 pkt_dev->vlan_cfi = 0;
3624 pkt_dev->vlan_id = 0xffff;
3625 pkt_dev->svlan_p = 0;
3626 pkt_dev->svlan_cfi = 0;
3627 pkt_dev->svlan_id = 0xffff;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003628 pkt_dev->burst = 1;
Robert Olssone99b99b2010-03-18 22:44:30 +00003629 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003630
Cong Wang4e58a022013-01-28 19:55:53 +00003631 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003632 if (err)
3633 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003634 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3635 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003636
Cong Wang4e58a022013-01-28 19:55:53 +00003637 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003638 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003639 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003640 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003641 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003642 err = -EINVAL;
3643 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003644 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003645#ifdef CONFIG_XFRM
3646 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3647 pkt_dev->ipsproto = IPPROTO_ESP;
Fan Ducf93d47e2014-01-03 11:18:31 +08003648
3649 /* xfrm tunnel mode needs additional dst to extract outter
3650 * ip header protocol/ttl/id field, here creat a phony one.
3651 * instead of looking for a valid rt, which definitely hurting
3652 * performance under such circumstance.
3653 */
3654 pkt_dev->dstops.family = AF_INET;
David Millerb6ca8bd2017-11-28 15:45:44 -05003655 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3656 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3657 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3658 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003659#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003660
3661 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003662out2:
3663 dev_put(pkt_dev->odev);
3664out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003665#ifdef CONFIG_XFRM
3666 free_SAs(pkt_dev);
3667#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003668 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003669 kfree(pkt_dev);
3670 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
Cong Wang4e58a022013-01-28 19:55:53 +00003673static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003675 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003676 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003677 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003679 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3680 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003681 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003682 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003683 return -ENOMEM;
3684 }
3685
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02003686 mutex_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003688
David S. Milleree74baa2007-01-01 20:51:53 -08003689 INIT_LIST_HEAD(&t->if_list);
3690
Cong Wang4e58a022013-01-28 19:55:53 +00003691 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003692 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003693
Eric Dumazet94dcf292011-03-22 16:30:45 -07003694 p = kthread_create_on_node(pktgen_thread_worker,
3695 t,
3696 cpu_to_node(cpu),
3697 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003698 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003699 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003700 list_del(&t->th_list);
3701 kfree(t);
3702 return PTR_ERR(p);
3703 }
3704 kthread_bind(p, cpu);
3705 t->tsk = p;
3706
Cong Wang4e58a022013-01-28 19:55:53 +00003707 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003708 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003709 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003710 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003711 PG_PROC_DIR, t->tsk->comm);
3712 kthread_stop(p);
3713 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003714 kfree(t);
3715 return -EINVAL;
3716 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003717
Cong Wang4e58a022013-01-28 19:55:53 +00003718 t->net = pn;
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003719 get_task_struct(p);
David S. Milleree74baa2007-01-01 20:51:53 -08003720 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003721 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
3723 return 0;
3724}
3725
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003726/*
3727 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003729static void _rem_dev_from_if_list(struct pktgen_thread *t,
3730 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003732 struct list_head *q, *n;
3733 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003735 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003736 list_for_each_safe(q, n, &t->if_list) {
3737 p = list_entry(q, struct pktgen_dev, list);
3738 if (p == pkt_dev)
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003739 list_del_rcu(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003741 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
Luiz Capitulino222f1802006-03-20 22:16:13 -08003744static int pktgen_remove_device(struct pktgen_thread *t,
3745 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003747 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Luiz Capitulino222f1802006-03-20 22:16:13 -08003749 if (pkt_dev->running) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07003750 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003751 pktgen_stop_device(pkt_dev);
3752 }
3753
3754 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
3756 if (pkt_dev->odev) {
3757 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003758 pkt_dev->odev = NULL;
3759 }
3760
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003761 /* Remove proc before if_list entry, because add_device uses
3762 * list to determine if interface already exist, avoid race
3763 * with proc_create_data() */
Markus Elfringef87c5d2014-11-18 20:10:34 +01003764 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003766 /* And update the thread if_list */
3767 _rem_dev_from_if_list(t, pkt_dev);
3768
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003769#ifdef CONFIG_XFRM
3770 free_SAs(pkt_dev);
3771#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003772 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003773 if (pkt_dev->page)
3774 put_page(pkt_dev->page);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003775 kfree_rcu(pkt_dev, rcu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777}
3778
Cong Wang4e58a022013-01-28 19:55:53 +00003779static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
Cong Wang4e58a022013-01-28 19:55:53 +00003781 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003782 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003783 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003784
Cong Wang4e58a022013-01-28 19:55:53 +00003785 pn->net = net;
3786 INIT_LIST_HEAD(&pn->pktgen_threads);
3787 pn->pktgen_exiting = false;
3788 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3789 if (!pn->proc_dir) {
3790 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003791 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003792 }
Cong Wang4e58a022013-01-28 19:55:53 +00003793 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3794 if (pe == NULL) {
3795 pr_err("cannot create %s procfs entry\n", PGCTRL);
3796 ret = -EINVAL;
3797 goto remove;
3798 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003799
John Hawkes670c02c2005-10-13 09:30:31 -07003800 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003801 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Cong Wang4e58a022013-01-28 19:55:53 +00003803 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003804 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003805 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003806 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003807 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003808
Cong Wang4e58a022013-01-28 19:55:53 +00003809 if (list_empty(&pn->pktgen_threads)) {
3810 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003811 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003812 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003813 }
3814
Luiz Capitulino222f1802006-03-20 22:16:13 -08003815 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003816
Cong Wang4e58a022013-01-28 19:55:53 +00003817remove_entry:
3818 remove_proc_entry(PGCTRL, pn->proc_dir);
3819remove:
Gao fengece31ff2013-02-18 01:34:56 +00003820 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003821 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
Cong Wang4e58a022013-01-28 19:55:53 +00003824static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
Cong Wang4e58a022013-01-28 19:55:53 +00003826 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003827 struct pktgen_thread *t;
3828 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003829 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Luiz Capitulino222f1802006-03-20 22:16:13 -08003831 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003832 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Eric Dumazetc57b5462012-05-09 13:29:51 +00003834 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003835 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003836 mutex_unlock(&pktgen_thread_lock);
3837
3838 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003839 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003840 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003841 kthread_stop(t->tsk);
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003842 put_task_struct(t->tsk);
David S. Milleree74baa2007-01-01 20:51:53 -08003843 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Cong Wang4e58a022013-01-28 19:55:53 +00003846 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003847 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Cong Wang4e58a022013-01-28 19:55:53 +00003850static struct pernet_operations pg_net_ops = {
3851 .init = pg_net_init,
3852 .exit = pg_net_exit,
3853 .id = &pg_net_id,
3854 .size = sizeof(struct pktgen_net),
3855};
3856
3857static int __init pg_init(void)
3858{
3859 int ret = 0;
3860
3861 pr_info("%s", version);
3862 ret = register_pernet_subsys(&pg_net_ops);
3863 if (ret)
3864 return ret;
3865 ret = register_netdevice_notifier(&pktgen_notifier_block);
3866 if (ret)
3867 unregister_pernet_subsys(&pg_net_ops);
3868
3869 return ret;
3870}
3871
3872static void __exit pg_cleanup(void)
3873{
3874 unregister_netdevice_notifier(&pktgen_notifier_block);
3875 unregister_pernet_subsys(&pg_net_ops);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003876 /* Don't need rcu_barrier() due to use of kfree_rcu() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877}
3878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879module_init(pg_init);
3880module_exit(pg_cleanup);
3881
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003882MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883MODULE_DESCRIPTION("Packet Generator tool");
3884MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003885MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003887MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003889MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003891MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003893MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");