blob: 17fb036d729751f15311077f3602109c73e9624f [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
Luiz Capitulino222f1802006-03-20 22:16:13 -0800833static ssize_t pktgen_if_write(struct file *file,
834 const char __user * user_buffer, size_t count,
835 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Joe Perches8a994a72010-07-12 10:50:23 +0000837 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800838 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000839 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 char name[16], valstr[32];
841 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800842 char *pg_result = NULL;
843 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800845
846 pg_result = &(pkt_dev->result[0]);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (count < 1) {
Joe Perches294a0b72014-09-09 21:17:30 -0700849 pr_warn("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return -EINVAL;
851 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800852
Paul Gortmakerd6182222010-10-18 12:14:44 +0000853 max = count;
854 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800855 if (tmp < 0) {
Joe Perches294a0b72014-09-09 21:17:30 -0700856 pr_warn("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800857 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000859 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* Read variable name */
862
863 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000864 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800865 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800868 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return -EFAULT;
870 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800871
872 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800874 if (len < 0)
875 return len;
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 i += len;
878
879 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000880 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700881 char tb[copy + 1];
882 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700884 tb[copy] = 0;
Joe Perchesf342cda2012-05-16 17:50:41 +0000885 pr_debug("%s,%lu buffer -:%s:-\n",
886 name, (unsigned long)count, tb);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (!strcmp(name, "min_pkt_size")) {
890 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000891 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800892 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800895 if (value < 14 + 20 + 8)
896 value = 14 + 20 + 8;
897 if (value != pkt_dev->min_pkt_size) {
898 pkt_dev->min_pkt_size = value;
899 pkt_dev->cur_pkt_size = value;
900 }
901 sprintf(pg_result, "OK: min_pkt_size=%u",
902 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return count;
904 }
905
Luiz Capitulino222f1802006-03-20 22:16:13 -0800906 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000908 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800909 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800912 if (value < 14 + 20 + 8)
913 value = 14 + 20 + 8;
914 if (value != pkt_dev->max_pkt_size) {
915 pkt_dev->max_pkt_size = value;
916 pkt_dev->cur_pkt_size = value;
917 }
918 sprintf(pg_result, "OK: max_pkt_size=%u",
919 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return count;
921 }
922
Luiz Capitulino222f1802006-03-20 22:16:13 -0800923 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (!strcmp(name, "pkt_size")) {
926 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000927 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800928 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800931 if (value < 14 + 20 + 8)
932 value = 14 + 20 + 8;
933 if (value != pkt_dev->min_pkt_size) {
934 pkt_dev->min_pkt_size = value;
935 pkt_dev->max_pkt_size = value;
936 pkt_dev->cur_pkt_size = value;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
939 return count;
940 }
941
Luiz Capitulino222f1802006-03-20 22:16:13 -0800942 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000944 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800945 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800948 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 sprintf(pg_result, "OK: debug=%u", debug);
950 return count;
951 }
952
Luiz Capitulino222f1802006-03-20 22:16:13 -0800953 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000955 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 i += len;
959 pkt_dev->nfrags = value;
960 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
961 return count;
962 }
963 if (!strcmp(name, "delay")) {
964 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000965 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800966 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000969 if (value == 0x7FFFFFFF)
970 pkt_dev->delay = ULLONG_MAX;
971 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000972 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000973
974 sprintf(pg_result, "OK: delay=%llu",
975 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return count;
977 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000978 if (!strcmp(name, "rate")) {
979 len = num_arg(&user_buffer[i], 10, &value);
980 if (len < 0)
981 return len;
982
983 i += len;
984 if (!value)
985 return len;
986 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
987 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000988 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000989
990 sprintf(pg_result, "OK: rate=%lu", value);
991 return count;
992 }
993 if (!strcmp(name, "ratep")) {
994 len = num_arg(&user_buffer[i], 10, &value);
995 if (len < 0)
996 return len;
997
998 i += len;
999 if (!value)
1000 return len;
1001 pkt_dev->delay = NSEC_PER_SEC/value;
1002 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001003 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001004
1005 sprintf(pg_result, "OK: rate=%lu", value);
1006 return count;
1007 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001008 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001010 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001011 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001014 if (value != pkt_dev->udp_src_min) {
1015 pkt_dev->udp_src_min = value;
1016 pkt_dev->cur_udp_src = value;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1019 return count;
1020 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001021 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001023 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001024 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001027 if (value != pkt_dev->udp_dst_min) {
1028 pkt_dev->udp_dst_min = value;
1029 pkt_dev->cur_udp_dst = value;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1032 return count;
1033 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001034 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001036 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001037 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001040 if (value != pkt_dev->udp_src_max) {
1041 pkt_dev->udp_src_max = value;
1042 pkt_dev->cur_udp_src = value;
1043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1045 return count;
1046 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001047 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001049 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001050 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001053 if (value != pkt_dev->udp_dst_max) {
1054 pkt_dev->udp_dst_max = value;
1055 pkt_dev->cur_udp_dst = value;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1058 return count;
1059 }
1060 if (!strcmp(name, "clone_skb")) {
1061 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001062 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001063 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001064 if ((value > 0) &&
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001065 ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
1066 !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
Neil Hormand8873312011-07-26 06:05:37 +00001067 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001069 pkt_dev->clone_skb = value;
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1072 return count;
1073 }
1074 if (!strcmp(name, "count")) {
1075 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001076 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001077 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 i += len;
1080 pkt_dev->count = value;
1081 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001082 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return count;
1084 }
1085 if (!strcmp(name, "src_mac_count")) {
1086 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001087 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001088 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 i += len;
1091 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001092 pkt_dev->src_mac_count = value;
1093 pkt_dev->cur_src_mac_offset = 0;
1094 }
1095 sprintf(pg_result, "OK: src_mac_count=%d",
1096 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return count;
1098 }
1099 if (!strcmp(name, "dst_mac_count")) {
1100 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001101 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001102 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 i += len;
1105 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001106 pkt_dev->dst_mac_count = value;
1107 pkt_dev->cur_dst_mac_offset = 0;
1108 }
1109 sprintf(pg_result, "OK: dst_mac_count=%d",
1110 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return count;
1112 }
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07001113 if (!strcmp(name, "burst")) {
1114 len = num_arg(&user_buffer[i], 10, &value);
1115 if (len < 0)
1116 return len;
1117
1118 i += len;
John Fastabend0967f242016-07-02 14:12:54 -07001119 if ((value > 1) &&
1120 ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
1121 ((pkt_dev->xmit_mode == M_START_XMIT) &&
1122 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
Eric Dumazet52d6c8c2015-02-22 17:03:41 -08001123 return -ENOTSUPP;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07001124 pkt_dev->burst = value < 1 ? 1 : value;
1125 sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
1126 return count;
1127 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001128 if (!strcmp(name, "node")) {
1129 len = num_arg(&user_buffer[i], 10, &value);
1130 if (len < 0)
1131 return len;
1132
1133 i += len;
1134
1135 if (node_possible(value)) {
1136 pkt_dev->node = value;
1137 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001138 if (pkt_dev->page) {
1139 put_page(pkt_dev->page);
1140 pkt_dev->page = NULL;
1141 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001142 }
1143 else
1144 sprintf(pg_result, "ERROR: node not possible");
1145 return count;
1146 }
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001147 if (!strcmp(name, "xmit_mode")) {
1148 char f[32];
1149
1150 memset(f, 0, 32);
1151 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1152 if (len < 0)
1153 return len;
1154
1155 if (copy_from_user(f, &user_buffer[i], len))
1156 return -EFAULT;
1157 i += len;
1158
1159 if (strcmp(f, "start_xmit") == 0) {
1160 pkt_dev->xmit_mode = M_START_XMIT;
1161 } else if (strcmp(f, "netif_receive") == 0) {
1162 /* clone_skb set earlier, not supported in this mode */
1163 if (pkt_dev->clone_skb > 0)
1164 return -ENOTSUPP;
1165
1166 pkt_dev->xmit_mode = M_NETIF_RECEIVE;
Alexei Starovoitov9eea9222015-05-11 15:19:48 -07001167
1168 /* make sure new packet is allocated every time
1169 * pktgen_xmit() is called
1170 */
1171 pkt_dev->last_ok = 1;
1172
1173 /* override clone_skb if user passed default value
1174 * at module loading time
1175 */
1176 pkt_dev->clone_skb = 0;
John Fastabend0967f242016-07-02 14:12:54 -07001177 } else if (strcmp(f, "queue_xmit") == 0) {
1178 pkt_dev->xmit_mode = M_QUEUE_XMIT;
1179 pkt_dev->last_ok = 1;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001180 } else {
1181 sprintf(pg_result,
1182 "xmit_mode -:%s:- unknown\nAvailable modes: %s",
1183 f, "start_xmit, netif_receive\n");
1184 return count;
1185 }
1186 sprintf(pg_result, "OK: xmit_mode=%s", f);
1187 return count;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001190 char f[32];
1191 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001193 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001194 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (copy_from_user(f, &user_buffer[i], len))
1197 return -EFAULT;
1198 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001199 if (strcmp(f, "IPSRC_RND") == 0)
1200 pkt_dev->flags |= F_IPSRC_RND;
1201
1202 else if (strcmp(f, "!IPSRC_RND") == 0)
1203 pkt_dev->flags &= ~F_IPSRC_RND;
1204
1205 else if (strcmp(f, "TXSIZE_RND") == 0)
1206 pkt_dev->flags |= F_TXSIZE_RND;
1207
1208 else if (strcmp(f, "!TXSIZE_RND") == 0)
1209 pkt_dev->flags &= ~F_TXSIZE_RND;
1210
1211 else if (strcmp(f, "IPDST_RND") == 0)
1212 pkt_dev->flags |= F_IPDST_RND;
1213
1214 else if (strcmp(f, "!IPDST_RND") == 0)
1215 pkt_dev->flags &= ~F_IPDST_RND;
1216
1217 else if (strcmp(f, "UDPSRC_RND") == 0)
1218 pkt_dev->flags |= F_UDPSRC_RND;
1219
1220 else if (strcmp(f, "!UDPSRC_RND") == 0)
1221 pkt_dev->flags &= ~F_UDPSRC_RND;
1222
1223 else if (strcmp(f, "UDPDST_RND") == 0)
1224 pkt_dev->flags |= F_UDPDST_RND;
1225
1226 else if (strcmp(f, "!UDPDST_RND") == 0)
1227 pkt_dev->flags &= ~F_UDPDST_RND;
1228
1229 else if (strcmp(f, "MACSRC_RND") == 0)
1230 pkt_dev->flags |= F_MACSRC_RND;
1231
1232 else if (strcmp(f, "!MACSRC_RND") == 0)
1233 pkt_dev->flags &= ~F_MACSRC_RND;
1234
1235 else if (strcmp(f, "MACDST_RND") == 0)
1236 pkt_dev->flags |= F_MACDST_RND;
1237
1238 else if (strcmp(f, "!MACDST_RND") == 0)
1239 pkt_dev->flags &= ~F_MACDST_RND;
1240
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001241 else if (strcmp(f, "MPLS_RND") == 0)
1242 pkt_dev->flags |= F_MPLS_RND;
1243
1244 else if (strcmp(f, "!MPLS_RND") == 0)
1245 pkt_dev->flags &= ~F_MPLS_RND;
1246
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001247 else if (strcmp(f, "VID_RND") == 0)
1248 pkt_dev->flags |= F_VID_RND;
1249
1250 else if (strcmp(f, "!VID_RND") == 0)
1251 pkt_dev->flags &= ~F_VID_RND;
1252
1253 else if (strcmp(f, "SVID_RND") == 0)
1254 pkt_dev->flags |= F_SVID_RND;
1255
1256 else if (strcmp(f, "!SVID_RND") == 0)
1257 pkt_dev->flags &= ~F_SVID_RND;
1258
Dmitry Safonov57a5749b2018-01-18 18:31:34 +00001259 else if (strcmp(f, "FLOW_SEQ") == 0 || strcmp(f, "!FLOW_RND") == 0)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001260 pkt_dev->flags |= F_FLOW_SEQ;
1261
Dmitry Safonov57a5749b2018-01-18 18:31:34 +00001262 else if (strcmp(f, "FLOW_RND") == 0 || strcmp(f, "!FLOW_SEQ") == 0)
1263 pkt_dev->flags &= ~F_FLOW_SEQ;
1264
Robert Olsson45b270f2007-08-28 15:45:55 -07001265 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1266 pkt_dev->flags |= F_QUEUE_MAP_RND;
1267
1268 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1269 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001270
1271 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1272 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1273
1274 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1275 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001276#ifdef CONFIG_XFRM
1277 else if (strcmp(f, "IPSEC") == 0)
Dmitry Safonov6f107c72018-01-18 18:31:35 +00001278 pkt_dev->flags |= F_IPSEC;
Dmitry Safonov57a5749b2018-01-18 18:31:34 +00001279
1280 else if (strcmp(f, "!IPSEC") == 0)
Dmitry Safonov6f107c72018-01-18 18:31:35 +00001281 pkt_dev->flags &= ~F_IPSEC;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001282#endif
1283
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001284 else if (strcmp(f, "!IPV6") == 0)
1285 pkt_dev->flags &= ~F_IPV6;
1286
Robert Olssone99b99b2010-03-18 22:44:30 +00001287 else if (strcmp(f, "NODE_ALLOC") == 0)
1288 pkt_dev->flags |= F_NODE;
1289
1290 else if (strcmp(f, "!NODE_ALLOC") == 0)
1291 pkt_dev->flags &= ~F_NODE;
1292
Thomas Grafc26bf4a2013-07-25 18:12:18 +02001293 else if (strcmp(f, "UDPCSUM") == 0)
1294 pkt_dev->flags |= F_UDPCSUM;
1295
1296 else if (strcmp(f, "!UDPCSUM") == 0)
1297 pkt_dev->flags &= ~F_UDPCSUM;
1298
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02001299 else if (strcmp(f, "NO_TIMESTAMP") == 0)
1300 pkt_dev->flags |= F_NO_TIMESTAMP;
1301
Jesper Dangaard Brouerf1f00d8ff2015-05-07 16:34:51 +02001302 else if (strcmp(f, "!NO_TIMESTAMP") == 0)
1303 pkt_dev->flags &= ~F_NO_TIMESTAMP;
1304
Luiz Capitulino222f1802006-03-20 22:16:13 -08001305 else {
1306 sprintf(pg_result,
1307 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1308 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001309 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001310 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1311 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1312 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02001313 "NO_TIMESTAMP, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001314#ifdef CONFIG_XFRM
1315 "IPSEC, "
1316#endif
1317 "NODE_ALLOC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001318 return count;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1321 return count;
1322 }
1323 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1324 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001325 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001326 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Luiz Capitulino222f1802006-03-20 22:16:13 -08001328 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001330 buf[len] = 0;
1331 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1332 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1333 strncpy(pkt_dev->dst_min, buf, len);
1334 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1335 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1336 }
1337 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001338 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001339 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1341 return count;
1342 }
1343 if (!strcmp(name, "dst_max")) {
1344 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001345 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001346 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Luiz Capitulino222f1802006-03-20 22:16:13 -08001349 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -EFAULT;
1351
Luiz Capitulino222f1802006-03-20 22:16:13 -08001352 buf[len] = 0;
1353 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1354 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1355 strncpy(pkt_dev->dst_max, buf, len);
1356 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1357 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1358 }
1359 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001360 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 i += len;
1362 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1363 return count;
1364 }
1365 if (!strcmp(name, "dst6")) {
1366 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001367 if (len < 0)
1368 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 pkt_dev->flags |= F_IPV6;
1371
Luiz Capitulino222f1802006-03-20 22:16:13 -08001372 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001374 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Amerigo Wangc468fb12012-10-09 17:48:20 +00001376 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001377 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001379 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001382 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Luiz Capitulino222f1802006-03-20 22:16:13 -08001384 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 sprintf(pg_result, "OK: dst6=%s", buf);
1386 return count;
1387 }
1388 if (!strcmp(name, "dst6_min")) {
1389 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001390 if (len < 0)
1391 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 pkt_dev->flags |= F_IPV6;
1394
Luiz Capitulino222f1802006-03-20 22:16:13 -08001395 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001397 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Amerigo Wangc468fb12012-10-09 17:48:20 +00001399 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001400 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001402 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001403 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001404 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Luiz Capitulino222f1802006-03-20 22:16:13 -08001406 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 sprintf(pg_result, "OK: dst6_min=%s", buf);
1408 return count;
1409 }
1410 if (!strcmp(name, "dst6_max")) {
1411 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001412 if (len < 0)
1413 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 pkt_dev->flags |= F_IPV6;
1416
Luiz Capitulino222f1802006-03-20 22:16:13 -08001417 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001419 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Amerigo Wangc468fb12012-10-09 17:48:20 +00001421 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001422 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Luiz Capitulino222f1802006-03-20 22:16:13 -08001424 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001425 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 sprintf(pg_result, "OK: dst6_max=%s", buf);
1429 return count;
1430 }
1431 if (!strcmp(name, "src6")) {
1432 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001433 if (len < 0)
1434 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 pkt_dev->flags |= F_IPV6;
1437
Luiz Capitulino222f1802006-03-20 22:16:13 -08001438 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001440 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Amerigo Wangc468fb12012-10-09 17:48:20 +00001442 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001443 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001445 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Luiz Capitulino222f1802006-03-20 22:16:13 -08001447 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001448 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001449
1450 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 sprintf(pg_result, "OK: src6=%s", buf);
1452 return count;
1453 }
1454 if (!strcmp(name, "src_min")) {
1455 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001456 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001457 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001458
Luiz Capitulino222f1802006-03-20 22:16:13 -08001459 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001461 buf[len] = 0;
1462 if (strcmp(buf, pkt_dev->src_min) != 0) {
1463 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1464 strncpy(pkt_dev->src_min, buf, len);
1465 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1466 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1467 }
1468 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001469 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 i += len;
1471 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1472 return count;
1473 }
1474 if (!strcmp(name, "src_max")) {
1475 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001476 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001477 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001478
Luiz Capitulino222f1802006-03-20 22:16:13 -08001479 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001481 buf[len] = 0;
1482 if (strcmp(buf, pkt_dev->src_max) != 0) {
1483 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1484 strncpy(pkt_dev->src_max, buf, len);
1485 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1486 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1487 }
1488 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001489 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 i += len;
1491 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1492 return count;
1493 }
1494 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001496 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001497 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001500 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001503 if (!mac_pton(valstr, pkt_dev->dst_mac))
1504 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001506 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001507
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001508 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return count;
1510 }
1511 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001513 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001514 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001517 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001520 if (!mac_pton(valstr, pkt_dev->src_mac))
1521 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001522 /* Set up Src MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001523 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001524
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001525 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return count;
1527 }
1528
Luiz Capitulino222f1802006-03-20 22:16:13 -08001529 if (!strcmp(name, "clear_counters")) {
1530 pktgen_clear_counters(pkt_dev);
1531 sprintf(pg_result, "OK: Clearing counters.\n");
1532 return count;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (!strcmp(name, "flows")) {
1536 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001537 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001538 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 i += len;
1541 if (value > MAX_CFLOWS)
1542 value = MAX_CFLOWS;
1543
1544 pkt_dev->cflows = value;
1545 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1546 return count;
1547 }
Fan Du6bae9192014-01-10 14:39:13 +08001548#ifdef CONFIG_XFRM
Fan Dude4aee72014-01-03 11:18:30 +08001549 if (!strcmp(name, "spi")) {
1550 len = num_arg(&user_buffer[i], 10, &value);
1551 if (len < 0)
1552 return len;
1553
1554 i += len;
1555 pkt_dev->spi = value;
1556 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1557 return count;
1558 }
Fan Du6bae9192014-01-10 14:39:13 +08001559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (!strcmp(name, "flowlen")) {
1561 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001562 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001563 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 i += len;
1566 pkt_dev->lflow = value;
1567 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1568 return count;
1569 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001570
Robert Olsson45b270f2007-08-28 15:45:55 -07001571 if (!strcmp(name, "queue_map_min")) {
1572 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001573 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001574 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001575
Robert Olsson45b270f2007-08-28 15:45:55 -07001576 i += len;
1577 pkt_dev->queue_map_min = value;
1578 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1579 return count;
1580 }
1581
1582 if (!strcmp(name, "queue_map_max")) {
1583 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001584 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001585 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001586
Robert Olsson45b270f2007-08-28 15:45:55 -07001587 i += len;
1588 pkt_dev->queue_map_max = value;
1589 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1590 return count;
1591 }
1592
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001593 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001594 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001595
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001596 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001597 if (len < 0)
1598 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001599 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001600 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001601 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001602 cnt += sprintf(pg_result + cnt,
1603 "%08x%s", ntohl(pkt_dev->labels[n]),
1604 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001605
1606 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1607 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1608 pkt_dev->svlan_id = 0xffff;
1609
1610 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001611 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001612 }
1613 return count;
1614 }
1615
1616 if (!strcmp(name, "vlan_id")) {
1617 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001618 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001619 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001620
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001621 i += len;
1622 if (value <= 4095) {
1623 pkt_dev->vlan_id = value; /* turn on VLAN */
1624
1625 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001626 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001627
1628 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001629 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001630
1631 pkt_dev->nr_labels = 0; /* turn off MPLS */
1632 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1633 } else {
1634 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1635 pkt_dev->svlan_id = 0xffff;
1636
1637 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001638 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001639 }
1640 return count;
1641 }
1642
1643 if (!strcmp(name, "vlan_p")) {
1644 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001645 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001646 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001647
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001648 i += len;
1649 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1650 pkt_dev->vlan_p = value;
1651 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1652 } else {
1653 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1654 }
1655 return count;
1656 }
1657
1658 if (!strcmp(name, "vlan_cfi")) {
1659 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001660 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001661 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001662
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001663 i += len;
1664 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1665 pkt_dev->vlan_cfi = value;
1666 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1667 } else {
1668 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1669 }
1670 return count;
1671 }
1672
1673 if (!strcmp(name, "svlan_id")) {
1674 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001675 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001676 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001677
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001678 i += len;
1679 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1680 pkt_dev->svlan_id = value; /* turn on SVLAN */
1681
1682 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001683 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001684
1685 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001686 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001687
1688 pkt_dev->nr_labels = 0; /* turn off MPLS */
1689 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1690 } else {
1691 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1692 pkt_dev->svlan_id = 0xffff;
1693
1694 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001695 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001696 }
1697 return count;
1698 }
1699
1700 if (!strcmp(name, "svlan_p")) {
1701 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001702 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001703 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001704
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001705 i += len;
1706 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1707 pkt_dev->svlan_p = value;
1708 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1709 } else {
1710 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1711 }
1712 return count;
1713 }
1714
1715 if (!strcmp(name, "svlan_cfi")) {
1716 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001717 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001718 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001719
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001720 i += len;
1721 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1722 pkt_dev->svlan_cfi = value;
1723 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1724 } else {
1725 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1726 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001727 return count;
1728 }
1729
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001730 if (!strcmp(name, "tos")) {
1731 __u32 tmp_value = 0;
1732 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001733 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001734 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001735
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001736 i += len;
1737 if (len == 2) {
1738 pkt_dev->tos = tmp_value;
1739 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1740 } else {
1741 sprintf(pg_result, "ERROR: tos must be 00-ff");
1742 }
1743 return count;
1744 }
1745
1746 if (!strcmp(name, "traffic_class")) {
1747 __u32 tmp_value = 0;
1748 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001749 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001750 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001751
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001752 i += len;
1753 if (len == 2) {
1754 pkt_dev->traffic_class = tmp_value;
1755 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1756 } else {
1757 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1758 }
1759 return count;
1760 }
1761
John Fastabend9e50e3a2010-11-16 19:12:28 +00001762 if (!strcmp(name, "skb_priority")) {
1763 len = num_arg(&user_buffer[i], 9, &value);
1764 if (len < 0)
1765 return len;
1766
1767 i += len;
1768 pkt_dev->skb_priority = value;
1769 sprintf(pg_result, "OK: skb_priority=%i",
1770 pkt_dev->skb_priority);
1771 return count;
1772 }
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1775 return -EINVAL;
1776}
1777
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001778static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Al Virod9dda782013-03-31 18:16:14 -04001780 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001781}
1782
Arjan van de Ven9a321442007-02-12 00:55:35 -08001783static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001784 .open = pktgen_if_open,
1785 .read = seq_read,
1786 .llseek = seq_lseek,
1787 .write = pktgen_if_write,
1788 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001789};
1790
1791static int pktgen_thread_show(struct seq_file *seq, void *v)
1792{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001793 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001794 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001796 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Thomas Graf97dc48e2014-05-16 23:28:54 +02001798 seq_puts(seq, "Running: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001799
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001800 rcu_read_lock();
1801 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001802 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001803 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Thomas Graf97dc48e2014-05-16 23:28:54 +02001805 seq_puts(seq, "\nStopped: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001806
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001807 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001808 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001809 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001812 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 else
Thomas Graf97dc48e2014-05-16 23:28:54 +02001814 seq_puts(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001816 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001821static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001822 const char __user * user_buffer,
1823 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Joe Perches8a994a72010-07-12 10:50:23 +00001825 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001826 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001827 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001832 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return -EINVAL;
1834 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001835
Paul Gortmakerd6182222010-10-18 12:14:44 +00001836 max = count;
1837 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001838 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001839 return len;
1840
Paul Gortmakerd6182222010-10-18 12:14:44 +00001841 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* Read variable name */
1844
1845 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001847 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 memset(name, 0, sizeof(name));
1850 if (copy_from_user(name, &user_buffer[i], len))
1851 return -EFAULT;
1852 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001853
Luiz Capitulino222f1802006-03-20 22:16:13 -08001854 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001856 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001857 return len;
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 i += len;
1860
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001861 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001862 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Luiz Capitulino222f1802006-03-20 22:16:13 -08001864 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001865 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 ret = -EINVAL;
1867 goto out;
1868 }
1869
1870 pg_result = &(t->result[0]);
1871
Luiz Capitulino222f1802006-03-20 22:16:13 -08001872 if (!strcmp(name, "add_device")) {
1873 char f[32];
1874 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001876 if (len < 0) {
1877 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 goto out;
1879 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001880 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return -EFAULT;
1882 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001883 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001884 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001885 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001886 if (!ret) {
1887 ret = count;
1888 sprintf(pg_result, "OK: add_device=%s", f);
1889 } else
1890 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 goto out;
1892 }
1893
Luiz Capitulino222f1802006-03-20 22:16:13 -08001894 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001895 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001896 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001897 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001898 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001900 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 goto out;
1902 }
1903
Luiz Capitulino222f1802006-03-20 22:16:13 -08001904 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001905 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001906 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 goto out;
1908 }
1909
1910 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001911out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return ret;
1913}
1914
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001915static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Al Virod9dda782013-03-31 18:16:14 -04001917 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Arjan van de Ven9a321442007-02-12 00:55:35 -08001920static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001921 .open = pktgen_thread_open,
1922 .read = seq_read,
1923 .llseek = seq_lseek,
1924 .write = pktgen_thread_write,
1925 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001926};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001929static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1930 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 struct pktgen_thread *t;
1933 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001934 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Cong Wang4e58a022013-01-28 19:55:53 +00001936 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001937 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001939 if (remove) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001940 pkt_dev->removal_mark = 1;
1941 t->control |= T_REMDEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 break;
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001946 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001949/*
1950 * mark a device for removal
1951 */
Cong Wang4e58a022013-01-28 19:55:53 +00001952static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001955 const int max_tries = 10, msec_per_try = 125;
1956 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001957
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001958 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001959 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001960
Luiz Capitulino222f1802006-03-20 22:16:13 -08001961 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001962
Cong Wang4e58a022013-01-28 19:55:53 +00001963 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001964 if (pkt_dev == NULL)
1965 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001966
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001967 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001968 pr_debug("%s: waiting for %s to disappear....\n",
1969 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001970 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001971 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001972
1973 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001974 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1975 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001976 break;
1977 }
1978
1979 }
1980
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001981 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001982}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001983
Cong Wang4e58a022013-01-28 19:55:53 +00001984static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001985{
1986 struct pktgen_thread *t;
1987
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001988 mutex_lock(&pktgen_thread_lock);
1989
Cong Wang4e58a022013-01-28 19:55:53 +00001990 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001991 struct pktgen_dev *pkt_dev;
1992
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001993 if_lock(t);
1994 list_for_each_entry(pkt_dev, &t->if_list, list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001995 if (pkt_dev->odev != dev)
1996 continue;
1997
David Howellsa8ca16e2013-04-12 17:27:28 +01001998 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001999
Alexey Dobriyan29753152009-08-28 23:34:43 -07002000 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00002001 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07002002 &pktgen_if_fops,
2003 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002004 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002005 pr_err("can't move proc entry for '%s'\n",
2006 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002007 break;
2008 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02002009 if_unlock(t);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002010 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02002011 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Luiz Capitulino222f1802006-03-20 22:16:13 -08002014static int pktgen_device_event(struct notifier_block *unused,
2015 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Jiri Pirko351638e2013-05-28 01:30:21 +00002017 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00002018 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Cong Wang4e58a022013-01-28 19:55:53 +00002020 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002021 return NOTIFY_DONE;
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /* It is OK that we do not hold the group lock right now,
2024 * as we run under the RTNL lock.
2025 */
2026
2027 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08002028 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00002029 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00002033 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 return NOTIFY_DONE;
2038}
2039
Cong Wang4e58a022013-01-28 19:55:53 +00002040static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
2041 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002042 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002043{
2044 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00002045 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07002046
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002047 for (i = 0; ifname[i] != '@'; i++) {
2048 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002049 break;
2050
2051 b[i] = ifname[i];
2052 }
2053 b[i] = 0;
2054
Cong Wang4e58a022013-01-28 19:55:53 +00002055 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07002056}
2057
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059/* Associate pktgen_dev with a device. */
2060
Cong Wang4e58a022013-01-28 19:55:53 +00002061static int pktgen_setup_dev(const struct pktgen_net *pn,
2062 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002065 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (pkt_dev->odev) {
2069 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002070 pkt_dev->odev = NULL;
2071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Cong Wang4e58a022013-01-28 19:55:53 +00002073 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002075 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002076 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002080 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002081 err = -EINVAL;
2082 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002083 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002084 err = -ENETDOWN;
2085 } else {
2086 pkt_dev->odev = odev;
2087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002091 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
2094/* Read pkt_dev from the interface and set up internal pktgen_dev
2095 * structure to have the right information to create/send packets
2096 */
2097static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2098{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002099 int ntxq;
2100
Luiz Capitulino222f1802006-03-20 22:16:13 -08002101 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002102 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002103 sprintf(pkt_dev->result,
2104 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2105 return;
2106 }
2107
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002108 /* make sure that we don't pick a non-existing transmit queue */
2109 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002110
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002111 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perches294a0b72014-09-09 21:17:30 -07002112 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2113 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2114 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002115 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002116 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002117 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perches294a0b72014-09-09 21:17:30 -07002118 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2119 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2120 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002121 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002122 }
2123
Luiz Capitulino222f1802006-03-20 22:16:13 -08002124 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002126 if (is_zero_ether_addr(pkt_dev->src_mac))
Joe Perches9ea08b12014-01-20 09:52:19 -08002127 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Luiz Capitulino222f1802006-03-20 22:16:13 -08002129 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08002130 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Luiz Capitulino222f1802006-03-20 22:16:13 -08002132 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002133 int i, set = 0, err = 1;
2134 struct inet6_dev *idev;
2135
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002136 if (pkt_dev->min_pkt_size == 0) {
2137 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2138 + sizeof(struct udphdr)
2139 + sizeof(struct pktgen_hdr)
2140 + pkt_dev->pkt_overhead;
2141 }
2142
Eric Dumazetdf7e8e22017-11-04 08:27:14 -07002143 for (i = 0; i < sizeof(struct in6_addr); i++)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002144 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 set = 1;
2146 break;
2147 }
2148
Luiz Capitulino222f1802006-03-20 22:16:13 -08002149 if (!set) {
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /*
2152 * Use linklevel address if unconfigured.
2153 *
2154 * use ipv6_get_lladdr if/when it's get exported
2155 */
2156
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002157 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002158 idev = __in6_dev_get(pkt_dev->odev);
2159 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 struct inet6_ifaddr *ifp;
2161
2162 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002163 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2164 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002165 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002166 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 err = 0;
2168 break;
2169 }
2170 }
2171 read_unlock_bh(&idev->lock);
2172 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002173 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002174 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002175 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002177 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002178 if (pkt_dev->min_pkt_size == 0) {
2179 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2180 + sizeof(struct udphdr)
2181 + sizeof(struct pktgen_hdr)
2182 + pkt_dev->pkt_overhead;
2183 }
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 pkt_dev->saddr_min = 0;
2186 pkt_dev->saddr_max = 0;
2187 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002188
2189 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002192 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (in_dev) {
2194 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002195 pkt_dev->saddr_min =
2196 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 pkt_dev->saddr_max = pkt_dev->saddr_min;
2198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002201 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2203 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2204 }
2205
2206 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2207 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2208 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002209 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002210 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2211 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2212 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2213
Luiz Capitulino222f1802006-03-20 22:16:13 -08002214 pkt_dev->cur_dst_mac_offset = 0;
2215 pkt_dev->cur_src_mac_offset = 0;
2216 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2217 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2218 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2219 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 pkt_dev->nflows = 0;
2221}
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002224static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2225{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002226 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002227 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002228 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002229
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002230 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2231 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002232
Daniel Turull43d28b62010-06-09 22:49:57 +00002233 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002234 if (remaining <= 0)
2235 goto out;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002236
Daniel Borkmann398f3822012-10-28 08:27:19 +00002237 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002238 if (remaining < 100000) {
2239 /* for small delays (<100us), just loop until limit is reached */
2240 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002241 end_time = ktime_get();
2242 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002243 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002244 /* see do_nanosleep */
2245 hrtimer_init_sleeper(&t, current);
2246 do {
2247 set_current_state(TASK_INTERRUPTIBLE);
2248 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002249
2250 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002253 hrtimer_cancel(&t.timer);
2254 } while (t.task && pkt_dev->running && !signal_pending(current));
2255 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002256 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002258
2259 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002260out:
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002261 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002262 destroy_hrtimer_on_stack(&t.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002265static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2266{
Paolo Abeni63d75462016-09-30 16:56:45 +02002267 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002268 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2269 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2270 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2271}
2272
Stephen Hemminger648fda72009-08-27 13:55:07 +00002273static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002274{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002275 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002276}
2277
2278static inline int f_pick(struct pktgen_dev *pkt_dev)
2279{
2280 int flow = pkt_dev->curfl;
2281
2282 if (pkt_dev->flags & F_FLOW_SEQ) {
2283 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2284 /* reset time */
2285 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002286 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002287 pkt_dev->curfl += 1;
2288 if (pkt_dev->curfl >= pkt_dev->cflows)
2289 pkt_dev->curfl = 0; /*reset */
2290 }
2291 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002292 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002293 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002294
Robert Olsson1211a642008-08-05 18:44:26 -07002295 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002296 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002297 pkt_dev->flows[flow].flags = 0;
2298 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002299 }
2300
2301 return pkt_dev->curfl;
2302}
2303
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002304
2305#ifdef CONFIG_XFRM
2306/* If there was already an IPSEC SA, we keep it as is, else
2307 * we go look for it ...
2308*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002309#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002310static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002311{
2312 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002313 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002314 if (!x) {
Fan Duc4549972014-01-03 11:18:32 +08002315
2316 if (pkt_dev->spi) {
2317 /* We need as quick as possible to find the right SA
2318 * Searching with minimum criteria to archieve this.
2319 */
2320 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2321 } else {
2322 /* slow path: we dont already have xfrm_state */
2323 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
2324 (xfrm_address_t *)&pkt_dev->cur_daddr,
2325 (xfrm_address_t *)&pkt_dev->cur_saddr,
2326 AF_INET,
2327 pkt_dev->ipsmode,
2328 pkt_dev->ipsproto, 0);
2329 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002330 if (x) {
2331 pkt_dev->flows[flow].x = x;
2332 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002333 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002334 }
2335
2336 }
2337}
2338#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002339static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2340{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002341
2342 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2343 pkt_dev->cur_queue_map = smp_processor_id();
2344
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002345 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002346 __u16 t;
2347 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002348 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002349 (pkt_dev->queue_map_max -
2350 pkt_dev->queue_map_min + 1)
2351 + pkt_dev->queue_map_min;
2352 } else {
2353 t = pkt_dev->cur_queue_map + 1;
2354 if (t > pkt_dev->queue_map_max)
2355 t = pkt_dev->queue_map_min;
2356 }
2357 pkt_dev->cur_queue_map = t;
2358 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002359 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 -07002360}
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362/* Increment/randomize headers according to flags and current values
2363 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2364 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002365static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2366{
2367 __u32 imn;
2368 __u32 imx;
2369 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002371 if (pkt_dev->cflows)
2372 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002375 if (pkt_dev->src_mac_count > 1) {
2376 __u32 mc;
2377 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Luiz Capitulino222f1802006-03-20 22:16:13 -08002379 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002380 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002381 else {
2382 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002383 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002384 pkt_dev->src_mac_count)
2385 pkt_dev->cur_src_mac_offset = 0;
2386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Luiz Capitulino222f1802006-03-20 22:16:13 -08002388 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2389 pkt_dev->hh[11] = tmp;
2390 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2391 pkt_dev->hh[10] = tmp;
2392 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2393 pkt_dev->hh[9] = tmp;
2394 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2395 pkt_dev->hh[8] = tmp;
2396 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2397 pkt_dev->hh[7] = tmp;
2398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 /* Deal with Destination MAC */
2401 if (pkt_dev->dst_mac_count > 1) {
2402 __u32 mc;
2403 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Luiz Capitulino222f1802006-03-20 22:16:13 -08002405 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002406 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Luiz Capitulino222f1802006-03-20 22:16:13 -08002408 else {
2409 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002410 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002411 pkt_dev->dst_mac_count) {
2412 pkt_dev->cur_dst_mac_offset = 0;
2413 }
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Luiz Capitulino222f1802006-03-20 22:16:13 -08002416 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2417 pkt_dev->hh[5] = tmp;
2418 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2419 pkt_dev->hh[4] = tmp;
2420 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2421 pkt_dev->hh[3] = tmp;
2422 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2423 pkt_dev->hh[2] = tmp;
2424 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2425 pkt_dev->hh[1] = tmp;
2426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002428 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002429 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002430 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002431 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2432 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002433 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002434 htonl(0x000fffff));
2435 }
2436
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002437 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002438 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002439 }
2440
2441 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002442 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002443 }
2444
Luiz Capitulino222f1802006-03-20 22:16:13 -08002445 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2446 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002447 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002448 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2449 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Luiz Capitulino222f1802006-03-20 22:16:13 -08002451 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 pkt_dev->cur_udp_src++;
2453 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2454 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002455 }
2456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Luiz Capitulino222f1802006-03-20 22:16:13 -08002458 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2459 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002460 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002461 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2462 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002463 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002465 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002467 }
2468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 if (!(pkt_dev->flags & F_IPV6)) {
2471
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002472 imn = ntohl(pkt_dev->saddr_min);
2473 imx = ntohl(pkt_dev->saddr_max);
2474 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002476 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002477 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 else {
2479 t = ntohl(pkt_dev->cur_saddr);
2480 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002481 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485 pkt_dev->cur_saddr = htonl(t);
2486 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002487
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002488 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2490 } else {
Al Viro252e33462006-11-14 20:48:11 -08002491 imn = ntohl(pkt_dev->daddr_min);
2492 imx = ntohl(pkt_dev->daddr_max);
2493 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002495 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (pkt_dev->flags & F_IPDST_RND) {
2497
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002498 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002499 t = prandom_u32() %
2500 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002501 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002502 } while (ipv4_is_loopback(s) ||
2503 ipv4_is_multicast(s) ||
2504 ipv4_is_lbcast(s) ||
2505 ipv4_is_zeronet(s) ||
2506 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002507 pkt_dev->cur_daddr = s;
2508 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 t = ntohl(pkt_dev->cur_daddr);
2510 t++;
2511 if (t > imx) {
2512 t = imn;
2513 }
2514 pkt_dev->cur_daddr = htonl(t);
2515 }
2516 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002517 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002518 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002519 pkt_dev->flows[flow].cur_daddr =
2520 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002521#ifdef CONFIG_XFRM
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002522 if (pkt_dev->flags & F_IPSEC)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002523 get_ipsec_sa(pkt_dev, flow);
2524#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 pkt_dev->nflows++;
2526 }
2527 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002528 } else { /* IPV6 * */
2529
Joe Perches06e30412012-10-18 17:55:31 +00002530 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 int i;
2532
2533 /* Only random destinations yet */
2534
Luiz Capitulino222f1802006-03-20 22:16:13 -08002535 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002537 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002538 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2539 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543
Luiz Capitulino222f1802006-03-20 22:16:13 -08002544 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2545 __u32 t;
2546 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002547 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002548 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2549 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002550 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002552 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002554 }
2555 pkt_dev->cur_pkt_size = t;
2556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002558 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 pkt_dev->flows[flow].count++;
2561}
2562
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002563
2564#ifdef CONFIG_XFRM
Fengguang Wu5537a052014-01-06 11:00:07 +01002565static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
Fan Ducf93d47e2014-01-03 11:18:31 +08002566
2567 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2568};
2569
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002570static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2571{
2572 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2573 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002574 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002575
2576 if (!x)
2577 return 0;
2578 /* XXX: we dont support tunnel mode for now until
2579 * we resolve the dst issue */
Fan Ducf93d47e2014-01-03 11:18:31 +08002580 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002581 return 0;
2582
Fan Ducf93d47e2014-01-03 11:18:31 +08002583 /* But when user specify an valid SPI, transformation
2584 * supports both transport/tunnel mode + ESP/AH type.
2585 */
2586 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
David Millerb6ca8bd2017-11-28 15:45:44 -05002587 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
Fan Ducf93d47e2014-01-03 11:18:31 +08002588
2589 rcu_read_lock_bh();
Herbert Xu13996372007-10-17 21:35:51 -07002590 err = x->outer_mode->output(x, skb);
Fan Ducf93d47e2014-01-03 11:18:31 +08002591 rcu_read_unlock_bh();
Fan Du6de9ace2014-01-03 11:18:28 +08002592 if (err) {
2593 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002594 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002595 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002596 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002597 if (err) {
2598 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002599 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002600 }
Fan Du0af0a412014-01-03 11:18:27 +08002601 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002602 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002603 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002604 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002605error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002606 return err;
2607}
2608
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002609static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002610{
2611 if (pkt_dev->cflows) {
2612 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002613 int i;
2614 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002615 struct xfrm_state *x = pkt_dev->flows[i].x;
2616 if (x) {
2617 xfrm_state_put(x);
2618 pkt_dev->flows[i].x = NULL;
2619 }
2620 }
2621 }
2622}
2623
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002624static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002625 struct sk_buff *skb, __be16 protocol)
2626{
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002627 if (pkt_dev->flags & F_IPSEC) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002628 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2629 int nhead = 0;
2630 if (x) {
Eric Dumazetd4969582015-05-25 16:06:37 -07002631 struct ethhdr *eth;
fan.du38682042013-12-01 16:28:48 +08002632 struct iphdr *iph;
Eric Dumazetd4969582015-05-25 16:06:37 -07002633 int ret;
fan.du38682042013-12-01 16:28:48 +08002634
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002635 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002636 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002637 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2638 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002639 pr_err("Error expanding ipsec packet %d\n",
2640 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002641 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002642 }
2643 }
2644
2645 /* ipsec is not expecting ll header */
2646 skb_pull(skb, ETH_HLEN);
2647 ret = pktgen_output_ipsec(skb, pkt_dev);
2648 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002649 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002650 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002651 }
2652 /* restore ll */
Johannes Bergd58ff352017-06-16 14:29:23 +02002653 eth = skb_push(skb, ETH_HLEN);
Eric Dumazetd4969582015-05-25 16:06:37 -07002654 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2655 eth->h_proto = protocol;
fan.du38682042013-12-01 16:28:48 +08002656
2657 /* Update IPv4 header len as well as checksum value */
2658 iph = ip_hdr(skb);
2659 iph->tot_len = htons(skb->len - ETH_HLEN);
2660 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002661 }
2662 }
2663 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002664err:
2665 kfree_skb(skb);
2666 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002667}
2668#endif
2669
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002670static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2671{
Eric Dumazet95c96172012-04-15 05:58:06 +00002672 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002673 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002674 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002675
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002676 mpls--;
2677 *mpls |= MPLS_STACK_BOTTOM;
2678}
2679
Al Viro0f37c602006-11-03 03:49:56 -08002680static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2681 unsigned int prio)
2682{
2683 return htons(id | (cfi << 12) | (prio << 13));
2684}
2685
Eric Dumazet26ad7872011-01-25 13:26:05 -08002686static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2687 int datalen)
2688{
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002689 struct timespec64 timestamp;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002690 struct pktgen_hdr *pgh;
2691
Johannes Berg4df864c2017-06-16 14:29:21 +02002692 pgh = skb_put(skb, sizeof(*pgh));
Eric Dumazet26ad7872011-01-25 13:26:05 -08002693 datalen -= sizeof(*pgh);
2694
2695 if (pkt_dev->nfrags <= 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002696 skb_put_zero(skb, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002697 } else {
2698 int frags = pkt_dev->nfrags;
2699 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002700 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002701
2702
2703 if (frags > MAX_SKB_FRAGS)
2704 frags = MAX_SKB_FRAGS;
2705 len = datalen - frags * PAGE_SIZE;
2706 if (len > 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002707 skb_put_zero(skb, len);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002708 datalen = frags * PAGE_SIZE;
2709 }
2710
2711 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002712 frag_len = (datalen/frags) < PAGE_SIZE ?
2713 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002714 while (datalen > 0) {
2715 if (unlikely(!pkt_dev->page)) {
2716 int node = numa_node_id();
2717
2718 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2719 node = pkt_dev->node;
2720 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2721 if (!pkt_dev->page)
2722 break;
2723 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002724 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002725 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002726 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002727 /*last fragment, fill rest of data*/
2728 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002729 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2730 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002731 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002732 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2733 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2734 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2735 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002736 i++;
2737 skb_shinfo(skb)->nr_frags = i;
2738 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002739 }
2740
2741 /* Stamp the time, and sequence number,
2742 * convert them to network byte order
2743 */
2744 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2745 pgh->seq_num = htonl(pkt_dev->seq_num);
2746
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002747 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2748 pgh->tv_sec = 0;
2749 pgh->tv_usec = 0;
2750 } else {
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002751 /*
2752 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2753 * as done by wireshark, or y2038 when interpreted as signed.
2754 * This is probably harmless, but if anyone wants to improve
2755 * it, we could introduce a variant that puts 64-bit nanoseconds
2756 * into the respective header bytes.
2757 * This would also be slightly faster to read.
2758 */
2759 ktime_get_real_ts64(&timestamp);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002760 pgh->tv_sec = htonl(timestamp.tv_sec);
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002761 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002762 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002763}
2764
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002765static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
Paolo Abeni63d75462016-09-30 16:56:45 +02002766 struct pktgen_dev *pkt_dev)
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002767{
Paolo Abeni63d75462016-09-30 16:56:45 +02002768 unsigned int extralen = LL_RESERVED_SPACE(dev);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002769 struct sk_buff *skb = NULL;
Paolo Abeni63d75462016-09-30 16:56:45 +02002770 unsigned int size;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002771
Paolo Abeni63d75462016-09-30 16:56:45 +02002772 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002773 if (pkt_dev->flags & F_NODE) {
2774 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2775
2776 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2777 if (likely(skb)) {
2778 skb_reserve(skb, NET_SKB_PAD);
2779 skb->dev = dev;
2780 }
2781 } else {
2782 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2783 }
John Fastabend3de03592016-01-10 21:38:44 -08002784
Paolo Abeni63d75462016-09-30 16:56:45 +02002785 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
John Fastabend3de03592016-01-10 21:38:44 -08002786 if (likely(skb))
Paolo Abeni63d75462016-09-30 16:56:45 +02002787 skb_reserve(skb, extralen - 16);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002788
2789 return skb;
2790}
2791
Luiz Capitulino222f1802006-03-20 22:16:13 -08002792static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2793 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 struct sk_buff *skb = NULL;
2796 __u8 *eth;
2797 struct udphdr *udph;
2798 int datalen, iplen;
2799 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002800 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002801 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002802 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2803 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2804 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2805 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002806 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002807
2808 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002809 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002810
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002811 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002812 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002813
Robert Olsson64053be2005-06-26 15:27:10 -07002814 /* Update any of the values, used when we're incrementing various
2815 * fields.
2816 */
2817 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002818 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002819
Paolo Abeni63d75462016-09-30 16:56:45 +02002820 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (!skb) {
2822 sprintf(pkt_dev->result, "No memory");
2823 return NULL;
2824 }
2825
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002826 prefetchw(skb->data);
Paolo Abeni63d75462016-09-30 16:56:45 +02002827 skb_reserve(skb, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02002830 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02002831 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002832 if (pkt_dev->nr_labels)
2833 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002834
2835 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002836 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02002837 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002838 *svlan_tci = build_tci(pkt_dev->svlan_id,
2839 pkt_dev->svlan_cfi,
2840 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002841 svlan_encapsulated_proto = skb_put(skb,
2842 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002843 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002844 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002845 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002846 *vlan_tci = build_tci(pkt_dev->vlan_id,
2847 pkt_dev->vlan_cfi,
2848 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002849 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002850 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002851 }
2852
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00002853 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00002854 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002855 iph = skb_put(skb, sizeof(struct iphdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00002856
2857 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002858 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002859 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002860 skb->priority = pkt_dev->skb_priority;
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002863 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002865 /* Eth + IPh + UDPh + mpls */
2866 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002867 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002868 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 udph->source = htons(pkt_dev->cur_udp_src);
2872 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002874 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 iph->ihl = 5;
2877 iph->version = 4;
2878 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002879 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002880 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 iph->saddr = pkt_dev->cur_saddr;
2882 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002883 iph->id = htons(pkt_dev->ip_id);
2884 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 iph->frag_off = 0;
2886 iplen = 20 + 8 + datalen;
2887 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002888 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002889 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 skb->dev = odev;
2891 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002892
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002893 pktgen_finalize_skb(pkt_dev, skb, datalen);
2894
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002895 if (!(pkt_dev->flags & F_UDPCSUM)) {
2896 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08002897 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002898 skb->ip_summed = CHECKSUM_PARTIAL;
2899 skb->csum = 0;
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002900 udp4_hwcsum(skb, iph->saddr, iph->daddr);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002901 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002902 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002903
2904 /* add protocol-dependent pseudo-header */
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002905 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002906 datalen + 8, IPPROTO_UDP, csum);
2907
2908 if (udph->check == 0)
2909 udph->check = CSUM_MANGLED_0;
2910 }
2911
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002912#ifdef CONFIG_XFRM
2913 if (!process_ipsec(pkt_dev, skb, protocol))
2914 return NULL;
2915#endif
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return skb;
2918}
2919
Luiz Capitulino222f1802006-03-20 22:16:13 -08002920static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2921 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 struct sk_buff *skb = NULL;
2924 __u8 *eth;
2925 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002926 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002928 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002929 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002930 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2931 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2932 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2933 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002934 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002935
2936 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002937 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002938
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002939 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002940 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002941
Robert Olsson64053be2005-06-26 15:27:10 -07002942 /* Update any of the values, used when we're incrementing various
2943 * fields.
2944 */
2945 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002946 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002947
Paolo Abeni63d75462016-09-30 16:56:45 +02002948 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (!skb) {
2950 sprintf(pkt_dev->result, "No memory");
2951 return NULL;
2952 }
2953
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002954 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 skb_reserve(skb, 16);
2956
2957 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02002958 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02002959 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002960 if (pkt_dev->nr_labels)
2961 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002962
2963 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002964 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02002965 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002966 *svlan_tci = build_tci(pkt_dev->svlan_id,
2967 pkt_dev->svlan_cfi,
2968 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002969 svlan_encapsulated_proto = skb_put(skb,
2970 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002971 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002972 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002973 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002974 *vlan_tci = build_tci(pkt_dev->vlan_id,
2975 pkt_dev->vlan_cfi,
2976 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002977 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002978 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002979 }
2980
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00002981 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00002982 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002983 iph = skb_put(skb, sizeof(struct ipv6hdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00002984
2985 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002986 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002987 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002988 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002991 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002992
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002993 /* Eth + IPh + UDPh + mpls */
2994 datalen = pkt_dev->cur_pkt_size - 14 -
2995 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002996 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002998 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00003000 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003003 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 udph->source = htons(pkt_dev->cur_udp_src);
3005 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003006 udph->len = htons(udplen);
3007 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003009 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003011 if (pkt_dev->traffic_class) {
3012 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08003013 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003014 }
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 iph->hop_limit = 32;
3017
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003018 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 iph->nexthdr = IPPROTO_UDP;
3020
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003021 iph->daddr = pkt_dev->cur_in6_daddr;
3022 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003024 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 skb->dev = odev;
3026 skb->pkt_type = PACKET_HOST;
3027
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01003028 pktgen_finalize_skb(pkt_dev, skb, datalen);
3029
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003030 if (!(pkt_dev->flags & F_UDPCSUM)) {
3031 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08003032 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003033 skb->ip_summed = CHECKSUM_PARTIAL;
3034 skb->csum_start = skb_transport_header(skb) - skb->head;
3035 skb->csum_offset = offsetof(struct udphdr, check);
3036 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
3037 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01003038 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02003039
3040 /* add protocol-dependent pseudo-header */
3041 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
3042
3043 if (udph->check == 0)
3044 udph->check = CSUM_MANGLED_0;
3045 }
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return skb;
3048}
3049
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003050static struct sk_buff *fill_packet(struct net_device *odev,
3051 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003053 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return fill_packet_ipv6(odev, pkt_dev);
3055 else
3056 return fill_packet_ipv4(odev, pkt_dev);
3057}
3058
Luiz Capitulino222f1802006-03-20 22:16:13 -08003059static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003061 pkt_dev->seq_num = 1;
3062 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003064 pkt_dev->tx_bytes = 0;
3065 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
3068/* Set up structure for sending pkts, clear counters */
3069
3070static void pktgen_run(struct pktgen_thread *t)
3071{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003072 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 int started = 0;
3074
Joe Perchesf9467ea2010-06-21 12:29:14 +00003075 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003077 rcu_read_lock();
3078 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 /*
3081 * setup odev and create initial packet.
3082 */
3083 pktgen_setup_inject(pkt_dev);
3084
Luiz Capitulino222f1802006-03-20 22:16:13 -08003085 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 pktgen_clear_counters(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003088 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003089
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003090 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 strcpy(pkt_dev->result, "Starting");
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003093 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003095 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 strcpy(pkt_dev->result, "Error starting");
3097 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003098 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003099 if (started)
3100 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Cong Wang4e58a022013-01-28 19:55:53 +00003103static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003105 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Joe Perchesf9467ea2010-06-21 12:29:14 +00003107 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003109 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003110
Cong Wang4e58a022013-01-28 19:55:53 +00003111 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003112 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003113
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003114 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
Stephen Hemminger648fda72009-08-27 13:55:07 +00003117static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003119 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003121 rcu_read_lock();
3122 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3123 if (pkt_dev->running) {
3124 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003125 return 1;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003126 }
3127 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Luiz Capitulino222f1802006-03-20 22:16:13 -08003131static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003133 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Luiz Capitulino222f1802006-03-20 22:16:13 -08003135 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Luiz Capitulino222f1802006-03-20 22:16:13 -08003137 if (signal_pending(current))
3138 goto signal;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003139 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003140 return 1;
3141signal:
3142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
3144
Cong Wang4e58a022013-01-28 19:55:53 +00003145static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003147 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003149
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003150 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003151
Cong Wang4e58a022013-01-28 19:55:53 +00003152 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154 if (sig == 0)
3155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003157
3158 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003159 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003161
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003162 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 return sig;
3164}
3165
Cong Wang4e58a022013-01-28 19:55:53 +00003166static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003168 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Joe Perchesf9467ea2010-06-21 12:29:14 +00003170 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003172 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Cong Wang4e58a022013-01-28 19:55:53 +00003174 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003176
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003177 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003179 /* Propagate thread->control */
3180 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003181
Cong Wang4e58a022013-01-28 19:55:53 +00003182 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
Cong Wang4e58a022013-01-28 19:55:53 +00003185static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003186{
3187 struct pktgen_thread *t;
3188
Joe Perchesf9467ea2010-06-21 12:29:14 +00003189 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003190
3191 mutex_lock(&pktgen_thread_lock);
3192
Cong Wang4e58a022013-01-28 19:55:53 +00003193 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003194 t->control |= (T_REMDEVALL);
3195
3196 mutex_unlock(&pktgen_thread_lock);
3197
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003198 /* Propagate thread->control */
3199 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003200
Cong Wang4e58a022013-01-28 19:55:53 +00003201 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003202}
3203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3205{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003206 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003207 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003208 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3209 pkt_dev->started_at);
3210 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Daniel Turull03a14ab2011-03-09 14:11:00 -08003212 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003213 (unsigned long long)ktime_to_us(elapsed),
3214 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3215 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003216 (unsigned long long)pkt_dev->sofar,
3217 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003219 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3220 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Luiz Capitulino222f1802006-03-20 22:16:13 -08003222 bps = pps * 8 * pkt_dev->cur_pkt_size;
3223
3224 mbps = bps;
3225 do_div(mbps, 1000000);
3226 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3227 (unsigned long long)pps,
3228 (unsigned long long)mbps,
3229 (unsigned long long)bps,
3230 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
3233/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003234static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003236 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003237
Luiz Capitulino222f1802006-03-20 22:16:13 -08003238 if (!pkt_dev->running) {
Joe Perches294a0b72014-09-09 21:17:30 -07003239 pr_warn("interface: %s is already stopped\n",
3240 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003241 return -EINVAL;
3242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003244 pkt_dev->running = 0;
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003245 kfree_skb(pkt_dev->skb);
3246 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003247 pkt_dev->stopped_at = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003249 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Luiz Capitulino222f1802006-03-20 22:16:13 -08003251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Luiz Capitulino222f1802006-03-20 22:16:13 -08003254static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003256 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003257
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003258 rcu_read_lock();
3259 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003260 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003261 continue;
3262 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003263 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003264 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003265 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003267 rcu_read_unlock();
3268
Luiz Capitulino222f1802006-03-20 22:16:13 -08003269 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Luiz Capitulino222f1802006-03-20 22:16:13 -08003272static void pktgen_stop(struct pktgen_thread *t)
3273{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003274 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Joe Perchesf9467ea2010-06-21 12:29:14 +00003276 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003278 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003280 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003281 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003284 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285}
3286
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003287/*
3288 * one of our devices needs to be removed - find it
3289 * and remove it
3290 */
3291static void pktgen_rem_one_if(struct pktgen_thread *t)
3292{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003293 struct list_head *q, *n;
3294 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003295
Joe Perchesf9467ea2010-06-21 12:29:14 +00003296 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003297
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003298 list_for_each_safe(q, n, &t->if_list) {
3299 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003300
Luiz Capitulino222f1802006-03-20 22:16:13 -08003301 if (!cur->removal_mark)
3302 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003303
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003304 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003305 cur->skb = NULL;
3306
3307 pktgen_remove_device(t, cur);
3308
3309 break;
3310 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003311}
3312
Luiz Capitulino222f1802006-03-20 22:16:13 -08003313static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003315 struct list_head *q, *n;
3316 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003317
Joe Perchesf9467ea2010-06-21 12:29:14 +00003318 func_enter();
3319
Luiz Capitulino222f1802006-03-20 22:16:13 -08003320 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003321
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003322 list_for_each_safe(q, n, &t->if_list) {
3323 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003324
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003325 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003326 cur->skb = NULL;
3327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 pktgen_remove_device(t, cur);
3329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
Luiz Capitulino222f1802006-03-20 22:16:13 -08003332static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003334 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003335 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336}
3337
Stephen Hemmingeref879792009-09-22 19:41:43 +00003338static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003339{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003340 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003341 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003342 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003343}
3344
Stephen Hemmingeref879792009-09-22 19:41:43 +00003345static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3346{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003347 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003348
Reshetova, Elena63354792017-06-30 13:07:58 +03003349 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003350 if (signal_pending(current))
3351 break;
3352
3353 if (need_resched())
3354 pktgen_resched(pkt_dev);
3355 else
3356 cpu_relax();
3357 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003358 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003359}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003360
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003361static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Mark Rutland6aa7de02017-10-23 14:07:29 -07003363 unsigned int burst = READ_ONCE(pkt_dev->burst);
Stephen Hemminger00829822008-11-20 20:14:53 -08003364 struct net_device *odev = pkt_dev->odev;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003365 struct netdev_queue *txq;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003366 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 int ret;
3368
Stephen Hemmingeref879792009-09-22 19:41:43 +00003369 /* If device is offline, then don't send */
3370 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3371 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003372 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003374
Stephen Hemmingeref879792009-09-22 19:41:43 +00003375 /* This is max DELAY, this has special meaning of
3376 * "never transmit"
3377 */
3378 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003379 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003380 return;
3381 }
3382
3383 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003384 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3385 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3386 /* build a new pkt */
3387 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003388
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003389 pkt_dev->skb = fill_packet(odev, pkt_dev);
3390 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003391 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003392 schedule();
3393 pkt_dev->clone_count--; /* back out increment, OOM */
3394 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003396 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003397 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003399
Stephen Hemmingeref879792009-09-22 19:41:43 +00003400 if (pkt_dev->delay && pkt_dev->last_ok)
3401 spin(pkt_dev, pkt_dev->next_tx);
3402
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003403 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3404 skb = pkt_dev->skb;
3405 skb->protocol = eth_type_trans(skb, skb->dev);
Reshetova, Elena63354792017-06-30 13:07:58 +03003406 refcount_add(burst, &skb->users);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003407 local_bh_disable();
3408 do {
3409 ret = netif_receive_skb(skb);
3410 if (ret == NET_RX_DROP)
3411 pkt_dev->errors++;
3412 pkt_dev->sofar++;
3413 pkt_dev->seq_num++;
Reshetova, Elena63354792017-06-30 13:07:58 +03003414 if (refcount_read(&skb->users) != burst) {
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003415 /* skb was queued by rps/rfs or taps,
3416 * so cannot reuse this skb
3417 */
Reshetova, Elena63354792017-06-30 13:07:58 +03003418 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003419 /* get out of the loop and wait
3420 * until skb is consumed
3421 */
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003422 break;
3423 }
3424 /* skb was 'freed' by stack, so clean few
3425 * bits and reuse it
3426 */
Willem de Bruijna5135bc2017-01-07 17:06:36 -05003427 skb_reset_tc(skb);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003428 } while (--burst > 0);
3429 goto out; /* Skips xmit_mode M_START_XMIT */
John Fastabend0967f242016-07-02 14:12:54 -07003430 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3431 local_bh_disable();
Reshetova, Elena63354792017-06-30 13:07:58 +03003432 refcount_inc(&pkt_dev->skb->users);
John Fastabend0967f242016-07-02 14:12:54 -07003433
3434 ret = dev_queue_xmit(pkt_dev->skb);
3435 switch (ret) {
3436 case NET_XMIT_SUCCESS:
3437 pkt_dev->sofar++;
3438 pkt_dev->seq_num++;
3439 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3440 break;
3441 case NET_XMIT_DROP:
3442 case NET_XMIT_CN:
3443 /* These are all valid return codes for a qdisc but
3444 * indicate packets are being dropped or will likely
3445 * be dropped soon.
3446 */
3447 case NETDEV_TX_BUSY:
3448 /* qdisc may call dev_hard_start_xmit directly in cases
3449 * where no queues exist e.g. loopback device, virtual
3450 * devices, etc. In this case we need to handle
3451 * NETDEV_TX_ codes.
3452 */
3453 default:
3454 pkt_dev->errors++;
3455 net_info_ratelimited("%s xmit error: %d\n",
3456 pkt_dev->odevname, ret);
3457 break;
3458 }
3459 goto out;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003460 }
3461
Daniel Borkmann10c51b56232014-08-27 11:11:27 +02003462 txq = skb_get_tx_queue(odev, pkt_dev->skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003463
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003464 local_bh_disable();
3465
3466 HARD_TX_LOCK(odev, txq, smp_processor_id());
Stephen Hemmingeref879792009-09-22 19:41:43 +00003467
Daniel Borkmann6f25cd42014-04-07 17:18:30 +02003468 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003469 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003470 pkt_dev->last_ok = 0;
3471 goto unlock;
3472 }
Reshetova, Elena63354792017-06-30 13:07:58 +03003473 refcount_add(burst, &pkt_dev->skb->users);
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003474
3475xmit_more:
3476 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
Stephen Hemmingeref879792009-09-22 19:41:43 +00003478 switch (ret) {
3479 case NETDEV_TX_OK:
Stephen Hemmingeref879792009-09-22 19:41:43 +00003480 pkt_dev->last_ok = 1;
3481 pkt_dev->sofar++;
3482 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003483 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003484 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3485 goto xmit_more;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003486 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003487 case NET_XMIT_DROP:
3488 case NET_XMIT_CN:
John Fastabendf466dba2009-12-23 22:02:57 -08003489 /* skb has been consumed */
3490 pkt_dev->errors++;
3491 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003492 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003493 net_info_ratelimited("%s xmit error: %d\n",
3494 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003495 pkt_dev->errors++;
3496 /* fallthru */
Stephen Hemmingeref879792009-09-22 19:41:43 +00003497 case NETDEV_TX_BUSY:
3498 /* Retry it next time */
Reshetova, Elena63354792017-06-30 13:07:58 +03003499 refcount_dec(&(pkt_dev->skb->users));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003500 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003501 }
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003502 if (unlikely(burst))
Reshetova, Elena63354792017-06-30 13:07:58 +03003503 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
Eric Dumazet0835acf2009-09-30 13:03:33 +00003504unlock:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003505 HARD_TX_UNLOCK(odev, txq);
3506
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003507out:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003508 local_bh_enable();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 /* If pkt_dev->count is zero, then run forever */
3511 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003512 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 /* Done with this */
3515 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003516 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003519/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 * Main loop of the thread goes here
3521 */
3522
David S. Milleree74baa2007-01-01 20:51:53 -08003523static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524{
3525 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003526 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003527 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
David S. Milleree74baa2007-01-01 20:51:53 -08003530 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003533 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Joe Perchesf9467ea2010-06-21 12:29:14 +00003535 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003537 set_freezable();
3538
David S. Milleree74baa2007-01-01 20:51:53 -08003539 while (!kthread_should_stop()) {
3540 pkt_dev = next_to_run(t);
3541
Stephen Hemmingeref879792009-09-22 19:41:43 +00003542 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003543 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003544 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003545 wait_event_interruptible_timeout(t->queue,
3546 t->control != 0,
3547 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003548 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003549 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003550 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003551
Stephen Hemmingeref879792009-09-22 19:41:43 +00003552 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 pktgen_xmit(pkt_dev);
3554
Stephen Hemmingeref879792009-09-22 19:41:43 +00003555 if (need_resched())
3556 pktgen_resched(pkt_dev);
3557 else
3558 cpu_relax();
3559 }
3560
Luiz Capitulino222f1802006-03-20 22:16:13 -08003561 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 pktgen_stop(t);
3563 t->control &= ~(T_STOP);
3564 }
3565
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 pktgen_run(t);
3568 t->control &= ~(T_RUN);
3569 }
3570
Luiz Capitulino222f1802006-03-20 22:16:13 -08003571 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003573 t->control &= ~(T_REMDEVALL);
3574 }
3575
Luiz Capitulino222f1802006-03-20 22:16:13 -08003576 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003577 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 t->control &= ~(T_REMDEV);
3579 }
3580
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003581 try_to_freeze();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Joe Perchesf9467ea2010-06-21 12:29:14 +00003584 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003585 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Joe Perchesf9467ea2010-06-21 12:29:14 +00003587 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Joe Perchesf9467ea2010-06-21 12:29:14 +00003590 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003591 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003592
David S. Milleree74baa2007-01-01 20:51:53 -08003593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
Luiz Capitulino222f1802006-03-20 22:16:13 -08003596static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003597 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003599 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003600 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003602 rcu_read_lock();
3603 list_for_each_entry_rcu(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003604 if (strncmp(p->odevname, ifname, len) == 0) {
3605 if (p->odevname[len]) {
3606 if (exact || p->odevname[len] != '@')
3607 continue;
3608 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003609 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003610 break;
3611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003613 rcu_read_unlock();
Joe Perchesf9467ea2010-06-21 12:29:14 +00003614 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
3617
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003618/*
3619 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 */
3621
Luiz Capitulino222f1802006-03-20 22:16:13 -08003622static int add_dev_to_thread(struct pktgen_thread *t,
3623 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
3625 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003627 /* This function cannot be called concurrently, as its called
3628 * under pktgen_thread_lock mutex, but it can run from
3629 * userspace on another CPU than the kthread. The if_lock()
3630 * is used here to sync with concurrent instances of
3631 * _rem_dev_from_if_list() invoked via kthread, which is also
3632 * updating the if_list */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003633 if_lock(t);
3634
3635 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003636 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003637 rv = -EBUSY;
3638 goto out;
3639 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003640
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 pkt_dev->running = 0;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003642 pkt_dev->pg_thread = t;
3643 list_add_rcu(&pkt_dev->list, &t->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
Luiz Capitulino222f1802006-03-20 22:16:13 -08003645out:
3646 if_unlock(t);
3647 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648}
3649
3650/* Called under thread lock */
3651
Luiz Capitulino222f1802006-03-20 22:16:13 -08003652static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003654 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003655 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003656 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 /* We don't allow a device to be on several threads */
3659
Cong Wang4e58a022013-01-28 19:55:53 +00003660 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003661 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003662 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003663 return -EBUSY;
3664 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003665
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003666 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003667 if (!pkt_dev)
3668 return -ENOMEM;
3669
Eric Dumazet593f63b2009-11-23 01:44:37 +00003670 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003671 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003672 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003673 if (pkt_dev->flows == NULL) {
3674 kfree(pkt_dev);
3675 return -ENOMEM;
3676 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003677
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003678 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003679 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003680 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003681 pkt_dev->count = pg_count_d;
3682 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003683 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003684 pkt_dev->udp_src_max = 9;
3685 pkt_dev->udp_dst_min = 9;
3686 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003687 pkt_dev->vlan_p = 0;
3688 pkt_dev->vlan_cfi = 0;
3689 pkt_dev->vlan_id = 0xffff;
3690 pkt_dev->svlan_p = 0;
3691 pkt_dev->svlan_cfi = 0;
3692 pkt_dev->svlan_id = 0xffff;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003693 pkt_dev->burst = 1;
Robert Olssone99b99b2010-03-18 22:44:30 +00003694 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003695
Cong Wang4e58a022013-01-28 19:55:53 +00003696 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003697 if (err)
3698 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003699 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3700 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003701
Cong Wang4e58a022013-01-28 19:55:53 +00003702 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003703 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003704 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003705 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003706 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003707 err = -EINVAL;
3708 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003709 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003710#ifdef CONFIG_XFRM
3711 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3712 pkt_dev->ipsproto = IPPROTO_ESP;
Fan Ducf93d47e2014-01-03 11:18:31 +08003713
3714 /* xfrm tunnel mode needs additional dst to extract outter
3715 * ip header protocol/ttl/id field, here creat a phony one.
3716 * instead of looking for a valid rt, which definitely hurting
3717 * performance under such circumstance.
3718 */
3719 pkt_dev->dstops.family = AF_INET;
David Millerb6ca8bd2017-11-28 15:45:44 -05003720 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3721 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3722 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3723 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003724#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003725
3726 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003727out2:
3728 dev_put(pkt_dev->odev);
3729out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003730#ifdef CONFIG_XFRM
3731 free_SAs(pkt_dev);
3732#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003733 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003734 kfree(pkt_dev);
3735 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Cong Wang4e58a022013-01-28 19:55:53 +00003738static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003740 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003741 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003742 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003744 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3745 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003746 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003747 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003748 return -ENOMEM;
3749 }
3750
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02003751 mutex_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003753
David S. Milleree74baa2007-01-01 20:51:53 -08003754 INIT_LIST_HEAD(&t->if_list);
3755
Cong Wang4e58a022013-01-28 19:55:53 +00003756 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003757 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003758
Eric Dumazet94dcf292011-03-22 16:30:45 -07003759 p = kthread_create_on_node(pktgen_thread_worker,
3760 t,
3761 cpu_to_node(cpu),
3762 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003763 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003764 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003765 list_del(&t->th_list);
3766 kfree(t);
3767 return PTR_ERR(p);
3768 }
3769 kthread_bind(p, cpu);
3770 t->tsk = p;
3771
Cong Wang4e58a022013-01-28 19:55:53 +00003772 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003773 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003774 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003775 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003776 PG_PROC_DIR, t->tsk->comm);
3777 kthread_stop(p);
3778 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003779 kfree(t);
3780 return -EINVAL;
3781 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003782
Cong Wang4e58a022013-01-28 19:55:53 +00003783 t->net = pn;
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003784 get_task_struct(p);
David S. Milleree74baa2007-01-01 20:51:53 -08003785 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003786 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
3788 return 0;
3789}
3790
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003791/*
3792 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003794static void _rem_dev_from_if_list(struct pktgen_thread *t,
3795 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003797 struct list_head *q, *n;
3798 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003800 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003801 list_for_each_safe(q, n, &t->if_list) {
3802 p = list_entry(q, struct pktgen_dev, list);
3803 if (p == pkt_dev)
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003804 list_del_rcu(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003806 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
Luiz Capitulino222f1802006-03-20 22:16:13 -08003809static int pktgen_remove_device(struct pktgen_thread *t,
3810 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003812 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Luiz Capitulino222f1802006-03-20 22:16:13 -08003814 if (pkt_dev->running) {
Joe Perches294a0b72014-09-09 21:17:30 -07003815 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003816 pktgen_stop_device(pkt_dev);
3817 }
3818
3819 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 if (pkt_dev->odev) {
3822 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003823 pkt_dev->odev = NULL;
3824 }
3825
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003826 /* Remove proc before if_list entry, because add_device uses
3827 * list to determine if interface already exist, avoid race
3828 * with proc_create_data() */
Markus Elfringef87c5d2014-11-18 20:10:34 +01003829 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003831 /* And update the thread if_list */
3832 _rem_dev_from_if_list(t, pkt_dev);
3833
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003834#ifdef CONFIG_XFRM
3835 free_SAs(pkt_dev);
3836#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003837 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003838 if (pkt_dev->page)
3839 put_page(pkt_dev->page);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003840 kfree_rcu(pkt_dev, rcu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842}
3843
Cong Wang4e58a022013-01-28 19:55:53 +00003844static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845{
Cong Wang4e58a022013-01-28 19:55:53 +00003846 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003847 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003848 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003849
Cong Wang4e58a022013-01-28 19:55:53 +00003850 pn->net = net;
3851 INIT_LIST_HEAD(&pn->pktgen_threads);
3852 pn->pktgen_exiting = false;
3853 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3854 if (!pn->proc_dir) {
3855 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003856 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003857 }
Cong Wang4e58a022013-01-28 19:55:53 +00003858 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3859 if (pe == NULL) {
3860 pr_err("cannot create %s procfs entry\n", PGCTRL);
3861 ret = -EINVAL;
3862 goto remove;
3863 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003864
John Hawkes670c02c2005-10-13 09:30:31 -07003865 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003866 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Cong Wang4e58a022013-01-28 19:55:53 +00003868 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003869 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003870 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003871 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003872 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003873
Cong Wang4e58a022013-01-28 19:55:53 +00003874 if (list_empty(&pn->pktgen_threads)) {
3875 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003876 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003877 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003878 }
3879
Luiz Capitulino222f1802006-03-20 22:16:13 -08003880 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003881
Cong Wang4e58a022013-01-28 19:55:53 +00003882remove_entry:
3883 remove_proc_entry(PGCTRL, pn->proc_dir);
3884remove:
Gao fengece31ff2013-02-18 01:34:56 +00003885 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003886 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887}
3888
Cong Wang4e58a022013-01-28 19:55:53 +00003889static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
Cong Wang4e58a022013-01-28 19:55:53 +00003891 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003892 struct pktgen_thread *t;
3893 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003894 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Luiz Capitulino222f1802006-03-20 22:16:13 -08003896 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003897 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Eric Dumazetc57b5462012-05-09 13:29:51 +00003899 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003900 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003901 mutex_unlock(&pktgen_thread_lock);
3902
3903 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003904 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003905 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003906 kthread_stop(t->tsk);
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003907 put_task_struct(t->tsk);
David S. Milleree74baa2007-01-01 20:51:53 -08003908 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Cong Wang4e58a022013-01-28 19:55:53 +00003911 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003912 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003913}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Cong Wang4e58a022013-01-28 19:55:53 +00003915static struct pernet_operations pg_net_ops = {
3916 .init = pg_net_init,
3917 .exit = pg_net_exit,
3918 .id = &pg_net_id,
3919 .size = sizeof(struct pktgen_net),
3920};
3921
3922static int __init pg_init(void)
3923{
3924 int ret = 0;
3925
3926 pr_info("%s", version);
3927 ret = register_pernet_subsys(&pg_net_ops);
3928 if (ret)
3929 return ret;
3930 ret = register_netdevice_notifier(&pktgen_notifier_block);
3931 if (ret)
3932 unregister_pernet_subsys(&pg_net_ops);
3933
3934 return ret;
3935}
3936
3937static void __exit pg_cleanup(void)
3938{
3939 unregister_netdevice_notifier(&pktgen_notifier_block);
3940 unregister_pernet_subsys(&pg_net_ops);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003941 /* Don't need rcu_barrier() due to use of kfree_rcu() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944module_init(pg_init);
3945module_exit(pg_cleanup);
3946
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003947MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948MODULE_DESCRIPTION("Packet Generator tool");
3949MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003950MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003952MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003954MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003956MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003958MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");