blob: e71766c35a193e80539fe01cdf0aef13e8b78458 [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 Hemmingerd50a6b52005-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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#include <asm/byteorder.h>
168#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700169#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000170#include <linux/io.h>
171#include <linux/timex.h>
172#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800174#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Daniel Turull43d28b62010-06-09 22:49:57 +0000176#define VERSION "2.74"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800178#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800179#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Joe Perchesf9467ea2010-06-21 12:29:14 +0000181#define func_enter() pr_debug("entering %s\n", __func__);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800184#define F_IPSRC_RND (1<<0) /* IP-Src Random */
185#define F_IPDST_RND (1<<1) /* IP-Dst Random */
186#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
187#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
188#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
189#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
190#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
191#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800192#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700193#define F_VID_RND (1<<9) /* Random VLAN ID */
194#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700195#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700196#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700197#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700198#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000199#define F_NODE (1<<15) /* Node memory alloc*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000202#define T_STOP (1<<0) /* Stop run */
203#define T_RUN (1<<1) /* Start run */
204#define T_REMDEVALL (1<<2) /* Remove all devs */
205#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/* If lock -- can be removed after some work */
208#define if_lock(t) spin_lock(&(t->if_lock));
209#define if_unlock(t) spin_unlock(&(t->if_lock));
210
211/* Used to help with determining the pkts on receive */
212#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700213#define PG_PROC_DIR "pktgen"
214#define PGCTRL "pgctrl"
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000215static struct proc_dir_entry *pg_proc_dir;
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
Luiz Capitulino222f1802006-03-20 22:16:13 -0800251 int min_pkt_size; /* = ETH_ZLEN; */
252 int max_pkt_size; /* = ETH_ZLEN; */
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 */
323 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
324 __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;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800376 unsigned cflows; /* Concurrent flows (config) */
377 unsigned lflow; /* Flow length (config) */
378 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700379 unsigned 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
Eric Dumazet551eaff2010-11-21 10:26:44 -0800400static bool pktgen_exiting __read_mostly;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000403 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800404 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800405 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800406 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800407 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000409 /* Field for thread to receive "posted" events terminate,
410 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800411
412 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int cpu;
414
Luiz Capitulino222f1802006-03-20 22:16:13 -0800415 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700416 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417};
418
419#define REMOVE 1
420#define FIND 0
421
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000422static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000423 "Packet Generator for packet performance testing. "
424 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Luiz Capitulino222f1802006-03-20 22:16:13 -0800426static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
427static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
428static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800429 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
431static void pktgen_run_all_threads(void);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800432static void pktgen_reset_all_threads(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static void pktgen_stop_all_threads_ifs(void);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000434
Luiz Capitulino222f1802006-03-20 22:16:13 -0800435static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800437
Luiz Capitulino222f1802006-03-20 22:16:13 -0800438static unsigned int scan_ip6(const char *s, char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000441static int pg_count_d __read_mostly = 1000;
442static int pg_delay_d __read_mostly;
443static int pg_clone_skb_d __read_mostly;
444static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Luiz Capitulino222fa072006-03-20 22:24:27 -0800446static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800447static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static struct notifier_block pktgen_notifier_block = {
450 .notifier_call = pktgen_device_event,
451};
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900454 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *
456 */
457
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700458static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800459{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000460 seq_puts(seq, version);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000464static ssize_t pgctrl_write(struct file *file, const char __user *buf,
465 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int err = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700468 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Luiz Capitulino222f1802006-03-20 22:16:13 -0800470 if (!capable(CAP_NET_ADMIN)) {
471 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700475 if (count > sizeof(data))
476 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700479 err = -EFAULT;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700480 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800481 }
482 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Luiz Capitulino222f1802006-03-20 22:16:13 -0800484 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 pktgen_stop_all_threads_ifs();
486
Luiz Capitulino222f1802006-03-20 22:16:13 -0800487 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 pktgen_run_all_threads();
489
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800490 else if (!strcmp(data, "reset"))
491 pktgen_reset_all_threads();
492
Luiz Capitulino222f1802006-03-20 22:16:13 -0800493 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000494 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 err = count;
497
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498out:
499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700502static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700504 return single_open(file, pgctrl_show, PDE(inode)->data);
505}
506
Arjan van de Ven9a321442007-02-12 00:55:35 -0800507static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800508 .owner = THIS_MODULE,
509 .open = pgctrl_open,
510 .read = seq_read,
511 .llseek = seq_lseek,
512 .write = pgctrl_write,
513 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700514};
515
516static int pktgen_if_show(struct seq_file *seq, void *v)
517{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000518 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000519 ktime_t stopped;
520 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Luiz Capitulino222f1802006-03-20 22:16:13 -0800522 seq_printf(seq,
523 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
524 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
525 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Luiz Capitulino222f1802006-03-20 22:16:13 -0800527 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000528 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
529 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000530 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Luiz Capitulino222f1802006-03-20 22:16:13 -0800532 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
533 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Robert Olsson45b270f2007-08-28 15:45:55 -0700535 seq_printf(seq,
536 " queue_map_min: %u queue_map_max: %u\n",
537 pkt_dev->queue_map_min,
538 pkt_dev->queue_map_max);
539
John Fastabend9e50e3a2010-11-16 19:12:28 +0000540 if (pkt_dev->skb_priority)
541 seq_printf(seq, " skb_priority: %u\n",
542 pkt_dev->skb_priority);
543
Luiz Capitulino222f1802006-03-20 22:16:13 -0800544 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800545 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000546 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
547 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
548 &pkt_dev->in6_saddr,
549 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
550 &pkt_dev->in6_daddr,
551 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000552 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800553 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000554 " dst_min: %s dst_max: %s\n",
555 pkt_dev->dst_min, pkt_dev->dst_max);
556 seq_printf(seq,
Jesper Dangaard Brouer4bbf9f42015-05-21 12:16:11 +0200557 " src_min: %s src_max: %s\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000558 pkt_dev->src_min, pkt_dev->src_max);
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700561 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Johannes Berge1749612008-10-27 15:59:26 -0700563 seq_printf(seq, "%pM ",
564 is_zero_ether_addr(pkt_dev->src_mac) ?
565 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Luiz Capitulino222f1802006-03-20 22:16:13 -0800567 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700568 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Luiz Capitulino222f1802006-03-20 22:16:13 -0800570 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000571 " udp_src_min: %d udp_src_max: %d"
572 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800573 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
574 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Luiz Capitulino222f1802006-03-20 22:16:13 -0800576 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800577 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700578 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800580 if (pkt_dev->nr_labels) {
581 unsigned i;
582 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700583 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800584 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
585 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
586 }
587
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000588 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700589 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000590 pkt_dev->vlan_id, pkt_dev->vlan_p,
591 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700592
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000593 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700594 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000595 pkt_dev->svlan_id, pkt_dev->svlan_p,
596 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700597
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000598 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700599 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700600
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000601 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700602 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700603
Robert Olssone99b99b2010-03-18 22:44:30 +0000604 if (pkt_dev->node >= 0)
605 seq_printf(seq, " node: %d\n", pkt_dev->node);
606
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800607 seq_printf(seq, " Flags: ");
608
Luiz Capitulino222f1802006-03-20 22:16:13 -0800609 if (pkt_dev->flags & F_IPV6)
610 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Luiz Capitulino222f1802006-03-20 22:16:13 -0800612 if (pkt_dev->flags & F_IPSRC_RND)
613 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Luiz Capitulino222f1802006-03-20 22:16:13 -0800615 if (pkt_dev->flags & F_IPDST_RND)
616 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Luiz Capitulino222f1802006-03-20 22:16:13 -0800618 if (pkt_dev->flags & F_TXSIZE_RND)
619 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Luiz Capitulino222f1802006-03-20 22:16:13 -0800621 if (pkt_dev->flags & F_UDPSRC_RND)
622 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Luiz Capitulino222f1802006-03-20 22:16:13 -0800624 if (pkt_dev->flags & F_UDPDST_RND)
625 seq_printf(seq, "UDPDST_RND ");
626
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800627 if (pkt_dev->flags & F_MPLS_RND)
628 seq_printf(seq, "MPLS_RND ");
629
Robert Olsson45b270f2007-08-28 15:45:55 -0700630 if (pkt_dev->flags & F_QUEUE_MAP_RND)
631 seq_printf(seq, "QUEUE_MAP_RND ");
632
Robert Olssone6fce5b2008-08-07 02:23:01 -0700633 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
634 seq_printf(seq, "QUEUE_MAP_CPU ");
635
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700636 if (pkt_dev->cflows) {
637 if (pkt_dev->flags & F_FLOW_SEQ)
638 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
639 else
640 seq_printf(seq, "FLOW_RND ");
641 }
642
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700643#ifdef CONFIG_XFRM
644 if (pkt_dev->flags & F_IPSEC_ON)
645 seq_printf(seq, "IPSEC ");
646#endif
647
Luiz Capitulino222f1802006-03-20 22:16:13 -0800648 if (pkt_dev->flags & F_MACSRC_RND)
649 seq_printf(seq, "MACSRC_RND ");
650
651 if (pkt_dev->flags & F_MACDST_RND)
652 seq_printf(seq, "MACDST_RND ");
653
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700654 if (pkt_dev->flags & F_VID_RND)
655 seq_printf(seq, "VID_RND ");
656
657 if (pkt_dev->flags & F_SVID_RND)
658 seq_printf(seq, "SVID_RND ");
659
Robert Olssone99b99b2010-03-18 22:44:30 +0000660 if (pkt_dev->flags & F_NODE)
661 seq_printf(seq, "NODE_ALLOC ");
662
Luiz Capitulino222f1802006-03-20 22:16:13 -0800663 seq_puts(seq, "\n");
664
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000665 /* not really stopped, more like last-running-at */
Daniel Borkmannc5f469a2012-10-28 08:27:19 +0000666 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000667 idle = pkt_dev->idle_acc;
668 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800669
670 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000671 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800672 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000673 (unsigned long long)pkt_dev->errors);
674
675 seq_printf(seq,
676 " started: %lluus stopped: %lluus idle: %lluus\n",
677 (unsigned long long) ktime_to_us(pkt_dev->started_at),
678 (unsigned long long) ktime_to_us(stopped),
679 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800680
681 seq_printf(seq,
682 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700683 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
684 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Luiz Capitulino222f1802006-03-20 22:16:13 -0800686 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000687 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
688 &pkt_dev->cur_in6_saddr,
689 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800690 } else
691 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700692 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Luiz Capitulino222f1802006-03-20 22:16:13 -0800694 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700695 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Robert Olsson45b270f2007-08-28 15:45:55 -0700697 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
698
Luiz Capitulino222f1802006-03-20 22:16:13 -0800699 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800702 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800704 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800709
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000710static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
711 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800712{
713 int i = 0;
714 *num = 0;
715
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700716 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000717 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800718 char c;
719 *num <<= 4;
720 if (get_user(c, &user_buffer[i]))
721 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000722 value = hex_to_bin(c);
723 if (value >= 0)
724 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800725 else
726 break;
727 }
728 return i;
729}
730
Luiz Capitulino222f1802006-03-20 22:16:13 -0800731static int count_trail_chars(const char __user * user_buffer,
732 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 int i;
735
736 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800737 char c;
738 if (get_user(c, &user_buffer[i]))
739 return -EFAULT;
740 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case '\"':
742 case '\n':
743 case '\r':
744 case '\t':
745 case ' ':
746 case '=':
747 break;
748 default:
749 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752done:
753 return i;
754}
755
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000756static long num_arg(const char __user *user_buffer, unsigned long maxlen,
757 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000759 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800761
Paul Gortmakerd6182222010-10-18 12:14:44 +0000762 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800763 char c;
764 if (get_user(c, &user_buffer[i]))
765 return -EFAULT;
766 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800768 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 } else
770 break;
771 }
772 return i;
773}
774
Luiz Capitulino222f1802006-03-20 22:16:13 -0800775static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000777 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Paul Gortmakerd6182222010-10-18 12:14:44 +0000779 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800780 char c;
781 if (get_user(c, &user_buffer[i]))
782 return -EFAULT;
783 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 case '\"':
785 case '\n':
786 case '\r':
787 case '\t':
788 case ' ':
789 goto done_str;
790 break;
791 default:
792 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return i;
797}
798
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800799static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
800{
801 unsigned n = 0;
802 char c;
803 ssize_t i = 0;
804 int len;
805
806 pkt_dev->nr_labels = 0;
807 do {
808 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700809 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800810 if (len <= 0)
811 return len;
812 pkt_dev->labels[n] = htonl(tmp);
813 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
814 pkt_dev->flags |= F_MPLS_RND;
815 i += len;
816 if (get_user(c, &buffer[i]))
817 return -EFAULT;
818 i++;
819 n++;
820 if (n >= MAX_MPLS_LABELS)
821 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700822 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800823
824 pkt_dev->nr_labels = n;
825 return i;
826}
827
Luiz Capitulino222f1802006-03-20 22:16:13 -0800828static ssize_t pktgen_if_write(struct file *file,
829 const char __user * user_buffer, size_t count,
830 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Joe Perches8a994a72010-07-12 10:50:23 +0000832 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800833 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000834 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 char name[16], valstr[32];
836 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800837 char *pg_result = NULL;
838 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800840
841 pg_result = &(pkt_dev->result[0]);
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000844 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -EINVAL;
846 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800847
Paul Gortmakerd6182222010-10-18 12:14:44 +0000848 max = count;
849 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800850 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000851 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800852 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000854 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* Read variable name */
857
858 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000859 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800860 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return -EFAULT;
865 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800866
867 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800869 if (len < 0)
870 return len;
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 i += len;
873
874 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000875 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700876 char tb[copy + 1];
877 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700879 tb[copy] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700880 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800881 (unsigned long)count, tb);
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (!strcmp(name, "min_pkt_size")) {
885 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000886 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800887 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800890 if (value < 14 + 20 + 8)
891 value = 14 + 20 + 8;
892 if (value != pkt_dev->min_pkt_size) {
893 pkt_dev->min_pkt_size = value;
894 pkt_dev->cur_pkt_size = value;
895 }
896 sprintf(pg_result, "OK: min_pkt_size=%u",
897 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return count;
899 }
900
Luiz Capitulino222f1802006-03-20 22:16:13 -0800901 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000903 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907 if (value < 14 + 20 + 8)
908 value = 14 + 20 + 8;
909 if (value != pkt_dev->max_pkt_size) {
910 pkt_dev->max_pkt_size = value;
911 pkt_dev->cur_pkt_size = value;
912 }
913 sprintf(pg_result, "OK: max_pkt_size=%u",
914 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return count;
916 }
917
Luiz Capitulino222f1802006-03-20 22:16:13 -0800918 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (!strcmp(name, "pkt_size")) {
921 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000922 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800923 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800926 if (value < 14 + 20 + 8)
927 value = 14 + 20 + 8;
928 if (value != pkt_dev->min_pkt_size) {
929 pkt_dev->min_pkt_size = value;
930 pkt_dev->max_pkt_size = value;
931 pkt_dev->cur_pkt_size = value;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
934 return count;
935 }
936
Luiz Capitulino222f1802006-03-20 22:16:13 -0800937 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000939 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800940 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800943 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 sprintf(pg_result, "OK: debug=%u", debug);
945 return count;
946 }
947
Luiz Capitulino222f1802006-03-20 22:16:13 -0800948 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000950 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800951 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 i += len;
954 pkt_dev->nfrags = value;
955 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
956 return count;
957 }
958 if (!strcmp(name, "delay")) {
959 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000960 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800961 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000964 if (value == 0x7FFFFFFF)
965 pkt_dev->delay = ULLONG_MAX;
966 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000967 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000968
969 sprintf(pg_result, "OK: delay=%llu",
970 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return count;
972 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000973 if (!strcmp(name, "rate")) {
974 len = num_arg(&user_buffer[i], 10, &value);
975 if (len < 0)
976 return len;
977
978 i += len;
979 if (!value)
980 return len;
981 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
982 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000983 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000984
985 sprintf(pg_result, "OK: rate=%lu", value);
986 return count;
987 }
988 if (!strcmp(name, "ratep")) {
989 len = num_arg(&user_buffer[i], 10, &value);
990 if (len < 0)
991 return len;
992
993 i += len;
994 if (!value)
995 return len;
996 pkt_dev->delay = NSEC_PER_SEC/value;
997 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000998 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000999
1000 sprintf(pg_result, "OK: rate=%lu", value);
1001 return count;
1002 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001003 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001005 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001006 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001009 if (value != pkt_dev->udp_src_min) {
1010 pkt_dev->udp_src_min = value;
1011 pkt_dev->cur_udp_src = value;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1014 return count;
1015 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001016 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001018 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001019 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001022 if (value != pkt_dev->udp_dst_min) {
1023 pkt_dev->udp_dst_min = value;
1024 pkt_dev->cur_udp_dst = value;
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1027 return count;
1028 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001031 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001032 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001035 if (value != pkt_dev->udp_src_max) {
1036 pkt_dev->udp_src_max = value;
1037 pkt_dev->cur_udp_src = value;
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1040 return count;
1041 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001042 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001044 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001048 if (value != pkt_dev->udp_dst_max) {
1049 pkt_dev->udp_dst_max = value;
1050 pkt_dev->cur_udp_dst = value;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1053 return count;
1054 }
1055 if (!strcmp(name, "clone_skb")) {
1056 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001057 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001058 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001059 if ((value > 0) &&
1060 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1061 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001063 pkt_dev->clone_skb = value;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1066 return count;
1067 }
1068 if (!strcmp(name, "count")) {
1069 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001070 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001071 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 i += len;
1074 pkt_dev->count = value;
1075 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001076 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return count;
1078 }
1079 if (!strcmp(name, "src_mac_count")) {
1080 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001081 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001082 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 i += len;
1085 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001086 pkt_dev->src_mac_count = value;
1087 pkt_dev->cur_src_mac_offset = 0;
1088 }
1089 sprintf(pg_result, "OK: src_mac_count=%d",
1090 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return count;
1092 }
1093 if (!strcmp(name, "dst_mac_count")) {
1094 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001095 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001096 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 i += len;
1099 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001100 pkt_dev->dst_mac_count = value;
1101 pkt_dev->cur_dst_mac_offset = 0;
1102 }
1103 sprintf(pg_result, "OK: dst_mac_count=%d",
1104 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return count;
1106 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001107 if (!strcmp(name, "node")) {
1108 len = num_arg(&user_buffer[i], 10, &value);
1109 if (len < 0)
1110 return len;
1111
1112 i += len;
1113
1114 if (node_possible(value)) {
1115 pkt_dev->node = value;
1116 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001117 if (pkt_dev->page) {
1118 put_page(pkt_dev->page);
1119 pkt_dev->page = NULL;
1120 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001121 }
1122 else
1123 sprintf(pg_result, "ERROR: node not possible");
1124 return count;
1125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001127 char f[32];
1128 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001130 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001131 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (copy_from_user(f, &user_buffer[i], len))
1134 return -EFAULT;
1135 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001136 if (strcmp(f, "IPSRC_RND") == 0)
1137 pkt_dev->flags |= F_IPSRC_RND;
1138
1139 else if (strcmp(f, "!IPSRC_RND") == 0)
1140 pkt_dev->flags &= ~F_IPSRC_RND;
1141
1142 else if (strcmp(f, "TXSIZE_RND") == 0)
1143 pkt_dev->flags |= F_TXSIZE_RND;
1144
1145 else if (strcmp(f, "!TXSIZE_RND") == 0)
1146 pkt_dev->flags &= ~F_TXSIZE_RND;
1147
1148 else if (strcmp(f, "IPDST_RND") == 0)
1149 pkt_dev->flags |= F_IPDST_RND;
1150
1151 else if (strcmp(f, "!IPDST_RND") == 0)
1152 pkt_dev->flags &= ~F_IPDST_RND;
1153
1154 else if (strcmp(f, "UDPSRC_RND") == 0)
1155 pkt_dev->flags |= F_UDPSRC_RND;
1156
1157 else if (strcmp(f, "!UDPSRC_RND") == 0)
1158 pkt_dev->flags &= ~F_UDPSRC_RND;
1159
1160 else if (strcmp(f, "UDPDST_RND") == 0)
1161 pkt_dev->flags |= F_UDPDST_RND;
1162
1163 else if (strcmp(f, "!UDPDST_RND") == 0)
1164 pkt_dev->flags &= ~F_UDPDST_RND;
1165
1166 else if (strcmp(f, "MACSRC_RND") == 0)
1167 pkt_dev->flags |= F_MACSRC_RND;
1168
1169 else if (strcmp(f, "!MACSRC_RND") == 0)
1170 pkt_dev->flags &= ~F_MACSRC_RND;
1171
1172 else if (strcmp(f, "MACDST_RND") == 0)
1173 pkt_dev->flags |= F_MACDST_RND;
1174
1175 else if (strcmp(f, "!MACDST_RND") == 0)
1176 pkt_dev->flags &= ~F_MACDST_RND;
1177
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001178 else if (strcmp(f, "MPLS_RND") == 0)
1179 pkt_dev->flags |= F_MPLS_RND;
1180
1181 else if (strcmp(f, "!MPLS_RND") == 0)
1182 pkt_dev->flags &= ~F_MPLS_RND;
1183
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001184 else if (strcmp(f, "VID_RND") == 0)
1185 pkt_dev->flags |= F_VID_RND;
1186
1187 else if (strcmp(f, "!VID_RND") == 0)
1188 pkt_dev->flags &= ~F_VID_RND;
1189
1190 else if (strcmp(f, "SVID_RND") == 0)
1191 pkt_dev->flags |= F_SVID_RND;
1192
1193 else if (strcmp(f, "!SVID_RND") == 0)
1194 pkt_dev->flags &= ~F_SVID_RND;
1195
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001196 else if (strcmp(f, "FLOW_SEQ") == 0)
1197 pkt_dev->flags |= F_FLOW_SEQ;
1198
Robert Olsson45b270f2007-08-28 15:45:55 -07001199 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1200 pkt_dev->flags |= F_QUEUE_MAP_RND;
1201
1202 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1203 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001204
1205 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1206 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1207
1208 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1209 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001210#ifdef CONFIG_XFRM
1211 else if (strcmp(f, "IPSEC") == 0)
1212 pkt_dev->flags |= F_IPSEC_ON;
1213#endif
1214
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001215 else if (strcmp(f, "!IPV6") == 0)
1216 pkt_dev->flags &= ~F_IPV6;
1217
Robert Olssone99b99b2010-03-18 22:44:30 +00001218 else if (strcmp(f, "NODE_ALLOC") == 0)
1219 pkt_dev->flags |= F_NODE;
1220
1221 else if (strcmp(f, "!NODE_ALLOC") == 0)
1222 pkt_dev->flags &= ~F_NODE;
1223
Luiz Capitulino222f1802006-03-20 22:16:13 -08001224 else {
1225 sprintf(pg_result,
1226 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1227 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001228 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001229 "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 -08001230 return count;
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1233 return count;
1234 }
1235 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1236 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001237 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001238 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Luiz Capitulino222f1802006-03-20 22:16:13 -08001240 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001242 buf[len] = 0;
1243 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1244 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1245 strncpy(pkt_dev->dst_min, buf, len);
1246 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1247 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1248 }
1249 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001250 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001251 pkt_dev->dst_min);
1252 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1254 return count;
1255 }
1256 if (!strcmp(name, "dst_max")) {
1257 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001258 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -EFAULT;
1264
Luiz Capitulino222f1802006-03-20 22:16:13 -08001265 buf[len] = 0;
1266 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1267 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1268 strncpy(pkt_dev->dst_max, buf, len);
1269 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1270 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1271 }
1272 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001273 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001274 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 i += len;
1276 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1277 return count;
1278 }
1279 if (!strcmp(name, "dst6")) {
1280 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001281 if (len < 0)
1282 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 pkt_dev->flags |= F_IPV6;
1285
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001288 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001291 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001293 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Luiz Capitulino222f1802006-03-20 22:16:13 -08001295 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001296 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Luiz Capitulino222f1802006-03-20 22:16:13 -08001298 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 sprintf(pg_result, "OK: dst6=%s", buf);
1300 return count;
1301 }
1302 if (!strcmp(name, "dst6_min")) {
1303 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001304 if (len < 0)
1305 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 pkt_dev->flags |= F_IPV6;
1308
Luiz Capitulino222f1802006-03-20 22:16:13 -08001309 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001311 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001314 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001316 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001318 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 sprintf(pg_result, "OK: dst6_min=%s", buf);
1322 return count;
1323 }
1324 if (!strcmp(name, "dst6_max")) {
1325 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001326 if (len < 0)
1327 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 pkt_dev->flags |= F_IPV6;
1330
Luiz Capitulino222f1802006-03-20 22:16:13 -08001331 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001333 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001336 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001339 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Luiz Capitulino222f1802006-03-20 22:16:13 -08001341 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 sprintf(pg_result, "OK: dst6_max=%s", buf);
1343 return count;
1344 }
1345 if (!strcmp(name, "src6")) {
1346 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001347 if (len < 0)
1348 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 pkt_dev->flags |= F_IPV6;
1351
Luiz Capitulino222f1802006-03-20 22:16:13 -08001352 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001354 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001357 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001359 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Luiz Capitulino222f1802006-03-20 22:16:13 -08001361 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001362 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001363
1364 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 sprintf(pg_result, "OK: src6=%s", buf);
1366 return count;
1367 }
1368 if (!strcmp(name, "src_min")) {
1369 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001370 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001371 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001372
Luiz Capitulino222f1802006-03-20 22:16:13 -08001373 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001375 buf[len] = 0;
1376 if (strcmp(buf, pkt_dev->src_min) != 0) {
1377 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1378 strncpy(pkt_dev->src_min, buf, len);
1379 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1380 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1381 }
1382 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001383 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001384 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 i += len;
1386 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1387 return count;
1388 }
1389 if (!strcmp(name, "src_max")) {
1390 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001391 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001392 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001393
Luiz Capitulino222f1802006-03-20 22:16:13 -08001394 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001396 buf[len] = 0;
1397 if (strcmp(buf, pkt_dev->src_max) != 0) {
1398 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1399 strncpy(pkt_dev->src_max, buf, len);
1400 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1401 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1402 }
1403 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001404 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001405 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 i += len;
1407 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1408 return count;
1409 }
1410 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001412 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001413 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001416 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001419 if (!mac_pton(valstr, pkt_dev->dst_mac))
1420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 /* Set up Dest MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001422 memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001423
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001424 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return count;
1426 }
1427 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001429 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001430 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001433 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001436 if (!mac_pton(valstr, pkt_dev->src_mac))
1437 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001438 /* Set up Src MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001439 memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001440
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001441 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return count;
1443 }
1444
Luiz Capitulino222f1802006-03-20 22:16:13 -08001445 if (!strcmp(name, "clear_counters")) {
1446 pktgen_clear_counters(pkt_dev);
1447 sprintf(pg_result, "OK: Clearing counters.\n");
1448 return count;
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (!strcmp(name, "flows")) {
1452 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001453 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001454 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 i += len;
1457 if (value > MAX_CFLOWS)
1458 value = MAX_CFLOWS;
1459
1460 pkt_dev->cflows = value;
1461 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1462 return count;
1463 }
1464
1465 if (!strcmp(name, "flowlen")) {
1466 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001467 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001468 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 i += len;
1471 pkt_dev->lflow = value;
1472 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1473 return count;
1474 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001475
Robert Olsson45b270f2007-08-28 15:45:55 -07001476 if (!strcmp(name, "queue_map_min")) {
1477 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001478 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001479 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001480
Robert Olsson45b270f2007-08-28 15:45:55 -07001481 i += len;
1482 pkt_dev->queue_map_min = value;
1483 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1484 return count;
1485 }
1486
1487 if (!strcmp(name, "queue_map_max")) {
1488 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001489 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001490 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001491
Robert Olsson45b270f2007-08-28 15:45:55 -07001492 i += len;
1493 pkt_dev->queue_map_max = value;
1494 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1495 return count;
1496 }
1497
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001498 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001499 unsigned n, cnt;
1500
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001501 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001502 if (len < 0)
1503 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001504 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001505 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001506 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001507 cnt += sprintf(pg_result + cnt,
1508 "%08x%s", ntohl(pkt_dev->labels[n]),
1509 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001510
1511 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1512 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1513 pkt_dev->svlan_id = 0xffff;
1514
1515 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001516 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001517 }
1518 return count;
1519 }
1520
1521 if (!strcmp(name, "vlan_id")) {
1522 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001523 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001524 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001525
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001526 i += len;
1527 if (value <= 4095) {
1528 pkt_dev->vlan_id = value; /* turn on VLAN */
1529
1530 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001531 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001532
1533 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001534 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001535
1536 pkt_dev->nr_labels = 0; /* turn off MPLS */
1537 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1538 } else {
1539 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1540 pkt_dev->svlan_id = 0xffff;
1541
1542 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001543 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001544 }
1545 return count;
1546 }
1547
1548 if (!strcmp(name, "vlan_p")) {
1549 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001550 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001551 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001552
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001553 i += len;
1554 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1555 pkt_dev->vlan_p = value;
1556 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1557 } else {
1558 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1559 }
1560 return count;
1561 }
1562
1563 if (!strcmp(name, "vlan_cfi")) {
1564 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001565 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001566 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001567
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001568 i += len;
1569 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1570 pkt_dev->vlan_cfi = value;
1571 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1572 } else {
1573 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1574 }
1575 return count;
1576 }
1577
1578 if (!strcmp(name, "svlan_id")) {
1579 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001580 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001581 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001582
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001583 i += len;
1584 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1585 pkt_dev->svlan_id = value; /* turn on SVLAN */
1586
1587 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001588 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001589
1590 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001591 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001592
1593 pkt_dev->nr_labels = 0; /* turn off MPLS */
1594 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1595 } else {
1596 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1597 pkt_dev->svlan_id = 0xffff;
1598
1599 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001600 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001601 }
1602 return count;
1603 }
1604
1605 if (!strcmp(name, "svlan_p")) {
1606 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001607 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001608 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001609
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001610 i += len;
1611 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1612 pkt_dev->svlan_p = value;
1613 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1614 } else {
1615 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1616 }
1617 return count;
1618 }
1619
1620 if (!strcmp(name, "svlan_cfi")) {
1621 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001622 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001623 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001624
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001625 i += len;
1626 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1627 pkt_dev->svlan_cfi = value;
1628 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1629 } else {
1630 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1631 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001632 return count;
1633 }
1634
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001635 if (!strcmp(name, "tos")) {
1636 __u32 tmp_value = 0;
1637 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001638 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001639 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001640
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001641 i += len;
1642 if (len == 2) {
1643 pkt_dev->tos = tmp_value;
1644 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1645 } else {
1646 sprintf(pg_result, "ERROR: tos must be 00-ff");
1647 }
1648 return count;
1649 }
1650
1651 if (!strcmp(name, "traffic_class")) {
1652 __u32 tmp_value = 0;
1653 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001654 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001655 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001656
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001657 i += len;
1658 if (len == 2) {
1659 pkt_dev->traffic_class = tmp_value;
1660 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1661 } else {
1662 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1663 }
1664 return count;
1665 }
1666
John Fastabend9e50e3a2010-11-16 19:12:28 +00001667 if (!strcmp(name, "skb_priority")) {
1668 len = num_arg(&user_buffer[i], 9, &value);
1669 if (len < 0)
1670 return len;
1671
1672 i += len;
1673 pkt_dev->skb_priority = value;
1674 sprintf(pg_result, "OK: skb_priority=%i",
1675 pkt_dev->skb_priority);
1676 return count;
1677 }
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1680 return -EINVAL;
1681}
1682
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001683static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001685 return single_open(file, pktgen_if_show, PDE(inode)->data);
1686}
1687
Arjan van de Ven9a321442007-02-12 00:55:35 -08001688static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001689 .owner = THIS_MODULE,
1690 .open = pktgen_if_open,
1691 .read = seq_read,
1692 .llseek = seq_lseek,
1693 .write = pktgen_if_write,
1694 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001695};
1696
1697static int pktgen_thread_show(struct seq_file *seq, void *v)
1698{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001699 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001700 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001702 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Luiz Capitulino222f1802006-03-20 22:16:13 -08001704 seq_printf(seq, "Running: ");
1705
1706 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001707 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001708 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001709 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Luiz Capitulino222f1802006-03-20 22:16:13 -08001711 seq_printf(seq, "\nStopped: ");
1712
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001713 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001714 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001715 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 if (t->result[0])
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001718 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 else
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001720 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Luiz Capitulino222f1802006-03-20 22:16:13 -08001722 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001727static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001728 const char __user * user_buffer,
1729 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Joe Perches8a994a72010-07-12 10:50:23 +00001731 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001732 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001733 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 char *pg_result;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001738 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -EINVAL;
1740 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001741
Paul Gortmakerd6182222010-10-18 12:14:44 +00001742 max = count;
1743 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001744 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001745 return len;
1746
Paul Gortmakerd6182222010-10-18 12:14:44 +00001747 i = len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /* Read variable name */
1750
1751 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001752 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001753 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 memset(name, 0, sizeof(name));
1756 if (copy_from_user(name, &user_buffer[i], len))
1757 return -EFAULT;
1758 i += len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001759
Luiz Capitulino222f1802006-03-20 22:16:13 -08001760 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001763 return len;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 i += len;
1766
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001767 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001768 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1769 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Luiz Capitulino222f1802006-03-20 22:16:13 -08001771 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001772 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 ret = -EINVAL;
1774 goto out;
1775 }
1776
1777 pg_result = &(t->result[0]);
1778
Luiz Capitulino222f1802006-03-20 22:16:13 -08001779 if (!strcmp(name, "add_device")) {
1780 char f[32];
1781 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783 if (len < 0) {
1784 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto out;
1786 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001787 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return -EFAULT;
1789 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001790 mutex_lock(&pktgen_thread_lock);
Cong Wanga86923f2013-01-27 21:14:08 +00001791 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001792 mutex_unlock(&pktgen_thread_lock);
Cong Wanga86923f2013-01-27 21:14:08 +00001793 if (!ret) {
1794 ret = count;
1795 sprintf(pg_result, "OK: add_device=%s", f);
1796 } else
1797 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto out;
1799 }
1800
Luiz Capitulino222f1802006-03-20 22:16:13 -08001801 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001802 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001803 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001804 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001807 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto out;
1809 }
1810
Luiz Capitulino222f1802006-03-20 22:16:13 -08001811 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001812 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto out;
1815 }
1816
1817 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001818out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return ret;
1820}
1821
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001822static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001824 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Arjan van de Ven9a321442007-02-12 00:55:35 -08001827static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001828 .owner = THIS_MODULE,
1829 .open = pktgen_thread_open,
1830 .read = seq_read,
1831 .llseek = seq_lseek,
1832 .write = pktgen_thread_write,
1833 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001834};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001837static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 struct pktgen_thread *t;
1840 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001841 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001843 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001844 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846 if (remove) {
1847 if_lock(t);
1848 pkt_dev->removal_mark = 1;
1849 t->control |= T_REMDEV;
1850 if_unlock(t);
1851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 break;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001855 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001858/*
1859 * mark a device for removal
1860 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001861static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001864 const int max_tries = 10, msec_per_try = 125;
1865 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001866
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001867 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001868 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001869
Luiz Capitulino222f1802006-03-20 22:16:13 -08001870 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001871
1872 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001873 if (pkt_dev == NULL)
1874 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001875
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001876 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001877 pr_debug("%s: waiting for %s to disappear....\n",
1878 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001879 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001880 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001881
1882 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001883 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1884 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001885 break;
1886 }
1887
1888 }
1889
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001890 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001891}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001892
Stephen Hemminger39df2322007-03-04 16:11:51 -08001893static void pktgen_change_name(struct net_device *dev)
1894{
1895 struct pktgen_thread *t;
1896
1897 list_for_each_entry(t, &pktgen_threads, th_list) {
1898 struct pktgen_dev *pkt_dev;
1899
1900 list_for_each_entry(pkt_dev, &t->if_list, list) {
1901 if (pkt_dev->odev != dev)
1902 continue;
1903
1904 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1905
Alexey Dobriyan29753152009-08-28 23:34:43 -07001906 pkt_dev->entry = proc_create_data(dev->name, 0600,
1907 pg_proc_dir,
1908 &pktgen_if_fops,
1909 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001910 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001911 pr_err("can't move proc entry for '%s'\n",
1912 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001913 break;
1914 }
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Luiz Capitulino222f1802006-03-20 22:16:13 -08001918static int pktgen_device_event(struct notifier_block *unused,
1919 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001921 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Eric Dumazetc57b5462012-05-09 13:29:51 +00001923 if (!net_eq(dev_net(dev), &init_net) || pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001924 return NOTIFY_DONE;
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /* It is OK that we do not hold the group lock right now,
1927 * as we run under the RTNL lock.
1928 */
1929
1930 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001931 case NETDEV_CHANGENAME:
1932 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001936 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 return NOTIFY_DONE;
1941}
1942
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001943static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1944 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001945{
1946 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001947 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001948
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001949 for (i = 0; ifname[i] != '@'; i++) {
1950 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001951 break;
1952
1953 b[i] = ifname[i];
1954 }
1955 b[i] = 0;
1956
1957 return dev_get_by_name(&init_net, b);
1958}
1959
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961/* Associate pktgen_dev with a device. */
1962
Stephen Hemminger39df2322007-03-04 16:11:51 -08001963static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001964{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001966 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (pkt_dev->odev) {
1970 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001971 pkt_dev->odev = NULL;
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Robert Olssone6fce5b2008-08-07 02:23:01 -07001974 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001976 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001977 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001981 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001982 err = -EINVAL;
1983 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001984 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001985 err = -ENETDOWN;
1986 } else {
1987 pkt_dev->odev = odev;
1988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995/* Read pkt_dev from the interface and set up internal pktgen_dev
1996 * structure to have the right information to create/send packets
1997 */
1998static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1999{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002000 int ntxq;
2001
Luiz Capitulino222f1802006-03-20 22:16:13 -08002002 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002003 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002004 sprintf(pkt_dev->result,
2005 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2006 return;
2007 }
2008
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002009 /* make sure that we don't pick a non-existing transmit queue */
2010 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002011
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002012 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002013 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2014 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2015 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002016 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002017 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002018 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002019 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2020 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2021 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002022 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002023 }
2024
Luiz Capitulino222f1802006-03-20 22:16:13 -08002025 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002027 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002028 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Luiz Capitulino222f1802006-03-20 22:16:13 -08002030 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002031 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Luiz Capitulino222f1802006-03-20 22:16:13 -08002033 /* Set up pkt size */
2034 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2035
2036 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002038 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 * gets exported
2040 */
2041
2042#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002043 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 struct inet6_dev *idev;
2045
Luiz Capitulino222f1802006-03-20 22:16:13 -08002046 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2047 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 set = 1;
2049 break;
2050 }
2051
Luiz Capitulino222f1802006-03-20 22:16:13 -08002052 if (!set) {
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /*
2055 * Use linklevel address if unconfigured.
2056 *
2057 * use ipv6_get_lladdr if/when it's get exported
2058 */
2059
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002060 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002061 idev = __in6_dev_get(pkt_dev->odev);
2062 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct inet6_ifaddr *ifp;
2064
2065 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002066 for (ifp = idev->addr_list; ifp;
2067 ifp = ifp->if_next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002068 if (ifp->scope == IFA_LINK &&
2069 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002070 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 err = 0;
2072 break;
2073 }
2074 }
2075 read_unlock_bh(&idev->lock);
2076 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002077 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002078 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002079 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
2081#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002082 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 pkt_dev->saddr_min = 0;
2084 pkt_dev->saddr_max = 0;
2085 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002086
2087 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002090 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (in_dev) {
2092 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002093 pkt_dev->saddr_min =
2094 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 pkt_dev->saddr_max = pkt_dev->saddr_min;
2096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002099 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2101 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2102 }
2103
2104 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2105 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2106 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002107 /* Initialize current values. */
2108 pkt_dev->cur_dst_mac_offset = 0;
2109 pkt_dev->cur_src_mac_offset = 0;
2110 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2111 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2112 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2113 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 pkt_dev->nflows = 0;
2115}
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002118static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2119{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002120 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002121 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002122 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002123
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002124 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2125 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002126
Daniel Turull43d28b62010-06-09 22:49:57 +00002127 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002128 if (remaining <= 0) {
2129 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002130 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002131 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002132
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00002133 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002134 if (remaining < 100000) {
2135 /* for small delays (<100us), just loop until limit is reached */
2136 do {
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00002137 end_time = ktime_get();
2138 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002139 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002140 /* see do_nanosleep */
2141 hrtimer_init_sleeper(&t, current);
2142 do {
2143 set_current_state(TASK_INTERRUPTIBLE);
2144 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2145 if (!hrtimer_active(&t.timer))
2146 t.task = NULL;
2147
2148 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002151 hrtimer_cancel(&t.timer);
2152 } while (t.task && pkt_dev->running && !signal_pending(current));
2153 __set_current_state(TASK_RUNNING);
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00002154 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002156
2157 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002158 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002161static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2162{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002163 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002164 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2165 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2166 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2167}
2168
Stephen Hemminger648fda72009-08-27 13:55:07 +00002169static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002170{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002171 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002172}
2173
2174static inline int f_pick(struct pktgen_dev *pkt_dev)
2175{
2176 int flow = pkt_dev->curfl;
2177
2178 if (pkt_dev->flags & F_FLOW_SEQ) {
2179 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2180 /* reset time */
2181 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002182 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002183 pkt_dev->curfl += 1;
2184 if (pkt_dev->curfl >= pkt_dev->cflows)
2185 pkt_dev->curfl = 0; /*reset */
2186 }
2187 } else {
2188 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002189 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002190
Robert Olsson1211a642008-08-05 18:44:26 -07002191 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002192 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002193 pkt_dev->flows[flow].flags = 0;
2194 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002195 }
2196
2197 return pkt_dev->curfl;
2198}
2199
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002200
2201#ifdef CONFIG_XFRM
2202/* If there was already an IPSEC SA, we keep it as is, else
2203 * we go look for it ...
2204*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002205#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002206static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002207{
2208 struct xfrm_state *x = pkt_dev->flows[flow].x;
2209 if (!x) {
2210 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002211 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002212 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002213 (xfrm_address_t *)&pkt_dev->cur_saddr,
2214 AF_INET,
2215 pkt_dev->ipsmode,
2216 pkt_dev->ipsproto, 0);
2217 if (x) {
2218 pkt_dev->flows[flow].x = x;
2219 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002220 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002221 }
2222
2223 }
2224}
2225#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002226static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2227{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002228
2229 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2230 pkt_dev->cur_queue_map = smp_processor_id();
2231
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002232 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002233 __u16 t;
2234 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2235 t = random32() %
2236 (pkt_dev->queue_map_max -
2237 pkt_dev->queue_map_min + 1)
2238 + pkt_dev->queue_map_min;
2239 } else {
2240 t = pkt_dev->cur_queue_map + 1;
2241 if (t > pkt_dev->queue_map_max)
2242 t = pkt_dev->queue_map_min;
2243 }
2244 pkt_dev->cur_queue_map = t;
2245 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002246 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 -07002247}
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249/* Increment/randomize headers according to flags and current values
2250 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2251 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002252static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2253{
2254 __u32 imn;
2255 __u32 imx;
2256 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002258 if (pkt_dev->cflows)
2259 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002262 if (pkt_dev->src_mac_count > 1) {
2263 __u32 mc;
2264 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Luiz Capitulino222f1802006-03-20 22:16:13 -08002266 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002267 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002268 else {
2269 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002270 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002271 pkt_dev->src_mac_count)
2272 pkt_dev->cur_src_mac_offset = 0;
2273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Luiz Capitulino222f1802006-03-20 22:16:13 -08002275 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2276 pkt_dev->hh[11] = tmp;
2277 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2278 pkt_dev->hh[10] = tmp;
2279 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2280 pkt_dev->hh[9] = tmp;
2281 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2282 pkt_dev->hh[8] = tmp;
2283 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2284 pkt_dev->hh[7] = tmp;
2285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Luiz Capitulino222f1802006-03-20 22:16:13 -08002287 /* Deal with Destination MAC */
2288 if (pkt_dev->dst_mac_count > 1) {
2289 __u32 mc;
2290 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Luiz Capitulino222f1802006-03-20 22:16:13 -08002292 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002293 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Luiz Capitulino222f1802006-03-20 22:16:13 -08002295 else {
2296 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002297 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002298 pkt_dev->dst_mac_count) {
2299 pkt_dev->cur_dst_mac_offset = 0;
2300 }
2301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Luiz Capitulino222f1802006-03-20 22:16:13 -08002303 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2304 pkt_dev->hh[5] = tmp;
2305 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2306 pkt_dev->hh[4] = tmp;
2307 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2308 pkt_dev->hh[3] = tmp;
2309 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2310 pkt_dev->hh[2] = tmp;
2311 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2312 pkt_dev->hh[1] = tmp;
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002315 if (pkt_dev->flags & F_MPLS_RND) {
2316 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002317 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002318 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2319 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002320 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002321 htonl(0x000fffff));
2322 }
2323
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002324 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002325 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002326 }
2327
2328 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002329 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002330 }
2331
Luiz Capitulino222f1802006-03-20 22:16:13 -08002332 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2333 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002334 pkt_dev->cur_udp_src = random32() %
2335 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2336 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Luiz Capitulino222f1802006-03-20 22:16:13 -08002338 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 pkt_dev->cur_udp_src++;
2340 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2341 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002342 }
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Luiz Capitulino222f1802006-03-20 22:16:13 -08002345 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2346 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002347 pkt_dev->cur_udp_dst = random32() %
2348 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2349 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002350 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002352 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002354 }
2355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 if (!(pkt_dev->flags & F_IPV6)) {
2358
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002359 imn = ntohl(pkt_dev->saddr_min);
2360 imx = ntohl(pkt_dev->saddr_max);
2361 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002363 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002364 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 else {
2366 t = ntohl(pkt_dev->cur_saddr);
2367 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002368 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
2372 pkt_dev->cur_saddr = htonl(t);
2373 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002374
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002375 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2377 } else {
Al Viro252e3342006-11-14 20:48:11 -08002378 imn = ntohl(pkt_dev->daddr_min);
2379 imx = ntohl(pkt_dev->daddr_max);
2380 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002382 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (pkt_dev->flags & F_IPDST_RND) {
2384
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002385 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002386 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Joe Perches21cf2252007-12-16 13:44:00 -08002388 while (ipv4_is_loopback(s) ||
2389 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002390 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002391 ipv4_is_zeronet(s) ||
2392 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002393 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002394 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
Al Viro252e3342006-11-14 20:48:11 -08002396 pkt_dev->cur_daddr = s;
2397 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 t = ntohl(pkt_dev->cur_daddr);
2399 t++;
2400 if (t > imx) {
2401 t = imn;
2402 }
2403 pkt_dev->cur_daddr = htonl(t);
2404 }
2405 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002406 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002407 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002408 pkt_dev->flows[flow].cur_daddr =
2409 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002410#ifdef CONFIG_XFRM
2411 if (pkt_dev->flags & F_IPSEC_ON)
2412 get_ipsec_sa(pkt_dev, flow);
2413#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 pkt_dev->nflows++;
2415 }
2416 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002417 } else { /* IPV6 * */
2418
2419 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2420 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2421 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2422 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 else {
2424 int i;
2425
2426 /* Only random destinations yet */
2427
Luiz Capitulino222f1802006-03-20 22:16:13 -08002428 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002430 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002431 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2432 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
2436
Luiz Capitulino222f1802006-03-20 22:16:13 -08002437 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2438 __u32 t;
2439 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002440 t = random32() %
2441 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2442 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002443 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002445 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002447 }
2448 pkt_dev->cur_pkt_size = t;
2449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002451 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 pkt_dev->flows[flow].count++;
2454}
2455
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002456
2457#ifdef CONFIG_XFRM
2458static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2459{
2460 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2461 int err = 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002462
2463 if (!x)
2464 return 0;
2465 /* XXX: we dont support tunnel mode for now until
2466 * we resolve the dst issue */
2467 if (x->props.mode != XFRM_MODE_TRANSPORT)
2468 return 0;
2469
2470 spin_lock(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002471
Herbert Xu13996372007-10-17 21:35:51 -07002472 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002473 if (err)
2474 goto error;
2475 err = x->type->output(x, skb);
2476 if (err)
2477 goto error;
2478
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002479 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002480 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002481error:
2482 spin_unlock(&x->lock);
2483 return err;
2484}
2485
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002486static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002487{
2488 if (pkt_dev->cflows) {
2489 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002490 int i;
2491 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002492 struct xfrm_state *x = pkt_dev->flows[i].x;
2493 if (x) {
2494 xfrm_state_put(x);
2495 pkt_dev->flows[i].x = NULL;
2496 }
2497 }
2498 }
2499}
2500
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002501static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002502 struct sk_buff *skb, __be16 protocol)
2503{
2504 if (pkt_dev->flags & F_IPSEC_ON) {
2505 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2506 int nhead = 0;
2507 if (x) {
2508 int ret;
2509 __u8 *eth;
fan.duc5bbae62013-12-01 16:28:48 +08002510 struct iphdr *iph;
2511
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002512 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002513 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002514 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2515 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002516 pr_err("Error expanding ipsec packet %d\n",
2517 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002518 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002519 }
2520 }
2521
2522 /* ipsec is not expecting ll header */
2523 skb_pull(skb, ETH_HLEN);
2524 ret = pktgen_output_ipsec(skb, pkt_dev);
2525 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002526 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002527 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002528 }
2529 /* restore ll */
2530 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2531 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002532 *(u16 *) &eth[12] = protocol;
fan.duc5bbae62013-12-01 16:28:48 +08002533
2534 /* Update IPv4 header len as well as checksum value */
2535 iph = ip_hdr(skb);
2536 iph->tot_len = htons(skb->len - ETH_HLEN);
2537 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002538 }
2539 }
2540 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002541err:
2542 kfree_skb(skb);
2543 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002544}
2545#endif
2546
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002547static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2548{
2549 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002550 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002551 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002552
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002553 mpls--;
2554 *mpls |= MPLS_STACK_BOTTOM;
2555}
2556
Al Viro0f37c602006-11-03 03:49:56 -08002557static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2558 unsigned int prio)
2559{
2560 return htons(id | (cfi << 12) | (prio << 13));
2561}
2562
Eric Dumazet26ad7872011-01-25 13:26:05 -08002563static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2564 int datalen)
2565{
2566 struct timeval timestamp;
2567 struct pktgen_hdr *pgh;
2568
2569 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2570 datalen -= sizeof(*pgh);
2571
2572 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002573 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002574 } else {
2575 int frags = pkt_dev->nfrags;
2576 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002577 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002578
2579
2580 if (frags > MAX_SKB_FRAGS)
2581 frags = MAX_SKB_FRAGS;
2582 len = datalen - frags * PAGE_SIZE;
2583 if (len > 0) {
2584 memset(skb_put(skb, len), 0, len);
2585 datalen = frags * PAGE_SIZE;
2586 }
2587
2588 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002589 frag_len = (datalen/frags) < PAGE_SIZE ?
2590 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002591 while (datalen > 0) {
2592 if (unlikely(!pkt_dev->page)) {
2593 int node = numa_node_id();
2594
2595 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2596 node = pkt_dev->node;
2597 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2598 if (!pkt_dev->page)
2599 break;
2600 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002601 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002602 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002603 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002604 /*last fragment, fill rest of data*/
2605 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002606 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2607 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002608 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002609 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2610 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2611 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2612 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002613 i++;
2614 skb_shinfo(skb)->nr_frags = i;
2615 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002616 }
2617
2618 /* Stamp the time, and sequence number,
2619 * convert them to network byte order
2620 */
2621 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2622 pgh->seq_num = htonl(pkt_dev->seq_num);
2623
2624 do_gettimeofday(&timestamp);
2625 pgh->tv_sec = htonl(timestamp.tv_sec);
2626 pgh->tv_usec = htonl(timestamp.tv_usec);
2627}
2628
Luiz Capitulino222f1802006-03-20 22:16:13 -08002629static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2630 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
2632 struct sk_buff *skb = NULL;
2633 __u8 *eth;
2634 struct udphdr *udph;
2635 int datalen, iplen;
2636 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002637 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002638 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002639 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2640 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2641 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2642 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002643 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002644
2645 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002646 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002647
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002648 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002649 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002650
Robert Olsson64053be2005-06-26 15:27:10 -07002651 /* Update any of the values, used when we're incrementing various
2652 * fields.
2653 */
2654 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002655 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002656
David S. Miller7ac54592006-01-18 14:19:10 -08002657 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002658
2659 if (pkt_dev->flags & F_NODE) {
2660 int node;
2661
2662 if (pkt_dev->node >= 0)
2663 node = pkt_dev->node;
2664 else
2665 node = numa_node_id();
2666
2667 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2668 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2669 if (likely(skb)) {
2670 skb_reserve(skb, NET_SKB_PAD);
2671 skb->dev = odev;
2672 }
2673 }
2674 else
2675 skb = __netdev_alloc_skb(odev,
2676 pkt_dev->cur_pkt_size + 64
2677 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (!skb) {
2680 sprintf(pkt_dev->result, "No memory");
2681 return NULL;
2682 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002683 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
David S. Miller7ac54592006-01-18 14:19:10 -08002685 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 /* Reserve for ethernet and IP header */
2688 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002689 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2690 if (pkt_dev->nr_labels)
2691 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002692
2693 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002694 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002695 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002696 *svlan_tci = build_tci(pkt_dev->svlan_id,
2697 pkt_dev->svlan_cfi,
2698 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002699 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002700 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002701 }
2702 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002703 *vlan_tci = build_tci(pkt_dev->vlan_id,
2704 pkt_dev->vlan_cfi,
2705 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002706 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002707 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002708 }
2709
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002710 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002711 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002712 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002713 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002714 skb->priority = pkt_dev->skb_priority;
2715
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002716 iph = ip_hdr(skb);
2717 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002720 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002722 /* Eth + IPh + UDPh + mpls */
2723 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002724 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002725 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 udph->source = htons(pkt_dev->cur_udp_src);
2729 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002730 udph->len = htons(datalen + 8); /* DATA + udphdr */
2731 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 iph->ihl = 5;
2734 iph->version = 4;
2735 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002736 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002737 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 iph->saddr = pkt_dev->cur_saddr;
2739 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002740 iph->id = htons(pkt_dev->ip_id);
2741 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 iph->frag_off = 0;
2743 iplen = 20 + 8 + datalen;
2744 iph->tot_len = htons(iplen);
2745 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002746 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002747 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002748 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002749 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 skb->dev = odev;
2751 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002752 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002753
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002754#ifdef CONFIG_XFRM
2755 if (!process_ipsec(pkt_dev, skb, protocol))
2756 return NULL;
2757#endif
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return skb;
2760}
2761
2762/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002763 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2765 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002766 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 * Should be candidate for net/ipv4/utils.c
2768 * --ro
2769 */
2770
Luiz Capitulino222f1802006-03-20 22:16:13 -08002771static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002774 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 unsigned long u;
2776 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002777 unsigned int prefixlen = 0;
2778 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002779 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002780 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Luiz Capitulino222f1802006-03-20 22:16:13 -08002782 for (i = 0; i < 16; i++)
2783 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 for (;;) {
2786 if (*s == ':') {
2787 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002788 if (s[1] == ':') { /* Found "::", skip to part 2 */
2789 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 len++;
2791 break;
2792 }
2793 s++;
2794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002796 u = simple_strtoul(s, &pos, 16);
2797 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002798 if (!i)
2799 return 0;
2800 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 /* the last 4 bytes may be written as IPv4 address */
2803
2804 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002805 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2806 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
2808 ip[prefixlen++] = (u >> 8);
2809 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002810 s += i;
2811 len += i;
2812 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return len;
2814 }
2815
2816/* part 2, after "::" */
2817 for (;;) {
2818 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002819 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 break;
2821 s++;
2822 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002823 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002825
2826 u = simple_strtol(s, &pos, 16);
2827 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002829 if (*s)
2830 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 break;
2832 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002833 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002835 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2836 sizeof(tmp));
2837 suffixlen += 4;
2838 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 break;
2840 }
2841 suffix[suffixlen++] = (u >> 8);
2842 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002843 s += i;
2844 len += i;
2845 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 break;
2847 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002848 for (i = 0; i < suffixlen; i++)
2849 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return len;
2851}
2852
Luiz Capitulino222f1802006-03-20 22:16:13 -08002853static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2854 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 struct sk_buff *skb = NULL;
2857 __u8 *eth;
2858 struct udphdr *udph;
2859 int datalen;
2860 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002861 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002862 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002863 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2864 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2865 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2866 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002867 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002868
2869 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002870 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002871
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002872 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002873 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002874
Robert Olsson64053be2005-06-26 15:27:10 -07002875 /* Update any of the values, used when we're incrementing various
2876 * fields.
2877 */
2878 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002879 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002880
Stephen Hemmingere4707572009-08-27 13:55:13 +00002881 skb = __netdev_alloc_skb(odev,
2882 pkt_dev->cur_pkt_size + 64
2883 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (!skb) {
2885 sprintf(pkt_dev->result, "No memory");
2886 return NULL;
2887 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002888 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 skb_reserve(skb, 16);
2891
2892 /* Reserve for ethernet and IP header */
2893 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002894 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2895 if (pkt_dev->nr_labels)
2896 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002897
2898 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002899 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002900 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002901 *svlan_tci = build_tci(pkt_dev->svlan_id,
2902 pkt_dev->svlan_cfi,
2903 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002904 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002905 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002906 }
2907 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002908 *vlan_tci = build_tci(pkt_dev->vlan_id,
2909 pkt_dev->vlan_cfi,
2910 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002911 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002912 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002913 }
2914
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002915 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002916 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002917 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002918 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002919 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002920 iph = ipv6_hdr(skb);
2921 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002924 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002925
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002926 /* Eth + IPh + UDPh + mpls */
2927 datalen = pkt_dev->cur_pkt_size - 14 -
2928 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002929 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Amerigo Wang9ace1a32012-10-09 17:48:16 +00002931 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 datalen = sizeof(struct pktgen_hdr);
2933 if (net_ratelimit())
Joe Perchesf9467ea2010-06-21 12:29:14 +00002934 pr_info("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
2936
2937 udph->source = htons(pkt_dev->cur_udp_src);
2938 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002939 udph->len = htons(datalen + sizeof(struct udphdr));
2940 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002942 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002944 if (pkt_dev->traffic_class) {
2945 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002946 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002947 }
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 iph->hop_limit = 32;
2950
2951 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2952 iph->nexthdr = IPPROTO_UDP;
2953
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002954 iph->daddr = pkt_dev->cur_in6_daddr;
2955 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002957 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002958 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002959 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 skb->dev = odev;
2961 skb->pkt_type = PACKET_HOST;
2962
Eric Dumazet26ad7872011-01-25 13:26:05 -08002963 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 return skb;
2966}
2967
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002968static struct sk_buff *fill_packet(struct net_device *odev,
2969 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002971 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return fill_packet_ipv6(odev, pkt_dev);
2973 else
2974 return fill_packet_ipv4(odev, pkt_dev);
2975}
2976
Luiz Capitulino222f1802006-03-20 22:16:13 -08002977static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002979 pkt_dev->seq_num = 1;
2980 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002982 pkt_dev->tx_bytes = 0;
2983 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
2986/* Set up structure for sending pkts, clear counters */
2987
2988static void pktgen_run(struct pktgen_thread *t)
2989{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002990 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 int started = 0;
2992
Joe Perchesf9467ea2010-06-21 12:29:14 +00002993 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002996 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 /*
2999 * setup odev and create initial packet.
3000 */
3001 pktgen_setup_inject(pkt_dev);
3002
Luiz Capitulino222f1802006-03-20 22:16:13 -08003003 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 pkt_dev->skb = NULL;
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00003007 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003008
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003009 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 strcpy(pkt_dev->result, "Starting");
3012 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003013 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 strcpy(pkt_dev->result, "Error starting");
3015 }
3016 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003017 if (started)
3018 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
3021static void pktgen_stop_all_threads_ifs(void)
3022{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003023 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Joe Perchesf9467ea2010-06-21 12:29:14 +00003025 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003027 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003028
3029 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003030 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003031
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003032 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Stephen Hemminger648fda72009-08-27 13:55:07 +00003035static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003037 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003039 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003040 if (pkt_dev->running)
3041 return 1;
3042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
Luiz Capitulino222f1802006-03-20 22:16:13 -08003045static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003047 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Luiz Capitulino222f1802006-03-20 22:16:13 -08003049 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Luiz Capitulino222f1802006-03-20 22:16:13 -08003051 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Luiz Capitulino222f1802006-03-20 22:16:13 -08003053 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Luiz Capitulino222f1802006-03-20 22:16:13 -08003055 if (signal_pending(current))
3056 goto signal;
3057 if_lock(t);
3058 }
3059 if_unlock(t);
3060 return 1;
3061signal:
3062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
3065static int pktgen_wait_all_threads_run(void)
3066{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003067 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003069
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003070 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003071
3072 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074 if (sig == 0)
3075 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003077
3078 if (sig == 0)
3079 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003081
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003082 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return sig;
3084}
3085
3086static void pktgen_run_all_threads(void)
3087{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003088 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Joe Perchesf9467ea2010-06-21 12:29:14 +00003090 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003092 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003094 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003096
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003097 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003099 /* Propagate thread->control */
3100 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 pktgen_wait_all_threads_run();
3103}
3104
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003105static void pktgen_reset_all_threads(void)
3106{
3107 struct pktgen_thread *t;
3108
Joe Perchesf9467ea2010-06-21 12:29:14 +00003109 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003110
3111 mutex_lock(&pktgen_thread_lock);
3112
3113 list_for_each_entry(t, &pktgen_threads, th_list)
3114 t->control |= (T_REMDEVALL);
3115
3116 mutex_unlock(&pktgen_thread_lock);
3117
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003118 /* Propagate thread->control */
3119 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003120
3121 pktgen_wait_all_threads_run();
3122}
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3125{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003126 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003127 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003128 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3129 pkt_dev->started_at);
3130 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Daniel Turull03a14ab2011-03-09 14:11:00 -08003132 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003133 (unsigned long long)ktime_to_us(elapsed),
3134 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3135 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003136 (unsigned long long)pkt_dev->sofar,
3137 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003139 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3140 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Luiz Capitulino222f1802006-03-20 22:16:13 -08003142 bps = pps * 8 * pkt_dev->cur_pkt_size;
3143
3144 mbps = bps;
3145 do_div(mbps, 1000000);
3146 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3147 (unsigned long long)pps,
3148 (unsigned long long)mbps,
3149 (unsigned long long)bps,
3150 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003157
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003159 pr_warning("interface: %s is already stopped\n",
3160 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003161 return -EINVAL;
3162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003164 kfree_skb(pkt_dev->skb);
3165 pkt_dev->skb = NULL;
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00003166 pkt_dev->stopped_at = ktime_get();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003167 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003169 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
Luiz Capitulino222f1802006-03-20 22:16:13 -08003174static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003176 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 if_lock(t);
3179
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003180 list_for_each_entry(pkt_dev, &t->if_list, list) {
3181 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003182 continue;
3183 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003184 best = pkt_dev;
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00003185 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003186 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
3188 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003189 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
Luiz Capitulino222f1802006-03-20 22:16:13 -08003192static void pktgen_stop(struct pktgen_thread *t)
3193{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003194 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Joe Perchesf9467ea2010-06-21 12:29:14 +00003196 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Luiz Capitulino222f1802006-03-20 22:16:13 -08003198 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003200 list_for_each_entry(pkt_dev, &t->if_list, list) {
3201 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Luiz Capitulino222f1802006-03-20 22:16:13 -08003204 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003207/*
3208 * one of our devices needs to be removed - find it
3209 * and remove it
3210 */
3211static void pktgen_rem_one_if(struct pktgen_thread *t)
3212{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003213 struct list_head *q, *n;
3214 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003215
Joe Perchesf9467ea2010-06-21 12:29:14 +00003216 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003217
3218 if_lock(t);
3219
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003220 list_for_each_safe(q, n, &t->if_list) {
3221 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003222
Luiz Capitulino222f1802006-03-20 22:16:13 -08003223 if (!cur->removal_mark)
3224 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003225
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003226 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003227 cur->skb = NULL;
3228
3229 pktgen_remove_device(t, cur);
3230
3231 break;
3232 }
3233
3234 if_unlock(t);
3235}
3236
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003239 struct list_head *q, *n;
3240 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003241
Joe Perchesf9467ea2010-06-21 12:29:14 +00003242 func_enter();
3243
Luiz Capitulino222f1802006-03-20 22:16:13 -08003244 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003245
Luiz Capitulino222f1802006-03-20 22:16:13 -08003246 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003248 list_for_each_safe(q, n, &t->if_list) {
3249 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003250
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003251 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003252 cur->skb = NULL;
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 pktgen_remove_device(t, cur);
3255 }
3256
Luiz Capitulino222f1802006-03-20 22:16:13 -08003257 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Luiz Capitulino222f1802006-03-20 22:16:13 -08003260static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003262 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
David S. Milleree74baa2007-01-01 20:51:53 -08003264 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Stephen Hemmingeref879792009-09-22 19:41:43 +00003268static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003269{
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00003270 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003271 schedule();
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00003272 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003273}
3274
Stephen Hemmingeref879792009-09-22 19:41:43 +00003275static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3276{
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00003277 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003278
3279 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3280 if (signal_pending(current))
3281 break;
3282
3283 if (need_resched())
3284 pktgen_resched(pkt_dev);
3285 else
3286 cpu_relax();
3287 }
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00003288 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003289}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003290
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003291static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Stephen Hemminger00829822008-11-20 20:14:53 -08003293 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003294 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003295 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003296 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003297 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 int ret;
3299
Stephen Hemmingeref879792009-09-22 19:41:43 +00003300 /* If device is offline, then don't send */
3301 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3302 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003303 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003305
Stephen Hemmingeref879792009-09-22 19:41:43 +00003306 /* This is max DELAY, this has special meaning of
3307 * "never transmit"
3308 */
3309 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmannc5f469a2012-10-28 08:27:19 +00003310 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003311 return;
3312 }
3313
3314 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003315 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3316 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3317 /* build a new pkt */
3318 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003319
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003320 pkt_dev->skb = fill_packet(odev, pkt_dev);
3321 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003322 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003323 schedule();
3324 pkt_dev->clone_count--; /* back out increment, OOM */
3325 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003327 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003328 pkt_dev->allocated_skbs++;
3329 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003331
Stephen Hemmingeref879792009-09-22 19:41:43 +00003332 if (pkt_dev->delay && pkt_dev->last_ok)
3333 spin(pkt_dev, pkt_dev->next_tx);
3334
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003335 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3336 txq = netdev_get_tx_queue(odev, queue_map);
3337
3338 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003339
Tom Herbert734664982011-11-28 16:32:44 +00003340 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003341 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003342 pkt_dev->last_ok = 0;
3343 goto unlock;
3344 }
3345 atomic_inc(&(pkt_dev->skb->users));
3346 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Stephen Hemmingeref879792009-09-22 19:41:43 +00003348 switch (ret) {
3349 case NETDEV_TX_OK:
3350 txq_trans_update(txq);
3351 pkt_dev->last_ok = 1;
3352 pkt_dev->sofar++;
3353 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003354 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003355 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003356 case NET_XMIT_DROP:
3357 case NET_XMIT_CN:
3358 case NET_XMIT_POLICED:
3359 /* skb has been consumed */
3360 pkt_dev->errors++;
3361 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003362 default: /* Drivers are not supposed to return other values! */
3363 if (net_ratelimit())
Joe Perches58231182010-12-20 10:30:06 -08003364 pr_info("%s xmit error: %d\n", pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003365 pkt_dev->errors++;
3366 /* fallthru */
3367 case NETDEV_TX_LOCKED:
3368 case NETDEV_TX_BUSY:
3369 /* Retry it next time */
3370 atomic_dec(&(pkt_dev->skb->users));
3371 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003372 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003373unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003374 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 /* If pkt_dev->count is zero, then run forever */
3377 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003378 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 /* Done with this */
3381 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003382 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003383}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003385/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 * Main loop of the thread goes here
3387 */
3388
David S. Milleree74baa2007-01-01 20:51:53 -08003389static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
3391 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003392 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003393 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
David S. Milleree74baa2007-01-01 20:51:53 -08003396 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
3398 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003399 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Joe Perchesf9467ea2010-06-21 12:29:14 +00003401 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
David S. Milleree74baa2007-01-01 20:51:53 -08003403 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003404
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003405 set_freezable();
3406
David S. Milleree74baa2007-01-01 20:51:53 -08003407 while (!kthread_should_stop()) {
3408 pkt_dev = next_to_run(t);
3409
Stephen Hemmingeref879792009-09-22 19:41:43 +00003410 if (unlikely(!pkt_dev && t->control == 0)) {
Eric Dumazet551eaff2010-11-21 10:26:44 -08003411 if (pktgen_exiting)
3412 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003413 wait_event_interruptible_timeout(t->queue,
3414 t->control != 0,
3415 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003416 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003417 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003418 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 __set_current_state(TASK_RUNNING);
3421
Stephen Hemmingeref879792009-09-22 19:41:43 +00003422 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 pktgen_xmit(pkt_dev);
3424
Stephen Hemmingeref879792009-09-22 19:41:43 +00003425 if (need_resched())
3426 pktgen_resched(pkt_dev);
3427 else
3428 cpu_relax();
3429 }
3430
Luiz Capitulino222f1802006-03-20 22:16:13 -08003431 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 pktgen_stop(t);
3433 t->control &= ~(T_STOP);
3434 }
3435
Luiz Capitulino222f1802006-03-20 22:16:13 -08003436 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 pktgen_run(t);
3438 t->control &= ~(T_RUN);
3439 }
3440
Luiz Capitulino222f1802006-03-20 22:16:13 -08003441 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003443 t->control &= ~(T_REMDEVALL);
3444 }
3445
Luiz Capitulino222f1802006-03-20 22:16:13 -08003446 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003447 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 t->control &= ~(T_REMDEV);
3449 }
3450
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003451 try_to_freeze();
3452
David S. Milleree74baa2007-01-01 20:51:53 -08003453 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Joe Perchesf9467ea2010-06-21 12:29:14 +00003456 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003457 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Joe Perchesf9467ea2010-06-21 12:29:14 +00003459 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003460 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Joe Perchesf9467ea2010-06-21 12:29:14 +00003462 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003463 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003464
Eric Dumazet551eaff2010-11-21 10:26:44 -08003465 /* Wait for kthread_stop */
3466 while (!kthread_should_stop()) {
3467 set_current_state(TASK_INTERRUPTIBLE);
3468 schedule();
3469 }
3470 __set_current_state(TASK_RUNNING);
3471
David S. Milleree74baa2007-01-01 20:51:53 -08003472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
Luiz Capitulino222f1802006-03-20 22:16:13 -08003475static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003476 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003478 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003479 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Eric Dumazet3e984842009-11-24 14:50:53 -08003481 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003482 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003483 if (strncmp(p->odevname, ifname, len) == 0) {
3484 if (p->odevname[len]) {
3485 if (exact || p->odevname[len] != '@')
3486 continue;
3487 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003488 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003489 break;
3490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Luiz Capitulino222f1802006-03-20 22:16:13 -08003492 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003493 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003494 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003497/*
3498 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 */
3500
Luiz Capitulino222f1802006-03-20 22:16:13 -08003501static int add_dev_to_thread(struct pktgen_thread *t,
3502 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503{
3504 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Luiz Capitulino222f1802006-03-20 22:16:13 -08003506 if_lock(t);
3507
3508 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003509 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003510 rv = -EBUSY;
3511 goto out;
3512 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003513
3514 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003515 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 pkt_dev->running = 0;
3517
Luiz Capitulino222f1802006-03-20 22:16:13 -08003518out:
3519 if_unlock(t);
3520 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
3523/* Called under thread lock */
3524
Luiz Capitulino222f1802006-03-20 22:16:13 -08003525static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003527 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003528 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003529 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 /* We don't allow a device to be on several threads */
3532
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003533 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3534 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003535 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003536 return -EBUSY;
3537 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003538
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003539 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003540 if (!pkt_dev)
3541 return -ENOMEM;
3542
Eric Dumazet593f63b2009-11-23 01:44:37 +00003543 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003544 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003545 node);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003546 if (pkt_dev->flows == NULL) {
3547 kfree(pkt_dev);
3548 return -ENOMEM;
3549 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003550
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003551 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003552 pkt_dev->min_pkt_size = ETH_ZLEN;
3553 pkt_dev->max_pkt_size = ETH_ZLEN;
3554 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003555 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003556 pkt_dev->count = pg_count_d;
3557 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003558 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003559 pkt_dev->udp_src_max = 9;
3560 pkt_dev->udp_dst_min = 9;
3561 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003562 pkt_dev->vlan_p = 0;
3563 pkt_dev->vlan_cfi = 0;
3564 pkt_dev->vlan_id = 0xffff;
3565 pkt_dev->svlan_p = 0;
3566 pkt_dev->svlan_cfi = 0;
3567 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003568 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003569
Stephen Hemminger39df2322007-03-04 16:11:51 -08003570 err = pktgen_setup_dev(pkt_dev, ifname);
3571 if (err)
3572 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003573 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3574 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003575
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003576 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3577 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003578 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003579 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003580 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003581 err = -EINVAL;
3582 goto out2;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003583 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003584#ifdef CONFIG_XFRM
3585 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3586 pkt_dev->ipsproto = IPPROTO_ESP;
3587#endif
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003588
3589 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003590out2:
3591 dev_put(pkt_dev->odev);
3592out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003593#ifdef CONFIG_XFRM
3594 free_SAs(pkt_dev);
3595#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003596 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003597 kfree(pkt_dev);
3598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599}
3600
David S. Milleree74baa2007-01-01 20:51:53 -08003601static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003603 struct pktgen_thread *t;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003604 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003605 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003607 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3608 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003609 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003610 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003611 return -ENOMEM;
3612 }
3613
Luiz Capitulino222f1802006-03-20 22:16:13 -08003614 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003616
David S. Milleree74baa2007-01-01 20:51:53 -08003617 INIT_LIST_HEAD(&t->if_list);
3618
3619 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003620 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003621
Eric Dumazet94dcf292011-03-22 16:30:45 -07003622 p = kthread_create_on_node(pktgen_thread_worker,
3623 t,
3624 cpu_to_node(cpu),
3625 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003626 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003627 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003628 list_del(&t->th_list);
3629 kfree(t);
3630 return PTR_ERR(p);
3631 }
3632 kthread_bind(p, cpu);
3633 t->tsk = p;
3634
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003635 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3636 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003637 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003638 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003639 PG_PROC_DIR, t->tsk->comm);
3640 kthread_stop(p);
3641 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003642 kfree(t);
3643 return -EINVAL;
3644 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003645
David S. Milleree74baa2007-01-01 20:51:53 -08003646 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003647 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
3649 return 0;
3650}
3651
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003652/*
3653 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003655static void _rem_dev_from_if_list(struct pktgen_thread *t,
3656 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003658 struct list_head *q, *n;
3659 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003661 list_for_each_safe(q, n, &t->if_list) {
3662 p = list_entry(q, struct pktgen_dev, list);
3663 if (p == pkt_dev)
3664 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666}
3667
Luiz Capitulino222f1802006-03-20 22:16:13 -08003668static int pktgen_remove_device(struct pktgen_thread *t,
3669 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670{
3671
Joe Perchesf9467ea2010-06-21 12:29:14 +00003672 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Luiz Capitulino222f1802006-03-20 22:16:13 -08003674 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003675 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003676 pktgen_stop_device(pkt_dev);
3677 }
3678
3679 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
3681 if (pkt_dev->odev) {
3682 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003683 pkt_dev->odev = NULL;
3684 }
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 /* And update the thread if_list */
3687
3688 _rem_dev_from_if_list(t, pkt_dev);
3689
Stephen Hemminger39df2322007-03-04 16:11:51 -08003690 if (pkt_dev->entry)
3691 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003693#ifdef CONFIG_XFRM
3694 free_SAs(pkt_dev);
3695#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003696 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003697 if (pkt_dev->page)
3698 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
Luiz Capitulino222f1802006-03-20 22:16:13 -08003703static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
3705 int cpu;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003706 struct proc_dir_entry *pe;
WANG Congce14f892011-05-22 00:52:08 +00003707 int ret = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003708
Joe Perchesf9467ea2010-06-21 12:29:14 +00003709 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003711 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003712 if (!pg_proc_dir)
3713 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Wang Chen25296d52008-02-28 14:11:49 -08003715 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003716 if (pe == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003717 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
WANG Congce14f892011-05-22 00:52:08 +00003718 ret = -EINVAL;
3719 goto remove_dir;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003723
John Hawkes670c02c2005-10-13 09:30:31 -07003724 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003725 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
David S. Milleree74baa2007-01-01 20:51:53 -08003727 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003728 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00003729 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3730 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003731 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003732
3733 if (list_empty(&pktgen_threads)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003734 pr_err("ERROR: Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003735 ret = -ENODEV;
3736 goto unregister;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003737 }
3738
Luiz Capitulino222f1802006-03-20 22:16:13 -08003739 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003740
3741 unregister:
3742 unregister_netdevice_notifier(&pktgen_notifier_block);
3743 remove_proc_entry(PGCTRL, pg_proc_dir);
3744 remove_dir:
3745 proc_net_remove(&init_net, PG_PROC_DIR);
3746 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
3749static void __exit pg_cleanup(void)
3750{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003751 struct pktgen_thread *t;
3752 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003753 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Luiz Capitulino222f1802006-03-20 22:16:13 -08003755 /* Stop all interfaces & threads */
Eric Dumazet551eaff2010-11-21 10:26:44 -08003756 pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Eric Dumazetc57b5462012-05-09 13:29:51 +00003758 mutex_lock(&pktgen_thread_lock);
Eric Dumazetd4b11332012-05-17 23:52:26 +00003759 list_splice_init(&pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003760 mutex_unlock(&pktgen_thread_lock);
3761
3762 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003763 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003764 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003765 kthread_stop(t->tsk);
3766 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Luiz Capitulino222f1802006-03-20 22:16:13 -08003769 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 unregister_netdevice_notifier(&pktgen_notifier_block);
3771
Luiz Capitulino222f1802006-03-20 22:16:13 -08003772 /* Clean up proc file system */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003773 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003774 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
3776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777module_init(pg_init);
3778module_exit(pg_cleanup);
3779
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003780MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781MODULE_DESCRIPTION("Packet Generator tool");
3782MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003783MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003785MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003787MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003789MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003791MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");