blob: de0c2c726420e2044c55fd6235e15e915de16a51 [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 */
118#include <linux/sys.h>
119#include <linux/types.h>
120#include <linux/module.h>
121#include <linux/moduleparam.h>
122#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800123#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <linux/sched.h>
125#include <linux/slab.h>
126#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <linux/unistd.h>
128#include <linux/string.h>
129#include <linux/ptrace.h>
130#include <linux/errno.h>
131#include <linux/ioport.h>
132#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800133#include <linux/capability.h>
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700134#include <linux/hrtimer.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700135#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#include <linux/delay.h>
137#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800138#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#include <linux/init.h>
140#include <linux/skbuff.h>
141#include <linux/netdevice.h>
142#include <linux/inet.h>
143#include <linux/inetdevice.h>
144#include <linux/rtnetlink.h>
145#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700146#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#include <linux/in.h>
148#include <linux/ip.h>
149#include <linux/ipv6.h>
150#include <linux/udp.h>
151#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700152#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800154#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800155#include <linux/kthread.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200156#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#include <net/checksum.h>
158#include <net/ipv6.h>
159#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700160#ifdef CONFIG_XFRM
161#include <net/xfrm.h>
162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#include <asm/byteorder.h>
164#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700165#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000166#include <linux/io.h>
167#include <linux/timex.h>
168#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800170#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000172#define VERSION "2.72"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800174#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800175#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800178#define F_IPSRC_RND (1<<0) /* IP-Src Random */
179#define F_IPDST_RND (1<<1) /* IP-Dst Random */
180#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800186#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700187#define F_VID_RND (1<<9) /* Random VLAN ID */
188#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700189#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700190#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700191#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700192#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000195#define T_STOP (1<<0) /* Stop run */
196#define T_RUN (1<<1) /* Start run */
197#define T_REMDEVALL (1<<2) /* Remove all devs */
198#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* If lock -- can be removed after some work */
201#define if_lock(t) spin_lock(&(t->if_lock));
202#define if_unlock(t) spin_unlock(&(t->if_lock));
203
204/* Used to help with determining the pkts on receive */
205#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700206#define PG_PROC_DIR "pktgen"
207#define PGCTRL "pgctrl"
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000208static struct proc_dir_entry *pg_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210#define MAX_CFLOWS 65536
211
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700212#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
213#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
214
Luiz Capitulino222f1802006-03-20 22:16:13 -0800215struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800216 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800217 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700218#ifdef CONFIG_XFRM
219 struct xfrm_state *x;
220#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700221 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700224/* flow flag bits */
225#define F_INIT (1<<0) /* flow has been initialized */
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * Try to keep frequent/infrequent used vars. separated.
230 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800231 struct proc_dir_entry *entry; /* proc file */
232 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000233 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000235 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800236
237 /* If min != max, then we will either do a linear iteration, or
238 * we will do a random selection from within the range.
239 */
240 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800241 int removal_mark; /* non-zero => the device is marked for
242 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Luiz Capitulino222f1802006-03-20 22:16:13 -0800244 int min_pkt_size; /* = ETH_ZLEN; */
245 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700246 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800247 int nfrags;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000248 u64 delay; /* nano-seconds */
249
Luiz Capitulino222f1802006-03-20 22:16:13 -0800250 __u64 count; /* Default No packets to send */
251 __u64 sofar; /* How many pkts we've sent so far */
252 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800253 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Luiz Capitulino222f1802006-03-20 22:16:13 -0800255 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Luiz Capitulino222f1802006-03-20 22:16:13 -0800257 __u64 allocated_skbs;
258 __u32 clone_count;
259 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000260 * Or a failed transmit of some sort?
261 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800262 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000263 ktime_t next_tx;
264 ktime_t started_at;
265 ktime_t stopped_at;
266 u64 idle_acc; /* nano-seconds */
267
Luiz Capitulino222f1802006-03-20 22:16:13 -0800268 __u32 seq_num;
269
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000270 int clone_skb; /*
271 * Use multiple SKBs during packet gen.
272 * If this number is greater than 1, then
273 * that many copies of the same packet will be
274 * sent before a new packet is allocated.
275 * If you want to send 1024 identical packets
276 * before creating a new packet,
277 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800278 */
279
280 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
283 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
284
285 struct in6_addr in6_saddr;
286 struct in6_addr in6_daddr;
287 struct in6_addr cur_in6_daddr;
288 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800290 struct in6_addr min_in6_daddr;
291 struct in6_addr max_in6_daddr;
292 struct in6_addr min_in6_saddr;
293 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Luiz Capitulino222f1802006-03-20 22:16:13 -0800295 /* If we're doing ranges, random or incremental, then this
296 * defines the min/max for those ranges.
297 */
Al Viro252e3342006-11-14 20:48:11 -0800298 __be32 saddr_min; /* inclusive, source IP address */
299 __be32 saddr_max; /* exclusive, source IP address */
300 __be32 daddr_min; /* inclusive, dest IP address */
301 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Luiz Capitulino222f1802006-03-20 22:16:13 -0800303 __u16 udp_src_min; /* inclusive, source UDP port */
304 __u16 udp_src_max; /* exclusive, source UDP port */
305 __u16 udp_dst_min; /* inclusive, dest UDP port */
306 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700308 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000309 __u8 tos; /* six MSB of (former) IPv4 TOS
310 are for dscp codepoint */
311 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
312 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700313
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800314 /* MPLS */
315 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
316 __be32 labels[MAX_MPLS_LABELS];
317
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700318 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
319 __u8 vlan_p;
320 __u8 vlan_cfi;
321 __u16 vlan_id; /* 0xffff means no vlan tag */
322
323 __u8 svlan_p;
324 __u8 svlan_cfi;
325 __u16 svlan_id; /* 0xffff means no svlan tag */
326
Luiz Capitulino222f1802006-03-20 22:16:13 -0800327 __u32 src_mac_count; /* How many MACs to iterate through */
328 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Luiz Capitulino222f1802006-03-20 22:16:13 -0800330 unsigned char dst_mac[ETH_ALEN];
331 unsigned char src_mac[ETH_ALEN];
332
333 __u32 cur_dst_mac_offset;
334 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800335 __be32 cur_saddr;
336 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700337 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800338 __u16 cur_udp_dst;
339 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700340 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800341 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800342 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800343
344 __u8 hh[14];
345 /* = {
346 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
347
348 We fill in SRC address later
349 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
350 0x08, 0x00
351 };
352 */
353 __u16 pad; /* pad out the hh struct to an even 16 bytes */
354
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000355 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800356 * are transmitting the same one multiple times
357 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000358 struct net_device *odev; /* The out-going device.
359 * Note that the device should have it's
360 * pg_info pointer pointing back to this
361 * device.
362 * Set when the user specifies the out-going
363 * device name (not when the inject is
364 * started as it used to do.)
365 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000366 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800368 unsigned cflows; /* Concurrent flows (config) */
369 unsigned lflow; /* Flow length (config) */
370 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700371 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700372
373 u16 queue_map_min;
374 u16 queue_map_max;
375
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700376#ifdef CONFIG_XFRM
377 __u8 ipsmode; /* IPSEC mode (config) */
378 __u8 ipsproto; /* IPSEC type (config) */
379#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800380 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381};
382
383struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800384 __be32 pgh_magic;
385 __be32 seq_num;
386 __be32 tv_sec;
387 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388};
389
390struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000391 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800392 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800393 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800394 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800395 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000397 /* Field for thread to receive "posted" events terminate,
398 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800399
400 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int cpu;
402
Luiz Capitulino222f1802006-03-20 22:16:13 -0800403 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700404 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405};
406
407#define REMOVE 1
408#define FIND 0
409
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000410static inline ktime_t ktime_now(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000412 struct timespec ts;
413 ktime_get_ts(&ts);
414
415 return timespec_to_ktime(ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000418/* This works even if 32 bit because of careful byte order choice */
419static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000421 return cmp1.tv64 < cmp2.tv64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000424static const char version[] =
425 "pktgen " VERSION ": Packet Generator for packet performance testing.\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Luiz Capitulino222f1802006-03-20 22:16:13 -0800427static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
428static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
429static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800430 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
432static void pktgen_run_all_threads(void);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800433static void pktgen_reset_all_threads(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434static void pktgen_stop_all_threads_ifs(void);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000435
Luiz Capitulino222f1802006-03-20 22:16:13 -0800436static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800438
Luiz Capitulino222f1802006-03-20 22:16:13 -0800439static unsigned int scan_ip6(const char *s, char ip[16]);
440static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000443static int pg_count_d __read_mostly = 1000;
444static int pg_delay_d __read_mostly;
445static int pg_clone_skb_d __read_mostly;
446static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Luiz Capitulino222fa072006-03-20 22:24:27 -0800448static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800449static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451static struct notifier_block pktgen_notifier_block = {
452 .notifier_call = pktgen_device_event,
453};
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900456 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *
458 */
459
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700460static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800461{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000462 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000466static ssize_t pgctrl_write(struct file *file, const char __user *buf,
467 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700470 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Luiz Capitulino222f1802006-03-20 22:16:13 -0800472 if (!capable(CAP_NET_ADMIN)) {
473 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700477 if (count > sizeof(data))
478 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700481 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700482 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800483 }
484 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Luiz Capitulino222f1802006-03-20 22:16:13 -0800486 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 pktgen_stop_all_threads_ifs();
488
Luiz Capitulino222f1802006-03-20 22:16:13 -0800489 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 pktgen_run_all_threads();
491
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800492 else if (!strcmp(data, "reset"))
493 pktgen_reset_all_threads();
494
Luiz Capitulino222f1802006-03-20 22:16:13 -0800495 else
David S. Miller25a8b252007-07-30 16:11:48 -0700496 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 err = count;
499
Luiz Capitulino222f1802006-03-20 22:16:13 -0800500out:
501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700504static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700506 return single_open(file, pgctrl_show, PDE(inode)->data);
507}
508
Arjan van de Ven9a321442007-02-12 00:55:35 -0800509static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800510 .owner = THIS_MODULE,
511 .open = pgctrl_open,
512 .read = seq_read,
513 .llseek = seq_lseek,
514 .write = pgctrl_write,
515 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700516};
517
518static int pktgen_if_show(struct seq_file *seq, void *v)
519{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000520 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000521 ktime_t stopped;
522 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Luiz Capitulino222f1802006-03-20 22:16:13 -0800524 seq_printf(seq,
525 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
526 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
527 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Luiz Capitulino222f1802006-03-20 22:16:13 -0800529 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000530 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
531 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000532 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Luiz Capitulino222f1802006-03-20 22:16:13 -0800534 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
535 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Robert Olsson45b270f2007-08-28 15:45:55 -0700537 seq_printf(seq,
538 " queue_map_min: %u queue_map_max: %u\n",
539 pkt_dev->queue_map_min,
540 pkt_dev->queue_map_max);
541
Luiz Capitulino222f1802006-03-20 22:16:13 -0800542 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800544 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
545 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
546 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
547 seq_printf(seq,
548 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
549 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Luiz Capitulino222f1802006-03-20 22:16:13 -0800551 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
552 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
553 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
554 seq_printf(seq,
555 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
556 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000558 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800559 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000560 " dst_min: %s dst_max: %s\n",
561 pkt_dev->dst_min, pkt_dev->dst_max);
562 seq_printf(seq,
563 " src_min: %s src_max: %s\n",
564 pkt_dev->src_min, pkt_dev->src_max);
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700567 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Johannes Berge1749612008-10-27 15:59:26 -0700569 seq_printf(seq, "%pM ",
570 is_zero_ether_addr(pkt_dev->src_mac) ?
571 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Luiz Capitulino222f1802006-03-20 22:16:13 -0800573 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700574 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Luiz Capitulino222f1802006-03-20 22:16:13 -0800576 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000577 " udp_src_min: %d udp_src_max: %d"
578 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800579 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
580 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Luiz Capitulino222f1802006-03-20 22:16:13 -0800582 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800583 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700584 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800586 if (pkt_dev->nr_labels) {
587 unsigned i;
588 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700589 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800590 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
591 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
592 }
593
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000594 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700595 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000596 pkt_dev->vlan_id, pkt_dev->vlan_p,
597 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700598
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000599 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700600 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000601 pkt_dev->svlan_id, pkt_dev->svlan_p,
602 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700603
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000604 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700605 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700606
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000607 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700608 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700609
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800610 seq_printf(seq, " Flags: ");
611
Luiz Capitulino222f1802006-03-20 22:16:13 -0800612 if (pkt_dev->flags & F_IPV6)
613 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Luiz Capitulino222f1802006-03-20 22:16:13 -0800615 if (pkt_dev->flags & F_IPSRC_RND)
616 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Luiz Capitulino222f1802006-03-20 22:16:13 -0800618 if (pkt_dev->flags & F_IPDST_RND)
619 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Luiz Capitulino222f1802006-03-20 22:16:13 -0800621 if (pkt_dev->flags & F_TXSIZE_RND)
622 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Luiz Capitulino222f1802006-03-20 22:16:13 -0800624 if (pkt_dev->flags & F_UDPSRC_RND)
625 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Luiz Capitulino222f1802006-03-20 22:16:13 -0800627 if (pkt_dev->flags & F_UDPDST_RND)
628 seq_printf(seq, "UDPDST_RND ");
629
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800630 if (pkt_dev->flags & F_MPLS_RND)
631 seq_printf(seq, "MPLS_RND ");
632
Robert Olsson45b270f2007-08-28 15:45:55 -0700633 if (pkt_dev->flags & F_QUEUE_MAP_RND)
634 seq_printf(seq, "QUEUE_MAP_RND ");
635
Robert Olssone6fce5b2008-08-07 02:23:01 -0700636 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
637 seq_printf(seq, "QUEUE_MAP_CPU ");
638
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700639 if (pkt_dev->cflows) {
640 if (pkt_dev->flags & F_FLOW_SEQ)
641 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
642 else
643 seq_printf(seq, "FLOW_RND ");
644 }
645
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700646#ifdef CONFIG_XFRM
647 if (pkt_dev->flags & F_IPSEC_ON)
648 seq_printf(seq, "IPSEC ");
649#endif
650
Luiz Capitulino222f1802006-03-20 22:16:13 -0800651 if (pkt_dev->flags & F_MACSRC_RND)
652 seq_printf(seq, "MACSRC_RND ");
653
654 if (pkt_dev->flags & F_MACDST_RND)
655 seq_printf(seq, "MACDST_RND ");
656
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700657 if (pkt_dev->flags & F_VID_RND)
658 seq_printf(seq, "VID_RND ");
659
660 if (pkt_dev->flags & F_SVID_RND)
661 seq_printf(seq, "SVID_RND ");
662
Luiz Capitulino222f1802006-03-20 22:16:13 -0800663 seq_puts(seq, "\n");
664
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000665 /* not really stopped, more like last-running-at */
666 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
667 idle = pkt_dev->idle_acc;
668 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800669
670 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000671 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800672 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000673 (unsigned long long)pkt_dev->errors);
674
675 seq_printf(seq,
676 " started: %lluus stopped: %lluus idle: %lluus\n",
677 (unsigned long long) ktime_to_us(pkt_dev->started_at),
678 (unsigned long long) ktime_to_us(stopped),
679 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800680
681 seq_printf(seq,
682 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700683 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
684 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Luiz Capitulino222f1802006-03-20 22:16:13 -0800686 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800688 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
689 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
690 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
691 } else
692 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700693 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Luiz Capitulino222f1802006-03-20 22:16:13 -0800695 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700696 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Robert Olsson45b270f2007-08-28 15:45:55 -0700698 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
699
Luiz Capitulino222f1802006-03-20 22:16:13 -0800700 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800703 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800705 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800710
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000711static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
712 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800713{
714 int i = 0;
715 *num = 0;
716
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700717 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800718 char c;
719 *num <<= 4;
720 if (get_user(c, &user_buffer[i]))
721 return -EFAULT;
722 if ((c >= '0') && (c <= '9'))
723 *num |= c - '0';
724 else if ((c >= 'a') && (c <= 'f'))
725 *num |= c - 'a' + 10;
726 else if ((c >= 'A') && (c <= 'F'))
727 *num |= c - 'A' + 10;
728 else
729 break;
730 }
731 return i;
732}
733
Luiz Capitulino222f1802006-03-20 22:16:13 -0800734static int count_trail_chars(const char __user * user_buffer,
735 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 int i;
738
739 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800740 char c;
741 if (get_user(c, &user_buffer[i]))
742 return -EFAULT;
743 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 case '\"':
745 case '\n':
746 case '\r':
747 case '\t':
748 case ' ':
749 case '=':
750 break;
751 default:
752 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755done:
756 return i;
757}
758
Luiz Capitulino222f1802006-03-20 22:16:13 -0800759static unsigned long num_arg(const char __user * user_buffer,
760 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 int i = 0;
763 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800764
765 for (; i < maxlen; i++) {
766 char c;
767 if (get_user(c, &user_buffer[i]))
768 return -EFAULT;
769 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800771 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 } else
773 break;
774 }
775 return i;
776}
777
Luiz Capitulino222f1802006-03-20 22:16:13 -0800778static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 int i = 0;
781
Luiz Capitulino222f1802006-03-20 22:16:13 -0800782 for (; i < maxlen; i++) {
783 char c;
784 if (get_user(c, &user_buffer[i]))
785 return -EFAULT;
786 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 case '\"':
788 case '\n':
789 case '\r':
790 case '\t':
791 case ' ':
792 goto done_str;
793 break;
794 default:
795 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return i;
800}
801
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800802static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
803{
804 unsigned n = 0;
805 char c;
806 ssize_t i = 0;
807 int len;
808
809 pkt_dev->nr_labels = 0;
810 do {
811 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700812 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800813 if (len <= 0)
814 return len;
815 pkt_dev->labels[n] = htonl(tmp);
816 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
817 pkt_dev->flags |= F_MPLS_RND;
818 i += len;
819 if (get_user(c, &buffer[i]))
820 return -EFAULT;
821 i++;
822 n++;
823 if (n >= MAX_MPLS_LABELS)
824 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700825 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800826
827 pkt_dev->nr_labels = n;
828 return i;
829}
830
Luiz Capitulino222f1802006-03-20 22:16:13 -0800831static ssize_t pktgen_if_write(struct file *file,
832 const char __user * user_buffer, size_t count,
833 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800835 struct seq_file *seq = (struct seq_file *)file->private_data;
836 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int i = 0, max, len;
838 char name[16], valstr[32];
839 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800840 char *pg_result = NULL;
841 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800843
844 pg_result = &(pkt_dev->result[0]);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700847 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -EINVAL;
849 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 max = count - i;
852 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700854 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800855 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800857 i += tmp;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /* Read variable name */
860
861 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000862 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800866 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -EFAULT;
868 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800869
870 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800872 if (len < 0)
873 return len;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 i += len;
876
877 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800878 char tb[count + 1];
879 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800881 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700882 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800883 (unsigned long)count, tb);
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (!strcmp(name, "min_pkt_size")) {
887 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000888 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800892 if (value < 14 + 20 + 8)
893 value = 14 + 20 + 8;
894 if (value != pkt_dev->min_pkt_size) {
895 pkt_dev->min_pkt_size = value;
896 pkt_dev->cur_pkt_size = value;
897 }
898 sprintf(pg_result, "OK: min_pkt_size=%u",
899 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return count;
901 }
902
Luiz Capitulino222f1802006-03-20 22:16:13 -0800903 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000905 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800906 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800909 if (value < 14 + 20 + 8)
910 value = 14 + 20 + 8;
911 if (value != pkt_dev->max_pkt_size) {
912 pkt_dev->max_pkt_size = value;
913 pkt_dev->cur_pkt_size = value;
914 }
915 sprintf(pg_result, "OK: max_pkt_size=%u",
916 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return count;
918 }
919
Luiz Capitulino222f1802006-03-20 22:16:13 -0800920 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 if (!strcmp(name, "pkt_size")) {
923 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000924 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800925 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800928 if (value < 14 + 20 + 8)
929 value = 14 + 20 + 8;
930 if (value != pkt_dev->min_pkt_size) {
931 pkt_dev->min_pkt_size = value;
932 pkt_dev->max_pkt_size = value;
933 pkt_dev->cur_pkt_size = value;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
936 return count;
937 }
938
Luiz Capitulino222f1802006-03-20 22:16:13 -0800939 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000941 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800942 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800945 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 sprintf(pg_result, "OK: debug=%u", debug);
947 return count;
948 }
949
Luiz Capitulino222f1802006-03-20 22:16:13 -0800950 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000952 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800953 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 i += len;
956 pkt_dev->nfrags = value;
957 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
958 return count;
959 }
960 if (!strcmp(name, "delay")) {
961 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;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000966 if (value == 0x7FFFFFFF)
967 pkt_dev->delay = ULLONG_MAX;
968 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000969 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000970
971 sprintf(pg_result, "OK: delay=%llu",
972 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return count;
974 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000977 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800978 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800981 if (value != pkt_dev->udp_src_min) {
982 pkt_dev->udp_src_min = value;
983 pkt_dev->cur_udp_src = value;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
986 return count;
987 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800988 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000990 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800991 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800994 if (value != pkt_dev->udp_dst_min) {
995 pkt_dev->udp_dst_min = value;
996 pkt_dev->cur_udp_dst = value;
997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
999 return count;
1000 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001001 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001003 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001004 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001007 if (value != pkt_dev->udp_src_max) {
1008 pkt_dev->udp_src_max = value;
1009 pkt_dev->cur_udp_src = value;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1012 return count;
1013 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001014 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001016 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001017 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001020 if (value != pkt_dev->udp_dst_max) {
1021 pkt_dev->udp_dst_max = value;
1022 pkt_dev->cur_udp_dst = value;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1025 return count;
1026 }
1027 if (!strcmp(name, "clone_skb")) {
1028 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001029 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001030 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001033 pkt_dev->clone_skb = value;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1036 return count;
1037 }
1038 if (!strcmp(name, "count")) {
1039 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001040 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001041 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 i += len;
1044 pkt_dev->count = value;
1045 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001046 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return count;
1048 }
1049 if (!strcmp(name, "src_mac_count")) {
1050 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001051 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 i += len;
1055 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001056 pkt_dev->src_mac_count = value;
1057 pkt_dev->cur_src_mac_offset = 0;
1058 }
1059 sprintf(pg_result, "OK: src_mac_count=%d",
1060 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return count;
1062 }
1063 if (!strcmp(name, "dst_mac_count")) {
1064 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001065 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001066 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 i += len;
1069 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001070 pkt_dev->dst_mac_count = value;
1071 pkt_dev->cur_dst_mac_offset = 0;
1072 }
1073 sprintf(pg_result, "OK: dst_mac_count=%d",
1074 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return count;
1076 }
1077 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001078 char f[32];
1079 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001081 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001082 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (copy_from_user(f, &user_buffer[i], len))
1085 return -EFAULT;
1086 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001087 if (strcmp(f, "IPSRC_RND") == 0)
1088 pkt_dev->flags |= F_IPSRC_RND;
1089
1090 else if (strcmp(f, "!IPSRC_RND") == 0)
1091 pkt_dev->flags &= ~F_IPSRC_RND;
1092
1093 else if (strcmp(f, "TXSIZE_RND") == 0)
1094 pkt_dev->flags |= F_TXSIZE_RND;
1095
1096 else if (strcmp(f, "!TXSIZE_RND") == 0)
1097 pkt_dev->flags &= ~F_TXSIZE_RND;
1098
1099 else if (strcmp(f, "IPDST_RND") == 0)
1100 pkt_dev->flags |= F_IPDST_RND;
1101
1102 else if (strcmp(f, "!IPDST_RND") == 0)
1103 pkt_dev->flags &= ~F_IPDST_RND;
1104
1105 else if (strcmp(f, "UDPSRC_RND") == 0)
1106 pkt_dev->flags |= F_UDPSRC_RND;
1107
1108 else if (strcmp(f, "!UDPSRC_RND") == 0)
1109 pkt_dev->flags &= ~F_UDPSRC_RND;
1110
1111 else if (strcmp(f, "UDPDST_RND") == 0)
1112 pkt_dev->flags |= F_UDPDST_RND;
1113
1114 else if (strcmp(f, "!UDPDST_RND") == 0)
1115 pkt_dev->flags &= ~F_UDPDST_RND;
1116
1117 else if (strcmp(f, "MACSRC_RND") == 0)
1118 pkt_dev->flags |= F_MACSRC_RND;
1119
1120 else if (strcmp(f, "!MACSRC_RND") == 0)
1121 pkt_dev->flags &= ~F_MACSRC_RND;
1122
1123 else if (strcmp(f, "MACDST_RND") == 0)
1124 pkt_dev->flags |= F_MACDST_RND;
1125
1126 else if (strcmp(f, "!MACDST_RND") == 0)
1127 pkt_dev->flags &= ~F_MACDST_RND;
1128
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001129 else if (strcmp(f, "MPLS_RND") == 0)
1130 pkt_dev->flags |= F_MPLS_RND;
1131
1132 else if (strcmp(f, "!MPLS_RND") == 0)
1133 pkt_dev->flags &= ~F_MPLS_RND;
1134
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001135 else if (strcmp(f, "VID_RND") == 0)
1136 pkt_dev->flags |= F_VID_RND;
1137
1138 else if (strcmp(f, "!VID_RND") == 0)
1139 pkt_dev->flags &= ~F_VID_RND;
1140
1141 else if (strcmp(f, "SVID_RND") == 0)
1142 pkt_dev->flags |= F_SVID_RND;
1143
1144 else if (strcmp(f, "!SVID_RND") == 0)
1145 pkt_dev->flags &= ~F_SVID_RND;
1146
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001147 else if (strcmp(f, "FLOW_SEQ") == 0)
1148 pkt_dev->flags |= F_FLOW_SEQ;
1149
Robert Olsson45b270f2007-08-28 15:45:55 -07001150 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1151 pkt_dev->flags |= F_QUEUE_MAP_RND;
1152
1153 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1154 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001155
1156 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1157 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1158
1159 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1160 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001161#ifdef CONFIG_XFRM
1162 else if (strcmp(f, "IPSEC") == 0)
1163 pkt_dev->flags |= F_IPSEC_ON;
1164#endif
1165
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001166 else if (strcmp(f, "!IPV6") == 0)
1167 pkt_dev->flags &= ~F_IPV6;
1168
Luiz Capitulino222f1802006-03-20 22:16:13 -08001169 else {
1170 sprintf(pg_result,
1171 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1172 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001173 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001174 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001175 return count;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1178 return count;
1179 }
1180 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1181 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001182 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001183 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Luiz Capitulino222f1802006-03-20 22:16:13 -08001185 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001187 buf[len] = 0;
1188 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1189 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1190 strncpy(pkt_dev->dst_min, buf, len);
1191 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1192 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1193 }
1194 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001195 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001196 pkt_dev->dst_min);
1197 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1199 return count;
1200 }
1201 if (!strcmp(name, "dst_max")) {
1202 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001203 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001204 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Luiz Capitulino222f1802006-03-20 22:16:13 -08001207 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -EFAULT;
1209
Luiz Capitulino222f1802006-03-20 22:16:13 -08001210 buf[len] = 0;
1211 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1212 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1213 strncpy(pkt_dev->dst_max, buf, len);
1214 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1215 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1216 }
1217 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001218 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001219 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 i += len;
1221 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1222 return count;
1223 }
1224 if (!strcmp(name, "dst6")) {
1225 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001226 if (len < 0)
1227 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 pkt_dev->flags |= F_IPV6;
1230
Luiz Capitulino222f1802006-03-20 22:16:13 -08001231 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001233 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001236 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1239
Luiz Capitulino222f1802006-03-20 22:16:13 -08001240 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001241 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Luiz Capitulino222f1802006-03-20 22:16:13 -08001243 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 sprintf(pg_result, "OK: dst6=%s", buf);
1245 return count;
1246 }
1247 if (!strcmp(name, "dst6_min")) {
1248 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001249 if (len < 0)
1250 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 pkt_dev->flags |= F_IPV6;
1253
Luiz Capitulino222f1802006-03-20 22:16:13 -08001254 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001256 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Luiz Capitulino222f1802006-03-20 22:16:13 -08001261 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1262 &pkt_dev->min_in6_daddr);
1263 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001264 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Luiz Capitulino222f1802006-03-20 22:16:13 -08001266 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 sprintf(pg_result, "OK: dst6_min=%s", buf);
1268 return count;
1269 }
1270 if (!strcmp(name, "dst6_max")) {
1271 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001272 if (len < 0)
1273 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 pkt_dev->flags |= F_IPV6;
1276
Luiz Capitulino222f1802006-03-20 22:16:13 -08001277 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001279 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001282 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Luiz Capitulino222f1802006-03-20 22:16:13 -08001284 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001285 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Luiz Capitulino222f1802006-03-20 22:16:13 -08001287 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 sprintf(pg_result, "OK: dst6_max=%s", buf);
1289 return count;
1290 }
1291 if (!strcmp(name, "src6")) {
1292 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001293 if (len < 0)
1294 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 pkt_dev->flags |= F_IPV6;
1297
Luiz Capitulino222f1802006-03-20 22:16:13 -08001298 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001300 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001303 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1306
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001308 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001309
1310 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 sprintf(pg_result, "OK: src6=%s", buf);
1312 return count;
1313 }
1314 if (!strcmp(name, "src_min")) {
1315 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001316 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001318
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001321 buf[len] = 0;
1322 if (strcmp(buf, pkt_dev->src_min) != 0) {
1323 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1324 strncpy(pkt_dev->src_min, buf, len);
1325 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1326 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1327 }
1328 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001329 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001330 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 i += len;
1332 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1333 return count;
1334 }
1335 if (!strcmp(name, "src_max")) {
1336 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001337 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001339
Luiz Capitulino222f1802006-03-20 22:16:13 -08001340 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001342 buf[len] = 0;
1343 if (strcmp(buf, pkt_dev->src_max) != 0) {
1344 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1345 strncpy(pkt_dev->src_max, buf, len);
1346 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1347 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1348 }
1349 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001350 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 i += len;
1353 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1354 return count;
1355 }
1356 if (!strcmp(name, "dst_mac")) {
1357 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001358 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001360 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001363 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001364 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001367 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return -EFAULT;
1369 i += len;
1370
Luiz Capitulino222f1802006-03-20 22:16:13 -08001371 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (*v >= '0' && *v <= '9') {
1373 *m *= 16;
1374 *m += *v - '0';
1375 }
1376 if (*v >= 'A' && *v <= 'F') {
1377 *m *= 16;
1378 *m += *v - 'A' + 10;
1379 }
1380 if (*v >= 'a' && *v <= 'f') {
1381 *m *= 16;
1382 *m += *v - 'a' + 10;
1383 }
1384 if (*v == ':') {
1385 m++;
1386 *m = 0;
1387 }
1388 }
1389
1390 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001391 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1392 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 sprintf(pg_result, "OK: dstmac");
1395 return count;
1396 }
1397 if (!strcmp(name, "src_mac")) {
1398 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001399 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 unsigned char *m = pkt_dev->src_mac;
1401
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001402 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001405 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001406 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001409 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EFAULT;
1411 i += len;
1412
Luiz Capitulino222f1802006-03-20 22:16:13 -08001413 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (*v >= '0' && *v <= '9') {
1415 *m *= 16;
1416 *m += *v - '0';
1417 }
1418 if (*v >= 'A' && *v <= 'F') {
1419 *m *= 16;
1420 *m += *v - 'A' + 10;
1421 }
1422 if (*v >= 'a' && *v <= 'f') {
1423 *m *= 16;
1424 *m += *v - 'a' + 10;
1425 }
1426 if (*v == ':') {
1427 m++;
1428 *m = 0;
1429 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001432 /* Set up Src MAC */
1433 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1434 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1435
Luiz Capitulino222f1802006-03-20 22:16:13 -08001436 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return count;
1438 }
1439
Luiz Capitulino222f1802006-03-20 22:16:13 -08001440 if (!strcmp(name, "clear_counters")) {
1441 pktgen_clear_counters(pkt_dev);
1442 sprintf(pg_result, "OK: Clearing counters.\n");
1443 return count;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 if (!strcmp(name, "flows")) {
1447 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001448 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001449 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 i += len;
1452 if (value > MAX_CFLOWS)
1453 value = MAX_CFLOWS;
1454
1455 pkt_dev->cflows = value;
1456 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1457 return count;
1458 }
1459
1460 if (!strcmp(name, "flowlen")) {
1461 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001462 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001463 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 i += len;
1466 pkt_dev->lflow = value;
1467 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1468 return count;
1469 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001470
Robert Olsson45b270f2007-08-28 15:45:55 -07001471 if (!strcmp(name, "queue_map_min")) {
1472 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001473 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001474 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001475
Robert Olsson45b270f2007-08-28 15:45:55 -07001476 i += len;
1477 pkt_dev->queue_map_min = value;
1478 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1479 return count;
1480 }
1481
1482 if (!strcmp(name, "queue_map_max")) {
1483 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001484 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001485 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001486
Robert Olsson45b270f2007-08-28 15:45:55 -07001487 i += len;
1488 pkt_dev->queue_map_max = value;
1489 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1490 return count;
1491 }
1492
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001493 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001494 unsigned n, cnt;
1495
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001496 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001497 if (len < 0)
1498 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001499 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001500 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001501 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001502 cnt += sprintf(pg_result + cnt,
1503 "%08x%s", ntohl(pkt_dev->labels[n]),
1504 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001505
1506 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1507 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1508 pkt_dev->svlan_id = 0xffff;
1509
1510 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001511 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001512 }
1513 return count;
1514 }
1515
1516 if (!strcmp(name, "vlan_id")) {
1517 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001518 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001519 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001520
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001521 i += len;
1522 if (value <= 4095) {
1523 pkt_dev->vlan_id = value; /* turn on VLAN */
1524
1525 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001526 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001527
1528 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001529 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001530
1531 pkt_dev->nr_labels = 0; /* turn off MPLS */
1532 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1533 } else {
1534 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1535 pkt_dev->svlan_id = 0xffff;
1536
1537 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001538 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001539 }
1540 return count;
1541 }
1542
1543 if (!strcmp(name, "vlan_p")) {
1544 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001545 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001546 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001547
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001548 i += len;
1549 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1550 pkt_dev->vlan_p = value;
1551 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1552 } else {
1553 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1554 }
1555 return count;
1556 }
1557
1558 if (!strcmp(name, "vlan_cfi")) {
1559 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001560 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001561 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001562
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001563 i += len;
1564 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1565 pkt_dev->vlan_cfi = value;
1566 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1567 } else {
1568 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1569 }
1570 return count;
1571 }
1572
1573 if (!strcmp(name, "svlan_id")) {
1574 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001575 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001576 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001577
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001578 i += len;
1579 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1580 pkt_dev->svlan_id = value; /* turn on SVLAN */
1581
1582 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001583 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001584
1585 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001586 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001587
1588 pkt_dev->nr_labels = 0; /* turn off MPLS */
1589 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1590 } else {
1591 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1592 pkt_dev->svlan_id = 0xffff;
1593
1594 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001595 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001596 }
1597 return count;
1598 }
1599
1600 if (!strcmp(name, "svlan_p")) {
1601 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001602 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001603 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001604
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001605 i += len;
1606 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1607 pkt_dev->svlan_p = value;
1608 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1609 } else {
1610 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1611 }
1612 return count;
1613 }
1614
1615 if (!strcmp(name, "svlan_cfi")) {
1616 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001617 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001618 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001619
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001620 i += len;
1621 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1622 pkt_dev->svlan_cfi = value;
1623 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1624 } else {
1625 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1626 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001627 return count;
1628 }
1629
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001630 if (!strcmp(name, "tos")) {
1631 __u32 tmp_value = 0;
1632 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001633 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001634 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001635
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001636 i += len;
1637 if (len == 2) {
1638 pkt_dev->tos = tmp_value;
1639 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1640 } else {
1641 sprintf(pg_result, "ERROR: tos must be 00-ff");
1642 }
1643 return count;
1644 }
1645
1646 if (!strcmp(name, "traffic_class")) {
1647 __u32 tmp_value = 0;
1648 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001649 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001650 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001651
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001652 i += len;
1653 if (len == 2) {
1654 pkt_dev->traffic_class = tmp_value;
1655 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1656 } else {
1657 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1658 }
1659 return count;
1660 }
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1663 return -EINVAL;
1664}
1665
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001666static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001668 return single_open(file, pktgen_if_show, PDE(inode)->data);
1669}
1670
Arjan van de Ven9a321442007-02-12 00:55:35 -08001671static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001672 .owner = THIS_MODULE,
1673 .open = pktgen_if_open,
1674 .read = seq_read,
1675 .llseek = seq_lseek,
1676 .write = pktgen_if_write,
1677 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001678};
1679
1680static int pktgen_thread_show(struct seq_file *seq, void *v)
1681{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001682 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001683 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001685 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Luiz Capitulino222f1802006-03-20 22:16:13 -08001687 seq_printf(seq, "Running: ");
1688
1689 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001690 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001691 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001692 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Luiz Capitulino222f1802006-03-20 22:16:13 -08001694 seq_printf(seq, "\nStopped: ");
1695
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001696 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001697 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001698 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001701 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001703 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Luiz Capitulino222f1802006-03-20 22:16:13 -08001705 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001710static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001711 const char __user * user_buffer,
1712 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001714 struct seq_file *seq = (struct seq_file *)file->private_data;
1715 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int i = 0, max, len, ret;
1717 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001718 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001721 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return -EINVAL;
1723 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001726 len = count_trail_chars(&user_buffer[i], max);
1727 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001728 return len;
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* Read variable name */
1733
1734 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001736 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 memset(name, 0, sizeof(name));
1739 if (copy_from_user(name, &user_buffer[i], len))
1740 return -EFAULT;
1741 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001742
Luiz Capitulino222f1802006-03-20 22:16:13 -08001743 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001745 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001746 return len;
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 i += len;
1749
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001750 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001751 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1752 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Luiz Capitulino222f1802006-03-20 22:16:13 -08001754 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001755 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 ret = -EINVAL;
1757 goto out;
1758 }
1759
1760 pg_result = &(t->result[0]);
1761
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762 if (!strcmp(name, "add_device")) {
1763 char f[32];
1764 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001766 if (len < 0) {
1767 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto out;
1769 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001770 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return -EFAULT;
1772 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001773 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001775 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001776 ret = count;
1777 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto out;
1779 }
1780
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001782 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001783 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001784 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001785 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001787 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto out;
1789 }
1790
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001792 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001793 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto out;
1795 }
1796
1797 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001798out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return ret;
1800}
1801
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001802static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001804 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Arjan van de Ven9a321442007-02-12 00:55:35 -08001807static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001808 .owner = THIS_MODULE,
1809 .open = pktgen_thread_open,
1810 .read = seq_read,
1811 .llseek = seq_lseek,
1812 .write = pktgen_thread_write,
1813 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001814};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001817static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 struct pktgen_thread *t;
1820 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001821 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001823 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001824 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001826 if (remove) {
1827 if_lock(t);
1828 pkt_dev->removal_mark = 1;
1829 t->control |= T_REMDEV;
1830 if_unlock(t);
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 break;
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001835 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001838/*
1839 * mark a device for removal
1840 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001841static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001844 const int max_tries = 10, msec_per_try = 125;
1845 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001846
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001847 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001848 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001849
Luiz Capitulino222f1802006-03-20 22:16:13 -08001850 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001851
1852 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001853 if (pkt_dev == NULL)
1854 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001855
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001856 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001857 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1858 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001859 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001860 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001861
1862 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001863 printk(KERN_ERR "pktgen_mark_device: timed out after "
1864 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001865 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001866 break;
1867 }
1868
1869 }
1870
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001871 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001872}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001873
Stephen Hemminger39df2322007-03-04 16:11:51 -08001874static void pktgen_change_name(struct net_device *dev)
1875{
1876 struct pktgen_thread *t;
1877
1878 list_for_each_entry(t, &pktgen_threads, th_list) {
1879 struct pktgen_dev *pkt_dev;
1880
1881 list_for_each_entry(pkt_dev, &t->if_list, list) {
1882 if (pkt_dev->odev != dev)
1883 continue;
1884
1885 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1886
Alexey Dobriyan29753152009-08-28 23:34:43 -07001887 pkt_dev->entry = proc_create_data(dev->name, 0600,
1888 pg_proc_dir,
1889 &pktgen_if_fops,
1890 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001891 if (!pkt_dev->entry)
1892 printk(KERN_ERR "pktgen: can't move proc "
1893 " entry for '%s'\n", dev->name);
1894 break;
1895 }
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Luiz Capitulino222f1802006-03-20 22:16:13 -08001899static int pktgen_device_event(struct notifier_block *unused,
1900 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001902 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001904 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001905 return NOTIFY_DONE;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /* It is OK that we do not hold the group lock right now,
1908 * as we run under the RTNL lock.
1909 */
1910
1911 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001912 case NETDEV_CHANGENAME:
1913 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001917 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 return NOTIFY_DONE;
1922}
1923
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001924static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1925 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001926{
1927 char b[IFNAMSIZ+5];
1928 int i = 0;
1929
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001930 for (i = 0; ifname[i] != '@'; i++) {
1931 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001932 break;
1933
1934 b[i] = ifname[i];
1935 }
1936 b[i] = 0;
1937
1938 return dev_get_by_name(&init_net, b);
1939}
1940
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942/* Associate pktgen_dev with a device. */
1943
Stephen Hemminger39df2322007-03-04 16:11:51 -08001944static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001945{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001947 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (pkt_dev->odev) {
1951 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001952 pkt_dev->odev = NULL;
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Robert Olssone6fce5b2008-08-07 02:23:01 -07001955 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001957 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001958 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001962 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001963 err = -EINVAL;
1964 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001965 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001966 err = -ENETDOWN;
1967 } else {
1968 pkt_dev->odev = odev;
1969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001973 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
1976/* Read pkt_dev from the interface and set up internal pktgen_dev
1977 * structure to have the right information to create/send packets
1978 */
1979static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1980{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001981 int ntxq;
1982
Luiz Capitulino222f1802006-03-20 22:16:13 -08001983 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001984 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1985 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001986 sprintf(pkt_dev->result,
1987 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1988 return;
1989 }
1990
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001991 /* make sure that we don't pick a non-existing transmit queue */
1992 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08001993
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001994 if (ntxq <= pkt_dev->queue_map_min) {
1995 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07001996 "queue_map_min (zero-based) (%d) exceeds valid range "
1997 "[0 - %d] for (%d) queues on %s, resetting\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001998 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
Eric Dumazet593f63b2009-11-23 01:44:37 +00001999 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002000 pkt_dev->queue_map_min = ntxq - 1;
2001 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002002 if (pkt_dev->queue_map_max >= ntxq) {
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002003 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07002004 "queue_map_max (zero-based) (%d) exceeds valid range "
2005 "[0 - %d] for (%d) queues on %s, resetting\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002006 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
Eric Dumazet593f63b2009-11-23 01:44:37 +00002007 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002008 pkt_dev->queue_map_max = ntxq - 1;
2009 }
2010
Luiz Capitulino222f1802006-03-20 22:16:13 -08002011 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002013 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002014 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Luiz Capitulino222f1802006-03-20 22:16:13 -08002016 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002017 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Luiz Capitulino222f1802006-03-20 22:16:13 -08002019 /* Set up pkt size */
2020 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2021
2022 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002024 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 * gets exported
2026 */
2027
2028#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002029 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 struct inet6_dev *idev;
2031
Luiz Capitulino222f1802006-03-20 22:16:13 -08002032 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2033 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 set = 1;
2035 break;
2036 }
2037
Luiz Capitulino222f1802006-03-20 22:16:13 -08002038 if (!set) {
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /*
2041 * Use linklevel address if unconfigured.
2042 *
2043 * use ipv6_get_lladdr if/when it's get exported
2044 */
2045
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002046 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002047 idev = __in6_dev_get(pkt_dev->odev);
2048 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 struct inet6_ifaddr *ifp;
2050
2051 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002052 for (ifp = idev->addr_list; ifp;
2053 ifp = ifp->if_next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002054 if (ifp->scope == IFA_LINK &&
2055 !(ifp->flags & IFA_F_TENTATIVE)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002056 ipv6_addr_copy(&pkt_dev->
2057 cur_in6_saddr,
2058 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 err = 0;
2060 break;
2061 }
2062 }
2063 read_unlock_bh(&idev->lock);
2064 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002065 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002066 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002067 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2068 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002071 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 pkt_dev->saddr_min = 0;
2073 pkt_dev->saddr_max = 0;
2074 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002075
2076 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002079 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (in_dev) {
2081 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002082 pkt_dev->saddr_min =
2083 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 pkt_dev->saddr_max = pkt_dev->saddr_min;
2085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002088 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2090 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2091 }
2092
2093 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2094 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2095 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002096 /* Initialize current values. */
2097 pkt_dev->cur_dst_mac_offset = 0;
2098 pkt_dev->cur_src_mac_offset = 0;
2099 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2100 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2101 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2102 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 pkt_dev->nflows = 0;
2104}
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002107static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2108{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002109 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002110 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002111 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002112
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002113 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2114 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002115
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002116 remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002117 if (remaining <= 0) {
2118 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002119 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002120 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002121
Stephen Hemmingeref879792009-09-22 19:41:43 +00002122 start_time = ktime_now();
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002123 if (remaining < 100)
2124 udelay(remaining); /* really small just spin */
2125 else {
2126 /* see do_nanosleep */
2127 hrtimer_init_sleeper(&t, current);
2128 do {
2129 set_current_state(TASK_INTERRUPTIBLE);
2130 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2131 if (!hrtimer_active(&t.timer))
2132 t.task = NULL;
2133
2134 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002137 hrtimer_cancel(&t.timer);
2138 } while (t.task && pkt_dev->running && !signal_pending(current));
2139 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002141 end_time = ktime_now();
2142
2143 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2144 pkt_dev->next_tx = ktime_add_ns(end_time, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002147static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2148{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002149 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002150 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2151 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2152 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2153}
2154
Stephen Hemminger648fda72009-08-27 13:55:07 +00002155static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002156{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002157 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002158}
2159
2160static inline int f_pick(struct pktgen_dev *pkt_dev)
2161{
2162 int flow = pkt_dev->curfl;
2163
2164 if (pkt_dev->flags & F_FLOW_SEQ) {
2165 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2166 /* reset time */
2167 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002168 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002169 pkt_dev->curfl += 1;
2170 if (pkt_dev->curfl >= pkt_dev->cflows)
2171 pkt_dev->curfl = 0; /*reset */
2172 }
2173 } else {
2174 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002175 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002176
Robert Olsson1211a642008-08-05 18:44:26 -07002177 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002178 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002179 pkt_dev->flows[flow].flags = 0;
2180 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002181 }
2182
2183 return pkt_dev->curfl;
2184}
2185
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002186
2187#ifdef CONFIG_XFRM
2188/* If there was already an IPSEC SA, we keep it as is, else
2189 * we go look for it ...
2190*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002191static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002192{
2193 struct xfrm_state *x = pkt_dev->flows[flow].x;
2194 if (!x) {
2195 /*slow path: we dont already have xfrm_state*/
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002196 x = xfrm_stateonly_find(&init_net,
2197 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002198 (xfrm_address_t *)&pkt_dev->cur_saddr,
2199 AF_INET,
2200 pkt_dev->ipsmode,
2201 pkt_dev->ipsproto, 0);
2202 if (x) {
2203 pkt_dev->flows[flow].x = x;
2204 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002205 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002206 }
2207
2208 }
2209}
2210#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002211static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2212{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002213
2214 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2215 pkt_dev->cur_queue_map = smp_processor_id();
2216
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002217 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002218 __u16 t;
2219 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2220 t = random32() %
2221 (pkt_dev->queue_map_max -
2222 pkt_dev->queue_map_min + 1)
2223 + pkt_dev->queue_map_min;
2224 } else {
2225 t = pkt_dev->cur_queue_map + 1;
2226 if (t > pkt_dev->queue_map_max)
2227 t = pkt_dev->queue_map_min;
2228 }
2229 pkt_dev->cur_queue_map = t;
2230 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002231 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 -07002232}
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234/* Increment/randomize headers according to flags and current values
2235 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2236 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002237static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2238{
2239 __u32 imn;
2240 __u32 imx;
2241 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002243 if (pkt_dev->cflows)
2244 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002247 if (pkt_dev->src_mac_count > 1) {
2248 __u32 mc;
2249 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Luiz Capitulino222f1802006-03-20 22:16:13 -08002251 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002252 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002253 else {
2254 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002255 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002256 pkt_dev->src_mac_count)
2257 pkt_dev->cur_src_mac_offset = 0;
2258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Luiz Capitulino222f1802006-03-20 22:16:13 -08002260 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2261 pkt_dev->hh[11] = tmp;
2262 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2263 pkt_dev->hh[10] = tmp;
2264 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2265 pkt_dev->hh[9] = tmp;
2266 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2267 pkt_dev->hh[8] = tmp;
2268 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2269 pkt_dev->hh[7] = tmp;
2270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Luiz Capitulino222f1802006-03-20 22:16:13 -08002272 /* Deal with Destination MAC */
2273 if (pkt_dev->dst_mac_count > 1) {
2274 __u32 mc;
2275 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Luiz Capitulino222f1802006-03-20 22:16:13 -08002277 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002278 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Luiz Capitulino222f1802006-03-20 22:16:13 -08002280 else {
2281 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002282 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002283 pkt_dev->dst_mac_count) {
2284 pkt_dev->cur_dst_mac_offset = 0;
2285 }
2286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Luiz Capitulino222f1802006-03-20 22:16:13 -08002288 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2289 pkt_dev->hh[5] = tmp;
2290 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2291 pkt_dev->hh[4] = tmp;
2292 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2293 pkt_dev->hh[3] = tmp;
2294 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2295 pkt_dev->hh[2] = tmp;
2296 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2297 pkt_dev->hh[1] = tmp;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002300 if (pkt_dev->flags & F_MPLS_RND) {
2301 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002302 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002303 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2304 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002305 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002306 htonl(0x000fffff));
2307 }
2308
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002309 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002310 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002311 }
2312
2313 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002314 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002315 }
2316
Luiz Capitulino222f1802006-03-20 22:16:13 -08002317 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2318 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002319 pkt_dev->cur_udp_src = random32() %
2320 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2321 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 pkt_dev->cur_udp_src++;
2325 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2326 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002327 }
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Luiz Capitulino222f1802006-03-20 22:16:13 -08002330 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2331 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002332 pkt_dev->cur_udp_dst = random32() %
2333 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2334 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002335 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002337 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002339 }
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 if (!(pkt_dev->flags & F_IPV6)) {
2343
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002344 imn = ntohl(pkt_dev->saddr_min);
2345 imx = ntohl(pkt_dev->saddr_max);
2346 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002348 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002349 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 else {
2351 t = ntohl(pkt_dev->cur_saddr);
2352 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002353 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
2357 pkt_dev->cur_saddr = htonl(t);
2358 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002359
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002360 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2362 } else {
Al Viro252e3342006-11-14 20:48:11 -08002363 imn = ntohl(pkt_dev->daddr_min);
2364 imx = ntohl(pkt_dev->daddr_max);
2365 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002367 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (pkt_dev->flags & F_IPDST_RND) {
2369
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002370 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002371 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Joe Perches21cf2252007-12-16 13:44:00 -08002373 while (ipv4_is_loopback(s) ||
2374 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002375 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002376 ipv4_is_zeronet(s) ||
2377 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002378 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002379 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Al Viro252e3342006-11-14 20:48:11 -08002381 pkt_dev->cur_daddr = s;
2382 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 t = ntohl(pkt_dev->cur_daddr);
2384 t++;
2385 if (t > imx) {
2386 t = imn;
2387 }
2388 pkt_dev->cur_daddr = htonl(t);
2389 }
2390 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002391 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002392 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002393 pkt_dev->flows[flow].cur_daddr =
2394 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002395#ifdef CONFIG_XFRM
2396 if (pkt_dev->flags & F_IPSEC_ON)
2397 get_ipsec_sa(pkt_dev, flow);
2398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 pkt_dev->nflows++;
2400 }
2401 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002402 } else { /* IPV6 * */
2403
2404 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2405 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2406 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2407 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 else {
2409 int i;
2410
2411 /* Only random destinations yet */
2412
Luiz Capitulino222f1802006-03-20 22:16:13 -08002413 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002415 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002416 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2417 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421
Luiz Capitulino222f1802006-03-20 22:16:13 -08002422 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2423 __u32 t;
2424 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002425 t = random32() %
2426 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2427 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002428 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002430 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002432 }
2433 pkt_dev->cur_pkt_size = t;
2434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002436 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 pkt_dev->flows[flow].count++;
2439}
2440
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002441
2442#ifdef CONFIG_XFRM
2443static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2444{
2445 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2446 int err = 0;
2447 struct iphdr *iph;
2448
2449 if (!x)
2450 return 0;
2451 /* XXX: we dont support tunnel mode for now until
2452 * we resolve the dst issue */
2453 if (x->props.mode != XFRM_MODE_TRANSPORT)
2454 return 0;
2455
2456 spin_lock(&x->lock);
2457 iph = ip_hdr(skb);
2458
Herbert Xu13996372007-10-17 21:35:51 -07002459 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002460 if (err)
2461 goto error;
2462 err = x->type->output(x, skb);
2463 if (err)
2464 goto error;
2465
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002466 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002467 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002468error:
2469 spin_unlock(&x->lock);
2470 return err;
2471}
2472
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002473static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002474{
2475 if (pkt_dev->cflows) {
2476 /* let go of the SAs if we have them */
2477 int i = 0;
Florian Westphal5b5f7922009-05-21 15:07:12 -07002478 for (; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002479 struct xfrm_state *x = pkt_dev->flows[i].x;
2480 if (x) {
2481 xfrm_state_put(x);
2482 pkt_dev->flows[i].x = NULL;
2483 }
2484 }
2485 }
2486}
2487
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002488static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002489 struct sk_buff *skb, __be16 protocol)
2490{
2491 if (pkt_dev->flags & F_IPSEC_ON) {
2492 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2493 int nhead = 0;
2494 if (x) {
2495 int ret;
2496 __u8 *eth;
2497 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002498 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002499 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2500 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002501 printk(KERN_ERR "Error expanding "
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002502 "ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002503 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002504 }
2505 }
2506
2507 /* ipsec is not expecting ll header */
2508 skb_pull(skb, ETH_HLEN);
2509 ret = pktgen_output_ipsec(skb, pkt_dev);
2510 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002511 printk(KERN_ERR "Error creating ipsec "
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002512 "packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002513 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002514 }
2515 /* restore ll */
2516 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2517 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002518 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002519 }
2520 }
2521 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002522err:
2523 kfree_skb(skb);
2524 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002525}
2526#endif
2527
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002528static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2529{
2530 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002531 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002532 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002533
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002534 mpls--;
2535 *mpls |= MPLS_STACK_BOTTOM;
2536}
2537
Al Viro0f37c602006-11-03 03:49:56 -08002538static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2539 unsigned int prio)
2540{
2541 return htons(id | (cfi << 12) | (prio << 13));
2542}
2543
Luiz Capitulino222f1802006-03-20 22:16:13 -08002544static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2545 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
2547 struct sk_buff *skb = NULL;
2548 __u8 *eth;
2549 struct udphdr *udph;
2550 int datalen, iplen;
2551 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002552 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002553 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002554 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002555 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2556 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2557 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2558 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002559 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002560
2561 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002562 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002563
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002564 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002565 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002566
Robert Olsson64053be2005-06-26 15:27:10 -07002567 /* Update any of the values, used when we're incrementing various
2568 * fields.
2569 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002570 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002571 mod_cur_headers(pkt_dev);
2572
David S. Miller7ac54592006-01-18 14:19:10 -08002573 datalen = (odev->hard_header_len + 16) & ~0xf;
Stephen Hemmingere4707572009-08-27 13:55:13 +00002574 skb = __netdev_alloc_skb(odev,
2575 pkt_dev->cur_pkt_size + 64
2576 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (!skb) {
2578 sprintf(pkt_dev->result, "No memory");
2579 return NULL;
2580 }
2581
David S. Miller7ac54592006-01-18 14:19:10 -08002582 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 /* Reserve for ethernet and IP header */
2585 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002586 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2587 if (pkt_dev->nr_labels)
2588 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002589
2590 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002591 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002592 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002593 *svlan_tci = build_tci(pkt_dev->svlan_id,
2594 pkt_dev->svlan_cfi,
2595 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002596 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002597 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002598 }
2599 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002600 *vlan_tci = build_tci(pkt_dev->vlan_id,
2601 pkt_dev->vlan_cfi,
2602 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002603 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002604 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002605 }
2606
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002607 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002608 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002609 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002610 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002611 iph = ip_hdr(skb);
2612 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002615 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002617 /* Eth + IPh + UDPh + mpls */
2618 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002619 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002620 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 udph->source = htons(pkt_dev->cur_udp_src);
2624 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002625 udph->len = htons(datalen + 8); /* DATA + udphdr */
2626 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 iph->ihl = 5;
2629 iph->version = 4;
2630 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002631 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002632 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 iph->saddr = pkt_dev->cur_saddr;
2634 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002635 iph->id = htons(pkt_dev->ip_id);
2636 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 iph->frag_off = 0;
2638 iplen = 20 + 8 + datalen;
2639 iph->tot_len = htons(iplen);
2640 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002641 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002642 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002643 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002644 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 skb->dev = odev;
2646 skb->pkt_type = PACKET_HOST;
2647
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002648 if (pkt_dev->nfrags <= 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002649 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002650 memset(pgh + 1, 0, datalen - sizeof(struct pktgen_hdr));
2651 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 int frags = pkt_dev->nfrags;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002653 int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Luiz Capitulino222f1802006-03-20 22:16:13 -08002655 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (frags > MAX_SKB_FRAGS)
2658 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002659 if (datalen > frags * PAGE_SIZE) {
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002660 len = datalen - frags * PAGE_SIZE;
2661 memset(skb_put(skb, len), 0, len);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002662 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664
2665 i = 0;
2666 while (datalen > 0) {
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002667 struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 skb_shinfo(skb)->frags[i].page = page;
2669 skb_shinfo(skb)->frags[i].page_offset = 0;
2670 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002671 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 datalen -= skb_shinfo(skb)->frags[i].size;
2673 skb->len += skb_shinfo(skb)->frags[i].size;
2674 skb->data_len += skb_shinfo(skb)->frags[i].size;
2675 i++;
2676 skb_shinfo(skb)->nr_frags = i;
2677 }
2678
2679 while (i < frags) {
2680 int rem;
2681
2682 if (i == 0)
2683 break;
2684
2685 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2686 if (rem == 0)
2687 break;
2688
2689 skb_shinfo(skb)->frags[i - 1].size -= rem;
2690
Luiz Capitulino222f1802006-03-20 22:16:13 -08002691 skb_shinfo(skb)->frags[i] =
2692 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002694 skb_shinfo(skb)->frags[i].page =
2695 skb_shinfo(skb)->frags[i - 1].page;
2696 skb_shinfo(skb)->frags[i].page_offset +=
2697 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 skb_shinfo(skb)->frags[i].size = rem;
2699 i++;
2700 skb_shinfo(skb)->nr_frags = i;
2701 }
2702 }
2703
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002704 /* Stamp the time, and sequence number,
2705 * convert them to network byte order
2706 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002707 if (pgh) {
2708 struct timeval timestamp;
2709
2710 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2711 pgh->seq_num = htonl(pkt_dev->seq_num);
2712
2713 do_gettimeofday(&timestamp);
2714 pgh->tv_sec = htonl(timestamp.tv_sec);
2715 pgh->tv_usec = htonl(timestamp.tv_usec);
2716 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002717
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002718#ifdef CONFIG_XFRM
2719 if (!process_ipsec(pkt_dev, skb, protocol))
2720 return NULL;
2721#endif
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return skb;
2724}
2725
2726/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002727 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2729 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002730 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 * Should be candidate for net/ipv4/utils.c
2732 * --ro
2733 */
2734
Luiz Capitulino222f1802006-03-20 22:16:13 -08002735static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
2737 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002738 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 unsigned long u;
2740 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002741 unsigned int prefixlen = 0;
2742 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002743 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002744 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Luiz Capitulino222f1802006-03-20 22:16:13 -08002746 for (i = 0; i < 16; i++)
2747 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 for (;;) {
2750 if (*s == ':') {
2751 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002752 if (s[1] == ':') { /* Found "::", skip to part 2 */
2753 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 len++;
2755 break;
2756 }
2757 s++;
2758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002760 u = simple_strtoul(s, &pos, 16);
2761 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002762 if (!i)
2763 return 0;
2764 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 /* the last 4 bytes may be written as IPv4 address */
2767
2768 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002769 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2770 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
2772 ip[prefixlen++] = (u >> 8);
2773 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002774 s += i;
2775 len += i;
2776 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return len;
2778 }
2779
2780/* part 2, after "::" */
2781 for (;;) {
2782 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002783 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 break;
2785 s++;
2786 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002787 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002789
2790 u = simple_strtol(s, &pos, 16);
2791 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002793 if (*s)
2794 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 break;
2796 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002797 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002799 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2800 sizeof(tmp));
2801 suffixlen += 4;
2802 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 break;
2804 }
2805 suffix[suffixlen++] = (u >> 8);
2806 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002807 s += i;
2808 len += i;
2809 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 break;
2811 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002812 for (i = 0; i < suffixlen; i++)
2813 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return len;
2815}
2816
Luiz Capitulino222f1802006-03-20 22:16:13 -08002817static char tohex(char hexdigit)
2818{
2819 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
Luiz Capitulino222f1802006-03-20 22:16:13 -08002822static int fmt_xlong(char *s, unsigned int i)
2823{
2824 char *bak = s;
2825 *s = tohex((i >> 12) & 0xf);
2826 if (s != bak || *s != '0')
2827 ++s;
2828 *s = tohex((i >> 8) & 0xf);
2829 if (s != bak || *s != '0')
2830 ++s;
2831 *s = tohex((i >> 4) & 0xf);
2832 if (s != bak || *s != '0')
2833 ++s;
2834 *s = tohex(i & 0xf);
2835 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Luiz Capitulino222f1802006-03-20 22:16:13 -08002838static unsigned int fmt_ip6(char *s, const char ip[16])
2839{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 unsigned int len;
2841 unsigned int i;
2842 unsigned int temp;
2843 unsigned int compressing;
2844 int j;
2845
Luiz Capitulino222f1802006-03-20 22:16:13 -08002846 len = 0;
2847 compressing = 0;
2848 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002851 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2852 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2853 temp = strlen(s);
2854 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002857 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2858 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (temp == 0) {
2860 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002861 compressing = 1;
2862 if (j == 0) {
2863 *s++ = ':';
2864 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
2866 }
2867 } else {
2868 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002869 compressing = 0;
2870 *s++ = ':';
2871 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873 i = fmt_xlong(s, temp);
2874 len += i;
2875 s += i;
2876 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 *s++ = ':';
2878 ++len;
2879 }
2880 }
2881 }
2882 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002883 *s++ = ':';
2884 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002886 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return len;
2888}
2889
Luiz Capitulino222f1802006-03-20 22:16:13 -08002890static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2891 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
2893 struct sk_buff *skb = NULL;
2894 __u8 *eth;
2895 struct udphdr *udph;
2896 int datalen;
2897 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002898 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002899 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002900 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002901 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2902 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2903 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2904 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002905 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002906
2907 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002908 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002909
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002910 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002911 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002912
Robert Olsson64053be2005-06-26 15:27:10 -07002913 /* Update any of the values, used when we're incrementing various
2914 * fields.
2915 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002916 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002917 mod_cur_headers(pkt_dev);
2918
Stephen Hemmingere4707572009-08-27 13:55:13 +00002919 skb = __netdev_alloc_skb(odev,
2920 pkt_dev->cur_pkt_size + 64
2921 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 if (!skb) {
2923 sprintf(pkt_dev->result, "No memory");
2924 return NULL;
2925 }
2926
2927 skb_reserve(skb, 16);
2928
2929 /* Reserve for ethernet and IP header */
2930 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002931 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2932 if (pkt_dev->nr_labels)
2933 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002934
2935 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002936 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002937 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002938 *svlan_tci = build_tci(pkt_dev->svlan_id,
2939 pkt_dev->svlan_cfi,
2940 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002941 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002942 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002943 }
2944 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002945 *vlan_tci = build_tci(pkt_dev->vlan_id,
2946 pkt_dev->vlan_cfi,
2947 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002948 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002949 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002950 }
2951
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002952 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002953 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002954 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002955 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002956 iph = ipv6_hdr(skb);
2957 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002960 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002961
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002962 /* Eth + IPh + UDPh + mpls */
2963 datalen = pkt_dev->cur_pkt_size - 14 -
2964 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002965 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Luiz Capitulino222f1802006-03-20 22:16:13 -08002967 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 datalen = sizeof(struct pktgen_hdr);
2969 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002970 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2971 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
2973
2974 udph->source = htons(pkt_dev->cur_udp_src);
2975 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002976 udph->len = htons(datalen + sizeof(struct udphdr));
2977 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002979 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002981 if (pkt_dev->traffic_class) {
2982 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002983 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002984 }
2985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 iph->hop_limit = 32;
2987
2988 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2989 iph->nexthdr = IPPROTO_UDP;
2990
2991 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2992 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2993
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002994 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002995 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002996 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 skb->dev = odev;
2998 skb->pkt_type = PACKET_HOST;
2999
Luiz Capitulino222f1802006-03-20 22:16:13 -08003000 if (pkt_dev->nfrags <= 0)
3001 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 else {
3003 int frags = pkt_dev->nfrags;
3004 int i;
3005
Luiz Capitulino222f1802006-03-20 22:16:13 -08003006 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (frags > MAX_SKB_FRAGS)
3009 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003010 if (datalen > frags * PAGE_SIZE) {
3011 skb_put(skb, datalen - frags * PAGE_SIZE);
3012 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
3014
3015 i = 0;
3016 while (datalen > 0) {
3017 struct page *page = alloc_pages(GFP_KERNEL, 0);
3018 skb_shinfo(skb)->frags[i].page = page;
3019 skb_shinfo(skb)->frags[i].page_offset = 0;
3020 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08003021 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 datalen -= skb_shinfo(skb)->frags[i].size;
3023 skb->len += skb_shinfo(skb)->frags[i].size;
3024 skb->data_len += skb_shinfo(skb)->frags[i].size;
3025 i++;
3026 skb_shinfo(skb)->nr_frags = i;
3027 }
3028
3029 while (i < frags) {
3030 int rem;
3031
3032 if (i == 0)
3033 break;
3034
3035 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3036 if (rem == 0)
3037 break;
3038
3039 skb_shinfo(skb)->frags[i - 1].size -= rem;
3040
Luiz Capitulino222f1802006-03-20 22:16:13 -08003041 skb_shinfo(skb)->frags[i] =
3042 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003044 skb_shinfo(skb)->frags[i].page =
3045 skb_shinfo(skb)->frags[i - 1].page;
3046 skb_shinfo(skb)->frags[i].page_offset +=
3047 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 skb_shinfo(skb)->frags[i].size = rem;
3049 i++;
3050 skb_shinfo(skb)->nr_frags = i;
3051 }
3052 }
3053
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003054 /* Stamp the time, and sequence number,
3055 * convert them to network byte order
3056 * should we update cloned packets too ?
3057 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003058 if (pgh) {
3059 struct timeval timestamp;
3060
3061 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3062 pgh->seq_num = htonl(pkt_dev->seq_num);
3063
3064 do_gettimeofday(&timestamp);
3065 pgh->tv_sec = htonl(timestamp.tv_sec);
3066 pgh->tv_usec = htonl(timestamp.tv_usec);
3067 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003068 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return skb;
3071}
3072
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003073static struct sk_buff *fill_packet(struct net_device *odev,
3074 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003076 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return fill_packet_ipv6(odev, pkt_dev);
3078 else
3079 return fill_packet_ipv4(odev, pkt_dev);
3080}
3081
Luiz Capitulino222f1802006-03-20 22:16:13 -08003082static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003084 pkt_dev->seq_num = 1;
3085 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003087 pkt_dev->tx_bytes = 0;
3088 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
3090
3091/* Set up structure for sending pkts, clear counters */
3092
3093static void pktgen_run(struct pktgen_thread *t)
3094{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003095 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 int started = 0;
3097
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003098 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003101 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103 /*
3104 * setup odev and create initial packet.
3105 */
3106 pktgen_setup_inject(pkt_dev);
3107
Luiz Capitulino222f1802006-03-20 22:16:13 -08003108 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003110 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003112 pkt_dev->started_at =
3113 pkt_dev->next_tx = ktime_now();
3114
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003115 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 strcpy(pkt_dev->result, "Starting");
3118 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003119 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 strcpy(pkt_dev->result, "Error starting");
3121 }
3122 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123 if (started)
3124 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127static void pktgen_stop_all_threads_ifs(void)
3128{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003129 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003131 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003133 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003134
3135 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003136 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003137
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003138 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
Stephen Hemminger648fda72009-08-27 13:55:07 +00003141static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003143 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003145 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003146 if (pkt_dev->running)
3147 return 1;
3148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003153 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Luiz Capitulino222f1802006-03-20 22:16:13 -08003155 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Luiz Capitulino222f1802006-03-20 22:16:13 -08003157 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Luiz Capitulino222f1802006-03-20 22:16:13 -08003161 if (signal_pending(current))
3162 goto signal;
3163 if_lock(t);
3164 }
3165 if_unlock(t);
3166 return 1;
3167signal:
3168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
3171static int pktgen_wait_all_threads_run(void)
3172{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003173 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003176 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003177
3178 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003180 if (sig == 0)
3181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003183
3184 if (sig == 0)
3185 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003187
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003188 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return sig;
3190}
3191
3192static void pktgen_run_all_threads(void)
3193{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003194 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003196 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003198 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003200 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003202
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003203 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003205 /* Propagate thread->control */
3206 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 pktgen_wait_all_threads_run();
3209}
3210
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003211static void pktgen_reset_all_threads(void)
3212{
3213 struct pktgen_thread *t;
3214
3215 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3216
3217 mutex_lock(&pktgen_thread_lock);
3218
3219 list_for_each_entry(t, &pktgen_threads, th_list)
3220 t->control |= (T_REMDEVALL);
3221
3222 mutex_unlock(&pktgen_thread_lock);
3223
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003224 /* Propagate thread->control */
3225 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003226
3227 pktgen_wait_all_threads_run();
3228}
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3231{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003232 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003233 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003234 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3235 pkt_dev->started_at);
3236 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003238 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3239 (unsigned long long)ktime_to_us(elapsed),
3240 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3241 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003242 (unsigned long long)pkt_dev->sofar,
3243 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003245 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3246 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Luiz Capitulino222f1802006-03-20 22:16:13 -08003248 bps = pps * 8 * pkt_dev->cur_pkt_size;
3249
3250 mbps = bps;
3251 do_div(mbps, 1000000);
3252 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3253 (unsigned long long)pps,
3254 (unsigned long long)mbps,
3255 (unsigned long long)bps,
3256 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003260static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003262 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003263
Luiz Capitulino222f1802006-03-20 22:16:13 -08003264 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003265 printk(KERN_WARNING "pktgen: interface: %s is already "
Eric Dumazet593f63b2009-11-23 01:44:37 +00003266 "stopped\n", pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003267 return -EINVAL;
3268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003270 kfree_skb(pkt_dev->skb);
3271 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003272 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003273 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003275 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Luiz Capitulino222f1802006-03-20 22:16:13 -08003277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Luiz Capitulino222f1802006-03-20 22:16:13 -08003280static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003282 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 if_lock(t);
3285
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003286 list_for_each_entry(pkt_dev, &t->if_list, list) {
3287 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003288 continue;
3289 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003290 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003291 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003292 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
3294 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003295 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
Luiz Capitulino222f1802006-03-20 22:16:13 -08003298static void pktgen_stop(struct pktgen_thread *t)
3299{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003300 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003302 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Luiz Capitulino222f1802006-03-20 22:16:13 -08003304 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003306 list_for_each_entry(pkt_dev, &t->if_list, list) {
3307 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Luiz Capitulino222f1802006-03-20 22:16:13 -08003310 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311}
3312
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003313/*
3314 * one of our devices needs to be removed - find it
3315 * and remove it
3316 */
3317static void pktgen_rem_one_if(struct pktgen_thread *t)
3318{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003319 struct list_head *q, *n;
3320 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003321
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003322 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003323
3324 if_lock(t);
3325
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003326 list_for_each_safe(q, n, &t->if_list) {
3327 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003328
Luiz Capitulino222f1802006-03-20 22:16:13 -08003329 if (!cur->removal_mark)
3330 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003331
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003332 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003333 cur->skb = NULL;
3334
3335 pktgen_remove_device(t, cur);
3336
3337 break;
3338 }
3339
3340 if_unlock(t);
3341}
3342
Luiz Capitulino222f1802006-03-20 22:16:13 -08003343static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003345 struct list_head *q, *n;
3346 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003347
3348 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003349
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003350 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003351 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003353 list_for_each_safe(q, n, &t->if_list) {
3354 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003355
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003356 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003357 cur->skb = NULL;
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 pktgen_remove_device(t, cur);
3360 }
3361
Luiz Capitulino222f1802006-03-20 22:16:13 -08003362 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
3364
Luiz Capitulino222f1802006-03-20 22:16:13 -08003365static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003367 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
David S. Milleree74baa2007-01-01 20:51:53 -08003369 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003371 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003373 list_del(&t->th_list);
3374
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003375 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
Stephen Hemmingeref879792009-09-22 19:41:43 +00003378static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003379{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003380 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003381 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003382 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003383}
3384
Stephen Hemmingeref879792009-09-22 19:41:43 +00003385static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3386{
3387 ktime_t idle_start = ktime_now();
3388
3389 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3390 if (signal_pending(current))
3391 break;
3392
3393 if (need_resched())
3394 pktgen_resched(pkt_dev);
3395 else
3396 cpu_relax();
3397 }
3398 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3399}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003400
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003401static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Stephen Hemminger00829822008-11-20 20:14:53 -08003403 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003404 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003405 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003406 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003407 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 int ret;
3409
Stephen Hemmingeref879792009-09-22 19:41:43 +00003410 /* If device is offline, then don't send */
3411 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3412 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003413 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003415
Stephen Hemmingeref879792009-09-22 19:41:43 +00003416 /* This is max DELAY, this has special meaning of
3417 * "never transmit"
3418 */
3419 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3420 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3421 return;
3422 }
3423
3424 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003425 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3426 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3427 /* build a new pkt */
3428 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003429
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003430 pkt_dev->skb = fill_packet(odev, pkt_dev);
3431 if (pkt_dev->skb == NULL) {
3432 printk(KERN_ERR "pktgen: ERROR: couldn't "
3433 "allocate skb in fill_packet.\n");
3434 schedule();
3435 pkt_dev->clone_count--; /* back out increment, OOM */
3436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003438 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003439 pkt_dev->allocated_skbs++;
3440 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003442
Stephen Hemmingeref879792009-09-22 19:41:43 +00003443 if (pkt_dev->delay && pkt_dev->last_ok)
3444 spin(pkt_dev, pkt_dev->next_tx);
3445
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003446 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3447 txq = netdev_get_tx_queue(odev, queue_map);
3448
3449 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003450
Eric Dumazet0835acf2009-09-30 13:03:33 +00003451 if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003452 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003453 pkt_dev->last_ok = 0;
3454 goto unlock;
3455 }
3456 atomic_inc(&(pkt_dev->skb->users));
3457 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Stephen Hemmingeref879792009-09-22 19:41:43 +00003459 switch (ret) {
3460 case NETDEV_TX_OK:
3461 txq_trans_update(txq);
3462 pkt_dev->last_ok = 1;
3463 pkt_dev->sofar++;
3464 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003465 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003466 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003467 case NET_XMIT_DROP:
3468 case NET_XMIT_CN:
3469 case NET_XMIT_POLICED:
3470 /* skb has been consumed */
3471 pkt_dev->errors++;
3472 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003473 default: /* Drivers are not supposed to return other values! */
3474 if (net_ratelimit())
3475 pr_info("pktgen: %s xmit error: %d\n",
Eric Dumazet593f63b2009-11-23 01:44:37 +00003476 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003477 pkt_dev->errors++;
3478 /* fallthru */
3479 case NETDEV_TX_LOCKED:
3480 case NETDEV_TX_BUSY:
3481 /* Retry it next time */
3482 atomic_dec(&(pkt_dev->skb->users));
3483 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003484 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003485unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003486 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 /* If pkt_dev->count is zero, then run forever */
3489 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003490 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003491
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 /* Done with this */
3493 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003494 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003497/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 * Main loop of the thread goes here
3499 */
3500
David S. Milleree74baa2007-01-01 20:51:53 -08003501static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
3503 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003504 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003505 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
David S. Milleree74baa2007-01-01 20:51:53 -08003508 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003511 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003513 pr_debug("pktgen: starting pktgen/%d: pid=%d\n",
3514 cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
David S. Milleree74baa2007-01-01 20:51:53 -08003516 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003518 set_freezable();
3519
David S. Milleree74baa2007-01-01 20:51:53 -08003520 while (!kthread_should_stop()) {
3521 pkt_dev = next_to_run(t);
3522
Stephen Hemmingeref879792009-09-22 19:41:43 +00003523 if (unlikely(!pkt_dev && t->control == 0)) {
3524 wait_event_interruptible_timeout(t->queue,
3525 t->control != 0,
3526 HZ/10);
3527 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003528 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 __set_current_state(TASK_RUNNING);
3531
Stephen Hemmingeref879792009-09-22 19:41:43 +00003532 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 pktgen_xmit(pkt_dev);
3534
Stephen Hemmingeref879792009-09-22 19:41:43 +00003535 if (need_resched())
3536 pktgen_resched(pkt_dev);
3537 else
3538 cpu_relax();
3539 }
3540
Luiz Capitulino222f1802006-03-20 22:16:13 -08003541 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 pktgen_stop(t);
3543 t->control &= ~(T_STOP);
3544 }
3545
Luiz Capitulino222f1802006-03-20 22:16:13 -08003546 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 pktgen_run(t);
3548 t->control &= ~(T_RUN);
3549 }
3550
Luiz Capitulino222f1802006-03-20 22:16:13 -08003551 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003553 t->control &= ~(T_REMDEVALL);
3554 }
3555
Luiz Capitulino222f1802006-03-20 22:16:13 -08003556 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003557 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 t->control &= ~(T_REMDEV);
3559 }
3560
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003561 try_to_freeze();
3562
David S. Milleree74baa2007-01-01 20:51:53 -08003563 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003566 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003567 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003569 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003570 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003572 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003573 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003574
David S. Milleree74baa2007-01-01 20:51:53 -08003575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Luiz Capitulino222f1802006-03-20 22:16:13 -08003578static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003579 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003581 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003582 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Eric Dumazet3e984842009-11-24 14:50:53 -08003584 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003585 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003586 if (strncmp(p->odevname, ifname, len) == 0) {
3587 if (p->odevname[len]) {
3588 if (exact || p->odevname[len] != '@')
3589 continue;
3590 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003591 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003592 break;
3593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Luiz Capitulino222f1802006-03-20 22:16:13 -08003595 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003596 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003597 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003600/*
3601 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 */
3603
Luiz Capitulino222f1802006-03-20 22:16:13 -08003604static int add_dev_to_thread(struct pktgen_thread *t,
3605 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
3607 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Luiz Capitulino222f1802006-03-20 22:16:13 -08003609 if_lock(t);
3610
3611 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003612 printk(KERN_ERR "pktgen: ERROR: already assigned "
3613 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003614 rv = -EBUSY;
3615 goto out;
3616 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003617
3618 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003619 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 pkt_dev->running = 0;
3621
Luiz Capitulino222f1802006-03-20 22:16:13 -08003622out:
3623 if_unlock(t);
3624 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
3627/* Called under thread lock */
3628
Luiz Capitulino222f1802006-03-20 22:16:13 -08003629static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003631 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003632 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003633 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 /* We don't allow a device to be on several threads */
3636
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003637 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3638 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003639 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003640 return -EBUSY;
3641 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003642
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003643 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003644 if (!pkt_dev)
3645 return -ENOMEM;
3646
Eric Dumazet593f63b2009-11-23 01:44:37 +00003647 strcpy(pkt_dev->odevname, ifname);
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003648 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3649 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003650 if (pkt_dev->flows == NULL) {
3651 kfree(pkt_dev);
3652 return -ENOMEM;
3653 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003654 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003655
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003656 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003657 pkt_dev->min_pkt_size = ETH_ZLEN;
3658 pkt_dev->max_pkt_size = ETH_ZLEN;
3659 pkt_dev->nfrags = 0;
3660 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003661 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003662 pkt_dev->count = pg_count_d;
3663 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003664 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003665 pkt_dev->udp_src_max = 9;
3666 pkt_dev->udp_dst_min = 9;
3667 pkt_dev->udp_dst_max = 9;
3668
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003669 pkt_dev->vlan_p = 0;
3670 pkt_dev->vlan_cfi = 0;
3671 pkt_dev->vlan_id = 0xffff;
3672 pkt_dev->svlan_p = 0;
3673 pkt_dev->svlan_cfi = 0;
3674 pkt_dev->svlan_id = 0xffff;
3675
Stephen Hemminger39df2322007-03-04 16:11:51 -08003676 err = pktgen_setup_dev(pkt_dev, ifname);
3677 if (err)
3678 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003679
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003680 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3681 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003682 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003683 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003684 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003685 err = -EINVAL;
3686 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003687 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003688#ifdef CONFIG_XFRM
3689 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3690 pkt_dev->ipsproto = IPPROTO_ESP;
3691#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003692
3693 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003694out2:
3695 dev_put(pkt_dev->odev);
3696out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003697#ifdef CONFIG_XFRM
3698 free_SAs(pkt_dev);
3699#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003700 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003701 kfree(pkt_dev);
3702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703}
3704
David S. Milleree74baa2007-01-01 20:51:53 -08003705static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003707 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003708 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003709 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003711 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3712 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003713 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003714 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3715 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003716 return -ENOMEM;
3717 }
3718
Luiz Capitulino222f1802006-03-20 22:16:13 -08003719 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003721
David S. Milleree74baa2007-01-01 20:51:53 -08003722 INIT_LIST_HEAD(&t->if_list);
3723
3724 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003725 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003726
3727 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3728 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003729 printk(KERN_ERR "pktgen: kernel_thread() failed "
3730 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003731 list_del(&t->th_list);
3732 kfree(t);
3733 return PTR_ERR(p);
3734 }
3735 kthread_bind(p, cpu);
3736 t->tsk = p;
3737
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003738 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3739 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003740 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003741 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003742 PG_PROC_DIR, t->tsk->comm);
3743 kthread_stop(p);
3744 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003745 kfree(t);
3746 return -EINVAL;
3747 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003748
David S. Milleree74baa2007-01-01 20:51:53 -08003749 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003750 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 return 0;
3753}
3754
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003755/*
3756 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003758static void _rem_dev_from_if_list(struct pktgen_thread *t,
3759 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003761 struct list_head *q, *n;
3762 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003764 list_for_each_safe(q, n, &t->if_list) {
3765 p = list_entry(q, struct pktgen_dev, list);
3766 if (p == pkt_dev)
3767 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 }
3769}
3770
Luiz Capitulino222f1802006-03-20 22:16:13 -08003771static int pktgen_remove_device(struct pktgen_thread *t,
3772 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
3774
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003775 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Luiz Capitulino222f1802006-03-20 22:16:13 -08003777 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003778 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3779 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003780 pktgen_stop_device(pkt_dev);
3781 }
3782
3783 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
3785 if (pkt_dev->odev) {
3786 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003787 pkt_dev->odev = NULL;
3788 }
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 /* And update the thread if_list */
3791
3792 _rem_dev_from_if_list(t, pkt_dev);
3793
Stephen Hemminger39df2322007-03-04 16:11:51 -08003794 if (pkt_dev->entry)
3795 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003797#ifdef CONFIG_XFRM
3798 free_SAs(pkt_dev);
3799#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003800 vfree(pkt_dev->flows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803}
3804
Luiz Capitulino222f1802006-03-20 22:16:13 -08003805static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
3807 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003808 struct proc_dir_entry *pe;
3809
David S. Miller25a8b252007-07-30 16:11:48 -07003810 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003812 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003813 if (!pg_proc_dir)
3814 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Wang Chen25296d52008-02-28 14:11:49 -08003816 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003817 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003818 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3819 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003820 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003821 return -EINVAL;
3822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 /* Register us to receive netdevice events */
3825 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003826
John Hawkes670c02c2005-10-13 09:30:31 -07003827 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003828 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
David S. Milleree74baa2007-01-01 20:51:53 -08003830 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003831 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003832 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3833 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003834 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003835
3836 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003837 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3838 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003839 unregister_netdevice_notifier(&pktgen_notifier_block);
3840 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003841 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003842 return -ENODEV;
3843 }
3844
Luiz Capitulino222f1802006-03-20 22:16:13 -08003845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846}
3847
3848static void __exit pg_cleanup(void)
3849{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003850 struct pktgen_thread *t;
3851 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 wait_queue_head_t queue;
3853 init_waitqueue_head(&queue);
3854
Luiz Capitulino222f1802006-03-20 22:16:13 -08003855 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003857 list_for_each_safe(q, n, &pktgen_threads) {
3858 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003859 kthread_stop(t->tsk);
3860 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Luiz Capitulino222f1802006-03-20 22:16:13 -08003863 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 unregister_netdevice_notifier(&pktgen_notifier_block);
3865
Luiz Capitulino222f1802006-03-20 22:16:13 -08003866 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003867 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003868 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869}
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871module_init(pg_init);
3872module_exit(pg_cleanup);
3873
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003874MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875MODULE_DESCRIPTION("Packet Generator tool");
3876MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003877MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003879MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003881MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003883MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003885MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");