blob: 8bc4ddd375a9d8332bc1c3e023e63c625a1f0072 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
116 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Joe Perchesf9467ea2010-06-21 12:29:14 +0000118
119#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <linux/sys.h>
122#include <linux/types.h>
123#include <linux/module.h>
124#include <linux/moduleparam.h>
125#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800126#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <linux/sched.h>
128#include <linux/slab.h>
129#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#include <linux/unistd.h>
131#include <linux/string.h>
132#include <linux/ptrace.h>
133#include <linux/errno.h>
134#include <linux/ioport.h>
135#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800136#include <linux/capability.h>
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700137#include <linux/hrtimer.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700138#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#include <linux/delay.h>
140#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800141#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#include <linux/init.h>
143#include <linux/skbuff.h>
144#include <linux/netdevice.h>
145#include <linux/inet.h>
146#include <linux/inetdevice.h>
147#include <linux/rtnetlink.h>
148#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700149#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#include <linux/in.h>
151#include <linux/ip.h>
152#include <linux/ipv6.h>
153#include <linux/udp.h>
154#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700155#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800157#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800158#include <linux/kthread.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700159#include <linux/prefetch.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200160#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#include <net/checksum.h>
162#include <net/ipv6.h>
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200163#include <net/udp.h>
Stephen Rothwell73d94e92013-07-29 16:21:26 +1000164#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700166#ifdef CONFIG_XFRM
167#include <net/xfrm.h>
168#endif
Cong Wang4e58a022013-01-28 19:55:53 +0000169#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#include <asm/byteorder.h>
171#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700172#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000173#include <linux/io.h>
174#include <linux/timex.h>
175#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800177#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Daniel Turull43d28b62010-06-09 22:49:57 +0000179#define VERSION "2.74"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800181#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800182#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Joe Perchesf9467ea2010-06-21 12:29:14 +0000184#define func_enter() pr_debug("entering %s\n", __func__);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800187#define F_IPSRC_RND (1<<0) /* IP-Src Random */
188#define F_IPDST_RND (1<<1) /* IP-Dst Random */
189#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
190#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
191#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
192#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
193#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
194#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800195#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700196#define F_VID_RND (1<<9) /* Random VLAN ID */
197#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700198#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700199#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700200#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700201#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000202#define F_NODE (1<<15) /* Node memory alloc*/
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200203#define F_UDPCSUM (1<<16) /* Include UDP checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000206#define T_STOP (1<<0) /* Stop run */
207#define T_RUN (1<<1) /* Start run */
208#define T_REMDEVALL (1<<2) /* Remove all devs */
209#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/* If lock -- can be removed after some work */
212#define if_lock(t) spin_lock(&(t->if_lock));
213#define if_unlock(t) spin_unlock(&(t->if_lock));
214
215/* Used to help with determining the pkts on receive */
216#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700217#define PG_PROC_DIR "pktgen"
218#define PGCTRL "pgctrl"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220#define MAX_CFLOWS 65536
221
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700222#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
223#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
224
Luiz Capitulino222f1802006-03-20 22:16:13 -0800225struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800226 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800227 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700228#ifdef CONFIG_XFRM
229 struct xfrm_state *x;
230#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700231 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700234/* flow flag bits */
235#define F_INIT (1<<0) /* flow has been initialized */
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /*
239 * Try to keep frequent/infrequent used vars. separated.
240 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800241 struct proc_dir_entry *entry; /* proc file */
242 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000243 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000245 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800246
247 /* If min != max, then we will either do a linear iteration, or
248 * we will do a random selection from within the range.
249 */
250 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800251 int removal_mark; /* non-zero => the device is marked for
252 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000254 int min_pkt_size;
255 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700256 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800257 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800258 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000259 u64 delay; /* nano-seconds */
260
Luiz Capitulino222f1802006-03-20 22:16:13 -0800261 __u64 count; /* Default No packets to send */
262 __u64 sofar; /* How many pkts we've sent so far */
263 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800264 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Luiz Capitulino222f1802006-03-20 22:16:13 -0800266 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Luiz Capitulino222f1802006-03-20 22:16:13 -0800268 __u64 allocated_skbs;
269 __u32 clone_count;
270 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000271 * Or a failed transmit of some sort?
272 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800273 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000274 ktime_t next_tx;
275 ktime_t started_at;
276 ktime_t stopped_at;
277 u64 idle_acc; /* nano-seconds */
278
Luiz Capitulino222f1802006-03-20 22:16:13 -0800279 __u32 seq_num;
280
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000281 int clone_skb; /*
282 * Use multiple SKBs during packet gen.
283 * If this number is greater than 1, then
284 * that many copies of the same packet will be
285 * sent before a new packet is allocated.
286 * If you want to send 1024 identical packets
287 * before creating a new packet,
288 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800289 */
290
291 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
292 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
293 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
294 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
295
296 struct in6_addr in6_saddr;
297 struct in6_addr in6_daddr;
298 struct in6_addr cur_in6_daddr;
299 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800301 struct in6_addr min_in6_daddr;
302 struct in6_addr max_in6_daddr;
303 struct in6_addr min_in6_saddr;
304 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Luiz Capitulino222f1802006-03-20 22:16:13 -0800306 /* If we're doing ranges, random or incremental, then this
307 * defines the min/max for those ranges.
308 */
Al Viro252e33462006-11-14 20:48:11 -0800309 __be32 saddr_min; /* inclusive, source IP address */
310 __be32 saddr_max; /* exclusive, source IP address */
311 __be32 daddr_min; /* inclusive, dest IP address */
312 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Luiz Capitulino222f1802006-03-20 22:16:13 -0800314 __u16 udp_src_min; /* inclusive, source UDP port */
315 __u16 udp_src_max; /* exclusive, source UDP port */
316 __u16 udp_dst_min; /* inclusive, dest UDP port */
317 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700319 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000320 __u8 tos; /* six MSB of (former) IPv4 TOS
321 are for dscp codepoint */
322 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
323 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700324
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800325 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000326 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800327 __be32 labels[MAX_MPLS_LABELS];
328
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700329 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
330 __u8 vlan_p;
331 __u8 vlan_cfi;
332 __u16 vlan_id; /* 0xffff means no vlan tag */
333
334 __u8 svlan_p;
335 __u8 svlan_cfi;
336 __u16 svlan_id; /* 0xffff means no svlan tag */
337
Luiz Capitulino222f1802006-03-20 22:16:13 -0800338 __u32 src_mac_count; /* How many MACs to iterate through */
339 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Luiz Capitulino222f1802006-03-20 22:16:13 -0800341 unsigned char dst_mac[ETH_ALEN];
342 unsigned char src_mac[ETH_ALEN];
343
344 __u32 cur_dst_mac_offset;
345 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800346 __be32 cur_saddr;
347 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700348 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800349 __u16 cur_udp_dst;
350 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700351 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800352 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800353 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800354
355 __u8 hh[14];
356 /* = {
357 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
358
359 We fill in SRC address later
360 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
361 0x08, 0x00
362 };
363 */
364 __u16 pad; /* pad out the hh struct to an even 16 bytes */
365
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000366 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800367 * are transmitting the same one multiple times
368 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000369 struct net_device *odev; /* The out-going device.
370 * Note that the device should have it's
371 * pg_info pointer pointing back to this
372 * device.
373 * Set when the user specifies the out-going
374 * device name (not when the inject is
375 * started as it used to do.)
376 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000377 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000379 unsigned int cflows; /* Concurrent flows (config) */
380 unsigned int lflow; /* Flow length (config) */
381 unsigned int nflows; /* accumulated flows (stats) */
382 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700383
384 u16 queue_map_min;
385 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000386 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000387 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700388
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700389#ifdef CONFIG_XFRM
390 __u8 ipsmode; /* IPSEC mode (config) */
391 __u8 ipsproto; /* IPSEC type (config) */
Fan Dude4aee72014-01-03 11:18:30 +0800392 __u32 spi;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700393#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800394 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
397struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800398 __be32 pgh_magic;
399 __be32 seq_num;
400 __be32 tv_sec;
401 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402};
403
Cong Wang4e58a022013-01-28 19:55:53 +0000404
405static int pg_net_id __read_mostly;
406
407struct pktgen_net {
408 struct net *net;
409 struct proc_dir_entry *proc_dir;
410 struct list_head pktgen_threads;
411 bool pktgen_exiting;
412};
Eric Dumazet551eaff2010-11-21 10:26:44 -0800413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000415 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800416 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800417 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800418 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800419 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000421 /* Field for thread to receive "posted" events terminate,
422 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800423
424 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int cpu;
426
Luiz Capitulino222f1802006-03-20 22:16:13 -0800427 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700428 struct completion start_done;
Cong Wang4e58a022013-01-28 19:55:53 +0000429 struct pktgen_net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430};
431
432#define REMOVE 1
433#define FIND 0
434
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000435static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000436 "Packet Generator for packet performance testing. "
437 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Luiz Capitulino222f1802006-03-20 22:16:13 -0800439static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
440static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
441static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800442 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
Cong Wang4e58a022013-01-28 19:55:53 +0000444static void pktgen_run_all_threads(struct pktgen_net *pn);
445static void pktgen_reset_all_threads(struct pktgen_net *pn);
446static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000447
Luiz Capitulino222f1802006-03-20 22:16:13 -0800448static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000452static int pg_count_d __read_mostly = 1000;
453static int pg_delay_d __read_mostly;
454static int pg_clone_skb_d __read_mostly;
455static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Luiz Capitulino222fa072006-03-20 22:24:27 -0800457static DEFINE_MUTEX(pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459static struct notifier_block pktgen_notifier_block = {
460 .notifier_call = pktgen_device_event,
461};
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900464 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *
466 */
467
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700468static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800469{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000470 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000474static ssize_t pgctrl_write(struct file *file, const char __user *buf,
475 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700478 char data[128];
Cong Wang4e58a022013-01-28 19:55:53 +0000479 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Luiz Capitulino222f1802006-03-20 22:16:13 -0800481 if (!capable(CAP_NET_ADMIN)) {
482 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700486 if (count > sizeof(data))
487 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700490 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700491 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800492 }
493 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Luiz Capitulino222f1802006-03-20 22:16:13 -0800495 if (!strcmp(data, "stop"))
Cong Wang4e58a022013-01-28 19:55:53 +0000496 pktgen_stop_all_threads_ifs(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498 else if (!strcmp(data, "start"))
Cong Wang4e58a022013-01-28 19:55:53 +0000499 pktgen_run_all_threads(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800501 else if (!strcmp(data, "reset"))
Cong Wang4e58a022013-01-28 19:55:53 +0000502 pktgen_reset_all_threads(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800503
Luiz Capitulino222f1802006-03-20 22:16:13 -0800504 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000505 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 err = count;
508
Luiz Capitulino222f1802006-03-20 22:16:13 -0800509out:
510 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700513static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Al Virod9dda782013-03-31 18:16:14 -0400515 return single_open(file, pgctrl_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700516}
517
Arjan van de Ven9a321442007-02-12 00:55:35 -0800518static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800519 .owner = THIS_MODULE,
520 .open = pgctrl_open,
521 .read = seq_read,
522 .llseek = seq_lseek,
523 .write = pgctrl_write,
524 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700525};
526
527static int pktgen_if_show(struct seq_file *seq, void *v)
528{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000529 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000530 ktime_t stopped;
531 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Luiz Capitulino222f1802006-03-20 22:16:13 -0800533 seq_printf(seq,
534 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
535 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
536 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Luiz Capitulino222f1802006-03-20 22:16:13 -0800538 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000539 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
540 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000541 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Luiz Capitulino222f1802006-03-20 22:16:13 -0800543 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
544 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Robert Olsson45b270f2007-08-28 15:45:55 -0700546 seq_printf(seq,
547 " queue_map_min: %u queue_map_max: %u\n",
548 pkt_dev->queue_map_min,
549 pkt_dev->queue_map_max);
550
John Fastabend9e50e3a2010-11-16 19:12:28 +0000551 if (pkt_dev->skb_priority)
552 seq_printf(seq, " skb_priority: %u\n",
553 pkt_dev->skb_priority);
554
Luiz Capitulino222f1802006-03-20 22:16:13 -0800555 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800556 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000557 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
558 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
559 &pkt_dev->in6_saddr,
560 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
561 &pkt_dev->in6_daddr,
562 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000563 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800564 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000565 " dst_min: %s dst_max: %s\n",
566 pkt_dev->dst_min, pkt_dev->dst_max);
567 seq_printf(seq,
568 " src_min: %s src_max: %s\n",
569 pkt_dev->src_min, pkt_dev->src_max);
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700572 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Johannes Berge1749612008-10-27 15:59:26 -0700574 seq_printf(seq, "%pM ",
575 is_zero_ether_addr(pkt_dev->src_mac) ?
576 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Luiz Capitulino222f1802006-03-20 22:16:13 -0800578 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700579 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Luiz Capitulino222f1802006-03-20 22:16:13 -0800581 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000582 " udp_src_min: %d udp_src_max: %d"
583 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800584 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
585 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Luiz Capitulino222f1802006-03-20 22:16:13 -0800587 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800588 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700589 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800591 if (pkt_dev->nr_labels) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000592 unsigned int i;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800593 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700594 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800595 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
596 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
597 }
598
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000599 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700600 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000601 pkt_dev->vlan_id, pkt_dev->vlan_p,
602 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700603
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000604 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700605 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000606 pkt_dev->svlan_id, pkt_dev->svlan_p,
607 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700608
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000609 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700610 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700611
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000612 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700613 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700614
Robert Olssone99b99b2010-03-18 22:44:30 +0000615 if (pkt_dev->node >= 0)
616 seq_printf(seq, " node: %d\n", pkt_dev->node);
617
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800618 seq_printf(seq, " Flags: ");
619
Luiz Capitulino222f1802006-03-20 22:16:13 -0800620 if (pkt_dev->flags & F_IPV6)
621 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Luiz Capitulino222f1802006-03-20 22:16:13 -0800623 if (pkt_dev->flags & F_IPSRC_RND)
624 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Luiz Capitulino222f1802006-03-20 22:16:13 -0800626 if (pkt_dev->flags & F_IPDST_RND)
627 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Luiz Capitulino222f1802006-03-20 22:16:13 -0800629 if (pkt_dev->flags & F_TXSIZE_RND)
630 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Luiz Capitulino222f1802006-03-20 22:16:13 -0800632 if (pkt_dev->flags & F_UDPSRC_RND)
633 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Luiz Capitulino222f1802006-03-20 22:16:13 -0800635 if (pkt_dev->flags & F_UDPDST_RND)
636 seq_printf(seq, "UDPDST_RND ");
637
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200638 if (pkt_dev->flags & F_UDPCSUM)
639 seq_printf(seq, "UDPCSUM ");
640
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800641 if (pkt_dev->flags & F_MPLS_RND)
642 seq_printf(seq, "MPLS_RND ");
643
Robert Olsson45b270f2007-08-28 15:45:55 -0700644 if (pkt_dev->flags & F_QUEUE_MAP_RND)
645 seq_printf(seq, "QUEUE_MAP_RND ");
646
Robert Olssone6fce5b2008-08-07 02:23:01 -0700647 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
648 seq_printf(seq, "QUEUE_MAP_CPU ");
649
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700650 if (pkt_dev->cflows) {
651 if (pkt_dev->flags & F_FLOW_SEQ)
652 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
653 else
654 seq_printf(seq, "FLOW_RND ");
655 }
656
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700657#ifdef CONFIG_XFRM
658 if (pkt_dev->flags & F_IPSEC_ON)
659 seq_printf(seq, "IPSEC ");
660#endif
661
Luiz Capitulino222f1802006-03-20 22:16:13 -0800662 if (pkt_dev->flags & F_MACSRC_RND)
663 seq_printf(seq, "MACSRC_RND ");
664
665 if (pkt_dev->flags & F_MACDST_RND)
666 seq_printf(seq, "MACDST_RND ");
667
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700668 if (pkt_dev->flags & F_VID_RND)
669 seq_printf(seq, "VID_RND ");
670
671 if (pkt_dev->flags & F_SVID_RND)
672 seq_printf(seq, "SVID_RND ");
673
Robert Olssone99b99b2010-03-18 22:44:30 +0000674 if (pkt_dev->flags & F_NODE)
675 seq_printf(seq, "NODE_ALLOC ");
676
Luiz Capitulino222f1802006-03-20 22:16:13 -0800677 seq_puts(seq, "\n");
678
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000679 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000680 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000681 idle = pkt_dev->idle_acc;
682 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800683
684 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000685 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800686 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000687 (unsigned long long)pkt_dev->errors);
688
689 seq_printf(seq,
690 " started: %lluus stopped: %lluus idle: %lluus\n",
691 (unsigned long long) ktime_to_us(pkt_dev->started_at),
692 (unsigned long long) ktime_to_us(stopped),
693 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800694
695 seq_printf(seq,
696 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700697 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
698 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Luiz Capitulino222f1802006-03-20 22:16:13 -0800700 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000701 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
702 &pkt_dev->cur_in6_saddr,
703 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800704 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000705 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
706 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Luiz Capitulino222f1802006-03-20 22:16:13 -0800708 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700709 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Robert Olsson45b270f2007-08-28 15:45:55 -0700711 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
712
Luiz Capitulino222f1802006-03-20 22:16:13 -0800713 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800716 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800718 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800723
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000724static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
725 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800726{
727 int i = 0;
728 *num = 0;
729
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700730 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000731 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800732 char c;
733 *num <<= 4;
734 if (get_user(c, &user_buffer[i]))
735 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000736 value = hex_to_bin(c);
737 if (value >= 0)
738 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800739 else
740 break;
741 }
742 return i;
743}
744
Luiz Capitulino222f1802006-03-20 22:16:13 -0800745static int count_trail_chars(const char __user * user_buffer,
746 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 int i;
749
750 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800751 char c;
752 if (get_user(c, &user_buffer[i]))
753 return -EFAULT;
754 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 case '\"':
756 case '\n':
757 case '\r':
758 case '\t':
759 case ' ':
760 case '=':
761 break;
762 default:
763 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766done:
767 return i;
768}
769
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000770static long num_arg(const char __user *user_buffer, unsigned long maxlen,
771 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000773 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800775
Paul Gortmakerd6182222010-10-18 12:14:44 +0000776 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800777 char c;
778 if (get_user(c, &user_buffer[i]))
779 return -EFAULT;
780 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800782 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 } else
784 break;
785 }
786 return i;
787}
788
Luiz Capitulino222f1802006-03-20 22:16:13 -0800789static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000791 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Paul Gortmakerd6182222010-10-18 12:14:44 +0000793 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800794 char c;
795 if (get_user(c, &user_buffer[i]))
796 return -EFAULT;
797 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 case '\"':
799 case '\n':
800 case '\r':
801 case '\t':
802 case ' ':
803 goto done_str;
804 break;
805 default:
806 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return i;
811}
812
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800813static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
814{
Eric Dumazet95c96172012-04-15 05:58:06 +0000815 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800816 char c;
817 ssize_t i = 0;
818 int len;
819
820 pkt_dev->nr_labels = 0;
821 do {
822 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700823 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800824 if (len <= 0)
825 return len;
826 pkt_dev->labels[n] = htonl(tmp);
827 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
828 pkt_dev->flags |= F_MPLS_RND;
829 i += len;
830 if (get_user(c, &buffer[i]))
831 return -EFAULT;
832 i++;
833 n++;
834 if (n >= MAX_MPLS_LABELS)
835 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700836 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800837
838 pkt_dev->nr_labels = n;
839 return i;
840}
841
Luiz Capitulino222f1802006-03-20 22:16:13 -0800842static ssize_t pktgen_if_write(struct file *file,
843 const char __user * user_buffer, size_t count,
844 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Joe Perches8a994a72010-07-12 10:50:23 +0000846 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800847 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000848 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 char name[16], valstr[32];
850 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800851 char *pg_result = NULL;
852 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800854
855 pg_result = &(pkt_dev->result[0]);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000858 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return -EINVAL;
860 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800861
Paul Gortmakerd6182222010-10-18 12:14:44 +0000862 max = count;
863 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800864 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000865 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800866 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000868 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* Read variable name */
871
872 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000873 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800874 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800877 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return -EFAULT;
879 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800880
881 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800883 if (len < 0)
884 return len;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 i += len;
887
888 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000889 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700890 char tb[copy + 1];
891 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700893 tb[copy] = 0;
Joe Perchesf342cda2012-05-16 17:50:41 +0000894 pr_debug("%s,%lu buffer -:%s:-\n",
895 name, (unsigned long)count, tb);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (!strcmp(name, "min_pkt_size")) {
899 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000900 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800901 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 if (value < 14 + 20 + 8)
905 value = 14 + 20 + 8;
906 if (value != pkt_dev->min_pkt_size) {
907 pkt_dev->min_pkt_size = value;
908 pkt_dev->cur_pkt_size = value;
909 }
910 sprintf(pg_result, "OK: min_pkt_size=%u",
911 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return count;
913 }
914
Luiz Capitulino222f1802006-03-20 22:16:13 -0800915 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000917 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800918 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800921 if (value < 14 + 20 + 8)
922 value = 14 + 20 + 8;
923 if (value != pkt_dev->max_pkt_size) {
924 pkt_dev->max_pkt_size = value;
925 pkt_dev->cur_pkt_size = value;
926 }
927 sprintf(pg_result, "OK: max_pkt_size=%u",
928 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return count;
930 }
931
Luiz Capitulino222f1802006-03-20 22:16:13 -0800932 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 if (!strcmp(name, "pkt_size")) {
935 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000936 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800937 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800940 if (value < 14 + 20 + 8)
941 value = 14 + 20 + 8;
942 if (value != pkt_dev->min_pkt_size) {
943 pkt_dev->min_pkt_size = value;
944 pkt_dev->max_pkt_size = value;
945 pkt_dev->cur_pkt_size = value;
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
948 return count;
949 }
950
Luiz Capitulino222f1802006-03-20 22:16:13 -0800951 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000953 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800954 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800957 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 sprintf(pg_result, "OK: debug=%u", debug);
959 return count;
960 }
961
Luiz Capitulino222f1802006-03-20 22:16:13 -0800962 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000964 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800965 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 i += len;
968 pkt_dev->nfrags = value;
969 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
970 return count;
971 }
972 if (!strcmp(name, "delay")) {
973 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000974 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000978 if (value == 0x7FFFFFFF)
979 pkt_dev->delay = ULLONG_MAX;
980 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000981 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000982
983 sprintf(pg_result, "OK: delay=%llu",
984 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return count;
986 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000987 if (!strcmp(name, "rate")) {
988 len = num_arg(&user_buffer[i], 10, &value);
989 if (len < 0)
990 return len;
991
992 i += len;
993 if (!value)
994 return len;
995 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
996 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000997 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000998
999 sprintf(pg_result, "OK: rate=%lu", value);
1000 return count;
1001 }
1002 if (!strcmp(name, "ratep")) {
1003 len = num_arg(&user_buffer[i], 10, &value);
1004 if (len < 0)
1005 return len;
1006
1007 i += len;
1008 if (!value)
1009 return len;
1010 pkt_dev->delay = NSEC_PER_SEC/value;
1011 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001012 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001013
1014 sprintf(pg_result, "OK: rate=%lu", value);
1015 return count;
1016 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001017 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001019 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001020 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001023 if (value != pkt_dev->udp_src_min) {
1024 pkt_dev->udp_src_min = value;
1025 pkt_dev->cur_udp_src = value;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1028 return count;
1029 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001030 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001032 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001033 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001036 if (value != pkt_dev->udp_dst_min) {
1037 pkt_dev->udp_dst_min = value;
1038 pkt_dev->cur_udp_dst = value;
1039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1041 return count;
1042 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001043 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001045 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001046 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001049 if (value != pkt_dev->udp_src_max) {
1050 pkt_dev->udp_src_max = value;
1051 pkt_dev->cur_udp_src = value;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1054 return count;
1055 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001056 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001058 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001059 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001062 if (value != pkt_dev->udp_dst_max) {
1063 pkt_dev->udp_dst_max = value;
1064 pkt_dev->cur_udp_dst = value;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1067 return count;
1068 }
1069 if (!strcmp(name, "clone_skb")) {
1070 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001071 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001072 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001073 if ((value > 0) &&
1074 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1075 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001077 pkt_dev->clone_skb = value;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1080 return count;
1081 }
1082 if (!strcmp(name, "count")) {
1083 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001084 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001085 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 i += len;
1088 pkt_dev->count = value;
1089 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001090 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return count;
1092 }
1093 if (!strcmp(name, "src_mac_count")) {
1094 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001095 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001096 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 i += len;
1099 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001100 pkt_dev->src_mac_count = value;
1101 pkt_dev->cur_src_mac_offset = 0;
1102 }
1103 sprintf(pg_result, "OK: src_mac_count=%d",
1104 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return count;
1106 }
1107 if (!strcmp(name, "dst_mac_count")) {
1108 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001109 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001110 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 i += len;
1113 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001114 pkt_dev->dst_mac_count = value;
1115 pkt_dev->cur_dst_mac_offset = 0;
1116 }
1117 sprintf(pg_result, "OK: dst_mac_count=%d",
1118 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return count;
1120 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001121 if (!strcmp(name, "node")) {
1122 len = num_arg(&user_buffer[i], 10, &value);
1123 if (len < 0)
1124 return len;
1125
1126 i += len;
1127
1128 if (node_possible(value)) {
1129 pkt_dev->node = value;
1130 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001131 if (pkt_dev->page) {
1132 put_page(pkt_dev->page);
1133 pkt_dev->page = NULL;
1134 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001135 }
1136 else
1137 sprintf(pg_result, "ERROR: node not possible");
1138 return count;
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001141 char f[32];
1142 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001144 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001145 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (copy_from_user(f, &user_buffer[i], len))
1148 return -EFAULT;
1149 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001150 if (strcmp(f, "IPSRC_RND") == 0)
1151 pkt_dev->flags |= F_IPSRC_RND;
1152
1153 else if (strcmp(f, "!IPSRC_RND") == 0)
1154 pkt_dev->flags &= ~F_IPSRC_RND;
1155
1156 else if (strcmp(f, "TXSIZE_RND") == 0)
1157 pkt_dev->flags |= F_TXSIZE_RND;
1158
1159 else if (strcmp(f, "!TXSIZE_RND") == 0)
1160 pkt_dev->flags &= ~F_TXSIZE_RND;
1161
1162 else if (strcmp(f, "IPDST_RND") == 0)
1163 pkt_dev->flags |= F_IPDST_RND;
1164
1165 else if (strcmp(f, "!IPDST_RND") == 0)
1166 pkt_dev->flags &= ~F_IPDST_RND;
1167
1168 else if (strcmp(f, "UDPSRC_RND") == 0)
1169 pkt_dev->flags |= F_UDPSRC_RND;
1170
1171 else if (strcmp(f, "!UDPSRC_RND") == 0)
1172 pkt_dev->flags &= ~F_UDPSRC_RND;
1173
1174 else if (strcmp(f, "UDPDST_RND") == 0)
1175 pkt_dev->flags |= F_UDPDST_RND;
1176
1177 else if (strcmp(f, "!UDPDST_RND") == 0)
1178 pkt_dev->flags &= ~F_UDPDST_RND;
1179
1180 else if (strcmp(f, "MACSRC_RND") == 0)
1181 pkt_dev->flags |= F_MACSRC_RND;
1182
1183 else if (strcmp(f, "!MACSRC_RND") == 0)
1184 pkt_dev->flags &= ~F_MACSRC_RND;
1185
1186 else if (strcmp(f, "MACDST_RND") == 0)
1187 pkt_dev->flags |= F_MACDST_RND;
1188
1189 else if (strcmp(f, "!MACDST_RND") == 0)
1190 pkt_dev->flags &= ~F_MACDST_RND;
1191
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001192 else if (strcmp(f, "MPLS_RND") == 0)
1193 pkt_dev->flags |= F_MPLS_RND;
1194
1195 else if (strcmp(f, "!MPLS_RND") == 0)
1196 pkt_dev->flags &= ~F_MPLS_RND;
1197
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001198 else if (strcmp(f, "VID_RND") == 0)
1199 pkt_dev->flags |= F_VID_RND;
1200
1201 else if (strcmp(f, "!VID_RND") == 0)
1202 pkt_dev->flags &= ~F_VID_RND;
1203
1204 else if (strcmp(f, "SVID_RND") == 0)
1205 pkt_dev->flags |= F_SVID_RND;
1206
1207 else if (strcmp(f, "!SVID_RND") == 0)
1208 pkt_dev->flags &= ~F_SVID_RND;
1209
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001210 else if (strcmp(f, "FLOW_SEQ") == 0)
1211 pkt_dev->flags |= F_FLOW_SEQ;
1212
Robert Olsson45b270f2007-08-28 15:45:55 -07001213 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1214 pkt_dev->flags |= F_QUEUE_MAP_RND;
1215
1216 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1217 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001218
1219 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1220 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1221
1222 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1223 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001224#ifdef CONFIG_XFRM
1225 else if (strcmp(f, "IPSEC") == 0)
1226 pkt_dev->flags |= F_IPSEC_ON;
1227#endif
1228
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001229 else if (strcmp(f, "!IPV6") == 0)
1230 pkt_dev->flags &= ~F_IPV6;
1231
Robert Olssone99b99b2010-03-18 22:44:30 +00001232 else if (strcmp(f, "NODE_ALLOC") == 0)
1233 pkt_dev->flags |= F_NODE;
1234
1235 else if (strcmp(f, "!NODE_ALLOC") == 0)
1236 pkt_dev->flags &= ~F_NODE;
1237
Thomas Grafc26bf4a2013-07-25 18:12:18 +02001238 else if (strcmp(f, "UDPCSUM") == 0)
1239 pkt_dev->flags |= F_UDPCSUM;
1240
1241 else if (strcmp(f, "!UDPCSUM") == 0)
1242 pkt_dev->flags &= ~F_UDPCSUM;
1243
Luiz Capitulino222f1802006-03-20 22:16:13 -08001244 else {
1245 sprintf(pg_result,
1246 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1247 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001248 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001249 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001250 return count;
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1253 return count;
1254 }
1255 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1256 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001257 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001258 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 buf[len] = 0;
1263 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1264 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1265 strncpy(pkt_dev->dst_min, buf, len);
1266 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1267 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1268 }
1269 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001270 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001271 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1273 return count;
1274 }
1275 if (!strcmp(name, "dst_max")) {
1276 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001277 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001278 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Luiz Capitulino222f1802006-03-20 22:16:13 -08001281 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -EFAULT;
1283
Luiz Capitulino222f1802006-03-20 22:16:13 -08001284 buf[len] = 0;
1285 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1286 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1287 strncpy(pkt_dev->dst_max, buf, len);
1288 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1289 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1290 }
1291 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001292 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 i += len;
1294 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1295 return count;
1296 }
1297 if (!strcmp(name, "dst6")) {
1298 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001299 if (len < 0)
1300 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 pkt_dev->flags |= F_IPV6;
1303
Luiz Capitulino222f1802006-03-20 22:16:13 -08001304 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Amerigo Wangc468fb12012-10-09 17:48:20 +00001308 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001309 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001311 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Luiz Capitulino222f1802006-03-20 22:16:13 -08001313 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001314 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Luiz Capitulino222f1802006-03-20 22:16:13 -08001316 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 sprintf(pg_result, "OK: dst6=%s", buf);
1318 return count;
1319 }
1320 if (!strcmp(name, "dst6_min")) {
1321 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 if (len < 0)
1323 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 pkt_dev->flags |= F_IPV6;
1326
Luiz Capitulino222f1802006-03-20 22:16:13 -08001327 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Amerigo Wangc468fb12012-10-09 17:48:20 +00001331 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001332 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001334 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001336 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 sprintf(pg_result, "OK: dst6_min=%s", buf);
1340 return count;
1341 }
1342 if (!strcmp(name, "dst6_max")) {
1343 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001344 if (len < 0)
1345 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 pkt_dev->flags |= F_IPV6;
1348
Luiz Capitulino222f1802006-03-20 22:16:13 -08001349 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Amerigo Wangc468fb12012-10-09 17:48:20 +00001353 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001354 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Luiz Capitulino222f1802006-03-20 22:16:13 -08001356 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001357 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 sprintf(pg_result, "OK: dst6_max=%s", buf);
1361 return count;
1362 }
1363 if (!strcmp(name, "src6")) {
1364 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001365 if (len < 0)
1366 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 pkt_dev->flags |= F_IPV6;
1369
Luiz Capitulino222f1802006-03-20 22:16:13 -08001370 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001372 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Amerigo Wangc468fb12012-10-09 17:48:20 +00001374 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001375 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001377 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Luiz Capitulino222f1802006-03-20 22:16:13 -08001379 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001380 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381
1382 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 sprintf(pg_result, "OK: src6=%s", buf);
1384 return count;
1385 }
1386 if (!strcmp(name, "src_min")) {
1387 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001388 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001389 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001390
Luiz Capitulino222f1802006-03-20 22:16:13 -08001391 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 buf[len] = 0;
1394 if (strcmp(buf, pkt_dev->src_min) != 0) {
1395 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1396 strncpy(pkt_dev->src_min, buf, len);
1397 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1398 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1399 }
1400 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001401 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 i += len;
1403 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1404 return count;
1405 }
1406 if (!strcmp(name, "src_max")) {
1407 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001408 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001409 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001410
Luiz Capitulino222f1802006-03-20 22:16:13 -08001411 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001413 buf[len] = 0;
1414 if (strcmp(buf, pkt_dev->src_max) != 0) {
1415 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1416 strncpy(pkt_dev->src_max, buf, len);
1417 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1418 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1419 }
1420 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001421 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 i += len;
1423 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1424 return count;
1425 }
1426 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001428 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001429 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001432 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001435 if (!mac_pton(valstr, pkt_dev->dst_mac))
1436 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /* Set up Dest MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001438 memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001439
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001440 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return count;
1442 }
1443 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001445 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001446 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001449 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001452 if (!mac_pton(valstr, pkt_dev->src_mac))
1453 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001454 /* Set up Src MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001455 memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001456
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001457 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return count;
1459 }
1460
Luiz Capitulino222f1802006-03-20 22:16:13 -08001461 if (!strcmp(name, "clear_counters")) {
1462 pktgen_clear_counters(pkt_dev);
1463 sprintf(pg_result, "OK: Clearing counters.\n");
1464 return count;
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (!strcmp(name, "flows")) {
1468 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001469 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001470 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 i += len;
1473 if (value > MAX_CFLOWS)
1474 value = MAX_CFLOWS;
1475
1476 pkt_dev->cflows = value;
1477 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1478 return count;
1479 }
1480
Fan Dude4aee72014-01-03 11:18:30 +08001481 if (!strcmp(name, "spi")) {
1482 len = num_arg(&user_buffer[i], 10, &value);
1483 if (len < 0)
1484 return len;
1485
1486 i += len;
1487 pkt_dev->spi = value;
1488 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1489 return count;
1490 }
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (!strcmp(name, "flowlen")) {
1493 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001494 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001495 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 i += len;
1498 pkt_dev->lflow = value;
1499 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1500 return count;
1501 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001502
Robert Olsson45b270f2007-08-28 15:45:55 -07001503 if (!strcmp(name, "queue_map_min")) {
1504 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001505 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001506 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001507
Robert Olsson45b270f2007-08-28 15:45:55 -07001508 i += len;
1509 pkt_dev->queue_map_min = value;
1510 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1511 return count;
1512 }
1513
1514 if (!strcmp(name, "queue_map_max")) {
1515 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001516 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001517 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001518
Robert Olsson45b270f2007-08-28 15:45:55 -07001519 i += len;
1520 pkt_dev->queue_map_max = value;
1521 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1522 return count;
1523 }
1524
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001525 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001526 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001527
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001528 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001529 if (len < 0)
1530 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001531 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001532 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001533 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001534 cnt += sprintf(pg_result + cnt,
1535 "%08x%s", ntohl(pkt_dev->labels[n]),
1536 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001537
1538 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1539 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1540 pkt_dev->svlan_id = 0xffff;
1541
1542 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001543 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001544 }
1545 return count;
1546 }
1547
1548 if (!strcmp(name, "vlan_id")) {
1549 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001550 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001551 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001552
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001553 i += len;
1554 if (value <= 4095) {
1555 pkt_dev->vlan_id = value; /* turn on VLAN */
1556
1557 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001558 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001559
1560 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001561 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001562
1563 pkt_dev->nr_labels = 0; /* turn off MPLS */
1564 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1565 } else {
1566 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1567 pkt_dev->svlan_id = 0xffff;
1568
1569 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001570 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001571 }
1572 return count;
1573 }
1574
1575 if (!strcmp(name, "vlan_p")) {
1576 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001577 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001578 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001579
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001580 i += len;
1581 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1582 pkt_dev->vlan_p = value;
1583 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1584 } else {
1585 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1586 }
1587 return count;
1588 }
1589
1590 if (!strcmp(name, "vlan_cfi")) {
1591 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001592 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001593 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001594
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001595 i += len;
1596 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1597 pkt_dev->vlan_cfi = value;
1598 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1599 } else {
1600 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1601 }
1602 return count;
1603 }
1604
1605 if (!strcmp(name, "svlan_id")) {
1606 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001607 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001608 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001609
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001610 i += len;
1611 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1612 pkt_dev->svlan_id = value; /* turn on SVLAN */
1613
1614 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001615 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001616
1617 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001618 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001619
1620 pkt_dev->nr_labels = 0; /* turn off MPLS */
1621 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1622 } else {
1623 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1624 pkt_dev->svlan_id = 0xffff;
1625
1626 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001627 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001628 }
1629 return count;
1630 }
1631
1632 if (!strcmp(name, "svlan_p")) {
1633 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001634 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001635 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001636
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001637 i += len;
1638 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1639 pkt_dev->svlan_p = value;
1640 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1641 } else {
1642 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1643 }
1644 return count;
1645 }
1646
1647 if (!strcmp(name, "svlan_cfi")) {
1648 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001649 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001650 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001651
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001652 i += len;
1653 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1654 pkt_dev->svlan_cfi = value;
1655 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1656 } else {
1657 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1658 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001659 return count;
1660 }
1661
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001662 if (!strcmp(name, "tos")) {
1663 __u32 tmp_value = 0;
1664 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001665 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001666 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001667
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001668 i += len;
1669 if (len == 2) {
1670 pkt_dev->tos = tmp_value;
1671 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1672 } else {
1673 sprintf(pg_result, "ERROR: tos must be 00-ff");
1674 }
1675 return count;
1676 }
1677
1678 if (!strcmp(name, "traffic_class")) {
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->traffic_class = tmp_value;
1687 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1688 } else {
1689 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1690 }
1691 return count;
1692 }
1693
John Fastabend9e50e3a2010-11-16 19:12:28 +00001694 if (!strcmp(name, "skb_priority")) {
1695 len = num_arg(&user_buffer[i], 9, &value);
1696 if (len < 0)
1697 return len;
1698
1699 i += len;
1700 pkt_dev->skb_priority = value;
1701 sprintf(pg_result, "OK: skb_priority=%i",
1702 pkt_dev->skb_priority);
1703 return count;
1704 }
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1707 return -EINVAL;
1708}
1709
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001710static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Al Virod9dda782013-03-31 18:16:14 -04001712 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001713}
1714
Arjan van de Ven9a321442007-02-12 00:55:35 -08001715static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001716 .owner = THIS_MODULE,
1717 .open = pktgen_if_open,
1718 .read = seq_read,
1719 .llseek = seq_lseek,
1720 .write = pktgen_if_write,
1721 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001722};
1723
1724static int pktgen_thread_show(struct seq_file *seq, void *v)
1725{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001726 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001727 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001729 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Luiz Capitulino222f1802006-03-20 22:16:13 -08001731 seq_printf(seq, "Running: ");
1732
1733 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001734 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001736 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Luiz Capitulino222f1802006-03-20 22:16:13 -08001738 seq_printf(seq, "\nStopped: ");
1739
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001740 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001741 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001742 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001745 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001747 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Luiz Capitulino222f1802006-03-20 22:16:13 -08001749 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001754static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001755 const char __user * user_buffer,
1756 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Joe Perches8a994a72010-07-12 10:50:23 +00001758 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001759 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001760 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001765 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return -EINVAL;
1767 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001768
Paul Gortmakerd6182222010-10-18 12:14:44 +00001769 max = count;
1770 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001771 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001772 return len;
1773
Paul Gortmakerd6182222010-10-18 12:14:44 +00001774 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /* Read variable name */
1777
1778 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001779 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001780 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 memset(name, 0, sizeof(name));
1783 if (copy_from_user(name, &user_buffer[i], len))
1784 return -EFAULT;
1785 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001786
Luiz Capitulino222f1802006-03-20 22:16:13 -08001787 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001789 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001790 return len;
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 i += len;
1793
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001794 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001795 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Luiz Capitulino222f1802006-03-20 22:16:13 -08001797 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001798 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 ret = -EINVAL;
1800 goto out;
1801 }
1802
1803 pg_result = &(t->result[0]);
1804
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805 if (!strcmp(name, "add_device")) {
1806 char f[32];
1807 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001809 if (len < 0) {
1810 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto out;
1812 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return -EFAULT;
1815 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001816 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001817 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001818 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001819 if (!ret) {
1820 ret = count;
1821 sprintf(pg_result, "OK: add_device=%s", f);
1822 } else
1823 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 goto out;
1825 }
1826
Luiz Capitulino222f1802006-03-20 22:16:13 -08001827 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001828 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001829 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001830 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001831 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001833 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 goto out;
1835 }
1836
Luiz Capitulino222f1802006-03-20 22:16:13 -08001837 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001838 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001839 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto out;
1841 }
1842
1843 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001844out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return ret;
1846}
1847
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001848static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Al Virod9dda782013-03-31 18:16:14 -04001850 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Arjan van de Ven9a321442007-02-12 00:55:35 -08001853static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001854 .owner = THIS_MODULE,
1855 .open = pktgen_thread_open,
1856 .read = seq_read,
1857 .llseek = seq_lseek,
1858 .write = pktgen_thread_write,
1859 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001860};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001863static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1864 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct pktgen_thread *t;
1867 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001868 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Cong Wang4e58a022013-01-28 19:55:53 +00001870 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001871 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001873 if (remove) {
1874 if_lock(t);
1875 pkt_dev->removal_mark = 1;
1876 t->control |= T_REMDEV;
1877 if_unlock(t);
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 break;
1880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001882 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001885/*
1886 * mark a device for removal
1887 */
Cong Wang4e58a022013-01-28 19:55:53 +00001888static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
1890 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001891 const int max_tries = 10, msec_per_try = 125;
1892 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001893
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001894 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001895 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001896
Luiz Capitulino222f1802006-03-20 22:16:13 -08001897 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001898
Cong Wang4e58a022013-01-28 19:55:53 +00001899 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001900 if (pkt_dev == NULL)
1901 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001902
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001903 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001904 pr_debug("%s: waiting for %s to disappear....\n",
1905 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001906 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001907 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001908
1909 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001910 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1911 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001912 break;
1913 }
1914
1915 }
1916
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001917 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001918}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001919
Cong Wang4e58a022013-01-28 19:55:53 +00001920static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001921{
1922 struct pktgen_thread *t;
1923
Cong Wang4e58a022013-01-28 19:55:53 +00001924 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001925 struct pktgen_dev *pkt_dev;
1926
1927 list_for_each_entry(pkt_dev, &t->if_list, list) {
1928 if (pkt_dev->odev != dev)
1929 continue;
1930
David Howellsa8ca16e2013-04-12 17:27:28 +01001931 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001932
Alexey Dobriyan29753152009-08-28 23:34:43 -07001933 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00001934 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07001935 &pktgen_if_fops,
1936 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001937 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001938 pr_err("can't move proc entry for '%s'\n",
1939 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001940 break;
1941 }
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Luiz Capitulino222f1802006-03-20 22:16:13 -08001945static int pktgen_device_event(struct notifier_block *unused,
1946 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Jiri Pirko351638e2013-05-28 01:30:21 +00001948 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00001949 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Cong Wang4e58a022013-01-28 19:55:53 +00001951 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001952 return NOTIFY_DONE;
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /* It is OK that we do not hold the group lock right now,
1955 * as we run under the RTNL lock.
1956 */
1957
1958 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001959 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00001960 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00001964 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 return NOTIFY_DONE;
1969}
1970
Cong Wang4e58a022013-01-28 19:55:53 +00001971static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1972 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001973 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001974{
1975 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001976 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001977
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001978 for (i = 0; ifname[i] != '@'; i++) {
1979 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001980 break;
1981
1982 b[i] = ifname[i];
1983 }
1984 b[i] = 0;
1985
Cong Wang4e58a022013-01-28 19:55:53 +00001986 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07001987}
1988
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990/* Associate pktgen_dev with a device. */
1991
Cong Wang4e58a022013-01-28 19:55:53 +00001992static int pktgen_setup_dev(const struct pktgen_net *pn,
1993 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001996 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (pkt_dev->odev) {
2000 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002001 pkt_dev->odev = NULL;
2002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Cong Wang4e58a022013-01-28 19:55:53 +00002004 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002006 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002007 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002011 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002012 err = -EINVAL;
2013 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002014 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002015 err = -ENETDOWN;
2016 } else {
2017 pkt_dev->odev = odev;
2018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002022 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
2025/* Read pkt_dev from the interface and set up internal pktgen_dev
2026 * structure to have the right information to create/send packets
2027 */
2028static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2029{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002030 int ntxq;
2031
Luiz Capitulino222f1802006-03-20 22:16:13 -08002032 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002033 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034 sprintf(pkt_dev->result,
2035 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2036 return;
2037 }
2038
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002039 /* make sure that we don't pick a non-existing transmit queue */
2040 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002041
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002042 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002043 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2044 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2045 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002046 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002047 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002048 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002049 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2050 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2051 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002052 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002053 }
2054
Luiz Capitulino222f1802006-03-20 22:16:13 -08002055 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002057 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002058 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Luiz Capitulino222f1802006-03-20 22:16:13 -08002060 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002061 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Luiz Capitulino222f1802006-03-20 22:16:13 -08002063 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002064 int i, set = 0, err = 1;
2065 struct inet6_dev *idev;
2066
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002067 if (pkt_dev->min_pkt_size == 0) {
2068 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2069 + sizeof(struct udphdr)
2070 + sizeof(struct pktgen_hdr)
2071 + pkt_dev->pkt_overhead;
2072 }
2073
Luiz Capitulino222f1802006-03-20 22:16:13 -08002074 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2075 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 set = 1;
2077 break;
2078 }
2079
Luiz Capitulino222f1802006-03-20 22:16:13 -08002080 if (!set) {
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /*
2083 * Use linklevel address if unconfigured.
2084 *
2085 * use ipv6_get_lladdr if/when it's get exported
2086 */
2087
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002088 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002089 idev = __in6_dev_get(pkt_dev->odev);
2090 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 struct inet6_ifaddr *ifp;
2092
2093 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002094 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2095 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002096 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002097 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 err = 0;
2099 break;
2100 }
2101 }
2102 read_unlock_bh(&idev->lock);
2103 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002104 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002105 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002106 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002108 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002109 if (pkt_dev->min_pkt_size == 0) {
2110 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2111 + sizeof(struct udphdr)
2112 + sizeof(struct pktgen_hdr)
2113 + pkt_dev->pkt_overhead;
2114 }
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 pkt_dev->saddr_min = 0;
2117 pkt_dev->saddr_max = 0;
2118 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002119
2120 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002123 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (in_dev) {
2125 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002126 pkt_dev->saddr_min =
2127 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 pkt_dev->saddr_max = pkt_dev->saddr_min;
2129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
2131 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002132 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2134 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2135 }
2136
2137 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2138 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2139 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002140 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002141 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2142 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2143 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2144
Luiz Capitulino222f1802006-03-20 22:16:13 -08002145 pkt_dev->cur_dst_mac_offset = 0;
2146 pkt_dev->cur_src_mac_offset = 0;
2147 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2148 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2149 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2150 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 pkt_dev->nflows = 0;
2152}
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002155static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2156{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002157 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002158 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002159 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002160
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002161 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2162 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002163
Daniel Turull43d28b62010-06-09 22:49:57 +00002164 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002165 if (remaining <= 0) {
2166 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002167 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002168 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002169
Daniel Borkmann398f3822012-10-28 08:27:19 +00002170 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002171 if (remaining < 100000) {
2172 /* for small delays (<100us), just loop until limit is reached */
2173 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002174 end_time = ktime_get();
2175 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002176 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002177 /* see do_nanosleep */
2178 hrtimer_init_sleeper(&t, current);
2179 do {
2180 set_current_state(TASK_INTERRUPTIBLE);
2181 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2182 if (!hrtimer_active(&t.timer))
2183 t.task = NULL;
2184
2185 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002188 hrtimer_cancel(&t.timer);
2189 } while (t.task && pkt_dev->running && !signal_pending(current));
2190 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002191 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002193
2194 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002195 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002198static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2199{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002200 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002201 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2202 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2203 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2204}
2205
Stephen Hemminger648fda72009-08-27 13:55:07 +00002206static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002207{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002208 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002209}
2210
2211static inline int f_pick(struct pktgen_dev *pkt_dev)
2212{
2213 int flow = pkt_dev->curfl;
2214
2215 if (pkt_dev->flags & F_FLOW_SEQ) {
2216 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2217 /* reset time */
2218 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002219 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002220 pkt_dev->curfl += 1;
2221 if (pkt_dev->curfl >= pkt_dev->cflows)
2222 pkt_dev->curfl = 0; /*reset */
2223 }
2224 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002225 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002226 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002227
Robert Olsson1211a642008-08-05 18:44:26 -07002228 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002229 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002230 pkt_dev->flows[flow].flags = 0;
2231 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002232 }
2233
2234 return pkt_dev->curfl;
2235}
2236
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002237
2238#ifdef CONFIG_XFRM
2239/* If there was already an IPSEC SA, we keep it as is, else
2240 * we go look for it ...
2241*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002242#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002243static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002244{
2245 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002246 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002247 if (!x) {
2248 /*slow path: we dont already have xfrm_state*/
Cong Wang4e58a022013-01-28 19:55:53 +00002249 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002250 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002251 (xfrm_address_t *)&pkt_dev->cur_saddr,
2252 AF_INET,
2253 pkt_dev->ipsmode,
2254 pkt_dev->ipsproto, 0);
2255 if (x) {
2256 pkt_dev->flows[flow].x = x;
2257 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002258 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002259 }
2260
2261 }
2262}
2263#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002264static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2265{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002266
2267 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2268 pkt_dev->cur_queue_map = smp_processor_id();
2269
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002270 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002271 __u16 t;
2272 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002273 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002274 (pkt_dev->queue_map_max -
2275 pkt_dev->queue_map_min + 1)
2276 + pkt_dev->queue_map_min;
2277 } else {
2278 t = pkt_dev->cur_queue_map + 1;
2279 if (t > pkt_dev->queue_map_max)
2280 t = pkt_dev->queue_map_min;
2281 }
2282 pkt_dev->cur_queue_map = t;
2283 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002284 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 -07002285}
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287/* Increment/randomize headers according to flags and current values
2288 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2289 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002290static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2291{
2292 __u32 imn;
2293 __u32 imx;
2294 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002296 if (pkt_dev->cflows)
2297 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002300 if (pkt_dev->src_mac_count > 1) {
2301 __u32 mc;
2302 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Luiz Capitulino222f1802006-03-20 22:16:13 -08002304 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002305 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002306 else {
2307 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002308 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002309 pkt_dev->src_mac_count)
2310 pkt_dev->cur_src_mac_offset = 0;
2311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Luiz Capitulino222f1802006-03-20 22:16:13 -08002313 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2314 pkt_dev->hh[11] = tmp;
2315 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2316 pkt_dev->hh[10] = tmp;
2317 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2318 pkt_dev->hh[9] = tmp;
2319 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2320 pkt_dev->hh[8] = tmp;
2321 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2322 pkt_dev->hh[7] = tmp;
2323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Luiz Capitulino222f1802006-03-20 22:16:13 -08002325 /* Deal with Destination MAC */
2326 if (pkt_dev->dst_mac_count > 1) {
2327 __u32 mc;
2328 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Luiz Capitulino222f1802006-03-20 22:16:13 -08002330 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002331 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Luiz Capitulino222f1802006-03-20 22:16:13 -08002333 else {
2334 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002335 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002336 pkt_dev->dst_mac_count) {
2337 pkt_dev->cur_dst_mac_offset = 0;
2338 }
2339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Luiz Capitulino222f1802006-03-20 22:16:13 -08002341 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2342 pkt_dev->hh[5] = tmp;
2343 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2344 pkt_dev->hh[4] = tmp;
2345 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2346 pkt_dev->hh[3] = tmp;
2347 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2348 pkt_dev->hh[2] = tmp;
2349 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2350 pkt_dev->hh[1] = tmp;
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002353 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002354 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002355 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002356 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2357 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002358 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002359 htonl(0x000fffff));
2360 }
2361
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002362 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002363 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002364 }
2365
2366 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002367 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002368 }
2369
Luiz Capitulino222f1802006-03-20 22:16:13 -08002370 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2371 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002372 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002373 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2374 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Luiz Capitulino222f1802006-03-20 22:16:13 -08002376 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 pkt_dev->cur_udp_src++;
2378 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2379 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002380 }
2381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Luiz Capitulino222f1802006-03-20 22:16:13 -08002383 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2384 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002385 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002386 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2387 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002388 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002390 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002392 }
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 if (!(pkt_dev->flags & F_IPV6)) {
2396
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002397 imn = ntohl(pkt_dev->saddr_min);
2398 imx = ntohl(pkt_dev->saddr_max);
2399 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002401 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002402 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 else {
2404 t = ntohl(pkt_dev->cur_saddr);
2405 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002406 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410 pkt_dev->cur_saddr = htonl(t);
2411 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002412
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002413 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2415 } else {
Al Viro252e33462006-11-14 20:48:11 -08002416 imn = ntohl(pkt_dev->daddr_min);
2417 imx = ntohl(pkt_dev->daddr_max);
2418 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002420 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (pkt_dev->flags & F_IPDST_RND) {
2422
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002423 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002424 t = prandom_u32() %
2425 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002426 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002427 } while (ipv4_is_loopback(s) ||
2428 ipv4_is_multicast(s) ||
2429 ipv4_is_lbcast(s) ||
2430 ipv4_is_zeronet(s) ||
2431 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002432 pkt_dev->cur_daddr = s;
2433 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 t = ntohl(pkt_dev->cur_daddr);
2435 t++;
2436 if (t > imx) {
2437 t = imn;
2438 }
2439 pkt_dev->cur_daddr = htonl(t);
2440 }
2441 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002442 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002443 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002444 pkt_dev->flows[flow].cur_daddr =
2445 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002446#ifdef CONFIG_XFRM
2447 if (pkt_dev->flags & F_IPSEC_ON)
2448 get_ipsec_sa(pkt_dev, flow);
2449#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 pkt_dev->nflows++;
2451 }
2452 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002453 } else { /* IPV6 * */
2454
Joe Perches06e30412012-10-18 17:55:31 +00002455 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 int i;
2457
2458 /* Only random destinations yet */
2459
Luiz Capitulino222f1802006-03-20 22:16:13 -08002460 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002462 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002463 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2464 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468
Luiz Capitulino222f1802006-03-20 22:16:13 -08002469 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2470 __u32 t;
2471 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002472 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002473 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2474 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002475 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002477 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002479 }
2480 pkt_dev->cur_pkt_size = t;
2481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002483 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 pkt_dev->flows[flow].count++;
2486}
2487
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002488
2489#ifdef CONFIG_XFRM
2490static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2491{
2492 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2493 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002494 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002495
2496 if (!x)
2497 return 0;
2498 /* XXX: we dont support tunnel mode for now until
2499 * we resolve the dst issue */
2500 if (x->props.mode != XFRM_MODE_TRANSPORT)
2501 return 0;
2502
Herbert Xu13996372007-10-17 21:35:51 -07002503 err = x->outer_mode->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002504 if (err) {
2505 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002506 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002507 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002508 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002509 if (err) {
2510 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002511 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002512 }
Fan Du0af0a412014-01-03 11:18:27 +08002513 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002514 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002515 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002516 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002517error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002518 return err;
2519}
2520
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002521static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002522{
2523 if (pkt_dev->cflows) {
2524 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002525 int i;
2526 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002527 struct xfrm_state *x = pkt_dev->flows[i].x;
2528 if (x) {
2529 xfrm_state_put(x);
2530 pkt_dev->flows[i].x = NULL;
2531 }
2532 }
2533 }
2534}
2535
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002536static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002537 struct sk_buff *skb, __be16 protocol)
2538{
2539 if (pkt_dev->flags & F_IPSEC_ON) {
2540 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2541 int nhead = 0;
2542 if (x) {
2543 int ret;
2544 __u8 *eth;
fan.du38682042013-12-01 16:28:48 +08002545 struct iphdr *iph;
2546
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002547 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002548 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002549 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2550 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002551 pr_err("Error expanding ipsec packet %d\n",
2552 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002553 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002554 }
2555 }
2556
2557 /* ipsec is not expecting ll header */
2558 skb_pull(skb, ETH_HLEN);
2559 ret = pktgen_output_ipsec(skb, pkt_dev);
2560 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002561 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002562 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002563 }
2564 /* restore ll */
2565 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2566 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002567 *(u16 *) &eth[12] = protocol;
fan.du38682042013-12-01 16:28:48 +08002568
2569 /* Update IPv4 header len as well as checksum value */
2570 iph = ip_hdr(skb);
2571 iph->tot_len = htons(skb->len - ETH_HLEN);
2572 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002573 }
2574 }
2575 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002576err:
2577 kfree_skb(skb);
2578 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002579}
2580#endif
2581
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002582static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2583{
Eric Dumazet95c96172012-04-15 05:58:06 +00002584 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002585 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002586 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002587
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002588 mpls--;
2589 *mpls |= MPLS_STACK_BOTTOM;
2590}
2591
Al Viro0f37c602006-11-03 03:49:56 -08002592static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2593 unsigned int prio)
2594{
2595 return htons(id | (cfi << 12) | (prio << 13));
2596}
2597
Eric Dumazet26ad7872011-01-25 13:26:05 -08002598static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2599 int datalen)
2600{
2601 struct timeval timestamp;
2602 struct pktgen_hdr *pgh;
2603
2604 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2605 datalen -= sizeof(*pgh);
2606
2607 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002608 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002609 } else {
2610 int frags = pkt_dev->nfrags;
2611 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002612 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002613
2614
2615 if (frags > MAX_SKB_FRAGS)
2616 frags = MAX_SKB_FRAGS;
2617 len = datalen - frags * PAGE_SIZE;
2618 if (len > 0) {
2619 memset(skb_put(skb, len), 0, len);
2620 datalen = frags * PAGE_SIZE;
2621 }
2622
2623 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002624 frag_len = (datalen/frags) < PAGE_SIZE ?
2625 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002626 while (datalen > 0) {
2627 if (unlikely(!pkt_dev->page)) {
2628 int node = numa_node_id();
2629
2630 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2631 node = pkt_dev->node;
2632 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2633 if (!pkt_dev->page)
2634 break;
2635 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002636 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002637 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002638 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002639 /*last fragment, fill rest of data*/
2640 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002641 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2642 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002643 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002644 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2645 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2646 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2647 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002648 i++;
2649 skb_shinfo(skb)->nr_frags = i;
2650 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002651 }
2652
2653 /* Stamp the time, and sequence number,
2654 * convert them to network byte order
2655 */
2656 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2657 pgh->seq_num = htonl(pkt_dev->seq_num);
2658
2659 do_gettimeofday(&timestamp);
2660 pgh->tv_sec = htonl(timestamp.tv_sec);
2661 pgh->tv_usec = htonl(timestamp.tv_usec);
2662}
2663
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002664static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2665 struct pktgen_dev *pkt_dev,
2666 unsigned int extralen)
2667{
2668 struct sk_buff *skb = NULL;
2669 unsigned int size = pkt_dev->cur_pkt_size + 64 + extralen +
2670 pkt_dev->pkt_overhead;
2671
2672 if (pkt_dev->flags & F_NODE) {
2673 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2674
2675 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2676 if (likely(skb)) {
2677 skb_reserve(skb, NET_SKB_PAD);
2678 skb->dev = dev;
2679 }
2680 } else {
2681 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2682 }
2683
2684 return skb;
2685}
2686
Luiz Capitulino222f1802006-03-20 22:16:13 -08002687static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2688 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
2690 struct sk_buff *skb = NULL;
2691 __u8 *eth;
2692 struct udphdr *udph;
2693 int datalen, iplen;
2694 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002695 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002696 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002697 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2698 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2699 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2700 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002701 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002702
2703 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002704 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002705
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002706 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002707 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002708
Robert Olsson64053be2005-06-26 15:27:10 -07002709 /* Update any of the values, used when we're incrementing various
2710 * fields.
2711 */
2712 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002713 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002714
David S. Miller7ac54592006-01-18 14:19:10 -08002715 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002716
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002717 skb = pktgen_alloc_skb(odev, pkt_dev, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (!skb) {
2719 sprintf(pkt_dev->result, "No memory");
2720 return NULL;
2721 }
2722
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002723 prefetchw(skb->data);
David S. Miller7ac54592006-01-18 14:19:10 -08002724 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 /* Reserve for ethernet and IP header */
2727 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002728 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2729 if (pkt_dev->nr_labels)
2730 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002731
2732 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002733 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002734 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002735 *svlan_tci = build_tci(pkt_dev->svlan_id,
2736 pkt_dev->svlan_cfi,
2737 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002738 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002739 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002740 }
2741 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002742 *vlan_tci = build_tci(pkt_dev->vlan_id,
2743 pkt_dev->vlan_cfi,
2744 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002745 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002746 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002747 }
2748
Thomas Graf525cebe2013-06-03 11:49:23 +00002749 skb_set_mac_header(skb, 0);
2750 skb_set_network_header(skb, skb->len);
2751 iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2752
2753 skb_set_transport_header(skb, skb->len);
2754 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002755 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002756 skb->priority = pkt_dev->skb_priority;
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002759 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002761 /* Eth + IPh + UDPh + mpls */
2762 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002763 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002764 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 udph->source = htons(pkt_dev->cur_udp_src);
2768 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002769 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002770 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 iph->ihl = 5;
2773 iph->version = 4;
2774 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002775 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002776 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 iph->saddr = pkt_dev->cur_saddr;
2778 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002779 iph->id = htons(pkt_dev->ip_id);
2780 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 iph->frag_off = 0;
2782 iplen = 20 + 8 + datalen;
2783 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002784 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002785 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 skb->dev = odev;
2787 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002788
2789 if (!(pkt_dev->flags & F_UDPCSUM)) {
2790 skb->ip_summed = CHECKSUM_NONE;
2791 } else if (odev->features & NETIF_F_V4_CSUM) {
2792 skb->ip_summed = CHECKSUM_PARTIAL;
2793 skb->csum = 0;
2794 udp4_hwcsum(skb, udph->source, udph->dest);
2795 } else {
2796 __wsum csum = udp_csum(skb);
2797
2798 /* add protocol-dependent pseudo-header */
2799 udph->check = csum_tcpudp_magic(udph->source, udph->dest,
2800 datalen + 8, IPPROTO_UDP, csum);
2801
2802 if (udph->check == 0)
2803 udph->check = CSUM_MANGLED_0;
2804 }
2805
Eric Dumazet26ad7872011-01-25 13:26:05 -08002806 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002807
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002808#ifdef CONFIG_XFRM
2809 if (!process_ipsec(pkt_dev, skb, protocol))
2810 return NULL;
2811#endif
2812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return skb;
2814}
2815
Luiz Capitulino222f1802006-03-20 22:16:13 -08002816static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2817 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 struct sk_buff *skb = NULL;
2820 __u8 *eth;
2821 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002822 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002824 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002825 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002826 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2827 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2828 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2829 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002830 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002831
2832 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002833 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002834
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002835 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002836 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002837
Robert Olsson64053be2005-06-26 15:27:10 -07002838 /* Update any of the values, used when we're incrementing various
2839 * fields.
2840 */
2841 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002842 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002843
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002844 skb = pktgen_alloc_skb(odev, pkt_dev, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (!skb) {
2846 sprintf(pkt_dev->result, "No memory");
2847 return NULL;
2848 }
2849
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002850 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 skb_reserve(skb, 16);
2852
2853 /* Reserve for ethernet and IP header */
2854 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002855 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2856 if (pkt_dev->nr_labels)
2857 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002858
2859 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002860 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002861 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002862 *svlan_tci = build_tci(pkt_dev->svlan_id,
2863 pkt_dev->svlan_cfi,
2864 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002865 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002866 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002867 }
2868 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002869 *vlan_tci = build_tci(pkt_dev->vlan_id,
2870 pkt_dev->vlan_cfi,
2871 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002872 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002873 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002874 }
2875
Thomas Graf525cebe2013-06-03 11:49:23 +00002876 skb_set_mac_header(skb, 0);
2877 skb_set_network_header(skb, skb->len);
2878 iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2879
2880 skb_set_transport_header(skb, skb->len);
2881 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002882 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002883 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002886 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002887
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002888 /* Eth + IPh + UDPh + mpls */
2889 datalen = pkt_dev->cur_pkt_size - 14 -
2890 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002891 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002893 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002895 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
2897
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002898 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 udph->source = htons(pkt_dev->cur_udp_src);
2900 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002901 udph->len = htons(udplen);
2902 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002904 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002906 if (pkt_dev->traffic_class) {
2907 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002908 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002909 }
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 iph->hop_limit = 32;
2912
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002913 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 iph->nexthdr = IPPROTO_UDP;
2915
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002916 iph->daddr = pkt_dev->cur_in6_daddr;
2917 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002919 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 skb->dev = odev;
2921 skb->pkt_type = PACKET_HOST;
2922
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002923 if (!(pkt_dev->flags & F_UDPCSUM)) {
2924 skb->ip_summed = CHECKSUM_NONE;
2925 } else if (odev->features & NETIF_F_V6_CSUM) {
2926 skb->ip_summed = CHECKSUM_PARTIAL;
2927 skb->csum_start = skb_transport_header(skb) - skb->head;
2928 skb->csum_offset = offsetof(struct udphdr, check);
2929 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2930 } else {
2931 __wsum csum = udp_csum(skb);
2932
2933 /* add protocol-dependent pseudo-header */
2934 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2935
2936 if (udph->check == 0)
2937 udph->check = CSUM_MANGLED_0;
2938 }
2939
Eric Dumazet26ad7872011-01-25 13:26:05 -08002940 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return skb;
2943}
2944
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002945static struct sk_buff *fill_packet(struct net_device *odev,
2946 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002948 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return fill_packet_ipv6(odev, pkt_dev);
2950 else
2951 return fill_packet_ipv4(odev, pkt_dev);
2952}
2953
Luiz Capitulino222f1802006-03-20 22:16:13 -08002954static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002956 pkt_dev->seq_num = 1;
2957 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002959 pkt_dev->tx_bytes = 0;
2960 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
2963/* Set up structure for sending pkts, clear counters */
2964
2965static void pktgen_run(struct pktgen_thread *t)
2966{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002967 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 int started = 0;
2969
Joe Perchesf9467ea2010-06-21 12:29:14 +00002970 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002973 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 /*
2976 * setup odev and create initial packet.
2977 */
2978 pktgen_setup_inject(pkt_dev);
2979
Luiz Capitulino222f1802006-03-20 22:16:13 -08002980 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002982 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00002984 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002985
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002986 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 strcpy(pkt_dev->result, "Starting");
2989 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002990 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 strcpy(pkt_dev->result, "Error starting");
2992 }
2993 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002994 if (started)
2995 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
2997
Cong Wang4e58a022013-01-28 19:55:53 +00002998static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003000 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Joe Perchesf9467ea2010-06-21 12:29:14 +00003002 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003004 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003005
Cong Wang4e58a022013-01-28 19:55:53 +00003006 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003007 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003008
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003009 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
Stephen Hemminger648fda72009-08-27 13:55:07 +00003012static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003014 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003016 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003017 if (pkt_dev->running)
3018 return 1;
3019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
Luiz Capitulino222f1802006-03-20 22:16:13 -08003022static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003024 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Luiz Capitulino222f1802006-03-20 22:16:13 -08003026 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Luiz Capitulino222f1802006-03-20 22:16:13 -08003028 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Luiz Capitulino222f1802006-03-20 22:16:13 -08003030 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Luiz Capitulino222f1802006-03-20 22:16:13 -08003032 if (signal_pending(current))
3033 goto signal;
3034 if_lock(t);
3035 }
3036 if_unlock(t);
3037 return 1;
3038signal:
3039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Cong Wang4e58a022013-01-28 19:55:53 +00003042static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003044 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003046
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003047 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003048
Cong Wang4e58a022013-01-28 19:55:53 +00003049 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003051 if (sig == 0)
3052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003054
3055 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003056 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003058
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003059 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return sig;
3061}
3062
Cong Wang4e58a022013-01-28 19:55:53 +00003063static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003065 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Joe Perchesf9467ea2010-06-21 12:29:14 +00003067 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003069 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Cong Wang4e58a022013-01-28 19:55:53 +00003071 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003073
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003074 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003076 /* Propagate thread->control */
3077 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003078
Cong Wang4e58a022013-01-28 19:55:53 +00003079 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080}
3081
Cong Wang4e58a022013-01-28 19:55:53 +00003082static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003083{
3084 struct pktgen_thread *t;
3085
Joe Perchesf9467ea2010-06-21 12:29:14 +00003086 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003087
3088 mutex_lock(&pktgen_thread_lock);
3089
Cong Wang4e58a022013-01-28 19:55:53 +00003090 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003091 t->control |= (T_REMDEVALL);
3092
3093 mutex_unlock(&pktgen_thread_lock);
3094
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003095 /* Propagate thread->control */
3096 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003097
Cong Wang4e58a022013-01-28 19:55:53 +00003098 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003099}
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3102{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003103 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003104 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003105 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3106 pkt_dev->started_at);
3107 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Daniel Turull03a14ab2011-03-09 14:11:00 -08003109 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003110 (unsigned long long)ktime_to_us(elapsed),
3111 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3112 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003113 (unsigned long long)pkt_dev->sofar,
3114 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003116 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3117 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Luiz Capitulino222f1802006-03-20 22:16:13 -08003119 bps = pps * 8 * pkt_dev->cur_pkt_size;
3120
3121 mbps = bps;
3122 do_div(mbps, 1000000);
3123 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3124 (unsigned long long)pps,
3125 (unsigned long long)mbps,
3126 (unsigned long long)bps,
3127 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003131static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003133 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003134
Luiz Capitulino222f1802006-03-20 22:16:13 -08003135 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003136 pr_warning("interface: %s is already stopped\n",
3137 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003138 return -EINVAL;
3139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003141 kfree_skb(pkt_dev->skb);
3142 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003143 pkt_dev->stopped_at = ktime_get();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003146 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Luiz Capitulino222f1802006-03-20 22:16:13 -08003148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003153 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if_lock(t);
3156
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003157 list_for_each_entry(pkt_dev, &t->if_list, list) {
3158 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 continue;
3160 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003161 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003162 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003163 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 }
3165 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169static void pktgen_stop(struct pktgen_thread *t)
3170{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003171 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Joe Perchesf9467ea2010-06-21 12:29:14 +00003173 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003177 list_for_each_entry(pkt_dev, &t->if_list, list) {
3178 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Luiz Capitulino222f1802006-03-20 22:16:13 -08003181 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003184/*
3185 * one of our devices needs to be removed - find it
3186 * and remove it
3187 */
3188static void pktgen_rem_one_if(struct pktgen_thread *t)
3189{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003190 struct list_head *q, *n;
3191 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003192
Joe Perchesf9467ea2010-06-21 12:29:14 +00003193 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003194
3195 if_lock(t);
3196
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003197 list_for_each_safe(q, n, &t->if_list) {
3198 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003199
Luiz Capitulino222f1802006-03-20 22:16:13 -08003200 if (!cur->removal_mark)
3201 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003202
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003203 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003204 cur->skb = NULL;
3205
3206 pktgen_remove_device(t, cur);
3207
3208 break;
3209 }
3210
3211 if_unlock(t);
3212}
3213
Luiz Capitulino222f1802006-03-20 22:16:13 -08003214static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003216 struct list_head *q, *n;
3217 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003218
Joe Perchesf9467ea2010-06-21 12:29:14 +00003219 func_enter();
3220
Luiz Capitulino222f1802006-03-20 22:16:13 -08003221 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003222
Luiz Capitulino222f1802006-03-20 22:16:13 -08003223 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003225 list_for_each_safe(q, n, &t->if_list) {
3226 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003227
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003228 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003229 cur->skb = NULL;
3230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 pktgen_remove_device(t, cur);
3232 }
3233
Luiz Capitulino222f1802006-03-20 22:16:13 -08003234 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003239 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003240 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
Stephen Hemmingeref879792009-09-22 19:41:43 +00003243static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003244{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003245 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003246 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003247 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003248}
3249
Stephen Hemmingeref879792009-09-22 19:41:43 +00003250static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3251{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003252 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003253
3254 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3255 if (signal_pending(current))
3256 break;
3257
3258 if (need_resched())
3259 pktgen_resched(pkt_dev);
3260 else
3261 cpu_relax();
3262 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003263 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003264}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003265
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003266static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Stephen Hemminger00829822008-11-20 20:14:53 -08003268 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003269 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003270 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003271 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003272 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 int ret;
3274
Stephen Hemmingeref879792009-09-22 19:41:43 +00003275 /* If device is offline, then don't send */
3276 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3277 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003278 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003280
Stephen Hemmingeref879792009-09-22 19:41:43 +00003281 /* This is max DELAY, this has special meaning of
3282 * "never transmit"
3283 */
3284 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003285 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003286 return;
3287 }
3288
3289 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003290 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3291 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3292 /* build a new pkt */
3293 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003294
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003295 pkt_dev->skb = fill_packet(odev, pkt_dev);
3296 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003297 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003298 schedule();
3299 pkt_dev->clone_count--; /* back out increment, OOM */
3300 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003302 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003303 pkt_dev->allocated_skbs++;
3304 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003306
Stephen Hemmingeref879792009-09-22 19:41:43 +00003307 if (pkt_dev->delay && pkt_dev->last_ok)
3308 spin(pkt_dev, pkt_dev->next_tx);
3309
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003310 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3311 txq = netdev_get_tx_queue(odev, queue_map);
3312
3313 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003314
Tom Herbert734664982011-11-28 16:32:44 +00003315 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003316 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003317 pkt_dev->last_ok = 0;
3318 goto unlock;
3319 }
3320 atomic_inc(&(pkt_dev->skb->users));
3321 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Stephen Hemmingeref879792009-09-22 19:41:43 +00003323 switch (ret) {
3324 case NETDEV_TX_OK:
3325 txq_trans_update(txq);
3326 pkt_dev->last_ok = 1;
3327 pkt_dev->sofar++;
3328 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003329 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003330 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003331 case NET_XMIT_DROP:
3332 case NET_XMIT_CN:
3333 case NET_XMIT_POLICED:
3334 /* skb has been consumed */
3335 pkt_dev->errors++;
3336 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003337 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003338 net_info_ratelimited("%s xmit error: %d\n",
3339 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003340 pkt_dev->errors++;
3341 /* fallthru */
3342 case NETDEV_TX_LOCKED:
3343 case NETDEV_TX_BUSY:
3344 /* Retry it next time */
3345 atomic_dec(&(pkt_dev->skb->users));
3346 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003347 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003348unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003349 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 /* If pkt_dev->count is zero, then run forever */
3352 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003353 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 /* Done with this */
3356 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003357 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003360/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 * Main loop of the thread goes here
3362 */
3363
David S. Milleree74baa2007-01-01 20:51:53 -08003364static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003367 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003368 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
David S. Milleree74baa2007-01-01 20:51:53 -08003371 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003374 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Joe Perchesf9467ea2010-06-21 12:29:14 +00003376 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
David S. Milleree74baa2007-01-01 20:51:53 -08003378 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003379
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003380 set_freezable();
3381
David S. Milleree74baa2007-01-01 20:51:53 -08003382 while (!kthread_should_stop()) {
3383 pkt_dev = next_to_run(t);
3384
Stephen Hemmingeref879792009-09-22 19:41:43 +00003385 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003386 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003387 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003388 wait_event_interruptible_timeout(t->queue,
3389 t->control != 0,
3390 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003391 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003392 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003393 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 __set_current_state(TASK_RUNNING);
3396
Stephen Hemmingeref879792009-09-22 19:41:43 +00003397 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 pktgen_xmit(pkt_dev);
3399
Stephen Hemmingeref879792009-09-22 19:41:43 +00003400 if (need_resched())
3401 pktgen_resched(pkt_dev);
3402 else
3403 cpu_relax();
3404 }
3405
Luiz Capitulino222f1802006-03-20 22:16:13 -08003406 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 pktgen_stop(t);
3408 t->control &= ~(T_STOP);
3409 }
3410
Luiz Capitulino222f1802006-03-20 22:16:13 -08003411 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 pktgen_run(t);
3413 t->control &= ~(T_RUN);
3414 }
3415
Luiz Capitulino222f1802006-03-20 22:16:13 -08003416 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003418 t->control &= ~(T_REMDEVALL);
3419 }
3420
Luiz Capitulino222f1802006-03-20 22:16:13 -08003421 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003422 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 t->control &= ~(T_REMDEV);
3424 }
3425
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003426 try_to_freeze();
3427
David S. Milleree74baa2007-01-01 20:51:53 -08003428 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Joe Perchesf9467ea2010-06-21 12:29:14 +00003431 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003432 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Joe Perchesf9467ea2010-06-21 12:29:14 +00003434 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003435 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Joe Perchesf9467ea2010-06-21 12:29:14 +00003437 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003438 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003439
Eric Dumazet551eaff2010-11-21 10:26:44 -08003440 /* Wait for kthread_stop */
3441 while (!kthread_should_stop()) {
3442 set_current_state(TASK_INTERRUPTIBLE);
3443 schedule();
3444 }
3445 __set_current_state(TASK_RUNNING);
3446
David S. Milleree74baa2007-01-01 20:51:53 -08003447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
Luiz Capitulino222f1802006-03-20 22:16:13 -08003450static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003451 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003453 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003454 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Eric Dumazet3e984842009-11-24 14:50:53 -08003456 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003457 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003458 if (strncmp(p->odevname, ifname, len) == 0) {
3459 if (p->odevname[len]) {
3460 if (exact || p->odevname[len] != '@')
3461 continue;
3462 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003463 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003464 break;
3465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Luiz Capitulino222f1802006-03-20 22:16:13 -08003467 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003468 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003469 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003472/*
3473 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 */
3475
Luiz Capitulino222f1802006-03-20 22:16:13 -08003476static int add_dev_to_thread(struct pktgen_thread *t,
3477 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
3479 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Luiz Capitulino222f1802006-03-20 22:16:13 -08003481 if_lock(t);
3482
3483 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003484 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003485 rv = -EBUSY;
3486 goto out;
3487 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003488
3489 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003490 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 pkt_dev->running = 0;
3492
Luiz Capitulino222f1802006-03-20 22:16:13 -08003493out:
3494 if_unlock(t);
3495 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
3498/* Called under thread lock */
3499
Luiz Capitulino222f1802006-03-20 22:16:13 -08003500static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003502 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003503 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003504 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 /* We don't allow a device to be on several threads */
3507
Cong Wang4e58a022013-01-28 19:55:53 +00003508 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003509 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003510 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003511 return -EBUSY;
3512 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003513
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003514 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003515 if (!pkt_dev)
3516 return -ENOMEM;
3517
Eric Dumazet593f63b2009-11-23 01:44:37 +00003518 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003519 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003520 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003521 if (pkt_dev->flows == NULL) {
3522 kfree(pkt_dev);
3523 return -ENOMEM;
3524 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003525
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003526 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003527 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003528 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003529 pkt_dev->count = pg_count_d;
3530 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003531 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003532 pkt_dev->udp_src_max = 9;
3533 pkt_dev->udp_dst_min = 9;
3534 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003535 pkt_dev->vlan_p = 0;
3536 pkt_dev->vlan_cfi = 0;
3537 pkt_dev->vlan_id = 0xffff;
3538 pkt_dev->svlan_p = 0;
3539 pkt_dev->svlan_cfi = 0;
3540 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003541 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003542
Cong Wang4e58a022013-01-28 19:55:53 +00003543 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003544 if (err)
3545 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003546 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3547 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003548
Cong Wang4e58a022013-01-28 19:55:53 +00003549 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003550 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003551 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003552 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003553 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003554 err = -EINVAL;
3555 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003556 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003557#ifdef CONFIG_XFRM
3558 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3559 pkt_dev->ipsproto = IPPROTO_ESP;
3560#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003561
3562 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003563out2:
3564 dev_put(pkt_dev->odev);
3565out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003566#ifdef CONFIG_XFRM
3567 free_SAs(pkt_dev);
3568#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003569 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003570 kfree(pkt_dev);
3571 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
Cong Wang4e58a022013-01-28 19:55:53 +00003574static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003576 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003577 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003578 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003580 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3581 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003582 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003583 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003584 return -ENOMEM;
3585 }
3586
Luiz Capitulino222f1802006-03-20 22:16:13 -08003587 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003589
David S. Milleree74baa2007-01-01 20:51:53 -08003590 INIT_LIST_HEAD(&t->if_list);
3591
Cong Wang4e58a022013-01-28 19:55:53 +00003592 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003593 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003594
Eric Dumazet94dcf292011-03-22 16:30:45 -07003595 p = kthread_create_on_node(pktgen_thread_worker,
3596 t,
3597 cpu_to_node(cpu),
3598 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003599 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003600 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003601 list_del(&t->th_list);
3602 kfree(t);
3603 return PTR_ERR(p);
3604 }
3605 kthread_bind(p, cpu);
3606 t->tsk = p;
3607
Cong Wang4e58a022013-01-28 19:55:53 +00003608 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003609 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003610 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003611 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003612 PG_PROC_DIR, t->tsk->comm);
3613 kthread_stop(p);
3614 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615 kfree(t);
3616 return -EINVAL;
3617 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003618
Cong Wang4e58a022013-01-28 19:55:53 +00003619 t->net = pn;
David S. Milleree74baa2007-01-01 20:51:53 -08003620 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003621 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623 return 0;
3624}
3625
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003626/*
3627 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003629static void _rem_dev_from_if_list(struct pktgen_thread *t,
3630 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003632 struct list_head *q, *n;
3633 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003635 list_for_each_safe(q, n, &t->if_list) {
3636 p = list_entry(q, struct pktgen_dev, list);
3637 if (p == pkt_dev)
3638 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
3640}
3641
Luiz Capitulino222f1802006-03-20 22:16:13 -08003642static int pktgen_remove_device(struct pktgen_thread *t,
3643 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003645 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003648 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003649 pktgen_stop_device(pkt_dev);
3650 }
3651
3652 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
3654 if (pkt_dev->odev) {
3655 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003656 pkt_dev->odev = NULL;
3657 }
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 /* And update the thread if_list */
3660
3661 _rem_dev_from_if_list(t, pkt_dev);
3662
Stephen Hemminger39df2322007-03-04 16:11:51 -08003663 if (pkt_dev->entry)
David Howellsa8ca16e2013-04-12 17:27:28 +01003664 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003666#ifdef CONFIG_XFRM
3667 free_SAs(pkt_dev);
3668#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003669 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003670 if (pkt_dev->page)
3671 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674}
3675
Cong Wang4e58a022013-01-28 19:55:53 +00003676static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
Cong Wang4e58a022013-01-28 19:55:53 +00003678 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003679 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003680 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003681
Cong Wang4e58a022013-01-28 19:55:53 +00003682 pn->net = net;
3683 INIT_LIST_HEAD(&pn->pktgen_threads);
3684 pn->pktgen_exiting = false;
3685 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3686 if (!pn->proc_dir) {
3687 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003688 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003689 }
Cong Wang4e58a022013-01-28 19:55:53 +00003690 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3691 if (pe == NULL) {
3692 pr_err("cannot create %s procfs entry\n", PGCTRL);
3693 ret = -EINVAL;
3694 goto remove;
3695 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003696
John Hawkes670c02c2005-10-13 09:30:31 -07003697 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003698 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Cong Wang4e58a022013-01-28 19:55:53 +00003700 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003701 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003702 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003703 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003704 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003705
Cong Wang4e58a022013-01-28 19:55:53 +00003706 if (list_empty(&pn->pktgen_threads)) {
3707 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003708 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003709 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003710 }
3711
Luiz Capitulino222f1802006-03-20 22:16:13 -08003712 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003713
Cong Wang4e58a022013-01-28 19:55:53 +00003714remove_entry:
3715 remove_proc_entry(PGCTRL, pn->proc_dir);
3716remove:
Gao fengece31ff2013-02-18 01:34:56 +00003717 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003718 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719}
3720
Cong Wang4e58a022013-01-28 19:55:53 +00003721static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722{
Cong Wang4e58a022013-01-28 19:55:53 +00003723 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003724 struct pktgen_thread *t;
3725 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003726 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Luiz Capitulino222f1802006-03-20 22:16:13 -08003728 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003729 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Eric Dumazetc57b5462012-05-09 13:29:51 +00003731 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003732 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003733 mutex_unlock(&pktgen_thread_lock);
3734
3735 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003736 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003737 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003738 kthread_stop(t->tsk);
3739 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Cong Wang4e58a022013-01-28 19:55:53 +00003742 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003743 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003744}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
Cong Wang4e58a022013-01-28 19:55:53 +00003746static struct pernet_operations pg_net_ops = {
3747 .init = pg_net_init,
3748 .exit = pg_net_exit,
3749 .id = &pg_net_id,
3750 .size = sizeof(struct pktgen_net),
3751};
3752
3753static int __init pg_init(void)
3754{
3755 int ret = 0;
3756
3757 pr_info("%s", version);
3758 ret = register_pernet_subsys(&pg_net_ops);
3759 if (ret)
3760 return ret;
3761 ret = register_netdevice_notifier(&pktgen_notifier_block);
3762 if (ret)
3763 unregister_pernet_subsys(&pg_net_ops);
3764
3765 return ret;
3766}
3767
3768static void __exit pg_cleanup(void)
3769{
3770 unregister_netdevice_notifier(&pktgen_notifier_block);
3771 unregister_pernet_subsys(&pg_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774module_init(pg_init);
3775module_exit(pg_cleanup);
3776
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003777MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778MODULE_DESCRIPTION("Packet Generator tool");
3779MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003780MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003782MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003784MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003786MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003788MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");