blob: 2ad68da418df6a6d6d4ae81db9e78d4a8f6ae981 [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
Robert Olssone99b99b2010-03-18 22:44:30 +0000172#define VERSION "2.73"
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() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000193#define F_NODE (1<<15) /* Node memory alloc*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000196#define T_STOP (1<<0) /* Stop run */
197#define T_RUN (1<<1) /* Start run */
198#define T_REMDEVALL (1<<2) /* Remove all devs */
199#define T_REMDEV (1<<3) /* 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 Viro252e3342006-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 */
John Fastabendf466dba2009-12-23 22:02:57 -0800254 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Luiz Capitulino222f1802006-03-20 22:16:13 -0800256 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Luiz Capitulino222f1802006-03-20 22:16:13 -0800258 __u64 allocated_skbs;
259 __u32 clone_count;
260 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000261 * Or a failed transmit of some sort?
262 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800263 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000264 ktime_t next_tx;
265 ktime_t started_at;
266 ktime_t stopped_at;
267 u64 idle_acc; /* nano-seconds */
268
Luiz Capitulino222f1802006-03-20 22:16:13 -0800269 __u32 seq_num;
270
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000271 int clone_skb; /*
272 * Use multiple SKBs during packet gen.
273 * If this number is greater than 1, then
274 * that many copies of the same packet will be
275 * sent before a new packet is allocated.
276 * If you want to send 1024 identical packets
277 * before creating a new packet,
278 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800279 */
280
281 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
283 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
284 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
285
286 struct in6_addr in6_saddr;
287 struct in6_addr in6_daddr;
288 struct in6_addr cur_in6_daddr;
289 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800291 struct in6_addr min_in6_daddr;
292 struct in6_addr max_in6_daddr;
293 struct in6_addr min_in6_saddr;
294 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Luiz Capitulino222f1802006-03-20 22:16:13 -0800296 /* If we're doing ranges, random or incremental, then this
297 * defines the min/max for those ranges.
298 */
Al Viro252e3342006-11-14 20:48:11 -0800299 __be32 saddr_min; /* inclusive, source IP address */
300 __be32 saddr_max; /* exclusive, source IP address */
301 __be32 daddr_min; /* inclusive, dest IP address */
302 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Luiz Capitulino222f1802006-03-20 22:16:13 -0800304 __u16 udp_src_min; /* inclusive, source UDP port */
305 __u16 udp_src_max; /* exclusive, source UDP port */
306 __u16 udp_dst_min; /* inclusive, dest UDP port */
307 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700309 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000310 __u8 tos; /* six MSB of (former) IPv4 TOS
311 are for dscp codepoint */
312 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
313 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700314
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800315 /* MPLS */
316 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
317 __be32 labels[MAX_MPLS_LABELS];
318
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700319 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
320 __u8 vlan_p;
321 __u8 vlan_cfi;
322 __u16 vlan_id; /* 0xffff means no vlan tag */
323
324 __u8 svlan_p;
325 __u8 svlan_cfi;
326 __u16 svlan_id; /* 0xffff means no svlan tag */
327
Luiz Capitulino222f1802006-03-20 22:16:13 -0800328 __u32 src_mac_count; /* How many MACs to iterate through */
329 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Luiz Capitulino222f1802006-03-20 22:16:13 -0800331 unsigned char dst_mac[ETH_ALEN];
332 unsigned char src_mac[ETH_ALEN];
333
334 __u32 cur_dst_mac_offset;
335 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800336 __be32 cur_saddr;
337 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700338 __u16 ip_id;
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;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800343 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800344
345 __u8 hh[14];
346 /* = {
347 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
348
349 We fill in SRC address later
350 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
351 0x08, 0x00
352 };
353 */
354 __u16 pad; /* pad out the hh struct to an even 16 bytes */
355
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000356 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800357 * are transmitting the same one multiple times
358 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000359 struct net_device *odev; /* The out-going device.
360 * Note that the device should have it's
361 * pg_info pointer pointing back to this
362 * device.
363 * Set when the user specifies the out-going
364 * device name (not when the inject is
365 * started as it used to do.)
366 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000367 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800369 unsigned cflows; /* Concurrent flows (config) */
370 unsigned lflow; /* Flow length (config) */
371 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700372 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700373
374 u16 queue_map_min;
375 u16 queue_map_max;
Robert Olssone99b99b2010-03-18 22:44:30 +0000376 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700377
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700378#ifdef CONFIG_XFRM
379 __u8 ipsmode; /* IPSEC mode (config) */
380 __u8 ipsproto; /* IPSEC type (config) */
381#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800382 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383};
384
385struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800386 __be32 pgh_magic;
387 __be32 seq_num;
388 __be32 tv_sec;
389 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390};
391
392struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000393 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800394 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800395 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800396 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800397 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000399 /* Field for thread to receive "posted" events terminate,
400 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800401
402 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int cpu;
404
Luiz Capitulino222f1802006-03-20 22:16:13 -0800405 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700406 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407};
408
409#define REMOVE 1
410#define FIND 0
411
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000412static inline ktime_t ktime_now(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000414 struct timespec ts;
415 ktime_get_ts(&ts);
416
417 return timespec_to_ktime(ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000420/* This works even if 32 bit because of careful byte order choice */
421static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000423 return cmp1.tv64 < cmp2.tv64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000426static const char version[] =
427 "pktgen " VERSION ": Packet Generator for packet performance testing.\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Luiz Capitulino222f1802006-03-20 22:16:13 -0800429static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
430static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
431static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800432 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
434static void pktgen_run_all_threads(void);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800435static void pktgen_reset_all_threads(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void pktgen_stop_all_threads_ifs(void);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000437
Luiz Capitulino222f1802006-03-20 22:16:13 -0800438static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800440
Luiz Capitulino222f1802006-03-20 22:16:13 -0800441static unsigned int scan_ip6(const char *s, char ip[16]);
442static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000445static int pg_count_d __read_mostly = 1000;
446static int pg_delay_d __read_mostly;
447static int pg_clone_skb_d __read_mostly;
448static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Luiz Capitulino222fa072006-03-20 22:24:27 -0800450static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800451static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static struct notifier_block pktgen_notifier_block = {
454 .notifier_call = pktgen_device_event,
455};
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900458 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *
460 */
461
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700462static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800463{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000464 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000468static ssize_t pgctrl_write(struct file *file, const char __user *buf,
469 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700472 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Luiz Capitulino222f1802006-03-20 22:16:13 -0800474 if (!capable(CAP_NET_ADMIN)) {
475 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700479 if (count > sizeof(data))
480 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700483 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700484 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800485 }
486 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Luiz Capitulino222f1802006-03-20 22:16:13 -0800488 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 pktgen_stop_all_threads_ifs();
490
Luiz Capitulino222f1802006-03-20 22:16:13 -0800491 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 pktgen_run_all_threads();
493
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800494 else if (!strcmp(data, "reset"))
495 pktgen_reset_all_threads();
496
Luiz Capitulino222f1802006-03-20 22:16:13 -0800497 else
David S. Miller25a8b252007-07-30 16:11:48 -0700498 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 err = count;
501
Luiz Capitulino222f1802006-03-20 22:16:13 -0800502out:
503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700506static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700508 return single_open(file, pgctrl_show, PDE(inode)->data);
509}
510
Arjan van de Ven9a321442007-02-12 00:55:35 -0800511static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800512 .owner = THIS_MODULE,
513 .open = pgctrl_open,
514 .read = seq_read,
515 .llseek = seq_lseek,
516 .write = pgctrl_write,
517 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700518};
519
520static int pktgen_if_show(struct seq_file *seq, void *v)
521{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000522 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000523 ktime_t stopped;
524 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Luiz Capitulino222f1802006-03-20 22:16:13 -0800526 seq_printf(seq,
527 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
528 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
529 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Luiz Capitulino222f1802006-03-20 22:16:13 -0800531 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000532 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
533 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000534 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Luiz Capitulino222f1802006-03-20 22:16:13 -0800536 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
537 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Robert Olsson45b270f2007-08-28 15:45:55 -0700539 seq_printf(seq,
540 " queue_map_min: %u queue_map_max: %u\n",
541 pkt_dev->queue_map_min,
542 pkt_dev->queue_map_max);
543
Luiz Capitulino222f1802006-03-20 22:16:13 -0800544 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800546 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
547 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
548 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
549 seq_printf(seq,
550 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
551 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Luiz Capitulino222f1802006-03-20 22:16:13 -0800553 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
554 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
555 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
556 seq_printf(seq,
557 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
558 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000560 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800561 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000562 " dst_min: %s dst_max: %s\n",
563 pkt_dev->dst_min, pkt_dev->dst_max);
564 seq_printf(seq,
565 " src_min: %s src_max: %s\n",
566 pkt_dev->src_min, pkt_dev->src_max);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700569 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Johannes Berge1749612008-10-27 15:59:26 -0700571 seq_printf(seq, "%pM ",
572 is_zero_ether_addr(pkt_dev->src_mac) ?
573 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Luiz Capitulino222f1802006-03-20 22:16:13 -0800575 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700576 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Luiz Capitulino222f1802006-03-20 22:16:13 -0800578 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000579 " udp_src_min: %d udp_src_max: %d"
580 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800581 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
582 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Luiz Capitulino222f1802006-03-20 22:16:13 -0800584 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800585 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700586 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800588 if (pkt_dev->nr_labels) {
589 unsigned i;
590 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700591 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800592 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
593 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
594 }
595
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000596 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700597 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000598 pkt_dev->vlan_id, pkt_dev->vlan_p,
599 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700600
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000601 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700602 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000603 pkt_dev->svlan_id, pkt_dev->svlan_p,
604 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700605
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000606 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700607 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700608
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000609 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700610 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700611
Robert Olssone99b99b2010-03-18 22:44:30 +0000612 if (pkt_dev->node >= 0)
613 seq_printf(seq, " node: %d\n", pkt_dev->node);
614
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800615 seq_printf(seq, " Flags: ");
616
Luiz Capitulino222f1802006-03-20 22:16:13 -0800617 if (pkt_dev->flags & F_IPV6)
618 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Luiz Capitulino222f1802006-03-20 22:16:13 -0800620 if (pkt_dev->flags & F_IPSRC_RND)
621 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Luiz Capitulino222f1802006-03-20 22:16:13 -0800623 if (pkt_dev->flags & F_IPDST_RND)
624 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Luiz Capitulino222f1802006-03-20 22:16:13 -0800626 if (pkt_dev->flags & F_TXSIZE_RND)
627 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Luiz Capitulino222f1802006-03-20 22:16:13 -0800629 if (pkt_dev->flags & F_UDPSRC_RND)
630 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Luiz Capitulino222f1802006-03-20 22:16:13 -0800632 if (pkt_dev->flags & F_UDPDST_RND)
633 seq_printf(seq, "UDPDST_RND ");
634
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800635 if (pkt_dev->flags & F_MPLS_RND)
636 seq_printf(seq, "MPLS_RND ");
637
Robert Olsson45b270f2007-08-28 15:45:55 -0700638 if (pkt_dev->flags & F_QUEUE_MAP_RND)
639 seq_printf(seq, "QUEUE_MAP_RND ");
640
Robert Olssone6fce5b2008-08-07 02:23:01 -0700641 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
642 seq_printf(seq, "QUEUE_MAP_CPU ");
643
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700644 if (pkt_dev->cflows) {
645 if (pkt_dev->flags & F_FLOW_SEQ)
646 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
647 else
648 seq_printf(seq, "FLOW_RND ");
649 }
650
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700651#ifdef CONFIG_XFRM
652 if (pkt_dev->flags & F_IPSEC_ON)
653 seq_printf(seq, "IPSEC ");
654#endif
655
Luiz Capitulino222f1802006-03-20 22:16:13 -0800656 if (pkt_dev->flags & F_MACSRC_RND)
657 seq_printf(seq, "MACSRC_RND ");
658
659 if (pkt_dev->flags & F_MACDST_RND)
660 seq_printf(seq, "MACDST_RND ");
661
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700662 if (pkt_dev->flags & F_VID_RND)
663 seq_printf(seq, "VID_RND ");
664
665 if (pkt_dev->flags & F_SVID_RND)
666 seq_printf(seq, "SVID_RND ");
667
Robert Olssone99b99b2010-03-18 22:44:30 +0000668 if (pkt_dev->flags & F_NODE)
669 seq_printf(seq, "NODE_ALLOC ");
670
Luiz Capitulino222f1802006-03-20 22:16:13 -0800671 seq_puts(seq, "\n");
672
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000673 /* not really stopped, more like last-running-at */
674 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
675 idle = pkt_dev->idle_acc;
676 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800677
678 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000679 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800680 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000681 (unsigned long long)pkt_dev->errors);
682
683 seq_printf(seq,
684 " started: %lluus stopped: %lluus idle: %lluus\n",
685 (unsigned long long) ktime_to_us(pkt_dev->started_at),
686 (unsigned long long) ktime_to_us(stopped),
687 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800688
689 seq_printf(seq,
690 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700691 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
692 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Luiz Capitulino222f1802006-03-20 22:16:13 -0800694 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800696 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
697 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
698 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
699 } else
700 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700701 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Luiz Capitulino222f1802006-03-20 22:16:13 -0800703 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700704 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Robert Olsson45b270f2007-08-28 15:45:55 -0700706 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
707
Luiz Capitulino222f1802006-03-20 22:16:13 -0800708 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800711 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800713 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800718
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000719static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
720 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800721{
722 int i = 0;
723 *num = 0;
724
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700725 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800726 char c;
727 *num <<= 4;
728 if (get_user(c, &user_buffer[i]))
729 return -EFAULT;
730 if ((c >= '0') && (c <= '9'))
731 *num |= c - '0';
732 else if ((c >= 'a') && (c <= 'f'))
733 *num |= c - 'a' + 10;
734 else if ((c >= 'A') && (c <= 'F'))
735 *num |= c - 'A' + 10;
736 else
737 break;
738 }
739 return i;
740}
741
Luiz Capitulino222f1802006-03-20 22:16:13 -0800742static int count_trail_chars(const char __user * user_buffer,
743 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 int i;
746
747 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800748 char c;
749 if (get_user(c, &user_buffer[i]))
750 return -EFAULT;
751 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 case '\"':
753 case '\n':
754 case '\r':
755 case '\t':
756 case ' ':
757 case '=':
758 break;
759 default:
760 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763done:
764 return i;
765}
766
Luiz Capitulino222f1802006-03-20 22:16:13 -0800767static unsigned long num_arg(const char __user * user_buffer,
768 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 int i = 0;
771 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800772
773 for (; i < maxlen; i++) {
774 char c;
775 if (get_user(c, &user_buffer[i]))
776 return -EFAULT;
777 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800779 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 } else
781 break;
782 }
783 return i;
784}
785
Luiz Capitulino222f1802006-03-20 22:16:13 -0800786static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 int i = 0;
789
Luiz Capitulino222f1802006-03-20 22:16:13 -0800790 for (; i < maxlen; i++) {
791 char c;
792 if (get_user(c, &user_buffer[i]))
793 return -EFAULT;
794 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 case '\"':
796 case '\n':
797 case '\r':
798 case '\t':
799 case ' ':
800 goto done_str;
801 break;
802 default:
803 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return i;
808}
809
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800810static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
811{
812 unsigned n = 0;
813 char c;
814 ssize_t i = 0;
815 int len;
816
817 pkt_dev->nr_labels = 0;
818 do {
819 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700820 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800821 if (len <= 0)
822 return len;
823 pkt_dev->labels[n] = htonl(tmp);
824 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
825 pkt_dev->flags |= F_MPLS_RND;
826 i += len;
827 if (get_user(c, &buffer[i]))
828 return -EFAULT;
829 i++;
830 n++;
831 if (n >= MAX_MPLS_LABELS)
832 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700833 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800834
835 pkt_dev->nr_labels = n;
836 return i;
837}
838
Luiz Capitulino222f1802006-03-20 22:16:13 -0800839static ssize_t pktgen_if_write(struct file *file,
840 const char __user * user_buffer, size_t count,
841 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800843 struct seq_file *seq = (struct seq_file *)file->private_data;
844 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 int i = 0, max, len;
846 char name[16], valstr[32];
847 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800848 char *pg_result = NULL;
849 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800851
852 pg_result = &(pkt_dev->result[0]);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700855 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return -EINVAL;
857 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 max = count - i;
860 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800861 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700862 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800865 i += tmp;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* Read variable name */
868
869 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000870 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800871 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800874 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return -EFAULT;
876 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800877
878 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800880 if (len < 0)
881 return len;
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 i += len;
884
885 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886 char tb[count + 1];
887 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700890 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800891 (unsigned long)count, tb);
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (!strcmp(name, "min_pkt_size")) {
895 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000896 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800897 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800900 if (value < 14 + 20 + 8)
901 value = 14 + 20 + 8;
902 if (value != pkt_dev->min_pkt_size) {
903 pkt_dev->min_pkt_size = value;
904 pkt_dev->cur_pkt_size = value;
905 }
906 sprintf(pg_result, "OK: min_pkt_size=%u",
907 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return count;
909 }
910
Luiz Capitulino222f1802006-03-20 22:16:13 -0800911 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000913 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800914 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800917 if (value < 14 + 20 + 8)
918 value = 14 + 20 + 8;
919 if (value != pkt_dev->max_pkt_size) {
920 pkt_dev->max_pkt_size = value;
921 pkt_dev->cur_pkt_size = value;
922 }
923 sprintf(pg_result, "OK: max_pkt_size=%u",
924 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return count;
926 }
927
Luiz Capitulino222f1802006-03-20 22:16:13 -0800928 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (!strcmp(name, "pkt_size")) {
931 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000932 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800933 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800936 if (value < 14 + 20 + 8)
937 value = 14 + 20 + 8;
938 if (value != pkt_dev->min_pkt_size) {
939 pkt_dev->min_pkt_size = value;
940 pkt_dev->max_pkt_size = value;
941 pkt_dev->cur_pkt_size = value;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
944 return count;
945 }
946
Luiz Capitulino222f1802006-03-20 22:16:13 -0800947 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000949 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800950 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800953 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 sprintf(pg_result, "OK: debug=%u", debug);
955 return count;
956 }
957
Luiz Capitulino222f1802006-03-20 22:16:13 -0800958 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000960 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800961 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 i += len;
964 pkt_dev->nfrags = value;
965 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
966 return count;
967 }
968 if (!strcmp(name, "delay")) {
969 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000970 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800971 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000974 if (value == 0x7FFFFFFF)
975 pkt_dev->delay = ULLONG_MAX;
976 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000977 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000978
979 sprintf(pg_result, "OK: delay=%llu",
980 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return count;
982 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800983 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000985 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800986 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800989 if (value != pkt_dev->udp_src_min) {
990 pkt_dev->udp_src_min = value;
991 pkt_dev->cur_udp_src = value;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
994 return count;
995 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800996 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000998 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800999 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001002 if (value != pkt_dev->udp_dst_min) {
1003 pkt_dev->udp_dst_min = value;
1004 pkt_dev->cur_udp_dst = value;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1007 return count;
1008 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001009 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001011 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001012 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001015 if (value != pkt_dev->udp_src_max) {
1016 pkt_dev->udp_src_max = value;
1017 pkt_dev->cur_udp_src = value;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1020 return count;
1021 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001022 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001024 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001025 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001028 if (value != pkt_dev->udp_dst_max) {
1029 pkt_dev->udp_dst_max = value;
1030 pkt_dev->cur_udp_dst = value;
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1033 return count;
1034 }
1035 if (!strcmp(name, "clone_skb")) {
1036 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001037 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001038 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001041 pkt_dev->clone_skb = value;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1044 return count;
1045 }
1046 if (!strcmp(name, "count")) {
1047 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001048 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001049 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 i += len;
1052 pkt_dev->count = value;
1053 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return count;
1056 }
1057 if (!strcmp(name, "src_mac_count")) {
1058 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001059 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001060 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 i += len;
1063 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001064 pkt_dev->src_mac_count = value;
1065 pkt_dev->cur_src_mac_offset = 0;
1066 }
1067 sprintf(pg_result, "OK: src_mac_count=%d",
1068 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return count;
1070 }
1071 if (!strcmp(name, "dst_mac_count")) {
1072 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001073 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001074 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 i += len;
1077 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001078 pkt_dev->dst_mac_count = value;
1079 pkt_dev->cur_dst_mac_offset = 0;
1080 }
1081 sprintf(pg_result, "OK: dst_mac_count=%d",
1082 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return count;
1084 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001085 if (!strcmp(name, "node")) {
1086 len = num_arg(&user_buffer[i], 10, &value);
1087 if (len < 0)
1088 return len;
1089
1090 i += len;
1091
1092 if (node_possible(value)) {
1093 pkt_dev->node = value;
1094 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1095 }
1096 else
1097 sprintf(pg_result, "ERROR: node not possible");
1098 return count;
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001101 char f[32];
1102 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001104 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001105 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (copy_from_user(f, &user_buffer[i], len))
1108 return -EFAULT;
1109 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001110 if (strcmp(f, "IPSRC_RND") == 0)
1111 pkt_dev->flags |= F_IPSRC_RND;
1112
1113 else if (strcmp(f, "!IPSRC_RND") == 0)
1114 pkt_dev->flags &= ~F_IPSRC_RND;
1115
1116 else if (strcmp(f, "TXSIZE_RND") == 0)
1117 pkt_dev->flags |= F_TXSIZE_RND;
1118
1119 else if (strcmp(f, "!TXSIZE_RND") == 0)
1120 pkt_dev->flags &= ~F_TXSIZE_RND;
1121
1122 else if (strcmp(f, "IPDST_RND") == 0)
1123 pkt_dev->flags |= F_IPDST_RND;
1124
1125 else if (strcmp(f, "!IPDST_RND") == 0)
1126 pkt_dev->flags &= ~F_IPDST_RND;
1127
1128 else if (strcmp(f, "UDPSRC_RND") == 0)
1129 pkt_dev->flags |= F_UDPSRC_RND;
1130
1131 else if (strcmp(f, "!UDPSRC_RND") == 0)
1132 pkt_dev->flags &= ~F_UDPSRC_RND;
1133
1134 else if (strcmp(f, "UDPDST_RND") == 0)
1135 pkt_dev->flags |= F_UDPDST_RND;
1136
1137 else if (strcmp(f, "!UDPDST_RND") == 0)
1138 pkt_dev->flags &= ~F_UDPDST_RND;
1139
1140 else if (strcmp(f, "MACSRC_RND") == 0)
1141 pkt_dev->flags |= F_MACSRC_RND;
1142
1143 else if (strcmp(f, "!MACSRC_RND") == 0)
1144 pkt_dev->flags &= ~F_MACSRC_RND;
1145
1146 else if (strcmp(f, "MACDST_RND") == 0)
1147 pkt_dev->flags |= F_MACDST_RND;
1148
1149 else if (strcmp(f, "!MACDST_RND") == 0)
1150 pkt_dev->flags &= ~F_MACDST_RND;
1151
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001152 else if (strcmp(f, "MPLS_RND") == 0)
1153 pkt_dev->flags |= F_MPLS_RND;
1154
1155 else if (strcmp(f, "!MPLS_RND") == 0)
1156 pkt_dev->flags &= ~F_MPLS_RND;
1157
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001158 else if (strcmp(f, "VID_RND") == 0)
1159 pkt_dev->flags |= F_VID_RND;
1160
1161 else if (strcmp(f, "!VID_RND") == 0)
1162 pkt_dev->flags &= ~F_VID_RND;
1163
1164 else if (strcmp(f, "SVID_RND") == 0)
1165 pkt_dev->flags |= F_SVID_RND;
1166
1167 else if (strcmp(f, "!SVID_RND") == 0)
1168 pkt_dev->flags &= ~F_SVID_RND;
1169
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001170 else if (strcmp(f, "FLOW_SEQ") == 0)
1171 pkt_dev->flags |= F_FLOW_SEQ;
1172
Robert Olsson45b270f2007-08-28 15:45:55 -07001173 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1174 pkt_dev->flags |= F_QUEUE_MAP_RND;
1175
1176 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1177 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001178
1179 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1180 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1181
1182 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1183 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001184#ifdef CONFIG_XFRM
1185 else if (strcmp(f, "IPSEC") == 0)
1186 pkt_dev->flags |= F_IPSEC_ON;
1187#endif
1188
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001189 else if (strcmp(f, "!IPV6") == 0)
1190 pkt_dev->flags &= ~F_IPV6;
1191
Robert Olssone99b99b2010-03-18 22:44:30 +00001192 else if (strcmp(f, "NODE_ALLOC") == 0)
1193 pkt_dev->flags |= F_NODE;
1194
1195 else if (strcmp(f, "!NODE_ALLOC") == 0)
1196 pkt_dev->flags &= ~F_NODE;
1197
Luiz Capitulino222f1802006-03-20 22:16:13 -08001198 else {
1199 sprintf(pg_result,
1200 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1201 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001202 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001203 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001204 return count;
1205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1207 return count;
1208 }
1209 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1210 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001211 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001212 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Luiz Capitulino222f1802006-03-20 22:16:13 -08001214 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001216 buf[len] = 0;
1217 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1218 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1219 strncpy(pkt_dev->dst_min, buf, len);
1220 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1221 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1222 }
1223 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001224 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001225 pkt_dev->dst_min);
1226 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1228 return count;
1229 }
1230 if (!strcmp(name, "dst_max")) {
1231 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001232 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001233 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Luiz Capitulino222f1802006-03-20 22:16:13 -08001236 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return -EFAULT;
1238
Luiz Capitulino222f1802006-03-20 22:16:13 -08001239 buf[len] = 0;
1240 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1241 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1242 strncpy(pkt_dev->dst_max, buf, len);
1243 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1244 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1245 }
1246 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001247 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001248 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 i += len;
1250 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1251 return count;
1252 }
1253 if (!strcmp(name, "dst6")) {
1254 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001255 if (len < 0)
1256 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 pkt_dev->flags |= F_IPV6;
1259
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001265 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1268
Luiz Capitulino222f1802006-03-20 22:16:13 -08001269 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001270 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Luiz Capitulino222f1802006-03-20 22:16:13 -08001272 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 sprintf(pg_result, "OK: dst6=%s", buf);
1274 return count;
1275 }
1276 if (!strcmp(name, "dst6_min")) {
1277 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001278 if (len < 0)
1279 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 pkt_dev->flags |= F_IPV6;
1282
Luiz Capitulino222f1802006-03-20 22:16:13 -08001283 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001285 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001288 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Luiz Capitulino222f1802006-03-20 22:16:13 -08001290 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1291 &pkt_dev->min_in6_daddr);
1292 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001293 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Luiz Capitulino222f1802006-03-20 22:16:13 -08001295 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 sprintf(pg_result, "OK: dst6_min=%s", buf);
1297 return count;
1298 }
1299 if (!strcmp(name, "dst6_max")) {
1300 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001301 if (len < 0)
1302 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 pkt_dev->flags |= F_IPV6;
1305
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001311 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Luiz Capitulino222f1802006-03-20 22:16:13 -08001313 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001314 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Luiz Capitulino222f1802006-03-20 22:16:13 -08001316 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 sprintf(pg_result, "OK: dst6_max=%s", buf);
1318 return count;
1319 }
1320 if (!strcmp(name, "src6")) {
1321 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 if (len < 0)
1323 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 pkt_dev->flags |= F_IPV6;
1326
Luiz Capitulino222f1802006-03-20 22:16:13 -08001327 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001332 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1335
Luiz Capitulino222f1802006-03-20 22:16:13 -08001336 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001337 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338
1339 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 sprintf(pg_result, "OK: src6=%s", buf);
1341 return count;
1342 }
1343 if (!strcmp(name, "src_min")) {
1344 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001345 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001346 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001347
Luiz Capitulino222f1802006-03-20 22:16:13 -08001348 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001350 buf[len] = 0;
1351 if (strcmp(buf, pkt_dev->src_min) != 0) {
1352 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1353 strncpy(pkt_dev->src_min, buf, len);
1354 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1355 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1356 }
1357 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001358 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 i += len;
1361 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1362 return count;
1363 }
1364 if (!strcmp(name, "src_max")) {
1365 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001366 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001367 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001368
Luiz Capitulino222f1802006-03-20 22:16:13 -08001369 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001371 buf[len] = 0;
1372 if (strcmp(buf, pkt_dev->src_max) != 0) {
1373 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1374 strncpy(pkt_dev->src_max, buf, len);
1375 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1376 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1377 }
1378 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001379 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001380 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 i += len;
1382 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1383 return count;
1384 }
1385 if (!strcmp(name, "dst_mac")) {
1386 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001387 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001389 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001392 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001396 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -EFAULT;
1398 i += len;
1399
Luiz Capitulino222f1802006-03-20 22:16:13 -08001400 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (*v >= '0' && *v <= '9') {
1402 *m *= 16;
1403 *m += *v - '0';
1404 }
1405 if (*v >= 'A' && *v <= 'F') {
1406 *m *= 16;
1407 *m += *v - 'A' + 10;
1408 }
1409 if (*v >= 'a' && *v <= 'f') {
1410 *m *= 16;
1411 *m += *v - 'a' + 10;
1412 }
1413 if (*v == ':') {
1414 m++;
1415 *m = 0;
1416 }
1417 }
1418
1419 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001420 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1421 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 sprintf(pg_result, "OK: dstmac");
1424 return count;
1425 }
1426 if (!strcmp(name, "src_mac")) {
1427 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001428 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 unsigned char *m = pkt_dev->src_mac;
1430
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001431 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001434 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001435 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001438 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EFAULT;
1440 i += len;
1441
Luiz Capitulino222f1802006-03-20 22:16:13 -08001442 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (*v >= '0' && *v <= '9') {
1444 *m *= 16;
1445 *m += *v - '0';
1446 }
1447 if (*v >= 'A' && *v <= 'F') {
1448 *m *= 16;
1449 *m += *v - 'A' + 10;
1450 }
1451 if (*v >= 'a' && *v <= 'f') {
1452 *m *= 16;
1453 *m += *v - 'a' + 10;
1454 }
1455 if (*v == ':') {
1456 m++;
1457 *m = 0;
1458 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001461 /* Set up Src MAC */
1462 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1463 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1464
Luiz Capitulino222f1802006-03-20 22:16:13 -08001465 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return count;
1467 }
1468
Luiz Capitulino222f1802006-03-20 22:16:13 -08001469 if (!strcmp(name, "clear_counters")) {
1470 pktgen_clear_counters(pkt_dev);
1471 sprintf(pg_result, "OK: Clearing counters.\n");
1472 return count;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 if (!strcmp(name, "flows")) {
1476 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001477 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001478 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 i += len;
1481 if (value > MAX_CFLOWS)
1482 value = MAX_CFLOWS;
1483
1484 pkt_dev->cflows = value;
1485 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1486 return count;
1487 }
1488
1489 if (!strcmp(name, "flowlen")) {
1490 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001491 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001492 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 i += len;
1495 pkt_dev->lflow = value;
1496 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1497 return count;
1498 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001499
Robert Olsson45b270f2007-08-28 15:45:55 -07001500 if (!strcmp(name, "queue_map_min")) {
1501 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001502 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001503 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001504
Robert Olsson45b270f2007-08-28 15:45:55 -07001505 i += len;
1506 pkt_dev->queue_map_min = value;
1507 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1508 return count;
1509 }
1510
1511 if (!strcmp(name, "queue_map_max")) {
1512 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001513 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001514 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001515
Robert Olsson45b270f2007-08-28 15:45:55 -07001516 i += len;
1517 pkt_dev->queue_map_max = value;
1518 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1519 return count;
1520 }
1521
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001522 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001523 unsigned n, cnt;
1524
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001525 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001526 if (len < 0)
1527 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001528 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001529 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001530 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001531 cnt += sprintf(pg_result + cnt,
1532 "%08x%s", ntohl(pkt_dev->labels[n]),
1533 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001534
1535 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1536 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1537 pkt_dev->svlan_id = 0xffff;
1538
1539 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001540 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001541 }
1542 return count;
1543 }
1544
1545 if (!strcmp(name, "vlan_id")) {
1546 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001547 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001548 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001549
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001550 i += len;
1551 if (value <= 4095) {
1552 pkt_dev->vlan_id = value; /* turn on VLAN */
1553
1554 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001555 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001556
1557 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001558 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001559
1560 pkt_dev->nr_labels = 0; /* turn off MPLS */
1561 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1562 } else {
1563 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1564 pkt_dev->svlan_id = 0xffff;
1565
1566 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001567 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001568 }
1569 return count;
1570 }
1571
1572 if (!strcmp(name, "vlan_p")) {
1573 len = num_arg(&user_buffer[i], 1, &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 <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1579 pkt_dev->vlan_p = value;
1580 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1581 } else {
1582 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1583 }
1584 return count;
1585 }
1586
1587 if (!strcmp(name, "vlan_cfi")) {
1588 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001589 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001590 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001591
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001592 i += len;
1593 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1594 pkt_dev->vlan_cfi = value;
1595 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1596 } else {
1597 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1598 }
1599 return count;
1600 }
1601
1602 if (!strcmp(name, "svlan_id")) {
1603 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001604 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001605 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001606
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001607 i += len;
1608 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1609 pkt_dev->svlan_id = value; /* turn on SVLAN */
1610
1611 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001612 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001613
1614 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001615 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001616
1617 pkt_dev->nr_labels = 0; /* turn off MPLS */
1618 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1619 } else {
1620 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1621 pkt_dev->svlan_id = 0xffff;
1622
1623 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001624 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001625 }
1626 return count;
1627 }
1628
1629 if (!strcmp(name, "svlan_p")) {
1630 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001631 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001632 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001633
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001634 i += len;
1635 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1636 pkt_dev->svlan_p = value;
1637 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1638 } else {
1639 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1640 }
1641 return count;
1642 }
1643
1644 if (!strcmp(name, "svlan_cfi")) {
1645 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001646 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001647 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001648
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001649 i += len;
1650 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1651 pkt_dev->svlan_cfi = value;
1652 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1653 } else {
1654 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1655 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001656 return count;
1657 }
1658
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001659 if (!strcmp(name, "tos")) {
1660 __u32 tmp_value = 0;
1661 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001662 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001663 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001664
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001665 i += len;
1666 if (len == 2) {
1667 pkt_dev->tos = tmp_value;
1668 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1669 } else {
1670 sprintf(pg_result, "ERROR: tos must be 00-ff");
1671 }
1672 return count;
1673 }
1674
1675 if (!strcmp(name, "traffic_class")) {
1676 __u32 tmp_value = 0;
1677 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001678 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001679 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001680
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001681 i += len;
1682 if (len == 2) {
1683 pkt_dev->traffic_class = tmp_value;
1684 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1685 } else {
1686 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1687 }
1688 return count;
1689 }
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1692 return -EINVAL;
1693}
1694
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001695static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001697 return single_open(file, pktgen_if_show, PDE(inode)->data);
1698}
1699
Arjan van de Ven9a321442007-02-12 00:55:35 -08001700static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001701 .owner = THIS_MODULE,
1702 .open = pktgen_if_open,
1703 .read = seq_read,
1704 .llseek = seq_lseek,
1705 .write = pktgen_if_write,
1706 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001707};
1708
1709static int pktgen_thread_show(struct seq_file *seq, void *v)
1710{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001711 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001712 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001714 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Luiz Capitulino222f1802006-03-20 22:16:13 -08001716 seq_printf(seq, "Running: ");
1717
1718 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001719 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001720 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001721 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Luiz Capitulino222f1802006-03-20 22:16:13 -08001723 seq_printf(seq, "\nStopped: ");
1724
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001725 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001726 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001727 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001730 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001732 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Luiz Capitulino222f1802006-03-20 22:16:13 -08001734 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001739static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001740 const char __user * user_buffer,
1741 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001743 struct seq_file *seq = (struct seq_file *)file->private_data;
1744 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 int i = 0, max, len, ret;
1746 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001747 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001750 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return -EINVAL;
1752 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001755 len = count_trail_chars(&user_buffer[i], max);
1756 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001757 return len;
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* Read variable name */
1762
1763 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001765 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 memset(name, 0, sizeof(name));
1768 if (copy_from_user(name, &user_buffer[i], len))
1769 return -EFAULT;
1770 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001771
Luiz Capitulino222f1802006-03-20 22:16:13 -08001772 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001775 return len;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 i += len;
1778
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001779 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001780 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1781 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001784 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 ret = -EINVAL;
1786 goto out;
1787 }
1788
1789 pg_result = &(t->result[0]);
1790
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 if (!strcmp(name, "add_device")) {
1792 char f[32];
1793 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001795 if (len < 0) {
1796 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 goto out;
1798 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001799 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return -EFAULT;
1801 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001802 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001804 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805 ret = count;
1806 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 goto out;
1808 }
1809
Luiz Capitulino222f1802006-03-20 22:16:13 -08001810 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001811 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001812 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001813 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001814 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001816 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto out;
1818 }
1819
Luiz Capitulino222f1802006-03-20 22:16:13 -08001820 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001821 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001822 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto out;
1824 }
1825
1826 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001827out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return ret;
1829}
1830
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001831static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001833 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Arjan van de Ven9a321442007-02-12 00:55:35 -08001836static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001837 .owner = THIS_MODULE,
1838 .open = pktgen_thread_open,
1839 .read = seq_read,
1840 .llseek = seq_lseek,
1841 .write = pktgen_thread_write,
1842 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001843};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 struct pktgen_thread *t;
1849 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001850 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001852 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001853 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001855 if (remove) {
1856 if_lock(t);
1857 pkt_dev->removal_mark = 1;
1858 t->control |= T_REMDEV;
1859 if_unlock(t);
1860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 break;
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001864 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001867/*
1868 * mark a device for removal
1869 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001870static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001873 const int max_tries = 10, msec_per_try = 125;
1874 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001875
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001876 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001877 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001878
Luiz Capitulino222f1802006-03-20 22:16:13 -08001879 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001880
1881 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001882 if (pkt_dev == NULL)
1883 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001884
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001885 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001886 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1887 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001888 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001889 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001890
1891 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001892 printk(KERN_ERR "pktgen_mark_device: timed out after "
1893 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001894 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001895 break;
1896 }
1897
1898 }
1899
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001900 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001901}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001902
Stephen Hemminger39df2322007-03-04 16:11:51 -08001903static void pktgen_change_name(struct net_device *dev)
1904{
1905 struct pktgen_thread *t;
1906
1907 list_for_each_entry(t, &pktgen_threads, th_list) {
1908 struct pktgen_dev *pkt_dev;
1909
1910 list_for_each_entry(pkt_dev, &t->if_list, list) {
1911 if (pkt_dev->odev != dev)
1912 continue;
1913
1914 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1915
Alexey Dobriyan29753152009-08-28 23:34:43 -07001916 pkt_dev->entry = proc_create_data(dev->name, 0600,
1917 pg_proc_dir,
1918 &pktgen_if_fops,
1919 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001920 if (!pkt_dev->entry)
1921 printk(KERN_ERR "pktgen: can't move proc "
1922 " entry for '%s'\n", dev->name);
1923 break;
1924 }
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Luiz Capitulino222f1802006-03-20 22:16:13 -08001928static int pktgen_device_event(struct notifier_block *unused,
1929 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001931 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001933 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001934 return NOTIFY_DONE;
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 /* It is OK that we do not hold the group lock right now,
1937 * as we run under the RTNL lock.
1938 */
1939
1940 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001941 case NETDEV_CHANGENAME:
1942 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001946 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 return NOTIFY_DONE;
1951}
1952
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001953static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1954 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001955{
1956 char b[IFNAMSIZ+5];
1957 int i = 0;
1958
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001959 for (i = 0; ifname[i] != '@'; i++) {
1960 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001961 break;
1962
1963 b[i] = ifname[i];
1964 }
1965 b[i] = 0;
1966
1967 return dev_get_by_name(&init_net, b);
1968}
1969
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971/* Associate pktgen_dev with a device. */
1972
Stephen Hemminger39df2322007-03-04 16:11:51 -08001973static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001976 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (pkt_dev->odev) {
1980 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001981 pkt_dev->odev = NULL;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Robert Olssone6fce5b2008-08-07 02:23:01 -07001984 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001986 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001987 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001991 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001992 err = -EINVAL;
1993 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001994 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001995 err = -ENETDOWN;
1996 } else {
1997 pkt_dev->odev = odev;
1998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002002 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
2005/* Read pkt_dev from the interface and set up internal pktgen_dev
2006 * structure to have the right information to create/send packets
2007 */
2008static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2009{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002010 int ntxq;
2011
Luiz Capitulino222f1802006-03-20 22:16:13 -08002012 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07002013 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
2014 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002015 sprintf(pkt_dev->result,
2016 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2017 return;
2018 }
2019
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002020 /* make sure that we don't pick a non-existing transmit queue */
2021 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002022
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002023 if (ntxq <= pkt_dev->queue_map_min) {
2024 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07002025 "queue_map_min (zero-based) (%d) exceeds valid range "
2026 "[0 - %d] for (%d) queues on %s, resetting\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002027 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
Eric Dumazet593f63b2009-11-23 01:44:37 +00002028 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002029 pkt_dev->queue_map_min = ntxq - 1;
2030 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002031 if (pkt_dev->queue_map_max >= ntxq) {
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002032 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07002033 "queue_map_max (zero-based) (%d) exceeds valid range "
2034 "[0 - %d] for (%d) queues on %s, resetting\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002035 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
Eric Dumazet593f63b2009-11-23 01:44:37 +00002036 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002037 pkt_dev->queue_map_max = ntxq - 1;
2038 }
2039
Luiz Capitulino222f1802006-03-20 22:16:13 -08002040 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002042 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002043 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Luiz Capitulino222f1802006-03-20 22:16:13 -08002045 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002046 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Luiz Capitulino222f1802006-03-20 22:16:13 -08002048 /* Set up pkt size */
2049 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2050
2051 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002053 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 * gets exported
2055 */
2056
2057#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002058 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct inet6_dev *idev;
2060
Luiz Capitulino222f1802006-03-20 22:16:13 -08002061 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2062 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 set = 1;
2064 break;
2065 }
2066
Luiz Capitulino222f1802006-03-20 22:16:13 -08002067 if (!set) {
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 /*
2070 * Use linklevel address if unconfigured.
2071 *
2072 * use ipv6_get_lladdr if/when it's get exported
2073 */
2074
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002075 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002076 idev = __in6_dev_get(pkt_dev->odev);
2077 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct inet6_ifaddr *ifp;
2079
2080 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002081 for (ifp = idev->addr_list; ifp;
2082 ifp = ifp->if_next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002083 if (ifp->scope == IFA_LINK &&
2084 !(ifp->flags & IFA_F_TENTATIVE)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002085 ipv6_addr_copy(&pkt_dev->
2086 cur_in6_saddr,
2087 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 err = 0;
2089 break;
2090 }
2091 }
2092 read_unlock_bh(&idev->lock);
2093 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002094 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002095 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002096 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2097 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002100 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 pkt_dev->saddr_min = 0;
2102 pkt_dev->saddr_max = 0;
2103 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002104
2105 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002108 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (in_dev) {
2110 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002111 pkt_dev->saddr_min =
2112 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 pkt_dev->saddr_max = pkt_dev->saddr_min;
2114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002117 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2119 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2120 }
2121
2122 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2123 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2124 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002125 /* Initialize current values. */
2126 pkt_dev->cur_dst_mac_offset = 0;
2127 pkt_dev->cur_src_mac_offset = 0;
2128 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2129 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2130 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2131 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 pkt_dev->nflows = 0;
2133}
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002136static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2137{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002138 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002139 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002140 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002141
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002142 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2143 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002144
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002145 remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002146 if (remaining <= 0) {
2147 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002148 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002149 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002150
Stephen Hemmingeref879792009-09-22 19:41:43 +00002151 start_time = ktime_now();
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002152 if (remaining < 100)
2153 udelay(remaining); /* really small just spin */
2154 else {
2155 /* see do_nanosleep */
2156 hrtimer_init_sleeper(&t, current);
2157 do {
2158 set_current_state(TASK_INTERRUPTIBLE);
2159 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2160 if (!hrtimer_active(&t.timer))
2161 t.task = NULL;
2162
2163 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002166 hrtimer_cancel(&t.timer);
2167 } while (t.task && pkt_dev->running && !signal_pending(current));
2168 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002170 end_time = ktime_now();
2171
2172 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2173 pkt_dev->next_tx = ktime_add_ns(end_time, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002176static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2177{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002178 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002179 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2180 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2181 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2182}
2183
Stephen Hemminger648fda72009-08-27 13:55:07 +00002184static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002185{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002186 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002187}
2188
2189static inline int f_pick(struct pktgen_dev *pkt_dev)
2190{
2191 int flow = pkt_dev->curfl;
2192
2193 if (pkt_dev->flags & F_FLOW_SEQ) {
2194 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2195 /* reset time */
2196 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002197 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002198 pkt_dev->curfl += 1;
2199 if (pkt_dev->curfl >= pkt_dev->cflows)
2200 pkt_dev->curfl = 0; /*reset */
2201 }
2202 } else {
2203 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002204 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002205
Robert Olsson1211a642008-08-05 18:44:26 -07002206 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002207 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002208 pkt_dev->flows[flow].flags = 0;
2209 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002210 }
2211
2212 return pkt_dev->curfl;
2213}
2214
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002215
2216#ifdef CONFIG_XFRM
2217/* If there was already an IPSEC SA, we keep it as is, else
2218 * we go look for it ...
2219*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002220#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002221static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002222{
2223 struct xfrm_state *x = pkt_dev->flows[flow].x;
2224 if (!x) {
2225 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002226 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002227 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002228 (xfrm_address_t *)&pkt_dev->cur_saddr,
2229 AF_INET,
2230 pkt_dev->ipsmode,
2231 pkt_dev->ipsproto, 0);
2232 if (x) {
2233 pkt_dev->flows[flow].x = x;
2234 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002235 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002236 }
2237
2238 }
2239}
2240#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002241static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2242{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002243
2244 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2245 pkt_dev->cur_queue_map = smp_processor_id();
2246
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002247 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002248 __u16 t;
2249 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2250 t = random32() %
2251 (pkt_dev->queue_map_max -
2252 pkt_dev->queue_map_min + 1)
2253 + pkt_dev->queue_map_min;
2254 } else {
2255 t = pkt_dev->cur_queue_map + 1;
2256 if (t > pkt_dev->queue_map_max)
2257 t = pkt_dev->queue_map_min;
2258 }
2259 pkt_dev->cur_queue_map = t;
2260 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002261 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 -07002262}
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264/* Increment/randomize headers according to flags and current values
2265 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2266 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002267static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2268{
2269 __u32 imn;
2270 __u32 imx;
2271 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002273 if (pkt_dev->cflows)
2274 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002277 if (pkt_dev->src_mac_count > 1) {
2278 __u32 mc;
2279 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Luiz Capitulino222f1802006-03-20 22:16:13 -08002281 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002282 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002283 else {
2284 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002285 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002286 pkt_dev->src_mac_count)
2287 pkt_dev->cur_src_mac_offset = 0;
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Luiz Capitulino222f1802006-03-20 22:16:13 -08002290 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2291 pkt_dev->hh[11] = tmp;
2292 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2293 pkt_dev->hh[10] = tmp;
2294 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2295 pkt_dev->hh[9] = tmp;
2296 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2297 pkt_dev->hh[8] = tmp;
2298 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2299 pkt_dev->hh[7] = tmp;
2300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Luiz Capitulino222f1802006-03-20 22:16:13 -08002302 /* Deal with Destination MAC */
2303 if (pkt_dev->dst_mac_count > 1) {
2304 __u32 mc;
2305 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002308 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Luiz Capitulino222f1802006-03-20 22:16:13 -08002310 else {
2311 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002312 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002313 pkt_dev->dst_mac_count) {
2314 pkt_dev->cur_dst_mac_offset = 0;
2315 }
2316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Luiz Capitulino222f1802006-03-20 22:16:13 -08002318 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2319 pkt_dev->hh[5] = tmp;
2320 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2321 pkt_dev->hh[4] = tmp;
2322 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2323 pkt_dev->hh[3] = tmp;
2324 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2325 pkt_dev->hh[2] = tmp;
2326 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2327 pkt_dev->hh[1] = tmp;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002330 if (pkt_dev->flags & F_MPLS_RND) {
2331 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002332 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002333 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2334 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002335 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002336 htonl(0x000fffff));
2337 }
2338
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002339 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002340 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002341 }
2342
2343 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002344 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002345 }
2346
Luiz Capitulino222f1802006-03-20 22:16:13 -08002347 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2348 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002349 pkt_dev->cur_udp_src = random32() %
2350 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2351 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Luiz Capitulino222f1802006-03-20 22:16:13 -08002353 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 pkt_dev->cur_udp_src++;
2355 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2356 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002357 }
2358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Luiz Capitulino222f1802006-03-20 22:16:13 -08002360 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2361 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002362 pkt_dev->cur_udp_dst = random32() %
2363 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2364 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002365 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002367 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002369 }
2370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 if (!(pkt_dev->flags & F_IPV6)) {
2373
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002374 imn = ntohl(pkt_dev->saddr_min);
2375 imx = ntohl(pkt_dev->saddr_max);
2376 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002378 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002379 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 else {
2381 t = ntohl(pkt_dev->cur_saddr);
2382 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002383 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387 pkt_dev->cur_saddr = htonl(t);
2388 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002389
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002390 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2392 } else {
Al Viro252e3342006-11-14 20:48:11 -08002393 imn = ntohl(pkt_dev->daddr_min);
2394 imx = ntohl(pkt_dev->daddr_max);
2395 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002397 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (pkt_dev->flags & F_IPDST_RND) {
2399
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002400 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002401 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Joe Perches21cf2252007-12-16 13:44:00 -08002403 while (ipv4_is_loopback(s) ||
2404 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002405 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002406 ipv4_is_zeronet(s) ||
2407 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002408 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002409 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
Al Viro252e3342006-11-14 20:48:11 -08002411 pkt_dev->cur_daddr = s;
2412 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 t = ntohl(pkt_dev->cur_daddr);
2414 t++;
2415 if (t > imx) {
2416 t = imn;
2417 }
2418 pkt_dev->cur_daddr = htonl(t);
2419 }
2420 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002421 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002422 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002423 pkt_dev->flows[flow].cur_daddr =
2424 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002425#ifdef CONFIG_XFRM
2426 if (pkt_dev->flags & F_IPSEC_ON)
2427 get_ipsec_sa(pkt_dev, flow);
2428#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 pkt_dev->nflows++;
2430 }
2431 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002432 } else { /* IPV6 * */
2433
2434 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2435 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2436 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2437 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 else {
2439 int i;
2440
2441 /* Only random destinations yet */
2442
Luiz Capitulino222f1802006-03-20 22:16:13 -08002443 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002445 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002446 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2447 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451
Luiz Capitulino222f1802006-03-20 22:16:13 -08002452 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2453 __u32 t;
2454 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002455 t = random32() %
2456 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2457 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002458 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002460 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002462 }
2463 pkt_dev->cur_pkt_size = t;
2464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002466 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 pkt_dev->flows[flow].count++;
2469}
2470
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002471
2472#ifdef CONFIG_XFRM
2473static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2474{
2475 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2476 int err = 0;
2477 struct iphdr *iph;
2478
2479 if (!x)
2480 return 0;
2481 /* XXX: we dont support tunnel mode for now until
2482 * we resolve the dst issue */
2483 if (x->props.mode != XFRM_MODE_TRANSPORT)
2484 return 0;
2485
2486 spin_lock(&x->lock);
2487 iph = ip_hdr(skb);
2488
Herbert Xu13996372007-10-17 21:35:51 -07002489 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002490 if (err)
2491 goto error;
2492 err = x->type->output(x, skb);
2493 if (err)
2494 goto error;
2495
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002496 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002497 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002498error:
2499 spin_unlock(&x->lock);
2500 return err;
2501}
2502
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002503static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002504{
2505 if (pkt_dev->cflows) {
2506 /* let go of the SAs if we have them */
2507 int i = 0;
Florian Westphal5b5f7922009-05-21 15:07:12 -07002508 for (; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002509 struct xfrm_state *x = pkt_dev->flows[i].x;
2510 if (x) {
2511 xfrm_state_put(x);
2512 pkt_dev->flows[i].x = NULL;
2513 }
2514 }
2515 }
2516}
2517
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002518static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002519 struct sk_buff *skb, __be16 protocol)
2520{
2521 if (pkt_dev->flags & F_IPSEC_ON) {
2522 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2523 int nhead = 0;
2524 if (x) {
2525 int ret;
2526 __u8 *eth;
2527 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002528 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002529 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2530 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002531 printk(KERN_ERR "Error expanding "
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002532 "ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002533 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002534 }
2535 }
2536
2537 /* ipsec is not expecting ll header */
2538 skb_pull(skb, ETH_HLEN);
2539 ret = pktgen_output_ipsec(skb, pkt_dev);
2540 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002541 printk(KERN_ERR "Error creating ipsec "
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002542 "packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002543 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002544 }
2545 /* restore ll */
2546 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2547 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002548 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002549 }
2550 }
2551 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002552err:
2553 kfree_skb(skb);
2554 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002555}
2556#endif
2557
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002558static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2559{
2560 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002561 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002562 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002563
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002564 mpls--;
2565 *mpls |= MPLS_STACK_BOTTOM;
2566}
2567
Al Viro0f37c602006-11-03 03:49:56 -08002568static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2569 unsigned int prio)
2570{
2571 return htons(id | (cfi << 12) | (prio << 13));
2572}
2573
Luiz Capitulino222f1802006-03-20 22:16:13 -08002574static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2575 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
2577 struct sk_buff *skb = NULL;
2578 __u8 *eth;
2579 struct udphdr *udph;
2580 int datalen, iplen;
2581 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002582 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002583 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002584 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002585 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2586 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2587 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2588 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002589 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002590
2591 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002592 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002593
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002594 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002595 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002596
Robert Olsson64053be2005-06-26 15:27:10 -07002597 /* Update any of the values, used when we're incrementing various
2598 * fields.
2599 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002600 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002601 mod_cur_headers(pkt_dev);
2602
David S. Miller7ac54592006-01-18 14:19:10 -08002603 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002604
2605 if (pkt_dev->flags & F_NODE) {
2606 int node;
2607
2608 if (pkt_dev->node >= 0)
2609 node = pkt_dev->node;
2610 else
2611 node = numa_node_id();
2612
2613 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2614 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2615 if (likely(skb)) {
2616 skb_reserve(skb, NET_SKB_PAD);
2617 skb->dev = odev;
2618 }
2619 }
2620 else
2621 skb = __netdev_alloc_skb(odev,
2622 pkt_dev->cur_pkt_size + 64
2623 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (!skb) {
2626 sprintf(pkt_dev->result, "No memory");
2627 return NULL;
2628 }
2629
David S. Miller7ac54592006-01-18 14:19:10 -08002630 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 /* Reserve for ethernet and IP header */
2633 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002634 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2635 if (pkt_dev->nr_labels)
2636 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002637
2638 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002639 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002640 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002641 *svlan_tci = build_tci(pkt_dev->svlan_id,
2642 pkt_dev->svlan_cfi,
2643 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002644 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002645 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002646 }
2647 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002648 *vlan_tci = build_tci(pkt_dev->vlan_id,
2649 pkt_dev->vlan_cfi,
2650 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002651 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002652 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002653 }
2654
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002655 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002656 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002657 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002658 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002659 iph = ip_hdr(skb);
2660 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002663 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002665 /* Eth + IPh + UDPh + mpls */
2666 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002667 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002668 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 udph->source = htons(pkt_dev->cur_udp_src);
2672 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002673 udph->len = htons(datalen + 8); /* DATA + udphdr */
2674 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 iph->ihl = 5;
2677 iph->version = 4;
2678 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002679 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002680 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 iph->saddr = pkt_dev->cur_saddr;
2682 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002683 iph->id = htons(pkt_dev->ip_id);
2684 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 iph->frag_off = 0;
2686 iplen = 20 + 8 + datalen;
2687 iph->tot_len = htons(iplen);
2688 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002689 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002690 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002691 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002692 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 skb->dev = odev;
2694 skb->pkt_type = PACKET_HOST;
2695
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002696 if (pkt_dev->nfrags <= 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002697 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002698 memset(pgh + 1, 0, datalen - sizeof(struct pktgen_hdr));
2699 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 int frags = pkt_dev->nfrags;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002701 int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Luiz Capitulino222f1802006-03-20 22:16:13 -08002703 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (frags > MAX_SKB_FRAGS)
2706 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002707 if (datalen > frags * PAGE_SIZE) {
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002708 len = datalen - frags * PAGE_SIZE;
2709 memset(skb_put(skb, len), 0, len);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002710 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
2712
2713 i = 0;
2714 while (datalen > 0) {
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002715 struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 skb_shinfo(skb)->frags[i].page = page;
2717 skb_shinfo(skb)->frags[i].page_offset = 0;
2718 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002719 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 datalen -= skb_shinfo(skb)->frags[i].size;
2721 skb->len += skb_shinfo(skb)->frags[i].size;
2722 skb->data_len += skb_shinfo(skb)->frags[i].size;
2723 i++;
2724 skb_shinfo(skb)->nr_frags = i;
2725 }
2726
2727 while (i < frags) {
2728 int rem;
2729
2730 if (i == 0)
2731 break;
2732
2733 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2734 if (rem == 0)
2735 break;
2736
2737 skb_shinfo(skb)->frags[i - 1].size -= rem;
2738
Luiz Capitulino222f1802006-03-20 22:16:13 -08002739 skb_shinfo(skb)->frags[i] =
2740 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002742 skb_shinfo(skb)->frags[i].page =
2743 skb_shinfo(skb)->frags[i - 1].page;
2744 skb_shinfo(skb)->frags[i].page_offset +=
2745 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 skb_shinfo(skb)->frags[i].size = rem;
2747 i++;
2748 skb_shinfo(skb)->nr_frags = i;
2749 }
2750 }
2751
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002752 /* Stamp the time, and sequence number,
2753 * convert them to network byte order
2754 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002755 if (pgh) {
2756 struct timeval timestamp;
2757
2758 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2759 pgh->seq_num = htonl(pkt_dev->seq_num);
2760
2761 do_gettimeofday(&timestamp);
2762 pgh->tv_sec = htonl(timestamp.tv_sec);
2763 pgh->tv_usec = htonl(timestamp.tv_usec);
2764 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002765
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002766#ifdef CONFIG_XFRM
2767 if (!process_ipsec(pkt_dev, skb, protocol))
2768 return NULL;
2769#endif
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return skb;
2772}
2773
2774/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002775 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2777 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002778 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 * Should be candidate for net/ipv4/utils.c
2780 * --ro
2781 */
2782
Luiz Capitulino222f1802006-03-20 22:16:13 -08002783static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
2785 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002786 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 unsigned long u;
2788 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002789 unsigned int prefixlen = 0;
2790 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002791 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002792 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Luiz Capitulino222f1802006-03-20 22:16:13 -08002794 for (i = 0; i < 16; i++)
2795 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 for (;;) {
2798 if (*s == ':') {
2799 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002800 if (s[1] == ':') { /* Found "::", skip to part 2 */
2801 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 len++;
2803 break;
2804 }
2805 s++;
2806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002808 u = simple_strtoul(s, &pos, 16);
2809 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002810 if (!i)
2811 return 0;
2812 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 /* the last 4 bytes may be written as IPv4 address */
2815
2816 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002817 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2818 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820 ip[prefixlen++] = (u >> 8);
2821 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002822 s += i;
2823 len += i;
2824 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return len;
2826 }
2827
2828/* part 2, after "::" */
2829 for (;;) {
2830 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002831 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 break;
2833 s++;
2834 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002835 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002837
2838 u = simple_strtol(s, &pos, 16);
2839 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002841 if (*s)
2842 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 break;
2844 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002845 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002847 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2848 sizeof(tmp));
2849 suffixlen += 4;
2850 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 break;
2852 }
2853 suffix[suffixlen++] = (u >> 8);
2854 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002855 s += i;
2856 len += i;
2857 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 break;
2859 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002860 for (i = 0; i < suffixlen; i++)
2861 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return len;
2863}
2864
Luiz Capitulino222f1802006-03-20 22:16:13 -08002865static char tohex(char hexdigit)
2866{
2867 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
Luiz Capitulino222f1802006-03-20 22:16:13 -08002870static int fmt_xlong(char *s, unsigned int i)
2871{
2872 char *bak = s;
2873 *s = tohex((i >> 12) & 0xf);
2874 if (s != bak || *s != '0')
2875 ++s;
2876 *s = tohex((i >> 8) & 0xf);
2877 if (s != bak || *s != '0')
2878 ++s;
2879 *s = tohex((i >> 4) & 0xf);
2880 if (s != bak || *s != '0')
2881 ++s;
2882 *s = tohex(i & 0xf);
2883 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
Luiz Capitulino222f1802006-03-20 22:16:13 -08002886static unsigned int fmt_ip6(char *s, const char ip[16])
2887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 unsigned int len;
2889 unsigned int i;
2890 unsigned int temp;
2891 unsigned int compressing;
2892 int j;
2893
Luiz Capitulino222f1802006-03-20 22:16:13 -08002894 len = 0;
2895 compressing = 0;
2896 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002899 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2900 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2901 temp = strlen(s);
2902 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
2904#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002905 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2906 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (temp == 0) {
2908 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002909 compressing = 1;
2910 if (j == 0) {
2911 *s++ = ':';
2912 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914 }
2915 } else {
2916 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002917 compressing = 0;
2918 *s++ = ':';
2919 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002921 i = fmt_xlong(s, temp);
2922 len += i;
2923 s += i;
2924 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 *s++ = ':';
2926 ++len;
2927 }
2928 }
2929 }
2930 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002931 *s++ = ':';
2932 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002934 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return len;
2936}
2937
Luiz Capitulino222f1802006-03-20 22:16:13 -08002938static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2939 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 struct sk_buff *skb = NULL;
2942 __u8 *eth;
2943 struct udphdr *udph;
2944 int datalen;
2945 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002946 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002947 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002948 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002949 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2950 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2951 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2952 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002953 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002954
2955 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002956 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002957
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002958 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002959 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002960
Robert Olsson64053be2005-06-26 15:27:10 -07002961 /* Update any of the values, used when we're incrementing various
2962 * fields.
2963 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002964 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002965 mod_cur_headers(pkt_dev);
2966
Stephen Hemmingere4707572009-08-27 13:55:13 +00002967 skb = __netdev_alloc_skb(odev,
2968 pkt_dev->cur_pkt_size + 64
2969 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (!skb) {
2971 sprintf(pkt_dev->result, "No memory");
2972 return NULL;
2973 }
2974
2975 skb_reserve(skb, 16);
2976
2977 /* Reserve for ethernet and IP header */
2978 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002979 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2980 if (pkt_dev->nr_labels)
2981 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002982
2983 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002984 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002985 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002986 *svlan_tci = build_tci(pkt_dev->svlan_id,
2987 pkt_dev->svlan_cfi,
2988 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002989 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002990 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002991 }
2992 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002993 *vlan_tci = build_tci(pkt_dev->vlan_id,
2994 pkt_dev->vlan_cfi,
2995 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002996 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002997 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002998 }
2999
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003000 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003001 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003002 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003003 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003004 iph = ipv6_hdr(skb);
3005 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003008 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07003009
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003010 /* Eth + IPh + UDPh + mpls */
3011 datalen = pkt_dev->cur_pkt_size - 14 -
3012 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003013 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Luiz Capitulino222f1802006-03-20 22:16:13 -08003015 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 datalen = sizeof(struct pktgen_hdr);
3017 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08003018 printk(KERN_INFO "pktgen: increased datalen to %d\n",
3019 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021
3022 udph->source = htons(pkt_dev->cur_udp_src);
3023 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003024 udph->len = htons(datalen + sizeof(struct udphdr));
3025 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003027 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003029 if (pkt_dev->traffic_class) {
3030 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08003031 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003032 }
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 iph->hop_limit = 32;
3035
3036 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3037 iph->nexthdr = IPPROTO_UDP;
3038
3039 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3040 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3041
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003042 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003043 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003044 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 skb->dev = odev;
3046 skb->pkt_type = PACKET_HOST;
3047
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048 if (pkt_dev->nfrags <= 0)
3049 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 else {
3051 int frags = pkt_dev->nfrags;
3052 int i;
3053
Luiz Capitulino222f1802006-03-20 22:16:13 -08003054 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if (frags > MAX_SKB_FRAGS)
3057 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003058 if (datalen > frags * PAGE_SIZE) {
3059 skb_put(skb, datalen - frags * PAGE_SIZE);
3060 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062
3063 i = 0;
3064 while (datalen > 0) {
3065 struct page *page = alloc_pages(GFP_KERNEL, 0);
3066 skb_shinfo(skb)->frags[i].page = page;
3067 skb_shinfo(skb)->frags[i].page_offset = 0;
3068 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08003069 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 datalen -= skb_shinfo(skb)->frags[i].size;
3071 skb->len += skb_shinfo(skb)->frags[i].size;
3072 skb->data_len += skb_shinfo(skb)->frags[i].size;
3073 i++;
3074 skb_shinfo(skb)->nr_frags = i;
3075 }
3076
3077 while (i < frags) {
3078 int rem;
3079
3080 if (i == 0)
3081 break;
3082
3083 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3084 if (rem == 0)
3085 break;
3086
3087 skb_shinfo(skb)->frags[i - 1].size -= rem;
3088
Luiz Capitulino222f1802006-03-20 22:16:13 -08003089 skb_shinfo(skb)->frags[i] =
3090 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003092 skb_shinfo(skb)->frags[i].page =
3093 skb_shinfo(skb)->frags[i - 1].page;
3094 skb_shinfo(skb)->frags[i].page_offset +=
3095 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 skb_shinfo(skb)->frags[i].size = rem;
3097 i++;
3098 skb_shinfo(skb)->nr_frags = i;
3099 }
3100 }
3101
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003102 /* Stamp the time, and sequence number,
3103 * convert them to network byte order
3104 * should we update cloned packets too ?
3105 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003106 if (pgh) {
3107 struct timeval timestamp;
3108
3109 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3110 pgh->seq_num = htonl(pkt_dev->seq_num);
3111
3112 do_gettimeofday(&timestamp);
3113 pgh->tv_sec = htonl(timestamp.tv_sec);
3114 pgh->tv_usec = htonl(timestamp.tv_usec);
3115 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003116 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return skb;
3119}
3120
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003121static struct sk_buff *fill_packet(struct net_device *odev,
3122 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003124 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return fill_packet_ipv6(odev, pkt_dev);
3126 else
3127 return fill_packet_ipv4(odev, pkt_dev);
3128}
3129
Luiz Capitulino222f1802006-03-20 22:16:13 -08003130static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003132 pkt_dev->seq_num = 1;
3133 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003135 pkt_dev->tx_bytes = 0;
3136 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
3139/* Set up structure for sending pkts, clear counters */
3140
3141static void pktgen_run(struct pktgen_thread *t)
3142{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003143 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 int started = 0;
3145
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003146 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003149 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
3151 /*
3152 * setup odev and create initial packet.
3153 */
3154 pktgen_setup_inject(pkt_dev);
3155
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003160 pkt_dev->started_at =
3161 pkt_dev->next_tx = ktime_now();
3162
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003163 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 strcpy(pkt_dev->result, "Starting");
3166 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003167 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 strcpy(pkt_dev->result, "Error starting");
3169 }
3170 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171 if (started)
3172 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173}
3174
3175static void pktgen_stop_all_threads_ifs(void)
3176{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003177 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003179 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003181 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003182
3183 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003184 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003185
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003186 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
Stephen Hemminger648fda72009-08-27 13:55:07 +00003189static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003191 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003193 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003194 if (pkt_dev->running)
3195 return 1;
3196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Luiz Capitulino222f1802006-03-20 22:16:13 -08003199static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003201 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Luiz Capitulino222f1802006-03-20 22:16:13 -08003203 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Luiz Capitulino222f1802006-03-20 22:16:13 -08003207 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Luiz Capitulino222f1802006-03-20 22:16:13 -08003209 if (signal_pending(current))
3210 goto signal;
3211 if_lock(t);
3212 }
3213 if_unlock(t);
3214 return 1;
3215signal:
3216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
3219static int pktgen_wait_all_threads_run(void)
3220{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003221 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003223
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003224 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003225
3226 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003228 if (sig == 0)
3229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003231
3232 if (sig == 0)
3233 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003235
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003236 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return sig;
3238}
3239
3240static void pktgen_run_all_threads(void)
3241{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003242 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003244 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003246 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003248 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003250
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003251 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003253 /* Propagate thread->control */
3254 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 pktgen_wait_all_threads_run();
3257}
3258
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003259static void pktgen_reset_all_threads(void)
3260{
3261 struct pktgen_thread *t;
3262
3263 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3264
3265 mutex_lock(&pktgen_thread_lock);
3266
3267 list_for_each_entry(t, &pktgen_threads, th_list)
3268 t->control |= (T_REMDEVALL);
3269
3270 mutex_unlock(&pktgen_thread_lock);
3271
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003272 /* Propagate thread->control */
3273 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003274
3275 pktgen_wait_all_threads_run();
3276}
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3279{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003280 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003281 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003282 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3283 pkt_dev->started_at);
3284 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003286 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3287 (unsigned long long)ktime_to_us(elapsed),
3288 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3289 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003290 (unsigned long long)pkt_dev->sofar,
3291 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003293 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3294 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Luiz Capitulino222f1802006-03-20 22:16:13 -08003296 bps = pps * 8 * pkt_dev->cur_pkt_size;
3297
3298 mbps = bps;
3299 do_div(mbps, 1000000);
3300 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3301 (unsigned long long)pps,
3302 (unsigned long long)mbps,
3303 (unsigned long long)bps,
3304 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003308static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003310 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003311
Luiz Capitulino222f1802006-03-20 22:16:13 -08003312 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003313 printk(KERN_WARNING "pktgen: interface: %s is already "
Eric Dumazet593f63b2009-11-23 01:44:37 +00003314 "stopped\n", pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003315 return -EINVAL;
3316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003318 kfree_skb(pkt_dev->skb);
3319 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003320 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003321 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003323 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Luiz Capitulino222f1802006-03-20 22:16:13 -08003325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
Luiz Capitulino222f1802006-03-20 22:16:13 -08003328static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003330 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 if_lock(t);
3333
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003334 list_for_each_entry(pkt_dev, &t->if_list, list) {
3335 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003336 continue;
3337 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003338 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003339 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003340 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
3342 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003343 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
Luiz Capitulino222f1802006-03-20 22:16:13 -08003346static void pktgen_stop(struct pktgen_thread *t)
3347{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003348 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003350 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Luiz Capitulino222f1802006-03-20 22:16:13 -08003352 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003354 list_for_each_entry(pkt_dev, &t->if_list, list) {
3355 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Luiz Capitulino222f1802006-03-20 22:16:13 -08003358 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003361/*
3362 * one of our devices needs to be removed - find it
3363 * and remove it
3364 */
3365static void pktgen_rem_one_if(struct pktgen_thread *t)
3366{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003367 struct list_head *q, *n;
3368 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003369
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003370 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003371
3372 if_lock(t);
3373
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003374 list_for_each_safe(q, n, &t->if_list) {
3375 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003376
Luiz Capitulino222f1802006-03-20 22:16:13 -08003377 if (!cur->removal_mark)
3378 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003379
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003380 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003381 cur->skb = NULL;
3382
3383 pktgen_remove_device(t, cur);
3384
3385 break;
3386 }
3387
3388 if_unlock(t);
3389}
3390
Luiz Capitulino222f1802006-03-20 22:16:13 -08003391static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003393 struct list_head *q, *n;
3394 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003395
3396 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003397
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003398 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003401 list_for_each_safe(q, n, &t->if_list) {
3402 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003403
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003404 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003405 cur->skb = NULL;
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 pktgen_remove_device(t, cur);
3408 }
3409
Luiz Capitulino222f1802006-03-20 22:16:13 -08003410 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
Luiz Capitulino222f1802006-03-20 22:16:13 -08003413static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003415 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
David S. Milleree74baa2007-01-01 20:51:53 -08003417 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003419 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003421 list_del(&t->th_list);
3422
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003423 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
Stephen Hemmingeref879792009-09-22 19:41:43 +00003426static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003427{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003428 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003429 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003430 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003431}
3432
Stephen Hemmingeref879792009-09-22 19:41:43 +00003433static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3434{
3435 ktime_t idle_start = ktime_now();
3436
3437 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3438 if (signal_pending(current))
3439 break;
3440
3441 if (need_resched())
3442 pktgen_resched(pkt_dev);
3443 else
3444 cpu_relax();
3445 }
3446 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3447}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003448
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003449static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
Stephen Hemminger00829822008-11-20 20:14:53 -08003451 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003452 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003453 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003454 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003455 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 int ret;
3457
Stephen Hemmingeref879792009-09-22 19:41:43 +00003458 /* If device is offline, then don't send */
3459 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3460 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003461 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003463
Stephen Hemmingeref879792009-09-22 19:41:43 +00003464 /* This is max DELAY, this has special meaning of
3465 * "never transmit"
3466 */
3467 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3468 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3469 return;
3470 }
3471
3472 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003473 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3474 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3475 /* build a new pkt */
3476 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003477
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003478 pkt_dev->skb = fill_packet(odev, pkt_dev);
3479 if (pkt_dev->skb == NULL) {
3480 printk(KERN_ERR "pktgen: ERROR: couldn't "
3481 "allocate skb in fill_packet.\n");
3482 schedule();
3483 pkt_dev->clone_count--; /* back out increment, OOM */
3484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003486 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003487 pkt_dev->allocated_skbs++;
3488 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003490
Stephen Hemmingeref879792009-09-22 19:41:43 +00003491 if (pkt_dev->delay && pkt_dev->last_ok)
3492 spin(pkt_dev, pkt_dev->next_tx);
3493
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003494 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3495 txq = netdev_get_tx_queue(odev, queue_map);
3496
3497 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003498
Eric Dumazet0835acf2009-09-30 13:03:33 +00003499 if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003500 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003501 pkt_dev->last_ok = 0;
3502 goto unlock;
3503 }
3504 atomic_inc(&(pkt_dev->skb->users));
3505 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Stephen Hemmingeref879792009-09-22 19:41:43 +00003507 switch (ret) {
3508 case NETDEV_TX_OK:
3509 txq_trans_update(txq);
3510 pkt_dev->last_ok = 1;
3511 pkt_dev->sofar++;
3512 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003513 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003514 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003515 case NET_XMIT_DROP:
3516 case NET_XMIT_CN:
3517 case NET_XMIT_POLICED:
3518 /* skb has been consumed */
3519 pkt_dev->errors++;
3520 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003521 default: /* Drivers are not supposed to return other values! */
3522 if (net_ratelimit())
3523 pr_info("pktgen: %s xmit error: %d\n",
Eric Dumazet593f63b2009-11-23 01:44:37 +00003524 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003525 pkt_dev->errors++;
3526 /* fallthru */
3527 case NETDEV_TX_LOCKED:
3528 case NETDEV_TX_BUSY:
3529 /* Retry it next time */
3530 atomic_dec(&(pkt_dev->skb->users));
3531 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003532 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003533unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003534 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 /* If pkt_dev->count is zero, then run forever */
3537 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003538 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 /* Done with this */
3541 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003542 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003545/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 * Main loop of the thread goes here
3547 */
3548
David S. Milleree74baa2007-01-01 20:51:53 -08003549static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
3551 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003552 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003553 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
David S. Milleree74baa2007-01-01 20:51:53 -08003556 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003559 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003561 pr_debug("pktgen: starting pktgen/%d: pid=%d\n",
3562 cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
David S. Milleree74baa2007-01-01 20:51:53 -08003564 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003565
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003566 set_freezable();
3567
David S. Milleree74baa2007-01-01 20:51:53 -08003568 while (!kthread_should_stop()) {
3569 pkt_dev = next_to_run(t);
3570
Stephen Hemmingeref879792009-09-22 19:41:43 +00003571 if (unlikely(!pkt_dev && t->control == 0)) {
3572 wait_event_interruptible_timeout(t->queue,
3573 t->control != 0,
3574 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003575 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003576 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003577 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 __set_current_state(TASK_RUNNING);
3580
Stephen Hemmingeref879792009-09-22 19:41:43 +00003581 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 pktgen_xmit(pkt_dev);
3583
Stephen Hemmingeref879792009-09-22 19:41:43 +00003584 if (need_resched())
3585 pktgen_resched(pkt_dev);
3586 else
3587 cpu_relax();
3588 }
3589
Luiz Capitulino222f1802006-03-20 22:16:13 -08003590 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 pktgen_stop(t);
3592 t->control &= ~(T_STOP);
3593 }
3594
Luiz Capitulino222f1802006-03-20 22:16:13 -08003595 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 pktgen_run(t);
3597 t->control &= ~(T_RUN);
3598 }
3599
Luiz Capitulino222f1802006-03-20 22:16:13 -08003600 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003602 t->control &= ~(T_REMDEVALL);
3603 }
3604
Luiz Capitulino222f1802006-03-20 22:16:13 -08003605 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003606 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 t->control &= ~(T_REMDEV);
3608 }
3609
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003610 try_to_freeze();
3611
David S. Milleree74baa2007-01-01 20:51:53 -08003612 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003615 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003616 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003618 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003619 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003621 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003622 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003623
David S. Milleree74baa2007-01-01 20:51:53 -08003624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
Luiz Capitulino222f1802006-03-20 22:16:13 -08003627static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003628 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003630 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003631 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Eric Dumazet3e984842009-11-24 14:50:53 -08003633 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003634 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003635 if (strncmp(p->odevname, ifname, len) == 0) {
3636 if (p->odevname[len]) {
3637 if (exact || p->odevname[len] != '@')
3638 continue;
3639 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003640 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003641 break;
3642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Luiz Capitulino222f1802006-03-20 22:16:13 -08003644 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003645 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003646 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647}
3648
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003649/*
3650 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 */
3652
Luiz Capitulino222f1802006-03-20 22:16:13 -08003653static int add_dev_to_thread(struct pktgen_thread *t,
3654 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
3656 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Luiz Capitulino222f1802006-03-20 22:16:13 -08003658 if_lock(t);
3659
3660 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003661 printk(KERN_ERR "pktgen: ERROR: already assigned "
3662 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003663 rv = -EBUSY;
3664 goto out;
3665 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003666
3667 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003668 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 pkt_dev->running = 0;
3670
Luiz Capitulino222f1802006-03-20 22:16:13 -08003671out:
3672 if_unlock(t);
3673 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674}
3675
3676/* Called under thread lock */
3677
Luiz Capitulino222f1802006-03-20 22:16:13 -08003678static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003680 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003681 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003682 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 /* We don't allow a device to be on several threads */
3685
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003686 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3687 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003688 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003689 return -EBUSY;
3690 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003691
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003692 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003693 if (!pkt_dev)
3694 return -ENOMEM;
3695
Eric Dumazet593f63b2009-11-23 01:44:37 +00003696 strcpy(pkt_dev->odevname, ifname);
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003697 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3698 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003699 if (pkt_dev->flows == NULL) {
3700 kfree(pkt_dev);
3701 return -ENOMEM;
3702 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003703 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003704
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003705 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003706 pkt_dev->min_pkt_size = ETH_ZLEN;
3707 pkt_dev->max_pkt_size = ETH_ZLEN;
3708 pkt_dev->nfrags = 0;
3709 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003710 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003711 pkt_dev->count = pg_count_d;
3712 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003713 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003714 pkt_dev->udp_src_max = 9;
3715 pkt_dev->udp_dst_min = 9;
3716 pkt_dev->udp_dst_max = 9;
3717
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003718 pkt_dev->vlan_p = 0;
3719 pkt_dev->vlan_cfi = 0;
3720 pkt_dev->vlan_id = 0xffff;
3721 pkt_dev->svlan_p = 0;
3722 pkt_dev->svlan_cfi = 0;
3723 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003724 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003725
Stephen Hemminger39df2322007-03-04 16:11:51 -08003726 err = pktgen_setup_dev(pkt_dev, ifname);
3727 if (err)
3728 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003729
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003730 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3731 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003732 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003733 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003734 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003735 err = -EINVAL;
3736 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003737 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003738#ifdef CONFIG_XFRM
3739 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3740 pkt_dev->ipsproto = IPPROTO_ESP;
3741#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003742
3743 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003744out2:
3745 dev_put(pkt_dev->odev);
3746out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003747#ifdef CONFIG_XFRM
3748 free_SAs(pkt_dev);
3749#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003750 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003751 kfree(pkt_dev);
3752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
David S. Milleree74baa2007-01-01 20:51:53 -08003755static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003757 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003758 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003759 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003761 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3762 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003763 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003764 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3765 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003766 return -ENOMEM;
3767 }
3768
Luiz Capitulino222f1802006-03-20 22:16:13 -08003769 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003771
David S. Milleree74baa2007-01-01 20:51:53 -08003772 INIT_LIST_HEAD(&t->if_list);
3773
3774 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003775 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003776
3777 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3778 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003779 printk(KERN_ERR "pktgen: kernel_thread() failed "
3780 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003781 list_del(&t->th_list);
3782 kfree(t);
3783 return PTR_ERR(p);
3784 }
3785 kthread_bind(p, cpu);
3786 t->tsk = p;
3787
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003788 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3789 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003790 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003791 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003792 PG_PROC_DIR, t->tsk->comm);
3793 kthread_stop(p);
3794 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003795 kfree(t);
3796 return -EINVAL;
3797 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003798
David S. Milleree74baa2007-01-01 20:51:53 -08003799 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003800 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 return 0;
3803}
3804
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003805/*
3806 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003808static void _rem_dev_from_if_list(struct pktgen_thread *t,
3809 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003811 struct list_head *q, *n;
3812 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003814 list_for_each_safe(q, n, &t->if_list) {
3815 p = list_entry(q, struct pktgen_dev, list);
3816 if (p == pkt_dev)
3817 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
3819}
3820
Luiz Capitulino222f1802006-03-20 22:16:13 -08003821static int pktgen_remove_device(struct pktgen_thread *t,
3822 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823{
3824
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003825 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Luiz Capitulino222f1802006-03-20 22:16:13 -08003827 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003828 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3829 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003830 pktgen_stop_device(pkt_dev);
3831 }
3832
3833 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
3835 if (pkt_dev->odev) {
3836 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003837 pkt_dev->odev = NULL;
3838 }
3839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 /* And update the thread if_list */
3841
3842 _rem_dev_from_if_list(t, pkt_dev);
3843
Stephen Hemminger39df2322007-03-04 16:11:51 -08003844 if (pkt_dev->entry)
3845 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003847#ifdef CONFIG_XFRM
3848 free_SAs(pkt_dev);
3849#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003850 vfree(pkt_dev->flows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
Luiz Capitulino222f1802006-03-20 22:16:13 -08003855static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856{
3857 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003858 struct proc_dir_entry *pe;
3859
David S. Miller25a8b252007-07-30 16:11:48 -07003860 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003862 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003863 if (!pg_proc_dir)
3864 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Wang Chen25296d52008-02-28 14:11:49 -08003866 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003867 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003868 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3869 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003870 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003871 return -EINVAL;
3872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 /* Register us to receive netdevice events */
3875 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003876
John Hawkes670c02c2005-10-13 09:30:31 -07003877 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003878 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
David S. Milleree74baa2007-01-01 20:51:53 -08003880 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003881 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003882 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3883 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003884 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003885
3886 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003887 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3888 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003889 unregister_netdevice_notifier(&pktgen_notifier_block);
3890 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003891 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003892 return -ENODEV;
3893 }
3894
Luiz Capitulino222f1802006-03-20 22:16:13 -08003895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896}
3897
3898static void __exit pg_cleanup(void)
3899{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003900 struct pktgen_thread *t;
3901 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 wait_queue_head_t queue;
3903 init_waitqueue_head(&queue);
3904
Luiz Capitulino222f1802006-03-20 22:16:13 -08003905 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003907 list_for_each_safe(q, n, &pktgen_threads) {
3908 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003909 kthread_stop(t->tsk);
3910 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Luiz Capitulino222f1802006-03-20 22:16:13 -08003913 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 unregister_netdevice_notifier(&pktgen_notifier_block);
3915
Luiz Capitulino222f1802006-03-20 22:16:13 -08003916 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003917 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003918 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921module_init(pg_init);
3922module_exit(pg_cleanup);
3923
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003924MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925MODULE_DESCRIPTION("Packet Generator tool");
3926MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003927MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003929MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003931MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003933MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003935MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");