blob: b553c36ea0caefab51633d989dad673651a4509f [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 Viro252e3342006-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 Viro252e3342006-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 Viro252e3342006-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;
Fan Ducf93d472014-01-03 11:18:31 +0800393 struct dst_entry dst;
394 struct dst_ops dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700395#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800396 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};
398
399struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800400 __be32 pgh_magic;
401 __be32 seq_num;
402 __be32 tv_sec;
403 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
Cong Wang4e58a022013-01-28 19:55:53 +0000406
407static int pg_net_id __read_mostly;
408
409struct pktgen_net {
410 struct net *net;
411 struct proc_dir_entry *proc_dir;
412 struct list_head pktgen_threads;
413 bool pktgen_exiting;
414};
Eric Dumazet551eaff2010-11-21 10:26:44 -0800415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000417 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800418 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800419 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800420 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800421 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000423 /* Field for thread to receive "posted" events terminate,
424 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800425
426 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 int cpu;
428
Luiz Capitulino222f1802006-03-20 22:16:13 -0800429 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700430 struct completion start_done;
Cong Wang4e58a022013-01-28 19:55:53 +0000431 struct pktgen_net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432};
433
434#define REMOVE 1
435#define FIND 0
436
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000437static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000438 "Packet Generator for packet performance testing. "
439 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Luiz Capitulino222f1802006-03-20 22:16:13 -0800441static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
442static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
443static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800444 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
Cong Wang4e58a022013-01-28 19:55:53 +0000446static void pktgen_run_all_threads(struct pktgen_net *pn);
447static void pktgen_reset_all_threads(struct pktgen_net *pn);
448static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000449
Luiz Capitulino222f1802006-03-20 22:16:13 -0800450static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000454static int pg_count_d __read_mostly = 1000;
455static int pg_delay_d __read_mostly;
456static int pg_clone_skb_d __read_mostly;
457static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Luiz Capitulino222fa072006-03-20 22:24:27 -0800459static DEFINE_MUTEX(pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static struct notifier_block pktgen_notifier_block = {
462 .notifier_call = pktgen_device_event,
463};
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900466 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *
468 */
469
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700470static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800471{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000472 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000476static ssize_t pgctrl_write(struct file *file, const char __user *buf,
477 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700480 char data[128];
Cong Wang4e58a022013-01-28 19:55:53 +0000481 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Luiz Capitulino222f1802006-03-20 22:16:13 -0800483 if (!capable(CAP_NET_ADMIN)) {
484 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700488 if (count > sizeof(data))
489 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700492 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700493 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800494 }
495 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Luiz Capitulino222f1802006-03-20 22:16:13 -0800497 if (!strcmp(data, "stop"))
Cong Wang4e58a022013-01-28 19:55:53 +0000498 pktgen_stop_all_threads_ifs(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Luiz Capitulino222f1802006-03-20 22:16:13 -0800500 else if (!strcmp(data, "start"))
Cong Wang4e58a022013-01-28 19:55:53 +0000501 pktgen_run_all_threads(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800503 else if (!strcmp(data, "reset"))
Cong Wang4e58a022013-01-28 19:55:53 +0000504 pktgen_reset_all_threads(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800505
Luiz Capitulino222f1802006-03-20 22:16:13 -0800506 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000507 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 err = count;
510
Luiz Capitulino222f1802006-03-20 22:16:13 -0800511out:
512 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700515static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Al Virod9dda782013-03-31 18:16:14 -0400517 return single_open(file, pgctrl_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700518}
519
Arjan van de Ven9a321442007-02-12 00:55:35 -0800520static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800521 .owner = THIS_MODULE,
522 .open = pgctrl_open,
523 .read = seq_read,
524 .llseek = seq_lseek,
525 .write = pgctrl_write,
526 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700527};
528
529static int pktgen_if_show(struct seq_file *seq, void *v)
530{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000531 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000532 ktime_t stopped;
533 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Luiz Capitulino222f1802006-03-20 22:16:13 -0800535 seq_printf(seq,
536 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
537 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
538 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Luiz Capitulino222f1802006-03-20 22:16:13 -0800540 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000541 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
542 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000543 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Luiz Capitulino222f1802006-03-20 22:16:13 -0800545 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
546 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Robert Olsson45b270f2007-08-28 15:45:55 -0700548 seq_printf(seq,
549 " queue_map_min: %u queue_map_max: %u\n",
550 pkt_dev->queue_map_min,
551 pkt_dev->queue_map_max);
552
John Fastabend9e50e3a2010-11-16 19:12:28 +0000553 if (pkt_dev->skb_priority)
554 seq_printf(seq, " skb_priority: %u\n",
555 pkt_dev->skb_priority);
556
Luiz Capitulino222f1802006-03-20 22:16:13 -0800557 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800558 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000559 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
560 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
561 &pkt_dev->in6_saddr,
562 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
563 &pkt_dev->in6_daddr,
564 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000565 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800566 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000567 " dst_min: %s dst_max: %s\n",
568 pkt_dev->dst_min, pkt_dev->dst_max);
569 seq_printf(seq,
570 " src_min: %s src_max: %s\n",
571 pkt_dev->src_min, pkt_dev->src_max);
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700574 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Johannes Berge1749612008-10-27 15:59:26 -0700576 seq_printf(seq, "%pM ",
577 is_zero_ether_addr(pkt_dev->src_mac) ?
578 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Luiz Capitulino222f1802006-03-20 22:16:13 -0800580 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700581 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Luiz Capitulino222f1802006-03-20 22:16:13 -0800583 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000584 " udp_src_min: %d udp_src_max: %d"
585 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800586 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
587 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Luiz Capitulino222f1802006-03-20 22:16:13 -0800589 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800590 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700591 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800593 if (pkt_dev->nr_labels) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000594 unsigned int i;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800595 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700596 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800597 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
598 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
599 }
600
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000601 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700602 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000603 pkt_dev->vlan_id, pkt_dev->vlan_p,
604 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700605
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000606 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700607 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000608 pkt_dev->svlan_id, pkt_dev->svlan_p,
609 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700610
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000611 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700612 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700613
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000614 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700615 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700616
Robert Olssone99b99b2010-03-18 22:44:30 +0000617 if (pkt_dev->node >= 0)
618 seq_printf(seq, " node: %d\n", pkt_dev->node);
619
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800620 seq_printf(seq, " Flags: ");
621
Luiz Capitulino222f1802006-03-20 22:16:13 -0800622 if (pkt_dev->flags & F_IPV6)
623 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Luiz Capitulino222f1802006-03-20 22:16:13 -0800625 if (pkt_dev->flags & F_IPSRC_RND)
626 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Luiz Capitulino222f1802006-03-20 22:16:13 -0800628 if (pkt_dev->flags & F_IPDST_RND)
629 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Luiz Capitulino222f1802006-03-20 22:16:13 -0800631 if (pkt_dev->flags & F_TXSIZE_RND)
632 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Luiz Capitulino222f1802006-03-20 22:16:13 -0800634 if (pkt_dev->flags & F_UDPSRC_RND)
635 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Luiz Capitulino222f1802006-03-20 22:16:13 -0800637 if (pkt_dev->flags & F_UDPDST_RND)
638 seq_printf(seq, "UDPDST_RND ");
639
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200640 if (pkt_dev->flags & F_UDPCSUM)
641 seq_printf(seq, "UDPCSUM ");
642
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800643 if (pkt_dev->flags & F_MPLS_RND)
644 seq_printf(seq, "MPLS_RND ");
645
Robert Olsson45b270f2007-08-28 15:45:55 -0700646 if (pkt_dev->flags & F_QUEUE_MAP_RND)
647 seq_printf(seq, "QUEUE_MAP_RND ");
648
Robert Olssone6fce5b2008-08-07 02:23:01 -0700649 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
650 seq_printf(seq, "QUEUE_MAP_CPU ");
651
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700652 if (pkt_dev->cflows) {
653 if (pkt_dev->flags & F_FLOW_SEQ)
654 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
655 else
656 seq_printf(seq, "FLOW_RND ");
657 }
658
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700659#ifdef CONFIG_XFRM
660 if (pkt_dev->flags & F_IPSEC_ON)
661 seq_printf(seq, "IPSEC ");
662#endif
663
Luiz Capitulino222f1802006-03-20 22:16:13 -0800664 if (pkt_dev->flags & F_MACSRC_RND)
665 seq_printf(seq, "MACSRC_RND ");
666
667 if (pkt_dev->flags & F_MACDST_RND)
668 seq_printf(seq, "MACDST_RND ");
669
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700670 if (pkt_dev->flags & F_VID_RND)
671 seq_printf(seq, "VID_RND ");
672
673 if (pkt_dev->flags & F_SVID_RND)
674 seq_printf(seq, "SVID_RND ");
675
Robert Olssone99b99b2010-03-18 22:44:30 +0000676 if (pkt_dev->flags & F_NODE)
677 seq_printf(seq, "NODE_ALLOC ");
678
Luiz Capitulino222f1802006-03-20 22:16:13 -0800679 seq_puts(seq, "\n");
680
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000681 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000682 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000683 idle = pkt_dev->idle_acc;
684 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800685
686 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000687 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800688 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000689 (unsigned long long)pkt_dev->errors);
690
691 seq_printf(seq,
692 " started: %lluus stopped: %lluus idle: %lluus\n",
693 (unsigned long long) ktime_to_us(pkt_dev->started_at),
694 (unsigned long long) ktime_to_us(stopped),
695 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800696
697 seq_printf(seq,
698 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700699 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
700 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Luiz Capitulino222f1802006-03-20 22:16:13 -0800702 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000703 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
704 &pkt_dev->cur_in6_saddr,
705 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800706 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000707 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
708 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Luiz Capitulino222f1802006-03-20 22:16:13 -0800710 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700711 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Robert Olsson45b270f2007-08-28 15:45:55 -0700713 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
714
Luiz Capitulino222f1802006-03-20 22:16:13 -0800715 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800718 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800720 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800725
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000726static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
727 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800728{
729 int i = 0;
730 *num = 0;
731
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700732 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000733 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800734 char c;
735 *num <<= 4;
736 if (get_user(c, &user_buffer[i]))
737 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000738 value = hex_to_bin(c);
739 if (value >= 0)
740 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800741 else
742 break;
743 }
744 return i;
745}
746
Luiz Capitulino222f1802006-03-20 22:16:13 -0800747static int count_trail_chars(const char __user * user_buffer,
748 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 int i;
751
752 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800753 char c;
754 if (get_user(c, &user_buffer[i]))
755 return -EFAULT;
756 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 case '\"':
758 case '\n':
759 case '\r':
760 case '\t':
761 case ' ':
762 case '=':
763 break;
764 default:
765 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768done:
769 return i;
770}
771
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000772static long num_arg(const char __user *user_buffer, unsigned long maxlen,
773 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000775 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800777
Paul Gortmakerd6182222010-10-18 12:14:44 +0000778 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800779 char c;
780 if (get_user(c, &user_buffer[i]))
781 return -EFAULT;
782 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800784 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 } else
786 break;
787 }
788 return i;
789}
790
Luiz Capitulino222f1802006-03-20 22:16:13 -0800791static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000793 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Paul Gortmakerd6182222010-10-18 12:14:44 +0000795 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800796 char c;
797 if (get_user(c, &user_buffer[i]))
798 return -EFAULT;
799 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 case '\"':
801 case '\n':
802 case '\r':
803 case '\t':
804 case ' ':
805 goto done_str;
806 break;
807 default:
808 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return i;
813}
814
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800815static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
816{
Eric Dumazet95c96172012-04-15 05:58:06 +0000817 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800818 char c;
819 ssize_t i = 0;
820 int len;
821
822 pkt_dev->nr_labels = 0;
823 do {
824 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700825 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800826 if (len <= 0)
827 return len;
828 pkt_dev->labels[n] = htonl(tmp);
829 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
830 pkt_dev->flags |= F_MPLS_RND;
831 i += len;
832 if (get_user(c, &buffer[i]))
833 return -EFAULT;
834 i++;
835 n++;
836 if (n >= MAX_MPLS_LABELS)
837 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700838 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800839
840 pkt_dev->nr_labels = n;
841 return i;
842}
843
Luiz Capitulino222f1802006-03-20 22:16:13 -0800844static ssize_t pktgen_if_write(struct file *file,
845 const char __user * user_buffer, size_t count,
846 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Joe Perches8a994a72010-07-12 10:50:23 +0000848 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800849 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000850 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 char name[16], valstr[32];
852 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853 char *pg_result = NULL;
854 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800856
857 pg_result = &(pkt_dev->result[0]);
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000860 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -EINVAL;
862 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863
Paul Gortmakerd6182222010-10-18 12:14:44 +0000864 max = count;
865 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800866 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000867 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800868 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000870 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* Read variable name */
873
874 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000875 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800876 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800879 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return -EFAULT;
881 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800882
883 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800885 if (len < 0)
886 return len;
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 i += len;
889
890 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000891 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700892 char tb[copy + 1];
893 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700895 tb[copy] = 0;
Joe Perchesf342cda2012-05-16 17:50:41 +0000896 pr_debug("%s,%lu buffer -:%s:-\n",
897 name, (unsigned long)count, tb);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (!strcmp(name, "min_pkt_size")) {
901 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000902 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800903 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800906 if (value < 14 + 20 + 8)
907 value = 14 + 20 + 8;
908 if (value != pkt_dev->min_pkt_size) {
909 pkt_dev->min_pkt_size = value;
910 pkt_dev->cur_pkt_size = value;
911 }
912 sprintf(pg_result, "OK: min_pkt_size=%u",
913 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return count;
915 }
916
Luiz Capitulino222f1802006-03-20 22:16:13 -0800917 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000919 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800920 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800923 if (value < 14 + 20 + 8)
924 value = 14 + 20 + 8;
925 if (value != pkt_dev->max_pkt_size) {
926 pkt_dev->max_pkt_size = value;
927 pkt_dev->cur_pkt_size = value;
928 }
929 sprintf(pg_result, "OK: max_pkt_size=%u",
930 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return count;
932 }
933
Luiz Capitulino222f1802006-03-20 22:16:13 -0800934 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (!strcmp(name, "pkt_size")) {
937 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000938 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800939 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800942 if (value < 14 + 20 + 8)
943 value = 14 + 20 + 8;
944 if (value != pkt_dev->min_pkt_size) {
945 pkt_dev->min_pkt_size = value;
946 pkt_dev->max_pkt_size = value;
947 pkt_dev->cur_pkt_size = value;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
950 return count;
951 }
952
Luiz Capitulino222f1802006-03-20 22:16:13 -0800953 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000955 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800959 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 sprintf(pg_result, "OK: debug=%u", debug);
961 return count;
962 }
963
Luiz Capitulino222f1802006-03-20 22:16:13 -0800964 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000966 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800967 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 i += len;
970 pkt_dev->nfrags = value;
971 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
972 return count;
973 }
974 if (!strcmp(name, "delay")) {
975 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000976 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800977 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000980 if (value == 0x7FFFFFFF)
981 pkt_dev->delay = ULLONG_MAX;
982 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000983 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000984
985 sprintf(pg_result, "OK: delay=%llu",
986 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return count;
988 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000989 if (!strcmp(name, "rate")) {
990 len = num_arg(&user_buffer[i], 10, &value);
991 if (len < 0)
992 return len;
993
994 i += len;
995 if (!value)
996 return len;
997 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
998 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000999 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001000
1001 sprintf(pg_result, "OK: rate=%lu", value);
1002 return count;
1003 }
1004 if (!strcmp(name, "ratep")) {
1005 len = num_arg(&user_buffer[i], 10, &value);
1006 if (len < 0)
1007 return len;
1008
1009 i += len;
1010 if (!value)
1011 return len;
1012 pkt_dev->delay = NSEC_PER_SEC/value;
1013 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001014 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001015
1016 sprintf(pg_result, "OK: rate=%lu", value);
1017 return count;
1018 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001019 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001021 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001022 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001025 if (value != pkt_dev->udp_src_min) {
1026 pkt_dev->udp_src_min = value;
1027 pkt_dev->cur_udp_src = value;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1030 return count;
1031 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001032 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001034 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001035 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001038 if (value != pkt_dev->udp_dst_min) {
1039 pkt_dev->udp_dst_min = value;
1040 pkt_dev->cur_udp_dst = value;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1043 return count;
1044 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001047 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001048 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001051 if (value != pkt_dev->udp_src_max) {
1052 pkt_dev->udp_src_max = value;
1053 pkt_dev->cur_udp_src = value;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1056 return count;
1057 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001058 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001060 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001061 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001064 if (value != pkt_dev->udp_dst_max) {
1065 pkt_dev->udp_dst_max = value;
1066 pkt_dev->cur_udp_dst = value;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1069 return count;
1070 }
1071 if (!strcmp(name, "clone_skb")) {
1072 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001073 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001074 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001075 if ((value > 0) &&
1076 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1077 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001079 pkt_dev->clone_skb = value;
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1082 return count;
1083 }
1084 if (!strcmp(name, "count")) {
1085 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001086 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001087 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 i += len;
1090 pkt_dev->count = value;
1091 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001092 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return count;
1094 }
1095 if (!strcmp(name, "src_mac_count")) {
1096 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001097 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001098 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 i += len;
1101 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001102 pkt_dev->src_mac_count = value;
1103 pkt_dev->cur_src_mac_offset = 0;
1104 }
1105 sprintf(pg_result, "OK: src_mac_count=%d",
1106 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return count;
1108 }
1109 if (!strcmp(name, "dst_mac_count")) {
1110 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001111 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001112 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 i += len;
1115 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001116 pkt_dev->dst_mac_count = value;
1117 pkt_dev->cur_dst_mac_offset = 0;
1118 }
1119 sprintf(pg_result, "OK: dst_mac_count=%d",
1120 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return count;
1122 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001123 if (!strcmp(name, "node")) {
1124 len = num_arg(&user_buffer[i], 10, &value);
1125 if (len < 0)
1126 return len;
1127
1128 i += len;
1129
1130 if (node_possible(value)) {
1131 pkt_dev->node = value;
1132 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001133 if (pkt_dev->page) {
1134 put_page(pkt_dev->page);
1135 pkt_dev->page = NULL;
1136 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001137 }
1138 else
1139 sprintf(pg_result, "ERROR: node not possible");
1140 return count;
1141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001143 char f[32];
1144 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001146 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001147 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (copy_from_user(f, &user_buffer[i], len))
1150 return -EFAULT;
1151 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001152 if (strcmp(f, "IPSRC_RND") == 0)
1153 pkt_dev->flags |= F_IPSRC_RND;
1154
1155 else if (strcmp(f, "!IPSRC_RND") == 0)
1156 pkt_dev->flags &= ~F_IPSRC_RND;
1157
1158 else if (strcmp(f, "TXSIZE_RND") == 0)
1159 pkt_dev->flags |= F_TXSIZE_RND;
1160
1161 else if (strcmp(f, "!TXSIZE_RND") == 0)
1162 pkt_dev->flags &= ~F_TXSIZE_RND;
1163
1164 else if (strcmp(f, "IPDST_RND") == 0)
1165 pkt_dev->flags |= F_IPDST_RND;
1166
1167 else if (strcmp(f, "!IPDST_RND") == 0)
1168 pkt_dev->flags &= ~F_IPDST_RND;
1169
1170 else if (strcmp(f, "UDPSRC_RND") == 0)
1171 pkt_dev->flags |= F_UDPSRC_RND;
1172
1173 else if (strcmp(f, "!UDPSRC_RND") == 0)
1174 pkt_dev->flags &= ~F_UDPSRC_RND;
1175
1176 else if (strcmp(f, "UDPDST_RND") == 0)
1177 pkt_dev->flags |= F_UDPDST_RND;
1178
1179 else if (strcmp(f, "!UDPDST_RND") == 0)
1180 pkt_dev->flags &= ~F_UDPDST_RND;
1181
1182 else if (strcmp(f, "MACSRC_RND") == 0)
1183 pkt_dev->flags |= F_MACSRC_RND;
1184
1185 else if (strcmp(f, "!MACSRC_RND") == 0)
1186 pkt_dev->flags &= ~F_MACSRC_RND;
1187
1188 else if (strcmp(f, "MACDST_RND") == 0)
1189 pkt_dev->flags |= F_MACDST_RND;
1190
1191 else if (strcmp(f, "!MACDST_RND") == 0)
1192 pkt_dev->flags &= ~F_MACDST_RND;
1193
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001194 else if (strcmp(f, "MPLS_RND") == 0)
1195 pkt_dev->flags |= F_MPLS_RND;
1196
1197 else if (strcmp(f, "!MPLS_RND") == 0)
1198 pkt_dev->flags &= ~F_MPLS_RND;
1199
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001200 else if (strcmp(f, "VID_RND") == 0)
1201 pkt_dev->flags |= F_VID_RND;
1202
1203 else if (strcmp(f, "!VID_RND") == 0)
1204 pkt_dev->flags &= ~F_VID_RND;
1205
1206 else if (strcmp(f, "SVID_RND") == 0)
1207 pkt_dev->flags |= F_SVID_RND;
1208
1209 else if (strcmp(f, "!SVID_RND") == 0)
1210 pkt_dev->flags &= ~F_SVID_RND;
1211
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001212 else if (strcmp(f, "FLOW_SEQ") == 0)
1213 pkt_dev->flags |= F_FLOW_SEQ;
1214
Robert Olsson45b270f2007-08-28 15:45:55 -07001215 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1216 pkt_dev->flags |= F_QUEUE_MAP_RND;
1217
1218 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1219 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001220
1221 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1222 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1223
1224 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1225 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001226#ifdef CONFIG_XFRM
1227 else if (strcmp(f, "IPSEC") == 0)
1228 pkt_dev->flags |= F_IPSEC_ON;
1229#endif
1230
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001231 else if (strcmp(f, "!IPV6") == 0)
1232 pkt_dev->flags &= ~F_IPV6;
1233
Robert Olssone99b99b2010-03-18 22:44:30 +00001234 else if (strcmp(f, "NODE_ALLOC") == 0)
1235 pkt_dev->flags |= F_NODE;
1236
1237 else if (strcmp(f, "!NODE_ALLOC") == 0)
1238 pkt_dev->flags &= ~F_NODE;
1239
Thomas Grafc26bf4a2013-07-25 18:12:18 +02001240 else if (strcmp(f, "UDPCSUM") == 0)
1241 pkt_dev->flags |= F_UDPCSUM;
1242
1243 else if (strcmp(f, "!UDPCSUM") == 0)
1244 pkt_dev->flags &= ~F_UDPCSUM;
1245
Luiz Capitulino222f1802006-03-20 22:16:13 -08001246 else {
1247 sprintf(pg_result,
1248 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1249 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001250 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001251 "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 -08001252 return count;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1255 return count;
1256 }
1257 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1258 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001259 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001264 buf[len] = 0;
1265 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1266 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1267 strncpy(pkt_dev->dst_min, buf, len);
1268 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1269 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1270 }
1271 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001272 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001273 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1275 return count;
1276 }
1277 if (!strcmp(name, "dst_max")) {
1278 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001279 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001280 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Luiz Capitulino222f1802006-03-20 22:16:13 -08001283 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EFAULT;
1285
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286 buf[len] = 0;
1287 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1288 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1289 strncpy(pkt_dev->dst_max, buf, len);
1290 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1291 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1292 }
1293 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001294 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 i += len;
1296 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1297 return count;
1298 }
1299 if (!strcmp(name, "dst6")) {
1300 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001301 if (len < 0)
1302 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 pkt_dev->flags |= F_IPV6;
1305
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Amerigo Wangc468fb12012-10-09 17:48:20 +00001310 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001311 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001313 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Luiz Capitulino222f1802006-03-20 22:16:13 -08001315 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001316 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Luiz Capitulino222f1802006-03-20 22:16:13 -08001318 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 sprintf(pg_result, "OK: dst6=%s", buf);
1320 return count;
1321 }
1322 if (!strcmp(name, "dst6_min")) {
1323 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001324 if (len < 0)
1325 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 pkt_dev->flags |= F_IPV6;
1328
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001331 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Amerigo Wangc468fb12012-10-09 17:48:20 +00001333 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001334 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001336 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001338 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Luiz Capitulino222f1802006-03-20 22:16:13 -08001340 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 sprintf(pg_result, "OK: dst6_min=%s", buf);
1342 return count;
1343 }
1344 if (!strcmp(name, "dst6_max")) {
1345 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001346 if (len < 0)
1347 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 pkt_dev->flags |= F_IPV6;
1350
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001353 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Amerigo Wangc468fb12012-10-09 17:48:20 +00001355 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001356 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Luiz Capitulino222f1802006-03-20 22:16:13 -08001358 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001359 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Luiz Capitulino222f1802006-03-20 22:16:13 -08001361 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 sprintf(pg_result, "OK: dst6_max=%s", buf);
1363 return count;
1364 }
1365 if (!strcmp(name, "src6")) {
1366 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001367 if (len < 0)
1368 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 pkt_dev->flags |= F_IPV6;
1371
Luiz Capitulino222f1802006-03-20 22:16:13 -08001372 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001374 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Amerigo Wangc468fb12012-10-09 17:48:20 +00001376 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001377 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001379 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001382 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001383
1384 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 sprintf(pg_result, "OK: src6=%s", buf);
1386 return count;
1387 }
1388 if (!strcmp(name, "src_min")) {
1389 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001390 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001391 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001392
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001395 buf[len] = 0;
1396 if (strcmp(buf, pkt_dev->src_min) != 0) {
1397 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1398 strncpy(pkt_dev->src_min, buf, len);
1399 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1400 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1401 }
1402 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001403 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 i += len;
1405 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1406 return count;
1407 }
1408 if (!strcmp(name, "src_max")) {
1409 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001410 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001411 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001412
Luiz Capitulino222f1802006-03-20 22:16:13 -08001413 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001415 buf[len] = 0;
1416 if (strcmp(buf, pkt_dev->src_max) != 0) {
1417 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1418 strncpy(pkt_dev->src_max, buf, len);
1419 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1420 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1421 }
1422 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001423 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 i += len;
1425 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1426 return count;
1427 }
1428 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001430 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001431 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001434 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001437 if (!mac_pton(valstr, pkt_dev->dst_mac))
1438 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /* Set up Dest MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001440 memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001441
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001442 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return count;
1444 }
1445 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001447 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001451 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001454 if (!mac_pton(valstr, pkt_dev->src_mac))
1455 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001456 /* Set up Src MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001457 memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001458
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001459 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return count;
1461 }
1462
Luiz Capitulino222f1802006-03-20 22:16:13 -08001463 if (!strcmp(name, "clear_counters")) {
1464 pktgen_clear_counters(pkt_dev);
1465 sprintf(pg_result, "OK: Clearing counters.\n");
1466 return count;
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if (!strcmp(name, "flows")) {
1470 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001471 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001472 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 i += len;
1475 if (value > MAX_CFLOWS)
1476 value = MAX_CFLOWS;
1477
1478 pkt_dev->cflows = value;
1479 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1480 return count;
1481 }
1482
Fan Dude4aee72014-01-03 11:18:30 +08001483 if (!strcmp(name, "spi")) {
1484 len = num_arg(&user_buffer[i], 10, &value);
1485 if (len < 0)
1486 return len;
1487
1488 i += len;
1489 pkt_dev->spi = value;
1490 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1491 return count;
1492 }
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!strcmp(name, "flowlen")) {
1495 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001496 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001497 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 i += len;
1500 pkt_dev->lflow = value;
1501 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1502 return count;
1503 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001504
Robert Olsson45b270f2007-08-28 15:45:55 -07001505 if (!strcmp(name, "queue_map_min")) {
1506 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001507 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001508 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001509
Robert Olsson45b270f2007-08-28 15:45:55 -07001510 i += len;
1511 pkt_dev->queue_map_min = value;
1512 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1513 return count;
1514 }
1515
1516 if (!strcmp(name, "queue_map_max")) {
1517 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001518 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001519 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001520
Robert Olsson45b270f2007-08-28 15:45:55 -07001521 i += len;
1522 pkt_dev->queue_map_max = value;
1523 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1524 return count;
1525 }
1526
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001527 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001528 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001529
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001530 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001531 if (len < 0)
1532 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001533 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001534 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001535 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001536 cnt += sprintf(pg_result + cnt,
1537 "%08x%s", ntohl(pkt_dev->labels[n]),
1538 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001539
1540 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1541 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1542 pkt_dev->svlan_id = 0xffff;
1543
1544 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001545 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001546 }
1547 return count;
1548 }
1549
1550 if (!strcmp(name, "vlan_id")) {
1551 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001552 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001553 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001554
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001555 i += len;
1556 if (value <= 4095) {
1557 pkt_dev->vlan_id = value; /* turn on VLAN */
1558
1559 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001560 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001561
1562 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001563 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001564
1565 pkt_dev->nr_labels = 0; /* turn off MPLS */
1566 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1567 } else {
1568 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1569 pkt_dev->svlan_id = 0xffff;
1570
1571 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001572 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001573 }
1574 return count;
1575 }
1576
1577 if (!strcmp(name, "vlan_p")) {
1578 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001579 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001580 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001581
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001582 i += len;
1583 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1584 pkt_dev->vlan_p = value;
1585 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1586 } else {
1587 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1588 }
1589 return count;
1590 }
1591
1592 if (!strcmp(name, "vlan_cfi")) {
1593 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001594 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001595 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001596
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001597 i += len;
1598 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1599 pkt_dev->vlan_cfi = value;
1600 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1601 } else {
1602 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1603 }
1604 return count;
1605 }
1606
1607 if (!strcmp(name, "svlan_id")) {
1608 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001609 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001610 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001611
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001612 i += len;
1613 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1614 pkt_dev->svlan_id = value; /* turn on SVLAN */
1615
1616 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001617 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001618
1619 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001620 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001621
1622 pkt_dev->nr_labels = 0; /* turn off MPLS */
1623 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1624 } else {
1625 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1626 pkt_dev->svlan_id = 0xffff;
1627
1628 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001629 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001630 }
1631 return count;
1632 }
1633
1634 if (!strcmp(name, "svlan_p")) {
1635 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001636 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001637 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001638
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001639 i += len;
1640 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1641 pkt_dev->svlan_p = value;
1642 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1643 } else {
1644 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1645 }
1646 return count;
1647 }
1648
1649 if (!strcmp(name, "svlan_cfi")) {
1650 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001651 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001652 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001653
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001654 i += len;
1655 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1656 pkt_dev->svlan_cfi = value;
1657 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1658 } else {
1659 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1660 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001661 return count;
1662 }
1663
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001664 if (!strcmp(name, "tos")) {
1665 __u32 tmp_value = 0;
1666 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001667 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001668 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001669
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001670 i += len;
1671 if (len == 2) {
1672 pkt_dev->tos = tmp_value;
1673 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1674 } else {
1675 sprintf(pg_result, "ERROR: tos must be 00-ff");
1676 }
1677 return count;
1678 }
1679
1680 if (!strcmp(name, "traffic_class")) {
1681 __u32 tmp_value = 0;
1682 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001683 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001684 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001685
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001686 i += len;
1687 if (len == 2) {
1688 pkt_dev->traffic_class = tmp_value;
1689 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1690 } else {
1691 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1692 }
1693 return count;
1694 }
1695
John Fastabend9e50e3a2010-11-16 19:12:28 +00001696 if (!strcmp(name, "skb_priority")) {
1697 len = num_arg(&user_buffer[i], 9, &value);
1698 if (len < 0)
1699 return len;
1700
1701 i += len;
1702 pkt_dev->skb_priority = value;
1703 sprintf(pg_result, "OK: skb_priority=%i",
1704 pkt_dev->skb_priority);
1705 return count;
1706 }
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1709 return -EINVAL;
1710}
1711
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001712static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Al Virod9dda782013-03-31 18:16:14 -04001714 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001715}
1716
Arjan van de Ven9a321442007-02-12 00:55:35 -08001717static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001718 .owner = THIS_MODULE,
1719 .open = pktgen_if_open,
1720 .read = seq_read,
1721 .llseek = seq_lseek,
1722 .write = pktgen_if_write,
1723 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001724};
1725
1726static int pktgen_thread_show(struct seq_file *seq, void *v)
1727{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001728 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001729 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001731 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Luiz Capitulino222f1802006-03-20 22:16:13 -08001733 seq_printf(seq, "Running: ");
1734
1735 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001736 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001737 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001738 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Luiz Capitulino222f1802006-03-20 22:16:13 -08001740 seq_printf(seq, "\nStopped: ");
1741
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001742 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001743 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001744 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001747 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001749 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Luiz Capitulino222f1802006-03-20 22:16:13 -08001751 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001756static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 const char __user * user_buffer,
1758 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Joe Perches8a994a72010-07-12 10:50:23 +00001760 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001761 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001762 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001767 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return -EINVAL;
1769 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001770
Paul Gortmakerd6182222010-10-18 12:14:44 +00001771 max = count;
1772 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001774 return len;
1775
Paul Gortmakerd6182222010-10-18 12:14:44 +00001776 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* Read variable name */
1779
1780 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001782 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 memset(name, 0, sizeof(name));
1785 if (copy_from_user(name, &user_buffer[i], len))
1786 return -EFAULT;
1787 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001788
Luiz Capitulino222f1802006-03-20 22:16:13 -08001789 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001792 return len;
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 i += len;
1795
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001796 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001797 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Luiz Capitulino222f1802006-03-20 22:16:13 -08001799 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001800 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 ret = -EINVAL;
1802 goto out;
1803 }
1804
1805 pg_result = &(t->result[0]);
1806
Luiz Capitulino222f1802006-03-20 22:16:13 -08001807 if (!strcmp(name, "add_device")) {
1808 char f[32];
1809 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001811 if (len < 0) {
1812 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto out;
1814 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001815 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return -EFAULT;
1817 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001818 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001819 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001820 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001821 if (!ret) {
1822 ret = count;
1823 sprintf(pg_result, "OK: add_device=%s", f);
1824 } else
1825 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto out;
1827 }
1828
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001830 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001831 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001832 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001833 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001835 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto out;
1837 }
1838
Luiz Capitulino222f1802006-03-20 22:16:13 -08001839 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001840 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001841 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 goto out;
1843 }
1844
1845 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return ret;
1848}
1849
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001850static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Al Virod9dda782013-03-31 18:16:14 -04001852 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Arjan van de Ven9a321442007-02-12 00:55:35 -08001855static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001856 .owner = THIS_MODULE,
1857 .open = pktgen_thread_open,
1858 .read = seq_read,
1859 .llseek = seq_lseek,
1860 .write = pktgen_thread_write,
1861 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001862};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001865static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1866 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
1868 struct pktgen_thread *t;
1869 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001870 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Cong Wang4e58a022013-01-28 19:55:53 +00001872 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001873 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001875 if (remove) {
1876 if_lock(t);
1877 pkt_dev->removal_mark = 1;
1878 t->control |= T_REMDEV;
1879 if_unlock(t);
1880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 break;
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001884 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001887/*
1888 * mark a device for removal
1889 */
Cong Wang4e58a022013-01-28 19:55:53 +00001890static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001893 const int max_tries = 10, msec_per_try = 125;
1894 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001895
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001896 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001897 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001898
Luiz Capitulino222f1802006-03-20 22:16:13 -08001899 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001900
Cong Wang4e58a022013-01-28 19:55:53 +00001901 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001902 if (pkt_dev == NULL)
1903 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001904
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001905 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001906 pr_debug("%s: waiting for %s to disappear....\n",
1907 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001908 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001909 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001910
1911 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001912 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1913 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001914 break;
1915 }
1916
1917 }
1918
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001919 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001920}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001921
Cong Wang4e58a022013-01-28 19:55:53 +00001922static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001923{
1924 struct pktgen_thread *t;
1925
Cong Wang4e58a022013-01-28 19:55:53 +00001926 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001927 struct pktgen_dev *pkt_dev;
1928
1929 list_for_each_entry(pkt_dev, &t->if_list, list) {
1930 if (pkt_dev->odev != dev)
1931 continue;
1932
David Howellsa8ca16e2013-04-12 17:27:28 +01001933 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001934
Alexey Dobriyan29753152009-08-28 23:34:43 -07001935 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00001936 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07001937 &pktgen_if_fops,
1938 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001939 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001940 pr_err("can't move proc entry for '%s'\n",
1941 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001942 break;
1943 }
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Luiz Capitulino222f1802006-03-20 22:16:13 -08001947static int pktgen_device_event(struct notifier_block *unused,
1948 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Jiri Pirko351638e2013-05-28 01:30:21 +00001950 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00001951 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Cong Wang4e58a022013-01-28 19:55:53 +00001953 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001954 return NOTIFY_DONE;
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /* It is OK that we do not hold the group lock right now,
1957 * as we run under the RTNL lock.
1958 */
1959
1960 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001961 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00001962 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00001966 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 return NOTIFY_DONE;
1971}
1972
Cong Wang4e58a022013-01-28 19:55:53 +00001973static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1974 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001975 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001976{
1977 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001978 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001979
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001980 for (i = 0; ifname[i] != '@'; i++) {
1981 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001982 break;
1983
1984 b[i] = ifname[i];
1985 }
1986 b[i] = 0;
1987
Cong Wang4e58a022013-01-28 19:55:53 +00001988 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07001989}
1990
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/* Associate pktgen_dev with a device. */
1993
Cong Wang4e58a022013-01-28 19:55:53 +00001994static int pktgen_setup_dev(const struct pktgen_net *pn,
1995 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001996{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001998 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (pkt_dev->odev) {
2002 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002003 pkt_dev->odev = NULL;
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Cong Wang4e58a022013-01-28 19:55:53 +00002006 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002008 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002009 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002013 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002014 err = -EINVAL;
2015 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002016 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002017 err = -ENETDOWN;
2018 } else {
2019 pkt_dev->odev = odev;
2020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002024 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027/* Read pkt_dev from the interface and set up internal pktgen_dev
2028 * structure to have the right information to create/send packets
2029 */
2030static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2031{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002032 int ntxq;
2033
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002035 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002036 sprintf(pkt_dev->result,
2037 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2038 return;
2039 }
2040
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002041 /* make sure that we don't pick a non-existing transmit queue */
2042 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002043
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002044 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002045 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2046 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2047 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002048 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002049 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002050 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002051 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2052 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2053 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002054 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002055 }
2056
Luiz Capitulino222f1802006-03-20 22:16:13 -08002057 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002059 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002060 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Luiz Capitulino222f1802006-03-20 22:16:13 -08002062 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002063 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Luiz Capitulino222f1802006-03-20 22:16:13 -08002065 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002066 int i, set = 0, err = 1;
2067 struct inet6_dev *idev;
2068
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002069 if (pkt_dev->min_pkt_size == 0) {
2070 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2071 + sizeof(struct udphdr)
2072 + sizeof(struct pktgen_hdr)
2073 + pkt_dev->pkt_overhead;
2074 }
2075
Luiz Capitulino222f1802006-03-20 22:16:13 -08002076 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2077 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 set = 1;
2079 break;
2080 }
2081
Luiz Capitulino222f1802006-03-20 22:16:13 -08002082 if (!set) {
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * Use linklevel address if unconfigured.
2086 *
2087 * use ipv6_get_lladdr if/when it's get exported
2088 */
2089
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002090 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002091 idev = __in6_dev_get(pkt_dev->odev);
2092 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 struct inet6_ifaddr *ifp;
2094
2095 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002096 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2097 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002098 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002099 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 err = 0;
2101 break;
2102 }
2103 }
2104 read_unlock_bh(&idev->lock);
2105 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002106 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002107 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002108 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002110 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002111 if (pkt_dev->min_pkt_size == 0) {
2112 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2113 + sizeof(struct udphdr)
2114 + sizeof(struct pktgen_hdr)
2115 + pkt_dev->pkt_overhead;
2116 }
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 pkt_dev->saddr_min = 0;
2119 pkt_dev->saddr_max = 0;
2120 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002121
2122 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002125 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (in_dev) {
2127 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002128 pkt_dev->saddr_min =
2129 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 pkt_dev->saddr_max = pkt_dev->saddr_min;
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002134 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2136 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2137 }
2138
2139 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2140 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2141 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002142 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002143 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2144 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2145 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2146
Luiz Capitulino222f1802006-03-20 22:16:13 -08002147 pkt_dev->cur_dst_mac_offset = 0;
2148 pkt_dev->cur_src_mac_offset = 0;
2149 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2150 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2151 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2152 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 pkt_dev->nflows = 0;
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002157static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2158{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002159 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002160 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002161 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002162
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002163 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2164 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002165
Daniel Turull43d28b62010-06-09 22:49:57 +00002166 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002167 if (remaining <= 0) {
2168 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002169 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002170 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002171
Daniel Borkmann398f3822012-10-28 08:27:19 +00002172 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002173 if (remaining < 100000) {
2174 /* for small delays (<100us), just loop until limit is reached */
2175 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002176 end_time = ktime_get();
2177 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002178 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002179 /* see do_nanosleep */
2180 hrtimer_init_sleeper(&t, current);
2181 do {
2182 set_current_state(TASK_INTERRUPTIBLE);
2183 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2184 if (!hrtimer_active(&t.timer))
2185 t.task = NULL;
2186
2187 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002190 hrtimer_cancel(&t.timer);
2191 } while (t.task && pkt_dev->running && !signal_pending(current));
2192 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002193 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002195
2196 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002197 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002200static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2201{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002202 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002203 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2204 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2205 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2206}
2207
Stephen Hemminger648fda72009-08-27 13:55:07 +00002208static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002209{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002210 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002211}
2212
2213static inline int f_pick(struct pktgen_dev *pkt_dev)
2214{
2215 int flow = pkt_dev->curfl;
2216
2217 if (pkt_dev->flags & F_FLOW_SEQ) {
2218 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2219 /* reset time */
2220 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002221 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002222 pkt_dev->curfl += 1;
2223 if (pkt_dev->curfl >= pkt_dev->cflows)
2224 pkt_dev->curfl = 0; /*reset */
2225 }
2226 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002227 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002228 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002229
Robert Olsson1211a642008-08-05 18:44:26 -07002230 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002231 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002232 pkt_dev->flows[flow].flags = 0;
2233 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002234 }
2235
2236 return pkt_dev->curfl;
2237}
2238
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002239
2240#ifdef CONFIG_XFRM
2241/* If there was already an IPSEC SA, we keep it as is, else
2242 * we go look for it ...
2243*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002244#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002245static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002246{
2247 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002248 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002249 if (!x) {
Fan Duc4549972014-01-03 11:18:32 +08002250
2251 if (pkt_dev->spi) {
2252 /* We need as quick as possible to find the right SA
2253 * Searching with minimum criteria to archieve this.
2254 */
2255 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2256 } else {
2257 /* slow path: we dont already have xfrm_state */
2258 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
2259 (xfrm_address_t *)&pkt_dev->cur_daddr,
2260 (xfrm_address_t *)&pkt_dev->cur_saddr,
2261 AF_INET,
2262 pkt_dev->ipsmode,
2263 pkt_dev->ipsproto, 0);
2264 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002265 if (x) {
2266 pkt_dev->flows[flow].x = x;
2267 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002268 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002269 }
2270
2271 }
2272}
2273#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002274static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2275{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002276
2277 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2278 pkt_dev->cur_queue_map = smp_processor_id();
2279
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002280 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002281 __u16 t;
2282 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002283 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002284 (pkt_dev->queue_map_max -
2285 pkt_dev->queue_map_min + 1)
2286 + pkt_dev->queue_map_min;
2287 } else {
2288 t = pkt_dev->cur_queue_map + 1;
2289 if (t > pkt_dev->queue_map_max)
2290 t = pkt_dev->queue_map_min;
2291 }
2292 pkt_dev->cur_queue_map = t;
2293 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002294 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297/* Increment/randomize headers according to flags and current values
2298 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2299 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002300static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2301{
2302 __u32 imn;
2303 __u32 imx;
2304 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002306 if (pkt_dev->cflows)
2307 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002310 if (pkt_dev->src_mac_count > 1) {
2311 __u32 mc;
2312 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Luiz Capitulino222f1802006-03-20 22:16:13 -08002314 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002315 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 else {
2317 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002318 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002319 pkt_dev->src_mac_count)
2320 pkt_dev->cur_src_mac_offset = 0;
2321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2324 pkt_dev->hh[11] = tmp;
2325 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2326 pkt_dev->hh[10] = tmp;
2327 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2328 pkt_dev->hh[9] = tmp;
2329 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2330 pkt_dev->hh[8] = tmp;
2331 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2332 pkt_dev->hh[7] = tmp;
2333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Luiz Capitulino222f1802006-03-20 22:16:13 -08002335 /* Deal with Destination MAC */
2336 if (pkt_dev->dst_mac_count > 1) {
2337 __u32 mc;
2338 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Luiz Capitulino222f1802006-03-20 22:16:13 -08002340 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002341 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Luiz Capitulino222f1802006-03-20 22:16:13 -08002343 else {
2344 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002345 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002346 pkt_dev->dst_mac_count) {
2347 pkt_dev->cur_dst_mac_offset = 0;
2348 }
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Luiz Capitulino222f1802006-03-20 22:16:13 -08002351 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2352 pkt_dev->hh[5] = tmp;
2353 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2354 pkt_dev->hh[4] = tmp;
2355 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2356 pkt_dev->hh[3] = tmp;
2357 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2358 pkt_dev->hh[2] = tmp;
2359 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2360 pkt_dev->hh[1] = tmp;
2361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002363 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002364 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002365 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002366 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2367 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002368 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002369 htonl(0x000fffff));
2370 }
2371
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002372 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002373 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002374 }
2375
2376 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002377 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002378 }
2379
Luiz Capitulino222f1802006-03-20 22:16:13 -08002380 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2381 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002382 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002383 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2384 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Luiz Capitulino222f1802006-03-20 22:16:13 -08002386 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 pkt_dev->cur_udp_src++;
2388 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2389 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002390 }
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Luiz Capitulino222f1802006-03-20 22:16:13 -08002393 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2394 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002395 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002396 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2397 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002398 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002402 }
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 if (!(pkt_dev->flags & F_IPV6)) {
2406
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002407 imn = ntohl(pkt_dev->saddr_min);
2408 imx = ntohl(pkt_dev->saddr_max);
2409 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002411 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002412 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 else {
2414 t = ntohl(pkt_dev->cur_saddr);
2415 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002416 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420 pkt_dev->cur_saddr = htonl(t);
2421 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002422
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002423 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2425 } else {
Al Viro252e3342006-11-14 20:48:11 -08002426 imn = ntohl(pkt_dev->daddr_min);
2427 imx = ntohl(pkt_dev->daddr_max);
2428 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002430 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (pkt_dev->flags & F_IPDST_RND) {
2432
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002433 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002434 t = prandom_u32() %
2435 (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002436 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002437 } while (ipv4_is_loopback(s) ||
2438 ipv4_is_multicast(s) ||
2439 ipv4_is_lbcast(s) ||
2440 ipv4_is_zeronet(s) ||
2441 ipv4_is_local_multicast(s));
Al Viro252e3342006-11-14 20:48:11 -08002442 pkt_dev->cur_daddr = s;
2443 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 t = ntohl(pkt_dev->cur_daddr);
2445 t++;
2446 if (t > imx) {
2447 t = imn;
2448 }
2449 pkt_dev->cur_daddr = htonl(t);
2450 }
2451 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002452 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002453 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002454 pkt_dev->flows[flow].cur_daddr =
2455 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002456#ifdef CONFIG_XFRM
2457 if (pkt_dev->flags & F_IPSEC_ON)
2458 get_ipsec_sa(pkt_dev, flow);
2459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 pkt_dev->nflows++;
2461 }
2462 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002463 } else { /* IPV6 * */
2464
Joe Perches06e30412012-10-18 17:55:31 +00002465 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 int i;
2467
2468 /* Only random destinations yet */
2469
Luiz Capitulino222f1802006-03-20 22:16:13 -08002470 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002472 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002473 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2474 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
Luiz Capitulino222f1802006-03-20 22:16:13 -08002479 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2480 __u32 t;
2481 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002482 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002483 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2484 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002485 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002487 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002489 }
2490 pkt_dev->cur_pkt_size = t;
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002493 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 pkt_dev->flows[flow].count++;
2496}
2497
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002498
2499#ifdef CONFIG_XFRM
Fan Ducf93d472014-01-03 11:18:31 +08002500u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2501
2502 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2503};
2504
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002505static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2506{
2507 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2508 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002509 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002510
2511 if (!x)
2512 return 0;
2513 /* XXX: we dont support tunnel mode for now until
2514 * we resolve the dst issue */
Fan Ducf93d472014-01-03 11:18:31 +08002515 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002516 return 0;
2517
Fan Ducf93d472014-01-03 11:18:31 +08002518 /* But when user specify an valid SPI, transformation
2519 * supports both transport/tunnel mode + ESP/AH type.
2520 */
2521 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2522 skb->_skb_refdst = (unsigned long)&pkt_dev->dst | SKB_DST_NOREF;
2523
2524 rcu_read_lock_bh();
Herbert Xu13996372007-10-17 21:35:51 -07002525 err = x->outer_mode->output(x, skb);
Fan Ducf93d472014-01-03 11:18:31 +08002526 rcu_read_unlock_bh();
Fan Du6de9ace2014-01-03 11:18:28 +08002527 if (err) {
2528 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002529 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002530 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002531 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002532 if (err) {
2533 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002534 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002535 }
Fan Du0af0a412014-01-03 11:18:27 +08002536 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002537 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002538 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002539 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002540error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002541 return err;
2542}
2543
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002544static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002545{
2546 if (pkt_dev->cflows) {
2547 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002548 int i;
2549 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002550 struct xfrm_state *x = pkt_dev->flows[i].x;
2551 if (x) {
2552 xfrm_state_put(x);
2553 pkt_dev->flows[i].x = NULL;
2554 }
2555 }
2556 }
2557}
2558
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002559static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002560 struct sk_buff *skb, __be16 protocol)
2561{
2562 if (pkt_dev->flags & F_IPSEC_ON) {
2563 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2564 int nhead = 0;
2565 if (x) {
2566 int ret;
2567 __u8 *eth;
fan.du38682042013-12-01 16:28:48 +08002568 struct iphdr *iph;
2569
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002570 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002571 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002572 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2573 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002574 pr_err("Error expanding ipsec packet %d\n",
2575 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002576 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002577 }
2578 }
2579
2580 /* ipsec is not expecting ll header */
2581 skb_pull(skb, ETH_HLEN);
2582 ret = pktgen_output_ipsec(skb, pkt_dev);
2583 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002584 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002585 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002586 }
2587 /* restore ll */
2588 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2589 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002590 *(u16 *) &eth[12] = protocol;
fan.du38682042013-12-01 16:28:48 +08002591
2592 /* Update IPv4 header len as well as checksum value */
2593 iph = ip_hdr(skb);
2594 iph->tot_len = htons(skb->len - ETH_HLEN);
2595 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002596 }
2597 }
2598 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002599err:
2600 kfree_skb(skb);
2601 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002602}
2603#endif
2604
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002605static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2606{
Eric Dumazet95c96172012-04-15 05:58:06 +00002607 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002608 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002609 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002610
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002611 mpls--;
2612 *mpls |= MPLS_STACK_BOTTOM;
2613}
2614
Al Viro0f37c602006-11-03 03:49:56 -08002615static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2616 unsigned int prio)
2617{
2618 return htons(id | (cfi << 12) | (prio << 13));
2619}
2620
Eric Dumazet26ad7872011-01-25 13:26:05 -08002621static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2622 int datalen)
2623{
2624 struct timeval timestamp;
2625 struct pktgen_hdr *pgh;
2626
2627 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2628 datalen -= sizeof(*pgh);
2629
2630 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002631 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002632 } else {
2633 int frags = pkt_dev->nfrags;
2634 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002635 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002636
2637
2638 if (frags > MAX_SKB_FRAGS)
2639 frags = MAX_SKB_FRAGS;
2640 len = datalen - frags * PAGE_SIZE;
2641 if (len > 0) {
2642 memset(skb_put(skb, len), 0, len);
2643 datalen = frags * PAGE_SIZE;
2644 }
2645
2646 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002647 frag_len = (datalen/frags) < PAGE_SIZE ?
2648 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002649 while (datalen > 0) {
2650 if (unlikely(!pkt_dev->page)) {
2651 int node = numa_node_id();
2652
2653 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2654 node = pkt_dev->node;
2655 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2656 if (!pkt_dev->page)
2657 break;
2658 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002659 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002660 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002661 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002662 /*last fragment, fill rest of data*/
2663 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002664 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2665 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002666 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002667 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2668 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2669 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2670 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002671 i++;
2672 skb_shinfo(skb)->nr_frags = i;
2673 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002674 }
2675
2676 /* Stamp the time, and sequence number,
2677 * convert them to network byte order
2678 */
2679 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2680 pgh->seq_num = htonl(pkt_dev->seq_num);
2681
2682 do_gettimeofday(&timestamp);
2683 pgh->tv_sec = htonl(timestamp.tv_sec);
2684 pgh->tv_usec = htonl(timestamp.tv_usec);
2685}
2686
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002687static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2688 struct pktgen_dev *pkt_dev,
2689 unsigned int extralen)
2690{
2691 struct sk_buff *skb = NULL;
2692 unsigned int size = pkt_dev->cur_pkt_size + 64 + extralen +
2693 pkt_dev->pkt_overhead;
2694
2695 if (pkt_dev->flags & F_NODE) {
2696 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2697
2698 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2699 if (likely(skb)) {
2700 skb_reserve(skb, NET_SKB_PAD);
2701 skb->dev = dev;
2702 }
2703 } else {
2704 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2705 }
2706
2707 return skb;
2708}
2709
Luiz Capitulino222f1802006-03-20 22:16:13 -08002710static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2711 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
2713 struct sk_buff *skb = NULL;
2714 __u8 *eth;
2715 struct udphdr *udph;
2716 int datalen, iplen;
2717 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002718 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002719 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002720 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2721 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2722 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2723 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002724 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002725
2726 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002727 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002728
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002729 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002730 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002731
Robert Olsson64053be2005-06-26 15:27:10 -07002732 /* Update any of the values, used when we're incrementing various
2733 * fields.
2734 */
2735 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002736 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002737
David S. Miller7ac54592006-01-18 14:19:10 -08002738 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002739
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002740 skb = pktgen_alloc_skb(odev, pkt_dev, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (!skb) {
2742 sprintf(pkt_dev->result, "No memory");
2743 return NULL;
2744 }
2745
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002746 prefetchw(skb->data);
David S. Miller7ac54592006-01-18 14:19:10 -08002747 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 /* Reserve for ethernet and IP header */
2750 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002751 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2752 if (pkt_dev->nr_labels)
2753 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002754
2755 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002756 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002757 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002758 *svlan_tci = build_tci(pkt_dev->svlan_id,
2759 pkt_dev->svlan_cfi,
2760 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002761 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002762 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002763 }
2764 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002765 *vlan_tci = build_tci(pkt_dev->vlan_id,
2766 pkt_dev->vlan_cfi,
2767 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002768 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002769 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002770 }
2771
Thomas Graf525cebe2013-06-03 11:49:23 +00002772 skb_set_mac_header(skb, 0);
2773 skb_set_network_header(skb, skb->len);
2774 iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2775
2776 skb_set_transport_header(skb, skb->len);
2777 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002778 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002779 skb->priority = pkt_dev->skb_priority;
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002782 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002784 /* Eth + IPh + UDPh + mpls */
2785 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002786 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002787 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 udph->source = htons(pkt_dev->cur_udp_src);
2791 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002792 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002793 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 iph->ihl = 5;
2796 iph->version = 4;
2797 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002798 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002799 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 iph->saddr = pkt_dev->cur_saddr;
2801 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002802 iph->id = htons(pkt_dev->ip_id);
2803 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 iph->frag_off = 0;
2805 iplen = 20 + 8 + datalen;
2806 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002807 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002808 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 skb->dev = odev;
2810 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002811
2812 if (!(pkt_dev->flags & F_UDPCSUM)) {
2813 skb->ip_summed = CHECKSUM_NONE;
2814 } else if (odev->features & NETIF_F_V4_CSUM) {
2815 skb->ip_summed = CHECKSUM_PARTIAL;
2816 skb->csum = 0;
2817 udp4_hwcsum(skb, udph->source, udph->dest);
2818 } else {
2819 __wsum csum = udp_csum(skb);
2820
2821 /* add protocol-dependent pseudo-header */
2822 udph->check = csum_tcpudp_magic(udph->source, udph->dest,
2823 datalen + 8, IPPROTO_UDP, csum);
2824
2825 if (udph->check == 0)
2826 udph->check = CSUM_MANGLED_0;
2827 }
2828
Eric Dumazet26ad7872011-01-25 13:26:05 -08002829 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002830
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002831#ifdef CONFIG_XFRM
2832 if (!process_ipsec(pkt_dev, skb, protocol))
2833 return NULL;
2834#endif
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return skb;
2837}
2838
Luiz Capitulino222f1802006-03-20 22:16:13 -08002839static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2840 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
2842 struct sk_buff *skb = NULL;
2843 __u8 *eth;
2844 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002845 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002847 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002848 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002849 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2850 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2851 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2852 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002853 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002854
2855 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002856 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002857
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002858 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002859 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002860
Robert Olsson64053be2005-06-26 15:27:10 -07002861 /* Update any of the values, used when we're incrementing various
2862 * fields.
2863 */
2864 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002865 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002866
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002867 skb = pktgen_alloc_skb(odev, pkt_dev, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (!skb) {
2869 sprintf(pkt_dev->result, "No memory");
2870 return NULL;
2871 }
2872
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002873 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 skb_reserve(skb, 16);
2875
2876 /* Reserve for ethernet and IP header */
2877 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002878 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2879 if (pkt_dev->nr_labels)
2880 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002881
2882 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002883 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002884 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002885 *svlan_tci = build_tci(pkt_dev->svlan_id,
2886 pkt_dev->svlan_cfi,
2887 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002888 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002889 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002890 }
2891 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002892 *vlan_tci = build_tci(pkt_dev->vlan_id,
2893 pkt_dev->vlan_cfi,
2894 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002895 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002896 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002897 }
2898
Thomas Graf525cebe2013-06-03 11:49:23 +00002899 skb_set_mac_header(skb, 0);
2900 skb_set_network_header(skb, skb->len);
2901 iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2902
2903 skb_set_transport_header(skb, skb->len);
2904 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002905 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002906 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002909 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002910
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002911 /* Eth + IPh + UDPh + mpls */
2912 datalen = pkt_dev->cur_pkt_size - 14 -
2913 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002914 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002916 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002918 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 }
2920
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002921 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 udph->source = htons(pkt_dev->cur_udp_src);
2923 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002924 udph->len = htons(udplen);
2925 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002927 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002929 if (pkt_dev->traffic_class) {
2930 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002931 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002932 }
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 iph->hop_limit = 32;
2935
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002936 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 iph->nexthdr = IPPROTO_UDP;
2938
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002939 iph->daddr = pkt_dev->cur_in6_daddr;
2940 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002942 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 skb->dev = odev;
2944 skb->pkt_type = PACKET_HOST;
2945
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002946 if (!(pkt_dev->flags & F_UDPCSUM)) {
2947 skb->ip_summed = CHECKSUM_NONE;
2948 } else if (odev->features & NETIF_F_V6_CSUM) {
2949 skb->ip_summed = CHECKSUM_PARTIAL;
2950 skb->csum_start = skb_transport_header(skb) - skb->head;
2951 skb->csum_offset = offsetof(struct udphdr, check);
2952 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2953 } else {
2954 __wsum csum = udp_csum(skb);
2955
2956 /* add protocol-dependent pseudo-header */
2957 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2958
2959 if (udph->check == 0)
2960 udph->check = CSUM_MANGLED_0;
2961 }
2962
Eric Dumazet26ad7872011-01-25 13:26:05 -08002963 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 return skb;
2966}
2967
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002968static struct sk_buff *fill_packet(struct net_device *odev,
2969 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002971 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return fill_packet_ipv6(odev, pkt_dev);
2973 else
2974 return fill_packet_ipv4(odev, pkt_dev);
2975}
2976
Luiz Capitulino222f1802006-03-20 22:16:13 -08002977static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002979 pkt_dev->seq_num = 1;
2980 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002982 pkt_dev->tx_bytes = 0;
2983 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
2986/* Set up structure for sending pkts, clear counters */
2987
2988static void pktgen_run(struct pktgen_thread *t)
2989{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002990 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 int started = 0;
2992
Joe Perchesf9467ea2010-06-21 12:29:14 +00002993 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002996 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 /*
2999 * setup odev and create initial packet.
3000 */
3001 pktgen_setup_inject(pkt_dev);
3002
Luiz Capitulino222f1802006-03-20 22:16:13 -08003003 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003007 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003008
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003009 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 strcpy(pkt_dev->result, "Starting");
3012 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003013 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 strcpy(pkt_dev->result, "Error starting");
3015 }
3016 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003017 if (started)
3018 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
Cong Wang4e58a022013-01-28 19:55:53 +00003021static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003023 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Joe Perchesf9467ea2010-06-21 12:29:14 +00003025 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003027 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003028
Cong Wang4e58a022013-01-28 19:55:53 +00003029 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003030 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003031
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003032 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Stephen Hemminger648fda72009-08-27 13:55:07 +00003035static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003037 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003039 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003040 if (pkt_dev->running)
3041 return 1;
3042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
Luiz Capitulino222f1802006-03-20 22:16:13 -08003045static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003047 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Luiz Capitulino222f1802006-03-20 22:16:13 -08003049 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Luiz Capitulino222f1802006-03-20 22:16:13 -08003051 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Luiz Capitulino222f1802006-03-20 22:16:13 -08003053 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Luiz Capitulino222f1802006-03-20 22:16:13 -08003055 if (signal_pending(current))
3056 goto signal;
3057 if_lock(t);
3058 }
3059 if_unlock(t);
3060 return 1;
3061signal:
3062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Cong Wang4e58a022013-01-28 19:55:53 +00003065static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003067 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003069
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003070 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003071
Cong Wang4e58a022013-01-28 19:55:53 +00003072 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074 if (sig == 0)
3075 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003077
3078 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003079 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003081
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003082 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return sig;
3084}
3085
Cong Wang4e58a022013-01-28 19:55:53 +00003086static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003088 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Joe Perchesf9467ea2010-06-21 12:29:14 +00003090 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003092 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Cong Wang4e58a022013-01-28 19:55:53 +00003094 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003096
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003097 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003099 /* Propagate thread->control */
3100 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003101
Cong Wang4e58a022013-01-28 19:55:53 +00003102 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Cong Wang4e58a022013-01-28 19:55:53 +00003105static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003106{
3107 struct pktgen_thread *t;
3108
Joe Perchesf9467ea2010-06-21 12:29:14 +00003109 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003110
3111 mutex_lock(&pktgen_thread_lock);
3112
Cong Wang4e58a022013-01-28 19:55:53 +00003113 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003114 t->control |= (T_REMDEVALL);
3115
3116 mutex_unlock(&pktgen_thread_lock);
3117
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003118 /* Propagate thread->control */
3119 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003120
Cong Wang4e58a022013-01-28 19:55:53 +00003121 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003122}
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3125{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003126 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003127 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003128 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3129 pkt_dev->started_at);
3130 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Daniel Turull03a14ab2011-03-09 14:11:00 -08003132 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003133 (unsigned long long)ktime_to_us(elapsed),
3134 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3135 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003136 (unsigned long long)pkt_dev->sofar,
3137 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003139 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3140 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Luiz Capitulino222f1802006-03-20 22:16:13 -08003142 bps = pps * 8 * pkt_dev->cur_pkt_size;
3143
3144 mbps = bps;
3145 do_div(mbps, 1000000);
3146 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3147 (unsigned long long)pps,
3148 (unsigned long long)mbps,
3149 (unsigned long long)bps,
3150 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003157
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003159 pr_warning("interface: %s is already stopped\n",
3160 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003161 return -EINVAL;
3162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003164 kfree_skb(pkt_dev->skb);
3165 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003166 pkt_dev->stopped_at = ktime_get();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003167 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003169 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
Luiz Capitulino222f1802006-03-20 22:16:13 -08003174static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003176 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 if_lock(t);
3179
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003180 list_for_each_entry(pkt_dev, &t->if_list, list) {
3181 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003182 continue;
3183 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003184 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003185 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003186 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
3188 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003189 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
Luiz Capitulino222f1802006-03-20 22:16:13 -08003192static void pktgen_stop(struct pktgen_thread *t)
3193{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003194 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Joe Perchesf9467ea2010-06-21 12:29:14 +00003196 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Luiz Capitulino222f1802006-03-20 22:16:13 -08003198 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003200 list_for_each_entry(pkt_dev, &t->if_list, list) {
3201 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Luiz Capitulino222f1802006-03-20 22:16:13 -08003204 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003207/*
3208 * one of our devices needs to be removed - find it
3209 * and remove it
3210 */
3211static void pktgen_rem_one_if(struct pktgen_thread *t)
3212{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003213 struct list_head *q, *n;
3214 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003215
Joe Perchesf9467ea2010-06-21 12:29:14 +00003216 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003217
3218 if_lock(t);
3219
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003220 list_for_each_safe(q, n, &t->if_list) {
3221 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003222
Luiz Capitulino222f1802006-03-20 22:16:13 -08003223 if (!cur->removal_mark)
3224 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003225
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003226 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003227 cur->skb = NULL;
3228
3229 pktgen_remove_device(t, cur);
3230
3231 break;
3232 }
3233
3234 if_unlock(t);
3235}
3236
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003239 struct list_head *q, *n;
3240 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003241
Joe Perchesf9467ea2010-06-21 12:29:14 +00003242 func_enter();
3243
Luiz Capitulino222f1802006-03-20 22:16:13 -08003244 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003245
Luiz Capitulino222f1802006-03-20 22:16:13 -08003246 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003248 list_for_each_safe(q, n, &t->if_list) {
3249 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003250
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003251 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003252 cur->skb = NULL;
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 pktgen_remove_device(t, cur);
3255 }
3256
Luiz Capitulino222f1802006-03-20 22:16:13 -08003257 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Luiz Capitulino222f1802006-03-20 22:16:13 -08003260static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003262 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003263 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
Stephen Hemmingeref879792009-09-22 19:41:43 +00003266static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003267{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003268 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003269 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003270 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003271}
3272
Stephen Hemmingeref879792009-09-22 19:41:43 +00003273static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3274{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003275 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003276
3277 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3278 if (signal_pending(current))
3279 break;
3280
3281 if (need_resched())
3282 pktgen_resched(pkt_dev);
3283 else
3284 cpu_relax();
3285 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003286 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003287}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003288
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003289static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
Stephen Hemminger00829822008-11-20 20:14:53 -08003291 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003292 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003293 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003294 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003295 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 int ret;
3297
Stephen Hemmingeref879792009-09-22 19:41:43 +00003298 /* If device is offline, then don't send */
3299 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3300 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003301 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003303
Stephen Hemmingeref879792009-09-22 19:41:43 +00003304 /* This is max DELAY, this has special meaning of
3305 * "never transmit"
3306 */
3307 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003308 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003309 return;
3310 }
3311
3312 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003313 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3314 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3315 /* build a new pkt */
3316 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003317
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003318 pkt_dev->skb = fill_packet(odev, pkt_dev);
3319 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003320 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003321 schedule();
3322 pkt_dev->clone_count--; /* back out increment, OOM */
3323 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003325 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003326 pkt_dev->allocated_skbs++;
3327 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003329
Stephen Hemmingeref879792009-09-22 19:41:43 +00003330 if (pkt_dev->delay && pkt_dev->last_ok)
3331 spin(pkt_dev, pkt_dev->next_tx);
3332
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003333 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3334 txq = netdev_get_tx_queue(odev, queue_map);
3335
3336 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003337
Tom Herbert734664982011-11-28 16:32:44 +00003338 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003339 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003340 pkt_dev->last_ok = 0;
3341 goto unlock;
3342 }
3343 atomic_inc(&(pkt_dev->skb->users));
3344 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Stephen Hemmingeref879792009-09-22 19:41:43 +00003346 switch (ret) {
3347 case NETDEV_TX_OK:
3348 txq_trans_update(txq);
3349 pkt_dev->last_ok = 1;
3350 pkt_dev->sofar++;
3351 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003352 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003353 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003354 case NET_XMIT_DROP:
3355 case NET_XMIT_CN:
3356 case NET_XMIT_POLICED:
3357 /* skb has been consumed */
3358 pkt_dev->errors++;
3359 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003360 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003361 net_info_ratelimited("%s xmit error: %d\n",
3362 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003363 pkt_dev->errors++;
3364 /* fallthru */
3365 case NETDEV_TX_LOCKED:
3366 case NETDEV_TX_BUSY:
3367 /* Retry it next time */
3368 atomic_dec(&(pkt_dev->skb->users));
3369 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003370 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003371unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003372 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 /* If pkt_dev->count is zero, then run forever */
3375 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003376 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 /* Done with this */
3379 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003380 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003383/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 * Main loop of the thread goes here
3385 */
3386
David S. Milleree74baa2007-01-01 20:51:53 -08003387static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
3389 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003390 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003391 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
David S. Milleree74baa2007-01-01 20:51:53 -08003394 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
3396 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003397 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Joe Perchesf9467ea2010-06-21 12:29:14 +00003399 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
David S. Milleree74baa2007-01-01 20:51:53 -08003401 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003402
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003403 set_freezable();
3404
David S. Milleree74baa2007-01-01 20:51:53 -08003405 while (!kthread_should_stop()) {
3406 pkt_dev = next_to_run(t);
3407
Stephen Hemmingeref879792009-09-22 19:41:43 +00003408 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003409 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003410 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003411 wait_event_interruptible_timeout(t->queue,
3412 t->control != 0,
3413 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003414 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003415 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003416 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 __set_current_state(TASK_RUNNING);
3419
Stephen Hemmingeref879792009-09-22 19:41:43 +00003420 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 pktgen_xmit(pkt_dev);
3422
Stephen Hemmingeref879792009-09-22 19:41:43 +00003423 if (need_resched())
3424 pktgen_resched(pkt_dev);
3425 else
3426 cpu_relax();
3427 }
3428
Luiz Capitulino222f1802006-03-20 22:16:13 -08003429 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 pktgen_stop(t);
3431 t->control &= ~(T_STOP);
3432 }
3433
Luiz Capitulino222f1802006-03-20 22:16:13 -08003434 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 pktgen_run(t);
3436 t->control &= ~(T_RUN);
3437 }
3438
Luiz Capitulino222f1802006-03-20 22:16:13 -08003439 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003441 t->control &= ~(T_REMDEVALL);
3442 }
3443
Luiz Capitulino222f1802006-03-20 22:16:13 -08003444 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003445 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 t->control &= ~(T_REMDEV);
3447 }
3448
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003449 try_to_freeze();
3450
David S. Milleree74baa2007-01-01 20:51:53 -08003451 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Joe Perchesf9467ea2010-06-21 12:29:14 +00003454 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003455 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Joe Perchesf9467ea2010-06-21 12:29:14 +00003457 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003458 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Joe Perchesf9467ea2010-06-21 12:29:14 +00003460 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003461 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003462
Eric Dumazet551eaff2010-11-21 10:26:44 -08003463 /* Wait for kthread_stop */
3464 while (!kthread_should_stop()) {
3465 set_current_state(TASK_INTERRUPTIBLE);
3466 schedule();
3467 }
3468 __set_current_state(TASK_RUNNING);
3469
David S. Milleree74baa2007-01-01 20:51:53 -08003470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Luiz Capitulino222f1802006-03-20 22:16:13 -08003473static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003474 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003476 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003477 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Eric Dumazet3e984842009-11-24 14:50:53 -08003479 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003480 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003481 if (strncmp(p->odevname, ifname, len) == 0) {
3482 if (p->odevname[len]) {
3483 if (exact || p->odevname[len] != '@')
3484 continue;
3485 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003486 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003487 break;
3488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Luiz Capitulino222f1802006-03-20 22:16:13 -08003490 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003491 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003492 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493}
3494
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003495/*
3496 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 */
3498
Luiz Capitulino222f1802006-03-20 22:16:13 -08003499static int add_dev_to_thread(struct pktgen_thread *t,
3500 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
3502 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Luiz Capitulino222f1802006-03-20 22:16:13 -08003504 if_lock(t);
3505
3506 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003507 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003508 rv = -EBUSY;
3509 goto out;
3510 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003511
3512 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003513 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 pkt_dev->running = 0;
3515
Luiz Capitulino222f1802006-03-20 22:16:13 -08003516out:
3517 if_unlock(t);
3518 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
3521/* Called under thread lock */
3522
Luiz Capitulino222f1802006-03-20 22:16:13 -08003523static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003525 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003526 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003527 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /* We don't allow a device to be on several threads */
3530
Cong Wang4e58a022013-01-28 19:55:53 +00003531 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003532 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003533 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003534 return -EBUSY;
3535 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003536
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003537 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003538 if (!pkt_dev)
3539 return -ENOMEM;
3540
Eric Dumazet593f63b2009-11-23 01:44:37 +00003541 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003542 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003543 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003544 if (pkt_dev->flows == NULL) {
3545 kfree(pkt_dev);
3546 return -ENOMEM;
3547 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003548
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003549 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003550 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003551 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003552 pkt_dev->count = pg_count_d;
3553 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003554 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003555 pkt_dev->udp_src_max = 9;
3556 pkt_dev->udp_dst_min = 9;
3557 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003558 pkt_dev->vlan_p = 0;
3559 pkt_dev->vlan_cfi = 0;
3560 pkt_dev->vlan_id = 0xffff;
3561 pkt_dev->svlan_p = 0;
3562 pkt_dev->svlan_cfi = 0;
3563 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003564 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003565
Cong Wang4e58a022013-01-28 19:55:53 +00003566 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003567 if (err)
3568 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003569 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3570 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003571
Cong Wang4e58a022013-01-28 19:55:53 +00003572 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003573 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003574 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003575 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003576 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003577 err = -EINVAL;
3578 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003579 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003580#ifdef CONFIG_XFRM
3581 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3582 pkt_dev->ipsproto = IPPROTO_ESP;
Fan Ducf93d472014-01-03 11:18:31 +08003583
3584 /* xfrm tunnel mode needs additional dst to extract outter
3585 * ip header protocol/ttl/id field, here creat a phony one.
3586 * instead of looking for a valid rt, which definitely hurting
3587 * performance under such circumstance.
3588 */
3589 pkt_dev->dstops.family = AF_INET;
3590 pkt_dev->dst.dev = pkt_dev->odev;
3591 dst_init_metrics(&pkt_dev->dst, pktgen_dst_metrics, false);
3592 pkt_dev->dst.child = &pkt_dev->dst;
3593 pkt_dev->dst.ops = &pkt_dev->dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003594#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003595
3596 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003597out2:
3598 dev_put(pkt_dev->odev);
3599out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003600#ifdef CONFIG_XFRM
3601 free_SAs(pkt_dev);
3602#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003603 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003604 kfree(pkt_dev);
3605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
3607
Cong Wang4e58a022013-01-28 19:55:53 +00003608static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003610 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003611 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003612 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003614 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3615 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003616 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003617 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003618 return -ENOMEM;
3619 }
3620
Luiz Capitulino222f1802006-03-20 22:16:13 -08003621 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003623
David S. Milleree74baa2007-01-01 20:51:53 -08003624 INIT_LIST_HEAD(&t->if_list);
3625
Cong Wang4e58a022013-01-28 19:55:53 +00003626 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003627 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003628
Eric Dumazet94dcf292011-03-22 16:30:45 -07003629 p = kthread_create_on_node(pktgen_thread_worker,
3630 t,
3631 cpu_to_node(cpu),
3632 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003633 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003634 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003635 list_del(&t->th_list);
3636 kfree(t);
3637 return PTR_ERR(p);
3638 }
3639 kthread_bind(p, cpu);
3640 t->tsk = p;
3641
Cong Wang4e58a022013-01-28 19:55:53 +00003642 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003643 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003644 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003645 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003646 PG_PROC_DIR, t->tsk->comm);
3647 kthread_stop(p);
3648 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003649 kfree(t);
3650 return -EINVAL;
3651 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003652
Cong Wang4e58a022013-01-28 19:55:53 +00003653 t->net = pn;
David S. Milleree74baa2007-01-01 20:51:53 -08003654 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003655 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 return 0;
3658}
3659
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003660/*
3661 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003663static void _rem_dev_from_if_list(struct pktgen_thread *t,
3664 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003666 struct list_head *q, *n;
3667 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003669 list_for_each_safe(q, n, &t->if_list) {
3670 p = list_entry(q, struct pktgen_dev, list);
3671 if (p == pkt_dev)
3672 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
3674}
3675
Luiz Capitulino222f1802006-03-20 22:16:13 -08003676static int pktgen_remove_device(struct pktgen_thread *t,
3677 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003679 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Luiz Capitulino222f1802006-03-20 22:16:13 -08003681 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003682 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003683 pktgen_stop_device(pkt_dev);
3684 }
3685
3686 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
3688 if (pkt_dev->odev) {
3689 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003690 pkt_dev->odev = NULL;
3691 }
3692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 /* And update the thread if_list */
3694
3695 _rem_dev_from_if_list(t, pkt_dev);
3696
Stephen Hemminger39df2322007-03-04 16:11:51 -08003697 if (pkt_dev->entry)
David Howellsa8ca16e2013-04-12 17:27:28 +01003698 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003700#ifdef CONFIG_XFRM
3701 free_SAs(pkt_dev);
3702#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003703 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003704 if (pkt_dev->page)
3705 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
3709
Cong Wang4e58a022013-01-28 19:55:53 +00003710static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
Cong Wang4e58a022013-01-28 19:55:53 +00003712 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003713 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003714 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003715
Cong Wang4e58a022013-01-28 19:55:53 +00003716 pn->net = net;
3717 INIT_LIST_HEAD(&pn->pktgen_threads);
3718 pn->pktgen_exiting = false;
3719 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3720 if (!pn->proc_dir) {
3721 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003722 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003723 }
Cong Wang4e58a022013-01-28 19:55:53 +00003724 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3725 if (pe == NULL) {
3726 pr_err("cannot create %s procfs entry\n", PGCTRL);
3727 ret = -EINVAL;
3728 goto remove;
3729 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003730
John Hawkes670c02c2005-10-13 09:30:31 -07003731 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003732 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Cong Wang4e58a022013-01-28 19:55:53 +00003734 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003735 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003736 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003737 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003738 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003739
Cong Wang4e58a022013-01-28 19:55:53 +00003740 if (list_empty(&pn->pktgen_threads)) {
3741 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003742 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003743 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003744 }
3745
Luiz Capitulino222f1802006-03-20 22:16:13 -08003746 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003747
Cong Wang4e58a022013-01-28 19:55:53 +00003748remove_entry:
3749 remove_proc_entry(PGCTRL, pn->proc_dir);
3750remove:
Gao fengece31ff2013-02-18 01:34:56 +00003751 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003752 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
Cong Wang4e58a022013-01-28 19:55:53 +00003755static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Cong Wang4e58a022013-01-28 19:55:53 +00003757 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003758 struct pktgen_thread *t;
3759 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003760 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Luiz Capitulino222f1802006-03-20 22:16:13 -08003762 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003763 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Eric Dumazetc57b5462012-05-09 13:29:51 +00003765 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003766 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003767 mutex_unlock(&pktgen_thread_lock);
3768
3769 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003770 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003771 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003772 kthread_stop(t->tsk);
3773 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Cong Wang4e58a022013-01-28 19:55:53 +00003776 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003777 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003778}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Cong Wang4e58a022013-01-28 19:55:53 +00003780static struct pernet_operations pg_net_ops = {
3781 .init = pg_net_init,
3782 .exit = pg_net_exit,
3783 .id = &pg_net_id,
3784 .size = sizeof(struct pktgen_net),
3785};
3786
3787static int __init pg_init(void)
3788{
3789 int ret = 0;
3790
3791 pr_info("%s", version);
3792 ret = register_pernet_subsys(&pg_net_ops);
3793 if (ret)
3794 return ret;
3795 ret = register_netdevice_notifier(&pktgen_notifier_block);
3796 if (ret)
3797 unregister_pernet_subsys(&pg_net_ops);
3798
3799 return ret;
3800}
3801
3802static void __exit pg_cleanup(void)
3803{
3804 unregister_netdevice_notifier(&pktgen_notifier_block);
3805 unregister_pernet_subsys(&pg_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806}
3807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808module_init(pg_init);
3809module_exit(pg_cleanup);
3810
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003811MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812MODULE_DESCRIPTION("Packet Generator tool");
3813MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003814MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003816MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003818MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003820MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003822MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");