blob: aeeece72b72fa25d29dfd3162a2f9c3a8ef6fcbd [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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-10-14 15:42:33 -0700516static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Stephen Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-10-14 15:42:33 -07001777 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 else
Stephen Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-10-14 15:42:33 -07001783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Stephen Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-10-14 15:42:33 -07001804 return len;
1805
Paul Gortmakerd6182222010-10-18 12:14:44 +00001806 i = len;
Stephen Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-10-14 15:42:33 -07001822 return len;
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 i += len;
1825
Stephen Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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 Hemmingerd50a6b52005-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) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002623 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002624 } else {
2625 int frags = pkt_dev->nfrags;
2626 int i, len;
2627
2628
2629 if (frags > MAX_SKB_FRAGS)
2630 frags = MAX_SKB_FRAGS;
2631 len = datalen - frags * PAGE_SIZE;
2632 if (len > 0) {
2633 memset(skb_put(skb, len), 0, len);
2634 datalen = frags * PAGE_SIZE;
2635 }
2636
2637 i = 0;
2638 while (datalen > 0) {
2639 if (unlikely(!pkt_dev->page)) {
2640 int node = numa_node_id();
2641
2642 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2643 node = pkt_dev->node;
2644 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2645 if (!pkt_dev->page)
2646 break;
2647 }
2648 skb_shinfo(skb)->frags[i].page = pkt_dev->page;
2649 get_page(pkt_dev->page);
2650 skb_shinfo(skb)->frags[i].page_offset = 0;
2651 skb_shinfo(skb)->frags[i].size =
2652 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2653 datalen -= skb_shinfo(skb)->frags[i].size;
2654 skb->len += skb_shinfo(skb)->frags[i].size;
2655 skb->data_len += skb_shinfo(skb)->frags[i].size;
2656 i++;
2657 skb_shinfo(skb)->nr_frags = i;
2658 }
2659
2660 while (i < frags) {
2661 int rem;
2662
2663 if (i == 0)
2664 break;
2665
2666 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2667 if (rem == 0)
2668 break;
2669
2670 skb_shinfo(skb)->frags[i - 1].size -= rem;
2671
2672 skb_shinfo(skb)->frags[i] =
2673 skb_shinfo(skb)->frags[i - 1];
2674 get_page(skb_shinfo(skb)->frags[i].page);
2675 skb_shinfo(skb)->frags[i].page =
2676 skb_shinfo(skb)->frags[i - 1].page;
2677 skb_shinfo(skb)->frags[i].page_offset +=
2678 skb_shinfo(skb)->frags[i - 1].size;
2679 skb_shinfo(skb)->frags[i].size = rem;
2680 i++;
2681 skb_shinfo(skb)->nr_frags = i;
2682 }
2683 }
2684
2685 /* Stamp the time, and sequence number,
2686 * convert them to network byte order
2687 */
2688 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2689 pgh->seq_num = htonl(pkt_dev->seq_num);
2690
2691 do_gettimeofday(&timestamp);
2692 pgh->tv_sec = htonl(timestamp.tv_sec);
2693 pgh->tv_usec = htonl(timestamp.tv_usec);
2694}
2695
Luiz Capitulino222f1802006-03-20 22:16:13 -08002696static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2697 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 struct sk_buff *skb = NULL;
2700 __u8 *eth;
2701 struct udphdr *udph;
2702 int datalen, iplen;
2703 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002704 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002705 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002706 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2707 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2708 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2709 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002710 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002711
2712 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002713 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002714
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002715 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002716 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002717
Robert Olsson64053be2005-06-26 15:27:10 -07002718 /* Update any of the values, used when we're incrementing various
2719 * fields.
2720 */
2721 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002722 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002723
David S. Miller7ac54592006-01-18 14:19:10 -08002724 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002725
2726 if (pkt_dev->flags & F_NODE) {
2727 int node;
2728
2729 if (pkt_dev->node >= 0)
2730 node = pkt_dev->node;
2731 else
2732 node = numa_node_id();
2733
2734 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2735 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2736 if (likely(skb)) {
2737 skb_reserve(skb, NET_SKB_PAD);
2738 skb->dev = odev;
2739 }
2740 }
2741 else
2742 skb = __netdev_alloc_skb(odev,
2743 pkt_dev->cur_pkt_size + 64
2744 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (!skb) {
2747 sprintf(pkt_dev->result, "No memory");
2748 return NULL;
2749 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002750 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
David S. Miller7ac54592006-01-18 14:19:10 -08002752 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /* Reserve for ethernet and IP header */
2755 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002756 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2757 if (pkt_dev->nr_labels)
2758 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002759
2760 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002761 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002762 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002763 *svlan_tci = build_tci(pkt_dev->svlan_id,
2764 pkt_dev->svlan_cfi,
2765 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002766 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002767 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002768 }
2769 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002770 *vlan_tci = build_tci(pkt_dev->vlan_id,
2771 pkt_dev->vlan_cfi,
2772 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002773 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002774 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002775 }
2776
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002777 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002778 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002779 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002780 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002781 skb->priority = pkt_dev->skb_priority;
2782
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002783 iph = ip_hdr(skb);
2784 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002787 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002789 /* Eth + IPh + UDPh + mpls */
2790 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002791 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002792 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 udph->source = htons(pkt_dev->cur_udp_src);
2796 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002797 udph->len = htons(datalen + 8); /* DATA + udphdr */
2798 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 iph->ihl = 5;
2801 iph->version = 4;
2802 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002803 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002804 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 iph->saddr = pkt_dev->cur_saddr;
2806 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002807 iph->id = htons(pkt_dev->ip_id);
2808 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 iph->frag_off = 0;
2810 iplen = 20 + 8 + datalen;
2811 iph->tot_len = htons(iplen);
2812 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002813 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002814 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002815 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002816 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 skb->dev = odev;
2818 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002819 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002820
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002821#ifdef CONFIG_XFRM
2822 if (!process_ipsec(pkt_dev, skb, protocol))
2823 return NULL;
2824#endif
2825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return skb;
2827}
2828
2829/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002830 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2832 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002833 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 * Should be candidate for net/ipv4/utils.c
2835 * --ro
2836 */
2837
Luiz Capitulino222f1802006-03-20 22:16:13 -08002838static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002841 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 unsigned long u;
2843 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002844 unsigned int prefixlen = 0;
2845 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002846 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002847 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Luiz Capitulino222f1802006-03-20 22:16:13 -08002849 for (i = 0; i < 16; i++)
2850 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 for (;;) {
2853 if (*s == ':') {
2854 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002855 if (s[1] == ':') { /* Found "::", skip to part 2 */
2856 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 len++;
2858 break;
2859 }
2860 s++;
2861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002863 u = simple_strtoul(s, &pos, 16);
2864 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002865 if (!i)
2866 return 0;
2867 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 /* the last 4 bytes may be written as IPv4 address */
2870
2871 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002872 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2873 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
2875 ip[prefixlen++] = (u >> 8);
2876 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002877 s += i;
2878 len += i;
2879 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return len;
2881 }
2882
2883/* part 2, after "::" */
2884 for (;;) {
2885 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002886 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 break;
2888 s++;
2889 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002890 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002892
2893 u = simple_strtol(s, &pos, 16);
2894 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002896 if (*s)
2897 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 break;
2899 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002900 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002902 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2903 sizeof(tmp));
2904 suffixlen += 4;
2905 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 break;
2907 }
2908 suffix[suffixlen++] = (u >> 8);
2909 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002910 s += i;
2911 len += i;
2912 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 break;
2914 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002915 for (i = 0; i < suffixlen; i++)
2916 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return len;
2918}
2919
Luiz Capitulino222f1802006-03-20 22:16:13 -08002920static char tohex(char hexdigit)
2921{
2922 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923}
2924
Luiz Capitulino222f1802006-03-20 22:16:13 -08002925static int fmt_xlong(char *s, unsigned int i)
2926{
2927 char *bak = s;
2928 *s = tohex((i >> 12) & 0xf);
2929 if (s != bak || *s != '0')
2930 ++s;
2931 *s = tohex((i >> 8) & 0xf);
2932 if (s != bak || *s != '0')
2933 ++s;
2934 *s = tohex((i >> 4) & 0xf);
2935 if (s != bak || *s != '0')
2936 ++s;
2937 *s = tohex(i & 0xf);
2938 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
Luiz Capitulino222f1802006-03-20 22:16:13 -08002941static unsigned int fmt_ip6(char *s, const char ip[16])
2942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 unsigned int len;
2944 unsigned int i;
2945 unsigned int temp;
2946 unsigned int compressing;
2947 int j;
2948
Luiz Capitulino222f1802006-03-20 22:16:13 -08002949 len = 0;
2950 compressing = 0;
2951 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002954 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2955 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2956 temp = strlen(s);
2957 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
2959#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002960 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2961 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (temp == 0) {
2963 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002964 compressing = 1;
2965 if (j == 0) {
2966 *s++ = ':';
2967 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
2969 }
2970 } else {
2971 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002972 compressing = 0;
2973 *s++ = ':';
2974 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002976 i = fmt_xlong(s, temp);
2977 len += i;
2978 s += i;
2979 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 *s++ = ':';
2981 ++len;
2982 }
2983 }
2984 }
2985 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002986 *s++ = ':';
2987 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002989 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return len;
2991}
2992
Luiz Capitulino222f1802006-03-20 22:16:13 -08002993static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2994 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 struct sk_buff *skb = NULL;
2997 __u8 *eth;
2998 struct udphdr *udph;
2999 int datalen;
3000 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003001 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003002 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003003 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
3004 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
3005 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
3006 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003007 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003008
3009 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003010 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07003011
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003012 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003013 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003014
Robert Olsson64053be2005-06-26 15:27:10 -07003015 /* Update any of the values, used when we're incrementing various
3016 * fields.
3017 */
3018 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00003019 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07003020
Stephen Hemmingere4707572009-08-27 13:55:13 +00003021 skb = __netdev_alloc_skb(odev,
3022 pkt_dev->cur_pkt_size + 64
3023 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (!skb) {
3025 sprintf(pkt_dev->result, "No memory");
3026 return NULL;
3027 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00003028 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 skb_reserve(skb, 16);
3031
3032 /* Reserve for ethernet and IP header */
3033 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003034 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
3035 if (pkt_dev->nr_labels)
3036 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003037
3038 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07003039 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003040 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003041 *svlan_tci = build_tci(pkt_dev->svlan_id,
3042 pkt_dev->svlan_cfi,
3043 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003044 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003045 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003046 }
3047 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003048 *vlan_tci = build_tci(pkt_dev->vlan_id,
3049 pkt_dev->vlan_cfi,
3050 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003051 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003052 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003053 }
3054
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003055 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003056 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003057 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003058 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00003059 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003060 iph = ipv6_hdr(skb);
3061 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003064 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07003065
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003066 /* Eth + IPh + UDPh + mpls */
3067 datalen = pkt_dev->cur_pkt_size - 14 -
3068 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003069 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Luiz Capitulino222f1802006-03-20 22:16:13 -08003071 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 datalen = sizeof(struct pktgen_hdr);
3073 if (net_ratelimit())
Joe Perchesf9467ea2010-06-21 12:29:14 +00003074 pr_info("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
3076
3077 udph->source = htons(pkt_dev->cur_udp_src);
3078 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003079 udph->len = htons(datalen + sizeof(struct udphdr));
3080 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003082 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003084 if (pkt_dev->traffic_class) {
3085 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08003086 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003087 }
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 iph->hop_limit = 32;
3090
3091 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3092 iph->nexthdr = IPPROTO_UDP;
3093
3094 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3095 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3096
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003097 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003098 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003099 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 skb->dev = odev;
3101 skb->pkt_type = PACKET_HOST;
3102
Eric Dumazet26ad7872011-01-25 13:26:05 -08003103 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 return skb;
3106}
3107
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003108static struct sk_buff *fill_packet(struct net_device *odev,
3109 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003111 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 return fill_packet_ipv6(odev, pkt_dev);
3113 else
3114 return fill_packet_ipv4(odev, pkt_dev);
3115}
3116
Luiz Capitulino222f1802006-03-20 22:16:13 -08003117static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003119 pkt_dev->seq_num = 1;
3120 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003122 pkt_dev->tx_bytes = 0;
3123 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
3126/* Set up structure for sending pkts, clear counters */
3127
3128static void pktgen_run(struct pktgen_thread *t)
3129{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003130 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 int started = 0;
3132
Joe Perchesf9467ea2010-06-21 12:29:14 +00003133 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
3135 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003136 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138 /*
3139 * setup odev and create initial packet.
3140 */
3141 pktgen_setup_inject(pkt_dev);
3142
Luiz Capitulino222f1802006-03-20 22:16:13 -08003143 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003145 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003147 pkt_dev->started_at =
3148 pkt_dev->next_tx = ktime_now();
3149
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003150 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 strcpy(pkt_dev->result, "Starting");
3153 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 strcpy(pkt_dev->result, "Error starting");
3156 }
3157 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 if (started)
3159 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
3161
3162static void pktgen_stop_all_threads_ifs(void)
3163{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003164 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Joe Perchesf9467ea2010-06-21 12:29:14 +00003166 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003168 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003169
3170 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003171 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003172
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003173 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
Stephen Hemminger648fda72009-08-27 13:55:07 +00003176static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003178 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003180 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003181 if (pkt_dev->running)
3182 return 1;
3183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Luiz Capitulino222f1802006-03-20 22:16:13 -08003186static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003188 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Luiz Capitulino222f1802006-03-20 22:16:13 -08003190 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Luiz Capitulino222f1802006-03-20 22:16:13 -08003192 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Luiz Capitulino222f1802006-03-20 22:16:13 -08003194 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Luiz Capitulino222f1802006-03-20 22:16:13 -08003196 if (signal_pending(current))
3197 goto signal;
3198 if_lock(t);
3199 }
3200 if_unlock(t);
3201 return 1;
3202signal:
3203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
3206static int pktgen_wait_all_threads_run(void)
3207{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003208 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003210
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003211 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003212
3213 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003215 if (sig == 0)
3216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003218
3219 if (sig == 0)
3220 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003222
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003223 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return sig;
3225}
3226
3227static void pktgen_run_all_threads(void)
3228{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003229 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Joe Perchesf9467ea2010-06-21 12:29:14 +00003231 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003233 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003235 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003237
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003238 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003240 /* Propagate thread->control */
3241 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003242
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 pktgen_wait_all_threads_run();
3244}
3245
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003246static void pktgen_reset_all_threads(void)
3247{
3248 struct pktgen_thread *t;
3249
Joe Perchesf9467ea2010-06-21 12:29:14 +00003250 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003251
3252 mutex_lock(&pktgen_thread_lock);
3253
3254 list_for_each_entry(t, &pktgen_threads, th_list)
3255 t->control |= (T_REMDEVALL);
3256
3257 mutex_unlock(&pktgen_thread_lock);
3258
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003259 /* Propagate thread->control */
3260 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003261
3262 pktgen_wait_all_threads_run();
3263}
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3266{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003267 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003268 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003269 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3270 pkt_dev->started_at);
3271 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Daniel Turull03a14ab2011-03-09 14:11:00 -08003273 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003274 (unsigned long long)ktime_to_us(elapsed),
3275 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3276 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003277 (unsigned long long)pkt_dev->sofar,
3278 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003280 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3281 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Luiz Capitulino222f1802006-03-20 22:16:13 -08003283 bps = pps * 8 * pkt_dev->cur_pkt_size;
3284
3285 mbps = bps;
3286 do_div(mbps, 1000000);
3287 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3288 (unsigned long long)pps,
3289 (unsigned long long)mbps,
3290 (unsigned long long)bps,
3291 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003295static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003297 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003298
Luiz Capitulino222f1802006-03-20 22:16:13 -08003299 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003300 pr_warning("interface: %s is already stopped\n",
3301 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003302 return -EINVAL;
3303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003305 kfree_skb(pkt_dev->skb);
3306 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003307 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003308 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003310 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Luiz Capitulino222f1802006-03-20 22:16:13 -08003312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
Luiz Capitulino222f1802006-03-20 22:16:13 -08003315static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003317 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if_lock(t);
3320
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003321 list_for_each_entry(pkt_dev, &t->if_list, list) {
3322 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003323 continue;
3324 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003325 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003326 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003327 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 }
3329 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003330 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
Luiz Capitulino222f1802006-03-20 22:16:13 -08003333static void pktgen_stop(struct pktgen_thread *t)
3334{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003335 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Joe Perchesf9467ea2010-06-21 12:29:14 +00003337 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Luiz Capitulino222f1802006-03-20 22:16:13 -08003339 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003341 list_for_each_entry(pkt_dev, &t->if_list, list) {
3342 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Luiz Capitulino222f1802006-03-20 22:16:13 -08003345 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003348/*
3349 * one of our devices needs to be removed - find it
3350 * and remove it
3351 */
3352static void pktgen_rem_one_if(struct pktgen_thread *t)
3353{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003354 struct list_head *q, *n;
3355 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003356
Joe Perchesf9467ea2010-06-21 12:29:14 +00003357 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003358
3359 if_lock(t);
3360
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003361 list_for_each_safe(q, n, &t->if_list) {
3362 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003363
Luiz Capitulino222f1802006-03-20 22:16:13 -08003364 if (!cur->removal_mark)
3365 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003366
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003367 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003368 cur->skb = NULL;
3369
3370 pktgen_remove_device(t, cur);
3371
3372 break;
3373 }
3374
3375 if_unlock(t);
3376}
3377
Luiz Capitulino222f1802006-03-20 22:16:13 -08003378static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003380 struct list_head *q, *n;
3381 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003382
Joe Perchesf9467ea2010-06-21 12:29:14 +00003383 func_enter();
3384
Luiz Capitulino222f1802006-03-20 22:16:13 -08003385 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003386
Luiz Capitulino222f1802006-03-20 22:16:13 -08003387 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003389 list_for_each_safe(q, n, &t->if_list) {
3390 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003391
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003392 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003393 cur->skb = NULL;
3394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 pktgen_remove_device(t, cur);
3396 }
3397
Luiz Capitulino222f1802006-03-20 22:16:13 -08003398 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
Luiz Capitulino222f1802006-03-20 22:16:13 -08003401static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003403 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
David S. Milleree74baa2007-01-01 20:51:53 -08003405 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
Stephen Hemmingeref879792009-09-22 19:41:43 +00003409static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003410{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003411 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003412 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003413 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003414}
3415
Stephen Hemmingeref879792009-09-22 19:41:43 +00003416static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3417{
3418 ktime_t idle_start = ktime_now();
3419
3420 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3421 if (signal_pending(current))
3422 break;
3423
3424 if (need_resched())
3425 pktgen_resched(pkt_dev);
3426 else
3427 cpu_relax();
3428 }
3429 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3430}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003431
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003432static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Stephen Hemminger00829822008-11-20 20:14:53 -08003434 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003435 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003436 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003437 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003438 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 int ret;
3440
Stephen Hemmingeref879792009-09-22 19:41:43 +00003441 /* If device is offline, then don't send */
3442 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3443 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003444 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003446
Stephen Hemmingeref879792009-09-22 19:41:43 +00003447 /* This is max DELAY, this has special meaning of
3448 * "never transmit"
3449 */
3450 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3451 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3452 return;
3453 }
3454
3455 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003456 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3457 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3458 /* build a new pkt */
3459 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003460
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003461 pkt_dev->skb = fill_packet(odev, pkt_dev);
3462 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003463 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003464 schedule();
3465 pkt_dev->clone_count--; /* back out increment, OOM */
3466 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003468 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003469 pkt_dev->allocated_skbs++;
3470 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003472
Stephen Hemmingeref879792009-09-22 19:41:43 +00003473 if (pkt_dev->delay && pkt_dev->last_ok)
3474 spin(pkt_dev, pkt_dev->next_tx);
3475
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003476 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3477 txq = netdev_get_tx_queue(odev, queue_map);
3478
3479 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003480
Eric Dumazet5a0d2262010-11-23 10:42:02 +00003481 if (unlikely(netif_tx_queue_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003482 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003483 pkt_dev->last_ok = 0;
3484 goto unlock;
3485 }
3486 atomic_inc(&(pkt_dev->skb->users));
3487 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Stephen Hemmingeref879792009-09-22 19:41:43 +00003489 switch (ret) {
3490 case NETDEV_TX_OK:
3491 txq_trans_update(txq);
3492 pkt_dev->last_ok = 1;
3493 pkt_dev->sofar++;
3494 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003495 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003496 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003497 case NET_XMIT_DROP:
3498 case NET_XMIT_CN:
3499 case NET_XMIT_POLICED:
3500 /* skb has been consumed */
3501 pkt_dev->errors++;
3502 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003503 default: /* Drivers are not supposed to return other values! */
3504 if (net_ratelimit())
Joe Perches58231182010-12-20 10:30:06 -08003505 pr_info("%s xmit error: %d\n", pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003506 pkt_dev->errors++;
3507 /* fallthru */
3508 case NETDEV_TX_LOCKED:
3509 case NETDEV_TX_BUSY:
3510 /* Retry it next time */
3511 atomic_dec(&(pkt_dev->skb->users));
3512 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003513 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003514unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003515 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003516
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 /* If pkt_dev->count is zero, then run forever */
3518 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003519 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 /* Done with this */
3522 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003523 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003526/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 * Main loop of the thread goes here
3528 */
3529
David S. Milleree74baa2007-01-01 20:51:53 -08003530static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
3532 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003533 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003534 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
David S. Milleree74baa2007-01-01 20:51:53 -08003537 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
3539 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003540 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Joe Perchesf9467ea2010-06-21 12:29:14 +00003542 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
David S. Milleree74baa2007-01-01 20:51:53 -08003544 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003545
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003546 set_freezable();
3547
David S. Milleree74baa2007-01-01 20:51:53 -08003548 while (!kthread_should_stop()) {
3549 pkt_dev = next_to_run(t);
3550
Stephen Hemmingeref879792009-09-22 19:41:43 +00003551 if (unlikely(!pkt_dev && t->control == 0)) {
Eric Dumazet551eaff2010-11-21 10:26:44 -08003552 if (pktgen_exiting)
3553 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003554 wait_event_interruptible_timeout(t->queue,
3555 t->control != 0,
3556 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003557 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003558 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003559 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 __set_current_state(TASK_RUNNING);
3562
Stephen Hemmingeref879792009-09-22 19:41:43 +00003563 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 pktgen_xmit(pkt_dev);
3565
Stephen Hemmingeref879792009-09-22 19:41:43 +00003566 if (need_resched())
3567 pktgen_resched(pkt_dev);
3568 else
3569 cpu_relax();
3570 }
3571
Luiz Capitulino222f1802006-03-20 22:16:13 -08003572 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 pktgen_stop(t);
3574 t->control &= ~(T_STOP);
3575 }
3576
Luiz Capitulino222f1802006-03-20 22:16:13 -08003577 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 pktgen_run(t);
3579 t->control &= ~(T_RUN);
3580 }
3581
Luiz Capitulino222f1802006-03-20 22:16:13 -08003582 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003584 t->control &= ~(T_REMDEVALL);
3585 }
3586
Luiz Capitulino222f1802006-03-20 22:16:13 -08003587 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003588 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 t->control &= ~(T_REMDEV);
3590 }
3591
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003592 try_to_freeze();
3593
David S. Milleree74baa2007-01-01 20:51:53 -08003594 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Joe Perchesf9467ea2010-06-21 12:29:14 +00003597 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003598 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Joe Perchesf9467ea2010-06-21 12:29:14 +00003600 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003601 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Joe Perchesf9467ea2010-06-21 12:29:14 +00003603 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003604 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003605
Eric Dumazet551eaff2010-11-21 10:26:44 -08003606 /* Wait for kthread_stop */
3607 while (!kthread_should_stop()) {
3608 set_current_state(TASK_INTERRUPTIBLE);
3609 schedule();
3610 }
3611 __set_current_state(TASK_RUNNING);
3612
David S. Milleree74baa2007-01-01 20:51:53 -08003613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614}
3615
Luiz Capitulino222f1802006-03-20 22:16:13 -08003616static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003617 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003619 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003620 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Eric Dumazet3e984842009-11-24 14:50:53 -08003622 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003623 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003624 if (strncmp(p->odevname, ifname, len) == 0) {
3625 if (p->odevname[len]) {
3626 if (exact || p->odevname[len] != '@')
3627 continue;
3628 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003629 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003630 break;
3631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Luiz Capitulino222f1802006-03-20 22:16:13 -08003633 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003634 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003635 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636}
3637
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003638/*
3639 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 */
3641
Luiz Capitulino222f1802006-03-20 22:16:13 -08003642static int add_dev_to_thread(struct pktgen_thread *t,
3643 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
3645 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647 if_lock(t);
3648
3649 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003650 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003651 rv = -EBUSY;
3652 goto out;
3653 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003654
3655 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003656 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 pkt_dev->running = 0;
3658
Luiz Capitulino222f1802006-03-20 22:16:13 -08003659out:
3660 if_unlock(t);
3661 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
3664/* Called under thread lock */
3665
Luiz Capitulino222f1802006-03-20 22:16:13 -08003666static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003668 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003669 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003670 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 /* We don't allow a device to be on several threads */
3673
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003674 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3675 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003676 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003677 return -EBUSY;
3678 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003679
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003680 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003681 if (!pkt_dev)
3682 return -ENOMEM;
3683
Eric Dumazet593f63b2009-11-23 01:44:37 +00003684 strcpy(pkt_dev->odevname, ifname);
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003685 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3686 node);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003687 if (pkt_dev->flows == NULL) {
3688 kfree(pkt_dev);
3689 return -ENOMEM;
3690 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003691 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003692
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003693 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003694 pkt_dev->min_pkt_size = ETH_ZLEN;
3695 pkt_dev->max_pkt_size = ETH_ZLEN;
3696 pkt_dev->nfrags = 0;
3697 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003698 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003699 pkt_dev->count = pg_count_d;
3700 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003701 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003702 pkt_dev->udp_src_max = 9;
3703 pkt_dev->udp_dst_min = 9;
3704 pkt_dev->udp_dst_max = 9;
3705
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003706 pkt_dev->vlan_p = 0;
3707 pkt_dev->vlan_cfi = 0;
3708 pkt_dev->vlan_id = 0xffff;
3709 pkt_dev->svlan_p = 0;
3710 pkt_dev->svlan_cfi = 0;
3711 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003712 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003713
Stephen Hemminger39df2322007-03-04 16:11:51 -08003714 err = pktgen_setup_dev(pkt_dev, ifname);
3715 if (err)
3716 goto out1;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003717
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003718 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3719 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003720 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003721 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003722 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003723 err = -EINVAL;
3724 goto out2;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003725 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003726#ifdef CONFIG_XFRM
3727 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3728 pkt_dev->ipsproto = IPPROTO_ESP;
3729#endif
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003730
3731 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003732out2:
3733 dev_put(pkt_dev->odev);
3734out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003735#ifdef CONFIG_XFRM
3736 free_SAs(pkt_dev);
3737#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003738 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003739 kfree(pkt_dev);
3740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
David S. Milleree74baa2007-01-01 20:51:53 -08003743static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003745 struct pktgen_thread *t;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003746 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003747 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003749 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3750 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003751 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003752 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003753 return -ENOMEM;
3754 }
3755
Luiz Capitulino222f1802006-03-20 22:16:13 -08003756 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003758
David S. Milleree74baa2007-01-01 20:51:53 -08003759 INIT_LIST_HEAD(&t->if_list);
3760
3761 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003762 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003763
Eric Dumazet94dcf292011-03-22 16:30:45 -07003764 p = kthread_create_on_node(pktgen_thread_worker,
3765 t,
3766 cpu_to_node(cpu),
3767 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003768 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003769 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003770 list_del(&t->th_list);
3771 kfree(t);
3772 return PTR_ERR(p);
3773 }
3774 kthread_bind(p, cpu);
3775 t->tsk = p;
3776
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003777 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3778 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003779 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003780 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003781 PG_PROC_DIR, t->tsk->comm);
3782 kthread_stop(p);
3783 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003784 kfree(t);
3785 return -EINVAL;
3786 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003787
David S. Milleree74baa2007-01-01 20:51:53 -08003788 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003789 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
3791 return 0;
3792}
3793
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003794/*
3795 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003797static void _rem_dev_from_if_list(struct pktgen_thread *t,
3798 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003800 struct list_head *q, *n;
3801 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003803 list_for_each_safe(q, n, &t->if_list) {
3804 p = list_entry(q, struct pktgen_dev, list);
3805 if (p == pkt_dev)
3806 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
3808}
3809
Luiz Capitulino222f1802006-03-20 22:16:13 -08003810static int pktgen_remove_device(struct pktgen_thread *t,
3811 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
3813
Joe Perchesf9467ea2010-06-21 12:29:14 +00003814 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Luiz Capitulino222f1802006-03-20 22:16:13 -08003816 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003817 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003818 pktgen_stop_device(pkt_dev);
3819 }
3820
3821 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
3823 if (pkt_dev->odev) {
3824 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003825 pkt_dev->odev = NULL;
3826 }
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 /* And update the thread if_list */
3829
3830 _rem_dev_from_if_list(t, pkt_dev);
3831
Stephen Hemminger39df2322007-03-04 16:11:51 -08003832 if (pkt_dev->entry)
3833 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003835#ifdef CONFIG_XFRM
3836 free_SAs(pkt_dev);
3837#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003838 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003839 if (pkt_dev->page)
3840 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843}
3844
Luiz Capitulino222f1802006-03-20 22:16:13 -08003845static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846{
3847 int cpu;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003848 struct proc_dir_entry *pe;
3849
Joe Perchesf9467ea2010-06-21 12:29:14 +00003850 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003852 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003853 if (!pg_proc_dir)
3854 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Wang Chen25296d52008-02-28 14:11:49 -08003856 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003857 if (pe == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003858 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003859 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003860 return -EINVAL;
3861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 /* Register us to receive netdevice events */
3864 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003865
John Hawkes670c02c2005-10-13 09:30:31 -07003866 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003867 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
David S. Milleree74baa2007-01-01 20:51:53 -08003869 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003870 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00003871 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3872 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003873 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003874
3875 if (list_empty(&pktgen_threads)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003876 pr_err("ERROR: Initialization failed for all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003877 unregister_netdevice_notifier(&pktgen_notifier_block);
3878 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003879 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003880 return -ENODEV;
3881 }
3882
Luiz Capitulino222f1802006-03-20 22:16:13 -08003883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
3886static void __exit pg_cleanup(void)
3887{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003888 struct pktgen_thread *t;
3889 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
Luiz Capitulino222f1802006-03-20 22:16:13 -08003891 /* Stop all interfaces & threads */
Eric Dumazet551eaff2010-11-21 10:26:44 -08003892 pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003894 list_for_each_safe(q, n, &pktgen_threads) {
3895 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003896 kthread_stop(t->tsk);
3897 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Luiz Capitulino222f1802006-03-20 22:16:13 -08003900 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 unregister_netdevice_notifier(&pktgen_notifier_block);
3902
Luiz Capitulino222f1802006-03-20 22:16:13 -08003903 /* Clean up proc file system */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003904 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003905 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906}
3907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908module_init(pg_init);
3909module_exit(pg_cleanup);
3910
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003911MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912MODULE_DESCRIPTION("Packet Generator tool");
3913MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003914MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003916MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003918MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003920MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003922MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");