blob: f3f5a78cd062973197dc3d514f3c701fd63dc20d [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>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800161#include <linux/mmzone.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200162#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#include <net/checksum.h>
164#include <net/ipv6.h>
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200165#include <net/udp.h>
Stephen Rothwell73d94e92013-07-29 16:21:26 +1000166#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700168#ifdef CONFIG_XFRM
169#include <net/xfrm.h>
170#endif
Cong Wang4e58a022013-01-28 19:55:53 +0000171#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#include <asm/byteorder.h>
173#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700174#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000175#include <linux/io.h>
176#include <linux/timex.h>
177#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800179#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Jesper Dangaard Brouer40207262015-05-21 12:16:56 +0200181#define VERSION "2.75"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800183#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800184#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Joe Perchesf9467ea2010-06-21 12:29:14 +0000186#define func_enter() pr_debug("entering %s\n", __func__);
187
Dmitry Safonov6f107c72018-01-18 18:31:35 +0000188#define PKT_FLAGS \
189 pf(IPV6) /* Interface in IPV6 Mode */ \
190 pf(IPSRC_RND) /* IP-Src Random */ \
191 pf(IPDST_RND) /* IP-Dst Random */ \
192 pf(TXSIZE_RND) /* Transmit size is random */ \
193 pf(UDPSRC_RND) /* UDP-Src Random */ \
194 pf(UDPDST_RND) /* UDP-Dst Random */ \
195 pf(UDPCSUM) /* Include UDP checksum */ \
196 pf(NO_TIMESTAMP) /* Don't timestamp packets (default TS) */ \
197 pf(MPLS_RND) /* Random MPLS labels */ \
198 pf(QUEUE_MAP_RND) /* queue map Random */ \
199 pf(QUEUE_MAP_CPU) /* queue map mirrors smp_processor_id() */ \
200 pf(FLOW_SEQ) /* Sequential flows */ \
201 pf(IPSEC) /* ipsec on for flows */ \
202 pf(MACSRC_RND) /* MAC-Src Random */ \
203 pf(MACDST_RND) /* MAC-Dst Random */ \
204 pf(VID_RND) /* Random VLAN ID */ \
205 pf(SVID_RND) /* Random SVLAN ID */ \
206 pf(NODE) /* Node memory alloc*/ \
207
208#define pf(flag) flag##_SHIFT,
209enum pkt_flags {
210 PKT_FLAGS
211};
212#undef pf
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/* Device flag bits */
Dmitry Safonov6f107c72018-01-18 18:31:35 +0000215#define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
216PKT_FLAGS
217#undef pf
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000219#define pf(flag) __stringify(flag),
220static char *pkt_flag_names[] = {
221 PKT_FLAGS
222};
223#undef pf
224
225#define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000228#define T_STOP (1<<0) /* Stop run */
229#define T_RUN (1<<1) /* Start run */
230#define T_REMDEVALL (1<<2) /* Remove all devs */
231#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200233/* Xmit modes */
234#define M_START_XMIT 0 /* Default normal TX */
235#define M_NETIF_RECEIVE 1 /* Inject packets into stack */
John Fastabend0967f242016-07-02 14:12:54 -0700236#define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200237
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200238/* If lock -- protects updating of if_list */
Eric Dumazet9a0b1e82016-10-15 17:50:49 +0200239#define if_lock(t) mutex_lock(&(t->if_lock));
240#define if_unlock(t) mutex_unlock(&(t->if_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/* Used to help with determining the pkts on receive */
243#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700244#define PG_PROC_DIR "pktgen"
245#define PGCTRL "pgctrl"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247#define MAX_CFLOWS 65536
248
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700249#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
250#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
251
Luiz Capitulino222f1802006-03-20 22:16:13 -0800252struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800253 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800254 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700255#ifdef CONFIG_XFRM
256 struct xfrm_state *x;
257#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700258 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700261/* flow flag bits */
262#define F_INIT (1<<0) /* flow has been initialized */
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /*
266 * Try to keep frequent/infrequent used vars. separated.
267 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800268 struct proc_dir_entry *entry; /* proc file */
269 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000270 struct list_head list; /* chaining in the thread's run-queue */
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200271 struct rcu_head rcu; /* freed by RCU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000273 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800274
275 /* If min != max, then we will either do a linear iteration, or
276 * we will do a random selection from within the range.
277 */
278 __u32 flags;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200279 int xmit_mode;
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000280 int min_pkt_size;
281 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700282 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800283 int nfrags;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200284 int removal_mark; /* non-zero => the device is marked for
285 * removal by worker thread */
286
Eric Dumazet26ad7872011-01-25 13:26:05 -0800287 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000288 u64 delay; /* nano-seconds */
289
Luiz Capitulino222f1802006-03-20 22:16:13 -0800290 __u64 count; /* Default No packets to send */
291 __u64 sofar; /* How many pkts we've sent so far */
292 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800293 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Luiz Capitulino222f1802006-03-20 22:16:13 -0800295 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Luiz Capitulino222f1802006-03-20 22:16:13 -0800297 __u32 clone_count;
298 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000299 * Or a failed transmit of some sort?
300 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800301 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000302 ktime_t next_tx;
303 ktime_t started_at;
304 ktime_t stopped_at;
305 u64 idle_acc; /* nano-seconds */
306
Luiz Capitulino222f1802006-03-20 22:16:13 -0800307 __u32 seq_num;
308
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000309 int clone_skb; /*
310 * Use multiple SKBs during packet gen.
311 * If this number is greater than 1, then
312 * that many copies of the same packet will be
313 * sent before a new packet is allocated.
314 * If you want to send 1024 identical packets
315 * before creating a new packet,
316 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800317 */
318
319 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
320 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
321 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
322 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
323
324 struct in6_addr in6_saddr;
325 struct in6_addr in6_daddr;
326 struct in6_addr cur_in6_daddr;
327 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800329 struct in6_addr min_in6_daddr;
330 struct in6_addr max_in6_daddr;
331 struct in6_addr min_in6_saddr;
332 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Luiz Capitulino222f1802006-03-20 22:16:13 -0800334 /* If we're doing ranges, random or incremental, then this
335 * defines the min/max for those ranges.
336 */
Al Viro252e33462006-11-14 20:48:11 -0800337 __be32 saddr_min; /* inclusive, source IP address */
338 __be32 saddr_max; /* exclusive, source IP address */
339 __be32 daddr_min; /* inclusive, dest IP address */
340 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Luiz Capitulino222f1802006-03-20 22:16:13 -0800342 __u16 udp_src_min; /* inclusive, source UDP port */
343 __u16 udp_src_max; /* exclusive, source UDP port */
344 __u16 udp_dst_min; /* inclusive, dest UDP port */
345 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700347 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000348 __u8 tos; /* six MSB of (former) IPv4 TOS
349 are for dscp codepoint */
350 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
351 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700352
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800353 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000354 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800355 __be32 labels[MAX_MPLS_LABELS];
356
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700357 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
358 __u8 vlan_p;
359 __u8 vlan_cfi;
360 __u16 vlan_id; /* 0xffff means no vlan tag */
361
362 __u8 svlan_p;
363 __u8 svlan_cfi;
364 __u16 svlan_id; /* 0xffff means no svlan tag */
365
Luiz Capitulino222f1802006-03-20 22:16:13 -0800366 __u32 src_mac_count; /* How many MACs to iterate through */
367 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Luiz Capitulino222f1802006-03-20 22:16:13 -0800369 unsigned char dst_mac[ETH_ALEN];
370 unsigned char src_mac[ETH_ALEN];
371
372 __u32 cur_dst_mac_offset;
373 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800374 __be32 cur_saddr;
375 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700376 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800377 __u16 cur_udp_dst;
378 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700379 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800380 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800381 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800382
383 __u8 hh[14];
384 /* = {
385 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
386
387 We fill in SRC address later
388 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
389 0x08, 0x00
390 };
391 */
392 __u16 pad; /* pad out the hh struct to an even 16 bytes */
393
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000394 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800395 * are transmitting the same one multiple times
396 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000397 struct net_device *odev; /* The out-going device.
398 * Note that the device should have it's
399 * pg_info pointer pointing back to this
400 * device.
401 * Set when the user specifies the out-going
402 * device name (not when the inject is
403 * started as it used to do.)
404 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000405 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000407 unsigned int cflows; /* Concurrent flows (config) */
408 unsigned int lflow; /* Flow length (config) */
409 unsigned int nflows; /* accumulated flows (stats) */
410 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700411
412 u16 queue_map_min;
413 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000414 __u32 skb_priority; /* skb priority field */
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -0700415 unsigned int burst; /* number of duplicated packets to burst */
Robert Olssone99b99b2010-03-18 22:44:30 +0000416 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700417
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700418#ifdef CONFIG_XFRM
419 __u8 ipsmode; /* IPSEC mode (config) */
420 __u8 ipsproto; /* IPSEC type (config) */
Fan Dude4aee72014-01-03 11:18:30 +0800421 __u32 spi;
David Millerb6ca8bd2017-11-28 15:45:44 -0500422 struct xfrm_dst xdst;
Fan Ducf93d47e2014-01-03 11:18:31 +0800423 struct dst_ops dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700424#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800425 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426};
427
428struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800429 __be32 pgh_magic;
430 __be32 seq_num;
431 __be32 tv_sec;
432 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
434
Cong Wang4e58a022013-01-28 19:55:53 +0000435
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300436static unsigned int pg_net_id __read_mostly;
Cong Wang4e58a022013-01-28 19:55:53 +0000437
438struct pktgen_net {
439 struct net *net;
440 struct proc_dir_entry *proc_dir;
441 struct list_head pktgen_threads;
442 bool pktgen_exiting;
443};
Eric Dumazet551eaff2010-11-21 10:26:44 -0800444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445struct pktgen_thread {
Eric Dumazet9a0b1e82016-10-15 17:50:49 +0200446 struct mutex if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800447 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800448 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800449 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800450 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000452 /* Field for thread to receive "posted" events terminate,
453 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800454
455 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int cpu;
457
Luiz Capitulino222f1802006-03-20 22:16:13 -0800458 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700459 struct completion start_done;
Cong Wang4e58a022013-01-28 19:55:53 +0000460 struct pktgen_net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461};
462
463#define REMOVE 1
464#define FIND 0
465
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000466static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000467 "Packet Generator for packet performance testing. "
468 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Luiz Capitulino222f1802006-03-20 22:16:13 -0800470static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
471static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
472static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800473 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
Cong Wang4e58a022013-01-28 19:55:53 +0000475static void pktgen_run_all_threads(struct pktgen_net *pn);
476static void pktgen_reset_all_threads(struct pktgen_net *pn);
477static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000478
Luiz Capitulino222f1802006-03-20 22:16:13 -0800479static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000483static int pg_count_d __read_mostly = 1000;
484static int pg_delay_d __read_mostly;
485static int pg_clone_skb_d __read_mostly;
486static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Luiz Capitulino222fa072006-03-20 22:24:27 -0800488static DEFINE_MUTEX(pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static struct notifier_block pktgen_notifier_block = {
491 .notifier_call = pktgen_device_event,
492};
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900495 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 *
497 */
498
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700499static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800500{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000501 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000505static ssize_t pgctrl_write(struct file *file, const char __user *buf,
506 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700508 char data[128];
Cong Wang4e58a022013-01-28 19:55:53 +0000509 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Mathias Krause09455742014-02-21 21:38:35 +0100511 if (!capable(CAP_NET_ADMIN))
512 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Mathias Krause20b0c712014-02-21 21:38:34 +0100514 if (count == 0)
515 return -EINVAL;
516
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700517 if (count > sizeof(data))
518 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Mathias Krause09455742014-02-21 21:38:35 +0100520 if (copy_from_user(data, buf, count))
521 return -EFAULT;
522
Mathias Krause20b0c712014-02-21 21:38:34 +0100523 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Luiz Capitulino222f1802006-03-20 22:16:13 -0800525 if (!strcmp(data, "stop"))
Cong Wang4e58a022013-01-28 19:55:53 +0000526 pktgen_stop_all_threads_ifs(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Luiz Capitulino222f1802006-03-20 22:16:13 -0800528 else if (!strcmp(data, "start"))
Cong Wang4e58a022013-01-28 19:55:53 +0000529 pktgen_run_all_threads(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800531 else if (!strcmp(data, "reset"))
Cong Wang4e58a022013-01-28 19:55:53 +0000532 pktgen_reset_all_threads(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800533
Luiz Capitulino222f1802006-03-20 22:16:13 -0800534 else
Jesper Dangaard Brouer40207262015-05-21 12:16:56 +0200535 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Mathias Krause09455742014-02-21 21:38:35 +0100537 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700540static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Al Virod9dda782013-03-31 18:16:14 -0400542 return single_open(file, pgctrl_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700543}
544
Arjan van de Ven9a321442007-02-12 00:55:35 -0800545static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800546 .open = pgctrl_open,
547 .read = seq_read,
548 .llseek = seq_lseek,
549 .write = pgctrl_write,
550 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700551};
552
553static int pktgen_if_show(struct seq_file *seq, void *v)
554{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000555 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000556 ktime_t stopped;
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000557 unsigned int i;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000558 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Luiz Capitulino222f1802006-03-20 22:16:13 -0800560 seq_printf(seq,
561 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
562 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
563 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Luiz Capitulino222f1802006-03-20 22:16:13 -0800565 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000566 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
567 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000568 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Luiz Capitulino222f1802006-03-20 22:16:13 -0800570 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
571 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Robert Olsson45b270f2007-08-28 15:45:55 -0700573 seq_printf(seq,
574 " queue_map_min: %u queue_map_max: %u\n",
575 pkt_dev->queue_map_min,
576 pkt_dev->queue_map_max);
577
John Fastabend9e50e3a2010-11-16 19:12:28 +0000578 if (pkt_dev->skb_priority)
579 seq_printf(seq, " skb_priority: %u\n",
580 pkt_dev->skb_priority);
581
Luiz Capitulino222f1802006-03-20 22:16:13 -0800582 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800583 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000584 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
585 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
586 &pkt_dev->in6_saddr,
587 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
588 &pkt_dev->in6_daddr,
589 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000590 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800591 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000592 " dst_min: %s dst_max: %s\n",
593 pkt_dev->dst_min, pkt_dev->dst_max);
594 seq_printf(seq,
Jesper Dangaard Brouerd079abd2015-05-21 12:16:11 +0200595 " src_min: %s src_max: %s\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000596 pkt_dev->src_min, pkt_dev->src_max);
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700599 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Johannes Berge1749612008-10-27 15:59:26 -0700601 seq_printf(seq, "%pM ",
602 is_zero_ether_addr(pkt_dev->src_mac) ?
603 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Thomas Graf97dc48e2014-05-16 23:28:54 +0200605 seq_puts(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700606 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Luiz Capitulino222f1802006-03-20 22:16:13 -0800608 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000609 " udp_src_min: %d udp_src_max: %d"
610 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800611 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
612 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Luiz Capitulino222f1802006-03-20 22:16:13 -0800614 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800615 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700616 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800618 if (pkt_dev->nr_labels) {
Thomas Graf97dc48e2014-05-16 23:28:54 +0200619 seq_puts(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700620 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800621 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
622 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
623 }
624
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000625 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700626 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000627 pkt_dev->vlan_id, pkt_dev->vlan_p,
628 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700629
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000630 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700631 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000632 pkt_dev->svlan_id, pkt_dev->svlan_p,
633 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700634
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000635 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700636 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700637
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000638 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700639 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700640
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -0700641 if (pkt_dev->burst > 1)
642 seq_printf(seq, " burst: %d\n", pkt_dev->burst);
643
Robert Olssone99b99b2010-03-18 22:44:30 +0000644 if (pkt_dev->node >= 0)
645 seq_printf(seq, " node: %d\n", pkt_dev->node);
646
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200647 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
648 seq_puts(seq, " xmit_mode: netif_receive\n");
John Fastabend0967f242016-07-02 14:12:54 -0700649 else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
650 seq_puts(seq, " xmit_mode: xmit_queue\n");
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +0200651
Thomas Graf97dc48e2014-05-16 23:28:54 +0200652 seq_puts(seq, " Flags: ");
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800653
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000654 for (i = 0; i < NR_PKT_FLAGS; i++) {
655 if (i == F_FLOW_SEQ)
656 if (!pkt_dev->cflows)
657 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000659 if (pkt_dev->flags & (1 << i))
660 seq_printf(seq, "%s ", pkt_flag_names[i]);
661 else if (i == F_FLOW_SEQ)
662 seq_puts(seq, "FLOW_RND ");
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700663
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700664#ifdef CONFIG_XFRM
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000665 if (i == F_IPSEC && pkt_dev->spi)
Fan Du81013282014-01-03 11:18:33 +0800666 seq_printf(seq, "spi:%u", pkt_dev->spi);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700667#endif
Dmitry Safonov99c6d3d2018-01-18 18:31:36 +0000668 }
Robert Olssone99b99b2010-03-18 22:44:30 +0000669
Luiz Capitulino222f1802006-03-20 22:16:13 -0800670 seq_puts(seq, "\n");
671
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000672 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000673 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000674 idle = pkt_dev->idle_acc;
675 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800676
677 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000678 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800679 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000680 (unsigned long long)pkt_dev->errors);
681
682 seq_printf(seq,
683 " started: %lluus stopped: %lluus idle: %lluus\n",
684 (unsigned long long) ktime_to_us(pkt_dev->started_at),
685 (unsigned long long) ktime_to_us(stopped),
686 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800687
688 seq_printf(seq,
689 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700690 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
691 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Luiz Capitulino222f1802006-03-20 22:16:13 -0800693 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000694 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
695 &pkt_dev->cur_in6_saddr,
696 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800697 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000698 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
699 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Luiz Capitulino222f1802006-03-20 22:16:13 -0800701 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700702 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Robert Olsson45b270f2007-08-28 15:45:55 -0700704 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
705
Luiz Capitulino222f1802006-03-20 22:16:13 -0800706 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800709 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 else
Thomas Graf97dc48e2014-05-16 23:28:54 +0200711 seq_puts(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800716
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000717static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
718 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800719{
720 int i = 0;
721 *num = 0;
722
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700723 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000724 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800725 char c;
726 *num <<= 4;
727 if (get_user(c, &user_buffer[i]))
728 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000729 value = hex_to_bin(c);
730 if (value >= 0)
731 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800732 else
733 break;
734 }
735 return i;
736}
737
Luiz Capitulino222f1802006-03-20 22:16:13 -0800738static int count_trail_chars(const char __user * user_buffer,
739 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int i;
742
743 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800744 char c;
745 if (get_user(c, &user_buffer[i]))
746 return -EFAULT;
747 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 case '\"':
749 case '\n':
750 case '\r':
751 case '\t':
752 case ' ':
753 case '=':
754 break;
755 default:
756 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759done:
760 return i;
761}
762
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000763static long num_arg(const char __user *user_buffer, unsigned long maxlen,
764 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000766 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800768
Paul Gortmakerd6182222010-10-18 12:14:44 +0000769 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800770 char c;
771 if (get_user(c, &user_buffer[i]))
772 return -EFAULT;
773 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800775 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 } else
777 break;
778 }
779 return i;
780}
781
Luiz Capitulino222f1802006-03-20 22:16:13 -0800782static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000784 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Paul Gortmakerd6182222010-10-18 12:14:44 +0000786 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800787 char c;
788 if (get_user(c, &user_buffer[i]))
789 return -EFAULT;
790 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 case '\"':
792 case '\n':
793 case '\r':
794 case '\t':
795 case ' ':
796 goto done_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 default:
798 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return i;
803}
804
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800805static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
806{
Eric Dumazet95c96172012-04-15 05:58:06 +0000807 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800808 char c;
809 ssize_t i = 0;
810 int len;
811
812 pkt_dev->nr_labels = 0;
813 do {
814 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700815 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800816 if (len <= 0)
817 return len;
818 pkt_dev->labels[n] = htonl(tmp);
819 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
820 pkt_dev->flags |= F_MPLS_RND;
821 i += len;
822 if (get_user(c, &buffer[i]))
823 return -EFAULT;
824 i++;
825 n++;
826 if (n >= MAX_MPLS_LABELS)
827 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700828 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800829
830 pkt_dev->nr_labels = n;
831 return i;
832}
833
Dmitry Safonov52e12d52018-01-18 18:31:37 +0000834static __u32 pktgen_read_flag(const char *f, bool *disable)
835{
836 __u32 i;
837
838 if (f[0] == '!') {
839 *disable = true;
840 f++;
841 }
842
843 for (i = 0; i < NR_PKT_FLAGS; i++) {
844 if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
845 continue;
846
847 /* allow only disabling ipv6 flag */
848 if (!*disable && i == IPV6_SHIFT)
849 continue;
850
851 if (strcmp(f, pkt_flag_names[i]) == 0)
852 return 1 << i;
853 }
854
855 if (strcmp(f, "FLOW_RND") == 0) {
856 *disable = !*disable;
857 return F_FLOW_SEQ;
858 }
859
860 return 0;
861}
862
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863static ssize_t pktgen_if_write(struct file *file,
864 const char __user * user_buffer, size_t count,
865 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Joe Perches8a994a72010-07-12 10:50:23 +0000867 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800868 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000869 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 char name[16], valstr[32];
871 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800872 char *pg_result = NULL;
873 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875
876 pg_result = &(pkt_dev->result[0]);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (count < 1) {
Joe Perches294a0b7f2014-09-09 21:17:30 -0700879 pr_warn("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return -EINVAL;
881 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800882
Paul Gortmakerd6182222010-10-18 12:14:44 +0000883 max = count;
884 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800885 if (tmp < 0) {
Joe Perches294a0b7f2014-09-09 21:17:30 -0700886 pr_warn("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800887 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000889 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* Read variable name */
892
893 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000894 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800895 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800898 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -EFAULT;
900 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800901
902 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 if (len < 0)
905 return len;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 i += len;
908
909 if (debug) {
Arnd Bergmanna870a022018-03-13 21:58:39 +0100910 size_t copy = min_t(size_t, count + 1, 1024);
911 char *tp = strndup_user(user_buffer, copy);
912
913 if (IS_ERR(tp))
914 return PTR_ERR(tp);
915
916 pr_debug("%s,%zu buffer -:%s:-\n", name, count, tp);
Gustavo A. R. Silva29d1df72018-03-14 03:07:27 -0500917 kfree(tp);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (!strcmp(name, "min_pkt_size")) {
921 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000922 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800923 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800926 if (value < 14 + 20 + 8)
927 value = 14 + 20 + 8;
928 if (value != pkt_dev->min_pkt_size) {
929 pkt_dev->min_pkt_size = value;
930 pkt_dev->cur_pkt_size = value;
931 }
932 sprintf(pg_result, "OK: min_pkt_size=%u",
933 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return count;
935 }
936
Luiz Capitulino222f1802006-03-20 22:16:13 -0800937 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000939 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800940 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800943 if (value < 14 + 20 + 8)
944 value = 14 + 20 + 8;
945 if (value != pkt_dev->max_pkt_size) {
946 pkt_dev->max_pkt_size = value;
947 pkt_dev->cur_pkt_size = value;
948 }
949 sprintf(pg_result, "OK: max_pkt_size=%u",
950 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return count;
952 }
953
Luiz Capitulino222f1802006-03-20 22:16:13 -0800954 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (!strcmp(name, "pkt_size")) {
957 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000958 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800959 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800962 if (value < 14 + 20 + 8)
963 value = 14 + 20 + 8;
964 if (value != pkt_dev->min_pkt_size) {
965 pkt_dev->min_pkt_size = value;
966 pkt_dev->max_pkt_size = value;
967 pkt_dev->cur_pkt_size = value;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
970 return count;
971 }
972
Luiz Capitulino222f1802006-03-20 22:16:13 -0800973 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000975 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800976 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800979 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sprintf(pg_result, "OK: debug=%u", debug);
981 return count;
982 }
983
Luiz Capitulino222f1802006-03-20 22:16:13 -0800984 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000986 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800987 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 i += len;
990 pkt_dev->nfrags = value;
991 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
992 return count;
993 }
994 if (!strcmp(name, "delay")) {
995 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000996 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800997 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00001000 if (value == 0x7FFFFFFF)
1001 pkt_dev->delay = ULLONG_MAX;
1002 else
Eric Dumazet9240d712009-10-03 01:39:18 +00001003 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00001004
1005 sprintf(pg_result, "OK: delay=%llu",
1006 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return count;
1008 }
Daniel Turull43d28b62010-06-09 22:49:57 +00001009 if (!strcmp(name, "rate")) {
1010 len = num_arg(&user_buffer[i], 10, &value);
1011 if (len < 0)
1012 return len;
1013
1014 i += len;
1015 if (!value)
1016 return len;
1017 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1018 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001019 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001020
1021 sprintf(pg_result, "OK: rate=%lu", value);
1022 return count;
1023 }
1024 if (!strcmp(name, "ratep")) {
1025 len = num_arg(&user_buffer[i], 10, &value);
1026 if (len < 0)
1027 return len;
1028
1029 i += len;
1030 if (!value)
1031 return len;
1032 pkt_dev->delay = NSEC_PER_SEC/value;
1033 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001034 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001035
1036 sprintf(pg_result, "OK: rate=%lu", value);
1037 return count;
1038 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001041 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001042 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 if (value != pkt_dev->udp_src_min) {
1046 pkt_dev->udp_src_min = value;
1047 pkt_dev->cur_udp_src = value;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1050 return count;
1051 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001054 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001055 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001058 if (value != pkt_dev->udp_dst_min) {
1059 pkt_dev->udp_dst_min = value;
1060 pkt_dev->cur_udp_dst = value;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1063 return count;
1064 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001065 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001067 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001068 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001071 if (value != pkt_dev->udp_src_max) {
1072 pkt_dev->udp_src_max = value;
1073 pkt_dev->cur_udp_src = value;
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1076 return count;
1077 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001078 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001080 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001081 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001084 if (value != pkt_dev->udp_dst_max) {
1085 pkt_dev->udp_dst_max = value;
1086 pkt_dev->cur_udp_dst = value;
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1089 return count;
1090 }
1091 if (!strcmp(name, "clone_skb")) {
1092 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001093 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001094 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001095 if ((value > 0) &&
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001096 ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
1097 !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
Neil Hormand8873312011-07-26 06:05:37 +00001098 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001100 pkt_dev->clone_skb = value;
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1103 return count;
1104 }
1105 if (!strcmp(name, "count")) {
1106 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001107 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001108 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 i += len;
1111 pkt_dev->count = value;
1112 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001113 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return count;
1115 }
1116 if (!strcmp(name, "src_mac_count")) {
1117 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001118 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001119 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 i += len;
1122 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001123 pkt_dev->src_mac_count = value;
1124 pkt_dev->cur_src_mac_offset = 0;
1125 }
1126 sprintf(pg_result, "OK: src_mac_count=%d",
1127 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return count;
1129 }
1130 if (!strcmp(name, "dst_mac_count")) {
1131 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001132 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001133 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 i += len;
1136 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001137 pkt_dev->dst_mac_count = value;
1138 pkt_dev->cur_dst_mac_offset = 0;
1139 }
1140 sprintf(pg_result, "OK: dst_mac_count=%d",
1141 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return count;
1143 }
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07001144 if (!strcmp(name, "burst")) {
1145 len = num_arg(&user_buffer[i], 10, &value);
1146 if (len < 0)
1147 return len;
1148
1149 i += len;
John Fastabend0967f242016-07-02 14:12:54 -07001150 if ((value > 1) &&
1151 ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
1152 ((pkt_dev->xmit_mode == M_START_XMIT) &&
1153 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
Eric Dumazet52d6c8c2015-02-22 17:03:41 -08001154 return -ENOTSUPP;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07001155 pkt_dev->burst = value < 1 ? 1 : value;
1156 sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
1157 return count;
1158 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001159 if (!strcmp(name, "node")) {
1160 len = num_arg(&user_buffer[i], 10, &value);
1161 if (len < 0)
1162 return len;
1163
1164 i += len;
1165
1166 if (node_possible(value)) {
1167 pkt_dev->node = value;
1168 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001169 if (pkt_dev->page) {
1170 put_page(pkt_dev->page);
1171 pkt_dev->page = NULL;
1172 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001173 }
1174 else
1175 sprintf(pg_result, "ERROR: node not possible");
1176 return count;
1177 }
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001178 if (!strcmp(name, "xmit_mode")) {
1179 char f[32];
1180
1181 memset(f, 0, 32);
1182 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1183 if (len < 0)
1184 return len;
1185
1186 if (copy_from_user(f, &user_buffer[i], len))
1187 return -EFAULT;
1188 i += len;
1189
1190 if (strcmp(f, "start_xmit") == 0) {
1191 pkt_dev->xmit_mode = M_START_XMIT;
1192 } else if (strcmp(f, "netif_receive") == 0) {
1193 /* clone_skb set earlier, not supported in this mode */
1194 if (pkt_dev->clone_skb > 0)
1195 return -ENOTSUPP;
1196
1197 pkt_dev->xmit_mode = M_NETIF_RECEIVE;
Alexei Starovoitov9eea9222015-05-11 15:19:48 -07001198
1199 /* make sure new packet is allocated every time
1200 * pktgen_xmit() is called
1201 */
1202 pkt_dev->last_ok = 1;
1203
1204 /* override clone_skb if user passed default value
1205 * at module loading time
1206 */
1207 pkt_dev->clone_skb = 0;
John Fastabend0967f242016-07-02 14:12:54 -07001208 } else if (strcmp(f, "queue_xmit") == 0) {
1209 pkt_dev->xmit_mode = M_QUEUE_XMIT;
1210 pkt_dev->last_ok = 1;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02001211 } else {
1212 sprintf(pg_result,
1213 "xmit_mode -:%s:- unknown\nAvailable modes: %s",
1214 f, "start_xmit, netif_receive\n");
1215 return count;
1216 }
1217 sprintf(pg_result, "OK: xmit_mode=%s", f);
1218 return count;
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!strcmp(name, "flag")) {
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001221 __u32 flag;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001222 char f[32];
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001223 bool disable = false;
1224
Luiz Capitulino222f1802006-03-20 22:16:13 -08001225 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001227 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001228 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (copy_from_user(f, &user_buffer[i], len))
1231 return -EFAULT;
1232 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001233
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001234 flag = pktgen_read_flag(f, &disable);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001235
Dmitry Safonov52e12d52018-01-18 18:31:37 +00001236 if (flag) {
1237 if (disable)
1238 pkt_dev->flags &= ~flag;
1239 else
1240 pkt_dev->flags |= flag;
1241 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001242 sprintf(pg_result,
1243 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1244 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001245 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001246 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1247 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1248 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02001249 "NO_TIMESTAMP, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001250#ifdef CONFIG_XFRM
1251 "IPSEC, "
1252#endif
1253 "NODE_ALLOC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001254 return count;
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1257 return count;
1258 }
1259 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1260 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001261 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Luiz Capitulino222f1802006-03-20 22:16:13 -08001264 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001266 buf[len] = 0;
1267 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1268 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001269 strcpy(pkt_dev->dst_min, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001270 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1271 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1272 }
1273 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001274 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001275 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1277 return count;
1278 }
1279 if (!strcmp(name, "dst_max")) {
1280 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001281 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001282 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001283
Luiz Capitulino222f1802006-03-20 22:16:13 -08001284 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286 buf[len] = 0;
1287 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1288 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001289 strcpy(pkt_dev->dst_max, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001290 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1291 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1292 }
1293 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001294 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 i += len;
1296 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1297 return count;
1298 }
1299 if (!strcmp(name, "dst6")) {
1300 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001301 if (len < 0)
1302 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 pkt_dev->flags |= F_IPV6;
1305
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Amerigo Wangc468fb12012-10-09 17:48:20 +00001310 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001311 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001313 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Luiz Capitulino222f1802006-03-20 22:16:13 -08001315 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001316 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Luiz Capitulino222f1802006-03-20 22:16:13 -08001318 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 sprintf(pg_result, "OK: dst6=%s", buf);
1320 return count;
1321 }
1322 if (!strcmp(name, "dst6_min")) {
1323 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001324 if (len < 0)
1325 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 pkt_dev->flags |= F_IPV6;
1328
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001331 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Amerigo Wangc468fb12012-10-09 17:48:20 +00001333 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001334 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001336 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001338 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Luiz Capitulino222f1802006-03-20 22:16:13 -08001340 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 sprintf(pg_result, "OK: dst6_min=%s", buf);
1342 return count;
1343 }
1344 if (!strcmp(name, "dst6_max")) {
1345 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001346 if (len < 0)
1347 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 pkt_dev->flags |= F_IPV6;
1350
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001353 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Amerigo Wangc468fb12012-10-09 17:48:20 +00001355 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001356 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Luiz Capitulino222f1802006-03-20 22:16:13 -08001358 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001359 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Luiz Capitulino222f1802006-03-20 22:16:13 -08001361 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 sprintf(pg_result, "OK: dst6_max=%s", buf);
1363 return count;
1364 }
1365 if (!strcmp(name, "src6")) {
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_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001377 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001379 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
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("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001383
1384 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 sprintf(pg_result, "OK: src6=%s", buf);
1386 return count;
1387 }
1388 if (!strcmp(name, "src_min")) {
1389 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001390 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001391 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001392
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001395 buf[len] = 0;
1396 if (strcmp(buf, pkt_dev->src_min) != 0) {
1397 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001398 strcpy(pkt_dev->src_min, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001399 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1400 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1401 }
1402 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001403 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 i += len;
1405 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1406 return count;
1407 }
1408 if (!strcmp(name, "src_max")) {
1409 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001410 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001411 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001412
Luiz Capitulino222f1802006-03-20 22:16:13 -08001413 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001415 buf[len] = 0;
1416 if (strcmp(buf, pkt_dev->src_max) != 0) {
1417 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
Jakub Kicinskif15f0842018-07-17 14:32:24 -07001418 strcpy(pkt_dev->src_max, buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001419 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1420 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1421 }
1422 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001423 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 i += len;
1425 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1426 return count;
1427 }
1428 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001430 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001431 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001434 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001437 if (!mac_pton(valstr, pkt_dev->dst_mac))
1438 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001440 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001441
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001442 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return count;
1444 }
1445 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001447 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001451 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001454 if (!mac_pton(valstr, pkt_dev->src_mac))
1455 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001456 /* Set up Src MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001457 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001458
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001459 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return count;
1461 }
1462
Luiz Capitulino222f1802006-03-20 22:16:13 -08001463 if (!strcmp(name, "clear_counters")) {
1464 pktgen_clear_counters(pkt_dev);
1465 sprintf(pg_result, "OK: Clearing counters.\n");
1466 return count;
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if (!strcmp(name, "flows")) {
1470 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001471 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001472 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 i += len;
1475 if (value > MAX_CFLOWS)
1476 value = MAX_CFLOWS;
1477
1478 pkt_dev->cflows = value;
1479 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1480 return count;
1481 }
Fan Du6bae9192014-01-10 14:39:13 +08001482#ifdef CONFIG_XFRM
Fan Dude4aee72014-01-03 11:18:30 +08001483 if (!strcmp(name, "spi")) {
1484 len = num_arg(&user_buffer[i], 10, &value);
1485 if (len < 0)
1486 return len;
1487
1488 i += len;
1489 pkt_dev->spi = value;
1490 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1491 return count;
1492 }
Fan Du6bae9192014-01-10 14:39:13 +08001493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!strcmp(name, "flowlen")) {
1495 len = num_arg(&user_buffer[i], 10, &value);
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 i += len;
1500 pkt_dev->lflow = value;
1501 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1502 return count;
1503 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001504
Robert Olsson45b270f2007-08-28 15:45:55 -07001505 if (!strcmp(name, "queue_map_min")) {
1506 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001507 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001508 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001509
Robert Olsson45b270f2007-08-28 15:45:55 -07001510 i += len;
1511 pkt_dev->queue_map_min = value;
1512 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1513 return count;
1514 }
1515
1516 if (!strcmp(name, "queue_map_max")) {
1517 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001518 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001519 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001520
Robert Olsson45b270f2007-08-28 15:45:55 -07001521 i += len;
1522 pkt_dev->queue_map_max = value;
1523 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1524 return count;
1525 }
1526
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001527 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001528 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001529
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001530 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001531 if (len < 0)
1532 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001533 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001534 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001535 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001536 cnt += sprintf(pg_result + cnt,
1537 "%08x%s", ntohl(pkt_dev->labels[n]),
1538 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001539
1540 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1541 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1542 pkt_dev->svlan_id = 0xffff;
1543
1544 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001545 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001546 }
1547 return count;
1548 }
1549
1550 if (!strcmp(name, "vlan_id")) {
1551 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001552 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001553 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001554
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001555 i += len;
1556 if (value <= 4095) {
1557 pkt_dev->vlan_id = value; /* turn on VLAN */
1558
1559 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001560 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001561
1562 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001563 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001564
1565 pkt_dev->nr_labels = 0; /* turn off MPLS */
1566 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1567 } else {
1568 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1569 pkt_dev->svlan_id = 0xffff;
1570
1571 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001572 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001573 }
1574 return count;
1575 }
1576
1577 if (!strcmp(name, "vlan_p")) {
1578 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001579 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001580 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001581
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001582 i += len;
1583 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1584 pkt_dev->vlan_p = value;
1585 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1586 } else {
1587 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1588 }
1589 return count;
1590 }
1591
1592 if (!strcmp(name, "vlan_cfi")) {
1593 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001594 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001595 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001596
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001597 i += len;
1598 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1599 pkt_dev->vlan_cfi = value;
1600 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1601 } else {
1602 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1603 }
1604 return count;
1605 }
1606
1607 if (!strcmp(name, "svlan_id")) {
1608 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001609 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001610 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001611
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001612 i += len;
1613 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1614 pkt_dev->svlan_id = value; /* turn on SVLAN */
1615
1616 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001617 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001618
1619 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001620 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001621
1622 pkt_dev->nr_labels = 0; /* turn off MPLS */
1623 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1624 } else {
1625 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1626 pkt_dev->svlan_id = 0xffff;
1627
1628 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001629 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001630 }
1631 return count;
1632 }
1633
1634 if (!strcmp(name, "svlan_p")) {
1635 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001636 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001637 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001638
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001639 i += len;
1640 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1641 pkt_dev->svlan_p = value;
1642 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1643 } else {
1644 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1645 }
1646 return count;
1647 }
1648
1649 if (!strcmp(name, "svlan_cfi")) {
1650 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001651 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001652 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001653
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001654 i += len;
1655 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1656 pkt_dev->svlan_cfi = value;
1657 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1658 } else {
1659 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1660 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001661 return count;
1662 }
1663
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001664 if (!strcmp(name, "tos")) {
1665 __u32 tmp_value = 0;
1666 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001667 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001668 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001669
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001670 i += len;
1671 if (len == 2) {
1672 pkt_dev->tos = tmp_value;
1673 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1674 } else {
1675 sprintf(pg_result, "ERROR: tos must be 00-ff");
1676 }
1677 return count;
1678 }
1679
1680 if (!strcmp(name, "traffic_class")) {
1681 __u32 tmp_value = 0;
1682 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001683 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001684 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001685
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001686 i += len;
1687 if (len == 2) {
1688 pkt_dev->traffic_class = tmp_value;
1689 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1690 } else {
1691 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1692 }
1693 return count;
1694 }
1695
John Fastabend9e50e3a2010-11-16 19:12:28 +00001696 if (!strcmp(name, "skb_priority")) {
1697 len = num_arg(&user_buffer[i], 9, &value);
1698 if (len < 0)
1699 return len;
1700
1701 i += len;
1702 pkt_dev->skb_priority = value;
1703 sprintf(pg_result, "OK: skb_priority=%i",
1704 pkt_dev->skb_priority);
1705 return count;
1706 }
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1709 return -EINVAL;
1710}
1711
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001712static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Al Virod9dda782013-03-31 18:16:14 -04001714 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001715}
1716
Arjan van de Ven9a321442007-02-12 00:55:35 -08001717static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001718 .open = pktgen_if_open,
1719 .read = seq_read,
1720 .llseek = seq_lseek,
1721 .write = pktgen_if_write,
1722 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001723};
1724
1725static int pktgen_thread_show(struct seq_file *seq, void *v)
1726{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001727 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001728 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001730 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Thomas Graf97dc48e2014-05-16 23:28:54 +02001732 seq_puts(seq, "Running: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001733
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001734 rcu_read_lock();
1735 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001736 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001737 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Thomas Graf97dc48e2014-05-16 23:28:54 +02001739 seq_puts(seq, "\nStopped: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001740
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001741 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001742 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001743 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001746 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 else
Thomas Graf97dc48e2014-05-16 23:28:54 +02001748 seq_puts(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001750 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001755static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001756 const char __user * user_buffer,
1757 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Joe Perches8a994a72010-07-12 10:50:23 +00001759 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001760 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001761 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001763 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001766 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return -EINVAL;
1768 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001769
Paul Gortmakerd6182222010-10-18 12:14:44 +00001770 max = count;
1771 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001772 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001773 return len;
1774
Paul Gortmakerd6182222010-10-18 12:14:44 +00001775 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* Read variable name */
1778
1779 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001780 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001781 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 memset(name, 0, sizeof(name));
1784 if (copy_from_user(name, &user_buffer[i], len))
1785 return -EFAULT;
1786 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001787
Luiz Capitulino222f1802006-03-20 22:16:13 -08001788 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001790 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001791 return len;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 i += len;
1794
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001795 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001796 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Luiz Capitulino222f1802006-03-20 22:16:13 -08001798 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001799 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 ret = -EINVAL;
1801 goto out;
1802 }
1803
1804 pg_result = &(t->result[0]);
1805
Luiz Capitulino222f1802006-03-20 22:16:13 -08001806 if (!strcmp(name, "add_device")) {
1807 char f[32];
1808 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001810 if (len < 0) {
1811 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto out;
1813 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001814 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return -EFAULT;
1816 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001817 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001818 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001819 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001820 if (!ret) {
1821 ret = count;
1822 sprintf(pg_result, "OK: add_device=%s", f);
1823 } else
1824 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 goto out;
1826 }
1827
Luiz Capitulino222f1802006-03-20 22:16:13 -08001828 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001829 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001830 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001831 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001832 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001834 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 goto out;
1836 }
1837
Luiz Capitulino222f1802006-03-20 22:16:13 -08001838 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001839 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001840 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 goto out;
1842 }
1843
1844 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001845out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return ret;
1847}
1848
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001849static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Al Virod9dda782013-03-31 18:16:14 -04001851 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Arjan van de Ven9a321442007-02-12 00:55:35 -08001854static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001855 .open = pktgen_thread_open,
1856 .read = seq_read,
1857 .llseek = seq_lseek,
1858 .write = pktgen_thread_write,
1859 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001860};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001863static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1864 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct pktgen_thread *t;
1867 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001868 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Cong Wang4e58a022013-01-28 19:55:53 +00001870 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001871 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001873 if (remove) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001874 pkt_dev->removal_mark = 1;
1875 t->control |= T_REMDEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 break;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001880 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001883/*
1884 * mark a device for removal
1885 */
Cong Wang4e58a022013-01-28 19:55:53 +00001886static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001889 const int max_tries = 10, msec_per_try = 125;
1890 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001891
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001892 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001893 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001894
Luiz Capitulino222f1802006-03-20 22:16:13 -08001895 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001896
Cong Wang4e58a022013-01-28 19:55:53 +00001897 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001898 if (pkt_dev == NULL)
1899 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001900
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001901 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001902 pr_debug("%s: waiting for %s to disappear....\n",
1903 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001904 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001905 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001906
1907 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001908 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1909 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001910 break;
1911 }
1912
1913 }
1914
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001915 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001916}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001917
Cong Wang4e58a022013-01-28 19:55:53 +00001918static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001919{
1920 struct pktgen_thread *t;
1921
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001922 mutex_lock(&pktgen_thread_lock);
1923
Cong Wang4e58a022013-01-28 19:55:53 +00001924 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001925 struct pktgen_dev *pkt_dev;
1926
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001927 if_lock(t);
1928 list_for_each_entry(pkt_dev, &t->if_list, list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001929 if (pkt_dev->odev != dev)
1930 continue;
1931
David Howellsa8ca16e2013-04-12 17:27:28 +01001932 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001933
Alexey Dobriyan29753152009-08-28 23:34:43 -07001934 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00001935 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07001936 &pktgen_if_fops,
1937 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001938 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001939 pr_err("can't move proc entry for '%s'\n",
1940 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001941 break;
1942 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001943 if_unlock(t);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001944 }
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02001945 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
Luiz Capitulino222f1802006-03-20 22:16:13 -08001948static int pktgen_device_event(struct notifier_block *unused,
1949 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Jiri Pirko351638e2013-05-28 01:30:21 +00001951 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00001952 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Cong Wang4e58a022013-01-28 19:55:53 +00001954 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001955 return NOTIFY_DONE;
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /* It is OK that we do not hold the group lock right now,
1958 * as we run under the RTNL lock.
1959 */
1960
1961 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001962 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00001963 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00001967 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 return NOTIFY_DONE;
1972}
1973
Cong Wang4e58a022013-01-28 19:55:53 +00001974static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1975 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001976 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001977{
1978 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001979 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001980
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001981 for (i = 0; ifname[i] != '@'; i++) {
1982 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001983 break;
1984
1985 b[i] = ifname[i];
1986 }
1987 b[i] = 0;
1988
Cong Wang4e58a022013-01-28 19:55:53 +00001989 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07001990}
1991
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993/* Associate pktgen_dev with a device. */
1994
Cong Wang4e58a022013-01-28 19:55:53 +00001995static int pktgen_setup_dev(const struct pktgen_net *pn,
1996 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001997{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001999 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (pkt_dev->odev) {
2003 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002004 pkt_dev->odev = NULL;
2005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Cong Wang4e58a022013-01-28 19:55:53 +00002007 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002009 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002010 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002014 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002015 err = -EINVAL;
2016 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002017 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002018 err = -ENETDOWN;
2019 } else {
2020 pkt_dev->odev = odev;
2021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002025 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
2028/* Read pkt_dev from the interface and set up internal pktgen_dev
2029 * structure to have the right information to create/send packets
2030 */
2031static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2032{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002033 int ntxq;
2034
Luiz Capitulino222f1802006-03-20 22:16:13 -08002035 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002036 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002037 sprintf(pkt_dev->result,
2038 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2039 return;
2040 }
2041
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002042 /* make sure that we don't pick a non-existing transmit queue */
2043 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002044
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002045 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07002046 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2047 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2048 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002049 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002050 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002051 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07002052 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2053 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2054 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002055 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002056 }
2057
Luiz Capitulino222f1802006-03-20 22:16:13 -08002058 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002060 if (is_zero_ether_addr(pkt_dev->src_mac))
Joe Perches9ea08b12014-01-20 09:52:19 -08002061 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Luiz Capitulino222f1802006-03-20 22:16:13 -08002063 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08002064 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Luiz Capitulino222f1802006-03-20 22:16:13 -08002066 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002067 int i, set = 0, err = 1;
2068 struct inet6_dev *idev;
2069
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002070 if (pkt_dev->min_pkt_size == 0) {
2071 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2072 + sizeof(struct udphdr)
2073 + sizeof(struct pktgen_hdr)
2074 + pkt_dev->pkt_overhead;
2075 }
2076
Eric Dumazetdf7e8e22017-11-04 08:27:14 -07002077 for (i = 0; i < sizeof(struct in6_addr); i++)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002078 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 set = 1;
2080 break;
2081 }
2082
Luiz Capitulino222f1802006-03-20 22:16:13 -08002083 if (!set) {
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /*
2086 * Use linklevel address if unconfigured.
2087 *
2088 * use ipv6_get_lladdr if/when it's get exported
2089 */
2090
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002091 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002092 idev = __in6_dev_get(pkt_dev->odev);
2093 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 struct inet6_ifaddr *ifp;
2095
2096 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002097 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2098 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002099 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002100 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 err = 0;
2102 break;
2103 }
2104 }
2105 read_unlock_bh(&idev->lock);
2106 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002107 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002108 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002109 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002111 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002112 if (pkt_dev->min_pkt_size == 0) {
2113 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2114 + sizeof(struct udphdr)
2115 + sizeof(struct pktgen_hdr)
2116 + pkt_dev->pkt_overhead;
2117 }
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 pkt_dev->saddr_min = 0;
2120 pkt_dev->saddr_max = 0;
2121 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002122
2123 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002126 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (in_dev) {
2128 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002129 pkt_dev->saddr_min =
2130 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 pkt_dev->saddr_max = pkt_dev->saddr_min;
2132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002135 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2137 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2138 }
2139
2140 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2141 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2142 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002143 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002144 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2145 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2146 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2147
Luiz Capitulino222f1802006-03-20 22:16:13 -08002148 pkt_dev->cur_dst_mac_offset = 0;
2149 pkt_dev->cur_src_mac_offset = 0;
2150 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2151 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2152 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2153 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 pkt_dev->nflows = 0;
2155}
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002158static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2159{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002160 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002161 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002162 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002163
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002164 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2165 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002166
Daniel Turull43d28b62010-06-09 22:49:57 +00002167 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002168 if (remaining <= 0)
2169 goto out;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002170
Daniel Borkmann398f3822012-10-28 08:27:19 +00002171 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002172 if (remaining < 100000) {
2173 /* for small delays (<100us), just loop until limit is reached */
2174 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002175 end_time = ktime_get();
2176 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002177 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002178 /* see do_nanosleep */
2179 hrtimer_init_sleeper(&t, current);
2180 do {
2181 set_current_state(TASK_INTERRUPTIBLE);
2182 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002183
2184 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002187 hrtimer_cancel(&t.timer);
2188 } while (t.task && pkt_dev->running && !signal_pending(current));
2189 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002190 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002192
2193 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002194out:
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002195 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Guenter Roeckbcf91bd2016-05-26 17:21:06 -07002196 destroy_hrtimer_on_stack(&t.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002199static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2200{
Paolo Abeni63d75462016-09-30 16:56:45 +02002201 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002202 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2203 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2204 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2205}
2206
Stephen Hemminger648fda72009-08-27 13:55:07 +00002207static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002208{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002209 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002210}
2211
2212static inline int f_pick(struct pktgen_dev *pkt_dev)
2213{
2214 int flow = pkt_dev->curfl;
2215
2216 if (pkt_dev->flags & F_FLOW_SEQ) {
2217 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2218 /* reset time */
2219 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002220 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002221 pkt_dev->curfl += 1;
2222 if (pkt_dev->curfl >= pkt_dev->cflows)
2223 pkt_dev->curfl = 0; /*reset */
2224 }
2225 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002226 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002227 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002228
Robert Olsson1211a642008-08-05 18:44:26 -07002229 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002230 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002231 pkt_dev->flows[flow].flags = 0;
2232 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002233 }
2234
2235 return pkt_dev->curfl;
2236}
2237
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002238
2239#ifdef CONFIG_XFRM
2240/* If there was already an IPSEC SA, we keep it as is, else
2241 * we go look for it ...
2242*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002243#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002244static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002245{
2246 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002247 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002248 if (!x) {
Fan Duc4549972014-01-03 11:18:32 +08002249
2250 if (pkt_dev->spi) {
2251 /* We need as quick as possible to find the right SA
2252 * Searching with minimum criteria to archieve this.
2253 */
2254 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2255 } else {
2256 /* slow path: we dont already have xfrm_state */
Steffen Klassert7e652642018-06-12 14:07:07 +02002257 x = xfrm_stateonly_find(pn->net, DUMMY_MARK, 0,
Fan Duc4549972014-01-03 11:18:32 +08002258 (xfrm_address_t *)&pkt_dev->cur_daddr,
2259 (xfrm_address_t *)&pkt_dev->cur_saddr,
2260 AF_INET,
2261 pkt_dev->ipsmode,
2262 pkt_dev->ipsproto, 0);
2263 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002264 if (x) {
2265 pkt_dev->flows[flow].x = x;
2266 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002267 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002268 }
2269
2270 }
2271}
2272#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002273static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2274{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002275
2276 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2277 pkt_dev->cur_queue_map = smp_processor_id();
2278
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002279 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002280 __u16 t;
2281 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002282 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002283 (pkt_dev->queue_map_max -
2284 pkt_dev->queue_map_min + 1)
2285 + pkt_dev->queue_map_min;
2286 } else {
2287 t = pkt_dev->cur_queue_map + 1;
2288 if (t > pkt_dev->queue_map_max)
2289 t = pkt_dev->queue_map_min;
2290 }
2291 pkt_dev->cur_queue_map = t;
2292 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002293 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 -07002294}
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296/* Increment/randomize headers according to flags and current values
2297 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2298 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002299static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2300{
2301 __u32 imn;
2302 __u32 imx;
2303 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002305 if (pkt_dev->cflows)
2306 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002309 if (pkt_dev->src_mac_count > 1) {
2310 __u32 mc;
2311 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Luiz Capitulino222f1802006-03-20 22:16:13 -08002313 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002314 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002315 else {
2316 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002317 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002318 pkt_dev->src_mac_count)
2319 pkt_dev->cur_src_mac_offset = 0;
2320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Luiz Capitulino222f1802006-03-20 22:16:13 -08002322 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2323 pkt_dev->hh[11] = tmp;
2324 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2325 pkt_dev->hh[10] = tmp;
2326 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2327 pkt_dev->hh[9] = tmp;
2328 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2329 pkt_dev->hh[8] = tmp;
2330 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2331 pkt_dev->hh[7] = tmp;
2332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Luiz Capitulino222f1802006-03-20 22:16:13 -08002334 /* Deal with Destination MAC */
2335 if (pkt_dev->dst_mac_count > 1) {
2336 __u32 mc;
2337 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Luiz Capitulino222f1802006-03-20 22:16:13 -08002339 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002340 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Luiz Capitulino222f1802006-03-20 22:16:13 -08002342 else {
2343 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002344 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002345 pkt_dev->dst_mac_count) {
2346 pkt_dev->cur_dst_mac_offset = 0;
2347 }
2348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Luiz Capitulino222f1802006-03-20 22:16:13 -08002350 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2351 pkt_dev->hh[5] = tmp;
2352 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2353 pkt_dev->hh[4] = tmp;
2354 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2355 pkt_dev->hh[3] = tmp;
2356 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2357 pkt_dev->hh[2] = tmp;
2358 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2359 pkt_dev->hh[1] = tmp;
2360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002362 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002363 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002364 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002365 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2366 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002367 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002368 htonl(0x000fffff));
2369 }
2370
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002371 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002372 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002373 }
2374
2375 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002376 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002377 }
2378
Luiz Capitulino222f1802006-03-20 22:16:13 -08002379 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2380 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002381 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002382 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2383 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Luiz Capitulino222f1802006-03-20 22:16:13 -08002385 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 pkt_dev->cur_udp_src++;
2387 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2388 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002389 }
2390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Luiz Capitulino222f1802006-03-20 22:16:13 -08002392 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2393 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002394 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002395 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2396 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002397 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002399 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002401 }
2402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 if (!(pkt_dev->flags & F_IPV6)) {
2405
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002406 imn = ntohl(pkt_dev->saddr_min);
2407 imx = ntohl(pkt_dev->saddr_max);
2408 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002410 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002411 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 else {
2413 t = ntohl(pkt_dev->cur_saddr);
2414 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002415 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
2419 pkt_dev->cur_saddr = htonl(t);
2420 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002421
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002422 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2424 } else {
Al Viro252e33462006-11-14 20:48:11 -08002425 imn = ntohl(pkt_dev->daddr_min);
2426 imx = ntohl(pkt_dev->daddr_max);
2427 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002429 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (pkt_dev->flags & F_IPDST_RND) {
2431
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002432 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002433 t = prandom_u32() %
2434 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002435 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002436 } while (ipv4_is_loopback(s) ||
2437 ipv4_is_multicast(s) ||
2438 ipv4_is_lbcast(s) ||
2439 ipv4_is_zeronet(s) ||
2440 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002441 pkt_dev->cur_daddr = s;
2442 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 t = ntohl(pkt_dev->cur_daddr);
2444 t++;
2445 if (t > imx) {
2446 t = imn;
2447 }
2448 pkt_dev->cur_daddr = htonl(t);
2449 }
2450 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002451 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002452 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002453 pkt_dev->flows[flow].cur_daddr =
2454 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002455#ifdef CONFIG_XFRM
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002456 if (pkt_dev->flags & F_IPSEC)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002457 get_ipsec_sa(pkt_dev, flow);
2458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 pkt_dev->nflows++;
2460 }
2461 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002462 } else { /* IPV6 * */
2463
Joe Perches06e30412012-10-18 17:55:31 +00002464 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 int i;
2466
2467 /* Only random destinations yet */
2468
Luiz Capitulino222f1802006-03-20 22:16:13 -08002469 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002471 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002472 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2473 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
2477
Luiz Capitulino222f1802006-03-20 22:16:13 -08002478 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2479 __u32 t;
2480 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002481 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002482 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2483 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002484 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002486 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002488 }
2489 pkt_dev->cur_pkt_size = t;
2490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002492 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 pkt_dev->flows[flow].count++;
2495}
2496
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002497
2498#ifdef CONFIG_XFRM
Fengguang Wu5537a052014-01-06 11:00:07 +01002499static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
Fan Ducf93d47e2014-01-03 11:18:31 +08002500
2501 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2502};
2503
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002504static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2505{
2506 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2507 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002508 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002509
2510 if (!x)
2511 return 0;
2512 /* XXX: we dont support tunnel mode for now until
2513 * we resolve the dst issue */
Fan Ducf93d47e2014-01-03 11:18:31 +08002514 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002515 return 0;
2516
Fan Ducf93d47e2014-01-03 11:18:31 +08002517 /* But when user specify an valid SPI, transformation
2518 * supports both transport/tunnel mode + ESP/AH type.
2519 */
2520 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
David Millerb6ca8bd2017-11-28 15:45:44 -05002521 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
Fan Ducf93d47e2014-01-03 11:18:31 +08002522
2523 rcu_read_lock_bh();
Herbert Xu13996372007-10-17 21:35:51 -07002524 err = x->outer_mode->output(x, skb);
Fan Ducf93d47e2014-01-03 11:18:31 +08002525 rcu_read_unlock_bh();
Fan Du6de9ace2014-01-03 11:18:28 +08002526 if (err) {
2527 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002528 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002529 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002530 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002531 if (err) {
2532 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002533 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002534 }
Fan Du0af0a412014-01-03 11:18:27 +08002535 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002536 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002537 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002538 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002539error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002540 return err;
2541}
2542
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002543static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002544{
2545 if (pkt_dev->cflows) {
2546 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002547 int i;
2548 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002549 struct xfrm_state *x = pkt_dev->flows[i].x;
2550 if (x) {
2551 xfrm_state_put(x);
2552 pkt_dev->flows[i].x = NULL;
2553 }
2554 }
2555 }
2556}
2557
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002558static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002559 struct sk_buff *skb, __be16 protocol)
2560{
Dmitry Safonov6f107c72018-01-18 18:31:35 +00002561 if (pkt_dev->flags & F_IPSEC) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002562 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2563 int nhead = 0;
2564 if (x) {
Eric Dumazetd4969582015-05-25 16:06:37 -07002565 struct ethhdr *eth;
fan.du38682042013-12-01 16:28:48 +08002566 struct iphdr *iph;
Eric Dumazetd4969582015-05-25 16:06:37 -07002567 int ret;
fan.du38682042013-12-01 16:28:48 +08002568
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002569 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002570 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002571 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2572 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002573 pr_err("Error expanding ipsec packet %d\n",
2574 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002575 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002576 }
2577 }
2578
2579 /* ipsec is not expecting ll header */
2580 skb_pull(skb, ETH_HLEN);
2581 ret = pktgen_output_ipsec(skb, pkt_dev);
2582 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002583 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002584 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002585 }
2586 /* restore ll */
Johannes Bergd58ff352017-06-16 14:29:23 +02002587 eth = skb_push(skb, ETH_HLEN);
Eric Dumazetd4969582015-05-25 16:06:37 -07002588 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2589 eth->h_proto = protocol;
fan.du38682042013-12-01 16:28:48 +08002590
2591 /* Update IPv4 header len as well as checksum value */
2592 iph = ip_hdr(skb);
2593 iph->tot_len = htons(skb->len - ETH_HLEN);
2594 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002595 }
2596 }
2597 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002598err:
2599 kfree_skb(skb);
2600 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002601}
2602#endif
2603
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002604static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2605{
Eric Dumazet95c96172012-04-15 05:58:06 +00002606 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002607 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002608 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002609
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002610 mpls--;
2611 *mpls |= MPLS_STACK_BOTTOM;
2612}
2613
Al Viro0f37c602006-11-03 03:49:56 -08002614static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2615 unsigned int prio)
2616{
2617 return htons(id | (cfi << 12) | (prio << 13));
2618}
2619
Eric Dumazet26ad7872011-01-25 13:26:05 -08002620static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2621 int datalen)
2622{
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002623 struct timespec64 timestamp;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002624 struct pktgen_hdr *pgh;
2625
Johannes Berg4df864c2017-06-16 14:29:21 +02002626 pgh = skb_put(skb, sizeof(*pgh));
Eric Dumazet26ad7872011-01-25 13:26:05 -08002627 datalen -= sizeof(*pgh);
2628
2629 if (pkt_dev->nfrags <= 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002630 skb_put_zero(skb, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002631 } else {
2632 int frags = pkt_dev->nfrags;
2633 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002634 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002635
2636
2637 if (frags > MAX_SKB_FRAGS)
2638 frags = MAX_SKB_FRAGS;
2639 len = datalen - frags * PAGE_SIZE;
2640 if (len > 0) {
Johannes Bergb080db52017-06-16 14:29:19 +02002641 skb_put_zero(skb, len);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002642 datalen = frags * PAGE_SIZE;
2643 }
2644
2645 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002646 frag_len = (datalen/frags) < PAGE_SIZE ?
2647 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002648 while (datalen > 0) {
2649 if (unlikely(!pkt_dev->page)) {
2650 int node = numa_node_id();
2651
2652 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2653 node = pkt_dev->node;
2654 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2655 if (!pkt_dev->page)
2656 break;
2657 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002658 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002659 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002660 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002661 /*last fragment, fill rest of data*/
2662 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002663 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2664 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002665 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002666 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2667 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2668 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2669 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002670 i++;
2671 skb_shinfo(skb)->nr_frags = i;
2672 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002673 }
2674
2675 /* Stamp the time, and sequence number,
2676 * convert them to network byte order
2677 */
2678 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2679 pgh->seq_num = htonl(pkt_dev->seq_num);
2680
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002681 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2682 pgh->tv_sec = 0;
2683 pgh->tv_usec = 0;
2684 } else {
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002685 /*
2686 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2687 * as done by wireshark, or y2038 when interpreted as signed.
2688 * This is probably harmless, but if anyone wants to improve
2689 * it, we could introduce a variant that puts 64-bit nanoseconds
2690 * into the respective header bytes.
2691 * This would also be slightly faster to read.
2692 */
2693 ktime_get_real_ts64(&timestamp);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002694 pgh->tv_sec = htonl(timestamp.tv_sec);
Arnd Bergmann7f5d3f22017-11-07 11:38:32 +01002695 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002696 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002697}
2698
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002699static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
Paolo Abeni63d75462016-09-30 16:56:45 +02002700 struct pktgen_dev *pkt_dev)
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002701{
Paolo Abeni63d75462016-09-30 16:56:45 +02002702 unsigned int extralen = LL_RESERVED_SPACE(dev);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002703 struct sk_buff *skb = NULL;
Paolo Abeni63d75462016-09-30 16:56:45 +02002704 unsigned int size;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002705
Paolo Abeni63d75462016-09-30 16:56:45 +02002706 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002707 if (pkt_dev->flags & F_NODE) {
2708 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2709
2710 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2711 if (likely(skb)) {
2712 skb_reserve(skb, NET_SKB_PAD);
2713 skb->dev = dev;
2714 }
2715 } else {
2716 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2717 }
John Fastabend3de03592016-01-10 21:38:44 -08002718
Paolo Abeni63d75462016-09-30 16:56:45 +02002719 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
John Fastabend3de03592016-01-10 21:38:44 -08002720 if (likely(skb))
Paolo Abeni63d75462016-09-30 16:56:45 +02002721 skb_reserve(skb, extralen - 16);
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002722
2723 return skb;
2724}
2725
Luiz Capitulino222f1802006-03-20 22:16:13 -08002726static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2727 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
2729 struct sk_buff *skb = NULL;
2730 __u8 *eth;
2731 struct udphdr *udph;
2732 int datalen, iplen;
2733 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002734 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002735 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002736 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2737 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2738 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2739 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002740 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002741
2742 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002743 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002744
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002745 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002746 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002747
Robert Olsson64053be2005-06-26 15:27:10 -07002748 /* Update any of the values, used when we're incrementing various
2749 * fields.
2750 */
2751 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002752 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002753
Paolo Abeni63d75462016-09-30 16:56:45 +02002754 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (!skb) {
2756 sprintf(pkt_dev->result, "No memory");
2757 return NULL;
2758 }
2759
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002760 prefetchw(skb->data);
Paolo Abeni63d75462016-09-30 16:56:45 +02002761 skb_reserve(skb, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02002764 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02002765 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002766 if (pkt_dev->nr_labels)
2767 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002768
2769 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002770 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02002771 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002772 *svlan_tci = build_tci(pkt_dev->svlan_id,
2773 pkt_dev->svlan_cfi,
2774 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002775 svlan_encapsulated_proto = skb_put(skb,
2776 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002777 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002778 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002779 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002780 *vlan_tci = build_tci(pkt_dev->vlan_id,
2781 pkt_dev->vlan_cfi,
2782 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002783 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002784 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002785 }
2786
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00002787 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00002788 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002789 iph = skb_put(skb, sizeof(struct iphdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00002790
2791 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002792 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002793 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002794 skb->priority = pkt_dev->skb_priority;
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002797 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002799 /* Eth + IPh + UDPh + mpls */
2800 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002801 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002802 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 udph->source = htons(pkt_dev->cur_udp_src);
2806 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002807 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002808 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 iph->ihl = 5;
2811 iph->version = 4;
2812 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002813 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002814 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 iph->saddr = pkt_dev->cur_saddr;
2816 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002817 iph->id = htons(pkt_dev->ip_id);
2818 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 iph->frag_off = 0;
2820 iplen = 20 + 8 + datalen;
2821 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002822 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002823 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 skb->dev = odev;
2825 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002826
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002827 pktgen_finalize_skb(pkt_dev, skb, datalen);
2828
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002829 if (!(pkt_dev->flags & F_UDPCSUM)) {
2830 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08002831 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002832 skb->ip_summed = CHECKSUM_PARTIAL;
2833 skb->csum = 0;
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002834 udp4_hwcsum(skb, iph->saddr, iph->daddr);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002835 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002836 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002837
2838 /* add protocol-dependent pseudo-header */
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002839 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002840 datalen + 8, IPPROTO_UDP, csum);
2841
2842 if (udph->check == 0)
2843 udph->check = CSUM_MANGLED_0;
2844 }
2845
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002846#ifdef CONFIG_XFRM
2847 if (!process_ipsec(pkt_dev, skb, protocol))
2848 return NULL;
2849#endif
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return skb;
2852}
2853
Luiz Capitulino222f1802006-03-20 22:16:13 -08002854static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2855 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
2857 struct sk_buff *skb = NULL;
2858 __u8 *eth;
2859 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002860 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002862 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002863 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002864 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2865 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2866 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2867 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002868 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002869
2870 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002871 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002872
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002873 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002874 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002875
Robert Olsson64053be2005-06-26 15:27:10 -07002876 /* Update any of the values, used when we're incrementing various
2877 * fields.
2878 */
2879 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002880 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002881
Paolo Abeni63d75462016-09-30 16:56:45 +02002882 skb = pktgen_alloc_skb(odev, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (!skb) {
2884 sprintf(pkt_dev->result, "No memory");
2885 return NULL;
2886 }
2887
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002888 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 skb_reserve(skb, 16);
2890
2891 /* Reserve for ethernet and IP header */
Johannes Bergd58ff352017-06-16 14:29:23 +02002892 eth = skb_push(skb, 14);
Johannes Berg4df864c2017-06-16 14:29:21 +02002893 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002894 if (pkt_dev->nr_labels)
2895 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002896
2897 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002898 if (pkt_dev->svlan_id != 0xffff) {
Johannes Berg4df864c2017-06-16 14:29:21 +02002899 svlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002900 *svlan_tci = build_tci(pkt_dev->svlan_id,
2901 pkt_dev->svlan_cfi,
2902 pkt_dev->svlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002903 svlan_encapsulated_proto = skb_put(skb,
2904 sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002905 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002906 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002907 vlan_tci = skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002908 *vlan_tci = build_tci(pkt_dev->vlan_id,
2909 pkt_dev->vlan_cfi,
2910 pkt_dev->vlan_p);
Johannes Berg4df864c2017-06-16 14:29:21 +02002911 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002912 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002913 }
2914
Zhang Shengjuc145aeb2016-02-29 08:21:30 +00002915 skb_reset_mac_header(skb);
Thomas Graf525cebe2013-06-03 11:49:23 +00002916 skb_set_network_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002917 iph = skb_put(skb, sizeof(struct ipv6hdr));
Thomas Graf525cebe2013-06-03 11:49:23 +00002918
2919 skb_set_transport_header(skb, skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02002920 udph = skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002921 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002922 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002925 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002926
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002927 /* Eth + IPh + UDPh + mpls */
2928 datalen = pkt_dev->cur_pkt_size - 14 -
2929 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002930 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002932 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002934 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
2936
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002937 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 udph->source = htons(pkt_dev->cur_udp_src);
2939 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002940 udph->len = htons(udplen);
2941 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002943 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002945 if (pkt_dev->traffic_class) {
2946 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002947 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002948 }
2949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 iph->hop_limit = 32;
2951
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002952 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 iph->nexthdr = IPPROTO_UDP;
2954
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002955 iph->daddr = pkt_dev->cur_in6_daddr;
2956 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002958 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 skb->dev = odev;
2960 skb->pkt_type = PACKET_HOST;
2961
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002962 pktgen_finalize_skb(pkt_dev, skb, datalen);
2963
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002964 if (!(pkt_dev->flags & F_UDPCSUM)) {
2965 skb->ip_summed = CHECKSUM_NONE;
Tom Herbertc8cd0982015-12-14 11:19:44 -08002966 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002967 skb->ip_summed = CHECKSUM_PARTIAL;
2968 skb->csum_start = skb_transport_header(skb) - skb->head;
2969 skb->csum_offset = offsetof(struct udphdr, check);
2970 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2971 } else {
Sabrina Dubroca7744b5f2015-02-04 23:08:50 +01002972 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002973
2974 /* add protocol-dependent pseudo-header */
2975 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2976
2977 if (udph->check == 0)
2978 udph->check = CSUM_MANGLED_0;
2979 }
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return skb;
2982}
2983
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002984static struct sk_buff *fill_packet(struct net_device *odev,
2985 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002987 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return fill_packet_ipv6(odev, pkt_dev);
2989 else
2990 return fill_packet_ipv4(odev, pkt_dev);
2991}
2992
Luiz Capitulino222f1802006-03-20 22:16:13 -08002993static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002995 pkt_dev->seq_num = 1;
2996 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002998 pkt_dev->tx_bytes = 0;
2999 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000}
3001
3002/* Set up structure for sending pkts, clear counters */
3003
3004static void pktgen_run(struct pktgen_thread *t)
3005{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003006 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 int started = 0;
3008
Joe Perchesf9467ea2010-06-21 12:29:14 +00003009 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003011 rcu_read_lock();
3012 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 /*
3015 * setup odev and create initial packet.
3016 */
3017 pktgen_setup_inject(pkt_dev);
3018
Luiz Capitulino222f1802006-03-20 22:16:13 -08003019 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 pktgen_clear_counters(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003022 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003023
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003024 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 strcpy(pkt_dev->result, "Starting");
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003027 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003029 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 strcpy(pkt_dev->result, "Error starting");
3031 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003032 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003033 if (started)
3034 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
3036
Cong Wang4e58a022013-01-28 19:55:53 +00003037static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003039 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Joe Perchesf9467ea2010-06-21 12:29:14 +00003041 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003043 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003044
Cong Wang4e58a022013-01-28 19:55:53 +00003045 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003046 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003047
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003048 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Stephen Hemminger648fda72009-08-27 13:55:07 +00003051static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003053 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003055 rcu_read_lock();
3056 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3057 if (pkt_dev->running) {
3058 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003059 return 1;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003060 }
3061 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Luiz Capitulino222f1802006-03-20 22:16:13 -08003065static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Luiz Capitulino222f1802006-03-20 22:16:13 -08003069 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Luiz Capitulino222f1802006-03-20 22:16:13 -08003071 if (signal_pending(current))
3072 goto signal;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003073 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074 return 1;
3075signal:
3076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Cong Wang4e58a022013-01-28 19:55:53 +00003079static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003081 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003083
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003084 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003085
Cong Wang4e58a022013-01-28 19:55:53 +00003086 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003088 if (sig == 0)
3089 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003091
3092 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003093 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003095
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003096 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return sig;
3098}
3099
Cong Wang4e58a022013-01-28 19:55:53 +00003100static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003102 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Joe Perchesf9467ea2010-06-21 12:29:14 +00003104 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003106 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Cong Wang4e58a022013-01-28 19:55:53 +00003108 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003110
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003111 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003113 /* Propagate thread->control */
3114 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003115
Cong Wang4e58a022013-01-28 19:55:53 +00003116 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
Cong Wang4e58a022013-01-28 19:55:53 +00003119static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003120{
3121 struct pktgen_thread *t;
3122
Joe Perchesf9467ea2010-06-21 12:29:14 +00003123 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003124
3125 mutex_lock(&pktgen_thread_lock);
3126
Cong Wang4e58a022013-01-28 19:55:53 +00003127 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003128 t->control |= (T_REMDEVALL);
3129
3130 mutex_unlock(&pktgen_thread_lock);
3131
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003132 /* Propagate thread->control */
3133 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003134
Cong Wang4e58a022013-01-28 19:55:53 +00003135 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003136}
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3139{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003140 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003141 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003142 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3143 pkt_dev->started_at);
3144 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Daniel Turull03a14ab2011-03-09 14:11:00 -08003146 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003147 (unsigned long long)ktime_to_us(elapsed),
3148 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3149 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003150 (unsigned long long)pkt_dev->sofar,
3151 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003153 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3154 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156 bps = pps * 8 * pkt_dev->cur_pkt_size;
3157
3158 mbps = bps;
3159 do_div(mbps, 1000000);
3160 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3161 (unsigned long long)pps,
3162 (unsigned long long)mbps,
3163 (unsigned long long)bps,
3164 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
3167/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003168static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003170 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003171
Luiz Capitulino222f1802006-03-20 22:16:13 -08003172 if (!pkt_dev->running) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07003173 pr_warn("interface: %s is already stopped\n",
3174 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 return -EINVAL;
3176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003178 pkt_dev->running = 0;
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003179 kfree_skb(pkt_dev->skb);
3180 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003181 pkt_dev->stopped_at = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003183 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Luiz Capitulino222f1802006-03-20 22:16:13 -08003185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
3187
Luiz Capitulino222f1802006-03-20 22:16:13 -08003188static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003190 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003191
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003192 rcu_read_lock();
3193 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003194 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195 continue;
3196 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003197 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003198 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003199 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003201 rcu_read_unlock();
3202
Luiz Capitulino222f1802006-03-20 22:16:13 -08003203 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
Luiz Capitulino222f1802006-03-20 22:16:13 -08003206static void pktgen_stop(struct pktgen_thread *t)
3207{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003208 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Joe Perchesf9467ea2010-06-21 12:29:14 +00003210 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003212 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003214 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003215 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003218 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219}
3220
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003221/*
3222 * one of our devices needs to be removed - find it
3223 * and remove it
3224 */
3225static void pktgen_rem_one_if(struct pktgen_thread *t)
3226{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003227 struct list_head *q, *n;
3228 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003229
Joe Perchesf9467ea2010-06-21 12:29:14 +00003230 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003231
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003232 list_for_each_safe(q, n, &t->if_list) {
3233 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003234
Luiz Capitulino222f1802006-03-20 22:16:13 -08003235 if (!cur->removal_mark)
3236 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003237
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003238 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003239 cur->skb = NULL;
3240
3241 pktgen_remove_device(t, cur);
3242
3243 break;
3244 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003245}
3246
Luiz Capitulino222f1802006-03-20 22:16:13 -08003247static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003249 struct list_head *q, *n;
3250 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003251
Joe Perchesf9467ea2010-06-21 12:29:14 +00003252 func_enter();
3253
Luiz Capitulino222f1802006-03-20 22:16:13 -08003254 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003255
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003256 list_for_each_safe(q, n, &t->if_list) {
3257 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003258
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003259 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003260 cur->skb = NULL;
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 pktgen_remove_device(t, cur);
3263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
Luiz Capitulino222f1802006-03-20 22:16:13 -08003266static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003268 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003269 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Stephen Hemmingeref879792009-09-22 19:41:43 +00003272static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003273{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003274 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003275 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003276 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003277}
3278
Stephen Hemmingeref879792009-09-22 19:41:43 +00003279static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3280{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003281 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003282
Reshetova, Elena63354792017-06-30 13:07:58 +03003283 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003284 if (signal_pending(current))
3285 break;
3286
3287 if (need_resched())
3288 pktgen_resched(pkt_dev);
3289 else
3290 cpu_relax();
3291 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003292 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003293}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003294
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003295static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
Mark Rutland6aa7de02017-10-23 14:07:29 -07003297 unsigned int burst = READ_ONCE(pkt_dev->burst);
Stephen Hemminger00829822008-11-20 20:14:53 -08003298 struct net_device *odev = pkt_dev->odev;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003299 struct netdev_queue *txq;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003300 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 int ret;
3302
Stephen Hemmingeref879792009-09-22 19:41:43 +00003303 /* If device is offline, then don't send */
3304 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3305 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003306 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003308
Stephen Hemmingeref879792009-09-22 19:41:43 +00003309 /* This is max DELAY, this has special meaning of
3310 * "never transmit"
3311 */
3312 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003313 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003314 return;
3315 }
3316
3317 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003318 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3319 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3320 /* build a new pkt */
3321 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003322
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003323 pkt_dev->skb = fill_packet(odev, pkt_dev);
3324 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003325 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003326 schedule();
3327 pkt_dev->clone_count--; /* back out increment, OOM */
3328 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003330 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003331 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003333
Stephen Hemmingeref879792009-09-22 19:41:43 +00003334 if (pkt_dev->delay && pkt_dev->last_ok)
3335 spin(pkt_dev, pkt_dev->next_tx);
3336
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003337 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3338 skb = pkt_dev->skb;
3339 skb->protocol = eth_type_trans(skb, skb->dev);
Reshetova, Elena63354792017-06-30 13:07:58 +03003340 refcount_add(burst, &skb->users);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003341 local_bh_disable();
3342 do {
3343 ret = netif_receive_skb(skb);
3344 if (ret == NET_RX_DROP)
3345 pkt_dev->errors++;
3346 pkt_dev->sofar++;
3347 pkt_dev->seq_num++;
Reshetova, Elena63354792017-06-30 13:07:58 +03003348 if (refcount_read(&skb->users) != burst) {
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003349 /* skb was queued by rps/rfs or taps,
3350 * so cannot reuse this skb
3351 */
Reshetova, Elena63354792017-06-30 13:07:58 +03003352 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003353 /* get out of the loop and wait
3354 * until skb is consumed
3355 */
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003356 break;
3357 }
3358 /* skb was 'freed' by stack, so clean few
3359 * bits and reuse it
3360 */
Willem de Bruijna5135bc2017-01-07 17:06:36 -05003361 skb_reset_tc(skb);
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003362 } while (--burst > 0);
3363 goto out; /* Skips xmit_mode M_START_XMIT */
John Fastabend0967f242016-07-02 14:12:54 -07003364 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3365 local_bh_disable();
Reshetova, Elena63354792017-06-30 13:07:58 +03003366 refcount_inc(&pkt_dev->skb->users);
John Fastabend0967f242016-07-02 14:12:54 -07003367
3368 ret = dev_queue_xmit(pkt_dev->skb);
3369 switch (ret) {
3370 case NET_XMIT_SUCCESS:
3371 pkt_dev->sofar++;
3372 pkt_dev->seq_num++;
3373 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3374 break;
3375 case NET_XMIT_DROP:
3376 case NET_XMIT_CN:
3377 /* These are all valid return codes for a qdisc but
3378 * indicate packets are being dropped or will likely
3379 * be dropped soon.
3380 */
3381 case NETDEV_TX_BUSY:
3382 /* qdisc may call dev_hard_start_xmit directly in cases
3383 * where no queues exist e.g. loopback device, virtual
3384 * devices, etc. In this case we need to handle
3385 * NETDEV_TX_ codes.
3386 */
3387 default:
3388 pkt_dev->errors++;
3389 net_info_ratelimited("%s xmit error: %d\n",
3390 pkt_dev->odevname, ret);
3391 break;
3392 }
3393 goto out;
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003394 }
3395
Daniel Borkmann10c51b56232014-08-27 11:11:27 +02003396 txq = skb_get_tx_queue(odev, pkt_dev->skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003397
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003398 local_bh_disable();
3399
3400 HARD_TX_LOCK(odev, txq, smp_processor_id());
Stephen Hemmingeref879792009-09-22 19:41:43 +00003401
Daniel Borkmann6f25cd42014-04-07 17:18:30 +02003402 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003403 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003404 pkt_dev->last_ok = 0;
3405 goto unlock;
3406 }
Reshetova, Elena63354792017-06-30 13:07:58 +03003407 refcount_add(burst, &pkt_dev->skb->users);
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003408
3409xmit_more:
3410 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Stephen Hemmingeref879792009-09-22 19:41:43 +00003412 switch (ret) {
3413 case NETDEV_TX_OK:
Stephen Hemmingeref879792009-09-22 19:41:43 +00003414 pkt_dev->last_ok = 1;
3415 pkt_dev->sofar++;
3416 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003417 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003418 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3419 goto xmit_more;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003420 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003421 case NET_XMIT_DROP:
3422 case NET_XMIT_CN:
John Fastabendf466dba2009-12-23 22:02:57 -08003423 /* skb has been consumed */
3424 pkt_dev->errors++;
3425 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003426 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003427 net_info_ratelimited("%s xmit error: %d\n",
3428 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003429 pkt_dev->errors++;
Gustavo A. R. Silvaf91845d2018-09-13 14:03:20 -05003430 /* fall through */
Stephen Hemmingeref879792009-09-22 19:41:43 +00003431 case NETDEV_TX_BUSY:
3432 /* Retry it next time */
Reshetova, Elena63354792017-06-30 13:07:58 +03003433 refcount_dec(&(pkt_dev->skb->users));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003434 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003435 }
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003436 if (unlikely(burst))
Reshetova, Elena63354792017-06-30 13:07:58 +03003437 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
Eric Dumazet0835acf2009-09-30 13:03:33 +00003438unlock:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003439 HARD_TX_UNLOCK(odev, txq);
3440
Alexei Starovoitov62f64ae2015-05-07 16:35:32 +02003441out:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003442 local_bh_enable();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 /* If pkt_dev->count is zero, then run forever */
3445 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003446 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 /* Done with this */
3449 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003450 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003453/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 * Main loop of the thread goes here
3455 */
3456
David S. Milleree74baa2007-01-01 20:51:53 -08003457static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
3459 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003460 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003461 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
David S. Milleree74baa2007-01-01 20:51:53 -08003464 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003467 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Joe Perchesf9467ea2010-06-21 12:29:14 +00003469 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003471 set_freezable();
3472
David S. Milleree74baa2007-01-01 20:51:53 -08003473 while (!kthread_should_stop()) {
3474 pkt_dev = next_to_run(t);
3475
Stephen Hemmingeref879792009-09-22 19:41:43 +00003476 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003477 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003478 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003479 wait_event_interruptible_timeout(t->queue,
3480 t->control != 0,
3481 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003482 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003483 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003484 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003485
Stephen Hemmingeref879792009-09-22 19:41:43 +00003486 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 pktgen_xmit(pkt_dev);
3488
Stephen Hemmingeref879792009-09-22 19:41:43 +00003489 if (need_resched())
3490 pktgen_resched(pkt_dev);
3491 else
3492 cpu_relax();
3493 }
3494
Luiz Capitulino222f1802006-03-20 22:16:13 -08003495 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 pktgen_stop(t);
3497 t->control &= ~(T_STOP);
3498 }
3499
Luiz Capitulino222f1802006-03-20 22:16:13 -08003500 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 pktgen_run(t);
3502 t->control &= ~(T_RUN);
3503 }
3504
Luiz Capitulino222f1802006-03-20 22:16:13 -08003505 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003507 t->control &= ~(T_REMDEVALL);
3508 }
3509
Luiz Capitulino222f1802006-03-20 22:16:13 -08003510 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003511 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 t->control &= ~(T_REMDEV);
3513 }
3514
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003515 try_to_freeze();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Joe Perchesf9467ea2010-06-21 12:29:14 +00003518 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003519 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Joe Perchesf9467ea2010-06-21 12:29:14 +00003521 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003522 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Joe Perchesf9467ea2010-06-21 12:29:14 +00003524 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003525 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003526
David S. Milleree74baa2007-01-01 20:51:53 -08003527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
Luiz Capitulino222f1802006-03-20 22:16:13 -08003530static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003531 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003533 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003534 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003536 rcu_read_lock();
3537 list_for_each_entry_rcu(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003538 if (strncmp(p->odevname, ifname, len) == 0) {
3539 if (p->odevname[len]) {
3540 if (exact || p->odevname[len] != '@')
3541 continue;
3542 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003543 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003544 break;
3545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003547 rcu_read_unlock();
Joe Perchesf9467ea2010-06-21 12:29:14 +00003548 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003549 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550}
3551
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003552/*
3553 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 */
3555
Luiz Capitulino222f1802006-03-20 22:16:13 -08003556static int add_dev_to_thread(struct pktgen_thread *t,
3557 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
3559 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003561 /* This function cannot be called concurrently, as its called
3562 * under pktgen_thread_lock mutex, but it can run from
3563 * userspace on another CPU than the kthread. The if_lock()
3564 * is used here to sync with concurrent instances of
3565 * _rem_dev_from_if_list() invoked via kthread, which is also
3566 * updating the if_list */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003567 if_lock(t);
3568
3569 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003570 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003571 rv = -EBUSY;
3572 goto out;
3573 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 pkt_dev->running = 0;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003576 pkt_dev->pg_thread = t;
3577 list_add_rcu(&pkt_dev->list, &t->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Luiz Capitulino222f1802006-03-20 22:16:13 -08003579out:
3580 if_unlock(t);
3581 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582}
3583
3584/* Called under thread lock */
3585
Luiz Capitulino222f1802006-03-20 22:16:13 -08003586static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003589 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003590 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 /* We don't allow a device to be on several threads */
3593
Cong Wang4e58a022013-01-28 19:55:53 +00003594 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003595 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003596 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003597 return -EBUSY;
3598 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003599
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003600 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003601 if (!pkt_dev)
3602 return -ENOMEM;
3603
Eric Dumazet593f63b2009-11-23 01:44:37 +00003604 strcpy(pkt_dev->odevname, ifname);
Kees Cookfd7bece2018-06-12 14:27:52 -07003605 pkt_dev->flows = vzalloc_node(array_size(MAX_CFLOWS,
3606 sizeof(struct flow_state)),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003607 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003608 if (pkt_dev->flows == NULL) {
3609 kfree(pkt_dev);
3610 return -ENOMEM;
3611 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003612
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003613 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003614 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003615 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003616 pkt_dev->count = pg_count_d;
3617 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003618 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003619 pkt_dev->udp_src_max = 9;
3620 pkt_dev->udp_dst_min = 9;
3621 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003622 pkt_dev->vlan_p = 0;
3623 pkt_dev->vlan_cfi = 0;
3624 pkt_dev->vlan_id = 0xffff;
3625 pkt_dev->svlan_p = 0;
3626 pkt_dev->svlan_cfi = 0;
3627 pkt_dev->svlan_id = 0xffff;
Alexei Starovoitov38b2cf22014-09-30 17:53:21 -07003628 pkt_dev->burst = 1;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08003629 pkt_dev->node = NUMA_NO_NODE;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003630
Cong Wang4e58a022013-01-28 19:55:53 +00003631 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003632 if (err)
3633 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003634 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3635 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003636
Cong Wang4e58a022013-01-28 19:55:53 +00003637 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003638 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003639 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003640 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003641 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003642 err = -EINVAL;
3643 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003644 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003645#ifdef CONFIG_XFRM
3646 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3647 pkt_dev->ipsproto = IPPROTO_ESP;
Fan Ducf93d47e2014-01-03 11:18:31 +08003648
3649 /* xfrm tunnel mode needs additional dst to extract outter
3650 * ip header protocol/ttl/id field, here creat a phony one.
3651 * instead of looking for a valid rt, which definitely hurting
3652 * performance under such circumstance.
3653 */
3654 pkt_dev->dstops.family = AF_INET;
David Millerb6ca8bd2017-11-28 15:45:44 -05003655 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3656 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3657 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3658 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003659#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003660
3661 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003662out2:
3663 dev_put(pkt_dev->odev);
3664out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003665#ifdef CONFIG_XFRM
3666 free_SAs(pkt_dev);
3667#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003668 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003669 kfree(pkt_dev);
3670 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
Cong Wang4e58a022013-01-28 19:55:53 +00003673static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003675 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003676 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003677 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003679 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3680 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003681 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003682 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003683 return -ENOMEM;
3684 }
3685
Eric Dumazet9a0b1e82016-10-15 17:50:49 +02003686 mutex_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003688
David S. Milleree74baa2007-01-01 20:51:53 -08003689 INIT_LIST_HEAD(&t->if_list);
3690
Cong Wang4e58a022013-01-28 19:55:53 +00003691 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003692 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003693
Eric Dumazet94dcf292011-03-22 16:30:45 -07003694 p = kthread_create_on_node(pktgen_thread_worker,
3695 t,
3696 cpu_to_node(cpu),
3697 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003698 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003699 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003700 list_del(&t->th_list);
3701 kfree(t);
3702 return PTR_ERR(p);
3703 }
3704 kthread_bind(p, cpu);
3705 t->tsk = p;
3706
Cong Wang4e58a022013-01-28 19:55:53 +00003707 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003708 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003709 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003710 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003711 PG_PROC_DIR, t->tsk->comm);
3712 kthread_stop(p);
3713 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003714 kfree(t);
3715 return -EINVAL;
3716 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003717
Cong Wang4e58a022013-01-28 19:55:53 +00003718 t->net = pn;
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003719 get_task_struct(p);
David S. Milleree74baa2007-01-01 20:51:53 -08003720 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003721 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
3723 return 0;
3724}
3725
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003726/*
3727 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003729static void _rem_dev_from_if_list(struct pktgen_thread *t,
3730 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003732 struct list_head *q, *n;
3733 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003735 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003736 list_for_each_safe(q, n, &t->if_list) {
3737 p = list_entry(q, struct pktgen_dev, list);
3738 if (p == pkt_dev)
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003739 list_del_rcu(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003741 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
Luiz Capitulino222f1802006-03-20 22:16:13 -08003744static int pktgen_remove_device(struct pktgen_thread *t,
3745 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003747 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Luiz Capitulino222f1802006-03-20 22:16:13 -08003749 if (pkt_dev->running) {
Joe Perches294a0b7f2014-09-09 21:17:30 -07003750 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003751 pktgen_stop_device(pkt_dev);
3752 }
3753
3754 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
3756 if (pkt_dev->odev) {
3757 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003758 pkt_dev->odev = NULL;
3759 }
3760
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003761 /* Remove proc before if_list entry, because add_device uses
3762 * list to determine if interface already exist, avoid race
3763 * with proc_create_data() */
Markus Elfringef87c5d2014-11-18 20:10:34 +01003764 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003766 /* And update the thread if_list */
3767 _rem_dev_from_if_list(t, pkt_dev);
3768
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003769#ifdef CONFIG_XFRM
3770 free_SAs(pkt_dev);
3771#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003772 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003773 if (pkt_dev->page)
3774 put_page(pkt_dev->page);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003775 kfree_rcu(pkt_dev, rcu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777}
3778
Cong Wang4e58a022013-01-28 19:55:53 +00003779static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
Cong Wang4e58a022013-01-28 19:55:53 +00003781 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003782 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003783 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003784
Cong Wang4e58a022013-01-28 19:55:53 +00003785 pn->net = net;
3786 INIT_LIST_HEAD(&pn->pktgen_threads);
3787 pn->pktgen_exiting = false;
3788 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3789 if (!pn->proc_dir) {
3790 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003791 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003792 }
Cong Wang4e58a022013-01-28 19:55:53 +00003793 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3794 if (pe == NULL) {
3795 pr_err("cannot create %s procfs entry\n", PGCTRL);
3796 ret = -EINVAL;
3797 goto remove;
3798 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003799
John Hawkes670c02c2005-10-13 09:30:31 -07003800 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003801 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Cong Wang4e58a022013-01-28 19:55:53 +00003803 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003804 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003805 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003806 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003807 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003808
Cong Wang4e58a022013-01-28 19:55:53 +00003809 if (list_empty(&pn->pktgen_threads)) {
3810 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003811 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003812 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003813 }
3814
Luiz Capitulino222f1802006-03-20 22:16:13 -08003815 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003816
Cong Wang4e58a022013-01-28 19:55:53 +00003817remove_entry:
3818 remove_proc_entry(PGCTRL, pn->proc_dir);
3819remove:
Gao fengece31ff2013-02-18 01:34:56 +00003820 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003821 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
Cong Wang4e58a022013-01-28 19:55:53 +00003824static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
Cong Wang4e58a022013-01-28 19:55:53 +00003826 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003827 struct pktgen_thread *t;
3828 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003829 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Luiz Capitulino222f1802006-03-20 22:16:13 -08003831 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003832 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Eric Dumazetc57b5462012-05-09 13:29:51 +00003834 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003835 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003836 mutex_unlock(&pktgen_thread_lock);
3837
3838 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003839 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003840 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003841 kthread_stop(t->tsk);
Oleg Nesterov1fbe4b462015-07-08 21:42:13 +02003842 put_task_struct(t->tsk);
David S. Milleree74baa2007-01-01 20:51:53 -08003843 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Cong Wang4e58a022013-01-28 19:55:53 +00003846 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003847 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Cong Wang4e58a022013-01-28 19:55:53 +00003850static struct pernet_operations pg_net_ops = {
3851 .init = pg_net_init,
3852 .exit = pg_net_exit,
3853 .id = &pg_net_id,
3854 .size = sizeof(struct pktgen_net),
3855};
3856
3857static int __init pg_init(void)
3858{
3859 int ret = 0;
3860
3861 pr_info("%s", version);
3862 ret = register_pernet_subsys(&pg_net_ops);
3863 if (ret)
3864 return ret;
3865 ret = register_netdevice_notifier(&pktgen_notifier_block);
3866 if (ret)
3867 unregister_pernet_subsys(&pg_net_ops);
3868
3869 return ret;
3870}
3871
3872static void __exit pg_cleanup(void)
3873{
3874 unregister_netdevice_notifier(&pktgen_notifier_block);
3875 unregister_pernet_subsys(&pg_net_ops);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003876 /* Don't need rcu_barrier() due to use of kfree_rcu() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877}
3878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879module_init(pg_init);
3880module_exit(pg_cleanup);
3881
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003882MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883MODULE_DESCRIPTION("Packet Generator tool");
3884MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003885MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003887MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003889MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003891MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003893MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");