blob: 6ed9e27d8202c9d4bf2ec43537a83a444c7d8822 [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
Luiz Capitulino222f1802006-03-20 22:16:13 -0800251 int min_pkt_size; /* = ETH_ZLEN; */
252 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700253 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800254 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800255 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000256 u64 delay; /* nano-seconds */
257
Luiz Capitulino222f1802006-03-20 22:16:13 -0800258 __u64 count; /* Default No packets to send */
259 __u64 sofar; /* How many pkts we've sent so far */
260 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800261 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Luiz Capitulino222f1802006-03-20 22:16:13 -0800263 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Luiz Capitulino222f1802006-03-20 22:16:13 -0800265 __u64 allocated_skbs;
266 __u32 clone_count;
267 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000268 * Or a failed transmit of some sort?
269 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800270 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000271 ktime_t next_tx;
272 ktime_t started_at;
273 ktime_t stopped_at;
274 u64 idle_acc; /* nano-seconds */
275
Luiz Capitulino222f1802006-03-20 22:16:13 -0800276 __u32 seq_num;
277
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000278 int clone_skb; /*
279 * Use multiple SKBs during packet gen.
280 * If this number is greater than 1, then
281 * that many copies of the same packet will be
282 * sent before a new packet is allocated.
283 * If you want to send 1024 identical packets
284 * before creating a new packet,
285 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800286 */
287
288 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
289 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
290 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
291 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
292
293 struct in6_addr in6_saddr;
294 struct in6_addr in6_daddr;
295 struct in6_addr cur_in6_daddr;
296 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800298 struct in6_addr min_in6_daddr;
299 struct in6_addr max_in6_daddr;
300 struct in6_addr min_in6_saddr;
301 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Luiz Capitulino222f1802006-03-20 22:16:13 -0800303 /* If we're doing ranges, random or incremental, then this
304 * defines the min/max for those ranges.
305 */
Al 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 */
323 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
324 __be32 labels[MAX_MPLS_LABELS];
325
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700326 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
327 __u8 vlan_p;
328 __u8 vlan_cfi;
329 __u16 vlan_id; /* 0xffff means no vlan tag */
330
331 __u8 svlan_p;
332 __u8 svlan_cfi;
333 __u16 svlan_id; /* 0xffff means no svlan tag */
334
Luiz Capitulino222f1802006-03-20 22:16:13 -0800335 __u32 src_mac_count; /* How many MACs to iterate through */
336 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Luiz Capitulino222f1802006-03-20 22:16:13 -0800338 unsigned char dst_mac[ETH_ALEN];
339 unsigned char src_mac[ETH_ALEN];
340
341 __u32 cur_dst_mac_offset;
342 __u32 cur_src_mac_offset;
Al 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;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800376 unsigned cflows; /* Concurrent flows (config) */
377 unsigned lflow; /* Flow length (config) */
378 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700379 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700380
381 u16 queue_map_min;
382 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000383 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000384 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700385
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700386#ifdef CONFIG_XFRM
387 __u8 ipsmode; /* IPSEC mode (config) */
388 __u8 ipsproto; /* IPSEC type (config) */
389#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800390 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391};
392
393struct pktgen_hdr {
Al 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
Luiz Capitulino222f1802006-03-20 22:16:13 -0800452static unsigned int scan_ip6(const char *s, char ip[16]);
453static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000456static int pg_count_d __read_mostly = 1000;
457static int pg_delay_d __read_mostly;
458static int pg_clone_skb_d __read_mostly;
459static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Luiz Capitulino222fa072006-03-20 22:24:27 -0800461static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800462static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static struct notifier_block pktgen_notifier_block = {
465 .notifier_call = pktgen_device_event,
466};
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900469 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
471 */
472
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700473static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800474{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000475 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000479static ssize_t pgctrl_write(struct file *file, const char __user *buf,
480 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700483 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Luiz Capitulino222f1802006-03-20 22:16:13 -0800485 if (!capable(CAP_NET_ADMIN)) {
486 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700490 if (count > sizeof(data))
491 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700494 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700495 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800496 }
497 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Luiz Capitulino222f1802006-03-20 22:16:13 -0800499 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 pktgen_stop_all_threads_ifs();
501
Luiz Capitulino222f1802006-03-20 22:16:13 -0800502 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 pktgen_run_all_threads();
504
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800505 else if (!strcmp(data, "reset"))
506 pktgen_reset_all_threads();
507
Luiz Capitulino222f1802006-03-20 22:16:13 -0800508 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000509 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 err = count;
512
Luiz Capitulino222f1802006-03-20 22:16:13 -0800513out:
514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700517static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700519 return single_open(file, pgctrl_show, PDE(inode)->data);
520}
521
Arjan van de Ven9a321442007-02-12 00:55:35 -0800522static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800523 .owner = THIS_MODULE,
524 .open = pgctrl_open,
525 .read = seq_read,
526 .llseek = seq_lseek,
527 .write = pgctrl_write,
528 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700529};
530
531static int pktgen_if_show(struct seq_file *seq, void *v)
532{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000533 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000534 ktime_t stopped;
535 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Luiz Capitulino222f1802006-03-20 22:16:13 -0800537 seq_printf(seq,
538 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
539 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
540 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Luiz Capitulino222f1802006-03-20 22:16:13 -0800542 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000543 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
544 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000545 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Luiz Capitulino222f1802006-03-20 22:16:13 -0800547 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
548 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Robert Olsson45b270f2007-08-28 15:45:55 -0700550 seq_printf(seq,
551 " queue_map_min: %u queue_map_max: %u\n",
552 pkt_dev->queue_map_min,
553 pkt_dev->queue_map_max);
554
John Fastabend9e50e3a2010-11-16 19:12:28 +0000555 if (pkt_dev->skb_priority)
556 seq_printf(seq, " skb_priority: %u\n",
557 pkt_dev->skb_priority);
558
Luiz Capitulino222f1802006-03-20 22:16:13 -0800559 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800561 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
562 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
563 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
564 seq_printf(seq,
565 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
566 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Luiz Capitulino222f1802006-03-20 22:16:13 -0800568 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
569 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
570 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
571 seq_printf(seq,
572 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
573 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000575 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800576 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000577 " dst_min: %s dst_max: %s\n",
578 pkt_dev->dst_min, pkt_dev->dst_max);
579 seq_printf(seq,
580 " src_min: %s src_max: %s\n",
581 pkt_dev->src_min, pkt_dev->src_max);
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700584 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Johannes Berge1749612008-10-27 15:59:26 -0700586 seq_printf(seq, "%pM ",
587 is_zero_ether_addr(pkt_dev->src_mac) ?
588 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Luiz Capitulino222f1802006-03-20 22:16:13 -0800590 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700591 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Luiz Capitulino222f1802006-03-20 22:16:13 -0800593 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000594 " udp_src_min: %d udp_src_max: %d"
595 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800596 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
597 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Luiz Capitulino222f1802006-03-20 22:16:13 -0800599 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800600 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700601 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800603 if (pkt_dev->nr_labels) {
604 unsigned i;
605 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700606 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800607 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
608 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
609 }
610
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000611 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700612 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000613 pkt_dev->vlan_id, pkt_dev->vlan_p,
614 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700615
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000616 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700617 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000618 pkt_dev->svlan_id, pkt_dev->svlan_p,
619 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700620
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000621 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700622 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700623
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000624 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700625 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700626
Robert Olssone99b99b2010-03-18 22:44:30 +0000627 if (pkt_dev->node >= 0)
628 seq_printf(seq, " node: %d\n", pkt_dev->node);
629
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800630 seq_printf(seq, " Flags: ");
631
Luiz Capitulino222f1802006-03-20 22:16:13 -0800632 if (pkt_dev->flags & F_IPV6)
633 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Luiz Capitulino222f1802006-03-20 22:16:13 -0800635 if (pkt_dev->flags & F_IPSRC_RND)
636 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Luiz Capitulino222f1802006-03-20 22:16:13 -0800638 if (pkt_dev->flags & F_IPDST_RND)
639 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Luiz Capitulino222f1802006-03-20 22:16:13 -0800641 if (pkt_dev->flags & F_TXSIZE_RND)
642 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Luiz Capitulino222f1802006-03-20 22:16:13 -0800644 if (pkt_dev->flags & F_UDPSRC_RND)
645 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Luiz Capitulino222f1802006-03-20 22:16:13 -0800647 if (pkt_dev->flags & F_UDPDST_RND)
648 seq_printf(seq, "UDPDST_RND ");
649
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800650 if (pkt_dev->flags & F_MPLS_RND)
651 seq_printf(seq, "MPLS_RND ");
652
Robert Olsson45b270f2007-08-28 15:45:55 -0700653 if (pkt_dev->flags & F_QUEUE_MAP_RND)
654 seq_printf(seq, "QUEUE_MAP_RND ");
655
Robert Olssone6fce5b2008-08-07 02:23:01 -0700656 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
657 seq_printf(seq, "QUEUE_MAP_CPU ");
658
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700659 if (pkt_dev->cflows) {
660 if (pkt_dev->flags & F_FLOW_SEQ)
661 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
662 else
663 seq_printf(seq, "FLOW_RND ");
664 }
665
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700666#ifdef CONFIG_XFRM
667 if (pkt_dev->flags & F_IPSEC_ON)
668 seq_printf(seq, "IPSEC ");
669#endif
670
Luiz Capitulino222f1802006-03-20 22:16:13 -0800671 if (pkt_dev->flags & F_MACSRC_RND)
672 seq_printf(seq, "MACSRC_RND ");
673
674 if (pkt_dev->flags & F_MACDST_RND)
675 seq_printf(seq, "MACDST_RND ");
676
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700677 if (pkt_dev->flags & F_VID_RND)
678 seq_printf(seq, "VID_RND ");
679
680 if (pkt_dev->flags & F_SVID_RND)
681 seq_printf(seq, "SVID_RND ");
682
Robert Olssone99b99b2010-03-18 22:44:30 +0000683 if (pkt_dev->flags & F_NODE)
684 seq_printf(seq, "NODE_ALLOC ");
685
Luiz Capitulino222f1802006-03-20 22:16:13 -0800686 seq_puts(seq, "\n");
687
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000688 /* not really stopped, more like last-running-at */
689 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
690 idle = pkt_dev->idle_acc;
691 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800692
693 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000694 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800695 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000696 (unsigned long long)pkt_dev->errors);
697
698 seq_printf(seq,
699 " started: %lluus stopped: %lluus idle: %lluus\n",
700 (unsigned long long) ktime_to_us(pkt_dev->started_at),
701 (unsigned long long) ktime_to_us(stopped),
702 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800703
704 seq_printf(seq,
705 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700706 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
707 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Luiz Capitulino222f1802006-03-20 22:16:13 -0800709 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800711 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
712 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
713 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
714 } else
715 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700716 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Luiz Capitulino222f1802006-03-20 22:16:13 -0800718 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700719 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Robert Olsson45b270f2007-08-28 15:45:55 -0700721 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
722
Luiz Capitulino222f1802006-03-20 22:16:13 -0800723 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800726 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800728 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700730 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800733
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000734static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
735 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800736{
737 int i = 0;
738 *num = 0;
739
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700740 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000741 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800742 char c;
743 *num <<= 4;
744 if (get_user(c, &user_buffer[i]))
745 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000746 value = hex_to_bin(c);
747 if (value >= 0)
748 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800749 else
750 break;
751 }
752 return i;
753}
754
Luiz Capitulino222f1802006-03-20 22:16:13 -0800755static int count_trail_chars(const char __user * user_buffer,
756 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 int i;
759
760 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800761 char c;
762 if (get_user(c, &user_buffer[i]))
763 return -EFAULT;
764 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 case '\"':
766 case '\n':
767 case '\r':
768 case '\t':
769 case ' ':
770 case '=':
771 break;
772 default:
773 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776done:
777 return i;
778}
779
Luiz Capitulino222f1802006-03-20 22:16:13 -0800780static unsigned long num_arg(const char __user * user_buffer,
781 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000783 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800785
Paul Gortmakerd6182222010-10-18 12:14:44 +0000786 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800787 char c;
788 if (get_user(c, &user_buffer[i]))
789 return -EFAULT;
790 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800792 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 } else
794 break;
795 }
796 return i;
797}
798
Luiz Capitulino222f1802006-03-20 22:16:13 -0800799static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000801 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Paul Gortmakerd6182222010-10-18 12:14:44 +0000803 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800804 char c;
805 if (get_user(c, &user_buffer[i]))
806 return -EFAULT;
807 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 case '\"':
809 case '\n':
810 case '\r':
811 case '\t':
812 case ' ':
813 goto done_str;
814 break;
815 default:
816 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return i;
821}
822
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800823static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
824{
825 unsigned n = 0;
826 char c;
827 ssize_t i = 0;
828 int len;
829
830 pkt_dev->nr_labels = 0;
831 do {
832 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700833 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800834 if (len <= 0)
835 return len;
836 pkt_dev->labels[n] = htonl(tmp);
837 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
838 pkt_dev->flags |= F_MPLS_RND;
839 i += len;
840 if (get_user(c, &buffer[i]))
841 return -EFAULT;
842 i++;
843 n++;
844 if (n >= MAX_MPLS_LABELS)
845 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700846 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800847
848 pkt_dev->nr_labels = n;
849 return i;
850}
851
Luiz Capitulino222f1802006-03-20 22:16:13 -0800852static ssize_t pktgen_if_write(struct file *file,
853 const char __user * user_buffer, size_t count,
854 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Joe Perches8a994a72010-07-12 10:50:23 +0000856 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800857 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000858 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 char name[16], valstr[32];
860 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800861 char *pg_result = NULL;
862 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800864
865 pg_result = &(pkt_dev->result[0]);
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000868 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return -EINVAL;
870 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800871
Paul Gortmakerd6182222010-10-18 12:14:44 +0000872 max = count;
873 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800874 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000875 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800876 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000878 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* Read variable name */
881
882 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000883 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800884 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800887 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EFAULT;
889 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800890
891 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800893 if (len < 0)
894 return len;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 i += len;
897
898 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000899 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700900 char tb[copy + 1];
901 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700903 tb[copy] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700904 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800905 (unsigned long)count, tb);
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (!strcmp(name, "min_pkt_size")) {
909 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000910 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800911 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800914 if (value < 14 + 20 + 8)
915 value = 14 + 20 + 8;
916 if (value != pkt_dev->min_pkt_size) {
917 pkt_dev->min_pkt_size = value;
918 pkt_dev->cur_pkt_size = value;
919 }
920 sprintf(pg_result, "OK: min_pkt_size=%u",
921 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return count;
923 }
924
Luiz Capitulino222f1802006-03-20 22:16:13 -0800925 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000927 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800928 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800931 if (value < 14 + 20 + 8)
932 value = 14 + 20 + 8;
933 if (value != pkt_dev->max_pkt_size) {
934 pkt_dev->max_pkt_size = value;
935 pkt_dev->cur_pkt_size = value;
936 }
937 sprintf(pg_result, "OK: max_pkt_size=%u",
938 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return count;
940 }
941
Luiz Capitulino222f1802006-03-20 22:16:13 -0800942 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (!strcmp(name, "pkt_size")) {
945 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000946 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800947 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800950 if (value < 14 + 20 + 8)
951 value = 14 + 20 + 8;
952 if (value != pkt_dev->min_pkt_size) {
953 pkt_dev->min_pkt_size = value;
954 pkt_dev->max_pkt_size = value;
955 pkt_dev->cur_pkt_size = value;
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
958 return count;
959 }
960
Luiz Capitulino222f1802006-03-20 22:16:13 -0800961 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000963 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800964 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800967 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 sprintf(pg_result, "OK: debug=%u", debug);
969 return count;
970 }
971
Luiz Capitulino222f1802006-03-20 22:16:13 -0800972 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000974 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 i += len;
978 pkt_dev->nfrags = value;
979 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
980 return count;
981 }
982 if (!strcmp(name, "delay")) {
983 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000984 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800985 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000988 if (value == 0x7FFFFFFF)
989 pkt_dev->delay = ULLONG_MAX;
990 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000991 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000992
993 sprintf(pg_result, "OK: delay=%llu",
994 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return count;
996 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000997 if (!strcmp(name, "rate")) {
998 len = num_arg(&user_buffer[i], 10, &value);
999 if (len < 0)
1000 return len;
1001
1002 i += len;
1003 if (!value)
1004 return len;
1005 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1006 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001007 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001008
1009 sprintf(pg_result, "OK: rate=%lu", value);
1010 return count;
1011 }
1012 if (!strcmp(name, "ratep")) {
1013 len = num_arg(&user_buffer[i], 10, &value);
1014 if (len < 0)
1015 return len;
1016
1017 i += len;
1018 if (!value)
1019 return len;
1020 pkt_dev->delay = NSEC_PER_SEC/value;
1021 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001022 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001023
1024 sprintf(pg_result, "OK: rate=%lu", value);
1025 return count;
1026 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001027 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001029 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001030 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001033 if (value != pkt_dev->udp_src_min) {
1034 pkt_dev->udp_src_min = value;
1035 pkt_dev->cur_udp_src = value;
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1038 return count;
1039 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001040 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001042 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001043 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001046 if (value != pkt_dev->udp_dst_min) {
1047 pkt_dev->udp_dst_min = value;
1048 pkt_dev->cur_udp_dst = value;
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1051 return count;
1052 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001053 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001055 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001056 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001059 if (value != pkt_dev->udp_src_max) {
1060 pkt_dev->udp_src_max = value;
1061 pkt_dev->cur_udp_src = value;
1062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1064 return count;
1065 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001066 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001068 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001069 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001072 if (value != pkt_dev->udp_dst_max) {
1073 pkt_dev->udp_dst_max = value;
1074 pkt_dev->cur_udp_dst = value;
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1077 return count;
1078 }
1079 if (!strcmp(name, "clone_skb")) {
1080 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001081 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001082 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001085 pkt_dev->clone_skb = value;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1088 return count;
1089 }
1090 if (!strcmp(name, "count")) {
1091 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001092 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001093 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 i += len;
1096 pkt_dev->count = value;
1097 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001098 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return count;
1100 }
1101 if (!strcmp(name, "src_mac_count")) {
1102 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001103 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001104 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 i += len;
1107 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001108 pkt_dev->src_mac_count = value;
1109 pkt_dev->cur_src_mac_offset = 0;
1110 }
1111 sprintf(pg_result, "OK: src_mac_count=%d",
1112 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return count;
1114 }
1115 if (!strcmp(name, "dst_mac_count")) {
1116 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001117 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001118 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 i += len;
1121 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001122 pkt_dev->dst_mac_count = value;
1123 pkt_dev->cur_dst_mac_offset = 0;
1124 }
1125 sprintf(pg_result, "OK: dst_mac_count=%d",
1126 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return count;
1128 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001129 if (!strcmp(name, "node")) {
1130 len = num_arg(&user_buffer[i], 10, &value);
1131 if (len < 0)
1132 return len;
1133
1134 i += len;
1135
1136 if (node_possible(value)) {
1137 pkt_dev->node = value;
1138 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001139 if (pkt_dev->page) {
1140 put_page(pkt_dev->page);
1141 pkt_dev->page = NULL;
1142 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001143 }
1144 else
1145 sprintf(pg_result, "ERROR: node not possible");
1146 return count;
1147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001149 char f[32];
1150 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001152 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001153 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (copy_from_user(f, &user_buffer[i], len))
1156 return -EFAULT;
1157 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001158 if (strcmp(f, "IPSRC_RND") == 0)
1159 pkt_dev->flags |= F_IPSRC_RND;
1160
1161 else if (strcmp(f, "!IPSRC_RND") == 0)
1162 pkt_dev->flags &= ~F_IPSRC_RND;
1163
1164 else if (strcmp(f, "TXSIZE_RND") == 0)
1165 pkt_dev->flags |= F_TXSIZE_RND;
1166
1167 else if (strcmp(f, "!TXSIZE_RND") == 0)
1168 pkt_dev->flags &= ~F_TXSIZE_RND;
1169
1170 else if (strcmp(f, "IPDST_RND") == 0)
1171 pkt_dev->flags |= F_IPDST_RND;
1172
1173 else if (strcmp(f, "!IPDST_RND") == 0)
1174 pkt_dev->flags &= ~F_IPDST_RND;
1175
1176 else if (strcmp(f, "UDPSRC_RND") == 0)
1177 pkt_dev->flags |= F_UDPSRC_RND;
1178
1179 else if (strcmp(f, "!UDPSRC_RND") == 0)
1180 pkt_dev->flags &= ~F_UDPSRC_RND;
1181
1182 else if (strcmp(f, "UDPDST_RND") == 0)
1183 pkt_dev->flags |= F_UDPDST_RND;
1184
1185 else if (strcmp(f, "!UDPDST_RND") == 0)
1186 pkt_dev->flags &= ~F_UDPDST_RND;
1187
1188 else if (strcmp(f, "MACSRC_RND") == 0)
1189 pkt_dev->flags |= F_MACSRC_RND;
1190
1191 else if (strcmp(f, "!MACSRC_RND") == 0)
1192 pkt_dev->flags &= ~F_MACSRC_RND;
1193
1194 else if (strcmp(f, "MACDST_RND") == 0)
1195 pkt_dev->flags |= F_MACDST_RND;
1196
1197 else if (strcmp(f, "!MACDST_RND") == 0)
1198 pkt_dev->flags &= ~F_MACDST_RND;
1199
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001200 else if (strcmp(f, "MPLS_RND") == 0)
1201 pkt_dev->flags |= F_MPLS_RND;
1202
1203 else if (strcmp(f, "!MPLS_RND") == 0)
1204 pkt_dev->flags &= ~F_MPLS_RND;
1205
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001206 else if (strcmp(f, "VID_RND") == 0)
1207 pkt_dev->flags |= F_VID_RND;
1208
1209 else if (strcmp(f, "!VID_RND") == 0)
1210 pkt_dev->flags &= ~F_VID_RND;
1211
1212 else if (strcmp(f, "SVID_RND") == 0)
1213 pkt_dev->flags |= F_SVID_RND;
1214
1215 else if (strcmp(f, "!SVID_RND") == 0)
1216 pkt_dev->flags &= ~F_SVID_RND;
1217
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001218 else if (strcmp(f, "FLOW_SEQ") == 0)
1219 pkt_dev->flags |= F_FLOW_SEQ;
1220
Robert Olsson45b270f2007-08-28 15:45:55 -07001221 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1222 pkt_dev->flags |= F_QUEUE_MAP_RND;
1223
1224 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1225 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001226
1227 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1228 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1229
1230 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1231 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001232#ifdef CONFIG_XFRM
1233 else if (strcmp(f, "IPSEC") == 0)
1234 pkt_dev->flags |= F_IPSEC_ON;
1235#endif
1236
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001237 else if (strcmp(f, "!IPV6") == 0)
1238 pkt_dev->flags &= ~F_IPV6;
1239
Robert Olssone99b99b2010-03-18 22:44:30 +00001240 else if (strcmp(f, "NODE_ALLOC") == 0)
1241 pkt_dev->flags |= F_NODE;
1242
1243 else if (strcmp(f, "!NODE_ALLOC") == 0)
1244 pkt_dev->flags &= ~F_NODE;
1245
Luiz Capitulino222f1802006-03-20 22:16:13 -08001246 else {
1247 sprintf(pg_result,
1248 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1249 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001250 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001251 "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 -08001252 return count;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1255 return count;
1256 }
1257 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1258 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001259 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001264 buf[len] = 0;
1265 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1266 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1267 strncpy(pkt_dev->dst_min, buf, len);
1268 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1269 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1270 }
1271 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001272 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001273 pkt_dev->dst_min);
1274 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1276 return count;
1277 }
1278 if (!strcmp(name, "dst_max")) {
1279 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001280 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001281 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Luiz Capitulino222f1802006-03-20 22:16:13 -08001284 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return -EFAULT;
1286
Luiz Capitulino222f1802006-03-20 22:16:13 -08001287 buf[len] = 0;
1288 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1289 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1290 strncpy(pkt_dev->dst_max, buf, len);
1291 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1292 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1293 }
1294 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001295 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001296 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 i += len;
1298 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1299 return count;
1300 }
1301 if (!strcmp(name, "dst6")) {
1302 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001303 if (len < 0)
1304 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 pkt_dev->flags |= F_IPV6;
1307
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001310 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001313 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1316
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001318 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 sprintf(pg_result, "OK: dst6=%s", buf);
1322 return count;
1323 }
1324 if (!strcmp(name, "dst6_min")) {
1325 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001326 if (len < 0)
1327 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 pkt_dev->flags |= F_IPV6;
1330
Luiz Capitulino222f1802006-03-20 22:16:13 -08001331 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001333 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001336 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1339 &pkt_dev->min_in6_daddr);
1340 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001341 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Luiz Capitulino222f1802006-03-20 22:16:13 -08001343 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 sprintf(pg_result, "OK: dst6_min=%s", buf);
1345 return count;
1346 }
1347 if (!strcmp(name, "dst6_max")) {
1348 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001349 if (len < 0)
1350 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 pkt_dev->flags |= F_IPV6;
1353
Luiz Capitulino222f1802006-03-20 22:16:13 -08001354 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001356 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Luiz Capitulino222f1802006-03-20 22:16:13 -08001361 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001362 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Luiz Capitulino222f1802006-03-20 22:16:13 -08001364 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 sprintf(pg_result, "OK: dst6_max=%s", buf);
1366 return count;
1367 }
1368 if (!strcmp(name, "src6")) {
1369 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001370 if (len < 0)
1371 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 pkt_dev->flags |= F_IPV6;
1374
Luiz Capitulino222f1802006-03-20 22:16:13 -08001375 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001377 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001380 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1383
Luiz Capitulino222f1802006-03-20 22:16:13 -08001384 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001385 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001386
1387 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 sprintf(pg_result, "OK: src6=%s", buf);
1389 return count;
1390 }
1391 if (!strcmp(name, "src_min")) {
1392 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001393 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001394 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001395
Luiz Capitulino222f1802006-03-20 22:16:13 -08001396 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001398 buf[len] = 0;
1399 if (strcmp(buf, pkt_dev->src_min) != 0) {
1400 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1401 strncpy(pkt_dev->src_min, buf, len);
1402 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1403 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1404 }
1405 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001406 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001407 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 i += len;
1409 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1410 return count;
1411 }
1412 if (!strcmp(name, "src_max")) {
1413 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001414 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001415 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001416
Luiz Capitulino222f1802006-03-20 22:16:13 -08001417 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001419 buf[len] = 0;
1420 if (strcmp(buf, pkt_dev->src_max) != 0) {
1421 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1422 strncpy(pkt_dev->src_max, buf, len);
1423 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1424 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1425 }
1426 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001427 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001428 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 i += len;
1430 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1431 return count;
1432 }
1433 if (!strcmp(name, "dst_mac")) {
1434 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001435 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001437 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001440 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001441 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001444 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EFAULT;
1446 i += len;
1447
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Andy Shevchenko451e07a2010-07-23 03:18:10 +00001449 int value;
1450
1451 value = hex_to_bin(*v);
1452 if (value >= 0)
1453 *m = *m * 16 + value;
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (*v == ':') {
1456 m++;
1457 *m = 0;
1458 }
1459 }
1460
1461 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001462 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1463 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 sprintf(pg_result, "OK: dstmac");
1466 return count;
1467 }
1468 if (!strcmp(name, "src_mac")) {
1469 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001470 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 unsigned char *m = pkt_dev->src_mac;
1472
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001473 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001476 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001477 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001480 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return -EFAULT;
1482 i += len;
1483
Luiz Capitulino222f1802006-03-20 22:16:13 -08001484 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Andy Shevchenko451e07a2010-07-23 03:18:10 +00001485 int value;
1486
1487 value = hex_to_bin(*v);
1488 if (value >= 0)
1489 *m = *m * 16 + value;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (*v == ':') {
1492 m++;
1493 *m = 0;
1494 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001497 /* Set up Src MAC */
1498 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1499 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1500
Luiz Capitulino222f1802006-03-20 22:16:13 -08001501 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return count;
1503 }
1504
Luiz Capitulino222f1802006-03-20 22:16:13 -08001505 if (!strcmp(name, "clear_counters")) {
1506 pktgen_clear_counters(pkt_dev);
1507 sprintf(pg_result, "OK: Clearing counters.\n");
1508 return count;
1509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 if (!strcmp(name, "flows")) {
1512 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001513 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001514 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 i += len;
1517 if (value > MAX_CFLOWS)
1518 value = MAX_CFLOWS;
1519
1520 pkt_dev->cflows = value;
1521 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1522 return count;
1523 }
1524
1525 if (!strcmp(name, "flowlen")) {
1526 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001527 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001528 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 i += len;
1531 pkt_dev->lflow = value;
1532 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1533 return count;
1534 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001535
Robert Olsson45b270f2007-08-28 15:45:55 -07001536 if (!strcmp(name, "queue_map_min")) {
1537 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001538 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001539 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001540
Robert Olsson45b270f2007-08-28 15:45:55 -07001541 i += len;
1542 pkt_dev->queue_map_min = value;
1543 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1544 return count;
1545 }
1546
1547 if (!strcmp(name, "queue_map_max")) {
1548 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001549 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001550 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001551
Robert Olsson45b270f2007-08-28 15:45:55 -07001552 i += len;
1553 pkt_dev->queue_map_max = value;
1554 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1555 return count;
1556 }
1557
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001558 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001559 unsigned n, cnt;
1560
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001561 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001562 if (len < 0)
1563 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001564 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001565 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001566 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001567 cnt += sprintf(pg_result + cnt,
1568 "%08x%s", ntohl(pkt_dev->labels[n]),
1569 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001570
1571 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1572 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1573 pkt_dev->svlan_id = 0xffff;
1574
1575 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001576 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001577 }
1578 return count;
1579 }
1580
1581 if (!strcmp(name, "vlan_id")) {
1582 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001583 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001584 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001585
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001586 i += len;
1587 if (value <= 4095) {
1588 pkt_dev->vlan_id = value; /* turn on VLAN */
1589
1590 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001591 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001592
1593 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001594 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001595
1596 pkt_dev->nr_labels = 0; /* turn off MPLS */
1597 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1598 } else {
1599 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1600 pkt_dev->svlan_id = 0xffff;
1601
1602 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001603 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001604 }
1605 return count;
1606 }
1607
1608 if (!strcmp(name, "vlan_p")) {
1609 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001610 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001611 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001612
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001613 i += len;
1614 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1615 pkt_dev->vlan_p = value;
1616 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1617 } else {
1618 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1619 }
1620 return count;
1621 }
1622
1623 if (!strcmp(name, "vlan_cfi")) {
1624 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001625 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001626 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001627
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001628 i += len;
1629 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1630 pkt_dev->vlan_cfi = value;
1631 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1632 } else {
1633 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1634 }
1635 return count;
1636 }
1637
1638 if (!strcmp(name, "svlan_id")) {
1639 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001640 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001641 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001642
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001643 i += len;
1644 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1645 pkt_dev->svlan_id = value; /* turn on SVLAN */
1646
1647 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001648 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001649
1650 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001651 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001652
1653 pkt_dev->nr_labels = 0; /* turn off MPLS */
1654 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1655 } else {
1656 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1657 pkt_dev->svlan_id = 0xffff;
1658
1659 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001660 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001661 }
1662 return count;
1663 }
1664
1665 if (!strcmp(name, "svlan_p")) {
1666 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001667 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001668 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001669
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001670 i += len;
1671 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1672 pkt_dev->svlan_p = value;
1673 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1674 } else {
1675 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1676 }
1677 return count;
1678 }
1679
1680 if (!strcmp(name, "svlan_cfi")) {
1681 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001682 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001683 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001684
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001685 i += len;
1686 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1687 pkt_dev->svlan_cfi = value;
1688 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1689 } else {
1690 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1691 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001692 return count;
1693 }
1694
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001695 if (!strcmp(name, "tos")) {
1696 __u32 tmp_value = 0;
1697 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001698 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001699 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001700
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001701 i += len;
1702 if (len == 2) {
1703 pkt_dev->tos = tmp_value;
1704 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1705 } else {
1706 sprintf(pg_result, "ERROR: tos must be 00-ff");
1707 }
1708 return count;
1709 }
1710
1711 if (!strcmp(name, "traffic_class")) {
1712 __u32 tmp_value = 0;
1713 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001714 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001715 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001716
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001717 i += len;
1718 if (len == 2) {
1719 pkt_dev->traffic_class = tmp_value;
1720 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1721 } else {
1722 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1723 }
1724 return count;
1725 }
1726
John Fastabend9e50e3a2010-11-16 19:12:28 +00001727 if (!strcmp(name, "skb_priority")) {
1728 len = num_arg(&user_buffer[i], 9, &value);
1729 if (len < 0)
1730 return len;
1731
1732 i += len;
1733 pkt_dev->skb_priority = value;
1734 sprintf(pg_result, "OK: skb_priority=%i",
1735 pkt_dev->skb_priority);
1736 return count;
1737 }
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1740 return -EINVAL;
1741}
1742
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001743static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001745 return single_open(file, pktgen_if_show, PDE(inode)->data);
1746}
1747
Arjan van de Ven9a321442007-02-12 00:55:35 -08001748static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001749 .owner = THIS_MODULE,
1750 .open = pktgen_if_open,
1751 .read = seq_read,
1752 .llseek = seq_lseek,
1753 .write = pktgen_if_write,
1754 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001755};
1756
1757static int pktgen_thread_show(struct seq_file *seq, void *v)
1758{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001759 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001760 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001762 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 seq_printf(seq, "Running: ");
1765
1766 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001767 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001768 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001769 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Luiz Capitulino222f1802006-03-20 22:16:13 -08001771 seq_printf(seq, "\nStopped: ");
1772
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001773 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001775 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001778 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001780 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Luiz Capitulino222f1802006-03-20 22:16:13 -08001782 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001787static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001788 const char __user * user_buffer,
1789 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Joe Perches8a994a72010-07-12 10:50:23 +00001791 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001792 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001793 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001795 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001798 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return -EINVAL;
1800 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001801
Paul Gortmakerd6182222010-10-18 12:14:44 +00001802 max = count;
1803 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001804 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001805 return len;
1806
Paul Gortmakerd6182222010-10-18 12:14:44 +00001807 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /* Read variable name */
1810
1811 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001812 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001813 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 memset(name, 0, sizeof(name));
1816 if (copy_from_user(name, &user_buffer[i], len))
1817 return -EFAULT;
1818 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001819
Luiz Capitulino222f1802006-03-20 22:16:13 -08001820 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001822 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001823 return len;
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 i += len;
1826
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001827 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001828 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1829 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Luiz Capitulino222f1802006-03-20 22:16:13 -08001831 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001832 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 ret = -EINVAL;
1834 goto out;
1835 }
1836
1837 pg_result = &(t->result[0]);
1838
Luiz Capitulino222f1802006-03-20 22:16:13 -08001839 if (!strcmp(name, "add_device")) {
1840 char f[32];
1841 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001843 if (len < 0) {
1844 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 goto out;
1846 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001847 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return -EFAULT;
1849 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001850 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001851 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001852 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001853 ret = count;
1854 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto out;
1856 }
1857
Luiz Capitulino222f1802006-03-20 22:16:13 -08001858 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001859 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001860 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001861 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001862 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001864 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 goto out;
1866 }
1867
Luiz Capitulino222f1802006-03-20 22:16:13 -08001868 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001869 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001870 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 goto out;
1872 }
1873
1874 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001875out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return ret;
1877}
1878
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001879static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001881 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Arjan van de Ven9a321442007-02-12 00:55:35 -08001884static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001885 .owner = THIS_MODULE,
1886 .open = pktgen_thread_open,
1887 .read = seq_read,
1888 .llseek = seq_lseek,
1889 .write = pktgen_thread_write,
1890 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001891};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001894static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 struct pktgen_thread *t;
1897 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001898 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001900 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001901 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001903 if (remove) {
1904 if_lock(t);
1905 pkt_dev->removal_mark = 1;
1906 t->control |= T_REMDEV;
1907 if_unlock(t);
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 break;
1910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001912 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001915/*
1916 * mark a device for removal
1917 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001918static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001921 const int max_tries = 10, msec_per_try = 125;
1922 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001923
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001924 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001925 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001926
Luiz Capitulino222f1802006-03-20 22:16:13 -08001927 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001928
1929 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001930 if (pkt_dev == NULL)
1931 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001932
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001933 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001934 pr_debug("%s: waiting for %s to disappear....\n",
1935 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001936 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001937 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001938
1939 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001940 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1941 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001942 break;
1943 }
1944
1945 }
1946
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001947 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001948}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001949
Stephen Hemminger39df2322007-03-04 16:11:51 -08001950static void pktgen_change_name(struct net_device *dev)
1951{
1952 struct pktgen_thread *t;
1953
1954 list_for_each_entry(t, &pktgen_threads, th_list) {
1955 struct pktgen_dev *pkt_dev;
1956
1957 list_for_each_entry(pkt_dev, &t->if_list, list) {
1958 if (pkt_dev->odev != dev)
1959 continue;
1960
1961 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1962
Alexey Dobriyan29753152009-08-28 23:34:43 -07001963 pkt_dev->entry = proc_create_data(dev->name, 0600,
1964 pg_proc_dir,
1965 &pktgen_if_fops,
1966 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001967 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001968 pr_err("can't move proc entry for '%s'\n",
1969 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001970 break;
1971 }
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Luiz Capitulino222f1802006-03-20 22:16:13 -08001975static int pktgen_device_event(struct notifier_block *unused,
1976 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001978 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001980 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001981 return NOTIFY_DONE;
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /* It is OK that we do not hold the group lock right now,
1984 * as we run under the RTNL lock.
1985 */
1986
1987 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001988 case NETDEV_CHANGENAME:
1989 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001993 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 return NOTIFY_DONE;
1998}
1999
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002000static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
2001 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002002{
2003 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00002004 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07002005
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002006 for (i = 0; ifname[i] != '@'; i++) {
2007 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002008 break;
2009
2010 b[i] = ifname[i];
2011 }
2012 b[i] = 0;
2013
2014 return dev_get_by_name(&init_net, b);
2015}
2016
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018/* Associate pktgen_dev with a device. */
2019
Stephen Hemminger39df2322007-03-04 16:11:51 -08002020static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002021{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002023 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (pkt_dev->odev) {
2027 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002028 pkt_dev->odev = NULL;
2029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Robert Olssone6fce5b2008-08-07 02:23:01 -07002031 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002033 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002034 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002038 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002039 err = -EINVAL;
2040 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002041 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002042 err = -ENETDOWN;
2043 } else {
2044 pkt_dev->odev = odev;
2045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002049 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
2052/* Read pkt_dev from the interface and set up internal pktgen_dev
2053 * structure to have the right information to create/send packets
2054 */
2055static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2056{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002057 int ntxq;
2058
Luiz Capitulino222f1802006-03-20 22:16:13 -08002059 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002060 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002061 sprintf(pkt_dev->result,
2062 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2063 return;
2064 }
2065
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002066 /* make sure that we don't pick a non-existing transmit queue */
2067 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002068
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002069 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002070 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2071 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2072 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002073 pkt_dev->queue_map_min = ntxq - 1;
2074 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002075 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002076 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2077 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2078 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002079 pkt_dev->queue_map_max = ntxq - 1;
2080 }
2081
Luiz Capitulino222f1802006-03-20 22:16:13 -08002082 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002084 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002085 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Luiz Capitulino222f1802006-03-20 22:16:13 -08002087 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002088 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Luiz Capitulino222f1802006-03-20 22:16:13 -08002090 /* Set up pkt size */
2091 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2092
2093 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002095 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 * gets exported
2097 */
2098
2099#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002100 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct inet6_dev *idev;
2102
Luiz Capitulino222f1802006-03-20 22:16:13 -08002103 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2104 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 set = 1;
2106 break;
2107 }
2108
Luiz Capitulino222f1802006-03-20 22:16:13 -08002109 if (!set) {
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /*
2112 * Use linklevel address if unconfigured.
2113 *
2114 * use ipv6_get_lladdr if/when it's get exported
2115 */
2116
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002117 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002118 idev = __in6_dev_get(pkt_dev->odev);
2119 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 struct inet6_ifaddr *ifp;
2121
2122 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002123 for (ifp = idev->addr_list; ifp;
2124 ifp = ifp->if_next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002125 if (ifp->scope == IFA_LINK &&
2126 !(ifp->flags & IFA_F_TENTATIVE)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002127 ipv6_addr_copy(&pkt_dev->
2128 cur_in6_saddr,
2129 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 err = 0;
2131 break;
2132 }
2133 }
2134 read_unlock_bh(&idev->lock);
2135 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002136 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002137 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002138 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002141 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 pkt_dev->saddr_min = 0;
2143 pkt_dev->saddr_max = 0;
2144 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002145
2146 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002149 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (in_dev) {
2151 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002152 pkt_dev->saddr_min =
2153 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 pkt_dev->saddr_max = pkt_dev->saddr_min;
2155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002158 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2160 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2161 }
2162
2163 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2164 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2165 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002166 /* Initialize current values. */
2167 pkt_dev->cur_dst_mac_offset = 0;
2168 pkt_dev->cur_src_mac_offset = 0;
2169 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2170 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2171 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2172 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 pkt_dev->nflows = 0;
2174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002177static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2178{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002179 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002180 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002181 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002182
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002183 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2184 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002185
Daniel Turull43d28b62010-06-09 22:49:57 +00002186 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002187 if (remaining <= 0) {
2188 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002189 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002190 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002191
Stephen Hemmingeref879792009-09-22 19:41:43 +00002192 start_time = ktime_now();
Daniel Turull43d28b62010-06-09 22:49:57 +00002193 if (remaining < 100000)
2194 ndelay(remaining); /* really small just spin */
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002195 else {
2196 /* see do_nanosleep */
2197 hrtimer_init_sleeper(&t, current);
2198 do {
2199 set_current_state(TASK_INTERRUPTIBLE);
2200 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2201 if (!hrtimer_active(&t.timer))
2202 t.task = NULL;
2203
2204 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002207 hrtimer_cancel(&t.timer);
2208 } while (t.task && pkt_dev->running && !signal_pending(current));
2209 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002211 end_time = ktime_now();
2212
2213 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002214 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002217static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2218{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002219 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002220 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2221 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2222 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2223}
2224
Stephen Hemminger648fda72009-08-27 13:55:07 +00002225static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002226{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002227 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002228}
2229
2230static inline int f_pick(struct pktgen_dev *pkt_dev)
2231{
2232 int flow = pkt_dev->curfl;
2233
2234 if (pkt_dev->flags & F_FLOW_SEQ) {
2235 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2236 /* reset time */
2237 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002238 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002239 pkt_dev->curfl += 1;
2240 if (pkt_dev->curfl >= pkt_dev->cflows)
2241 pkt_dev->curfl = 0; /*reset */
2242 }
2243 } else {
2244 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002245 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002246
Robert Olsson1211a642008-08-05 18:44:26 -07002247 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002248 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002249 pkt_dev->flows[flow].flags = 0;
2250 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002251 }
2252
2253 return pkt_dev->curfl;
2254}
2255
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002256
2257#ifdef CONFIG_XFRM
2258/* If there was already an IPSEC SA, we keep it as is, else
2259 * we go look for it ...
2260*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002261#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002262static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002263{
2264 struct xfrm_state *x = pkt_dev->flows[flow].x;
2265 if (!x) {
2266 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002267 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002268 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002269 (xfrm_address_t *)&pkt_dev->cur_saddr,
2270 AF_INET,
2271 pkt_dev->ipsmode,
2272 pkt_dev->ipsproto, 0);
2273 if (x) {
2274 pkt_dev->flows[flow].x = x;
2275 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002276 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002277 }
2278
2279 }
2280}
2281#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002282static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2283{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002284
2285 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2286 pkt_dev->cur_queue_map = smp_processor_id();
2287
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002288 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002289 __u16 t;
2290 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2291 t = random32() %
2292 (pkt_dev->queue_map_max -
2293 pkt_dev->queue_map_min + 1)
2294 + pkt_dev->queue_map_min;
2295 } else {
2296 t = pkt_dev->cur_queue_map + 1;
2297 if (t > pkt_dev->queue_map_max)
2298 t = pkt_dev->queue_map_min;
2299 }
2300 pkt_dev->cur_queue_map = t;
2301 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002302 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 -07002303}
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305/* Increment/randomize headers according to flags and current values
2306 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2307 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002308static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2309{
2310 __u32 imn;
2311 __u32 imx;
2312 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002314 if (pkt_dev->cflows)
2315 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002318 if (pkt_dev->src_mac_count > 1) {
2319 __u32 mc;
2320 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Luiz Capitulino222f1802006-03-20 22:16:13 -08002322 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002323 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002324 else {
2325 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002326 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002327 pkt_dev->src_mac_count)
2328 pkt_dev->cur_src_mac_offset = 0;
2329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Luiz Capitulino222f1802006-03-20 22:16:13 -08002331 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2332 pkt_dev->hh[11] = tmp;
2333 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2334 pkt_dev->hh[10] = tmp;
2335 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2336 pkt_dev->hh[9] = tmp;
2337 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2338 pkt_dev->hh[8] = tmp;
2339 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2340 pkt_dev->hh[7] = tmp;
2341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Luiz Capitulino222f1802006-03-20 22:16:13 -08002343 /* Deal with Destination MAC */
2344 if (pkt_dev->dst_mac_count > 1) {
2345 __u32 mc;
2346 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Luiz Capitulino222f1802006-03-20 22:16:13 -08002348 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002349 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Luiz Capitulino222f1802006-03-20 22:16:13 -08002351 else {
2352 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002353 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002354 pkt_dev->dst_mac_count) {
2355 pkt_dev->cur_dst_mac_offset = 0;
2356 }
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Luiz Capitulino222f1802006-03-20 22:16:13 -08002359 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2360 pkt_dev->hh[5] = tmp;
2361 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2362 pkt_dev->hh[4] = tmp;
2363 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2364 pkt_dev->hh[3] = tmp;
2365 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2366 pkt_dev->hh[2] = tmp;
2367 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2368 pkt_dev->hh[1] = tmp;
2369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002371 if (pkt_dev->flags & F_MPLS_RND) {
2372 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002373 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002374 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2375 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002376 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002377 htonl(0x000fffff));
2378 }
2379
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002380 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002381 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002382 }
2383
2384 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002385 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002386 }
2387
Luiz Capitulino222f1802006-03-20 22:16:13 -08002388 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2389 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002390 pkt_dev->cur_udp_src = random32() %
2391 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2392 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Luiz Capitulino222f1802006-03-20 22:16:13 -08002394 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 pkt_dev->cur_udp_src++;
2396 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2397 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002398 }
2399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Luiz Capitulino222f1802006-03-20 22:16:13 -08002401 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2402 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002403 pkt_dev->cur_udp_dst = random32() %
2404 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2405 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002406 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002408 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002410 }
2411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 if (!(pkt_dev->flags & F_IPV6)) {
2414
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002415 imn = ntohl(pkt_dev->saddr_min);
2416 imx = ntohl(pkt_dev->saddr_max);
2417 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002419 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002420 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 else {
2422 t = ntohl(pkt_dev->cur_saddr);
2423 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002424 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428 pkt_dev->cur_saddr = htonl(t);
2429 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002430
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002431 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2433 } else {
Al Viro252e33462006-11-14 20:48:11 -08002434 imn = ntohl(pkt_dev->daddr_min);
2435 imx = ntohl(pkt_dev->daddr_max);
2436 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002438 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (pkt_dev->flags & F_IPDST_RND) {
2440
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002441 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002442 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Joe Perches21cf2252007-12-16 13:44:00 -08002444 while (ipv4_is_loopback(s) ||
2445 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002446 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002447 ipv4_is_zeronet(s) ||
2448 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002449 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002450 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
Al Viro252e33462006-11-14 20:48:11 -08002452 pkt_dev->cur_daddr = s;
2453 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 t = ntohl(pkt_dev->cur_daddr);
2455 t++;
2456 if (t > imx) {
2457 t = imn;
2458 }
2459 pkt_dev->cur_daddr = htonl(t);
2460 }
2461 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002462 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002463 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002464 pkt_dev->flows[flow].cur_daddr =
2465 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002466#ifdef CONFIG_XFRM
2467 if (pkt_dev->flags & F_IPSEC_ON)
2468 get_ipsec_sa(pkt_dev, flow);
2469#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 pkt_dev->nflows++;
2471 }
2472 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002473 } else { /* IPV6 * */
2474
2475 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2476 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2477 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2478 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 else {
2480 int i;
2481
2482 /* Only random destinations yet */
2483
Luiz Capitulino222f1802006-03-20 22:16:13 -08002484 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002486 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002487 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2488 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492
Luiz Capitulino222f1802006-03-20 22:16:13 -08002493 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2494 __u32 t;
2495 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002496 t = random32() %
2497 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2498 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002499 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002501 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002503 }
2504 pkt_dev->cur_pkt_size = t;
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002507 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 pkt_dev->flows[flow].count++;
2510}
2511
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002512
2513#ifdef CONFIG_XFRM
2514static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2515{
2516 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2517 int err = 0;
2518 struct iphdr *iph;
2519
2520 if (!x)
2521 return 0;
2522 /* XXX: we dont support tunnel mode for now until
2523 * we resolve the dst issue */
2524 if (x->props.mode != XFRM_MODE_TRANSPORT)
2525 return 0;
2526
2527 spin_lock(&x->lock);
2528 iph = ip_hdr(skb);
2529
Herbert Xu13996372007-10-17 21:35:51 -07002530 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002531 if (err)
2532 goto error;
2533 err = x->type->output(x, skb);
2534 if (err)
2535 goto error;
2536
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002537 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002538 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002539error:
2540 spin_unlock(&x->lock);
2541 return err;
2542}
2543
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002544static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002545{
2546 if (pkt_dev->cflows) {
2547 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002548 int i;
2549 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002550 struct xfrm_state *x = pkt_dev->flows[i].x;
2551 if (x) {
2552 xfrm_state_put(x);
2553 pkt_dev->flows[i].x = NULL;
2554 }
2555 }
2556 }
2557}
2558
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002559static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002560 struct sk_buff *skb, __be16 protocol)
2561{
2562 if (pkt_dev->flags & F_IPSEC_ON) {
2563 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2564 int nhead = 0;
2565 if (x) {
2566 int ret;
2567 __u8 *eth;
2568 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002569 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002570 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2571 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002572 pr_err("Error expanding ipsec packet %d\n",
2573 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002574 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002575 }
2576 }
2577
2578 /* ipsec is not expecting ll header */
2579 skb_pull(skb, ETH_HLEN);
2580 ret = pktgen_output_ipsec(skb, pkt_dev);
2581 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002582 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002583 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002584 }
2585 /* restore ll */
2586 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2587 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002588 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002589 }
2590 }
2591 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002592err:
2593 kfree_skb(skb);
2594 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002595}
2596#endif
2597
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002598static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2599{
2600 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002601 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002602 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002603
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002604 mpls--;
2605 *mpls |= MPLS_STACK_BOTTOM;
2606}
2607
Al Viro0f37c602006-11-03 03:49:56 -08002608static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2609 unsigned int prio)
2610{
2611 return htons(id | (cfi << 12) | (prio << 13));
2612}
2613
Eric Dumazet26ad7872011-01-25 13:26:05 -08002614static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2615 int datalen)
2616{
2617 struct timeval timestamp;
2618 struct pktgen_hdr *pgh;
2619
2620 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2621 datalen -= sizeof(*pgh);
2622
2623 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002624 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002625 } else {
2626 int frags = pkt_dev->nfrags;
2627 int i, len;
2628
2629
2630 if (frags > MAX_SKB_FRAGS)
2631 frags = MAX_SKB_FRAGS;
2632 len = datalen - frags * PAGE_SIZE;
2633 if (len > 0) {
2634 memset(skb_put(skb, len), 0, len);
2635 datalen = frags * PAGE_SIZE;
2636 }
2637
2638 i = 0;
2639 while (datalen > 0) {
2640 if (unlikely(!pkt_dev->page)) {
2641 int node = numa_node_id();
2642
2643 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2644 node = pkt_dev->node;
2645 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2646 if (!pkt_dev->page)
2647 break;
2648 }
2649 skb_shinfo(skb)->frags[i].page = pkt_dev->page;
2650 get_page(pkt_dev->page);
2651 skb_shinfo(skb)->frags[i].page_offset = 0;
2652 skb_shinfo(skb)->frags[i].size =
2653 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2654 datalen -= skb_shinfo(skb)->frags[i].size;
2655 skb->len += skb_shinfo(skb)->frags[i].size;
2656 skb->data_len += skb_shinfo(skb)->frags[i].size;
2657 i++;
2658 skb_shinfo(skb)->nr_frags = i;
2659 }
2660
2661 while (i < frags) {
2662 int rem;
2663
2664 if (i == 0)
2665 break;
2666
2667 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2668 if (rem == 0)
2669 break;
2670
2671 skb_shinfo(skb)->frags[i - 1].size -= rem;
2672
2673 skb_shinfo(skb)->frags[i] =
2674 skb_shinfo(skb)->frags[i - 1];
2675 get_page(skb_shinfo(skb)->frags[i].page);
2676 skb_shinfo(skb)->frags[i].page =
2677 skb_shinfo(skb)->frags[i - 1].page;
2678 skb_shinfo(skb)->frags[i].page_offset +=
2679 skb_shinfo(skb)->frags[i - 1].size;
2680 skb_shinfo(skb)->frags[i].size = rem;
2681 i++;
2682 skb_shinfo(skb)->nr_frags = i;
2683 }
2684 }
2685
2686 /* Stamp the time, and sequence number,
2687 * convert them to network byte order
2688 */
2689 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2690 pgh->seq_num = htonl(pkt_dev->seq_num);
2691
2692 do_gettimeofday(&timestamp);
2693 pgh->tv_sec = htonl(timestamp.tv_sec);
2694 pgh->tv_usec = htonl(timestamp.tv_usec);
2695}
2696
Luiz Capitulino222f1802006-03-20 22:16:13 -08002697static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2698 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct sk_buff *skb = NULL;
2701 __u8 *eth;
2702 struct udphdr *udph;
2703 int datalen, iplen;
2704 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002705 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002706 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002707 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2708 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2709 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2710 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002711 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002712
2713 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002714 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002715
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002716 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002717 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002718
Robert Olsson64053be2005-06-26 15:27:10 -07002719 /* Update any of the values, used when we're incrementing various
2720 * fields.
2721 */
2722 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002723 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002724
David S. Miller7ac54592006-01-18 14:19:10 -08002725 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002726
2727 if (pkt_dev->flags & F_NODE) {
2728 int node;
2729
2730 if (pkt_dev->node >= 0)
2731 node = pkt_dev->node;
2732 else
2733 node = numa_node_id();
2734
2735 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2736 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2737 if (likely(skb)) {
2738 skb_reserve(skb, NET_SKB_PAD);
2739 skb->dev = odev;
2740 }
2741 }
2742 else
2743 skb = __netdev_alloc_skb(odev,
2744 pkt_dev->cur_pkt_size + 64
2745 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 if (!skb) {
2748 sprintf(pkt_dev->result, "No memory");
2749 return NULL;
2750 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002751 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
David S. Miller7ac54592006-01-18 14:19:10 -08002753 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 /* Reserve for ethernet and IP header */
2756 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002757 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2758 if (pkt_dev->nr_labels)
2759 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002760
2761 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002762 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002763 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002764 *svlan_tci = build_tci(pkt_dev->svlan_id,
2765 pkt_dev->svlan_cfi,
2766 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002767 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002768 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002769 }
2770 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002771 *vlan_tci = build_tci(pkt_dev->vlan_id,
2772 pkt_dev->vlan_cfi,
2773 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002774 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002775 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002776 }
2777
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002778 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002779 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002780 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002781 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002782 skb->priority = pkt_dev->skb_priority;
2783
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002784 iph = ip_hdr(skb);
2785 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002788 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002790 /* Eth + IPh + UDPh + mpls */
2791 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002792 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002793 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 udph->source = htons(pkt_dev->cur_udp_src);
2797 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002798 udph->len = htons(datalen + 8); /* DATA + udphdr */
2799 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 iph->ihl = 5;
2802 iph->version = 4;
2803 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002804 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002805 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 iph->saddr = pkt_dev->cur_saddr;
2807 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002808 iph->id = htons(pkt_dev->ip_id);
2809 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 iph->frag_off = 0;
2811 iplen = 20 + 8 + datalen;
2812 iph->tot_len = htons(iplen);
2813 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002814 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002815 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002816 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002817 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 skb->dev = odev;
2819 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002820 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002821
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002822#ifdef CONFIG_XFRM
2823 if (!process_ipsec(pkt_dev, skb, protocol))
2824 return NULL;
2825#endif
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return skb;
2828}
2829
2830/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002831 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2833 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002834 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 * Should be candidate for net/ipv4/utils.c
2836 * --ro
2837 */
2838
Luiz Capitulino222f1802006-03-20 22:16:13 -08002839static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
2841 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002842 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 unsigned long u;
2844 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002845 unsigned int prefixlen = 0;
2846 unsigned int suffixlen = 0;
Al Viro252e33462006-11-14 20:48:11 -08002847 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002848 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Luiz Capitulino222f1802006-03-20 22:16:13 -08002850 for (i = 0; i < 16; i++)
2851 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853 for (;;) {
2854 if (*s == ':') {
2855 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002856 if (s[1] == ':') { /* Found "::", skip to part 2 */
2857 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 len++;
2859 break;
2860 }
2861 s++;
2862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002864 u = simple_strtoul(s, &pos, 16);
2865 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002866 if (!i)
2867 return 0;
2868 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 /* the last 4 bytes may be written as IPv4 address */
2871
2872 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2874 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
2876 ip[prefixlen++] = (u >> 8);
2877 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002878 s += i;
2879 len += i;
2880 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return len;
2882 }
2883
2884/* part 2, after "::" */
2885 for (;;) {
2886 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002887 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 break;
2889 s++;
2890 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002891 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002893
2894 u = simple_strtol(s, &pos, 16);
2895 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002897 if (*s)
2898 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 break;
2900 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002901 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002903 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2904 sizeof(tmp));
2905 suffixlen += 4;
2906 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 break;
2908 }
2909 suffix[suffixlen++] = (u >> 8);
2910 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002911 s += i;
2912 len += i;
2913 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 break;
2915 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002916 for (i = 0; i < suffixlen; i++)
2917 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return len;
2919}
2920
Luiz Capitulino222f1802006-03-20 22:16:13 -08002921static char tohex(char hexdigit)
2922{
2923 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
Luiz Capitulino222f1802006-03-20 22:16:13 -08002926static int fmt_xlong(char *s, unsigned int i)
2927{
2928 char *bak = s;
2929 *s = tohex((i >> 12) & 0xf);
2930 if (s != bak || *s != '0')
2931 ++s;
2932 *s = tohex((i >> 8) & 0xf);
2933 if (s != bak || *s != '0')
2934 ++s;
2935 *s = tohex((i >> 4) & 0xf);
2936 if (s != bak || *s != '0')
2937 ++s;
2938 *s = tohex(i & 0xf);
2939 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Luiz Capitulino222f1802006-03-20 22:16:13 -08002942static unsigned int fmt_ip6(char *s, const char ip[16])
2943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 unsigned int len;
2945 unsigned int i;
2946 unsigned int temp;
2947 unsigned int compressing;
2948 int j;
2949
Luiz Capitulino222f1802006-03-20 22:16:13 -08002950 len = 0;
2951 compressing = 0;
2952 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002955 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2956 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2957 temp = strlen(s);
2958 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
2960#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002961 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2962 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (temp == 0) {
2964 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002965 compressing = 1;
2966 if (j == 0) {
2967 *s++ = ':';
2968 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970 }
2971 } else {
2972 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002973 compressing = 0;
2974 *s++ = ':';
2975 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002977 i = fmt_xlong(s, temp);
2978 len += i;
2979 s += i;
2980 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 *s++ = ':';
2982 ++len;
2983 }
2984 }
2985 }
2986 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002987 *s++ = ':';
2988 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002990 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return len;
2992}
2993
Luiz Capitulino222f1802006-03-20 22:16:13 -08002994static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2995 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
2997 struct sk_buff *skb = NULL;
2998 __u8 *eth;
2999 struct udphdr *udph;
3000 int datalen;
3001 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003002 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003003 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003004 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
3005 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
3006 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
3007 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003008 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003009
3010 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003011 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07003012
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003013 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003014 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003015
Robert Olsson64053be2005-06-26 15:27:10 -07003016 /* Update any of the values, used when we're incrementing various
3017 * fields.
3018 */
3019 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00003020 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07003021
Stephen Hemmingere4707572009-08-27 13:55:13 +00003022 skb = __netdev_alloc_skb(odev,
3023 pkt_dev->cur_pkt_size + 64
3024 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (!skb) {
3026 sprintf(pkt_dev->result, "No memory");
3027 return NULL;
3028 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00003029 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 skb_reserve(skb, 16);
3032
3033 /* Reserve for ethernet and IP header */
3034 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003035 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
3036 if (pkt_dev->nr_labels)
3037 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003038
3039 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07003040 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003041 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003042 *svlan_tci = build_tci(pkt_dev->svlan_id,
3043 pkt_dev->svlan_cfi,
3044 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003045 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003046 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003047 }
3048 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003049 *vlan_tci = build_tci(pkt_dev->vlan_id,
3050 pkt_dev->vlan_cfi,
3051 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003052 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003053 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003054 }
3055
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003056 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003057 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003058 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003059 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00003060 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003061 iph = ipv6_hdr(skb);
3062 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003065 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07003066
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003067 /* Eth + IPh + UDPh + mpls */
3068 datalen = pkt_dev->cur_pkt_size - 14 -
3069 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003070 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Luiz Capitulino222f1802006-03-20 22:16:13 -08003072 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 datalen = sizeof(struct pktgen_hdr);
3074 if (net_ratelimit())
Joe Perchesf9467ea2010-06-21 12:29:14 +00003075 pr_info("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
3077
3078 udph->source = htons(pkt_dev->cur_udp_src);
3079 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003080 udph->len = htons(datalen + sizeof(struct udphdr));
3081 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003083 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003085 if (pkt_dev->traffic_class) {
3086 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08003087 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003088 }
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 iph->hop_limit = 32;
3091
3092 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3093 iph->nexthdr = IPPROTO_UDP;
3094
3095 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3096 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3097
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003098 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003099 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003100 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 skb->dev = odev;
3102 skb->pkt_type = PACKET_HOST;
3103
Eric Dumazet26ad7872011-01-25 13:26:05 -08003104 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return skb;
3107}
3108
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003109static struct sk_buff *fill_packet(struct net_device *odev,
3110 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003112 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return fill_packet_ipv6(odev, pkt_dev);
3114 else
3115 return fill_packet_ipv4(odev, pkt_dev);
3116}
3117
Luiz Capitulino222f1802006-03-20 22:16:13 -08003118static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003120 pkt_dev->seq_num = 1;
3121 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123 pkt_dev->tx_bytes = 0;
3124 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127/* Set up structure for sending pkts, clear counters */
3128
3129static void pktgen_run(struct pktgen_thread *t)
3130{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003131 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 int started = 0;
3133
Joe Perchesf9467ea2010-06-21 12:29:14 +00003134 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003137 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 /*
3140 * setup odev and create initial packet.
3141 */
3142 pktgen_setup_inject(pkt_dev);
3143
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003146 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003148 pkt_dev->started_at =
3149 pkt_dev->next_tx = ktime_now();
3150
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003151 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 strcpy(pkt_dev->result, "Starting");
3154 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003155 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 strcpy(pkt_dev->result, "Error starting");
3157 }
3158 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 if (started)
3160 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
3163static void pktgen_stop_all_threads_ifs(void)
3164{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003165 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Joe Perchesf9467ea2010-06-21 12:29:14 +00003167 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003169 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003170
3171 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003172 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003173
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003174 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
Stephen Hemminger648fda72009-08-27 13:55:07 +00003177static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003179 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003181 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003182 if (pkt_dev->running)
3183 return 1;
3184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Luiz Capitulino222f1802006-03-20 22:16:13 -08003187static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003189 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Luiz Capitulino222f1802006-03-20 22:16:13 -08003191 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Luiz Capitulino222f1802006-03-20 22:16:13 -08003193 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Luiz Capitulino222f1802006-03-20 22:16:13 -08003197 if (signal_pending(current))
3198 goto signal;
3199 if_lock(t);
3200 }
3201 if_unlock(t);
3202 return 1;
3203signal:
3204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
3207static int pktgen_wait_all_threads_run(void)
3208{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003209 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003211
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003212 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003213
3214 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003216 if (sig == 0)
3217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003219
3220 if (sig == 0)
3221 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003223
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003224 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return sig;
3226}
3227
3228static void pktgen_run_all_threads(void)
3229{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003230 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Joe Perchesf9467ea2010-06-21 12:29:14 +00003232 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003234 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003236 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003238
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003239 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003241 /* Propagate thread->control */
3242 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 pktgen_wait_all_threads_run();
3245}
3246
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003247static void pktgen_reset_all_threads(void)
3248{
3249 struct pktgen_thread *t;
3250
Joe Perchesf9467ea2010-06-21 12:29:14 +00003251 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003252
3253 mutex_lock(&pktgen_thread_lock);
3254
3255 list_for_each_entry(t, &pktgen_threads, th_list)
3256 t->control |= (T_REMDEVALL);
3257
3258 mutex_unlock(&pktgen_thread_lock);
3259
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003260 /* Propagate thread->control */
3261 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003262
3263 pktgen_wait_all_threads_run();
3264}
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3267{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003268 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003269 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003270 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3271 pkt_dev->started_at);
3272 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Daniel Turull03a14ab2011-03-09 14:11:00 -08003274 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003275 (unsigned long long)ktime_to_us(elapsed),
3276 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3277 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003278 (unsigned long long)pkt_dev->sofar,
3279 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003281 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3282 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Luiz Capitulino222f1802006-03-20 22:16:13 -08003284 bps = pps * 8 * pkt_dev->cur_pkt_size;
3285
3286 mbps = bps;
3287 do_div(mbps, 1000000);
3288 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3289 (unsigned long long)pps,
3290 (unsigned long long)mbps,
3291 (unsigned long long)bps,
3292 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003296static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003298 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003299
Luiz Capitulino222f1802006-03-20 22:16:13 -08003300 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003301 pr_warning("interface: %s is already stopped\n",
3302 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003303 return -EINVAL;
3304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003306 kfree_skb(pkt_dev->skb);
3307 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003308 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003309 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003311 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Luiz Capitulino222f1802006-03-20 22:16:13 -08003313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Luiz Capitulino222f1802006-03-20 22:16:13 -08003316static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003318 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 if_lock(t);
3321
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003322 list_for_each_entry(pkt_dev, &t->if_list, list) {
3323 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003324 continue;
3325 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003326 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003327 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003328 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
3330 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003331 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Luiz Capitulino222f1802006-03-20 22:16:13 -08003334static void pktgen_stop(struct pktgen_thread *t)
3335{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003336 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Joe Perchesf9467ea2010-06-21 12:29:14 +00003338 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Luiz Capitulino222f1802006-03-20 22:16:13 -08003340 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003342 list_for_each_entry(pkt_dev, &t->if_list, list) {
3343 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Luiz Capitulino222f1802006-03-20 22:16:13 -08003346 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003349/*
3350 * one of our devices needs to be removed - find it
3351 * and remove it
3352 */
3353static void pktgen_rem_one_if(struct pktgen_thread *t)
3354{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003355 struct list_head *q, *n;
3356 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003357
Joe Perchesf9467ea2010-06-21 12:29:14 +00003358 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003359
3360 if_lock(t);
3361
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003362 list_for_each_safe(q, n, &t->if_list) {
3363 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003364
Luiz Capitulino222f1802006-03-20 22:16:13 -08003365 if (!cur->removal_mark)
3366 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003367
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003368 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003369 cur->skb = NULL;
3370
3371 pktgen_remove_device(t, cur);
3372
3373 break;
3374 }
3375
3376 if_unlock(t);
3377}
3378
Luiz Capitulino222f1802006-03-20 22:16:13 -08003379static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003381 struct list_head *q, *n;
3382 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003383
Joe Perchesf9467ea2010-06-21 12:29:14 +00003384 func_enter();
3385
Luiz Capitulino222f1802006-03-20 22:16:13 -08003386 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003387
Luiz Capitulino222f1802006-03-20 22:16:13 -08003388 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003390 list_for_each_safe(q, n, &t->if_list) {
3391 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003392
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003393 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003394 cur->skb = NULL;
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 pktgen_remove_device(t, cur);
3397 }
3398
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Luiz Capitulino222f1802006-03-20 22:16:13 -08003402static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003404 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
David S. Milleree74baa2007-01-01 20:51:53 -08003406 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
3409
Stephen Hemmingeref879792009-09-22 19:41:43 +00003410static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003411{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003412 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003413 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003414 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003415}
3416
Stephen Hemmingeref879792009-09-22 19:41:43 +00003417static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3418{
3419 ktime_t idle_start = ktime_now();
3420
3421 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3422 if (signal_pending(current))
3423 break;
3424
3425 if (need_resched())
3426 pktgen_resched(pkt_dev);
3427 else
3428 cpu_relax();
3429 }
3430 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3431}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003432
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003433static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
Stephen Hemminger00829822008-11-20 20:14:53 -08003435 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003436 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003437 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003438 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003439 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 int ret;
3441
Stephen Hemmingeref879792009-09-22 19:41:43 +00003442 /* If device is offline, then don't send */
3443 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3444 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003445 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003447
Stephen Hemmingeref879792009-09-22 19:41:43 +00003448 /* This is max DELAY, this has special meaning of
3449 * "never transmit"
3450 */
3451 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3452 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3453 return;
3454 }
3455
3456 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003457 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3458 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3459 /* build a new pkt */
3460 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003461
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003462 pkt_dev->skb = fill_packet(odev, pkt_dev);
3463 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003464 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003465 schedule();
3466 pkt_dev->clone_count--; /* back out increment, OOM */
3467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003469 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003470 pkt_dev->allocated_skbs++;
3471 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003473
Stephen Hemmingeref879792009-09-22 19:41:43 +00003474 if (pkt_dev->delay && pkt_dev->last_ok)
3475 spin(pkt_dev, pkt_dev->next_tx);
3476
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003477 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3478 txq = netdev_get_tx_queue(odev, queue_map);
3479
3480 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003481
Eric Dumazet5a0d2262010-11-23 10:42:02 +00003482 if (unlikely(netif_tx_queue_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003483 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003484 pkt_dev->last_ok = 0;
3485 goto unlock;
3486 }
3487 atomic_inc(&(pkt_dev->skb->users));
3488 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Stephen Hemmingeref879792009-09-22 19:41:43 +00003490 switch (ret) {
3491 case NETDEV_TX_OK:
3492 txq_trans_update(txq);
3493 pkt_dev->last_ok = 1;
3494 pkt_dev->sofar++;
3495 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003496 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003497 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003498 case NET_XMIT_DROP:
3499 case NET_XMIT_CN:
3500 case NET_XMIT_POLICED:
3501 /* skb has been consumed */
3502 pkt_dev->errors++;
3503 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003504 default: /* Drivers are not supposed to return other values! */
3505 if (net_ratelimit())
Joe Perches58231182010-12-20 10:30:06 -08003506 pr_info("%s xmit error: %d\n", pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003507 pkt_dev->errors++;
3508 /* fallthru */
3509 case NETDEV_TX_LOCKED:
3510 case NETDEV_TX_BUSY:
3511 /* Retry it next time */
3512 atomic_dec(&(pkt_dev->skb->users));
3513 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003514 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003515unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003516 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 /* If pkt_dev->count is zero, then run forever */
3519 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003520 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 /* Done with this */
3523 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003524 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003527/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 * Main loop of the thread goes here
3529 */
3530
David S. Milleree74baa2007-01-01 20:51:53 -08003531static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003534 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003535 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
David S. Milleree74baa2007-01-01 20:51:53 -08003538 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003541 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Joe Perchesf9467ea2010-06-21 12:29:14 +00003543 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
David S. Milleree74baa2007-01-01 20:51:53 -08003545 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003546
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003547 set_freezable();
3548
David S. Milleree74baa2007-01-01 20:51:53 -08003549 while (!kthread_should_stop()) {
3550 pkt_dev = next_to_run(t);
3551
Stephen Hemmingeref879792009-09-22 19:41:43 +00003552 if (unlikely(!pkt_dev && t->control == 0)) {
Eric Dumazet551eaff2010-11-21 10:26:44 -08003553 if (pktgen_exiting)
3554 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003555 wait_event_interruptible_timeout(t->queue,
3556 t->control != 0,
3557 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003558 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003559 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003560 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 __set_current_state(TASK_RUNNING);
3563
Stephen Hemmingeref879792009-09-22 19:41:43 +00003564 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 pktgen_xmit(pkt_dev);
3566
Stephen Hemmingeref879792009-09-22 19:41:43 +00003567 if (need_resched())
3568 pktgen_resched(pkt_dev);
3569 else
3570 cpu_relax();
3571 }
3572
Luiz Capitulino222f1802006-03-20 22:16:13 -08003573 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 pktgen_stop(t);
3575 t->control &= ~(T_STOP);
3576 }
3577
Luiz Capitulino222f1802006-03-20 22:16:13 -08003578 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 pktgen_run(t);
3580 t->control &= ~(T_RUN);
3581 }
3582
Luiz Capitulino222f1802006-03-20 22:16:13 -08003583 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003585 t->control &= ~(T_REMDEVALL);
3586 }
3587
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003589 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 t->control &= ~(T_REMDEV);
3591 }
3592
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003593 try_to_freeze();
3594
David S. Milleree74baa2007-01-01 20:51:53 -08003595 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Joe Perchesf9467ea2010-06-21 12:29:14 +00003598 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003599 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Joe Perchesf9467ea2010-06-21 12:29:14 +00003601 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003602 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Joe Perchesf9467ea2010-06-21 12:29:14 +00003604 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003605 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003606
Eric Dumazet551eaff2010-11-21 10:26:44 -08003607 /* Wait for kthread_stop */
3608 while (!kthread_should_stop()) {
3609 set_current_state(TASK_INTERRUPTIBLE);
3610 schedule();
3611 }
3612 __set_current_state(TASK_RUNNING);
3613
David S. Milleree74baa2007-01-01 20:51:53 -08003614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
Luiz Capitulino222f1802006-03-20 22:16:13 -08003617static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003618 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003620 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003621 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Eric Dumazet3e984842009-11-24 14:50:53 -08003623 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003624 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003625 if (strncmp(p->odevname, ifname, len) == 0) {
3626 if (p->odevname[len]) {
3627 if (exact || p->odevname[len] != '@')
3628 continue;
3629 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003630 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003631 break;
3632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Luiz Capitulino222f1802006-03-20 22:16:13 -08003634 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003635 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003636 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
3638
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003639/*
3640 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 */
3642
Luiz Capitulino222f1802006-03-20 22:16:13 -08003643static int add_dev_to_thread(struct pktgen_thread *t,
3644 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
3646 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Luiz Capitulino222f1802006-03-20 22:16:13 -08003648 if_lock(t);
3649
3650 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003651 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003652 rv = -EBUSY;
3653 goto out;
3654 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003655
3656 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003657 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 pkt_dev->running = 0;
3659
Luiz Capitulino222f1802006-03-20 22:16:13 -08003660out:
3661 if_unlock(t);
3662 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
3665/* Called under thread lock */
3666
Luiz Capitulino222f1802006-03-20 22:16:13 -08003667static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003669 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003670 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003671 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 /* We don't allow a device to be on several threads */
3674
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003675 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3676 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003677 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003678 return -EBUSY;
3679 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003680
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003681 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003682 if (!pkt_dev)
3683 return -ENOMEM;
3684
Eric Dumazet593f63b2009-11-23 01:44:37 +00003685 strcpy(pkt_dev->odevname, ifname);
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003686 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3687 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003688 if (pkt_dev->flows == NULL) {
3689 kfree(pkt_dev);
3690 return -ENOMEM;
3691 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003692 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003693
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003694 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003695 pkt_dev->min_pkt_size = ETH_ZLEN;
3696 pkt_dev->max_pkt_size = ETH_ZLEN;
3697 pkt_dev->nfrags = 0;
3698 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003699 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003700 pkt_dev->count = pg_count_d;
3701 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003702 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003703 pkt_dev->udp_src_max = 9;
3704 pkt_dev->udp_dst_min = 9;
3705 pkt_dev->udp_dst_max = 9;
3706
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003707 pkt_dev->vlan_p = 0;
3708 pkt_dev->vlan_cfi = 0;
3709 pkt_dev->vlan_id = 0xffff;
3710 pkt_dev->svlan_p = 0;
3711 pkt_dev->svlan_cfi = 0;
3712 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003713 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003714
Stephen Hemminger39df2322007-03-04 16:11:51 -08003715 err = pktgen_setup_dev(pkt_dev, ifname);
3716 if (err)
3717 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003718
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003719 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3720 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003721 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003722 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003723 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003724 err = -EINVAL;
3725 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003726 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003727#ifdef CONFIG_XFRM
3728 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3729 pkt_dev->ipsproto = IPPROTO_ESP;
3730#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003731
3732 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003733out2:
3734 dev_put(pkt_dev->odev);
3735out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003736#ifdef CONFIG_XFRM
3737 free_SAs(pkt_dev);
3738#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003739 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003740 kfree(pkt_dev);
3741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
David S. Milleree74baa2007-01-01 20:51:53 -08003744static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003746 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003747 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003748 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003750 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3751 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003752 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003753 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003754 return -ENOMEM;
3755 }
3756
Luiz Capitulino222f1802006-03-20 22:16:13 -08003757 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003759
David S. Milleree74baa2007-01-01 20:51:53 -08003760 INIT_LIST_HEAD(&t->if_list);
3761
3762 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003763 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003764
Eric Dumazet94dcf292011-03-22 16:30:45 -07003765 p = kthread_create_on_node(pktgen_thread_worker,
3766 t,
3767 cpu_to_node(cpu),
3768 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003769 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003770 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003771 list_del(&t->th_list);
3772 kfree(t);
3773 return PTR_ERR(p);
3774 }
3775 kthread_bind(p, cpu);
3776 t->tsk = p;
3777
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003778 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3779 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003780 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003781 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003782 PG_PROC_DIR, t->tsk->comm);
3783 kthread_stop(p);
3784 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003785 kfree(t);
3786 return -EINVAL;
3787 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003788
David S. Milleree74baa2007-01-01 20:51:53 -08003789 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003790 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 return 0;
3793}
3794
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003795/*
3796 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003798static void _rem_dev_from_if_list(struct pktgen_thread *t,
3799 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003801 struct list_head *q, *n;
3802 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003804 list_for_each_safe(q, n, &t->if_list) {
3805 p = list_entry(q, struct pktgen_dev, list);
3806 if (p == pkt_dev)
3807 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
3809}
3810
Luiz Capitulino222f1802006-03-20 22:16:13 -08003811static int pktgen_remove_device(struct pktgen_thread *t,
3812 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
3814
Joe Perchesf9467ea2010-06-21 12:29:14 +00003815 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Luiz Capitulino222f1802006-03-20 22:16:13 -08003817 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003818 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003819 pktgen_stop_device(pkt_dev);
3820 }
3821
3822 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 if (pkt_dev->odev) {
3825 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003826 pkt_dev->odev = NULL;
3827 }
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 /* And update the thread if_list */
3830
3831 _rem_dev_from_if_list(t, pkt_dev);
3832
Stephen Hemminger39df2322007-03-04 16:11:51 -08003833 if (pkt_dev->entry)
3834 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003836#ifdef CONFIG_XFRM
3837 free_SAs(pkt_dev);
3838#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003839 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003840 if (pkt_dev->page)
3841 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844}
3845
Luiz Capitulino222f1802006-03-20 22:16:13 -08003846static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
3848 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003849 struct proc_dir_entry *pe;
3850
Joe Perchesf9467ea2010-06-21 12:29:14 +00003851 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003853 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003854 if (!pg_proc_dir)
3855 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Wang Chen25296d52008-02-28 14:11:49 -08003857 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003858 if (pe == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003859 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003860 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003861 return -EINVAL;
3862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 /* Register us to receive netdevice events */
3865 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003866
John Hawkes670c02c2005-10-13 09:30:31 -07003867 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003868 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
David S. Milleree74baa2007-01-01 20:51:53 -08003870 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003871 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00003872 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3873 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003874 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003875
3876 if (list_empty(&pktgen_threads)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003877 pr_err("ERROR: Initialization failed for all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003878 unregister_netdevice_notifier(&pktgen_notifier_block);
3879 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003880 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003881 return -ENODEV;
3882 }
3883
Luiz Capitulino222f1802006-03-20 22:16:13 -08003884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885}
3886
3887static void __exit pg_cleanup(void)
3888{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003889 struct pktgen_thread *t;
3890 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Luiz Capitulino222f1802006-03-20 22:16:13 -08003892 /* Stop all interfaces & threads */
Eric Dumazet551eaff2010-11-21 10:26:44 -08003893 pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003895 list_for_each_safe(q, n, &pktgen_threads) {
3896 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003897 kthread_stop(t->tsk);
3898 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Luiz Capitulino222f1802006-03-20 22:16:13 -08003901 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 unregister_netdevice_notifier(&pktgen_notifier_block);
3903
Luiz Capitulino222f1802006-03-20 22:16:13 -08003904 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003905 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003906 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907}
3908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909module_init(pg_init);
3910module_exit(pg_cleanup);
3911
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003912MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913MODULE_DESCRIPTION("Packet Generator tool");
3914MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003915MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003917MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003919MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003921MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003923MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");