blob: d41d88b53e184fb3e7faab43ab7cc784fa58e01b [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 Capitulinoc26a8012006-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")) {
1423 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001424 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001426 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001429 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001430 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001433 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return -EFAULT;
1435 i += len;
1436
Luiz Capitulino222f1802006-03-20 22:16:13 -08001437 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Andy Shevchenko451e07a2010-07-23 03:18:10 +00001438 int value;
1439
1440 value = hex_to_bin(*v);
1441 if (value >= 0)
1442 *m = *m * 16 + value;
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (*v == ':') {
1445 m++;
1446 *m = 0;
1447 }
1448 }
1449
1450 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001451 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1452 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 sprintf(pg_result, "OK: dstmac");
1455 return count;
1456 }
1457 if (!strcmp(name, "src_mac")) {
1458 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001459 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 unsigned char *m = pkt_dev->src_mac;
1461
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001462 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
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 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001469 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -EFAULT;
1471 i += len;
1472
Luiz Capitulino222f1802006-03-20 22:16:13 -08001473 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Andy Shevchenko451e07a2010-07-23 03:18:10 +00001474 int value;
1475
1476 value = hex_to_bin(*v);
1477 if (value >= 0)
1478 *m = *m * 16 + value;
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (*v == ':') {
1481 m++;
1482 *m = 0;
1483 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001486 /* Set up Src MAC */
1487 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1488 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1489
Luiz Capitulino222f1802006-03-20 22:16:13 -08001490 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return count;
1492 }
1493
Luiz Capitulino222f1802006-03-20 22:16:13 -08001494 if (!strcmp(name, "clear_counters")) {
1495 pktgen_clear_counters(pkt_dev);
1496 sprintf(pg_result, "OK: Clearing counters.\n");
1497 return count;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (!strcmp(name, "flows")) {
1501 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001502 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001503 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 i += len;
1506 if (value > MAX_CFLOWS)
1507 value = MAX_CFLOWS;
1508
1509 pkt_dev->cflows = value;
1510 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1511 return count;
1512 }
1513
1514 if (!strcmp(name, "flowlen")) {
1515 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001516 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001517 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 i += len;
1520 pkt_dev->lflow = value;
1521 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1522 return count;
1523 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001524
Robert Olsson45b270f2007-08-28 15:45:55 -07001525 if (!strcmp(name, "queue_map_min")) {
1526 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001527 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001528 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001529
Robert Olsson45b270f2007-08-28 15:45:55 -07001530 i += len;
1531 pkt_dev->queue_map_min = value;
1532 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1533 return count;
1534 }
1535
1536 if (!strcmp(name, "queue_map_max")) {
1537 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001538 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001539 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001540
Robert Olsson45b270f2007-08-28 15:45:55 -07001541 i += len;
1542 pkt_dev->queue_map_max = value;
1543 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1544 return count;
1545 }
1546
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001547 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001548 unsigned n, cnt;
1549
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001550 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001551 if (len < 0)
1552 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001553 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001554 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001555 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001556 cnt += sprintf(pg_result + cnt,
1557 "%08x%s", ntohl(pkt_dev->labels[n]),
1558 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001559
1560 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1561 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1562 pkt_dev->svlan_id = 0xffff;
1563
1564 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001565 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001566 }
1567 return count;
1568 }
1569
1570 if (!strcmp(name, "vlan_id")) {
1571 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001572 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001573 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001574
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001575 i += len;
1576 if (value <= 4095) {
1577 pkt_dev->vlan_id = value; /* turn on VLAN */
1578
1579 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001580 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001581
1582 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001583 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001584
1585 pkt_dev->nr_labels = 0; /* turn off MPLS */
1586 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1587 } else {
1588 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1589 pkt_dev->svlan_id = 0xffff;
1590
1591 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001592 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001593 }
1594 return count;
1595 }
1596
1597 if (!strcmp(name, "vlan_p")) {
1598 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001599 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001600 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001601
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001602 i += len;
1603 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1604 pkt_dev->vlan_p = value;
1605 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1606 } else {
1607 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1608 }
1609 return count;
1610 }
1611
1612 if (!strcmp(name, "vlan_cfi")) {
1613 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001614 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001615 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001616
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001617 i += len;
1618 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1619 pkt_dev->vlan_cfi = value;
1620 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1621 } else {
1622 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1623 }
1624 return count;
1625 }
1626
1627 if (!strcmp(name, "svlan_id")) {
1628 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001629 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001630 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001631
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001632 i += len;
1633 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1634 pkt_dev->svlan_id = value; /* turn on SVLAN */
1635
1636 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001637 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001638
1639 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001640 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001641
1642 pkt_dev->nr_labels = 0; /* turn off MPLS */
1643 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1644 } else {
1645 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1646 pkt_dev->svlan_id = 0xffff;
1647
1648 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001649 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001650 }
1651 return count;
1652 }
1653
1654 if (!strcmp(name, "svlan_p")) {
1655 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001656 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001657 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001658
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001659 i += len;
1660 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1661 pkt_dev->svlan_p = value;
1662 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1663 } else {
1664 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1665 }
1666 return count;
1667 }
1668
1669 if (!strcmp(name, "svlan_cfi")) {
1670 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001671 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001672 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001673
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001674 i += len;
1675 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1676 pkt_dev->svlan_cfi = value;
1677 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1678 } else {
1679 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1680 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001681 return count;
1682 }
1683
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001684 if (!strcmp(name, "tos")) {
1685 __u32 tmp_value = 0;
1686 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001687 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001688 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001689
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001690 i += len;
1691 if (len == 2) {
1692 pkt_dev->tos = tmp_value;
1693 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1694 } else {
1695 sprintf(pg_result, "ERROR: tos must be 00-ff");
1696 }
1697 return count;
1698 }
1699
1700 if (!strcmp(name, "traffic_class")) {
1701 __u32 tmp_value = 0;
1702 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001703 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001704 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001705
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001706 i += len;
1707 if (len == 2) {
1708 pkt_dev->traffic_class = tmp_value;
1709 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1710 } else {
1711 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1712 }
1713 return count;
1714 }
1715
John Fastabend9e50e3a2010-11-16 19:12:28 +00001716 if (!strcmp(name, "skb_priority")) {
1717 len = num_arg(&user_buffer[i], 9, &value);
1718 if (len < 0)
1719 return len;
1720
1721 i += len;
1722 pkt_dev->skb_priority = value;
1723 sprintf(pg_result, "OK: skb_priority=%i",
1724 pkt_dev->skb_priority);
1725 return count;
1726 }
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1729 return -EINVAL;
1730}
1731
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001732static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001734 return single_open(file, pktgen_if_show, PDE(inode)->data);
1735}
1736
Arjan van de Ven9a321442007-02-12 00:55:35 -08001737static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001738 .owner = THIS_MODULE,
1739 .open = pktgen_if_open,
1740 .read = seq_read,
1741 .llseek = seq_lseek,
1742 .write = pktgen_if_write,
1743 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001744};
1745
1746static int pktgen_thread_show(struct seq_file *seq, void *v)
1747{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001748 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001749 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001751 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Luiz Capitulino222f1802006-03-20 22:16:13 -08001753 seq_printf(seq, "Running: ");
1754
1755 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001756 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001758 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Luiz Capitulino222f1802006-03-20 22:16:13 -08001760 seq_printf(seq, "\nStopped: ");
1761
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001762 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001763 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001764 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001767 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001769 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Luiz Capitulino222f1802006-03-20 22:16:13 -08001771 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001776static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001777 const char __user * user_buffer,
1778 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Joe Perches8a994a72010-07-12 10:50:23 +00001780 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001782 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001784 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001787 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return -EINVAL;
1789 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001790
Paul Gortmakerd6182222010-10-18 12:14:44 +00001791 max = count;
1792 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001793 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001794 return len;
1795
Paul Gortmakerd6182222010-10-18 12:14:44 +00001796 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /* Read variable name */
1799
1800 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001801 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001802 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 memset(name, 0, sizeof(name));
1805 if (copy_from_user(name, &user_buffer[i], len))
1806 return -EFAULT;
1807 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001808
Luiz Capitulino222f1802006-03-20 22:16:13 -08001809 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001811 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001812 return len;
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 i += len;
1815
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001816 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001817 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1818 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Luiz Capitulino222f1802006-03-20 22:16:13 -08001820 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001821 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 ret = -EINVAL;
1823 goto out;
1824 }
1825
1826 pg_result = &(t->result[0]);
1827
Luiz Capitulino222f1802006-03-20 22:16:13 -08001828 if (!strcmp(name, "add_device")) {
1829 char f[32];
1830 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001832 if (len < 0) {
1833 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 goto out;
1835 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001836 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return -EFAULT;
1838 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001839 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001840 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001841 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001842 ret = count;
1843 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto out;
1845 }
1846
Luiz Capitulino222f1802006-03-20 22:16:13 -08001847 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001848 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001849 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001850 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001851 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001853 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto out;
1855 }
1856
Luiz Capitulino222f1802006-03-20 22:16:13 -08001857 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001858 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001859 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 goto out;
1861 }
1862
1863 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001864out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return ret;
1866}
1867
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001868static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001870 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Arjan van de Ven9a321442007-02-12 00:55:35 -08001873static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001874 .owner = THIS_MODULE,
1875 .open = pktgen_thread_open,
1876 .read = seq_read,
1877 .llseek = seq_lseek,
1878 .write = pktgen_thread_write,
1879 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001880};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001883static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 struct pktgen_thread *t;
1886 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001887 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001889 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001890 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001892 if (remove) {
1893 if_lock(t);
1894 pkt_dev->removal_mark = 1;
1895 t->control |= T_REMDEV;
1896 if_unlock(t);
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 break;
1899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001901 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001904/*
1905 * mark a device for removal
1906 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001907static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001910 const int max_tries = 10, msec_per_try = 125;
1911 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001912
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001913 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001914 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001915
Luiz Capitulino222f1802006-03-20 22:16:13 -08001916 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001917
1918 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001919 if (pkt_dev == NULL)
1920 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001921
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001922 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001923 pr_debug("%s: waiting for %s to disappear....\n",
1924 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001925 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001926 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001927
1928 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001929 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1930 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001931 break;
1932 }
1933
1934 }
1935
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001936 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001937}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001938
Stephen Hemminger39df2322007-03-04 16:11:51 -08001939static void pktgen_change_name(struct net_device *dev)
1940{
1941 struct pktgen_thread *t;
1942
1943 list_for_each_entry(t, &pktgen_threads, th_list) {
1944 struct pktgen_dev *pkt_dev;
1945
1946 list_for_each_entry(pkt_dev, &t->if_list, list) {
1947 if (pkt_dev->odev != dev)
1948 continue;
1949
1950 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1951
Alexey Dobriyan29753152009-08-28 23:34:43 -07001952 pkt_dev->entry = proc_create_data(dev->name, 0600,
1953 pg_proc_dir,
1954 &pktgen_if_fops,
1955 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001956 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001957 pr_err("can't move proc entry for '%s'\n",
1958 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001959 break;
1960 }
1961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
Luiz Capitulino222f1802006-03-20 22:16:13 -08001964static int pktgen_device_event(struct notifier_block *unused,
1965 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001967 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001969 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001970 return NOTIFY_DONE;
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /* It is OK that we do not hold the group lock right now,
1973 * as we run under the RTNL lock.
1974 */
1975
1976 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001977 case NETDEV_CHANGENAME:
1978 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001982 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 return NOTIFY_DONE;
1987}
1988
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001989static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1990 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001991{
1992 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001993 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001994
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001995 for (i = 0; ifname[i] != '@'; i++) {
1996 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001997 break;
1998
1999 b[i] = ifname[i];
2000 }
2001 b[i] = 0;
2002
2003 return dev_get_by_name(&init_net, b);
2004}
2005
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007/* Associate pktgen_dev with a device. */
2008
Stephen Hemminger39df2322007-03-04 16:11:51 -08002009static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002010{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002012 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (pkt_dev->odev) {
2016 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002017 pkt_dev->odev = NULL;
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Robert Olssone6fce5b2008-08-07 02:23:01 -07002020 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002022 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002023 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002027 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002028 err = -EINVAL;
2029 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002030 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002031 err = -ENETDOWN;
2032 } else {
2033 pkt_dev->odev = odev;
2034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002038 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
2041/* Read pkt_dev from the interface and set up internal pktgen_dev
2042 * structure to have the right information to create/send packets
2043 */
2044static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2045{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002046 int ntxq;
2047
Luiz Capitulino222f1802006-03-20 22:16:13 -08002048 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002049 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002050 sprintf(pkt_dev->result,
2051 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2052 return;
2053 }
2054
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002055 /* make sure that we don't pick a non-existing transmit queue */
2056 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002057
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002058 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002059 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2060 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2061 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002062 pkt_dev->queue_map_min = ntxq - 1;
2063 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002064 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002065 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2066 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2067 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002068 pkt_dev->queue_map_max = ntxq - 1;
2069 }
2070
Luiz Capitulino222f1802006-03-20 22:16:13 -08002071 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002073 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002074 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Luiz Capitulino222f1802006-03-20 22:16:13 -08002076 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002077 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Luiz Capitulino222f1802006-03-20 22:16:13 -08002079 /* Set up pkt size */
2080 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2081
2082 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002084 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 * gets exported
2086 */
2087
2088#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002089 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct inet6_dev *idev;
2091
Luiz Capitulino222f1802006-03-20 22:16:13 -08002092 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2093 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 set = 1;
2095 break;
2096 }
2097
Luiz Capitulino222f1802006-03-20 22:16:13 -08002098 if (!set) {
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /*
2101 * Use linklevel address if unconfigured.
2102 *
2103 * use ipv6_get_lladdr if/when it's get exported
2104 */
2105
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002106 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002107 idev = __in6_dev_get(pkt_dev->odev);
2108 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct inet6_ifaddr *ifp;
2110
2111 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002112 for (ifp = idev->addr_list; ifp;
2113 ifp = ifp->if_next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002114 if (ifp->scope == IFA_LINK &&
2115 !(ifp->flags & IFA_F_TENTATIVE)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002116 ipv6_addr_copy(&pkt_dev->
2117 cur_in6_saddr,
2118 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 err = 0;
2120 break;
2121 }
2122 }
2123 read_unlock_bh(&idev->lock);
2124 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002125 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002126 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002127 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002130 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 pkt_dev->saddr_min = 0;
2132 pkt_dev->saddr_max = 0;
2133 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002134
2135 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002138 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (in_dev) {
2140 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002141 pkt_dev->saddr_min =
2142 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 pkt_dev->saddr_max = pkt_dev->saddr_min;
2144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
2146 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002147 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2149 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2150 }
2151
2152 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2153 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2154 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002155 /* Initialize current values. */
2156 pkt_dev->cur_dst_mac_offset = 0;
2157 pkt_dev->cur_src_mac_offset = 0;
2158 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2159 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2160 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2161 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 pkt_dev->nflows = 0;
2163}
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002166static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2167{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002168 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002169 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002170 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002171
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002172 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2173 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002174
Daniel Turull43d28b62010-06-09 22:49:57 +00002175 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002176 if (remaining <= 0) {
2177 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002178 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002179 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002180
Stephen Hemmingeref879792009-09-22 19:41:43 +00002181 start_time = ktime_now();
Daniel Turull43d28b62010-06-09 22:49:57 +00002182 if (remaining < 100000)
2183 ndelay(remaining); /* really small just spin */
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002184 else {
2185 /* see do_nanosleep */
2186 hrtimer_init_sleeper(&t, current);
2187 do {
2188 set_current_state(TASK_INTERRUPTIBLE);
2189 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2190 if (!hrtimer_active(&t.timer))
2191 t.task = NULL;
2192
2193 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002196 hrtimer_cancel(&t.timer);
2197 } while (t.task && pkt_dev->running && !signal_pending(current));
2198 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002200 end_time = ktime_now();
2201
2202 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002203 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002206static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2207{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002208 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002209 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2210 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2211 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2212}
2213
Stephen Hemminger648fda72009-08-27 13:55:07 +00002214static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002215{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002216 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002217}
2218
2219static inline int f_pick(struct pktgen_dev *pkt_dev)
2220{
2221 int flow = pkt_dev->curfl;
2222
2223 if (pkt_dev->flags & F_FLOW_SEQ) {
2224 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2225 /* reset time */
2226 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002227 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002228 pkt_dev->curfl += 1;
2229 if (pkt_dev->curfl >= pkt_dev->cflows)
2230 pkt_dev->curfl = 0; /*reset */
2231 }
2232 } else {
2233 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002234 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002235
Robert Olsson1211a642008-08-05 18:44:26 -07002236 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002237 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002238 pkt_dev->flows[flow].flags = 0;
2239 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002240 }
2241
2242 return pkt_dev->curfl;
2243}
2244
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002245
2246#ifdef CONFIG_XFRM
2247/* If there was already an IPSEC SA, we keep it as is, else
2248 * we go look for it ...
2249*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002250#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002251static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002252{
2253 struct xfrm_state *x = pkt_dev->flows[flow].x;
2254 if (!x) {
2255 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002256 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002257 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002258 (xfrm_address_t *)&pkt_dev->cur_saddr,
2259 AF_INET,
2260 pkt_dev->ipsmode,
2261 pkt_dev->ipsproto, 0);
2262 if (x) {
2263 pkt_dev->flows[flow].x = x;
2264 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002265 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002266 }
2267
2268 }
2269}
2270#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002271static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2272{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002273
2274 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2275 pkt_dev->cur_queue_map = smp_processor_id();
2276
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002277 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002278 __u16 t;
2279 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2280 t = random32() %
2281 (pkt_dev->queue_map_max -
2282 pkt_dev->queue_map_min + 1)
2283 + pkt_dev->queue_map_min;
2284 } else {
2285 t = pkt_dev->cur_queue_map + 1;
2286 if (t > pkt_dev->queue_map_max)
2287 t = pkt_dev->queue_map_min;
2288 }
2289 pkt_dev->cur_queue_map = t;
2290 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002291 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 -07002292}
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294/* Increment/randomize headers according to flags and current values
2295 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2296 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002297static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2298{
2299 __u32 imn;
2300 __u32 imx;
2301 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002303 if (pkt_dev->cflows)
2304 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307 if (pkt_dev->src_mac_count > 1) {
2308 __u32 mc;
2309 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Luiz Capitulino222f1802006-03-20 22:16:13 -08002311 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002312 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002313 else {
2314 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002315 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 pkt_dev->src_mac_count)
2317 pkt_dev->cur_src_mac_offset = 0;
2318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Luiz Capitulino222f1802006-03-20 22:16:13 -08002320 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2321 pkt_dev->hh[11] = tmp;
2322 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2323 pkt_dev->hh[10] = tmp;
2324 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2325 pkt_dev->hh[9] = tmp;
2326 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2327 pkt_dev->hh[8] = tmp;
2328 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2329 pkt_dev->hh[7] = tmp;
2330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Luiz Capitulino222f1802006-03-20 22:16:13 -08002332 /* Deal with Destination MAC */
2333 if (pkt_dev->dst_mac_count > 1) {
2334 __u32 mc;
2335 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Luiz Capitulino222f1802006-03-20 22:16:13 -08002337 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002338 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Luiz Capitulino222f1802006-03-20 22:16:13 -08002340 else {
2341 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002342 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002343 pkt_dev->dst_mac_count) {
2344 pkt_dev->cur_dst_mac_offset = 0;
2345 }
2346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Luiz Capitulino222f1802006-03-20 22:16:13 -08002348 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2349 pkt_dev->hh[5] = tmp;
2350 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2351 pkt_dev->hh[4] = tmp;
2352 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2353 pkt_dev->hh[3] = tmp;
2354 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2355 pkt_dev->hh[2] = tmp;
2356 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2357 pkt_dev->hh[1] = tmp;
2358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002360 if (pkt_dev->flags & F_MPLS_RND) {
2361 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002362 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002363 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2364 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002365 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002366 htonl(0x000fffff));
2367 }
2368
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002369 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002370 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002371 }
2372
2373 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002374 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002375 }
2376
Luiz Capitulino222f1802006-03-20 22:16:13 -08002377 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2378 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002379 pkt_dev->cur_udp_src = random32() %
2380 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2381 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Luiz Capitulino222f1802006-03-20 22:16:13 -08002383 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 pkt_dev->cur_udp_src++;
2385 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2386 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 }
2388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Luiz Capitulino222f1802006-03-20 22:16:13 -08002390 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2391 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002392 pkt_dev->cur_udp_dst = random32() %
2393 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2394 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002395 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002397 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002399 }
2400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 if (!(pkt_dev->flags & F_IPV6)) {
2403
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002404 imn = ntohl(pkt_dev->saddr_min);
2405 imx = ntohl(pkt_dev->saddr_max);
2406 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002408 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002409 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 else {
2411 t = ntohl(pkt_dev->cur_saddr);
2412 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002413 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
2417 pkt_dev->cur_saddr = htonl(t);
2418 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002419
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002420 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2422 } else {
Al Viro252e33462006-11-14 20:48:11 -08002423 imn = ntohl(pkt_dev->daddr_min);
2424 imx = ntohl(pkt_dev->daddr_max);
2425 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002427 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (pkt_dev->flags & F_IPDST_RND) {
2429
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002430 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002431 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Joe Perches21cf2252007-12-16 13:44:00 -08002433 while (ipv4_is_loopback(s) ||
2434 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002435 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002436 ipv4_is_zeronet(s) ||
2437 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002438 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002439 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
Al Viro252e33462006-11-14 20:48:11 -08002441 pkt_dev->cur_daddr = s;
2442 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 t = ntohl(pkt_dev->cur_daddr);
2444 t++;
2445 if (t > imx) {
2446 t = imn;
2447 }
2448 pkt_dev->cur_daddr = htonl(t);
2449 }
2450 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002451 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002452 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002453 pkt_dev->flows[flow].cur_daddr =
2454 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002455#ifdef CONFIG_XFRM
2456 if (pkt_dev->flags & F_IPSEC_ON)
2457 get_ipsec_sa(pkt_dev, flow);
2458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 pkt_dev->nflows++;
2460 }
2461 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002462 } else { /* IPV6 * */
2463
2464 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2465 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2466 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2467 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 else {
2469 int i;
2470
2471 /* Only random destinations yet */
2472
Luiz Capitulino222f1802006-03-20 22:16:13 -08002473 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002475 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002476 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2477 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481
Luiz Capitulino222f1802006-03-20 22:16:13 -08002482 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2483 __u32 t;
2484 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002485 t = random32() %
2486 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2487 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002488 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002490 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002492 }
2493 pkt_dev->cur_pkt_size = t;
2494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002496 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 pkt_dev->flows[flow].count++;
2499}
2500
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002501
2502#ifdef CONFIG_XFRM
2503static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2504{
2505 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2506 int err = 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002507
2508 if (!x)
2509 return 0;
2510 /* XXX: we dont support tunnel mode for now until
2511 * we resolve the dst issue */
2512 if (x->props.mode != XFRM_MODE_TRANSPORT)
2513 return 0;
2514
2515 spin_lock(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002516
Herbert Xu13996372007-10-17 21:35:51 -07002517 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002518 if (err)
2519 goto error;
2520 err = x->type->output(x, skb);
2521 if (err)
2522 goto error;
2523
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002524 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002525 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002526error:
2527 spin_unlock(&x->lock);
2528 return err;
2529}
2530
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002531static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002532{
2533 if (pkt_dev->cflows) {
2534 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002535 int i;
2536 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002537 struct xfrm_state *x = pkt_dev->flows[i].x;
2538 if (x) {
2539 xfrm_state_put(x);
2540 pkt_dev->flows[i].x = NULL;
2541 }
2542 }
2543 }
2544}
2545
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002546static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002547 struct sk_buff *skb, __be16 protocol)
2548{
2549 if (pkt_dev->flags & F_IPSEC_ON) {
2550 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2551 int nhead = 0;
2552 if (x) {
2553 int ret;
2554 __u8 *eth;
2555 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002556 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002557 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2558 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002559 pr_err("Error expanding ipsec packet %d\n",
2560 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002561 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002562 }
2563 }
2564
2565 /* ipsec is not expecting ll header */
2566 skb_pull(skb, ETH_HLEN);
2567 ret = pktgen_output_ipsec(skb, pkt_dev);
2568 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002569 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002570 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002571 }
2572 /* restore ll */
2573 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2574 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002575 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002576 }
2577 }
2578 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002579err:
2580 kfree_skb(skb);
2581 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002582}
2583#endif
2584
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002585static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2586{
2587 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002588 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002589 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002590
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002591 mpls--;
2592 *mpls |= MPLS_STACK_BOTTOM;
2593}
2594
Al Viro0f37c602006-11-03 03:49:56 -08002595static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2596 unsigned int prio)
2597{
2598 return htons(id | (cfi << 12) | (prio << 13));
2599}
2600
Eric Dumazet26ad7872011-01-25 13:26:05 -08002601static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2602 int datalen)
2603{
2604 struct timeval timestamp;
2605 struct pktgen_hdr *pgh;
2606
2607 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2608 datalen -= sizeof(*pgh);
2609
2610 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002611 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002612 } else {
2613 int frags = pkt_dev->nfrags;
2614 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002615 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002616
2617
2618 if (frags > MAX_SKB_FRAGS)
2619 frags = MAX_SKB_FRAGS;
2620 len = datalen - frags * PAGE_SIZE;
2621 if (len > 0) {
2622 memset(skb_put(skb, len), 0, len);
2623 datalen = frags * PAGE_SIZE;
2624 }
2625
2626 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002627 frag_len = (datalen/frags) < PAGE_SIZE ?
2628 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002629 while (datalen > 0) {
2630 if (unlikely(!pkt_dev->page)) {
2631 int node = numa_node_id();
2632
2633 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2634 node = pkt_dev->node;
2635 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2636 if (!pkt_dev->page)
2637 break;
2638 }
2639 skb_shinfo(skb)->frags[i].page = pkt_dev->page;
2640 get_page(pkt_dev->page);
2641 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002642 /*last fragment, fill rest of data*/
2643 if (i == (frags - 1))
2644 skb_shinfo(skb)->frags[i].size =
2645 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2646 else
2647 skb_shinfo(skb)->frags[i].size = frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002648 datalen -= skb_shinfo(skb)->frags[i].size;
2649 skb->len += skb_shinfo(skb)->frags[i].size;
2650 skb->data_len += skb_shinfo(skb)->frags[i].size;
2651 i++;
2652 skb_shinfo(skb)->nr_frags = i;
2653 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002654 }
2655
2656 /* Stamp the time, and sequence number,
2657 * convert them to network byte order
2658 */
2659 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2660 pgh->seq_num = htonl(pkt_dev->seq_num);
2661
2662 do_gettimeofday(&timestamp);
2663 pgh->tv_sec = htonl(timestamp.tv_sec);
2664 pgh->tv_usec = htonl(timestamp.tv_usec);
2665}
2666
Luiz Capitulino222f1802006-03-20 22:16:13 -08002667static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2668 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 struct sk_buff *skb = NULL;
2671 __u8 *eth;
2672 struct udphdr *udph;
2673 int datalen, iplen;
2674 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002675 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002676 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002677 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2678 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2679 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2680 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002681 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002682
2683 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002684 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002685
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002686 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002687 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002688
Robert Olsson64053be2005-06-26 15:27:10 -07002689 /* Update any of the values, used when we're incrementing various
2690 * fields.
2691 */
2692 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002693 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002694
David S. Miller7ac54592006-01-18 14:19:10 -08002695 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002696
2697 if (pkt_dev->flags & F_NODE) {
2698 int node;
2699
2700 if (pkt_dev->node >= 0)
2701 node = pkt_dev->node;
2702 else
2703 node = numa_node_id();
2704
2705 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2706 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2707 if (likely(skb)) {
2708 skb_reserve(skb, NET_SKB_PAD);
2709 skb->dev = odev;
2710 }
2711 }
2712 else
2713 skb = __netdev_alloc_skb(odev,
2714 pkt_dev->cur_pkt_size + 64
2715 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (!skb) {
2718 sprintf(pkt_dev->result, "No memory");
2719 return NULL;
2720 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002721 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
David S. Miller7ac54592006-01-18 14:19:10 -08002723 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 /* Reserve for ethernet and IP header */
2726 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002727 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2728 if (pkt_dev->nr_labels)
2729 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002730
2731 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002732 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002733 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002734 *svlan_tci = build_tci(pkt_dev->svlan_id,
2735 pkt_dev->svlan_cfi,
2736 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002737 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002738 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002739 }
2740 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002741 *vlan_tci = build_tci(pkt_dev->vlan_id,
2742 pkt_dev->vlan_cfi,
2743 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002744 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002745 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002746 }
2747
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002748 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002749 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002750 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002751 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002752 skb->priority = pkt_dev->skb_priority;
2753
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002754 iph = ip_hdr(skb);
2755 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002758 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002760 /* Eth + IPh + UDPh + mpls */
2761 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002762 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002763 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 udph->source = htons(pkt_dev->cur_udp_src);
2767 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002768 udph->len = htons(datalen + 8); /* DATA + udphdr */
2769 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 iph->ihl = 5;
2772 iph->version = 4;
2773 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002774 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002775 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 iph->saddr = pkt_dev->cur_saddr;
2777 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002778 iph->id = htons(pkt_dev->ip_id);
2779 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 iph->frag_off = 0;
2781 iplen = 20 + 8 + datalen;
2782 iph->tot_len = htons(iplen);
2783 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002784 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002785 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002786 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002787 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 skb->dev = odev;
2789 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002790 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002791
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002792#ifdef CONFIG_XFRM
2793 if (!process_ipsec(pkt_dev, skb, protocol))
2794 return NULL;
2795#endif
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return skb;
2798}
2799
2800/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002801 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2803 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002804 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 * Should be candidate for net/ipv4/utils.c
2806 * --ro
2807 */
2808
Luiz Capitulino222f1802006-03-20 22:16:13 -08002809static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
2811 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002812 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 unsigned long u;
2814 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002815 unsigned int prefixlen = 0;
2816 unsigned int suffixlen = 0;
Al Viro252e33462006-11-14 20:48:11 -08002817 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002818 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Luiz Capitulino222f1802006-03-20 22:16:13 -08002820 for (i = 0; i < 16; i++)
2821 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 for (;;) {
2824 if (*s == ':') {
2825 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002826 if (s[1] == ':') { /* Found "::", skip to part 2 */
2827 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 len++;
2829 break;
2830 }
2831 s++;
2832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002834 u = simple_strtoul(s, &pos, 16);
2835 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002836 if (!i)
2837 return 0;
2838 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 /* the last 4 bytes may be written as IPv4 address */
2841
2842 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002843 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2844 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846 ip[prefixlen++] = (u >> 8);
2847 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002848 s += i;
2849 len += i;
2850 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return len;
2852 }
2853
2854/* part 2, after "::" */
2855 for (;;) {
2856 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002857 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 break;
2859 s++;
2860 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002861 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002863
2864 u = simple_strtol(s, &pos, 16);
2865 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002867 if (*s)
2868 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 break;
2870 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002871 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2874 sizeof(tmp));
2875 suffixlen += 4;
2876 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 break;
2878 }
2879 suffix[suffixlen++] = (u >> 8);
2880 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002881 s += i;
2882 len += i;
2883 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 break;
2885 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002886 for (i = 0; i < suffixlen; i++)
2887 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return len;
2889}
2890
Luiz Capitulino222f1802006-03-20 22:16:13 -08002891static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2892 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
2894 struct sk_buff *skb = NULL;
2895 __u8 *eth;
2896 struct udphdr *udph;
2897 int datalen;
2898 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002899 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002900 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002901 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2902 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2903 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2904 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002905 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002906
2907 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002908 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002909
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002910 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002911 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002912
Robert Olsson64053be2005-06-26 15:27:10 -07002913 /* Update any of the values, used when we're incrementing various
2914 * fields.
2915 */
2916 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002917 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002918
Stephen Hemmingere4707572009-08-27 13:55:13 +00002919 skb = __netdev_alloc_skb(odev,
2920 pkt_dev->cur_pkt_size + 64
2921 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 if (!skb) {
2923 sprintf(pkt_dev->result, "No memory");
2924 return NULL;
2925 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002926 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 skb_reserve(skb, 16);
2929
2930 /* Reserve for ethernet and IP header */
2931 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002932 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2933 if (pkt_dev->nr_labels)
2934 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002935
2936 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002937 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002938 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002939 *svlan_tci = build_tci(pkt_dev->svlan_id,
2940 pkt_dev->svlan_cfi,
2941 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002942 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002943 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002944 }
2945 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002946 *vlan_tci = build_tci(pkt_dev->vlan_id,
2947 pkt_dev->vlan_cfi,
2948 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002949 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002950 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002951 }
2952
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002953 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002954 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002955 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002956 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002957 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002958 iph = ipv6_hdr(skb);
2959 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002962 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002963
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002964 /* Eth + IPh + UDPh + mpls */
2965 datalen = pkt_dev->cur_pkt_size - 14 -
2966 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002967 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Luiz Capitulino222f1802006-03-20 22:16:13 -08002969 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 datalen = sizeof(struct pktgen_hdr);
2971 if (net_ratelimit())
Joe Perchesf9467ea2010-06-21 12:29:14 +00002972 pr_info("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
2974
2975 udph->source = htons(pkt_dev->cur_udp_src);
2976 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002977 udph->len = htons(datalen + sizeof(struct udphdr));
2978 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002980 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002982 if (pkt_dev->traffic_class) {
2983 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002984 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002985 }
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 iph->hop_limit = 32;
2988
2989 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2990 iph->nexthdr = IPPROTO_UDP;
2991
2992 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2993 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2994
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002995 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002996 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002997 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 skb->dev = odev;
2999 skb->pkt_type = PACKET_HOST;
3000
Eric Dumazet26ad7872011-01-25 13:26:05 -08003001 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return skb;
3004}
3005
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003006static struct sk_buff *fill_packet(struct net_device *odev,
3007 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003009 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return fill_packet_ipv6(odev, pkt_dev);
3011 else
3012 return fill_packet_ipv4(odev, pkt_dev);
3013}
3014
Luiz Capitulino222f1802006-03-20 22:16:13 -08003015static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003017 pkt_dev->seq_num = 1;
3018 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003020 pkt_dev->tx_bytes = 0;
3021 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
3024/* Set up structure for sending pkts, clear counters */
3025
3026static void pktgen_run(struct pktgen_thread *t)
3027{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003028 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 int started = 0;
3030
Joe Perchesf9467ea2010-06-21 12:29:14 +00003031 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003034 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 /*
3037 * setup odev and create initial packet.
3038 */
3039 pktgen_setup_inject(pkt_dev);
3040
Luiz Capitulino222f1802006-03-20 22:16:13 -08003041 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003043 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003045 pkt_dev->started_at =
3046 pkt_dev->next_tx = ktime_now();
3047
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003048 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 strcpy(pkt_dev->result, "Starting");
3051 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003052 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 strcpy(pkt_dev->result, "Error starting");
3054 }
3055 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003056 if (started)
3057 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
3060static void pktgen_stop_all_threads_ifs(void)
3061{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003062 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Joe Perchesf9467ea2010-06-21 12:29:14 +00003064 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003066 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003067
3068 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003069 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003070
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003071 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
Stephen Hemminger648fda72009-08-27 13:55:07 +00003074static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003076 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003078 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003079 if (pkt_dev->running)
3080 return 1;
3081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
Luiz Capitulino222f1802006-03-20 22:16:13 -08003084static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003086 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Luiz Capitulino222f1802006-03-20 22:16:13 -08003088 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Luiz Capitulino222f1802006-03-20 22:16:13 -08003090 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Luiz Capitulino222f1802006-03-20 22:16:13 -08003092 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Luiz Capitulino222f1802006-03-20 22:16:13 -08003094 if (signal_pending(current))
3095 goto signal;
3096 if_lock(t);
3097 }
3098 if_unlock(t);
3099 return 1;
3100signal:
3101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
3104static int pktgen_wait_all_threads_run(void)
3105{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003106 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003108
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003109 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003110
3111 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003113 if (sig == 0)
3114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003116
3117 if (sig == 0)
3118 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003120
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003121 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 return sig;
3123}
3124
3125static void pktgen_run_all_threads(void)
3126{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003127 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Joe Perchesf9467ea2010-06-21 12:29:14 +00003129 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003131 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003133 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003135
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003136 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003138 /* Propagate thread->control */
3139 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 pktgen_wait_all_threads_run();
3142}
3143
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003144static void pktgen_reset_all_threads(void)
3145{
3146 struct pktgen_thread *t;
3147
Joe Perchesf9467ea2010-06-21 12:29:14 +00003148 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003149
3150 mutex_lock(&pktgen_thread_lock);
3151
3152 list_for_each_entry(t, &pktgen_threads, th_list)
3153 t->control |= (T_REMDEVALL);
3154
3155 mutex_unlock(&pktgen_thread_lock);
3156
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003157 /* Propagate thread->control */
3158 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003159
3160 pktgen_wait_all_threads_run();
3161}
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3164{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003165 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003167 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3168 pkt_dev->started_at);
3169 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Daniel Turull03a14ab2011-03-09 14:11:00 -08003171 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003172 (unsigned long long)ktime_to_us(elapsed),
3173 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3174 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 (unsigned long long)pkt_dev->sofar,
3176 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003178 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3179 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Luiz Capitulino222f1802006-03-20 22:16:13 -08003181 bps = pps * 8 * pkt_dev->cur_pkt_size;
3182
3183 mbps = bps;
3184 do_div(mbps, 1000000);
3185 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3186 (unsigned long long)pps,
3187 (unsigned long long)mbps,
3188 (unsigned long long)bps,
3189 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003193static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003196
Luiz Capitulino222f1802006-03-20 22:16:13 -08003197 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003198 pr_warning("interface: %s is already stopped\n",
3199 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003200 return -EINVAL;
3201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003203 kfree_skb(pkt_dev->skb);
3204 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003205 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003206 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003208 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Luiz Capitulino222f1802006-03-20 22:16:13 -08003210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212
Luiz Capitulino222f1802006-03-20 22:16:13 -08003213static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003215 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if_lock(t);
3218
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003219 list_for_each_entry(pkt_dev, &t->if_list, list) {
3220 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003221 continue;
3222 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003223 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003224 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003225 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003228 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Luiz Capitulino222f1802006-03-20 22:16:13 -08003231static void pktgen_stop(struct pktgen_thread *t)
3232{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003233 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Joe Perchesf9467ea2010-06-21 12:29:14 +00003235 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003239 list_for_each_entry(pkt_dev, &t->if_list, list) {
3240 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003246/*
3247 * one of our devices needs to be removed - find it
3248 * and remove it
3249 */
3250static void pktgen_rem_one_if(struct pktgen_thread *t)
3251{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003252 struct list_head *q, *n;
3253 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003254
Joe Perchesf9467ea2010-06-21 12:29:14 +00003255 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003256
3257 if_lock(t);
3258
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003259 list_for_each_safe(q, n, &t->if_list) {
3260 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003261
Luiz Capitulino222f1802006-03-20 22:16:13 -08003262 if (!cur->removal_mark)
3263 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003264
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003265 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003266 cur->skb = NULL;
3267
3268 pktgen_remove_device(t, cur);
3269
3270 break;
3271 }
3272
3273 if_unlock(t);
3274}
3275
Luiz Capitulino222f1802006-03-20 22:16:13 -08003276static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003278 struct list_head *q, *n;
3279 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003280
Joe Perchesf9467ea2010-06-21 12:29:14 +00003281 func_enter();
3282
Luiz Capitulino222f1802006-03-20 22:16:13 -08003283 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003284
Luiz Capitulino222f1802006-03-20 22:16:13 -08003285 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003287 list_for_each_safe(q, n, &t->if_list) {
3288 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003289
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003290 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003291 cur->skb = NULL;
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 pktgen_remove_device(t, cur);
3294 }
3295
Luiz Capitulino222f1802006-03-20 22:16:13 -08003296 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
Luiz Capitulino222f1802006-03-20 22:16:13 -08003299static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003301 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
David S. Milleree74baa2007-01-01 20:51:53 -08003303 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305}
3306
Stephen Hemmingeref879792009-09-22 19:41:43 +00003307static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003308{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003309 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003310 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003311 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003312}
3313
Stephen Hemmingeref879792009-09-22 19:41:43 +00003314static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3315{
3316 ktime_t idle_start = ktime_now();
3317
3318 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3319 if (signal_pending(current))
3320 break;
3321
3322 if (need_resched())
3323 pktgen_resched(pkt_dev);
3324 else
3325 cpu_relax();
3326 }
3327 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3328}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003329
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003330static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Stephen Hemminger00829822008-11-20 20:14:53 -08003332 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003333 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003334 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003335 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003336 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 int ret;
3338
Stephen Hemmingeref879792009-09-22 19:41:43 +00003339 /* If device is offline, then don't send */
3340 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3341 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003342 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003344
Stephen Hemmingeref879792009-09-22 19:41:43 +00003345 /* This is max DELAY, this has special meaning of
3346 * "never transmit"
3347 */
3348 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3349 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3350 return;
3351 }
3352
3353 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003354 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3355 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3356 /* build a new pkt */
3357 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003358
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003359 pkt_dev->skb = fill_packet(odev, pkt_dev);
3360 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003361 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003362 schedule();
3363 pkt_dev->clone_count--; /* back out increment, OOM */
3364 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003366 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003367 pkt_dev->allocated_skbs++;
3368 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003370
Stephen Hemmingeref879792009-09-22 19:41:43 +00003371 if (pkt_dev->delay && pkt_dev->last_ok)
3372 spin(pkt_dev, pkt_dev->next_tx);
3373
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003374 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3375 txq = netdev_get_tx_queue(odev, queue_map);
3376
3377 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003378
Eric Dumazet5a0d2262010-11-23 10:42:02 +00003379 if (unlikely(netif_tx_queue_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003380 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003381 pkt_dev->last_ok = 0;
3382 goto unlock;
3383 }
3384 atomic_inc(&(pkt_dev->skb->users));
3385 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Stephen Hemmingeref879792009-09-22 19:41:43 +00003387 switch (ret) {
3388 case NETDEV_TX_OK:
3389 txq_trans_update(txq);
3390 pkt_dev->last_ok = 1;
3391 pkt_dev->sofar++;
3392 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003393 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003394 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003395 case NET_XMIT_DROP:
3396 case NET_XMIT_CN:
3397 case NET_XMIT_POLICED:
3398 /* skb has been consumed */
3399 pkt_dev->errors++;
3400 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003401 default: /* Drivers are not supposed to return other values! */
3402 if (net_ratelimit())
Joe Perches58231182010-12-20 10:30:06 -08003403 pr_info("%s xmit error: %d\n", pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003404 pkt_dev->errors++;
3405 /* fallthru */
3406 case NETDEV_TX_LOCKED:
3407 case NETDEV_TX_BUSY:
3408 /* Retry it next time */
3409 atomic_dec(&(pkt_dev->skb->users));
3410 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003411 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003412unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003413 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 /* If pkt_dev->count is zero, then run forever */
3416 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003417 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /* Done with this */
3420 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003421 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003424/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 * Main loop of the thread goes here
3426 */
3427
David S. Milleree74baa2007-01-01 20:51:53 -08003428static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003431 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003432 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
David S. Milleree74baa2007-01-01 20:51:53 -08003435 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003438 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Joe Perchesf9467ea2010-06-21 12:29:14 +00003440 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
David S. Milleree74baa2007-01-01 20:51:53 -08003442 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003443
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003444 set_freezable();
3445
David S. Milleree74baa2007-01-01 20:51:53 -08003446 while (!kthread_should_stop()) {
3447 pkt_dev = next_to_run(t);
3448
Stephen Hemmingeref879792009-09-22 19:41:43 +00003449 if (unlikely(!pkt_dev && t->control == 0)) {
Eric Dumazet551eaff2010-11-21 10:26:44 -08003450 if (pktgen_exiting)
3451 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003452 wait_event_interruptible_timeout(t->queue,
3453 t->control != 0,
3454 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003455 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003456 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003457 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 __set_current_state(TASK_RUNNING);
3460
Stephen Hemmingeref879792009-09-22 19:41:43 +00003461 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 pktgen_xmit(pkt_dev);
3463
Stephen Hemmingeref879792009-09-22 19:41:43 +00003464 if (need_resched())
3465 pktgen_resched(pkt_dev);
3466 else
3467 cpu_relax();
3468 }
3469
Luiz Capitulino222f1802006-03-20 22:16:13 -08003470 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 pktgen_stop(t);
3472 t->control &= ~(T_STOP);
3473 }
3474
Luiz Capitulino222f1802006-03-20 22:16:13 -08003475 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 pktgen_run(t);
3477 t->control &= ~(T_RUN);
3478 }
3479
Luiz Capitulino222f1802006-03-20 22:16:13 -08003480 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003482 t->control &= ~(T_REMDEVALL);
3483 }
3484
Luiz Capitulino222f1802006-03-20 22:16:13 -08003485 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003486 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 t->control &= ~(T_REMDEV);
3488 }
3489
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003490 try_to_freeze();
3491
David S. Milleree74baa2007-01-01 20:51:53 -08003492 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Joe Perchesf9467ea2010-06-21 12:29:14 +00003495 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003496 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Joe Perchesf9467ea2010-06-21 12:29:14 +00003498 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003499 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Joe Perchesf9467ea2010-06-21 12:29:14 +00003501 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003502 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003503
Eric Dumazet551eaff2010-11-21 10:26:44 -08003504 /* Wait for kthread_stop */
3505 while (!kthread_should_stop()) {
3506 set_current_state(TASK_INTERRUPTIBLE);
3507 schedule();
3508 }
3509 __set_current_state(TASK_RUNNING);
3510
David S. Milleree74baa2007-01-01 20:51:53 -08003511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Luiz Capitulino222f1802006-03-20 22:16:13 -08003514static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003515 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003517 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003518 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Eric Dumazet3e984842009-11-24 14:50:53 -08003520 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003521 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003522 if (strncmp(p->odevname, ifname, len) == 0) {
3523 if (p->odevname[len]) {
3524 if (exact || p->odevname[len] != '@')
3525 continue;
3526 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003527 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003528 break;
3529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Luiz Capitulino222f1802006-03-20 22:16:13 -08003531 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003532 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003533 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003536/*
3537 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 */
3539
Luiz Capitulino222f1802006-03-20 22:16:13 -08003540static int add_dev_to_thread(struct pktgen_thread *t,
3541 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Luiz Capitulino222f1802006-03-20 22:16:13 -08003545 if_lock(t);
3546
3547 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003548 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003549 rv = -EBUSY;
3550 goto out;
3551 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003552
3553 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003554 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 pkt_dev->running = 0;
3556
Luiz Capitulino222f1802006-03-20 22:16:13 -08003557out:
3558 if_unlock(t);
3559 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
3562/* Called under thread lock */
3563
Luiz Capitulino222f1802006-03-20 22:16:13 -08003564static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003567 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003568 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 /* We don't allow a device to be on several threads */
3571
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003572 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3573 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003574 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003575 return -EBUSY;
3576 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003577
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003578 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003579 if (!pkt_dev)
3580 return -ENOMEM;
3581
Eric Dumazet593f63b2009-11-23 01:44:37 +00003582 strcpy(pkt_dev->odevname, ifname);
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003583 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3584 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003585 if (pkt_dev->flows == NULL) {
3586 kfree(pkt_dev);
3587 return -ENOMEM;
3588 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003589 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003590
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003591 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003592 pkt_dev->min_pkt_size = ETH_ZLEN;
3593 pkt_dev->max_pkt_size = ETH_ZLEN;
3594 pkt_dev->nfrags = 0;
3595 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003596 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003597 pkt_dev->count = pg_count_d;
3598 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003599 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003600 pkt_dev->udp_src_max = 9;
3601 pkt_dev->udp_dst_min = 9;
3602 pkt_dev->udp_dst_max = 9;
3603
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003604 pkt_dev->vlan_p = 0;
3605 pkt_dev->vlan_cfi = 0;
3606 pkt_dev->vlan_id = 0xffff;
3607 pkt_dev->svlan_p = 0;
3608 pkt_dev->svlan_cfi = 0;
3609 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003610 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003611
Stephen Hemminger39df2322007-03-04 16:11:51 -08003612 err = pktgen_setup_dev(pkt_dev, ifname);
3613 if (err)
3614 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003615
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003616 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3617 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003618 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003619 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003620 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003621 err = -EINVAL;
3622 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003623 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003624#ifdef CONFIG_XFRM
3625 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3626 pkt_dev->ipsproto = IPPROTO_ESP;
3627#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003628
3629 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003630out2:
3631 dev_put(pkt_dev->odev);
3632out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003633#ifdef CONFIG_XFRM
3634 free_SAs(pkt_dev);
3635#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003636 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003637 kfree(pkt_dev);
3638 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
David S. Milleree74baa2007-01-01 20:51:53 -08003641static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003643 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003644 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003645 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003647 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3648 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003649 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003650 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003651 return -ENOMEM;
3652 }
3653
Luiz Capitulino222f1802006-03-20 22:16:13 -08003654 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003656
David S. Milleree74baa2007-01-01 20:51:53 -08003657 INIT_LIST_HEAD(&t->if_list);
3658
3659 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003660 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003661
Eric Dumazet94dcf292011-03-22 16:30:45 -07003662 p = kthread_create_on_node(pktgen_thread_worker,
3663 t,
3664 cpu_to_node(cpu),
3665 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003666 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003667 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003668 list_del(&t->th_list);
3669 kfree(t);
3670 return PTR_ERR(p);
3671 }
3672 kthread_bind(p, cpu);
3673 t->tsk = p;
3674
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003675 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3676 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003677 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003678 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003679 PG_PROC_DIR, t->tsk->comm);
3680 kthread_stop(p);
3681 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003682 kfree(t);
3683 return -EINVAL;
3684 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003685
David S. Milleree74baa2007-01-01 20:51:53 -08003686 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003687 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689 return 0;
3690}
3691
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003692/*
3693 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003695static void _rem_dev_from_if_list(struct pktgen_thread *t,
3696 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003698 struct list_head *q, *n;
3699 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003701 list_for_each_safe(q, n, &t->if_list) {
3702 p = list_entry(q, struct pktgen_dev, list);
3703 if (p == pkt_dev)
3704 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
3706}
3707
Luiz Capitulino222f1802006-03-20 22:16:13 -08003708static int pktgen_remove_device(struct pktgen_thread *t,
3709 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
3711
Joe Perchesf9467ea2010-06-21 12:29:14 +00003712 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Luiz Capitulino222f1802006-03-20 22:16:13 -08003714 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003715 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003716 pktgen_stop_device(pkt_dev);
3717 }
3718
3719 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 if (pkt_dev->odev) {
3722 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003723 pkt_dev->odev = NULL;
3724 }
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 /* And update the thread if_list */
3727
3728 _rem_dev_from_if_list(t, pkt_dev);
3729
Stephen Hemminger39df2322007-03-04 16:11:51 -08003730 if (pkt_dev->entry)
3731 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003733#ifdef CONFIG_XFRM
3734 free_SAs(pkt_dev);
3735#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003736 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003737 if (pkt_dev->page)
3738 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
Luiz Capitulino222f1802006-03-20 22:16:13 -08003743static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
3745 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003746 struct proc_dir_entry *pe;
3747
Joe Perchesf9467ea2010-06-21 12:29:14 +00003748 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003750 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003751 if (!pg_proc_dir)
3752 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Wang Chen25296d52008-02-28 14:11:49 -08003754 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003755 if (pe == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003756 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003757 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003758 return -EINVAL;
3759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 /* Register us to receive netdevice events */
3762 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003763
John Hawkes670c02c2005-10-13 09:30:31 -07003764 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003765 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
David S. Milleree74baa2007-01-01 20:51:53 -08003767 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003768 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00003769 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3770 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003771 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003772
3773 if (list_empty(&pktgen_threads)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003774 pr_err("ERROR: Initialization failed for all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003775 unregister_netdevice_notifier(&pktgen_notifier_block);
3776 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003777 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003778 return -ENODEV;
3779 }
3780
Luiz Capitulino222f1802006-03-20 22:16:13 -08003781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
3784static void __exit pg_cleanup(void)
3785{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003786 struct pktgen_thread *t;
3787 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Luiz Capitulino222f1802006-03-20 22:16:13 -08003789 /* Stop all interfaces & threads */
Eric Dumazet551eaff2010-11-21 10:26:44 -08003790 pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003792 list_for_each_safe(q, n, &pktgen_threads) {
3793 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003794 kthread_stop(t->tsk);
3795 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Luiz Capitulino222f1802006-03-20 22:16:13 -08003798 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 unregister_netdevice_notifier(&pktgen_notifier_block);
3800
Luiz Capitulino222f1802006-03-20 22:16:13 -08003801 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003802 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003803 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806module_init(pg_init);
3807module_exit(pg_cleanup);
3808
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003809MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810MODULE_DESCRIPTION("Packet Generator tool");
3811MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003812MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003814MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003816MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003818MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003820MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");