blob: 11f2704c3810d54b3539daddea36d50ef230636f [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 Viro252e3342006-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 Viro252e3342006-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 Viro252e3342006-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 Viro252e3342006-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{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001924 struct net_device *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 Viro252e3342006-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 Viro252e3342006-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 Viro252e3342006-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 Viro252e3342006-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
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002711 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002712 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002713 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002714 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002715 skb->priority = pkt_dev->skb_priority;
2716
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002717 iph = ip_hdr(skb);
2718 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-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;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002749 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002750 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 skb->dev = odev;
2752 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002753 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002754
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002755#ifdef CONFIG_XFRM
2756 if (!process_ipsec(pkt_dev, skb, protocol))
2757 return NULL;
2758#endif
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return skb;
2761}
2762
Luiz Capitulino222f1802006-03-20 22:16:13 -08002763static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2764 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 struct sk_buff *skb = NULL;
2767 __u8 *eth;
2768 struct udphdr *udph;
2769 int datalen;
2770 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002771 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002772 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002773 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2774 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2775 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2776 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002777 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002778
2779 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002780 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002781
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002782 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002783 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002784
Robert Olsson64053be2005-06-26 15:27:10 -07002785 /* Update any of the values, used when we're incrementing various
2786 * fields.
2787 */
2788 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002789 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002790
Stephen Hemmingere4707572009-08-27 13:55:13 +00002791 skb = __netdev_alloc_skb(odev,
2792 pkt_dev->cur_pkt_size + 64
2793 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (!skb) {
2795 sprintf(pkt_dev->result, "No memory");
2796 return NULL;
2797 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002798 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 skb_reserve(skb, 16);
2801
2802 /* Reserve for ethernet and IP header */
2803 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002804 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2805 if (pkt_dev->nr_labels)
2806 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002807
2808 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002809 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002810 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002811 *svlan_tci = build_tci(pkt_dev->svlan_id,
2812 pkt_dev->svlan_cfi,
2813 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002814 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002815 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002816 }
2817 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002818 *vlan_tci = build_tci(pkt_dev->vlan_id,
2819 pkt_dev->vlan_cfi,
2820 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002821 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002822 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002823 }
2824
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002825 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002826 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002827 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002828 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002829 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002830 iph = ipv6_hdr(skb);
2831 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002834 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002835
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002836 /* Eth + IPh + UDPh + mpls */
2837 datalen = pkt_dev->cur_pkt_size - 14 -
2838 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002839 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002841 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002843 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
2845
2846 udph->source = htons(pkt_dev->cur_udp_src);
2847 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002848 udph->len = htons(datalen + sizeof(struct udphdr));
2849 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002851 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002853 if (pkt_dev->traffic_class) {
2854 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002855 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002856 }
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 iph->hop_limit = 32;
2859
2860 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2861 iph->nexthdr = IPPROTO_UDP;
2862
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002863 iph->daddr = pkt_dev->cur_in6_daddr;
2864 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002866 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002867 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002868 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 skb->dev = odev;
2870 skb->pkt_type = PACKET_HOST;
2871
Eric Dumazet26ad7872011-01-25 13:26:05 -08002872 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 return skb;
2875}
2876
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002877static struct sk_buff *fill_packet(struct net_device *odev,
2878 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002880 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return fill_packet_ipv6(odev, pkt_dev);
2882 else
2883 return fill_packet_ipv4(odev, pkt_dev);
2884}
2885
Luiz Capitulino222f1802006-03-20 22:16:13 -08002886static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002888 pkt_dev->seq_num = 1;
2889 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002891 pkt_dev->tx_bytes = 0;
2892 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
2894
2895/* Set up structure for sending pkts, clear counters */
2896
2897static void pktgen_run(struct pktgen_thread *t)
2898{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002899 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 int started = 0;
2901
Joe Perchesf9467ea2010-06-21 12:29:14 +00002902 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002905 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 /*
2908 * setup odev and create initial packet.
2909 */
2910 pktgen_setup_inject(pkt_dev);
2911
Luiz Capitulino222f1802006-03-20 22:16:13 -08002912 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002914 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00002916 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002917
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002918 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 strcpy(pkt_dev->result, "Starting");
2921 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002922 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 strcpy(pkt_dev->result, "Error starting");
2924 }
2925 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002926 if (started)
2927 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
Cong Wang4e58a022013-01-28 19:55:53 +00002930static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002932 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Joe Perchesf9467ea2010-06-21 12:29:14 +00002934 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002936 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002937
Cong Wang4e58a022013-01-28 19:55:53 +00002938 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002939 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002940
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002941 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
Stephen Hemminger648fda72009-08-27 13:55:07 +00002944static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002946 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002948 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00002949 if (pkt_dev->running)
2950 return 1;
2951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
Luiz Capitulino222f1802006-03-20 22:16:13 -08002954static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002956 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Luiz Capitulino222f1802006-03-20 22:16:13 -08002958 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Luiz Capitulino222f1802006-03-20 22:16:13 -08002960 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Luiz Capitulino222f1802006-03-20 22:16:13 -08002962 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Luiz Capitulino222f1802006-03-20 22:16:13 -08002964 if (signal_pending(current))
2965 goto signal;
2966 if_lock(t);
2967 }
2968 if_unlock(t);
2969 return 1;
2970signal:
2971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Cong Wang4e58a022013-01-28 19:55:53 +00002974static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002976 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002978
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002979 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002980
Cong Wang4e58a022013-01-28 19:55:53 +00002981 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002983 if (sig == 0)
2984 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002986
2987 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00002988 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002990
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002991 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return sig;
2993}
2994
Cong Wang4e58a022013-01-28 19:55:53 +00002995static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002997 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Joe Perchesf9467ea2010-06-21 12:29:14 +00002999 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003001 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Cong Wang4e58a022013-01-28 19:55:53 +00003003 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003005
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003006 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003008 /* Propagate thread->control */
3009 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003010
Cong Wang4e58a022013-01-28 19:55:53 +00003011 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
Cong Wang4e58a022013-01-28 19:55:53 +00003014static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003015{
3016 struct pktgen_thread *t;
3017
Joe Perchesf9467ea2010-06-21 12:29:14 +00003018 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003019
3020 mutex_lock(&pktgen_thread_lock);
3021
Cong Wang4e58a022013-01-28 19:55:53 +00003022 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003023 t->control |= (T_REMDEVALL);
3024
3025 mutex_unlock(&pktgen_thread_lock);
3026
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003027 /* Propagate thread->control */
3028 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003029
Cong Wang4e58a022013-01-28 19:55:53 +00003030 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003031}
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3034{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003035 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003036 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003037 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3038 pkt_dev->started_at);
3039 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Daniel Turull03a14ab2011-03-09 14:11:00 -08003041 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003042 (unsigned long long)ktime_to_us(elapsed),
3043 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3044 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003045 (unsigned long long)pkt_dev->sofar,
3046 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003048 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3049 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Luiz Capitulino222f1802006-03-20 22:16:13 -08003051 bps = pps * 8 * pkt_dev->cur_pkt_size;
3052
3053 mbps = bps;
3054 do_div(mbps, 1000000);
3055 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3056 (unsigned long long)pps,
3057 (unsigned long long)mbps,
3058 (unsigned long long)bps,
3059 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003063static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003065 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003066
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003068 pr_warning("interface: %s is already stopped\n",
3069 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003070 return -EINVAL;
3071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003073 kfree_skb(pkt_dev->skb);
3074 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003075 pkt_dev->stopped_at = ktime_get();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003076 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003078 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Luiz Capitulino222f1802006-03-20 22:16:13 -08003080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
Luiz Capitulino222f1802006-03-20 22:16:13 -08003083static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003085 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if_lock(t);
3088
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003089 list_for_each_entry(pkt_dev, &t->if_list, list) {
3090 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003091 continue;
3092 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003093 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003094 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003095 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 }
3097 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003098 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
Luiz Capitulino222f1802006-03-20 22:16:13 -08003101static void pktgen_stop(struct pktgen_thread *t)
3102{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003103 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Joe Perchesf9467ea2010-06-21 12:29:14 +00003105 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Luiz Capitulino222f1802006-03-20 22:16:13 -08003107 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003109 list_for_each_entry(pkt_dev, &t->if_list, list) {
3110 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Luiz Capitulino222f1802006-03-20 22:16:13 -08003113 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003116/*
3117 * one of our devices needs to be removed - find it
3118 * and remove it
3119 */
3120static void pktgen_rem_one_if(struct pktgen_thread *t)
3121{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003122 struct list_head *q, *n;
3123 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003124
Joe Perchesf9467ea2010-06-21 12:29:14 +00003125 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003126
3127 if_lock(t);
3128
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003129 list_for_each_safe(q, n, &t->if_list) {
3130 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003131
Luiz Capitulino222f1802006-03-20 22:16:13 -08003132 if (!cur->removal_mark)
3133 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003134
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003135 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003136 cur->skb = NULL;
3137
3138 pktgen_remove_device(t, cur);
3139
3140 break;
3141 }
3142
3143 if_unlock(t);
3144}
3145
Luiz Capitulino222f1802006-03-20 22:16:13 -08003146static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003148 struct list_head *q, *n;
3149 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003150
Joe Perchesf9467ea2010-06-21 12:29:14 +00003151 func_enter();
3152
Luiz Capitulino222f1802006-03-20 22:16:13 -08003153 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003154
Luiz Capitulino222f1802006-03-20 22:16:13 -08003155 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003157 list_for_each_safe(q, n, &t->if_list) {
3158 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003159
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003160 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003161 cur->skb = NULL;
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 pktgen_remove_device(t, cur);
3164 }
3165
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003172 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173}
3174
Stephen Hemmingeref879792009-09-22 19:41:43 +00003175static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003176{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003177 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003178 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003179 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003180}
3181
Stephen Hemmingeref879792009-09-22 19:41:43 +00003182static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3183{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003184 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003185
3186 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3187 if (signal_pending(current))
3188 break;
3189
3190 if (need_resched())
3191 pktgen_resched(pkt_dev);
3192 else
3193 cpu_relax();
3194 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003195 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003196}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003197
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003198static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Stephen Hemminger00829822008-11-20 20:14:53 -08003200 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003201 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003202 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003203 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003204 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 int ret;
3206
Stephen Hemmingeref879792009-09-22 19:41:43 +00003207 /* If device is offline, then don't send */
3208 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3209 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003210 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003212
Stephen Hemmingeref879792009-09-22 19:41:43 +00003213 /* This is max DELAY, this has special meaning of
3214 * "never transmit"
3215 */
3216 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003217 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003218 return;
3219 }
3220
3221 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003222 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3223 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3224 /* build a new pkt */
3225 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003226
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003227 pkt_dev->skb = fill_packet(odev, pkt_dev);
3228 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003229 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003230 schedule();
3231 pkt_dev->clone_count--; /* back out increment, OOM */
3232 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003234 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003235 pkt_dev->allocated_skbs++;
3236 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003238
Stephen Hemmingeref879792009-09-22 19:41:43 +00003239 if (pkt_dev->delay && pkt_dev->last_ok)
3240 spin(pkt_dev, pkt_dev->next_tx);
3241
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003242 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3243 txq = netdev_get_tx_queue(odev, queue_map);
3244
3245 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003246
Tom Herbert734664982011-11-28 16:32:44 +00003247 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003248 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003249 pkt_dev->last_ok = 0;
3250 goto unlock;
3251 }
3252 atomic_inc(&(pkt_dev->skb->users));
3253 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Stephen Hemmingeref879792009-09-22 19:41:43 +00003255 switch (ret) {
3256 case NETDEV_TX_OK:
3257 txq_trans_update(txq);
3258 pkt_dev->last_ok = 1;
3259 pkt_dev->sofar++;
3260 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003261 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003262 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003263 case NET_XMIT_DROP:
3264 case NET_XMIT_CN:
3265 case NET_XMIT_POLICED:
3266 /* skb has been consumed */
3267 pkt_dev->errors++;
3268 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003269 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003270 net_info_ratelimited("%s xmit error: %d\n",
3271 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003272 pkt_dev->errors++;
3273 /* fallthru */
3274 case NETDEV_TX_LOCKED:
3275 case NETDEV_TX_BUSY:
3276 /* Retry it next time */
3277 atomic_dec(&(pkt_dev->skb->users));
3278 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003279 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003280unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003281 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /* If pkt_dev->count is zero, then run forever */
3284 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003285 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 /* Done with this */
3288 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003289 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 * Main loop of the thread goes here
3294 */
3295
David S. Milleree74baa2007-01-01 20:51:53 -08003296static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
3298 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003299 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003300 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
David S. Milleree74baa2007-01-01 20:51:53 -08003303 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
3305 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003306 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Joe Perchesf9467ea2010-06-21 12:29:14 +00003308 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
David S. Milleree74baa2007-01-01 20:51:53 -08003310 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003311
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003312 set_freezable();
3313
David S. Milleree74baa2007-01-01 20:51:53 -08003314 while (!kthread_should_stop()) {
3315 pkt_dev = next_to_run(t);
3316
Stephen Hemmingeref879792009-09-22 19:41:43 +00003317 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003318 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003319 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003320 wait_event_interruptible_timeout(t->queue,
3321 t->control != 0,
3322 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003323 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003324 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003325 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 __set_current_state(TASK_RUNNING);
3328
Stephen Hemmingeref879792009-09-22 19:41:43 +00003329 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 pktgen_xmit(pkt_dev);
3331
Stephen Hemmingeref879792009-09-22 19:41:43 +00003332 if (need_resched())
3333 pktgen_resched(pkt_dev);
3334 else
3335 cpu_relax();
3336 }
3337
Luiz Capitulino222f1802006-03-20 22:16:13 -08003338 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 pktgen_stop(t);
3340 t->control &= ~(T_STOP);
3341 }
3342
Luiz Capitulino222f1802006-03-20 22:16:13 -08003343 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 pktgen_run(t);
3345 t->control &= ~(T_RUN);
3346 }
3347
Luiz Capitulino222f1802006-03-20 22:16:13 -08003348 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003350 t->control &= ~(T_REMDEVALL);
3351 }
3352
Luiz Capitulino222f1802006-03-20 22:16:13 -08003353 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003354 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 t->control &= ~(T_REMDEV);
3356 }
3357
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003358 try_to_freeze();
3359
David S. Milleree74baa2007-01-01 20:51:53 -08003360 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Joe Perchesf9467ea2010-06-21 12:29:14 +00003363 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003364 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Joe Perchesf9467ea2010-06-21 12:29:14 +00003366 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003367 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Joe Perchesf9467ea2010-06-21 12:29:14 +00003369 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003370 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003371
Eric Dumazet551eaff2010-11-21 10:26:44 -08003372 /* Wait for kthread_stop */
3373 while (!kthread_should_stop()) {
3374 set_current_state(TASK_INTERRUPTIBLE);
3375 schedule();
3376 }
3377 __set_current_state(TASK_RUNNING);
3378
David S. Milleree74baa2007-01-01 20:51:53 -08003379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
3381
Luiz Capitulino222f1802006-03-20 22:16:13 -08003382static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003383 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003385 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003386 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Eric Dumazet3e984842009-11-24 14:50:53 -08003388 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003389 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003390 if (strncmp(p->odevname, ifname, len) == 0) {
3391 if (p->odevname[len]) {
3392 if (exact || p->odevname[len] != '@')
3393 continue;
3394 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003395 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003396 break;
3397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003400 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003401 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003404/*
3405 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 */
3407
Luiz Capitulino222f1802006-03-20 22:16:13 -08003408static int add_dev_to_thread(struct pktgen_thread *t,
3409 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Luiz Capitulino222f1802006-03-20 22:16:13 -08003413 if_lock(t);
3414
3415 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003416 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003417 rv = -EBUSY;
3418 goto out;
3419 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003420
3421 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003422 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 pkt_dev->running = 0;
3424
Luiz Capitulino222f1802006-03-20 22:16:13 -08003425out:
3426 if_unlock(t);
3427 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428}
3429
3430/* Called under thread lock */
3431
Luiz Capitulino222f1802006-03-20 22:16:13 -08003432static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003434 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003435 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003436 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 /* We don't allow a device to be on several threads */
3439
Cong Wang4e58a022013-01-28 19:55:53 +00003440 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003441 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003442 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003443 return -EBUSY;
3444 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003445
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003446 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003447 if (!pkt_dev)
3448 return -ENOMEM;
3449
Eric Dumazet593f63b2009-11-23 01:44:37 +00003450 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003451 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003452 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003453 if (pkt_dev->flows == NULL) {
3454 kfree(pkt_dev);
3455 return -ENOMEM;
3456 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003457
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003458 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003459 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003460 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003461 pkt_dev->count = pg_count_d;
3462 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003463 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003464 pkt_dev->udp_src_max = 9;
3465 pkt_dev->udp_dst_min = 9;
3466 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003467 pkt_dev->vlan_p = 0;
3468 pkt_dev->vlan_cfi = 0;
3469 pkt_dev->vlan_id = 0xffff;
3470 pkt_dev->svlan_p = 0;
3471 pkt_dev->svlan_cfi = 0;
3472 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003473 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003474
Cong Wang4e58a022013-01-28 19:55:53 +00003475 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003476 if (err)
3477 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003478 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3479 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003480
Cong Wang4e58a022013-01-28 19:55:53 +00003481 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003482 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003483 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003484 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003485 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003486 err = -EINVAL;
3487 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003488 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003489#ifdef CONFIG_XFRM
3490 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3491 pkt_dev->ipsproto = IPPROTO_ESP;
3492#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003493
3494 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003495out2:
3496 dev_put(pkt_dev->odev);
3497out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003498#ifdef CONFIG_XFRM
3499 free_SAs(pkt_dev);
3500#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003501 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003502 kfree(pkt_dev);
3503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
3505
Cong Wang4e58a022013-01-28 19:55:53 +00003506static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003508 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003509 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003510 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003512 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3513 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003514 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003515 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003516 return -ENOMEM;
3517 }
3518
Luiz Capitulino222f1802006-03-20 22:16:13 -08003519 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003521
David S. Milleree74baa2007-01-01 20:51:53 -08003522 INIT_LIST_HEAD(&t->if_list);
3523
Cong Wang4e58a022013-01-28 19:55:53 +00003524 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003525 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003526
Eric Dumazet94dcf292011-03-22 16:30:45 -07003527 p = kthread_create_on_node(pktgen_thread_worker,
3528 t,
3529 cpu_to_node(cpu),
3530 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003531 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003532 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003533 list_del(&t->th_list);
3534 kfree(t);
3535 return PTR_ERR(p);
3536 }
3537 kthread_bind(p, cpu);
3538 t->tsk = p;
3539
Cong Wang4e58a022013-01-28 19:55:53 +00003540 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003541 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003542 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003543 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003544 PG_PROC_DIR, t->tsk->comm);
3545 kthread_stop(p);
3546 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003547 kfree(t);
3548 return -EINVAL;
3549 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003550
Cong Wang4e58a022013-01-28 19:55:53 +00003551 t->net = pn;
David S. Milleree74baa2007-01-01 20:51:53 -08003552 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003553 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
3555 return 0;
3556}
3557
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003558/*
3559 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003561static void _rem_dev_from_if_list(struct pktgen_thread *t,
3562 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003564 struct list_head *q, *n;
3565 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003567 list_for_each_safe(q, n, &t->if_list) {
3568 p = list_entry(q, struct pktgen_dev, list);
3569 if (p == pkt_dev)
3570 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 }
3572}
3573
Luiz Capitulino222f1802006-03-20 22:16:13 -08003574static int pktgen_remove_device(struct pktgen_thread *t,
3575 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003577 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Luiz Capitulino222f1802006-03-20 22:16:13 -08003579 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003580 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003581 pktgen_stop_device(pkt_dev);
3582 }
3583
3584 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
3586 if (pkt_dev->odev) {
3587 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 pkt_dev->odev = NULL;
3589 }
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 /* And update the thread if_list */
3592
3593 _rem_dev_from_if_list(t, pkt_dev);
3594
Stephen Hemminger39df2322007-03-04 16:11:51 -08003595 if (pkt_dev->entry)
David Howellsa8ca16e2013-04-12 17:27:28 +01003596 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003598#ifdef CONFIG_XFRM
3599 free_SAs(pkt_dev);
3600#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003601 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003602 if (pkt_dev->page)
3603 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
3607
Cong Wang4e58a022013-01-28 19:55:53 +00003608static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Cong Wang4e58a022013-01-28 19:55:53 +00003610 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003611 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003612 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003613
Cong Wang4e58a022013-01-28 19:55:53 +00003614 pn->net = net;
3615 INIT_LIST_HEAD(&pn->pktgen_threads);
3616 pn->pktgen_exiting = false;
3617 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3618 if (!pn->proc_dir) {
3619 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003620 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003621 }
Cong Wang4e58a022013-01-28 19:55:53 +00003622 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3623 if (pe == NULL) {
3624 pr_err("cannot create %s procfs entry\n", PGCTRL);
3625 ret = -EINVAL;
3626 goto remove;
3627 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003628
John Hawkes670c02c2005-10-13 09:30:31 -07003629 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003630 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Cong Wang4e58a022013-01-28 19:55:53 +00003632 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003633 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003634 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003635 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003636 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003637
Cong Wang4e58a022013-01-28 19:55:53 +00003638 if (list_empty(&pn->pktgen_threads)) {
3639 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003640 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003641 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003642 }
3643
Luiz Capitulino222f1802006-03-20 22:16:13 -08003644 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003645
Cong Wang4e58a022013-01-28 19:55:53 +00003646remove_entry:
3647 remove_proc_entry(PGCTRL, pn->proc_dir);
3648remove:
Gao fengece31ff2013-02-18 01:34:56 +00003649 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
3652
Cong Wang4e58a022013-01-28 19:55:53 +00003653static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
Cong Wang4e58a022013-01-28 19:55:53 +00003655 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003656 struct pktgen_thread *t;
3657 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003658 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Luiz Capitulino222f1802006-03-20 22:16:13 -08003660 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003661 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Eric Dumazetc57b5462012-05-09 13:29:51 +00003663 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003664 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003665 mutex_unlock(&pktgen_thread_lock);
3666
3667 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003668 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003669 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003670 kthread_stop(t->tsk);
3671 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Cong Wang4e58a022013-01-28 19:55:53 +00003674 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003675 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003676}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Cong Wang4e58a022013-01-28 19:55:53 +00003678static struct pernet_operations pg_net_ops = {
3679 .init = pg_net_init,
3680 .exit = pg_net_exit,
3681 .id = &pg_net_id,
3682 .size = sizeof(struct pktgen_net),
3683};
3684
3685static int __init pg_init(void)
3686{
3687 int ret = 0;
3688
3689 pr_info("%s", version);
3690 ret = register_pernet_subsys(&pg_net_ops);
3691 if (ret)
3692 return ret;
3693 ret = register_netdevice_notifier(&pktgen_notifier_block);
3694 if (ret)
3695 unregister_pernet_subsys(&pg_net_ops);
3696
3697 return ret;
3698}
3699
3700static void __exit pg_cleanup(void)
3701{
3702 unregister_netdevice_notifier(&pktgen_notifier_block);
3703 unregister_pernet_subsys(&pg_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704}
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706module_init(pg_init);
3707module_exit(pg_cleanup);
3708
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003709MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710MODULE_DESCRIPTION("Packet Generator tool");
3711MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003712MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003714MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003716MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003718MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003720MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");