blob: 5c728aaf8d6c75568e66730939d6dac50dadf0d9 [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 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900100 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
102 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
103 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900105 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900108 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800110 *
111 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700113 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700115 * Fixed src_mac command to set source mac of packet to value specified in
116 * command by Adit Ranadive <adit.262@gmail.com>
117 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
Joe Perchesf9467ea2010-06-21 12:29:14 +0000119
120#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <linux/sys.h>
123#include <linux/types.h>
124#include <linux/module.h>
125#include <linux/moduleparam.h>
126#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800127#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#include <linux/sched.h>
129#include <linux/slab.h>
130#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#include <linux/unistd.h>
132#include <linux/string.h>
133#include <linux/ptrace.h>
134#include <linux/errno.h>
135#include <linux/ioport.h>
136#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800137#include <linux/capability.h>
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700138#include <linux/hrtimer.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700139#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#include <linux/delay.h>
141#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800142#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#include <linux/init.h>
144#include <linux/skbuff.h>
145#include <linux/netdevice.h>
146#include <linux/inet.h>
147#include <linux/inetdevice.h>
148#include <linux/rtnetlink.h>
149#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700150#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#include <linux/in.h>
152#include <linux/ip.h>
153#include <linux/ipv6.h>
154#include <linux/udp.h>
155#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700156#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800158#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800159#include <linux/kthread.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700160#include <linux/prefetch.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200161#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#include <net/checksum.h>
163#include <net/ipv6.h>
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200164#include <net/udp.h>
Stephen Rothwell73d94e92013-07-29 16:21:26 +1000165#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700167#ifdef CONFIG_XFRM
168#include <net/xfrm.h>
169#endif
Cong Wang4e58a022013-01-28 19:55:53 +0000170#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#include <asm/byteorder.h>
172#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700173#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000174#include <linux/io.h>
175#include <linux/timex.h>
176#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800178#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Daniel Turull43d28b62010-06-09 22:49:57 +0000180#define VERSION "2.74"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800182#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800183#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Joe Perchesf9467ea2010-06-21 12:29:14 +0000185#define func_enter() pr_debug("entering %s\n", __func__);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800188#define F_IPSRC_RND (1<<0) /* IP-Src Random */
189#define F_IPDST_RND (1<<1) /* IP-Dst Random */
190#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
191#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
192#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
193#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
194#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
195#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800196#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700197#define F_VID_RND (1<<9) /* Random VLAN ID */
198#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700199#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700200#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700201#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700202#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000203#define F_NODE (1<<15) /* Node memory alloc*/
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200204#define F_UDPCSUM (1<<16) /* Include UDP checksum */
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +0200205#define F_NO_TIMESTAMP (1<<17) /* Don't timestamp packets (default TS) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000208#define T_STOP (1<<0) /* Stop run */
209#define T_RUN (1<<1) /* Start run */
210#define T_REMDEVALL (1<<2) /* Remove all devs */
211#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200213/* If lock -- protects updating of if_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define if_lock(t) spin_lock(&(t->if_lock));
215#define if_unlock(t) spin_unlock(&(t->if_lock));
216
217/* Used to help with determining the pkts on receive */
218#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700219#define PG_PROC_DIR "pktgen"
220#define PGCTRL "pgctrl"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222#define MAX_CFLOWS 65536
223
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700224#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
225#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
226
Luiz Capitulino222f1802006-03-20 22:16:13 -0800227struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800228 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800229 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700230#ifdef CONFIG_XFRM
231 struct xfrm_state *x;
232#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700233 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700236/* flow flag bits */
237#define F_INIT (1<<0) /* flow has been initialized */
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /*
241 * Try to keep frequent/infrequent used vars. separated.
242 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800243 struct proc_dir_entry *entry; /* proc file */
244 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000245 struct list_head list; /* chaining in the thread's run-queue */
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200246 struct rcu_head rcu; /* freed by RCU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000248 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800249
250 /* If min != max, then we will either do a linear iteration, or
251 * we will do a random selection from within the range.
252 */
253 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800254 int removal_mark; /* non-zero => the device is marked for
255 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000257 int min_pkt_size;
258 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700259 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800260 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800261 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000262 u64 delay; /* nano-seconds */
263
Luiz Capitulino222f1802006-03-20 22:16:13 -0800264 __u64 count; /* Default No packets to send */
265 __u64 sofar; /* How many pkts we've sent so far */
266 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800267 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Luiz Capitulino222f1802006-03-20 22:16:13 -0800269 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Luiz Capitulino222f1802006-03-20 22:16:13 -0800271 __u64 allocated_skbs;
272 __u32 clone_count;
273 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000274 * Or a failed transmit of some sort?
275 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800276 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000277 ktime_t next_tx;
278 ktime_t started_at;
279 ktime_t stopped_at;
280 u64 idle_acc; /* nano-seconds */
281
Luiz Capitulino222f1802006-03-20 22:16:13 -0800282 __u32 seq_num;
283
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000284 int clone_skb; /*
285 * Use multiple SKBs during packet gen.
286 * If this number is greater than 1, then
287 * that many copies of the same packet will be
288 * sent before a new packet is allocated.
289 * If you want to send 1024 identical packets
290 * before creating a new packet,
291 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800292 */
293
294 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
295 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
296 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
297 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
298
299 struct in6_addr in6_saddr;
300 struct in6_addr in6_daddr;
301 struct in6_addr cur_in6_daddr;
302 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800304 struct in6_addr min_in6_daddr;
305 struct in6_addr max_in6_daddr;
306 struct in6_addr min_in6_saddr;
307 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Luiz Capitulino222f1802006-03-20 22:16:13 -0800309 /* If we're doing ranges, random or incremental, then this
310 * defines the min/max for those ranges.
311 */
Al Viro252e3342006-11-14 20:48:11 -0800312 __be32 saddr_min; /* inclusive, source IP address */
313 __be32 saddr_max; /* exclusive, source IP address */
314 __be32 daddr_min; /* inclusive, dest IP address */
315 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Luiz Capitulino222f1802006-03-20 22:16:13 -0800317 __u16 udp_src_min; /* inclusive, source UDP port */
318 __u16 udp_src_max; /* exclusive, source UDP port */
319 __u16 udp_dst_min; /* inclusive, dest UDP port */
320 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700322 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000323 __u8 tos; /* six MSB of (former) IPv4 TOS
324 are for dscp codepoint */
325 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
326 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700327
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800328 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000329 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800330 __be32 labels[MAX_MPLS_LABELS];
331
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700332 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
333 __u8 vlan_p;
334 __u8 vlan_cfi;
335 __u16 vlan_id; /* 0xffff means no vlan tag */
336
337 __u8 svlan_p;
338 __u8 svlan_cfi;
339 __u16 svlan_id; /* 0xffff means no svlan tag */
340
Luiz Capitulino222f1802006-03-20 22:16:13 -0800341 __u32 src_mac_count; /* How many MACs to iterate through */
342 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Luiz Capitulino222f1802006-03-20 22:16:13 -0800344 unsigned char dst_mac[ETH_ALEN];
345 unsigned char src_mac[ETH_ALEN];
346
347 __u32 cur_dst_mac_offset;
348 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800349 __be32 cur_saddr;
350 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700351 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800352 __u16 cur_udp_dst;
353 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700354 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800355 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800356 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800357
358 __u8 hh[14];
359 /* = {
360 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
361
362 We fill in SRC address later
363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
364 0x08, 0x00
365 };
366 */
367 __u16 pad; /* pad out the hh struct to an even 16 bytes */
368
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000369 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800370 * are transmitting the same one multiple times
371 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000372 struct net_device *odev; /* The out-going device.
373 * Note that the device should have it's
374 * pg_info pointer pointing back to this
375 * device.
376 * Set when the user specifies the out-going
377 * device name (not when the inject is
378 * started as it used to do.)
379 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000380 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000382 unsigned int cflows; /* Concurrent flows (config) */
383 unsigned int lflow; /* Flow length (config) */
384 unsigned int nflows; /* accumulated flows (stats) */
385 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700386
387 u16 queue_map_min;
388 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000389 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000390 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700391
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700392#ifdef CONFIG_XFRM
393 __u8 ipsmode; /* IPSEC mode (config) */
394 __u8 ipsproto; /* IPSEC type (config) */
Fan Dude4aee72014-01-03 11:18:30 +0800395 __u32 spi;
Fan Ducf93d472014-01-03 11:18:31 +0800396 struct dst_entry dst;
397 struct dst_ops dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700398#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800399 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
402struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800403 __be32 pgh_magic;
404 __be32 seq_num;
405 __be32 tv_sec;
406 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407};
408
Cong Wang4e58a022013-01-28 19:55:53 +0000409
410static int pg_net_id __read_mostly;
411
412struct pktgen_net {
413 struct net *net;
414 struct proc_dir_entry *proc_dir;
415 struct list_head pktgen_threads;
416 bool pktgen_exiting;
417};
Eric Dumazet551eaff2010-11-21 10:26:44 -0800418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000420 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800421 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800422 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800423 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800424 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000426 /* Field for thread to receive "posted" events terminate,
427 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800428
429 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int cpu;
431
Luiz Capitulino222f1802006-03-20 22:16:13 -0800432 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700433 struct completion start_done;
Cong Wang4e58a022013-01-28 19:55:53 +0000434 struct pktgen_net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435};
436
437#define REMOVE 1
438#define FIND 0
439
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000440static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000441 "Packet Generator for packet performance testing. "
442 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Luiz Capitulino222f1802006-03-20 22:16:13 -0800444static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
445static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
446static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800447 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
Cong Wang4e58a022013-01-28 19:55:53 +0000449static void pktgen_run_all_threads(struct pktgen_net *pn);
450static void pktgen_reset_all_threads(struct pktgen_net *pn);
451static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000452
Luiz Capitulino222f1802006-03-20 22:16:13 -0800453static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000457static int pg_count_d __read_mostly = 1000;
458static int pg_delay_d __read_mostly;
459static int pg_clone_skb_d __read_mostly;
460static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Luiz Capitulino222fa072006-03-20 22:24:27 -0800462static DEFINE_MUTEX(pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static struct notifier_block pktgen_notifier_block = {
465 .notifier_call = pktgen_device_event,
466};
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900469 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
471 */
472
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700473static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800474{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000475 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000479static ssize_t pgctrl_write(struct file *file, const char __user *buf,
480 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700482 char data[128];
Cong Wang4e58a022013-01-28 19:55:53 +0000483 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Mathias Krause09455742014-02-21 21:38:35 +0100485 if (!capable(CAP_NET_ADMIN))
486 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Mathias Krause20b0c712014-02-21 21:38:34 +0100488 if (count == 0)
489 return -EINVAL;
490
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700491 if (count > sizeof(data))
492 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Mathias Krause09455742014-02-21 21:38:35 +0100494 if (copy_from_user(data, buf, count))
495 return -EFAULT;
496
Mathias Krause20b0c712014-02-21 21:38:34 +0100497 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Luiz Capitulino222f1802006-03-20 22:16:13 -0800499 if (!strcmp(data, "stop"))
Cong Wang4e58a022013-01-28 19:55:53 +0000500 pktgen_stop_all_threads_ifs(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Luiz Capitulino222f1802006-03-20 22:16:13 -0800502 else if (!strcmp(data, "start"))
Cong Wang4e58a022013-01-28 19:55:53 +0000503 pktgen_run_all_threads(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800505 else if (!strcmp(data, "reset"))
Cong Wang4e58a022013-01-28 19:55:53 +0000506 pktgen_reset_all_threads(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800507
Luiz Capitulino222f1802006-03-20 22:16:13 -0800508 else
Joe Perches294a0b72014-09-09 21:17:30 -0700509 pr_warn("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Mathias Krause09455742014-02-21 21:38:35 +0100511 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700514static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Al Virod9dda782013-03-31 18:16:14 -0400516 return single_open(file, pgctrl_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700517}
518
Arjan van de Ven9a321442007-02-12 00:55:35 -0800519static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800520 .owner = THIS_MODULE,
521 .open = pgctrl_open,
522 .read = seq_read,
523 .llseek = seq_lseek,
524 .write = pgctrl_write,
525 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700526};
527
528static int pktgen_if_show(struct seq_file *seq, void *v)
529{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000530 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000531 ktime_t stopped;
532 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Luiz Capitulino222f1802006-03-20 22:16:13 -0800534 seq_printf(seq,
535 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
536 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
537 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Luiz Capitulino222f1802006-03-20 22:16:13 -0800539 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000540 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
541 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000542 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Luiz Capitulino222f1802006-03-20 22:16:13 -0800544 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
545 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Robert Olsson45b270f2007-08-28 15:45:55 -0700547 seq_printf(seq,
548 " queue_map_min: %u queue_map_max: %u\n",
549 pkt_dev->queue_map_min,
550 pkt_dev->queue_map_max);
551
John Fastabend9e50e3a2010-11-16 19:12:28 +0000552 if (pkt_dev->skb_priority)
553 seq_printf(seq, " skb_priority: %u\n",
554 pkt_dev->skb_priority);
555
Luiz Capitulino222f1802006-03-20 22:16:13 -0800556 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800557 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000558 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
559 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
560 &pkt_dev->in6_saddr,
561 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
562 &pkt_dev->in6_daddr,
563 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000564 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800565 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000566 " dst_min: %s dst_max: %s\n",
567 pkt_dev->dst_min, pkt_dev->dst_max);
568 seq_printf(seq,
569 " src_min: %s src_max: %s\n",
570 pkt_dev->src_min, pkt_dev->src_max);
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700573 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Johannes Berge1749612008-10-27 15:59:26 -0700575 seq_printf(seq, "%pM ",
576 is_zero_ether_addr(pkt_dev->src_mac) ?
577 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Thomas Graf97dc48e2014-05-16 23:28:54 +0200579 seq_puts(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700580 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Luiz Capitulino222f1802006-03-20 22:16:13 -0800582 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000583 " udp_src_min: %d udp_src_max: %d"
584 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800585 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
586 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Luiz Capitulino222f1802006-03-20 22:16:13 -0800588 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800589 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700590 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800592 if (pkt_dev->nr_labels) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000593 unsigned int i;
Thomas Graf97dc48e2014-05-16 23:28:54 +0200594 seq_puts(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700595 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800596 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
597 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
598 }
599
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000600 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700601 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000602 pkt_dev->vlan_id, pkt_dev->vlan_p,
603 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700604
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000605 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700606 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000607 pkt_dev->svlan_id, pkt_dev->svlan_p,
608 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700609
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000610 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700611 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700612
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000613 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700614 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700615
Robert Olssone99b99b2010-03-18 22:44:30 +0000616 if (pkt_dev->node >= 0)
617 seq_printf(seq, " node: %d\n", pkt_dev->node);
618
Thomas Graf97dc48e2014-05-16 23:28:54 +0200619 seq_puts(seq, " Flags: ");
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800620
Luiz Capitulino222f1802006-03-20 22:16:13 -0800621 if (pkt_dev->flags & F_IPV6)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200622 seq_puts(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Luiz Capitulino222f1802006-03-20 22:16:13 -0800624 if (pkt_dev->flags & F_IPSRC_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200625 seq_puts(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Luiz Capitulino222f1802006-03-20 22:16:13 -0800627 if (pkt_dev->flags & F_IPDST_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200628 seq_puts(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Luiz Capitulino222f1802006-03-20 22:16:13 -0800630 if (pkt_dev->flags & F_TXSIZE_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200631 seq_puts(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Luiz Capitulino222f1802006-03-20 22:16:13 -0800633 if (pkt_dev->flags & F_UDPSRC_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200634 seq_puts(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Luiz Capitulino222f1802006-03-20 22:16:13 -0800636 if (pkt_dev->flags & F_UDPDST_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200637 seq_puts(seq, "UDPDST_RND ");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800638
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200639 if (pkt_dev->flags & F_UDPCSUM)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200640 seq_puts(seq, "UDPCSUM ");
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200641
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +0200642 if (pkt_dev->flags & F_NO_TIMESTAMP)
643 seq_puts(seq, "NO_TIMESTAMP ");
644
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800645 if (pkt_dev->flags & F_MPLS_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200646 seq_puts(seq, "MPLS_RND ");
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800647
Robert Olsson45b270f2007-08-28 15:45:55 -0700648 if (pkt_dev->flags & F_QUEUE_MAP_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200649 seq_puts(seq, "QUEUE_MAP_RND ");
Robert Olsson45b270f2007-08-28 15:45:55 -0700650
Robert Olssone6fce5b2008-08-07 02:23:01 -0700651 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200652 seq_puts(seq, "QUEUE_MAP_CPU ");
Robert Olssone6fce5b2008-08-07 02:23:01 -0700653
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700654 if (pkt_dev->cflows) {
655 if (pkt_dev->flags & F_FLOW_SEQ)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200656 seq_puts(seq, "FLOW_SEQ "); /*in sequence flows*/
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700657 else
Thomas Graf97dc48e2014-05-16 23:28:54 +0200658 seq_puts(seq, "FLOW_RND ");
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700659 }
660
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700661#ifdef CONFIG_XFRM
Fan Du81013282014-01-03 11:18:33 +0800662 if (pkt_dev->flags & F_IPSEC_ON) {
Thomas Graf97dc48e2014-05-16 23:28:54 +0200663 seq_puts(seq, "IPSEC ");
Fan Du81013282014-01-03 11:18:33 +0800664 if (pkt_dev->spi)
665 seq_printf(seq, "spi:%u", pkt_dev->spi);
666 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700667#endif
668
Luiz Capitulino222f1802006-03-20 22:16:13 -0800669 if (pkt_dev->flags & F_MACSRC_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200670 seq_puts(seq, "MACSRC_RND ");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800671
672 if (pkt_dev->flags & F_MACDST_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200673 seq_puts(seq, "MACDST_RND ");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800674
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700675 if (pkt_dev->flags & F_VID_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200676 seq_puts(seq, "VID_RND ");
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700677
678 if (pkt_dev->flags & F_SVID_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200679 seq_puts(seq, "SVID_RND ");
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700680
Robert Olssone99b99b2010-03-18 22:44:30 +0000681 if (pkt_dev->flags & F_NODE)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200682 seq_puts(seq, "NODE_ALLOC ");
Robert Olssone99b99b2010-03-18 22:44:30 +0000683
Luiz Capitulino222f1802006-03-20 22:16:13 -0800684 seq_puts(seq, "\n");
685
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000686 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000687 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000688 idle = pkt_dev->idle_acc;
689 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800690
691 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000692 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800693 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000694 (unsigned long long)pkt_dev->errors);
695
696 seq_printf(seq,
697 " started: %lluus stopped: %lluus idle: %lluus\n",
698 (unsigned long long) ktime_to_us(pkt_dev->started_at),
699 (unsigned long long) ktime_to_us(stopped),
700 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800701
702 seq_printf(seq,
703 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700704 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
705 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Luiz Capitulino222f1802006-03-20 22:16:13 -0800707 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000708 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
709 &pkt_dev->cur_in6_saddr,
710 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800711 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000712 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
713 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Luiz Capitulino222f1802006-03-20 22:16:13 -0800715 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700716 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Robert Olsson45b270f2007-08-28 15:45:55 -0700718 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
719
Luiz Capitulino222f1802006-03-20 22:16:13 -0800720 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800723 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 else
Thomas Graf97dc48e2014-05-16 23:28:54 +0200725 seq_puts(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800730
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000731static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
732 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800733{
734 int i = 0;
735 *num = 0;
736
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700737 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000738 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800739 char c;
740 *num <<= 4;
741 if (get_user(c, &user_buffer[i]))
742 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000743 value = hex_to_bin(c);
744 if (value >= 0)
745 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800746 else
747 break;
748 }
749 return i;
750}
751
Luiz Capitulino222f1802006-03-20 22:16:13 -0800752static int count_trail_chars(const char __user * user_buffer,
753 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 int i;
756
757 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800758 char c;
759 if (get_user(c, &user_buffer[i]))
760 return -EFAULT;
761 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 case '\"':
763 case '\n':
764 case '\r':
765 case '\t':
766 case ' ':
767 case '=':
768 break;
769 default:
770 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773done:
774 return i;
775}
776
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000777static long num_arg(const char __user *user_buffer, unsigned long maxlen,
778 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000780 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800782
Paul Gortmakerd6182222010-10-18 12:14:44 +0000783 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800784 char c;
785 if (get_user(c, &user_buffer[i]))
786 return -EFAULT;
787 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800789 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 } else
791 break;
792 }
793 return i;
794}
795
Luiz Capitulino222f1802006-03-20 22:16:13 -0800796static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000798 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Paul Gortmakerd6182222010-10-18 12:14:44 +0000800 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800801 char c;
802 if (get_user(c, &user_buffer[i]))
803 return -EFAULT;
804 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 case '\"':
806 case '\n':
807 case '\r':
808 case '\t':
809 case ' ':
810 goto done_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 default:
812 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return i;
817}
818
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800819static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
820{
Eric Dumazet95c96172012-04-15 05:58:06 +0000821 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800822 char c;
823 ssize_t i = 0;
824 int len;
825
826 pkt_dev->nr_labels = 0;
827 do {
828 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700829 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800830 if (len <= 0)
831 return len;
832 pkt_dev->labels[n] = htonl(tmp);
833 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
834 pkt_dev->flags |= F_MPLS_RND;
835 i += len;
836 if (get_user(c, &buffer[i]))
837 return -EFAULT;
838 i++;
839 n++;
840 if (n >= MAX_MPLS_LABELS)
841 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700842 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800843
844 pkt_dev->nr_labels = n;
845 return i;
846}
847
Luiz Capitulino222f1802006-03-20 22:16:13 -0800848static ssize_t pktgen_if_write(struct file *file,
849 const char __user * user_buffer, size_t count,
850 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Joe Perches8a994a72010-07-12 10:50:23 +0000852 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000854 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 char name[16], valstr[32];
856 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800857 char *pg_result = NULL;
858 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800860
861 pg_result = &(pkt_dev->result[0]);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (count < 1) {
Joe Perches294a0b72014-09-09 21:17:30 -0700864 pr_warn("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return -EINVAL;
866 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867
Paul Gortmakerd6182222010-10-18 12:14:44 +0000868 max = count;
869 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800870 if (tmp < 0) {
Joe Perches294a0b72014-09-09 21:17:30 -0700871 pr_warn("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800872 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000874 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* Read variable name */
877
878 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000879 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800880 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800883 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return -EFAULT;
885 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886
887 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889 if (len < 0)
890 return len;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 i += len;
893
894 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000895 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700896 char tb[copy + 1];
897 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700899 tb[copy] = 0;
Joe Perchesf342cda2012-05-16 17:50:41 +0000900 pr_debug("%s,%lu buffer -:%s:-\n",
901 name, (unsigned long)count, tb);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (!strcmp(name, "min_pkt_size")) {
905 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000906 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800910 if (value < 14 + 20 + 8)
911 value = 14 + 20 + 8;
912 if (value != pkt_dev->min_pkt_size) {
913 pkt_dev->min_pkt_size = value;
914 pkt_dev->cur_pkt_size = value;
915 }
916 sprintf(pg_result, "OK: min_pkt_size=%u",
917 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return count;
919 }
920
Luiz Capitulino222f1802006-03-20 22:16:13 -0800921 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000923 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800927 if (value < 14 + 20 + 8)
928 value = 14 + 20 + 8;
929 if (value != pkt_dev->max_pkt_size) {
930 pkt_dev->max_pkt_size = value;
931 pkt_dev->cur_pkt_size = value;
932 }
933 sprintf(pg_result, "OK: max_pkt_size=%u",
934 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return count;
936 }
937
Luiz Capitulino222f1802006-03-20 22:16:13 -0800938 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (!strcmp(name, "pkt_size")) {
941 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000942 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800943 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800946 if (value < 14 + 20 + 8)
947 value = 14 + 20 + 8;
948 if (value != pkt_dev->min_pkt_size) {
949 pkt_dev->min_pkt_size = value;
950 pkt_dev->max_pkt_size = value;
951 pkt_dev->cur_pkt_size = value;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
954 return count;
955 }
956
Luiz Capitulino222f1802006-03-20 22:16:13 -0800957 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000959 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800963 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 sprintf(pg_result, "OK: debug=%u", debug);
965 return count;
966 }
967
Luiz Capitulino222f1802006-03-20 22:16:13 -0800968 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000970 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800971 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 i += len;
974 pkt_dev->nfrags = value;
975 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
976 return count;
977 }
978 if (!strcmp(name, "delay")) {
979 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000980 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800981 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000984 if (value == 0x7FFFFFFF)
985 pkt_dev->delay = ULLONG_MAX;
986 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000987 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000988
989 sprintf(pg_result, "OK: delay=%llu",
990 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return count;
992 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000993 if (!strcmp(name, "rate")) {
994 len = num_arg(&user_buffer[i], 10, &value);
995 if (len < 0)
996 return len;
997
998 i += len;
999 if (!value)
1000 return len;
1001 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1002 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001003 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001004
1005 sprintf(pg_result, "OK: rate=%lu", value);
1006 return count;
1007 }
1008 if (!strcmp(name, "ratep")) {
1009 len = num_arg(&user_buffer[i], 10, &value);
1010 if (len < 0)
1011 return len;
1012
1013 i += len;
1014 if (!value)
1015 return len;
1016 pkt_dev->delay = NSEC_PER_SEC/value;
1017 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001018 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001019
1020 sprintf(pg_result, "OK: rate=%lu", value);
1021 return count;
1022 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001023 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001025 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001026 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 if (value != pkt_dev->udp_src_min) {
1030 pkt_dev->udp_src_min = value;
1031 pkt_dev->cur_udp_src = value;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1034 return count;
1035 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001036 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001038 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001042 if (value != pkt_dev->udp_dst_min) {
1043 pkt_dev->udp_dst_min = value;
1044 pkt_dev->cur_udp_dst = value;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1047 return count;
1048 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001049 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001051 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001055 if (value != pkt_dev->udp_src_max) {
1056 pkt_dev->udp_src_max = value;
1057 pkt_dev->cur_udp_src = value;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1060 return count;
1061 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001062 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001064 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001065 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001068 if (value != pkt_dev->udp_dst_max) {
1069 pkt_dev->udp_dst_max = value;
1070 pkt_dev->cur_udp_dst = value;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1073 return count;
1074 }
1075 if (!strcmp(name, "clone_skb")) {
1076 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001077 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001078 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001079 if ((value > 0) &&
1080 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1081 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 pkt_dev->clone_skb = value;
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1086 return count;
1087 }
1088 if (!strcmp(name, "count")) {
1089 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001090 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001091 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 i += len;
1094 pkt_dev->count = value;
1095 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001096 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return count;
1098 }
1099 if (!strcmp(name, "src_mac_count")) {
1100 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001101 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001102 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 i += len;
1105 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001106 pkt_dev->src_mac_count = value;
1107 pkt_dev->cur_src_mac_offset = 0;
1108 }
1109 sprintf(pg_result, "OK: src_mac_count=%d",
1110 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return count;
1112 }
1113 if (!strcmp(name, "dst_mac_count")) {
1114 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001115 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001116 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 i += len;
1119 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001120 pkt_dev->dst_mac_count = value;
1121 pkt_dev->cur_dst_mac_offset = 0;
1122 }
1123 sprintf(pg_result, "OK: dst_mac_count=%d",
1124 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return count;
1126 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001127 if (!strcmp(name, "node")) {
1128 len = num_arg(&user_buffer[i], 10, &value);
1129 if (len < 0)
1130 return len;
1131
1132 i += len;
1133
1134 if (node_possible(value)) {
1135 pkt_dev->node = value;
1136 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001137 if (pkt_dev->page) {
1138 put_page(pkt_dev->page);
1139 pkt_dev->page = NULL;
1140 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001141 }
1142 else
1143 sprintf(pg_result, "ERROR: node not possible");
1144 return count;
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001147 char f[32];
1148 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001150 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001151 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (copy_from_user(f, &user_buffer[i], len))
1154 return -EFAULT;
1155 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001156 if (strcmp(f, "IPSRC_RND") == 0)
1157 pkt_dev->flags |= F_IPSRC_RND;
1158
1159 else if (strcmp(f, "!IPSRC_RND") == 0)
1160 pkt_dev->flags &= ~F_IPSRC_RND;
1161
1162 else if (strcmp(f, "TXSIZE_RND") == 0)
1163 pkt_dev->flags |= F_TXSIZE_RND;
1164
1165 else if (strcmp(f, "!TXSIZE_RND") == 0)
1166 pkt_dev->flags &= ~F_TXSIZE_RND;
1167
1168 else if (strcmp(f, "IPDST_RND") == 0)
1169 pkt_dev->flags |= F_IPDST_RND;
1170
1171 else if (strcmp(f, "!IPDST_RND") == 0)
1172 pkt_dev->flags &= ~F_IPDST_RND;
1173
1174 else if (strcmp(f, "UDPSRC_RND") == 0)
1175 pkt_dev->flags |= F_UDPSRC_RND;
1176
1177 else if (strcmp(f, "!UDPSRC_RND") == 0)
1178 pkt_dev->flags &= ~F_UDPSRC_RND;
1179
1180 else if (strcmp(f, "UDPDST_RND") == 0)
1181 pkt_dev->flags |= F_UDPDST_RND;
1182
1183 else if (strcmp(f, "!UDPDST_RND") == 0)
1184 pkt_dev->flags &= ~F_UDPDST_RND;
1185
1186 else if (strcmp(f, "MACSRC_RND") == 0)
1187 pkt_dev->flags |= F_MACSRC_RND;
1188
1189 else if (strcmp(f, "!MACSRC_RND") == 0)
1190 pkt_dev->flags &= ~F_MACSRC_RND;
1191
1192 else if (strcmp(f, "MACDST_RND") == 0)
1193 pkt_dev->flags |= F_MACDST_RND;
1194
1195 else if (strcmp(f, "!MACDST_RND") == 0)
1196 pkt_dev->flags &= ~F_MACDST_RND;
1197
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001198 else if (strcmp(f, "MPLS_RND") == 0)
1199 pkt_dev->flags |= F_MPLS_RND;
1200
1201 else if (strcmp(f, "!MPLS_RND") == 0)
1202 pkt_dev->flags &= ~F_MPLS_RND;
1203
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001204 else if (strcmp(f, "VID_RND") == 0)
1205 pkt_dev->flags |= F_VID_RND;
1206
1207 else if (strcmp(f, "!VID_RND") == 0)
1208 pkt_dev->flags &= ~F_VID_RND;
1209
1210 else if (strcmp(f, "SVID_RND") == 0)
1211 pkt_dev->flags |= F_SVID_RND;
1212
1213 else if (strcmp(f, "!SVID_RND") == 0)
1214 pkt_dev->flags &= ~F_SVID_RND;
1215
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001216 else if (strcmp(f, "FLOW_SEQ") == 0)
1217 pkt_dev->flags |= F_FLOW_SEQ;
1218
Robert Olsson45b270f2007-08-28 15:45:55 -07001219 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1220 pkt_dev->flags |= F_QUEUE_MAP_RND;
1221
1222 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1223 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001224
1225 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1226 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1227
1228 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1229 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001230#ifdef CONFIG_XFRM
1231 else if (strcmp(f, "IPSEC") == 0)
1232 pkt_dev->flags |= F_IPSEC_ON;
1233#endif
1234
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001235 else if (strcmp(f, "!IPV6") == 0)
1236 pkt_dev->flags &= ~F_IPV6;
1237
Robert Olssone99b99b2010-03-18 22:44:30 +00001238 else if (strcmp(f, "NODE_ALLOC") == 0)
1239 pkt_dev->flags |= F_NODE;
1240
1241 else if (strcmp(f, "!NODE_ALLOC") == 0)
1242 pkt_dev->flags &= ~F_NODE;
1243
Thomas Grafc26bf4a2013-07-25 18:12:18 +02001244 else if (strcmp(f, "UDPCSUM") == 0)
1245 pkt_dev->flags |= F_UDPCSUM;
1246
1247 else if (strcmp(f, "!UDPCSUM") == 0)
1248 pkt_dev->flags &= ~F_UDPCSUM;
1249
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02001250 else if (strcmp(f, "NO_TIMESTAMP") == 0)
1251 pkt_dev->flags |= F_NO_TIMESTAMP;
1252
Luiz Capitulino222f1802006-03-20 22:16:13 -08001253 else {
1254 sprintf(pg_result,
1255 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1256 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001257 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001258 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1259 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1260 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02001261 "NO_TIMESTAMP, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001262#ifdef CONFIG_XFRM
1263 "IPSEC, "
1264#endif
1265 "NODE_ALLOC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001266 return count;
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1269 return count;
1270 }
1271 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1272 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001273 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001274 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Luiz Capitulino222f1802006-03-20 22:16:13 -08001276 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001278 buf[len] = 0;
1279 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1280 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1281 strncpy(pkt_dev->dst_min, buf, len);
1282 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1283 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1284 }
1285 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001286 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001287 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1289 return count;
1290 }
1291 if (!strcmp(name, "dst_max")) {
1292 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001293 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001294 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Luiz Capitulino222f1802006-03-20 22:16:13 -08001297 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EFAULT;
1299
Luiz Capitulino222f1802006-03-20 22:16:13 -08001300 buf[len] = 0;
1301 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1302 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1303 strncpy(pkt_dev->dst_max, buf, len);
1304 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1305 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1306 }
1307 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001308 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 i += len;
1310 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1311 return count;
1312 }
1313 if (!strcmp(name, "dst6")) {
1314 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001315 if (len < 0)
1316 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 pkt_dev->flags |= F_IPV6;
1319
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Amerigo Wangc468fb12012-10-09 17:48:20 +00001324 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001325 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001327 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001330 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Luiz Capitulino222f1802006-03-20 22:16:13 -08001332 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 sprintf(pg_result, "OK: dst6=%s", buf);
1334 return count;
1335 }
1336 if (!strcmp(name, "dst6_min")) {
1337 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 if (len < 0)
1339 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 pkt_dev->flags |= F_IPV6;
1342
Luiz Capitulino222f1802006-03-20 22:16:13 -08001343 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001345 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Amerigo Wangc468fb12012-10-09 17:48:20 +00001347 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001348 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001350 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001352 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Luiz Capitulino222f1802006-03-20 22:16:13 -08001354 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 sprintf(pg_result, "OK: dst6_min=%s", buf);
1356 return count;
1357 }
1358 if (!strcmp(name, "dst6_max")) {
1359 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001360 if (len < 0)
1361 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 pkt_dev->flags |= F_IPV6;
1364
Luiz Capitulino222f1802006-03-20 22:16:13 -08001365 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001367 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Amerigo Wangc468fb12012-10-09 17:48:20 +00001369 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001370 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Luiz Capitulino222f1802006-03-20 22:16:13 -08001372 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001373 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Luiz Capitulino222f1802006-03-20 22:16:13 -08001375 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 sprintf(pg_result, "OK: dst6_max=%s", buf);
1377 return count;
1378 }
1379 if (!strcmp(name, "src6")) {
1380 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381 if (len < 0)
1382 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 pkt_dev->flags |= F_IPV6;
1385
Luiz Capitulino222f1802006-03-20 22:16:13 -08001386 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001388 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Amerigo Wangc468fb12012-10-09 17:48:20 +00001390 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001391 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001393 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Luiz Capitulino222f1802006-03-20 22:16:13 -08001395 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001396 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001397
1398 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 sprintf(pg_result, "OK: src6=%s", buf);
1400 return count;
1401 }
1402 if (!strcmp(name, "src_min")) {
1403 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001404 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001405 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001406
Luiz Capitulino222f1802006-03-20 22:16:13 -08001407 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001409 buf[len] = 0;
1410 if (strcmp(buf, pkt_dev->src_min) != 0) {
1411 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1412 strncpy(pkt_dev->src_min, buf, len);
1413 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1414 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1415 }
1416 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001417 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 i += len;
1419 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1420 return count;
1421 }
1422 if (!strcmp(name, "src_max")) {
1423 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001424 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001425 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001426
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001429 buf[len] = 0;
1430 if (strcmp(buf, pkt_dev->src_max) != 0) {
1431 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1432 strncpy(pkt_dev->src_max, buf, len);
1433 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1434 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1435 }
1436 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001437 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 i += len;
1439 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1440 return count;
1441 }
1442 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001444 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001445 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001451 if (!mac_pton(valstr, pkt_dev->dst_mac))
1452 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001454 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001455
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001456 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return count;
1458 }
1459 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001461 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001462 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001465 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001468 if (!mac_pton(valstr, pkt_dev->src_mac))
1469 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001470 /* Set up Src MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001471 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001472
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001473 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return count;
1475 }
1476
Luiz Capitulino222f1802006-03-20 22:16:13 -08001477 if (!strcmp(name, "clear_counters")) {
1478 pktgen_clear_counters(pkt_dev);
1479 sprintf(pg_result, "OK: Clearing counters.\n");
1480 return count;
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (!strcmp(name, "flows")) {
1484 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001485 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001486 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 i += len;
1489 if (value > MAX_CFLOWS)
1490 value = MAX_CFLOWS;
1491
1492 pkt_dev->cflows = value;
1493 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1494 return count;
1495 }
Fan Du6bae9192014-01-10 14:39:13 +08001496#ifdef CONFIG_XFRM
Fan Dude4aee72014-01-03 11:18:30 +08001497 if (!strcmp(name, "spi")) {
1498 len = num_arg(&user_buffer[i], 10, &value);
1499 if (len < 0)
1500 return len;
1501
1502 i += len;
1503 pkt_dev->spi = value;
1504 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1505 return count;
1506 }
Fan Du6bae9192014-01-10 14:39:13 +08001507#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (!strcmp(name, "flowlen")) {
1509 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001510 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001511 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 i += len;
1514 pkt_dev->lflow = value;
1515 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1516 return count;
1517 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001518
Robert Olsson45b270f2007-08-28 15:45:55 -07001519 if (!strcmp(name, "queue_map_min")) {
1520 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001521 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001522 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001523
Robert Olsson45b270f2007-08-28 15:45:55 -07001524 i += len;
1525 pkt_dev->queue_map_min = value;
1526 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1527 return count;
1528 }
1529
1530 if (!strcmp(name, "queue_map_max")) {
1531 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001532 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001533 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001534
Robert Olsson45b270f2007-08-28 15:45:55 -07001535 i += len;
1536 pkt_dev->queue_map_max = value;
1537 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1538 return count;
1539 }
1540
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001541 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001542 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001543
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001544 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001545 if (len < 0)
1546 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001547 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001548 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001549 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001550 cnt += sprintf(pg_result + cnt,
1551 "%08x%s", ntohl(pkt_dev->labels[n]),
1552 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001553
1554 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1555 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1556 pkt_dev->svlan_id = 0xffff;
1557
1558 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001559 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001560 }
1561 return count;
1562 }
1563
1564 if (!strcmp(name, "vlan_id")) {
1565 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001566 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001567 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001568
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001569 i += len;
1570 if (value <= 4095) {
1571 pkt_dev->vlan_id = value; /* turn on VLAN */
1572
1573 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001574 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001575
1576 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001577 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001578
1579 pkt_dev->nr_labels = 0; /* turn off MPLS */
1580 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1581 } else {
1582 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1583 pkt_dev->svlan_id = 0xffff;
1584
1585 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001586 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001587 }
1588 return count;
1589 }
1590
1591 if (!strcmp(name, "vlan_p")) {
1592 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001593 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001594 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001595
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001596 i += len;
1597 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1598 pkt_dev->vlan_p = value;
1599 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1600 } else {
1601 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1602 }
1603 return count;
1604 }
1605
1606 if (!strcmp(name, "vlan_cfi")) {
1607 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001608 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001609 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001610
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001611 i += len;
1612 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1613 pkt_dev->vlan_cfi = value;
1614 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1615 } else {
1616 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1617 }
1618 return count;
1619 }
1620
1621 if (!strcmp(name, "svlan_id")) {
1622 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001623 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001624 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001625
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001626 i += len;
1627 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1628 pkt_dev->svlan_id = value; /* turn on SVLAN */
1629
1630 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001631 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001632
1633 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001634 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001635
1636 pkt_dev->nr_labels = 0; /* turn off MPLS */
1637 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1638 } else {
1639 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1640 pkt_dev->svlan_id = 0xffff;
1641
1642 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001643 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001644 }
1645 return count;
1646 }
1647
1648 if (!strcmp(name, "svlan_p")) {
1649 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001650 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001651 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001652
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001653 i += len;
1654 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1655 pkt_dev->svlan_p = value;
1656 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1657 } else {
1658 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1659 }
1660 return count;
1661 }
1662
1663 if (!strcmp(name, "svlan_cfi")) {
1664 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001665 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001666 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001667
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001668 i += len;
1669 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1670 pkt_dev->svlan_cfi = value;
1671 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1672 } else {
1673 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1674 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001675 return count;
1676 }
1677
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001678 if (!strcmp(name, "tos")) {
1679 __u32 tmp_value = 0;
1680 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001681 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001682 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001683
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001684 i += len;
1685 if (len == 2) {
1686 pkt_dev->tos = tmp_value;
1687 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1688 } else {
1689 sprintf(pg_result, "ERROR: tos must be 00-ff");
1690 }
1691 return count;
1692 }
1693
1694 if (!strcmp(name, "traffic_class")) {
1695 __u32 tmp_value = 0;
1696 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001697 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001698 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001699
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001700 i += len;
1701 if (len == 2) {
1702 pkt_dev->traffic_class = tmp_value;
1703 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1704 } else {
1705 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1706 }
1707 return count;
1708 }
1709
John Fastabend9e50e3a2010-11-16 19:12:28 +00001710 if (!strcmp(name, "skb_priority")) {
1711 len = num_arg(&user_buffer[i], 9, &value);
1712 if (len < 0)
1713 return len;
1714
1715 i += len;
1716 pkt_dev->skb_priority = value;
1717 sprintf(pg_result, "OK: skb_priority=%i",
1718 pkt_dev->skb_priority);
1719 return count;
1720 }
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1723 return -EINVAL;
1724}
1725
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001726static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Al Virod9dda782013-03-31 18:16:14 -04001728 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001729}
1730
Arjan van de Ven9a321442007-02-12 00:55:35 -08001731static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001732 .owner = THIS_MODULE,
1733 .open = pktgen_if_open,
1734 .read = seq_read,
1735 .llseek = seq_lseek,
1736 .write = pktgen_if_write,
1737 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001738};
1739
1740static int pktgen_thread_show(struct seq_file *seq, void *v)
1741{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001742 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001743 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001745 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Thomas Graf97dc48e2014-05-16 23:28:54 +02001747 seq_puts(seq, "Running: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001748
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001749 rcu_read_lock();
1750 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001751 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001752 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Thomas Graf97dc48e2014-05-16 23:28:54 +02001754 seq_puts(seq, "\nStopped: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001755
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001756 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001758 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001761 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 else
Thomas Graf97dc48e2014-05-16 23:28:54 +02001763 seq_puts(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001765 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001770static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001771 const char __user * user_buffer,
1772 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Joe Perches8a994a72010-07-12 10:50:23 +00001774 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001775 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001776 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001778 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return -EINVAL;
1783 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001784
Paul Gortmakerd6182222010-10-18 12:14:44 +00001785 max = count;
1786 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001787 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001788 return len;
1789
Paul Gortmakerd6182222010-10-18 12:14:44 +00001790 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* Read variable name */
1793
1794 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001795 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001796 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 memset(name, 0, sizeof(name));
1799 if (copy_from_user(name, &user_buffer[i], len))
1800 return -EFAULT;
1801 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001802
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001806 return len;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 i += len;
1809
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001810 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001811 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001814 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 ret = -EINVAL;
1816 goto out;
1817 }
1818
1819 pg_result = &(t->result[0]);
1820
Luiz Capitulino222f1802006-03-20 22:16:13 -08001821 if (!strcmp(name, "add_device")) {
1822 char f[32];
1823 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001825 if (len < 0) {
1826 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto out;
1828 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return -EFAULT;
1831 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001832 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001833 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001834 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001835 if (!ret) {
1836 ret = count;
1837 sprintf(pg_result, "OK: add_device=%s", f);
1838 } else
1839 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto out;
1841 }
1842
Luiz Capitulino222f1802006-03-20 22:16:13 -08001843 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001844 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001845 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001846 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001847 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001849 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 goto out;
1851 }
1852
Luiz Capitulino222f1802006-03-20 22:16:13 -08001853 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001854 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001855 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 goto out;
1857 }
1858
1859 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001860out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return ret;
1862}
1863
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001864static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Al Virod9dda782013-03-31 18:16:14 -04001866 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Arjan van de Ven9a321442007-02-12 00:55:35 -08001869static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001870 .owner = THIS_MODULE,
1871 .open = pktgen_thread_open,
1872 .read = seq_read,
1873 .llseek = seq_lseek,
1874 .write = pktgen_thread_write,
1875 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001876};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001879static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1880 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 struct pktgen_thread *t;
1883 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001884 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Cong Wang4e58a022013-01-28 19:55:53 +00001886 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001887 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001889 if (remove) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001890 pkt_dev->removal_mark = 1;
1891 t->control |= T_REMDEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 break;
1894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001896 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001899/*
1900 * mark a device for removal
1901 */
Cong Wang4e58a022013-01-28 19:55:53 +00001902static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001905 const int max_tries = 10, msec_per_try = 125;
1906 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001907
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001908 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001909 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001910
Luiz Capitulino222f1802006-03-20 22:16:13 -08001911 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001912
Cong Wang4e58a022013-01-28 19:55:53 +00001913 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001914 if (pkt_dev == NULL)
1915 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001916
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001917 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001918 pr_debug("%s: waiting for %s to disappear....\n",
1919 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001920 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001921 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001922
1923 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001924 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1925 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001926 break;
1927 }
1928
1929 }
1930
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001931 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001932}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001933
Cong Wang4e58a022013-01-28 19:55:53 +00001934static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001935{
1936 struct pktgen_thread *t;
1937
Cong Wang4e58a022013-01-28 19:55:53 +00001938 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001939 struct pktgen_dev *pkt_dev;
1940
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001941 rcu_read_lock();
1942 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001943 if (pkt_dev->odev != dev)
1944 continue;
1945
David Howellsa8ca16e2013-04-12 17:27:28 +01001946 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001947
Alexey Dobriyan29753152009-08-28 23:34:43 -07001948 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00001949 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07001950 &pktgen_if_fops,
1951 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001952 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001953 pr_err("can't move proc entry for '%s'\n",
1954 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001955 break;
1956 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001957 rcu_read_unlock();
Stephen Hemminger39df2322007-03-04 16:11:51 -08001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Luiz Capitulino222f1802006-03-20 22:16:13 -08001961static int pktgen_device_event(struct notifier_block *unused,
1962 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Jiri Pirko351638e2013-05-28 01:30:21 +00001964 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00001965 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Cong Wang4e58a022013-01-28 19:55:53 +00001967 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001968 return NOTIFY_DONE;
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* It is OK that we do not hold the group lock right now,
1971 * as we run under the RTNL lock.
1972 */
1973
1974 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001975 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00001976 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00001980 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 return NOTIFY_DONE;
1985}
1986
Cong Wang4e58a022013-01-28 19:55:53 +00001987static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1988 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001989 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001990{
1991 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001992 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001993
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001994 for (i = 0; ifname[i] != '@'; i++) {
1995 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001996 break;
1997
1998 b[i] = ifname[i];
1999 }
2000 b[i] = 0;
2001
Cong Wang4e58a022013-01-28 19:55:53 +00002002 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07002003}
2004
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006/* Associate pktgen_dev with a device. */
2007
Cong Wang4e58a022013-01-28 19:55:53 +00002008static int pktgen_setup_dev(const struct pktgen_net *pn,
2009 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002010{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002012 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (pkt_dev->odev) {
2016 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002017 pkt_dev->odev = NULL;
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Cong Wang4e58a022013-01-28 19:55:53 +00002020 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002022 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002023 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002027 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002028 err = -EINVAL;
2029 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002030 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002031 err = -ENETDOWN;
2032 } else {
2033 pkt_dev->odev = odev;
2034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002038 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
2041/* Read pkt_dev from the interface and set up internal pktgen_dev
2042 * structure to have the right information to create/send packets
2043 */
2044static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2045{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002046 int ntxq;
2047
Luiz Capitulino222f1802006-03-20 22:16:13 -08002048 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002049 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002050 sprintf(pkt_dev->result,
2051 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2052 return;
2053 }
2054
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002055 /* make sure that we don't pick a non-existing transmit queue */
2056 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002057
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002058 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perches294a0b72014-09-09 21:17:30 -07002059 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2060 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2061 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002062 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002063 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002064 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perches294a0b72014-09-09 21:17:30 -07002065 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2066 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2067 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002068 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002069 }
2070
Luiz Capitulino222f1802006-03-20 22:16:13 -08002071 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002073 if (is_zero_ether_addr(pkt_dev->src_mac))
Joe Perches9ea08b12014-01-20 09:52:19 -08002074 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Luiz Capitulino222f1802006-03-20 22:16:13 -08002076 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08002077 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Luiz Capitulino222f1802006-03-20 22:16:13 -08002079 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002080 int i, set = 0, err = 1;
2081 struct inet6_dev *idev;
2082
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002083 if (pkt_dev->min_pkt_size == 0) {
2084 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2085 + sizeof(struct udphdr)
2086 + sizeof(struct pktgen_hdr)
2087 + pkt_dev->pkt_overhead;
2088 }
2089
Luiz Capitulino222f1802006-03-20 22:16:13 -08002090 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2091 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 set = 1;
2093 break;
2094 }
2095
Luiz Capitulino222f1802006-03-20 22:16:13 -08002096 if (!set) {
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /*
2099 * Use linklevel address if unconfigured.
2100 *
2101 * use ipv6_get_lladdr if/when it's get exported
2102 */
2103
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002104 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002105 idev = __in6_dev_get(pkt_dev->odev);
2106 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 struct inet6_ifaddr *ifp;
2108
2109 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002110 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2111 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002112 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002113 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 err = 0;
2115 break;
2116 }
2117 }
2118 read_unlock_bh(&idev->lock);
2119 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002120 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002121 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002122 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002124 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002125 if (pkt_dev->min_pkt_size == 0) {
2126 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2127 + sizeof(struct udphdr)
2128 + sizeof(struct pktgen_hdr)
2129 + pkt_dev->pkt_overhead;
2130 }
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 pkt_dev->saddr_min = 0;
2133 pkt_dev->saddr_max = 0;
2134 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002135
2136 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002139 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (in_dev) {
2141 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002142 pkt_dev->saddr_min =
2143 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 pkt_dev->saddr_max = pkt_dev->saddr_min;
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002148 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2150 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2151 }
2152
2153 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2154 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2155 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002156 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002157 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2158 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2159 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2160
Luiz Capitulino222f1802006-03-20 22:16:13 -08002161 pkt_dev->cur_dst_mac_offset = 0;
2162 pkt_dev->cur_src_mac_offset = 0;
2163 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2164 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2165 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2166 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 pkt_dev->nflows = 0;
2168}
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002171static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2172{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002173 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002174 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002175 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002176
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002177 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2178 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002179
Daniel Turull43d28b62010-06-09 22:49:57 +00002180 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002181 if (remaining <= 0) {
2182 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002183 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002184 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002185
Daniel Borkmann398f3822012-10-28 08:27:19 +00002186 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002187 if (remaining < 100000) {
2188 /* for small delays (<100us), just loop until limit is reached */
2189 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002190 end_time = ktime_get();
2191 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002192 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002193 /* see do_nanosleep */
2194 hrtimer_init_sleeper(&t, current);
2195 do {
2196 set_current_state(TASK_INTERRUPTIBLE);
2197 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2198 if (!hrtimer_active(&t.timer))
2199 t.task = NULL;
2200
2201 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002204 hrtimer_cancel(&t.timer);
2205 } while (t.task && pkt_dev->running && !signal_pending(current));
2206 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002207 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002209
2210 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002211 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002214static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2215{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002216 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002217 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2218 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2219 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2220}
2221
Stephen Hemminger648fda72009-08-27 13:55:07 +00002222static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002223{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002224 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002225}
2226
2227static inline int f_pick(struct pktgen_dev *pkt_dev)
2228{
2229 int flow = pkt_dev->curfl;
2230
2231 if (pkt_dev->flags & F_FLOW_SEQ) {
2232 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2233 /* reset time */
2234 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002235 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002236 pkt_dev->curfl += 1;
2237 if (pkt_dev->curfl >= pkt_dev->cflows)
2238 pkt_dev->curfl = 0; /*reset */
2239 }
2240 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002241 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002242 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002243
Robert Olsson1211a642008-08-05 18:44:26 -07002244 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002245 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002246 pkt_dev->flows[flow].flags = 0;
2247 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002248 }
2249
2250 return pkt_dev->curfl;
2251}
2252
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002253
2254#ifdef CONFIG_XFRM
2255/* If there was already an IPSEC SA, we keep it as is, else
2256 * we go look for it ...
2257*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002258#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002259static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002260{
2261 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002262 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002263 if (!x) {
Fan Duc4549972014-01-03 11:18:32 +08002264
2265 if (pkt_dev->spi) {
2266 /* We need as quick as possible to find the right SA
2267 * Searching with minimum criteria to archieve this.
2268 */
2269 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2270 } else {
2271 /* slow path: we dont already have xfrm_state */
2272 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
2273 (xfrm_address_t *)&pkt_dev->cur_daddr,
2274 (xfrm_address_t *)&pkt_dev->cur_saddr,
2275 AF_INET,
2276 pkt_dev->ipsmode,
2277 pkt_dev->ipsproto, 0);
2278 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002279 if (x) {
2280 pkt_dev->flows[flow].x = x;
2281 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002282 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002283 }
2284
2285 }
2286}
2287#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002288static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2289{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002290
2291 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2292 pkt_dev->cur_queue_map = smp_processor_id();
2293
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002294 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002295 __u16 t;
2296 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002297 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002298 (pkt_dev->queue_map_max -
2299 pkt_dev->queue_map_min + 1)
2300 + pkt_dev->queue_map_min;
2301 } else {
2302 t = pkt_dev->cur_queue_map + 1;
2303 if (t > pkt_dev->queue_map_max)
2304 t = pkt_dev->queue_map_min;
2305 }
2306 pkt_dev->cur_queue_map = t;
2307 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002308 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 -07002309}
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311/* Increment/randomize headers according to flags and current values
2312 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2313 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002314static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2315{
2316 __u32 imn;
2317 __u32 imx;
2318 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002320 if (pkt_dev->cflows)
2321 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002324 if (pkt_dev->src_mac_count > 1) {
2325 __u32 mc;
2326 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Luiz Capitulino222f1802006-03-20 22:16:13 -08002328 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002329 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002330 else {
2331 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002332 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002333 pkt_dev->src_mac_count)
2334 pkt_dev->cur_src_mac_offset = 0;
2335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Luiz Capitulino222f1802006-03-20 22:16:13 -08002337 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2338 pkt_dev->hh[11] = tmp;
2339 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2340 pkt_dev->hh[10] = tmp;
2341 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2342 pkt_dev->hh[9] = tmp;
2343 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2344 pkt_dev->hh[8] = tmp;
2345 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2346 pkt_dev->hh[7] = tmp;
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Luiz Capitulino222f1802006-03-20 22:16:13 -08002349 /* Deal with Destination MAC */
2350 if (pkt_dev->dst_mac_count > 1) {
2351 __u32 mc;
2352 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Luiz Capitulino222f1802006-03-20 22:16:13 -08002354 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002355 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Luiz Capitulino222f1802006-03-20 22:16:13 -08002357 else {
2358 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002359 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002360 pkt_dev->dst_mac_count) {
2361 pkt_dev->cur_dst_mac_offset = 0;
2362 }
2363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Luiz Capitulino222f1802006-03-20 22:16:13 -08002365 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2366 pkt_dev->hh[5] = tmp;
2367 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2368 pkt_dev->hh[4] = tmp;
2369 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2370 pkt_dev->hh[3] = tmp;
2371 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2372 pkt_dev->hh[2] = tmp;
2373 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2374 pkt_dev->hh[1] = tmp;
2375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002377 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002378 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002379 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002380 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2381 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002382 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002383 htonl(0x000fffff));
2384 }
2385
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002386 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002387 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002388 }
2389
2390 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002391 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002392 }
2393
Luiz Capitulino222f1802006-03-20 22:16:13 -08002394 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2395 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002396 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002397 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2398 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 pkt_dev->cur_udp_src++;
2402 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2403 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002404 }
2405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Luiz Capitulino222f1802006-03-20 22:16:13 -08002407 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2408 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002409 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002410 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2411 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002412 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002414 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002416 }
2417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 if (!(pkt_dev->flags & F_IPV6)) {
2420
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002421 imn = ntohl(pkt_dev->saddr_min);
2422 imx = ntohl(pkt_dev->saddr_max);
2423 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002425 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002426 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 else {
2428 t = ntohl(pkt_dev->cur_saddr);
2429 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002430 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434 pkt_dev->cur_saddr = htonl(t);
2435 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002436
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002437 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2439 } else {
Al Viro252e3342006-11-14 20:48:11 -08002440 imn = ntohl(pkt_dev->daddr_min);
2441 imx = ntohl(pkt_dev->daddr_max);
2442 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002444 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (pkt_dev->flags & F_IPDST_RND) {
2446
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002447 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002448 t = prandom_u32() %
2449 (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002450 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002451 } while (ipv4_is_loopback(s) ||
2452 ipv4_is_multicast(s) ||
2453 ipv4_is_lbcast(s) ||
2454 ipv4_is_zeronet(s) ||
2455 ipv4_is_local_multicast(s));
Al Viro252e3342006-11-14 20:48:11 -08002456 pkt_dev->cur_daddr = s;
2457 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 t = ntohl(pkt_dev->cur_daddr);
2459 t++;
2460 if (t > imx) {
2461 t = imn;
2462 }
2463 pkt_dev->cur_daddr = htonl(t);
2464 }
2465 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002466 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002467 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002468 pkt_dev->flows[flow].cur_daddr =
2469 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002470#ifdef CONFIG_XFRM
2471 if (pkt_dev->flags & F_IPSEC_ON)
2472 get_ipsec_sa(pkt_dev, flow);
2473#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 pkt_dev->nflows++;
2475 }
2476 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002477 } else { /* IPV6 * */
2478
Joe Perches06e30412012-10-18 17:55:31 +00002479 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 int i;
2481
2482 /* Only random destinations yet */
2483
Luiz Capitulino222f1802006-03-20 22:16:13 -08002484 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002486 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002487 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2488 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492
Luiz Capitulino222f1802006-03-20 22:16:13 -08002493 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2494 __u32 t;
2495 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002496 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002497 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2498 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002499 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002501 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002503 }
2504 pkt_dev->cur_pkt_size = t;
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002507 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 pkt_dev->flows[flow].count++;
2510}
2511
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002512
2513#ifdef CONFIG_XFRM
Fengguang Wu5537a052014-01-06 11:00:07 +01002514static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
Fan Ducf93d472014-01-03 11:18:31 +08002515
2516 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2517};
2518
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002519static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2520{
2521 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2522 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002523 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002524
2525 if (!x)
2526 return 0;
2527 /* XXX: we dont support tunnel mode for now until
2528 * we resolve the dst issue */
Fan Ducf93d472014-01-03 11:18:31 +08002529 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002530 return 0;
2531
Fan Ducf93d472014-01-03 11:18:31 +08002532 /* But when user specify an valid SPI, transformation
2533 * supports both transport/tunnel mode + ESP/AH type.
2534 */
2535 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2536 skb->_skb_refdst = (unsigned long)&pkt_dev->dst | SKB_DST_NOREF;
2537
2538 rcu_read_lock_bh();
Herbert Xu13996372007-10-17 21:35:51 -07002539 err = x->outer_mode->output(x, skb);
Fan Ducf93d472014-01-03 11:18:31 +08002540 rcu_read_unlock_bh();
Fan Du6de9ace2014-01-03 11:18:28 +08002541 if (err) {
2542 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002543 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002544 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002545 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002546 if (err) {
2547 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002548 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002549 }
Fan Du0af0a412014-01-03 11:18:27 +08002550 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002551 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002552 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002553 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002554error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002555 return err;
2556}
2557
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002558static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002559{
2560 if (pkt_dev->cflows) {
2561 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002562 int i;
2563 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002564 struct xfrm_state *x = pkt_dev->flows[i].x;
2565 if (x) {
2566 xfrm_state_put(x);
2567 pkt_dev->flows[i].x = NULL;
2568 }
2569 }
2570 }
2571}
2572
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002573static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002574 struct sk_buff *skb, __be16 protocol)
2575{
2576 if (pkt_dev->flags & F_IPSEC_ON) {
2577 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2578 int nhead = 0;
2579 if (x) {
2580 int ret;
2581 __u8 *eth;
fan.du38682042013-12-01 16:28:48 +08002582 struct iphdr *iph;
2583
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002584 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002585 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002586 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2587 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002588 pr_err("Error expanding ipsec packet %d\n",
2589 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002590 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002591 }
2592 }
2593
2594 /* ipsec is not expecting ll header */
2595 skb_pull(skb, ETH_HLEN);
2596 ret = pktgen_output_ipsec(skb, pkt_dev);
2597 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002598 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002599 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002600 }
2601 /* restore ll */
2602 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2603 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002604 *(u16 *) &eth[12] = protocol;
fan.du38682042013-12-01 16:28:48 +08002605
2606 /* Update IPv4 header len as well as checksum value */
2607 iph = ip_hdr(skb);
2608 iph->tot_len = htons(skb->len - ETH_HLEN);
2609 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002610 }
2611 }
2612 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002613err:
2614 kfree_skb(skb);
2615 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002616}
2617#endif
2618
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002619static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2620{
Eric Dumazet95c96172012-04-15 05:58:06 +00002621 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002622 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002623 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002624
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002625 mpls--;
2626 *mpls |= MPLS_STACK_BOTTOM;
2627}
2628
Al Viro0f37c602006-11-03 03:49:56 -08002629static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2630 unsigned int prio)
2631{
2632 return htons(id | (cfi << 12) | (prio << 13));
2633}
2634
Eric Dumazet26ad7872011-01-25 13:26:05 -08002635static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2636 int datalen)
2637{
2638 struct timeval timestamp;
2639 struct pktgen_hdr *pgh;
2640
2641 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2642 datalen -= sizeof(*pgh);
2643
2644 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002645 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002646 } else {
2647 int frags = pkt_dev->nfrags;
2648 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002649 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002650
2651
2652 if (frags > MAX_SKB_FRAGS)
2653 frags = MAX_SKB_FRAGS;
2654 len = datalen - frags * PAGE_SIZE;
2655 if (len > 0) {
2656 memset(skb_put(skb, len), 0, len);
2657 datalen = frags * PAGE_SIZE;
2658 }
2659
2660 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002661 frag_len = (datalen/frags) < PAGE_SIZE ?
2662 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002663 while (datalen > 0) {
2664 if (unlikely(!pkt_dev->page)) {
2665 int node = numa_node_id();
2666
2667 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2668 node = pkt_dev->node;
2669 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2670 if (!pkt_dev->page)
2671 break;
2672 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002673 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002674 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002675 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002676 /*last fragment, fill rest of data*/
2677 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002678 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2679 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002680 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002681 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2682 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2683 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2684 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002685 i++;
2686 skb_shinfo(skb)->nr_frags = i;
2687 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002688 }
2689
2690 /* Stamp the time, and sequence number,
2691 * convert them to network byte order
2692 */
2693 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2694 pgh->seq_num = htonl(pkt_dev->seq_num);
2695
Jesper Dangaard Brouerafb84b622014-08-28 18:14:47 +02002696 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2697 pgh->tv_sec = 0;
2698 pgh->tv_usec = 0;
2699 } else {
2700 do_gettimeofday(&timestamp);
2701 pgh->tv_sec = htonl(timestamp.tv_sec);
2702 pgh->tv_usec = htonl(timestamp.tv_usec);
2703 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002704}
2705
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002706static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2707 struct pktgen_dev *pkt_dev,
2708 unsigned int extralen)
2709{
2710 struct sk_buff *skb = NULL;
2711 unsigned int size = pkt_dev->cur_pkt_size + 64 + extralen +
2712 pkt_dev->pkt_overhead;
2713
2714 if (pkt_dev->flags & F_NODE) {
2715 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2716
2717 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2718 if (likely(skb)) {
2719 skb_reserve(skb, NET_SKB_PAD);
2720 skb->dev = dev;
2721 }
2722 } else {
2723 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2724 }
2725
2726 return skb;
2727}
2728
Luiz Capitulino222f1802006-03-20 22:16:13 -08002729static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2730 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
2732 struct sk_buff *skb = NULL;
2733 __u8 *eth;
2734 struct udphdr *udph;
2735 int datalen, iplen;
2736 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002737 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002738 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002739 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2740 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2741 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2742 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002743 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002744
2745 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002746 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002747
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002748 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002749 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002750
Robert Olsson64053be2005-06-26 15:27:10 -07002751 /* Update any of the values, used when we're incrementing various
2752 * fields.
2753 */
2754 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002755 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002756
David S. Miller7ac54592006-01-18 14:19:10 -08002757 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002758
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002759 skb = pktgen_alloc_skb(odev, pkt_dev, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (!skb) {
2761 sprintf(pkt_dev->result, "No memory");
2762 return NULL;
2763 }
2764
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002765 prefetchw(skb->data);
David S. Miller7ac54592006-01-18 14:19:10 -08002766 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 /* Reserve for ethernet and IP header */
2769 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002770 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2771 if (pkt_dev->nr_labels)
2772 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002773
2774 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002775 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002776 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002777 *svlan_tci = build_tci(pkt_dev->svlan_id,
2778 pkt_dev->svlan_cfi,
2779 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002780 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002781 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002782 }
2783 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002784 *vlan_tci = build_tci(pkt_dev->vlan_id,
2785 pkt_dev->vlan_cfi,
2786 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002787 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002788 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002789 }
2790
Thomas Graf525cebe2013-06-03 11:49:23 +00002791 skb_set_mac_header(skb, 0);
2792 skb_set_network_header(skb, skb->len);
2793 iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2794
2795 skb_set_transport_header(skb, skb->len);
2796 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002797 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002798 skb->priority = pkt_dev->skb_priority;
2799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002801 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002803 /* Eth + IPh + UDPh + mpls */
2804 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002805 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002806 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 udph->source = htons(pkt_dev->cur_udp_src);
2810 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002811 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002812 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 iph->ihl = 5;
2815 iph->version = 4;
2816 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002817 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002818 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 iph->saddr = pkt_dev->cur_saddr;
2820 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002821 iph->id = htons(pkt_dev->ip_id);
2822 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 iph->frag_off = 0;
2824 iplen = 20 + 8 + datalen;
2825 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002826 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002827 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 skb->dev = odev;
2829 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002830
2831 if (!(pkt_dev->flags & F_UDPCSUM)) {
2832 skb->ip_summed = CHECKSUM_NONE;
2833 } else if (odev->features & NETIF_F_V4_CSUM) {
2834 skb->ip_summed = CHECKSUM_PARTIAL;
2835 skb->csum = 0;
2836 udp4_hwcsum(skb, udph->source, udph->dest);
2837 } else {
2838 __wsum csum = udp_csum(skb);
2839
2840 /* add protocol-dependent pseudo-header */
2841 udph->check = csum_tcpudp_magic(udph->source, udph->dest,
2842 datalen + 8, IPPROTO_UDP, csum);
2843
2844 if (udph->check == 0)
2845 udph->check = CSUM_MANGLED_0;
2846 }
2847
Eric Dumazet26ad7872011-01-25 13:26:05 -08002848 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002849
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002850#ifdef CONFIG_XFRM
2851 if (!process_ipsec(pkt_dev, skb, protocol))
2852 return NULL;
2853#endif
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 return skb;
2856}
2857
Luiz Capitulino222f1802006-03-20 22:16:13 -08002858static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2859 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
2861 struct sk_buff *skb = NULL;
2862 __u8 *eth;
2863 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002864 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002866 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002867 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002868 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2869 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2870 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2871 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002872 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002873
2874 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002875 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002876
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002877 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002878 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002879
Robert Olsson64053be2005-06-26 15:27:10 -07002880 /* Update any of the values, used when we're incrementing various
2881 * fields.
2882 */
2883 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002884 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002885
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002886 skb = pktgen_alloc_skb(odev, pkt_dev, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (!skb) {
2888 sprintf(pkt_dev->result, "No memory");
2889 return NULL;
2890 }
2891
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002892 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 skb_reserve(skb, 16);
2894
2895 /* Reserve for ethernet and IP header */
2896 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002897 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2898 if (pkt_dev->nr_labels)
2899 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002900
2901 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002902 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002903 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002904 *svlan_tci = build_tci(pkt_dev->svlan_id,
2905 pkt_dev->svlan_cfi,
2906 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002907 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002908 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002909 }
2910 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002911 *vlan_tci = build_tci(pkt_dev->vlan_id,
2912 pkt_dev->vlan_cfi,
2913 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002914 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002915 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002916 }
2917
Thomas Graf525cebe2013-06-03 11:49:23 +00002918 skb_set_mac_header(skb, 0);
2919 skb_set_network_header(skb, skb->len);
2920 iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2921
2922 skb_set_transport_header(skb, skb->len);
2923 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002924 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002925 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002928 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002929
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002930 /* Eth + IPh + UDPh + mpls */
2931 datalen = pkt_dev->cur_pkt_size - 14 -
2932 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002933 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002935 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002937 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
2939
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002940 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 udph->source = htons(pkt_dev->cur_udp_src);
2942 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002943 udph->len = htons(udplen);
2944 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002946 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002948 if (pkt_dev->traffic_class) {
2949 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002950 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002951 }
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 iph->hop_limit = 32;
2954
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002955 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 iph->nexthdr = IPPROTO_UDP;
2957
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002958 iph->daddr = pkt_dev->cur_in6_daddr;
2959 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002961 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 skb->dev = odev;
2963 skb->pkt_type = PACKET_HOST;
2964
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002965 if (!(pkt_dev->flags & F_UDPCSUM)) {
2966 skb->ip_summed = CHECKSUM_NONE;
2967 } else if (odev->features & NETIF_F_V6_CSUM) {
2968 skb->ip_summed = CHECKSUM_PARTIAL;
2969 skb->csum_start = skb_transport_header(skb) - skb->head;
2970 skb->csum_offset = offsetof(struct udphdr, check);
2971 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2972 } else {
2973 __wsum csum = udp_csum(skb);
2974
2975 /* add protocol-dependent pseudo-header */
2976 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2977
2978 if (udph->check == 0)
2979 udph->check = CSUM_MANGLED_0;
2980 }
2981
Eric Dumazet26ad7872011-01-25 13:26:05 -08002982 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return skb;
2985}
2986
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002987static struct sk_buff *fill_packet(struct net_device *odev,
2988 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002990 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return fill_packet_ipv6(odev, pkt_dev);
2992 else
2993 return fill_packet_ipv4(odev, pkt_dev);
2994}
2995
Luiz Capitulino222f1802006-03-20 22:16:13 -08002996static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002998 pkt_dev->seq_num = 1;
2999 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003001 pkt_dev->tx_bytes = 0;
3002 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
3005/* Set up structure for sending pkts, clear counters */
3006
3007static void pktgen_run(struct pktgen_thread *t)
3008{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003009 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 int started = 0;
3011
Joe Perchesf9467ea2010-06-21 12:29:14 +00003012 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003014 rcu_read_lock();
3015 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 /*
3018 * setup odev and create initial packet.
3019 */
3020 pktgen_setup_inject(pkt_dev);
3021
Luiz Capitulino222f1802006-03-20 22:16:13 -08003022 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 pktgen_clear_counters(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003025 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003026
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003027 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 strcpy(pkt_dev->result, "Starting");
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003030 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003032 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 strcpy(pkt_dev->result, "Error starting");
3034 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003035 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003036 if (started)
3037 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Cong Wang4e58a022013-01-28 19:55:53 +00003040static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003042 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Joe Perchesf9467ea2010-06-21 12:29:14 +00003044 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003046 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003047
Cong Wang4e58a022013-01-28 19:55:53 +00003048 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003049 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003050
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003051 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
Stephen Hemminger648fda72009-08-27 13:55:07 +00003054static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003056 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003058 rcu_read_lock();
3059 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3060 if (pkt_dev->running) {
3061 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003062 return 1;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003063 }
3064 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Luiz Capitulino222f1802006-03-20 22:16:13 -08003068static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003070 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Luiz Capitulino222f1802006-03-20 22:16:13 -08003072 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074 if (signal_pending(current))
3075 goto signal;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003076 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003077 return 1;
3078signal:
3079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080}
3081
Cong Wang4e58a022013-01-28 19:55:53 +00003082static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003084 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003086
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003087 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003088
Cong Wang4e58a022013-01-28 19:55:53 +00003089 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003091 if (sig == 0)
3092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003094
3095 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003096 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003098
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003099 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return sig;
3101}
3102
Cong Wang4e58a022013-01-28 19:55:53 +00003103static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003105 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Joe Perchesf9467ea2010-06-21 12:29:14 +00003107 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003109 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Cong Wang4e58a022013-01-28 19:55:53 +00003111 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003113
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003114 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003116 /* Propagate thread->control */
3117 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003118
Cong Wang4e58a022013-01-28 19:55:53 +00003119 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
Cong Wang4e58a022013-01-28 19:55:53 +00003122static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003123{
3124 struct pktgen_thread *t;
3125
Joe Perchesf9467ea2010-06-21 12:29:14 +00003126 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003127
3128 mutex_lock(&pktgen_thread_lock);
3129
Cong Wang4e58a022013-01-28 19:55:53 +00003130 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003131 t->control |= (T_REMDEVALL);
3132
3133 mutex_unlock(&pktgen_thread_lock);
3134
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003135 /* Propagate thread->control */
3136 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003137
Cong Wang4e58a022013-01-28 19:55:53 +00003138 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003139}
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3142{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003143 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003145 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3146 pkt_dev->started_at);
3147 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Daniel Turull03a14ab2011-03-09 14:11:00 -08003149 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003150 (unsigned long long)ktime_to_us(elapsed),
3151 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3152 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003153 (unsigned long long)pkt_dev->sofar,
3154 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003156 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3157 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 bps = pps * 8 * pkt_dev->cur_pkt_size;
3160
3161 mbps = bps;
3162 do_div(mbps, 1000000);
3163 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3164 (unsigned long long)pps,
3165 (unsigned long long)mbps,
3166 (unsigned long long)bps,
3167 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003173 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003174
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 if (!pkt_dev->running) {
Joe Perches294a0b72014-09-09 21:17:30 -07003176 pr_warn("interface: %s is already stopped\n",
3177 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003178 return -EINVAL;
3179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003181 pkt_dev->running = 0;
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003182 kfree_skb(pkt_dev->skb);
3183 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003184 pkt_dev->stopped_at = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003186 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Luiz Capitulino222f1802006-03-20 22:16:13 -08003188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
Luiz Capitulino222f1802006-03-20 22:16:13 -08003191static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003193 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003194
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003195 rcu_read_lock();
3196 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003197 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003198 continue;
3199 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003200 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003201 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003202 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003204 rcu_read_unlock();
3205
Luiz Capitulino222f1802006-03-20 22:16:13 -08003206 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
Luiz Capitulino222f1802006-03-20 22:16:13 -08003209static void pktgen_stop(struct pktgen_thread *t)
3210{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003211 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Joe Perchesf9467ea2010-06-21 12:29:14 +00003213 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003215 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003217 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003218 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003221 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003224/*
3225 * one of our devices needs to be removed - find it
3226 * and remove it
3227 */
3228static void pktgen_rem_one_if(struct pktgen_thread *t)
3229{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003230 struct list_head *q, *n;
3231 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003232
Joe Perchesf9467ea2010-06-21 12:29:14 +00003233 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003234
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003235 list_for_each_safe(q, n, &t->if_list) {
3236 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003237
Luiz Capitulino222f1802006-03-20 22:16:13 -08003238 if (!cur->removal_mark)
3239 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003240
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003241 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003242 cur->skb = NULL;
3243
3244 pktgen_remove_device(t, cur);
3245
3246 break;
3247 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003248}
3249
Luiz Capitulino222f1802006-03-20 22:16:13 -08003250static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003252 struct list_head *q, *n;
3253 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003254
Joe Perchesf9467ea2010-06-21 12:29:14 +00003255 func_enter();
3256
Luiz Capitulino222f1802006-03-20 22:16:13 -08003257 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003258
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003259 list_for_each_safe(q, n, &t->if_list) {
3260 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003261
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003262 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003263 cur->skb = NULL;
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 pktgen_remove_device(t, cur);
3266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
Luiz Capitulino222f1802006-03-20 22:16:13 -08003269static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003271 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003272 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Stephen Hemmingeref879792009-09-22 19:41:43 +00003275static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003276{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003277 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003278 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003279 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003280}
3281
Stephen Hemmingeref879792009-09-22 19:41:43 +00003282static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3283{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003284 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003285
3286 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3287 if (signal_pending(current))
3288 break;
3289
3290 if (need_resched())
3291 pktgen_resched(pkt_dev);
3292 else
3293 cpu_relax();
3294 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003295 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003296}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003297
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003298static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Stephen Hemminger00829822008-11-20 20:14:53 -08003300 struct net_device *odev = pkt_dev->odev;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003301 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 int ret;
3303
Stephen Hemmingeref879792009-09-22 19:41:43 +00003304 /* If device is offline, then don't send */
3305 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3306 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003307 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003309
Stephen Hemmingeref879792009-09-22 19:41:43 +00003310 /* This is max DELAY, this has special meaning of
3311 * "never transmit"
3312 */
3313 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003314 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003315 return;
3316 }
3317
3318 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003319 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3320 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3321 /* build a new pkt */
3322 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003323
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003324 pkt_dev->skb = fill_packet(odev, pkt_dev);
3325 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003326 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003327 schedule();
3328 pkt_dev->clone_count--; /* back out increment, OOM */
3329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003331 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003332 pkt_dev->allocated_skbs++;
3333 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003335
Stephen Hemmingeref879792009-09-22 19:41:43 +00003336 if (pkt_dev->delay && pkt_dev->last_ok)
3337 spin(pkt_dev, pkt_dev->next_tx);
3338
Daniel Borkmann10c51b56232014-08-27 11:11:27 +02003339 txq = skb_get_tx_queue(odev, pkt_dev->skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003340
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003341 local_bh_disable();
3342
3343 HARD_TX_LOCK(odev, txq, smp_processor_id());
Stephen Hemmingeref879792009-09-22 19:41:43 +00003344
Daniel Borkmann6f25cd42014-04-07 17:18:30 +02003345 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003346 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003347 pkt_dev->last_ok = 0;
3348 goto unlock;
3349 }
3350 atomic_inc(&(pkt_dev->skb->users));
David S. Millerfa2dbdc2014-08-29 21:55:22 -07003351 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Stephen Hemmingeref879792009-09-22 19:41:43 +00003353 switch (ret) {
3354 case NETDEV_TX_OK:
Stephen Hemmingeref879792009-09-22 19:41:43 +00003355 pkt_dev->last_ok = 1;
3356 pkt_dev->sofar++;
3357 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003358 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003359 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003360 case NET_XMIT_DROP:
3361 case NET_XMIT_CN:
3362 case NET_XMIT_POLICED:
3363 /* skb has been consumed */
3364 pkt_dev->errors++;
3365 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003366 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003367 net_info_ratelimited("%s xmit error: %d\n",
3368 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003369 pkt_dev->errors++;
3370 /* fallthru */
3371 case NETDEV_TX_LOCKED:
3372 case NETDEV_TX_BUSY:
3373 /* Retry it next time */
3374 atomic_dec(&(pkt_dev->skb->users));
3375 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003376 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003377unlock:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003378 HARD_TX_UNLOCK(odev, txq);
3379
3380 local_bh_enable();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 /* If pkt_dev->count is zero, then run forever */
3383 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003384 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003385
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 /* Done with this */
3387 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003388 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003391/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 * Main loop of the thread goes here
3393 */
3394
David S. Milleree74baa2007-01-01 20:51:53 -08003395static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
3397 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003398 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
David S. Milleree74baa2007-01-01 20:51:53 -08003402 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003405 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Joe Perchesf9467ea2010-06-21 12:29:14 +00003407 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003409 set_freezable();
3410
Jesper Dangaard Brouerbaac1672014-06-26 13:16:49 +02003411 __set_current_state(TASK_RUNNING);
3412
David S. Milleree74baa2007-01-01 20:51:53 -08003413 while (!kthread_should_stop()) {
3414 pkt_dev = next_to_run(t);
3415
Stephen Hemmingeref879792009-09-22 19:41:43 +00003416 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003417 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003418 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003419 wait_event_interruptible_timeout(t->queue,
3420 t->control != 0,
3421 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003422 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003423 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003424 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003425
Stephen Hemmingeref879792009-09-22 19:41:43 +00003426 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 pktgen_xmit(pkt_dev);
3428
Stephen Hemmingeref879792009-09-22 19:41:43 +00003429 if (need_resched())
3430 pktgen_resched(pkt_dev);
3431 else
3432 cpu_relax();
3433 }
3434
Luiz Capitulino222f1802006-03-20 22:16:13 -08003435 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 pktgen_stop(t);
3437 t->control &= ~(T_STOP);
3438 }
3439
Luiz Capitulino222f1802006-03-20 22:16:13 -08003440 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 pktgen_run(t);
3442 t->control &= ~(T_RUN);
3443 }
3444
Luiz Capitulino222f1802006-03-20 22:16:13 -08003445 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003447 t->control &= ~(T_REMDEVALL);
3448 }
3449
Luiz Capitulino222f1802006-03-20 22:16:13 -08003450 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003451 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 t->control &= ~(T_REMDEV);
3453 }
3454
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003455 try_to_freeze();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003456 }
Jesper Dangaard Brouerbaac1672014-06-26 13:16:49 +02003457 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Joe Perchesf9467ea2010-06-21 12:29:14 +00003459 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003460 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Joe Perchesf9467ea2010-06-21 12:29:14 +00003462 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003463 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Joe Perchesf9467ea2010-06-21 12:29:14 +00003465 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003466 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003467
Eric Dumazet551eaff2010-11-21 10:26:44 -08003468 /* Wait for kthread_stop */
3469 while (!kthread_should_stop()) {
3470 set_current_state(TASK_INTERRUPTIBLE);
3471 schedule();
3472 }
3473 __set_current_state(TASK_RUNNING);
3474
David S. Milleree74baa2007-01-01 20:51:53 -08003475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Luiz Capitulino222f1802006-03-20 22:16:13 -08003478static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003479 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003481 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003482 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003484 rcu_read_lock();
3485 list_for_each_entry_rcu(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003486 if (strncmp(p->odevname, ifname, len) == 0) {
3487 if (p->odevname[len]) {
3488 if (exact || p->odevname[len] != '@')
3489 continue;
3490 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003491 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003492 break;
3493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003495 rcu_read_unlock();
Joe Perchesf9467ea2010-06-21 12:29:14 +00003496 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003497 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003500/*
3501 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 */
3503
Luiz Capitulino222f1802006-03-20 22:16:13 -08003504static int add_dev_to_thread(struct pktgen_thread *t,
3505 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506{
3507 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003509 /* This function cannot be called concurrently, as its called
3510 * under pktgen_thread_lock mutex, but it can run from
3511 * userspace on another CPU than the kthread. The if_lock()
3512 * is used here to sync with concurrent instances of
3513 * _rem_dev_from_if_list() invoked via kthread, which is also
3514 * updating the if_list */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003515 if_lock(t);
3516
3517 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003518 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003519 rv = -EBUSY;
3520 goto out;
3521 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 pkt_dev->running = 0;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003524 pkt_dev->pg_thread = t;
3525 list_add_rcu(&pkt_dev->list, &t->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Luiz Capitulino222f1802006-03-20 22:16:13 -08003527out:
3528 if_unlock(t);
3529 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530}
3531
3532/* Called under thread lock */
3533
Luiz Capitulino222f1802006-03-20 22:16:13 -08003534static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003536 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003537 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003538 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 /* We don't allow a device to be on several threads */
3541
Cong Wang4e58a022013-01-28 19:55:53 +00003542 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003543 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003544 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003545 return -EBUSY;
3546 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003547
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003548 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003549 if (!pkt_dev)
3550 return -ENOMEM;
3551
Eric Dumazet593f63b2009-11-23 01:44:37 +00003552 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003553 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003554 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003555 if (pkt_dev->flows == NULL) {
3556 kfree(pkt_dev);
3557 return -ENOMEM;
3558 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003559
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003560 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003561 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003562 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003563 pkt_dev->count = pg_count_d;
3564 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003565 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003566 pkt_dev->udp_src_max = 9;
3567 pkt_dev->udp_dst_min = 9;
3568 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003569 pkt_dev->vlan_p = 0;
3570 pkt_dev->vlan_cfi = 0;
3571 pkt_dev->vlan_id = 0xffff;
3572 pkt_dev->svlan_p = 0;
3573 pkt_dev->svlan_cfi = 0;
3574 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003575 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003576
Cong Wang4e58a022013-01-28 19:55:53 +00003577 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003578 if (err)
3579 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003580 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3581 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003582
Cong Wang4e58a022013-01-28 19:55:53 +00003583 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003584 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003585 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003586 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003587 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003588 err = -EINVAL;
3589 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003590 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003591#ifdef CONFIG_XFRM
3592 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3593 pkt_dev->ipsproto = IPPROTO_ESP;
Fan Ducf93d472014-01-03 11:18:31 +08003594
3595 /* xfrm tunnel mode needs additional dst to extract outter
3596 * ip header protocol/ttl/id field, here creat a phony one.
3597 * instead of looking for a valid rt, which definitely hurting
3598 * performance under such circumstance.
3599 */
3600 pkt_dev->dstops.family = AF_INET;
3601 pkt_dev->dst.dev = pkt_dev->odev;
3602 dst_init_metrics(&pkt_dev->dst, pktgen_dst_metrics, false);
3603 pkt_dev->dst.child = &pkt_dev->dst;
3604 pkt_dev->dst.ops = &pkt_dev->dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003605#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003606
3607 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003608out2:
3609 dev_put(pkt_dev->odev);
3610out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003611#ifdef CONFIG_XFRM
3612 free_SAs(pkt_dev);
3613#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003614 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003615 kfree(pkt_dev);
3616 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
3618
Cong Wang4e58a022013-01-28 19:55:53 +00003619static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003621 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003622 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003623 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003625 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3626 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003627 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003628 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003629 return -ENOMEM;
3630 }
3631
Luiz Capitulino222f1802006-03-20 22:16:13 -08003632 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003634
David S. Milleree74baa2007-01-01 20:51:53 -08003635 INIT_LIST_HEAD(&t->if_list);
3636
Cong Wang4e58a022013-01-28 19:55:53 +00003637 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003638 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003639
Eric Dumazet94dcf292011-03-22 16:30:45 -07003640 p = kthread_create_on_node(pktgen_thread_worker,
3641 t,
3642 cpu_to_node(cpu),
3643 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003644 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003645 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003646 list_del(&t->th_list);
3647 kfree(t);
3648 return PTR_ERR(p);
3649 }
3650 kthread_bind(p, cpu);
3651 t->tsk = p;
3652
Cong Wang4e58a022013-01-28 19:55:53 +00003653 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003654 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003655 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003656 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003657 PG_PROC_DIR, t->tsk->comm);
3658 kthread_stop(p);
3659 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003660 kfree(t);
3661 return -EINVAL;
3662 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003663
Cong Wang4e58a022013-01-28 19:55:53 +00003664 t->net = pn;
David S. Milleree74baa2007-01-01 20:51:53 -08003665 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003666 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
3668 return 0;
3669}
3670
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003671/*
3672 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003674static void _rem_dev_from_if_list(struct pktgen_thread *t,
3675 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003677 struct list_head *q, *n;
3678 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003680 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003681 list_for_each_safe(q, n, &t->if_list) {
3682 p = list_entry(q, struct pktgen_dev, list);
3683 if (p == pkt_dev)
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003684 list_del_rcu(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003686 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687}
3688
Luiz Capitulino222f1802006-03-20 22:16:13 -08003689static int pktgen_remove_device(struct pktgen_thread *t,
3690 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003692 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Luiz Capitulino222f1802006-03-20 22:16:13 -08003694 if (pkt_dev->running) {
Joe Perches294a0b72014-09-09 21:17:30 -07003695 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003696 pktgen_stop_device(pkt_dev);
3697 }
3698
3699 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
3701 if (pkt_dev->odev) {
3702 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003703 pkt_dev->odev = NULL;
3704 }
3705
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003706 /* Remove proc before if_list entry, because add_device uses
3707 * list to determine if interface already exist, avoid race
3708 * with proc_create_data() */
Stephen Hemminger39df2322007-03-04 16:11:51 -08003709 if (pkt_dev->entry)
David Howellsa8ca16e2013-04-12 17:27:28 +01003710 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003712 /* And update the thread if_list */
3713 _rem_dev_from_if_list(t, pkt_dev);
3714
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003715#ifdef CONFIG_XFRM
3716 free_SAs(pkt_dev);
3717#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003718 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003719 if (pkt_dev->page)
3720 put_page(pkt_dev->page);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003721 kfree_rcu(pkt_dev, rcu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723}
3724
Cong Wang4e58a022013-01-28 19:55:53 +00003725static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
Cong Wang4e58a022013-01-28 19:55:53 +00003727 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003728 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003729 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003730
Cong Wang4e58a022013-01-28 19:55:53 +00003731 pn->net = net;
3732 INIT_LIST_HEAD(&pn->pktgen_threads);
3733 pn->pktgen_exiting = false;
3734 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3735 if (!pn->proc_dir) {
3736 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003737 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003738 }
Cong Wang4e58a022013-01-28 19:55:53 +00003739 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3740 if (pe == NULL) {
3741 pr_err("cannot create %s procfs entry\n", PGCTRL);
3742 ret = -EINVAL;
3743 goto remove;
3744 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003745
John Hawkes670c02c2005-10-13 09:30:31 -07003746 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003747 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Cong Wang4e58a022013-01-28 19:55:53 +00003749 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003750 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003751 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003752 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003753 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003754
Cong Wang4e58a022013-01-28 19:55:53 +00003755 if (list_empty(&pn->pktgen_threads)) {
3756 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003757 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003758 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003759 }
3760
Luiz Capitulino222f1802006-03-20 22:16:13 -08003761 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003762
Cong Wang4e58a022013-01-28 19:55:53 +00003763remove_entry:
3764 remove_proc_entry(PGCTRL, pn->proc_dir);
3765remove:
Gao fengece31ff2013-02-18 01:34:56 +00003766 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003767 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768}
3769
Cong Wang4e58a022013-01-28 19:55:53 +00003770static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
Cong Wang4e58a022013-01-28 19:55:53 +00003772 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003773 struct pktgen_thread *t;
3774 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003775 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Luiz Capitulino222f1802006-03-20 22:16:13 -08003777 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003778 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Eric Dumazetc57b5462012-05-09 13:29:51 +00003780 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003781 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003782 mutex_unlock(&pktgen_thread_lock);
3783
3784 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003785 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003786 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003787 kthread_stop(t->tsk);
3788 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Cong Wang4e58a022013-01-28 19:55:53 +00003791 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003792 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003793}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Cong Wang4e58a022013-01-28 19:55:53 +00003795static struct pernet_operations pg_net_ops = {
3796 .init = pg_net_init,
3797 .exit = pg_net_exit,
3798 .id = &pg_net_id,
3799 .size = sizeof(struct pktgen_net),
3800};
3801
3802static int __init pg_init(void)
3803{
3804 int ret = 0;
3805
3806 pr_info("%s", version);
3807 ret = register_pernet_subsys(&pg_net_ops);
3808 if (ret)
3809 return ret;
3810 ret = register_netdevice_notifier(&pktgen_notifier_block);
3811 if (ret)
3812 unregister_pernet_subsys(&pg_net_ops);
3813
3814 return ret;
3815}
3816
3817static void __exit pg_cleanup(void)
3818{
3819 unregister_netdevice_notifier(&pktgen_notifier_block);
3820 unregister_pernet_subsys(&pg_net_ops);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003821 /* Don't need rcu_barrier() due to use of kfree_rcu() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824module_init(pg_init);
3825module_exit(pg_cleanup);
3826
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003827MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828MODULE_DESCRIPTION("Packet Generator tool");
3829MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003830MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003832MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003834MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003836MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003838MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");