blob: 2001cb1cf5cc37e9eedaaf67c3f7231463166bd6 [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 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800195#define T_TERMINATE (1<<0)
196#define T_STOP (1<<1) /* Stop run */
197#define T_RUN (1<<2) /* Start run */
198#define T_REMDEVALL (1<<3) /* Remove all devs */
199#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/* If lock -- can be removed after some work */
202#define if_lock(t) spin_lock(&(t->if_lock));
203#define if_unlock(t) spin_unlock(&(t->if_lock));
204
205/* Used to help with determining the pkts on receive */
206#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700207#define PG_PROC_DIR "pktgen"
208#define PGCTRL "pgctrl"
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000209static struct proc_dir_entry *pg_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211#define MAX_CFLOWS 65536
212
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700213#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
214#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
215
Luiz Capitulino222f1802006-03-20 22:16:13 -0800216struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800217 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800218 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700219#ifdef CONFIG_XFRM
220 struct xfrm_state *x;
221#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700222 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700225/* flow flag bits */
226#define F_INIT (1<<0) /* flow has been initialized */
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /*
230 * Try to keep frequent/infrequent used vars. separated.
231 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800232 struct proc_dir_entry *entry; /* proc file */
233 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000234 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000236 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800237
238 /* If min != max, then we will either do a linear iteration, or
239 * we will do a random selection from within the range.
240 */
241 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800242 int removal_mark; /* non-zero => the device is marked for
243 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Luiz Capitulino222f1802006-03-20 22:16:13 -0800245 int min_pkt_size; /* = ETH_ZLEN; */
246 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700247 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800248 int nfrags;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000249 u64 delay; /* nano-seconds */
250
Luiz Capitulino222f1802006-03-20 22:16:13 -0800251 __u64 count; /* Default No packets to send */
252 __u64 sofar; /* How many pkts we've sent so far */
253 __u64 tx_bytes; /* How many bytes we've transmitted */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000254 __u64 errors; /* Errors when trying to transmit,
255 pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Luiz Capitulino222f1802006-03-20 22:16:13 -0800257 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Luiz Capitulino222f1802006-03-20 22:16:13 -0800259 __u64 allocated_skbs;
260 __u32 clone_count;
261 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000262 * Or a failed transmit of some sort?
263 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800264 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000265 ktime_t next_tx;
266 ktime_t started_at;
267 ktime_t stopped_at;
268 u64 idle_acc; /* nano-seconds */
269
Luiz Capitulino222f1802006-03-20 22:16:13 -0800270 __u32 seq_num;
271
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000272 int clone_skb; /*
273 * Use multiple SKBs during packet gen.
274 * If this number is greater than 1, then
275 * that many copies of the same packet will be
276 * sent before a new packet is allocated.
277 * If you want to send 1024 identical packets
278 * before creating a new packet,
279 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800280 */
281
282 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
283 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
284 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
285 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
286
287 struct in6_addr in6_saddr;
288 struct in6_addr in6_daddr;
289 struct in6_addr cur_in6_daddr;
290 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800292 struct in6_addr min_in6_daddr;
293 struct in6_addr max_in6_daddr;
294 struct in6_addr min_in6_saddr;
295 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Luiz Capitulino222f1802006-03-20 22:16:13 -0800297 /* If we're doing ranges, random or incremental, then this
298 * defines the min/max for those ranges.
299 */
Al Viro252e33462006-11-14 20:48:11 -0800300 __be32 saddr_min; /* inclusive, source IP address */
301 __be32 saddr_max; /* exclusive, source IP address */
302 __be32 daddr_min; /* inclusive, dest IP address */
303 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Luiz Capitulino222f1802006-03-20 22:16:13 -0800305 __u16 udp_src_min; /* inclusive, source UDP port */
306 __u16 udp_src_max; /* exclusive, source UDP port */
307 __u16 udp_dst_min; /* inclusive, dest UDP port */
308 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700310 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000311 __u8 tos; /* six MSB of (former) IPv4 TOS
312 are for dscp codepoint */
313 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
314 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700315
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800316 /* MPLS */
317 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
318 __be32 labels[MAX_MPLS_LABELS];
319
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700320 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
321 __u8 vlan_p;
322 __u8 vlan_cfi;
323 __u16 vlan_id; /* 0xffff means no vlan tag */
324
325 __u8 svlan_p;
326 __u8 svlan_cfi;
327 __u16 svlan_id; /* 0xffff means no svlan tag */
328
Luiz Capitulino222f1802006-03-20 22:16:13 -0800329 __u32 src_mac_count; /* How many MACs to iterate through */
330 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Luiz Capitulino222f1802006-03-20 22:16:13 -0800332 unsigned char dst_mac[ETH_ALEN];
333 unsigned char src_mac[ETH_ALEN];
334
335 __u32 cur_dst_mac_offset;
336 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800337 __be32 cur_saddr;
338 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800339 __u16 cur_udp_dst;
340 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700341 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800342 __u32 cur_pkt_size;
343
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800367 unsigned cflows; /* Concurrent flows (config) */
368 unsigned lflow; /* Flow length (config) */
369 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700370 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700371
372 u16 queue_map_min;
373 u16 queue_map_max;
374
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700375#ifdef CONFIG_XFRM
376 __u8 ipsmode; /* IPSEC mode (config) */
377 __u8 ipsproto; /* IPSEC type (config) */
378#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800379 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380};
381
382struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800383 __be32 pgh_magic;
384 __be32 seq_num;
385 __be32 tv_sec;
386 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387};
388
389struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000390 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a80162006-03-20 22:18:16 -0800391 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800392 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800393 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800394 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000396 /* Field for thread to receive "posted" events terminate,
397 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800398
399 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int cpu;
401
Luiz Capitulino222f1802006-03-20 22:16:13 -0800402 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700403 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
406#define REMOVE 1
407#define FIND 0
408
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000409static inline ktime_t ktime_now(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000411 struct timespec ts;
412 ktime_get_ts(&ts);
413
414 return timespec_to_ktime(ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000417/* This works even if 32 bit because of careful byte order choice */
418static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000420 return cmp1.tv64 < cmp2.tv64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000423static const char version[] =
424 "pktgen " VERSION ": Packet Generator for packet performance testing.\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Luiz Capitulino222f1802006-03-20 22:16:13 -0800426static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
427static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
428static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
429 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
431static void pktgen_run_all_threads(void);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800432static void pktgen_reset_all_threads(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static void pktgen_stop_all_threads_ifs(void);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000434
Luiz Capitulino222f1802006-03-20 22:16:13 -0800435static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800437
Luiz Capitulino222f1802006-03-20 22:16:13 -0800438static unsigned int scan_ip6(const char *s, char ip[16]);
439static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000442static int pg_count_d __read_mostly = 1000;
443static int pg_delay_d __read_mostly;
444static int pg_clone_skb_d __read_mostly;
445static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Luiz Capitulino222fa072006-03-20 22:24:27 -0800447static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800448static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static struct notifier_block pktgen_notifier_block = {
451 .notifier_call = pktgen_device_event,
452};
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900455 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *
457 */
458
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700459static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800460{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000461 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000465static ssize_t pgctrl_write(struct file *file, const char __user *buf,
466 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700469 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Luiz Capitulino222f1802006-03-20 22:16:13 -0800471 if (!capable(CAP_NET_ADMIN)) {
472 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700476 if (count > sizeof(data))
477 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700480 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700481 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800482 }
483 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Luiz Capitulino222f1802006-03-20 22:16:13 -0800485 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 pktgen_stop_all_threads_ifs();
487
Luiz Capitulino222f1802006-03-20 22:16:13 -0800488 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 pktgen_run_all_threads();
490
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800491 else if (!strcmp(data, "reset"))
492 pktgen_reset_all_threads();
493
Luiz Capitulino222f1802006-03-20 22:16:13 -0800494 else
David S. Miller25a8b252007-07-30 16:11:48 -0700495 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 err = count;
498
Luiz Capitulino222f1802006-03-20 22:16:13 -0800499out:
500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700503static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700505 return single_open(file, pgctrl_show, PDE(inode)->data);
506}
507
Arjan van de Ven9a321442007-02-12 00:55:35 -0800508static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800509 .owner = THIS_MODULE,
510 .open = pgctrl_open,
511 .read = seq_read,
512 .llseek = seq_lseek,
513 .write = pgctrl_write,
514 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700515};
516
517static int pktgen_if_show(struct seq_file *seq, void *v)
518{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000519 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000520 ktime_t stopped;
521 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Luiz Capitulino222f1802006-03-20 22:16:13 -0800523 seq_printf(seq,
524 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
525 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
526 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Luiz Capitulino222f1802006-03-20 22:16:13 -0800528 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000529 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
530 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800531 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Luiz Capitulino222f1802006-03-20 22:16:13 -0800533 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
534 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Robert Olsson45b270f2007-08-28 15:45:55 -0700536 seq_printf(seq,
537 " queue_map_min: %u queue_map_max: %u\n",
538 pkt_dev->queue_map_min,
539 pkt_dev->queue_map_max);
540
Luiz Capitulino222f1802006-03-20 22:16:13 -0800541 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800543 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
544 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
545 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
546 seq_printf(seq,
547 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
548 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Luiz Capitulino222f1802006-03-20 22:16:13 -0800550 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
551 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
552 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
553 seq_printf(seq,
554 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
555 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000557 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800558 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000559 " dst_min: %s dst_max: %s\n",
560 pkt_dev->dst_min, pkt_dev->dst_max);
561 seq_printf(seq,
562 " src_min: %s src_max: %s\n",
563 pkt_dev->src_min, pkt_dev->src_max);
564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700566 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Johannes Berge1749612008-10-27 15:59:26 -0700568 seq_printf(seq, "%pM ",
569 is_zero_ether_addr(pkt_dev->src_mac) ?
570 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Luiz Capitulino222f1802006-03-20 22:16:13 -0800572 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700573 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Luiz Capitulino222f1802006-03-20 22:16:13 -0800575 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000576 " udp_src_min: %d udp_src_max: %d"
577 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800578 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
579 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Luiz Capitulino222f1802006-03-20 22:16:13 -0800581 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800582 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700583 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800585 if (pkt_dev->nr_labels) {
586 unsigned i;
587 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700588 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800589 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
590 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
591 }
592
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000593 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700594 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000595 pkt_dev->vlan_id, pkt_dev->vlan_p,
596 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700597
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000598 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700599 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000600 pkt_dev->svlan_id, pkt_dev->svlan_p,
601 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700602
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000603 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700604 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700605
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000606 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700607 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700608
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800609 seq_printf(seq, " Flags: ");
610
Luiz Capitulino222f1802006-03-20 22:16:13 -0800611 if (pkt_dev->flags & F_IPV6)
612 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Luiz Capitulino222f1802006-03-20 22:16:13 -0800614 if (pkt_dev->flags & F_IPSRC_RND)
615 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Luiz Capitulino222f1802006-03-20 22:16:13 -0800617 if (pkt_dev->flags & F_IPDST_RND)
618 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Luiz Capitulino222f1802006-03-20 22:16:13 -0800620 if (pkt_dev->flags & F_TXSIZE_RND)
621 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Luiz Capitulino222f1802006-03-20 22:16:13 -0800623 if (pkt_dev->flags & F_UDPSRC_RND)
624 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Luiz Capitulino222f1802006-03-20 22:16:13 -0800626 if (pkt_dev->flags & F_UDPDST_RND)
627 seq_printf(seq, "UDPDST_RND ");
628
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800629 if (pkt_dev->flags & F_MPLS_RND)
630 seq_printf(seq, "MPLS_RND ");
631
Robert Olsson45b270f2007-08-28 15:45:55 -0700632 if (pkt_dev->flags & F_QUEUE_MAP_RND)
633 seq_printf(seq, "QUEUE_MAP_RND ");
634
Robert Olssone6fce5b2008-08-07 02:23:01 -0700635 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
636 seq_printf(seq, "QUEUE_MAP_CPU ");
637
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700638 if (pkt_dev->cflows) {
639 if (pkt_dev->flags & F_FLOW_SEQ)
640 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
641 else
642 seq_printf(seq, "FLOW_RND ");
643 }
644
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700645#ifdef CONFIG_XFRM
646 if (pkt_dev->flags & F_IPSEC_ON)
647 seq_printf(seq, "IPSEC ");
648#endif
649
Luiz Capitulino222f1802006-03-20 22:16:13 -0800650 if (pkt_dev->flags & F_MACSRC_RND)
651 seq_printf(seq, "MACSRC_RND ");
652
653 if (pkt_dev->flags & F_MACDST_RND)
654 seq_printf(seq, "MACDST_RND ");
655
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700656 if (pkt_dev->flags & F_VID_RND)
657 seq_printf(seq, "VID_RND ");
658
659 if (pkt_dev->flags & F_SVID_RND)
660 seq_printf(seq, "SVID_RND ");
661
Luiz Capitulino222f1802006-03-20 22:16:13 -0800662 seq_puts(seq, "\n");
663
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000664 /* not really stopped, more like last-running-at */
665 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
666 idle = pkt_dev->idle_acc;
667 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800668
669 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000670 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800671 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000672 (unsigned long long)pkt_dev->errors);
673
674 seq_printf(seq,
675 " started: %lluus stopped: %lluus idle: %lluus\n",
676 (unsigned long long) ktime_to_us(pkt_dev->started_at),
677 (unsigned long long) ktime_to_us(stopped),
678 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800679
680 seq_printf(seq,
681 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700682 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
683 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Luiz Capitulino222f1802006-03-20 22:16:13 -0800685 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800687 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
688 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
689 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
690 } else
691 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700692 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Luiz Capitulino222f1802006-03-20 22:16:13 -0800694 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700695 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Robert Olsson45b270f2007-08-28 15:45:55 -0700697 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
698
Luiz Capitulino222f1802006-03-20 22:16:13 -0800699 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800702 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800704 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800709
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000710static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
711 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800712{
713 int i = 0;
714 *num = 0;
715
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700716 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800717 char c;
718 *num <<= 4;
719 if (get_user(c, &user_buffer[i]))
720 return -EFAULT;
721 if ((c >= '0') && (c <= '9'))
722 *num |= c - '0';
723 else if ((c >= 'a') && (c <= 'f'))
724 *num |= c - 'a' + 10;
725 else if ((c >= 'A') && (c <= 'F'))
726 *num |= c - 'A' + 10;
727 else
728 break;
729 }
730 return i;
731}
732
Luiz Capitulino222f1802006-03-20 22:16:13 -0800733static int count_trail_chars(const char __user * user_buffer,
734 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 int i;
737
738 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800739 char c;
740 if (get_user(c, &user_buffer[i]))
741 return -EFAULT;
742 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 case '\"':
744 case '\n':
745 case '\r':
746 case '\t':
747 case ' ':
748 case '=':
749 break;
750 default:
751 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754done:
755 return i;
756}
757
Luiz Capitulino222f1802006-03-20 22:16:13 -0800758static unsigned long num_arg(const char __user * user_buffer,
759 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 int i = 0;
762 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800763
764 for (; i < maxlen; i++) {
765 char c;
766 if (get_user(c, &user_buffer[i]))
767 return -EFAULT;
768 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800770 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 } else
772 break;
773 }
774 return i;
775}
776
Luiz Capitulino222f1802006-03-20 22:16:13 -0800777static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 int i = 0;
780
Luiz Capitulino222f1802006-03-20 22:16:13 -0800781 for (; i < maxlen; i++) {
782 char c;
783 if (get_user(c, &user_buffer[i]))
784 return -EFAULT;
785 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 case '\"':
787 case '\n':
788 case '\r':
789 case '\t':
790 case ' ':
791 goto done_str;
792 break;
793 default:
794 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return i;
799}
800
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800801static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
802{
803 unsigned n = 0;
804 char c;
805 ssize_t i = 0;
806 int len;
807
808 pkt_dev->nr_labels = 0;
809 do {
810 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700811 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800812 if (len <= 0)
813 return len;
814 pkt_dev->labels[n] = htonl(tmp);
815 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
816 pkt_dev->flags |= F_MPLS_RND;
817 i += len;
818 if (get_user(c, &buffer[i]))
819 return -EFAULT;
820 i++;
821 n++;
822 if (n >= MAX_MPLS_LABELS)
823 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700824 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800825
826 pkt_dev->nr_labels = n;
827 return i;
828}
829
Luiz Capitulino222f1802006-03-20 22:16:13 -0800830static ssize_t pktgen_if_write(struct file *file,
831 const char __user * user_buffer, size_t count,
832 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800834 struct seq_file *seq = (struct seq_file *)file->private_data;
835 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int i = 0, max, len;
837 char name[16], valstr[32];
838 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800839 char *pg_result = NULL;
840 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800842
843 pg_result = &(pkt_dev->result[0]);
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700846 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return -EINVAL;
848 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 max = count - i;
851 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800852 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700853 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800854 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800856 i += tmp;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* Read variable name */
859
860 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000861 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800862 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800865 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return -EFAULT;
867 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800868
869 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800871 if (len < 0)
872 return len;
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 i += len;
875
876 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800877 char tb[count + 1];
878 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800880 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700881 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800882 (unsigned long)count, tb);
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (!strcmp(name, "min_pkt_size")) {
886 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000887 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800888 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800891 if (value < 14 + 20 + 8)
892 value = 14 + 20 + 8;
893 if (value != pkt_dev->min_pkt_size) {
894 pkt_dev->min_pkt_size = value;
895 pkt_dev->cur_pkt_size = value;
896 }
897 sprintf(pg_result, "OK: min_pkt_size=%u",
898 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return count;
900 }
901
Luiz Capitulino222f1802006-03-20 22:16:13 -0800902 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000904 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800905 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800908 if (value < 14 + 20 + 8)
909 value = 14 + 20 + 8;
910 if (value != pkt_dev->max_pkt_size) {
911 pkt_dev->max_pkt_size = value;
912 pkt_dev->cur_pkt_size = value;
913 }
914 sprintf(pg_result, "OK: max_pkt_size=%u",
915 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return count;
917 }
918
Luiz Capitulino222f1802006-03-20 22:16:13 -0800919 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!strcmp(name, "pkt_size")) {
922 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000923 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800927 if (value < 14 + 20 + 8)
928 value = 14 + 20 + 8;
929 if (value != pkt_dev->min_pkt_size) {
930 pkt_dev->min_pkt_size = value;
931 pkt_dev->max_pkt_size = value;
932 pkt_dev->cur_pkt_size = value;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
935 return count;
936 }
937
Luiz Capitulino222f1802006-03-20 22:16:13 -0800938 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000940 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800941 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800944 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sprintf(pg_result, "OK: debug=%u", debug);
946 return count;
947 }
948
Luiz Capitulino222f1802006-03-20 22:16:13 -0800949 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000951 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800952 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 i += len;
955 pkt_dev->nfrags = value;
956 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
957 return count;
958 }
959 if (!strcmp(name, "delay")) {
960 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000961 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800962 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000965 if (value == 0x7FFFFFFF)
966 pkt_dev->delay = ULLONG_MAX;
967 else
968 pkt_dev->delay = (u64)value * NSEC_PER_USEC;
969
970 sprintf(pg_result, "OK: delay=%llu",
971 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return count;
973 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800974 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000976 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800977 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800980 if (value != pkt_dev->udp_src_min) {
981 pkt_dev->udp_src_min = value;
982 pkt_dev->cur_udp_src = value;
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
985 return count;
986 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800987 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000989 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800990 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800993 if (value != pkt_dev->udp_dst_min) {
994 pkt_dev->udp_dst_min = value;
995 pkt_dev->cur_udp_dst = value;
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
998 return count;
999 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001000 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001002 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001003 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001006 if (value != pkt_dev->udp_src_max) {
1007 pkt_dev->udp_src_max = value;
1008 pkt_dev->cur_udp_src = value;
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1011 return count;
1012 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001013 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001015 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001016 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001019 if (value != pkt_dev->udp_dst_max) {
1020 pkt_dev->udp_dst_max = value;
1021 pkt_dev->cur_udp_dst = value;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1024 return count;
1025 }
1026 if (!strcmp(name, "clone_skb")) {
1027 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001028 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001032 pkt_dev->clone_skb = value;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1035 return count;
1036 }
1037 if (!strcmp(name, "count")) {
1038 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001039 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001040 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 i += len;
1043 pkt_dev->count = value;
1044 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return count;
1047 }
1048 if (!strcmp(name, "src_mac_count")) {
1049 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001050 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001051 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 i += len;
1054 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001055 pkt_dev->src_mac_count = value;
1056 pkt_dev->cur_src_mac_offset = 0;
1057 }
1058 sprintf(pg_result, "OK: src_mac_count=%d",
1059 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return count;
1061 }
1062 if (!strcmp(name, "dst_mac_count")) {
1063 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001064 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001065 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 i += len;
1068 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001069 pkt_dev->dst_mac_count = value;
1070 pkt_dev->cur_dst_mac_offset = 0;
1071 }
1072 sprintf(pg_result, "OK: dst_mac_count=%d",
1073 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return count;
1075 }
1076 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001077 char f[32];
1078 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001080 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001081 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (copy_from_user(f, &user_buffer[i], len))
1084 return -EFAULT;
1085 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001086 if (strcmp(f, "IPSRC_RND") == 0)
1087 pkt_dev->flags |= F_IPSRC_RND;
1088
1089 else if (strcmp(f, "!IPSRC_RND") == 0)
1090 pkt_dev->flags &= ~F_IPSRC_RND;
1091
1092 else if (strcmp(f, "TXSIZE_RND") == 0)
1093 pkt_dev->flags |= F_TXSIZE_RND;
1094
1095 else if (strcmp(f, "!TXSIZE_RND") == 0)
1096 pkt_dev->flags &= ~F_TXSIZE_RND;
1097
1098 else if (strcmp(f, "IPDST_RND") == 0)
1099 pkt_dev->flags |= F_IPDST_RND;
1100
1101 else if (strcmp(f, "!IPDST_RND") == 0)
1102 pkt_dev->flags &= ~F_IPDST_RND;
1103
1104 else if (strcmp(f, "UDPSRC_RND") == 0)
1105 pkt_dev->flags |= F_UDPSRC_RND;
1106
1107 else if (strcmp(f, "!UDPSRC_RND") == 0)
1108 pkt_dev->flags &= ~F_UDPSRC_RND;
1109
1110 else if (strcmp(f, "UDPDST_RND") == 0)
1111 pkt_dev->flags |= F_UDPDST_RND;
1112
1113 else if (strcmp(f, "!UDPDST_RND") == 0)
1114 pkt_dev->flags &= ~F_UDPDST_RND;
1115
1116 else if (strcmp(f, "MACSRC_RND") == 0)
1117 pkt_dev->flags |= F_MACSRC_RND;
1118
1119 else if (strcmp(f, "!MACSRC_RND") == 0)
1120 pkt_dev->flags &= ~F_MACSRC_RND;
1121
1122 else if (strcmp(f, "MACDST_RND") == 0)
1123 pkt_dev->flags |= F_MACDST_RND;
1124
1125 else if (strcmp(f, "!MACDST_RND") == 0)
1126 pkt_dev->flags &= ~F_MACDST_RND;
1127
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001128 else if (strcmp(f, "MPLS_RND") == 0)
1129 pkt_dev->flags |= F_MPLS_RND;
1130
1131 else if (strcmp(f, "!MPLS_RND") == 0)
1132 pkt_dev->flags &= ~F_MPLS_RND;
1133
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001134 else if (strcmp(f, "VID_RND") == 0)
1135 pkt_dev->flags |= F_VID_RND;
1136
1137 else if (strcmp(f, "!VID_RND") == 0)
1138 pkt_dev->flags &= ~F_VID_RND;
1139
1140 else if (strcmp(f, "SVID_RND") == 0)
1141 pkt_dev->flags |= F_SVID_RND;
1142
1143 else if (strcmp(f, "!SVID_RND") == 0)
1144 pkt_dev->flags &= ~F_SVID_RND;
1145
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001146 else if (strcmp(f, "FLOW_SEQ") == 0)
1147 pkt_dev->flags |= F_FLOW_SEQ;
1148
Robert Olsson45b270f2007-08-28 15:45:55 -07001149 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1150 pkt_dev->flags |= F_QUEUE_MAP_RND;
1151
1152 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1153 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001154
1155 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1156 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1157
1158 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1159 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001160#ifdef CONFIG_XFRM
1161 else if (strcmp(f, "IPSEC") == 0)
1162 pkt_dev->flags |= F_IPSEC_ON;
1163#endif
1164
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001165 else if (strcmp(f, "!IPV6") == 0)
1166 pkt_dev->flags &= ~F_IPV6;
1167
Luiz Capitulino222f1802006-03-20 22:16:13 -08001168 else {
1169 sprintf(pg_result,
1170 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1171 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001172 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001173 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001174 return count;
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1177 return count;
1178 }
1179 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1180 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001181 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001182 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Luiz Capitulino222f1802006-03-20 22:16:13 -08001184 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001186 buf[len] = 0;
1187 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1188 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1189 strncpy(pkt_dev->dst_min, buf, len);
1190 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1191 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1192 }
1193 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001194 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001195 pkt_dev->dst_min);
1196 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1198 return count;
1199 }
1200 if (!strcmp(name, "dst_max")) {
1201 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001202 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001203 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Luiz Capitulino222f1802006-03-20 22:16:13 -08001206 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return -EFAULT;
1208
Luiz Capitulino222f1802006-03-20 22:16:13 -08001209 buf[len] = 0;
1210 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1211 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1212 strncpy(pkt_dev->dst_max, buf, len);
1213 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1214 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1215 }
1216 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001217 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001218 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 i += len;
1220 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1221 return count;
1222 }
1223 if (!strcmp(name, "dst6")) {
1224 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001225 if (len < 0)
1226 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 pkt_dev->flags |= F_IPV6;
1229
Luiz Capitulino222f1802006-03-20 22:16:13 -08001230 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001232 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001235 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1238
Luiz Capitulino222f1802006-03-20 22:16:13 -08001239 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001240 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Luiz Capitulino222f1802006-03-20 22:16:13 -08001242 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 sprintf(pg_result, "OK: dst6=%s", buf);
1244 return count;
1245 }
1246 if (!strcmp(name, "dst6_min")) {
1247 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001248 if (len < 0)
1249 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 pkt_dev->flags |= F_IPV6;
1252
Luiz Capitulino222f1802006-03-20 22:16:13 -08001253 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001255 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001258 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1261 &pkt_dev->min_in6_daddr);
1262 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001263 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Luiz Capitulino222f1802006-03-20 22:16:13 -08001265 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 sprintf(pg_result, "OK: dst6_min=%s", buf);
1267 return count;
1268 }
1269 if (!strcmp(name, "dst6_max")) {
1270 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001271 if (len < 0)
1272 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 pkt_dev->flags |= F_IPV6;
1275
Luiz Capitulino222f1802006-03-20 22:16:13 -08001276 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001278 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001281 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Luiz Capitulino222f1802006-03-20 22:16:13 -08001283 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001284 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 sprintf(pg_result, "OK: dst6_max=%s", buf);
1288 return count;
1289 }
1290 if (!strcmp(name, "src6")) {
1291 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001292 if (len < 0)
1293 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 pkt_dev->flags |= F_IPV6;
1296
Luiz Capitulino222f1802006-03-20 22:16:13 -08001297 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001299 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001302 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1305
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001307 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308
1309 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 sprintf(pg_result, "OK: src6=%s", buf);
1311 return count;
1312 }
1313 if (!strcmp(name, "src_min")) {
1314 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001315 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001316 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001317
Luiz Capitulino222f1802006-03-20 22:16:13 -08001318 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 buf[len] = 0;
1321 if (strcmp(buf, pkt_dev->src_min) != 0) {
1322 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1323 strncpy(pkt_dev->src_min, buf, len);
1324 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1325 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1326 }
1327 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001328 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 i += len;
1331 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1332 return count;
1333 }
1334 if (!strcmp(name, "src_max")) {
1335 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001336 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001338
Luiz Capitulino222f1802006-03-20 22:16:13 -08001339 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001341 buf[len] = 0;
1342 if (strcmp(buf, pkt_dev->src_max) != 0) {
1343 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1344 strncpy(pkt_dev->src_max, buf, len);
1345 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1346 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1347 }
1348 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001349 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001350 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 i += len;
1352 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1353 return count;
1354 }
1355 if (!strcmp(name, "dst_mac")) {
1356 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001357 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001359 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001362 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001363 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001366 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -EFAULT;
1368 i += len;
1369
Luiz Capitulino222f1802006-03-20 22:16:13 -08001370 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (*v >= '0' && *v <= '9') {
1372 *m *= 16;
1373 *m += *v - '0';
1374 }
1375 if (*v >= 'A' && *v <= 'F') {
1376 *m *= 16;
1377 *m += *v - 'A' + 10;
1378 }
1379 if (*v >= 'a' && *v <= 'f') {
1380 *m *= 16;
1381 *m += *v - 'a' + 10;
1382 }
1383 if (*v == ':') {
1384 m++;
1385 *m = 0;
1386 }
1387 }
1388
1389 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001390 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1391 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 sprintf(pg_result, "OK: dstmac");
1394 return count;
1395 }
1396 if (!strcmp(name, "src_mac")) {
1397 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001398 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 unsigned char *m = pkt_dev->src_mac;
1400
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001401 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001404 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001405 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001408 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EFAULT;
1410 i += len;
1411
Luiz Capitulino222f1802006-03-20 22:16:13 -08001412 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (*v >= '0' && *v <= '9') {
1414 *m *= 16;
1415 *m += *v - '0';
1416 }
1417 if (*v >= 'A' && *v <= 'F') {
1418 *m *= 16;
1419 *m += *v - 'A' + 10;
1420 }
1421 if (*v >= 'a' && *v <= 'f') {
1422 *m *= 16;
1423 *m += *v - 'a' + 10;
1424 }
1425 if (*v == ':') {
1426 m++;
1427 *m = 0;
1428 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001431 /* Set up Src MAC */
1432 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1433 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1434
Luiz Capitulino222f1802006-03-20 22:16:13 -08001435 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return count;
1437 }
1438
Luiz Capitulino222f1802006-03-20 22:16:13 -08001439 if (!strcmp(name, "clear_counters")) {
1440 pktgen_clear_counters(pkt_dev);
1441 sprintf(pg_result, "OK: Clearing counters.\n");
1442 return count;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if (!strcmp(name, "flows")) {
1446 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001447 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 i += len;
1451 if (value > MAX_CFLOWS)
1452 value = MAX_CFLOWS;
1453
1454 pkt_dev->cflows = value;
1455 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1456 return count;
1457 }
1458
1459 if (!strcmp(name, "flowlen")) {
1460 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001461 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001462 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 i += len;
1465 pkt_dev->lflow = value;
1466 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1467 return count;
1468 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001469
Robert Olsson45b270f2007-08-28 15:45:55 -07001470 if (!strcmp(name, "queue_map_min")) {
1471 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001472 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001473 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001474
Robert Olsson45b270f2007-08-28 15:45:55 -07001475 i += len;
1476 pkt_dev->queue_map_min = value;
1477 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1478 return count;
1479 }
1480
1481 if (!strcmp(name, "queue_map_max")) {
1482 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001483 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001484 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001485
Robert Olsson45b270f2007-08-28 15:45:55 -07001486 i += len;
1487 pkt_dev->queue_map_max = value;
1488 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1489 return count;
1490 }
1491
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001492 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001493 unsigned n, cnt;
1494
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001495 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001496 if (len < 0)
1497 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001498 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001499 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001500 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001501 cnt += sprintf(pg_result + cnt,
1502 "%08x%s", ntohl(pkt_dev->labels[n]),
1503 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001504
1505 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1506 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1507 pkt_dev->svlan_id = 0xffff;
1508
1509 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001510 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001511 }
1512 return count;
1513 }
1514
1515 if (!strcmp(name, "vlan_id")) {
1516 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001517 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001518 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001519
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001520 i += len;
1521 if (value <= 4095) {
1522 pkt_dev->vlan_id = value; /* turn on VLAN */
1523
1524 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001525 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001526
1527 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001528 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001529
1530 pkt_dev->nr_labels = 0; /* turn off MPLS */
1531 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1532 } else {
1533 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1534 pkt_dev->svlan_id = 0xffff;
1535
1536 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001537 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001538 }
1539 return count;
1540 }
1541
1542 if (!strcmp(name, "vlan_p")) {
1543 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001544 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001545 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001546
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001547 i += len;
1548 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1549 pkt_dev->vlan_p = value;
1550 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1551 } else {
1552 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1553 }
1554 return count;
1555 }
1556
1557 if (!strcmp(name, "vlan_cfi")) {
1558 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001559 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001560 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001561
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001562 i += len;
1563 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1564 pkt_dev->vlan_cfi = value;
1565 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1566 } else {
1567 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1568 }
1569 return count;
1570 }
1571
1572 if (!strcmp(name, "svlan_id")) {
1573 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001574 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001575 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001576
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001577 i += len;
1578 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1579 pkt_dev->svlan_id = value; /* turn on SVLAN */
1580
1581 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001582 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001583
1584 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001585 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001586
1587 pkt_dev->nr_labels = 0; /* turn off MPLS */
1588 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1589 } else {
1590 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1591 pkt_dev->svlan_id = 0xffff;
1592
1593 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001594 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001595 }
1596 return count;
1597 }
1598
1599 if (!strcmp(name, "svlan_p")) {
1600 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001601 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001602 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001603
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001604 i += len;
1605 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1606 pkt_dev->svlan_p = value;
1607 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1608 } else {
1609 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1610 }
1611 return count;
1612 }
1613
1614 if (!strcmp(name, "svlan_cfi")) {
1615 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001616 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001617 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001618
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001619 i += len;
1620 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1621 pkt_dev->svlan_cfi = value;
1622 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1623 } else {
1624 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1625 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001626 return count;
1627 }
1628
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001629 if (!strcmp(name, "tos")) {
1630 __u32 tmp_value = 0;
1631 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001632 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001633 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001634
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001635 i += len;
1636 if (len == 2) {
1637 pkt_dev->tos = tmp_value;
1638 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1639 } else {
1640 sprintf(pg_result, "ERROR: tos must be 00-ff");
1641 }
1642 return count;
1643 }
1644
1645 if (!strcmp(name, "traffic_class")) {
1646 __u32 tmp_value = 0;
1647 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001648 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001649 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001650
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001651 i += len;
1652 if (len == 2) {
1653 pkt_dev->traffic_class = tmp_value;
1654 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1655 } else {
1656 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1657 }
1658 return count;
1659 }
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1662 return -EINVAL;
1663}
1664
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001665static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001667 return single_open(file, pktgen_if_show, PDE(inode)->data);
1668}
1669
Arjan van de Ven9a321442007-02-12 00:55:35 -08001670static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001671 .owner = THIS_MODULE,
1672 .open = pktgen_if_open,
1673 .read = seq_read,
1674 .llseek = seq_lseek,
1675 .write = pktgen_if_write,
1676 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001677};
1678
1679static int pktgen_thread_show(struct seq_file *seq, void *v)
1680{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001681 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001682 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001684 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Luiz Capitulino222f1802006-03-20 22:16:13 -08001686 seq_printf(seq, "Running: ");
1687
1688 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08001689 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001690 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001691 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Luiz Capitulino222f1802006-03-20 22:16:13 -08001693 seq_printf(seq, "\nStopped: ");
1694
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08001695 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001696 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001697 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001700 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001702 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Luiz Capitulino222f1802006-03-20 22:16:13 -08001704 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001709static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001710 const char __user * user_buffer,
1711 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001713 struct seq_file *seq = (struct seq_file *)file->private_data;
1714 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int i = 0, max, len, ret;
1716 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001717 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001720 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return -EINVAL;
1722 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001725 len = count_trail_chars(&user_buffer[i], max);
1726 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001727 return len;
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /* Read variable name */
1732
1733 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001734 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001735 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 memset(name, 0, sizeof(name));
1738 if (copy_from_user(name, &user_buffer[i], len))
1739 return -EFAULT;
1740 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001741
Luiz Capitulino222f1802006-03-20 22:16:13 -08001742 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001744 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001745 return len;
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 i += len;
1748
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001749 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001750 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1751 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Luiz Capitulino222f1802006-03-20 22:16:13 -08001753 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001754 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 ret = -EINVAL;
1756 goto out;
1757 }
1758
1759 pg_result = &(t->result[0]);
1760
Luiz Capitulino222f1802006-03-20 22:16:13 -08001761 if (!strcmp(name, "add_device")) {
1762 char f[32];
1763 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001765 if (len < 0) {
1766 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto out;
1768 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001769 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return -EFAULT;
1771 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001772 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001774 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001775 ret = count;
1776 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 goto out;
1778 }
1779
Luiz Capitulino222f1802006-03-20 22:16:13 -08001780 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001781 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001782 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001783 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001784 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001786 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto out;
1788 }
1789
Luiz Capitulino222f1802006-03-20 22:16:13 -08001790 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001791 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001792 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 goto out;
1794 }
1795
1796 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001797out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return ret;
1799}
1800
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001801static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001803 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Arjan van de Ven9a321442007-02-12 00:55:35 -08001806static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001807 .owner = THIS_MODULE,
1808 .open = pktgen_thread_open,
1809 .read = seq_read,
1810 .llseek = seq_lseek,
1811 .write = pktgen_thread_write,
1812 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001813};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001816static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct pktgen_thread *t;
1819 struct pktgen_dev *pkt_dev = NULL;
1820
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001821 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 pkt_dev = pktgen_find_dev(t, ifname);
1823 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001824 if (remove) {
1825 if_lock(t);
1826 pkt_dev->removal_mark = 1;
1827 t->control |= T_REMDEV;
1828 if_unlock(t);
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 break;
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001833 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001836/*
1837 * mark a device for removal
1838 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001839static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001842 const int max_tries = 10, msec_per_try = 125;
1843 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001844
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001845 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001846 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001847
Luiz Capitulino222f1802006-03-20 22:16:13 -08001848 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001849
1850 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001851 if (pkt_dev == NULL)
1852 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001853
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001854 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001855 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1856 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001857 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001858 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001859
1860 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001861 printk(KERN_ERR "pktgen_mark_device: timed out after "
1862 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001863 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001864 break;
1865 }
1866
1867 }
1868
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001869 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001870}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001871
Stephen Hemminger39df2322007-03-04 16:11:51 -08001872static void pktgen_change_name(struct net_device *dev)
1873{
1874 struct pktgen_thread *t;
1875
1876 list_for_each_entry(t, &pktgen_threads, th_list) {
1877 struct pktgen_dev *pkt_dev;
1878
1879 list_for_each_entry(pkt_dev, &t->if_list, list) {
1880 if (pkt_dev->odev != dev)
1881 continue;
1882
1883 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1884
Alexey Dobriyan29753152009-08-28 23:34:43 -07001885 pkt_dev->entry = proc_create_data(dev->name, 0600,
1886 pg_proc_dir,
1887 &pktgen_if_fops,
1888 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001889 if (!pkt_dev->entry)
1890 printk(KERN_ERR "pktgen: can't move proc "
1891 " entry for '%s'\n", dev->name);
1892 break;
1893 }
1894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
Luiz Capitulino222f1802006-03-20 22:16:13 -08001897static int pktgen_device_event(struct notifier_block *unused,
1898 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001900 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001902 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001903 return NOTIFY_DONE;
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 /* It is OK that we do not hold the group lock right now,
1906 * as we run under the RTNL lock.
1907 */
1908
1909 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001910 case NETDEV_CHANGENAME:
1911 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001915 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 return NOTIFY_DONE;
1920}
1921
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001922static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1923 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001924{
1925 char b[IFNAMSIZ+5];
1926 int i = 0;
1927
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001928 for (i = 0; ifname[i] != '@'; i++) {
1929 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001930 break;
1931
1932 b[i] = ifname[i];
1933 }
1934 b[i] = 0;
1935
1936 return dev_get_by_name(&init_net, b);
1937}
1938
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940/* Associate pktgen_dev with a device. */
1941
Stephen Hemminger39df2322007-03-04 16:11:51 -08001942static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001945 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (pkt_dev->odev) {
1949 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001950 pkt_dev->odev = NULL;
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Robert Olssone6fce5b2008-08-07 02:23:01 -07001953 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001955 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001956 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001960 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001961 err = -EINVAL;
1962 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001963 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001964 err = -ENETDOWN;
1965 } else {
1966 pkt_dev->odev = odev;
1967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001971 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974/* Read pkt_dev from the interface and set up internal pktgen_dev
1975 * structure to have the right information to create/send packets
1976 */
1977static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1978{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001979 int ntxq;
1980
Luiz Capitulino222f1802006-03-20 22:16:13 -08001981 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001982 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1983 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001984 sprintf(pkt_dev->result,
1985 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1986 return;
1987 }
1988
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001989 /* make sure that we don't pick a non-existing transmit queue */
1990 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08001991
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001992 if (ntxq <= pkt_dev->queue_map_min) {
1993 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07001994 "queue_map_min (zero-based) (%d) exceeds valid range "
1995 "[0 - %d] for (%d) queues on %s, resetting\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001996 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
Jesse Brandeburg88271662008-10-28 13:21:51 -07001997 pkt_dev->odev->name);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001998 pkt_dev->queue_map_min = ntxq - 1;
1999 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002000 if (pkt_dev->queue_map_max >= ntxq) {
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002001 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07002002 "queue_map_max (zero-based) (%d) exceeds valid range "
2003 "[0 - %d] for (%d) queues on %s, resetting\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002004 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
Jesse Brandeburg88271662008-10-28 13:21:51 -07002005 pkt_dev->odev->name);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002006 pkt_dev->queue_map_max = ntxq - 1;
2007 }
2008
Luiz Capitulino222f1802006-03-20 22:16:13 -08002009 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002011 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002012 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Luiz Capitulino222f1802006-03-20 22:16:13 -08002014 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002015 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Luiz Capitulino222f1802006-03-20 22:16:13 -08002017 /* Set up pkt size */
2018 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2019
2020 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002022 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 * gets exported
2024 */
2025
2026#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002027 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 struct inet6_dev *idev;
2029
Luiz Capitulino222f1802006-03-20 22:16:13 -08002030 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2031 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 set = 1;
2033 break;
2034 }
2035
Luiz Capitulino222f1802006-03-20 22:16:13 -08002036 if (!set) {
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /*
2039 * Use linklevel address if unconfigured.
2040 *
2041 * use ipv6_get_lladdr if/when it's get exported
2042 */
2043
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002044 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002045 idev = __in6_dev_get(pkt_dev->odev);
2046 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct inet6_ifaddr *ifp;
2048
2049 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002050 for (ifp = idev->addr_list; ifp;
2051 ifp = ifp->if_next) {
2052 if (ifp->scope == IFA_LINK
2053 && !(ifp->
2054 flags & IFA_F_TENTATIVE)) {
2055 ipv6_addr_copy(&pkt_dev->
2056 cur_in6_saddr,
2057 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 err = 0;
2059 break;
2060 }
2061 }
2062 read_unlock_bh(&idev->lock);
2063 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002064 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002065 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002066 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2067 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002070 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 pkt_dev->saddr_min = 0;
2072 pkt_dev->saddr_max = 0;
2073 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002074
2075 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002078 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (in_dev) {
2080 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002081 pkt_dev->saddr_min =
2082 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 pkt_dev->saddr_max = pkt_dev->saddr_min;
2084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002087 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2089 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2090 }
2091
2092 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2093 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2094 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002095 /* Initialize current values. */
2096 pkt_dev->cur_dst_mac_offset = 0;
2097 pkt_dev->cur_src_mac_offset = 0;
2098 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2099 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2100 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2101 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 pkt_dev->nflows = 0;
2103}
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002106static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2107{
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002108 ktime_t start;
2109 s32 remaining;
2110 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002111
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002112 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2113 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002114
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002115 remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
2116 if (remaining <= 0)
2117 return;
2118
2119 start = ktime_now();
2120 if (remaining < 100)
2121 udelay(remaining); /* really small just spin */
2122 else {
2123 /* see do_nanosleep */
2124 hrtimer_init_sleeper(&t, current);
2125 do {
2126 set_current_state(TASK_INTERRUPTIBLE);
2127 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2128 if (!hrtimer_active(&t.timer))
2129 t.task = NULL;
2130
2131 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002134 hrtimer_cancel(&t.timer);
2135 } while (t.task && pkt_dev->running && !signal_pending(current));
2136 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002138 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002141static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2142{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002143 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002144 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2145 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2146 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2147}
2148
Stephen Hemminger648fda72009-08-27 13:55:07 +00002149static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002150{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002151 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002152}
2153
2154static inline int f_pick(struct pktgen_dev *pkt_dev)
2155{
2156 int flow = pkt_dev->curfl;
2157
2158 if (pkt_dev->flags & F_FLOW_SEQ) {
2159 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2160 /* reset time */
2161 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002162 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002163 pkt_dev->curfl += 1;
2164 if (pkt_dev->curfl >= pkt_dev->cflows)
2165 pkt_dev->curfl = 0; /*reset */
2166 }
2167 } else {
2168 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002169 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002170
Robert Olsson1211a642008-08-05 18:44:26 -07002171 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002172 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002173 pkt_dev->flows[flow].flags = 0;
2174 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002175 }
2176
2177 return pkt_dev->curfl;
2178}
2179
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002180
2181#ifdef CONFIG_XFRM
2182/* If there was already an IPSEC SA, we keep it as is, else
2183 * we go look for it ...
2184*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002185static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002186{
2187 struct xfrm_state *x = pkt_dev->flows[flow].x;
2188 if (!x) {
2189 /*slow path: we dont already have xfrm_state*/
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002190 x = xfrm_stateonly_find(&init_net,
2191 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002192 (xfrm_address_t *)&pkt_dev->cur_saddr,
2193 AF_INET,
2194 pkt_dev->ipsmode,
2195 pkt_dev->ipsproto, 0);
2196 if (x) {
2197 pkt_dev->flows[flow].x = x;
2198 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002199 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002200 }
2201
2202 }
2203}
2204#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002205static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2206{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002207
2208 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2209 pkt_dev->cur_queue_map = smp_processor_id();
2210
2211 else if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002212 __u16 t;
2213 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2214 t = random32() %
2215 (pkt_dev->queue_map_max -
2216 pkt_dev->queue_map_min + 1)
2217 + pkt_dev->queue_map_min;
2218 } else {
2219 t = pkt_dev->cur_queue_map + 1;
2220 if (t > pkt_dev->queue_map_max)
2221 t = pkt_dev->queue_map_min;
2222 }
2223 pkt_dev->cur_queue_map = t;
2224 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002225 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 -07002226}
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228/* Increment/randomize headers according to flags and current values
2229 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2230 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002231static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2232{
2233 __u32 imn;
2234 __u32 imx;
2235 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002237 if (pkt_dev->cflows)
2238 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002241 if (pkt_dev->src_mac_count > 1) {
2242 __u32 mc;
2243 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Luiz Capitulino222f1802006-03-20 22:16:13 -08002245 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002246 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002247 else {
2248 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002249 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002250 pkt_dev->src_mac_count)
2251 pkt_dev->cur_src_mac_offset = 0;
2252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Luiz Capitulino222f1802006-03-20 22:16:13 -08002254 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2255 pkt_dev->hh[11] = tmp;
2256 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2257 pkt_dev->hh[10] = tmp;
2258 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2259 pkt_dev->hh[9] = tmp;
2260 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2261 pkt_dev->hh[8] = tmp;
2262 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2263 pkt_dev->hh[7] = tmp;
2264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Luiz Capitulino222f1802006-03-20 22:16:13 -08002266 /* Deal with Destination MAC */
2267 if (pkt_dev->dst_mac_count > 1) {
2268 __u32 mc;
2269 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Luiz Capitulino222f1802006-03-20 22:16:13 -08002271 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002272 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Luiz Capitulino222f1802006-03-20 22:16:13 -08002274 else {
2275 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002276 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002277 pkt_dev->dst_mac_count) {
2278 pkt_dev->cur_dst_mac_offset = 0;
2279 }
2280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Luiz Capitulino222f1802006-03-20 22:16:13 -08002282 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2283 pkt_dev->hh[5] = tmp;
2284 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2285 pkt_dev->hh[4] = tmp;
2286 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2287 pkt_dev->hh[3] = tmp;
2288 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2289 pkt_dev->hh[2] = tmp;
2290 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2291 pkt_dev->hh[1] = tmp;
2292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002294 if (pkt_dev->flags & F_MPLS_RND) {
2295 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002296 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002297 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2298 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002299 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002300 htonl(0x000fffff));
2301 }
2302
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002303 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002304 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002305 }
2306
2307 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002308 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002309 }
2310
Luiz Capitulino222f1802006-03-20 22:16:13 -08002311 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2312 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002313 pkt_dev->cur_udp_src = random32() %
2314 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2315 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Luiz Capitulino222f1802006-03-20 22:16:13 -08002317 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 pkt_dev->cur_udp_src++;
2319 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2320 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002321 }
2322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Luiz Capitulino222f1802006-03-20 22:16:13 -08002324 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2325 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002326 pkt_dev->cur_udp_dst = random32() %
2327 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2328 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002329 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002331 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002333 }
2334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 if (!(pkt_dev->flags & F_IPV6)) {
2337
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002338 imn = ntohl(pkt_dev->saddr_min);
2339 imx = ntohl(pkt_dev->saddr_max);
2340 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002342 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002343 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 else {
2345 t = ntohl(pkt_dev->cur_saddr);
2346 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002347 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
2351 pkt_dev->cur_saddr = htonl(t);
2352 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002353
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002354 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2356 } else {
Al Viro252e33462006-11-14 20:48:11 -08002357 imn = ntohl(pkt_dev->daddr_min);
2358 imx = ntohl(pkt_dev->daddr_max);
2359 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002361 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (pkt_dev->flags & F_IPDST_RND) {
2363
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002364 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002365 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Joe Perches21cf2252007-12-16 13:44:00 -08002367 while (ipv4_is_loopback(s) ||
2368 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002369 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002370 ipv4_is_zeronet(s) ||
2371 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002372 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002373 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
Al Viro252e33462006-11-14 20:48:11 -08002375 pkt_dev->cur_daddr = s;
2376 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 t = ntohl(pkt_dev->cur_daddr);
2378 t++;
2379 if (t > imx) {
2380 t = imn;
2381 }
2382 pkt_dev->cur_daddr = htonl(t);
2383 }
2384 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002385 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002386 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 pkt_dev->flows[flow].cur_daddr =
2388 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002389#ifdef CONFIG_XFRM
2390 if (pkt_dev->flags & F_IPSEC_ON)
2391 get_ipsec_sa(pkt_dev, flow);
2392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 pkt_dev->nflows++;
2394 }
2395 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002396 } else { /* IPV6 * */
2397
2398 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2399 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2400 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2401 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 else {
2403 int i;
2404
2405 /* Only random destinations yet */
2406
Luiz Capitulino222f1802006-03-20 22:16:13 -08002407 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002409 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002410 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2411 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415
Luiz Capitulino222f1802006-03-20 22:16:13 -08002416 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2417 __u32 t;
2418 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002419 t = random32() %
2420 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2421 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002422 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002424 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002426 }
2427 pkt_dev->cur_pkt_size = t;
2428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002430 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 pkt_dev->flows[flow].count++;
2433}
2434
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002435
2436#ifdef CONFIG_XFRM
2437static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2438{
2439 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2440 int err = 0;
2441 struct iphdr *iph;
2442
2443 if (!x)
2444 return 0;
2445 /* XXX: we dont support tunnel mode for now until
2446 * we resolve the dst issue */
2447 if (x->props.mode != XFRM_MODE_TRANSPORT)
2448 return 0;
2449
2450 spin_lock(&x->lock);
2451 iph = ip_hdr(skb);
2452
Herbert Xu13996372007-10-17 21:35:51 -07002453 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002454 if (err)
2455 goto error;
2456 err = x->type->output(x, skb);
2457 if (err)
2458 goto error;
2459
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002460 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002461 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002462error:
2463 spin_unlock(&x->lock);
2464 return err;
2465}
2466
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002467static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002468{
2469 if (pkt_dev->cflows) {
2470 /* let go of the SAs if we have them */
2471 int i = 0;
Florian Westphal5b5f7922009-05-21 15:07:12 -07002472 for (; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002473 struct xfrm_state *x = pkt_dev->flows[i].x;
2474 if (x) {
2475 xfrm_state_put(x);
2476 pkt_dev->flows[i].x = NULL;
2477 }
2478 }
2479 }
2480}
2481
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002482static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002483 struct sk_buff *skb, __be16 protocol)
2484{
2485 if (pkt_dev->flags & F_IPSEC_ON) {
2486 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2487 int nhead = 0;
2488 if (x) {
2489 int ret;
2490 __u8 *eth;
2491 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002492 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002493 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2494 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002495 printk(KERN_ERR "Error expanding "
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002496 "ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002497 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002498 }
2499 }
2500
2501 /* ipsec is not expecting ll header */
2502 skb_pull(skb, ETH_HLEN);
2503 ret = pktgen_output_ipsec(skb, pkt_dev);
2504 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002505 printk(KERN_ERR "Error creating ipsec "
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002506 "packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002507 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002508 }
2509 /* restore ll */
2510 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2511 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002512 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002513 }
2514 }
2515 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002516err:
2517 kfree_skb(skb);
2518 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002519}
2520#endif
2521
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002522static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2523{
2524 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002525 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002526 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002527
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002528 mpls--;
2529 *mpls |= MPLS_STACK_BOTTOM;
2530}
2531
Al Viro0f37c602006-11-03 03:49:56 -08002532static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2533 unsigned int prio)
2534{
2535 return htons(id | (cfi << 12) | (prio << 13));
2536}
2537
Luiz Capitulino222f1802006-03-20 22:16:13 -08002538static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2539 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
2541 struct sk_buff *skb = NULL;
2542 __u8 *eth;
2543 struct udphdr *udph;
2544 int datalen, iplen;
2545 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002546 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002547 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002548 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002549 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2550 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2551 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2552 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002553 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002554
2555 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002556 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002557
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002558 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002559 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002560
Robert Olsson64053be2005-06-26 15:27:10 -07002561 /* Update any of the values, used when we're incrementing various
2562 * fields.
2563 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002564 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002565 mod_cur_headers(pkt_dev);
2566
David S. Miller7ac54592006-01-18 14:19:10 -08002567 datalen = (odev->hard_header_len + 16) & ~0xf;
Stephen Hemmingere4707572009-08-27 13:55:13 +00002568 skb = __netdev_alloc_skb(odev,
2569 pkt_dev->cur_pkt_size + 64
2570 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (!skb) {
2572 sprintf(pkt_dev->result, "No memory");
2573 return NULL;
2574 }
2575
David S. Miller7ac54592006-01-18 14:19:10 -08002576 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 /* Reserve for ethernet and IP header */
2579 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002580 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2581 if (pkt_dev->nr_labels)
2582 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002583
2584 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002585 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002586 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002587 *svlan_tci = build_tci(pkt_dev->svlan_id,
2588 pkt_dev->svlan_cfi,
2589 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002590 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002591 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002592 }
2593 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002594 *vlan_tci = build_tci(pkt_dev->vlan_id,
2595 pkt_dev->vlan_cfi,
2596 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002597 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002598 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002599 }
2600
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002601 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002602 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002603 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002604 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002605 iph = ip_hdr(skb);
2606 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002609 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002611 /* Eth + IPh + UDPh + mpls */
2612 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002613 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002614 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 udph->source = htons(pkt_dev->cur_udp_src);
2618 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002619 udph->len = htons(datalen + 8); /* DATA + udphdr */
2620 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 iph->ihl = 5;
2623 iph->version = 4;
2624 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002625 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002626 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 iph->saddr = pkt_dev->cur_saddr;
2628 iph->daddr = pkt_dev->cur_daddr;
2629 iph->frag_off = 0;
2630 iplen = 20 + 8 + datalen;
2631 iph->tot_len = htons(iplen);
2632 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002633 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002634 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002635 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002636 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 skb->dev = odev;
2638 skb->pkt_type = PACKET_HOST;
2639
Luiz Capitulino222f1802006-03-20 22:16:13 -08002640 if (pkt_dev->nfrags <= 0)
2641 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 else {
2643 int frags = pkt_dev->nfrags;
2644 int i;
2645
Luiz Capitulino222f1802006-03-20 22:16:13 -08002646 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (frags > MAX_SKB_FRAGS)
2649 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002650 if (datalen > frags * PAGE_SIZE) {
2651 skb_put(skb, datalen - frags * PAGE_SIZE);
2652 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654
2655 i = 0;
2656 while (datalen > 0) {
2657 struct page *page = alloc_pages(GFP_KERNEL, 0);
2658 skb_shinfo(skb)->frags[i].page = page;
2659 skb_shinfo(skb)->frags[i].page_offset = 0;
2660 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002661 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 datalen -= skb_shinfo(skb)->frags[i].size;
2663 skb->len += skb_shinfo(skb)->frags[i].size;
2664 skb->data_len += skb_shinfo(skb)->frags[i].size;
2665 i++;
2666 skb_shinfo(skb)->nr_frags = i;
2667 }
2668
2669 while (i < frags) {
2670 int rem;
2671
2672 if (i == 0)
2673 break;
2674
2675 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2676 if (rem == 0)
2677 break;
2678
2679 skb_shinfo(skb)->frags[i - 1].size -= rem;
2680
Luiz Capitulino222f1802006-03-20 22:16:13 -08002681 skb_shinfo(skb)->frags[i] =
2682 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002684 skb_shinfo(skb)->frags[i].page =
2685 skb_shinfo(skb)->frags[i - 1].page;
2686 skb_shinfo(skb)->frags[i].page_offset +=
2687 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 skb_shinfo(skb)->frags[i].size = rem;
2689 i++;
2690 skb_shinfo(skb)->nr_frags = i;
2691 }
2692 }
2693
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002694 /* Stamp the time, and sequence number,
2695 * convert them to network byte order
2696 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002697 if (pgh) {
2698 struct timeval timestamp;
2699
2700 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2701 pgh->seq_num = htonl(pkt_dev->seq_num);
2702
2703 do_gettimeofday(&timestamp);
2704 pgh->tv_sec = htonl(timestamp.tv_sec);
2705 pgh->tv_usec = htonl(timestamp.tv_usec);
2706 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002707
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002708#ifdef CONFIG_XFRM
2709 if (!process_ipsec(pkt_dev, skb, protocol))
2710 return NULL;
2711#endif
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return skb;
2714}
2715
2716/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002717 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2719 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002720 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 * Should be candidate for net/ipv4/utils.c
2722 * --ro
2723 */
2724
Luiz Capitulino222f1802006-03-20 22:16:13 -08002725static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002728 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 unsigned long u;
2730 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002731 unsigned int prefixlen = 0;
2732 unsigned int suffixlen = 0;
Al Viro252e33462006-11-14 20:48:11 -08002733 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002734 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Luiz Capitulino222f1802006-03-20 22:16:13 -08002736 for (i = 0; i < 16; i++)
2737 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 for (;;) {
2740 if (*s == ':') {
2741 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002742 if (s[1] == ':') { /* Found "::", skip to part 2 */
2743 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 len++;
2745 break;
2746 }
2747 s++;
2748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002750 u = simple_strtoul(s, &pos, 16);
2751 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002752 if (!i)
2753 return 0;
2754 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 /* the last 4 bytes may be written as IPv4 address */
2757
2758 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002759 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2760 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
2762 ip[prefixlen++] = (u >> 8);
2763 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002764 s += i;
2765 len += i;
2766 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return len;
2768 }
2769
2770/* part 2, after "::" */
2771 for (;;) {
2772 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002773 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 break;
2775 s++;
2776 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002777 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002779
2780 u = simple_strtol(s, &pos, 16);
2781 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002783 if (*s)
2784 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 break;
2786 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002787 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002789 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2790 sizeof(tmp));
2791 suffixlen += 4;
2792 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 break;
2794 }
2795 suffix[suffixlen++] = (u >> 8);
2796 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002797 s += i;
2798 len += i;
2799 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 break;
2801 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002802 for (i = 0; i < suffixlen; i++)
2803 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return len;
2805}
2806
Luiz Capitulino222f1802006-03-20 22:16:13 -08002807static char tohex(char hexdigit)
2808{
2809 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810}
2811
Luiz Capitulino222f1802006-03-20 22:16:13 -08002812static int fmt_xlong(char *s, unsigned int i)
2813{
2814 char *bak = s;
2815 *s = tohex((i >> 12) & 0xf);
2816 if (s != bak || *s != '0')
2817 ++s;
2818 *s = tohex((i >> 8) & 0xf);
2819 if (s != bak || *s != '0')
2820 ++s;
2821 *s = tohex((i >> 4) & 0xf);
2822 if (s != bak || *s != '0')
2823 ++s;
2824 *s = tohex(i & 0xf);
2825 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
Luiz Capitulino222f1802006-03-20 22:16:13 -08002828static unsigned int fmt_ip6(char *s, const char ip[16])
2829{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 unsigned int len;
2831 unsigned int i;
2832 unsigned int temp;
2833 unsigned int compressing;
2834 int j;
2835
Luiz Capitulino222f1802006-03-20 22:16:13 -08002836 len = 0;
2837 compressing = 0;
2838 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002841 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2842 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2843 temp = strlen(s);
2844 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002847 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2848 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 if (temp == 0) {
2850 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002851 compressing = 1;
2852 if (j == 0) {
2853 *s++ = ':';
2854 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856 }
2857 } else {
2858 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002859 compressing = 0;
2860 *s++ = ':';
2861 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002863 i = fmt_xlong(s, temp);
2864 len += i;
2865 s += i;
2866 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 *s++ = ':';
2868 ++len;
2869 }
2870 }
2871 }
2872 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873 *s++ = ':';
2874 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002876 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return len;
2878}
2879
Luiz Capitulino222f1802006-03-20 22:16:13 -08002880static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2881 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 struct sk_buff *skb = NULL;
2884 __u8 *eth;
2885 struct udphdr *udph;
2886 int datalen;
2887 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002888 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002889 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002890 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002891 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2892 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2893 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2894 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002895 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002896
2897 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002898 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002899
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002900 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002901 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002902
Robert Olsson64053be2005-06-26 15:27:10 -07002903 /* Update any of the values, used when we're incrementing various
2904 * fields.
2905 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002906 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002907 mod_cur_headers(pkt_dev);
2908
Stephen Hemmingere4707572009-08-27 13:55:13 +00002909 skb = __netdev_alloc_skb(odev,
2910 pkt_dev->cur_pkt_size + 64
2911 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (!skb) {
2913 sprintf(pkt_dev->result, "No memory");
2914 return NULL;
2915 }
2916
2917 skb_reserve(skb, 16);
2918
2919 /* Reserve for ethernet and IP header */
2920 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002921 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2922 if (pkt_dev->nr_labels)
2923 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002924
2925 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002926 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002927 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002928 *svlan_tci = build_tci(pkt_dev->svlan_id,
2929 pkt_dev->svlan_cfi,
2930 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002931 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002932 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002933 }
2934 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002935 *vlan_tci = build_tci(pkt_dev->vlan_id,
2936 pkt_dev->vlan_cfi,
2937 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002938 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002939 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002940 }
2941
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002942 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002943 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002944 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002945 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002946 iph = ipv6_hdr(skb);
2947 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002950 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002951
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002952 /* Eth + IPh + UDPh + mpls */
2953 datalen = pkt_dev->cur_pkt_size - 14 -
2954 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002955 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Luiz Capitulino222f1802006-03-20 22:16:13 -08002957 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 datalen = sizeof(struct pktgen_hdr);
2959 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002960 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2961 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963
2964 udph->source = htons(pkt_dev->cur_udp_src);
2965 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002966 udph->len = htons(datalen + sizeof(struct udphdr));
2967 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002969 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002971 if (pkt_dev->traffic_class) {
2972 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002973 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002974 }
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 iph->hop_limit = 32;
2977
2978 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2979 iph->nexthdr = IPPROTO_UDP;
2980
2981 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2982 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2983
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002984 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002985 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002986 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 skb->dev = odev;
2988 skb->pkt_type = PACKET_HOST;
2989
Luiz Capitulino222f1802006-03-20 22:16:13 -08002990 if (pkt_dev->nfrags <= 0)
2991 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 else {
2993 int frags = pkt_dev->nfrags;
2994 int i;
2995
Luiz Capitulino222f1802006-03-20 22:16:13 -08002996 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (frags > MAX_SKB_FRAGS)
2999 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003000 if (datalen > frags * PAGE_SIZE) {
3001 skb_put(skb, datalen - frags * PAGE_SIZE);
3002 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
3004
3005 i = 0;
3006 while (datalen > 0) {
3007 struct page *page = alloc_pages(GFP_KERNEL, 0);
3008 skb_shinfo(skb)->frags[i].page = page;
3009 skb_shinfo(skb)->frags[i].page_offset = 0;
3010 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08003011 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 datalen -= skb_shinfo(skb)->frags[i].size;
3013 skb->len += skb_shinfo(skb)->frags[i].size;
3014 skb->data_len += skb_shinfo(skb)->frags[i].size;
3015 i++;
3016 skb_shinfo(skb)->nr_frags = i;
3017 }
3018
3019 while (i < frags) {
3020 int rem;
3021
3022 if (i == 0)
3023 break;
3024
3025 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3026 if (rem == 0)
3027 break;
3028
3029 skb_shinfo(skb)->frags[i - 1].size -= rem;
3030
Luiz Capitulino222f1802006-03-20 22:16:13 -08003031 skb_shinfo(skb)->frags[i] =
3032 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003034 skb_shinfo(skb)->frags[i].page =
3035 skb_shinfo(skb)->frags[i - 1].page;
3036 skb_shinfo(skb)->frags[i].page_offset +=
3037 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 skb_shinfo(skb)->frags[i].size = rem;
3039 i++;
3040 skb_shinfo(skb)->nr_frags = i;
3041 }
3042 }
3043
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003044 /* Stamp the time, and sequence number,
3045 * convert them to network byte order
3046 * should we update cloned packets too ?
3047 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048 if (pgh) {
3049 struct timeval timestamp;
3050
3051 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3052 pgh->seq_num = htonl(pkt_dev->seq_num);
3053
3054 do_gettimeofday(&timestamp);
3055 pgh->tv_sec = htonl(timestamp.tv_sec);
3056 pgh->tv_usec = htonl(timestamp.tv_usec);
3057 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003058 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return skb;
3061}
3062
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003063static struct sk_buff *fill_packet(struct net_device *odev,
3064 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003066 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return fill_packet_ipv6(odev, pkt_dev);
3068 else
3069 return fill_packet_ipv4(odev, pkt_dev);
3070}
3071
Luiz Capitulino222f1802006-03-20 22:16:13 -08003072static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074 pkt_dev->seq_num = 1;
3075 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003077 pkt_dev->tx_bytes = 0;
3078 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
3081/* Set up structure for sending pkts, clear counters */
3082
3083static void pktgen_run(struct pktgen_thread *t)
3084{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003085 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 int started = 0;
3087
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003088 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
3090 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003091 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 /*
3094 * setup odev and create initial packet.
3095 */
3096 pktgen_setup_inject(pkt_dev);
3097
Luiz Capitulino222f1802006-03-20 22:16:13 -08003098 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003100 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003102 pkt_dev->started_at =
3103 pkt_dev->next_tx = ktime_now();
3104
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003105 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 strcpy(pkt_dev->result, "Starting");
3108 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003109 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 strcpy(pkt_dev->result, "Error starting");
3111 }
3112 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003113 if (started)
3114 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
3117static void pktgen_stop_all_threads_ifs(void)
3118{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003119 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003121 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003123 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003124
3125 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003126 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003127
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003128 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Stephen Hemminger648fda72009-08-27 13:55:07 +00003131static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003133 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003135 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003136 if (pkt_dev->running)
3137 return 1;
3138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
Luiz Capitulino222f1802006-03-20 22:16:13 -08003141static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003143 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Luiz Capitulino222f1802006-03-20 22:16:13 -08003145 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Luiz Capitulino222f1802006-03-20 22:16:13 -08003147 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Luiz Capitulino222f1802006-03-20 22:16:13 -08003149 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151 if (signal_pending(current))
3152 goto signal;
3153 if_lock(t);
3154 }
3155 if_unlock(t);
3156 return 1;
3157signal:
3158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
3161static int pktgen_wait_all_threads_run(void)
3162{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003163 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003165
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003166 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003167
3168 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003170 if (sig == 0)
3171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003173
3174 if (sig == 0)
3175 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003177
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003178 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 return sig;
3180}
3181
3182static void pktgen_run_all_threads(void)
3183{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003184 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003186 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003188 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003190 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003192
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003193 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003195 /* Propagate thread->control */
3196 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 pktgen_wait_all_threads_run();
3199}
3200
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003201static void pktgen_reset_all_threads(void)
3202{
3203 struct pktgen_thread *t;
3204
3205 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3206
3207 mutex_lock(&pktgen_thread_lock);
3208
3209 list_for_each_entry(t, &pktgen_threads, th_list)
3210 t->control |= (T_REMDEVALL);
3211
3212 mutex_unlock(&pktgen_thread_lock);
3213
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003214 /* Propagate thread->control */
3215 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003216
3217 pktgen_wait_all_threads_run();
3218}
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3221{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003222 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003223 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003224 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3225 pkt_dev->started_at);
3226 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003228 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3229 (unsigned long long)ktime_to_us(elapsed),
3230 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3231 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003232 (unsigned long long)pkt_dev->sofar,
3233 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003235 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3236 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Luiz Capitulino222f1802006-03-20 22:16:13 -08003238 bps = pps * 8 * pkt_dev->cur_pkt_size;
3239
3240 mbps = bps;
3241 do_div(mbps, 1000000);
3242 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3243 (unsigned long long)pps,
3244 (unsigned long long)mbps,
3245 (unsigned long long)bps,
3246 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003250static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003252 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003253
Luiz Capitulino222f1802006-03-20 22:16:13 -08003254 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003255 printk(KERN_WARNING "pktgen: interface: %s is already "
3256 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003257 return -EINVAL;
3258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003260 kfree_skb(pkt_dev->skb);
3261 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003262 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003263 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003265 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Luiz Capitulino222f1802006-03-20 22:16:13 -08003267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268}
3269
Luiz Capitulino222f1802006-03-20 22:16:13 -08003270static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003272 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if_lock(t);
3275
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003276 list_for_each_entry(pkt_dev, &t->if_list, list) {
3277 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003278 continue;
3279 if (best == NULL)
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003280 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003281 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003282 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 }
3284 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003285 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
Luiz Capitulino222f1802006-03-20 22:16:13 -08003288static void pktgen_stop(struct pktgen_thread *t)
3289{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003290 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003292 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Luiz Capitulino222f1802006-03-20 22:16:13 -08003294 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003296 list_for_each_entry(pkt_dev, &t->if_list, list) {
3297 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Luiz Capitulino222f1802006-03-20 22:16:13 -08003300 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003303/*
3304 * one of our devices needs to be removed - find it
3305 * and remove it
3306 */
3307static void pktgen_rem_one_if(struct pktgen_thread *t)
3308{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003309 struct list_head *q, *n;
3310 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003311
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003312 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003313
3314 if_lock(t);
3315
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003316 list_for_each_safe(q, n, &t->if_list) {
3317 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003318
Luiz Capitulino222f1802006-03-20 22:16:13 -08003319 if (!cur->removal_mark)
3320 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003321
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003322 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003323 cur->skb = NULL;
3324
3325 pktgen_remove_device(t, cur);
3326
3327 break;
3328 }
3329
3330 if_unlock(t);
3331}
3332
Luiz Capitulino222f1802006-03-20 22:16:13 -08003333static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003335 struct list_head *q, *n;
3336 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003337
3338 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003339
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003340 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003341 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003343 list_for_each_safe(q, n, &t->if_list) {
3344 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003345
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003346 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003347 cur->skb = NULL;
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 pktgen_remove_device(t, cur);
3350 }
3351
Luiz Capitulino222f1802006-03-20 22:16:13 -08003352 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Luiz Capitulino222f1802006-03-20 22:16:13 -08003355static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003357 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
David S. Milleree74baa2007-01-01 20:51:53 -08003359 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003361 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003363 list_del(&t->th_list);
3364
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003365 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003368static void idle(struct pktgen_dev *pkt_dev)
3369{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003370 ktime_t idle_start = ktime_now();
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003371
3372 if (need_resched())
3373 schedule();
3374 else
3375 cpu_relax();
3376
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003377 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003378}
3379
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003380
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003381static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382{
Stephen Hemminger00829822008-11-20 20:14:53 -08003383 struct net_device *odev = pkt_dev->odev;
3384 int (*xmit)(struct sk_buff *, struct net_device *)
3385 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003386 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003387 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 int ret;
3389
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003390 if (pkt_dev->delay) {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07003391 spin(pkt_dev, pkt_dev->next_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 /* This is max DELAY, this has special meaning of
3394 * "never transmit"
3395 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003396 if (pkt_dev->delay == ULLONG_MAX) {
3397 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003398 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 }
3400 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003401
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003402 if (!pkt_dev->skb) {
3403 set_cur_queue_map(pkt_dev);
3404 queue_map = pkt_dev->cur_queue_map;
3405 } else {
3406 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3407 }
3408
3409 txq = netdev_get_tx_queue(odev, queue_map);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003410 /* Did we saturate the queue already? */
3411 if (netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)) {
3412 /* If device is down, then all queues are permnantly frozen */
3413 if (netif_running(odev))
3414 idle(pkt_dev);
3415 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003419
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003420 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3421 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3422 /* build a new pkt */
3423 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003424
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003425 pkt_dev->skb = fill_packet(odev, pkt_dev);
3426 if (pkt_dev->skb == NULL) {
3427 printk(KERN_ERR "pktgen: ERROR: couldn't "
3428 "allocate skb in fill_packet.\n");
3429 schedule();
3430 pkt_dev->clone_count--; /* back out increment, OOM */
3431 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003433
3434 pkt_dev->allocated_skbs++;
3435 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003437
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003438 /* fill_packet() might have changed the queue */
3439 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3440 txq = netdev_get_tx_queue(odev, queue_map);
3441
3442 __netif_tx_lock_bh(txq);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003443 if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)))
3444 pkt_dev->last_ok = 0;
3445 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 atomic_inc(&(pkt_dev->skb->users));
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003447
3448 retry_now:
Stephen Hemminger00829822008-11-20 20:14:53 -08003449 ret = (*xmit)(pkt_dev->skb, odev);
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003450 switch (ret) {
3451 case NETDEV_TX_OK:
Eric Dumazet08baf562009-05-25 22:58:01 -07003452 txq_trans_update(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003453 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 pkt_dev->sofar++;
3455 pkt_dev->seq_num++;
3456 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003457 break;
3458 case NETDEV_TX_LOCKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 cpu_relax();
3460 goto retry_now;
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003461 default: /* Drivers are not supposed to return other values! */
3462 if (net_ratelimit())
3463 pr_info("pktgen: %s xmit error: %d\n",
3464 odev->name, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 pkt_dev->errors++;
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003466 /* fallthru */
3467 case NETDEV_TX_BUSY:
3468 /* Retry it next time */
3469 atomic_dec(&(pkt_dev->skb->users));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 pkt_dev->last_ok = 0;
3471 }
3472
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003473 if (pkt_dev->delay)
3474 pkt_dev->next_tx = ktime_add_ns(ktime_now(),
3475 pkt_dev->delay);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003476 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003477 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 /* If pkt_dev->count is zero, then run forever */
3480 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemminger64e8ff52009-08-27 13:55:18 +00003481 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3482 if (signal_pending(current))
3483 break;
3484 idle(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 /* Done with this */
3488 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003489 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003492/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 * Main loop of the thread goes here
3494 */
3495
David S. Milleree74baa2007-01-01 20:51:53 -08003496static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
3498 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003499 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003500 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
David S. Milleree74baa2007-01-01 20:51:53 -08003503 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003506 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003508 pr_debug("pktgen: starting pktgen/%d: pid=%d\n",
3509 cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
David S. Milleree74baa2007-01-01 20:51:53 -08003511 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003512
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003513 set_freezable();
3514
David S. Milleree74baa2007-01-01 20:51:53 -08003515 while (!kthread_should_stop()) {
3516 pkt_dev = next_to_run(t);
3517
3518 if (!pkt_dev &&
3519 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3520 == 0) {
3521 prepare_to_wait(&(t->queue), &wait,
3522 TASK_INTERRUPTIBLE);
3523 schedule_timeout(HZ / 10);
3524 finish_wait(&(t->queue), &wait);
3525 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 __set_current_state(TASK_RUNNING);
3528
Robert Olssonb1639112007-08-28 15:46:58 -07003529 if (pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 pktgen_xmit(pkt_dev);
3531
Luiz Capitulino222f1802006-03-20 22:16:13 -08003532 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 pktgen_stop(t);
3534 t->control &= ~(T_STOP);
3535 }
3536
Luiz Capitulino222f1802006-03-20 22:16:13 -08003537 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 pktgen_run(t);
3539 t->control &= ~(T_RUN);
3540 }
3541
Luiz Capitulino222f1802006-03-20 22:16:13 -08003542 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003544 t->control &= ~(T_REMDEVALL);
3545 }
3546
Luiz Capitulino222f1802006-03-20 22:16:13 -08003547 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003548 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 t->control &= ~(T_REMDEV);
3550 }
3551
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003552 try_to_freeze();
3553
David S. Milleree74baa2007-01-01 20:51:53 -08003554 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003557 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003558 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003560 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003561 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003563 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003564 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003565
David S. Milleree74baa2007-01-01 20:51:53 -08003566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
Luiz Capitulino222f1802006-03-20 22:16:13 -08003569static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3570 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003572 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003573 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003575 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003576 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003577 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003578 break;
3579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Luiz Capitulino222f1802006-03-20 22:16:13 -08003581 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003582 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003583 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584}
3585
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003586/*
3587 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 */
3589
Luiz Capitulino222f1802006-03-20 22:16:13 -08003590static int add_dev_to_thread(struct pktgen_thread *t,
3591 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Luiz Capitulino222f1802006-03-20 22:16:13 -08003595 if_lock(t);
3596
3597 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003598 printk(KERN_ERR "pktgen: ERROR: already assigned "
3599 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003600 rv = -EBUSY;
3601 goto out;
3602 }
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003603
3604 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003605 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 pkt_dev->running = 0;
3607
Luiz Capitulino222f1802006-03-20 22:16:13 -08003608out:
3609 if_unlock(t);
3610 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
3613/* Called under thread lock */
3614
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003617 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003618 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 /* We don't allow a device to be on several threads */
3621
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003622 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3623 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003624 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003625 return -EBUSY;
3626 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003627
3628 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3629 if (!pkt_dev)
3630 return -ENOMEM;
3631
Luiz Capitulino222f1802006-03-20 22:16:13 -08003632 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003633 if (pkt_dev->flows == NULL) {
3634 kfree(pkt_dev);
3635 return -ENOMEM;
3636 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003637 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003638
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003639 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003640 pkt_dev->min_pkt_size = ETH_ZLEN;
3641 pkt_dev->max_pkt_size = ETH_ZLEN;
3642 pkt_dev->nfrags = 0;
3643 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003644 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003645 pkt_dev->count = pg_count_d;
3646 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003648 pkt_dev->udp_src_max = 9;
3649 pkt_dev->udp_dst_min = 9;
3650 pkt_dev->udp_dst_max = 9;
3651
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003652 pkt_dev->vlan_p = 0;
3653 pkt_dev->vlan_cfi = 0;
3654 pkt_dev->vlan_id = 0xffff;
3655 pkt_dev->svlan_p = 0;
3656 pkt_dev->svlan_cfi = 0;
3657 pkt_dev->svlan_id = 0xffff;
3658
Stephen Hemminger39df2322007-03-04 16:11:51 -08003659 err = pktgen_setup_dev(pkt_dev, ifname);
3660 if (err)
3661 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003662
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003663 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3664 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003665 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003666 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003667 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003668 err = -EINVAL;
3669 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003670 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003671#ifdef CONFIG_XFRM
3672 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3673 pkt_dev->ipsproto = IPPROTO_ESP;
3674#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003675
3676 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003677out2:
3678 dev_put(pkt_dev->odev);
3679out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003680#ifdef CONFIG_XFRM
3681 free_SAs(pkt_dev);
3682#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003683 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003684 kfree(pkt_dev);
3685 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686}
3687
David S. Milleree74baa2007-01-01 20:51:53 -08003688static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003690 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003691 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003692 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Luiz Capitulino222f1802006-03-20 22:16:13 -08003694 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3695 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003696 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3697 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003698 return -ENOMEM;
3699 }
3700
Luiz Capitulino222f1802006-03-20 22:16:13 -08003701 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003703
David S. Milleree74baa2007-01-01 20:51:53 -08003704 INIT_LIST_HEAD(&t->if_list);
3705
3706 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003707 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003708
3709 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3710 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003711 printk(KERN_ERR "pktgen: kernel_thread() failed "
3712 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003713 list_del(&t->th_list);
3714 kfree(t);
3715 return PTR_ERR(p);
3716 }
3717 kthread_bind(p, cpu);
3718 t->tsk = p;
3719
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003720 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3721 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003722 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003723 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003724 PG_PROC_DIR, t->tsk->comm);
3725 kthread_stop(p);
3726 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003727 kfree(t);
3728 return -EINVAL;
3729 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003730
David S. Milleree74baa2007-01-01 20:51:53 -08003731 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003732 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
3734 return 0;
3735}
3736
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003737/*
3738 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003740static void _rem_dev_from_if_list(struct pktgen_thread *t,
3741 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003743 struct list_head *q, *n;
3744 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003746 list_for_each_safe(q, n, &t->if_list) {
3747 p = list_entry(q, struct pktgen_dev, list);
3748 if (p == pkt_dev)
3749 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 }
3751}
3752
Luiz Capitulino222f1802006-03-20 22:16:13 -08003753static int pktgen_remove_device(struct pktgen_thread *t,
3754 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
3756
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003757 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Luiz Capitulino222f1802006-03-20 22:16:13 -08003759 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003760 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3761 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003762 pktgen_stop_device(pkt_dev);
3763 }
3764
3765 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 if (pkt_dev->odev) {
3768 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003769 pkt_dev->odev = NULL;
3770 }
3771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 /* And update the thread if_list */
3773
3774 _rem_dev_from_if_list(t, pkt_dev);
3775
Stephen Hemminger39df2322007-03-04 16:11:51 -08003776 if (pkt_dev->entry)
3777 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003779#ifdef CONFIG_XFRM
3780 free_SAs(pkt_dev);
3781#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003782 vfree(pkt_dev->flows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785}
3786
Luiz Capitulino222f1802006-03-20 22:16:13 -08003787static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
3789 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003790 struct proc_dir_entry *pe;
3791
David S. Miller25a8b252007-07-30 16:11:48 -07003792 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003794 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003795 if (!pg_proc_dir)
3796 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Wang Chen25296d52008-02-28 14:11:49 -08003798 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003799 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003800 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3801 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003802 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003803 return -EINVAL;
3804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 /* Register us to receive netdevice events */
3807 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003808
John Hawkes670c02c2005-10-13 09:30:31 -07003809 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003810 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
David S. Milleree74baa2007-01-01 20:51:53 -08003812 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003813 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003814 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3815 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003816 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003817
3818 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003819 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3820 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003821 unregister_netdevice_notifier(&pktgen_notifier_block);
3822 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003823 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003824 return -ENODEV;
3825 }
3826
Luiz Capitulino222f1802006-03-20 22:16:13 -08003827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828}
3829
3830static void __exit pg_cleanup(void)
3831{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003832 struct pktgen_thread *t;
3833 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 wait_queue_head_t queue;
3835 init_waitqueue_head(&queue);
3836
Luiz Capitulino222f1802006-03-20 22:16:13 -08003837 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003839 list_for_each_safe(q, n, &pktgen_threads) {
3840 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003841 kthread_stop(t->tsk);
3842 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Luiz Capitulino222f1802006-03-20 22:16:13 -08003845 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 unregister_netdevice_notifier(&pktgen_notifier_block);
3847
Luiz Capitulino222f1802006-03-20 22:16:13 -08003848 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003849 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003850 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851}
3852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853module_init(pg_init);
3854module_exit(pg_cleanup);
3855
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003856MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857MODULE_DESCRIPTION("Packet Generator tool");
3858MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003859MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003861MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003863MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003865MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003867MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");