blob: b29dacf900f9496a42a565a3b2c48ad9cf502c74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
Jan Engelhardt96de0e22007-10-19 23:21:04 +02009 * Jens Låås <jens.laas@data.slu.se>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090047 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090068 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
116 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Joe Perchesf9467ea2010-06-21 12:29:14 +0000118
119#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <linux/sys.h>
122#include <linux/types.h>
123#include <linux/module.h>
124#include <linux/moduleparam.h>
125#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800126#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <linux/sched.h>
128#include <linux/slab.h>
129#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#include <linux/unistd.h>
131#include <linux/string.h>
132#include <linux/ptrace.h>
133#include <linux/errno.h>
134#include <linux/ioport.h>
135#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800136#include <linux/capability.h>
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700137#include <linux/hrtimer.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700138#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#include <linux/delay.h>
140#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800141#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#include <linux/init.h>
143#include <linux/skbuff.h>
144#include <linux/netdevice.h>
145#include <linux/inet.h>
146#include <linux/inetdevice.h>
147#include <linux/rtnetlink.h>
148#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700149#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#include <linux/in.h>
151#include <linux/ip.h>
152#include <linux/ipv6.h>
153#include <linux/udp.h>
154#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700155#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800157#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800158#include <linux/kthread.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700159#include <linux/prefetch.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200160#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#include <net/checksum.h>
162#include <net/ipv6.h>
163#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700164#ifdef CONFIG_XFRM
165#include <net/xfrm.h>
166#endif
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 Hemmingerd50a6b562005-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
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000251 int min_pkt_size;
252 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700253 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800254 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800255 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000256 u64 delay; /* nano-seconds */
257
Luiz Capitulino222f1802006-03-20 22:16:13 -0800258 __u64 count; /* Default No packets to send */
259 __u64 sofar; /* How many pkts we've sent so far */
260 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800261 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Luiz Capitulino222f1802006-03-20 22:16:13 -0800263 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Luiz Capitulino222f1802006-03-20 22:16:13 -0800265 __u64 allocated_skbs;
266 __u32 clone_count;
267 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000268 * Or a failed transmit of some sort?
269 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800270 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000271 ktime_t next_tx;
272 ktime_t started_at;
273 ktime_t stopped_at;
274 u64 idle_acc; /* nano-seconds */
275
Luiz Capitulino222f1802006-03-20 22:16:13 -0800276 __u32 seq_num;
277
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000278 int clone_skb; /*
279 * Use multiple SKBs during packet gen.
280 * If this number is greater than 1, then
281 * that many copies of the same packet will be
282 * sent before a new packet is allocated.
283 * If you want to send 1024 identical packets
284 * before creating a new packet,
285 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800286 */
287
288 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
289 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
290 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
291 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
292
293 struct in6_addr in6_saddr;
294 struct in6_addr in6_daddr;
295 struct in6_addr cur_in6_daddr;
296 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800298 struct in6_addr min_in6_daddr;
299 struct in6_addr max_in6_daddr;
300 struct in6_addr min_in6_saddr;
301 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Luiz Capitulino222f1802006-03-20 22:16:13 -0800303 /* If we're doing ranges, random or incremental, then this
304 * defines the min/max for those ranges.
305 */
Al Viro252e3342006-11-14 20:48:11 -0800306 __be32 saddr_min; /* inclusive, source IP address */
307 __be32 saddr_max; /* exclusive, source IP address */
308 __be32 daddr_min; /* inclusive, dest IP address */
309 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Luiz Capitulino222f1802006-03-20 22:16:13 -0800311 __u16 udp_src_min; /* inclusive, source UDP port */
312 __u16 udp_src_max; /* exclusive, source UDP port */
313 __u16 udp_dst_min; /* inclusive, dest UDP port */
314 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700316 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000317 __u8 tos; /* six MSB of (former) IPv4 TOS
318 are for dscp codepoint */
319 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
320 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700321
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800322 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000323 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800324 __be32 labels[MAX_MPLS_LABELS];
325
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700326 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
327 __u8 vlan_p;
328 __u8 vlan_cfi;
329 __u16 vlan_id; /* 0xffff means no vlan tag */
330
331 __u8 svlan_p;
332 __u8 svlan_cfi;
333 __u16 svlan_id; /* 0xffff means no svlan tag */
334
Luiz Capitulino222f1802006-03-20 22:16:13 -0800335 __u32 src_mac_count; /* How many MACs to iterate through */
336 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Luiz Capitulino222f1802006-03-20 22:16:13 -0800338 unsigned char dst_mac[ETH_ALEN];
339 unsigned char src_mac[ETH_ALEN];
340
341 __u32 cur_dst_mac_offset;
342 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800343 __be32 cur_saddr;
344 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700345 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800346 __u16 cur_udp_dst;
347 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700348 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800349 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800350 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800351
352 __u8 hh[14];
353 /* = {
354 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
355
356 We fill in SRC address later
357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358 0x08, 0x00
359 };
360 */
361 __u16 pad; /* pad out the hh struct to an even 16 bytes */
362
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000363 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800364 * are transmitting the same one multiple times
365 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000366 struct net_device *odev; /* The out-going device.
367 * Note that the device should have it's
368 * pg_info pointer pointing back to this
369 * device.
370 * Set when the user specifies the out-going
371 * device name (not when the inject is
372 * started as it used to do.)
373 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000374 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000376 unsigned int cflows; /* Concurrent flows (config) */
377 unsigned int lflow; /* Flow length (config) */
378 unsigned int nflows; /* accumulated flows (stats) */
379 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700380
381 u16 queue_map_min;
382 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000383 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000384 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700385
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700386#ifdef CONFIG_XFRM
387 __u8 ipsmode; /* IPSEC mode (config) */
388 __u8 ipsproto; /* IPSEC type (config) */
389#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800390 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391};
392
393struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800394 __be32 pgh_magic;
395 __be32 seq_num;
396 __be32 tv_sec;
397 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000439static int pg_count_d __read_mostly = 1000;
440static int pg_delay_d __read_mostly;
441static int pg_clone_skb_d __read_mostly;
442static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Luiz Capitulino222fa072006-03-20 22:24:27 -0800444static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800445static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static struct notifier_block pktgen_notifier_block = {
448 .notifier_call = pktgen_device_event,
449};
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900452 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 *
454 */
455
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700456static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800457{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000458 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000462static ssize_t pgctrl_write(struct file *file, const char __user *buf,
463 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700466 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Luiz Capitulino222f1802006-03-20 22:16:13 -0800468 if (!capable(CAP_NET_ADMIN)) {
469 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700473 if (count > sizeof(data))
474 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700477 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700478 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800479 }
480 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Luiz Capitulino222f1802006-03-20 22:16:13 -0800482 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 pktgen_stop_all_threads_ifs();
484
Luiz Capitulino222f1802006-03-20 22:16:13 -0800485 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 pktgen_run_all_threads();
487
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800488 else if (!strcmp(data, "reset"))
489 pktgen_reset_all_threads();
490
Luiz Capitulino222f1802006-03-20 22:16:13 -0800491 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000492 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 err = count;
495
Luiz Capitulino222f1802006-03-20 22:16:13 -0800496out:
497 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700500static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700502 return single_open(file, pgctrl_show, PDE(inode)->data);
503}
504
Arjan van de Ven9a321442007-02-12 00:55:35 -0800505static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800506 .owner = THIS_MODULE,
507 .open = pgctrl_open,
508 .read = seq_read,
509 .llseek = seq_lseek,
510 .write = pgctrl_write,
511 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700512};
513
514static int pktgen_if_show(struct seq_file *seq, void *v)
515{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000516 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000517 ktime_t stopped;
518 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Luiz Capitulino222f1802006-03-20 22:16:13 -0800520 seq_printf(seq,
521 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
522 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
523 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Luiz Capitulino222f1802006-03-20 22:16:13 -0800525 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000526 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
527 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000528 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Luiz Capitulino222f1802006-03-20 22:16:13 -0800530 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
531 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Robert Olsson45b270f2007-08-28 15:45:55 -0700533 seq_printf(seq,
534 " queue_map_min: %u queue_map_max: %u\n",
535 pkt_dev->queue_map_min,
536 pkt_dev->queue_map_max);
537
John Fastabend9e50e3a2010-11-16 19:12:28 +0000538 if (pkt_dev->skb_priority)
539 seq_printf(seq, " skb_priority: %u\n",
540 pkt_dev->skb_priority);
541
Luiz Capitulino222f1802006-03-20 22:16:13 -0800542 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800543 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000544 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
545 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
546 &pkt_dev->in6_saddr,
547 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
548 &pkt_dev->in6_daddr,
549 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000550 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800551 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000552 " dst_min: %s dst_max: %s\n",
553 pkt_dev->dst_min, pkt_dev->dst_max);
554 seq_printf(seq,
555 " src_min: %s src_max: %s\n",
556 pkt_dev->src_min, pkt_dev->src_max);
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700559 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Johannes Berge1749612008-10-27 15:59:26 -0700561 seq_printf(seq, "%pM ",
562 is_zero_ether_addr(pkt_dev->src_mac) ?
563 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Luiz Capitulino222f1802006-03-20 22:16:13 -0800565 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700566 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Luiz Capitulino222f1802006-03-20 22:16:13 -0800568 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000569 " udp_src_min: %d udp_src_max: %d"
570 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800571 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
572 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Luiz Capitulino222f1802006-03-20 22:16:13 -0800574 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800575 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700576 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800578 if (pkt_dev->nr_labels) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000579 unsigned int i;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800580 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700581 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800582 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
583 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
584 }
585
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000586 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700587 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000588 pkt_dev->vlan_id, pkt_dev->vlan_p,
589 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700590
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000591 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700592 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000593 pkt_dev->svlan_id, pkt_dev->svlan_p,
594 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700595
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000596 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700597 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700598
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000599 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700600 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700601
Robert Olssone99b99b2010-03-18 22:44:30 +0000602 if (pkt_dev->node >= 0)
603 seq_printf(seq, " node: %d\n", pkt_dev->node);
604
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800605 seq_printf(seq, " Flags: ");
606
Luiz Capitulino222f1802006-03-20 22:16:13 -0800607 if (pkt_dev->flags & F_IPV6)
608 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Luiz Capitulino222f1802006-03-20 22:16:13 -0800610 if (pkt_dev->flags & F_IPSRC_RND)
611 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Luiz Capitulino222f1802006-03-20 22:16:13 -0800613 if (pkt_dev->flags & F_IPDST_RND)
614 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Luiz Capitulino222f1802006-03-20 22:16:13 -0800616 if (pkt_dev->flags & F_TXSIZE_RND)
617 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Luiz Capitulino222f1802006-03-20 22:16:13 -0800619 if (pkt_dev->flags & F_UDPSRC_RND)
620 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Luiz Capitulino222f1802006-03-20 22:16:13 -0800622 if (pkt_dev->flags & F_UDPDST_RND)
623 seq_printf(seq, "UDPDST_RND ");
624
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800625 if (pkt_dev->flags & F_MPLS_RND)
626 seq_printf(seq, "MPLS_RND ");
627
Robert Olsson45b270f2007-08-28 15:45:55 -0700628 if (pkt_dev->flags & F_QUEUE_MAP_RND)
629 seq_printf(seq, "QUEUE_MAP_RND ");
630
Robert Olssone6fce5b2008-08-07 02:23:01 -0700631 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
632 seq_printf(seq, "QUEUE_MAP_CPU ");
633
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700634 if (pkt_dev->cflows) {
635 if (pkt_dev->flags & F_FLOW_SEQ)
636 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
637 else
638 seq_printf(seq, "FLOW_RND ");
639 }
640
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700641#ifdef CONFIG_XFRM
642 if (pkt_dev->flags & F_IPSEC_ON)
643 seq_printf(seq, "IPSEC ");
644#endif
645
Luiz Capitulino222f1802006-03-20 22:16:13 -0800646 if (pkt_dev->flags & F_MACSRC_RND)
647 seq_printf(seq, "MACSRC_RND ");
648
649 if (pkt_dev->flags & F_MACDST_RND)
650 seq_printf(seq, "MACDST_RND ");
651
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700652 if (pkt_dev->flags & F_VID_RND)
653 seq_printf(seq, "VID_RND ");
654
655 if (pkt_dev->flags & F_SVID_RND)
656 seq_printf(seq, "SVID_RND ");
657
Robert Olssone99b99b2010-03-18 22:44:30 +0000658 if (pkt_dev->flags & F_NODE)
659 seq_printf(seq, "NODE_ALLOC ");
660
Luiz Capitulino222f1802006-03-20 22:16:13 -0800661 seq_puts(seq, "\n");
662
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000663 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000664 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000665 idle = pkt_dev->idle_acc;
666 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800667
668 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000669 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800670 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000671 (unsigned long long)pkt_dev->errors);
672
673 seq_printf(seq,
674 " started: %lluus stopped: %lluus idle: %lluus\n",
675 (unsigned long long) ktime_to_us(pkt_dev->started_at),
676 (unsigned long long) ktime_to_us(stopped),
677 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800678
679 seq_printf(seq,
680 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700681 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
682 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Luiz Capitulino222f1802006-03-20 22:16:13 -0800684 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000685 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
686 &pkt_dev->cur_in6_saddr,
687 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800688 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000689 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
690 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Luiz Capitulino222f1802006-03-20 22:16:13 -0800692 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700693 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Robert Olsson45b270f2007-08-28 15:45:55 -0700695 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
696
Luiz Capitulino222f1802006-03-20 22:16:13 -0800697 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800700 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800702 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700704 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800707
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000708static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
709 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800710{
711 int i = 0;
712 *num = 0;
713
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700714 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000715 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800716 char c;
717 *num <<= 4;
718 if (get_user(c, &user_buffer[i]))
719 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000720 value = hex_to_bin(c);
721 if (value >= 0)
722 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800723 else
724 break;
725 }
726 return i;
727}
728
Luiz Capitulino222f1802006-03-20 22:16:13 -0800729static int count_trail_chars(const char __user * user_buffer,
730 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 int i;
733
734 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800735 char c;
736 if (get_user(c, &user_buffer[i]))
737 return -EFAULT;
738 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 case '\"':
740 case '\n':
741 case '\r':
742 case '\t':
743 case ' ':
744 case '=':
745 break;
746 default:
747 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750done:
751 return i;
752}
753
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000754static long num_arg(const char __user *user_buffer, unsigned long maxlen,
755 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000757 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800759
Paul Gortmakerd6182222010-10-18 12:14:44 +0000760 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800761 char c;
762 if (get_user(c, &user_buffer[i]))
763 return -EFAULT;
764 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800766 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 } else
768 break;
769 }
770 return i;
771}
772
Luiz Capitulino222f1802006-03-20 22:16:13 -0800773static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000775 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Paul Gortmakerd6182222010-10-18 12:14:44 +0000777 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800778 char c;
779 if (get_user(c, &user_buffer[i]))
780 return -EFAULT;
781 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 case '\"':
783 case '\n':
784 case '\r':
785 case '\t':
786 case ' ':
787 goto done_str;
788 break;
789 default:
790 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return i;
795}
796
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800797static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
798{
Eric Dumazet95c96172012-04-15 05:58:06 +0000799 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800800 char c;
801 ssize_t i = 0;
802 int len;
803
804 pkt_dev->nr_labels = 0;
805 do {
806 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700807 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800808 if (len <= 0)
809 return len;
810 pkt_dev->labels[n] = htonl(tmp);
811 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
812 pkt_dev->flags |= F_MPLS_RND;
813 i += len;
814 if (get_user(c, &buffer[i]))
815 return -EFAULT;
816 i++;
817 n++;
818 if (n >= MAX_MPLS_LABELS)
819 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700820 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800821
822 pkt_dev->nr_labels = n;
823 return i;
824}
825
Luiz Capitulino222f1802006-03-20 22:16:13 -0800826static ssize_t pktgen_if_write(struct file *file,
827 const char __user * user_buffer, size_t count,
828 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Joe Perches8a994a72010-07-12 10:50:23 +0000830 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800831 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000832 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 char name[16], valstr[32];
834 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800835 char *pg_result = NULL;
836 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800838
839 pg_result = &(pkt_dev->result[0]);
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000842 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return -EINVAL;
844 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800845
Paul Gortmakerd6182222010-10-18 12:14:44 +0000846 max = count;
847 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800848 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000849 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800850 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000852 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* Read variable name */
855
856 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000857 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800858 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800861 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return -EFAULT;
863 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800864
865 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867 if (len < 0)
868 return len;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 i += len;
871
872 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000873 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700874 char tb[copy + 1];
875 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700877 tb[copy] = 0;
Joe Perchesf342cda2012-05-16 17:50:41 +0000878 pr_debug("%s,%lu buffer -:%s:-\n",
879 name, (unsigned long)count, tb);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (!strcmp(name, "min_pkt_size")) {
883 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000884 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800885 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800888 if (value < 14 + 20 + 8)
889 value = 14 + 20 + 8;
890 if (value != pkt_dev->min_pkt_size) {
891 pkt_dev->min_pkt_size = value;
892 pkt_dev->cur_pkt_size = value;
893 }
894 sprintf(pg_result, "OK: min_pkt_size=%u",
895 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return count;
897 }
898
Luiz Capitulino222f1802006-03-20 22:16:13 -0800899 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000901 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800902 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800905 if (value < 14 + 20 + 8)
906 value = 14 + 20 + 8;
907 if (value != pkt_dev->max_pkt_size) {
908 pkt_dev->max_pkt_size = value;
909 pkt_dev->cur_pkt_size = value;
910 }
911 sprintf(pg_result, "OK: max_pkt_size=%u",
912 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return count;
914 }
915
Luiz Capitulino222f1802006-03-20 22:16:13 -0800916 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (!strcmp(name, "pkt_size")) {
919 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000920 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800921 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 if (value < 14 + 20 + 8)
925 value = 14 + 20 + 8;
926 if (value != pkt_dev->min_pkt_size) {
927 pkt_dev->min_pkt_size = value;
928 pkt_dev->max_pkt_size = value;
929 pkt_dev->cur_pkt_size = value;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
932 return count;
933 }
934
Luiz Capitulino222f1802006-03-20 22:16:13 -0800935 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000937 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800938 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800941 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 sprintf(pg_result, "OK: debug=%u", debug);
943 return count;
944 }
945
Luiz Capitulino222f1802006-03-20 22:16:13 -0800946 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000948 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800949 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 i += len;
952 pkt_dev->nfrags = value;
953 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
954 return count;
955 }
956 if (!strcmp(name, "delay")) {
957 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000958 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800959 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000962 if (value == 0x7FFFFFFF)
963 pkt_dev->delay = ULLONG_MAX;
964 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000965 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000966
967 sprintf(pg_result, "OK: delay=%llu",
968 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return count;
970 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000971 if (!strcmp(name, "rate")) {
972 len = num_arg(&user_buffer[i], 10, &value);
973 if (len < 0)
974 return len;
975
976 i += len;
977 if (!value)
978 return len;
979 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
980 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000981 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000982
983 sprintf(pg_result, "OK: rate=%lu", value);
984 return count;
985 }
986 if (!strcmp(name, "ratep")) {
987 len = num_arg(&user_buffer[i], 10, &value);
988 if (len < 0)
989 return len;
990
991 i += len;
992 if (!value)
993 return len;
994 pkt_dev->delay = NSEC_PER_SEC/value;
995 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000996 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000997
998 sprintf(pg_result, "OK: rate=%lu", value);
999 return count;
1000 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001001 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001003 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001004 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001007 if (value != pkt_dev->udp_src_min) {
1008 pkt_dev->udp_src_min = value;
1009 pkt_dev->cur_udp_src = value;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1012 return count;
1013 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001014 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001016 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001017 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001020 if (value != pkt_dev->udp_dst_min) {
1021 pkt_dev->udp_dst_min = value;
1022 pkt_dev->cur_udp_dst = value;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1025 return count;
1026 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001027 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001029 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001030 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001033 if (value != pkt_dev->udp_src_max) {
1034 pkt_dev->udp_src_max = value;
1035 pkt_dev->cur_udp_src = value;
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1038 return count;
1039 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001040 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001042 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001043 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001046 if (value != pkt_dev->udp_dst_max) {
1047 pkt_dev->udp_dst_max = value;
1048 pkt_dev->cur_udp_dst = value;
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1051 return count;
1052 }
1053 if (!strcmp(name, "clone_skb")) {
1054 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001055 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001056 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001057 if ((value > 0) &&
1058 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1059 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001061 pkt_dev->clone_skb = value;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1064 return count;
1065 }
1066 if (!strcmp(name, "count")) {
1067 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001068 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001069 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 i += len;
1072 pkt_dev->count = value;
1073 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001074 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return count;
1076 }
1077 if (!strcmp(name, "src_mac_count")) {
1078 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001079 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001080 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 i += len;
1083 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001084 pkt_dev->src_mac_count = value;
1085 pkt_dev->cur_src_mac_offset = 0;
1086 }
1087 sprintf(pg_result, "OK: src_mac_count=%d",
1088 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return count;
1090 }
1091 if (!strcmp(name, "dst_mac_count")) {
1092 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001093 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001094 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 i += len;
1097 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001098 pkt_dev->dst_mac_count = value;
1099 pkt_dev->cur_dst_mac_offset = 0;
1100 }
1101 sprintf(pg_result, "OK: dst_mac_count=%d",
1102 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return count;
1104 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001105 if (!strcmp(name, "node")) {
1106 len = num_arg(&user_buffer[i], 10, &value);
1107 if (len < 0)
1108 return len;
1109
1110 i += len;
1111
1112 if (node_possible(value)) {
1113 pkt_dev->node = value;
1114 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001115 if (pkt_dev->page) {
1116 put_page(pkt_dev->page);
1117 pkt_dev->page = NULL;
1118 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001119 }
1120 else
1121 sprintf(pg_result, "ERROR: node not possible");
1122 return count;
1123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001125 char f[32];
1126 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001128 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001129 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (copy_from_user(f, &user_buffer[i], len))
1132 return -EFAULT;
1133 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001134 if (strcmp(f, "IPSRC_RND") == 0)
1135 pkt_dev->flags |= F_IPSRC_RND;
1136
1137 else if (strcmp(f, "!IPSRC_RND") == 0)
1138 pkt_dev->flags &= ~F_IPSRC_RND;
1139
1140 else if (strcmp(f, "TXSIZE_RND") == 0)
1141 pkt_dev->flags |= F_TXSIZE_RND;
1142
1143 else if (strcmp(f, "!TXSIZE_RND") == 0)
1144 pkt_dev->flags &= ~F_TXSIZE_RND;
1145
1146 else if (strcmp(f, "IPDST_RND") == 0)
1147 pkt_dev->flags |= F_IPDST_RND;
1148
1149 else if (strcmp(f, "!IPDST_RND") == 0)
1150 pkt_dev->flags &= ~F_IPDST_RND;
1151
1152 else if (strcmp(f, "UDPSRC_RND") == 0)
1153 pkt_dev->flags |= F_UDPSRC_RND;
1154
1155 else if (strcmp(f, "!UDPSRC_RND") == 0)
1156 pkt_dev->flags &= ~F_UDPSRC_RND;
1157
1158 else if (strcmp(f, "UDPDST_RND") == 0)
1159 pkt_dev->flags |= F_UDPDST_RND;
1160
1161 else if (strcmp(f, "!UDPDST_RND") == 0)
1162 pkt_dev->flags &= ~F_UDPDST_RND;
1163
1164 else if (strcmp(f, "MACSRC_RND") == 0)
1165 pkt_dev->flags |= F_MACSRC_RND;
1166
1167 else if (strcmp(f, "!MACSRC_RND") == 0)
1168 pkt_dev->flags &= ~F_MACSRC_RND;
1169
1170 else if (strcmp(f, "MACDST_RND") == 0)
1171 pkt_dev->flags |= F_MACDST_RND;
1172
1173 else if (strcmp(f, "!MACDST_RND") == 0)
1174 pkt_dev->flags &= ~F_MACDST_RND;
1175
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001176 else if (strcmp(f, "MPLS_RND") == 0)
1177 pkt_dev->flags |= F_MPLS_RND;
1178
1179 else if (strcmp(f, "!MPLS_RND") == 0)
1180 pkt_dev->flags &= ~F_MPLS_RND;
1181
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001182 else if (strcmp(f, "VID_RND") == 0)
1183 pkt_dev->flags |= F_VID_RND;
1184
1185 else if (strcmp(f, "!VID_RND") == 0)
1186 pkt_dev->flags &= ~F_VID_RND;
1187
1188 else if (strcmp(f, "SVID_RND") == 0)
1189 pkt_dev->flags |= F_SVID_RND;
1190
1191 else if (strcmp(f, "!SVID_RND") == 0)
1192 pkt_dev->flags &= ~F_SVID_RND;
1193
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001194 else if (strcmp(f, "FLOW_SEQ") == 0)
1195 pkt_dev->flags |= F_FLOW_SEQ;
1196
Robert Olsson45b270f2007-08-28 15:45:55 -07001197 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1198 pkt_dev->flags |= F_QUEUE_MAP_RND;
1199
1200 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1201 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001202
1203 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1204 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1205
1206 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1207 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001208#ifdef CONFIG_XFRM
1209 else if (strcmp(f, "IPSEC") == 0)
1210 pkt_dev->flags |= F_IPSEC_ON;
1211#endif
1212
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001213 else if (strcmp(f, "!IPV6") == 0)
1214 pkt_dev->flags &= ~F_IPV6;
1215
Robert Olssone99b99b2010-03-18 22:44:30 +00001216 else if (strcmp(f, "NODE_ALLOC") == 0)
1217 pkt_dev->flags |= F_NODE;
1218
1219 else if (strcmp(f, "!NODE_ALLOC") == 0)
1220 pkt_dev->flags &= ~F_NODE;
1221
Luiz Capitulino222f1802006-03-20 22:16:13 -08001222 else {
1223 sprintf(pg_result,
1224 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1225 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001226 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001227 "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 -08001228 return count;
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1231 return count;
1232 }
1233 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1234 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001235 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001236 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Luiz Capitulino222f1802006-03-20 22:16:13 -08001238 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001240 buf[len] = 0;
1241 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1242 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1243 strncpy(pkt_dev->dst_min, buf, len);
1244 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1245 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1246 }
1247 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001248 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001249 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1251 return count;
1252 }
1253 if (!strcmp(name, "dst_max")) {
1254 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001255 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001256 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return -EFAULT;
1261
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 buf[len] = 0;
1263 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1264 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1265 strncpy(pkt_dev->dst_max, buf, len);
1266 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1267 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1268 }
1269 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001270 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 i += len;
1272 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1273 return count;
1274 }
1275 if (!strcmp(name, "dst6")) {
1276 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001277 if (len < 0)
1278 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 pkt_dev->flags |= F_IPV6;
1281
Luiz Capitulino222f1802006-03-20 22:16:13 -08001282 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001284 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Amerigo Wangc468fb12012-10-09 17:48:20 +00001286 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001287 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001289 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Luiz Capitulino222f1802006-03-20 22:16:13 -08001291 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001292 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Luiz Capitulino222f1802006-03-20 22:16:13 -08001294 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 sprintf(pg_result, "OK: dst6=%s", buf);
1296 return count;
1297 }
1298 if (!strcmp(name, "dst6_min")) {
1299 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001300 if (len < 0)
1301 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 pkt_dev->flags |= F_IPV6;
1304
Luiz Capitulino222f1802006-03-20 22:16:13 -08001305 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Amerigo Wangc468fb12012-10-09 17:48:20 +00001309 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001310 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001312 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001313 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001314 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Luiz Capitulino222f1802006-03-20 22:16:13 -08001316 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 sprintf(pg_result, "OK: dst6_min=%s", buf);
1318 return count;
1319 }
1320 if (!strcmp(name, "dst6_max")) {
1321 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 if (len < 0)
1323 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 pkt_dev->flags |= F_IPV6;
1326
Luiz Capitulino222f1802006-03-20 22:16:13 -08001327 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Amerigo Wangc468fb12012-10-09 17:48:20 +00001331 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001332 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Luiz Capitulino222f1802006-03-20 22:16:13 -08001334 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001335 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 sprintf(pg_result, "OK: dst6_max=%s", buf);
1339 return count;
1340 }
1341 if (!strcmp(name, "src6")) {
1342 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001343 if (len < 0)
1344 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 pkt_dev->flags |= F_IPV6;
1347
Luiz Capitulino222f1802006-03-20 22:16:13 -08001348 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001350 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Amerigo Wangc468fb12012-10-09 17:48:20 +00001352 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001353 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001355 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Luiz Capitulino222f1802006-03-20 22:16:13 -08001357 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001358 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359
1360 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 sprintf(pg_result, "OK: src6=%s", buf);
1362 return count;
1363 }
1364 if (!strcmp(name, "src_min")) {
1365 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001366 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001367 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001368
Luiz Capitulino222f1802006-03-20 22:16:13 -08001369 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001371 buf[len] = 0;
1372 if (strcmp(buf, pkt_dev->src_min) != 0) {
1373 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1374 strncpy(pkt_dev->src_min, buf, len);
1375 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1376 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1377 }
1378 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001379 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 i += len;
1381 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1382 return count;
1383 }
1384 if (!strcmp(name, "src_max")) {
1385 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001386 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001387 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001388
Luiz Capitulino222f1802006-03-20 22:16:13 -08001389 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001391 buf[len] = 0;
1392 if (strcmp(buf, pkt_dev->src_max) != 0) {
1393 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1394 strncpy(pkt_dev->src_max, buf, len);
1395 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1396 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1397 }
1398 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001399 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 i += len;
1401 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1402 return count;
1403 }
1404 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001406 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001407 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001410 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001413 if (!mac_pton(valstr, pkt_dev->dst_mac))
1414 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /* Set up Dest MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001416 memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001417
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001418 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return count;
1420 }
1421 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001423 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001424 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001430 if (!mac_pton(valstr, pkt_dev->src_mac))
1431 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001432 /* Set up Src MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001433 memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001434
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001435 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return count;
1437 }
1438
Luiz Capitulino222f1802006-03-20 22:16:13 -08001439 if (!strcmp(name, "clear_counters")) {
1440 pktgen_clear_counters(pkt_dev);
1441 sprintf(pg_result, "OK: Clearing counters.\n");
1442 return count;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if (!strcmp(name, "flows")) {
1446 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001447 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 i += len;
1451 if (value > MAX_CFLOWS)
1452 value = MAX_CFLOWS;
1453
1454 pkt_dev->cflows = value;
1455 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1456 return count;
1457 }
1458
1459 if (!strcmp(name, "flowlen")) {
1460 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001461 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001462 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 i += len;
1465 pkt_dev->lflow = value;
1466 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1467 return count;
1468 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001469
Robert Olsson45b270f2007-08-28 15:45:55 -07001470 if (!strcmp(name, "queue_map_min")) {
1471 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001472 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001473 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001474
Robert Olsson45b270f2007-08-28 15:45:55 -07001475 i += len;
1476 pkt_dev->queue_map_min = value;
1477 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1478 return count;
1479 }
1480
1481 if (!strcmp(name, "queue_map_max")) {
1482 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001483 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001484 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001485
Robert Olsson45b270f2007-08-28 15:45:55 -07001486 i += len;
1487 pkt_dev->queue_map_max = value;
1488 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1489 return count;
1490 }
1491
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001492 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001493 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001494
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001495 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001496 if (len < 0)
1497 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001498 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001499 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001500 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001501 cnt += sprintf(pg_result + cnt,
1502 "%08x%s", ntohl(pkt_dev->labels[n]),
1503 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001504
1505 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1506 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1507 pkt_dev->svlan_id = 0xffff;
1508
1509 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001510 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001511 }
1512 return count;
1513 }
1514
1515 if (!strcmp(name, "vlan_id")) {
1516 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001517 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001518 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001519
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001520 i += len;
1521 if (value <= 4095) {
1522 pkt_dev->vlan_id = value; /* turn on VLAN */
1523
1524 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001525 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001526
1527 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001528 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001529
1530 pkt_dev->nr_labels = 0; /* turn off MPLS */
1531 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1532 } else {
1533 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1534 pkt_dev->svlan_id = 0xffff;
1535
1536 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001537 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001538 }
1539 return count;
1540 }
1541
1542 if (!strcmp(name, "vlan_p")) {
1543 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001544 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001545 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001546
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001547 i += len;
1548 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1549 pkt_dev->vlan_p = value;
1550 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1551 } else {
1552 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1553 }
1554 return count;
1555 }
1556
1557 if (!strcmp(name, "vlan_cfi")) {
1558 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001559 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001560 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001561
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001562 i += len;
1563 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1564 pkt_dev->vlan_cfi = value;
1565 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1566 } else {
1567 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1568 }
1569 return count;
1570 }
1571
1572 if (!strcmp(name, "svlan_id")) {
1573 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001574 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001575 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001576
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001577 i += len;
1578 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1579 pkt_dev->svlan_id = value; /* turn on SVLAN */
1580
1581 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001582 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001583
1584 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001585 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001586
1587 pkt_dev->nr_labels = 0; /* turn off MPLS */
1588 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1589 } else {
1590 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1591 pkt_dev->svlan_id = 0xffff;
1592
1593 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001594 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001595 }
1596 return count;
1597 }
1598
1599 if (!strcmp(name, "svlan_p")) {
1600 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001601 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001602 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001603
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001604 i += len;
1605 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1606 pkt_dev->svlan_p = value;
1607 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1608 } else {
1609 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1610 }
1611 return count;
1612 }
1613
1614 if (!strcmp(name, "svlan_cfi")) {
1615 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001616 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001617 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001618
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001619 i += len;
1620 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1621 pkt_dev->svlan_cfi = value;
1622 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1623 } else {
1624 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1625 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001626 return count;
1627 }
1628
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001629 if (!strcmp(name, "tos")) {
1630 __u32 tmp_value = 0;
1631 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001632 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001633 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001634
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001635 i += len;
1636 if (len == 2) {
1637 pkt_dev->tos = tmp_value;
1638 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1639 } else {
1640 sprintf(pg_result, "ERROR: tos must be 00-ff");
1641 }
1642 return count;
1643 }
1644
1645 if (!strcmp(name, "traffic_class")) {
1646 __u32 tmp_value = 0;
1647 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001648 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001649 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001650
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001651 i += len;
1652 if (len == 2) {
1653 pkt_dev->traffic_class = tmp_value;
1654 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1655 } else {
1656 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1657 }
1658 return count;
1659 }
1660
John Fastabend9e50e3a2010-11-16 19:12:28 +00001661 if (!strcmp(name, "skb_priority")) {
1662 len = num_arg(&user_buffer[i], 9, &value);
1663 if (len < 0)
1664 return len;
1665
1666 i += len;
1667 pkt_dev->skb_priority = value;
1668 sprintf(pg_result, "OK: skb_priority=%i",
1669 pkt_dev->skb_priority);
1670 return count;
1671 }
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1674 return -EINVAL;
1675}
1676
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001677static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001679 return single_open(file, pktgen_if_show, PDE(inode)->data);
1680}
1681
Arjan van de Ven9a321442007-02-12 00:55:35 -08001682static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001683 .owner = THIS_MODULE,
1684 .open = pktgen_if_open,
1685 .read = seq_read,
1686 .llseek = seq_lseek,
1687 .write = pktgen_if_write,
1688 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001689};
1690
1691static int pktgen_thread_show(struct seq_file *seq, void *v)
1692{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001693 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001694 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001696 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Luiz Capitulino222f1802006-03-20 22:16:13 -08001698 seq_printf(seq, "Running: ");
1699
1700 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001701 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001702 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001703 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Luiz Capitulino222f1802006-03-20 22:16:13 -08001705 seq_printf(seq, "\nStopped: ");
1706
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
1711 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001712 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001714 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Luiz Capitulino222f1802006-03-20 22:16:13 -08001716 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001721static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001722 const char __user * user_buffer,
1723 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Joe Perches8a994a72010-07-12 10:50:23 +00001725 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001726 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001727 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001729 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001732 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return -EINVAL;
1734 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001735
Paul Gortmakerd6182222010-10-18 12:14:44 +00001736 max = count;
1737 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001738 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001739 return len;
1740
Paul Gortmakerd6182222010-10-18 12:14:44 +00001741 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /* Read variable name */
1744
1745 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001746 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001747 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 memset(name, 0, sizeof(name));
1750 if (copy_from_user(name, &user_buffer[i], len))
1751 return -EFAULT;
1752 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001753
Luiz Capitulino222f1802006-03-20 22:16:13 -08001754 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001756 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001757 return len;
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 i += len;
1760
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001761 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001762 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001765 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 ret = -EINVAL;
1767 goto out;
1768 }
1769
1770 pg_result = &(t->result[0]);
1771
Luiz Capitulino222f1802006-03-20 22:16:13 -08001772 if (!strcmp(name, "add_device")) {
1773 char f[32];
1774 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001776 if (len < 0) {
1777 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto out;
1779 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001780 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return -EFAULT;
1782 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001783 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001784 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001785 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001786 ret = count;
1787 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto out;
1789 }
1790
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001792 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001793 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001794 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001795 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001797 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto out;
1799 }
1800
Luiz Capitulino222f1802006-03-20 22:16:13 -08001801 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001802 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 goto out;
1805 }
1806
1807 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001808out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return ret;
1810}
1811
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001812static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001814 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Arjan van de Ven9a321442007-02-12 00:55:35 -08001817static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001818 .owner = THIS_MODULE,
1819 .open = pktgen_thread_open,
1820 .read = seq_read,
1821 .llseek = seq_lseek,
1822 .write = pktgen_thread_write,
1823 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001824};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001827static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 struct pktgen_thread *t;
1830 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001831 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001833 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001834 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001836 if (remove) {
1837 if_lock(t);
1838 pkt_dev->removal_mark = 1;
1839 t->control |= T_REMDEV;
1840 if_unlock(t);
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 break;
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001845 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001848/*
1849 * mark a device for removal
1850 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001851static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001854 const int max_tries = 10, msec_per_try = 125;
1855 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001856
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001857 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001858 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001859
Luiz Capitulino222f1802006-03-20 22:16:13 -08001860 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001861
1862 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001863 if (pkt_dev == NULL)
1864 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001865
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001866 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001867 pr_debug("%s: waiting for %s to disappear....\n",
1868 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001869 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001870 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001871
1872 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001873 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1874 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001875 break;
1876 }
1877
1878 }
1879
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001880 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001881}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001882
Stephen Hemminger39df2322007-03-04 16:11:51 -08001883static void pktgen_change_name(struct net_device *dev)
1884{
1885 struct pktgen_thread *t;
1886
1887 list_for_each_entry(t, &pktgen_threads, th_list) {
1888 struct pktgen_dev *pkt_dev;
1889
1890 list_for_each_entry(pkt_dev, &t->if_list, list) {
1891 if (pkt_dev->odev != dev)
1892 continue;
1893
1894 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1895
Alexey Dobriyan29753152009-08-28 23:34:43 -07001896 pkt_dev->entry = proc_create_data(dev->name, 0600,
1897 pg_proc_dir,
1898 &pktgen_if_fops,
1899 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001900 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001901 pr_err("can't move proc entry for '%s'\n",
1902 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001903 break;
1904 }
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Luiz Capitulino222f1802006-03-20 22:16:13 -08001908static int pktgen_device_event(struct notifier_block *unused,
1909 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001911 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Eric Dumazetc57b5462012-05-09 13:29:51 +00001913 if (!net_eq(dev_net(dev), &init_net) || pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001914 return NOTIFY_DONE;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /* It is OK that we do not hold the group lock right now,
1917 * as we run under the RTNL lock.
1918 */
1919
1920 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001921 case NETDEV_CHANGENAME:
1922 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001926 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 return NOTIFY_DONE;
1931}
1932
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001933static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1934 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001935{
1936 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001937 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001938
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001939 for (i = 0; ifname[i] != '@'; i++) {
1940 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001941 break;
1942
1943 b[i] = ifname[i];
1944 }
1945 b[i] = 0;
1946
1947 return dev_get_by_name(&init_net, b);
1948}
1949
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951/* Associate pktgen_dev with a device. */
1952
Stephen Hemminger39df2322007-03-04 16:11:51 -08001953static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001954{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001956 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (pkt_dev->odev) {
1960 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001961 pkt_dev->odev = NULL;
1962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Robert Olssone6fce5b2008-08-07 02:23:01 -07001964 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001966 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001967 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001971 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001972 err = -EINVAL;
1973 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001974 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001975 err = -ENETDOWN;
1976 } else {
1977 pkt_dev->odev = odev;
1978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001982 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985/* Read pkt_dev from the interface and set up internal pktgen_dev
1986 * structure to have the right information to create/send packets
1987 */
1988static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1989{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001990 int ntxq;
1991
Luiz Capitulino222f1802006-03-20 22:16:13 -08001992 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001993 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001994 sprintf(pkt_dev->result,
1995 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1996 return;
1997 }
1998
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001999 /* make sure that we don't pick a non-existing transmit queue */
2000 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002001
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002002 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002003 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2004 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2005 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002006 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002007 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002008 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002009 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2010 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2011 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002012 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002013 }
2014
Luiz Capitulino222f1802006-03-20 22:16:13 -08002015 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002017 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002018 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Luiz Capitulino222f1802006-03-20 22:16:13 -08002020 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002021 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Luiz Capitulino222f1802006-03-20 22:16:13 -08002023 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002024 int i, set = 0, err = 1;
2025 struct inet6_dev *idev;
2026
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002027 if (pkt_dev->min_pkt_size == 0) {
2028 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2029 + sizeof(struct udphdr)
2030 + sizeof(struct pktgen_hdr)
2031 + pkt_dev->pkt_overhead;
2032 }
2033
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2035 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 set = 1;
2037 break;
2038 }
2039
Luiz Capitulino222f1802006-03-20 22:16:13 -08002040 if (!set) {
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * Use linklevel address if unconfigured.
2044 *
2045 * use ipv6_get_lladdr if/when it's get exported
2046 */
2047
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002048 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002049 idev = __in6_dev_get(pkt_dev->odev);
2050 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 struct inet6_ifaddr *ifp;
2052
2053 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002054 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2055 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002056 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002057 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 err = 0;
2059 break;
2060 }
2061 }
2062 read_unlock_bh(&idev->lock);
2063 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002064 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002065 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002066 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002068 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002069 if (pkt_dev->min_pkt_size == 0) {
2070 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2071 + sizeof(struct udphdr)
2072 + sizeof(struct pktgen_hdr)
2073 + pkt_dev->pkt_overhead;
2074 }
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 pkt_dev->saddr_min = 0;
2077 pkt_dev->saddr_max = 0;
2078 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002079
2080 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002083 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (in_dev) {
2085 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002086 pkt_dev->saddr_min =
2087 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 pkt_dev->saddr_max = pkt_dev->saddr_min;
2089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002092 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2094 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2095 }
2096
2097 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2098 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2099 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002100 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002101 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2102 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2103 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2104
Luiz Capitulino222f1802006-03-20 22:16:13 -08002105 pkt_dev->cur_dst_mac_offset = 0;
2106 pkt_dev->cur_src_mac_offset = 0;
2107 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2108 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2109 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2110 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 pkt_dev->nflows = 0;
2112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002115static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2116{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002117 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002118 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002119 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002120
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002121 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2122 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002123
Daniel Turull43d28b62010-06-09 22:49:57 +00002124 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002125 if (remaining <= 0) {
2126 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002127 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002128 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002129
Daniel Borkmann398f3822012-10-28 08:27:19 +00002130 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002131 if (remaining < 100000) {
2132 /* for small delays (<100us), just loop until limit is reached */
2133 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002134 end_time = ktime_get();
2135 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002136 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002137 /* see do_nanosleep */
2138 hrtimer_init_sleeper(&t, current);
2139 do {
2140 set_current_state(TASK_INTERRUPTIBLE);
2141 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2142 if (!hrtimer_active(&t.timer))
2143 t.task = NULL;
2144
2145 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002148 hrtimer_cancel(&t.timer);
2149 } while (t.task && pkt_dev->running && !signal_pending(current));
2150 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002151 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002153
2154 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002155 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002158static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2159{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002160 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002161 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2162 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2163 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2164}
2165
Stephen Hemminger648fda72009-08-27 13:55:07 +00002166static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002167{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002168 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002169}
2170
2171static inline int f_pick(struct pktgen_dev *pkt_dev)
2172{
2173 int flow = pkt_dev->curfl;
2174
2175 if (pkt_dev->flags & F_FLOW_SEQ) {
2176 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2177 /* reset time */
2178 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002179 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002180 pkt_dev->curfl += 1;
2181 if (pkt_dev->curfl >= pkt_dev->cflows)
2182 pkt_dev->curfl = 0; /*reset */
2183 }
2184 } else {
2185 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002186 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002187
Robert Olsson1211a642008-08-05 18:44:26 -07002188 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002189 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002190 pkt_dev->flows[flow].flags = 0;
2191 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002192 }
2193
2194 return pkt_dev->curfl;
2195}
2196
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002197
2198#ifdef CONFIG_XFRM
2199/* If there was already an IPSEC SA, we keep it as is, else
2200 * we go look for it ...
2201*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002202#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002203static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002204{
2205 struct xfrm_state *x = pkt_dev->flows[flow].x;
2206 if (!x) {
2207 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002208 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002209 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002210 (xfrm_address_t *)&pkt_dev->cur_saddr,
2211 AF_INET,
2212 pkt_dev->ipsmode,
2213 pkt_dev->ipsproto, 0);
2214 if (x) {
2215 pkt_dev->flows[flow].x = x;
2216 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002217 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002218 }
2219
2220 }
2221}
2222#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002223static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2224{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002225
2226 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2227 pkt_dev->cur_queue_map = smp_processor_id();
2228
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002229 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002230 __u16 t;
2231 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2232 t = random32() %
2233 (pkt_dev->queue_map_max -
2234 pkt_dev->queue_map_min + 1)
2235 + pkt_dev->queue_map_min;
2236 } else {
2237 t = pkt_dev->cur_queue_map + 1;
2238 if (t > pkt_dev->queue_map_max)
2239 t = pkt_dev->queue_map_min;
2240 }
2241 pkt_dev->cur_queue_map = t;
2242 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002243 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 -07002244}
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246/* Increment/randomize headers according to flags and current values
2247 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2248 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002249static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2250{
2251 __u32 imn;
2252 __u32 imx;
2253 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002255 if (pkt_dev->cflows)
2256 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002259 if (pkt_dev->src_mac_count > 1) {
2260 __u32 mc;
2261 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Luiz Capitulino222f1802006-03-20 22:16:13 -08002263 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002264 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002265 else {
2266 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002267 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002268 pkt_dev->src_mac_count)
2269 pkt_dev->cur_src_mac_offset = 0;
2270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Luiz Capitulino222f1802006-03-20 22:16:13 -08002272 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2273 pkt_dev->hh[11] = tmp;
2274 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2275 pkt_dev->hh[10] = tmp;
2276 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2277 pkt_dev->hh[9] = tmp;
2278 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2279 pkt_dev->hh[8] = tmp;
2280 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2281 pkt_dev->hh[7] = tmp;
2282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Luiz Capitulino222f1802006-03-20 22:16:13 -08002284 /* Deal with Destination MAC */
2285 if (pkt_dev->dst_mac_count > 1) {
2286 __u32 mc;
2287 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Luiz Capitulino222f1802006-03-20 22:16:13 -08002289 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002290 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Luiz Capitulino222f1802006-03-20 22:16:13 -08002292 else {
2293 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002294 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002295 pkt_dev->dst_mac_count) {
2296 pkt_dev->cur_dst_mac_offset = 0;
2297 }
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Luiz Capitulino222f1802006-03-20 22:16:13 -08002300 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2301 pkt_dev->hh[5] = tmp;
2302 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2303 pkt_dev->hh[4] = tmp;
2304 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2305 pkt_dev->hh[3] = tmp;
2306 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2307 pkt_dev->hh[2] = tmp;
2308 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2309 pkt_dev->hh[1] = tmp;
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002312 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002313 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002314 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002315 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2316 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002317 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002318 htonl(0x000fffff));
2319 }
2320
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002321 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002322 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002323 }
2324
2325 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002326 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002327 }
2328
Luiz Capitulino222f1802006-03-20 22:16:13 -08002329 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2330 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002331 pkt_dev->cur_udp_src = random32() %
2332 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2333 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Luiz Capitulino222f1802006-03-20 22:16:13 -08002335 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 pkt_dev->cur_udp_src++;
2337 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2338 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002339 }
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Luiz Capitulino222f1802006-03-20 22:16:13 -08002342 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2343 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002344 pkt_dev->cur_udp_dst = random32() %
2345 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2346 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002347 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002349 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002351 }
2352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 if (!(pkt_dev->flags & F_IPV6)) {
2355
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002356 imn = ntohl(pkt_dev->saddr_min);
2357 imx = ntohl(pkt_dev->saddr_max);
2358 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002360 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002361 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 else {
2363 t = ntohl(pkt_dev->cur_saddr);
2364 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002365 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369 pkt_dev->cur_saddr = htonl(t);
2370 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002371
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002372 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2374 } else {
Al Viro252e3342006-11-14 20:48:11 -08002375 imn = ntohl(pkt_dev->daddr_min);
2376 imx = ntohl(pkt_dev->daddr_max);
2377 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002379 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (pkt_dev->flags & F_IPDST_RND) {
2381
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002382 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002383 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Joe Perches21cf2252007-12-16 13:44:00 -08002385 while (ipv4_is_loopback(s) ||
2386 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002387 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002388 ipv4_is_zeronet(s) ||
2389 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002390 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002391 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
Al Viro252e3342006-11-14 20:48:11 -08002393 pkt_dev->cur_daddr = s;
2394 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 t = ntohl(pkt_dev->cur_daddr);
2396 t++;
2397 if (t > imx) {
2398 t = imn;
2399 }
2400 pkt_dev->cur_daddr = htonl(t);
2401 }
2402 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002403 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002404 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002405 pkt_dev->flows[flow].cur_daddr =
2406 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002407#ifdef CONFIG_XFRM
2408 if (pkt_dev->flags & F_IPSEC_ON)
2409 get_ipsec_sa(pkt_dev, flow);
2410#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 pkt_dev->nflows++;
2412 }
2413 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002414 } else { /* IPV6 * */
2415
Joe Perches06e30412012-10-18 17:55:31 +00002416 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 int i;
2418
2419 /* Only random destinations yet */
2420
Luiz Capitulino222f1802006-03-20 22:16:13 -08002421 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002423 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002424 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2425 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429
Luiz Capitulino222f1802006-03-20 22:16:13 -08002430 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2431 __u32 t;
2432 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002433 t = random32() %
2434 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2435 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002436 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002438 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002440 }
2441 pkt_dev->cur_pkt_size = t;
2442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002444 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 pkt_dev->flows[flow].count++;
2447}
2448
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002449
2450#ifdef CONFIG_XFRM
2451static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2452{
2453 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2454 int err = 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002455
2456 if (!x)
2457 return 0;
2458 /* XXX: we dont support tunnel mode for now until
2459 * we resolve the dst issue */
2460 if (x->props.mode != XFRM_MODE_TRANSPORT)
2461 return 0;
2462
2463 spin_lock(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002464
Herbert Xu13996372007-10-17 21:35:51 -07002465 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002466 if (err)
2467 goto error;
2468 err = x->type->output(x, skb);
2469 if (err)
2470 goto error;
2471
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002472 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002473 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002474error:
2475 spin_unlock(&x->lock);
2476 return err;
2477}
2478
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002479static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002480{
2481 if (pkt_dev->cflows) {
2482 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002483 int i;
2484 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002485 struct xfrm_state *x = pkt_dev->flows[i].x;
2486 if (x) {
2487 xfrm_state_put(x);
2488 pkt_dev->flows[i].x = NULL;
2489 }
2490 }
2491 }
2492}
2493
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002494static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002495 struct sk_buff *skb, __be16 protocol)
2496{
2497 if (pkt_dev->flags & F_IPSEC_ON) {
2498 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2499 int nhead = 0;
2500 if (x) {
2501 int ret;
2502 __u8 *eth;
2503 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002504 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002505 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2506 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002507 pr_err("Error expanding ipsec packet %d\n",
2508 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002509 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002510 }
2511 }
2512
2513 /* ipsec is not expecting ll header */
2514 skb_pull(skb, ETH_HLEN);
2515 ret = pktgen_output_ipsec(skb, pkt_dev);
2516 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002517 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002518 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002519 }
2520 /* restore ll */
2521 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2522 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002523 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002524 }
2525 }
2526 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002527err:
2528 kfree_skb(skb);
2529 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002530}
2531#endif
2532
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002533static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2534{
Eric Dumazet95c96172012-04-15 05:58:06 +00002535 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002536 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002537 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002538
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002539 mpls--;
2540 *mpls |= MPLS_STACK_BOTTOM;
2541}
2542
Al Viro0f37c602006-11-03 03:49:56 -08002543static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2544 unsigned int prio)
2545{
2546 return htons(id | (cfi << 12) | (prio << 13));
2547}
2548
Eric Dumazet26ad7872011-01-25 13:26:05 -08002549static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2550 int datalen)
2551{
2552 struct timeval timestamp;
2553 struct pktgen_hdr *pgh;
2554
2555 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2556 datalen -= sizeof(*pgh);
2557
2558 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002559 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002560 } else {
2561 int frags = pkt_dev->nfrags;
2562 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002563 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002564
2565
2566 if (frags > MAX_SKB_FRAGS)
2567 frags = MAX_SKB_FRAGS;
2568 len = datalen - frags * PAGE_SIZE;
2569 if (len > 0) {
2570 memset(skb_put(skb, len), 0, len);
2571 datalen = frags * PAGE_SIZE;
2572 }
2573
2574 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002575 frag_len = (datalen/frags) < PAGE_SIZE ?
2576 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002577 while (datalen > 0) {
2578 if (unlikely(!pkt_dev->page)) {
2579 int node = numa_node_id();
2580
2581 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2582 node = pkt_dev->node;
2583 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2584 if (!pkt_dev->page)
2585 break;
2586 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002587 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002588 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002589 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002590 /*last fragment, fill rest of data*/
2591 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002592 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2593 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002594 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002595 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2596 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2597 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2598 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002599 i++;
2600 skb_shinfo(skb)->nr_frags = i;
2601 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002602 }
2603
2604 /* Stamp the time, and sequence number,
2605 * convert them to network byte order
2606 */
2607 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2608 pgh->seq_num = htonl(pkt_dev->seq_num);
2609
2610 do_gettimeofday(&timestamp);
2611 pgh->tv_sec = htonl(timestamp.tv_sec);
2612 pgh->tv_usec = htonl(timestamp.tv_usec);
2613}
2614
Luiz Capitulino222f1802006-03-20 22:16:13 -08002615static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2616 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
2618 struct sk_buff *skb = NULL;
2619 __u8 *eth;
2620 struct udphdr *udph;
2621 int datalen, iplen;
2622 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002623 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002624 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002625 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2626 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2627 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2628 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002629 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002630
2631 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002632 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002633
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002634 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002635 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002636
Robert Olsson64053be2005-06-26 15:27:10 -07002637 /* Update any of the values, used when we're incrementing various
2638 * fields.
2639 */
2640 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002641 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002642
David S. Miller7ac54592006-01-18 14:19:10 -08002643 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002644
2645 if (pkt_dev->flags & F_NODE) {
2646 int node;
2647
2648 if (pkt_dev->node >= 0)
2649 node = pkt_dev->node;
2650 else
2651 node = numa_node_id();
2652
2653 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2654 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2655 if (likely(skb)) {
2656 skb_reserve(skb, NET_SKB_PAD);
2657 skb->dev = odev;
2658 }
2659 }
2660 else
2661 skb = __netdev_alloc_skb(odev,
2662 pkt_dev->cur_pkt_size + 64
2663 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (!skb) {
2666 sprintf(pkt_dev->result, "No memory");
2667 return NULL;
2668 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002669 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
David S. Miller7ac54592006-01-18 14:19:10 -08002671 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 /* Reserve for ethernet and IP header */
2674 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002675 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2676 if (pkt_dev->nr_labels)
2677 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002678
2679 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002680 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002681 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002682 *svlan_tci = build_tci(pkt_dev->svlan_id,
2683 pkt_dev->svlan_cfi,
2684 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002685 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002686 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002687 }
2688 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002689 *vlan_tci = build_tci(pkt_dev->vlan_id,
2690 pkt_dev->vlan_cfi,
2691 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002692 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002693 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002694 }
2695
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002696 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002697 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002698 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002699 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002700 skb->priority = pkt_dev->skb_priority;
2701
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002702 iph = ip_hdr(skb);
2703 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002706 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002708 /* Eth + IPh + UDPh + mpls */
2709 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002710 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002711 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 udph->source = htons(pkt_dev->cur_udp_src);
2715 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002716 udph->len = htons(datalen + 8); /* DATA + udphdr */
2717 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 iph->ihl = 5;
2720 iph->version = 4;
2721 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002722 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002723 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 iph->saddr = pkt_dev->cur_saddr;
2725 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002726 iph->id = htons(pkt_dev->ip_id);
2727 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 iph->frag_off = 0;
2729 iplen = 20 + 8 + datalen;
2730 iph->tot_len = htons(iplen);
2731 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002732 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002733 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002734 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002735 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 skb->dev = odev;
2737 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002738 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002739
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002740#ifdef CONFIG_XFRM
2741 if (!process_ipsec(pkt_dev, skb, protocol))
2742 return NULL;
2743#endif
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return skb;
2746}
2747
Luiz Capitulino222f1802006-03-20 22:16:13 -08002748static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2749 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
2751 struct sk_buff *skb = NULL;
2752 __u8 *eth;
2753 struct udphdr *udph;
2754 int datalen;
2755 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002756 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002757 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002758 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2759 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2760 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2761 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002762 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002763
2764 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002765 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002766
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002767 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002768 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002769
Robert Olsson64053be2005-06-26 15:27:10 -07002770 /* Update any of the values, used when we're incrementing various
2771 * fields.
2772 */
2773 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002774 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002775
Stephen Hemmingere4707572009-08-27 13:55:13 +00002776 skb = __netdev_alloc_skb(odev,
2777 pkt_dev->cur_pkt_size + 64
2778 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (!skb) {
2780 sprintf(pkt_dev->result, "No memory");
2781 return NULL;
2782 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002783 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 skb_reserve(skb, 16);
2786
2787 /* Reserve for ethernet and IP header */
2788 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002789 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2790 if (pkt_dev->nr_labels)
2791 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002792
2793 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002794 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002795 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002796 *svlan_tci = build_tci(pkt_dev->svlan_id,
2797 pkt_dev->svlan_cfi,
2798 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002799 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002800 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002801 }
2802 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002803 *vlan_tci = build_tci(pkt_dev->vlan_id,
2804 pkt_dev->vlan_cfi,
2805 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002806 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002807 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002808 }
2809
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002810 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002811 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002812 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002813 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002814 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002815 iph = ipv6_hdr(skb);
2816 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002819 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002820
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002821 /* Eth + IPh + UDPh + mpls */
2822 datalen = pkt_dev->cur_pkt_size - 14 -
2823 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002824 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002826 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002828 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830
2831 udph->source = htons(pkt_dev->cur_udp_src);
2832 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002833 udph->len = htons(datalen + sizeof(struct udphdr));
2834 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002836 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002838 if (pkt_dev->traffic_class) {
2839 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002840 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002841 }
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 iph->hop_limit = 32;
2844
2845 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2846 iph->nexthdr = IPPROTO_UDP;
2847
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002848 iph->daddr = pkt_dev->cur_in6_daddr;
2849 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002851 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002852 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002853 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 skb->dev = odev;
2855 skb->pkt_type = PACKET_HOST;
2856
Eric Dumazet26ad7872011-01-25 13:26:05 -08002857 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return skb;
2860}
2861
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002862static struct sk_buff *fill_packet(struct net_device *odev,
2863 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002865 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return fill_packet_ipv6(odev, pkt_dev);
2867 else
2868 return fill_packet_ipv4(odev, pkt_dev);
2869}
2870
Luiz Capitulino222f1802006-03-20 22:16:13 -08002871static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873 pkt_dev->seq_num = 1;
2874 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002876 pkt_dev->tx_bytes = 0;
2877 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
2880/* Set up structure for sending pkts, clear counters */
2881
2882static void pktgen_run(struct pktgen_thread *t)
2883{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002884 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 int started = 0;
2886
Joe Perchesf9467ea2010-06-21 12:29:14 +00002887 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002890 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 /*
2893 * setup odev and create initial packet.
2894 */
2895 pktgen_setup_inject(pkt_dev);
2896
Luiz Capitulino222f1802006-03-20 22:16:13 -08002897 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002899 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00002901 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002902
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002903 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 strcpy(pkt_dev->result, "Starting");
2906 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002907 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 strcpy(pkt_dev->result, "Error starting");
2909 }
2910 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002911 if (started)
2912 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
2915static void pktgen_stop_all_threads_ifs(void)
2916{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002917 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Joe Perchesf9467ea2010-06-21 12:29:14 +00002919 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002921 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002922
2923 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002924 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002925
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002926 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Stephen Hemminger648fda72009-08-27 13:55:07 +00002929static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002931 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002933 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00002934 if (pkt_dev->running)
2935 return 1;
2936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
Luiz Capitulino222f1802006-03-20 22:16:13 -08002939static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002941 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Luiz Capitulino222f1802006-03-20 22:16:13 -08002943 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Luiz Capitulino222f1802006-03-20 22:16:13 -08002945 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Luiz Capitulino222f1802006-03-20 22:16:13 -08002947 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Luiz Capitulino222f1802006-03-20 22:16:13 -08002949 if (signal_pending(current))
2950 goto signal;
2951 if_lock(t);
2952 }
2953 if_unlock(t);
2954 return 1;
2955signal:
2956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
2959static int pktgen_wait_all_threads_run(void)
2960{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002961 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002963
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002964 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002965
2966 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002968 if (sig == 0)
2969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002971
2972 if (sig == 0)
2973 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002975
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002976 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 return sig;
2978}
2979
2980static void pktgen_run_all_threads(void)
2981{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002982 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Joe Perchesf9467ea2010-06-21 12:29:14 +00002984 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002986 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002988 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002990
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002991 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002993 /* Propagate thread->control */
2994 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08002995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 pktgen_wait_all_threads_run();
2997}
2998
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08002999static void pktgen_reset_all_threads(void)
3000{
3001 struct pktgen_thread *t;
3002
Joe Perchesf9467ea2010-06-21 12:29:14 +00003003 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003004
3005 mutex_lock(&pktgen_thread_lock);
3006
3007 list_for_each_entry(t, &pktgen_threads, th_list)
3008 t->control |= (T_REMDEVALL);
3009
3010 mutex_unlock(&pktgen_thread_lock);
3011
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003012 /* Propagate thread->control */
3013 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003014
3015 pktgen_wait_all_threads_run();
3016}
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3019{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003020 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003021 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003022 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3023 pkt_dev->started_at);
3024 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Daniel Turull03a14ab2011-03-09 14:11:00 -08003026 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003027 (unsigned long long)ktime_to_us(elapsed),
3028 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3029 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003030 (unsigned long long)pkt_dev->sofar,
3031 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003033 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3034 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Luiz Capitulino222f1802006-03-20 22:16:13 -08003036 bps = pps * 8 * pkt_dev->cur_pkt_size;
3037
3038 mbps = bps;
3039 do_div(mbps, 1000000);
3040 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3041 (unsigned long long)pps,
3042 (unsigned long long)mbps,
3043 (unsigned long long)bps,
3044 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003050 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003051
Luiz Capitulino222f1802006-03-20 22:16:13 -08003052 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003053 pr_warning("interface: %s is already stopped\n",
3054 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003055 return -EINVAL;
3056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003058 kfree_skb(pkt_dev->skb);
3059 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003060 pkt_dev->stopped_at = ktime_get();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003061 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003063 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Luiz Capitulino222f1802006-03-20 22:16:13 -08003065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Luiz Capitulino222f1802006-03-20 22:16:13 -08003068static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003070 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if_lock(t);
3073
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003074 list_for_each_entry(pkt_dev, &t->if_list, list) {
3075 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003076 continue;
3077 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003078 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003079 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003080 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
3082 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003083 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
Luiz Capitulino222f1802006-03-20 22:16:13 -08003086static void pktgen_stop(struct pktgen_thread *t)
3087{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003088 struct pktgen_dev *pkt_dev;
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 Capitulino222f1802006-03-20 22:16:13 -08003092 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003094 list_for_each_entry(pkt_dev, &t->if_list, list) {
3095 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Luiz Capitulino222f1802006-03-20 22:16:13 -08003098 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003101/*
3102 * one of our devices needs to be removed - find it
3103 * and remove it
3104 */
3105static void pktgen_rem_one_if(struct pktgen_thread *t)
3106{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003107 struct list_head *q, *n;
3108 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003109
Joe Perchesf9467ea2010-06-21 12:29:14 +00003110 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003111
3112 if_lock(t);
3113
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003114 list_for_each_safe(q, n, &t->if_list) {
3115 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003116
Luiz Capitulino222f1802006-03-20 22:16:13 -08003117 if (!cur->removal_mark)
3118 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003119
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003120 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003121 cur->skb = NULL;
3122
3123 pktgen_remove_device(t, cur);
3124
3125 break;
3126 }
3127
3128 if_unlock(t);
3129}
3130
Luiz Capitulino222f1802006-03-20 22:16:13 -08003131static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003133 struct list_head *q, *n;
3134 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003135
Joe Perchesf9467ea2010-06-21 12:29:14 +00003136 func_enter();
3137
Luiz Capitulino222f1802006-03-20 22:16:13 -08003138 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003139
Luiz Capitulino222f1802006-03-20 22:16:13 -08003140 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003142 list_for_each_safe(q, n, &t->if_list) {
3143 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003144
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003145 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003146 cur->skb = NULL;
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 pktgen_remove_device(t, cur);
3149 }
3150
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
David S. Milleree74baa2007-01-01 20:51:53 -08003158 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
3161
Stephen Hemmingeref879792009-09-22 19:41:43 +00003162static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003163{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003164 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003165 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003166 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003167}
3168
Stephen Hemmingeref879792009-09-22 19:41:43 +00003169static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3170{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003171 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003172
3173 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3174 if (signal_pending(current))
3175 break;
3176
3177 if (need_resched())
3178 pktgen_resched(pkt_dev);
3179 else
3180 cpu_relax();
3181 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003182 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003183}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003184
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003185static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
Stephen Hemminger00829822008-11-20 20:14:53 -08003187 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003188 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003189 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003190 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003191 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 int ret;
3193
Stephen Hemmingeref879792009-09-22 19:41:43 +00003194 /* If device is offline, then don't send */
3195 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3196 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003199
Stephen Hemmingeref879792009-09-22 19:41:43 +00003200 /* This is max DELAY, this has special meaning of
3201 * "never transmit"
3202 */
3203 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003204 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003205 return;
3206 }
3207
3208 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003209 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3210 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3211 /* build a new pkt */
3212 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003213
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003214 pkt_dev->skb = fill_packet(odev, pkt_dev);
3215 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003216 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003217 schedule();
3218 pkt_dev->clone_count--; /* back out increment, OOM */
3219 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003221 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003222 pkt_dev->allocated_skbs++;
3223 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003225
Stephen Hemmingeref879792009-09-22 19:41:43 +00003226 if (pkt_dev->delay && pkt_dev->last_ok)
3227 spin(pkt_dev, pkt_dev->next_tx);
3228
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003229 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3230 txq = netdev_get_tx_queue(odev, queue_map);
3231
3232 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003233
Tom Herbert734664982011-11-28 16:32:44 +00003234 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003235 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003236 pkt_dev->last_ok = 0;
3237 goto unlock;
3238 }
3239 atomic_inc(&(pkt_dev->skb->users));
3240 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Stephen Hemmingeref879792009-09-22 19:41:43 +00003242 switch (ret) {
3243 case NETDEV_TX_OK:
3244 txq_trans_update(txq);
3245 pkt_dev->last_ok = 1;
3246 pkt_dev->sofar++;
3247 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003248 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003249 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003250 case NET_XMIT_DROP:
3251 case NET_XMIT_CN:
3252 case NET_XMIT_POLICED:
3253 /* skb has been consumed */
3254 pkt_dev->errors++;
3255 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003256 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003257 net_info_ratelimited("%s xmit error: %d\n",
3258 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003259 pkt_dev->errors++;
3260 /* fallthru */
3261 case NETDEV_TX_LOCKED:
3262 case NETDEV_TX_BUSY:
3263 /* Retry it next time */
3264 atomic_dec(&(pkt_dev->skb->users));
3265 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003266 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003267unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003268 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 /* If pkt_dev->count is zero, then run forever */
3271 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003272 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 /* Done with this */
3275 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003276 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003277}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003279/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 * Main loop of the thread goes here
3281 */
3282
David S. Milleree74baa2007-01-01 20:51:53 -08003283static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
3285 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003286 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003287 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
David S. Milleree74baa2007-01-01 20:51:53 -08003290 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003293 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Joe Perchesf9467ea2010-06-21 12:29:14 +00003295 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
David S. Milleree74baa2007-01-01 20:51:53 -08003297 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003298
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003299 set_freezable();
3300
David S. Milleree74baa2007-01-01 20:51:53 -08003301 while (!kthread_should_stop()) {
3302 pkt_dev = next_to_run(t);
3303
Stephen Hemmingeref879792009-09-22 19:41:43 +00003304 if (unlikely(!pkt_dev && t->control == 0)) {
Eric Dumazet551eaff2010-11-21 10:26:44 -08003305 if (pktgen_exiting)
3306 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003307 wait_event_interruptible_timeout(t->queue,
3308 t->control != 0,
3309 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003310 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003311 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003312 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 __set_current_state(TASK_RUNNING);
3315
Stephen Hemmingeref879792009-09-22 19:41:43 +00003316 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 pktgen_xmit(pkt_dev);
3318
Stephen Hemmingeref879792009-09-22 19:41:43 +00003319 if (need_resched())
3320 pktgen_resched(pkt_dev);
3321 else
3322 cpu_relax();
3323 }
3324
Luiz Capitulino222f1802006-03-20 22:16:13 -08003325 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 pktgen_stop(t);
3327 t->control &= ~(T_STOP);
3328 }
3329
Luiz Capitulino222f1802006-03-20 22:16:13 -08003330 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 pktgen_run(t);
3332 t->control &= ~(T_RUN);
3333 }
3334
Luiz Capitulino222f1802006-03-20 22:16:13 -08003335 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003337 t->control &= ~(T_REMDEVALL);
3338 }
3339
Luiz Capitulino222f1802006-03-20 22:16:13 -08003340 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003341 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 t->control &= ~(T_REMDEV);
3343 }
3344
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003345 try_to_freeze();
3346
David S. Milleree74baa2007-01-01 20:51:53 -08003347 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Joe Perchesf9467ea2010-06-21 12:29:14 +00003350 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003351 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Joe Perchesf9467ea2010-06-21 12:29:14 +00003353 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003354 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Joe Perchesf9467ea2010-06-21 12:29:14 +00003356 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003357 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003358
Eric Dumazet551eaff2010-11-21 10:26:44 -08003359 /* Wait for kthread_stop */
3360 while (!kthread_should_stop()) {
3361 set_current_state(TASK_INTERRUPTIBLE);
3362 schedule();
3363 }
3364 __set_current_state(TASK_RUNNING);
3365
David S. Milleree74baa2007-01-01 20:51:53 -08003366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
Luiz Capitulino222f1802006-03-20 22:16:13 -08003369static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003370 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003372 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003373 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Eric Dumazet3e984842009-11-24 14:50:53 -08003375 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003376 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003377 if (strncmp(p->odevname, ifname, len) == 0) {
3378 if (p->odevname[len]) {
3379 if (exact || p->odevname[len] != '@')
3380 continue;
3381 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003382 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003383 break;
3384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Luiz Capitulino222f1802006-03-20 22:16:13 -08003386 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003387 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003388 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003391/*
3392 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 */
3394
Luiz Capitulino222f1802006-03-20 22:16:13 -08003395static int add_dev_to_thread(struct pktgen_thread *t,
3396 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Luiz Capitulino222f1802006-03-20 22:16:13 -08003400 if_lock(t);
3401
3402 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003403 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003404 rv = -EBUSY;
3405 goto out;
3406 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003407
3408 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003409 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 pkt_dev->running = 0;
3411
Luiz Capitulino222f1802006-03-20 22:16:13 -08003412out:
3413 if_unlock(t);
3414 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
3417/* Called under thread lock */
3418
Luiz Capitulino222f1802006-03-20 22:16:13 -08003419static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003421 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003422 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003423 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 /* We don't allow a device to be on several threads */
3426
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003427 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3428 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003429 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003430 return -EBUSY;
3431 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003432
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003433 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003434 if (!pkt_dev)
3435 return -ENOMEM;
3436
Eric Dumazet593f63b2009-11-23 01:44:37 +00003437 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003438 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003439 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003440 if (pkt_dev->flows == NULL) {
3441 kfree(pkt_dev);
3442 return -ENOMEM;
3443 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003444
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003445 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003446 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003447 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003448 pkt_dev->count = pg_count_d;
3449 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003450 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003451 pkt_dev->udp_src_max = 9;
3452 pkt_dev->udp_dst_min = 9;
3453 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003454 pkt_dev->vlan_p = 0;
3455 pkt_dev->vlan_cfi = 0;
3456 pkt_dev->vlan_id = 0xffff;
3457 pkt_dev->svlan_p = 0;
3458 pkt_dev->svlan_cfi = 0;
3459 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003460 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003461
Stephen Hemminger39df2322007-03-04 16:11:51 -08003462 err = pktgen_setup_dev(pkt_dev, ifname);
3463 if (err)
3464 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003465 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3466 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003467
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003468 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3469 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003470 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003471 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003472 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003473 err = -EINVAL;
3474 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003475 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003476#ifdef CONFIG_XFRM
3477 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3478 pkt_dev->ipsproto = IPPROTO_ESP;
3479#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003480
3481 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003482out2:
3483 dev_put(pkt_dev->odev);
3484out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003485#ifdef CONFIG_XFRM
3486 free_SAs(pkt_dev);
3487#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003488 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003489 kfree(pkt_dev);
3490 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
David S. Milleree74baa2007-01-01 20:51:53 -08003493static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003495 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003496 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003497 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003499 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3500 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003501 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003502 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003503 return -ENOMEM;
3504 }
3505
Luiz Capitulino222f1802006-03-20 22:16:13 -08003506 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003508
David S. Milleree74baa2007-01-01 20:51:53 -08003509 INIT_LIST_HEAD(&t->if_list);
3510
3511 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003512 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003513
Eric Dumazet94dcf292011-03-22 16:30:45 -07003514 p = kthread_create_on_node(pktgen_thread_worker,
3515 t,
3516 cpu_to_node(cpu),
3517 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003518 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003519 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003520 list_del(&t->th_list);
3521 kfree(t);
3522 return PTR_ERR(p);
3523 }
3524 kthread_bind(p, cpu);
3525 t->tsk = p;
3526
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003527 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3528 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003529 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003530 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003531 PG_PROC_DIR, t->tsk->comm);
3532 kthread_stop(p);
3533 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003534 kfree(t);
3535 return -EINVAL;
3536 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003537
David S. Milleree74baa2007-01-01 20:51:53 -08003538 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003539 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 return 0;
3542}
3543
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003544/*
3545 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003547static void _rem_dev_from_if_list(struct pktgen_thread *t,
3548 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003550 struct list_head *q, *n;
3551 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003553 list_for_each_safe(q, n, &t->if_list) {
3554 p = list_entry(q, struct pktgen_dev, list);
3555 if (p == pkt_dev)
3556 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 }
3558}
3559
Luiz Capitulino222f1802006-03-20 22:16:13 -08003560static int pktgen_remove_device(struct pktgen_thread *t,
3561 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
3563
Joe Perchesf9467ea2010-06-21 12:29:14 +00003564 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003567 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003568 pktgen_stop_device(pkt_dev);
3569 }
3570
3571 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
3573 if (pkt_dev->odev) {
3574 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003575 pkt_dev->odev = NULL;
3576 }
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 /* And update the thread if_list */
3579
3580 _rem_dev_from_if_list(t, pkt_dev);
3581
Stephen Hemminger39df2322007-03-04 16:11:51 -08003582 if (pkt_dev->entry)
3583 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003585#ifdef CONFIG_XFRM
3586 free_SAs(pkt_dev);
3587#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003588 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003589 if (pkt_dev->page)
3590 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Luiz Capitulino222f1802006-03-20 22:16:13 -08003595static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
3597 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003598 struct proc_dir_entry *pe;
WANG Congce14f892011-05-22 00:52:08 +00003599 int ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003600
Joe Perchesf9467ea2010-06-21 12:29:14 +00003601 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003603 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003604 if (!pg_proc_dir)
3605 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Wang Chen25296d52008-02-28 14:11:49 -08003607 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003608 if (pe == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003609 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
WANG Congce14f892011-05-22 00:52:08 +00003610 ret = -EINVAL;
3611 goto remove_dir;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615
John Hawkes670c02c2005-10-13 09:30:31 -07003616 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003617 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
David S. Milleree74baa2007-01-01 20:51:53 -08003619 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003620 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00003621 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3622 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003623 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003624
3625 if (list_empty(&pktgen_threads)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003626 pr_err("ERROR: Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003627 ret = -ENODEV;
3628 goto unregister;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003629 }
3630
Luiz Capitulino222f1802006-03-20 22:16:13 -08003631 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003632
3633 unregister:
3634 unregister_netdevice_notifier(&pktgen_notifier_block);
3635 remove_proc_entry(PGCTRL, pg_proc_dir);
3636 remove_dir:
3637 proc_net_remove(&init_net, PG_PROC_DIR);
3638 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
3641static void __exit pg_cleanup(void)
3642{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003643 struct pktgen_thread *t;
3644 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003645 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647 /* Stop all interfaces & threads */
Eric Dumazet551eaff2010-11-21 10:26:44 -08003648 pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Eric Dumazetc57b5462012-05-09 13:29:51 +00003650 mutex_lock(&pktgen_thread_lock);
Eric Dumazetd4b11332012-05-17 23:52:26 +00003651 list_splice_init(&pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003652 mutex_unlock(&pktgen_thread_lock);
3653
3654 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003655 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003656 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003657 kthread_stop(t->tsk);
3658 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Luiz Capitulino222f1802006-03-20 22:16:13 -08003661 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 unregister_netdevice_notifier(&pktgen_notifier_block);
3663
Luiz Capitulino222f1802006-03-20 22:16:13 -08003664 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003665 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003666 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667}
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669module_init(pg_init);
3670module_exit(pg_cleanup);
3671
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003672MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673MODULE_DESCRIPTION("Packet Generator tool");
3674MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003675MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003677MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003679MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003681MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003683MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");