blob: 6428653e9498aee2def19601fed77ddad9d5a1c0 [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
Daniel Turull43d28b62010-06-09 22:49:57 +0000172#define VERSION "2.74"
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 Viro252e33462006-11-14 20:48:11 -0800217 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800218 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700219#ifdef CONFIG_XFRM
220 struct xfrm_state *x;
221#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700222 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700225/* flow flag bits */
226#define F_INIT (1<<0) /* flow has been initialized */
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /*
230 * Try to keep frequent/infrequent used vars. separated.
231 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800232 struct proc_dir_entry *entry; /* proc file */
233 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000234 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000236 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800237
238 /* If min != max, then we will either do a linear iteration, or
239 * we will do a random selection from within the range.
240 */
241 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800242 int removal_mark; /* non-zero => the device is marked for
243 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Luiz Capitulino222f1802006-03-20 22:16:13 -0800245 int min_pkt_size; /* = ETH_ZLEN; */
246 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700247 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800248 int nfrags;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000249 u64 delay; /* nano-seconds */
250
Luiz Capitulino222f1802006-03-20 22:16:13 -0800251 __u64 count; /* Default No packets to send */
252 __u64 sofar; /* How many pkts we've sent so far */
253 __u64 tx_bytes; /* How many bytes we've transmitted */
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 Viro252e33462006-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 Viro252e33462006-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 Viro252e33462006-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 Capitulinoc26a80162006-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 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000983 if (!strcmp(name, "rate")) {
984 len = num_arg(&user_buffer[i], 10, &value);
985 if (len < 0)
986 return len;
987
988 i += len;
989 if (!value)
990 return len;
991 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
992 if (debug)
993 printk(KERN_INFO
994 "pktgen: Delay set at: %llu ns\n",
995 pkt_dev->delay);
996
997 sprintf(pg_result, "OK: rate=%lu", value);
998 return count;
999 }
1000 if (!strcmp(name, "ratep")) {
1001 len = num_arg(&user_buffer[i], 10, &value);
1002 if (len < 0)
1003 return len;
1004
1005 i += len;
1006 if (!value)
1007 return len;
1008 pkt_dev->delay = NSEC_PER_SEC/value;
1009 if (debug)
1010 printk(KERN_INFO
1011 "pktgen: Delay set at: %llu ns\n",
1012 pkt_dev->delay);
1013
1014 sprintf(pg_result, "OK: rate=%lu", value);
1015 return count;
1016 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001017 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001019 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001020 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001023 if (value != pkt_dev->udp_src_min) {
1024 pkt_dev->udp_src_min = value;
1025 pkt_dev->cur_udp_src = value;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1028 return count;
1029 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001030 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001032 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001033 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001036 if (value != pkt_dev->udp_dst_min) {
1037 pkt_dev->udp_dst_min = value;
1038 pkt_dev->cur_udp_dst = value;
1039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1041 return count;
1042 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001043 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001045 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001046 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001049 if (value != pkt_dev->udp_src_max) {
1050 pkt_dev->udp_src_max = value;
1051 pkt_dev->cur_udp_src = value;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1054 return count;
1055 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001056 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001058 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001059 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001062 if (value != pkt_dev->udp_dst_max) {
1063 pkt_dev->udp_dst_max = value;
1064 pkt_dev->cur_udp_dst = value;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1067 return count;
1068 }
1069 if (!strcmp(name, "clone_skb")) {
1070 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001071 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001072 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001075 pkt_dev->clone_skb = value;
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1078 return count;
1079 }
1080 if (!strcmp(name, "count")) {
1081 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001082 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 i += len;
1086 pkt_dev->count = value;
1087 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001088 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return count;
1090 }
1091 if (!strcmp(name, "src_mac_count")) {
1092 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001093 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001094 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 i += len;
1097 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001098 pkt_dev->src_mac_count = value;
1099 pkt_dev->cur_src_mac_offset = 0;
1100 }
1101 sprintf(pg_result, "OK: src_mac_count=%d",
1102 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return count;
1104 }
1105 if (!strcmp(name, "dst_mac_count")) {
1106 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001107 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001108 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 i += len;
1111 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001112 pkt_dev->dst_mac_count = value;
1113 pkt_dev->cur_dst_mac_offset = 0;
1114 }
1115 sprintf(pg_result, "OK: dst_mac_count=%d",
1116 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return count;
1118 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001119 if (!strcmp(name, "node")) {
1120 len = num_arg(&user_buffer[i], 10, &value);
1121 if (len < 0)
1122 return len;
1123
1124 i += len;
1125
1126 if (node_possible(value)) {
1127 pkt_dev->node = value;
1128 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1129 }
1130 else
1131 sprintf(pg_result, "ERROR: node not possible");
1132 return count;
1133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001135 char f[32];
1136 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001138 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001139 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (copy_from_user(f, &user_buffer[i], len))
1142 return -EFAULT;
1143 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001144 if (strcmp(f, "IPSRC_RND") == 0)
1145 pkt_dev->flags |= F_IPSRC_RND;
1146
1147 else if (strcmp(f, "!IPSRC_RND") == 0)
1148 pkt_dev->flags &= ~F_IPSRC_RND;
1149
1150 else if (strcmp(f, "TXSIZE_RND") == 0)
1151 pkt_dev->flags |= F_TXSIZE_RND;
1152
1153 else if (strcmp(f, "!TXSIZE_RND") == 0)
1154 pkt_dev->flags &= ~F_TXSIZE_RND;
1155
1156 else if (strcmp(f, "IPDST_RND") == 0)
1157 pkt_dev->flags |= F_IPDST_RND;
1158
1159 else if (strcmp(f, "!IPDST_RND") == 0)
1160 pkt_dev->flags &= ~F_IPDST_RND;
1161
1162 else if (strcmp(f, "UDPSRC_RND") == 0)
1163 pkt_dev->flags |= F_UDPSRC_RND;
1164
1165 else if (strcmp(f, "!UDPSRC_RND") == 0)
1166 pkt_dev->flags &= ~F_UDPSRC_RND;
1167
1168 else if (strcmp(f, "UDPDST_RND") == 0)
1169 pkt_dev->flags |= F_UDPDST_RND;
1170
1171 else if (strcmp(f, "!UDPDST_RND") == 0)
1172 pkt_dev->flags &= ~F_UDPDST_RND;
1173
1174 else if (strcmp(f, "MACSRC_RND") == 0)
1175 pkt_dev->flags |= F_MACSRC_RND;
1176
1177 else if (strcmp(f, "!MACSRC_RND") == 0)
1178 pkt_dev->flags &= ~F_MACSRC_RND;
1179
1180 else if (strcmp(f, "MACDST_RND") == 0)
1181 pkt_dev->flags |= F_MACDST_RND;
1182
1183 else if (strcmp(f, "!MACDST_RND") == 0)
1184 pkt_dev->flags &= ~F_MACDST_RND;
1185
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001186 else if (strcmp(f, "MPLS_RND") == 0)
1187 pkt_dev->flags |= F_MPLS_RND;
1188
1189 else if (strcmp(f, "!MPLS_RND") == 0)
1190 pkt_dev->flags &= ~F_MPLS_RND;
1191
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001192 else if (strcmp(f, "VID_RND") == 0)
1193 pkt_dev->flags |= F_VID_RND;
1194
1195 else if (strcmp(f, "!VID_RND") == 0)
1196 pkt_dev->flags &= ~F_VID_RND;
1197
1198 else if (strcmp(f, "SVID_RND") == 0)
1199 pkt_dev->flags |= F_SVID_RND;
1200
1201 else if (strcmp(f, "!SVID_RND") == 0)
1202 pkt_dev->flags &= ~F_SVID_RND;
1203
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001204 else if (strcmp(f, "FLOW_SEQ") == 0)
1205 pkt_dev->flags |= F_FLOW_SEQ;
1206
Robert Olsson45b270f2007-08-28 15:45:55 -07001207 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1208 pkt_dev->flags |= F_QUEUE_MAP_RND;
1209
1210 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1211 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001212
1213 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1214 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1215
1216 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1217 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001218#ifdef CONFIG_XFRM
1219 else if (strcmp(f, "IPSEC") == 0)
1220 pkt_dev->flags |= F_IPSEC_ON;
1221#endif
1222
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001223 else if (strcmp(f, "!IPV6") == 0)
1224 pkt_dev->flags &= ~F_IPV6;
1225
Robert Olssone99b99b2010-03-18 22:44:30 +00001226 else if (strcmp(f, "NODE_ALLOC") == 0)
1227 pkt_dev->flags |= F_NODE;
1228
1229 else if (strcmp(f, "!NODE_ALLOC") == 0)
1230 pkt_dev->flags &= ~F_NODE;
1231
Luiz Capitulino222f1802006-03-20 22:16:13 -08001232 else {
1233 sprintf(pg_result,
1234 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1235 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001236 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001237 "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 -08001238 return count;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1241 return count;
1242 }
1243 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1244 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001245 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001246 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Luiz Capitulino222f1802006-03-20 22:16:13 -08001248 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001250 buf[len] = 0;
1251 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1252 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1253 strncpy(pkt_dev->dst_min, buf, len);
1254 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1255 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1256 }
1257 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001258 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 pkt_dev->dst_min);
1260 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1262 return count;
1263 }
1264 if (!strcmp(name, "dst_max")) {
1265 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001266 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001267 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Luiz Capitulino222f1802006-03-20 22:16:13 -08001270 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return -EFAULT;
1272
Luiz Capitulino222f1802006-03-20 22:16:13 -08001273 buf[len] = 0;
1274 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1275 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1276 strncpy(pkt_dev->dst_max, buf, len);
1277 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1278 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1279 }
1280 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001281 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001282 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 i += len;
1284 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1285 return count;
1286 }
1287 if (!strcmp(name, "dst6")) {
1288 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001289 if (len < 0)
1290 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 pkt_dev->flags |= F_IPV6;
1293
Luiz Capitulino222f1802006-03-20 22:16:13 -08001294 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001296 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001299 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1302
Luiz Capitulino222f1802006-03-20 22:16:13 -08001303 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001304 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 sprintf(pg_result, "OK: dst6=%s", buf);
1308 return count;
1309 }
1310 if (!strcmp(name, "dst6_min")) {
1311 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001312 if (len < 0)
1313 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 pkt_dev->flags |= F_IPV6;
1316
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Luiz Capitulino222f1802006-03-20 22:16:13 -08001324 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1325 &pkt_dev->min_in6_daddr);
1326 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001327 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 sprintf(pg_result, "OK: dst6_min=%s", buf);
1331 return count;
1332 }
1333 if (!strcmp(name, "dst6_max")) {
1334 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335 if (len < 0)
1336 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 pkt_dev->flags |= F_IPV6;
1339
Luiz Capitulino222f1802006-03-20 22:16:13 -08001340 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001342 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001345 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Luiz Capitulino222f1802006-03-20 22:16:13 -08001347 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001348 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Luiz Capitulino222f1802006-03-20 22:16:13 -08001350 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 sprintf(pg_result, "OK: dst6_max=%s", buf);
1352 return count;
1353 }
1354 if (!strcmp(name, "src6")) {
1355 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001356 if (len < 0)
1357 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 pkt_dev->flags |= F_IPV6;
1360
Luiz Capitulino222f1802006-03-20 22:16:13 -08001361 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001363 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001366 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1369
Luiz Capitulino222f1802006-03-20 22:16:13 -08001370 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001371 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001372
1373 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 sprintf(pg_result, "OK: src6=%s", buf);
1375 return count;
1376 }
1377 if (!strcmp(name, "src_min")) {
1378 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001379 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001380 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001381
Luiz Capitulino222f1802006-03-20 22:16:13 -08001382 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001384 buf[len] = 0;
1385 if (strcmp(buf, pkt_dev->src_min) != 0) {
1386 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1387 strncpy(pkt_dev->src_min, buf, len);
1388 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1389 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1390 }
1391 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001392 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 i += len;
1395 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1396 return count;
1397 }
1398 if (!strcmp(name, "src_max")) {
1399 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001400 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001401 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001402
Luiz Capitulino222f1802006-03-20 22:16:13 -08001403 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001405 buf[len] = 0;
1406 if (strcmp(buf, pkt_dev->src_max) != 0) {
1407 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1408 strncpy(pkt_dev->src_max, buf, len);
1409 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1410 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1411 }
1412 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001413 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001414 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 i += len;
1416 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1417 return count;
1418 }
1419 if (!strcmp(name, "dst_mac")) {
1420 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001421 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001423 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001426 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001430 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return -EFAULT;
1432 i += len;
1433
Luiz Capitulino222f1802006-03-20 22:16:13 -08001434 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (*v >= '0' && *v <= '9') {
1436 *m *= 16;
1437 *m += *v - '0';
1438 }
1439 if (*v >= 'A' && *v <= 'F') {
1440 *m *= 16;
1441 *m += *v - 'A' + 10;
1442 }
1443 if (*v >= 'a' && *v <= 'f') {
1444 *m *= 16;
1445 *m += *v - 'a' + 10;
1446 }
1447 if (*v == ':') {
1448 m++;
1449 *m = 0;
1450 }
1451 }
1452
1453 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001454 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1455 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 sprintf(pg_result, "OK: dstmac");
1458 return count;
1459 }
1460 if (!strcmp(name, "src_mac")) {
1461 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001462 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 unsigned char *m = pkt_dev->src_mac;
1464
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001465 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001468 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001469 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001472 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -EFAULT;
1474 i += len;
1475
Luiz Capitulino222f1802006-03-20 22:16:13 -08001476 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (*v >= '0' && *v <= '9') {
1478 *m *= 16;
1479 *m += *v - '0';
1480 }
1481 if (*v >= 'A' && *v <= 'F') {
1482 *m *= 16;
1483 *m += *v - 'A' + 10;
1484 }
1485 if (*v >= 'a' && *v <= 'f') {
1486 *m *= 16;
1487 *m += *v - 'a' + 10;
1488 }
1489 if (*v == ':') {
1490 m++;
1491 *m = 0;
1492 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001495 /* Set up Src MAC */
1496 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1497 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1498
Luiz Capitulino222f1802006-03-20 22:16:13 -08001499 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return count;
1501 }
1502
Luiz Capitulino222f1802006-03-20 22:16:13 -08001503 if (!strcmp(name, "clear_counters")) {
1504 pktgen_clear_counters(pkt_dev);
1505 sprintf(pg_result, "OK: Clearing counters.\n");
1506 return count;
1507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 if (!strcmp(name, "flows")) {
1510 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001511 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001512 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 i += len;
1515 if (value > MAX_CFLOWS)
1516 value = MAX_CFLOWS;
1517
1518 pkt_dev->cflows = value;
1519 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1520 return count;
1521 }
1522
1523 if (!strcmp(name, "flowlen")) {
1524 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001525 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001526 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 i += len;
1529 pkt_dev->lflow = value;
1530 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1531 return count;
1532 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001533
Robert Olsson45b270f2007-08-28 15:45:55 -07001534 if (!strcmp(name, "queue_map_min")) {
1535 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001536 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001537 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001538
Robert Olsson45b270f2007-08-28 15:45:55 -07001539 i += len;
1540 pkt_dev->queue_map_min = value;
1541 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1542 return count;
1543 }
1544
1545 if (!strcmp(name, "queue_map_max")) {
1546 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001547 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001548 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001549
Robert Olsson45b270f2007-08-28 15:45:55 -07001550 i += len;
1551 pkt_dev->queue_map_max = value;
1552 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1553 return count;
1554 }
1555
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001556 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001557 unsigned n, cnt;
1558
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001559 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001560 if (len < 0)
1561 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001562 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001563 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001564 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001565 cnt += sprintf(pg_result + cnt,
1566 "%08x%s", ntohl(pkt_dev->labels[n]),
1567 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001568
1569 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1570 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1571 pkt_dev->svlan_id = 0xffff;
1572
1573 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001574 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001575 }
1576 return count;
1577 }
1578
1579 if (!strcmp(name, "vlan_id")) {
1580 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001581 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001582 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001583
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001584 i += len;
1585 if (value <= 4095) {
1586 pkt_dev->vlan_id = value; /* turn on VLAN */
1587
1588 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001589 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001590
1591 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001592 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001593
1594 pkt_dev->nr_labels = 0; /* turn off MPLS */
1595 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1596 } else {
1597 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1598 pkt_dev->svlan_id = 0xffff;
1599
1600 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001601 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001602 }
1603 return count;
1604 }
1605
1606 if (!strcmp(name, "vlan_p")) {
1607 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001608 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001609 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001610
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001611 i += len;
1612 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1613 pkt_dev->vlan_p = value;
1614 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1615 } else {
1616 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1617 }
1618 return count;
1619 }
1620
1621 if (!strcmp(name, "vlan_cfi")) {
1622 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001623 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001624 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001625
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001626 i += len;
1627 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1628 pkt_dev->vlan_cfi = value;
1629 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1630 } else {
1631 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1632 }
1633 return count;
1634 }
1635
1636 if (!strcmp(name, "svlan_id")) {
1637 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001638 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001639 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001640
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001641 i += len;
1642 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1643 pkt_dev->svlan_id = value; /* turn on SVLAN */
1644
1645 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001646 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001647
1648 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001649 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001650
1651 pkt_dev->nr_labels = 0; /* turn off MPLS */
1652 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1653 } else {
1654 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1655 pkt_dev->svlan_id = 0xffff;
1656
1657 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001658 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001659 }
1660 return count;
1661 }
1662
1663 if (!strcmp(name, "svlan_p")) {
1664 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001665 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001666 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001667
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001668 i += len;
1669 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1670 pkt_dev->svlan_p = value;
1671 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1672 } else {
1673 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1674 }
1675 return count;
1676 }
1677
1678 if (!strcmp(name, "svlan_cfi")) {
1679 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001680 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001681 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001682
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001683 i += len;
1684 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1685 pkt_dev->svlan_cfi = value;
1686 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1687 } else {
1688 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1689 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001690 return count;
1691 }
1692
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001693 if (!strcmp(name, "tos")) {
1694 __u32 tmp_value = 0;
1695 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001696 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001697 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001698
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001699 i += len;
1700 if (len == 2) {
1701 pkt_dev->tos = tmp_value;
1702 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1703 } else {
1704 sprintf(pg_result, "ERROR: tos must be 00-ff");
1705 }
1706 return count;
1707 }
1708
1709 if (!strcmp(name, "traffic_class")) {
1710 __u32 tmp_value = 0;
1711 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001712 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001713 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001714
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001715 i += len;
1716 if (len == 2) {
1717 pkt_dev->traffic_class = tmp_value;
1718 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1719 } else {
1720 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1721 }
1722 return count;
1723 }
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1726 return -EINVAL;
1727}
1728
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001729static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001731 return single_open(file, pktgen_if_show, PDE(inode)->data);
1732}
1733
Arjan van de Ven9a321442007-02-12 00:55:35 -08001734static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 .owner = THIS_MODULE,
1736 .open = pktgen_if_open,
1737 .read = seq_read,
1738 .llseek = seq_lseek,
1739 .write = pktgen_if_write,
1740 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001741};
1742
1743static int pktgen_thread_show(struct seq_file *seq, void *v)
1744{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001745 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001746 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001748 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Luiz Capitulino222f1802006-03-20 22:16:13 -08001750 seq_printf(seq, "Running: ");
1751
1752 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08001753 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001754 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001755 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 seq_printf(seq, "\nStopped: ");
1758
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08001759 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001760 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001761 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001764 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001766 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Luiz Capitulino222f1802006-03-20 22:16:13 -08001768 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001773static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 const char __user * user_buffer,
1775 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001777 struct seq_file *seq = (struct seq_file *)file->private_data;
1778 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 int i = 0, max, len, ret;
1780 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001784 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return -EINVAL;
1786 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001789 len = count_trail_chars(&user_buffer[i], max);
1790 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001791 return len;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /* Read variable name */
1796
1797 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001798 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001799 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 memset(name, 0, sizeof(name));
1802 if (copy_from_user(name, &user_buffer[i], len))
1803 return -EFAULT;
1804 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001805
Luiz Capitulino222f1802006-03-20 22:16:13 -08001806 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001808 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001809 return len;
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 i += len;
1812
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001813 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001814 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1815 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Luiz Capitulino222f1802006-03-20 22:16:13 -08001817 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001818 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 ret = -EINVAL;
1820 goto out;
1821 }
1822
1823 pg_result = &(t->result[0]);
1824
Luiz Capitulino222f1802006-03-20 22:16:13 -08001825 if (!strcmp(name, "add_device")) {
1826 char f[32];
1827 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829 if (len < 0) {
1830 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 goto out;
1832 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001833 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return -EFAULT;
1835 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001836 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001837 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001838 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001839 ret = count;
1840 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 goto out;
1842 }
1843
Luiz Capitulino222f1802006-03-20 22:16:13 -08001844 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001845 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001846 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001847 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001848 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001850 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 goto out;
1852 }
1853
Luiz Capitulino222f1802006-03-20 22:16:13 -08001854 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001855 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001856 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto out;
1858 }
1859
1860 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001861out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return ret;
1863}
1864
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001865static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001867 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Arjan van de Ven9a321442007-02-12 00:55:35 -08001870static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001871 .owner = THIS_MODULE,
1872 .open = pktgen_thread_open,
1873 .read = seq_read,
1874 .llseek = seq_lseek,
1875 .write = pktgen_thread_write,
1876 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001877};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001880static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 struct pktgen_thread *t;
1883 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001884 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001886 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001887 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001889 if (remove) {
1890 if_lock(t);
1891 pkt_dev->removal_mark = 1;
1892 t->control |= T_REMDEV;
1893 if_unlock(t);
1894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 break;
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001898 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001901/*
1902 * mark a device for removal
1903 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001904static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001907 const int max_tries = 10, msec_per_try = 125;
1908 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001909
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001910 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001911 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001912
Luiz Capitulino222f1802006-03-20 22:16:13 -08001913 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001914
1915 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001916 if (pkt_dev == NULL)
1917 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001918
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001919 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001920 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1921 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001922 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001923 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001924
1925 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001926 printk(KERN_ERR "pktgen_mark_device: timed out after "
1927 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001928 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001929 break;
1930 }
1931
1932 }
1933
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001934 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001935}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001936
Stephen Hemminger39df2322007-03-04 16:11:51 -08001937static void pktgen_change_name(struct net_device *dev)
1938{
1939 struct pktgen_thread *t;
1940
1941 list_for_each_entry(t, &pktgen_threads, th_list) {
1942 struct pktgen_dev *pkt_dev;
1943
1944 list_for_each_entry(pkt_dev, &t->if_list, list) {
1945 if (pkt_dev->odev != dev)
1946 continue;
1947
1948 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1949
Alexey Dobriyan29753152009-08-28 23:34:43 -07001950 pkt_dev->entry = proc_create_data(dev->name, 0600,
1951 pg_proc_dir,
1952 &pktgen_if_fops,
1953 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001954 if (!pkt_dev->entry)
1955 printk(KERN_ERR "pktgen: can't move proc "
1956 " entry for '%s'\n", dev->name);
1957 break;
1958 }
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Luiz Capitulino222f1802006-03-20 22:16:13 -08001962static int pktgen_device_event(struct notifier_block *unused,
1963 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001965 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001967 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001968 return NOTIFY_DONE;
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* It is OK that we do not hold the group lock right now,
1971 * as we run under the RTNL lock.
1972 */
1973
1974 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001975 case NETDEV_CHANGENAME:
1976 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001980 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 return NOTIFY_DONE;
1985}
1986
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001987static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1988 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001989{
1990 char b[IFNAMSIZ+5];
1991 int i = 0;
1992
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001993 for (i = 0; ifname[i] != '@'; i++) {
1994 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001995 break;
1996
1997 b[i] = ifname[i];
1998 }
1999 b[i] = 0;
2000
2001 return dev_get_by_name(&init_net, b);
2002}
2003
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005/* Associate pktgen_dev with a device. */
2006
Stephen Hemminger39df2322007-03-04 16:11:51 -08002007static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002008{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002010 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (pkt_dev->odev) {
2014 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002015 pkt_dev->odev = NULL;
2016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Robert Olssone6fce5b2008-08-07 02:23:01 -07002018 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07002020 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002021 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07002025 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002026 err = -EINVAL;
2027 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07002028 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002029 err = -ENETDOWN;
2030 } else {
2031 pkt_dev->odev = odev;
2032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002036 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
2039/* Read pkt_dev from the interface and set up internal pktgen_dev
2040 * structure to have the right information to create/send packets
2041 */
2042static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2043{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002044 int ntxq;
2045
Luiz Capitulino222f1802006-03-20 22:16:13 -08002046 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07002047 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
2048 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002049 sprintf(pkt_dev->result,
2050 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2051 return;
2052 }
2053
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002054 /* make sure that we don't pick a non-existing transmit queue */
2055 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002056
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002057 if (ntxq <= pkt_dev->queue_map_min) {
2058 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07002059 "queue_map_min (zero-based) (%d) exceeds valid range "
2060 "[0 - %d] for (%d) queues on %s, resetting\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002061 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
Eric Dumazet593f63b2009-11-23 01:44:37 +00002062 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002063 pkt_dev->queue_map_min = ntxq - 1;
2064 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002065 if (pkt_dev->queue_map_max >= ntxq) {
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002066 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07002067 "queue_map_max (zero-based) (%d) exceeds valid range "
2068 "[0 - %d] for (%d) queues on %s, resetting\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002069 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
Eric Dumazet593f63b2009-11-23 01:44:37 +00002070 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002071 pkt_dev->queue_map_max = ntxq - 1;
2072 }
2073
Luiz Capitulino222f1802006-03-20 22:16:13 -08002074 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002076 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002077 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Luiz Capitulino222f1802006-03-20 22:16:13 -08002079 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002080 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Luiz Capitulino222f1802006-03-20 22:16:13 -08002082 /* Set up pkt size */
2083 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2084
2085 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002087 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 * gets exported
2089 */
2090
2091#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002092 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 struct inet6_dev *idev;
2094
Luiz Capitulino222f1802006-03-20 22:16:13 -08002095 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2096 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 set = 1;
2098 break;
2099 }
2100
Luiz Capitulino222f1802006-03-20 22:16:13 -08002101 if (!set) {
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /*
2104 * Use linklevel address if unconfigured.
2105 *
2106 * use ipv6_get_lladdr if/when it's get exported
2107 */
2108
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002109 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002110 idev = __in6_dev_get(pkt_dev->odev);
2111 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct inet6_ifaddr *ifp;
2113
2114 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002115 for (ifp = idev->addr_list; ifp;
2116 ifp = ifp->if_next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002117 if (ifp->scope == IFA_LINK &&
2118 !(ifp->flags & IFA_F_TENTATIVE)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002119 ipv6_addr_copy(&pkt_dev->
2120 cur_in6_saddr,
2121 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 err = 0;
2123 break;
2124 }
2125 }
2126 read_unlock_bh(&idev->lock);
2127 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002128 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002129 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002130 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2131 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002134 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 pkt_dev->saddr_min = 0;
2136 pkt_dev->saddr_max = 0;
2137 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002138
2139 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002142 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (in_dev) {
2144 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002145 pkt_dev->saddr_min =
2146 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 pkt_dev->saddr_max = pkt_dev->saddr_min;
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002151 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2153 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2154 }
2155
2156 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2157 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2158 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002159 /* Initialize current values. */
2160 pkt_dev->cur_dst_mac_offset = 0;
2161 pkt_dev->cur_src_mac_offset = 0;
2162 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2163 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2164 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2165 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 pkt_dev->nflows = 0;
2167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002170static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2171{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002172 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002173 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002174 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002175
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002176 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2177 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002178
Daniel Turull43d28b62010-06-09 22:49:57 +00002179 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002180 if (remaining <= 0) {
2181 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002182 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002183 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002184
Stephen Hemmingeref879792009-09-22 19:41:43 +00002185 start_time = ktime_now();
Daniel Turull43d28b62010-06-09 22:49:57 +00002186 if (remaining < 100000)
2187 ndelay(remaining); /* really small just spin */
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002188 else {
2189 /* see do_nanosleep */
2190 hrtimer_init_sleeper(&t, current);
2191 do {
2192 set_current_state(TASK_INTERRUPTIBLE);
2193 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2194 if (!hrtimer_active(&t.timer))
2195 t.task = NULL;
2196
2197 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002200 hrtimer_cancel(&t.timer);
2201 } while (t.task && pkt_dev->running && !signal_pending(current));
2202 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002204 end_time = ktime_now();
2205
2206 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002207 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002210static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2211{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002212 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002213 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2214 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2215 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2216}
2217
Stephen Hemminger648fda72009-08-27 13:55:07 +00002218static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002219{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002220 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002221}
2222
2223static inline int f_pick(struct pktgen_dev *pkt_dev)
2224{
2225 int flow = pkt_dev->curfl;
2226
2227 if (pkt_dev->flags & F_FLOW_SEQ) {
2228 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2229 /* reset time */
2230 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002231 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002232 pkt_dev->curfl += 1;
2233 if (pkt_dev->curfl >= pkt_dev->cflows)
2234 pkt_dev->curfl = 0; /*reset */
2235 }
2236 } else {
2237 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002238 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002239
Robert Olsson1211a642008-08-05 18:44:26 -07002240 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002241 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002242 pkt_dev->flows[flow].flags = 0;
2243 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002244 }
2245
2246 return pkt_dev->curfl;
2247}
2248
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002249
2250#ifdef CONFIG_XFRM
2251/* If there was already an IPSEC SA, we keep it as is, else
2252 * we go look for it ...
2253*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002254#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002255static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002256{
2257 struct xfrm_state *x = pkt_dev->flows[flow].x;
2258 if (!x) {
2259 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002260 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002261 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002262 (xfrm_address_t *)&pkt_dev->cur_saddr,
2263 AF_INET,
2264 pkt_dev->ipsmode,
2265 pkt_dev->ipsproto, 0);
2266 if (x) {
2267 pkt_dev->flows[flow].x = x;
2268 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002269 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002270 }
2271
2272 }
2273}
2274#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002275static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2276{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002277
2278 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2279 pkt_dev->cur_queue_map = smp_processor_id();
2280
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002281 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002282 __u16 t;
2283 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2284 t = random32() %
2285 (pkt_dev->queue_map_max -
2286 pkt_dev->queue_map_min + 1)
2287 + pkt_dev->queue_map_min;
2288 } else {
2289 t = pkt_dev->cur_queue_map + 1;
2290 if (t > pkt_dev->queue_map_max)
2291 t = pkt_dev->queue_map_min;
2292 }
2293 pkt_dev->cur_queue_map = t;
2294 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002295 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 -07002296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298/* Increment/randomize headers according to flags and current values
2299 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2300 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002301static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2302{
2303 __u32 imn;
2304 __u32 imx;
2305 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002307 if (pkt_dev->cflows)
2308 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002311 if (pkt_dev->src_mac_count > 1) {
2312 __u32 mc;
2313 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Luiz Capitulino222f1802006-03-20 22:16:13 -08002315 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002316 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002317 else {
2318 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002319 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002320 pkt_dev->src_mac_count)
2321 pkt_dev->cur_src_mac_offset = 0;
2322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Luiz Capitulino222f1802006-03-20 22:16:13 -08002324 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2325 pkt_dev->hh[11] = tmp;
2326 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2327 pkt_dev->hh[10] = tmp;
2328 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2329 pkt_dev->hh[9] = tmp;
2330 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2331 pkt_dev->hh[8] = tmp;
2332 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2333 pkt_dev->hh[7] = tmp;
2334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Luiz Capitulino222f1802006-03-20 22:16:13 -08002336 /* Deal with Destination MAC */
2337 if (pkt_dev->dst_mac_count > 1) {
2338 __u32 mc;
2339 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Luiz Capitulino222f1802006-03-20 22:16:13 -08002341 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002342 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Luiz Capitulino222f1802006-03-20 22:16:13 -08002344 else {
2345 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002346 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002347 pkt_dev->dst_mac_count) {
2348 pkt_dev->cur_dst_mac_offset = 0;
2349 }
2350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Luiz Capitulino222f1802006-03-20 22:16:13 -08002352 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2353 pkt_dev->hh[5] = tmp;
2354 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2355 pkt_dev->hh[4] = tmp;
2356 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2357 pkt_dev->hh[3] = tmp;
2358 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2359 pkt_dev->hh[2] = tmp;
2360 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2361 pkt_dev->hh[1] = tmp;
2362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002364 if (pkt_dev->flags & F_MPLS_RND) {
2365 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002366 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002367 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2368 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002369 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002370 htonl(0x000fffff));
2371 }
2372
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002373 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002374 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002375 }
2376
2377 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002378 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002379 }
2380
Luiz Capitulino222f1802006-03-20 22:16:13 -08002381 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2382 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002383 pkt_dev->cur_udp_src = random32() %
2384 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2385 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 pkt_dev->cur_udp_src++;
2389 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2390 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002391 }
2392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Luiz Capitulino222f1802006-03-20 22:16:13 -08002394 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2395 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002396 pkt_dev->cur_udp_dst = random32() %
2397 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2398 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002399 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002401 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002403 }
2404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 if (!(pkt_dev->flags & F_IPV6)) {
2407
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002408 imn = ntohl(pkt_dev->saddr_min);
2409 imx = ntohl(pkt_dev->saddr_max);
2410 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002412 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002413 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 else {
2415 t = ntohl(pkt_dev->cur_saddr);
2416 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002417 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421 pkt_dev->cur_saddr = htonl(t);
2422 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002423
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002424 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2426 } else {
Al Viro252e33462006-11-14 20:48:11 -08002427 imn = ntohl(pkt_dev->daddr_min);
2428 imx = ntohl(pkt_dev->daddr_max);
2429 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002431 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (pkt_dev->flags & F_IPDST_RND) {
2433
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002434 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002435 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Joe Perches21cf2252007-12-16 13:44:00 -08002437 while (ipv4_is_loopback(s) ||
2438 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002439 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002440 ipv4_is_zeronet(s) ||
2441 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002442 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002443 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Al Viro252e33462006-11-14 20:48:11 -08002445 pkt_dev->cur_daddr = s;
2446 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 t = ntohl(pkt_dev->cur_daddr);
2448 t++;
2449 if (t > imx) {
2450 t = imn;
2451 }
2452 pkt_dev->cur_daddr = htonl(t);
2453 }
2454 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002455 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002456 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002457 pkt_dev->flows[flow].cur_daddr =
2458 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002459#ifdef CONFIG_XFRM
2460 if (pkt_dev->flags & F_IPSEC_ON)
2461 get_ipsec_sa(pkt_dev, flow);
2462#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 pkt_dev->nflows++;
2464 }
2465 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002466 } else { /* IPV6 * */
2467
2468 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2469 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2470 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2471 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 else {
2473 int i;
2474
2475 /* Only random destinations yet */
2476
Luiz Capitulino222f1802006-03-20 22:16:13 -08002477 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002479 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002480 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2481 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485
Luiz Capitulino222f1802006-03-20 22:16:13 -08002486 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2487 __u32 t;
2488 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002489 t = random32() %
2490 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2491 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002492 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002494 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002496 }
2497 pkt_dev->cur_pkt_size = t;
2498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002500 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 pkt_dev->flows[flow].count++;
2503}
2504
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002505
2506#ifdef CONFIG_XFRM
2507static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2508{
2509 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2510 int err = 0;
2511 struct iphdr *iph;
2512
2513 if (!x)
2514 return 0;
2515 /* XXX: we dont support tunnel mode for now until
2516 * we resolve the dst issue */
2517 if (x->props.mode != XFRM_MODE_TRANSPORT)
2518 return 0;
2519
2520 spin_lock(&x->lock);
2521 iph = ip_hdr(skb);
2522
Herbert Xu13996372007-10-17 21:35:51 -07002523 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002524 if (err)
2525 goto error;
2526 err = x->type->output(x, skb);
2527 if (err)
2528 goto error;
2529
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002530 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002531 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002532error:
2533 spin_unlock(&x->lock);
2534 return err;
2535}
2536
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002537static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002538{
2539 if (pkt_dev->cflows) {
2540 /* let go of the SAs if we have them */
2541 int i = 0;
Florian Westphal5b5f7922009-05-21 15:07:12 -07002542 for (; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002543 struct xfrm_state *x = pkt_dev->flows[i].x;
2544 if (x) {
2545 xfrm_state_put(x);
2546 pkt_dev->flows[i].x = NULL;
2547 }
2548 }
2549 }
2550}
2551
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002552static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002553 struct sk_buff *skb, __be16 protocol)
2554{
2555 if (pkt_dev->flags & F_IPSEC_ON) {
2556 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2557 int nhead = 0;
2558 if (x) {
2559 int ret;
2560 __u8 *eth;
2561 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002562 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002563 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2564 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002565 printk(KERN_ERR "Error expanding "
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002566 "ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002567 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002568 }
2569 }
2570
2571 /* ipsec is not expecting ll header */
2572 skb_pull(skb, ETH_HLEN);
2573 ret = pktgen_output_ipsec(skb, pkt_dev);
2574 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002575 printk(KERN_ERR "Error creating ipsec "
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002576 "packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002577 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002578 }
2579 /* restore ll */
2580 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2581 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002582 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002583 }
2584 }
2585 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002586err:
2587 kfree_skb(skb);
2588 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002589}
2590#endif
2591
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002592static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2593{
2594 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002595 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002596 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002597
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002598 mpls--;
2599 *mpls |= MPLS_STACK_BOTTOM;
2600}
2601
Al Viro0f37c602006-11-03 03:49:56 -08002602static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2603 unsigned int prio)
2604{
2605 return htons(id | (cfi << 12) | (prio << 13));
2606}
2607
Luiz Capitulino222f1802006-03-20 22:16:13 -08002608static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2609 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
2611 struct sk_buff *skb = NULL;
2612 __u8 *eth;
2613 struct udphdr *udph;
2614 int datalen, iplen;
2615 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002616 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002617 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002618 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002619 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2620 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2621 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2622 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002623 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002624
2625 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002626 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002627
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002628 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002629 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002630
Robert Olsson64053be2005-06-26 15:27:10 -07002631 /* Update any of the values, used when we're incrementing various
2632 * fields.
2633 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002634 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002635 mod_cur_headers(pkt_dev);
2636
David S. Miller7ac54592006-01-18 14:19:10 -08002637 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002638
2639 if (pkt_dev->flags & F_NODE) {
2640 int node;
2641
2642 if (pkt_dev->node >= 0)
2643 node = pkt_dev->node;
2644 else
2645 node = numa_node_id();
2646
2647 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2648 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2649 if (likely(skb)) {
2650 skb_reserve(skb, NET_SKB_PAD);
2651 skb->dev = odev;
2652 }
2653 }
2654 else
2655 skb = __netdev_alloc_skb(odev,
2656 pkt_dev->cur_pkt_size + 64
2657 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (!skb) {
2660 sprintf(pkt_dev->result, "No memory");
2661 return NULL;
2662 }
2663
David S. Miller7ac54592006-01-18 14:19:10 -08002664 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 /* Reserve for ethernet and IP header */
2667 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002668 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2669 if (pkt_dev->nr_labels)
2670 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002671
2672 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002673 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002674 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002675 *svlan_tci = build_tci(pkt_dev->svlan_id,
2676 pkt_dev->svlan_cfi,
2677 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002678 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002679 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002680 }
2681 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002682 *vlan_tci = build_tci(pkt_dev->vlan_id,
2683 pkt_dev->vlan_cfi,
2684 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002685 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002686 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002687 }
2688
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002689 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002690 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002691 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002692 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002693 iph = ip_hdr(skb);
2694 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002697 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002699 /* Eth + IPh + UDPh + mpls */
2700 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002701 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002702 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 udph->source = htons(pkt_dev->cur_udp_src);
2706 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002707 udph->len = htons(datalen + 8); /* DATA + udphdr */
2708 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 iph->ihl = 5;
2711 iph->version = 4;
2712 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002713 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002714 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 iph->saddr = pkt_dev->cur_saddr;
2716 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002717 iph->id = htons(pkt_dev->ip_id);
2718 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 iph->frag_off = 0;
2720 iplen = 20 + 8 + datalen;
2721 iph->tot_len = htons(iplen);
2722 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002723 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002724 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002725 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002726 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 skb->dev = odev;
2728 skb->pkt_type = PACKET_HOST;
2729
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002730 if (pkt_dev->nfrags <= 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002731 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002732 memset(pgh + 1, 0, datalen - sizeof(struct pktgen_hdr));
2733 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 int frags = pkt_dev->nfrags;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002735 int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Luiz Capitulino222f1802006-03-20 22:16:13 -08002737 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (frags > MAX_SKB_FRAGS)
2740 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002741 if (datalen > frags * PAGE_SIZE) {
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002742 len = datalen - frags * PAGE_SIZE;
2743 memset(skb_put(skb, len), 0, len);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002744 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
2746
2747 i = 0;
2748 while (datalen > 0) {
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002749 struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 skb_shinfo(skb)->frags[i].page = page;
2751 skb_shinfo(skb)->frags[i].page_offset = 0;
2752 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002753 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 datalen -= skb_shinfo(skb)->frags[i].size;
2755 skb->len += skb_shinfo(skb)->frags[i].size;
2756 skb->data_len += skb_shinfo(skb)->frags[i].size;
2757 i++;
2758 skb_shinfo(skb)->nr_frags = i;
2759 }
2760
2761 while (i < frags) {
2762 int rem;
2763
2764 if (i == 0)
2765 break;
2766
2767 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2768 if (rem == 0)
2769 break;
2770
2771 skb_shinfo(skb)->frags[i - 1].size -= rem;
2772
Luiz Capitulino222f1802006-03-20 22:16:13 -08002773 skb_shinfo(skb)->frags[i] =
2774 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002776 skb_shinfo(skb)->frags[i].page =
2777 skb_shinfo(skb)->frags[i - 1].page;
2778 skb_shinfo(skb)->frags[i].page_offset +=
2779 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 skb_shinfo(skb)->frags[i].size = rem;
2781 i++;
2782 skb_shinfo(skb)->nr_frags = i;
2783 }
2784 }
2785
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002786 /* Stamp the time, and sequence number,
2787 * convert them to network byte order
2788 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002789 if (pgh) {
2790 struct timeval timestamp;
2791
2792 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2793 pgh->seq_num = htonl(pkt_dev->seq_num);
2794
2795 do_gettimeofday(&timestamp);
2796 pgh->tv_sec = htonl(timestamp.tv_sec);
2797 pgh->tv_usec = htonl(timestamp.tv_usec);
2798 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002799
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002800#ifdef CONFIG_XFRM
2801 if (!process_ipsec(pkt_dev, skb, protocol))
2802 return NULL;
2803#endif
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return skb;
2806}
2807
2808/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002809 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2811 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002812 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 * Should be candidate for net/ipv4/utils.c
2814 * --ro
2815 */
2816
Luiz Capitulino222f1802006-03-20 22:16:13 -08002817static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002820 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 unsigned long u;
2822 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002823 unsigned int prefixlen = 0;
2824 unsigned int suffixlen = 0;
Al Viro252e33462006-11-14 20:48:11 -08002825 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002826 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Luiz Capitulino222f1802006-03-20 22:16:13 -08002828 for (i = 0; i < 16; i++)
2829 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 for (;;) {
2832 if (*s == ':') {
2833 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002834 if (s[1] == ':') { /* Found "::", skip to part 2 */
2835 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 len++;
2837 break;
2838 }
2839 s++;
2840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002842 u = simple_strtoul(s, &pos, 16);
2843 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002844 if (!i)
2845 return 0;
2846 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 /* the last 4 bytes may be written as IPv4 address */
2849
2850 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002851 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2852 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
2854 ip[prefixlen++] = (u >> 8);
2855 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002856 s += i;
2857 len += i;
2858 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return len;
2860 }
2861
2862/* part 2, after "::" */
2863 for (;;) {
2864 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002865 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 break;
2867 s++;
2868 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002869 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002871
2872 u = simple_strtol(s, &pos, 16);
2873 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002875 if (*s)
2876 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 break;
2878 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002879 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002881 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2882 sizeof(tmp));
2883 suffixlen += 4;
2884 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 break;
2886 }
2887 suffix[suffixlen++] = (u >> 8);
2888 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002889 s += i;
2890 len += i;
2891 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 break;
2893 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002894 for (i = 0; i < suffixlen; i++)
2895 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return len;
2897}
2898
Luiz Capitulino222f1802006-03-20 22:16:13 -08002899static char tohex(char hexdigit)
2900{
2901 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
Luiz Capitulino222f1802006-03-20 22:16:13 -08002904static int fmt_xlong(char *s, unsigned int i)
2905{
2906 char *bak = s;
2907 *s = tohex((i >> 12) & 0xf);
2908 if (s != bak || *s != '0')
2909 ++s;
2910 *s = tohex((i >> 8) & 0xf);
2911 if (s != bak || *s != '0')
2912 ++s;
2913 *s = tohex((i >> 4) & 0xf);
2914 if (s != bak || *s != '0')
2915 ++s;
2916 *s = tohex(i & 0xf);
2917 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
2919
Luiz Capitulino222f1802006-03-20 22:16:13 -08002920static unsigned int fmt_ip6(char *s, const char ip[16])
2921{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 unsigned int len;
2923 unsigned int i;
2924 unsigned int temp;
2925 unsigned int compressing;
2926 int j;
2927
Luiz Capitulino222f1802006-03-20 22:16:13 -08002928 len = 0;
2929 compressing = 0;
2930 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002933 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2934 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2935 temp = strlen(s);
2936 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002939 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2940 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if (temp == 0) {
2942 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002943 compressing = 1;
2944 if (j == 0) {
2945 *s++ = ':';
2946 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
2948 }
2949 } else {
2950 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002951 compressing = 0;
2952 *s++ = ':';
2953 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002955 i = fmt_xlong(s, temp);
2956 len += i;
2957 s += i;
2958 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 *s++ = ':';
2960 ++len;
2961 }
2962 }
2963 }
2964 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002965 *s++ = ':';
2966 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002968 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return len;
2970}
2971
Luiz Capitulino222f1802006-03-20 22:16:13 -08002972static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2973 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
2975 struct sk_buff *skb = NULL;
2976 __u8 *eth;
2977 struct udphdr *udph;
2978 int datalen;
2979 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002980 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002981 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002982 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002983 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2984 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2985 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2986 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002987 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002988
2989 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002990 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002991
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002992 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002993 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002994
Robert Olsson64053be2005-06-26 15:27:10 -07002995 /* Update any of the values, used when we're incrementing various
2996 * fields.
2997 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002998 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002999 mod_cur_headers(pkt_dev);
3000
Stephen Hemmingere4707572009-08-27 13:55:13 +00003001 skb = __netdev_alloc_skb(odev,
3002 pkt_dev->cur_pkt_size + 64
3003 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (!skb) {
3005 sprintf(pkt_dev->result, "No memory");
3006 return NULL;
3007 }
3008
3009 skb_reserve(skb, 16);
3010
3011 /* Reserve for ethernet and IP header */
3012 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003013 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
3014 if (pkt_dev->nr_labels)
3015 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003016
3017 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07003018 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003019 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003020 *svlan_tci = build_tci(pkt_dev->svlan_id,
3021 pkt_dev->svlan_cfi,
3022 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003023 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003024 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003025 }
3026 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003027 *vlan_tci = build_tci(pkt_dev->vlan_id,
3028 pkt_dev->vlan_cfi,
3029 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003030 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003031 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003032 }
3033
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003034 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003035 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003036 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003037 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003038 iph = ipv6_hdr(skb);
3039 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003042 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07003043
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003044 /* Eth + IPh + UDPh + mpls */
3045 datalen = pkt_dev->cur_pkt_size - 14 -
3046 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003047 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Luiz Capitulino222f1802006-03-20 22:16:13 -08003049 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 datalen = sizeof(struct pktgen_hdr);
3051 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08003052 printk(KERN_INFO "pktgen: increased datalen to %d\n",
3053 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
3055
3056 udph->source = htons(pkt_dev->cur_udp_src);
3057 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003058 udph->len = htons(datalen + sizeof(struct udphdr));
3059 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003061 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003063 if (pkt_dev->traffic_class) {
3064 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08003065 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003066 }
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 iph->hop_limit = 32;
3069
3070 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3071 iph->nexthdr = IPPROTO_UDP;
3072
3073 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3074 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3075
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003076 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003077 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003078 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 skb->dev = odev;
3080 skb->pkt_type = PACKET_HOST;
3081
Luiz Capitulino222f1802006-03-20 22:16:13 -08003082 if (pkt_dev->nfrags <= 0)
3083 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 else {
3085 int frags = pkt_dev->nfrags;
3086 int i;
3087
Luiz Capitulino222f1802006-03-20 22:16:13 -08003088 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (frags > MAX_SKB_FRAGS)
3091 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003092 if (datalen > frags * PAGE_SIZE) {
3093 skb_put(skb, datalen - frags * PAGE_SIZE);
3094 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096
3097 i = 0;
3098 while (datalen > 0) {
3099 struct page *page = alloc_pages(GFP_KERNEL, 0);
3100 skb_shinfo(skb)->frags[i].page = page;
3101 skb_shinfo(skb)->frags[i].page_offset = 0;
3102 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08003103 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 datalen -= skb_shinfo(skb)->frags[i].size;
3105 skb->len += skb_shinfo(skb)->frags[i].size;
3106 skb->data_len += skb_shinfo(skb)->frags[i].size;
3107 i++;
3108 skb_shinfo(skb)->nr_frags = i;
3109 }
3110
3111 while (i < frags) {
3112 int rem;
3113
3114 if (i == 0)
3115 break;
3116
3117 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3118 if (rem == 0)
3119 break;
3120
3121 skb_shinfo(skb)->frags[i - 1].size -= rem;
3122
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123 skb_shinfo(skb)->frags[i] =
3124 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003126 skb_shinfo(skb)->frags[i].page =
3127 skb_shinfo(skb)->frags[i - 1].page;
3128 skb_shinfo(skb)->frags[i].page_offset +=
3129 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 skb_shinfo(skb)->frags[i].size = rem;
3131 i++;
3132 skb_shinfo(skb)->nr_frags = i;
3133 }
3134 }
3135
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003136 /* Stamp the time, and sequence number,
3137 * convert them to network byte order
3138 * should we update cloned packets too ?
3139 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003140 if (pgh) {
3141 struct timeval timestamp;
3142
3143 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3144 pgh->seq_num = htonl(pkt_dev->seq_num);
3145
3146 do_gettimeofday(&timestamp);
3147 pgh->tv_sec = htonl(timestamp.tv_sec);
3148 pgh->tv_usec = htonl(timestamp.tv_usec);
3149 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003150 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return skb;
3153}
3154
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003155static struct sk_buff *fill_packet(struct net_device *odev,
3156 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return fill_packet_ipv6(odev, pkt_dev);
3160 else
3161 return fill_packet_ipv4(odev, pkt_dev);
3162}
3163
Luiz Capitulino222f1802006-03-20 22:16:13 -08003164static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 pkt_dev->seq_num = 1;
3167 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169 pkt_dev->tx_bytes = 0;
3170 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
3173/* Set up structure for sending pkts, clear counters */
3174
3175static void pktgen_run(struct pktgen_thread *t)
3176{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003177 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 int started = 0;
3179
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003180 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003183 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 /*
3186 * setup odev and create initial packet.
3187 */
3188 pktgen_setup_inject(pkt_dev);
3189
Luiz Capitulino222f1802006-03-20 22:16:13 -08003190 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003192 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003194 pkt_dev->started_at =
3195 pkt_dev->next_tx = ktime_now();
3196
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003197 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 strcpy(pkt_dev->result, "Starting");
3200 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003201 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 strcpy(pkt_dev->result, "Error starting");
3203 }
3204 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205 if (started)
3206 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
3209static void pktgen_stop_all_threads_ifs(void)
3210{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003211 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003213 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003215 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003216
3217 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003218 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003219
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003220 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Stephen Hemminger648fda72009-08-27 13:55:07 +00003223static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003225 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003227 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003228 if (pkt_dev->running)
3229 return 1;
3230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
Luiz Capitulino222f1802006-03-20 22:16:13 -08003233static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003235 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Luiz Capitulino222f1802006-03-20 22:16:13 -08003239 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Luiz Capitulino222f1802006-03-20 22:16:13 -08003241 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243 if (signal_pending(current))
3244 goto signal;
3245 if_lock(t);
3246 }
3247 if_unlock(t);
3248 return 1;
3249signal:
3250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
3253static int pktgen_wait_all_threads_run(void)
3254{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003255 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003257
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003258 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003259
3260 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003262 if (sig == 0)
3263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003265
3266 if (sig == 0)
3267 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003269
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003270 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return sig;
3272}
3273
3274static void pktgen_run_all_threads(void)
3275{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003276 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003278 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003280 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003282 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003284
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003285 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003287 /* Propagate thread->control */
3288 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 pktgen_wait_all_threads_run();
3291}
3292
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003293static void pktgen_reset_all_threads(void)
3294{
3295 struct pktgen_thread *t;
3296
3297 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3298
3299 mutex_lock(&pktgen_thread_lock);
3300
3301 list_for_each_entry(t, &pktgen_threads, th_list)
3302 t->control |= (T_REMDEVALL);
3303
3304 mutex_unlock(&pktgen_thread_lock);
3305
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003306 /* Propagate thread->control */
3307 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003308
3309 pktgen_wait_all_threads_run();
3310}
3311
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3313{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003314 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003315 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003316 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3317 pkt_dev->started_at);
3318 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003320 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3321 (unsigned long long)ktime_to_us(elapsed),
3322 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3323 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003324 (unsigned long long)pkt_dev->sofar,
3325 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003327 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3328 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Luiz Capitulino222f1802006-03-20 22:16:13 -08003330 bps = pps * 8 * pkt_dev->cur_pkt_size;
3331
3332 mbps = bps;
3333 do_div(mbps, 1000000);
3334 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3335 (unsigned long long)pps,
3336 (unsigned long long)mbps,
3337 (unsigned long long)bps,
3338 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003342static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003344 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003345
Luiz Capitulino222f1802006-03-20 22:16:13 -08003346 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003347 printk(KERN_WARNING "pktgen: interface: %s is already "
Eric Dumazet593f63b2009-11-23 01:44:37 +00003348 "stopped\n", pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003349 return -EINVAL;
3350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003352 kfree_skb(pkt_dev->skb);
3353 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003354 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003355 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003357 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Luiz Capitulino222f1802006-03-20 22:16:13 -08003359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
Luiz Capitulino222f1802006-03-20 22:16:13 -08003362static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003364 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if_lock(t);
3367
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003368 list_for_each_entry(pkt_dev, &t->if_list, list) {
3369 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003370 continue;
3371 if (best == NULL)
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003372 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003373 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003374 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003377 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
Luiz Capitulino222f1802006-03-20 22:16:13 -08003380static void pktgen_stop(struct pktgen_thread *t)
3381{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003382 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003384 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Luiz Capitulino222f1802006-03-20 22:16:13 -08003386 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003388 list_for_each_entry(pkt_dev, &t->if_list, list) {
3389 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Luiz Capitulino222f1802006-03-20 22:16:13 -08003392 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003395/*
3396 * one of our devices needs to be removed - find it
3397 * and remove it
3398 */
3399static void pktgen_rem_one_if(struct pktgen_thread *t)
3400{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003401 struct list_head *q, *n;
3402 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003403
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003404 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003405
3406 if_lock(t);
3407
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003408 list_for_each_safe(q, n, &t->if_list) {
3409 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003410
Luiz Capitulino222f1802006-03-20 22:16:13 -08003411 if (!cur->removal_mark)
3412 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003413
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003414 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003415 cur->skb = NULL;
3416
3417 pktgen_remove_device(t, cur);
3418
3419 break;
3420 }
3421
3422 if_unlock(t);
3423}
3424
Luiz Capitulino222f1802006-03-20 22:16:13 -08003425static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003427 struct list_head *q, *n;
3428 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003429
3430 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003431
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003432 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003433 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003435 list_for_each_safe(q, n, &t->if_list) {
3436 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003437
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003438 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003439 cur->skb = NULL;
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 pktgen_remove_device(t, cur);
3442 }
3443
Luiz Capitulino222f1802006-03-20 22:16:13 -08003444 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
3446
Luiz Capitulino222f1802006-03-20 22:16:13 -08003447static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003449 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
David S. Milleree74baa2007-01-01 20:51:53 -08003451 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003453 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003455 list_del(&t->th_list);
3456
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003457 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Stephen Hemmingeref879792009-09-22 19:41:43 +00003460static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003461{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003462 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003463 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003464 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003465}
3466
Stephen Hemmingeref879792009-09-22 19:41:43 +00003467static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3468{
3469 ktime_t idle_start = ktime_now();
3470
3471 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3472 if (signal_pending(current))
3473 break;
3474
3475 if (need_resched())
3476 pktgen_resched(pkt_dev);
3477 else
3478 cpu_relax();
3479 }
3480 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3481}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003482
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003483static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484{
Stephen Hemminger00829822008-11-20 20:14:53 -08003485 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003486 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003487 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003488 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003489 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 int ret;
3491
Stephen Hemmingeref879792009-09-22 19:41:43 +00003492 /* If device is offline, then don't send */
3493 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3494 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003495 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003497
Stephen Hemmingeref879792009-09-22 19:41:43 +00003498 /* This is max DELAY, this has special meaning of
3499 * "never transmit"
3500 */
3501 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3502 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3503 return;
3504 }
3505
3506 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003507 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3508 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3509 /* build a new pkt */
3510 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003511
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003512 pkt_dev->skb = fill_packet(odev, pkt_dev);
3513 if (pkt_dev->skb == NULL) {
3514 printk(KERN_ERR "pktgen: ERROR: couldn't "
3515 "allocate skb in fill_packet.\n");
3516 schedule();
3517 pkt_dev->clone_count--; /* back out increment, OOM */
3518 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003520 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003521 pkt_dev->allocated_skbs++;
3522 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003524
Stephen Hemmingeref879792009-09-22 19:41:43 +00003525 if (pkt_dev->delay && pkt_dev->last_ok)
3526 spin(pkt_dev, pkt_dev->next_tx);
3527
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003528 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3529 txq = netdev_get_tx_queue(odev, queue_map);
3530
3531 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003532
Eric Dumazet0835acf2009-09-30 13:03:33 +00003533 if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003534 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003535 pkt_dev->last_ok = 0;
3536 goto unlock;
3537 }
3538 atomic_inc(&(pkt_dev->skb->users));
3539 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Stephen Hemmingeref879792009-09-22 19:41:43 +00003541 switch (ret) {
3542 case NETDEV_TX_OK:
3543 txq_trans_update(txq);
3544 pkt_dev->last_ok = 1;
3545 pkt_dev->sofar++;
3546 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003547 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003548 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003549 case NET_XMIT_DROP:
3550 case NET_XMIT_CN:
3551 case NET_XMIT_POLICED:
3552 /* skb has been consumed */
3553 pkt_dev->errors++;
3554 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003555 default: /* Drivers are not supposed to return other values! */
3556 if (net_ratelimit())
3557 pr_info("pktgen: %s xmit error: %d\n",
Eric Dumazet593f63b2009-11-23 01:44:37 +00003558 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003559 pkt_dev->errors++;
3560 /* fallthru */
3561 case NETDEV_TX_LOCKED:
3562 case NETDEV_TX_BUSY:
3563 /* Retry it next time */
3564 atomic_dec(&(pkt_dev->skb->users));
3565 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003567unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003568 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 /* If pkt_dev->count is zero, then run forever */
3571 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003572 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 /* Done with this */
3575 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003576 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003577}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003579/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 * Main loop of the thread goes here
3581 */
3582
David S. Milleree74baa2007-01-01 20:51:53 -08003583static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
3585 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003586 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003587 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
David S. Milleree74baa2007-01-01 20:51:53 -08003590 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003593 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003595 pr_debug("pktgen: starting pktgen/%d: pid=%d\n",
3596 cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
David S. Milleree74baa2007-01-01 20:51:53 -08003598 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003599
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003600 set_freezable();
3601
David S. Milleree74baa2007-01-01 20:51:53 -08003602 while (!kthread_should_stop()) {
3603 pkt_dev = next_to_run(t);
3604
Stephen Hemmingeref879792009-09-22 19:41:43 +00003605 if (unlikely(!pkt_dev && t->control == 0)) {
3606 wait_event_interruptible_timeout(t->queue,
3607 t->control != 0,
3608 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003609 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003610 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003611 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 __set_current_state(TASK_RUNNING);
3614
Stephen Hemmingeref879792009-09-22 19:41:43 +00003615 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 pktgen_xmit(pkt_dev);
3617
Stephen Hemmingeref879792009-09-22 19:41:43 +00003618 if (need_resched())
3619 pktgen_resched(pkt_dev);
3620 else
3621 cpu_relax();
3622 }
3623
Luiz Capitulino222f1802006-03-20 22:16:13 -08003624 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 pktgen_stop(t);
3626 t->control &= ~(T_STOP);
3627 }
3628
Luiz Capitulino222f1802006-03-20 22:16:13 -08003629 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 pktgen_run(t);
3631 t->control &= ~(T_RUN);
3632 }
3633
Luiz Capitulino222f1802006-03-20 22:16:13 -08003634 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003636 t->control &= ~(T_REMDEVALL);
3637 }
3638
Luiz Capitulino222f1802006-03-20 22:16:13 -08003639 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003640 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 t->control &= ~(T_REMDEV);
3642 }
3643
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003644 try_to_freeze();
3645
David S. Milleree74baa2007-01-01 20:51:53 -08003646 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003649 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003650 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003652 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003653 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003655 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003656 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003657
David S. Milleree74baa2007-01-01 20:51:53 -08003658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
Luiz Capitulino222f1802006-03-20 22:16:13 -08003661static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003662 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003664 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003665 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Eric Dumazet3e984842009-11-24 14:50:53 -08003667 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003668 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003669 if (strncmp(p->odevname, ifname, len) == 0) {
3670 if (p->odevname[len]) {
3671 if (exact || p->odevname[len] != '@')
3672 continue;
3673 }
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003674 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003675 break;
3676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Luiz Capitulino222f1802006-03-20 22:16:13 -08003678 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003679 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003680 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003683/*
3684 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 */
3686
Luiz Capitulino222f1802006-03-20 22:16:13 -08003687static int add_dev_to_thread(struct pktgen_thread *t,
3688 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
3690 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Luiz Capitulino222f1802006-03-20 22:16:13 -08003692 if_lock(t);
3693
3694 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003695 printk(KERN_ERR "pktgen: ERROR: already assigned "
3696 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003697 rv = -EBUSY;
3698 goto out;
3699 }
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003700
3701 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003702 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 pkt_dev->running = 0;
3704
Luiz Capitulino222f1802006-03-20 22:16:13 -08003705out:
3706 if_unlock(t);
3707 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
3709
3710/* Called under thread lock */
3711
Luiz Capitulino222f1802006-03-20 22:16:13 -08003712static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003714 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003715 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003716 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 /* We don't allow a device to be on several threads */
3719
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003720 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3721 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003722 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003723 return -EBUSY;
3724 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003725
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003726 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003727 if (!pkt_dev)
3728 return -ENOMEM;
3729
Eric Dumazet593f63b2009-11-23 01:44:37 +00003730 strcpy(pkt_dev->odevname, ifname);
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003731 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3732 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003733 if (pkt_dev->flows == NULL) {
3734 kfree(pkt_dev);
3735 return -ENOMEM;
3736 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003737 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003738
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003739 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003740 pkt_dev->min_pkt_size = ETH_ZLEN;
3741 pkt_dev->max_pkt_size = ETH_ZLEN;
3742 pkt_dev->nfrags = 0;
3743 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003744 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003745 pkt_dev->count = pg_count_d;
3746 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003747 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003748 pkt_dev->udp_src_max = 9;
3749 pkt_dev->udp_dst_min = 9;
3750 pkt_dev->udp_dst_max = 9;
3751
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003752 pkt_dev->vlan_p = 0;
3753 pkt_dev->vlan_cfi = 0;
3754 pkt_dev->vlan_id = 0xffff;
3755 pkt_dev->svlan_p = 0;
3756 pkt_dev->svlan_cfi = 0;
3757 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003758 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003759
Stephen Hemminger39df2322007-03-04 16:11:51 -08003760 err = pktgen_setup_dev(pkt_dev, ifname);
3761 if (err)
3762 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003763
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003764 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3765 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003766 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003767 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003768 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003769 err = -EINVAL;
3770 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003771 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003772#ifdef CONFIG_XFRM
3773 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3774 pkt_dev->ipsproto = IPPROTO_ESP;
3775#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003776
3777 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003778out2:
3779 dev_put(pkt_dev->odev);
3780out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003781#ifdef CONFIG_XFRM
3782 free_SAs(pkt_dev);
3783#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003784 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003785 kfree(pkt_dev);
3786 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
David S. Milleree74baa2007-01-01 20:51:53 -08003789static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003791 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003792 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003793 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003795 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3796 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003797 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003798 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3799 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003800 return -ENOMEM;
3801 }
3802
Luiz Capitulino222f1802006-03-20 22:16:13 -08003803 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003805
David S. Milleree74baa2007-01-01 20:51:53 -08003806 INIT_LIST_HEAD(&t->if_list);
3807
3808 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003809 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003810
3811 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3812 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003813 printk(KERN_ERR "pktgen: kernel_thread() failed "
3814 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003815 list_del(&t->th_list);
3816 kfree(t);
3817 return PTR_ERR(p);
3818 }
3819 kthread_bind(p, cpu);
3820 t->tsk = p;
3821
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003822 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3823 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003824 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003825 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003826 PG_PROC_DIR, t->tsk->comm);
3827 kthread_stop(p);
3828 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003829 kfree(t);
3830 return -EINVAL;
3831 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003832
David S. Milleree74baa2007-01-01 20:51:53 -08003833 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003834 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 return 0;
3837}
3838
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003839/*
3840 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003842static void _rem_dev_from_if_list(struct pktgen_thread *t,
3843 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003845 struct list_head *q, *n;
3846 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003848 list_for_each_safe(q, n, &t->if_list) {
3849 p = list_entry(q, struct pktgen_dev, list);
3850 if (p == pkt_dev)
3851 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
3853}
3854
Luiz Capitulino222f1802006-03-20 22:16:13 -08003855static int pktgen_remove_device(struct pktgen_thread *t,
3856 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
3858
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003859 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Luiz Capitulino222f1802006-03-20 22:16:13 -08003861 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003862 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3863 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003864 pktgen_stop_device(pkt_dev);
3865 }
3866
3867 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
3869 if (pkt_dev->odev) {
3870 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003871 pkt_dev->odev = NULL;
3872 }
3873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 /* And update the thread if_list */
3875
3876 _rem_dev_from_if_list(t, pkt_dev);
3877
Stephen Hemminger39df2322007-03-04 16:11:51 -08003878 if (pkt_dev->entry)
3879 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003881#ifdef CONFIG_XFRM
3882 free_SAs(pkt_dev);
3883#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003884 vfree(pkt_dev->flows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887}
3888
Luiz Capitulino222f1802006-03-20 22:16:13 -08003889static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
3891 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003892 struct proc_dir_entry *pe;
3893
David S. Miller25a8b252007-07-30 16:11:48 -07003894 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003896 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003897 if (!pg_proc_dir)
3898 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Wang Chen25296d52008-02-28 14:11:49 -08003900 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003901 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003902 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3903 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003904 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003905 return -EINVAL;
3906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 /* Register us to receive netdevice events */
3909 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003910
John Hawkes670c02c2005-10-13 09:30:31 -07003911 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003912 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
David S. Milleree74baa2007-01-01 20:51:53 -08003914 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003915 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003916 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3917 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003918 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003919
3920 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003921 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3922 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003923 unregister_netdevice_notifier(&pktgen_notifier_block);
3924 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003925 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003926 return -ENODEV;
3927 }
3928
Luiz Capitulino222f1802006-03-20 22:16:13 -08003929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930}
3931
3932static void __exit pg_cleanup(void)
3933{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003934 struct pktgen_thread *t;
3935 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 wait_queue_head_t queue;
3937 init_waitqueue_head(&queue);
3938
Luiz Capitulino222f1802006-03-20 22:16:13 -08003939 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003941 list_for_each_safe(q, n, &pktgen_threads) {
3942 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003943 kthread_stop(t->tsk);
3944 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Luiz Capitulino222f1802006-03-20 22:16:13 -08003947 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 unregister_netdevice_notifier(&pktgen_notifier_block);
3949
Luiz Capitulino222f1802006-03-20 22:16:13 -08003950 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003951 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003952 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953}
3954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955module_init(pg_init);
3956module_exit(pg_cleanup);
3957
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003958MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959MODULE_DESCRIPTION("Packet Generator tool");
3960MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003961MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003963MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003965MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003967MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003969MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");