blob: 3045dd133071f84033f0ba1528ee95aafd3c01fd [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 Hemmingerd50a6b52005-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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#include <asm/io.h>
167#include <asm/dma.h>
168#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800169#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#include <asm/timex.h>
171
Robert Olssone6fce5b2008-08-07 02:23:01 -0700172#define VERSION "pktgen v2.70: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800175#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800176#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800179#define F_IPSRC_RND (1<<0) /* IP-Src Random */
180#define F_IPDST_RND (1<<1) /* IP-Dst Random */
181#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
182#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
183#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
184#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
185#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
186#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800187#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700188#define F_VID_RND (1<<9) /* Random VLAN ID */
189#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700190#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700191#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700192#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700193#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800196#define T_TERMINATE (1<<0)
197#define T_STOP (1<<1) /* Stop run */
198#define T_RUN (1<<2) /* Start run */
199#define T_REMDEVALL (1<<3) /* Remove all devs */
200#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* If lock -- can be removed after some work */
203#define if_lock(t) spin_lock(&(t->if_lock));
204#define if_unlock(t) spin_unlock(&(t->if_lock));
205
206/* Used to help with determining the pkts on receive */
207#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700208#define PG_PROC_DIR "pktgen"
209#define PGCTRL "pgctrl"
210static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212#define MAX_CFLOWS 65536
213
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700214#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
215#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
216
Luiz Capitulino222f1802006-03-20 22:16:13 -0800217struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800218 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800219 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700220#ifdef CONFIG_XFRM
221 struct xfrm_state *x;
222#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700223 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700226/* flow flag bits */
227#define F_INIT (1<<0) /* flow has been initialized */
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /*
231 * Try to keep frequent/infrequent used vars. separated.
232 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800233 struct proc_dir_entry *entry; /* proc file */
234 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800235 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Luiz Capitulino222f1802006-03-20 22:16:13 -0800237 int running; /* if this changes to false, the test will stop */
238
239 /* If min != max, then we will either do a linear iteration, or
240 * we will do a random selection from within the range.
241 */
242 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800243 int removal_mark; /* non-zero => the device is marked for
244 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Luiz Capitulino222f1802006-03-20 22:16:13 -0800246 int min_pkt_size; /* = ETH_ZLEN; */
247 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700248 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800249 int nfrags;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000250 u64 delay; /* nano-seconds */
251
Luiz Capitulino222f1802006-03-20 22:16:13 -0800252 __u64 count; /* Default No packets to send */
253 __u64 sofar; /* How many pkts we've sent so far */
254 __u64 tx_bytes; /* How many bytes we've transmitted */
255 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Luiz Capitulino222f1802006-03-20 22:16:13 -0800257 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Luiz Capitulino222f1802006-03-20 22:16:13 -0800259 __u64 allocated_skbs;
260 __u32 clone_count;
261 int last_ok; /* Was last skb sent?
262 * Or a failed transmit of some sort? This will keep
263 * sequence numbers in order, for example.
264 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000265 ktime_t next_tx;
266 ktime_t started_at;
267 ktime_t stopped_at;
268 u64 idle_acc; /* nano-seconds */
269
Luiz Capitulino222f1802006-03-20 22:16:13 -0800270 __u32 seq_num;
271
272 int clone_skb; /* Use multiple SKBs during packet gen. If this number
273 * is greater than 1, then that many copies of the same
274 * packet will be sent before a new packet is allocated.
275 * For instance, if you want to send 1024 identical packets
276 * before creating a new packet, set clone_skb to 1024.
277 */
278
279 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
283
284 struct in6_addr in6_saddr;
285 struct in6_addr in6_daddr;
286 struct in6_addr cur_in6_daddr;
287 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800289 struct in6_addr min_in6_daddr;
290 struct in6_addr max_in6_daddr;
291 struct in6_addr min_in6_saddr;
292 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Luiz Capitulino222f1802006-03-20 22:16:13 -0800294 /* If we're doing ranges, random or incremental, then this
295 * defines the min/max for those ranges.
296 */
Al Viro252e3342006-11-14 20:48:11 -0800297 __be32 saddr_min; /* inclusive, source IP address */
298 __be32 saddr_max; /* exclusive, source IP address */
299 __be32 daddr_min; /* inclusive, dest IP address */
300 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Luiz Capitulino222f1802006-03-20 22:16:13 -0800302 __u16 udp_src_min; /* inclusive, source UDP port */
303 __u16 udp_src_max; /* exclusive, source UDP port */
304 __u16 udp_dst_min; /* inclusive, dest UDP port */
305 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700307 /* DSCP + ECN */
308 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
309 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
310
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800311 /* MPLS */
312 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
313 __be32 labels[MAX_MPLS_LABELS];
314
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700315 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
316 __u8 vlan_p;
317 __u8 vlan_cfi;
318 __u16 vlan_id; /* 0xffff means no vlan tag */
319
320 __u8 svlan_p;
321 __u8 svlan_cfi;
322 __u16 svlan_id; /* 0xffff means no svlan tag */
323
Luiz Capitulino222f1802006-03-20 22:16:13 -0800324 __u32 src_mac_count; /* How many MACs to iterate through */
325 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Luiz Capitulino222f1802006-03-20 22:16:13 -0800327 unsigned char dst_mac[ETH_ALEN];
328 unsigned char src_mac[ETH_ALEN];
329
330 __u32 cur_dst_mac_offset;
331 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800332 __be32 cur_saddr;
333 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800334 __u16 cur_udp_dst;
335 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700336 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800337 __u32 cur_pkt_size;
338
339 __u8 hh[14];
340 /* = {
341 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
342
343 We fill in SRC address later
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x08, 0x00
346 };
347 */
348 __u16 pad; /* pad out the hh struct to an even 16 bytes */
349
350 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
351 * are transmitting the same one multiple times
352 */
353 struct net_device *odev; /* The out-going device. Note that the device should
354 * have it's pg_info pointer pointing back to this
355 * device. This will be set when the user specifies
356 * the out-going device name (not when the inject is
357 * started as it used to do.)
358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800360 unsigned cflows; /* Concurrent flows (config) */
361 unsigned lflow; /* Flow length (config) */
362 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700363 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700364
365 u16 queue_map_min;
366 u16 queue_map_max;
367
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700368#ifdef CONFIG_XFRM
369 __u8 ipsmode; /* IPSEC mode (config) */
370 __u8 ipsproto; /* IPSEC type (config) */
371#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800372 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
375struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800376 __be32 pgh_magic;
377 __be32 seq_num;
378 __be32 tv_sec;
379 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380};
381
382struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800383 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800384 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800385 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800386 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800387 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Luiz Capitulino222f1802006-03-20 22:16:13 -0800389 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
390
391 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int cpu;
393
Luiz Capitulino222f1802006-03-20 22:16:13 -0800394 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700395 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
398#define REMOVE 1
399#define FIND 0
400
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000401static inline ktime_t ktime_now(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000403 struct timespec ts;
404 ktime_get_ts(&ts);
405
406 return timespec_to_ktime(ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000409/* This works even if 32 bit because of careful byte order choice */
410static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000412 return cmp1.tv64 < cmp2.tv64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000415static const char version[] __initconst = VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Luiz Capitulino222f1802006-03-20 22:16:13 -0800417static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
418static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
419static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
420 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
422static void pktgen_run_all_threads(void);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800423static void pktgen_reset_all_threads(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static void pktgen_stop_all_threads_ifs(void);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000425
Luiz Capitulino222f1802006-03-20 22:16:13 -0800426static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800428
Luiz Capitulino222f1802006-03-20 22:16:13 -0800429static unsigned int scan_ip6(const char *s, char ip[16]);
430static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000433static int pg_count_d __read_mostly = 1000;
434static int pg_delay_d __read_mostly;
435static int pg_clone_skb_d __read_mostly;
436static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Luiz Capitulino222fa072006-03-20 22:24:27 -0800438static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800439static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static struct notifier_block pktgen_notifier_block = {
442 .notifier_call = pktgen_device_event,
443};
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900446 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *
448 */
449
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700450static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800451{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700452 seq_puts(seq, VERSION);
453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Luiz Capitulino222f1802006-03-20 22:16:13 -0800456static ssize_t pgctrl_write(struct file *file, const char __user * buf,
457 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int err = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700460 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Luiz Capitulino222f1802006-03-20 22:16:13 -0800462 if (!capable(CAP_NET_ADMIN)) {
463 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700467 if (count > sizeof(data))
468 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700471 err = -EFAULT;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700472 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800473 }
474 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Luiz Capitulino222f1802006-03-20 22:16:13 -0800476 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 pktgen_stop_all_threads_ifs();
478
Luiz Capitulino222f1802006-03-20 22:16:13 -0800479 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 pktgen_run_all_threads();
481
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800482 else if (!strcmp(data, "reset"))
483 pktgen_reset_all_threads();
484
Luiz Capitulino222f1802006-03-20 22:16:13 -0800485 else
David S. Miller25a8b252007-07-30 16:11:48 -0700486 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 err = count;
489
Luiz Capitulino222f1802006-03-20 22:16:13 -0800490out:
491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700494static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700496 return single_open(file, pgctrl_show, PDE(inode)->data);
497}
498
Arjan van de Ven9a321442007-02-12 00:55:35 -0800499static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800500 .owner = THIS_MODULE,
501 .open = pgctrl_open,
502 .read = seq_read,
503 .llseek = seq_lseek,
504 .write = pgctrl_write,
505 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700506};
507
508static int pktgen_if_show(struct seq_file *seq, void *v)
509{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000510 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000511 ktime_t stopped;
512 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Luiz Capitulino222f1802006-03-20 22:16:13 -0800514 seq_printf(seq,
515 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
516 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
517 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Luiz Capitulino222f1802006-03-20 22:16:13 -0800519 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000520 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
521 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800522 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Luiz Capitulino222f1802006-03-20 22:16:13 -0800524 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
525 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Robert Olsson45b270f2007-08-28 15:45:55 -0700527 seq_printf(seq,
528 " queue_map_min: %u queue_map_max: %u\n",
529 pkt_dev->queue_map_min,
530 pkt_dev->queue_map_max);
531
Luiz Capitulino222f1802006-03-20 22:16:13 -0800532 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800534 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
535 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
536 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
537 seq_printf(seq,
538 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
539 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Luiz Capitulino222f1802006-03-20 22:16:13 -0800541 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
542 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
543 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
544 seq_printf(seq,
545 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
546 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Luiz Capitulino222f1802006-03-20 22:16:13 -0800548 } else
549 seq_printf(seq,
550 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
551 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
552 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700554 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Johannes Berge1749612008-10-27 15:59:26 -0700556 seq_printf(seq, "%pM ",
557 is_zero_ether_addr(pkt_dev->src_mac) ?
558 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Luiz Capitulino222f1802006-03-20 22:16:13 -0800560 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700561 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Luiz Capitulino222f1802006-03-20 22:16:13 -0800563 seq_printf(seq,
564 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
565 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
566 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Luiz Capitulino222f1802006-03-20 22:16:13 -0800568 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800569 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700570 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800572 if (pkt_dev->nr_labels) {
573 unsigned i;
574 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700575 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800576 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
577 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
578 }
579
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700580 if (pkt_dev->vlan_id != 0xffff) {
581 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
582 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
583 }
584
585 if (pkt_dev->svlan_id != 0xffff) {
586 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
587 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
588 }
589
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700590 if (pkt_dev->tos) {
591 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
592 }
593
594 if (pkt_dev->traffic_class) {
595 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
596 }
597
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800598 seq_printf(seq, " Flags: ");
599
Luiz Capitulino222f1802006-03-20 22:16:13 -0800600 if (pkt_dev->flags & F_IPV6)
601 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Luiz Capitulino222f1802006-03-20 22:16:13 -0800603 if (pkt_dev->flags & F_IPSRC_RND)
604 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Luiz Capitulino222f1802006-03-20 22:16:13 -0800606 if (pkt_dev->flags & F_IPDST_RND)
607 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Luiz Capitulino222f1802006-03-20 22:16:13 -0800609 if (pkt_dev->flags & F_TXSIZE_RND)
610 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Luiz Capitulino222f1802006-03-20 22:16:13 -0800612 if (pkt_dev->flags & F_UDPSRC_RND)
613 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Luiz Capitulino222f1802006-03-20 22:16:13 -0800615 if (pkt_dev->flags & F_UDPDST_RND)
616 seq_printf(seq, "UDPDST_RND ");
617
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800618 if (pkt_dev->flags & F_MPLS_RND)
619 seq_printf(seq, "MPLS_RND ");
620
Robert Olsson45b270f2007-08-28 15:45:55 -0700621 if (pkt_dev->flags & F_QUEUE_MAP_RND)
622 seq_printf(seq, "QUEUE_MAP_RND ");
623
Robert Olssone6fce5b2008-08-07 02:23:01 -0700624 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
625 seq_printf(seq, "QUEUE_MAP_CPU ");
626
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700627 if (pkt_dev->cflows) {
628 if (pkt_dev->flags & F_FLOW_SEQ)
629 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
630 else
631 seq_printf(seq, "FLOW_RND ");
632 }
633
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700634#ifdef CONFIG_XFRM
635 if (pkt_dev->flags & F_IPSEC_ON)
636 seq_printf(seq, "IPSEC ");
637#endif
638
Luiz Capitulino222f1802006-03-20 22:16:13 -0800639 if (pkt_dev->flags & F_MACSRC_RND)
640 seq_printf(seq, "MACSRC_RND ");
641
642 if (pkt_dev->flags & F_MACDST_RND)
643 seq_printf(seq, "MACDST_RND ");
644
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700645 if (pkt_dev->flags & F_VID_RND)
646 seq_printf(seq, "VID_RND ");
647
648 if (pkt_dev->flags & F_SVID_RND)
649 seq_printf(seq, "SVID_RND ");
650
Luiz Capitulino222f1802006-03-20 22:16:13 -0800651 seq_puts(seq, "\n");
652
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000653 /* not really stopped, more like last-running-at */
654 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
655 idle = pkt_dev->idle_acc;
656 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800657
658 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000659 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800660 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000661 (unsigned long long)pkt_dev->errors);
662
663 seq_printf(seq,
664 " started: %lluus stopped: %lluus idle: %lluus\n",
665 (unsigned long long) ktime_to_us(pkt_dev->started_at),
666 (unsigned long long) ktime_to_us(stopped),
667 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800668
669 seq_printf(seq,
670 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700671 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
672 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Luiz Capitulino222f1802006-03-20 22:16:13 -0800674 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800676 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
677 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
678 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
679 } else
680 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700681 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Luiz Capitulino222f1802006-03-20 22:16:13 -0800683 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700684 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Robert Olsson45b270f2007-08-28 15:45:55 -0700686 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
687
Luiz Capitulino222f1802006-03-20 22:16:13 -0800688 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800691 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800693 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800698
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700699static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800700{
701 int i = 0;
702 *num = 0;
703
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700704 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800705 char c;
706 *num <<= 4;
707 if (get_user(c, &user_buffer[i]))
708 return -EFAULT;
709 if ((c >= '0') && (c <= '9'))
710 *num |= c - '0';
711 else if ((c >= 'a') && (c <= 'f'))
712 *num |= c - 'a' + 10;
713 else if ((c >= 'A') && (c <= 'F'))
714 *num |= c - 'A' + 10;
715 else
716 break;
717 }
718 return i;
719}
720
Luiz Capitulino222f1802006-03-20 22:16:13 -0800721static int count_trail_chars(const char __user * user_buffer,
722 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 int i;
725
726 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800727 char c;
728 if (get_user(c, &user_buffer[i]))
729 return -EFAULT;
730 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 case '\"':
732 case '\n':
733 case '\r':
734 case '\t':
735 case ' ':
736 case '=':
737 break;
738 default:
739 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742done:
743 return i;
744}
745
Luiz Capitulino222f1802006-03-20 22:16:13 -0800746static unsigned long num_arg(const char __user * user_buffer,
747 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 int i = 0;
750 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800751
752 for (; i < maxlen; i++) {
753 char c;
754 if (get_user(c, &user_buffer[i]))
755 return -EFAULT;
756 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800758 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 } else
760 break;
761 }
762 return i;
763}
764
Luiz Capitulino222f1802006-03-20 22:16:13 -0800765static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 int i = 0;
768
Luiz Capitulino222f1802006-03-20 22:16:13 -0800769 for (; i < maxlen; i++) {
770 char c;
771 if (get_user(c, &user_buffer[i]))
772 return -EFAULT;
773 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case '\"':
775 case '\n':
776 case '\r':
777 case '\t':
778 case ' ':
779 goto done_str;
780 break;
781 default:
782 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return i;
787}
788
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800789static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
790{
791 unsigned n = 0;
792 char c;
793 ssize_t i = 0;
794 int len;
795
796 pkt_dev->nr_labels = 0;
797 do {
798 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700799 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800800 if (len <= 0)
801 return len;
802 pkt_dev->labels[n] = htonl(tmp);
803 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
804 pkt_dev->flags |= F_MPLS_RND;
805 i += len;
806 if (get_user(c, &buffer[i]))
807 return -EFAULT;
808 i++;
809 n++;
810 if (n >= MAX_MPLS_LABELS)
811 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700812 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800813
814 pkt_dev->nr_labels = n;
815 return i;
816}
817
Luiz Capitulino222f1802006-03-20 22:16:13 -0800818static ssize_t pktgen_if_write(struct file *file,
819 const char __user * user_buffer, size_t count,
820 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800822 struct seq_file *seq = (struct seq_file *)file->private_data;
823 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int i = 0, max, len;
825 char name[16], valstr[32];
826 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800827 char *pg_result = NULL;
828 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800830
831 pg_result = &(pkt_dev->result[0]);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700834 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return -EINVAL;
836 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 max = count - i;
839 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800840 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700841 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800842 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800844 i += tmp;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* Read variable name */
847
848 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800849 if (len < 0) {
850 return len;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -EFAULT;
855 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800856
857 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800859 if (len < 0)
860 return len;
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 i += len;
863
864 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800865 char tb[count + 1];
866 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800868 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700869 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800870 (unsigned long)count, tb);
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 if (!strcmp(name, "min_pkt_size")) {
874 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875 if (len < 0) {
876 return len;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800879 if (value < 14 + 20 + 8)
880 value = 14 + 20 + 8;
881 if (value != pkt_dev->min_pkt_size) {
882 pkt_dev->min_pkt_size = value;
883 pkt_dev->cur_pkt_size = value;
884 }
885 sprintf(pg_result, "OK: min_pkt_size=%u",
886 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return count;
888 }
889
Luiz Capitulino222f1802006-03-20 22:16:13 -0800890 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800892 if (len < 0) {
893 return len;
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800896 if (value < 14 + 20 + 8)
897 value = 14 + 20 + 8;
898 if (value != pkt_dev->max_pkt_size) {
899 pkt_dev->max_pkt_size = value;
900 pkt_dev->cur_pkt_size = value;
901 }
902 sprintf(pg_result, "OK: max_pkt_size=%u",
903 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return count;
905 }
906
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (!strcmp(name, "pkt_size")) {
910 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800911 if (len < 0) {
912 return len;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800915 if (value < 14 + 20 + 8)
916 value = 14 + 20 + 8;
917 if (value != pkt_dev->min_pkt_size) {
918 pkt_dev->min_pkt_size = value;
919 pkt_dev->max_pkt_size = value;
920 pkt_dev->cur_pkt_size = value;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
923 return count;
924 }
925
Luiz Capitulino222f1802006-03-20 22:16:13 -0800926 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800928 if (len < 0) {
929 return len;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800932 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 sprintf(pg_result, "OK: debug=%u", debug);
934 return count;
935 }
936
Luiz Capitulino222f1802006-03-20 22:16:13 -0800937 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800939 if (len < 0) {
940 return len;
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 i += len;
943 pkt_dev->nfrags = value;
944 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
945 return count;
946 }
947 if (!strcmp(name, "delay")) {
948 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800949 if (len < 0) {
950 return len;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000953 if (value == 0x7FFFFFFF)
954 pkt_dev->delay = ULLONG_MAX;
955 else
956 pkt_dev->delay = (u64)value * NSEC_PER_USEC;
957
958 sprintf(pg_result, "OK: delay=%llu",
959 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return count;
961 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800962 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800964 if (len < 0) {
965 return len;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800968 if (value != pkt_dev->udp_src_min) {
969 pkt_dev->udp_src_min = value;
970 pkt_dev->cur_udp_src = value;
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
973 return count;
974 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800977 if (len < 0) {
978 return len;
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800981 if (value != pkt_dev->udp_dst_min) {
982 pkt_dev->udp_dst_min = value;
983 pkt_dev->cur_udp_dst = value;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
986 return count;
987 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800988 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800990 if (len < 0) {
991 return len;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800994 if (value != pkt_dev->udp_src_max) {
995 pkt_dev->udp_src_max = value;
996 pkt_dev->cur_udp_src = value;
997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
999 return count;
1000 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001001 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001003 if (len < 0) {
1004 return len;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001007 if (value != pkt_dev->udp_dst_max) {
1008 pkt_dev->udp_dst_max = value;
1009 pkt_dev->cur_udp_dst = value;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1012 return count;
1013 }
1014 if (!strcmp(name, "clone_skb")) {
1015 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001016 if (len < 0) {
1017 return len;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001020 pkt_dev->clone_skb = value;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1023 return count;
1024 }
1025 if (!strcmp(name, "count")) {
1026 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001027 if (len < 0) {
1028 return len;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 i += len;
1031 pkt_dev->count = value;
1032 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001033 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return count;
1035 }
1036 if (!strcmp(name, "src_mac_count")) {
1037 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001038 if (len < 0) {
1039 return len;
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 i += len;
1042 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001043 pkt_dev->src_mac_count = value;
1044 pkt_dev->cur_src_mac_offset = 0;
1045 }
1046 sprintf(pg_result, "OK: src_mac_count=%d",
1047 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return count;
1049 }
1050 if (!strcmp(name, "dst_mac_count")) {
1051 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 if (len < 0) {
1053 return len;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 i += len;
1056 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001057 pkt_dev->dst_mac_count = value;
1058 pkt_dev->cur_dst_mac_offset = 0;
1059 }
1060 sprintf(pg_result, "OK: dst_mac_count=%d",
1061 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return count;
1063 }
1064 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001065 char f[32];
1066 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001068 if (len < 0) {
1069 return len;
1070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (copy_from_user(f, &user_buffer[i], len))
1072 return -EFAULT;
1073 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001074 if (strcmp(f, "IPSRC_RND") == 0)
1075 pkt_dev->flags |= F_IPSRC_RND;
1076
1077 else if (strcmp(f, "!IPSRC_RND") == 0)
1078 pkt_dev->flags &= ~F_IPSRC_RND;
1079
1080 else if (strcmp(f, "TXSIZE_RND") == 0)
1081 pkt_dev->flags |= F_TXSIZE_RND;
1082
1083 else if (strcmp(f, "!TXSIZE_RND") == 0)
1084 pkt_dev->flags &= ~F_TXSIZE_RND;
1085
1086 else if (strcmp(f, "IPDST_RND") == 0)
1087 pkt_dev->flags |= F_IPDST_RND;
1088
1089 else if (strcmp(f, "!IPDST_RND") == 0)
1090 pkt_dev->flags &= ~F_IPDST_RND;
1091
1092 else if (strcmp(f, "UDPSRC_RND") == 0)
1093 pkt_dev->flags |= F_UDPSRC_RND;
1094
1095 else if (strcmp(f, "!UDPSRC_RND") == 0)
1096 pkt_dev->flags &= ~F_UDPSRC_RND;
1097
1098 else if (strcmp(f, "UDPDST_RND") == 0)
1099 pkt_dev->flags |= F_UDPDST_RND;
1100
1101 else if (strcmp(f, "!UDPDST_RND") == 0)
1102 pkt_dev->flags &= ~F_UDPDST_RND;
1103
1104 else if (strcmp(f, "MACSRC_RND") == 0)
1105 pkt_dev->flags |= F_MACSRC_RND;
1106
1107 else if (strcmp(f, "!MACSRC_RND") == 0)
1108 pkt_dev->flags &= ~F_MACSRC_RND;
1109
1110 else if (strcmp(f, "MACDST_RND") == 0)
1111 pkt_dev->flags |= F_MACDST_RND;
1112
1113 else if (strcmp(f, "!MACDST_RND") == 0)
1114 pkt_dev->flags &= ~F_MACDST_RND;
1115
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001116 else if (strcmp(f, "MPLS_RND") == 0)
1117 pkt_dev->flags |= F_MPLS_RND;
1118
1119 else if (strcmp(f, "!MPLS_RND") == 0)
1120 pkt_dev->flags &= ~F_MPLS_RND;
1121
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001122 else if (strcmp(f, "VID_RND") == 0)
1123 pkt_dev->flags |= F_VID_RND;
1124
1125 else if (strcmp(f, "!VID_RND") == 0)
1126 pkt_dev->flags &= ~F_VID_RND;
1127
1128 else if (strcmp(f, "SVID_RND") == 0)
1129 pkt_dev->flags |= F_SVID_RND;
1130
1131 else if (strcmp(f, "!SVID_RND") == 0)
1132 pkt_dev->flags &= ~F_SVID_RND;
1133
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001134 else if (strcmp(f, "FLOW_SEQ") == 0)
1135 pkt_dev->flags |= F_FLOW_SEQ;
1136
Robert Olsson45b270f2007-08-28 15:45:55 -07001137 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1138 pkt_dev->flags |= F_QUEUE_MAP_RND;
1139
1140 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1141 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001142
1143 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1144 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1145
1146 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1147 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001148#ifdef CONFIG_XFRM
1149 else if (strcmp(f, "IPSEC") == 0)
1150 pkt_dev->flags |= F_IPSEC_ON;
1151#endif
1152
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001153 else if (strcmp(f, "!IPV6") == 0)
1154 pkt_dev->flags &= ~F_IPV6;
1155
Luiz Capitulino222f1802006-03-20 22:16:13 -08001156 else {
1157 sprintf(pg_result,
1158 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1159 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001160 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001161 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001162 return count;
1163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1165 return count;
1166 }
1167 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1168 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001169 if (len < 0) {
1170 return len;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Luiz Capitulino222f1802006-03-20 22:16:13 -08001173 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001175 buf[len] = 0;
1176 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1177 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1178 strncpy(pkt_dev->dst_min, buf, len);
1179 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1180 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1181 }
1182 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001183 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001184 pkt_dev->dst_min);
1185 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1187 return count;
1188 }
1189 if (!strcmp(name, "dst_max")) {
1190 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001191 if (len < 0) {
1192 return len;
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Luiz Capitulino222f1802006-03-20 22:16:13 -08001195 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return -EFAULT;
1197
Luiz Capitulino222f1802006-03-20 22:16:13 -08001198 buf[len] = 0;
1199 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1200 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1201 strncpy(pkt_dev->dst_max, buf, len);
1202 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1203 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1204 }
1205 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001206 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001207 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 i += len;
1209 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1210 return count;
1211 }
1212 if (!strcmp(name, "dst6")) {
1213 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001214 if (len < 0)
1215 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 pkt_dev->flags |= F_IPV6;
1218
Luiz Capitulino222f1802006-03-20 22:16:13 -08001219 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001221 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001224 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1227
Luiz Capitulino222f1802006-03-20 22:16:13 -08001228 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001229 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Luiz Capitulino222f1802006-03-20 22:16:13 -08001231 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 sprintf(pg_result, "OK: dst6=%s", buf);
1233 return count;
1234 }
1235 if (!strcmp(name, "dst6_min")) {
1236 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001237 if (len < 0)
1238 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 pkt_dev->flags |= F_IPV6;
1241
Luiz Capitulino222f1802006-03-20 22:16:13 -08001242 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001244 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001247 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Luiz Capitulino222f1802006-03-20 22:16:13 -08001249 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1250 &pkt_dev->min_in6_daddr);
1251 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001252 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Luiz Capitulino222f1802006-03-20 22:16:13 -08001254 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 sprintf(pg_result, "OK: dst6_min=%s", buf);
1256 return count;
1257 }
1258 if (!strcmp(name, "dst6_max")) {
1259 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 if (len < 0)
1261 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 pkt_dev->flags |= F_IPV6;
1264
Luiz Capitulino222f1802006-03-20 22:16:13 -08001265 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001267 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001270 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Luiz Capitulino222f1802006-03-20 22:16:13 -08001272 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001273 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Luiz Capitulino222f1802006-03-20 22:16:13 -08001275 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 sprintf(pg_result, "OK: dst6_max=%s", buf);
1277 return count;
1278 }
1279 if (!strcmp(name, "src6")) {
1280 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001281 if (len < 0)
1282 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 pkt_dev->flags |= F_IPV6;
1285
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001288 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001291 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1294
Luiz Capitulino222f1802006-03-20 22:16:13 -08001295 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001296 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001297
1298 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 sprintf(pg_result, "OK: src6=%s", buf);
1300 return count;
1301 }
1302 if (!strcmp(name, "src_min")) {
1303 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001304 if (len < 0) {
1305 return len;
1306 }
1307 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001309 buf[len] = 0;
1310 if (strcmp(buf, pkt_dev->src_min) != 0) {
1311 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1312 strncpy(pkt_dev->src_min, buf, len);
1313 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1314 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1315 }
1316 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001317 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001318 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 i += len;
1320 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1321 return count;
1322 }
1323 if (!strcmp(name, "src_max")) {
1324 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001325 if (len < 0) {
1326 return len;
1327 }
1328 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001330 buf[len] = 0;
1331 if (strcmp(buf, pkt_dev->src_max) != 0) {
1332 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1333 strncpy(pkt_dev->src_max, buf, len);
1334 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1335 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1336 }
1337 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001338 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001339 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 i += len;
1341 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1342 return count;
1343 }
1344 if (!strcmp(name, "dst_mac")) {
1345 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001346 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001348 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 if (len < 0) {
1352 return len;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001355 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EFAULT;
1357 i += len;
1358
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (*v >= '0' && *v <= '9') {
1361 *m *= 16;
1362 *m += *v - '0';
1363 }
1364 if (*v >= 'A' && *v <= 'F') {
1365 *m *= 16;
1366 *m += *v - 'A' + 10;
1367 }
1368 if (*v >= 'a' && *v <= 'f') {
1369 *m *= 16;
1370 *m += *v - 'a' + 10;
1371 }
1372 if (*v == ':') {
1373 m++;
1374 *m = 0;
1375 }
1376 }
1377
1378 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001379 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1380 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 sprintf(pg_result, "OK: dstmac");
1383 return count;
1384 }
1385 if (!strcmp(name, "src_mac")) {
1386 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001387 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 unsigned char *m = pkt_dev->src_mac;
1389
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001390 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 if (len < 0) {
1394 return len;
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001397 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return -EFAULT;
1399 i += len;
1400
Luiz Capitulino222f1802006-03-20 22:16:13 -08001401 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (*v >= '0' && *v <= '9') {
1403 *m *= 16;
1404 *m += *v - '0';
1405 }
1406 if (*v >= 'A' && *v <= 'F') {
1407 *m *= 16;
1408 *m += *v - 'A' + 10;
1409 }
1410 if (*v >= 'a' && *v <= 'f') {
1411 *m *= 16;
1412 *m += *v - 'a' + 10;
1413 }
1414 if (*v == ':') {
1415 m++;
1416 *m = 0;
1417 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001420 /* Set up Src MAC */
1421 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1422 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1423
Luiz Capitulino222f1802006-03-20 22:16:13 -08001424 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return count;
1426 }
1427
Luiz Capitulino222f1802006-03-20 22:16:13 -08001428 if (!strcmp(name, "clear_counters")) {
1429 pktgen_clear_counters(pkt_dev);
1430 sprintf(pg_result, "OK: Clearing counters.\n");
1431 return count;
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (!strcmp(name, "flows")) {
1435 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001436 if (len < 0) {
1437 return len;
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 i += len;
1440 if (value > MAX_CFLOWS)
1441 value = MAX_CFLOWS;
1442
1443 pkt_dev->cflows = value;
1444 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1445 return count;
1446 }
1447
1448 if (!strcmp(name, "flowlen")) {
1449 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001450 if (len < 0) {
1451 return len;
1452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 i += len;
1454 pkt_dev->lflow = value;
1455 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1456 return count;
1457 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001458
Robert Olsson45b270f2007-08-28 15:45:55 -07001459 if (!strcmp(name, "queue_map_min")) {
1460 len = num_arg(&user_buffer[i], 5, &value);
1461 if (len < 0) {
1462 return len;
1463 }
1464 i += len;
1465 pkt_dev->queue_map_min = value;
1466 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1467 return count;
1468 }
1469
1470 if (!strcmp(name, "queue_map_max")) {
1471 len = num_arg(&user_buffer[i], 5, &value);
1472 if (len < 0) {
1473 return len;
1474 }
1475 i += len;
1476 pkt_dev->queue_map_max = value;
1477 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1478 return count;
1479 }
1480
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001481 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001482 unsigned n, cnt;
1483
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001484 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001485 if (len < 0)
1486 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001487 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001488 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001489 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001490 cnt += sprintf(pg_result + cnt,
1491 "%08x%s", ntohl(pkt_dev->labels[n]),
1492 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001493
1494 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1495 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1496 pkt_dev->svlan_id = 0xffff;
1497
1498 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001499 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001500 }
1501 return count;
1502 }
1503
1504 if (!strcmp(name, "vlan_id")) {
1505 len = num_arg(&user_buffer[i], 4, &value);
1506 if (len < 0) {
1507 return len;
1508 }
1509 i += len;
1510 if (value <= 4095) {
1511 pkt_dev->vlan_id = value; /* turn on VLAN */
1512
1513 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001514 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001515
1516 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001517 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001518
1519 pkt_dev->nr_labels = 0; /* turn off MPLS */
1520 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1521 } else {
1522 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1523 pkt_dev->svlan_id = 0xffff;
1524
1525 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001526 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001527 }
1528 return count;
1529 }
1530
1531 if (!strcmp(name, "vlan_p")) {
1532 len = num_arg(&user_buffer[i], 1, &value);
1533 if (len < 0) {
1534 return len;
1535 }
1536 i += len;
1537 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1538 pkt_dev->vlan_p = value;
1539 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1540 } else {
1541 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1542 }
1543 return count;
1544 }
1545
1546 if (!strcmp(name, "vlan_cfi")) {
1547 len = num_arg(&user_buffer[i], 1, &value);
1548 if (len < 0) {
1549 return len;
1550 }
1551 i += len;
1552 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1553 pkt_dev->vlan_cfi = value;
1554 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1555 } else {
1556 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1557 }
1558 return count;
1559 }
1560
1561 if (!strcmp(name, "svlan_id")) {
1562 len = num_arg(&user_buffer[i], 4, &value);
1563 if (len < 0) {
1564 return len;
1565 }
1566 i += len;
1567 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1568 pkt_dev->svlan_id = value; /* turn on SVLAN */
1569
1570 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001571 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001572
1573 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001574 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001575
1576 pkt_dev->nr_labels = 0; /* turn off MPLS */
1577 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1578 } else {
1579 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1580 pkt_dev->svlan_id = 0xffff;
1581
1582 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001583 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001584 }
1585 return count;
1586 }
1587
1588 if (!strcmp(name, "svlan_p")) {
1589 len = num_arg(&user_buffer[i], 1, &value);
1590 if (len < 0) {
1591 return len;
1592 }
1593 i += len;
1594 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1595 pkt_dev->svlan_p = value;
1596 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1597 } else {
1598 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1599 }
1600 return count;
1601 }
1602
1603 if (!strcmp(name, "svlan_cfi")) {
1604 len = num_arg(&user_buffer[i], 1, &value);
1605 if (len < 0) {
1606 return len;
1607 }
1608 i += len;
1609 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1610 pkt_dev->svlan_cfi = value;
1611 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1612 } else {
1613 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1614 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001615 return count;
1616 }
1617
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001618 if (!strcmp(name, "tos")) {
1619 __u32 tmp_value = 0;
1620 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1621 if (len < 0) {
1622 return len;
1623 }
1624 i += len;
1625 if (len == 2) {
1626 pkt_dev->tos = tmp_value;
1627 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1628 } else {
1629 sprintf(pg_result, "ERROR: tos must be 00-ff");
1630 }
1631 return count;
1632 }
1633
1634 if (!strcmp(name, "traffic_class")) {
1635 __u32 tmp_value = 0;
1636 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1637 if (len < 0) {
1638 return len;
1639 }
1640 i += len;
1641 if (len == 2) {
1642 pkt_dev->traffic_class = tmp_value;
1643 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1644 } else {
1645 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1646 }
1647 return count;
1648 }
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1651 return -EINVAL;
1652}
1653
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001654static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001656 return single_open(file, pktgen_if_show, PDE(inode)->data);
1657}
1658
Arjan van de Ven9a321442007-02-12 00:55:35 -08001659static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001660 .owner = THIS_MODULE,
1661 .open = pktgen_if_open,
1662 .read = seq_read,
1663 .llseek = seq_lseek,
1664 .write = pktgen_if_write,
1665 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001666};
1667
1668static int pktgen_thread_show(struct seq_file *seq, void *v)
1669{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001670 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001671 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001673 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Luiz Capitulino222f1802006-03-20 22:16:13 -08001675 seq_printf(seq, "Running: ");
1676
1677 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001678 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001679 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001680 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Luiz Capitulino222f1802006-03-20 22:16:13 -08001682 seq_printf(seq, "\nStopped: ");
1683
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001684 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001685 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001686 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 if (t->result[0])
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001689 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 else
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001691 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Luiz Capitulino222f1802006-03-20 22:16:13 -08001693 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001698static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001699 const char __user * user_buffer,
1700 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001702 struct seq_file *seq = (struct seq_file *)file->private_data;
1703 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 int i = 0, max, len, ret;
1705 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001706 char *pg_result;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001709 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return -EINVAL;
1711 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001714 len = count_trail_chars(&user_buffer[i], max);
1715 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001716 return len;
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 i += len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* Read variable name */
1721
1722 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001723 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001724 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 memset(name, 0, sizeof(name));
1727 if (copy_from_user(name, &user_buffer[i], len))
1728 return -EFAULT;
1729 i += len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001730
Luiz Capitulino222f1802006-03-20 22:16:13 -08001731 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001733 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001734 return len;
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 i += len;
1737
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001738 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001739 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1740 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Luiz Capitulino222f1802006-03-20 22:16:13 -08001742 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001743 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 ret = -EINVAL;
1745 goto out;
1746 }
1747
1748 pg_result = &(t->result[0]);
1749
Luiz Capitulino222f1802006-03-20 22:16:13 -08001750 if (!strcmp(name, "add_device")) {
1751 char f[32];
1752 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001754 if (len < 0) {
1755 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 goto out;
1757 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001758 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return -EFAULT;
1760 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001761 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001763 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 ret = count;
1765 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto out;
1767 }
1768
Luiz Capitulino222f1802006-03-20 22:16:13 -08001769 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001770 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001771 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001772 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001775 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto out;
1777 }
1778
Luiz Capitulino222f1802006-03-20 22:16:13 -08001779 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001780 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 goto out;
1783 }
1784
1785 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001786out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return ret;
1788}
1789
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001790static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001792 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Arjan van de Ven9a321442007-02-12 00:55:35 -08001795static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001796 .owner = THIS_MODULE,
1797 .open = pktgen_thread_open,
1798 .read = seq_read,
1799 .llseek = seq_lseek,
1800 .write = pktgen_thread_write,
1801 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001802};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 struct pktgen_thread *t;
1808 struct pktgen_dev *pkt_dev = NULL;
1809
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001810 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 pkt_dev = pktgen_find_dev(t, ifname);
1812 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813 if (remove) {
1814 if_lock(t);
1815 pkt_dev->removal_mark = 1;
1816 t->control |= T_REMDEV;
1817 if_unlock(t);
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 break;
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001822 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001825/*
1826 * mark a device for removal
1827 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001828static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001831 const int max_tries = 10, msec_per_try = 125;
1832 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001833
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001834 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001835 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001836
Luiz Capitulino222f1802006-03-20 22:16:13 -08001837 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001838
1839 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001840 if (pkt_dev == NULL)
1841 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001842
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001843 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001844 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1845 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001846 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001847 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001848
1849 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001850 printk(KERN_ERR "pktgen_mark_device: timed out after "
1851 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001852 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001853 break;
1854 }
1855
1856 }
1857
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001858 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001859}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001860
Stephen Hemminger39df2322007-03-04 16:11:51 -08001861static void pktgen_change_name(struct net_device *dev)
1862{
1863 struct pktgen_thread *t;
1864
1865 list_for_each_entry(t, &pktgen_threads, th_list) {
1866 struct pktgen_dev *pkt_dev;
1867
1868 list_for_each_entry(pkt_dev, &t->if_list, list) {
1869 if (pkt_dev->odev != dev)
1870 continue;
1871
1872 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1873
1874 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1875 pg_proc_dir);
1876 if (!pkt_dev->entry)
1877 printk(KERN_ERR "pktgen: can't move proc "
1878 " entry for '%s'\n", dev->name);
1879 break;
1880 }
1881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Luiz Capitulino222f1802006-03-20 22:16:13 -08001884static int pktgen_device_event(struct notifier_block *unused,
1885 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001887 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001889 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001890 return NOTIFY_DONE;
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /* It is OK that we do not hold the group lock right now,
1893 * as we run under the RTNL lock.
1894 */
1895
1896 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001897 case NETDEV_CHANGENAME:
1898 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001902 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 return NOTIFY_DONE;
1907}
1908
Robert Olssone6fce5b2008-08-07 02:23:01 -07001909static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev, const char *ifname)
1910{
1911 char b[IFNAMSIZ+5];
1912 int i = 0;
1913
1914 for(i=0; ifname[i] != '@'; i++) {
1915 if(i == IFNAMSIZ)
1916 break;
1917
1918 b[i] = ifname[i];
1919 }
1920 b[i] = 0;
1921
1922 return dev_get_by_name(&init_net, b);
1923}
1924
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/* Associate pktgen_dev with a device. */
1927
Stephen Hemminger39df2322007-03-04 16:11:51 -08001928static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001929{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001931 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (pkt_dev->odev) {
1935 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001936 pkt_dev->odev = NULL;
1937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Robert Olssone6fce5b2008-08-07 02:23:01 -07001939 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001941 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001942 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001946 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001947 err = -EINVAL;
1948 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001949 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001950 err = -ENETDOWN;
1951 } else {
1952 pkt_dev->odev = odev;
1953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001957 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960/* Read pkt_dev from the interface and set up internal pktgen_dev
1961 * structure to have the right information to create/send packets
1962 */
1963static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1964{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001965 int ntxq;
1966
Luiz Capitulino222f1802006-03-20 22:16:13 -08001967 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001968 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1969 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001970 sprintf(pkt_dev->result,
1971 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1972 return;
1973 }
1974
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001975 /* make sure that we don't pick a non-existing transmit queue */
1976 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08001977
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001978 if (ntxq <= pkt_dev->queue_map_min) {
1979 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07001980 "queue_map_min (zero-based) (%d) exceeds valid range "
1981 "[0 - %d] for (%d) queues on %s, resetting\n",
1982 pkt_dev->queue_map_min, (ntxq ?: 1)- 1, ntxq,
1983 pkt_dev->odev->name);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001984 pkt_dev->queue_map_min = ntxq - 1;
1985 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07001986 if (pkt_dev->queue_map_max >= ntxq) {
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001987 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07001988 "queue_map_max (zero-based) (%d) exceeds valid range "
1989 "[0 - %d] for (%d) queues on %s, resetting\n",
1990 pkt_dev->queue_map_max, (ntxq ?: 1)- 1, ntxq,
1991 pkt_dev->odev->name);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001992 pkt_dev->queue_map_max = ntxq - 1;
1993 }
1994
Luiz Capitulino222f1802006-03-20 22:16:13 -08001995 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001997 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001998 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Luiz Capitulino222f1802006-03-20 22:16:13 -08002000 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002001 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Luiz Capitulino222f1802006-03-20 22:16:13 -08002003 /* Set up pkt size */
2004 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2005
2006 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002008 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 * gets exported
2010 */
2011
2012#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002013 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct inet6_dev *idev;
2015
Luiz Capitulino222f1802006-03-20 22:16:13 -08002016 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2017 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 set = 1;
2019 break;
2020 }
2021
Luiz Capitulino222f1802006-03-20 22:16:13 -08002022 if (!set) {
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 /*
2025 * Use linklevel address if unconfigured.
2026 *
2027 * use ipv6_get_lladdr if/when it's get exported
2028 */
2029
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002030 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2032 struct inet6_ifaddr *ifp;
2033
2034 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002035 for (ifp = idev->addr_list; ifp;
2036 ifp = ifp->if_next) {
2037 if (ifp->scope == IFA_LINK
2038 && !(ifp->
2039 flags & IFA_F_TENTATIVE)) {
2040 ipv6_addr_copy(&pkt_dev->
2041 cur_in6_saddr,
2042 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 err = 0;
2044 break;
2045 }
2046 }
2047 read_unlock_bh(&idev->lock);
2048 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002049 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002050 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002051 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2052 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002055 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 pkt_dev->saddr_min = 0;
2057 pkt_dev->saddr_max = 0;
2058 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002059
2060 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002063 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (in_dev) {
2065 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002066 pkt_dev->saddr_min =
2067 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 pkt_dev->saddr_max = pkt_dev->saddr_min;
2069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002072 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2074 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2075 }
2076
2077 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2078 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2079 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002080 /* Initialize current values. */
2081 pkt_dev->cur_dst_mac_offset = 0;
2082 pkt_dev->cur_src_mac_offset = 0;
2083 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2084 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2085 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2086 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 pkt_dev->nflows = 0;
2088}
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002091static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2092{
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002093 ktime_t start;
2094 s32 remaining;
2095 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002096
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002097 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2098 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002099
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002100 remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
2101 if (remaining <= 0)
2102 return;
2103
2104 start = ktime_now();
2105 if (remaining < 100)
2106 udelay(remaining); /* really small just spin */
2107 else {
2108 /* see do_nanosleep */
2109 hrtimer_init_sleeper(&t, current);
2110 do {
2111 set_current_state(TASK_INTERRUPTIBLE);
2112 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2113 if (!hrtimer_active(&t.timer))
2114 t.task = NULL;
2115
2116 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002119 hrtimer_cancel(&t.timer);
2120 } while (t.task && pkt_dev->running && !signal_pending(current));
2121 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002123 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002126static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2127{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002128 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002129 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2130 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2131 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2132}
2133
Stephen Hemminger648fda72009-08-27 13:55:07 +00002134static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002135{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002136 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002137}
2138
2139static inline int f_pick(struct pktgen_dev *pkt_dev)
2140{
2141 int flow = pkt_dev->curfl;
2142
2143 if (pkt_dev->flags & F_FLOW_SEQ) {
2144 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2145 /* reset time */
2146 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002147 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002148 pkt_dev->curfl += 1;
2149 if (pkt_dev->curfl >= pkt_dev->cflows)
2150 pkt_dev->curfl = 0; /*reset */
2151 }
2152 } else {
2153 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002154 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002155
Robert Olsson1211a642008-08-05 18:44:26 -07002156 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002157 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002158 pkt_dev->flows[flow].flags = 0;
2159 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002160 }
2161
2162 return pkt_dev->curfl;
2163}
2164
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002165
2166#ifdef CONFIG_XFRM
2167/* If there was already an IPSEC SA, we keep it as is, else
2168 * we go look for it ...
2169*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002170static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002171{
2172 struct xfrm_state *x = pkt_dev->flows[flow].x;
2173 if (!x) {
2174 /*slow path: we dont already have xfrm_state*/
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002175 x = xfrm_stateonly_find(&init_net,
2176 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002177 (xfrm_address_t *)&pkt_dev->cur_saddr,
2178 AF_INET,
2179 pkt_dev->ipsmode,
2180 pkt_dev->ipsproto, 0);
2181 if (x) {
2182 pkt_dev->flows[flow].x = x;
2183 set_pkt_overhead(pkt_dev);
2184 pkt_dev->pkt_overhead+=x->props.header_len;
2185 }
2186
2187 }
2188}
2189#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002190static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2191{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002192
2193 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2194 pkt_dev->cur_queue_map = smp_processor_id();
2195
2196 else if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002197 __u16 t;
2198 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2199 t = random32() %
2200 (pkt_dev->queue_map_max -
2201 pkt_dev->queue_map_min + 1)
2202 + pkt_dev->queue_map_min;
2203 } else {
2204 t = pkt_dev->cur_queue_map + 1;
2205 if (t > pkt_dev->queue_map_max)
2206 t = pkt_dev->queue_map_min;
2207 }
2208 pkt_dev->cur_queue_map = t;
2209 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002210 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 -07002211}
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213/* Increment/randomize headers according to flags and current values
2214 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2215 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002216static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2217{
2218 __u32 imn;
2219 __u32 imx;
2220 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002222 if (pkt_dev->cflows)
2223 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002226 if (pkt_dev->src_mac_count > 1) {
2227 __u32 mc;
2228 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Luiz Capitulino222f1802006-03-20 22:16:13 -08002230 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002231 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002232 else {
2233 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002234 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002235 pkt_dev->src_mac_count)
2236 pkt_dev->cur_src_mac_offset = 0;
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Luiz Capitulino222f1802006-03-20 22:16:13 -08002239 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2240 pkt_dev->hh[11] = tmp;
2241 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2242 pkt_dev->hh[10] = tmp;
2243 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2244 pkt_dev->hh[9] = tmp;
2245 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2246 pkt_dev->hh[8] = tmp;
2247 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2248 pkt_dev->hh[7] = tmp;
2249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Luiz Capitulino222f1802006-03-20 22:16:13 -08002251 /* Deal with Destination MAC */
2252 if (pkt_dev->dst_mac_count > 1) {
2253 __u32 mc;
2254 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Luiz Capitulino222f1802006-03-20 22:16:13 -08002256 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002257 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Luiz Capitulino222f1802006-03-20 22:16:13 -08002259 else {
2260 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002261 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002262 pkt_dev->dst_mac_count) {
2263 pkt_dev->cur_dst_mac_offset = 0;
2264 }
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Luiz Capitulino222f1802006-03-20 22:16:13 -08002267 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2268 pkt_dev->hh[5] = tmp;
2269 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2270 pkt_dev->hh[4] = tmp;
2271 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2272 pkt_dev->hh[3] = tmp;
2273 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2274 pkt_dev->hh[2] = tmp;
2275 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2276 pkt_dev->hh[1] = tmp;
2277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002279 if (pkt_dev->flags & F_MPLS_RND) {
2280 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002281 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002282 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2283 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002284 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002285 htonl(0x000fffff));
2286 }
2287
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002288 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002289 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002290 }
2291
2292 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002293 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002294 }
2295
Luiz Capitulino222f1802006-03-20 22:16:13 -08002296 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2297 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002298 pkt_dev->cur_udp_src = random32() %
2299 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2300 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Luiz Capitulino222f1802006-03-20 22:16:13 -08002302 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 pkt_dev->cur_udp_src++;
2304 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2305 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002306 }
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Luiz Capitulino222f1802006-03-20 22:16:13 -08002309 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2310 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002311 pkt_dev->cur_udp_dst = random32() %
2312 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2313 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002314 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002318 }
2319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 if (!(pkt_dev->flags & F_IPV6)) {
2322
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2324 ntohl(pkt_dev->
2325 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002327 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002328 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 else {
2330 t = ntohl(pkt_dev->cur_saddr);
2331 t++;
2332 if (t > imx) {
2333 t = imn;
2334 }
2335 }
2336 pkt_dev->cur_saddr = htonl(t);
2337 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002338
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002339 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2341 } else {
Al Viro252e3342006-11-14 20:48:11 -08002342 imn = ntohl(pkt_dev->daddr_min);
2343 imx = ntohl(pkt_dev->daddr_max);
2344 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002346 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (pkt_dev->flags & F_IPDST_RND) {
2348
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002349 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002350 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Joe Perches21cf2252007-12-16 13:44:00 -08002352 while (ipv4_is_loopback(s) ||
2353 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002354 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002355 ipv4_is_zeronet(s) ||
2356 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002357 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002358 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
Al Viro252e3342006-11-14 20:48:11 -08002360 pkt_dev->cur_daddr = s;
2361 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 t = ntohl(pkt_dev->cur_daddr);
2363 t++;
2364 if (t > imx) {
2365 t = imn;
2366 }
2367 pkt_dev->cur_daddr = htonl(t);
2368 }
2369 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002370 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002371 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002372 pkt_dev->flows[flow].cur_daddr =
2373 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002374#ifdef CONFIG_XFRM
2375 if (pkt_dev->flags & F_IPSEC_ON)
2376 get_ipsec_sa(pkt_dev, flow);
2377#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 pkt_dev->nflows++;
2379 }
2380 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002381 } else { /* IPV6 * */
2382
2383 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2384 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2385 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2386 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 else {
2388 int i;
2389
2390 /* Only random destinations yet */
2391
Luiz Capitulino222f1802006-03-20 22:16:13 -08002392 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002394 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002395 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2396 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400
Luiz Capitulino222f1802006-03-20 22:16:13 -08002401 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2402 __u32 t;
2403 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002404 t = random32() %
2405 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2406 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002407 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002409 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002411 }
2412 pkt_dev->cur_pkt_size = t;
2413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002415 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 pkt_dev->flows[flow].count++;
2418}
2419
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002420
2421#ifdef CONFIG_XFRM
2422static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2423{
2424 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2425 int err = 0;
2426 struct iphdr *iph;
2427
2428 if (!x)
2429 return 0;
2430 /* XXX: we dont support tunnel mode for now until
2431 * we resolve the dst issue */
2432 if (x->props.mode != XFRM_MODE_TRANSPORT)
2433 return 0;
2434
2435 spin_lock(&x->lock);
2436 iph = ip_hdr(skb);
2437
Herbert Xu13996372007-10-17 21:35:51 -07002438 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002439 if (err)
2440 goto error;
2441 err = x->type->output(x, skb);
2442 if (err)
2443 goto error;
2444
2445 x->curlft.bytes +=skb->len;
2446 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002447error:
2448 spin_unlock(&x->lock);
2449 return err;
2450}
2451
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002452static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002453{
2454 if (pkt_dev->cflows) {
2455 /* let go of the SAs if we have them */
2456 int i = 0;
Florian Westphal5b5f7922009-05-21 15:07:12 -07002457 for (; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002458 struct xfrm_state *x = pkt_dev->flows[i].x;
2459 if (x) {
2460 xfrm_state_put(x);
2461 pkt_dev->flows[i].x = NULL;
2462 }
2463 }
2464 }
2465}
2466
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002467static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002468 struct sk_buff *skb, __be16 protocol)
2469{
2470 if (pkt_dev->flags & F_IPSEC_ON) {
2471 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2472 int nhead = 0;
2473 if (x) {
2474 int ret;
2475 __u8 *eth;
2476 nhead = x->props.header_len - skb_headroom(skb);
2477 if (nhead >0) {
2478 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2479 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002480 printk(KERN_ERR "Error expanding "
2481 "ipsec packet %d\n",ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002482 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002483 }
2484 }
2485
2486 /* ipsec is not expecting ll header */
2487 skb_pull(skb, ETH_HLEN);
2488 ret = pktgen_output_ipsec(skb, pkt_dev);
2489 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002490 printk(KERN_ERR "Error creating ipsec "
2491 "packet %d\n",ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002492 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002493 }
2494 /* restore ll */
2495 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2496 memcpy(eth, pkt_dev->hh, 12);
2497 *(u16 *) & eth[12] = protocol;
2498 }
2499 }
2500 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002501err:
2502 kfree_skb(skb);
2503 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002504}
2505#endif
2506
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002507static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2508{
2509 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002510 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002511 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2512 }
2513 mpls--;
2514 *mpls |= MPLS_STACK_BOTTOM;
2515}
2516
Al Viro0f37c602006-11-03 03:49:56 -08002517static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2518 unsigned int prio)
2519{
2520 return htons(id | (cfi << 12) | (prio << 13));
2521}
2522
Luiz Capitulino222f1802006-03-20 22:16:13 -08002523static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2524 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 struct sk_buff *skb = NULL;
2527 __u8 *eth;
2528 struct udphdr *udph;
2529 int datalen, iplen;
2530 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002531 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002532 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002533 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002534 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2535 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2536 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2537 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002538 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002539
2540 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002541 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002542
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002543 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002544 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002545
Robert Olsson64053be2005-06-26 15:27:10 -07002546 /* Update any of the values, used when we're incrementing various
2547 * fields.
2548 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002549 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002550 mod_cur_headers(pkt_dev);
2551
David S. Miller7ac54592006-01-18 14:19:10 -08002552 datalen = (odev->hard_header_len + 16) & ~0xf;
Stephen Hemmingere4707572009-08-27 13:55:13 +00002553 skb = __netdev_alloc_skb(odev,
2554 pkt_dev->cur_pkt_size + 64
2555 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (!skb) {
2557 sprintf(pkt_dev->result, "No memory");
2558 return NULL;
2559 }
2560
David S. Miller7ac54592006-01-18 14:19:10 -08002561 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 /* Reserve for ethernet and IP header */
2564 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002565 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2566 if (pkt_dev->nr_labels)
2567 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002568
2569 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002570 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002571 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002572 *svlan_tci = build_tci(pkt_dev->svlan_id,
2573 pkt_dev->svlan_cfi,
2574 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002575 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002576 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002577 }
2578 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002579 *vlan_tci = build_tci(pkt_dev->vlan_id,
2580 pkt_dev->vlan_cfi,
2581 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002582 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002583 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002584 }
2585
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002586 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002587 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002588 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002589 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002590 iph = ip_hdr(skb);
2591 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002594 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002596 /* Eth + IPh + UDPh + mpls */
2597 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002598 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002599 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 udph->source = htons(pkt_dev->cur_udp_src);
2603 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002604 udph->len = htons(datalen + 8); /* DATA + udphdr */
2605 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 iph->ihl = 5;
2608 iph->version = 4;
2609 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002610 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002611 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 iph->saddr = pkt_dev->cur_saddr;
2613 iph->daddr = pkt_dev->cur_daddr;
2614 iph->frag_off = 0;
2615 iplen = 20 + 8 + datalen;
2616 iph->tot_len = htons(iplen);
2617 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002618 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002619 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002620 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002621 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 skb->dev = odev;
2623 skb->pkt_type = PACKET_HOST;
2624
Luiz Capitulino222f1802006-03-20 22:16:13 -08002625 if (pkt_dev->nfrags <= 0)
2626 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 else {
2628 int frags = pkt_dev->nfrags;
2629 int i;
2630
Luiz Capitulino222f1802006-03-20 22:16:13 -08002631 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (frags > MAX_SKB_FRAGS)
2634 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002635 if (datalen > frags * PAGE_SIZE) {
2636 skb_put(skb, datalen - frags * PAGE_SIZE);
2637 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639
2640 i = 0;
2641 while (datalen > 0) {
2642 struct page *page = alloc_pages(GFP_KERNEL, 0);
2643 skb_shinfo(skb)->frags[i].page = page;
2644 skb_shinfo(skb)->frags[i].page_offset = 0;
2645 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002646 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 datalen -= skb_shinfo(skb)->frags[i].size;
2648 skb->len += skb_shinfo(skb)->frags[i].size;
2649 skb->data_len += skb_shinfo(skb)->frags[i].size;
2650 i++;
2651 skb_shinfo(skb)->nr_frags = i;
2652 }
2653
2654 while (i < frags) {
2655 int rem;
2656
2657 if (i == 0)
2658 break;
2659
2660 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2661 if (rem == 0)
2662 break;
2663
2664 skb_shinfo(skb)->frags[i - 1].size -= rem;
2665
Luiz Capitulino222f1802006-03-20 22:16:13 -08002666 skb_shinfo(skb)->frags[i] =
2667 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002669 skb_shinfo(skb)->frags[i].page =
2670 skb_shinfo(skb)->frags[i - 1].page;
2671 skb_shinfo(skb)->frags[i].page_offset +=
2672 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 skb_shinfo(skb)->frags[i].size = rem;
2674 i++;
2675 skb_shinfo(skb)->nr_frags = i;
2676 }
2677 }
2678
Luiz Capitulino222f1802006-03-20 22:16:13 -08002679 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Luiz Capitulino222f1802006-03-20 22:16:13 -08002681 if (pgh) {
2682 struct timeval timestamp;
2683
2684 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2685 pgh->seq_num = htonl(pkt_dev->seq_num);
2686
2687 do_gettimeofday(&timestamp);
2688 pgh->tv_sec = htonl(timestamp.tv_sec);
2689 pgh->tv_usec = htonl(timestamp.tv_usec);
2690 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002691
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002692#ifdef CONFIG_XFRM
2693 if (!process_ipsec(pkt_dev, skb, protocol))
2694 return NULL;
2695#endif
2696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return skb;
2698}
2699
2700/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002701 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2703 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002704 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 * Should be candidate for net/ipv4/utils.c
2706 * --ro
2707 */
2708
Luiz Capitulino222f1802006-03-20 22:16:13 -08002709static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002712 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 unsigned long u;
2714 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002715 unsigned int prefixlen = 0;
2716 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002717 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002718 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Luiz Capitulino222f1802006-03-20 22:16:13 -08002720 for (i = 0; i < 16; i++)
2721 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 for (;;) {
2724 if (*s == ':') {
2725 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002726 if (s[1] == ':') { /* Found "::", skip to part 2 */
2727 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 len++;
2729 break;
2730 }
2731 s++;
2732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002734 u = simple_strtoul(s, &pos, 16);
2735 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002736 if (!i)
2737 return 0;
2738 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 /* the last 4 bytes may be written as IPv4 address */
2741
2742 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002743 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2744 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
2746 ip[prefixlen++] = (u >> 8);
2747 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002748 s += i;
2749 len += i;
2750 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return len;
2752 }
2753
2754/* part 2, after "::" */
2755 for (;;) {
2756 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002757 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 break;
2759 s++;
2760 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002761 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002763
2764 u = simple_strtol(s, &pos, 16);
2765 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002767 if (*s)
2768 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 break;
2770 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002771 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002773 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2774 sizeof(tmp));
2775 suffixlen += 4;
2776 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 break;
2778 }
2779 suffix[suffixlen++] = (u >> 8);
2780 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002781 s += i;
2782 len += i;
2783 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 break;
2785 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002786 for (i = 0; i < suffixlen; i++)
2787 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return len;
2789}
2790
Luiz Capitulino222f1802006-03-20 22:16:13 -08002791static char tohex(char hexdigit)
2792{
2793 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
Luiz Capitulino222f1802006-03-20 22:16:13 -08002796static int fmt_xlong(char *s, unsigned int i)
2797{
2798 char *bak = s;
2799 *s = tohex((i >> 12) & 0xf);
2800 if (s != bak || *s != '0')
2801 ++s;
2802 *s = tohex((i >> 8) & 0xf);
2803 if (s != bak || *s != '0')
2804 ++s;
2805 *s = tohex((i >> 4) & 0xf);
2806 if (s != bak || *s != '0')
2807 ++s;
2808 *s = tohex(i & 0xf);
2809 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810}
2811
Luiz Capitulino222f1802006-03-20 22:16:13 -08002812static unsigned int fmt_ip6(char *s, const char ip[16])
2813{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 unsigned int len;
2815 unsigned int i;
2816 unsigned int temp;
2817 unsigned int compressing;
2818 int j;
2819
Luiz Capitulino222f1802006-03-20 22:16:13 -08002820 len = 0;
2821 compressing = 0;
2822 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002825 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2826 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2827 temp = strlen(s);
2828 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002831 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2832 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (temp == 0) {
2834 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002835 compressing = 1;
2836 if (j == 0) {
2837 *s++ = ':';
2838 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
2840 }
2841 } else {
2842 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002843 compressing = 0;
2844 *s++ = ':';
2845 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002847 i = fmt_xlong(s, temp);
2848 len += i;
2849 s += i;
2850 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 *s++ = ':';
2852 ++len;
2853 }
2854 }
2855 }
2856 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002857 *s++ = ':';
2858 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002860 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return len;
2862}
2863
Luiz Capitulino222f1802006-03-20 22:16:13 -08002864static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2865 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 struct sk_buff *skb = NULL;
2868 __u8 *eth;
2869 struct udphdr *udph;
2870 int datalen;
2871 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002872 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002873 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002874 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002875 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2876 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2877 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2878 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002879 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002880
2881 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002882 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002883
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002884 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002885 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002886
Robert Olsson64053be2005-06-26 15:27:10 -07002887 /* Update any of the values, used when we're incrementing various
2888 * fields.
2889 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002890 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002891 mod_cur_headers(pkt_dev);
2892
Stephen Hemmingere4707572009-08-27 13:55:13 +00002893 skb = __netdev_alloc_skb(odev,
2894 pkt_dev->cur_pkt_size + 64
2895 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (!skb) {
2897 sprintf(pkt_dev->result, "No memory");
2898 return NULL;
2899 }
2900
2901 skb_reserve(skb, 16);
2902
2903 /* Reserve for ethernet and IP header */
2904 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002905 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2906 if (pkt_dev->nr_labels)
2907 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002908
2909 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002910 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002911 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002912 *svlan_tci = build_tci(pkt_dev->svlan_id,
2913 pkt_dev->svlan_cfi,
2914 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002915 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002916 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002917 }
2918 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002919 *vlan_tci = build_tci(pkt_dev->vlan_id,
2920 pkt_dev->vlan_cfi,
2921 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002922 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002923 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002924 }
2925
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002926 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002927 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002928 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002929 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002930 iph = ipv6_hdr(skb);
2931 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002934 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002935
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002936 /* Eth + IPh + UDPh + mpls */
2937 datalen = pkt_dev->cur_pkt_size - 14 -
2938 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002939 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Luiz Capitulino222f1802006-03-20 22:16:13 -08002941 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 datalen = sizeof(struct pktgen_hdr);
2943 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002944 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2945 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
2947
2948 udph->source = htons(pkt_dev->cur_udp_src);
2949 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002950 udph->len = htons(datalen + sizeof(struct udphdr));
2951 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002953 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002955 if (pkt_dev->traffic_class) {
2956 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002957 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002958 }
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 iph->hop_limit = 32;
2961
2962 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2963 iph->nexthdr = IPPROTO_UDP;
2964
2965 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2966 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2967
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002968 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002969 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002970 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 skb->dev = odev;
2972 skb->pkt_type = PACKET_HOST;
2973
Luiz Capitulino222f1802006-03-20 22:16:13 -08002974 if (pkt_dev->nfrags <= 0)
2975 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 else {
2977 int frags = pkt_dev->nfrags;
2978 int i;
2979
Luiz Capitulino222f1802006-03-20 22:16:13 -08002980 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (frags > MAX_SKB_FRAGS)
2983 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002984 if (datalen > frags * PAGE_SIZE) {
2985 skb_put(skb, datalen - frags * PAGE_SIZE);
2986 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
2988
2989 i = 0;
2990 while (datalen > 0) {
2991 struct page *page = alloc_pages(GFP_KERNEL, 0);
2992 skb_shinfo(skb)->frags[i].page = page;
2993 skb_shinfo(skb)->frags[i].page_offset = 0;
2994 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002995 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 datalen -= skb_shinfo(skb)->frags[i].size;
2997 skb->len += skb_shinfo(skb)->frags[i].size;
2998 skb->data_len += skb_shinfo(skb)->frags[i].size;
2999 i++;
3000 skb_shinfo(skb)->nr_frags = i;
3001 }
3002
3003 while (i < frags) {
3004 int rem;
3005
3006 if (i == 0)
3007 break;
3008
3009 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3010 if (rem == 0)
3011 break;
3012
3013 skb_shinfo(skb)->frags[i - 1].size -= rem;
3014
Luiz Capitulino222f1802006-03-20 22:16:13 -08003015 skb_shinfo(skb)->frags[i] =
3016 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003018 skb_shinfo(skb)->frags[i].page =
3019 skb_shinfo(skb)->frags[i - 1].page;
3020 skb_shinfo(skb)->frags[i].page_offset +=
3021 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 skb_shinfo(skb)->frags[i].size = rem;
3023 i++;
3024 skb_shinfo(skb)->nr_frags = i;
3025 }
3026 }
3027
Luiz Capitulino222f1802006-03-20 22:16:13 -08003028 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003030 if (pgh) {
3031 struct timeval timestamp;
3032
3033 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3034 pgh->seq_num = htonl(pkt_dev->seq_num);
3035
3036 do_gettimeofday(&timestamp);
3037 pgh->tv_sec = htonl(timestamp.tv_sec);
3038 pgh->tv_usec = htonl(timestamp.tv_usec);
3039 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003040 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return skb;
3043}
3044
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003045static struct sk_buff *fill_packet(struct net_device *odev,
3046 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return fill_packet_ipv6(odev, pkt_dev);
3050 else
3051 return fill_packet_ipv4(odev, pkt_dev);
3052}
3053
Luiz Capitulino222f1802006-03-20 22:16:13 -08003054static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003056 pkt_dev->seq_num = 1;
3057 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003059 pkt_dev->tx_bytes = 0;
3060 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
3063/* Set up structure for sending pkts, clear counters */
3064
3065static void pktgen_run(struct pktgen_thread *t)
3066{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003067 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 int started = 0;
3069
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003070 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003073 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075 /*
3076 * setup odev and create initial packet.
3077 */
3078 pktgen_setup_inject(pkt_dev);
3079
Luiz Capitulino222f1802006-03-20 22:16:13 -08003080 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003082 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003084 pkt_dev->started_at =
3085 pkt_dev->next_tx = ktime_now();
3086
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003087 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 strcpy(pkt_dev->result, "Starting");
3090 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003091 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 strcpy(pkt_dev->result, "Error starting");
3093 }
3094 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003095 if (started)
3096 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
3099static void pktgen_stop_all_threads_ifs(void)
3100{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003101 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003103 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003105 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003106
3107 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003108 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003109
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003110 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Stephen Hemminger648fda72009-08-27 13:55:07 +00003113static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003115 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003117 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003118 if (pkt_dev->running)
3119 return 1;
3120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003125 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Luiz Capitulino222f1802006-03-20 22:16:13 -08003127 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Luiz Capitulino222f1802006-03-20 22:16:13 -08003129 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Luiz Capitulino222f1802006-03-20 22:16:13 -08003131 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Luiz Capitulino222f1802006-03-20 22:16:13 -08003133 if (signal_pending(current))
3134 goto signal;
3135 if_lock(t);
3136 }
3137 if_unlock(t);
3138 return 1;
3139signal:
3140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
3143static int pktgen_wait_all_threads_run(void)
3144{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003145 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003147
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003148 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003149
3150 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152 if (sig == 0)
3153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003155
3156 if (sig == 0)
3157 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003159
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003160 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return sig;
3162}
3163
3164static void pktgen_run_all_threads(void)
3165{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003166 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003168 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003170 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003172 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003174
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003175 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Luiz Capitulino222f1802006-03-20 22:16:13 -08003177 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 pktgen_wait_all_threads_run();
3180}
3181
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003182static void pktgen_reset_all_threads(void)
3183{
3184 struct pktgen_thread *t;
3185
3186 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3187
3188 mutex_lock(&pktgen_thread_lock);
3189
3190 list_for_each_entry(t, &pktgen_threads, th_list)
3191 t->control |= (T_REMDEVALL);
3192
3193 mutex_unlock(&pktgen_thread_lock);
3194
3195 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3196
3197 pktgen_wait_all_threads_run();
3198}
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3201{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003202 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003203 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003204 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3205 pkt_dev->started_at);
3206 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003208 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3209 (unsigned long long)ktime_to_us(elapsed),
3210 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3211 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003212 (unsigned long long)pkt_dev->sofar,
3213 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003215 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3216 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Luiz Capitulino222f1802006-03-20 22:16:13 -08003218 bps = pps * 8 * pkt_dev->cur_pkt_size;
3219
3220 mbps = bps;
3221 do_div(mbps, 1000000);
3222 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3223 (unsigned long long)pps,
3224 (unsigned long long)mbps,
3225 (unsigned long long)bps,
3226 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003230static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003232 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003233
Luiz Capitulino222f1802006-03-20 22:16:13 -08003234 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003235 printk(KERN_WARNING "pktgen: interface: %s is already "
3236 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237 return -EINVAL;
3238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003240 kfree_skb(pkt_dev->skb);
3241 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003242 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003245 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Luiz Capitulino222f1802006-03-20 22:16:13 -08003247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Luiz Capitulino222f1802006-03-20 22:16:13 -08003250static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003252 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if_lock(t);
3255
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003256 list_for_each_entry(pkt_dev, &t->if_list, list) {
3257 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003258 continue;
3259 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003260 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003261 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003262 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
3264 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003265 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Luiz Capitulino222f1802006-03-20 22:16:13 -08003268static void pktgen_stop(struct pktgen_thread *t)
3269{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003270 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003272 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Luiz Capitulino222f1802006-03-20 22:16:13 -08003274 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003276 list_for_each_entry(pkt_dev, &t->if_list, list) {
3277 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Luiz Capitulino222f1802006-03-20 22:16:13 -08003280 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003283/*
3284 * one of our devices needs to be removed - find it
3285 * and remove it
3286 */
3287static void pktgen_rem_one_if(struct pktgen_thread *t)
3288{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003289 struct list_head *q, *n;
3290 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003291
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003292 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003293
3294 if_lock(t);
3295
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003296 list_for_each_safe(q, n, &t->if_list) {
3297 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003298
Luiz Capitulino222f1802006-03-20 22:16:13 -08003299 if (!cur->removal_mark)
3300 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003301
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003302 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003303 cur->skb = NULL;
3304
3305 pktgen_remove_device(t, cur);
3306
3307 break;
3308 }
3309
3310 if_unlock(t);
3311}
3312
Luiz Capitulino222f1802006-03-20 22:16:13 -08003313static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003315 struct list_head *q, *n;
3316 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003317
3318 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003319
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003320 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003321 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003323 list_for_each_safe(q, n, &t->if_list) {
3324 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003325
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003326 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003327 cur->skb = NULL;
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 pktgen_remove_device(t, cur);
3330 }
3331
Luiz Capitulino222f1802006-03-20 22:16:13 -08003332 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
Luiz Capitulino222f1802006-03-20 22:16:13 -08003335static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003337 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
David S. Milleree74baa2007-01-01 20:51:53 -08003339 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003341 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003343 list_del(&t->th_list);
3344
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003345 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003348static void idle(struct pktgen_dev *pkt_dev)
3349{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003350 ktime_t idle_start = ktime_now();
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003351
3352 if (need_resched())
3353 schedule();
3354 else
3355 cpu_relax();
3356
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003357 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003358}
3359
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003360
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003361static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Stephen Hemminger00829822008-11-20 20:14:53 -08003363 struct net_device *odev = pkt_dev->odev;
3364 int (*xmit)(struct sk_buff *, struct net_device *)
3365 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003366 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003367 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 int ret;
3369
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003370 if (pkt_dev->delay) {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07003371 spin(pkt_dev, pkt_dev->next_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 /* This is max DELAY, this has special meaning of
3374 * "never transmit"
3375 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003376 if (pkt_dev->delay == ULLONG_MAX) {
3377 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003378 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
3380 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003381
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003382 if (!pkt_dev->skb) {
3383 set_cur_queue_map(pkt_dev);
3384 queue_map = pkt_dev->cur_queue_map;
3385 } else {
3386 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3387 }
3388
3389 txq = netdev_get_tx_queue(odev, queue_map);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003390 /* Did we saturate the queue already? */
3391 if (netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)) {
3392 /* If device is down, then all queues are permnantly frozen */
3393 if (netif_running(odev))
3394 idle(pkt_dev);
3395 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003397 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003400 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3401 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3402 /* build a new pkt */
3403 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003404
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003405 pkt_dev->skb = fill_packet(odev, pkt_dev);
3406 if (pkt_dev->skb == NULL) {
3407 printk(KERN_ERR "pktgen: ERROR: couldn't "
3408 "allocate skb in fill_packet.\n");
3409 schedule();
3410 pkt_dev->clone_count--; /* back out increment, OOM */
3411 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003413
3414 pkt_dev->allocated_skbs++;
3415 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003417
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003418 /* fill_packet() might have changed the queue */
3419 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3420 txq = netdev_get_tx_queue(odev, queue_map);
3421
3422 __netif_tx_lock_bh(txq);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003423 if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)))
3424 pkt_dev->last_ok = 0;
3425 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 atomic_inc(&(pkt_dev->skb->users));
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003427
3428 retry_now:
Stephen Hemminger00829822008-11-20 20:14:53 -08003429 ret = (*xmit)(pkt_dev->skb, odev);
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003430 switch (ret) {
3431 case NETDEV_TX_OK:
Eric Dumazet08baf562009-05-25 22:58:01 -07003432 txq_trans_update(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003433 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 pkt_dev->sofar++;
3435 pkt_dev->seq_num++;
3436 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003437 break;
3438 case NETDEV_TX_LOCKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 cpu_relax();
3440 goto retry_now;
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003441 default: /* Drivers are not supposed to return other values! */
3442 if (net_ratelimit())
3443 pr_info("pktgen: %s xmit error: %d\n",
3444 odev->name, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 pkt_dev->errors++;
Stephen Hemminger5b8db2f2009-08-27 13:55:14 +00003446 /* fallthru */
3447 case NETDEV_TX_BUSY:
3448 /* Retry it next time */
3449 atomic_dec(&(pkt_dev->skb->users));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 pkt_dev->last_ok = 0;
3451 }
3452
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003453 if (pkt_dev->delay)
3454 pkt_dev->next_tx = ktime_add_ns(ktime_now(),
3455 pkt_dev->delay);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003456 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003457 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 /* If pkt_dev->count is zero, then run forever */
3460 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3461 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003462 ktime_t idle_start = ktime_now();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3464 if (signal_pending(current)) {
3465 break;
3466 }
3467 schedule();
3468 }
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003469 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(),
3470 idle_start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 /* Done with this */
3474 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003475 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 * Main loop of the thread goes here
3480 */
3481
David S. Milleree74baa2007-01-01 20:51:53 -08003482static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
3484 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003485 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003486 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
David S. Milleree74baa2007-01-01 20:51:53 -08003489 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
3491 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003492 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003494 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
David S. Milleree74baa2007-01-01 20:51:53 -08003496 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003497
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003498 set_freezable();
3499
David S. Milleree74baa2007-01-01 20:51:53 -08003500 while (!kthread_should_stop()) {
3501 pkt_dev = next_to_run(t);
3502
3503 if (!pkt_dev &&
3504 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3505 == 0) {
3506 prepare_to_wait(&(t->queue), &wait,
3507 TASK_INTERRUPTIBLE);
3508 schedule_timeout(HZ / 10);
3509 finish_wait(&(t->queue), &wait);
3510 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 __set_current_state(TASK_RUNNING);
3513
Robert Olssonb1639112007-08-28 15:46:58 -07003514 if (pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 pktgen_xmit(pkt_dev);
3516
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 pktgen_stop(t);
3519 t->control &= ~(T_STOP);
3520 }
3521
Luiz Capitulino222f1802006-03-20 22:16:13 -08003522 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 pktgen_run(t);
3524 t->control &= ~(T_RUN);
3525 }
3526
Luiz Capitulino222f1802006-03-20 22:16:13 -08003527 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003529 t->control &= ~(T_REMDEVALL);
3530 }
3531
Luiz Capitulino222f1802006-03-20 22:16:13 -08003532 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003533 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 t->control &= ~(T_REMDEV);
3535 }
3536
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003537 try_to_freeze();
3538
David S. Milleree74baa2007-01-01 20:51:53 -08003539 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003542 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003543 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003545 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003546 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003548 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003549 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003550
David S. Milleree74baa2007-01-01 20:51:53 -08003551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
3553
Luiz Capitulino222f1802006-03-20 22:16:13 -08003554static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3555 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003557 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003558 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003560 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003561 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003562 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003563 break;
3564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003567 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003568 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569}
3570
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003571/*
3572 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 */
3574
Luiz Capitulino222f1802006-03-20 22:16:13 -08003575static int add_dev_to_thread(struct pktgen_thread *t,
3576 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
3578 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Luiz Capitulino222f1802006-03-20 22:16:13 -08003580 if_lock(t);
3581
3582 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003583 printk(KERN_ERR "pktgen: ERROR: already assigned "
3584 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003585 rv = -EBUSY;
3586 goto out;
3587 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003588
3589 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003590 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 pkt_dev->running = 0;
3592
Luiz Capitulino222f1802006-03-20 22:16:13 -08003593out:
3594 if_unlock(t);
3595 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596}
3597
3598/* Called under thread lock */
3599
Luiz Capitulino222f1802006-03-20 22:16:13 -08003600static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003602 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003603 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 /* We don't allow a device to be on several threads */
3606
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003607 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3608 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003609 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003610 return -EBUSY;
3611 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003612
3613 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3614 if (!pkt_dev)
3615 return -ENOMEM;
3616
Luiz Capitulino222f1802006-03-20 22:16:13 -08003617 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003618 if (pkt_dev->flows == NULL) {
3619 kfree(pkt_dev);
3620 return -ENOMEM;
3621 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003622 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003623
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003624 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003625 pkt_dev->min_pkt_size = ETH_ZLEN;
3626 pkt_dev->max_pkt_size = ETH_ZLEN;
3627 pkt_dev->nfrags = 0;
3628 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003629 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003630 pkt_dev->count = pg_count_d;
3631 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003632 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003633 pkt_dev->udp_src_max = 9;
3634 pkt_dev->udp_dst_min = 9;
3635 pkt_dev->udp_dst_max = 9;
3636
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003637 pkt_dev->vlan_p = 0;
3638 pkt_dev->vlan_cfi = 0;
3639 pkt_dev->vlan_id = 0xffff;
3640 pkt_dev->svlan_p = 0;
3641 pkt_dev->svlan_cfi = 0;
3642 pkt_dev->svlan_id = 0xffff;
3643
Stephen Hemminger39df2322007-03-04 16:11:51 -08003644 err = pktgen_setup_dev(pkt_dev, ifname);
3645 if (err)
3646 goto out1;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003647
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003648 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3649 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003650 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003651 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003652 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003653 err = -EINVAL;
3654 goto out2;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003655 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003656#ifdef CONFIG_XFRM
3657 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3658 pkt_dev->ipsproto = IPPROTO_ESP;
3659#endif
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003660
3661 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003662out2:
3663 dev_put(pkt_dev->odev);
3664out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003665#ifdef CONFIG_XFRM
3666 free_SAs(pkt_dev);
3667#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003668 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003669 kfree(pkt_dev);
3670 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
David S. Milleree74baa2007-01-01 20:51:53 -08003673static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003675 struct pktgen_thread *t;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003676 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003677 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Luiz Capitulino222f1802006-03-20 22:16:13 -08003679 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3680 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003681 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3682 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003683 return -ENOMEM;
3684 }
3685
Luiz Capitulino222f1802006-03-20 22:16:13 -08003686 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003688
David S. Milleree74baa2007-01-01 20:51:53 -08003689 INIT_LIST_HEAD(&t->if_list);
3690
3691 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003692 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003693
3694 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3695 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003696 printk(KERN_ERR "pktgen: kernel_thread() failed "
3697 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003698 list_del(&t->th_list);
3699 kfree(t);
3700 return PTR_ERR(p);
3701 }
3702 kthread_bind(p, cpu);
3703 t->tsk = p;
3704
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003705 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3706 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003707 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003708 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003709 PG_PROC_DIR, t->tsk->comm);
3710 kthread_stop(p);
3711 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003712 kfree(t);
3713 return -EINVAL;
3714 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003715
David S. Milleree74baa2007-01-01 20:51:53 -08003716 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003717 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719 return 0;
3720}
3721
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003722/*
3723 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003725static void _rem_dev_from_if_list(struct pktgen_thread *t,
3726 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003728 struct list_head *q, *n;
3729 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003731 list_for_each_safe(q, n, &t->if_list) {
3732 p = list_entry(q, struct pktgen_dev, list);
3733 if (p == pkt_dev)
3734 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 }
3736}
3737
Luiz Capitulino222f1802006-03-20 22:16:13 -08003738static int pktgen_remove_device(struct pktgen_thread *t,
3739 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
3741
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003742 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Luiz Capitulino222f1802006-03-20 22:16:13 -08003744 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003745 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3746 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003747 pktgen_stop_device(pkt_dev);
3748 }
3749
3750 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 if (pkt_dev->odev) {
3753 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003754 pkt_dev->odev = NULL;
3755 }
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 /* And update the thread if_list */
3758
3759 _rem_dev_from_if_list(t, pkt_dev);
3760
Stephen Hemminger39df2322007-03-04 16:11:51 -08003761 if (pkt_dev->entry)
3762 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003764#ifdef CONFIG_XFRM
3765 free_SAs(pkt_dev);
3766#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003767 vfree(pkt_dev->flows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}
3771
Luiz Capitulino222f1802006-03-20 22:16:13 -08003772static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
3774 int cpu;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003775 struct proc_dir_entry *pe;
3776
David S. Miller25a8b252007-07-30 16:11:48 -07003777 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003779 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003780 if (!pg_proc_dir)
3781 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Wang Chen25296d52008-02-28 14:11:49 -08003783 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003784 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003785 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3786 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003787 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003788 return -EINVAL;
3789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 /* Register us to receive netdevice events */
3792 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003793
John Hawkes670c02c2005-10-13 09:30:31 -07003794 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003795 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
David S. Milleree74baa2007-01-01 20:51:53 -08003797 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003798 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003799 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3800 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003801 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003802
3803 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003804 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3805 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003806 unregister_netdevice_notifier(&pktgen_notifier_block);
3807 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003808 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003809 return -ENODEV;
3810 }
3811
Luiz Capitulino222f1802006-03-20 22:16:13 -08003812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813}
3814
3815static void __exit pg_cleanup(void)
3816{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003817 struct pktgen_thread *t;
3818 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 wait_queue_head_t queue;
3820 init_waitqueue_head(&queue);
3821
Luiz Capitulino222f1802006-03-20 22:16:13 -08003822 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003824 list_for_each_safe(q, n, &pktgen_threads) {
3825 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003826 kthread_stop(t->tsk);
3827 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Luiz Capitulino222f1802006-03-20 22:16:13 -08003830 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 unregister_netdevice_notifier(&pktgen_notifier_block);
3832
Luiz Capitulino222f1802006-03-20 22:16:13 -08003833 /* Clean up proc file system */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003834 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003835 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
3837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838module_init(pg_init);
3839module_exit(pg_cleanup);
3840
3841MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3842MODULE_DESCRIPTION("Packet Generator tool");
3843MODULE_LICENSE("GPL");
3844module_param(pg_count_d, int, 0);
3845module_param(pg_delay_d, int, 0);
3846module_param(pg_clone_skb_d, int, 0);
3847module_param(debug, int, 0);