blob: d73b77adb676942a00d3e04bd6c20e23162d5bc7 [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 Viro252e3342006-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 Viro252e3342006-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 Viro252e3342006-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 Viro252e3342006-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]);
452static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000455static int pg_count_d __read_mostly = 1000;
456static int pg_delay_d __read_mostly;
457static int pg_clone_skb_d __read_mostly;
458static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Luiz Capitulino222fa072006-03-20 22:24:27 -0800460static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800461static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static struct notifier_block pktgen_notifier_block = {
464 .notifier_call = pktgen_device_event,
465};
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900468 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
470 */
471
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700472static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800473{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000474 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000478static ssize_t pgctrl_write(struct file *file, const char __user *buf,
479 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700482 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Luiz Capitulino222f1802006-03-20 22:16:13 -0800484 if (!capable(CAP_NET_ADMIN)) {
485 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700489 if (count > sizeof(data))
490 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700493 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700494 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800495 }
496 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 pktgen_stop_all_threads_ifs();
500
Luiz Capitulino222f1802006-03-20 22:16:13 -0800501 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 pktgen_run_all_threads();
503
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800504 else if (!strcmp(data, "reset"))
505 pktgen_reset_all_threads();
506
Luiz Capitulino222f1802006-03-20 22:16:13 -0800507 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000508 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 err = count;
511
Luiz Capitulino222f1802006-03-20 22:16:13 -0800512out:
513 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700516static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700518 return single_open(file, pgctrl_show, PDE(inode)->data);
519}
520
Arjan van de Ven9a321442007-02-12 00:55:35 -0800521static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800522 .owner = THIS_MODULE,
523 .open = pgctrl_open,
524 .read = seq_read,
525 .llseek = seq_lseek,
526 .write = pgctrl_write,
527 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700528};
529
530static int pktgen_if_show(struct seq_file *seq, void *v)
531{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000532 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000533 ktime_t stopped;
534 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Luiz Capitulino222f1802006-03-20 22:16:13 -0800536 seq_printf(seq,
537 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
538 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
539 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Luiz Capitulino222f1802006-03-20 22:16:13 -0800541 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000542 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
543 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000544 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Luiz Capitulino222f1802006-03-20 22:16:13 -0800546 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
547 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Robert Olsson45b270f2007-08-28 15:45:55 -0700549 seq_printf(seq,
550 " queue_map_min: %u queue_map_max: %u\n",
551 pkt_dev->queue_map_min,
552 pkt_dev->queue_map_max);
553
John Fastabend9e50e3a2010-11-16 19:12:28 +0000554 if (pkt_dev->skb_priority)
555 seq_printf(seq, " skb_priority: %u\n",
556 pkt_dev->skb_priority);
557
Luiz Capitulino222f1802006-03-20 22:16:13 -0800558 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800560 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
561 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
562 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
563 seq_printf(seq,
564 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
565 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Luiz Capitulino222f1802006-03-20 22:16:13 -0800567 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
568 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
569 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
570 seq_printf(seq,
571 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
572 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000574 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800575 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000576 " dst_min: %s dst_max: %s\n",
577 pkt_dev->dst_min, pkt_dev->dst_max);
578 seq_printf(seq,
579 " src_min: %s src_max: %s\n",
580 pkt_dev->src_min, pkt_dev->src_max);
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700583 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Johannes Berge1749612008-10-27 15:59:26 -0700585 seq_printf(seq, "%pM ",
586 is_zero_ether_addr(pkt_dev->src_mac) ?
587 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Luiz Capitulino222f1802006-03-20 22:16:13 -0800589 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700590 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Luiz Capitulino222f1802006-03-20 22:16:13 -0800592 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000593 " udp_src_min: %d udp_src_max: %d"
594 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800595 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
596 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Luiz Capitulino222f1802006-03-20 22:16:13 -0800598 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800599 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700600 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800602 if (pkt_dev->nr_labels) {
603 unsigned i;
604 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700605 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800606 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
607 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
608 }
609
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000610 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700611 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000612 pkt_dev->vlan_id, pkt_dev->vlan_p,
613 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700614
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000615 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700616 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000617 pkt_dev->svlan_id, pkt_dev->svlan_p,
618 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700619
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000620 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700621 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700622
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000623 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700624 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700625
Robert Olssone99b99b2010-03-18 22:44:30 +0000626 if (pkt_dev->node >= 0)
627 seq_printf(seq, " node: %d\n", pkt_dev->node);
628
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800629 seq_printf(seq, " Flags: ");
630
Luiz Capitulino222f1802006-03-20 22:16:13 -0800631 if (pkt_dev->flags & F_IPV6)
632 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Luiz Capitulino222f1802006-03-20 22:16:13 -0800634 if (pkt_dev->flags & F_IPSRC_RND)
635 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Luiz Capitulino222f1802006-03-20 22:16:13 -0800637 if (pkt_dev->flags & F_IPDST_RND)
638 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Luiz Capitulino222f1802006-03-20 22:16:13 -0800640 if (pkt_dev->flags & F_TXSIZE_RND)
641 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Luiz Capitulino222f1802006-03-20 22:16:13 -0800643 if (pkt_dev->flags & F_UDPSRC_RND)
644 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Luiz Capitulino222f1802006-03-20 22:16:13 -0800646 if (pkt_dev->flags & F_UDPDST_RND)
647 seq_printf(seq, "UDPDST_RND ");
648
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800649 if (pkt_dev->flags & F_MPLS_RND)
650 seq_printf(seq, "MPLS_RND ");
651
Robert Olsson45b270f2007-08-28 15:45:55 -0700652 if (pkt_dev->flags & F_QUEUE_MAP_RND)
653 seq_printf(seq, "QUEUE_MAP_RND ");
654
Robert Olssone6fce5b2008-08-07 02:23:01 -0700655 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
656 seq_printf(seq, "QUEUE_MAP_CPU ");
657
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700658 if (pkt_dev->cflows) {
659 if (pkt_dev->flags & F_FLOW_SEQ)
660 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
661 else
662 seq_printf(seq, "FLOW_RND ");
663 }
664
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700665#ifdef CONFIG_XFRM
666 if (pkt_dev->flags & F_IPSEC_ON)
667 seq_printf(seq, "IPSEC ");
668#endif
669
Luiz Capitulino222f1802006-03-20 22:16:13 -0800670 if (pkt_dev->flags & F_MACSRC_RND)
671 seq_printf(seq, "MACSRC_RND ");
672
673 if (pkt_dev->flags & F_MACDST_RND)
674 seq_printf(seq, "MACDST_RND ");
675
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700676 if (pkt_dev->flags & F_VID_RND)
677 seq_printf(seq, "VID_RND ");
678
679 if (pkt_dev->flags & F_SVID_RND)
680 seq_printf(seq, "SVID_RND ");
681
Robert Olssone99b99b2010-03-18 22:44:30 +0000682 if (pkt_dev->flags & F_NODE)
683 seq_printf(seq, "NODE_ALLOC ");
684
Luiz Capitulino222f1802006-03-20 22:16:13 -0800685 seq_puts(seq, "\n");
686
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000687 /* not really stopped, more like last-running-at */
688 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
689 idle = pkt_dev->idle_acc;
690 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800691
692 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000693 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800694 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000695 (unsigned long long)pkt_dev->errors);
696
697 seq_printf(seq,
698 " started: %lluus stopped: %lluus idle: %lluus\n",
699 (unsigned long long) ktime_to_us(pkt_dev->started_at),
700 (unsigned long long) ktime_to_us(stopped),
701 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800702
703 seq_printf(seq,
704 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700705 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
706 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Luiz Capitulino222f1802006-03-20 22:16:13 -0800708 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800710 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
711 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
712 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
713 } else
714 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700715 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Luiz Capitulino222f1802006-03-20 22:16:13 -0800717 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700718 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Robert Olsson45b270f2007-08-28 15:45:55 -0700720 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
721
Luiz Capitulino222f1802006-03-20 22:16:13 -0800722 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800725 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800727 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800732
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000733static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
734 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800735{
736 int i = 0;
737 *num = 0;
738
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700739 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000740 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800741 char c;
742 *num <<= 4;
743 if (get_user(c, &user_buffer[i]))
744 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000745 value = hex_to_bin(c);
746 if (value >= 0)
747 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800748 else
749 break;
750 }
751 return i;
752}
753
Luiz Capitulino222f1802006-03-20 22:16:13 -0800754static int count_trail_chars(const char __user * user_buffer,
755 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 int i;
758
759 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800760 char c;
761 if (get_user(c, &user_buffer[i]))
762 return -EFAULT;
763 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 case '\"':
765 case '\n':
766 case '\r':
767 case '\t':
768 case ' ':
769 case '=':
770 break;
771 default:
772 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775done:
776 return i;
777}
778
Luiz Capitulino222f1802006-03-20 22:16:13 -0800779static unsigned long num_arg(const char __user * user_buffer,
780 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000782 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800784
Paul Gortmakerd6182222010-10-18 12:14:44 +0000785 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800786 char c;
787 if (get_user(c, &user_buffer[i]))
788 return -EFAULT;
789 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800791 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 } else
793 break;
794 }
795 return i;
796}
797
Luiz Capitulino222f1802006-03-20 22:16:13 -0800798static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000800 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Paul Gortmakerd6182222010-10-18 12:14:44 +0000802 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800803 char c;
804 if (get_user(c, &user_buffer[i]))
805 return -EFAULT;
806 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 case '\"':
808 case '\n':
809 case '\r':
810 case '\t':
811 case ' ':
812 goto done_str;
813 break;
814 default:
815 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return i;
820}
821
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800822static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
823{
824 unsigned n = 0;
825 char c;
826 ssize_t i = 0;
827 int len;
828
829 pkt_dev->nr_labels = 0;
830 do {
831 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700832 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800833 if (len <= 0)
834 return len;
835 pkt_dev->labels[n] = htonl(tmp);
836 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
837 pkt_dev->flags |= F_MPLS_RND;
838 i += len;
839 if (get_user(c, &buffer[i]))
840 return -EFAULT;
841 i++;
842 n++;
843 if (n >= MAX_MPLS_LABELS)
844 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700845 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800846
847 pkt_dev->nr_labels = n;
848 return i;
849}
850
Luiz Capitulino222f1802006-03-20 22:16:13 -0800851static ssize_t pktgen_if_write(struct file *file,
852 const char __user * user_buffer, size_t count,
853 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Joe Perches8a994a72010-07-12 10:50:23 +0000855 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800856 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000857 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 char name[16], valstr[32];
859 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800860 char *pg_result = NULL;
861 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863
864 pg_result = &(pkt_dev->result[0]);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000867 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return -EINVAL;
869 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800870
Paul Gortmakerd6182222010-10-18 12:14:44 +0000871 max = count;
872 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800873 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000874 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000877 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* Read variable name */
880
881 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000882 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800883 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -EFAULT;
888 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889
890 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800892 if (len < 0)
893 return len;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 i += len;
896
897 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000898 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700899 char tb[copy + 1];
900 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700902 tb[copy] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700903 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 (unsigned long)count, tb);
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (!strcmp(name, "min_pkt_size")) {
908 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000909 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800910 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800913 if (value < 14 + 20 + 8)
914 value = 14 + 20 + 8;
915 if (value != pkt_dev->min_pkt_size) {
916 pkt_dev->min_pkt_size = value;
917 pkt_dev->cur_pkt_size = value;
918 }
919 sprintf(pg_result, "OK: min_pkt_size=%u",
920 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return count;
922 }
923
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000926 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800927 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 if (value < 14 + 20 + 8)
931 value = 14 + 20 + 8;
932 if (value != pkt_dev->max_pkt_size) {
933 pkt_dev->max_pkt_size = value;
934 pkt_dev->cur_pkt_size = value;
935 }
936 sprintf(pg_result, "OK: max_pkt_size=%u",
937 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return count;
939 }
940
Luiz Capitulino222f1802006-03-20 22:16:13 -0800941 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (!strcmp(name, "pkt_size")) {
944 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000945 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800946 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800949 if (value < 14 + 20 + 8)
950 value = 14 + 20 + 8;
951 if (value != pkt_dev->min_pkt_size) {
952 pkt_dev->min_pkt_size = value;
953 pkt_dev->max_pkt_size = value;
954 pkt_dev->cur_pkt_size = value;
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
957 return count;
958 }
959
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000962 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800963 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800966 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 sprintf(pg_result, "OK: debug=%u", debug);
968 return count;
969 }
970
Luiz Capitulino222f1802006-03-20 22:16:13 -0800971 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 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;
977 pkt_dev->nfrags = value;
978 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
979 return count;
980 }
981 if (!strcmp(name, "delay")) {
982 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000983 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800984 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000987 if (value == 0x7FFFFFFF)
988 pkt_dev->delay = ULLONG_MAX;
989 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000990 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000991
992 sprintf(pg_result, "OK: delay=%llu",
993 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return count;
995 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000996 if (!strcmp(name, "rate")) {
997 len = num_arg(&user_buffer[i], 10, &value);
998 if (len < 0)
999 return len;
1000
1001 i += len;
1002 if (!value)
1003 return len;
1004 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1005 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001006 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001007
1008 sprintf(pg_result, "OK: rate=%lu", value);
1009 return count;
1010 }
1011 if (!strcmp(name, "ratep")) {
1012 len = num_arg(&user_buffer[i], 10, &value);
1013 if (len < 0)
1014 return len;
1015
1016 i += len;
1017 if (!value)
1018 return len;
1019 pkt_dev->delay = NSEC_PER_SEC/value;
1020 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001021 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001022
1023 sprintf(pg_result, "OK: rate=%lu", value);
1024 return count;
1025 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001026 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001028 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001032 if (value != pkt_dev->udp_src_min) {
1033 pkt_dev->udp_src_min = value;
1034 pkt_dev->cur_udp_src = value;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1037 return count;
1038 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001041 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001042 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 if (value != pkt_dev->udp_dst_min) {
1046 pkt_dev->udp_dst_min = value;
1047 pkt_dev->cur_udp_dst = value;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1050 return count;
1051 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001054 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001055 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001058 if (value != pkt_dev->udp_src_max) {
1059 pkt_dev->udp_src_max = value;
1060 pkt_dev->cur_udp_src = value;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1063 return count;
1064 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001065 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001067 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001068 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001071 if (value != pkt_dev->udp_dst_max) {
1072 pkt_dev->udp_dst_max = value;
1073 pkt_dev->cur_udp_dst = value;
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1076 return count;
1077 }
1078 if (!strcmp(name, "clone_skb")) {
1079 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001080 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001081 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001084 pkt_dev->clone_skb = value;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1087 return count;
1088 }
1089 if (!strcmp(name, "count")) {
1090 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001091 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001092 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 i += len;
1095 pkt_dev->count = value;
1096 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001097 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return count;
1099 }
1100 if (!strcmp(name, "src_mac_count")) {
1101 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001102 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001103 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 i += len;
1106 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001107 pkt_dev->src_mac_count = value;
1108 pkt_dev->cur_src_mac_offset = 0;
1109 }
1110 sprintf(pg_result, "OK: src_mac_count=%d",
1111 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return count;
1113 }
1114 if (!strcmp(name, "dst_mac_count")) {
1115 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001116 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001117 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 i += len;
1120 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001121 pkt_dev->dst_mac_count = value;
1122 pkt_dev->cur_dst_mac_offset = 0;
1123 }
1124 sprintf(pg_result, "OK: dst_mac_count=%d",
1125 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return count;
1127 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001128 if (!strcmp(name, "node")) {
1129 len = num_arg(&user_buffer[i], 10, &value);
1130 if (len < 0)
1131 return len;
1132
1133 i += len;
1134
1135 if (node_possible(value)) {
1136 pkt_dev->node = value;
1137 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001138 if (pkt_dev->page) {
1139 put_page(pkt_dev->page);
1140 pkt_dev->page = NULL;
1141 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001142 }
1143 else
1144 sprintf(pg_result, "ERROR: node not possible");
1145 return count;
1146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001148 char f[32];
1149 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001151 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001152 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (copy_from_user(f, &user_buffer[i], len))
1155 return -EFAULT;
1156 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001157 if (strcmp(f, "IPSRC_RND") == 0)
1158 pkt_dev->flags |= F_IPSRC_RND;
1159
1160 else if (strcmp(f, "!IPSRC_RND") == 0)
1161 pkt_dev->flags &= ~F_IPSRC_RND;
1162
1163 else if (strcmp(f, "TXSIZE_RND") == 0)
1164 pkt_dev->flags |= F_TXSIZE_RND;
1165
1166 else if (strcmp(f, "!TXSIZE_RND") == 0)
1167 pkt_dev->flags &= ~F_TXSIZE_RND;
1168
1169 else if (strcmp(f, "IPDST_RND") == 0)
1170 pkt_dev->flags |= F_IPDST_RND;
1171
1172 else if (strcmp(f, "!IPDST_RND") == 0)
1173 pkt_dev->flags &= ~F_IPDST_RND;
1174
1175 else if (strcmp(f, "UDPSRC_RND") == 0)
1176 pkt_dev->flags |= F_UDPSRC_RND;
1177
1178 else if (strcmp(f, "!UDPSRC_RND") == 0)
1179 pkt_dev->flags &= ~F_UDPSRC_RND;
1180
1181 else if (strcmp(f, "UDPDST_RND") == 0)
1182 pkt_dev->flags |= F_UDPDST_RND;
1183
1184 else if (strcmp(f, "!UDPDST_RND") == 0)
1185 pkt_dev->flags &= ~F_UDPDST_RND;
1186
1187 else if (strcmp(f, "MACSRC_RND") == 0)
1188 pkt_dev->flags |= F_MACSRC_RND;
1189
1190 else if (strcmp(f, "!MACSRC_RND") == 0)
1191 pkt_dev->flags &= ~F_MACSRC_RND;
1192
1193 else if (strcmp(f, "MACDST_RND") == 0)
1194 pkt_dev->flags |= F_MACDST_RND;
1195
1196 else if (strcmp(f, "!MACDST_RND") == 0)
1197 pkt_dev->flags &= ~F_MACDST_RND;
1198
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001199 else if (strcmp(f, "MPLS_RND") == 0)
1200 pkt_dev->flags |= F_MPLS_RND;
1201
1202 else if (strcmp(f, "!MPLS_RND") == 0)
1203 pkt_dev->flags &= ~F_MPLS_RND;
1204
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001205 else if (strcmp(f, "VID_RND") == 0)
1206 pkt_dev->flags |= F_VID_RND;
1207
1208 else if (strcmp(f, "!VID_RND") == 0)
1209 pkt_dev->flags &= ~F_VID_RND;
1210
1211 else if (strcmp(f, "SVID_RND") == 0)
1212 pkt_dev->flags |= F_SVID_RND;
1213
1214 else if (strcmp(f, "!SVID_RND") == 0)
1215 pkt_dev->flags &= ~F_SVID_RND;
1216
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001217 else if (strcmp(f, "FLOW_SEQ") == 0)
1218 pkt_dev->flags |= F_FLOW_SEQ;
1219
Robert Olsson45b270f2007-08-28 15:45:55 -07001220 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1221 pkt_dev->flags |= F_QUEUE_MAP_RND;
1222
1223 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1224 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001225
1226 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1227 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1228
1229 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1230 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001231#ifdef CONFIG_XFRM
1232 else if (strcmp(f, "IPSEC") == 0)
1233 pkt_dev->flags |= F_IPSEC_ON;
1234#endif
1235
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001236 else if (strcmp(f, "!IPV6") == 0)
1237 pkt_dev->flags &= ~F_IPV6;
1238
Robert Olssone99b99b2010-03-18 22:44:30 +00001239 else if (strcmp(f, "NODE_ALLOC") == 0)
1240 pkt_dev->flags |= F_NODE;
1241
1242 else if (strcmp(f, "!NODE_ALLOC") == 0)
1243 pkt_dev->flags &= ~F_NODE;
1244
Luiz Capitulino222f1802006-03-20 22:16:13 -08001245 else {
1246 sprintf(pg_result,
1247 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1248 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001249 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001250 "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 -08001251 return count;
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1254 return count;
1255 }
1256 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1257 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001258 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Luiz Capitulino222f1802006-03-20 22:16:13 -08001261 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001263 buf[len] = 0;
1264 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1265 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1266 strncpy(pkt_dev->dst_min, buf, len);
1267 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1268 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1269 }
1270 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001271 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001272 pkt_dev->dst_min);
1273 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1275 return count;
1276 }
1277 if (!strcmp(name, "dst_max")) {
1278 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001279 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001280 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Luiz Capitulino222f1802006-03-20 22:16:13 -08001283 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EFAULT;
1285
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286 buf[len] = 0;
1287 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1288 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1289 strncpy(pkt_dev->dst_max, buf, len);
1290 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1291 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1292 }
1293 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001294 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001295 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 i += len;
1297 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1298 return count;
1299 }
1300 if (!strcmp(name, "dst6")) {
1301 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001302 if (len < 0)
1303 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 pkt_dev->flags |= F_IPV6;
1306
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001309 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001312 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1315
Luiz Capitulino222f1802006-03-20 22:16:13 -08001316 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001317 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sprintf(pg_result, "OK: dst6=%s", buf);
1321 return count;
1322 }
1323 if (!strcmp(name, "dst6_min")) {
1324 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001325 if (len < 0)
1326 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 pkt_dev->flags |= F_IPV6;
1329
Luiz Capitulino222f1802006-03-20 22:16:13 -08001330 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001332 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1338 &pkt_dev->min_in6_daddr);
1339 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001340 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Luiz Capitulino222f1802006-03-20 22:16:13 -08001342 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 sprintf(pg_result, "OK: dst6_min=%s", buf);
1344 return count;
1345 }
1346 if (!strcmp(name, "dst6_max")) {
1347 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001348 if (len < 0)
1349 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 pkt_dev->flags |= F_IPV6;
1352
Luiz Capitulino222f1802006-03-20 22:16:13 -08001353 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001355 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001358 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Luiz Capitulino222f1802006-03-20 22:16:13 -08001360 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001361 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Luiz Capitulino222f1802006-03-20 22:16:13 -08001363 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 sprintf(pg_result, "OK: dst6_max=%s", buf);
1365 return count;
1366 }
1367 if (!strcmp(name, "src6")) {
1368 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001369 if (len < 0)
1370 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 pkt_dev->flags |= F_IPV6;
1373
Luiz Capitulino222f1802006-03-20 22:16:13 -08001374 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001376 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001379 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1382
Luiz Capitulino222f1802006-03-20 22:16:13 -08001383 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001384 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001385
1386 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 sprintf(pg_result, "OK: src6=%s", buf);
1388 return count;
1389 }
1390 if (!strcmp(name, "src_min")) {
1391 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001392 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001394
Luiz Capitulino222f1802006-03-20 22:16:13 -08001395 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001397 buf[len] = 0;
1398 if (strcmp(buf, pkt_dev->src_min) != 0) {
1399 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1400 strncpy(pkt_dev->src_min, buf, len);
1401 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1402 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1403 }
1404 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001405 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001406 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 i += len;
1408 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1409 return count;
1410 }
1411 if (!strcmp(name, "src_max")) {
1412 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001413 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001414 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001415
Luiz Capitulino222f1802006-03-20 22:16:13 -08001416 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001418 buf[len] = 0;
1419 if (strcmp(buf, pkt_dev->src_max) != 0) {
1420 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1421 strncpy(pkt_dev->src_max, buf, len);
1422 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1423 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1424 }
1425 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001426 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 i += len;
1429 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1430 return count;
1431 }
1432 if (!strcmp(name, "dst_mac")) {
1433 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001434 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001436 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001439 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001440 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001443 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -EFAULT;
1445 i += len;
1446
Luiz Capitulino222f1802006-03-20 22:16:13 -08001447 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Andy Shevchenko451e07a2010-07-23 03:18:10 +00001448 int value;
1449
1450 value = hex_to_bin(*v);
1451 if (value >= 0)
1452 *m = *m * 16 + value;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (*v == ':') {
1455 m++;
1456 *m = 0;
1457 }
1458 }
1459
1460 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001461 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1462 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 sprintf(pg_result, "OK: dstmac");
1465 return count;
1466 }
1467 if (!strcmp(name, "src_mac")) {
1468 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001469 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 unsigned char *m = pkt_dev->src_mac;
1471
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001472 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001475 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001476 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001479 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -EFAULT;
1481 i += len;
1482
Luiz Capitulino222f1802006-03-20 22:16:13 -08001483 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Andy Shevchenko451e07a2010-07-23 03:18:10 +00001484 int value;
1485
1486 value = hex_to_bin(*v);
1487 if (value >= 0)
1488 *m = *m * 16 + value;
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (*v == ':') {
1491 m++;
1492 *m = 0;
1493 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001496 /* Set up Src MAC */
1497 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1498 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1499
Luiz Capitulino222f1802006-03-20 22:16:13 -08001500 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return count;
1502 }
1503
Luiz Capitulino222f1802006-03-20 22:16:13 -08001504 if (!strcmp(name, "clear_counters")) {
1505 pktgen_clear_counters(pkt_dev);
1506 sprintf(pg_result, "OK: Clearing counters.\n");
1507 return count;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 if (!strcmp(name, "flows")) {
1511 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001512 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001513 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 i += len;
1516 if (value > MAX_CFLOWS)
1517 value = MAX_CFLOWS;
1518
1519 pkt_dev->cflows = value;
1520 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1521 return count;
1522 }
1523
1524 if (!strcmp(name, "flowlen")) {
1525 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001526 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001527 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 i += len;
1530 pkt_dev->lflow = value;
1531 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1532 return count;
1533 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001534
Robert Olsson45b270f2007-08-28 15:45:55 -07001535 if (!strcmp(name, "queue_map_min")) {
1536 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001537 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001538 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001539
Robert Olsson45b270f2007-08-28 15:45:55 -07001540 i += len;
1541 pkt_dev->queue_map_min = value;
1542 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1543 return count;
1544 }
1545
1546 if (!strcmp(name, "queue_map_max")) {
1547 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001548 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001549 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001550
Robert Olsson45b270f2007-08-28 15:45:55 -07001551 i += len;
1552 pkt_dev->queue_map_max = value;
1553 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1554 return count;
1555 }
1556
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001557 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001558 unsigned n, cnt;
1559
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001560 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001561 if (len < 0)
1562 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001563 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001564 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001565 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001566 cnt += sprintf(pg_result + cnt,
1567 "%08x%s", ntohl(pkt_dev->labels[n]),
1568 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001569
1570 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1571 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1572 pkt_dev->svlan_id = 0xffff;
1573
1574 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001575 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001576 }
1577 return count;
1578 }
1579
1580 if (!strcmp(name, "vlan_id")) {
1581 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001582 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001583 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001584
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001585 i += len;
1586 if (value <= 4095) {
1587 pkt_dev->vlan_id = value; /* turn on VLAN */
1588
1589 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001590 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001591
1592 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001593 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001594
1595 pkt_dev->nr_labels = 0; /* turn off MPLS */
1596 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1597 } else {
1598 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1599 pkt_dev->svlan_id = 0xffff;
1600
1601 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001602 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001603 }
1604 return count;
1605 }
1606
1607 if (!strcmp(name, "vlan_p")) {
1608 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001609 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001610 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001611
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001612 i += len;
1613 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1614 pkt_dev->vlan_p = value;
1615 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1616 } else {
1617 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1618 }
1619 return count;
1620 }
1621
1622 if (!strcmp(name, "vlan_cfi")) {
1623 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001624 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001625 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001626
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001627 i += len;
1628 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1629 pkt_dev->vlan_cfi = value;
1630 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1631 } else {
1632 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1633 }
1634 return count;
1635 }
1636
1637 if (!strcmp(name, "svlan_id")) {
1638 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001639 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001640 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001641
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001642 i += len;
1643 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1644 pkt_dev->svlan_id = value; /* turn on SVLAN */
1645
1646 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001647 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001648
1649 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001650 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001651
1652 pkt_dev->nr_labels = 0; /* turn off MPLS */
1653 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1654 } else {
1655 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1656 pkt_dev->svlan_id = 0xffff;
1657
1658 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001659 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001660 }
1661 return count;
1662 }
1663
1664 if (!strcmp(name, "svlan_p")) {
1665 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001666 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001667 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001668
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001669 i += len;
1670 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1671 pkt_dev->svlan_p = value;
1672 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1673 } else {
1674 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1675 }
1676 return count;
1677 }
1678
1679 if (!strcmp(name, "svlan_cfi")) {
1680 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001681 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001682 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001683
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001684 i += len;
1685 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1686 pkt_dev->svlan_cfi = value;
1687 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1688 } else {
1689 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1690 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001691 return count;
1692 }
1693
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001694 if (!strcmp(name, "tos")) {
1695 __u32 tmp_value = 0;
1696 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001697 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001698 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001699
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001700 i += len;
1701 if (len == 2) {
1702 pkt_dev->tos = tmp_value;
1703 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1704 } else {
1705 sprintf(pg_result, "ERROR: tos must be 00-ff");
1706 }
1707 return count;
1708 }
1709
1710 if (!strcmp(name, "traffic_class")) {
1711 __u32 tmp_value = 0;
1712 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001713 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001714 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001715
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001716 i += len;
1717 if (len == 2) {
1718 pkt_dev->traffic_class = tmp_value;
1719 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1720 } else {
1721 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1722 }
1723 return count;
1724 }
1725
John Fastabend9e50e3a2010-11-16 19:12:28 +00001726 if (!strcmp(name, "skb_priority")) {
1727 len = num_arg(&user_buffer[i], 9, &value);
1728 if (len < 0)
1729 return len;
1730
1731 i += len;
1732 pkt_dev->skb_priority = value;
1733 sprintf(pg_result, "OK: skb_priority=%i",
1734 pkt_dev->skb_priority);
1735 return count;
1736 }
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1739 return -EINVAL;
1740}
1741
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001742static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001744 return single_open(file, pktgen_if_show, PDE(inode)->data);
1745}
1746
Arjan van de Ven9a321442007-02-12 00:55:35 -08001747static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001748 .owner = THIS_MODULE,
1749 .open = pktgen_if_open,
1750 .read = seq_read,
1751 .llseek = seq_lseek,
1752 .write = pktgen_if_write,
1753 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001754};
1755
1756static int pktgen_thread_show(struct seq_file *seq, void *v)
1757{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001758 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001759 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001761 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Luiz Capitulino222f1802006-03-20 22:16:13 -08001763 seq_printf(seq, "Running: ");
1764
1765 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001766 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001767 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001768 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Luiz Capitulino222f1802006-03-20 22:16:13 -08001770 seq_printf(seq, "\nStopped: ");
1771
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001772 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001774 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001777 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001779 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001786static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001787 const char __user * user_buffer,
1788 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Joe Perches8a994a72010-07-12 10:50:23 +00001790 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001792 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001794 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001797 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return -EINVAL;
1799 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001800
Paul Gortmakerd6182222010-10-18 12:14:44 +00001801 max = count;
1802 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001804 return len;
1805
Paul Gortmakerd6182222010-10-18 12:14:44 +00001806 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /* Read variable name */
1809
1810 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001811 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001812 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 memset(name, 0, sizeof(name));
1815 if (copy_from_user(name, &user_buffer[i], len))
1816 return -EFAULT;
1817 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001818
Luiz Capitulino222f1802006-03-20 22:16:13 -08001819 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001821 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001822 return len;
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 i += len;
1825
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001826 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001827 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1828 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Luiz Capitulino222f1802006-03-20 22:16:13 -08001830 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001831 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 ret = -EINVAL;
1833 goto out;
1834 }
1835
1836 pg_result = &(t->result[0]);
1837
Luiz Capitulino222f1802006-03-20 22:16:13 -08001838 if (!strcmp(name, "add_device")) {
1839 char f[32];
1840 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001842 if (len < 0) {
1843 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto out;
1845 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return -EFAULT;
1848 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001849 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001850 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001851 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001852 ret = count;
1853 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto out;
1855 }
1856
Luiz Capitulino222f1802006-03-20 22:16:13 -08001857 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001858 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001859 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001860 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001861 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001863 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 goto out;
1865 }
1866
Luiz Capitulino222f1802006-03-20 22:16:13 -08001867 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001868 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001869 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto out;
1871 }
1872
1873 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001874out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return ret;
1876}
1877
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001878static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001880 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Arjan van de Ven9a321442007-02-12 00:55:35 -08001883static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001884 .owner = THIS_MODULE,
1885 .open = pktgen_thread_open,
1886 .read = seq_read,
1887 .llseek = seq_lseek,
1888 .write = pktgen_thread_write,
1889 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001890};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001893static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct pktgen_thread *t;
1896 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001897 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001899 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001900 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001902 if (remove) {
1903 if_lock(t);
1904 pkt_dev->removal_mark = 1;
1905 t->control |= T_REMDEV;
1906 if_unlock(t);
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 break;
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001911 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001914/*
1915 * mark a device for removal
1916 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001917static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001920 const int max_tries = 10, msec_per_try = 125;
1921 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001922
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001923 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001924 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001925
Luiz Capitulino222f1802006-03-20 22:16:13 -08001926 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001927
1928 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001929 if (pkt_dev == NULL)
1930 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001931
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001932 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001933 pr_debug("%s: waiting for %s to disappear....\n",
1934 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001935 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001936 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001937
1938 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001939 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1940 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001941 break;
1942 }
1943
1944 }
1945
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001946 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001947}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001948
Stephen Hemminger39df2322007-03-04 16:11:51 -08001949static void pktgen_change_name(struct net_device *dev)
1950{
1951 struct pktgen_thread *t;
1952
1953 list_for_each_entry(t, &pktgen_threads, th_list) {
1954 struct pktgen_dev *pkt_dev;
1955
1956 list_for_each_entry(pkt_dev, &t->if_list, list) {
1957 if (pkt_dev->odev != dev)
1958 continue;
1959
1960 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1961
Alexey Dobriyan29753152009-08-28 23:34:43 -07001962 pkt_dev->entry = proc_create_data(dev->name, 0600,
1963 pg_proc_dir,
1964 &pktgen_if_fops,
1965 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001966 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001967 pr_err("can't move proc entry for '%s'\n",
1968 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001969 break;
1970 }
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Luiz Capitulino222f1802006-03-20 22:16:13 -08001974static int pktgen_device_event(struct notifier_block *unused,
1975 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001977 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001979 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001980 return NOTIFY_DONE;
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /* It is OK that we do not hold the group lock right now,
1983 * as we run under the RTNL lock.
1984 */
1985
1986 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001987 case NETDEV_CHANGENAME:
1988 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001992 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 return NOTIFY_DONE;
1997}
1998
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001999static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
2000 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002001{
2002 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00002003 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07002004
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002005 for (i = 0; ifname[i] != '@'; i++) {
2006 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002007 break;
2008
2009 b[i] = ifname[i];
2010 }
2011 b[i] = 0;
2012
2013 return dev_get_by_name(&init_net, b);
2014}
2015
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017/* Associate pktgen_dev with a device. */
2018
Stephen Hemminger39df2322007-03-04 16:11:51 -08002019static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002022 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (pkt_dev->odev) {
2026 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002027 pkt_dev->odev = NULL;
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Robert Olssone6fce5b2008-08-07 02:23:01 -07002030 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002032 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002033 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002037 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002038 err = -EINVAL;
2039 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002040 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002041 err = -ENETDOWN;
2042 } else {
2043 pkt_dev->odev = odev;
2044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002048 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
2051/* Read pkt_dev from the interface and set up internal pktgen_dev
2052 * structure to have the right information to create/send packets
2053 */
2054static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2055{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002056 int ntxq;
2057
Luiz Capitulino222f1802006-03-20 22:16:13 -08002058 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002059 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002060 sprintf(pkt_dev->result,
2061 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2062 return;
2063 }
2064
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002065 /* make sure that we don't pick a non-existing transmit queue */
2066 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002067
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002068 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002069 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2070 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2071 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002072 pkt_dev->queue_map_min = ntxq - 1;
2073 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002074 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002075 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2076 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2077 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002078 pkt_dev->queue_map_max = ntxq - 1;
2079 }
2080
Luiz Capitulino222f1802006-03-20 22:16:13 -08002081 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002083 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002084 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Luiz Capitulino222f1802006-03-20 22:16:13 -08002086 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002087 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Luiz Capitulino222f1802006-03-20 22:16:13 -08002089 /* Set up pkt size */
2090 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2091
2092 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002094 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 * gets exported
2096 */
2097
2098#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002099 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct inet6_dev *idev;
2101
Luiz Capitulino222f1802006-03-20 22:16:13 -08002102 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2103 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 set = 1;
2105 break;
2106 }
2107
Luiz Capitulino222f1802006-03-20 22:16:13 -08002108 if (!set) {
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /*
2111 * Use linklevel address if unconfigured.
2112 *
2113 * use ipv6_get_lladdr if/when it's get exported
2114 */
2115
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002116 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002117 idev = __in6_dev_get(pkt_dev->odev);
2118 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 struct inet6_ifaddr *ifp;
2120
2121 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002122 for (ifp = idev->addr_list; ifp;
2123 ifp = ifp->if_next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002124 if (ifp->scope == IFA_LINK &&
2125 !(ifp->flags & IFA_F_TENTATIVE)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002126 ipv6_addr_copy(&pkt_dev->
2127 cur_in6_saddr,
2128 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 err = 0;
2130 break;
2131 }
2132 }
2133 read_unlock_bh(&idev->lock);
2134 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002135 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002136 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002137 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002140 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 pkt_dev->saddr_min = 0;
2142 pkt_dev->saddr_max = 0;
2143 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002144
2145 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002148 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (in_dev) {
2150 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002151 pkt_dev->saddr_min =
2152 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 pkt_dev->saddr_max = pkt_dev->saddr_min;
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002157 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2159 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2160 }
2161
2162 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2163 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2164 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002165 /* Initialize current values. */
2166 pkt_dev->cur_dst_mac_offset = 0;
2167 pkt_dev->cur_src_mac_offset = 0;
2168 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2169 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2170 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2171 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 pkt_dev->nflows = 0;
2173}
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002176static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2177{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002178 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002179 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002180 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002181
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002182 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2183 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002184
Daniel Turull43d28b62010-06-09 22:49:57 +00002185 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002186 if (remaining <= 0) {
2187 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002188 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002189 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002190
Stephen Hemmingeref879792009-09-22 19:41:43 +00002191 start_time = ktime_now();
Daniel Turull43d28b62010-06-09 22:49:57 +00002192 if (remaining < 100000)
2193 ndelay(remaining); /* really small just spin */
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002194 else {
2195 /* see do_nanosleep */
2196 hrtimer_init_sleeper(&t, current);
2197 do {
2198 set_current_state(TASK_INTERRUPTIBLE);
2199 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2200 if (!hrtimer_active(&t.timer))
2201 t.task = NULL;
2202
2203 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002206 hrtimer_cancel(&t.timer);
2207 } while (t.task && pkt_dev->running && !signal_pending(current));
2208 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002210 end_time = ktime_now();
2211
2212 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002213 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002216static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2217{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002218 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002219 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2220 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2221 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2222}
2223
Stephen Hemminger648fda72009-08-27 13:55:07 +00002224static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002225{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002226 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002227}
2228
2229static inline int f_pick(struct pktgen_dev *pkt_dev)
2230{
2231 int flow = pkt_dev->curfl;
2232
2233 if (pkt_dev->flags & F_FLOW_SEQ) {
2234 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2235 /* reset time */
2236 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002237 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002238 pkt_dev->curfl += 1;
2239 if (pkt_dev->curfl >= pkt_dev->cflows)
2240 pkt_dev->curfl = 0; /*reset */
2241 }
2242 } else {
2243 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002244 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002245
Robert Olsson1211a642008-08-05 18:44:26 -07002246 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002247 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002248 pkt_dev->flows[flow].flags = 0;
2249 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002250 }
2251
2252 return pkt_dev->curfl;
2253}
2254
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002255
2256#ifdef CONFIG_XFRM
2257/* If there was already an IPSEC SA, we keep it as is, else
2258 * we go look for it ...
2259*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002260#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002261static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002262{
2263 struct xfrm_state *x = pkt_dev->flows[flow].x;
2264 if (!x) {
2265 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002266 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002267 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002268 (xfrm_address_t *)&pkt_dev->cur_saddr,
2269 AF_INET,
2270 pkt_dev->ipsmode,
2271 pkt_dev->ipsproto, 0);
2272 if (x) {
2273 pkt_dev->flows[flow].x = x;
2274 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002275 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002276 }
2277
2278 }
2279}
2280#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002281static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2282{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002283
2284 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2285 pkt_dev->cur_queue_map = smp_processor_id();
2286
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002287 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002288 __u16 t;
2289 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2290 t = random32() %
2291 (pkt_dev->queue_map_max -
2292 pkt_dev->queue_map_min + 1)
2293 + pkt_dev->queue_map_min;
2294 } else {
2295 t = pkt_dev->cur_queue_map + 1;
2296 if (t > pkt_dev->queue_map_max)
2297 t = pkt_dev->queue_map_min;
2298 }
2299 pkt_dev->cur_queue_map = t;
2300 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002301 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 -07002302}
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304/* Increment/randomize headers according to flags and current values
2305 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2306 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2308{
2309 __u32 imn;
2310 __u32 imx;
2311 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002313 if (pkt_dev->cflows)
2314 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002317 if (pkt_dev->src_mac_count > 1) {
2318 __u32 mc;
2319 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Luiz Capitulino222f1802006-03-20 22:16:13 -08002321 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002322 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 else {
2324 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002325 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002326 pkt_dev->src_mac_count)
2327 pkt_dev->cur_src_mac_offset = 0;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Luiz Capitulino222f1802006-03-20 22:16:13 -08002330 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2331 pkt_dev->hh[11] = tmp;
2332 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2333 pkt_dev->hh[10] = tmp;
2334 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2335 pkt_dev->hh[9] = tmp;
2336 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2337 pkt_dev->hh[8] = tmp;
2338 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2339 pkt_dev->hh[7] = tmp;
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Luiz Capitulino222f1802006-03-20 22:16:13 -08002342 /* Deal with Destination MAC */
2343 if (pkt_dev->dst_mac_count > 1) {
2344 __u32 mc;
2345 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Luiz Capitulino222f1802006-03-20 22:16:13 -08002347 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002348 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Luiz Capitulino222f1802006-03-20 22:16:13 -08002350 else {
2351 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002352 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002353 pkt_dev->dst_mac_count) {
2354 pkt_dev->cur_dst_mac_offset = 0;
2355 }
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Luiz Capitulino222f1802006-03-20 22:16:13 -08002358 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2359 pkt_dev->hh[5] = tmp;
2360 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2361 pkt_dev->hh[4] = tmp;
2362 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2363 pkt_dev->hh[3] = tmp;
2364 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2365 pkt_dev->hh[2] = tmp;
2366 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2367 pkt_dev->hh[1] = tmp;
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002370 if (pkt_dev->flags & F_MPLS_RND) {
2371 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002372 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002373 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2374 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002375 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002376 htonl(0x000fffff));
2377 }
2378
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002379 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002380 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002381 }
2382
2383 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002384 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002385 }
2386
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2388 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002389 pkt_dev->cur_udp_src = random32() %
2390 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2391 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Luiz Capitulino222f1802006-03-20 22:16:13 -08002393 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 pkt_dev->cur_udp_src++;
2395 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2396 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002397 }
2398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2401 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002402 pkt_dev->cur_udp_dst = random32() %
2403 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2404 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002405 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002407 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002409 }
2410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 if (!(pkt_dev->flags & F_IPV6)) {
2413
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002414 imn = ntohl(pkt_dev->saddr_min);
2415 imx = ntohl(pkt_dev->saddr_max);
2416 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002418 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002419 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 else {
2421 t = ntohl(pkt_dev->cur_saddr);
2422 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002423 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
2427 pkt_dev->cur_saddr = htonl(t);
2428 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002429
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002430 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2432 } else {
Al Viro252e3342006-11-14 20:48:11 -08002433 imn = ntohl(pkt_dev->daddr_min);
2434 imx = ntohl(pkt_dev->daddr_max);
2435 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002437 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (pkt_dev->flags & F_IPDST_RND) {
2439
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002440 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002441 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Joe Perches21cf2252007-12-16 13:44:00 -08002443 while (ipv4_is_loopback(s) ||
2444 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002445 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002446 ipv4_is_zeronet(s) ||
2447 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002448 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002449 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
Al Viro252e3342006-11-14 20:48:11 -08002451 pkt_dev->cur_daddr = s;
2452 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 t = ntohl(pkt_dev->cur_daddr);
2454 t++;
2455 if (t > imx) {
2456 t = imn;
2457 }
2458 pkt_dev->cur_daddr = htonl(t);
2459 }
2460 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002461 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002462 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002463 pkt_dev->flows[flow].cur_daddr =
2464 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002465#ifdef CONFIG_XFRM
2466 if (pkt_dev->flags & F_IPSEC_ON)
2467 get_ipsec_sa(pkt_dev, flow);
2468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 pkt_dev->nflows++;
2470 }
2471 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002472 } else { /* IPV6 * */
2473
2474 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2475 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2476 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2477 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 else {
2479 int i;
2480
2481 /* Only random destinations yet */
2482
Luiz Capitulino222f1802006-03-20 22:16:13 -08002483 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002485 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002486 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2487 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491
Luiz Capitulino222f1802006-03-20 22:16:13 -08002492 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2493 __u32 t;
2494 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002495 t = random32() %
2496 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2497 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002498 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002500 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002502 }
2503 pkt_dev->cur_pkt_size = t;
2504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002506 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 pkt_dev->flows[flow].count++;
2509}
2510
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002511
2512#ifdef CONFIG_XFRM
2513static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2514{
2515 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2516 int err = 0;
2517 struct iphdr *iph;
2518
2519 if (!x)
2520 return 0;
2521 /* XXX: we dont support tunnel mode for now until
2522 * we resolve the dst issue */
2523 if (x->props.mode != XFRM_MODE_TRANSPORT)
2524 return 0;
2525
2526 spin_lock(&x->lock);
2527 iph = ip_hdr(skb);
2528
Herbert Xu13996372007-10-17 21:35:51 -07002529 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002530 if (err)
2531 goto error;
2532 err = x->type->output(x, skb);
2533 if (err)
2534 goto error;
2535
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002536 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002537 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002538error:
2539 spin_unlock(&x->lock);
2540 return err;
2541}
2542
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002543static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002544{
2545 if (pkt_dev->cflows) {
2546 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002547 int i;
2548 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002549 struct xfrm_state *x = pkt_dev->flows[i].x;
2550 if (x) {
2551 xfrm_state_put(x);
2552 pkt_dev->flows[i].x = NULL;
2553 }
2554 }
2555 }
2556}
2557
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002558static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002559 struct sk_buff *skb, __be16 protocol)
2560{
2561 if (pkt_dev->flags & F_IPSEC_ON) {
2562 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2563 int nhead = 0;
2564 if (x) {
2565 int ret;
2566 __u8 *eth;
2567 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002568 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002569 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2570 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002571 pr_err("Error expanding ipsec packet %d\n",
2572 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002573 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002574 }
2575 }
2576
2577 /* ipsec is not expecting ll header */
2578 skb_pull(skb, ETH_HLEN);
2579 ret = pktgen_output_ipsec(skb, pkt_dev);
2580 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002581 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002582 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002583 }
2584 /* restore ll */
2585 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2586 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002587 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002588 }
2589 }
2590 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002591err:
2592 kfree_skb(skb);
2593 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002594}
2595#endif
2596
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002597static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2598{
2599 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002600 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002601 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002602
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002603 mpls--;
2604 *mpls |= MPLS_STACK_BOTTOM;
2605}
2606
Al Viro0f37c602006-11-03 03:49:56 -08002607static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2608 unsigned int prio)
2609{
2610 return htons(id | (cfi << 12) | (prio << 13));
2611}
2612
Eric Dumazet26ad7872011-01-25 13:26:05 -08002613static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2614 int datalen)
2615{
2616 struct timeval timestamp;
2617 struct pktgen_hdr *pgh;
2618
2619 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2620 datalen -= sizeof(*pgh);
2621
2622 if (pkt_dev->nfrags <= 0) {
2623 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2624 memset(pgh + 1, 0, datalen);
2625 } else {
2626 int frags = pkt_dev->nfrags;
2627 int i, len;
2628
2629
2630 if (frags > MAX_SKB_FRAGS)
2631 frags = MAX_SKB_FRAGS;
2632 len = datalen - frags * PAGE_SIZE;
2633 if (len > 0) {
2634 memset(skb_put(skb, len), 0, len);
2635 datalen = frags * PAGE_SIZE;
2636 }
2637
2638 i = 0;
2639 while (datalen > 0) {
2640 if (unlikely(!pkt_dev->page)) {
2641 int node = numa_node_id();
2642
2643 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2644 node = pkt_dev->node;
2645 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2646 if (!pkt_dev->page)
2647 break;
2648 }
2649 skb_shinfo(skb)->frags[i].page = pkt_dev->page;
2650 get_page(pkt_dev->page);
2651 skb_shinfo(skb)->frags[i].page_offset = 0;
2652 skb_shinfo(skb)->frags[i].size =
2653 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2654 datalen -= skb_shinfo(skb)->frags[i].size;
2655 skb->len += skb_shinfo(skb)->frags[i].size;
2656 skb->data_len += skb_shinfo(skb)->frags[i].size;
2657 i++;
2658 skb_shinfo(skb)->nr_frags = i;
2659 }
2660
2661 while (i < frags) {
2662 int rem;
2663
2664 if (i == 0)
2665 break;
2666
2667 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2668 if (rem == 0)
2669 break;
2670
2671 skb_shinfo(skb)->frags[i - 1].size -= rem;
2672
2673 skb_shinfo(skb)->frags[i] =
2674 skb_shinfo(skb)->frags[i - 1];
2675 get_page(skb_shinfo(skb)->frags[i].page);
2676 skb_shinfo(skb)->frags[i].page =
2677 skb_shinfo(skb)->frags[i - 1].page;
2678 skb_shinfo(skb)->frags[i].page_offset +=
2679 skb_shinfo(skb)->frags[i - 1].size;
2680 skb_shinfo(skb)->frags[i].size = rem;
2681 i++;
2682 skb_shinfo(skb)->nr_frags = i;
2683 }
2684 }
2685
2686 /* Stamp the time, and sequence number,
2687 * convert them to network byte order
2688 */
2689 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2690 pgh->seq_num = htonl(pkt_dev->seq_num);
2691
2692 do_gettimeofday(&timestamp);
2693 pgh->tv_sec = htonl(timestamp.tv_sec);
2694 pgh->tv_usec = htonl(timestamp.tv_usec);
2695}
2696
Luiz Capitulino222f1802006-03-20 22:16:13 -08002697static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2698 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct sk_buff *skb = NULL;
2701 __u8 *eth;
2702 struct udphdr *udph;
2703 int datalen, iplen;
2704 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002705 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002706 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002707 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2708 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2709 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2710 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002711 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002712
2713 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002714 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002715
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002716 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002717 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002718
Robert Olsson64053be2005-06-26 15:27:10 -07002719 /* Update any of the values, used when we're incrementing various
2720 * fields.
2721 */
2722 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002723 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002724
David S. Miller7ac54592006-01-18 14:19:10 -08002725 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002726
2727 if (pkt_dev->flags & F_NODE) {
2728 int node;
2729
2730 if (pkt_dev->node >= 0)
2731 node = pkt_dev->node;
2732 else
2733 node = numa_node_id();
2734
2735 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2736 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2737 if (likely(skb)) {
2738 skb_reserve(skb, NET_SKB_PAD);
2739 skb->dev = odev;
2740 }
2741 }
2742 else
2743 skb = __netdev_alloc_skb(odev,
2744 pkt_dev->cur_pkt_size + 64
2745 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 if (!skb) {
2748 sprintf(pkt_dev->result, "No memory");
2749 return NULL;
2750 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002751 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
David S. Miller7ac54592006-01-18 14:19:10 -08002753 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 /* Reserve for ethernet and IP header */
2756 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002757 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2758 if (pkt_dev->nr_labels)
2759 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002760
2761 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002762 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002763 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002764 *svlan_tci = build_tci(pkt_dev->svlan_id,
2765 pkt_dev->svlan_cfi,
2766 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002767 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002768 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002769 }
2770 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002771 *vlan_tci = build_tci(pkt_dev->vlan_id,
2772 pkt_dev->vlan_cfi,
2773 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002774 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002775 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002776 }
2777
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002778 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002779 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002780 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002781 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002782 skb->priority = pkt_dev->skb_priority;
2783
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002784 iph = ip_hdr(skb);
2785 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002788 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002790 /* Eth + IPh + UDPh + mpls */
2791 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002792 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002793 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 udph->source = htons(pkt_dev->cur_udp_src);
2797 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002798 udph->len = htons(datalen + 8); /* DATA + udphdr */
2799 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 iph->ihl = 5;
2802 iph->version = 4;
2803 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002804 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002805 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 iph->saddr = pkt_dev->cur_saddr;
2807 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002808 iph->id = htons(pkt_dev->ip_id);
2809 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 iph->frag_off = 0;
2811 iplen = 20 + 8 + datalen;
2812 iph->tot_len = htons(iplen);
2813 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002814 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002815 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002816 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002817 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 skb->dev = odev;
2819 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002820 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002821
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002822#ifdef CONFIG_XFRM
2823 if (!process_ipsec(pkt_dev, skb, protocol))
2824 return NULL;
2825#endif
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return skb;
2828}
2829
2830/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002831 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2833 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002834 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 * Should be candidate for net/ipv4/utils.c
2836 * --ro
2837 */
2838
Luiz Capitulino222f1802006-03-20 22:16:13 -08002839static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
2841 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002842 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 unsigned long u;
2844 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002845 unsigned int prefixlen = 0;
2846 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002847 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002848 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Luiz Capitulino222f1802006-03-20 22:16:13 -08002850 for (i = 0; i < 16; i++)
2851 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853 for (;;) {
2854 if (*s == ':') {
2855 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002856 if (s[1] == ':') { /* Found "::", skip to part 2 */
2857 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 len++;
2859 break;
2860 }
2861 s++;
2862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002864 u = simple_strtoul(s, &pos, 16);
2865 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002866 if (!i)
2867 return 0;
2868 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 /* the last 4 bytes may be written as IPv4 address */
2871
2872 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2874 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
2876 ip[prefixlen++] = (u >> 8);
2877 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002878 s += i;
2879 len += i;
2880 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return len;
2882 }
2883
2884/* part 2, after "::" */
2885 for (;;) {
2886 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002887 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 break;
2889 s++;
2890 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002891 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002893
2894 u = simple_strtol(s, &pos, 16);
2895 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002897 if (*s)
2898 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 break;
2900 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002901 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002903 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2904 sizeof(tmp));
2905 suffixlen += 4;
2906 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 break;
2908 }
2909 suffix[suffixlen++] = (u >> 8);
2910 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002911 s += i;
2912 len += i;
2913 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 break;
2915 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002916 for (i = 0; i < suffixlen; i++)
2917 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return len;
2919}
2920
Luiz Capitulino222f1802006-03-20 22:16:13 -08002921static char tohex(char hexdigit)
2922{
2923 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
Luiz Capitulino222f1802006-03-20 22:16:13 -08002926static int fmt_xlong(char *s, unsigned int i)
2927{
2928 char *bak = s;
2929 *s = tohex((i >> 12) & 0xf);
2930 if (s != bak || *s != '0')
2931 ++s;
2932 *s = tohex((i >> 8) & 0xf);
2933 if (s != bak || *s != '0')
2934 ++s;
2935 *s = tohex((i >> 4) & 0xf);
2936 if (s != bak || *s != '0')
2937 ++s;
2938 *s = tohex(i & 0xf);
2939 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Luiz Capitulino222f1802006-03-20 22:16:13 -08002942static unsigned int fmt_ip6(char *s, const char ip[16])
2943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 unsigned int len;
2945 unsigned int i;
2946 unsigned int temp;
2947 unsigned int compressing;
2948 int j;
2949
Luiz Capitulino222f1802006-03-20 22:16:13 -08002950 len = 0;
2951 compressing = 0;
2952 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002955 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2956 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2957 temp = strlen(s);
2958 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
2960#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002961 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2962 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (temp == 0) {
2964 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002965 compressing = 1;
2966 if (j == 0) {
2967 *s++ = ':';
2968 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970 }
2971 } else {
2972 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002973 compressing = 0;
2974 *s++ = ':';
2975 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002977 i = fmt_xlong(s, temp);
2978 len += i;
2979 s += i;
2980 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 *s++ = ':';
2982 ++len;
2983 }
2984 }
2985 }
2986 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002987 *s++ = ':';
2988 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002990 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return len;
2992}
2993
Luiz Capitulino222f1802006-03-20 22:16:13 -08002994static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2995 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
2997 struct sk_buff *skb = NULL;
2998 __u8 *eth;
2999 struct udphdr *udph;
3000 int datalen;
3001 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003002 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003003 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003004 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
3005 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
3006 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
3007 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003008 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003009
3010 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003011 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07003012
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003013 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003014 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003015
Robert Olsson64053be2005-06-26 15:27:10 -07003016 /* Update any of the values, used when we're incrementing various
3017 * fields.
3018 */
3019 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00003020 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07003021
Stephen Hemmingere4707572009-08-27 13:55:13 +00003022 skb = __netdev_alloc_skb(odev,
3023 pkt_dev->cur_pkt_size + 64
3024 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (!skb) {
3026 sprintf(pkt_dev->result, "No memory");
3027 return NULL;
3028 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00003029 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 skb_reserve(skb, 16);
3032
3033 /* Reserve for ethernet and IP header */
3034 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003035 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
3036 if (pkt_dev->nr_labels)
3037 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003038
3039 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07003040 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003041 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003042 *svlan_tci = build_tci(pkt_dev->svlan_id,
3043 pkt_dev->svlan_cfi,
3044 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003045 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003046 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003047 }
3048 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003049 *vlan_tci = build_tci(pkt_dev->vlan_id,
3050 pkt_dev->vlan_cfi,
3051 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003052 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003053 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003054 }
3055
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003056 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003057 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003058 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003059 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00003060 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003061 iph = ipv6_hdr(skb);
3062 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003065 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07003066
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003067 /* Eth + IPh + UDPh + mpls */
3068 datalen = pkt_dev->cur_pkt_size - 14 -
3069 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003070 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Luiz Capitulino222f1802006-03-20 22:16:13 -08003072 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 datalen = sizeof(struct pktgen_hdr);
3074 if (net_ratelimit())
Joe Perchesf9467ea2010-06-21 12:29:14 +00003075 pr_info("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
3077
3078 udph->source = htons(pkt_dev->cur_udp_src);
3079 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003080 udph->len = htons(datalen + sizeof(struct udphdr));
3081 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003083 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003085 if (pkt_dev->traffic_class) {
3086 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08003087 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003088 }
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 iph->hop_limit = 32;
3091
3092 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3093 iph->nexthdr = IPPROTO_UDP;
3094
3095 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3096 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3097
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003098 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003099 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003100 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 skb->dev = odev;
3102 skb->pkt_type = PACKET_HOST;
3103
Eric Dumazet26ad7872011-01-25 13:26:05 -08003104 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return skb;
3107}
3108
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003109static struct sk_buff *fill_packet(struct net_device *odev,
3110 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003112 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return fill_packet_ipv6(odev, pkt_dev);
3114 else
3115 return fill_packet_ipv4(odev, pkt_dev);
3116}
3117
Luiz Capitulino222f1802006-03-20 22:16:13 -08003118static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003120 pkt_dev->seq_num = 1;
3121 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123 pkt_dev->tx_bytes = 0;
3124 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127/* Set up structure for sending pkts, clear counters */
3128
3129static void pktgen_run(struct pktgen_thread *t)
3130{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003131 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 int started = 0;
3133
Joe Perchesf9467ea2010-06-21 12:29:14 +00003134 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003137 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 /*
3140 * setup odev and create initial packet.
3141 */
3142 pktgen_setup_inject(pkt_dev);
3143
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003146 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003148 pkt_dev->started_at =
3149 pkt_dev->next_tx = ktime_now();
3150
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003151 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 strcpy(pkt_dev->result, "Starting");
3154 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003155 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 strcpy(pkt_dev->result, "Error starting");
3157 }
3158 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 if (started)
3160 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
3163static void pktgen_stop_all_threads_ifs(void)
3164{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003165 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Joe Perchesf9467ea2010-06-21 12:29:14 +00003167 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003169 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003170
3171 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003172 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003173
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003174 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
Stephen Hemminger648fda72009-08-27 13:55:07 +00003177static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003179 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003181 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003182 if (pkt_dev->running)
3183 return 1;
3184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Luiz Capitulino222f1802006-03-20 22:16:13 -08003187static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003189 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Luiz Capitulino222f1802006-03-20 22:16:13 -08003191 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Luiz Capitulino222f1802006-03-20 22:16:13 -08003193 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Luiz Capitulino222f1802006-03-20 22:16:13 -08003197 if (signal_pending(current))
3198 goto signal;
3199 if_lock(t);
3200 }
3201 if_unlock(t);
3202 return 1;
3203signal:
3204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
3207static int pktgen_wait_all_threads_run(void)
3208{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003209 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003211
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003212 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003213
3214 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003216 if (sig == 0)
3217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003219
3220 if (sig == 0)
3221 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003223
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003224 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return sig;
3226}
3227
3228static void pktgen_run_all_threads(void)
3229{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003230 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Joe Perchesf9467ea2010-06-21 12:29:14 +00003232 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003234 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003236 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003238
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003239 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003241 /* Propagate thread->control */
3242 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 pktgen_wait_all_threads_run();
3245}
3246
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003247static void pktgen_reset_all_threads(void)
3248{
3249 struct pktgen_thread *t;
3250
Joe Perchesf9467ea2010-06-21 12:29:14 +00003251 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003252
3253 mutex_lock(&pktgen_thread_lock);
3254
3255 list_for_each_entry(t, &pktgen_threads, th_list)
3256 t->control |= (T_REMDEVALL);
3257
3258 mutex_unlock(&pktgen_thread_lock);
3259
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003260 /* Propagate thread->control */
3261 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003262
3263 pktgen_wait_all_threads_run();
3264}
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3267{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003268 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003269 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003270 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3271 pkt_dev->started_at);
3272 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003274 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3275 (unsigned long long)ktime_to_us(elapsed),
3276 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3277 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003278 (unsigned long long)pkt_dev->sofar,
3279 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003281 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3282 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Luiz Capitulino222f1802006-03-20 22:16:13 -08003284 bps = pps * 8 * pkt_dev->cur_pkt_size;
3285
3286 mbps = bps;
3287 do_div(mbps, 1000000);
3288 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3289 (unsigned long long)pps,
3290 (unsigned long long)mbps,
3291 (unsigned long long)bps,
3292 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003296static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003298 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003299
Luiz Capitulino222f1802006-03-20 22:16:13 -08003300 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003301 pr_warning("interface: %s is already stopped\n",
3302 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003303 return -EINVAL;
3304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003306 kfree_skb(pkt_dev->skb);
3307 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003308 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003309 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003311 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Luiz Capitulino222f1802006-03-20 22:16:13 -08003313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Luiz Capitulino222f1802006-03-20 22:16:13 -08003316static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003318 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 if_lock(t);
3321
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003322 list_for_each_entry(pkt_dev, &t->if_list, list) {
3323 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003324 continue;
3325 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003326 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003327 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003328 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
3330 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003331 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Luiz Capitulino222f1802006-03-20 22:16:13 -08003334static void pktgen_stop(struct pktgen_thread *t)
3335{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003336 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Joe Perchesf9467ea2010-06-21 12:29:14 +00003338 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Luiz Capitulino222f1802006-03-20 22:16:13 -08003340 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003342 list_for_each_entry(pkt_dev, &t->if_list, list) {
3343 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Luiz Capitulino222f1802006-03-20 22:16:13 -08003346 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003349/*
3350 * one of our devices needs to be removed - find it
3351 * and remove it
3352 */
3353static void pktgen_rem_one_if(struct pktgen_thread *t)
3354{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003355 struct list_head *q, *n;
3356 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003357
Joe Perchesf9467ea2010-06-21 12:29:14 +00003358 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003359
3360 if_lock(t);
3361
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003362 list_for_each_safe(q, n, &t->if_list) {
3363 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003364
Luiz Capitulino222f1802006-03-20 22:16:13 -08003365 if (!cur->removal_mark)
3366 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003367
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003368 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003369 cur->skb = NULL;
3370
3371 pktgen_remove_device(t, cur);
3372
3373 break;
3374 }
3375
3376 if_unlock(t);
3377}
3378
Luiz Capitulino222f1802006-03-20 22:16:13 -08003379static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003381 struct list_head *q, *n;
3382 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003383
Joe Perchesf9467ea2010-06-21 12:29:14 +00003384 func_enter();
3385
Luiz Capitulino222f1802006-03-20 22:16:13 -08003386 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003387
Luiz Capitulino222f1802006-03-20 22:16:13 -08003388 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003390 list_for_each_safe(q, n, &t->if_list) {
3391 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003392
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003393 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003394 cur->skb = NULL;
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 pktgen_remove_device(t, cur);
3397 }
3398
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Luiz Capitulino222f1802006-03-20 22:16:13 -08003402static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003404 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
David S. Milleree74baa2007-01-01 20:51:53 -08003406 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
3409
Stephen Hemmingeref879792009-09-22 19:41:43 +00003410static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003411{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003412 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003413 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003414 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003415}
3416
Stephen Hemmingeref879792009-09-22 19:41:43 +00003417static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3418{
3419 ktime_t idle_start = ktime_now();
3420
3421 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3422 if (signal_pending(current))
3423 break;
3424
3425 if (need_resched())
3426 pktgen_resched(pkt_dev);
3427 else
3428 cpu_relax();
3429 }
3430 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3431}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003432
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003433static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
Stephen Hemminger00829822008-11-20 20:14:53 -08003435 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003436 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003437 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003438 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003439 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 int ret;
3441
Stephen Hemmingeref879792009-09-22 19:41:43 +00003442 /* If device is offline, then don't send */
3443 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3444 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003445 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003447
Stephen Hemmingeref879792009-09-22 19:41:43 +00003448 /* This is max DELAY, this has special meaning of
3449 * "never transmit"
3450 */
3451 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3452 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3453 return;
3454 }
3455
3456 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003457 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3458 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3459 /* build a new pkt */
3460 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003461
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003462 pkt_dev->skb = fill_packet(odev, pkt_dev);
3463 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003464 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003465 schedule();
3466 pkt_dev->clone_count--; /* back out increment, OOM */
3467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003469 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003470 pkt_dev->allocated_skbs++;
3471 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003473
Stephen Hemmingeref879792009-09-22 19:41:43 +00003474 if (pkt_dev->delay && pkt_dev->last_ok)
3475 spin(pkt_dev, pkt_dev->next_tx);
3476
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003477 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3478 txq = netdev_get_tx_queue(odev, queue_map);
3479
3480 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003481
Eric Dumazet5a0d2262010-11-23 10:42:02 +00003482 if (unlikely(netif_tx_queue_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003483 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003484 pkt_dev->last_ok = 0;
3485 goto unlock;
3486 }
3487 atomic_inc(&(pkt_dev->skb->users));
3488 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Stephen Hemmingeref879792009-09-22 19:41:43 +00003490 switch (ret) {
3491 case NETDEV_TX_OK:
3492 txq_trans_update(txq);
3493 pkt_dev->last_ok = 1;
3494 pkt_dev->sofar++;
3495 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003496 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003497 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003498 case NET_XMIT_DROP:
3499 case NET_XMIT_CN:
3500 case NET_XMIT_POLICED:
3501 /* skb has been consumed */
3502 pkt_dev->errors++;
3503 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003504 default: /* Drivers are not supposed to return other values! */
3505 if (net_ratelimit())
Joe Perches58231182010-12-20 10:30:06 -08003506 pr_info("%s xmit error: %d\n", pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003507 pkt_dev->errors++;
3508 /* fallthru */
3509 case NETDEV_TX_LOCKED:
3510 case NETDEV_TX_BUSY:
3511 /* Retry it next time */
3512 atomic_dec(&(pkt_dev->skb->users));
3513 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003514 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003515unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003516 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 /* If pkt_dev->count is zero, then run forever */
3519 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003520 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 /* Done with this */
3523 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003524 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003527/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 * Main loop of the thread goes here
3529 */
3530
David S. Milleree74baa2007-01-01 20:51:53 -08003531static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003534 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003535 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
David S. Milleree74baa2007-01-01 20:51:53 -08003538 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003541 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Joe Perchesf9467ea2010-06-21 12:29:14 +00003543 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
David S. Milleree74baa2007-01-01 20:51:53 -08003545 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003546
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003547 set_freezable();
3548
David S. Milleree74baa2007-01-01 20:51:53 -08003549 while (!kthread_should_stop()) {
3550 pkt_dev = next_to_run(t);
3551
Stephen Hemmingeref879792009-09-22 19:41:43 +00003552 if (unlikely(!pkt_dev && t->control == 0)) {
Eric Dumazet551eaff2010-11-21 10:26:44 -08003553 if (pktgen_exiting)
3554 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003555 wait_event_interruptible_timeout(t->queue,
3556 t->control != 0,
3557 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003558 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003559 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003560 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 __set_current_state(TASK_RUNNING);
3563
Stephen Hemmingeref879792009-09-22 19:41:43 +00003564 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 pktgen_xmit(pkt_dev);
3566
Stephen Hemmingeref879792009-09-22 19:41:43 +00003567 if (need_resched())
3568 pktgen_resched(pkt_dev);
3569 else
3570 cpu_relax();
3571 }
3572
Luiz Capitulino222f1802006-03-20 22:16:13 -08003573 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 pktgen_stop(t);
3575 t->control &= ~(T_STOP);
3576 }
3577
Luiz Capitulino222f1802006-03-20 22:16:13 -08003578 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 pktgen_run(t);
3580 t->control &= ~(T_RUN);
3581 }
3582
Luiz Capitulino222f1802006-03-20 22:16:13 -08003583 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003585 t->control &= ~(T_REMDEVALL);
3586 }
3587
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003589 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 t->control &= ~(T_REMDEV);
3591 }
3592
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003593 try_to_freeze();
3594
David S. Milleree74baa2007-01-01 20:51:53 -08003595 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Joe Perchesf9467ea2010-06-21 12:29:14 +00003598 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003599 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Joe Perchesf9467ea2010-06-21 12:29:14 +00003601 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003602 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Joe Perchesf9467ea2010-06-21 12:29:14 +00003604 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003605 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003606
Eric Dumazet551eaff2010-11-21 10:26:44 -08003607 /* Wait for kthread_stop */
3608 while (!kthread_should_stop()) {
3609 set_current_state(TASK_INTERRUPTIBLE);
3610 schedule();
3611 }
3612 __set_current_state(TASK_RUNNING);
3613
David S. Milleree74baa2007-01-01 20:51:53 -08003614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
Luiz Capitulino222f1802006-03-20 22:16:13 -08003617static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003618 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003620 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003621 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Eric Dumazet3e984842009-11-24 14:50:53 -08003623 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003624 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003625 if (strncmp(p->odevname, ifname, len) == 0) {
3626 if (p->odevname[len]) {
3627 if (exact || p->odevname[len] != '@')
3628 continue;
3629 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003630 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003631 break;
3632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Luiz Capitulino222f1802006-03-20 22:16:13 -08003634 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003635 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003636 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
3638
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003639/*
3640 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 */
3642
Luiz Capitulino222f1802006-03-20 22:16:13 -08003643static int add_dev_to_thread(struct pktgen_thread *t,
3644 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
3646 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Luiz Capitulino222f1802006-03-20 22:16:13 -08003648 if_lock(t);
3649
3650 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003651 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003652 rv = -EBUSY;
3653 goto out;
3654 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003655
3656 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003657 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 pkt_dev->running = 0;
3659
Luiz Capitulino222f1802006-03-20 22:16:13 -08003660out:
3661 if_unlock(t);
3662 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
3665/* Called under thread lock */
3666
Luiz Capitulino222f1802006-03-20 22:16:13 -08003667static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003669 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003670 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003671 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 /* We don't allow a device to be on several threads */
3674
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003675 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3676 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003677 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003678 return -EBUSY;
3679 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003680
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003681 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003682 if (!pkt_dev)
3683 return -ENOMEM;
3684
Eric Dumazet593f63b2009-11-23 01:44:37 +00003685 strcpy(pkt_dev->odevname, ifname);
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003686 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3687 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003688 if (pkt_dev->flows == NULL) {
3689 kfree(pkt_dev);
3690 return -ENOMEM;
3691 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003692 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003693
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003694 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003695 pkt_dev->min_pkt_size = ETH_ZLEN;
3696 pkt_dev->max_pkt_size = ETH_ZLEN;
3697 pkt_dev->nfrags = 0;
3698 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003699 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003700 pkt_dev->count = pg_count_d;
3701 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003702 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003703 pkt_dev->udp_src_max = 9;
3704 pkt_dev->udp_dst_min = 9;
3705 pkt_dev->udp_dst_max = 9;
3706
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003707 pkt_dev->vlan_p = 0;
3708 pkt_dev->vlan_cfi = 0;
3709 pkt_dev->vlan_id = 0xffff;
3710 pkt_dev->svlan_p = 0;
3711 pkt_dev->svlan_cfi = 0;
3712 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003713 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003714
Stephen Hemminger39df2322007-03-04 16:11:51 -08003715 err = pktgen_setup_dev(pkt_dev, ifname);
3716 if (err)
3717 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003718
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003719 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3720 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003721 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003722 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003723 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003724 err = -EINVAL;
3725 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003726 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003727#ifdef CONFIG_XFRM
3728 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3729 pkt_dev->ipsproto = IPPROTO_ESP;
3730#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003731
3732 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003733out2:
3734 dev_put(pkt_dev->odev);
3735out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003736#ifdef CONFIG_XFRM
3737 free_SAs(pkt_dev);
3738#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003739 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003740 kfree(pkt_dev);
3741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
David S. Milleree74baa2007-01-01 20:51:53 -08003744static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003746 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003747 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003748 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003750 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3751 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003752 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003753 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003754 return -ENOMEM;
3755 }
3756
Luiz Capitulino222f1802006-03-20 22:16:13 -08003757 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003759
David S. Milleree74baa2007-01-01 20:51:53 -08003760 INIT_LIST_HEAD(&t->if_list);
3761
3762 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003763 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003764
3765 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3766 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003767 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003768 list_del(&t->th_list);
3769 kfree(t);
3770 return PTR_ERR(p);
3771 }
3772 kthread_bind(p, cpu);
3773 t->tsk = p;
3774
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003775 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3776 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003777 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003778 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003779 PG_PROC_DIR, t->tsk->comm);
3780 kthread_stop(p);
3781 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003782 kfree(t);
3783 return -EINVAL;
3784 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003785
David S. Milleree74baa2007-01-01 20:51:53 -08003786 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003787 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 return 0;
3790}
3791
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003792/*
3793 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003795static void _rem_dev_from_if_list(struct pktgen_thread *t,
3796 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003798 struct list_head *q, *n;
3799 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003801 list_for_each_safe(q, n, &t->if_list) {
3802 p = list_entry(q, struct pktgen_dev, list);
3803 if (p == pkt_dev)
3804 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
3806}
3807
Luiz Capitulino222f1802006-03-20 22:16:13 -08003808static int pktgen_remove_device(struct pktgen_thread *t,
3809 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
3811
Joe Perchesf9467ea2010-06-21 12:29:14 +00003812 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Luiz Capitulino222f1802006-03-20 22:16:13 -08003814 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003815 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003816 pktgen_stop_device(pkt_dev);
3817 }
3818
3819 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 if (pkt_dev->odev) {
3822 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003823 pkt_dev->odev = NULL;
3824 }
3825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 /* And update the thread if_list */
3827
3828 _rem_dev_from_if_list(t, pkt_dev);
3829
Stephen Hemminger39df2322007-03-04 16:11:51 -08003830 if (pkt_dev->entry)
3831 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003833#ifdef CONFIG_XFRM
3834 free_SAs(pkt_dev);
3835#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003836 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003837 if (pkt_dev->page)
3838 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841}
3842
Luiz Capitulino222f1802006-03-20 22:16:13 -08003843static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844{
3845 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003846 struct proc_dir_entry *pe;
3847
Joe Perchesf9467ea2010-06-21 12:29:14 +00003848 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003850 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003851 if (!pg_proc_dir)
3852 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Wang Chen25296d52008-02-28 14:11:49 -08003854 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003855 if (pe == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003856 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003857 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003858 return -EINVAL;
3859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 /* Register us to receive netdevice events */
3862 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003863
John Hawkes670c02c2005-10-13 09:30:31 -07003864 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003865 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
David S. Milleree74baa2007-01-01 20:51:53 -08003867 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003868 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00003869 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3870 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003871 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003872
3873 if (list_empty(&pktgen_threads)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003874 pr_err("ERROR: Initialization failed for all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003875 unregister_netdevice_notifier(&pktgen_notifier_block);
3876 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003877 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003878 return -ENODEV;
3879 }
3880
Luiz Capitulino222f1802006-03-20 22:16:13 -08003881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882}
3883
3884static void __exit pg_cleanup(void)
3885{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003886 struct pktgen_thread *t;
3887 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Luiz Capitulino222f1802006-03-20 22:16:13 -08003889 /* Stop all interfaces & threads */
Eric Dumazet551eaff2010-11-21 10:26:44 -08003890 pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003892 list_for_each_safe(q, n, &pktgen_threads) {
3893 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003894 kthread_stop(t->tsk);
3895 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Luiz Capitulino222f1802006-03-20 22:16:13 -08003898 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 unregister_netdevice_notifier(&pktgen_notifier_block);
3900
Luiz Capitulino222f1802006-03-20 22:16:13 -08003901 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003902 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003903 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904}
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906module_init(pg_init);
3907module_exit(pg_cleanup);
3908
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003909MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910MODULE_DESCRIPTION("Packet Generator tool");
3911MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003912MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003914MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003916MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003918MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003920MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");