blob: 628f7c572c6ef72c463a6f8d99ba2a2695cd80ad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
Jan Engelhardt96de0e22007-10-19 23:21:04 +02009 * Jens Låås <jens.laas@data.slu.se>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090047 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090068 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
116 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Joe Perchesf9467ea2010-06-21 12:29:14 +0000118
119#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <linux/sys.h>
122#include <linux/types.h>
123#include <linux/module.h>
124#include <linux/moduleparam.h>
125#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800126#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <linux/sched.h>
128#include <linux/slab.h>
129#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#include <linux/unistd.h>
131#include <linux/string.h>
132#include <linux/ptrace.h>
133#include <linux/errno.h>
134#include <linux/ioport.h>
135#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800136#include <linux/capability.h>
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700137#include <linux/hrtimer.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700138#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#include <linux/delay.h>
140#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800141#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#include <linux/init.h>
143#include <linux/skbuff.h>
144#include <linux/netdevice.h>
145#include <linux/inet.h>
146#include <linux/inetdevice.h>
147#include <linux/rtnetlink.h>
148#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700149#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#include <linux/in.h>
151#include <linux/ip.h>
152#include <linux/ipv6.h>
153#include <linux/udp.h>
154#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700155#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800157#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800158#include <linux/kthread.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700159#include <linux/prefetch.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200160#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#include <net/checksum.h>
162#include <net/ipv6.h>
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200163#include <net/udp.h>
Stephen Rothwell73d94e92013-07-29 16:21:26 +1000164#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700166#ifdef CONFIG_XFRM
167#include <net/xfrm.h>
168#endif
Cong Wang4e58a022013-01-28 19:55:53 +0000169#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#include <asm/byteorder.h>
171#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700172#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000173#include <linux/io.h>
174#include <linux/timex.h>
175#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800177#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Daniel Turull43d28b62010-06-09 22:49:57 +0000179#define VERSION "2.74"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800181#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800182#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Joe Perchesf9467ea2010-06-21 12:29:14 +0000184#define func_enter() pr_debug("entering %s\n", __func__);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800187#define F_IPSRC_RND (1<<0) /* IP-Src Random */
188#define F_IPDST_RND (1<<1) /* IP-Dst Random */
189#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
190#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
191#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
192#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
193#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
194#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800195#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700196#define F_VID_RND (1<<9) /* Random VLAN ID */
197#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700198#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700199#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700200#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700201#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000202#define F_NODE (1<<15) /* Node memory alloc*/
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200203#define F_UDPCSUM (1<<16) /* Include UDP checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000206#define T_STOP (1<<0) /* Stop run */
207#define T_RUN (1<<1) /* Start run */
208#define T_REMDEVALL (1<<2) /* Remove all devs */
209#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/* If lock -- can be removed after some work */
212#define if_lock(t) spin_lock(&(t->if_lock));
213#define if_unlock(t) spin_unlock(&(t->if_lock));
214
215/* Used to help with determining the pkts on receive */
216#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700217#define PG_PROC_DIR "pktgen"
218#define PGCTRL "pgctrl"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220#define MAX_CFLOWS 65536
221
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700222#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
223#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
224
Luiz Capitulino222f1802006-03-20 22:16:13 -0800225struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800226 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800227 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700228#ifdef CONFIG_XFRM
229 struct xfrm_state *x;
230#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700231 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700234/* flow flag bits */
235#define F_INIT (1<<0) /* flow has been initialized */
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /*
239 * Try to keep frequent/infrequent used vars. separated.
240 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800241 struct proc_dir_entry *entry; /* proc file */
242 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000243 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000245 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800246
247 /* If min != max, then we will either do a linear iteration, or
248 * we will do a random selection from within the range.
249 */
250 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800251 int removal_mark; /* non-zero => the device is marked for
252 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000254 int min_pkt_size;
255 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700256 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800257 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800258 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000259 u64 delay; /* nano-seconds */
260
Luiz Capitulino222f1802006-03-20 22:16:13 -0800261 __u64 count; /* Default No packets to send */
262 __u64 sofar; /* How many pkts we've sent so far */
263 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800264 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Luiz Capitulino222f1802006-03-20 22:16:13 -0800266 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Luiz Capitulino222f1802006-03-20 22:16:13 -0800268 __u64 allocated_skbs;
269 __u32 clone_count;
270 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000271 * Or a failed transmit of some sort?
272 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800273 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000274 ktime_t next_tx;
275 ktime_t started_at;
276 ktime_t stopped_at;
277 u64 idle_acc; /* nano-seconds */
278
Luiz Capitulino222f1802006-03-20 22:16:13 -0800279 __u32 seq_num;
280
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000281 int clone_skb; /*
282 * Use multiple SKBs during packet gen.
283 * If this number is greater than 1, then
284 * that many copies of the same packet will be
285 * sent before a new packet is allocated.
286 * If you want to send 1024 identical packets
287 * before creating a new packet,
288 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800289 */
290
291 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
292 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
293 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
294 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
295
296 struct in6_addr in6_saddr;
297 struct in6_addr in6_daddr;
298 struct in6_addr cur_in6_daddr;
299 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800301 struct in6_addr min_in6_daddr;
302 struct in6_addr max_in6_daddr;
303 struct in6_addr min_in6_saddr;
304 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Luiz Capitulino222f1802006-03-20 22:16:13 -0800306 /* If we're doing ranges, random or incremental, then this
307 * defines the min/max for those ranges.
308 */
Al Viro252e33462006-11-14 20:48:11 -0800309 __be32 saddr_min; /* inclusive, source IP address */
310 __be32 saddr_max; /* exclusive, source IP address */
311 __be32 daddr_min; /* inclusive, dest IP address */
312 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Luiz Capitulino222f1802006-03-20 22:16:13 -0800314 __u16 udp_src_min; /* inclusive, source UDP port */
315 __u16 udp_src_max; /* exclusive, source UDP port */
316 __u16 udp_dst_min; /* inclusive, dest UDP port */
317 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700319 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000320 __u8 tos; /* six MSB of (former) IPv4 TOS
321 are for dscp codepoint */
322 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
323 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700324
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800325 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000326 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800327 __be32 labels[MAX_MPLS_LABELS];
328
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700329 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
330 __u8 vlan_p;
331 __u8 vlan_cfi;
332 __u16 vlan_id; /* 0xffff means no vlan tag */
333
334 __u8 svlan_p;
335 __u8 svlan_cfi;
336 __u16 svlan_id; /* 0xffff means no svlan tag */
337
Luiz Capitulino222f1802006-03-20 22:16:13 -0800338 __u32 src_mac_count; /* How many MACs to iterate through */
339 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Luiz Capitulino222f1802006-03-20 22:16:13 -0800341 unsigned char dst_mac[ETH_ALEN];
342 unsigned char src_mac[ETH_ALEN];
343
344 __u32 cur_dst_mac_offset;
345 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800346 __be32 cur_saddr;
347 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700348 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800349 __u16 cur_udp_dst;
350 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700351 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800352 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800353 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800354
355 __u8 hh[14];
356 /* = {
357 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
358
359 We fill in SRC address later
360 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
361 0x08, 0x00
362 };
363 */
364 __u16 pad; /* pad out the hh struct to an even 16 bytes */
365
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000366 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800367 * are transmitting the same one multiple times
368 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000369 struct net_device *odev; /* The out-going device.
370 * Note that the device should have it's
371 * pg_info pointer pointing back to this
372 * device.
373 * Set when the user specifies the out-going
374 * device name (not when the inject is
375 * started as it used to do.)
376 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000377 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000379 unsigned int cflows; /* Concurrent flows (config) */
380 unsigned int lflow; /* Flow length (config) */
381 unsigned int nflows; /* accumulated flows (stats) */
382 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700383
384 u16 queue_map_min;
385 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000386 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000387 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700388
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700389#ifdef CONFIG_XFRM
390 __u8 ipsmode; /* IPSEC mode (config) */
391 __u8 ipsproto; /* IPSEC type (config) */
Fan Dude4aee72014-01-03 11:18:30 +0800392 __u32 spi;
Fan Ducf93d47e2014-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 Viro252e33462006-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) {
2250 /*slow path: we dont already have xfrm_state*/
Cong Wang4e58a022013-01-28 19:55:53 +00002251 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002252 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002253 (xfrm_address_t *)&pkt_dev->cur_saddr,
2254 AF_INET,
2255 pkt_dev->ipsmode,
2256 pkt_dev->ipsproto, 0);
2257 if (x) {
2258 pkt_dev->flows[flow].x = x;
2259 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002260 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002261 }
2262
2263 }
2264}
2265#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002266static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2267{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002268
2269 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2270 pkt_dev->cur_queue_map = smp_processor_id();
2271
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002272 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002273 __u16 t;
2274 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002275 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002276 (pkt_dev->queue_map_max -
2277 pkt_dev->queue_map_min + 1)
2278 + pkt_dev->queue_map_min;
2279 } else {
2280 t = pkt_dev->cur_queue_map + 1;
2281 if (t > pkt_dev->queue_map_max)
2282 t = pkt_dev->queue_map_min;
2283 }
2284 pkt_dev->cur_queue_map = t;
2285 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002286 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 -07002287}
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289/* Increment/randomize headers according to flags and current values
2290 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2291 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002292static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2293{
2294 __u32 imn;
2295 __u32 imx;
2296 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002298 if (pkt_dev->cflows)
2299 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002302 if (pkt_dev->src_mac_count > 1) {
2303 __u32 mc;
2304 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Luiz Capitulino222f1802006-03-20 22:16:13 -08002306 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002307 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002308 else {
2309 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002310 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002311 pkt_dev->src_mac_count)
2312 pkt_dev->cur_src_mac_offset = 0;
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Luiz Capitulino222f1802006-03-20 22:16:13 -08002315 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2316 pkt_dev->hh[11] = tmp;
2317 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2318 pkt_dev->hh[10] = tmp;
2319 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2320 pkt_dev->hh[9] = tmp;
2321 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2322 pkt_dev->hh[8] = tmp;
2323 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2324 pkt_dev->hh[7] = tmp;
2325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Luiz Capitulino222f1802006-03-20 22:16:13 -08002327 /* Deal with Destination MAC */
2328 if (pkt_dev->dst_mac_count > 1) {
2329 __u32 mc;
2330 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Luiz Capitulino222f1802006-03-20 22:16:13 -08002332 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002333 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Luiz Capitulino222f1802006-03-20 22:16:13 -08002335 else {
2336 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002337 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002338 pkt_dev->dst_mac_count) {
2339 pkt_dev->cur_dst_mac_offset = 0;
2340 }
2341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Luiz Capitulino222f1802006-03-20 22:16:13 -08002343 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2344 pkt_dev->hh[5] = tmp;
2345 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2346 pkt_dev->hh[4] = tmp;
2347 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2348 pkt_dev->hh[3] = tmp;
2349 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2350 pkt_dev->hh[2] = tmp;
2351 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2352 pkt_dev->hh[1] = tmp;
2353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002355 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002356 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002357 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002358 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2359 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002360 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002361 htonl(0x000fffff));
2362 }
2363
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002364 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002365 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002366 }
2367
2368 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002369 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002370 }
2371
Luiz Capitulino222f1802006-03-20 22:16:13 -08002372 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2373 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002374 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002375 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2376 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Luiz Capitulino222f1802006-03-20 22:16:13 -08002378 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 pkt_dev->cur_udp_src++;
2380 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2381 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002382 }
2383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Luiz Capitulino222f1802006-03-20 22:16:13 -08002385 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2386 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002387 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002388 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2389 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002390 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002392 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002394 }
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 if (!(pkt_dev->flags & F_IPV6)) {
2398
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002399 imn = ntohl(pkt_dev->saddr_min);
2400 imx = ntohl(pkt_dev->saddr_max);
2401 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002403 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002404 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 else {
2406 t = ntohl(pkt_dev->cur_saddr);
2407 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002408 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412 pkt_dev->cur_saddr = htonl(t);
2413 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002414
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002415 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2417 } else {
Al Viro252e33462006-11-14 20:48:11 -08002418 imn = ntohl(pkt_dev->daddr_min);
2419 imx = ntohl(pkt_dev->daddr_max);
2420 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002422 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (pkt_dev->flags & F_IPDST_RND) {
2424
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002425 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002426 t = prandom_u32() %
2427 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002428 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002429 } while (ipv4_is_loopback(s) ||
2430 ipv4_is_multicast(s) ||
2431 ipv4_is_lbcast(s) ||
2432 ipv4_is_zeronet(s) ||
2433 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002434 pkt_dev->cur_daddr = s;
2435 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 t = ntohl(pkt_dev->cur_daddr);
2437 t++;
2438 if (t > imx) {
2439 t = imn;
2440 }
2441 pkt_dev->cur_daddr = htonl(t);
2442 }
2443 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002444 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002445 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002446 pkt_dev->flows[flow].cur_daddr =
2447 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002448#ifdef CONFIG_XFRM
2449 if (pkt_dev->flags & F_IPSEC_ON)
2450 get_ipsec_sa(pkt_dev, flow);
2451#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 pkt_dev->nflows++;
2453 }
2454 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002455 } else { /* IPV6 * */
2456
Joe Perches06e30412012-10-18 17:55:31 +00002457 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 int i;
2459
2460 /* Only random destinations yet */
2461
Luiz Capitulino222f1802006-03-20 22:16:13 -08002462 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002464 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002465 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2466 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470
Luiz Capitulino222f1802006-03-20 22:16:13 -08002471 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2472 __u32 t;
2473 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002474 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002475 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2476 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002477 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002479 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002481 }
2482 pkt_dev->cur_pkt_size = t;
2483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002485 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 pkt_dev->flows[flow].count++;
2488}
2489
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002490
2491#ifdef CONFIG_XFRM
Fan Ducf93d47e2014-01-03 11:18:31 +08002492u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2493
2494 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2495};
2496
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002497static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2498{
2499 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2500 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002501 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002502
2503 if (!x)
2504 return 0;
2505 /* XXX: we dont support tunnel mode for now until
2506 * we resolve the dst issue */
Fan Ducf93d47e2014-01-03 11:18:31 +08002507 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002508 return 0;
2509
Fan Ducf93d47e2014-01-03 11:18:31 +08002510 /* But when user specify an valid SPI, transformation
2511 * supports both transport/tunnel mode + ESP/AH type.
2512 */
2513 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2514 skb->_skb_refdst = (unsigned long)&pkt_dev->dst | SKB_DST_NOREF;
2515
2516 rcu_read_lock_bh();
Herbert Xu13996372007-10-17 21:35:51 -07002517 err = x->outer_mode->output(x, skb);
Fan Ducf93d47e2014-01-03 11:18:31 +08002518 rcu_read_unlock_bh();
Fan Du6de9ace2014-01-03 11:18:28 +08002519 if (err) {
2520 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002521 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002522 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002523 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002524 if (err) {
2525 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002526 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002527 }
Fan Du0af0a412014-01-03 11:18:27 +08002528 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002529 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002530 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002531 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002532error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002533 return err;
2534}
2535
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002536static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002537{
2538 if (pkt_dev->cflows) {
2539 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002540 int i;
2541 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002542 struct xfrm_state *x = pkt_dev->flows[i].x;
2543 if (x) {
2544 xfrm_state_put(x);
2545 pkt_dev->flows[i].x = NULL;
2546 }
2547 }
2548 }
2549}
2550
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002551static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002552 struct sk_buff *skb, __be16 protocol)
2553{
2554 if (pkt_dev->flags & F_IPSEC_ON) {
2555 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2556 int nhead = 0;
2557 if (x) {
2558 int ret;
2559 __u8 *eth;
fan.du38682042013-12-01 16:28:48 +08002560 struct iphdr *iph;
2561
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002562 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002563 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002564 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2565 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002566 pr_err("Error expanding ipsec packet %d\n",
2567 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002568 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002569 }
2570 }
2571
2572 /* ipsec is not expecting ll header */
2573 skb_pull(skb, ETH_HLEN);
2574 ret = pktgen_output_ipsec(skb, pkt_dev);
2575 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002576 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002577 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002578 }
2579 /* restore ll */
2580 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2581 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002582 *(u16 *) &eth[12] = protocol;
fan.du38682042013-12-01 16:28:48 +08002583
2584 /* Update IPv4 header len as well as checksum value */
2585 iph = ip_hdr(skb);
2586 iph->tot_len = htons(skb->len - ETH_HLEN);
2587 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002588 }
2589 }
2590 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002591err:
2592 kfree_skb(skb);
2593 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002594}
2595#endif
2596
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002597static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2598{
Eric Dumazet95c96172012-04-15 05:58:06 +00002599 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002600 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002601 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002602
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002603 mpls--;
2604 *mpls |= MPLS_STACK_BOTTOM;
2605}
2606
Al Viro0f37c602006-11-03 03:49:56 -08002607static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2608 unsigned int prio)
2609{
2610 return htons(id | (cfi << 12) | (prio << 13));
2611}
2612
Eric Dumazet26ad7872011-01-25 13:26:05 -08002613static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2614 int datalen)
2615{
2616 struct timeval timestamp;
2617 struct pktgen_hdr *pgh;
2618
2619 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2620 datalen -= sizeof(*pgh);
2621
2622 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002623 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002624 } else {
2625 int frags = pkt_dev->nfrags;
2626 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002627 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002628
2629
2630 if (frags > MAX_SKB_FRAGS)
2631 frags = MAX_SKB_FRAGS;
2632 len = datalen - frags * PAGE_SIZE;
2633 if (len > 0) {
2634 memset(skb_put(skb, len), 0, len);
2635 datalen = frags * PAGE_SIZE;
2636 }
2637
2638 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002639 frag_len = (datalen/frags) < PAGE_SIZE ?
2640 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002641 while (datalen > 0) {
2642 if (unlikely(!pkt_dev->page)) {
2643 int node = numa_node_id();
2644
2645 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2646 node = pkt_dev->node;
2647 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2648 if (!pkt_dev->page)
2649 break;
2650 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002651 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002652 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002653 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002654 /*last fragment, fill rest of data*/
2655 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002656 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2657 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002658 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002659 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2660 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2661 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2662 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002663 i++;
2664 skb_shinfo(skb)->nr_frags = i;
2665 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002666 }
2667
2668 /* Stamp the time, and sequence number,
2669 * convert them to network byte order
2670 */
2671 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2672 pgh->seq_num = htonl(pkt_dev->seq_num);
2673
2674 do_gettimeofday(&timestamp);
2675 pgh->tv_sec = htonl(timestamp.tv_sec);
2676 pgh->tv_usec = htonl(timestamp.tv_usec);
2677}
2678
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002679static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2680 struct pktgen_dev *pkt_dev,
2681 unsigned int extralen)
2682{
2683 struct sk_buff *skb = NULL;
2684 unsigned int size = pkt_dev->cur_pkt_size + 64 + extralen +
2685 pkt_dev->pkt_overhead;
2686
2687 if (pkt_dev->flags & F_NODE) {
2688 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2689
2690 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2691 if (likely(skb)) {
2692 skb_reserve(skb, NET_SKB_PAD);
2693 skb->dev = dev;
2694 }
2695 } else {
2696 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2697 }
2698
2699 return skb;
2700}
2701
Luiz Capitulino222f1802006-03-20 22:16:13 -08002702static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2703 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
2705 struct sk_buff *skb = NULL;
2706 __u8 *eth;
2707 struct udphdr *udph;
2708 int datalen, iplen;
2709 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002710 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002711 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002712 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2713 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2714 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2715 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002716 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002717
2718 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002719 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002720
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002721 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002722 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002723
Robert Olsson64053be2005-06-26 15:27:10 -07002724 /* Update any of the values, used when we're incrementing various
2725 * fields.
2726 */
2727 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002728 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002729
David S. Miller7ac54592006-01-18 14:19:10 -08002730 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002731
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002732 skb = pktgen_alloc_skb(odev, pkt_dev, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (!skb) {
2734 sprintf(pkt_dev->result, "No memory");
2735 return NULL;
2736 }
2737
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002738 prefetchw(skb->data);
David S. Miller7ac54592006-01-18 14:19:10 -08002739 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 /* Reserve for ethernet and IP header */
2742 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002743 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2744 if (pkt_dev->nr_labels)
2745 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002746
2747 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002748 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002749 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002750 *svlan_tci = build_tci(pkt_dev->svlan_id,
2751 pkt_dev->svlan_cfi,
2752 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002753 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002754 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002755 }
2756 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002757 *vlan_tci = build_tci(pkt_dev->vlan_id,
2758 pkt_dev->vlan_cfi,
2759 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002760 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002761 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002762 }
2763
Thomas Graf525cebe2013-06-03 11:49:23 +00002764 skb_set_mac_header(skb, 0);
2765 skb_set_network_header(skb, skb->len);
2766 iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2767
2768 skb_set_transport_header(skb, skb->len);
2769 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002770 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002771 skb->priority = pkt_dev->skb_priority;
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002774 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002776 /* Eth + IPh + UDPh + mpls */
2777 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002778 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002779 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 udph->source = htons(pkt_dev->cur_udp_src);
2783 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002784 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002785 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 iph->ihl = 5;
2788 iph->version = 4;
2789 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002790 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002791 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 iph->saddr = pkt_dev->cur_saddr;
2793 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002794 iph->id = htons(pkt_dev->ip_id);
2795 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 iph->frag_off = 0;
2797 iplen = 20 + 8 + datalen;
2798 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002799 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002800 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 skb->dev = odev;
2802 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002803
2804 if (!(pkt_dev->flags & F_UDPCSUM)) {
2805 skb->ip_summed = CHECKSUM_NONE;
2806 } else if (odev->features & NETIF_F_V4_CSUM) {
2807 skb->ip_summed = CHECKSUM_PARTIAL;
2808 skb->csum = 0;
2809 udp4_hwcsum(skb, udph->source, udph->dest);
2810 } else {
2811 __wsum csum = udp_csum(skb);
2812
2813 /* add protocol-dependent pseudo-header */
2814 udph->check = csum_tcpudp_magic(udph->source, udph->dest,
2815 datalen + 8, IPPROTO_UDP, csum);
2816
2817 if (udph->check == 0)
2818 udph->check = CSUM_MANGLED_0;
2819 }
2820
Eric Dumazet26ad7872011-01-25 13:26:05 -08002821 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002822
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002823#ifdef CONFIG_XFRM
2824 if (!process_ipsec(pkt_dev, skb, protocol))
2825 return NULL;
2826#endif
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return skb;
2829}
2830
Luiz Capitulino222f1802006-03-20 22:16:13 -08002831static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2832 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
2834 struct sk_buff *skb = NULL;
2835 __u8 *eth;
2836 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002837 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002839 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002840 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002841 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2842 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2843 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2844 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002845 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002846
2847 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002848 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002849
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002850 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002851 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002852
Robert Olsson64053be2005-06-26 15:27:10 -07002853 /* Update any of the values, used when we're incrementing various
2854 * fields.
2855 */
2856 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002857 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002858
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002859 skb = pktgen_alloc_skb(odev, pkt_dev, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (!skb) {
2861 sprintf(pkt_dev->result, "No memory");
2862 return NULL;
2863 }
2864
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002865 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 skb_reserve(skb, 16);
2867
2868 /* Reserve for ethernet and IP header */
2869 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002870 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2871 if (pkt_dev->nr_labels)
2872 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002873
2874 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002875 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002876 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002877 *svlan_tci = build_tci(pkt_dev->svlan_id,
2878 pkt_dev->svlan_cfi,
2879 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002880 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002881 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002882 }
2883 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002884 *vlan_tci = build_tci(pkt_dev->vlan_id,
2885 pkt_dev->vlan_cfi,
2886 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002887 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002888 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002889 }
2890
Thomas Graf525cebe2013-06-03 11:49:23 +00002891 skb_set_mac_header(skb, 0);
2892 skb_set_network_header(skb, skb->len);
2893 iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2894
2895 skb_set_transport_header(skb, skb->len);
2896 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002897 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002898 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002901 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002902
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002903 /* Eth + IPh + UDPh + mpls */
2904 datalen = pkt_dev->cur_pkt_size - 14 -
2905 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002906 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002908 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002910 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
2912
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002913 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 udph->source = htons(pkt_dev->cur_udp_src);
2915 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002916 udph->len = htons(udplen);
2917 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002919 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002921 if (pkt_dev->traffic_class) {
2922 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002923 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002924 }
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 iph->hop_limit = 32;
2927
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002928 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 iph->nexthdr = IPPROTO_UDP;
2930
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002931 iph->daddr = pkt_dev->cur_in6_daddr;
2932 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002934 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 skb->dev = odev;
2936 skb->pkt_type = PACKET_HOST;
2937
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002938 if (!(pkt_dev->flags & F_UDPCSUM)) {
2939 skb->ip_summed = CHECKSUM_NONE;
2940 } else if (odev->features & NETIF_F_V6_CSUM) {
2941 skb->ip_summed = CHECKSUM_PARTIAL;
2942 skb->csum_start = skb_transport_header(skb) - skb->head;
2943 skb->csum_offset = offsetof(struct udphdr, check);
2944 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2945 } else {
2946 __wsum csum = udp_csum(skb);
2947
2948 /* add protocol-dependent pseudo-header */
2949 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2950
2951 if (udph->check == 0)
2952 udph->check = CSUM_MANGLED_0;
2953 }
2954
Eric Dumazet26ad7872011-01-25 13:26:05 -08002955 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return skb;
2958}
2959
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002960static struct sk_buff *fill_packet(struct net_device *odev,
2961 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002963 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return fill_packet_ipv6(odev, pkt_dev);
2965 else
2966 return fill_packet_ipv4(odev, pkt_dev);
2967}
2968
Luiz Capitulino222f1802006-03-20 22:16:13 -08002969static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002971 pkt_dev->seq_num = 1;
2972 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002974 pkt_dev->tx_bytes = 0;
2975 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
2978/* Set up structure for sending pkts, clear counters */
2979
2980static void pktgen_run(struct pktgen_thread *t)
2981{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002982 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 int started = 0;
2984
Joe Perchesf9467ea2010-06-21 12:29:14 +00002985 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002988 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990 /*
2991 * setup odev and create initial packet.
2992 */
2993 pktgen_setup_inject(pkt_dev);
2994
Luiz Capitulino222f1802006-03-20 22:16:13 -08002995 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002997 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00002999 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003000
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003001 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 strcpy(pkt_dev->result, "Starting");
3004 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 strcpy(pkt_dev->result, "Error starting");
3007 }
3008 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003009 if (started)
3010 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
Cong Wang4e58a022013-01-28 19:55:53 +00003013static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003015 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Joe Perchesf9467ea2010-06-21 12:29:14 +00003017 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003019 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003020
Cong Wang4e58a022013-01-28 19:55:53 +00003021 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003022 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003023
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003024 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
Stephen Hemminger648fda72009-08-27 13:55:07 +00003027static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003029 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003031 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003032 if (pkt_dev->running)
3033 return 1;
3034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
3036
Luiz Capitulino222f1802006-03-20 22:16:13 -08003037static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003039 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Luiz Capitulino222f1802006-03-20 22:16:13 -08003041 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Luiz Capitulino222f1802006-03-20 22:16:13 -08003043 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Luiz Capitulino222f1802006-03-20 22:16:13 -08003045 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Luiz Capitulino222f1802006-03-20 22:16:13 -08003047 if (signal_pending(current))
3048 goto signal;
3049 if_lock(t);
3050 }
3051 if_unlock(t);
3052 return 1;
3053signal:
3054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
3056
Cong Wang4e58a022013-01-28 19:55:53 +00003057static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003059 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003061
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003062 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003063
Cong Wang4e58a022013-01-28 19:55:53 +00003064 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003066 if (sig == 0)
3067 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003069
3070 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003071 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003073
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003074 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return sig;
3076}
3077
Cong Wang4e58a022013-01-28 19:55:53 +00003078static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003080 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Joe Perchesf9467ea2010-06-21 12:29:14 +00003082 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003084 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Cong Wang4e58a022013-01-28 19:55:53 +00003086 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003088
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003089 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003091 /* Propagate thread->control */
3092 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003093
Cong Wang4e58a022013-01-28 19:55:53 +00003094 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
Cong Wang4e58a022013-01-28 19:55:53 +00003097static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003098{
3099 struct pktgen_thread *t;
3100
Joe Perchesf9467ea2010-06-21 12:29:14 +00003101 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003102
3103 mutex_lock(&pktgen_thread_lock);
3104
Cong Wang4e58a022013-01-28 19:55:53 +00003105 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003106 t->control |= (T_REMDEVALL);
3107
3108 mutex_unlock(&pktgen_thread_lock);
3109
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003110 /* Propagate thread->control */
3111 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003112
Cong Wang4e58a022013-01-28 19:55:53 +00003113 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003114}
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3117{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003118 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003119 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003120 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3121 pkt_dev->started_at);
3122 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Daniel Turull03a14ab2011-03-09 14:11:00 -08003124 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003125 (unsigned long long)ktime_to_us(elapsed),
3126 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3127 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003128 (unsigned long long)pkt_dev->sofar,
3129 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003131 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3132 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Luiz Capitulino222f1802006-03-20 22:16:13 -08003134 bps = pps * 8 * pkt_dev->cur_pkt_size;
3135
3136 mbps = bps;
3137 do_div(mbps, 1000000);
3138 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3139 (unsigned long long)pps,
3140 (unsigned long long)mbps,
3141 (unsigned long long)bps,
3142 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003146static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003148 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003149
Luiz Capitulino222f1802006-03-20 22:16:13 -08003150 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003151 pr_warning("interface: %s is already stopped\n",
3152 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003153 return -EINVAL;
3154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003156 kfree_skb(pkt_dev->skb);
3157 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003158 pkt_dev->stopped_at = ktime_get();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003161 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Luiz Capitulino222f1802006-03-20 22:16:13 -08003163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003168 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 if_lock(t);
3171
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003172 list_for_each_entry(pkt_dev, &t->if_list, list) {
3173 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003174 continue;
3175 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003176 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003177 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003178 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
3180 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003181 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
Luiz Capitulino222f1802006-03-20 22:16:13 -08003184static void pktgen_stop(struct pktgen_thread *t)
3185{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003186 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Joe Perchesf9467ea2010-06-21 12:29:14 +00003188 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Luiz Capitulino222f1802006-03-20 22:16:13 -08003190 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003192 list_for_each_entry(pkt_dev, &t->if_list, list) {
3193 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Luiz Capitulino222f1802006-03-20 22:16:13 -08003196 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003199/*
3200 * one of our devices needs to be removed - find it
3201 * and remove it
3202 */
3203static void pktgen_rem_one_if(struct pktgen_thread *t)
3204{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003205 struct list_head *q, *n;
3206 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003207
Joe Perchesf9467ea2010-06-21 12:29:14 +00003208 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003209
3210 if_lock(t);
3211
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003212 list_for_each_safe(q, n, &t->if_list) {
3213 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003214
Luiz Capitulino222f1802006-03-20 22:16:13 -08003215 if (!cur->removal_mark)
3216 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003217
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003218 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003219 cur->skb = NULL;
3220
3221 pktgen_remove_device(t, cur);
3222
3223 break;
3224 }
3225
3226 if_unlock(t);
3227}
3228
Luiz Capitulino222f1802006-03-20 22:16:13 -08003229static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003231 struct list_head *q, *n;
3232 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003233
Joe Perchesf9467ea2010-06-21 12:29:14 +00003234 func_enter();
3235
Luiz Capitulino222f1802006-03-20 22:16:13 -08003236 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003237
Luiz Capitulino222f1802006-03-20 22:16:13 -08003238 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003240 list_for_each_safe(q, n, &t->if_list) {
3241 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003242
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003243 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003244 cur->skb = NULL;
3245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 pktgen_remove_device(t, cur);
3247 }
3248
Luiz Capitulino222f1802006-03-20 22:16:13 -08003249 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
Luiz Capitulino222f1802006-03-20 22:16:13 -08003252static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003254 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003255 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
Stephen Hemmingeref879792009-09-22 19:41:43 +00003258static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003259{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003260 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003261 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003262 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003263}
3264
Stephen Hemmingeref879792009-09-22 19:41:43 +00003265static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3266{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003267 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003268
3269 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3270 if (signal_pending(current))
3271 break;
3272
3273 if (need_resched())
3274 pktgen_resched(pkt_dev);
3275 else
3276 cpu_relax();
3277 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003278 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003279}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003280
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003281static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Stephen Hemminger00829822008-11-20 20:14:53 -08003283 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003284 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003285 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003286 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003287 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 int ret;
3289
Stephen Hemmingeref879792009-09-22 19:41:43 +00003290 /* If device is offline, then don't send */
3291 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3292 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003295
Stephen Hemmingeref879792009-09-22 19:41:43 +00003296 /* This is max DELAY, this has special meaning of
3297 * "never transmit"
3298 */
3299 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003300 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003301 return;
3302 }
3303
3304 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003305 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3306 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3307 /* build a new pkt */
3308 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003309
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003310 pkt_dev->skb = fill_packet(odev, pkt_dev);
3311 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003312 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003313 schedule();
3314 pkt_dev->clone_count--; /* back out increment, OOM */
3315 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003317 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003318 pkt_dev->allocated_skbs++;
3319 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003321
Stephen Hemmingeref879792009-09-22 19:41:43 +00003322 if (pkt_dev->delay && pkt_dev->last_ok)
3323 spin(pkt_dev, pkt_dev->next_tx);
3324
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003325 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3326 txq = netdev_get_tx_queue(odev, queue_map);
3327
3328 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003329
Tom Herbert734664982011-11-28 16:32:44 +00003330 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003331 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003332 pkt_dev->last_ok = 0;
3333 goto unlock;
3334 }
3335 atomic_inc(&(pkt_dev->skb->users));
3336 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Stephen Hemmingeref879792009-09-22 19:41:43 +00003338 switch (ret) {
3339 case NETDEV_TX_OK:
3340 txq_trans_update(txq);
3341 pkt_dev->last_ok = 1;
3342 pkt_dev->sofar++;
3343 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003344 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003345 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003346 case NET_XMIT_DROP:
3347 case NET_XMIT_CN:
3348 case NET_XMIT_POLICED:
3349 /* skb has been consumed */
3350 pkt_dev->errors++;
3351 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003352 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003353 net_info_ratelimited("%s xmit error: %d\n",
3354 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003355 pkt_dev->errors++;
3356 /* fallthru */
3357 case NETDEV_TX_LOCKED:
3358 case NETDEV_TX_BUSY:
3359 /* Retry it next time */
3360 atomic_dec(&(pkt_dev->skb->users));
3361 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003362 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003363unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003364 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 /* If pkt_dev->count is zero, then run forever */
3367 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003368 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 /* Done with this */
3371 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003372 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003375/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 * Main loop of the thread goes here
3377 */
3378
David S. Milleree74baa2007-01-01 20:51:53 -08003379static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
3381 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003382 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003383 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
David S. Milleree74baa2007-01-01 20:51:53 -08003386 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
3388 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003389 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Joe Perchesf9467ea2010-06-21 12:29:14 +00003391 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
David S. Milleree74baa2007-01-01 20:51:53 -08003393 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003394
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003395 set_freezable();
3396
David S. Milleree74baa2007-01-01 20:51:53 -08003397 while (!kthread_should_stop()) {
3398 pkt_dev = next_to_run(t);
3399
Stephen Hemmingeref879792009-09-22 19:41:43 +00003400 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003401 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003402 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003403 wait_event_interruptible_timeout(t->queue,
3404 t->control != 0,
3405 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003406 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003407 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003408 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 __set_current_state(TASK_RUNNING);
3411
Stephen Hemmingeref879792009-09-22 19:41:43 +00003412 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 pktgen_xmit(pkt_dev);
3414
Stephen Hemmingeref879792009-09-22 19:41:43 +00003415 if (need_resched())
3416 pktgen_resched(pkt_dev);
3417 else
3418 cpu_relax();
3419 }
3420
Luiz Capitulino222f1802006-03-20 22:16:13 -08003421 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 pktgen_stop(t);
3423 t->control &= ~(T_STOP);
3424 }
3425
Luiz Capitulino222f1802006-03-20 22:16:13 -08003426 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 pktgen_run(t);
3428 t->control &= ~(T_RUN);
3429 }
3430
Luiz Capitulino222f1802006-03-20 22:16:13 -08003431 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003433 t->control &= ~(T_REMDEVALL);
3434 }
3435
Luiz Capitulino222f1802006-03-20 22:16:13 -08003436 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003437 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 t->control &= ~(T_REMDEV);
3439 }
3440
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003441 try_to_freeze();
3442
David S. Milleree74baa2007-01-01 20:51:53 -08003443 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Joe Perchesf9467ea2010-06-21 12:29:14 +00003446 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003447 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Joe Perchesf9467ea2010-06-21 12:29:14 +00003449 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003450 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Joe Perchesf9467ea2010-06-21 12:29:14 +00003452 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003453 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003454
Eric Dumazet551eaff2010-11-21 10:26:44 -08003455 /* Wait for kthread_stop */
3456 while (!kthread_should_stop()) {
3457 set_current_state(TASK_INTERRUPTIBLE);
3458 schedule();
3459 }
3460 __set_current_state(TASK_RUNNING);
3461
David S. Milleree74baa2007-01-01 20:51:53 -08003462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463}
3464
Luiz Capitulino222f1802006-03-20 22:16:13 -08003465static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003466 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003468 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003469 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Eric Dumazet3e984842009-11-24 14:50:53 -08003471 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003472 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003473 if (strncmp(p->odevname, ifname, len) == 0) {
3474 if (p->odevname[len]) {
3475 if (exact || p->odevname[len] != '@')
3476 continue;
3477 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003478 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003479 break;
3480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Luiz Capitulino222f1802006-03-20 22:16:13 -08003482 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003483 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003484 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003487/*
3488 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 */
3490
Luiz Capitulino222f1802006-03-20 22:16:13 -08003491static int add_dev_to_thread(struct pktgen_thread *t,
3492 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
3494 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Luiz Capitulino222f1802006-03-20 22:16:13 -08003496 if_lock(t);
3497
3498 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003499 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003500 rv = -EBUSY;
3501 goto out;
3502 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003503
3504 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003505 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 pkt_dev->running = 0;
3507
Luiz Capitulino222f1802006-03-20 22:16:13 -08003508out:
3509 if_unlock(t);
3510 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
3513/* Called under thread lock */
3514
Luiz Capitulino222f1802006-03-20 22:16:13 -08003515static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003518 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003519 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 /* We don't allow a device to be on several threads */
3522
Cong Wang4e58a022013-01-28 19:55:53 +00003523 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003524 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003525 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003526 return -EBUSY;
3527 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003528
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003529 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003530 if (!pkt_dev)
3531 return -ENOMEM;
3532
Eric Dumazet593f63b2009-11-23 01:44:37 +00003533 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003534 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003535 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003536 if (pkt_dev->flows == NULL) {
3537 kfree(pkt_dev);
3538 return -ENOMEM;
3539 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003540
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003541 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003542 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003543 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003544 pkt_dev->count = pg_count_d;
3545 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003546 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003547 pkt_dev->udp_src_max = 9;
3548 pkt_dev->udp_dst_min = 9;
3549 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003550 pkt_dev->vlan_p = 0;
3551 pkt_dev->vlan_cfi = 0;
3552 pkt_dev->vlan_id = 0xffff;
3553 pkt_dev->svlan_p = 0;
3554 pkt_dev->svlan_cfi = 0;
3555 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003556 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003557
Cong Wang4e58a022013-01-28 19:55:53 +00003558 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003559 if (err)
3560 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003561 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3562 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003563
Cong Wang4e58a022013-01-28 19:55:53 +00003564 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003565 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003566 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003567 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003568 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003569 err = -EINVAL;
3570 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003571 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003572#ifdef CONFIG_XFRM
3573 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3574 pkt_dev->ipsproto = IPPROTO_ESP;
Fan Ducf93d47e2014-01-03 11:18:31 +08003575
3576 /* xfrm tunnel mode needs additional dst to extract outter
3577 * ip header protocol/ttl/id field, here creat a phony one.
3578 * instead of looking for a valid rt, which definitely hurting
3579 * performance under such circumstance.
3580 */
3581 pkt_dev->dstops.family = AF_INET;
3582 pkt_dev->dst.dev = pkt_dev->odev;
3583 dst_init_metrics(&pkt_dev->dst, pktgen_dst_metrics, false);
3584 pkt_dev->dst.child = &pkt_dev->dst;
3585 pkt_dev->dst.ops = &pkt_dev->dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003586#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003587
3588 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003589out2:
3590 dev_put(pkt_dev->odev);
3591out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003592#ifdef CONFIG_XFRM
3593 free_SAs(pkt_dev);
3594#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003595 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003596 kfree(pkt_dev);
3597 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Cong Wang4e58a022013-01-28 19:55:53 +00003600static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003602 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003603 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003604 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003606 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3607 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003608 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003609 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003610 return -ENOMEM;
3611 }
3612
Luiz Capitulino222f1802006-03-20 22:16:13 -08003613 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615
David S. Milleree74baa2007-01-01 20:51:53 -08003616 INIT_LIST_HEAD(&t->if_list);
3617
Cong Wang4e58a022013-01-28 19:55:53 +00003618 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003619 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003620
Eric Dumazet94dcf292011-03-22 16:30:45 -07003621 p = kthread_create_on_node(pktgen_thread_worker,
3622 t,
3623 cpu_to_node(cpu),
3624 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003625 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003626 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003627 list_del(&t->th_list);
3628 kfree(t);
3629 return PTR_ERR(p);
3630 }
3631 kthread_bind(p, cpu);
3632 t->tsk = p;
3633
Cong Wang4e58a022013-01-28 19:55:53 +00003634 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003635 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003636 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003637 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003638 PG_PROC_DIR, t->tsk->comm);
3639 kthread_stop(p);
3640 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003641 kfree(t);
3642 return -EINVAL;
3643 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003644
Cong Wang4e58a022013-01-28 19:55:53 +00003645 t->net = pn;
David S. Milleree74baa2007-01-01 20:51:53 -08003646 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003647 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
3649 return 0;
3650}
3651
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003652/*
3653 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003655static void _rem_dev_from_if_list(struct pktgen_thread *t,
3656 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003658 struct list_head *q, *n;
3659 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003661 list_for_each_safe(q, n, &t->if_list) {
3662 p = list_entry(q, struct pktgen_dev, list);
3663 if (p == pkt_dev)
3664 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666}
3667
Luiz Capitulino222f1802006-03-20 22:16:13 -08003668static int pktgen_remove_device(struct pktgen_thread *t,
3669 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003671 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Luiz Capitulino222f1802006-03-20 22:16:13 -08003673 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003674 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003675 pktgen_stop_device(pkt_dev);
3676 }
3677
3678 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
3680 if (pkt_dev->odev) {
3681 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003682 pkt_dev->odev = NULL;
3683 }
3684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 /* And update the thread if_list */
3686
3687 _rem_dev_from_if_list(t, pkt_dev);
3688
Stephen Hemminger39df2322007-03-04 16:11:51 -08003689 if (pkt_dev->entry)
David Howellsa8ca16e2013-04-12 17:27:28 +01003690 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003692#ifdef CONFIG_XFRM
3693 free_SAs(pkt_dev);
3694#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003695 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003696 if (pkt_dev->page)
3697 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701
Cong Wang4e58a022013-01-28 19:55:53 +00003702static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
Cong Wang4e58a022013-01-28 19:55:53 +00003704 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003705 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003706 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003707
Cong Wang4e58a022013-01-28 19:55:53 +00003708 pn->net = net;
3709 INIT_LIST_HEAD(&pn->pktgen_threads);
3710 pn->pktgen_exiting = false;
3711 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3712 if (!pn->proc_dir) {
3713 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003714 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003715 }
Cong Wang4e58a022013-01-28 19:55:53 +00003716 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3717 if (pe == NULL) {
3718 pr_err("cannot create %s procfs entry\n", PGCTRL);
3719 ret = -EINVAL;
3720 goto remove;
3721 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003722
John Hawkes670c02c2005-10-13 09:30:31 -07003723 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003724 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Cong Wang4e58a022013-01-28 19:55:53 +00003726 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003727 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003728 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003729 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003730 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003731
Cong Wang4e58a022013-01-28 19:55:53 +00003732 if (list_empty(&pn->pktgen_threads)) {
3733 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003734 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003735 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003736 }
3737
Luiz Capitulino222f1802006-03-20 22:16:13 -08003738 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003739
Cong Wang4e58a022013-01-28 19:55:53 +00003740remove_entry:
3741 remove_proc_entry(PGCTRL, pn->proc_dir);
3742remove:
Gao fengece31ff2013-02-18 01:34:56 +00003743 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003744 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
Cong Wang4e58a022013-01-28 19:55:53 +00003747static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
Cong Wang4e58a022013-01-28 19:55:53 +00003749 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003750 struct pktgen_thread *t;
3751 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003752 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Luiz Capitulino222f1802006-03-20 22:16:13 -08003754 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003755 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Eric Dumazetc57b5462012-05-09 13:29:51 +00003757 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003758 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003759 mutex_unlock(&pktgen_thread_lock);
3760
3761 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003762 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003763 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003764 kthread_stop(t->tsk);
3765 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Cong Wang4e58a022013-01-28 19:55:53 +00003768 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003769 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003770}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Cong Wang4e58a022013-01-28 19:55:53 +00003772static struct pernet_operations pg_net_ops = {
3773 .init = pg_net_init,
3774 .exit = pg_net_exit,
3775 .id = &pg_net_id,
3776 .size = sizeof(struct pktgen_net),
3777};
3778
3779static int __init pg_init(void)
3780{
3781 int ret = 0;
3782
3783 pr_info("%s", version);
3784 ret = register_pernet_subsys(&pg_net_ops);
3785 if (ret)
3786 return ret;
3787 ret = register_netdevice_notifier(&pktgen_notifier_block);
3788 if (ret)
3789 unregister_pernet_subsys(&pg_net_ops);
3790
3791 return ret;
3792}
3793
3794static void __exit pg_cleanup(void)
3795{
3796 unregister_netdevice_notifier(&pktgen_notifier_block);
3797 unregister_pernet_subsys(&pg_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798}
3799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800module_init(pg_init);
3801module_exit(pg_cleanup);
3802
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003803MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804MODULE_DESCRIPTION("Packet Generator tool");
3805MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003806MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003808MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003810MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003812MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003814MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");