blob: 303412d8332b3c92a66d97d4c947051cf7fd8d6d [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>
163#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700164#ifdef CONFIG_XFRM
165#include <net/xfrm.h>
166#endif
Cong Wang4e58a022013-01-28 19:55:53 +0000167#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#include <asm/byteorder.h>
169#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700170#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000171#include <linux/io.h>
172#include <linux/timex.h>
173#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800175#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Daniel Turull43d28b62010-06-09 22:49:57 +0000177#define VERSION "2.74"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800179#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800180#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Joe Perchesf9467ea2010-06-21 12:29:14 +0000182#define func_enter() pr_debug("entering %s\n", __func__);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800185#define F_IPSRC_RND (1<<0) /* IP-Src Random */
186#define F_IPDST_RND (1<<1) /* IP-Dst Random */
187#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
188#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
189#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
190#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
191#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
192#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800193#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700194#define F_VID_RND (1<<9) /* Random VLAN ID */
195#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700196#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700197#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700198#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700199#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000200#define F_NODE (1<<15) /* Node memory alloc*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000203#define T_STOP (1<<0) /* Stop run */
204#define T_RUN (1<<1) /* Start run */
205#define T_REMDEVALL (1<<2) /* Remove all devs */
206#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* If lock -- can be removed after some work */
209#define if_lock(t) spin_lock(&(t->if_lock));
210#define if_unlock(t) spin_unlock(&(t->if_lock));
211
212/* Used to help with determining the pkts on receive */
213#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700214#define PG_PROC_DIR "pktgen"
215#define PGCTRL "pgctrl"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217#define MAX_CFLOWS 65536
218
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700219#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
220#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
221
Luiz Capitulino222f1802006-03-20 22:16:13 -0800222struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800223 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800224 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700225#ifdef CONFIG_XFRM
226 struct xfrm_state *x;
227#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700228 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700231/* flow flag bits */
232#define F_INIT (1<<0) /* flow has been initialized */
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
236 * Try to keep frequent/infrequent used vars. separated.
237 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800238 struct proc_dir_entry *entry; /* proc file */
239 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000240 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000242 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800243
244 /* If min != max, then we will either do a linear iteration, or
245 * we will do a random selection from within the range.
246 */
247 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800248 int removal_mark; /* non-zero => the device is marked for
249 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000251 int min_pkt_size;
252 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700253 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800254 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800255 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000256 u64 delay; /* nano-seconds */
257
Luiz Capitulino222f1802006-03-20 22:16:13 -0800258 __u64 count; /* Default No packets to send */
259 __u64 sofar; /* How many pkts we've sent so far */
260 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800261 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Luiz Capitulino222f1802006-03-20 22:16:13 -0800263 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Luiz Capitulino222f1802006-03-20 22:16:13 -0800265 __u64 allocated_skbs;
266 __u32 clone_count;
267 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000268 * Or a failed transmit of some sort?
269 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800270 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000271 ktime_t next_tx;
272 ktime_t started_at;
273 ktime_t stopped_at;
274 u64 idle_acc; /* nano-seconds */
275
Luiz Capitulino222f1802006-03-20 22:16:13 -0800276 __u32 seq_num;
277
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000278 int clone_skb; /*
279 * Use multiple SKBs during packet gen.
280 * If this number is greater than 1, then
281 * that many copies of the same packet will be
282 * sent before a new packet is allocated.
283 * If you want to send 1024 identical packets
284 * before creating a new packet,
285 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800286 */
287
288 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
289 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
290 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
291 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
292
293 struct in6_addr in6_saddr;
294 struct in6_addr in6_daddr;
295 struct in6_addr cur_in6_daddr;
296 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800298 struct in6_addr min_in6_daddr;
299 struct in6_addr max_in6_daddr;
300 struct in6_addr min_in6_saddr;
301 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Luiz Capitulino222f1802006-03-20 22:16:13 -0800303 /* If we're doing ranges, random or incremental, then this
304 * defines the min/max for those ranges.
305 */
Al Viro252e33462006-11-14 20:48:11 -0800306 __be32 saddr_min; /* inclusive, source IP address */
307 __be32 saddr_max; /* exclusive, source IP address */
308 __be32 daddr_min; /* inclusive, dest IP address */
309 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Luiz Capitulino222f1802006-03-20 22:16:13 -0800311 __u16 udp_src_min; /* inclusive, source UDP port */
312 __u16 udp_src_max; /* exclusive, source UDP port */
313 __u16 udp_dst_min; /* inclusive, dest UDP port */
314 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700316 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000317 __u8 tos; /* six MSB of (former) IPv4 TOS
318 are for dscp codepoint */
319 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
320 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700321
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800322 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000323 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800324 __be32 labels[MAX_MPLS_LABELS];
325
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700326 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
327 __u8 vlan_p;
328 __u8 vlan_cfi;
329 __u16 vlan_id; /* 0xffff means no vlan tag */
330
331 __u8 svlan_p;
332 __u8 svlan_cfi;
333 __u16 svlan_id; /* 0xffff means no svlan tag */
334
Luiz Capitulino222f1802006-03-20 22:16:13 -0800335 __u32 src_mac_count; /* How many MACs to iterate through */
336 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Luiz Capitulino222f1802006-03-20 22:16:13 -0800338 unsigned char dst_mac[ETH_ALEN];
339 unsigned char src_mac[ETH_ALEN];
340
341 __u32 cur_dst_mac_offset;
342 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800343 __be32 cur_saddr;
344 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700345 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800346 __u16 cur_udp_dst;
347 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700348 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800349 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800350 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800351
352 __u8 hh[14];
353 /* = {
354 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
355
356 We fill in SRC address later
357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358 0x08, 0x00
359 };
360 */
361 __u16 pad; /* pad out the hh struct to an even 16 bytes */
362
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000363 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800364 * are transmitting the same one multiple times
365 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000366 struct net_device *odev; /* The out-going device.
367 * Note that the device should have it's
368 * pg_info pointer pointing back to this
369 * device.
370 * Set when the user specifies the out-going
371 * device name (not when the inject is
372 * started as it used to do.)
373 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000374 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000376 unsigned int cflows; /* Concurrent flows (config) */
377 unsigned int lflow; /* Flow length (config) */
378 unsigned int nflows; /* accumulated flows (stats) */
379 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700380
381 u16 queue_map_min;
382 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000383 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000384 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700385
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700386#ifdef CONFIG_XFRM
387 __u8 ipsmode; /* IPSEC mode (config) */
388 __u8 ipsproto; /* IPSEC type (config) */
389#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800390 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391};
392
393struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800394 __be32 pgh_magic;
395 __be32 seq_num;
396 __be32 tv_sec;
397 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
Cong Wang4e58a022013-01-28 19:55:53 +0000400
401static int pg_net_id __read_mostly;
402
403struct pktgen_net {
404 struct net *net;
405 struct proc_dir_entry *proc_dir;
406 struct list_head pktgen_threads;
407 bool pktgen_exiting;
408};
Eric Dumazet551eaff2010-11-21 10:26:44 -0800409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000411 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800412 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800413 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800414 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800415 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000417 /* Field for thread to receive "posted" events terminate,
418 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800419
420 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int cpu;
422
Luiz Capitulino222f1802006-03-20 22:16:13 -0800423 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700424 struct completion start_done;
Cong Wang4e58a022013-01-28 19:55:53 +0000425 struct pktgen_net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426};
427
428#define REMOVE 1
429#define FIND 0
430
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000431static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000432 "Packet Generator for packet performance testing. "
433 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Luiz Capitulino222f1802006-03-20 22:16:13 -0800435static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
436static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
437static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800438 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
Cong Wang4e58a022013-01-28 19:55:53 +0000440static void pktgen_run_all_threads(struct pktgen_net *pn);
441static void pktgen_reset_all_threads(struct pktgen_net *pn);
442static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000443
Luiz Capitulino222f1802006-03-20 22:16:13 -0800444static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000448static int pg_count_d __read_mostly = 1000;
449static int pg_delay_d __read_mostly;
450static int pg_clone_skb_d __read_mostly;
451static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Luiz Capitulino222fa072006-03-20 22:24:27 -0800453static DEFINE_MUTEX(pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455static struct notifier_block pktgen_notifier_block = {
456 .notifier_call = pktgen_device_event,
457};
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900460 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *
462 */
463
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700464static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800465{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000466 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000470static ssize_t pgctrl_write(struct file *file, const char __user *buf,
471 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700474 char data[128];
Cong Wang4e58a022013-01-28 19:55:53 +0000475 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Luiz Capitulino222f1802006-03-20 22:16:13 -0800477 if (!capable(CAP_NET_ADMIN)) {
478 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700482 if (count > sizeof(data))
483 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700486 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700487 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800488 }
489 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Luiz Capitulino222f1802006-03-20 22:16:13 -0800491 if (!strcmp(data, "stop"))
Cong Wang4e58a022013-01-28 19:55:53 +0000492 pktgen_stop_all_threads_ifs(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Luiz Capitulino222f1802006-03-20 22:16:13 -0800494 else if (!strcmp(data, "start"))
Cong Wang4e58a022013-01-28 19:55:53 +0000495 pktgen_run_all_threads(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800497 else if (!strcmp(data, "reset"))
Cong Wang4e58a022013-01-28 19:55:53 +0000498 pktgen_reset_all_threads(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800499
Luiz Capitulino222f1802006-03-20 22:16:13 -0800500 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000501 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 err = count;
504
Luiz Capitulino222f1802006-03-20 22:16:13 -0800505out:
506 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700509static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Al Virod9dda782013-03-31 18:16:14 -0400511 return single_open(file, pgctrl_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700512}
513
Arjan van de Ven9a321442007-02-12 00:55:35 -0800514static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800515 .owner = THIS_MODULE,
516 .open = pgctrl_open,
517 .read = seq_read,
518 .llseek = seq_lseek,
519 .write = pgctrl_write,
520 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700521};
522
523static int pktgen_if_show(struct seq_file *seq, void *v)
524{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000525 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000526 ktime_t stopped;
527 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Luiz Capitulino222f1802006-03-20 22:16:13 -0800529 seq_printf(seq,
530 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
531 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
532 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Luiz Capitulino222f1802006-03-20 22:16:13 -0800534 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000535 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
536 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000537 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Luiz Capitulino222f1802006-03-20 22:16:13 -0800539 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
540 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Robert Olsson45b270f2007-08-28 15:45:55 -0700542 seq_printf(seq,
543 " queue_map_min: %u queue_map_max: %u\n",
544 pkt_dev->queue_map_min,
545 pkt_dev->queue_map_max);
546
John Fastabend9e50e3a2010-11-16 19:12:28 +0000547 if (pkt_dev->skb_priority)
548 seq_printf(seq, " skb_priority: %u\n",
549 pkt_dev->skb_priority);
550
Luiz Capitulino222f1802006-03-20 22:16:13 -0800551 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800552 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000553 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
554 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
555 &pkt_dev->in6_saddr,
556 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
557 &pkt_dev->in6_daddr,
558 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000559 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800560 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000561 " dst_min: %s dst_max: %s\n",
562 pkt_dev->dst_min, pkt_dev->dst_max);
563 seq_printf(seq,
564 " src_min: %s src_max: %s\n",
565 pkt_dev->src_min, pkt_dev->src_max);
566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700568 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Johannes Berge1749612008-10-27 15:59:26 -0700570 seq_printf(seq, "%pM ",
571 is_zero_ether_addr(pkt_dev->src_mac) ?
572 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Luiz Capitulino222f1802006-03-20 22:16:13 -0800574 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700575 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Luiz Capitulino222f1802006-03-20 22:16:13 -0800577 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000578 " udp_src_min: %d udp_src_max: %d"
579 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800580 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
581 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Luiz Capitulino222f1802006-03-20 22:16:13 -0800583 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800584 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700585 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800587 if (pkt_dev->nr_labels) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000588 unsigned int i;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800589 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700590 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800591 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
592 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
593 }
594
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000595 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700596 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000597 pkt_dev->vlan_id, pkt_dev->vlan_p,
598 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700599
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000600 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700601 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000602 pkt_dev->svlan_id, pkt_dev->svlan_p,
603 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700604
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000605 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700606 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700607
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000608 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700609 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700610
Robert Olssone99b99b2010-03-18 22:44:30 +0000611 if (pkt_dev->node >= 0)
612 seq_printf(seq, " node: %d\n", pkt_dev->node);
613
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800614 seq_printf(seq, " Flags: ");
615
Luiz Capitulino222f1802006-03-20 22:16:13 -0800616 if (pkt_dev->flags & F_IPV6)
617 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Luiz Capitulino222f1802006-03-20 22:16:13 -0800619 if (pkt_dev->flags & F_IPSRC_RND)
620 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Luiz Capitulino222f1802006-03-20 22:16:13 -0800622 if (pkt_dev->flags & F_IPDST_RND)
623 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Luiz Capitulino222f1802006-03-20 22:16:13 -0800625 if (pkt_dev->flags & F_TXSIZE_RND)
626 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Luiz Capitulino222f1802006-03-20 22:16:13 -0800628 if (pkt_dev->flags & F_UDPSRC_RND)
629 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Luiz Capitulino222f1802006-03-20 22:16:13 -0800631 if (pkt_dev->flags & F_UDPDST_RND)
632 seq_printf(seq, "UDPDST_RND ");
633
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800634 if (pkt_dev->flags & F_MPLS_RND)
635 seq_printf(seq, "MPLS_RND ");
636
Robert Olsson45b270f2007-08-28 15:45:55 -0700637 if (pkt_dev->flags & F_QUEUE_MAP_RND)
638 seq_printf(seq, "QUEUE_MAP_RND ");
639
Robert Olssone6fce5b2008-08-07 02:23:01 -0700640 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
641 seq_printf(seq, "QUEUE_MAP_CPU ");
642
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700643 if (pkt_dev->cflows) {
644 if (pkt_dev->flags & F_FLOW_SEQ)
645 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
646 else
647 seq_printf(seq, "FLOW_RND ");
648 }
649
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700650#ifdef CONFIG_XFRM
651 if (pkt_dev->flags & F_IPSEC_ON)
652 seq_printf(seq, "IPSEC ");
653#endif
654
Luiz Capitulino222f1802006-03-20 22:16:13 -0800655 if (pkt_dev->flags & F_MACSRC_RND)
656 seq_printf(seq, "MACSRC_RND ");
657
658 if (pkt_dev->flags & F_MACDST_RND)
659 seq_printf(seq, "MACDST_RND ");
660
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700661 if (pkt_dev->flags & F_VID_RND)
662 seq_printf(seq, "VID_RND ");
663
664 if (pkt_dev->flags & F_SVID_RND)
665 seq_printf(seq, "SVID_RND ");
666
Robert Olssone99b99b2010-03-18 22:44:30 +0000667 if (pkt_dev->flags & F_NODE)
668 seq_printf(seq, "NODE_ALLOC ");
669
Luiz Capitulino222f1802006-03-20 22:16:13 -0800670 seq_puts(seq, "\n");
671
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000672 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000673 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000674 idle = pkt_dev->idle_acc;
675 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800676
677 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000678 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800679 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000680 (unsigned long long)pkt_dev->errors);
681
682 seq_printf(seq,
683 " started: %lluus stopped: %lluus idle: %lluus\n",
684 (unsigned long long) ktime_to_us(pkt_dev->started_at),
685 (unsigned long long) ktime_to_us(stopped),
686 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800687
688 seq_printf(seq,
689 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700690 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
691 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Luiz Capitulino222f1802006-03-20 22:16:13 -0800693 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000694 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
695 &pkt_dev->cur_in6_saddr,
696 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800697 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000698 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
699 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Luiz Capitulino222f1802006-03-20 22:16:13 -0800701 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700702 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Robert Olsson45b270f2007-08-28 15:45:55 -0700704 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
705
Luiz Capitulino222f1802006-03-20 22:16:13 -0800706 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800709 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800711 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800716
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000717static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
718 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800719{
720 int i = 0;
721 *num = 0;
722
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700723 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000724 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800725 char c;
726 *num <<= 4;
727 if (get_user(c, &user_buffer[i]))
728 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000729 value = hex_to_bin(c);
730 if (value >= 0)
731 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800732 else
733 break;
734 }
735 return i;
736}
737
Luiz Capitulino222f1802006-03-20 22:16:13 -0800738static int count_trail_chars(const char __user * user_buffer,
739 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int i;
742
743 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800744 char c;
745 if (get_user(c, &user_buffer[i]))
746 return -EFAULT;
747 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 case '\"':
749 case '\n':
750 case '\r':
751 case '\t':
752 case ' ':
753 case '=':
754 break;
755 default:
756 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759done:
760 return i;
761}
762
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000763static long num_arg(const char __user *user_buffer, unsigned long maxlen,
764 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000766 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800768
Paul Gortmakerd6182222010-10-18 12:14:44 +0000769 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800770 char c;
771 if (get_user(c, &user_buffer[i]))
772 return -EFAULT;
773 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800775 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 } else
777 break;
778 }
779 return i;
780}
781
Luiz Capitulino222f1802006-03-20 22:16:13 -0800782static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000784 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Paul Gortmakerd6182222010-10-18 12:14:44 +0000786 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800787 char c;
788 if (get_user(c, &user_buffer[i]))
789 return -EFAULT;
790 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 case '\"':
792 case '\n':
793 case '\r':
794 case '\t':
795 case ' ':
796 goto done_str;
797 break;
798 default:
799 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return i;
804}
805
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800806static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
807{
Eric Dumazet95c96172012-04-15 05:58:06 +0000808 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800809 char c;
810 ssize_t i = 0;
811 int len;
812
813 pkt_dev->nr_labels = 0;
814 do {
815 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700816 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800817 if (len <= 0)
818 return len;
819 pkt_dev->labels[n] = htonl(tmp);
820 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
821 pkt_dev->flags |= F_MPLS_RND;
822 i += len;
823 if (get_user(c, &buffer[i]))
824 return -EFAULT;
825 i++;
826 n++;
827 if (n >= MAX_MPLS_LABELS)
828 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700829 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800830
831 pkt_dev->nr_labels = n;
832 return i;
833}
834
Luiz Capitulino222f1802006-03-20 22:16:13 -0800835static ssize_t pktgen_if_write(struct file *file,
836 const char __user * user_buffer, size_t count,
837 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Joe Perches8a994a72010-07-12 10:50:23 +0000839 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800840 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000841 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 char name[16], valstr[32];
843 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800844 char *pg_result = NULL;
845 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800847
848 pg_result = &(pkt_dev->result[0]);
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000851 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return -EINVAL;
853 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800854
Paul Gortmakerd6182222010-10-18 12:14:44 +0000855 max = count;
856 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800857 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000858 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800859 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000861 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* Read variable name */
864
865 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000866 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800870 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return -EFAULT;
872 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800873
874 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800876 if (len < 0)
877 return len;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 i += len;
880
881 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000882 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700883 char tb[copy + 1];
884 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700886 tb[copy] = 0;
Joe Perchesf342cda2012-05-16 17:50:41 +0000887 pr_debug("%s,%lu buffer -:%s:-\n",
888 name, (unsigned long)count, tb);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 if (!strcmp(name, "min_pkt_size")) {
892 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000893 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800894 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800897 if (value < 14 + 20 + 8)
898 value = 14 + 20 + 8;
899 if (value != pkt_dev->min_pkt_size) {
900 pkt_dev->min_pkt_size = value;
901 pkt_dev->cur_pkt_size = value;
902 }
903 sprintf(pg_result, "OK: min_pkt_size=%u",
904 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return count;
906 }
907
Luiz Capitulino222f1802006-03-20 22:16:13 -0800908 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000910 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800911 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800914 if (value < 14 + 20 + 8)
915 value = 14 + 20 + 8;
916 if (value != pkt_dev->max_pkt_size) {
917 pkt_dev->max_pkt_size = value;
918 pkt_dev->cur_pkt_size = value;
919 }
920 sprintf(pg_result, "OK: max_pkt_size=%u",
921 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return count;
923 }
924
Luiz Capitulino222f1802006-03-20 22:16:13 -0800925 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (!strcmp(name, "pkt_size")) {
928 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000929 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800933 if (value < 14 + 20 + 8)
934 value = 14 + 20 + 8;
935 if (value != pkt_dev->min_pkt_size) {
936 pkt_dev->min_pkt_size = value;
937 pkt_dev->max_pkt_size = value;
938 pkt_dev->cur_pkt_size = value;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
941 return count;
942 }
943
Luiz Capitulino222f1802006-03-20 22:16:13 -0800944 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000946 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800947 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800950 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 sprintf(pg_result, "OK: debug=%u", debug);
952 return count;
953 }
954
Luiz Capitulino222f1802006-03-20 22:16:13 -0800955 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000957 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800958 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 i += len;
961 pkt_dev->nfrags = value;
962 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
963 return count;
964 }
965 if (!strcmp(name, "delay")) {
966 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000967 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800968 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000971 if (value == 0x7FFFFFFF)
972 pkt_dev->delay = ULLONG_MAX;
973 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000974 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000975
976 sprintf(pg_result, "OK: delay=%llu",
977 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return count;
979 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000980 if (!strcmp(name, "rate")) {
981 len = num_arg(&user_buffer[i], 10, &value);
982 if (len < 0)
983 return len;
984
985 i += len;
986 if (!value)
987 return len;
988 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
989 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000990 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000991
992 sprintf(pg_result, "OK: rate=%lu", value);
993 return count;
994 }
995 if (!strcmp(name, "ratep")) {
996 len = num_arg(&user_buffer[i], 10, &value);
997 if (len < 0)
998 return len;
999
1000 i += len;
1001 if (!value)
1002 return len;
1003 pkt_dev->delay = NSEC_PER_SEC/value;
1004 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001005 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001006
1007 sprintf(pg_result, "OK: rate=%lu", value);
1008 return count;
1009 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001010 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001012 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001013 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001016 if (value != pkt_dev->udp_src_min) {
1017 pkt_dev->udp_src_min = value;
1018 pkt_dev->cur_udp_src = value;
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1021 return count;
1022 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001023 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001025 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001026 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 if (value != pkt_dev->udp_dst_min) {
1030 pkt_dev->udp_dst_min = value;
1031 pkt_dev->cur_udp_dst = value;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1034 return count;
1035 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001036 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001038 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001042 if (value != pkt_dev->udp_src_max) {
1043 pkt_dev->udp_src_max = value;
1044 pkt_dev->cur_udp_src = value;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1047 return count;
1048 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001049 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001051 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001055 if (value != pkt_dev->udp_dst_max) {
1056 pkt_dev->udp_dst_max = value;
1057 pkt_dev->cur_udp_dst = value;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1060 return count;
1061 }
1062 if (!strcmp(name, "clone_skb")) {
1063 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001064 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001065 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001066 if ((value > 0) &&
1067 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1068 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001070 pkt_dev->clone_skb = value;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1073 return count;
1074 }
1075 if (!strcmp(name, "count")) {
1076 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001077 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001078 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 i += len;
1081 pkt_dev->count = value;
1082 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return count;
1085 }
1086 if (!strcmp(name, "src_mac_count")) {
1087 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001088 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001089 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 i += len;
1092 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001093 pkt_dev->src_mac_count = value;
1094 pkt_dev->cur_src_mac_offset = 0;
1095 }
1096 sprintf(pg_result, "OK: src_mac_count=%d",
1097 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return count;
1099 }
1100 if (!strcmp(name, "dst_mac_count")) {
1101 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001102 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001103 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 i += len;
1106 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001107 pkt_dev->dst_mac_count = value;
1108 pkt_dev->cur_dst_mac_offset = 0;
1109 }
1110 sprintf(pg_result, "OK: dst_mac_count=%d",
1111 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return count;
1113 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001114 if (!strcmp(name, "node")) {
1115 len = num_arg(&user_buffer[i], 10, &value);
1116 if (len < 0)
1117 return len;
1118
1119 i += len;
1120
1121 if (node_possible(value)) {
1122 pkt_dev->node = value;
1123 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001124 if (pkt_dev->page) {
1125 put_page(pkt_dev->page);
1126 pkt_dev->page = NULL;
1127 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001128 }
1129 else
1130 sprintf(pg_result, "ERROR: node not possible");
1131 return count;
1132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001134 char f[32];
1135 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001137 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001138 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (copy_from_user(f, &user_buffer[i], len))
1141 return -EFAULT;
1142 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001143 if (strcmp(f, "IPSRC_RND") == 0)
1144 pkt_dev->flags |= F_IPSRC_RND;
1145
1146 else if (strcmp(f, "!IPSRC_RND") == 0)
1147 pkt_dev->flags &= ~F_IPSRC_RND;
1148
1149 else if (strcmp(f, "TXSIZE_RND") == 0)
1150 pkt_dev->flags |= F_TXSIZE_RND;
1151
1152 else if (strcmp(f, "!TXSIZE_RND") == 0)
1153 pkt_dev->flags &= ~F_TXSIZE_RND;
1154
1155 else if (strcmp(f, "IPDST_RND") == 0)
1156 pkt_dev->flags |= F_IPDST_RND;
1157
1158 else if (strcmp(f, "!IPDST_RND") == 0)
1159 pkt_dev->flags &= ~F_IPDST_RND;
1160
1161 else if (strcmp(f, "UDPSRC_RND") == 0)
1162 pkt_dev->flags |= F_UDPSRC_RND;
1163
1164 else if (strcmp(f, "!UDPSRC_RND") == 0)
1165 pkt_dev->flags &= ~F_UDPSRC_RND;
1166
1167 else if (strcmp(f, "UDPDST_RND") == 0)
1168 pkt_dev->flags |= F_UDPDST_RND;
1169
1170 else if (strcmp(f, "!UDPDST_RND") == 0)
1171 pkt_dev->flags &= ~F_UDPDST_RND;
1172
1173 else if (strcmp(f, "MACSRC_RND") == 0)
1174 pkt_dev->flags |= F_MACSRC_RND;
1175
1176 else if (strcmp(f, "!MACSRC_RND") == 0)
1177 pkt_dev->flags &= ~F_MACSRC_RND;
1178
1179 else if (strcmp(f, "MACDST_RND") == 0)
1180 pkt_dev->flags |= F_MACDST_RND;
1181
1182 else if (strcmp(f, "!MACDST_RND") == 0)
1183 pkt_dev->flags &= ~F_MACDST_RND;
1184
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001185 else if (strcmp(f, "MPLS_RND") == 0)
1186 pkt_dev->flags |= F_MPLS_RND;
1187
1188 else if (strcmp(f, "!MPLS_RND") == 0)
1189 pkt_dev->flags &= ~F_MPLS_RND;
1190
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001191 else if (strcmp(f, "VID_RND") == 0)
1192 pkt_dev->flags |= F_VID_RND;
1193
1194 else if (strcmp(f, "!VID_RND") == 0)
1195 pkt_dev->flags &= ~F_VID_RND;
1196
1197 else if (strcmp(f, "SVID_RND") == 0)
1198 pkt_dev->flags |= F_SVID_RND;
1199
1200 else if (strcmp(f, "!SVID_RND") == 0)
1201 pkt_dev->flags &= ~F_SVID_RND;
1202
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001203 else if (strcmp(f, "FLOW_SEQ") == 0)
1204 pkt_dev->flags |= F_FLOW_SEQ;
1205
Robert Olsson45b270f2007-08-28 15:45:55 -07001206 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1207 pkt_dev->flags |= F_QUEUE_MAP_RND;
1208
1209 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1210 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001211
1212 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1213 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1214
1215 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1216 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001217#ifdef CONFIG_XFRM
1218 else if (strcmp(f, "IPSEC") == 0)
1219 pkt_dev->flags |= F_IPSEC_ON;
1220#endif
1221
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001222 else if (strcmp(f, "!IPV6") == 0)
1223 pkt_dev->flags &= ~F_IPV6;
1224
Robert Olssone99b99b2010-03-18 22:44:30 +00001225 else if (strcmp(f, "NODE_ALLOC") == 0)
1226 pkt_dev->flags |= F_NODE;
1227
1228 else if (strcmp(f, "!NODE_ALLOC") == 0)
1229 pkt_dev->flags &= ~F_NODE;
1230
Luiz Capitulino222f1802006-03-20 22:16:13 -08001231 else {
1232 sprintf(pg_result,
1233 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1234 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001235 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001236 "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 -08001237 return count;
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1240 return count;
1241 }
1242 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1243 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001244 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001245 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Luiz Capitulino222f1802006-03-20 22:16:13 -08001247 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001249 buf[len] = 0;
1250 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1251 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1252 strncpy(pkt_dev->dst_min, buf, len);
1253 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1254 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1255 }
1256 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001257 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001258 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1260 return count;
1261 }
1262 if (!strcmp(name, "dst_max")) {
1263 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001264 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001265 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Luiz Capitulino222f1802006-03-20 22:16:13 -08001268 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return -EFAULT;
1270
Luiz Capitulino222f1802006-03-20 22:16:13 -08001271 buf[len] = 0;
1272 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1273 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1274 strncpy(pkt_dev->dst_max, buf, len);
1275 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1276 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1277 }
1278 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001279 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 i += len;
1281 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1282 return count;
1283 }
1284 if (!strcmp(name, "dst6")) {
1285 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286 if (len < 0)
1287 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 pkt_dev->flags |= F_IPV6;
1290
Luiz Capitulino222f1802006-03-20 22:16:13 -08001291 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001293 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Amerigo Wangc468fb12012-10-09 17:48:20 +00001295 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001296 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001298 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Luiz Capitulino222f1802006-03-20 22:16:13 -08001300 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001301 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Luiz Capitulino222f1802006-03-20 22:16:13 -08001303 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 sprintf(pg_result, "OK: dst6=%s", buf);
1305 return count;
1306 }
1307 if (!strcmp(name, "dst6_min")) {
1308 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001309 if (len < 0)
1310 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 pkt_dev->flags |= F_IPV6;
1313
Luiz Capitulino222f1802006-03-20 22:16:13 -08001314 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001316 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Amerigo Wangc468fb12012-10-09 17:48:20 +00001318 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001319 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001321 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001323 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Luiz Capitulino222f1802006-03-20 22:16:13 -08001325 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 sprintf(pg_result, "OK: dst6_min=%s", buf);
1327 return count;
1328 }
1329 if (!strcmp(name, "dst6_max")) {
1330 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001331 if (len < 0)
1332 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 pkt_dev->flags |= F_IPV6;
1335
Luiz Capitulino222f1802006-03-20 22:16:13 -08001336 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Amerigo Wangc468fb12012-10-09 17:48:20 +00001340 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001341 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Luiz Capitulino222f1802006-03-20 22:16:13 -08001343 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001344 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Luiz Capitulino222f1802006-03-20 22:16:13 -08001346 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 sprintf(pg_result, "OK: dst6_max=%s", buf);
1348 return count;
1349 }
1350 if (!strcmp(name, "src6")) {
1351 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001352 if (len < 0)
1353 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 pkt_dev->flags |= F_IPV6;
1356
Luiz Capitulino222f1802006-03-20 22:16:13 -08001357 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Amerigo Wangc468fb12012-10-09 17:48:20 +00001361 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001362 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001364 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Luiz Capitulino222f1802006-03-20 22:16:13 -08001366 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001367 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001368
1369 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 sprintf(pg_result, "OK: src6=%s", buf);
1371 return count;
1372 }
1373 if (!strcmp(name, "src_min")) {
1374 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001375 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001376 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001377
Luiz Capitulino222f1802006-03-20 22:16:13 -08001378 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001380 buf[len] = 0;
1381 if (strcmp(buf, pkt_dev->src_min) != 0) {
1382 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1383 strncpy(pkt_dev->src_min, buf, len);
1384 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1385 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1386 }
1387 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001388 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 i += len;
1390 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1391 return count;
1392 }
1393 if (!strcmp(name, "src_max")) {
1394 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001395 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001396 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001397
Luiz Capitulino222f1802006-03-20 22:16:13 -08001398 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001400 buf[len] = 0;
1401 if (strcmp(buf, pkt_dev->src_max) != 0) {
1402 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1403 strncpy(pkt_dev->src_max, buf, len);
1404 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1405 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1406 }
1407 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001408 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 i += len;
1410 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1411 return count;
1412 }
1413 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001415 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001416 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001419 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001422 if (!mac_pton(valstr, pkt_dev->dst_mac))
1423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* Set up Dest MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001425 memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001426
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001427 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return count;
1429 }
1430 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001432 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001433 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001436 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001439 if (!mac_pton(valstr, pkt_dev->src_mac))
1440 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001441 /* Set up Src MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001442 memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001443
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001444 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return count;
1446 }
1447
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448 if (!strcmp(name, "clear_counters")) {
1449 pktgen_clear_counters(pkt_dev);
1450 sprintf(pg_result, "OK: Clearing counters.\n");
1451 return count;
1452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (!strcmp(name, "flows")) {
1455 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001456 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001457 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 i += len;
1460 if (value > MAX_CFLOWS)
1461 value = MAX_CFLOWS;
1462
1463 pkt_dev->cflows = value;
1464 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1465 return count;
1466 }
1467
1468 if (!strcmp(name, "flowlen")) {
1469 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001470 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001471 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 i += len;
1474 pkt_dev->lflow = value;
1475 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1476 return count;
1477 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001478
Robert Olsson45b270f2007-08-28 15:45:55 -07001479 if (!strcmp(name, "queue_map_min")) {
1480 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001481 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001482 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001483
Robert Olsson45b270f2007-08-28 15:45:55 -07001484 i += len;
1485 pkt_dev->queue_map_min = value;
1486 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1487 return count;
1488 }
1489
1490 if (!strcmp(name, "queue_map_max")) {
1491 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001492 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001493 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001494
Robert Olsson45b270f2007-08-28 15:45:55 -07001495 i += len;
1496 pkt_dev->queue_map_max = value;
1497 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1498 return count;
1499 }
1500
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001501 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001502 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001503
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001504 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001505 if (len < 0)
1506 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001507 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001508 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001509 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001510 cnt += sprintf(pg_result + cnt,
1511 "%08x%s", ntohl(pkt_dev->labels[n]),
1512 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001513
1514 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1515 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1516 pkt_dev->svlan_id = 0xffff;
1517
1518 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001519 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001520 }
1521 return count;
1522 }
1523
1524 if (!strcmp(name, "vlan_id")) {
1525 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001526 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001527 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001528
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001529 i += len;
1530 if (value <= 4095) {
1531 pkt_dev->vlan_id = value; /* turn on VLAN */
1532
1533 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001534 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001535
1536 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001537 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001538
1539 pkt_dev->nr_labels = 0; /* turn off MPLS */
1540 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1541 } else {
1542 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1543 pkt_dev->svlan_id = 0xffff;
1544
1545 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001546 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001547 }
1548 return count;
1549 }
1550
1551 if (!strcmp(name, "vlan_p")) {
1552 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001553 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001554 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001555
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001556 i += len;
1557 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1558 pkt_dev->vlan_p = value;
1559 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1560 } else {
1561 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1562 }
1563 return count;
1564 }
1565
1566 if (!strcmp(name, "vlan_cfi")) {
1567 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001568 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001569 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001570
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001571 i += len;
1572 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1573 pkt_dev->vlan_cfi = value;
1574 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1575 } else {
1576 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1577 }
1578 return count;
1579 }
1580
1581 if (!strcmp(name, "svlan_id")) {
1582 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001583 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001584 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001585
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001586 i += len;
1587 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1588 pkt_dev->svlan_id = value; /* turn on SVLAN */
1589
1590 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001591 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001592
1593 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001594 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001595
1596 pkt_dev->nr_labels = 0; /* turn off MPLS */
1597 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1598 } else {
1599 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1600 pkt_dev->svlan_id = 0xffff;
1601
1602 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001603 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001604 }
1605 return count;
1606 }
1607
1608 if (!strcmp(name, "svlan_p")) {
1609 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001610 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001611 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001612
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001613 i += len;
1614 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1615 pkt_dev->svlan_p = value;
1616 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1617 } else {
1618 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1619 }
1620 return count;
1621 }
1622
1623 if (!strcmp(name, "svlan_cfi")) {
1624 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001625 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001626 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001627
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001628 i += len;
1629 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1630 pkt_dev->svlan_cfi = value;
1631 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1632 } else {
1633 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1634 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001635 return count;
1636 }
1637
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001638 if (!strcmp(name, "tos")) {
1639 __u32 tmp_value = 0;
1640 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001641 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001642 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001643
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001644 i += len;
1645 if (len == 2) {
1646 pkt_dev->tos = tmp_value;
1647 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1648 } else {
1649 sprintf(pg_result, "ERROR: tos must be 00-ff");
1650 }
1651 return count;
1652 }
1653
1654 if (!strcmp(name, "traffic_class")) {
1655 __u32 tmp_value = 0;
1656 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001657 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001658 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001659
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001660 i += len;
1661 if (len == 2) {
1662 pkt_dev->traffic_class = tmp_value;
1663 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1664 } else {
1665 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1666 }
1667 return count;
1668 }
1669
John Fastabend9e50e3a2010-11-16 19:12:28 +00001670 if (!strcmp(name, "skb_priority")) {
1671 len = num_arg(&user_buffer[i], 9, &value);
1672 if (len < 0)
1673 return len;
1674
1675 i += len;
1676 pkt_dev->skb_priority = value;
1677 sprintf(pg_result, "OK: skb_priority=%i",
1678 pkt_dev->skb_priority);
1679 return count;
1680 }
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1683 return -EINVAL;
1684}
1685
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001686static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Al Virod9dda782013-03-31 18:16:14 -04001688 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001689}
1690
Arjan van de Ven9a321442007-02-12 00:55:35 -08001691static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001692 .owner = THIS_MODULE,
1693 .open = pktgen_if_open,
1694 .read = seq_read,
1695 .llseek = seq_lseek,
1696 .write = pktgen_if_write,
1697 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001698};
1699
1700static int pktgen_thread_show(struct seq_file *seq, void *v)
1701{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001702 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001703 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001705 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Luiz Capitulino222f1802006-03-20 22:16:13 -08001707 seq_printf(seq, "Running: ");
1708
1709 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001710 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001711 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001712 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Luiz Capitulino222f1802006-03-20 22:16:13 -08001714 seq_printf(seq, "\nStopped: ");
1715
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001716 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001717 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001718 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001721 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001723 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Luiz Capitulino222f1802006-03-20 22:16:13 -08001725 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001730static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001731 const char __user * user_buffer,
1732 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Joe Perches8a994a72010-07-12 10:50:23 +00001734 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001736 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001738 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001741 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return -EINVAL;
1743 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001744
Paul Gortmakerd6182222010-10-18 12:14:44 +00001745 max = count;
1746 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001747 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001748 return len;
1749
Paul Gortmakerd6182222010-10-18 12:14:44 +00001750 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /* Read variable name */
1753
1754 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001755 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001756 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 memset(name, 0, sizeof(name));
1759 if (copy_from_user(name, &user_buffer[i], len))
1760 return -EFAULT;
1761 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001762
Luiz Capitulino222f1802006-03-20 22:16:13 -08001763 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001765 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001766 return len;
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 i += len;
1769
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001770 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001771 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001774 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 ret = -EINVAL;
1776 goto out;
1777 }
1778
1779 pg_result = &(t->result[0]);
1780
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 if (!strcmp(name, "add_device")) {
1782 char f[32];
1783 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001785 if (len < 0) {
1786 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto out;
1788 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001789 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return -EFAULT;
1791 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001792 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001793 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001794 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001795 if (!ret) {
1796 ret = count;
1797 sprintf(pg_result, "OK: add_device=%s", f);
1798 } else
1799 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 goto out;
1801 }
1802
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001804 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001805 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001806 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001807 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001809 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto out;
1811 }
1812
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001814 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001815 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 goto out;
1817 }
1818
1819 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001820out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return ret;
1822}
1823
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001824static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Al Virod9dda782013-03-31 18:16:14 -04001826 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Arjan van de Ven9a321442007-02-12 00:55:35 -08001829static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001830 .owner = THIS_MODULE,
1831 .open = pktgen_thread_open,
1832 .read = seq_read,
1833 .llseek = seq_lseek,
1834 .write = pktgen_thread_write,
1835 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001836};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001839static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1840 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 struct pktgen_thread *t;
1843 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001844 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Cong Wang4e58a022013-01-28 19:55:53 +00001846 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001847 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001849 if (remove) {
1850 if_lock(t);
1851 pkt_dev->removal_mark = 1;
1852 t->control |= T_REMDEV;
1853 if_unlock(t);
1854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 break;
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001858 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001861/*
1862 * mark a device for removal
1863 */
Cong Wang4e58a022013-01-28 19:55:53 +00001864static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001867 const int max_tries = 10, msec_per_try = 125;
1868 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001869
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001870 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001871 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001872
Luiz Capitulino222f1802006-03-20 22:16:13 -08001873 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001874
Cong Wang4e58a022013-01-28 19:55:53 +00001875 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001876 if (pkt_dev == NULL)
1877 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001878
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001879 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001880 pr_debug("%s: waiting for %s to disappear....\n",
1881 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001882 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001883 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001884
1885 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001886 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1887 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001888 break;
1889 }
1890
1891 }
1892
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001893 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001894}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001895
Cong Wang4e58a022013-01-28 19:55:53 +00001896static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001897{
1898 struct pktgen_thread *t;
1899
Cong Wang4e58a022013-01-28 19:55:53 +00001900 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001901 struct pktgen_dev *pkt_dev;
1902
1903 list_for_each_entry(pkt_dev, &t->if_list, list) {
1904 if (pkt_dev->odev != dev)
1905 continue;
1906
David Howellsa8ca16e2013-04-12 17:27:28 +01001907 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001908
Alexey Dobriyan29753152009-08-28 23:34:43 -07001909 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00001910 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07001911 &pktgen_if_fops,
1912 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001913 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001914 pr_err("can't move proc entry for '%s'\n",
1915 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001916 break;
1917 }
1918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
Luiz Capitulino222f1802006-03-20 22:16:13 -08001921static int pktgen_device_event(struct notifier_block *unused,
1922 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Jiri Pirko351638e2013-05-28 01:30:21 +00001924 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00001925 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Cong Wang4e58a022013-01-28 19:55:53 +00001927 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001928 return NOTIFY_DONE;
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /* It is OK that we do not hold the group lock right now,
1931 * as we run under the RTNL lock.
1932 */
1933
1934 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001935 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00001936 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00001940 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 return NOTIFY_DONE;
1945}
1946
Cong Wang4e58a022013-01-28 19:55:53 +00001947static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1948 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001949 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001950{
1951 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001952 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001953
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001954 for (i = 0; ifname[i] != '@'; i++) {
1955 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001956 break;
1957
1958 b[i] = ifname[i];
1959 }
1960 b[i] = 0;
1961
Cong Wang4e58a022013-01-28 19:55:53 +00001962 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07001963}
1964
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/* Associate pktgen_dev with a device. */
1967
Cong Wang4e58a022013-01-28 19:55:53 +00001968static int pktgen_setup_dev(const struct pktgen_net *pn,
1969 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001972 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (pkt_dev->odev) {
1976 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001977 pkt_dev->odev = NULL;
1978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Cong Wang4e58a022013-01-28 19:55:53 +00001980 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001982 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001983 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001987 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001988 err = -EINVAL;
1989 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001990 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001991 err = -ENETDOWN;
1992 } else {
1993 pkt_dev->odev = odev;
1994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001998 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
2001/* Read pkt_dev from the interface and set up internal pktgen_dev
2002 * structure to have the right information to create/send packets
2003 */
2004static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2005{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002006 int ntxq;
2007
Luiz Capitulino222f1802006-03-20 22:16:13 -08002008 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002009 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002010 sprintf(pkt_dev->result,
2011 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2012 return;
2013 }
2014
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002015 /* make sure that we don't pick a non-existing transmit queue */
2016 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002017
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002018 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002019 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2020 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2021 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002022 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002023 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002024 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002025 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2026 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2027 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002028 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002029 }
2030
Luiz Capitulino222f1802006-03-20 22:16:13 -08002031 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002033 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Luiz Capitulino222f1802006-03-20 22:16:13 -08002036 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002037 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Luiz Capitulino222f1802006-03-20 22:16:13 -08002039 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002040 int i, set = 0, err = 1;
2041 struct inet6_dev *idev;
2042
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002043 if (pkt_dev->min_pkt_size == 0) {
2044 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2045 + sizeof(struct udphdr)
2046 + sizeof(struct pktgen_hdr)
2047 + pkt_dev->pkt_overhead;
2048 }
2049
Luiz Capitulino222f1802006-03-20 22:16:13 -08002050 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2051 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 set = 1;
2053 break;
2054 }
2055
Luiz Capitulino222f1802006-03-20 22:16:13 -08002056 if (!set) {
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /*
2059 * Use linklevel address if unconfigured.
2060 *
2061 * use ipv6_get_lladdr if/when it's get exported
2062 */
2063
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002064 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002065 idev = __in6_dev_get(pkt_dev->odev);
2066 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 struct inet6_ifaddr *ifp;
2068
2069 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002070 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2071 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002072 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002073 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 err = 0;
2075 break;
2076 }
2077 }
2078 read_unlock_bh(&idev->lock);
2079 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002080 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002081 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002082 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002084 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002085 if (pkt_dev->min_pkt_size == 0) {
2086 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2087 + sizeof(struct udphdr)
2088 + sizeof(struct pktgen_hdr)
2089 + pkt_dev->pkt_overhead;
2090 }
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 pkt_dev->saddr_min = 0;
2093 pkt_dev->saddr_max = 0;
2094 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002095
2096 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002099 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (in_dev) {
2101 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002102 pkt_dev->saddr_min =
2103 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 pkt_dev->saddr_max = pkt_dev->saddr_min;
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002108 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2110 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2111 }
2112
2113 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2114 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2115 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002116 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002117 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2118 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2119 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2120
Luiz Capitulino222f1802006-03-20 22:16:13 -08002121 pkt_dev->cur_dst_mac_offset = 0;
2122 pkt_dev->cur_src_mac_offset = 0;
2123 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2124 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2125 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2126 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 pkt_dev->nflows = 0;
2128}
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002131static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2132{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002133 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002134 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002135 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002136
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002137 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2138 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002139
Daniel Turull43d28b62010-06-09 22:49:57 +00002140 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002141 if (remaining <= 0) {
2142 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002143 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002144 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002145
Daniel Borkmann398f3822012-10-28 08:27:19 +00002146 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002147 if (remaining < 100000) {
2148 /* for small delays (<100us), just loop until limit is reached */
2149 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002150 end_time = ktime_get();
2151 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002152 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002153 /* see do_nanosleep */
2154 hrtimer_init_sleeper(&t, current);
2155 do {
2156 set_current_state(TASK_INTERRUPTIBLE);
2157 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2158 if (!hrtimer_active(&t.timer))
2159 t.task = NULL;
2160
2161 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002164 hrtimer_cancel(&t.timer);
2165 } while (t.task && pkt_dev->running && !signal_pending(current));
2166 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002167 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002169
2170 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002171 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002174static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2175{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002176 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002177 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2178 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2179 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2180}
2181
Stephen Hemminger648fda72009-08-27 13:55:07 +00002182static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002183{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002184 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002185}
2186
2187static inline int f_pick(struct pktgen_dev *pkt_dev)
2188{
2189 int flow = pkt_dev->curfl;
2190
2191 if (pkt_dev->flags & F_FLOW_SEQ) {
2192 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2193 /* reset time */
2194 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002195 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002196 pkt_dev->curfl += 1;
2197 if (pkt_dev->curfl >= pkt_dev->cflows)
2198 pkt_dev->curfl = 0; /*reset */
2199 }
2200 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002201 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002202 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002203
Robert Olsson1211a642008-08-05 18:44:26 -07002204 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002205 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002206 pkt_dev->flows[flow].flags = 0;
2207 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002208 }
2209
2210 return pkt_dev->curfl;
2211}
2212
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002213
2214#ifdef CONFIG_XFRM
2215/* If there was already an IPSEC SA, we keep it as is, else
2216 * we go look for it ...
2217*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002218#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002219static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002220{
2221 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002222 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002223 if (!x) {
2224 /*slow path: we dont already have xfrm_state*/
Cong Wang4e58a022013-01-28 19:55:53 +00002225 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002226 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002227 (xfrm_address_t *)&pkt_dev->cur_saddr,
2228 AF_INET,
2229 pkt_dev->ipsmode,
2230 pkt_dev->ipsproto, 0);
2231 if (x) {
2232 pkt_dev->flows[flow].x = x;
2233 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002234 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002235 }
2236
2237 }
2238}
2239#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002240static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2241{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002242
2243 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2244 pkt_dev->cur_queue_map = smp_processor_id();
2245
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002246 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002247 __u16 t;
2248 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002249 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002250 (pkt_dev->queue_map_max -
2251 pkt_dev->queue_map_min + 1)
2252 + pkt_dev->queue_map_min;
2253 } else {
2254 t = pkt_dev->cur_queue_map + 1;
2255 if (t > pkt_dev->queue_map_max)
2256 t = pkt_dev->queue_map_min;
2257 }
2258 pkt_dev->cur_queue_map = t;
2259 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002260 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 -07002261}
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263/* Increment/randomize headers according to flags and current values
2264 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2265 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002266static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2267{
2268 __u32 imn;
2269 __u32 imx;
2270 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002272 if (pkt_dev->cflows)
2273 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002276 if (pkt_dev->src_mac_count > 1) {
2277 __u32 mc;
2278 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Luiz Capitulino222f1802006-03-20 22:16:13 -08002280 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002281 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002282 else {
2283 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002284 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002285 pkt_dev->src_mac_count)
2286 pkt_dev->cur_src_mac_offset = 0;
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Luiz Capitulino222f1802006-03-20 22:16:13 -08002289 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2290 pkt_dev->hh[11] = tmp;
2291 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2292 pkt_dev->hh[10] = tmp;
2293 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2294 pkt_dev->hh[9] = tmp;
2295 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2296 pkt_dev->hh[8] = tmp;
2297 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2298 pkt_dev->hh[7] = tmp;
2299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Luiz Capitulino222f1802006-03-20 22:16:13 -08002301 /* Deal with Destination MAC */
2302 if (pkt_dev->dst_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_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002307 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Luiz Capitulino222f1802006-03-20 22:16:13 -08002309 else {
2310 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002311 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002312 pkt_dev->dst_mac_count) {
2313 pkt_dev->cur_dst_mac_offset = 0;
2314 }
2315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Luiz Capitulino222f1802006-03-20 22:16:13 -08002317 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2318 pkt_dev->hh[5] = tmp;
2319 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2320 pkt_dev->hh[4] = tmp;
2321 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2322 pkt_dev->hh[3] = tmp;
2323 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2324 pkt_dev->hh[2] = tmp;
2325 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2326 pkt_dev->hh[1] = tmp;
2327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002329 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002330 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002331 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002332 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2333 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002334 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002335 htonl(0x000fffff));
2336 }
2337
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002338 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002339 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002340 }
2341
2342 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002343 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002344 }
2345
Luiz Capitulino222f1802006-03-20 22:16:13 -08002346 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2347 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002348 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002349 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2350 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Luiz Capitulino222f1802006-03-20 22:16:13 -08002352 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 pkt_dev->cur_udp_src++;
2354 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2355 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002356 }
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Luiz Capitulino222f1802006-03-20 22:16:13 -08002359 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2360 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002361 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002362 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2363 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002364 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002366 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002368 }
2369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 if (!(pkt_dev->flags & F_IPV6)) {
2372
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002373 imn = ntohl(pkt_dev->saddr_min);
2374 imx = ntohl(pkt_dev->saddr_max);
2375 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002377 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002378 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 else {
2380 t = ntohl(pkt_dev->cur_saddr);
2381 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002382 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
2386 pkt_dev->cur_saddr = htonl(t);
2387 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002388
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002389 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2391 } else {
Al Viro252e33462006-11-14 20:48:11 -08002392 imn = ntohl(pkt_dev->daddr_min);
2393 imx = ntohl(pkt_dev->daddr_max);
2394 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002396 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (pkt_dev->flags & F_IPDST_RND) {
2398
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002399 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002400 t = prandom_u32() %
2401 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002402 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002403 } while (ipv4_is_loopback(s) ||
2404 ipv4_is_multicast(s) ||
2405 ipv4_is_lbcast(s) ||
2406 ipv4_is_zeronet(s) ||
2407 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002408 pkt_dev->cur_daddr = s;
2409 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 t = ntohl(pkt_dev->cur_daddr);
2411 t++;
2412 if (t > imx) {
2413 t = imn;
2414 }
2415 pkt_dev->cur_daddr = htonl(t);
2416 }
2417 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002418 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002419 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002420 pkt_dev->flows[flow].cur_daddr =
2421 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002422#ifdef CONFIG_XFRM
2423 if (pkt_dev->flags & F_IPSEC_ON)
2424 get_ipsec_sa(pkt_dev, flow);
2425#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 pkt_dev->nflows++;
2427 }
2428 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002429 } else { /* IPV6 * */
2430
Joe Perches06e30412012-10-18 17:55:31 +00002431 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 int i;
2433
2434 /* Only random destinations yet */
2435
Luiz Capitulino222f1802006-03-20 22:16:13 -08002436 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002438 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002439 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2440 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444
Luiz Capitulino222f1802006-03-20 22:16:13 -08002445 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2446 __u32 t;
2447 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002448 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002449 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2450 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002451 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002453 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002455 }
2456 pkt_dev->cur_pkt_size = t;
2457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002459 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 pkt_dev->flows[flow].count++;
2462}
2463
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002464
2465#ifdef CONFIG_XFRM
2466static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2467{
2468 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2469 int err = 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002470
2471 if (!x)
2472 return 0;
2473 /* XXX: we dont support tunnel mode for now until
2474 * we resolve the dst issue */
2475 if (x->props.mode != XFRM_MODE_TRANSPORT)
2476 return 0;
2477
2478 spin_lock(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002479
Herbert Xu13996372007-10-17 21:35:51 -07002480 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002481 if (err)
2482 goto error;
2483 err = x->type->output(x, skb);
2484 if (err)
2485 goto error;
2486
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002487 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002488 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002489error:
2490 spin_unlock(&x->lock);
2491 return err;
2492}
2493
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002494static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002495{
2496 if (pkt_dev->cflows) {
2497 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002498 int i;
2499 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002500 struct xfrm_state *x = pkt_dev->flows[i].x;
2501 if (x) {
2502 xfrm_state_put(x);
2503 pkt_dev->flows[i].x = NULL;
2504 }
2505 }
2506 }
2507}
2508
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002509static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002510 struct sk_buff *skb, __be16 protocol)
2511{
2512 if (pkt_dev->flags & F_IPSEC_ON) {
2513 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2514 int nhead = 0;
2515 if (x) {
2516 int ret;
2517 __u8 *eth;
2518 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002519 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002520 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2521 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002522 pr_err("Error expanding ipsec packet %d\n",
2523 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002524 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002525 }
2526 }
2527
2528 /* ipsec is not expecting ll header */
2529 skb_pull(skb, ETH_HLEN);
2530 ret = pktgen_output_ipsec(skb, pkt_dev);
2531 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002532 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002533 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002534 }
2535 /* restore ll */
2536 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2537 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002538 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002539 }
2540 }
2541 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002542err:
2543 kfree_skb(skb);
2544 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002545}
2546#endif
2547
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002548static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2549{
Eric Dumazet95c96172012-04-15 05:58:06 +00002550 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002551 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002552 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002553
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002554 mpls--;
2555 *mpls |= MPLS_STACK_BOTTOM;
2556}
2557
Al Viro0f37c602006-11-03 03:49:56 -08002558static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2559 unsigned int prio)
2560{
2561 return htons(id | (cfi << 12) | (prio << 13));
2562}
2563
Eric Dumazet26ad7872011-01-25 13:26:05 -08002564static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2565 int datalen)
2566{
2567 struct timeval timestamp;
2568 struct pktgen_hdr *pgh;
2569
2570 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2571 datalen -= sizeof(*pgh);
2572
2573 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002574 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002575 } else {
2576 int frags = pkt_dev->nfrags;
2577 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002578 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002579
2580
2581 if (frags > MAX_SKB_FRAGS)
2582 frags = MAX_SKB_FRAGS;
2583 len = datalen - frags * PAGE_SIZE;
2584 if (len > 0) {
2585 memset(skb_put(skb, len), 0, len);
2586 datalen = frags * PAGE_SIZE;
2587 }
2588
2589 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002590 frag_len = (datalen/frags) < PAGE_SIZE ?
2591 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002592 while (datalen > 0) {
2593 if (unlikely(!pkt_dev->page)) {
2594 int node = numa_node_id();
2595
2596 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2597 node = pkt_dev->node;
2598 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2599 if (!pkt_dev->page)
2600 break;
2601 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002602 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002603 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002604 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002605 /*last fragment, fill rest of data*/
2606 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002607 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2608 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002609 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002610 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2611 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2612 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2613 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002614 i++;
2615 skb_shinfo(skb)->nr_frags = i;
2616 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002617 }
2618
2619 /* Stamp the time, and sequence number,
2620 * convert them to network byte order
2621 */
2622 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2623 pgh->seq_num = htonl(pkt_dev->seq_num);
2624
2625 do_gettimeofday(&timestamp);
2626 pgh->tv_sec = htonl(timestamp.tv_sec);
2627 pgh->tv_usec = htonl(timestamp.tv_usec);
2628}
2629
Luiz Capitulino222f1802006-03-20 22:16:13 -08002630static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2631 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 struct sk_buff *skb = NULL;
2634 __u8 *eth;
2635 struct udphdr *udph;
2636 int datalen, iplen;
2637 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002638 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002639 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002640 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2641 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2642 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2643 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002644 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002645
2646 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002647 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002648
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002649 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002650 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002651
Robert Olsson64053be2005-06-26 15:27:10 -07002652 /* Update any of the values, used when we're incrementing various
2653 * fields.
2654 */
2655 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002656 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002657
David S. Miller7ac54592006-01-18 14:19:10 -08002658 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002659
2660 if (pkt_dev->flags & F_NODE) {
2661 int node;
2662
2663 if (pkt_dev->node >= 0)
2664 node = pkt_dev->node;
2665 else
2666 node = numa_node_id();
2667
2668 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2669 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2670 if (likely(skb)) {
2671 skb_reserve(skb, NET_SKB_PAD);
2672 skb->dev = odev;
2673 }
2674 }
2675 else
2676 skb = __netdev_alloc_skb(odev,
2677 pkt_dev->cur_pkt_size + 64
2678 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (!skb) {
2681 sprintf(pkt_dev->result, "No memory");
2682 return NULL;
2683 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002684 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
David S. Miller7ac54592006-01-18 14:19:10 -08002686 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 /* Reserve for ethernet and IP header */
2689 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002690 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2691 if (pkt_dev->nr_labels)
2692 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002693
2694 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002695 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002696 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002697 *svlan_tci = build_tci(pkt_dev->svlan_id,
2698 pkt_dev->svlan_cfi,
2699 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002700 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002701 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002702 }
2703 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002704 *vlan_tci = build_tci(pkt_dev->vlan_id,
2705 pkt_dev->vlan_cfi,
2706 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002707 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002708 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002709 }
2710
Thomas Graf525cebe2013-06-03 11:49:23 +00002711 skb_set_mac_header(skb, 0);
2712 skb_set_network_header(skb, skb->len);
2713 iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2714
2715 skb_set_transport_header(skb, skb->len);
2716 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002717 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002718 skb->priority = pkt_dev->skb_priority;
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002721 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002723 /* Eth + IPh + UDPh + mpls */
2724 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002725 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002726 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 udph->source = htons(pkt_dev->cur_udp_src);
2730 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002731 udph->len = htons(datalen + 8); /* DATA + udphdr */
2732 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 iph->ihl = 5;
2735 iph->version = 4;
2736 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002737 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002738 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 iph->saddr = pkt_dev->cur_saddr;
2740 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002741 iph->id = htons(pkt_dev->ip_id);
2742 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 iph->frag_off = 0;
2744 iplen = 20 + 8 + datalen;
2745 iph->tot_len = htons(iplen);
2746 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002747 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002748 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 skb->dev = odev;
2750 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002751 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002752
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002753#ifdef CONFIG_XFRM
2754 if (!process_ipsec(pkt_dev, skb, protocol))
2755 return NULL;
2756#endif
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return skb;
2759}
2760
Luiz Capitulino222f1802006-03-20 22:16:13 -08002761static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2762 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 struct sk_buff *skb = NULL;
2765 __u8 *eth;
2766 struct udphdr *udph;
2767 int datalen;
2768 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002769 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002770 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002771 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2772 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2773 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2774 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002775 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002776
2777 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002778 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002779
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002780 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002781 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002782
Robert Olsson64053be2005-06-26 15:27:10 -07002783 /* Update any of the values, used when we're incrementing various
2784 * fields.
2785 */
2786 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002787 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002788
Stephen Hemmingere4707572009-08-27 13:55:13 +00002789 skb = __netdev_alloc_skb(odev,
2790 pkt_dev->cur_pkt_size + 64
2791 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (!skb) {
2793 sprintf(pkt_dev->result, "No memory");
2794 return NULL;
2795 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002796 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 skb_reserve(skb, 16);
2799
2800 /* Reserve for ethernet and IP header */
2801 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002802 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2803 if (pkt_dev->nr_labels)
2804 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002805
2806 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002807 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002808 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002809 *svlan_tci = build_tci(pkt_dev->svlan_id,
2810 pkt_dev->svlan_cfi,
2811 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002812 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002813 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002814 }
2815 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002816 *vlan_tci = build_tci(pkt_dev->vlan_id,
2817 pkt_dev->vlan_cfi,
2818 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002819 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002820 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002821 }
2822
Thomas Graf525cebe2013-06-03 11:49:23 +00002823 skb_set_mac_header(skb, 0);
2824 skb_set_network_header(skb, skb->len);
2825 iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2826
2827 skb_set_transport_header(skb, skb->len);
2828 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002829 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002830 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002833 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002834
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002835 /* Eth + IPh + UDPh + mpls */
2836 datalen = pkt_dev->cur_pkt_size - 14 -
2837 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002838 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002840 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002842 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
2844
2845 udph->source = htons(pkt_dev->cur_udp_src);
2846 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002847 udph->len = htons(datalen + sizeof(struct udphdr));
2848 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002850 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002852 if (pkt_dev->traffic_class) {
2853 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002854 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002855 }
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 iph->hop_limit = 32;
2858
2859 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2860 iph->nexthdr = IPPROTO_UDP;
2861
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002862 iph->daddr = pkt_dev->cur_in6_daddr;
2863 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002865 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 skb->dev = odev;
2867 skb->pkt_type = PACKET_HOST;
2868
Eric Dumazet26ad7872011-01-25 13:26:05 -08002869 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return skb;
2872}
2873
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002874static struct sk_buff *fill_packet(struct net_device *odev,
2875 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002877 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return fill_packet_ipv6(odev, pkt_dev);
2879 else
2880 return fill_packet_ipv4(odev, pkt_dev);
2881}
2882
Luiz Capitulino222f1802006-03-20 22:16:13 -08002883static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002885 pkt_dev->seq_num = 1;
2886 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002888 pkt_dev->tx_bytes = 0;
2889 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
2892/* Set up structure for sending pkts, clear counters */
2893
2894static void pktgen_run(struct pktgen_thread *t)
2895{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002896 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 int started = 0;
2898
Joe Perchesf9467ea2010-06-21 12:29:14 +00002899 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002902 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 /*
2905 * setup odev and create initial packet.
2906 */
2907 pktgen_setup_inject(pkt_dev);
2908
Luiz Capitulino222f1802006-03-20 22:16:13 -08002909 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002911 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00002913 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002914
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002915 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 strcpy(pkt_dev->result, "Starting");
2918 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002919 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 strcpy(pkt_dev->result, "Error starting");
2921 }
2922 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002923 if (started)
2924 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
Cong Wang4e58a022013-01-28 19:55:53 +00002927static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002929 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Joe Perchesf9467ea2010-06-21 12:29:14 +00002931 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002933 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002934
Cong Wang4e58a022013-01-28 19:55:53 +00002935 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002936 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002937
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002938 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
Stephen Hemminger648fda72009-08-27 13:55:07 +00002941static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002943 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002945 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00002946 if (pkt_dev->running)
2947 return 1;
2948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Luiz Capitulino222f1802006-03-20 22:16:13 -08002951static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002953 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Luiz Capitulino222f1802006-03-20 22:16:13 -08002955 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Luiz Capitulino222f1802006-03-20 22:16:13 -08002957 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Luiz Capitulino222f1802006-03-20 22:16:13 -08002959 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Luiz Capitulino222f1802006-03-20 22:16:13 -08002961 if (signal_pending(current))
2962 goto signal;
2963 if_lock(t);
2964 }
2965 if_unlock(t);
2966 return 1;
2967signal:
2968 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
Cong Wang4e58a022013-01-28 19:55:53 +00002971static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002973 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002975
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002976 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002977
Cong Wang4e58a022013-01-28 19:55:53 +00002978 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002980 if (sig == 0)
2981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002983
2984 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00002985 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002987
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002988 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return sig;
2990}
2991
Cong Wang4e58a022013-01-28 19:55:53 +00002992static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002994 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Joe Perchesf9467ea2010-06-21 12:29:14 +00002996 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002998 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Cong Wang4e58a022013-01-28 19:55:53 +00003000 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003002
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003003 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003005 /* Propagate thread->control */
3006 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003007
Cong Wang4e58a022013-01-28 19:55:53 +00003008 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
Cong Wang4e58a022013-01-28 19:55:53 +00003011static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003012{
3013 struct pktgen_thread *t;
3014
Joe Perchesf9467ea2010-06-21 12:29:14 +00003015 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003016
3017 mutex_lock(&pktgen_thread_lock);
3018
Cong Wang4e58a022013-01-28 19:55:53 +00003019 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003020 t->control |= (T_REMDEVALL);
3021
3022 mutex_unlock(&pktgen_thread_lock);
3023
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003024 /* Propagate thread->control */
3025 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003026
Cong Wang4e58a022013-01-28 19:55:53 +00003027 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003028}
3029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3031{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003032 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003033 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003034 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3035 pkt_dev->started_at);
3036 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Daniel Turull03a14ab2011-03-09 14:11:00 -08003038 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003039 (unsigned long long)ktime_to_us(elapsed),
3040 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3041 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003042 (unsigned long long)pkt_dev->sofar,
3043 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003045 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3046 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048 bps = pps * 8 * pkt_dev->cur_pkt_size;
3049
3050 mbps = bps;
3051 do_div(mbps, 1000000);
3052 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3053 (unsigned long long)pps,
3054 (unsigned long long)mbps,
3055 (unsigned long long)bps,
3056 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003060static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003062 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003063
Luiz Capitulino222f1802006-03-20 22:16:13 -08003064 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003065 pr_warning("interface: %s is already stopped\n",
3066 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067 return -EINVAL;
3068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003070 kfree_skb(pkt_dev->skb);
3071 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003072 pkt_dev->stopped_at = ktime_get();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003073 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003075 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Luiz Capitulino222f1802006-03-20 22:16:13 -08003077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
Luiz Capitulino222f1802006-03-20 22:16:13 -08003080static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003082 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if_lock(t);
3085
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003086 list_for_each_entry(pkt_dev, &t->if_list, list) {
3087 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003088 continue;
3089 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003090 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003091 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003092 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
3094 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003095 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Luiz Capitulino222f1802006-03-20 22:16:13 -08003098static void pktgen_stop(struct pktgen_thread *t)
3099{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003100 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Joe Perchesf9467ea2010-06-21 12:29:14 +00003102 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Luiz Capitulino222f1802006-03-20 22:16:13 -08003104 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003106 list_for_each_entry(pkt_dev, &t->if_list, list) {
3107 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Luiz Capitulino222f1802006-03-20 22:16:13 -08003110 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003113/*
3114 * one of our devices needs to be removed - find it
3115 * and remove it
3116 */
3117static void pktgen_rem_one_if(struct pktgen_thread *t)
3118{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003119 struct list_head *q, *n;
3120 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003121
Joe Perchesf9467ea2010-06-21 12:29:14 +00003122 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003123
3124 if_lock(t);
3125
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003126 list_for_each_safe(q, n, &t->if_list) {
3127 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003128
Luiz Capitulino222f1802006-03-20 22:16:13 -08003129 if (!cur->removal_mark)
3130 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003131
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003132 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003133 cur->skb = NULL;
3134
3135 pktgen_remove_device(t, cur);
3136
3137 break;
3138 }
3139
3140 if_unlock(t);
3141}
3142
Luiz Capitulino222f1802006-03-20 22:16:13 -08003143static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003145 struct list_head *q, *n;
3146 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003147
Joe Perchesf9467ea2010-06-21 12:29:14 +00003148 func_enter();
3149
Luiz Capitulino222f1802006-03-20 22:16:13 -08003150 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003151
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003154 list_for_each_safe(q, n, &t->if_list) {
3155 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003156
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003157 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003158 cur->skb = NULL;
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 pktgen_remove_device(t, cur);
3161 }
3162
Luiz Capitulino222f1802006-03-20 22:16:13 -08003163 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003168 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003169 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Stephen Hemmingeref879792009-09-22 19:41:43 +00003172static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003173{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003174 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003175 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003176 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003177}
3178
Stephen Hemmingeref879792009-09-22 19:41:43 +00003179static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3180{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003181 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003182
3183 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3184 if (signal_pending(current))
3185 break;
3186
3187 if (need_resched())
3188 pktgen_resched(pkt_dev);
3189 else
3190 cpu_relax();
3191 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003192 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003193}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003194
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003195static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Stephen Hemminger00829822008-11-20 20:14:53 -08003197 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003198 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003199 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003200 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003201 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 int ret;
3203
Stephen Hemmingeref879792009-09-22 19:41:43 +00003204 /* If device is offline, then don't send */
3205 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3206 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003207 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003209
Stephen Hemmingeref879792009-09-22 19:41:43 +00003210 /* This is max DELAY, this has special meaning of
3211 * "never transmit"
3212 */
3213 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003214 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003215 return;
3216 }
3217
3218 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003219 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3220 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3221 /* build a new pkt */
3222 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003223
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003224 pkt_dev->skb = fill_packet(odev, pkt_dev);
3225 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003226 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003227 schedule();
3228 pkt_dev->clone_count--; /* back out increment, OOM */
3229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003231 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003232 pkt_dev->allocated_skbs++;
3233 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003235
Stephen Hemmingeref879792009-09-22 19:41:43 +00003236 if (pkt_dev->delay && pkt_dev->last_ok)
3237 spin(pkt_dev, pkt_dev->next_tx);
3238
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003239 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3240 txq = netdev_get_tx_queue(odev, queue_map);
3241
3242 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003243
Tom Herbert734664982011-11-28 16:32:44 +00003244 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003245 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003246 pkt_dev->last_ok = 0;
3247 goto unlock;
3248 }
3249 atomic_inc(&(pkt_dev->skb->users));
3250 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Stephen Hemmingeref879792009-09-22 19:41:43 +00003252 switch (ret) {
3253 case NETDEV_TX_OK:
3254 txq_trans_update(txq);
3255 pkt_dev->last_ok = 1;
3256 pkt_dev->sofar++;
3257 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003258 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003259 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003260 case NET_XMIT_DROP:
3261 case NET_XMIT_CN:
3262 case NET_XMIT_POLICED:
3263 /* skb has been consumed */
3264 pkt_dev->errors++;
3265 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003266 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003267 net_info_ratelimited("%s xmit error: %d\n",
3268 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003269 pkt_dev->errors++;
3270 /* fallthru */
3271 case NETDEV_TX_LOCKED:
3272 case NETDEV_TX_BUSY:
3273 /* Retry it next time */
3274 atomic_dec(&(pkt_dev->skb->users));
3275 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003276 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003277unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003278 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /* If pkt_dev->count is zero, then run forever */
3281 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003282 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 /* Done with this */
3285 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003286 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003289/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 * Main loop of the thread goes here
3291 */
3292
David S. Milleree74baa2007-01-01 20:51:53 -08003293static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
3295 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003296 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003297 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
David S. Milleree74baa2007-01-01 20:51:53 -08003300 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
3302 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003303 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Joe Perchesf9467ea2010-06-21 12:29:14 +00003305 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
David S. Milleree74baa2007-01-01 20:51:53 -08003307 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003308
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003309 set_freezable();
3310
David S. Milleree74baa2007-01-01 20:51:53 -08003311 while (!kthread_should_stop()) {
3312 pkt_dev = next_to_run(t);
3313
Stephen Hemmingeref879792009-09-22 19:41:43 +00003314 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003315 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003316 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003317 wait_event_interruptible_timeout(t->queue,
3318 t->control != 0,
3319 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003320 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003321 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003322 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 __set_current_state(TASK_RUNNING);
3325
Stephen Hemmingeref879792009-09-22 19:41:43 +00003326 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 pktgen_xmit(pkt_dev);
3328
Stephen Hemmingeref879792009-09-22 19:41:43 +00003329 if (need_resched())
3330 pktgen_resched(pkt_dev);
3331 else
3332 cpu_relax();
3333 }
3334
Luiz Capitulino222f1802006-03-20 22:16:13 -08003335 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 pktgen_stop(t);
3337 t->control &= ~(T_STOP);
3338 }
3339
Luiz Capitulino222f1802006-03-20 22:16:13 -08003340 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 pktgen_run(t);
3342 t->control &= ~(T_RUN);
3343 }
3344
Luiz Capitulino222f1802006-03-20 22:16:13 -08003345 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003347 t->control &= ~(T_REMDEVALL);
3348 }
3349
Luiz Capitulino222f1802006-03-20 22:16:13 -08003350 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003351 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 t->control &= ~(T_REMDEV);
3353 }
3354
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003355 try_to_freeze();
3356
David S. Milleree74baa2007-01-01 20:51:53 -08003357 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Joe Perchesf9467ea2010-06-21 12:29:14 +00003360 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003361 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Joe Perchesf9467ea2010-06-21 12:29:14 +00003363 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003364 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Joe Perchesf9467ea2010-06-21 12:29:14 +00003366 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003367 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003368
Eric Dumazet551eaff2010-11-21 10:26:44 -08003369 /* Wait for kthread_stop */
3370 while (!kthread_should_stop()) {
3371 set_current_state(TASK_INTERRUPTIBLE);
3372 schedule();
3373 }
3374 __set_current_state(TASK_RUNNING);
3375
David S. Milleree74baa2007-01-01 20:51:53 -08003376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
Luiz Capitulino222f1802006-03-20 22:16:13 -08003379static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003380 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003382 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003383 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Eric Dumazet3e984842009-11-24 14:50:53 -08003385 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003386 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003387 if (strncmp(p->odevname, ifname, len) == 0) {
3388 if (p->odevname[len]) {
3389 if (exact || p->odevname[len] != '@')
3390 continue;
3391 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003392 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003393 break;
3394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Luiz Capitulino222f1802006-03-20 22:16:13 -08003396 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003397 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003398 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003401/*
3402 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 */
3404
Luiz Capitulino222f1802006-03-20 22:16:13 -08003405static int add_dev_to_thread(struct pktgen_thread *t,
3406 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Luiz Capitulino222f1802006-03-20 22:16:13 -08003410 if_lock(t);
3411
3412 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003413 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003414 rv = -EBUSY;
3415 goto out;
3416 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003417
3418 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003419 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 pkt_dev->running = 0;
3421
Luiz Capitulino222f1802006-03-20 22:16:13 -08003422out:
3423 if_unlock(t);
3424 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
3427/* Called under thread lock */
3428
Luiz Capitulino222f1802006-03-20 22:16:13 -08003429static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003431 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003432 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003433 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003434
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 /* We don't allow a device to be on several threads */
3436
Cong Wang4e58a022013-01-28 19:55:53 +00003437 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003438 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003439 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003440 return -EBUSY;
3441 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003442
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003443 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003444 if (!pkt_dev)
3445 return -ENOMEM;
3446
Eric Dumazet593f63b2009-11-23 01:44:37 +00003447 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003448 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003449 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003450 if (pkt_dev->flows == NULL) {
3451 kfree(pkt_dev);
3452 return -ENOMEM;
3453 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003454
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003455 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003456 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003457 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003458 pkt_dev->count = pg_count_d;
3459 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003460 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003461 pkt_dev->udp_src_max = 9;
3462 pkt_dev->udp_dst_min = 9;
3463 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003464 pkt_dev->vlan_p = 0;
3465 pkt_dev->vlan_cfi = 0;
3466 pkt_dev->vlan_id = 0xffff;
3467 pkt_dev->svlan_p = 0;
3468 pkt_dev->svlan_cfi = 0;
3469 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003470 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003471
Cong Wang4e58a022013-01-28 19:55:53 +00003472 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003473 if (err)
3474 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003475 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3476 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003477
Cong Wang4e58a022013-01-28 19:55:53 +00003478 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003479 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003480 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003481 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003482 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003483 err = -EINVAL;
3484 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003485 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003486#ifdef CONFIG_XFRM
3487 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3488 pkt_dev->ipsproto = IPPROTO_ESP;
3489#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003490
3491 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003492out2:
3493 dev_put(pkt_dev->odev);
3494out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003495#ifdef CONFIG_XFRM
3496 free_SAs(pkt_dev);
3497#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003498 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003499 kfree(pkt_dev);
3500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501}
3502
Cong Wang4e58a022013-01-28 19:55:53 +00003503static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003505 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003506 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003507 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003509 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3510 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003511 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003512 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003513 return -ENOMEM;
3514 }
3515
Luiz Capitulino222f1802006-03-20 22:16:13 -08003516 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003518
David S. Milleree74baa2007-01-01 20:51:53 -08003519 INIT_LIST_HEAD(&t->if_list);
3520
Cong Wang4e58a022013-01-28 19:55:53 +00003521 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003522 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003523
Eric Dumazet94dcf292011-03-22 16:30:45 -07003524 p = kthread_create_on_node(pktgen_thread_worker,
3525 t,
3526 cpu_to_node(cpu),
3527 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003528 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003529 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003530 list_del(&t->th_list);
3531 kfree(t);
3532 return PTR_ERR(p);
3533 }
3534 kthread_bind(p, cpu);
3535 t->tsk = p;
3536
Cong Wang4e58a022013-01-28 19:55:53 +00003537 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003538 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003539 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003540 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003541 PG_PROC_DIR, t->tsk->comm);
3542 kthread_stop(p);
3543 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003544 kfree(t);
3545 return -EINVAL;
3546 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003547
Cong Wang4e58a022013-01-28 19:55:53 +00003548 t->net = pn;
David S. Milleree74baa2007-01-01 20:51:53 -08003549 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003550 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552 return 0;
3553}
3554
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003555/*
3556 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003558static void _rem_dev_from_if_list(struct pktgen_thread *t,
3559 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003561 struct list_head *q, *n;
3562 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003564 list_for_each_safe(q, n, &t->if_list) {
3565 p = list_entry(q, struct pktgen_dev, list);
3566 if (p == pkt_dev)
3567 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
3569}
3570
Luiz Capitulino222f1802006-03-20 22:16:13 -08003571static int pktgen_remove_device(struct pktgen_thread *t,
3572 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003574 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Luiz Capitulino222f1802006-03-20 22:16:13 -08003576 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003577 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003578 pktgen_stop_device(pkt_dev);
3579 }
3580
3581 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 if (pkt_dev->odev) {
3584 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003585 pkt_dev->odev = NULL;
3586 }
3587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 /* And update the thread if_list */
3589
3590 _rem_dev_from_if_list(t, pkt_dev);
3591
Stephen Hemminger39df2322007-03-04 16:11:51 -08003592 if (pkt_dev->entry)
David Howellsa8ca16e2013-04-12 17:27:28 +01003593 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003595#ifdef CONFIG_XFRM
3596 free_SAs(pkt_dev);
3597#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003598 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003599 if (pkt_dev->page)
3600 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Cong Wang4e58a022013-01-28 19:55:53 +00003605static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
Cong Wang4e58a022013-01-28 19:55:53 +00003607 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003608 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003609 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003610
Cong Wang4e58a022013-01-28 19:55:53 +00003611 pn->net = net;
3612 INIT_LIST_HEAD(&pn->pktgen_threads);
3613 pn->pktgen_exiting = false;
3614 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3615 if (!pn->proc_dir) {
3616 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003617 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003618 }
Cong Wang4e58a022013-01-28 19:55:53 +00003619 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3620 if (pe == NULL) {
3621 pr_err("cannot create %s procfs entry\n", PGCTRL);
3622 ret = -EINVAL;
3623 goto remove;
3624 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003625
John Hawkes670c02c2005-10-13 09:30:31 -07003626 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003627 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Cong Wang4e58a022013-01-28 19:55:53 +00003629 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003630 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003631 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003632 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003633 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003634
Cong Wang4e58a022013-01-28 19:55:53 +00003635 if (list_empty(&pn->pktgen_threads)) {
3636 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003637 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003638 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003639 }
3640
Luiz Capitulino222f1802006-03-20 22:16:13 -08003641 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003642
Cong Wang4e58a022013-01-28 19:55:53 +00003643remove_entry:
3644 remove_proc_entry(PGCTRL, pn->proc_dir);
3645remove:
Gao fengece31ff2013-02-18 01:34:56 +00003646 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003647 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648}
3649
Cong Wang4e58a022013-01-28 19:55:53 +00003650static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
Cong Wang4e58a022013-01-28 19:55:53 +00003652 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003653 struct pktgen_thread *t;
3654 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003655 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Luiz Capitulino222f1802006-03-20 22:16:13 -08003657 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003658 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Eric Dumazetc57b5462012-05-09 13:29:51 +00003660 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003661 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003662 mutex_unlock(&pktgen_thread_lock);
3663
3664 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003665 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003666 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003667 kthread_stop(t->tsk);
3668 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Cong Wang4e58a022013-01-28 19:55:53 +00003671 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003672 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Cong Wang4e58a022013-01-28 19:55:53 +00003675static struct pernet_operations pg_net_ops = {
3676 .init = pg_net_init,
3677 .exit = pg_net_exit,
3678 .id = &pg_net_id,
3679 .size = sizeof(struct pktgen_net),
3680};
3681
3682static int __init pg_init(void)
3683{
3684 int ret = 0;
3685
3686 pr_info("%s", version);
3687 ret = register_pernet_subsys(&pg_net_ops);
3688 if (ret)
3689 return ret;
3690 ret = register_netdevice_notifier(&pktgen_notifier_block);
3691 if (ret)
3692 unregister_pernet_subsys(&pg_net_ops);
3693
3694 return ret;
3695}
3696
3697static void __exit pg_cleanup(void)
3698{
3699 unregister_netdevice_notifier(&pktgen_notifier_block);
3700 unregister_pernet_subsys(&pg_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703module_init(pg_init);
3704module_exit(pg_cleanup);
3705
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003706MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707MODULE_DESCRIPTION("Packet Generator tool");
3708MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003709MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003711MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003713MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003715MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003717MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");