blob: 379270f14771efe13c4ca12a36b6f23bce686f5f [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>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200159#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#include <net/checksum.h>
161#include <net/ipv6.h>
162#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700163#ifdef CONFIG_XFRM
164#include <net/xfrm.h>
165#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#include <asm/byteorder.h>
167#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700168#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000169#include <linux/io.h>
170#include <linux/timex.h>
171#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800173#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Daniel Turull43d28b62010-06-09 22:49:57 +0000175#define VERSION "2.74"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800177#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800178#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Joe Perchesf9467ea2010-06-21 12:29:14 +0000180#define func_enter() pr_debug("entering %s\n", __func__);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800183#define F_IPSRC_RND (1<<0) /* IP-Src Random */
184#define F_IPDST_RND (1<<1) /* IP-Dst Random */
185#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
186#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
187#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
188#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
189#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
190#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800191#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700192#define F_VID_RND (1<<9) /* Random VLAN ID */
193#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700194#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700195#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700196#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700197#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000198#define F_NODE (1<<15) /* Node memory alloc*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000201#define T_STOP (1<<0) /* Stop run */
202#define T_RUN (1<<1) /* Start run */
203#define T_REMDEVALL (1<<2) /* Remove all devs */
204#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* If lock -- can be removed after some work */
207#define if_lock(t) spin_lock(&(t->if_lock));
208#define if_unlock(t) spin_unlock(&(t->if_lock));
209
210/* Used to help with determining the pkts on receive */
211#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700212#define PG_PROC_DIR "pktgen"
213#define PGCTRL "pgctrl"
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000214static struct proc_dir_entry *pg_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216#define MAX_CFLOWS 65536
217
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700218#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
219#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
220
Luiz Capitulino222f1802006-03-20 22:16:13 -0800221struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800222 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800223 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700224#ifdef CONFIG_XFRM
225 struct xfrm_state *x;
226#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700227 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
229
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700230/* flow flag bits */
231#define F_INIT (1<<0) /* flow has been initialized */
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /*
235 * Try to keep frequent/infrequent used vars. separated.
236 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800237 struct proc_dir_entry *entry; /* proc file */
238 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000239 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000241 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800242
243 /* If min != max, then we will either do a linear iteration, or
244 * we will do a random selection from within the range.
245 */
246 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800247 int removal_mark; /* non-zero => the device is marked for
248 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Luiz Capitulino222f1802006-03-20 22:16:13 -0800250 int min_pkt_size; /* = ETH_ZLEN; */
251 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700252 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800253 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800254 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000255 u64 delay; /* nano-seconds */
256
Luiz Capitulino222f1802006-03-20 22:16:13 -0800257 __u64 count; /* Default No packets to send */
258 __u64 sofar; /* How many pkts we've sent so far */
259 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800260 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Luiz Capitulino222f1802006-03-20 22:16:13 -0800262 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Luiz Capitulino222f1802006-03-20 22:16:13 -0800264 __u64 allocated_skbs;
265 __u32 clone_count;
266 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000267 * Or a failed transmit of some sort?
268 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800269 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000270 ktime_t next_tx;
271 ktime_t started_at;
272 ktime_t stopped_at;
273 u64 idle_acc; /* nano-seconds */
274
Luiz Capitulino222f1802006-03-20 22:16:13 -0800275 __u32 seq_num;
276
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000277 int clone_skb; /*
278 * Use multiple SKBs during packet gen.
279 * If this number is greater than 1, then
280 * that many copies of the same packet will be
281 * sent before a new packet is allocated.
282 * If you want to send 1024 identical packets
283 * before creating a new packet,
284 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800285 */
286
287 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
288 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
289 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
290 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
291
292 struct in6_addr in6_saddr;
293 struct in6_addr in6_daddr;
294 struct in6_addr cur_in6_daddr;
295 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800297 struct in6_addr min_in6_daddr;
298 struct in6_addr max_in6_daddr;
299 struct in6_addr min_in6_saddr;
300 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Luiz Capitulino222f1802006-03-20 22:16:13 -0800302 /* If we're doing ranges, random or incremental, then this
303 * defines the min/max for those ranges.
304 */
Al Viro252e33462006-11-14 20:48:11 -0800305 __be32 saddr_min; /* inclusive, source IP address */
306 __be32 saddr_max; /* exclusive, source IP address */
307 __be32 daddr_min; /* inclusive, dest IP address */
308 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Luiz Capitulino222f1802006-03-20 22:16:13 -0800310 __u16 udp_src_min; /* inclusive, source UDP port */
311 __u16 udp_src_max; /* exclusive, source UDP port */
312 __u16 udp_dst_min; /* inclusive, dest UDP port */
313 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700315 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000316 __u8 tos; /* six MSB of (former) IPv4 TOS
317 are for dscp codepoint */
318 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
319 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700320
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800321 /* MPLS */
322 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
323 __be32 labels[MAX_MPLS_LABELS];
324
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700325 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
326 __u8 vlan_p;
327 __u8 vlan_cfi;
328 __u16 vlan_id; /* 0xffff means no vlan tag */
329
330 __u8 svlan_p;
331 __u8 svlan_cfi;
332 __u16 svlan_id; /* 0xffff means no svlan tag */
333
Luiz Capitulino222f1802006-03-20 22:16:13 -0800334 __u32 src_mac_count; /* How many MACs to iterate through */
335 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Luiz Capitulino222f1802006-03-20 22:16:13 -0800337 unsigned char dst_mac[ETH_ALEN];
338 unsigned char src_mac[ETH_ALEN];
339
340 __u32 cur_dst_mac_offset;
341 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800342 __be32 cur_saddr;
343 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700344 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800345 __u16 cur_udp_dst;
346 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700347 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800348 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800349 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800350
351 __u8 hh[14];
352 /* = {
353 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
354
355 We fill in SRC address later
356 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
357 0x08, 0x00
358 };
359 */
360 __u16 pad; /* pad out the hh struct to an even 16 bytes */
361
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000362 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800363 * are transmitting the same one multiple times
364 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000365 struct net_device *odev; /* The out-going device.
366 * Note that the device should have it's
367 * pg_info pointer pointing back to this
368 * device.
369 * Set when the user specifies the out-going
370 * device name (not when the inject is
371 * started as it used to do.)
372 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000373 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800375 unsigned cflows; /* Concurrent flows (config) */
376 unsigned lflow; /* Flow length (config) */
377 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700378 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700379
380 u16 queue_map_min;
381 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000382 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000383 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700384
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700385#ifdef CONFIG_XFRM
386 __u8 ipsmode; /* IPSEC mode (config) */
387 __u8 ipsproto; /* IPSEC type (config) */
388#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800389 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390};
391
392struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800393 __be32 pgh_magic;
394 __be32 seq_num;
395 __be32 tv_sec;
396 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};
398
Eric Dumazet551eaff2010-11-21 10:26:44 -0800399static bool pktgen_exiting __read_mostly;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000402 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a80162006-03-20 22:18:16 -0800403 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800404 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800405 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800406 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000408 /* Field for thread to receive "posted" events terminate,
409 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800410
411 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int cpu;
413
Luiz Capitulino222f1802006-03-20 22:16:13 -0800414 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700415 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416};
417
418#define REMOVE 1
419#define FIND 0
420
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000421static inline ktime_t ktime_now(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000423 struct timespec ts;
424 ktime_get_ts(&ts);
425
426 return timespec_to_ktime(ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000429/* This works even if 32 bit because of careful byte order choice */
430static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000432 return cmp1.tv64 < cmp2.tv64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000435static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000436 "Packet Generator for packet performance testing. "
437 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Luiz Capitulino222f1802006-03-20 22:16:13 -0800439static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
440static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
441static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800442 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
444static void pktgen_run_all_threads(void);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800445static void pktgen_reset_all_threads(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446static void pktgen_stop_all_threads_ifs(void);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000447
Luiz Capitulino222f1802006-03-20 22:16:13 -0800448static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800450
Luiz Capitulino222f1802006-03-20 22:16:13 -0800451static unsigned int scan_ip6(const char *s, char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000454static int pg_count_d __read_mostly = 1000;
455static int pg_delay_d __read_mostly;
456static int pg_clone_skb_d __read_mostly;
457static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Luiz Capitulino222fa072006-03-20 22:24:27 -0800459static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800460static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462static struct notifier_block pktgen_notifier_block = {
463 .notifier_call = pktgen_device_event,
464};
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900467 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *
469 */
470
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700471static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800472{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000473 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000477static ssize_t pgctrl_write(struct file *file, const char __user *buf,
478 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700481 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Luiz Capitulino222f1802006-03-20 22:16:13 -0800483 if (!capable(CAP_NET_ADMIN)) {
484 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700488 if (count > sizeof(data))
489 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700492 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700493 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800494 }
495 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Luiz Capitulino222f1802006-03-20 22:16:13 -0800497 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 pktgen_stop_all_threads_ifs();
499
Luiz Capitulino222f1802006-03-20 22:16:13 -0800500 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 pktgen_run_all_threads();
502
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800503 else if (!strcmp(data, "reset"))
504 pktgen_reset_all_threads();
505
Luiz Capitulino222f1802006-03-20 22:16:13 -0800506 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000507 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 err = count;
510
Luiz Capitulino222f1802006-03-20 22:16:13 -0800511out:
512 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700515static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700517 return single_open(file, pgctrl_show, PDE(inode)->data);
518}
519
Arjan van de Ven9a321442007-02-12 00:55:35 -0800520static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800521 .owner = THIS_MODULE,
522 .open = pgctrl_open,
523 .read = seq_read,
524 .llseek = seq_lseek,
525 .write = pgctrl_write,
526 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700527};
528
529static int pktgen_if_show(struct seq_file *seq, void *v)
530{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000531 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000532 ktime_t stopped;
533 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Luiz Capitulino222f1802006-03-20 22:16:13 -0800535 seq_printf(seq,
536 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
537 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
538 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Luiz Capitulino222f1802006-03-20 22:16:13 -0800540 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000541 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
542 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000543 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Luiz Capitulino222f1802006-03-20 22:16:13 -0800545 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
546 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Robert Olsson45b270f2007-08-28 15:45:55 -0700548 seq_printf(seq,
549 " queue_map_min: %u queue_map_max: %u\n",
550 pkt_dev->queue_map_min,
551 pkt_dev->queue_map_max);
552
John Fastabend9e50e3a2010-11-16 19:12:28 +0000553 if (pkt_dev->skb_priority)
554 seq_printf(seq, " skb_priority: %u\n",
555 pkt_dev->skb_priority);
556
Luiz Capitulino222f1802006-03-20 22:16:13 -0800557 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800558 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000559 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
560 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
561 &pkt_dev->in6_saddr,
562 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
563 &pkt_dev->in6_daddr,
564 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000565 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800566 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000567 " dst_min: %s dst_max: %s\n",
568 pkt_dev->dst_min, pkt_dev->dst_max);
569 seq_printf(seq,
570 " src_min: %s src_max: %s\n",
571 pkt_dev->src_min, pkt_dev->src_max);
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700574 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Johannes Berge1749612008-10-27 15:59:26 -0700576 seq_printf(seq, "%pM ",
577 is_zero_ether_addr(pkt_dev->src_mac) ?
578 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Luiz Capitulino222f1802006-03-20 22:16:13 -0800580 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700581 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Luiz Capitulino222f1802006-03-20 22:16:13 -0800583 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000584 " udp_src_min: %d udp_src_max: %d"
585 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800586 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
587 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Luiz Capitulino222f1802006-03-20 22:16:13 -0800589 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800590 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700591 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800593 if (pkt_dev->nr_labels) {
594 unsigned i;
595 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700596 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800597 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
598 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
599 }
600
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000601 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700602 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000603 pkt_dev->vlan_id, pkt_dev->vlan_p,
604 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700605
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000606 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700607 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000608 pkt_dev->svlan_id, pkt_dev->svlan_p,
609 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700610
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000611 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700612 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700613
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000614 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700615 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700616
Robert Olssone99b99b2010-03-18 22:44:30 +0000617 if (pkt_dev->node >= 0)
618 seq_printf(seq, " node: %d\n", pkt_dev->node);
619
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800620 seq_printf(seq, " Flags: ");
621
Luiz Capitulino222f1802006-03-20 22:16:13 -0800622 if (pkt_dev->flags & F_IPV6)
623 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Luiz Capitulino222f1802006-03-20 22:16:13 -0800625 if (pkt_dev->flags & F_IPSRC_RND)
626 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Luiz Capitulino222f1802006-03-20 22:16:13 -0800628 if (pkt_dev->flags & F_IPDST_RND)
629 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Luiz Capitulino222f1802006-03-20 22:16:13 -0800631 if (pkt_dev->flags & F_TXSIZE_RND)
632 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Luiz Capitulino222f1802006-03-20 22:16:13 -0800634 if (pkt_dev->flags & F_UDPSRC_RND)
635 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Luiz Capitulino222f1802006-03-20 22:16:13 -0800637 if (pkt_dev->flags & F_UDPDST_RND)
638 seq_printf(seq, "UDPDST_RND ");
639
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800640 if (pkt_dev->flags & F_MPLS_RND)
641 seq_printf(seq, "MPLS_RND ");
642
Robert Olsson45b270f2007-08-28 15:45:55 -0700643 if (pkt_dev->flags & F_QUEUE_MAP_RND)
644 seq_printf(seq, "QUEUE_MAP_RND ");
645
Robert Olssone6fce5b2008-08-07 02:23:01 -0700646 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
647 seq_printf(seq, "QUEUE_MAP_CPU ");
648
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700649 if (pkt_dev->cflows) {
650 if (pkt_dev->flags & F_FLOW_SEQ)
651 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
652 else
653 seq_printf(seq, "FLOW_RND ");
654 }
655
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700656#ifdef CONFIG_XFRM
657 if (pkt_dev->flags & F_IPSEC_ON)
658 seq_printf(seq, "IPSEC ");
659#endif
660
Luiz Capitulino222f1802006-03-20 22:16:13 -0800661 if (pkt_dev->flags & F_MACSRC_RND)
662 seq_printf(seq, "MACSRC_RND ");
663
664 if (pkt_dev->flags & F_MACDST_RND)
665 seq_printf(seq, "MACDST_RND ");
666
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700667 if (pkt_dev->flags & F_VID_RND)
668 seq_printf(seq, "VID_RND ");
669
670 if (pkt_dev->flags & F_SVID_RND)
671 seq_printf(seq, "SVID_RND ");
672
Robert Olssone99b99b2010-03-18 22:44:30 +0000673 if (pkt_dev->flags & F_NODE)
674 seq_printf(seq, "NODE_ALLOC ");
675
Luiz Capitulino222f1802006-03-20 22:16:13 -0800676 seq_puts(seq, "\n");
677
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000678 /* not really stopped, more like last-running-at */
679 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
680 idle = pkt_dev->idle_acc;
681 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800682
683 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000684 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800685 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000686 (unsigned long long)pkt_dev->errors);
687
688 seq_printf(seq,
689 " started: %lluus stopped: %lluus idle: %lluus\n",
690 (unsigned long long) ktime_to_us(pkt_dev->started_at),
691 (unsigned long long) ktime_to_us(stopped),
692 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800693
694 seq_printf(seq,
695 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700696 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
697 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Luiz Capitulino222f1802006-03-20 22:16:13 -0800699 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000700 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
701 &pkt_dev->cur_in6_saddr,
702 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800703 } else
704 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700705 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Luiz Capitulino222f1802006-03-20 22:16:13 -0800707 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700708 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Robert Olsson45b270f2007-08-28 15:45:55 -0700710 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
711
Luiz Capitulino222f1802006-03-20 22:16:13 -0800712 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800715 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800717 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800722
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000723static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
724 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800725{
726 int i = 0;
727 *num = 0;
728
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700729 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000730 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800731 char c;
732 *num <<= 4;
733 if (get_user(c, &user_buffer[i]))
734 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000735 value = hex_to_bin(c);
736 if (value >= 0)
737 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800738 else
739 break;
740 }
741 return i;
742}
743
Luiz Capitulino222f1802006-03-20 22:16:13 -0800744static int count_trail_chars(const char __user * user_buffer,
745 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 int i;
748
749 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800750 char c;
751 if (get_user(c, &user_buffer[i]))
752 return -EFAULT;
753 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 case '\"':
755 case '\n':
756 case '\r':
757 case '\t':
758 case ' ':
759 case '=':
760 break;
761 default:
762 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765done:
766 return i;
767}
768
Luiz Capitulino222f1802006-03-20 22:16:13 -0800769static unsigned long num_arg(const char __user * user_buffer,
770 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000772 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800774
Paul Gortmakerd6182222010-10-18 12:14:44 +0000775 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800776 char c;
777 if (get_user(c, &user_buffer[i]))
778 return -EFAULT;
779 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800781 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 } else
783 break;
784 }
785 return i;
786}
787
Luiz Capitulino222f1802006-03-20 22:16:13 -0800788static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000790 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Paul Gortmakerd6182222010-10-18 12:14:44 +0000792 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800793 char c;
794 if (get_user(c, &user_buffer[i]))
795 return -EFAULT;
796 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 case '\"':
798 case '\n':
799 case '\r':
800 case '\t':
801 case ' ':
802 goto done_str;
803 break;
804 default:
805 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return i;
810}
811
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800812static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
813{
814 unsigned n = 0;
815 char c;
816 ssize_t i = 0;
817 int len;
818
819 pkt_dev->nr_labels = 0;
820 do {
821 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700822 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800823 if (len <= 0)
824 return len;
825 pkt_dev->labels[n] = htonl(tmp);
826 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
827 pkt_dev->flags |= F_MPLS_RND;
828 i += len;
829 if (get_user(c, &buffer[i]))
830 return -EFAULT;
831 i++;
832 n++;
833 if (n >= MAX_MPLS_LABELS)
834 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700835 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800836
837 pkt_dev->nr_labels = n;
838 return i;
839}
840
Luiz Capitulino222f1802006-03-20 22:16:13 -0800841static ssize_t pktgen_if_write(struct file *file,
842 const char __user * user_buffer, size_t count,
843 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Joe Perches8a994a72010-07-12 10:50:23 +0000845 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800846 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000847 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 char name[16], valstr[32];
849 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800850 char *pg_result = NULL;
851 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853
854 pg_result = &(pkt_dev->result[0]);
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000857 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EINVAL;
859 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800860
Paul Gortmakerd6182222010-10-18 12:14:44 +0000861 max = count;
862 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000864 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800865 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000867 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /* Read variable name */
870
871 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000872 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800873 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800876 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return -EFAULT;
878 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800879
880 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800882 if (len < 0)
883 return len;
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 i += len;
886
887 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000888 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700889 char tb[copy + 1];
890 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700892 tb[copy] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700893 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800894 (unsigned long)count, tb);
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if (!strcmp(name, "min_pkt_size")) {
898 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000899 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800900 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800903 if (value < 14 + 20 + 8)
904 value = 14 + 20 + 8;
905 if (value != pkt_dev->min_pkt_size) {
906 pkt_dev->min_pkt_size = value;
907 pkt_dev->cur_pkt_size = value;
908 }
909 sprintf(pg_result, "OK: min_pkt_size=%u",
910 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return count;
912 }
913
Luiz Capitulino222f1802006-03-20 22:16:13 -0800914 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000916 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800917 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800920 if (value < 14 + 20 + 8)
921 value = 14 + 20 + 8;
922 if (value != pkt_dev->max_pkt_size) {
923 pkt_dev->max_pkt_size = value;
924 pkt_dev->cur_pkt_size = value;
925 }
926 sprintf(pg_result, "OK: max_pkt_size=%u",
927 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return count;
929 }
930
Luiz Capitulino222f1802006-03-20 22:16:13 -0800931 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (!strcmp(name, "pkt_size")) {
934 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000935 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800936 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800939 if (value < 14 + 20 + 8)
940 value = 14 + 20 + 8;
941 if (value != pkt_dev->min_pkt_size) {
942 pkt_dev->min_pkt_size = value;
943 pkt_dev->max_pkt_size = value;
944 pkt_dev->cur_pkt_size = value;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
947 return count;
948 }
949
Luiz Capitulino222f1802006-03-20 22:16:13 -0800950 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000952 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800953 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 sprintf(pg_result, "OK: debug=%u", debug);
958 return count;
959 }
960
Luiz Capitulino222f1802006-03-20 22:16:13 -0800961 if (!strcmp(name, "frags")) {
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;
967 pkt_dev->nfrags = value;
968 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
969 return count;
970 }
971 if (!strcmp(name, "delay")) {
972 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000973 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800974 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000977 if (value == 0x7FFFFFFF)
978 pkt_dev->delay = ULLONG_MAX;
979 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000980 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000981
982 sprintf(pg_result, "OK: delay=%llu",
983 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return count;
985 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000986 if (!strcmp(name, "rate")) {
987 len = num_arg(&user_buffer[i], 10, &value);
988 if (len < 0)
989 return len;
990
991 i += len;
992 if (!value)
993 return len;
994 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
995 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000996 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000997
998 sprintf(pg_result, "OK: rate=%lu", value);
999 return count;
1000 }
1001 if (!strcmp(name, "ratep")) {
1002 len = num_arg(&user_buffer[i], 10, &value);
1003 if (len < 0)
1004 return len;
1005
1006 i += len;
1007 if (!value)
1008 return len;
1009 pkt_dev->delay = NSEC_PER_SEC/value;
1010 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001011 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001012
1013 sprintf(pg_result, "OK: rate=%lu", value);
1014 return count;
1015 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001016 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001018 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001019 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001022 if (value != pkt_dev->udp_src_min) {
1023 pkt_dev->udp_src_min = value;
1024 pkt_dev->cur_udp_src = value;
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1027 return count;
1028 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001031 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001032 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001035 if (value != pkt_dev->udp_dst_min) {
1036 pkt_dev->udp_dst_min = value;
1037 pkt_dev->cur_udp_dst = value;
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1040 return count;
1041 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001042 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001044 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001048 if (value != pkt_dev->udp_src_max) {
1049 pkt_dev->udp_src_max = value;
1050 pkt_dev->cur_udp_src = value;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1053 return count;
1054 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001055 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001057 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001058 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001061 if (value != pkt_dev->udp_dst_max) {
1062 pkt_dev->udp_dst_max = value;
1063 pkt_dev->cur_udp_dst = value;
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1066 return count;
1067 }
1068 if (!strcmp(name, "clone_skb")) {
1069 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001070 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001071 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001074 pkt_dev->clone_skb = value;
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1077 return count;
1078 }
1079 if (!strcmp(name, "count")) {
1080 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001081 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001082 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 i += len;
1085 pkt_dev->count = value;
1086 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001087 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return count;
1089 }
1090 if (!strcmp(name, "src_mac_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 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001097 pkt_dev->src_mac_count = value;
1098 pkt_dev->cur_src_mac_offset = 0;
1099 }
1100 sprintf(pg_result, "OK: src_mac_count=%d",
1101 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return count;
1103 }
1104 if (!strcmp(name, "dst_mac_count")) {
1105 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001106 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001107 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 i += len;
1110 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001111 pkt_dev->dst_mac_count = value;
1112 pkt_dev->cur_dst_mac_offset = 0;
1113 }
1114 sprintf(pg_result, "OK: dst_mac_count=%d",
1115 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return count;
1117 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001118 if (!strcmp(name, "node")) {
1119 len = num_arg(&user_buffer[i], 10, &value);
1120 if (len < 0)
1121 return len;
1122
1123 i += len;
1124
1125 if (node_possible(value)) {
1126 pkt_dev->node = value;
1127 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001128 if (pkt_dev->page) {
1129 put_page(pkt_dev->page);
1130 pkt_dev->page = NULL;
1131 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001132 }
1133 else
1134 sprintf(pg_result, "ERROR: node not possible");
1135 return count;
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001138 char f[32];
1139 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001141 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001142 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (copy_from_user(f, &user_buffer[i], len))
1145 return -EFAULT;
1146 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001147 if (strcmp(f, "IPSRC_RND") == 0)
1148 pkt_dev->flags |= F_IPSRC_RND;
1149
1150 else if (strcmp(f, "!IPSRC_RND") == 0)
1151 pkt_dev->flags &= ~F_IPSRC_RND;
1152
1153 else if (strcmp(f, "TXSIZE_RND") == 0)
1154 pkt_dev->flags |= F_TXSIZE_RND;
1155
1156 else if (strcmp(f, "!TXSIZE_RND") == 0)
1157 pkt_dev->flags &= ~F_TXSIZE_RND;
1158
1159 else if (strcmp(f, "IPDST_RND") == 0)
1160 pkt_dev->flags |= F_IPDST_RND;
1161
1162 else if (strcmp(f, "!IPDST_RND") == 0)
1163 pkt_dev->flags &= ~F_IPDST_RND;
1164
1165 else if (strcmp(f, "UDPSRC_RND") == 0)
1166 pkt_dev->flags |= F_UDPSRC_RND;
1167
1168 else if (strcmp(f, "!UDPSRC_RND") == 0)
1169 pkt_dev->flags &= ~F_UDPSRC_RND;
1170
1171 else if (strcmp(f, "UDPDST_RND") == 0)
1172 pkt_dev->flags |= F_UDPDST_RND;
1173
1174 else if (strcmp(f, "!UDPDST_RND") == 0)
1175 pkt_dev->flags &= ~F_UDPDST_RND;
1176
1177 else if (strcmp(f, "MACSRC_RND") == 0)
1178 pkt_dev->flags |= F_MACSRC_RND;
1179
1180 else if (strcmp(f, "!MACSRC_RND") == 0)
1181 pkt_dev->flags &= ~F_MACSRC_RND;
1182
1183 else if (strcmp(f, "MACDST_RND") == 0)
1184 pkt_dev->flags |= F_MACDST_RND;
1185
1186 else if (strcmp(f, "!MACDST_RND") == 0)
1187 pkt_dev->flags &= ~F_MACDST_RND;
1188
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001189 else if (strcmp(f, "MPLS_RND") == 0)
1190 pkt_dev->flags |= F_MPLS_RND;
1191
1192 else if (strcmp(f, "!MPLS_RND") == 0)
1193 pkt_dev->flags &= ~F_MPLS_RND;
1194
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001195 else if (strcmp(f, "VID_RND") == 0)
1196 pkt_dev->flags |= F_VID_RND;
1197
1198 else if (strcmp(f, "!VID_RND") == 0)
1199 pkt_dev->flags &= ~F_VID_RND;
1200
1201 else if (strcmp(f, "SVID_RND") == 0)
1202 pkt_dev->flags |= F_SVID_RND;
1203
1204 else if (strcmp(f, "!SVID_RND") == 0)
1205 pkt_dev->flags &= ~F_SVID_RND;
1206
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001207 else if (strcmp(f, "FLOW_SEQ") == 0)
1208 pkt_dev->flags |= F_FLOW_SEQ;
1209
Robert Olsson45b270f2007-08-28 15:45:55 -07001210 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1211 pkt_dev->flags |= F_QUEUE_MAP_RND;
1212
1213 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1214 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001215
1216 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1217 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1218
1219 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1220 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001221#ifdef CONFIG_XFRM
1222 else if (strcmp(f, "IPSEC") == 0)
1223 pkt_dev->flags |= F_IPSEC_ON;
1224#endif
1225
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001226 else if (strcmp(f, "!IPV6") == 0)
1227 pkt_dev->flags &= ~F_IPV6;
1228
Robert Olssone99b99b2010-03-18 22:44:30 +00001229 else if (strcmp(f, "NODE_ALLOC") == 0)
1230 pkt_dev->flags |= F_NODE;
1231
1232 else if (strcmp(f, "!NODE_ALLOC") == 0)
1233 pkt_dev->flags &= ~F_NODE;
1234
Luiz Capitulino222f1802006-03-20 22:16:13 -08001235 else {
1236 sprintf(pg_result,
1237 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1238 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001239 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001240 "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 -08001241 return count;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1244 return count;
1245 }
1246 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1247 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001248 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001249 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Luiz Capitulino222f1802006-03-20 22:16:13 -08001251 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001253 buf[len] = 0;
1254 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1255 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1256 strncpy(pkt_dev->dst_min, buf, len);
1257 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1258 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1259 }
1260 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001261 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 pkt_dev->dst_min);
1263 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1265 return count;
1266 }
1267 if (!strcmp(name, "dst_max")) {
1268 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001269 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001270 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Luiz Capitulino222f1802006-03-20 22:16:13 -08001273 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return -EFAULT;
1275
Luiz Capitulino222f1802006-03-20 22:16:13 -08001276 buf[len] = 0;
1277 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1278 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1279 strncpy(pkt_dev->dst_max, buf, len);
1280 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1281 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1282 }
1283 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001284 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001285 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 i += len;
1287 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1288 return count;
1289 }
1290 if (!strcmp(name, "dst6")) {
1291 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001292 if (len < 0)
1293 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 pkt_dev->flags |= F_IPV6;
1296
Luiz Capitulino222f1802006-03-20 22:16:13 -08001297 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001299 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001302 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1305
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001307 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Luiz Capitulino222f1802006-03-20 22:16:13 -08001309 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 sprintf(pg_result, "OK: dst6=%s", buf);
1311 return count;
1312 }
1313 if (!strcmp(name, "dst6_min")) {
1314 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001315 if (len < 0)
1316 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 pkt_dev->flags |= F_IPV6;
1319
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001325 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Luiz Capitulino222f1802006-03-20 22:16:13 -08001327 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1328 &pkt_dev->min_in6_daddr);
1329 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001330 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Luiz Capitulino222f1802006-03-20 22:16:13 -08001332 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 sprintf(pg_result, "OK: dst6_min=%s", buf);
1334 return count;
1335 }
1336 if (!strcmp(name, "dst6_max")) {
1337 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 if (len < 0)
1339 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 pkt_dev->flags |= F_IPV6;
1342
Luiz Capitulino222f1802006-03-20 22:16:13 -08001343 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001345 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001348 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Luiz Capitulino222f1802006-03-20 22:16:13 -08001350 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001351 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Luiz Capitulino222f1802006-03-20 22:16:13 -08001353 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 sprintf(pg_result, "OK: dst6_max=%s", buf);
1355 return count;
1356 }
1357 if (!strcmp(name, "src6")) {
1358 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359 if (len < 0)
1360 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 pkt_dev->flags |= F_IPV6;
1363
Luiz Capitulino222f1802006-03-20 22:16:13 -08001364 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001366 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001369 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1372
Luiz Capitulino222f1802006-03-20 22:16:13 -08001373 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001374 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001375
1376 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 sprintf(pg_result, "OK: src6=%s", buf);
1378 return count;
1379 }
1380 if (!strcmp(name, "src_min")) {
1381 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001382 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001383 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001384
Luiz Capitulino222f1802006-03-20 22:16:13 -08001385 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001387 buf[len] = 0;
1388 if (strcmp(buf, pkt_dev->src_min) != 0) {
1389 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1390 strncpy(pkt_dev->src_min, buf, len);
1391 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1392 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1393 }
1394 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001395 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001396 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 i += len;
1398 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1399 return count;
1400 }
1401 if (!strcmp(name, "src_max")) {
1402 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001403 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001404 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001405
Luiz Capitulino222f1802006-03-20 22:16:13 -08001406 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001408 buf[len] = 0;
1409 if (strcmp(buf, pkt_dev->src_max) != 0) {
1410 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1411 strncpy(pkt_dev->src_max, buf, len);
1412 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1413 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1414 }
1415 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001416 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001417 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 i += len;
1419 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1420 return count;
1421 }
1422 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001424 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001425 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001428 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001431 if (!mac_pton(valstr, pkt_dev->dst_mac))
1432 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* Set up Dest MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001434 memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001435
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001436 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return count;
1438 }
1439 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001441 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001442 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001445 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001448 if (!mac_pton(valstr, pkt_dev->src_mac))
1449 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001450 /* Set up Src MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001451 memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001452
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001453 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return count;
1455 }
1456
Luiz Capitulino222f1802006-03-20 22:16:13 -08001457 if (!strcmp(name, "clear_counters")) {
1458 pktgen_clear_counters(pkt_dev);
1459 sprintf(pg_result, "OK: Clearing counters.\n");
1460 return count;
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 if (!strcmp(name, "flows")) {
1464 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001465 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001466 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 i += len;
1469 if (value > MAX_CFLOWS)
1470 value = MAX_CFLOWS;
1471
1472 pkt_dev->cflows = value;
1473 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1474 return count;
1475 }
1476
1477 if (!strcmp(name, "flowlen")) {
1478 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001479 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001480 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 i += len;
1483 pkt_dev->lflow = value;
1484 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1485 return count;
1486 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001487
Robert Olsson45b270f2007-08-28 15:45:55 -07001488 if (!strcmp(name, "queue_map_min")) {
1489 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001490 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001491 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001492
Robert Olsson45b270f2007-08-28 15:45:55 -07001493 i += len;
1494 pkt_dev->queue_map_min = value;
1495 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1496 return count;
1497 }
1498
1499 if (!strcmp(name, "queue_map_max")) {
1500 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001501 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001502 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001503
Robert Olsson45b270f2007-08-28 15:45:55 -07001504 i += len;
1505 pkt_dev->queue_map_max = value;
1506 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1507 return count;
1508 }
1509
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001510 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001511 unsigned n, cnt;
1512
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001513 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001514 if (len < 0)
1515 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001516 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001517 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001518 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001519 cnt += sprintf(pg_result + cnt,
1520 "%08x%s", ntohl(pkt_dev->labels[n]),
1521 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001522
1523 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1524 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1525 pkt_dev->svlan_id = 0xffff;
1526
1527 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001528 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001529 }
1530 return count;
1531 }
1532
1533 if (!strcmp(name, "vlan_id")) {
1534 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001535 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001536 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001537
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001538 i += len;
1539 if (value <= 4095) {
1540 pkt_dev->vlan_id = value; /* turn on VLAN */
1541
1542 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001543 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001544
1545 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001546 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001547
1548 pkt_dev->nr_labels = 0; /* turn off MPLS */
1549 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1550 } else {
1551 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1552 pkt_dev->svlan_id = 0xffff;
1553
1554 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001555 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001556 }
1557 return count;
1558 }
1559
1560 if (!strcmp(name, "vlan_p")) {
1561 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001562 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001563 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001564
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001565 i += len;
1566 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1567 pkt_dev->vlan_p = value;
1568 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1569 } else {
1570 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1571 }
1572 return count;
1573 }
1574
1575 if (!strcmp(name, "vlan_cfi")) {
1576 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001577 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001578 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001579
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001580 i += len;
1581 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1582 pkt_dev->vlan_cfi = value;
1583 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1584 } else {
1585 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1586 }
1587 return count;
1588 }
1589
1590 if (!strcmp(name, "svlan_id")) {
1591 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001592 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001593 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001594
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001595 i += len;
1596 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1597 pkt_dev->svlan_id = value; /* turn on SVLAN */
1598
1599 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001600 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001601
1602 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001603 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001604
1605 pkt_dev->nr_labels = 0; /* turn off MPLS */
1606 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1607 } else {
1608 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1609 pkt_dev->svlan_id = 0xffff;
1610
1611 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001612 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001613 }
1614 return count;
1615 }
1616
1617 if (!strcmp(name, "svlan_p")) {
1618 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001619 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001620 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001621
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001622 i += len;
1623 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1624 pkt_dev->svlan_p = value;
1625 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1626 } else {
1627 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1628 }
1629 return count;
1630 }
1631
1632 if (!strcmp(name, "svlan_cfi")) {
1633 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001634 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001635 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001636
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001637 i += len;
1638 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1639 pkt_dev->svlan_cfi = value;
1640 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1641 } else {
1642 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1643 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001644 return count;
1645 }
1646
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001647 if (!strcmp(name, "tos")) {
1648 __u32 tmp_value = 0;
1649 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001650 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001651 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001652
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001653 i += len;
1654 if (len == 2) {
1655 pkt_dev->tos = tmp_value;
1656 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1657 } else {
1658 sprintf(pg_result, "ERROR: tos must be 00-ff");
1659 }
1660 return count;
1661 }
1662
1663 if (!strcmp(name, "traffic_class")) {
1664 __u32 tmp_value = 0;
1665 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001666 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001667 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001668
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001669 i += len;
1670 if (len == 2) {
1671 pkt_dev->traffic_class = tmp_value;
1672 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1673 } else {
1674 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1675 }
1676 return count;
1677 }
1678
John Fastabend9e50e3a2010-11-16 19:12:28 +00001679 if (!strcmp(name, "skb_priority")) {
1680 len = num_arg(&user_buffer[i], 9, &value);
1681 if (len < 0)
1682 return len;
1683
1684 i += len;
1685 pkt_dev->skb_priority = value;
1686 sprintf(pg_result, "OK: skb_priority=%i",
1687 pkt_dev->skb_priority);
1688 return count;
1689 }
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1692 return -EINVAL;
1693}
1694
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001695static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001697 return single_open(file, pktgen_if_show, PDE(inode)->data);
1698}
1699
Arjan van de Ven9a321442007-02-12 00:55:35 -08001700static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001701 .owner = THIS_MODULE,
1702 .open = pktgen_if_open,
1703 .read = seq_read,
1704 .llseek = seq_lseek,
1705 .write = pktgen_if_write,
1706 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001707};
1708
1709static int pktgen_thread_show(struct seq_file *seq, void *v)
1710{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001711 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001712 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001714 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Luiz Capitulino222f1802006-03-20 22:16:13 -08001716 seq_printf(seq, "Running: ");
1717
1718 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08001719 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001720 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001721 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Luiz Capitulino222f1802006-03-20 22:16:13 -08001723 seq_printf(seq, "\nStopped: ");
1724
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08001725 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001726 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001727 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001730 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001732 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Luiz Capitulino222f1802006-03-20 22:16:13 -08001734 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001739static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001740 const char __user * user_buffer,
1741 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Joe Perches8a994a72010-07-12 10:50:23 +00001743 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001744 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001745 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001747 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001750 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return -EINVAL;
1752 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001753
Paul Gortmakerd6182222010-10-18 12:14:44 +00001754 max = count;
1755 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001756 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001757 return len;
1758
Paul Gortmakerd6182222010-10-18 12:14:44 +00001759 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* Read variable name */
1762
1763 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001765 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 memset(name, 0, sizeof(name));
1768 if (copy_from_user(name, &user_buffer[i], len))
1769 return -EFAULT;
1770 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001771
Luiz Capitulino222f1802006-03-20 22:16:13 -08001772 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001775 return len;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 i += len;
1778
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001779 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001780 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1781 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001784 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 ret = -EINVAL;
1786 goto out;
1787 }
1788
1789 pg_result = &(t->result[0]);
1790
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 if (!strcmp(name, "add_device")) {
1792 char f[32];
1793 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001795 if (len < 0) {
1796 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 goto out;
1798 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001799 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return -EFAULT;
1801 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001802 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001804 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805 ret = count;
1806 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 goto out;
1808 }
1809
Luiz Capitulino222f1802006-03-20 22:16:13 -08001810 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001811 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001812 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001813 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001814 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001816 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto out;
1818 }
1819
Luiz Capitulino222f1802006-03-20 22:16:13 -08001820 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001821 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001822 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto out;
1824 }
1825
1826 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001827out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return ret;
1829}
1830
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001831static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001833 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Arjan van de Ven9a321442007-02-12 00:55:35 -08001836static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001837 .owner = THIS_MODULE,
1838 .open = pktgen_thread_open,
1839 .read = seq_read,
1840 .llseek = seq_lseek,
1841 .write = pktgen_thread_write,
1842 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001843};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 struct pktgen_thread *t;
1849 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001850 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001852 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001853 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001855 if (remove) {
1856 if_lock(t);
1857 pkt_dev->removal_mark = 1;
1858 t->control |= T_REMDEV;
1859 if_unlock(t);
1860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 break;
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001864 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001867/*
1868 * mark a device for removal
1869 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001870static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001873 const int max_tries = 10, msec_per_try = 125;
1874 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001875
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001876 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001877 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001878
Luiz Capitulino222f1802006-03-20 22:16:13 -08001879 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001880
1881 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001882 if (pkt_dev == NULL)
1883 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001884
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001885 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001886 pr_debug("%s: waiting for %s to disappear....\n",
1887 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001888 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001889 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001890
1891 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001892 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1893 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001894 break;
1895 }
1896
1897 }
1898
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001899 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001900}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001901
Stephen Hemminger39df2322007-03-04 16:11:51 -08001902static void pktgen_change_name(struct net_device *dev)
1903{
1904 struct pktgen_thread *t;
1905
1906 list_for_each_entry(t, &pktgen_threads, th_list) {
1907 struct pktgen_dev *pkt_dev;
1908
1909 list_for_each_entry(pkt_dev, &t->if_list, list) {
1910 if (pkt_dev->odev != dev)
1911 continue;
1912
1913 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1914
Alexey Dobriyan29753152009-08-28 23:34:43 -07001915 pkt_dev->entry = proc_create_data(dev->name, 0600,
1916 pg_proc_dir,
1917 &pktgen_if_fops,
1918 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001919 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001920 pr_err("can't move proc entry for '%s'\n",
1921 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001922 break;
1923 }
1924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Luiz Capitulino222f1802006-03-20 22:16:13 -08001927static int pktgen_device_event(struct notifier_block *unused,
1928 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001930 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001932 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001933 return NOTIFY_DONE;
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 /* It is OK that we do not hold the group lock right now,
1936 * as we run under the RTNL lock.
1937 */
1938
1939 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001940 case NETDEV_CHANGENAME:
1941 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001945 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 return NOTIFY_DONE;
1950}
1951
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001952static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1953 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001954{
1955 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001956 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001957
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001958 for (i = 0; ifname[i] != '@'; i++) {
1959 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001960 break;
1961
1962 b[i] = ifname[i];
1963 }
1964 b[i] = 0;
1965
1966 return dev_get_by_name(&init_net, b);
1967}
1968
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970/* Associate pktgen_dev with a device. */
1971
Stephen Hemminger39df2322007-03-04 16:11:51 -08001972static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001973{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001975 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (pkt_dev->odev) {
1979 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001980 pkt_dev->odev = NULL;
1981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Robert Olssone6fce5b2008-08-07 02:23:01 -07001983 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001985 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001986 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001990 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001991 err = -EINVAL;
1992 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001993 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001994 err = -ENETDOWN;
1995 } else {
1996 pkt_dev->odev = odev;
1997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002001 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
2004/* Read pkt_dev from the interface and set up internal pktgen_dev
2005 * structure to have the right information to create/send packets
2006 */
2007static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2008{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002009 int ntxq;
2010
Luiz Capitulino222f1802006-03-20 22:16:13 -08002011 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002012 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002013 sprintf(pkt_dev->result,
2014 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2015 return;
2016 }
2017
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002018 /* make sure that we don't pick a non-existing transmit queue */
2019 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002020
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002021 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002022 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2023 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2024 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002025 pkt_dev->queue_map_min = ntxq - 1;
2026 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002027 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002028 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2029 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2030 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002031 pkt_dev->queue_map_max = ntxq - 1;
2032 }
2033
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002036 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002037 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Luiz Capitulino222f1802006-03-20 22:16:13 -08002039 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002040 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Luiz Capitulino222f1802006-03-20 22:16:13 -08002042 /* Set up pkt size */
2043 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2044
2045 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002047 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * gets exported
2049 */
2050
2051#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002052 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 struct inet6_dev *idev;
2054
Luiz Capitulino222f1802006-03-20 22:16:13 -08002055 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2056 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 set = 1;
2058 break;
2059 }
2060
Luiz Capitulino222f1802006-03-20 22:16:13 -08002061 if (!set) {
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 /*
2064 * Use linklevel address if unconfigured.
2065 *
2066 * use ipv6_get_lladdr if/when it's get exported
2067 */
2068
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002069 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002070 idev = __in6_dev_get(pkt_dev->odev);
2071 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct inet6_ifaddr *ifp;
2073
2074 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002075 for (ifp = idev->addr_list; ifp;
2076 ifp = ifp->if_next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002077 if (ifp->scope == IFA_LINK &&
2078 !(ifp->flags & IFA_F_TENTATIVE)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002079 ipv6_addr_copy(&pkt_dev->
2080 cur_in6_saddr,
2081 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 err = 0;
2083 break;
2084 }
2085 }
2086 read_unlock_bh(&idev->lock);
2087 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002088 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002089 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002090 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002093 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 pkt_dev->saddr_min = 0;
2095 pkt_dev->saddr_max = 0;
2096 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002097
2098 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002101 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (in_dev) {
2103 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002104 pkt_dev->saddr_min =
2105 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 pkt_dev->saddr_max = pkt_dev->saddr_min;
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002110 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2112 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2113 }
2114
2115 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2116 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2117 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002118 /* Initialize current values. */
2119 pkt_dev->cur_dst_mac_offset = 0;
2120 pkt_dev->cur_src_mac_offset = 0;
2121 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2122 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2123 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2124 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 pkt_dev->nflows = 0;
2126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002129static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2130{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002131 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002132 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002133 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002134
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002135 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2136 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002137
Daniel Turull43d28b62010-06-09 22:49:57 +00002138 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002139 if (remaining <= 0) {
2140 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002141 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002142 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002143
Stephen Hemmingeref879792009-09-22 19:41:43 +00002144 start_time = ktime_now();
Daniel Turull43d28b62010-06-09 22:49:57 +00002145 if (remaining < 100000)
2146 ndelay(remaining); /* really small just spin */
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002147 else {
2148 /* see do_nanosleep */
2149 hrtimer_init_sleeper(&t, current);
2150 do {
2151 set_current_state(TASK_INTERRUPTIBLE);
2152 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2153 if (!hrtimer_active(&t.timer))
2154 t.task = NULL;
2155
2156 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002159 hrtimer_cancel(&t.timer);
2160 } while (t.task && pkt_dev->running && !signal_pending(current));
2161 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002163 end_time = ktime_now();
2164
2165 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002166 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002169static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2170{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002171 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002172 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2173 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2174 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2175}
2176
Stephen Hemminger648fda72009-08-27 13:55:07 +00002177static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002178{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002179 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002180}
2181
2182static inline int f_pick(struct pktgen_dev *pkt_dev)
2183{
2184 int flow = pkt_dev->curfl;
2185
2186 if (pkt_dev->flags & F_FLOW_SEQ) {
2187 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2188 /* reset time */
2189 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002190 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002191 pkt_dev->curfl += 1;
2192 if (pkt_dev->curfl >= pkt_dev->cflows)
2193 pkt_dev->curfl = 0; /*reset */
2194 }
2195 } else {
2196 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002197 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002198
Robert Olsson1211a642008-08-05 18:44:26 -07002199 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002200 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002201 pkt_dev->flows[flow].flags = 0;
2202 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002203 }
2204
2205 return pkt_dev->curfl;
2206}
2207
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002208
2209#ifdef CONFIG_XFRM
2210/* If there was already an IPSEC SA, we keep it as is, else
2211 * we go look for it ...
2212*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002213#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002214static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002215{
2216 struct xfrm_state *x = pkt_dev->flows[flow].x;
2217 if (!x) {
2218 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002219 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002220 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002221 (xfrm_address_t *)&pkt_dev->cur_saddr,
2222 AF_INET,
2223 pkt_dev->ipsmode,
2224 pkt_dev->ipsproto, 0);
2225 if (x) {
2226 pkt_dev->flows[flow].x = x;
2227 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002228 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002229 }
2230
2231 }
2232}
2233#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002234static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2235{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002236
2237 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2238 pkt_dev->cur_queue_map = smp_processor_id();
2239
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002240 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002241 __u16 t;
2242 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2243 t = random32() %
2244 (pkt_dev->queue_map_max -
2245 pkt_dev->queue_map_min + 1)
2246 + pkt_dev->queue_map_min;
2247 } else {
2248 t = pkt_dev->cur_queue_map + 1;
2249 if (t > pkt_dev->queue_map_max)
2250 t = pkt_dev->queue_map_min;
2251 }
2252 pkt_dev->cur_queue_map = t;
2253 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002254 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 -07002255}
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257/* Increment/randomize headers according to flags and current values
2258 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2259 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002260static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2261{
2262 __u32 imn;
2263 __u32 imx;
2264 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002266 if (pkt_dev->cflows)
2267 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002270 if (pkt_dev->src_mac_count > 1) {
2271 __u32 mc;
2272 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Luiz Capitulino222f1802006-03-20 22:16:13 -08002274 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002275 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002276 else {
2277 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002278 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002279 pkt_dev->src_mac_count)
2280 pkt_dev->cur_src_mac_offset = 0;
2281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Luiz Capitulino222f1802006-03-20 22:16:13 -08002283 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2284 pkt_dev->hh[11] = tmp;
2285 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2286 pkt_dev->hh[10] = tmp;
2287 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2288 pkt_dev->hh[9] = tmp;
2289 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2290 pkt_dev->hh[8] = tmp;
2291 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2292 pkt_dev->hh[7] = tmp;
2293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Luiz Capitulino222f1802006-03-20 22:16:13 -08002295 /* Deal with Destination MAC */
2296 if (pkt_dev->dst_mac_count > 1) {
2297 __u32 mc;
2298 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Luiz Capitulino222f1802006-03-20 22:16:13 -08002300 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002301 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Luiz Capitulino222f1802006-03-20 22:16:13 -08002303 else {
2304 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002305 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002306 pkt_dev->dst_mac_count) {
2307 pkt_dev->cur_dst_mac_offset = 0;
2308 }
2309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Luiz Capitulino222f1802006-03-20 22:16:13 -08002311 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2312 pkt_dev->hh[5] = tmp;
2313 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2314 pkt_dev->hh[4] = tmp;
2315 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2316 pkt_dev->hh[3] = tmp;
2317 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2318 pkt_dev->hh[2] = tmp;
2319 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2320 pkt_dev->hh[1] = tmp;
2321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002323 if (pkt_dev->flags & F_MPLS_RND) {
2324 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002325 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002326 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2327 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002328 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002329 htonl(0x000fffff));
2330 }
2331
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002332 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002333 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002334 }
2335
2336 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002337 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002338 }
2339
Luiz Capitulino222f1802006-03-20 22:16:13 -08002340 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2341 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002342 pkt_dev->cur_udp_src = random32() %
2343 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2344 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Luiz Capitulino222f1802006-03-20 22:16:13 -08002346 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 pkt_dev->cur_udp_src++;
2348 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2349 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002350 }
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Luiz Capitulino222f1802006-03-20 22:16:13 -08002353 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2354 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002355 pkt_dev->cur_udp_dst = random32() %
2356 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2357 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002358 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002360 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002362 }
2363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 if (!(pkt_dev->flags & F_IPV6)) {
2366
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002367 imn = ntohl(pkt_dev->saddr_min);
2368 imx = ntohl(pkt_dev->saddr_max);
2369 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002371 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002372 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 else {
2374 t = ntohl(pkt_dev->cur_saddr);
2375 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002376 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 pkt_dev->cur_saddr = htonl(t);
2381 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002382
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002383 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2385 } else {
Al Viro252e33462006-11-14 20:48:11 -08002386 imn = ntohl(pkt_dev->daddr_min);
2387 imx = ntohl(pkt_dev->daddr_max);
2388 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002390 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (pkt_dev->flags & F_IPDST_RND) {
2392
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002393 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002394 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Joe Perches21cf2252007-12-16 13:44:00 -08002396 while (ipv4_is_loopback(s) ||
2397 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002398 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002399 ipv4_is_zeronet(s) ||
2400 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002401 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002402 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
Al Viro252e33462006-11-14 20:48:11 -08002404 pkt_dev->cur_daddr = s;
2405 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 t = ntohl(pkt_dev->cur_daddr);
2407 t++;
2408 if (t > imx) {
2409 t = imn;
2410 }
2411 pkt_dev->cur_daddr = htonl(t);
2412 }
2413 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002414 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002415 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002416 pkt_dev->flows[flow].cur_daddr =
2417 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002418#ifdef CONFIG_XFRM
2419 if (pkt_dev->flags & F_IPSEC_ON)
2420 get_ipsec_sa(pkt_dev, flow);
2421#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 pkt_dev->nflows++;
2423 }
2424 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002425 } else { /* IPV6 * */
2426
2427 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2428 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2429 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2430 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 else {
2432 int i;
2433
2434 /* Only random destinations yet */
2435
Luiz Capitulino222f1802006-03-20 22:16:13 -08002436 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002438 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002439 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2440 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444
Luiz Capitulino222f1802006-03-20 22:16:13 -08002445 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2446 __u32 t;
2447 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002448 t = random32() %
2449 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2450 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002451 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002453 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002455 }
2456 pkt_dev->cur_pkt_size = t;
2457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002459 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 pkt_dev->flows[flow].count++;
2462}
2463
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002464
2465#ifdef CONFIG_XFRM
2466static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2467{
2468 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2469 int err = 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002470
2471 if (!x)
2472 return 0;
2473 /* XXX: we dont support tunnel mode for now until
2474 * we resolve the dst issue */
2475 if (x->props.mode != XFRM_MODE_TRANSPORT)
2476 return 0;
2477
2478 spin_lock(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002479
Herbert Xu13996372007-10-17 21:35:51 -07002480 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002481 if (err)
2482 goto error;
2483 err = x->type->output(x, skb);
2484 if (err)
2485 goto error;
2486
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002487 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002488 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002489error:
2490 spin_unlock(&x->lock);
2491 return err;
2492}
2493
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002494static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002495{
2496 if (pkt_dev->cflows) {
2497 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002498 int i;
2499 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002500 struct xfrm_state *x = pkt_dev->flows[i].x;
2501 if (x) {
2502 xfrm_state_put(x);
2503 pkt_dev->flows[i].x = NULL;
2504 }
2505 }
2506 }
2507}
2508
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002509static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002510 struct sk_buff *skb, __be16 protocol)
2511{
2512 if (pkt_dev->flags & F_IPSEC_ON) {
2513 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2514 int nhead = 0;
2515 if (x) {
2516 int ret;
2517 __u8 *eth;
2518 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002519 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002520 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2521 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002522 pr_err("Error expanding ipsec packet %d\n",
2523 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002524 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002525 }
2526 }
2527
2528 /* ipsec is not expecting ll header */
2529 skb_pull(skb, ETH_HLEN);
2530 ret = pktgen_output_ipsec(skb, pkt_dev);
2531 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002532 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002533 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002534 }
2535 /* restore ll */
2536 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2537 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002538 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002539 }
2540 }
2541 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002542err:
2543 kfree_skb(skb);
2544 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002545}
2546#endif
2547
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002548static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2549{
2550 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002551 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002552 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002553
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002554 mpls--;
2555 *mpls |= MPLS_STACK_BOTTOM;
2556}
2557
Al Viro0f37c602006-11-03 03:49:56 -08002558static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2559 unsigned int prio)
2560{
2561 return htons(id | (cfi << 12) | (prio << 13));
2562}
2563
Eric Dumazet26ad7872011-01-25 13:26:05 -08002564static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2565 int datalen)
2566{
2567 struct timeval timestamp;
2568 struct pktgen_hdr *pgh;
2569
2570 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2571 datalen -= sizeof(*pgh);
2572
2573 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002574 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002575 } else {
2576 int frags = pkt_dev->nfrags;
2577 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002578 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002579
2580
2581 if (frags > MAX_SKB_FRAGS)
2582 frags = MAX_SKB_FRAGS;
2583 len = datalen - frags * PAGE_SIZE;
2584 if (len > 0) {
2585 memset(skb_put(skb, len), 0, len);
2586 datalen = frags * PAGE_SIZE;
2587 }
2588
2589 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002590 frag_len = (datalen/frags) < PAGE_SIZE ?
2591 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002592 while (datalen > 0) {
2593 if (unlikely(!pkt_dev->page)) {
2594 int node = numa_node_id();
2595
2596 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2597 node = pkt_dev->node;
2598 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2599 if (!pkt_dev->page)
2600 break;
2601 }
2602 skb_shinfo(skb)->frags[i].page = pkt_dev->page;
2603 get_page(pkt_dev->page);
2604 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002605 /*last fragment, fill rest of data*/
2606 if (i == (frags - 1))
2607 skb_shinfo(skb)->frags[i].size =
2608 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2609 else
2610 skb_shinfo(skb)->frags[i].size = frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002611 datalen -= skb_shinfo(skb)->frags[i].size;
2612 skb->len += skb_shinfo(skb)->frags[i].size;
2613 skb->data_len += skb_shinfo(skb)->frags[i].size;
2614 i++;
2615 skb_shinfo(skb)->nr_frags = i;
2616 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002617 }
2618
2619 /* Stamp the time, and sequence number,
2620 * convert them to network byte order
2621 */
2622 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2623 pgh->seq_num = htonl(pkt_dev->seq_num);
2624
2625 do_gettimeofday(&timestamp);
2626 pgh->tv_sec = htonl(timestamp.tv_sec);
2627 pgh->tv_usec = htonl(timestamp.tv_usec);
2628}
2629
Luiz Capitulino222f1802006-03-20 22:16:13 -08002630static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2631 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 struct sk_buff *skb = NULL;
2634 __u8 *eth;
2635 struct udphdr *udph;
2636 int datalen, iplen;
2637 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002638 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002639 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002640 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2641 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2642 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2643 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002644 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002645
2646 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002647 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002648
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002649 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002650 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002651
Robert Olsson64053be2005-06-26 15:27:10 -07002652 /* Update any of the values, used when we're incrementing various
2653 * fields.
2654 */
2655 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002656 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002657
David S. Miller7ac54592006-01-18 14:19:10 -08002658 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002659
2660 if (pkt_dev->flags & F_NODE) {
2661 int node;
2662
2663 if (pkt_dev->node >= 0)
2664 node = pkt_dev->node;
2665 else
2666 node = numa_node_id();
2667
2668 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2669 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2670 if (likely(skb)) {
2671 skb_reserve(skb, NET_SKB_PAD);
2672 skb->dev = odev;
2673 }
2674 }
2675 else
2676 skb = __netdev_alloc_skb(odev,
2677 pkt_dev->cur_pkt_size + 64
2678 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (!skb) {
2681 sprintf(pkt_dev->result, "No memory");
2682 return NULL;
2683 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002684 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
David S. Miller7ac54592006-01-18 14:19:10 -08002686 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 /* Reserve for ethernet and IP header */
2689 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002690 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2691 if (pkt_dev->nr_labels)
2692 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002693
2694 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002695 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002696 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002697 *svlan_tci = build_tci(pkt_dev->svlan_id,
2698 pkt_dev->svlan_cfi,
2699 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002700 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002701 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002702 }
2703 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002704 *vlan_tci = build_tci(pkt_dev->vlan_id,
2705 pkt_dev->vlan_cfi,
2706 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002707 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002708 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002709 }
2710
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002711 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002712 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002713 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002714 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002715 skb->priority = pkt_dev->skb_priority;
2716
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002717 iph = ip_hdr(skb);
2718 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002721 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002723 /* Eth + IPh + UDPh + mpls */
2724 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002725 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002726 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 udph->source = htons(pkt_dev->cur_udp_src);
2730 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002731 udph->len = htons(datalen + 8); /* DATA + udphdr */
2732 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 iph->ihl = 5;
2735 iph->version = 4;
2736 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002737 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002738 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 iph->saddr = pkt_dev->cur_saddr;
2740 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002741 iph->id = htons(pkt_dev->ip_id);
2742 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 iph->frag_off = 0;
2744 iplen = 20 + 8 + datalen;
2745 iph->tot_len = htons(iplen);
2746 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002747 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002748 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002749 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002750 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 skb->dev = odev;
2752 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002753 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002754
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002755#ifdef CONFIG_XFRM
2756 if (!process_ipsec(pkt_dev, skb, protocol))
2757 return NULL;
2758#endif
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return skb;
2761}
2762
2763/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002764 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2766 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002767 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 * Should be candidate for net/ipv4/utils.c
2769 * --ro
2770 */
2771
Luiz Capitulino222f1802006-03-20 22:16:13 -08002772static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002775 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 unsigned long u;
2777 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002778 unsigned int prefixlen = 0;
2779 unsigned int suffixlen = 0;
Al Viro252e33462006-11-14 20:48:11 -08002780 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002781 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Luiz Capitulino222f1802006-03-20 22:16:13 -08002783 for (i = 0; i < 16; i++)
2784 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 for (;;) {
2787 if (*s == ':') {
2788 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002789 if (s[1] == ':') { /* Found "::", skip to part 2 */
2790 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 len++;
2792 break;
2793 }
2794 s++;
2795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002797 u = simple_strtoul(s, &pos, 16);
2798 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002799 if (!i)
2800 return 0;
2801 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 /* the last 4 bytes may be written as IPv4 address */
2804
2805 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002806 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2807 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
2809 ip[prefixlen++] = (u >> 8);
2810 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002811 s += i;
2812 len += i;
2813 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return len;
2815 }
2816
2817/* part 2, after "::" */
2818 for (;;) {
2819 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002820 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 break;
2822 s++;
2823 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002824 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002826
2827 u = simple_strtol(s, &pos, 16);
2828 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002830 if (*s)
2831 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 break;
2833 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002834 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002836 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2837 sizeof(tmp));
2838 suffixlen += 4;
2839 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 break;
2841 }
2842 suffix[suffixlen++] = (u >> 8);
2843 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002844 s += i;
2845 len += i;
2846 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 break;
2848 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002849 for (i = 0; i < suffixlen; i++)
2850 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return len;
2852}
2853
Luiz Capitulino222f1802006-03-20 22:16:13 -08002854static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2855 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
2857 struct sk_buff *skb = NULL;
2858 __u8 *eth;
2859 struct udphdr *udph;
2860 int datalen;
2861 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002862 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002863 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002864 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2865 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2866 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2867 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002868 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002869
2870 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002871 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002872
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002873 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002874 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002875
Robert Olsson64053be2005-06-26 15:27:10 -07002876 /* Update any of the values, used when we're incrementing various
2877 * fields.
2878 */
2879 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002880 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002881
Stephen Hemmingere4707572009-08-27 13:55:13 +00002882 skb = __netdev_alloc_skb(odev,
2883 pkt_dev->cur_pkt_size + 64
2884 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (!skb) {
2886 sprintf(pkt_dev->result, "No memory");
2887 return NULL;
2888 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002889 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 skb_reserve(skb, 16);
2892
2893 /* Reserve for ethernet and IP header */
2894 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002895 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2896 if (pkt_dev->nr_labels)
2897 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002898
2899 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002900 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002901 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002902 *svlan_tci = build_tci(pkt_dev->svlan_id,
2903 pkt_dev->svlan_cfi,
2904 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002905 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002906 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002907 }
2908 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002909 *vlan_tci = build_tci(pkt_dev->vlan_id,
2910 pkt_dev->vlan_cfi,
2911 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002912 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002913 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002914 }
2915
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002916 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002917 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002918 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002919 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002920 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002921 iph = ipv6_hdr(skb);
2922 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002925 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002926
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002927 /* Eth + IPh + UDPh + mpls */
2928 datalen = pkt_dev->cur_pkt_size - 14 -
2929 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002930 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Luiz Capitulino222f1802006-03-20 22:16:13 -08002932 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 datalen = sizeof(struct pktgen_hdr);
2934 if (net_ratelimit())
Joe Perchesf9467ea2010-06-21 12:29:14 +00002935 pr_info("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
2937
2938 udph->source = htons(pkt_dev->cur_udp_src);
2939 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002940 udph->len = htons(datalen + sizeof(struct udphdr));
2941 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002943 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002945 if (pkt_dev->traffic_class) {
2946 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002947 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002948 }
2949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 iph->hop_limit = 32;
2951
2952 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2953 iph->nexthdr = IPPROTO_UDP;
2954
2955 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2956 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2957
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002958 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002959 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002960 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 skb->dev = odev;
2962 skb->pkt_type = PACKET_HOST;
2963
Eric Dumazet26ad7872011-01-25 13:26:05 -08002964 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return skb;
2967}
2968
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002969static struct sk_buff *fill_packet(struct net_device *odev,
2970 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002972 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return fill_packet_ipv6(odev, pkt_dev);
2974 else
2975 return fill_packet_ipv4(odev, pkt_dev);
2976}
2977
Luiz Capitulino222f1802006-03-20 22:16:13 -08002978static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002980 pkt_dev->seq_num = 1;
2981 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002983 pkt_dev->tx_bytes = 0;
2984 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
2986
2987/* Set up structure for sending pkts, clear counters */
2988
2989static void pktgen_run(struct pktgen_thread *t)
2990{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08002991 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 int started = 0;
2993
Joe Perchesf9467ea2010-06-21 12:29:14 +00002994 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08002997 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 /*
3000 * setup odev and create initial packet.
3001 */
3002 pktgen_setup_inject(pkt_dev);
3003
Luiz Capitulino222f1802006-03-20 22:16:13 -08003004 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003006 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003008 pkt_dev->started_at =
3009 pkt_dev->next_tx = ktime_now();
3010
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003011 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 strcpy(pkt_dev->result, "Starting");
3014 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003015 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 strcpy(pkt_dev->result, "Error starting");
3017 }
3018 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003019 if (started)
3020 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021}
3022
3023static void pktgen_stop_all_threads_ifs(void)
3024{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003025 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Joe Perchesf9467ea2010-06-21 12:29:14 +00003027 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003029 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003030
3031 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003032 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003033
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003034 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
3036
Stephen Hemminger648fda72009-08-27 13:55:07 +00003037static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003039 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003041 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003042 if (pkt_dev->running)
3043 return 1;
3044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Luiz Capitulino222f1802006-03-20 22:16:13 -08003047static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003049 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Luiz Capitulino222f1802006-03-20 22:16:13 -08003051 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Luiz Capitulino222f1802006-03-20 22:16:13 -08003053 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Luiz Capitulino222f1802006-03-20 22:16:13 -08003055 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Luiz Capitulino222f1802006-03-20 22:16:13 -08003057 if (signal_pending(current))
3058 goto signal;
3059 if_lock(t);
3060 }
3061 if_unlock(t);
3062 return 1;
3063signal:
3064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
3067static int pktgen_wait_all_threads_run(void)
3068{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003069 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003071
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003072 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003073
3074 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003076 if (sig == 0)
3077 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003079
3080 if (sig == 0)
3081 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003083
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003084 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return sig;
3086}
3087
3088static void pktgen_run_all_threads(void)
3089{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003090 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Joe Perchesf9467ea2010-06-21 12:29:14 +00003092 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003094 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003096 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003098
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003099 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003101 /* Propagate thread->control */
3102 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 pktgen_wait_all_threads_run();
3105}
3106
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003107static void pktgen_reset_all_threads(void)
3108{
3109 struct pktgen_thread *t;
3110
Joe Perchesf9467ea2010-06-21 12:29:14 +00003111 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003112
3113 mutex_lock(&pktgen_thread_lock);
3114
3115 list_for_each_entry(t, &pktgen_threads, th_list)
3116 t->control |= (T_REMDEVALL);
3117
3118 mutex_unlock(&pktgen_thread_lock);
3119
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003120 /* Propagate thread->control */
3121 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003122
3123 pktgen_wait_all_threads_run();
3124}
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3127{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003128 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003129 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003130 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3131 pkt_dev->started_at);
3132 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Daniel Turull03a14ab2011-03-09 14:11:00 -08003134 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003135 (unsigned long long)ktime_to_us(elapsed),
3136 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3137 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003138 (unsigned long long)pkt_dev->sofar,
3139 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003141 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3142 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144 bps = pps * 8 * pkt_dev->cur_pkt_size;
3145
3146 mbps = bps;
3147 do_div(mbps, 1000000);
3148 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3149 (unsigned long long)pps,
3150 (unsigned long long)mbps,
3151 (unsigned long long)bps,
3152 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003159
Luiz Capitulino222f1802006-03-20 22:16:13 -08003160 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003161 pr_warning("interface: %s is already stopped\n",
3162 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003163 return -EINVAL;
3164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003166 kfree_skb(pkt_dev->skb);
3167 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003168 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003171 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Luiz Capitulino222f1802006-03-20 22:16:13 -08003173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
Luiz Capitulino222f1802006-03-20 22:16:13 -08003176static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003178 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 if_lock(t);
3181
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003182 list_for_each_entry(pkt_dev, &t->if_list, list) {
3183 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003184 continue;
3185 if (best == NULL)
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003186 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003187 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003188 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
3190 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003191 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
Luiz Capitulino222f1802006-03-20 22:16:13 -08003194static void pktgen_stop(struct pktgen_thread *t)
3195{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003196 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Joe Perchesf9467ea2010-06-21 12:29:14 +00003198 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Luiz Capitulino222f1802006-03-20 22:16:13 -08003200 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003202 list_for_each_entry(pkt_dev, &t->if_list, list) {
3203 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Luiz Capitulino222f1802006-03-20 22:16:13 -08003206 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003209/*
3210 * one of our devices needs to be removed - find it
3211 * and remove it
3212 */
3213static void pktgen_rem_one_if(struct pktgen_thread *t)
3214{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003215 struct list_head *q, *n;
3216 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003217
Joe Perchesf9467ea2010-06-21 12:29:14 +00003218 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003219
3220 if_lock(t);
3221
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003222 list_for_each_safe(q, n, &t->if_list) {
3223 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003224
Luiz Capitulino222f1802006-03-20 22:16:13 -08003225 if (!cur->removal_mark)
3226 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003227
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003228 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003229 cur->skb = NULL;
3230
3231 pktgen_remove_device(t, cur);
3232
3233 break;
3234 }
3235
3236 if_unlock(t);
3237}
3238
Luiz Capitulino222f1802006-03-20 22:16:13 -08003239static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003241 struct list_head *q, *n;
3242 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243
Joe Perchesf9467ea2010-06-21 12:29:14 +00003244 func_enter();
3245
Luiz Capitulino222f1802006-03-20 22:16:13 -08003246 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003247
Luiz Capitulino222f1802006-03-20 22:16:13 -08003248 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003250 list_for_each_safe(q, n, &t->if_list) {
3251 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003252
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003253 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003254 cur->skb = NULL;
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 pktgen_remove_device(t, cur);
3257 }
3258
Luiz Capitulino222f1802006-03-20 22:16:13 -08003259 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Luiz Capitulino222f1802006-03-20 22:16:13 -08003262static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003264 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
David S. Milleree74baa2007-01-01 20:51:53 -08003266 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268}
3269
Stephen Hemmingeref879792009-09-22 19:41:43 +00003270static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003271{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003272 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003273 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003274 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003275}
3276
Stephen Hemmingeref879792009-09-22 19:41:43 +00003277static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3278{
3279 ktime_t idle_start = ktime_now();
3280
3281 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3282 if (signal_pending(current))
3283 break;
3284
3285 if (need_resched())
3286 pktgen_resched(pkt_dev);
3287 else
3288 cpu_relax();
3289 }
3290 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3291}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003292
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003293static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
Stephen Hemminger00829822008-11-20 20:14:53 -08003295 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003296 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003297 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003298 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003299 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 int ret;
3301
Stephen Hemmingeref879792009-09-22 19:41:43 +00003302 /* If device is offline, then don't send */
3303 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3304 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003305 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003307
Stephen Hemmingeref879792009-09-22 19:41:43 +00003308 /* This is max DELAY, this has special meaning of
3309 * "never transmit"
3310 */
3311 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3312 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3313 return;
3314 }
3315
3316 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003317 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3318 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3319 /* build a new pkt */
3320 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003321
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003322 pkt_dev->skb = fill_packet(odev, pkt_dev);
3323 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003324 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003325 schedule();
3326 pkt_dev->clone_count--; /* back out increment, OOM */
3327 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003329 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003330 pkt_dev->allocated_skbs++;
3331 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003333
Stephen Hemmingeref879792009-09-22 19:41:43 +00003334 if (pkt_dev->delay && pkt_dev->last_ok)
3335 spin(pkt_dev, pkt_dev->next_tx);
3336
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003337 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3338 txq = netdev_get_tx_queue(odev, queue_map);
3339
3340 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003341
Eric Dumazet5a0d2262010-11-23 10:42:02 +00003342 if (unlikely(netif_tx_queue_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003343 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003344 pkt_dev->last_ok = 0;
3345 goto unlock;
3346 }
3347 atomic_inc(&(pkt_dev->skb->users));
3348 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Stephen Hemmingeref879792009-09-22 19:41:43 +00003350 switch (ret) {
3351 case NETDEV_TX_OK:
3352 txq_trans_update(txq);
3353 pkt_dev->last_ok = 1;
3354 pkt_dev->sofar++;
3355 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003356 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003357 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003358 case NET_XMIT_DROP:
3359 case NET_XMIT_CN:
3360 case NET_XMIT_POLICED:
3361 /* skb has been consumed */
3362 pkt_dev->errors++;
3363 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003364 default: /* Drivers are not supposed to return other values! */
3365 if (net_ratelimit())
Joe Perches58231182010-12-20 10:30:06 -08003366 pr_info("%s xmit error: %d\n", pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003367 pkt_dev->errors++;
3368 /* fallthru */
3369 case NETDEV_TX_LOCKED:
3370 case NETDEV_TX_BUSY:
3371 /* Retry it next time */
3372 atomic_dec(&(pkt_dev->skb->users));
3373 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003374 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003375unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003376 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 /* If pkt_dev->count is zero, then run forever */
3379 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003380 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 /* Done with this */
3383 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003384 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003387/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 * Main loop of the thread goes here
3389 */
3390
David S. Milleree74baa2007-01-01 20:51:53 -08003391static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003394 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003395 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
David S. Milleree74baa2007-01-01 20:51:53 -08003398 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
3400 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003401 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Joe Perchesf9467ea2010-06-21 12:29:14 +00003403 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
David S. Milleree74baa2007-01-01 20:51:53 -08003405 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003406
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003407 set_freezable();
3408
David S. Milleree74baa2007-01-01 20:51:53 -08003409 while (!kthread_should_stop()) {
3410 pkt_dev = next_to_run(t);
3411
Stephen Hemmingeref879792009-09-22 19:41:43 +00003412 if (unlikely(!pkt_dev && t->control == 0)) {
Eric Dumazet551eaff2010-11-21 10:26:44 -08003413 if (pktgen_exiting)
3414 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003415 wait_event_interruptible_timeout(t->queue,
3416 t->control != 0,
3417 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003418 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003419 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003420 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 __set_current_state(TASK_RUNNING);
3423
Stephen Hemmingeref879792009-09-22 19:41:43 +00003424 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 pktgen_xmit(pkt_dev);
3426
Stephen Hemmingeref879792009-09-22 19:41:43 +00003427 if (need_resched())
3428 pktgen_resched(pkt_dev);
3429 else
3430 cpu_relax();
3431 }
3432
Luiz Capitulino222f1802006-03-20 22:16:13 -08003433 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 pktgen_stop(t);
3435 t->control &= ~(T_STOP);
3436 }
3437
Luiz Capitulino222f1802006-03-20 22:16:13 -08003438 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 pktgen_run(t);
3440 t->control &= ~(T_RUN);
3441 }
3442
Luiz Capitulino222f1802006-03-20 22:16:13 -08003443 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003445 t->control &= ~(T_REMDEVALL);
3446 }
3447
Luiz Capitulino222f1802006-03-20 22:16:13 -08003448 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003449 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 t->control &= ~(T_REMDEV);
3451 }
3452
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003453 try_to_freeze();
3454
David S. Milleree74baa2007-01-01 20:51:53 -08003455 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Joe Perchesf9467ea2010-06-21 12:29:14 +00003458 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003459 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Joe Perchesf9467ea2010-06-21 12:29:14 +00003461 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003462 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Joe Perchesf9467ea2010-06-21 12:29:14 +00003464 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003465 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003466
Eric Dumazet551eaff2010-11-21 10:26:44 -08003467 /* Wait for kthread_stop */
3468 while (!kthread_should_stop()) {
3469 set_current_state(TASK_INTERRUPTIBLE);
3470 schedule();
3471 }
3472 __set_current_state(TASK_RUNNING);
3473
David S. Milleree74baa2007-01-01 20:51:53 -08003474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475}
3476
Luiz Capitulino222f1802006-03-20 22:16:13 -08003477static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003478 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003480 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003481 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Eric Dumazet3e984842009-11-24 14:50:53 -08003483 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003484 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003485 if (strncmp(p->odevname, ifname, len) == 0) {
3486 if (p->odevname[len]) {
3487 if (exact || p->odevname[len] != '@')
3488 continue;
3489 }
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003490 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003491 break;
3492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Luiz Capitulino222f1802006-03-20 22:16:13 -08003494 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003495 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003496 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497}
3498
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003499/*
3500 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 */
3502
Luiz Capitulino222f1802006-03-20 22:16:13 -08003503static int add_dev_to_thread(struct pktgen_thread *t,
3504 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
3506 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Luiz Capitulino222f1802006-03-20 22:16:13 -08003508 if_lock(t);
3509
3510 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003511 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003512 rv = -EBUSY;
3513 goto out;
3514 }
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003515
3516 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 pkt_dev->running = 0;
3519
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520out:
3521 if_unlock(t);
3522 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
3525/* Called under thread lock */
3526
Luiz Capitulino222f1802006-03-20 22:16:13 -08003527static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003529 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003530 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003531 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 /* We don't allow a device to be on several threads */
3534
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003535 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3536 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003537 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003538 return -EBUSY;
3539 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003540
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003541 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003542 if (!pkt_dev)
3543 return -ENOMEM;
3544
Eric Dumazet593f63b2009-11-23 01:44:37 +00003545 strcpy(pkt_dev->odevname, ifname);
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003546 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3547 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003548 if (pkt_dev->flows == NULL) {
3549 kfree(pkt_dev);
3550 return -ENOMEM;
3551 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003552 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003553
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003554 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003555 pkt_dev->min_pkt_size = ETH_ZLEN;
3556 pkt_dev->max_pkt_size = ETH_ZLEN;
3557 pkt_dev->nfrags = 0;
3558 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003559 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003560 pkt_dev->count = pg_count_d;
3561 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003562 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003563 pkt_dev->udp_src_max = 9;
3564 pkt_dev->udp_dst_min = 9;
3565 pkt_dev->udp_dst_max = 9;
3566
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003567 pkt_dev->vlan_p = 0;
3568 pkt_dev->vlan_cfi = 0;
3569 pkt_dev->vlan_id = 0xffff;
3570 pkt_dev->svlan_p = 0;
3571 pkt_dev->svlan_cfi = 0;
3572 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003573 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003574
Stephen Hemminger39df2322007-03-04 16:11:51 -08003575 err = pktgen_setup_dev(pkt_dev, ifname);
3576 if (err)
3577 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003578
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003579 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3580 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003581 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003582 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003583 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003584 err = -EINVAL;
3585 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003586 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003587#ifdef CONFIG_XFRM
3588 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3589 pkt_dev->ipsproto = IPPROTO_ESP;
3590#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003591
3592 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003593out2:
3594 dev_put(pkt_dev->odev);
3595out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003596#ifdef CONFIG_XFRM
3597 free_SAs(pkt_dev);
3598#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003599 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003600 kfree(pkt_dev);
3601 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
David S. Milleree74baa2007-01-01 20:51:53 -08003604static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003606 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003607 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003608 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003610 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3611 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003612 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003613 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003614 return -ENOMEM;
3615 }
3616
Luiz Capitulino222f1802006-03-20 22:16:13 -08003617 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003619
David S. Milleree74baa2007-01-01 20:51:53 -08003620 INIT_LIST_HEAD(&t->if_list);
3621
3622 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003623 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003624
Eric Dumazet94dcf292011-03-22 16:30:45 -07003625 p = kthread_create_on_node(pktgen_thread_worker,
3626 t,
3627 cpu_to_node(cpu),
3628 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003629 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003630 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003631 list_del(&t->th_list);
3632 kfree(t);
3633 return PTR_ERR(p);
3634 }
3635 kthread_bind(p, cpu);
3636 t->tsk = p;
3637
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003638 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3639 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003640 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003641 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003642 PG_PROC_DIR, t->tsk->comm);
3643 kthread_stop(p);
3644 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003645 kfree(t);
3646 return -EINVAL;
3647 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003648
David S. Milleree74baa2007-01-01 20:51:53 -08003649 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003650 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
3652 return 0;
3653}
3654
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003655/*
3656 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003658static void _rem_dev_from_if_list(struct pktgen_thread *t,
3659 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003661 struct list_head *q, *n;
3662 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003664 list_for_each_safe(q, n, &t->if_list) {
3665 p = list_entry(q, struct pktgen_dev, list);
3666 if (p == pkt_dev)
3667 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
3669}
3670
Luiz Capitulino222f1802006-03-20 22:16:13 -08003671static int pktgen_remove_device(struct pktgen_thread *t,
3672 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
3674
Joe Perchesf9467ea2010-06-21 12:29:14 +00003675 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Luiz Capitulino222f1802006-03-20 22:16:13 -08003677 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003678 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003679 pktgen_stop_device(pkt_dev);
3680 }
3681
3682 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 if (pkt_dev->odev) {
3685 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003686 pkt_dev->odev = NULL;
3687 }
3688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 /* And update the thread if_list */
3690
3691 _rem_dev_from_if_list(t, pkt_dev);
3692
Stephen Hemminger39df2322007-03-04 16:11:51 -08003693 if (pkt_dev->entry)
3694 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003696#ifdef CONFIG_XFRM
3697 free_SAs(pkt_dev);
3698#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003699 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003700 if (pkt_dev->page)
3701 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704}
3705
Luiz Capitulino222f1802006-03-20 22:16:13 -08003706static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707{
3708 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003709 struct proc_dir_entry *pe;
3710
Joe Perchesf9467ea2010-06-21 12:29:14 +00003711 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003713 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003714 if (!pg_proc_dir)
3715 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Wang Chen25296d52008-02-28 14:11:49 -08003717 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003718 if (pe == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003719 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003720 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003721 return -EINVAL;
3722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 /* Register us to receive netdevice events */
3725 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003726
John Hawkes670c02c2005-10-13 09:30:31 -07003727 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003728 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
David S. Milleree74baa2007-01-01 20:51:53 -08003730 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003731 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00003732 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3733 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003734 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003735
3736 if (list_empty(&pktgen_threads)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003737 pr_err("ERROR: Initialization failed for all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003738 unregister_netdevice_notifier(&pktgen_notifier_block);
3739 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003740 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003741 return -ENODEV;
3742 }
3743
Luiz Capitulino222f1802006-03-20 22:16:13 -08003744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
3747static void __exit pg_cleanup(void)
3748{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003749 struct pktgen_thread *t;
3750 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Luiz Capitulino222f1802006-03-20 22:16:13 -08003752 /* Stop all interfaces & threads */
Eric Dumazet551eaff2010-11-21 10:26:44 -08003753 pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003755 list_for_each_safe(q, n, &pktgen_threads) {
3756 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003757 kthread_stop(t->tsk);
3758 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Luiz Capitulino222f1802006-03-20 22:16:13 -08003761 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 unregister_netdevice_notifier(&pktgen_notifier_block);
3763
Luiz Capitulino222f1802006-03-20 22:16:13 -08003764 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003765 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003766 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769module_init(pg_init);
3770module_exit(pg_cleanup);
3771
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003772MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773MODULE_DESCRIPTION("Packet Generator tool");
3774MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003775MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003777MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003779MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003781MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003783MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");