blob: d1dc14c2aac4ee00fa6dab706131bbe41d3790f7 [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 Viro252e33462006-11-14 20:48:11 -0800223 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800224 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700225#ifdef CONFIG_XFRM
226 struct xfrm_state *x;
227#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700228 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700231/* flow flag bits */
232#define F_INIT (1<<0) /* flow has been initialized */
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
236 * Try to keep frequent/infrequent used vars. separated.
237 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800238 struct proc_dir_entry *entry; /* proc file */
239 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000240 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000242 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800243
244 /* If min != max, then we will either do a linear iteration, or
245 * we will do a random selection from within the range.
246 */
247 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800248 int removal_mark; /* non-zero => the device is marked for
249 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000251 int min_pkt_size;
252 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700253 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800254 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800255 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000256 u64 delay; /* nano-seconds */
257
Luiz Capitulino222f1802006-03-20 22:16:13 -0800258 __u64 count; /* Default No packets to send */
259 __u64 sofar; /* How many pkts we've sent so far */
260 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800261 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Luiz Capitulino222f1802006-03-20 22:16:13 -0800263 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Luiz Capitulino222f1802006-03-20 22:16:13 -0800265 __u64 allocated_skbs;
266 __u32 clone_count;
267 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000268 * Or a failed transmit of some sort?
269 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800270 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000271 ktime_t next_tx;
272 ktime_t started_at;
273 ktime_t stopped_at;
274 u64 idle_acc; /* nano-seconds */
275
Luiz Capitulino222f1802006-03-20 22:16:13 -0800276 __u32 seq_num;
277
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000278 int clone_skb; /*
279 * Use multiple SKBs during packet gen.
280 * If this number is greater than 1, then
281 * that many copies of the same packet will be
282 * sent before a new packet is allocated.
283 * If you want to send 1024 identical packets
284 * before creating a new packet,
285 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800286 */
287
288 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
289 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
290 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
291 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
292
293 struct in6_addr in6_saddr;
294 struct in6_addr in6_daddr;
295 struct in6_addr cur_in6_daddr;
296 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800298 struct in6_addr min_in6_daddr;
299 struct in6_addr max_in6_daddr;
300 struct in6_addr min_in6_saddr;
301 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Luiz Capitulino222f1802006-03-20 22:16:13 -0800303 /* If we're doing ranges, random or incremental, then this
304 * defines the min/max for those ranges.
305 */
Al Viro252e33462006-11-14 20:48:11 -0800306 __be32 saddr_min; /* inclusive, source IP address */
307 __be32 saddr_max; /* exclusive, source IP address */
308 __be32 daddr_min; /* inclusive, dest IP address */
309 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Luiz Capitulino222f1802006-03-20 22:16:13 -0800311 __u16 udp_src_min; /* inclusive, source UDP port */
312 __u16 udp_src_max; /* exclusive, source UDP port */
313 __u16 udp_dst_min; /* inclusive, dest UDP port */
314 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700316 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000317 __u8 tos; /* six MSB of (former) IPv4 TOS
318 are for dscp codepoint */
319 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
320 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700321
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800322 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000323 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800324 __be32 labels[MAX_MPLS_LABELS];
325
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700326 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
327 __u8 vlan_p;
328 __u8 vlan_cfi;
329 __u16 vlan_id; /* 0xffff means no vlan tag */
330
331 __u8 svlan_p;
332 __u8 svlan_cfi;
333 __u16 svlan_id; /* 0xffff means no svlan tag */
334
Luiz Capitulino222f1802006-03-20 22:16:13 -0800335 __u32 src_mac_count; /* How many MACs to iterate through */
336 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Luiz Capitulino222f1802006-03-20 22:16:13 -0800338 unsigned char dst_mac[ETH_ALEN];
339 unsigned char src_mac[ETH_ALEN];
340
341 __u32 cur_dst_mac_offset;
342 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800343 __be32 cur_saddr;
344 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700345 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800346 __u16 cur_udp_dst;
347 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700348 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800349 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800350 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800351
352 __u8 hh[14];
353 /* = {
354 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
355
356 We fill in SRC address later
357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358 0x08, 0x00
359 };
360 */
361 __u16 pad; /* pad out the hh struct to an even 16 bytes */
362
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000363 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800364 * are transmitting the same one multiple times
365 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000366 struct net_device *odev; /* The out-going device.
367 * Note that the device should have it's
368 * pg_info pointer pointing back to this
369 * device.
370 * Set when the user specifies the out-going
371 * device name (not when the inject is
372 * started as it used to do.)
373 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000374 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000376 unsigned int cflows; /* Concurrent flows (config) */
377 unsigned int lflow; /* Flow length (config) */
378 unsigned int nflows; /* accumulated flows (stats) */
379 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700380
381 u16 queue_map_min;
382 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000383 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000384 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700385
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700386#ifdef CONFIG_XFRM
387 __u8 ipsmode; /* IPSEC mode (config) */
388 __u8 ipsproto; /* IPSEC type (config) */
389#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800390 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391};
392
393struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800394 __be32 pgh_magic;
395 __be32 seq_num;
396 __be32 tv_sec;
397 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
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 Hemmingerfd29cf72009-08-27 13:55:16 +0000422static inline ktime_t ktime_now(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000424 struct timespec ts;
425 ktime_get_ts(&ts);
426
427 return timespec_to_ktime(ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000430/* This works even if 32 bit because of careful byte order choice */
431static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000433 return cmp1.tv64 < cmp2.tv64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000436static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000437 "Packet Generator for packet performance testing. "
438 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Luiz Capitulino222f1802006-03-20 22:16:13 -0800440static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
441static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
442static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800443 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
445static void pktgen_run_all_threads(void);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800446static void pktgen_reset_all_threads(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static void pktgen_stop_all_threads_ifs(void);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000448
Luiz Capitulino222f1802006-03-20 22:16:13 -0800449static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000453static int pg_count_d __read_mostly = 1000;
454static int pg_delay_d __read_mostly;
455static int pg_clone_skb_d __read_mostly;
456static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Luiz Capitulino222fa072006-03-20 22:24:27 -0800458static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800459static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static struct notifier_block pktgen_notifier_block = {
462 .notifier_call = pktgen_device_event,
463};
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900466 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *
468 */
469
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700470static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800471{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000472 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000476static ssize_t pgctrl_write(struct file *file, const char __user *buf,
477 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700480 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Luiz Capitulino222f1802006-03-20 22:16:13 -0800482 if (!capable(CAP_NET_ADMIN)) {
483 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700487 if (count > sizeof(data))
488 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700491 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700492 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800493 }
494 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Luiz Capitulino222f1802006-03-20 22:16:13 -0800496 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 pktgen_stop_all_threads_ifs();
498
Luiz Capitulino222f1802006-03-20 22:16:13 -0800499 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 pktgen_run_all_threads();
501
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800502 else if (!strcmp(data, "reset"))
503 pktgen_reset_all_threads();
504
Luiz Capitulino222f1802006-03-20 22:16:13 -0800505 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000506 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 err = count;
509
Luiz Capitulino222f1802006-03-20 22:16:13 -0800510out:
511 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700514static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700516 return single_open(file, pgctrl_show, PDE(inode)->data);
517}
518
Arjan van de Ven9a321442007-02-12 00:55:35 -0800519static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800520 .owner = THIS_MODULE,
521 .open = pgctrl_open,
522 .read = seq_read,
523 .llseek = seq_lseek,
524 .write = pgctrl_write,
525 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700526};
527
528static int pktgen_if_show(struct seq_file *seq, void *v)
529{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000530 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000531 ktime_t stopped;
532 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Luiz Capitulino222f1802006-03-20 22:16:13 -0800534 seq_printf(seq,
535 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
536 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
537 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Luiz Capitulino222f1802006-03-20 22:16:13 -0800539 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000540 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
541 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000542 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Luiz Capitulino222f1802006-03-20 22:16:13 -0800544 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
545 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Robert Olsson45b270f2007-08-28 15:45:55 -0700547 seq_printf(seq,
548 " queue_map_min: %u queue_map_max: %u\n",
549 pkt_dev->queue_map_min,
550 pkt_dev->queue_map_max);
551
John Fastabend9e50e3a2010-11-16 19:12:28 +0000552 if (pkt_dev->skb_priority)
553 seq_printf(seq, " skb_priority: %u\n",
554 pkt_dev->skb_priority);
555
Luiz Capitulino222f1802006-03-20 22:16:13 -0800556 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800557 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000558 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
559 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
560 &pkt_dev->in6_saddr,
561 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
562 &pkt_dev->in6_daddr,
563 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000564 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800565 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000566 " dst_min: %s dst_max: %s\n",
567 pkt_dev->dst_min, pkt_dev->dst_max);
568 seq_printf(seq,
569 " src_min: %s src_max: %s\n",
570 pkt_dev->src_min, pkt_dev->src_max);
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700573 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Johannes Berge1749612008-10-27 15:59:26 -0700575 seq_printf(seq, "%pM ",
576 is_zero_ether_addr(pkt_dev->src_mac) ?
577 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Luiz Capitulino222f1802006-03-20 22:16:13 -0800579 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700580 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Luiz Capitulino222f1802006-03-20 22:16:13 -0800582 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000583 " udp_src_min: %d udp_src_max: %d"
584 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800585 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
586 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Luiz Capitulino222f1802006-03-20 22:16:13 -0800588 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800589 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700590 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800592 if (pkt_dev->nr_labels) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000593 unsigned int i;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800594 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700595 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800596 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
597 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
598 }
599
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000600 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700601 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000602 pkt_dev->vlan_id, pkt_dev->vlan_p,
603 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700604
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000605 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700606 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000607 pkt_dev->svlan_id, pkt_dev->svlan_p,
608 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700609
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000610 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700611 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700612
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000613 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700614 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700615
Robert Olssone99b99b2010-03-18 22:44:30 +0000616 if (pkt_dev->node >= 0)
617 seq_printf(seq, " node: %d\n", pkt_dev->node);
618
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800619 seq_printf(seq, " Flags: ");
620
Luiz Capitulino222f1802006-03-20 22:16:13 -0800621 if (pkt_dev->flags & F_IPV6)
622 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Luiz Capitulino222f1802006-03-20 22:16:13 -0800624 if (pkt_dev->flags & F_IPSRC_RND)
625 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Luiz Capitulino222f1802006-03-20 22:16:13 -0800627 if (pkt_dev->flags & F_IPDST_RND)
628 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Luiz Capitulino222f1802006-03-20 22:16:13 -0800630 if (pkt_dev->flags & F_TXSIZE_RND)
631 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Luiz Capitulino222f1802006-03-20 22:16:13 -0800633 if (pkt_dev->flags & F_UDPSRC_RND)
634 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Luiz Capitulino222f1802006-03-20 22:16:13 -0800636 if (pkt_dev->flags & F_UDPDST_RND)
637 seq_printf(seq, "UDPDST_RND ");
638
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800639 if (pkt_dev->flags & F_MPLS_RND)
640 seq_printf(seq, "MPLS_RND ");
641
Robert Olsson45b270f2007-08-28 15:45:55 -0700642 if (pkt_dev->flags & F_QUEUE_MAP_RND)
643 seq_printf(seq, "QUEUE_MAP_RND ");
644
Robert Olssone6fce5b2008-08-07 02:23:01 -0700645 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
646 seq_printf(seq, "QUEUE_MAP_CPU ");
647
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700648 if (pkt_dev->cflows) {
649 if (pkt_dev->flags & F_FLOW_SEQ)
650 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
651 else
652 seq_printf(seq, "FLOW_RND ");
653 }
654
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700655#ifdef CONFIG_XFRM
656 if (pkt_dev->flags & F_IPSEC_ON)
657 seq_printf(seq, "IPSEC ");
658#endif
659
Luiz Capitulino222f1802006-03-20 22:16:13 -0800660 if (pkt_dev->flags & F_MACSRC_RND)
661 seq_printf(seq, "MACSRC_RND ");
662
663 if (pkt_dev->flags & F_MACDST_RND)
664 seq_printf(seq, "MACDST_RND ");
665
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700666 if (pkt_dev->flags & F_VID_RND)
667 seq_printf(seq, "VID_RND ");
668
669 if (pkt_dev->flags & F_SVID_RND)
670 seq_printf(seq, "SVID_RND ");
671
Robert Olssone99b99b2010-03-18 22:44:30 +0000672 if (pkt_dev->flags & F_NODE)
673 seq_printf(seq, "NODE_ALLOC ");
674
Luiz Capitulino222f1802006-03-20 22:16:13 -0800675 seq_puts(seq, "\n");
676
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000677 /* not really stopped, more like last-running-at */
678 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
679 idle = pkt_dev->idle_acc;
680 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800681
682 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000683 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800684 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000685 (unsigned long long)pkt_dev->errors);
686
687 seq_printf(seq,
688 " started: %lluus stopped: %lluus idle: %lluus\n",
689 (unsigned long long) ktime_to_us(pkt_dev->started_at),
690 (unsigned long long) ktime_to_us(stopped),
691 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800692
693 seq_printf(seq,
694 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700695 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
696 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Luiz Capitulino222f1802006-03-20 22:16:13 -0800698 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000699 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
700 &pkt_dev->cur_in6_saddr,
701 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800702 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000703 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
704 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Luiz Capitulino222f1802006-03-20 22:16:13 -0800706 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700707 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Robert Olsson45b270f2007-08-28 15:45:55 -0700709 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
710
Luiz Capitulino222f1802006-03-20 22:16:13 -0800711 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800714 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800716 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800721
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000722static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
723 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800724{
725 int i = 0;
726 *num = 0;
727
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700728 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000729 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800730 char c;
731 *num <<= 4;
732 if (get_user(c, &user_buffer[i]))
733 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000734 value = hex_to_bin(c);
735 if (value >= 0)
736 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800737 else
738 break;
739 }
740 return i;
741}
742
Luiz Capitulino222f1802006-03-20 22:16:13 -0800743static int count_trail_chars(const char __user * user_buffer,
744 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 int i;
747
748 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800749 char c;
750 if (get_user(c, &user_buffer[i]))
751 return -EFAULT;
752 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 case '\"':
754 case '\n':
755 case '\r':
756 case '\t':
757 case ' ':
758 case '=':
759 break;
760 default:
761 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764done:
765 return i;
766}
767
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000768static long num_arg(const char __user *user_buffer, unsigned long maxlen,
769 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000771 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800773
Paul Gortmakerd6182222010-10-18 12:14:44 +0000774 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800775 char c;
776 if (get_user(c, &user_buffer[i]))
777 return -EFAULT;
778 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800780 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 } else
782 break;
783 }
784 return i;
785}
786
Luiz Capitulino222f1802006-03-20 22:16:13 -0800787static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000789 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Paul Gortmakerd6182222010-10-18 12:14:44 +0000791 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800792 char c;
793 if (get_user(c, &user_buffer[i]))
794 return -EFAULT;
795 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 case '\"':
797 case '\n':
798 case '\r':
799 case '\t':
800 case ' ':
801 goto done_str;
802 break;
803 default:
804 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return i;
809}
810
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800811static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
812{
Eric Dumazet95c96172012-04-15 05:58:06 +0000813 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800814 char c;
815 ssize_t i = 0;
816 int len;
817
818 pkt_dev->nr_labels = 0;
819 do {
820 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700821 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800822 if (len <= 0)
823 return len;
824 pkt_dev->labels[n] = htonl(tmp);
825 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
826 pkt_dev->flags |= F_MPLS_RND;
827 i += len;
828 if (get_user(c, &buffer[i]))
829 return -EFAULT;
830 i++;
831 n++;
832 if (n >= MAX_MPLS_LABELS)
833 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700834 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800835
836 pkt_dev->nr_labels = n;
837 return i;
838}
839
Luiz Capitulino222f1802006-03-20 22:16:13 -0800840static ssize_t pktgen_if_write(struct file *file,
841 const char __user * user_buffer, size_t count,
842 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Joe Perches8a994a72010-07-12 10:50:23 +0000844 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800845 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000846 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 char name[16], valstr[32];
848 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800849 char *pg_result = NULL;
850 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800852
853 pg_result = &(pkt_dev->result[0]);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000856 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -EINVAL;
858 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800859
Paul Gortmakerd6182222010-10-18 12:14:44 +0000860 max = count;
861 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800862 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000863 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800864 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000866 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* Read variable name */
869
870 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000871 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800872 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -EFAULT;
877 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800878
879 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800881 if (len < 0)
882 return len;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 i += len;
885
886 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000887 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700888 char tb[copy + 1];
889 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700891 tb[copy] = 0;
Joe Perchesf342cda2012-05-16 17:50:41 +0000892 pr_debug("%s,%lu buffer -:%s:-\n",
893 name, (unsigned long)count, tb);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (!strcmp(name, "min_pkt_size")) {
897 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000898 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800899 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800902 if (value < 14 + 20 + 8)
903 value = 14 + 20 + 8;
904 if (value != pkt_dev->min_pkt_size) {
905 pkt_dev->min_pkt_size = value;
906 pkt_dev->cur_pkt_size = value;
907 }
908 sprintf(pg_result, "OK: min_pkt_size=%u",
909 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return count;
911 }
912
Luiz Capitulino222f1802006-03-20 22:16:13 -0800913 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000915 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800916 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800919 if (value < 14 + 20 + 8)
920 value = 14 + 20 + 8;
921 if (value != pkt_dev->max_pkt_size) {
922 pkt_dev->max_pkt_size = value;
923 pkt_dev->cur_pkt_size = value;
924 }
925 sprintf(pg_result, "OK: max_pkt_size=%u",
926 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return count;
928 }
929
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (!strcmp(name, "pkt_size")) {
933 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000934 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800935 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800938 if (value < 14 + 20 + 8)
939 value = 14 + 20 + 8;
940 if (value != pkt_dev->min_pkt_size) {
941 pkt_dev->min_pkt_size = value;
942 pkt_dev->max_pkt_size = value;
943 pkt_dev->cur_pkt_size = value;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
946 return count;
947 }
948
Luiz Capitulino222f1802006-03-20 22:16:13 -0800949 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000951 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800952 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800955 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 sprintf(pg_result, "OK: debug=%u", debug);
957 return count;
958 }
959
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000962 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800963 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 i += len;
966 pkt_dev->nfrags = value;
967 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
968 return count;
969 }
970 if (!strcmp(name, "delay")) {
971 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000972 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800973 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000976 if (value == 0x7FFFFFFF)
977 pkt_dev->delay = ULLONG_MAX;
978 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000979 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000980
981 sprintf(pg_result, "OK: delay=%llu",
982 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return count;
984 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000985 if (!strcmp(name, "rate")) {
986 len = num_arg(&user_buffer[i], 10, &value);
987 if (len < 0)
988 return len;
989
990 i += len;
991 if (!value)
992 return len;
993 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
994 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000995 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000996
997 sprintf(pg_result, "OK: rate=%lu", value);
998 return count;
999 }
1000 if (!strcmp(name, "ratep")) {
1001 len = num_arg(&user_buffer[i], 10, &value);
1002 if (len < 0)
1003 return len;
1004
1005 i += len;
1006 if (!value)
1007 return len;
1008 pkt_dev->delay = NSEC_PER_SEC/value;
1009 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001010 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001011
1012 sprintf(pg_result, "OK: rate=%lu", value);
1013 return count;
1014 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001015 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001017 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001018 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001021 if (value != pkt_dev->udp_src_min) {
1022 pkt_dev->udp_src_min = value;
1023 pkt_dev->cur_udp_src = value;
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1026 return count;
1027 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001028 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001030 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001031 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001034 if (value != pkt_dev->udp_dst_min) {
1035 pkt_dev->udp_dst_min = value;
1036 pkt_dev->cur_udp_dst = value;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1039 return count;
1040 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001041 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001043 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001044 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001047 if (value != pkt_dev->udp_src_max) {
1048 pkt_dev->udp_src_max = value;
1049 pkt_dev->cur_udp_src = value;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1052 return count;
1053 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001056 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001057 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001060 if (value != pkt_dev->udp_dst_max) {
1061 pkt_dev->udp_dst_max = value;
1062 pkt_dev->cur_udp_dst = value;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1065 return count;
1066 }
1067 if (!strcmp(name, "clone_skb")) {
1068 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001069 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001070 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001071 if ((value > 0) &&
1072 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1073 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001075 pkt_dev->clone_skb = value;
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1078 return count;
1079 }
1080 if (!strcmp(name, "count")) {
1081 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001082 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 i += len;
1086 pkt_dev->count = value;
1087 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001088 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return count;
1090 }
1091 if (!strcmp(name, "src_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->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001098 pkt_dev->src_mac_count = value;
1099 pkt_dev->cur_src_mac_offset = 0;
1100 }
1101 sprintf(pg_result, "OK: src_mac_count=%d",
1102 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return count;
1104 }
1105 if (!strcmp(name, "dst_mac_count")) {
1106 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001107 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001108 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 i += len;
1111 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001112 pkt_dev->dst_mac_count = value;
1113 pkt_dev->cur_dst_mac_offset = 0;
1114 }
1115 sprintf(pg_result, "OK: dst_mac_count=%d",
1116 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return count;
1118 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001119 if (!strcmp(name, "node")) {
1120 len = num_arg(&user_buffer[i], 10, &value);
1121 if (len < 0)
1122 return len;
1123
1124 i += len;
1125
1126 if (node_possible(value)) {
1127 pkt_dev->node = value;
1128 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001129 if (pkt_dev->page) {
1130 put_page(pkt_dev->page);
1131 pkt_dev->page = NULL;
1132 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001133 }
1134 else
1135 sprintf(pg_result, "ERROR: node not possible");
1136 return count;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001139 char f[32];
1140 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001142 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001143 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (copy_from_user(f, &user_buffer[i], len))
1146 return -EFAULT;
1147 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001148 if (strcmp(f, "IPSRC_RND") == 0)
1149 pkt_dev->flags |= F_IPSRC_RND;
1150
1151 else if (strcmp(f, "!IPSRC_RND") == 0)
1152 pkt_dev->flags &= ~F_IPSRC_RND;
1153
1154 else if (strcmp(f, "TXSIZE_RND") == 0)
1155 pkt_dev->flags |= F_TXSIZE_RND;
1156
1157 else if (strcmp(f, "!TXSIZE_RND") == 0)
1158 pkt_dev->flags &= ~F_TXSIZE_RND;
1159
1160 else if (strcmp(f, "IPDST_RND") == 0)
1161 pkt_dev->flags |= F_IPDST_RND;
1162
1163 else if (strcmp(f, "!IPDST_RND") == 0)
1164 pkt_dev->flags &= ~F_IPDST_RND;
1165
1166 else if (strcmp(f, "UDPSRC_RND") == 0)
1167 pkt_dev->flags |= F_UDPSRC_RND;
1168
1169 else if (strcmp(f, "!UDPSRC_RND") == 0)
1170 pkt_dev->flags &= ~F_UDPSRC_RND;
1171
1172 else if (strcmp(f, "UDPDST_RND") == 0)
1173 pkt_dev->flags |= F_UDPDST_RND;
1174
1175 else if (strcmp(f, "!UDPDST_RND") == 0)
1176 pkt_dev->flags &= ~F_UDPDST_RND;
1177
1178 else if (strcmp(f, "MACSRC_RND") == 0)
1179 pkt_dev->flags |= F_MACSRC_RND;
1180
1181 else if (strcmp(f, "!MACSRC_RND") == 0)
1182 pkt_dev->flags &= ~F_MACSRC_RND;
1183
1184 else if (strcmp(f, "MACDST_RND") == 0)
1185 pkt_dev->flags |= F_MACDST_RND;
1186
1187 else if (strcmp(f, "!MACDST_RND") == 0)
1188 pkt_dev->flags &= ~F_MACDST_RND;
1189
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001190 else if (strcmp(f, "MPLS_RND") == 0)
1191 pkt_dev->flags |= F_MPLS_RND;
1192
1193 else if (strcmp(f, "!MPLS_RND") == 0)
1194 pkt_dev->flags &= ~F_MPLS_RND;
1195
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001196 else if (strcmp(f, "VID_RND") == 0)
1197 pkt_dev->flags |= F_VID_RND;
1198
1199 else if (strcmp(f, "!VID_RND") == 0)
1200 pkt_dev->flags &= ~F_VID_RND;
1201
1202 else if (strcmp(f, "SVID_RND") == 0)
1203 pkt_dev->flags |= F_SVID_RND;
1204
1205 else if (strcmp(f, "!SVID_RND") == 0)
1206 pkt_dev->flags &= ~F_SVID_RND;
1207
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001208 else if (strcmp(f, "FLOW_SEQ") == 0)
1209 pkt_dev->flags |= F_FLOW_SEQ;
1210
Robert Olsson45b270f2007-08-28 15:45:55 -07001211 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1212 pkt_dev->flags |= F_QUEUE_MAP_RND;
1213
1214 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1215 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001216
1217 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1218 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1219
1220 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1221 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001222#ifdef CONFIG_XFRM
1223 else if (strcmp(f, "IPSEC") == 0)
1224 pkt_dev->flags |= F_IPSEC_ON;
1225#endif
1226
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001227 else if (strcmp(f, "!IPV6") == 0)
1228 pkt_dev->flags &= ~F_IPV6;
1229
Robert Olssone99b99b2010-03-18 22:44:30 +00001230 else if (strcmp(f, "NODE_ALLOC") == 0)
1231 pkt_dev->flags |= F_NODE;
1232
1233 else if (strcmp(f, "!NODE_ALLOC") == 0)
1234 pkt_dev->flags &= ~F_NODE;
1235
Luiz Capitulino222f1802006-03-20 22:16:13 -08001236 else {
1237 sprintf(pg_result,
1238 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1239 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001240 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001241 "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 -08001242 return count;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1245 return count;
1246 }
1247 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1248 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001249 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001250 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Luiz Capitulino222f1802006-03-20 22:16:13 -08001252 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001254 buf[len] = 0;
1255 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1256 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1257 strncpy(pkt_dev->dst_min, buf, len);
1258 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1259 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1260 }
1261 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001262 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001263 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1265 return count;
1266 }
1267 if (!strcmp(name, "dst_max")) {
1268 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001269 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001270 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Luiz Capitulino222f1802006-03-20 22:16:13 -08001273 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return -EFAULT;
1275
Luiz Capitulino222f1802006-03-20 22:16:13 -08001276 buf[len] = 0;
1277 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1278 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1279 strncpy(pkt_dev->dst_max, buf, len);
1280 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1281 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1282 }
1283 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001284 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 i += len;
1286 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1287 return count;
1288 }
1289 if (!strcmp(name, "dst6")) {
1290 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001291 if (len < 0)
1292 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 pkt_dev->flags |= F_IPV6;
1295
Luiz Capitulino222f1802006-03-20 22:16:13 -08001296 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001298 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Amerigo Wangc468fb12012-10-09 17:48:20 +00001300 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001301 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001303 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Luiz Capitulino222f1802006-03-20 22:16:13 -08001305 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001306 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 sprintf(pg_result, "OK: dst6=%s", buf);
1310 return count;
1311 }
1312 if (!strcmp(name, "dst6_min")) {
1313 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001314 if (len < 0)
1315 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 pkt_dev->flags |= F_IPV6;
1318
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001321 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Amerigo Wangc468fb12012-10-09 17:48:20 +00001323 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001324 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001326 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001327 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001328 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Luiz Capitulino222f1802006-03-20 22:16:13 -08001330 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 sprintf(pg_result, "OK: dst6_min=%s", buf);
1332 return count;
1333 }
1334 if (!strcmp(name, "dst6_max")) {
1335 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001336 if (len < 0)
1337 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 pkt_dev->flags |= F_IPV6;
1340
Luiz Capitulino222f1802006-03-20 22:16:13 -08001341 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001343 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Amerigo Wangc468fb12012-10-09 17:48:20 +00001345 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001346 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Luiz Capitulino222f1802006-03-20 22:16:13 -08001348 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001349 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 sprintf(pg_result, "OK: dst6_max=%s", buf);
1353 return count;
1354 }
1355 if (!strcmp(name, "src6")) {
1356 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001357 if (len < 0)
1358 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 pkt_dev->flags |= F_IPV6;
1361
Luiz Capitulino222f1802006-03-20 22:16:13 -08001362 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001364 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Amerigo Wangc468fb12012-10-09 17:48:20 +00001366 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001367 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001369 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Luiz Capitulino222f1802006-03-20 22:16:13 -08001371 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001372 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001373
1374 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 sprintf(pg_result, "OK: src6=%s", buf);
1376 return count;
1377 }
1378 if (!strcmp(name, "src_min")) {
1379 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001380 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001382
Luiz Capitulino222f1802006-03-20 22:16:13 -08001383 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001385 buf[len] = 0;
1386 if (strcmp(buf, pkt_dev->src_min) != 0) {
1387 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1388 strncpy(pkt_dev->src_min, buf, len);
1389 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1390 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1391 }
1392 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001393 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 i += len;
1395 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1396 return count;
1397 }
1398 if (!strcmp(name, "src_max")) {
1399 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001400 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001401 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001402
Luiz Capitulino222f1802006-03-20 22:16:13 -08001403 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001405 buf[len] = 0;
1406 if (strcmp(buf, pkt_dev->src_max) != 0) {
1407 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1408 strncpy(pkt_dev->src_max, buf, len);
1409 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1410 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1411 }
1412 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001413 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 i += len;
1415 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1416 return count;
1417 }
1418 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001420 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001421 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001424 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001427 if (!mac_pton(valstr, pkt_dev->dst_mac))
1428 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* Set up Dest MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001430 memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001431
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001432 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return count;
1434 }
1435 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001437 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001438 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001441 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001444 if (!mac_pton(valstr, pkt_dev->src_mac))
1445 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001446 /* Set up Src MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001447 memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001448
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001449 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return count;
1451 }
1452
Luiz Capitulino222f1802006-03-20 22:16:13 -08001453 if (!strcmp(name, "clear_counters")) {
1454 pktgen_clear_counters(pkt_dev);
1455 sprintf(pg_result, "OK: Clearing counters.\n");
1456 return count;
1457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 if (!strcmp(name, "flows")) {
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 if (value > MAX_CFLOWS)
1466 value = MAX_CFLOWS;
1467
1468 pkt_dev->cflows = value;
1469 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1470 return count;
1471 }
1472
1473 if (!strcmp(name, "flowlen")) {
1474 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001475 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001476 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 i += len;
1479 pkt_dev->lflow = value;
1480 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1481 return count;
1482 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001483
Robert Olsson45b270f2007-08-28 15:45:55 -07001484 if (!strcmp(name, "queue_map_min")) {
1485 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001486 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001487 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001488
Robert Olsson45b270f2007-08-28 15:45:55 -07001489 i += len;
1490 pkt_dev->queue_map_min = value;
1491 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1492 return count;
1493 }
1494
1495 if (!strcmp(name, "queue_map_max")) {
1496 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001497 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001498 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001499
Robert Olsson45b270f2007-08-28 15:45:55 -07001500 i += len;
1501 pkt_dev->queue_map_max = value;
1502 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1503 return count;
1504 }
1505
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001506 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001507 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001508
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001509 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001510 if (len < 0)
1511 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001512 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001513 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001514 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001515 cnt += sprintf(pg_result + cnt,
1516 "%08x%s", ntohl(pkt_dev->labels[n]),
1517 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001518
1519 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1520 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1521 pkt_dev->svlan_id = 0xffff;
1522
1523 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001524 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001525 }
1526 return count;
1527 }
1528
1529 if (!strcmp(name, "vlan_id")) {
1530 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001531 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001532 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001533
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001534 i += len;
1535 if (value <= 4095) {
1536 pkt_dev->vlan_id = value; /* turn on VLAN */
1537
1538 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001539 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001540
1541 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001542 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001543
1544 pkt_dev->nr_labels = 0; /* turn off MPLS */
1545 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1546 } else {
1547 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1548 pkt_dev->svlan_id = 0xffff;
1549
1550 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001551 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001552 }
1553 return count;
1554 }
1555
1556 if (!strcmp(name, "vlan_p")) {
1557 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001558 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001559 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001560
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001561 i += len;
1562 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1563 pkt_dev->vlan_p = value;
1564 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1565 } else {
1566 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1567 }
1568 return count;
1569 }
1570
1571 if (!strcmp(name, "vlan_cfi")) {
1572 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001573 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001574 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001575
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001576 i += len;
1577 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1578 pkt_dev->vlan_cfi = value;
1579 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1580 } else {
1581 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1582 }
1583 return count;
1584 }
1585
1586 if (!strcmp(name, "svlan_id")) {
1587 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001588 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001589 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001590
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001591 i += len;
1592 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1593 pkt_dev->svlan_id = value; /* turn on SVLAN */
1594
1595 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001596 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001597
1598 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001599 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001600
1601 pkt_dev->nr_labels = 0; /* turn off MPLS */
1602 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1603 } else {
1604 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1605 pkt_dev->svlan_id = 0xffff;
1606
1607 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001608 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001609 }
1610 return count;
1611 }
1612
1613 if (!strcmp(name, "svlan_p")) {
1614 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001615 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001616 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001617
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001618 i += len;
1619 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1620 pkt_dev->svlan_p = value;
1621 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1622 } else {
1623 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1624 }
1625 return count;
1626 }
1627
1628 if (!strcmp(name, "svlan_cfi")) {
1629 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001630 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001631 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001632
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001633 i += len;
1634 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1635 pkt_dev->svlan_cfi = value;
1636 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1637 } else {
1638 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1639 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001640 return count;
1641 }
1642
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001643 if (!strcmp(name, "tos")) {
1644 __u32 tmp_value = 0;
1645 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001646 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001647 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001648
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001649 i += len;
1650 if (len == 2) {
1651 pkt_dev->tos = tmp_value;
1652 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1653 } else {
1654 sprintf(pg_result, "ERROR: tos must be 00-ff");
1655 }
1656 return count;
1657 }
1658
1659 if (!strcmp(name, "traffic_class")) {
1660 __u32 tmp_value = 0;
1661 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001662 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001663 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001664
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001665 i += len;
1666 if (len == 2) {
1667 pkt_dev->traffic_class = tmp_value;
1668 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1669 } else {
1670 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1671 }
1672 return count;
1673 }
1674
John Fastabend9e50e3a2010-11-16 19:12:28 +00001675 if (!strcmp(name, "skb_priority")) {
1676 len = num_arg(&user_buffer[i], 9, &value);
1677 if (len < 0)
1678 return len;
1679
1680 i += len;
1681 pkt_dev->skb_priority = value;
1682 sprintf(pg_result, "OK: skb_priority=%i",
1683 pkt_dev->skb_priority);
1684 return count;
1685 }
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1688 return -EINVAL;
1689}
1690
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001691static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001693 return single_open(file, pktgen_if_show, PDE(inode)->data);
1694}
1695
Arjan van de Ven9a321442007-02-12 00:55:35 -08001696static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001697 .owner = THIS_MODULE,
1698 .open = pktgen_if_open,
1699 .read = seq_read,
1700 .llseek = seq_lseek,
1701 .write = pktgen_if_write,
1702 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001703};
1704
1705static int pktgen_thread_show(struct seq_file *seq, void *v)
1706{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001707 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001708 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001710 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Luiz Capitulino222f1802006-03-20 22:16:13 -08001712 seq_printf(seq, "Running: ");
1713
1714 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001715 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001716 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001717 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Luiz Capitulino222f1802006-03-20 22:16:13 -08001719 seq_printf(seq, "\nStopped: ");
1720
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001721 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001722 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001723 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001726 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001728 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Luiz Capitulino222f1802006-03-20 22:16:13 -08001730 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001735static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001736 const char __user * user_buffer,
1737 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Joe Perches8a994a72010-07-12 10:50:23 +00001739 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001740 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001741 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001743 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001746 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return -EINVAL;
1748 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001749
Paul Gortmakerd6182222010-10-18 12:14:44 +00001750 max = count;
1751 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001752 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001753 return len;
1754
Paul Gortmakerd6182222010-10-18 12:14:44 +00001755 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /* Read variable name */
1758
1759 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001760 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001761 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 memset(name, 0, sizeof(name));
1764 if (copy_from_user(name, &user_buffer[i], len))
1765 return -EFAULT;
1766 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001767
Luiz Capitulino222f1802006-03-20 22:16:13 -08001768 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001770 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001771 return len;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 i += len;
1774
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001775 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001776 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Luiz Capitulino222f1802006-03-20 22:16:13 -08001778 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001779 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 ret = -EINVAL;
1781 goto out;
1782 }
1783
1784 pg_result = &(t->result[0]);
1785
Luiz Capitulino222f1802006-03-20 22:16:13 -08001786 if (!strcmp(name, "add_device")) {
1787 char f[32];
1788 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001790 if (len < 0) {
1791 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto out;
1793 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001794 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return -EFAULT;
1796 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001797 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001798 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001799 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001800 ret = count;
1801 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto out;
1803 }
1804
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001806 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001807 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001808 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001809 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001811 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto out;
1813 }
1814
Luiz Capitulino222f1802006-03-20 22:16:13 -08001815 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001816 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001817 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 goto out;
1819 }
1820
1821 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001822out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return ret;
1824}
1825
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001826static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001828 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
Arjan van de Ven9a321442007-02-12 00:55:35 -08001831static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001832 .owner = THIS_MODULE,
1833 .open = pktgen_thread_open,
1834 .read = seq_read,
1835 .llseek = seq_lseek,
1836 .write = pktgen_thread_write,
1837 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001838};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001841static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct pktgen_thread *t;
1844 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001845 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001847 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001848 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001850 if (remove) {
1851 if_lock(t);
1852 pkt_dev->removal_mark = 1;
1853 t->control |= T_REMDEV;
1854 if_unlock(t);
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 break;
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001859 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001862/*
1863 * mark a device for removal
1864 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001865static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001868 const int max_tries = 10, msec_per_try = 125;
1869 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001870
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001871 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001872 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001873
Luiz Capitulino222f1802006-03-20 22:16:13 -08001874 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001875
1876 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001877 if (pkt_dev == NULL)
1878 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001879
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001880 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001881 pr_debug("%s: waiting for %s to disappear....\n",
1882 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001883 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001884 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001885
1886 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001887 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1888 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001889 break;
1890 }
1891
1892 }
1893
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001894 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001895}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001896
Stephen Hemminger39df2322007-03-04 16:11:51 -08001897static void pktgen_change_name(struct net_device *dev)
1898{
1899 struct pktgen_thread *t;
1900
1901 list_for_each_entry(t, &pktgen_threads, th_list) {
1902 struct pktgen_dev *pkt_dev;
1903
1904 list_for_each_entry(pkt_dev, &t->if_list, list) {
1905 if (pkt_dev->odev != dev)
1906 continue;
1907
1908 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1909
Alexey Dobriyan29753152009-08-28 23:34:43 -07001910 pkt_dev->entry = proc_create_data(dev->name, 0600,
1911 pg_proc_dir,
1912 &pktgen_if_fops,
1913 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001914 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001915 pr_err("can't move proc entry for '%s'\n",
1916 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001917 break;
1918 }
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
Luiz Capitulino222f1802006-03-20 22:16:13 -08001922static int pktgen_device_event(struct notifier_block *unused,
1923 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001925 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Eric Dumazetc57b5462012-05-09 13:29:51 +00001927 if (!net_eq(dev_net(dev), &init_net) || pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001928 return NOTIFY_DONE;
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /* It is OK that we do not hold the group lock right now,
1931 * as we run under the RTNL lock.
1932 */
1933
1934 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001935 case NETDEV_CHANGENAME:
1936 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001940 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 return NOTIFY_DONE;
1945}
1946
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001947static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1948 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001949{
1950 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001951 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001952
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001953 for (i = 0; ifname[i] != '@'; i++) {
1954 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001955 break;
1956
1957 b[i] = ifname[i];
1958 }
1959 b[i] = 0;
1960
1961 return dev_get_by_name(&init_net, b);
1962}
1963
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965/* Associate pktgen_dev with a device. */
1966
Stephen Hemminger39df2322007-03-04 16:11:51 -08001967static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001970 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (pkt_dev->odev) {
1974 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001975 pkt_dev->odev = NULL;
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Robert Olssone6fce5b2008-08-07 02:23:01 -07001978 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001980 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001981 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001985 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001986 err = -EINVAL;
1987 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001988 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001989 err = -ENETDOWN;
1990 } else {
1991 pkt_dev->odev = odev;
1992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001996 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999/* Read pkt_dev from the interface and set up internal pktgen_dev
2000 * structure to have the right information to create/send packets
2001 */
2002static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2003{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002004 int ntxq;
2005
Luiz Capitulino222f1802006-03-20 22:16:13 -08002006 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002007 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002008 sprintf(pkt_dev->result,
2009 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2010 return;
2011 }
2012
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002013 /* make sure that we don't pick a non-existing transmit queue */
2014 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002015
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002016 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002017 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2018 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2019 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002020 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002021 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002022 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002023 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2024 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2025 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002026 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002027 }
2028
Luiz Capitulino222f1802006-03-20 22:16:13 -08002029 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002031 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002032 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002035 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Luiz Capitulino222f1802006-03-20 22:16:13 -08002037 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002038 int i, set = 0, err = 1;
2039 struct inet6_dev *idev;
2040
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002041 if (pkt_dev->min_pkt_size == 0) {
2042 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2043 + sizeof(struct udphdr)
2044 + sizeof(struct pktgen_hdr)
2045 + pkt_dev->pkt_overhead;
2046 }
2047
Luiz Capitulino222f1802006-03-20 22:16:13 -08002048 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2049 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 set = 1;
2051 break;
2052 }
2053
Luiz Capitulino222f1802006-03-20 22:16:13 -08002054 if (!set) {
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /*
2057 * Use linklevel address if unconfigured.
2058 *
2059 * use ipv6_get_lladdr if/when it's get exported
2060 */
2061
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002062 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002063 idev = __in6_dev_get(pkt_dev->odev);
2064 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct inet6_ifaddr *ifp;
2066
2067 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002068 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2069 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002070 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002071 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 err = 0;
2073 break;
2074 }
2075 }
2076 read_unlock_bh(&idev->lock);
2077 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002078 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002079 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002080 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002082 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002083 if (pkt_dev->min_pkt_size == 0) {
2084 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2085 + sizeof(struct udphdr)
2086 + sizeof(struct pktgen_hdr)
2087 + pkt_dev->pkt_overhead;
2088 }
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 pkt_dev->saddr_min = 0;
2091 pkt_dev->saddr_max = 0;
2092 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002093
2094 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002097 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (in_dev) {
2099 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002100 pkt_dev->saddr_min =
2101 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 pkt_dev->saddr_max = pkt_dev->saddr_min;
2103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002106 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2108 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2109 }
2110
2111 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2112 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2113 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002114 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002115 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2116 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2117 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2118
Luiz Capitulino222f1802006-03-20 22:16:13 -08002119 pkt_dev->cur_dst_mac_offset = 0;
2120 pkt_dev->cur_src_mac_offset = 0;
2121 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2122 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2123 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2124 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 pkt_dev->nflows = 0;
2126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002129static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2130{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002131 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002132 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002133 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002134
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002135 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2136 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002137
Daniel Turull43d28b62010-06-09 22:49:57 +00002138 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002139 if (remaining <= 0) {
2140 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002141 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002142 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002143
Stephen Hemmingeref879792009-09-22 19:41:43 +00002144 start_time = ktime_now();
Eric Dumazet33136d12011-10-20 17:00:21 -04002145 if (remaining < 100000) {
2146 /* for small delays (<100us), just loop until limit is reached */
2147 do {
2148 end_time = ktime_now();
2149 } while (ktime_lt(end_time, spin_until));
2150 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002151 /* see do_nanosleep */
2152 hrtimer_init_sleeper(&t, current);
2153 do {
2154 set_current_state(TASK_INTERRUPTIBLE);
2155 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2156 if (!hrtimer_active(&t.timer))
2157 t.task = NULL;
2158
2159 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002162 hrtimer_cancel(&t.timer);
2163 } while (t.task && pkt_dev->running && !signal_pending(current));
2164 __set_current_state(TASK_RUNNING);
Eric Dumazet33136d12011-10-20 17:00:21 -04002165 end_time = ktime_now();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002167
2168 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002169 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002172static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2173{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002174 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002175 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2176 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2177 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2178}
2179
Stephen Hemminger648fda72009-08-27 13:55:07 +00002180static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002181{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002182 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002183}
2184
2185static inline int f_pick(struct pktgen_dev *pkt_dev)
2186{
2187 int flow = pkt_dev->curfl;
2188
2189 if (pkt_dev->flags & F_FLOW_SEQ) {
2190 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2191 /* reset time */
2192 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002193 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002194 pkt_dev->curfl += 1;
2195 if (pkt_dev->curfl >= pkt_dev->cflows)
2196 pkt_dev->curfl = 0; /*reset */
2197 }
2198 } else {
2199 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002200 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002201
Robert Olsson1211a642008-08-05 18:44:26 -07002202 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002203 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002204 pkt_dev->flows[flow].flags = 0;
2205 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002206 }
2207
2208 return pkt_dev->curfl;
2209}
2210
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002211
2212#ifdef CONFIG_XFRM
2213/* If there was already an IPSEC SA, we keep it as is, else
2214 * we go look for it ...
2215*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002216#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002217static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002218{
2219 struct xfrm_state *x = pkt_dev->flows[flow].x;
2220 if (!x) {
2221 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002222 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002223 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002224 (xfrm_address_t *)&pkt_dev->cur_saddr,
2225 AF_INET,
2226 pkt_dev->ipsmode,
2227 pkt_dev->ipsproto, 0);
2228 if (x) {
2229 pkt_dev->flows[flow].x = x;
2230 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002231 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002232 }
2233
2234 }
2235}
2236#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002237static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2238{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002239
2240 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2241 pkt_dev->cur_queue_map = smp_processor_id();
2242
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002243 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002244 __u16 t;
2245 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2246 t = random32() %
2247 (pkt_dev->queue_map_max -
2248 pkt_dev->queue_map_min + 1)
2249 + pkt_dev->queue_map_min;
2250 } else {
2251 t = pkt_dev->cur_queue_map + 1;
2252 if (t > pkt_dev->queue_map_max)
2253 t = pkt_dev->queue_map_min;
2254 }
2255 pkt_dev->cur_queue_map = t;
2256 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002257 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 -07002258}
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260/* Increment/randomize headers according to flags and current values
2261 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2262 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002263static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2264{
2265 __u32 imn;
2266 __u32 imx;
2267 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002269 if (pkt_dev->cflows)
2270 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002273 if (pkt_dev->src_mac_count > 1) {
2274 __u32 mc;
2275 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Luiz Capitulino222f1802006-03-20 22:16:13 -08002277 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002278 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002279 else {
2280 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002281 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002282 pkt_dev->src_mac_count)
2283 pkt_dev->cur_src_mac_offset = 0;
2284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Luiz Capitulino222f1802006-03-20 22:16:13 -08002286 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2287 pkt_dev->hh[11] = tmp;
2288 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2289 pkt_dev->hh[10] = tmp;
2290 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2291 pkt_dev->hh[9] = tmp;
2292 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2293 pkt_dev->hh[8] = tmp;
2294 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2295 pkt_dev->hh[7] = tmp;
2296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Luiz Capitulino222f1802006-03-20 22:16:13 -08002298 /* Deal with Destination MAC */
2299 if (pkt_dev->dst_mac_count > 1) {
2300 __u32 mc;
2301 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Luiz Capitulino222f1802006-03-20 22:16:13 -08002303 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002304 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Luiz Capitulino222f1802006-03-20 22:16:13 -08002306 else {
2307 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002308 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002309 pkt_dev->dst_mac_count) {
2310 pkt_dev->cur_dst_mac_offset = 0;
2311 }
2312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Luiz Capitulino222f1802006-03-20 22:16:13 -08002314 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2315 pkt_dev->hh[5] = tmp;
2316 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2317 pkt_dev->hh[4] = tmp;
2318 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2319 pkt_dev->hh[3] = tmp;
2320 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2321 pkt_dev->hh[2] = tmp;
2322 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2323 pkt_dev->hh[1] = tmp;
2324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002326 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002327 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002328 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002329 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2330 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002331 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002332 htonl(0x000fffff));
2333 }
2334
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002335 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002336 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002337 }
2338
2339 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002340 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002341 }
2342
Luiz Capitulino222f1802006-03-20 22:16:13 -08002343 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2344 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002345 pkt_dev->cur_udp_src = random32() %
2346 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2347 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Luiz Capitulino222f1802006-03-20 22:16:13 -08002349 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 pkt_dev->cur_udp_src++;
2351 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2352 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002353 }
2354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Luiz Capitulino222f1802006-03-20 22:16:13 -08002356 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2357 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002358 pkt_dev->cur_udp_dst = random32() %
2359 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2360 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002361 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002363 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002365 }
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 if (!(pkt_dev->flags & F_IPV6)) {
2369
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002370 imn = ntohl(pkt_dev->saddr_min);
2371 imx = ntohl(pkt_dev->saddr_max);
2372 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002374 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002375 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 else {
2377 t = ntohl(pkt_dev->cur_saddr);
2378 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002379 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383 pkt_dev->cur_saddr = htonl(t);
2384 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002385
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002386 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2388 } else {
Al Viro252e33462006-11-14 20:48:11 -08002389 imn = ntohl(pkt_dev->daddr_min);
2390 imx = ntohl(pkt_dev->daddr_max);
2391 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002393 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (pkt_dev->flags & F_IPDST_RND) {
2395
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002396 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002397 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Joe Perches21cf2252007-12-16 13:44:00 -08002399 while (ipv4_is_loopback(s) ||
2400 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002401 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002402 ipv4_is_zeronet(s) ||
2403 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002404 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002405 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Al Viro252e33462006-11-14 20:48:11 -08002407 pkt_dev->cur_daddr = s;
2408 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 t = ntohl(pkt_dev->cur_daddr);
2410 t++;
2411 if (t > imx) {
2412 t = imn;
2413 }
2414 pkt_dev->cur_daddr = htonl(t);
2415 }
2416 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002417 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002418 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002419 pkt_dev->flows[flow].cur_daddr =
2420 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002421#ifdef CONFIG_XFRM
2422 if (pkt_dev->flags & F_IPSEC_ON)
2423 get_ipsec_sa(pkt_dev, flow);
2424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 pkt_dev->nflows++;
2426 }
2427 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002428 } else { /* IPV6 * */
2429
2430 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2431 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2432 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2433 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 else {
2435 int i;
2436
2437 /* Only random destinations yet */
2438
Luiz Capitulino222f1802006-03-20 22:16:13 -08002439 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002441 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002442 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2443 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447
Luiz Capitulino222f1802006-03-20 22:16:13 -08002448 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2449 __u32 t;
2450 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002451 t = random32() %
2452 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2453 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002454 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002456 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002458 }
2459 pkt_dev->cur_pkt_size = t;
2460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002462 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 pkt_dev->flows[flow].count++;
2465}
2466
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002467
2468#ifdef CONFIG_XFRM
2469static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2470{
2471 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2472 int err = 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002473
2474 if (!x)
2475 return 0;
2476 /* XXX: we dont support tunnel mode for now until
2477 * we resolve the dst issue */
2478 if (x->props.mode != XFRM_MODE_TRANSPORT)
2479 return 0;
2480
2481 spin_lock(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002482
Herbert Xu13996372007-10-17 21:35:51 -07002483 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002484 if (err)
2485 goto error;
2486 err = x->type->output(x, skb);
2487 if (err)
2488 goto error;
2489
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002490 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002491 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002492error:
2493 spin_unlock(&x->lock);
2494 return err;
2495}
2496
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002497static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002498{
2499 if (pkt_dev->cflows) {
2500 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002501 int i;
2502 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002503 struct xfrm_state *x = pkt_dev->flows[i].x;
2504 if (x) {
2505 xfrm_state_put(x);
2506 pkt_dev->flows[i].x = NULL;
2507 }
2508 }
2509 }
2510}
2511
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002512static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002513 struct sk_buff *skb, __be16 protocol)
2514{
2515 if (pkt_dev->flags & F_IPSEC_ON) {
2516 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2517 int nhead = 0;
2518 if (x) {
2519 int ret;
2520 __u8 *eth;
2521 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002522 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002523 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2524 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002525 pr_err("Error expanding ipsec packet %d\n",
2526 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002527 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002528 }
2529 }
2530
2531 /* ipsec is not expecting ll header */
2532 skb_pull(skb, ETH_HLEN);
2533 ret = pktgen_output_ipsec(skb, pkt_dev);
2534 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002535 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002536 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002537 }
2538 /* restore ll */
2539 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2540 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002541 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002542 }
2543 }
2544 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002545err:
2546 kfree_skb(skb);
2547 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002548}
2549#endif
2550
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002551static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2552{
Eric Dumazet95c96172012-04-15 05:58:06 +00002553 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002554 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002555 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002556
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002557 mpls--;
2558 *mpls |= MPLS_STACK_BOTTOM;
2559}
2560
Al Viro0f37c602006-11-03 03:49:56 -08002561static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2562 unsigned int prio)
2563{
2564 return htons(id | (cfi << 12) | (prio << 13));
2565}
2566
Eric Dumazet26ad7872011-01-25 13:26:05 -08002567static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2568 int datalen)
2569{
2570 struct timeval timestamp;
2571 struct pktgen_hdr *pgh;
2572
2573 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2574 datalen -= sizeof(*pgh);
2575
2576 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002577 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002578 } else {
2579 int frags = pkt_dev->nfrags;
2580 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002581 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002582
2583
2584 if (frags > MAX_SKB_FRAGS)
2585 frags = MAX_SKB_FRAGS;
2586 len = datalen - frags * PAGE_SIZE;
2587 if (len > 0) {
2588 memset(skb_put(skb, len), 0, len);
2589 datalen = frags * PAGE_SIZE;
2590 }
2591
2592 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002593 frag_len = (datalen/frags) < PAGE_SIZE ?
2594 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002595 while (datalen > 0) {
2596 if (unlikely(!pkt_dev->page)) {
2597 int node = numa_node_id();
2598
2599 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2600 node = pkt_dev->node;
2601 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2602 if (!pkt_dev->page)
2603 break;
2604 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002605 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002606 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002607 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002608 /*last fragment, fill rest of data*/
2609 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002610 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2611 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002612 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002613 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2614 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2615 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2616 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002617 i++;
2618 skb_shinfo(skb)->nr_frags = i;
2619 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002620 }
2621
2622 /* Stamp the time, and sequence number,
2623 * convert them to network byte order
2624 */
2625 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2626 pgh->seq_num = htonl(pkt_dev->seq_num);
2627
2628 do_gettimeofday(&timestamp);
2629 pgh->tv_sec = htonl(timestamp.tv_sec);
2630 pgh->tv_usec = htonl(timestamp.tv_usec);
2631}
2632
Luiz Capitulino222f1802006-03-20 22:16:13 -08002633static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2634 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
2636 struct sk_buff *skb = NULL;
2637 __u8 *eth;
2638 struct udphdr *udph;
2639 int datalen, iplen;
2640 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002641 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002642 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002643 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2644 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2645 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2646 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002647 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002648
2649 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002650 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002651
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002652 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002653 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002654
Robert Olsson64053be2005-06-26 15:27:10 -07002655 /* Update any of the values, used when we're incrementing various
2656 * fields.
2657 */
2658 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002659 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002660
David S. Miller7ac54592006-01-18 14:19:10 -08002661 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002662
2663 if (pkt_dev->flags & F_NODE) {
2664 int node;
2665
2666 if (pkt_dev->node >= 0)
2667 node = pkt_dev->node;
2668 else
2669 node = numa_node_id();
2670
2671 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2672 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2673 if (likely(skb)) {
2674 skb_reserve(skb, NET_SKB_PAD);
2675 skb->dev = odev;
2676 }
2677 }
2678 else
2679 skb = __netdev_alloc_skb(odev,
2680 pkt_dev->cur_pkt_size + 64
2681 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (!skb) {
2684 sprintf(pkt_dev->result, "No memory");
2685 return NULL;
2686 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002687 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
David S. Miller7ac54592006-01-18 14:19:10 -08002689 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 /* Reserve for ethernet and IP header */
2692 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002693 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2694 if (pkt_dev->nr_labels)
2695 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002696
2697 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002698 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002699 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002700 *svlan_tci = build_tci(pkt_dev->svlan_id,
2701 pkt_dev->svlan_cfi,
2702 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002703 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002704 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002705 }
2706 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002707 *vlan_tci = build_tci(pkt_dev->vlan_id,
2708 pkt_dev->vlan_cfi,
2709 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002710 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002711 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002712 }
2713
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002714 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002715 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002716 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002717 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002718 skb->priority = pkt_dev->skb_priority;
2719
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002720 iph = ip_hdr(skb);
2721 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002724 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002726 /* Eth + IPh + UDPh + mpls */
2727 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002728 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002729 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 udph->source = htons(pkt_dev->cur_udp_src);
2733 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002734 udph->len = htons(datalen + 8); /* DATA + udphdr */
2735 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 iph->ihl = 5;
2738 iph->version = 4;
2739 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002740 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002741 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 iph->saddr = pkt_dev->cur_saddr;
2743 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002744 iph->id = htons(pkt_dev->ip_id);
2745 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 iph->frag_off = 0;
2747 iplen = 20 + 8 + datalen;
2748 iph->tot_len = htons(iplen);
2749 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002750 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002751 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002752 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002753 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 skb->dev = odev;
2755 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002756 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002757
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002758#ifdef CONFIG_XFRM
2759 if (!process_ipsec(pkt_dev, skb, protocol))
2760 return NULL;
2761#endif
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return skb;
2764}
2765
Luiz Capitulino222f1802006-03-20 22:16:13 -08002766static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2767 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 struct sk_buff *skb = NULL;
2770 __u8 *eth;
2771 struct udphdr *udph;
2772 int datalen;
2773 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002774 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002775 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002776 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2777 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2778 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2779 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002780 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002781
2782 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002783 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002784
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002785 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002786 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002787
Robert Olsson64053be2005-06-26 15:27:10 -07002788 /* Update any of the values, used when we're incrementing various
2789 * fields.
2790 */
2791 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002792 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002793
Stephen Hemmingere4707572009-08-27 13:55:13 +00002794 skb = __netdev_alloc_skb(odev,
2795 pkt_dev->cur_pkt_size + 64
2796 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (!skb) {
2798 sprintf(pkt_dev->result, "No memory");
2799 return NULL;
2800 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002801 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 skb_reserve(skb, 16);
2804
2805 /* Reserve for ethernet and IP header */
2806 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002807 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2808 if (pkt_dev->nr_labels)
2809 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002810
2811 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002812 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002813 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002814 *svlan_tci = build_tci(pkt_dev->svlan_id,
2815 pkt_dev->svlan_cfi,
2816 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002817 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002818 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002819 }
2820 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002821 *vlan_tci = build_tci(pkt_dev->vlan_id,
2822 pkt_dev->vlan_cfi,
2823 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002824 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002825 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002826 }
2827
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002828 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002829 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002830 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002831 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002832 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002833 iph = ipv6_hdr(skb);
2834 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002837 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002838
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002839 /* Eth + IPh + UDPh + mpls */
2840 datalen = pkt_dev->cur_pkt_size - 14 -
2841 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002842 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002844 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002846 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 }
2848
2849 udph->source = htons(pkt_dev->cur_udp_src);
2850 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002851 udph->len = htons(datalen + sizeof(struct udphdr));
2852 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002854 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002856 if (pkt_dev->traffic_class) {
2857 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002858 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002859 }
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 iph->hop_limit = 32;
2862
2863 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2864 iph->nexthdr = IPPROTO_UDP;
2865
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002866 iph->daddr = pkt_dev->cur_in6_daddr;
2867 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002869 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002870 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002871 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 skb->dev = odev;
2873 skb->pkt_type = PACKET_HOST;
2874
Eric Dumazet26ad7872011-01-25 13:26:05 -08002875 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return skb;
2878}
2879
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002880static struct sk_buff *fill_packet(struct net_device *odev,
2881 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002883 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return fill_packet_ipv6(odev, pkt_dev);
2885 else
2886 return fill_packet_ipv4(odev, pkt_dev);
2887}
2888
Luiz Capitulino222f1802006-03-20 22:16:13 -08002889static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002891 pkt_dev->seq_num = 1;
2892 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002894 pkt_dev->tx_bytes = 0;
2895 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
2898/* Set up structure for sending pkts, clear counters */
2899
2900static void pktgen_run(struct pktgen_thread *t)
2901{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002902 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 int started = 0;
2904
Joe Perchesf9467ea2010-06-21 12:29:14 +00002905 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002908 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910 /*
2911 * setup odev and create initial packet.
2912 */
2913 pktgen_setup_inject(pkt_dev);
2914
Luiz Capitulino222f1802006-03-20 22:16:13 -08002915 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002917 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002919 pkt_dev->started_at =
2920 pkt_dev->next_tx = ktime_now();
2921
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002922 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 strcpy(pkt_dev->result, "Starting");
2925 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002926 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 strcpy(pkt_dev->result, "Error starting");
2928 }
2929 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002930 if (started)
2931 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
2934static void pktgen_stop_all_threads_ifs(void)
2935{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002936 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Joe Perchesf9467ea2010-06-21 12:29:14 +00002938 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002940 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002941
2942 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002943 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002944
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002945 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
Stephen Hemminger648fda72009-08-27 13:55:07 +00002948static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002950 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002952 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00002953 if (pkt_dev->running)
2954 return 1;
2955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
2957
Luiz Capitulino222f1802006-03-20 22:16:13 -08002958static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002960 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Luiz Capitulino222f1802006-03-20 22:16:13 -08002962 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Luiz Capitulino222f1802006-03-20 22:16:13 -08002964 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Luiz Capitulino222f1802006-03-20 22:16:13 -08002966 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Luiz Capitulino222f1802006-03-20 22:16:13 -08002968 if (signal_pending(current))
2969 goto signal;
2970 if_lock(t);
2971 }
2972 if_unlock(t);
2973 return 1;
2974signal:
2975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
2978static int pktgen_wait_all_threads_run(void)
2979{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002980 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002982
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002983 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002984
2985 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002987 if (sig == 0)
2988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002990
2991 if (sig == 0)
2992 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002994
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002995 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return sig;
2997}
2998
2999static void pktgen_run_all_threads(void)
3000{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003001 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Joe Perchesf9467ea2010-06-21 12:29:14 +00003003 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003005 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003007 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003009
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003010 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003012 /* Propagate thread->control */
3013 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 pktgen_wait_all_threads_run();
3016}
3017
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003018static void pktgen_reset_all_threads(void)
3019{
3020 struct pktgen_thread *t;
3021
Joe Perchesf9467ea2010-06-21 12:29:14 +00003022 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003023
3024 mutex_lock(&pktgen_thread_lock);
3025
3026 list_for_each_entry(t, &pktgen_threads, th_list)
3027 t->control |= (T_REMDEVALL);
3028
3029 mutex_unlock(&pktgen_thread_lock);
3030
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003031 /* Propagate thread->control */
3032 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003033
3034 pktgen_wait_all_threads_run();
3035}
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3038{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003039 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003040 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003041 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3042 pkt_dev->started_at);
3043 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Daniel Turull03a14ab2011-03-09 14:11:00 -08003045 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003046 (unsigned long long)ktime_to_us(elapsed),
3047 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3048 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003049 (unsigned long long)pkt_dev->sofar,
3050 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003052 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3053 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Luiz Capitulino222f1802006-03-20 22:16:13 -08003055 bps = pps * 8 * pkt_dev->cur_pkt_size;
3056
3057 mbps = bps;
3058 do_div(mbps, 1000000);
3059 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3060 (unsigned long long)pps,
3061 (unsigned long long)mbps,
3062 (unsigned long long)bps,
3063 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003069 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003070
Luiz Capitulino222f1802006-03-20 22:16:13 -08003071 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003072 pr_warning("interface: %s is already stopped\n",
3073 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074 return -EINVAL;
3075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003077 kfree_skb(pkt_dev->skb);
3078 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003079 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003080 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003082 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Luiz Capitulino222f1802006-03-20 22:16:13 -08003084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
Luiz Capitulino222f1802006-03-20 22:16:13 -08003087static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003089 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if_lock(t);
3092
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003093 list_for_each_entry(pkt_dev, &t->if_list, list) {
3094 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003095 continue;
3096 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003097 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003098 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003099 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
3101 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003102 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Luiz Capitulino222f1802006-03-20 22:16:13 -08003105static void pktgen_stop(struct pktgen_thread *t)
3106{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003107 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Joe Perchesf9467ea2010-06-21 12:29:14 +00003109 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Luiz Capitulino222f1802006-03-20 22:16:13 -08003111 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003113 list_for_each_entry(pkt_dev, &t->if_list, list) {
3114 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Luiz Capitulino222f1802006-03-20 22:16:13 -08003117 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003120/*
3121 * one of our devices needs to be removed - find it
3122 * and remove it
3123 */
3124static void pktgen_rem_one_if(struct pktgen_thread *t)
3125{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003126 struct list_head *q, *n;
3127 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003128
Joe Perchesf9467ea2010-06-21 12:29:14 +00003129 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003130
3131 if_lock(t);
3132
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003133 list_for_each_safe(q, n, &t->if_list) {
3134 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003135
Luiz Capitulino222f1802006-03-20 22:16:13 -08003136 if (!cur->removal_mark)
3137 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003138
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003139 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003140 cur->skb = NULL;
3141
3142 pktgen_remove_device(t, cur);
3143
3144 break;
3145 }
3146
3147 if_unlock(t);
3148}
3149
Luiz Capitulino222f1802006-03-20 22:16:13 -08003150static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003152 struct list_head *q, *n;
3153 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154
Joe Perchesf9467ea2010-06-21 12:29:14 +00003155 func_enter();
3156
Luiz Capitulino222f1802006-03-20 22:16:13 -08003157 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003158
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003161 list_for_each_safe(q, n, &t->if_list) {
3162 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003163
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003164 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003165 cur->skb = NULL;
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 pktgen_remove_device(t, cur);
3168 }
3169
Luiz Capitulino222f1802006-03-20 22:16:13 -08003170 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
Luiz Capitulino222f1802006-03-20 22:16:13 -08003173static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
David S. Milleree74baa2007-01-01 20:51:53 -08003177 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
Stephen Hemmingeref879792009-09-22 19:41:43 +00003181static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003182{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003183 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003184 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003185 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003186}
3187
Stephen Hemmingeref879792009-09-22 19:41:43 +00003188static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3189{
3190 ktime_t idle_start = ktime_now();
3191
3192 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3193 if (signal_pending(current))
3194 break;
3195
3196 if (need_resched())
3197 pktgen_resched(pkt_dev);
3198 else
3199 cpu_relax();
3200 }
3201 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3202}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003203
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003204static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
Stephen Hemminger00829822008-11-20 20:14:53 -08003206 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003207 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003208 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003209 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003210 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 int ret;
3212
Stephen Hemmingeref879792009-09-22 19:41:43 +00003213 /* If device is offline, then don't send */
3214 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3215 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003216 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003218
Stephen Hemmingeref879792009-09-22 19:41:43 +00003219 /* This is max DELAY, this has special meaning of
3220 * "never transmit"
3221 */
3222 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3223 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3224 return;
3225 }
3226
3227 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003228 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3229 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3230 /* build a new pkt */
3231 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003232
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003233 pkt_dev->skb = fill_packet(odev, pkt_dev);
3234 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003235 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003236 schedule();
3237 pkt_dev->clone_count--; /* back out increment, OOM */
3238 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003240 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003241 pkt_dev->allocated_skbs++;
3242 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003244
Stephen Hemmingeref879792009-09-22 19:41:43 +00003245 if (pkt_dev->delay && pkt_dev->last_ok)
3246 spin(pkt_dev, pkt_dev->next_tx);
3247
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003248 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3249 txq = netdev_get_tx_queue(odev, queue_map);
3250
3251 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003252
Tom Herbert734664982011-11-28 16:32:44 +00003253 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003254 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003255 pkt_dev->last_ok = 0;
3256 goto unlock;
3257 }
3258 atomic_inc(&(pkt_dev->skb->users));
3259 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Stephen Hemmingeref879792009-09-22 19:41:43 +00003261 switch (ret) {
3262 case NETDEV_TX_OK:
3263 txq_trans_update(txq);
3264 pkt_dev->last_ok = 1;
3265 pkt_dev->sofar++;
3266 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003267 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003268 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003269 case NET_XMIT_DROP:
3270 case NET_XMIT_CN:
3271 case NET_XMIT_POLICED:
3272 /* skb has been consumed */
3273 pkt_dev->errors++;
3274 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003275 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003276 net_info_ratelimited("%s xmit error: %d\n",
3277 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003278 pkt_dev->errors++;
3279 /* fallthru */
3280 case NETDEV_TX_LOCKED:
3281 case NETDEV_TX_BUSY:
3282 /* Retry it next time */
3283 atomic_dec(&(pkt_dev->skb->users));
3284 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003285 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003286unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003287 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 /* If pkt_dev->count is zero, then run forever */
3290 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003291 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 /* Done with this */
3294 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003295 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003298/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 * Main loop of the thread goes here
3300 */
3301
David S. Milleree74baa2007-01-01 20:51:53 -08003302static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
3304 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003305 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003306 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
David S. Milleree74baa2007-01-01 20:51:53 -08003309 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003312 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Joe Perchesf9467ea2010-06-21 12:29:14 +00003314 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
David S. Milleree74baa2007-01-01 20:51:53 -08003316 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003317
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003318 set_freezable();
3319
David S. Milleree74baa2007-01-01 20:51:53 -08003320 while (!kthread_should_stop()) {
3321 pkt_dev = next_to_run(t);
3322
Stephen Hemmingeref879792009-09-22 19:41:43 +00003323 if (unlikely(!pkt_dev && t->control == 0)) {
Eric Dumazet551eaff2010-11-21 10:26:44 -08003324 if (pktgen_exiting)
3325 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003326 wait_event_interruptible_timeout(t->queue,
3327 t->control != 0,
3328 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003329 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003330 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003331 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 __set_current_state(TASK_RUNNING);
3334
Stephen Hemmingeref879792009-09-22 19:41:43 +00003335 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 pktgen_xmit(pkt_dev);
3337
Stephen Hemmingeref879792009-09-22 19:41:43 +00003338 if (need_resched())
3339 pktgen_resched(pkt_dev);
3340 else
3341 cpu_relax();
3342 }
3343
Luiz Capitulino222f1802006-03-20 22:16:13 -08003344 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 pktgen_stop(t);
3346 t->control &= ~(T_STOP);
3347 }
3348
Luiz Capitulino222f1802006-03-20 22:16:13 -08003349 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 pktgen_run(t);
3351 t->control &= ~(T_RUN);
3352 }
3353
Luiz Capitulino222f1802006-03-20 22:16:13 -08003354 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003356 t->control &= ~(T_REMDEVALL);
3357 }
3358
Luiz Capitulino222f1802006-03-20 22:16:13 -08003359 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003360 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 t->control &= ~(T_REMDEV);
3362 }
3363
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003364 try_to_freeze();
3365
David S. Milleree74baa2007-01-01 20:51:53 -08003366 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Joe Perchesf9467ea2010-06-21 12:29:14 +00003369 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003370 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Joe Perchesf9467ea2010-06-21 12:29:14 +00003372 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003373 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Joe Perchesf9467ea2010-06-21 12:29:14 +00003375 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003376 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003377
Eric Dumazet551eaff2010-11-21 10:26:44 -08003378 /* Wait for kthread_stop */
3379 while (!kthread_should_stop()) {
3380 set_current_state(TASK_INTERRUPTIBLE);
3381 schedule();
3382 }
3383 __set_current_state(TASK_RUNNING);
3384
David S. Milleree74baa2007-01-01 20:51:53 -08003385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
3387
Luiz Capitulino222f1802006-03-20 22:16:13 -08003388static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003389 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003391 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003392 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Eric Dumazet3e984842009-11-24 14:50:53 -08003394 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003395 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003396 if (strncmp(p->odevname, ifname, len) == 0) {
3397 if (p->odevname[len]) {
3398 if (exact || p->odevname[len] != '@')
3399 continue;
3400 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003401 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003402 break;
3403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Luiz Capitulino222f1802006-03-20 22:16:13 -08003405 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003406 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003407 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
3409
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003410/*
3411 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 */
3413
Luiz Capitulino222f1802006-03-20 22:16:13 -08003414static int add_dev_to_thread(struct pktgen_thread *t,
3415 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
3417 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Luiz Capitulino222f1802006-03-20 22:16:13 -08003419 if_lock(t);
3420
3421 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003422 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003423 rv = -EBUSY;
3424 goto out;
3425 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003426
3427 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003428 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 pkt_dev->running = 0;
3430
Luiz Capitulino222f1802006-03-20 22:16:13 -08003431out:
3432 if_unlock(t);
3433 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
3436/* Called under thread lock */
3437
Luiz Capitulino222f1802006-03-20 22:16:13 -08003438static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003440 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003441 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003442 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 /* We don't allow a device to be on several threads */
3445
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003446 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3447 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003448 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003449 return -EBUSY;
3450 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003451
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003452 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003453 if (!pkt_dev)
3454 return -ENOMEM;
3455
Eric Dumazet593f63b2009-11-23 01:44:37 +00003456 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003457 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003458 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003459 if (pkt_dev->flows == NULL) {
3460 kfree(pkt_dev);
3461 return -ENOMEM;
3462 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003463
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003464 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003465 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003466 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003467 pkt_dev->count = pg_count_d;
3468 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003469 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003470 pkt_dev->udp_src_max = 9;
3471 pkt_dev->udp_dst_min = 9;
3472 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003473 pkt_dev->vlan_p = 0;
3474 pkt_dev->vlan_cfi = 0;
3475 pkt_dev->vlan_id = 0xffff;
3476 pkt_dev->svlan_p = 0;
3477 pkt_dev->svlan_cfi = 0;
3478 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003479 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003480
Stephen Hemminger39df2322007-03-04 16:11:51 -08003481 err = pktgen_setup_dev(pkt_dev, ifname);
3482 if (err)
3483 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003484 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3485 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003486
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003487 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3488 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003489 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003490 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003491 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003492 err = -EINVAL;
3493 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003494 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003495#ifdef CONFIG_XFRM
3496 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3497 pkt_dev->ipsproto = IPPROTO_ESP;
3498#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003499
3500 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003501out2:
3502 dev_put(pkt_dev->odev);
3503out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003504#ifdef CONFIG_XFRM
3505 free_SAs(pkt_dev);
3506#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003507 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003508 kfree(pkt_dev);
3509 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
David S. Milleree74baa2007-01-01 20:51:53 -08003512static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003514 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003515 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003516 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003518 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3519 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003521 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003522 return -ENOMEM;
3523 }
3524
Luiz Capitulino222f1802006-03-20 22:16:13 -08003525 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003527
David S. Milleree74baa2007-01-01 20:51:53 -08003528 INIT_LIST_HEAD(&t->if_list);
3529
3530 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003531 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003532
Eric Dumazet94dcf292011-03-22 16:30:45 -07003533 p = kthread_create_on_node(pktgen_thread_worker,
3534 t,
3535 cpu_to_node(cpu),
3536 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003537 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003538 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003539 list_del(&t->th_list);
3540 kfree(t);
3541 return PTR_ERR(p);
3542 }
3543 kthread_bind(p, cpu);
3544 t->tsk = p;
3545
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003546 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3547 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003548 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003549 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003550 PG_PROC_DIR, t->tsk->comm);
3551 kthread_stop(p);
3552 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003553 kfree(t);
3554 return -EINVAL;
3555 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003556
David S. Milleree74baa2007-01-01 20:51:53 -08003557 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003558 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560 return 0;
3561}
3562
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003563/*
3564 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566static void _rem_dev_from_if_list(struct pktgen_thread *t,
3567 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003569 struct list_head *q, *n;
3570 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003572 list_for_each_safe(q, n, &t->if_list) {
3573 p = list_entry(q, struct pktgen_dev, list);
3574 if (p == pkt_dev)
3575 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
3577}
3578
Luiz Capitulino222f1802006-03-20 22:16:13 -08003579static int pktgen_remove_device(struct pktgen_thread *t,
3580 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
3582
Joe Perchesf9467ea2010-06-21 12:29:14 +00003583 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Luiz Capitulino222f1802006-03-20 22:16:13 -08003585 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003586 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003587 pktgen_stop_device(pkt_dev);
3588 }
3589
3590 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592 if (pkt_dev->odev) {
3593 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003594 pkt_dev->odev = NULL;
3595 }
3596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 /* And update the thread if_list */
3598
3599 _rem_dev_from_if_list(t, pkt_dev);
3600
Stephen Hemminger39df2322007-03-04 16:11:51 -08003601 if (pkt_dev->entry)
3602 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003604#ifdef CONFIG_XFRM
3605 free_SAs(pkt_dev);
3606#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003607 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003608 if (pkt_dev->page)
3609 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612}
3613
Luiz Capitulino222f1802006-03-20 22:16:13 -08003614static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
3616 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003617 struct proc_dir_entry *pe;
WANG Congce14f892011-05-22 00:52:08 +00003618 int ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003619
Joe Perchesf9467ea2010-06-21 12:29:14 +00003620 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003622 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003623 if (!pg_proc_dir)
3624 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
Wang Chen25296d52008-02-28 14:11:49 -08003626 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003627 if (pe == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003628 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
WANG Congce14f892011-05-22 00:52:08 +00003629 ret = -EINVAL;
3630 goto remove_dir;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003634
John Hawkes670c02c2005-10-13 09:30:31 -07003635 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003636 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
David S. Milleree74baa2007-01-01 20:51:53 -08003638 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003639 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00003640 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3641 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003642 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003643
3644 if (list_empty(&pktgen_threads)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003645 pr_err("ERROR: Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003646 ret = -ENODEV;
3647 goto unregister;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003648 }
3649
Luiz Capitulino222f1802006-03-20 22:16:13 -08003650 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003651
3652 unregister:
3653 unregister_netdevice_notifier(&pktgen_notifier_block);
3654 remove_proc_entry(PGCTRL, pg_proc_dir);
3655 remove_dir:
3656 proc_net_remove(&init_net, PG_PROC_DIR);
3657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658}
3659
3660static void __exit pg_cleanup(void)
3661{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003662 struct pktgen_thread *t;
3663 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003664 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Luiz Capitulino222f1802006-03-20 22:16:13 -08003666 /* Stop all interfaces & threads */
Eric Dumazet551eaff2010-11-21 10:26:44 -08003667 pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Eric Dumazetc57b5462012-05-09 13:29:51 +00003669 mutex_lock(&pktgen_thread_lock);
Eric Dumazetd4b11332012-05-17 23:52:26 +00003670 list_splice_init(&pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003671 mutex_unlock(&pktgen_thread_lock);
3672
3673 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003674 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003675 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003676 kthread_stop(t->tsk);
3677 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Luiz Capitulino222f1802006-03-20 22:16:13 -08003680 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 unregister_netdevice_notifier(&pktgen_notifier_block);
3682
Luiz Capitulino222f1802006-03-20 22:16:13 -08003683 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003684 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003685 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686}
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688module_init(pg_init);
3689module_exit(pg_cleanup);
3690
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003691MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692MODULE_DESCRIPTION("Packet Generator tool");
3693MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003694MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003696MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003698MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003700MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003702MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");