blob: 8997e912aaaf4854b4f3e027e3b2ea496b9d1de3 [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>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700134#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#include <linux/delay.h>
136#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800137#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#include <linux/init.h>
139#include <linux/skbuff.h>
140#include <linux/netdevice.h>
141#include <linux/inet.h>
142#include <linux/inetdevice.h>
143#include <linux/rtnetlink.h>
144#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700145#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#include <linux/in.h>
147#include <linux/ip.h>
148#include <linux/ipv6.h>
149#include <linux/udp.h>
150#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700151#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800153#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800154#include <linux/kthread.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200155#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#include <net/checksum.h>
157#include <net/ipv6.h>
158#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700159#ifdef CONFIG_XFRM
160#include <net/xfrm.h>
161#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#include <asm/byteorder.h>
163#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700164#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#include <asm/io.h>
166#include <asm/dma.h>
167#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800168#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#include <asm/timex.h>
170
Robert Olssone6fce5b2008-08-07 02:23:01 -0700171#define VERSION "pktgen v2.70: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800174#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800175#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800178#define F_IPSRC_RND (1<<0) /* IP-Src Random */
179#define F_IPDST_RND (1<<1) /* IP-Dst Random */
180#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800186#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700187#define F_VID_RND (1<<9) /* Random VLAN ID */
188#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700189#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700190#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700191#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700192#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800195#define T_TERMINATE (1<<0)
196#define T_STOP (1<<1) /* Stop run */
197#define T_RUN (1<<2) /* Start run */
198#define T_REMDEVALL (1<<3) /* Remove all devs */
199#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/* If lock -- can be removed after some work */
202#define if_lock(t) spin_lock(&(t->if_lock));
203#define if_unlock(t) spin_unlock(&(t->if_lock));
204
205/* Used to help with determining the pkts on receive */
206#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700207#define PG_PROC_DIR "pktgen"
208#define PGCTRL "pgctrl"
209static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211#define MAX_CFLOWS 65536
212
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700213#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
214#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
215
Luiz Capitulino222f1802006-03-20 22:16:13 -0800216struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800217 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800218 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700219#ifdef CONFIG_XFRM
220 struct xfrm_state *x;
221#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700222 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700225/* flow flag bits */
226#define F_INIT (1<<0) /* flow has been initialized */
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /*
230 * Try to keep frequent/infrequent used vars. separated.
231 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800232 struct proc_dir_entry *entry; /* proc file */
233 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800234 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Luiz Capitulino222f1802006-03-20 22:16:13 -0800236 int running; /* if this changes to false, the test will stop */
237
238 /* If min != max, then we will either do a linear iteration, or
239 * we will do a random selection from within the range.
240 */
241 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800242 int removal_mark; /* non-zero => the device is marked for
243 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Luiz Capitulino222f1802006-03-20 22:16:13 -0800245 int min_pkt_size; /* = ETH_ZLEN; */
246 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700247 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800248 int nfrags;
249 __u32 delay_us; /* Default delay */
250 __u32 delay_ns;
251 __u64 count; /* Default No packets to send */
252 __u64 sofar; /* How many pkts we've sent so far */
253 __u64 tx_bytes; /* How many bytes we've transmitted */
254 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Luiz Capitulino222f1802006-03-20 22:16:13 -0800256 /* runtime counters relating to clone_skb */
257 __u64 next_tx_us; /* timestamp of when to tx next */
258 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Luiz Capitulino222f1802006-03-20 22:16:13 -0800260 __u64 allocated_skbs;
261 __u32 clone_count;
262 int last_ok; /* Was last skb sent?
263 * Or a failed transmit of some sort? This will keep
264 * sequence numbers in order, for example.
265 */
266 __u64 started_at; /* micro-seconds */
267 __u64 stopped_at; /* micro-seconds */
268 __u64 idle_acc; /* micro-seconds */
269 __u32 seq_num;
270
271 int clone_skb; /* Use multiple SKBs during packet gen. If this number
272 * is greater than 1, then that many copies of the same
273 * packet will be sent before a new packet is allocated.
274 * For instance, if you want to send 1024 identical packets
275 * before creating a new packet, set clone_skb to 1024.
276 */
277
278 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282
283 struct in6_addr in6_saddr;
284 struct in6_addr in6_daddr;
285 struct in6_addr cur_in6_daddr;
286 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800288 struct in6_addr min_in6_daddr;
289 struct in6_addr max_in6_daddr;
290 struct in6_addr min_in6_saddr;
291 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Luiz Capitulino222f1802006-03-20 22:16:13 -0800293 /* If we're doing ranges, random or incremental, then this
294 * defines the min/max for those ranges.
295 */
Al Viro252e3342006-11-14 20:48:11 -0800296 __be32 saddr_min; /* inclusive, source IP address */
297 __be32 saddr_max; /* exclusive, source IP address */
298 __be32 daddr_min; /* inclusive, dest IP address */
299 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Luiz Capitulino222f1802006-03-20 22:16:13 -0800301 __u16 udp_src_min; /* inclusive, source UDP port */
302 __u16 udp_src_max; /* exclusive, source UDP port */
303 __u16 udp_dst_min; /* inclusive, dest UDP port */
304 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700306 /* DSCP + ECN */
307 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
308 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
309
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800310 /* MPLS */
311 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
312 __be32 labels[MAX_MPLS_LABELS];
313
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700314 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
315 __u8 vlan_p;
316 __u8 vlan_cfi;
317 __u16 vlan_id; /* 0xffff means no vlan tag */
318
319 __u8 svlan_p;
320 __u8 svlan_cfi;
321 __u16 svlan_id; /* 0xffff means no svlan tag */
322
Luiz Capitulino222f1802006-03-20 22:16:13 -0800323 __u32 src_mac_count; /* How many MACs to iterate through */
324 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Luiz Capitulino222f1802006-03-20 22:16:13 -0800326 unsigned char dst_mac[ETH_ALEN];
327 unsigned char src_mac[ETH_ALEN];
328
329 __u32 cur_dst_mac_offset;
330 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800331 __be32 cur_saddr;
332 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800333 __u16 cur_udp_dst;
334 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700335 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800336 __u32 cur_pkt_size;
337
338 __u8 hh[14];
339 /* = {
340 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
341
342 We fill in SRC address later
343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
344 0x08, 0x00
345 };
346 */
347 __u16 pad; /* pad out the hh struct to an even 16 bytes */
348
349 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
350 * are transmitting the same one multiple times
351 */
352 struct net_device *odev; /* The out-going device. Note that the device should
353 * have it's pg_info pointer pointing back to this
354 * device. This will be set when the user specifies
355 * the out-going device name (not when the inject is
356 * started as it used to do.)
357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800359 unsigned cflows; /* Concurrent flows (config) */
360 unsigned lflow; /* Flow length (config) */
361 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700362 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700363
364 u16 queue_map_min;
365 u16 queue_map_max;
366
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700367#ifdef CONFIG_XFRM
368 __u8 ipsmode; /* IPSEC mode (config) */
369 __u8 ipsproto; /* IPSEC type (config) */
370#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800371 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373
374struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800375 __be32 pgh_magic;
376 __be32 seq_num;
377 __be32 tv_sec;
378 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379};
380
381struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800382 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800383 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800384 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800385 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800386 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Luiz Capitulino222f1802006-03-20 22:16:13 -0800388 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
389
390 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int cpu;
392
Luiz Capitulino222f1802006-03-20 22:16:13 -0800393 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700394 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
397#define REMOVE 1
398#define FIND 0
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800401static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800403 __u64 us = tv->tv_usec;
404 us += (__u64) tv->tv_sec * (__u64) 1000000;
405 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Ilpo Järvinen6db105d2008-01-12 03:25:00 -0800408static __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800410 struct timeval tv;
411 do_gettimeofday(&tv);
412 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/* old include end */
416
417static char version[] __initdata = VERSION;
418
Luiz Capitulino222f1802006-03-20 22:16:13 -0800419static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
420static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
421static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
422 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
424static void pktgen_run_all_threads(void);
425static void pktgen_stop_all_threads_ifs(void);
426static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800427static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800429
Luiz Capitulino222f1802006-03-20 22:16:13 -0800430static unsigned int scan_ip6(const char *s, char ip[16]);
431static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800434static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb92005-12-22 12:51:46 -0800435static int pg_delay_d;
436static int pg_clone_skb_d;
437static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Luiz Capitulino222fa072006-03-20 22:24:27 -0800439static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800440static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static struct notifier_block pktgen_notifier_block = {
443 .notifier_call = pktgen_device_event,
444};
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900447 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 *
449 */
450
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700451static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800452{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700453 seq_puts(seq, VERSION);
454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Luiz Capitulino222f1802006-03-20 22:16:13 -0800457static ssize_t pgctrl_write(struct file *file, const char __user * buf,
458 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int err = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700461 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Luiz Capitulino222f1802006-03-20 22:16:13 -0800463 if (!capable(CAP_NET_ADMIN)) {
464 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700468 if (count > sizeof(data))
469 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700472 err = -EFAULT;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700473 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800474 }
475 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Luiz Capitulino222f1802006-03-20 22:16:13 -0800477 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 pktgen_stop_all_threads_ifs();
479
Luiz Capitulino222f1802006-03-20 22:16:13 -0800480 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 pktgen_run_all_threads();
482
Luiz Capitulino222f1802006-03-20 22:16:13 -0800483 else
David S. Miller25a8b252007-07-30 16:11:48 -0700484 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 err = count;
487
Luiz Capitulino222f1802006-03-20 22:16:13 -0800488out:
489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700492static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700494 return single_open(file, pgctrl_show, PDE(inode)->data);
495}
496
Arjan van de Ven9a321442007-02-12 00:55:35 -0800497static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498 .owner = THIS_MODULE,
499 .open = pgctrl_open,
500 .read = seq_read,
501 .llseek = seq_lseek,
502 .write = pgctrl_write,
503 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700504};
505
506static int pktgen_if_show(struct seq_file *seq, void *v)
507{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800508 struct pktgen_dev *pkt_dev = seq->private;
509 __u64 sa;
510 __u64 stopped;
511 __u64 now = getCurUs();
Joe Perches0795af52007-10-03 17:59:30 -0700512 DECLARE_MAC_BUF(mac);
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,
520 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
521 pkt_dev->nfrags,
522 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800523 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Luiz Capitulino222f1802006-03-20 22:16:13 -0800525 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
526 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Robert Olsson45b270f2007-08-28 15:45:55 -0700528 seq_printf(seq,
529 " queue_map_min: %u queue_map_max: %u\n",
530 pkt_dev->queue_map_min,
531 pkt_dev->queue_map_max);
532
Luiz Capitulino222f1802006-03-20 22:16:13 -0800533 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800535 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
536 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
537 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
538 seq_printf(seq,
539 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
540 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Luiz Capitulino222f1802006-03-20 22:16:13 -0800542 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
543 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
544 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
545 seq_printf(seq,
546 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
547 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Luiz Capitulino222f1802006-03-20 22:16:13 -0800549 } else
550 seq_printf(seq,
551 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
552 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
553 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700555 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Joe Perches0795af52007-10-03 17:59:30 -0700557 seq_printf(seq, "%s ",
558 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
559 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Luiz Capitulino222f1802006-03-20 22:16:13 -0800561 seq_printf(seq, "dst_mac: ");
Joe Perches0795af52007-10-03 17:59:30 -0700562 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Luiz Capitulino222f1802006-03-20 22:16:13 -0800564 seq_printf(seq,
565 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
566 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
567 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Luiz Capitulino222f1802006-03-20 22:16:13 -0800569 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800570 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700571 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800573 if (pkt_dev->nr_labels) {
574 unsigned i;
575 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700576 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800577 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
578 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
579 }
580
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700581 if (pkt_dev->vlan_id != 0xffff) {
582 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
583 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
584 }
585
586 if (pkt_dev->svlan_id != 0xffff) {
587 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
588 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
589 }
590
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700591 if (pkt_dev->tos) {
592 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
593 }
594
595 if (pkt_dev->traffic_class) {
596 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
597 }
598
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800599 seq_printf(seq, " Flags: ");
600
Luiz Capitulino222f1802006-03-20 22:16:13 -0800601 if (pkt_dev->flags & F_IPV6)
602 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Luiz Capitulino222f1802006-03-20 22:16:13 -0800604 if (pkt_dev->flags & F_IPSRC_RND)
605 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Luiz Capitulino222f1802006-03-20 22:16:13 -0800607 if (pkt_dev->flags & F_IPDST_RND)
608 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Luiz Capitulino222f1802006-03-20 22:16:13 -0800610 if (pkt_dev->flags & F_TXSIZE_RND)
611 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Luiz Capitulino222f1802006-03-20 22:16:13 -0800613 if (pkt_dev->flags & F_UDPSRC_RND)
614 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Luiz Capitulino222f1802006-03-20 22:16:13 -0800616 if (pkt_dev->flags & F_UDPDST_RND)
617 seq_printf(seq, "UDPDST_RND ");
618
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800619 if (pkt_dev->flags & F_MPLS_RND)
620 seq_printf(seq, "MPLS_RND ");
621
Robert Olsson45b270f2007-08-28 15:45:55 -0700622 if (pkt_dev->flags & F_QUEUE_MAP_RND)
623 seq_printf(seq, "QUEUE_MAP_RND ");
624
Robert Olssone6fce5b2008-08-07 02:23:01 -0700625 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
626 seq_printf(seq, "QUEUE_MAP_CPU ");
627
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700628 if (pkt_dev->cflows) {
629 if (pkt_dev->flags & F_FLOW_SEQ)
630 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
631 else
632 seq_printf(seq, "FLOW_RND ");
633 }
634
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700635#ifdef CONFIG_XFRM
636 if (pkt_dev->flags & F_IPSEC_ON)
637 seq_printf(seq, "IPSEC ");
638#endif
639
Luiz Capitulino222f1802006-03-20 22:16:13 -0800640 if (pkt_dev->flags & F_MACSRC_RND)
641 seq_printf(seq, "MACSRC_RND ");
642
643 if (pkt_dev->flags & F_MACDST_RND)
644 seq_printf(seq, "MACDST_RND ");
645
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700646 if (pkt_dev->flags & F_VID_RND)
647 seq_printf(seq, "VID_RND ");
648
649 if (pkt_dev->flags & F_SVID_RND)
650 seq_printf(seq, "SVID_RND ");
651
Luiz Capitulino222f1802006-03-20 22:16:13 -0800652 seq_puts(seq, "\n");
653
654 sa = pkt_dev->started_at;
655 stopped = pkt_dev->stopped_at;
656 if (pkt_dev->running)
657 stopped = now; /* not really stopped, more like last-running-at */
658
659 seq_printf(seq,
660 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
661 (unsigned long long)pkt_dev->sofar,
662 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
663 (unsigned long long)stopped,
664 (unsigned long long)pkt_dev->idle_acc);
665
666 seq_printf(seq,
667 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700668 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
669 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Luiz Capitulino222f1802006-03-20 22:16:13 -0800671 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800673 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
674 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
675 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
676 } else
677 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700678 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Luiz Capitulino222f1802006-03-20 22:16:13 -0800680 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700681 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Robert Olsson45b270f2007-08-28 15:45:55 -0700683 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
684
Luiz Capitulino222f1802006-03-20 22:16:13 -0800685 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800688 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800690 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800695
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700696static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800697{
698 int i = 0;
699 *num = 0;
700
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700701 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800702 char c;
703 *num <<= 4;
704 if (get_user(c, &user_buffer[i]))
705 return -EFAULT;
706 if ((c >= '0') && (c <= '9'))
707 *num |= c - '0';
708 else if ((c >= 'a') && (c <= 'f'))
709 *num |= c - 'a' + 10;
710 else if ((c >= 'A') && (c <= 'F'))
711 *num |= c - 'A' + 10;
712 else
713 break;
714 }
715 return i;
716}
717
Luiz Capitulino222f1802006-03-20 22:16:13 -0800718static int count_trail_chars(const char __user * user_buffer,
719 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 int i;
722
723 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800724 char c;
725 if (get_user(c, &user_buffer[i]))
726 return -EFAULT;
727 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 case '\"':
729 case '\n':
730 case '\r':
731 case '\t':
732 case ' ':
733 case '=':
734 break;
735 default:
736 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739done:
740 return i;
741}
742
Luiz Capitulino222f1802006-03-20 22:16:13 -0800743static unsigned long num_arg(const char __user * user_buffer,
744 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 int i = 0;
747 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800748
749 for (; i < maxlen; i++) {
750 char c;
751 if (get_user(c, &user_buffer[i]))
752 return -EFAULT;
753 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800755 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 } else
757 break;
758 }
759 return i;
760}
761
Luiz Capitulino222f1802006-03-20 22:16:13 -0800762static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 int i = 0;
765
Luiz Capitulino222f1802006-03-20 22:16:13 -0800766 for (; i < maxlen; i++) {
767 char c;
768 if (get_user(c, &user_buffer[i]))
769 return -EFAULT;
770 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 case '\"':
772 case '\n':
773 case '\r':
774 case '\t':
775 case ' ':
776 goto done_str;
777 break;
778 default:
779 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return i;
784}
785
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800786static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
787{
788 unsigned n = 0;
789 char c;
790 ssize_t i = 0;
791 int len;
792
793 pkt_dev->nr_labels = 0;
794 do {
795 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700796 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800797 if (len <= 0)
798 return len;
799 pkt_dev->labels[n] = htonl(tmp);
800 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
801 pkt_dev->flags |= F_MPLS_RND;
802 i += len;
803 if (get_user(c, &buffer[i]))
804 return -EFAULT;
805 i++;
806 n++;
807 if (n >= MAX_MPLS_LABELS)
808 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700809 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800810
811 pkt_dev->nr_labels = n;
812 return i;
813}
814
Luiz Capitulino222f1802006-03-20 22:16:13 -0800815static ssize_t pktgen_if_write(struct file *file,
816 const char __user * user_buffer, size_t count,
817 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800819 struct seq_file *seq = (struct seq_file *)file->private_data;
820 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 int i = 0, max, len;
822 char name[16], valstr[32];
823 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800824 char *pg_result = NULL;
825 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800827
828 pg_result = &(pkt_dev->result[0]);
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700831 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return -EINVAL;
833 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 max = count - i;
836 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800837 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700838 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800839 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800841 i += tmp;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* Read variable name */
844
845 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800846 if (len < 0) {
847 return len;
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800850 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -EFAULT;
852 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853
854 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800856 if (len < 0)
857 return len;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 i += len;
860
861 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800862 char tb[count + 1];
863 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800865 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700866 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867 (unsigned long)count, tb);
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if (!strcmp(name, "min_pkt_size")) {
871 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800872 if (len < 0) {
873 return len;
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800876 if (value < 14 + 20 + 8)
877 value = 14 + 20 + 8;
878 if (value != pkt_dev->min_pkt_size) {
879 pkt_dev->min_pkt_size = value;
880 pkt_dev->cur_pkt_size = value;
881 }
882 sprintf(pg_result, "OK: min_pkt_size=%u",
883 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return count;
885 }
886
Luiz Capitulino222f1802006-03-20 22:16:13 -0800887 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889 if (len < 0) {
890 return len;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800893 if (value < 14 + 20 + 8)
894 value = 14 + 20 + 8;
895 if (value != pkt_dev->max_pkt_size) {
896 pkt_dev->max_pkt_size = value;
897 pkt_dev->cur_pkt_size = value;
898 }
899 sprintf(pg_result, "OK: max_pkt_size=%u",
900 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return count;
902 }
903
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (!strcmp(name, "pkt_size")) {
907 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800908 if (len < 0) {
909 return len;
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800912 if (value < 14 + 20 + 8)
913 value = 14 + 20 + 8;
914 if (value != pkt_dev->min_pkt_size) {
915 pkt_dev->min_pkt_size = value;
916 pkt_dev->max_pkt_size = value;
917 pkt_dev->cur_pkt_size = value;
918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
920 return count;
921 }
922
Luiz Capitulino222f1802006-03-20 22:16:13 -0800923 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800925 if (len < 0) {
926 return len;
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800929 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 sprintf(pg_result, "OK: debug=%u", debug);
931 return count;
932 }
933
Luiz Capitulino222f1802006-03-20 22:16:13 -0800934 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800936 if (len < 0) {
937 return len;
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 i += len;
940 pkt_dev->nfrags = value;
941 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
942 return count;
943 }
944 if (!strcmp(name, "delay")) {
945 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800946 if (len < 0) {
947 return len;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 i += len;
950 if (value == 0x7FFFFFFF) {
951 pkt_dev->delay_us = 0x7FFFFFFF;
952 pkt_dev->delay_ns = 0;
953 } else {
954 pkt_dev->delay_us = value / 1000;
955 pkt_dev->delay_ns = value % 1000;
956 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800957 sprintf(pg_result, "OK: delay=%u",
958 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return count;
960 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800961 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800963 if (len < 0) {
964 return len;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800967 if (value != pkt_dev->udp_src_min) {
968 pkt_dev->udp_src_min = value;
969 pkt_dev->cur_udp_src = value;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
972 return count;
973 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800974 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800976 if (len < 0) {
977 return len;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800980 if (value != pkt_dev->udp_dst_min) {
981 pkt_dev->udp_dst_min = value;
982 pkt_dev->cur_udp_dst = value;
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
985 return count;
986 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800987 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800989 if (len < 0) {
990 return len;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800993 if (value != pkt_dev->udp_src_max) {
994 pkt_dev->udp_src_max = value;
995 pkt_dev->cur_udp_src = value;
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
998 return count;
999 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001000 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001002 if (len < 0) {
1003 return len;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001006 if (value != pkt_dev->udp_dst_max) {
1007 pkt_dev->udp_dst_max = value;
1008 pkt_dev->cur_udp_dst = value;
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1011 return count;
1012 }
1013 if (!strcmp(name, "clone_skb")) {
1014 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001015 if (len < 0) {
1016 return len;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001019 pkt_dev->clone_skb = value;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1022 return count;
1023 }
1024 if (!strcmp(name, "count")) {
1025 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001026 if (len < 0) {
1027 return len;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 i += len;
1030 pkt_dev->count = value;
1031 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001032 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return count;
1034 }
1035 if (!strcmp(name, "src_mac_count")) {
1036 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001037 if (len < 0) {
1038 return len;
1039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 i += len;
1041 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001042 pkt_dev->src_mac_count = value;
1043 pkt_dev->cur_src_mac_offset = 0;
1044 }
1045 sprintf(pg_result, "OK: src_mac_count=%d",
1046 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return count;
1048 }
1049 if (!strcmp(name, "dst_mac_count")) {
1050 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001051 if (len < 0) {
1052 return len;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 i += len;
1055 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001056 pkt_dev->dst_mac_count = value;
1057 pkt_dev->cur_dst_mac_offset = 0;
1058 }
1059 sprintf(pg_result, "OK: dst_mac_count=%d",
1060 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return count;
1062 }
1063 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001064 char f[32];
1065 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001067 if (len < 0) {
1068 return len;
1069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (copy_from_user(f, &user_buffer[i], len))
1071 return -EFAULT;
1072 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001073 if (strcmp(f, "IPSRC_RND") == 0)
1074 pkt_dev->flags |= F_IPSRC_RND;
1075
1076 else if (strcmp(f, "!IPSRC_RND") == 0)
1077 pkt_dev->flags &= ~F_IPSRC_RND;
1078
1079 else if (strcmp(f, "TXSIZE_RND") == 0)
1080 pkt_dev->flags |= F_TXSIZE_RND;
1081
1082 else if (strcmp(f, "!TXSIZE_RND") == 0)
1083 pkt_dev->flags &= ~F_TXSIZE_RND;
1084
1085 else if (strcmp(f, "IPDST_RND") == 0)
1086 pkt_dev->flags |= F_IPDST_RND;
1087
1088 else if (strcmp(f, "!IPDST_RND") == 0)
1089 pkt_dev->flags &= ~F_IPDST_RND;
1090
1091 else if (strcmp(f, "UDPSRC_RND") == 0)
1092 pkt_dev->flags |= F_UDPSRC_RND;
1093
1094 else if (strcmp(f, "!UDPSRC_RND") == 0)
1095 pkt_dev->flags &= ~F_UDPSRC_RND;
1096
1097 else if (strcmp(f, "UDPDST_RND") == 0)
1098 pkt_dev->flags |= F_UDPDST_RND;
1099
1100 else if (strcmp(f, "!UDPDST_RND") == 0)
1101 pkt_dev->flags &= ~F_UDPDST_RND;
1102
1103 else if (strcmp(f, "MACSRC_RND") == 0)
1104 pkt_dev->flags |= F_MACSRC_RND;
1105
1106 else if (strcmp(f, "!MACSRC_RND") == 0)
1107 pkt_dev->flags &= ~F_MACSRC_RND;
1108
1109 else if (strcmp(f, "MACDST_RND") == 0)
1110 pkt_dev->flags |= F_MACDST_RND;
1111
1112 else if (strcmp(f, "!MACDST_RND") == 0)
1113 pkt_dev->flags &= ~F_MACDST_RND;
1114
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001115 else if (strcmp(f, "MPLS_RND") == 0)
1116 pkt_dev->flags |= F_MPLS_RND;
1117
1118 else if (strcmp(f, "!MPLS_RND") == 0)
1119 pkt_dev->flags &= ~F_MPLS_RND;
1120
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001121 else if (strcmp(f, "VID_RND") == 0)
1122 pkt_dev->flags |= F_VID_RND;
1123
1124 else if (strcmp(f, "!VID_RND") == 0)
1125 pkt_dev->flags &= ~F_VID_RND;
1126
1127 else if (strcmp(f, "SVID_RND") == 0)
1128 pkt_dev->flags |= F_SVID_RND;
1129
1130 else if (strcmp(f, "!SVID_RND") == 0)
1131 pkt_dev->flags &= ~F_SVID_RND;
1132
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001133 else if (strcmp(f, "FLOW_SEQ") == 0)
1134 pkt_dev->flags |= F_FLOW_SEQ;
1135
Robert Olsson45b270f2007-08-28 15:45:55 -07001136 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1137 pkt_dev->flags |= F_QUEUE_MAP_RND;
1138
1139 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1140 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001141
1142 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1143 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1144
1145 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1146 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001147#ifdef CONFIG_XFRM
1148 else if (strcmp(f, "IPSEC") == 0)
1149 pkt_dev->flags |= F_IPSEC_ON;
1150#endif
1151
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001152 else if (strcmp(f, "!IPV6") == 0)
1153 pkt_dev->flags &= ~F_IPV6;
1154
Luiz Capitulino222f1802006-03-20 22:16:13 -08001155 else {
1156 sprintf(pg_result,
1157 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1158 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001159 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001160 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001161 return count;
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1164 return count;
1165 }
1166 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1167 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001168 if (len < 0) {
1169 return len;
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Luiz Capitulino222f1802006-03-20 22:16:13 -08001172 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001174 buf[len] = 0;
1175 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1176 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1177 strncpy(pkt_dev->dst_min, buf, len);
1178 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1179 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1180 }
1181 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001182 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001183 pkt_dev->dst_min);
1184 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1186 return count;
1187 }
1188 if (!strcmp(name, "dst_max")) {
1189 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001190 if (len < 0) {
1191 return len;
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Luiz Capitulino222f1802006-03-20 22:16:13 -08001194 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return -EFAULT;
1196
Luiz Capitulino222f1802006-03-20 22:16:13 -08001197 buf[len] = 0;
1198 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1199 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1200 strncpy(pkt_dev->dst_max, buf, len);
1201 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1202 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1203 }
1204 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001205 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001206 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 i += len;
1208 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1209 return count;
1210 }
1211 if (!strcmp(name, "dst6")) {
1212 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001213 if (len < 0)
1214 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 pkt_dev->flags |= F_IPV6;
1217
Luiz Capitulino222f1802006-03-20 22:16:13 -08001218 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001220 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001223 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1226
Luiz Capitulino222f1802006-03-20 22:16:13 -08001227 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001228 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Luiz Capitulino222f1802006-03-20 22:16:13 -08001230 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 sprintf(pg_result, "OK: dst6=%s", buf);
1232 return count;
1233 }
1234 if (!strcmp(name, "dst6_min")) {
1235 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001236 if (len < 0)
1237 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 pkt_dev->flags |= F_IPV6;
1240
Luiz Capitulino222f1802006-03-20 22:16:13 -08001241 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001243 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001246 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Luiz Capitulino222f1802006-03-20 22:16:13 -08001248 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1249 &pkt_dev->min_in6_daddr);
1250 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001251 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Luiz Capitulino222f1802006-03-20 22:16:13 -08001253 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 sprintf(pg_result, "OK: dst6_min=%s", buf);
1255 return count;
1256 }
1257 if (!strcmp(name, "dst6_max")) {
1258 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 if (len < 0)
1260 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 pkt_dev->flags |= F_IPV6;
1263
Luiz Capitulino222f1802006-03-20 22:16:13 -08001264 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001266 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001269 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Luiz Capitulino222f1802006-03-20 22:16:13 -08001271 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001272 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Luiz Capitulino222f1802006-03-20 22:16:13 -08001274 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 sprintf(pg_result, "OK: dst6_max=%s", buf);
1276 return count;
1277 }
1278 if (!strcmp(name, "src6")) {
1279 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001280 if (len < 0)
1281 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 pkt_dev->flags |= F_IPV6;
1284
Luiz Capitulino222f1802006-03-20 22:16:13 -08001285 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001287 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001290 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1293
Luiz Capitulino222f1802006-03-20 22:16:13 -08001294 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001295 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001296
1297 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 sprintf(pg_result, "OK: src6=%s", buf);
1299 return count;
1300 }
1301 if (!strcmp(name, "src_min")) {
1302 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001303 if (len < 0) {
1304 return len;
1305 }
1306 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 buf[len] = 0;
1309 if (strcmp(buf, pkt_dev->src_min) != 0) {
1310 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1311 strncpy(pkt_dev->src_min, buf, len);
1312 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1313 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1314 }
1315 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001316 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 i += len;
1319 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1320 return count;
1321 }
1322 if (!strcmp(name, "src_max")) {
1323 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001324 if (len < 0) {
1325 return len;
1326 }
1327 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 buf[len] = 0;
1330 if (strcmp(buf, pkt_dev->src_max) != 0) {
1331 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1332 strncpy(pkt_dev->src_max, buf, len);
1333 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1334 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1335 }
1336 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001337 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 i += len;
1340 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1341 return count;
1342 }
1343 if (!strcmp(name, "dst_mac")) {
1344 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001345 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001347 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001350 if (len < 0) {
1351 return len;
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001354 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EFAULT;
1356 i += len;
1357
Luiz Capitulino222f1802006-03-20 22:16:13 -08001358 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (*v >= '0' && *v <= '9') {
1360 *m *= 16;
1361 *m += *v - '0';
1362 }
1363 if (*v >= 'A' && *v <= 'F') {
1364 *m *= 16;
1365 *m += *v - 'A' + 10;
1366 }
1367 if (*v >= 'a' && *v <= 'f') {
1368 *m *= 16;
1369 *m += *v - 'a' + 10;
1370 }
1371 if (*v == ':') {
1372 m++;
1373 *m = 0;
1374 }
1375 }
1376
1377 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001378 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1379 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 sprintf(pg_result, "OK: dstmac");
1382 return count;
1383 }
1384 if (!strcmp(name, "src_mac")) {
1385 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001386 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 unsigned char *m = pkt_dev->src_mac;
1388
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001389 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001392 if (len < 0) {
1393 return len;
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001396 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -EFAULT;
1398 i += len;
1399
Luiz Capitulino222f1802006-03-20 22:16:13 -08001400 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (*v >= '0' && *v <= '9') {
1402 *m *= 16;
1403 *m += *v - '0';
1404 }
1405 if (*v >= 'A' && *v <= 'F') {
1406 *m *= 16;
1407 *m += *v - 'A' + 10;
1408 }
1409 if (*v >= 'a' && *v <= 'f') {
1410 *m *= 16;
1411 *m += *v - 'a' + 10;
1412 }
1413 if (*v == ':') {
1414 m++;
1415 *m = 0;
1416 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001419 /* Set up Src MAC */
1420 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1421 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1422
Luiz Capitulino222f1802006-03-20 22:16:13 -08001423 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return count;
1425 }
1426
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427 if (!strcmp(name, "clear_counters")) {
1428 pktgen_clear_counters(pkt_dev);
1429 sprintf(pg_result, "OK: Clearing counters.\n");
1430 return count;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (!strcmp(name, "flows")) {
1434 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001435 if (len < 0) {
1436 return len;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 i += len;
1439 if (value > MAX_CFLOWS)
1440 value = MAX_CFLOWS;
1441
1442 pkt_dev->cflows = value;
1443 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1444 return count;
1445 }
1446
1447 if (!strcmp(name, "flowlen")) {
1448 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001449 if (len < 0) {
1450 return len;
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 i += len;
1453 pkt_dev->lflow = value;
1454 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1455 return count;
1456 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001457
Robert Olsson45b270f2007-08-28 15:45:55 -07001458 if (!strcmp(name, "queue_map_min")) {
1459 len = num_arg(&user_buffer[i], 5, &value);
1460 if (len < 0) {
1461 return len;
1462 }
1463 i += len;
1464 pkt_dev->queue_map_min = value;
1465 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1466 return count;
1467 }
1468
1469 if (!strcmp(name, "queue_map_max")) {
1470 len = num_arg(&user_buffer[i], 5, &value);
1471 if (len < 0) {
1472 return len;
1473 }
1474 i += len;
1475 pkt_dev->queue_map_max = value;
1476 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1477 return count;
1478 }
1479
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001480 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001481 unsigned n, cnt;
1482
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001483 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001484 if (len < 0)
1485 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001486 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001487 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001488 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001489 cnt += sprintf(pg_result + cnt,
1490 "%08x%s", ntohl(pkt_dev->labels[n]),
1491 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001492
1493 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1494 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1495 pkt_dev->svlan_id = 0xffff;
1496
1497 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001498 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001499 }
1500 return count;
1501 }
1502
1503 if (!strcmp(name, "vlan_id")) {
1504 len = num_arg(&user_buffer[i], 4, &value);
1505 if (len < 0) {
1506 return len;
1507 }
1508 i += len;
1509 if (value <= 4095) {
1510 pkt_dev->vlan_id = value; /* turn on VLAN */
1511
1512 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001513 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001514
1515 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001516 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001517
1518 pkt_dev->nr_labels = 0; /* turn off MPLS */
1519 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1520 } else {
1521 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1522 pkt_dev->svlan_id = 0xffff;
1523
1524 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001525 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001526 }
1527 return count;
1528 }
1529
1530 if (!strcmp(name, "vlan_p")) {
1531 len = num_arg(&user_buffer[i], 1, &value);
1532 if (len < 0) {
1533 return len;
1534 }
1535 i += len;
1536 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1537 pkt_dev->vlan_p = value;
1538 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1539 } else {
1540 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1541 }
1542 return count;
1543 }
1544
1545 if (!strcmp(name, "vlan_cfi")) {
1546 len = num_arg(&user_buffer[i], 1, &value);
1547 if (len < 0) {
1548 return len;
1549 }
1550 i += len;
1551 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1552 pkt_dev->vlan_cfi = value;
1553 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1554 } else {
1555 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1556 }
1557 return count;
1558 }
1559
1560 if (!strcmp(name, "svlan_id")) {
1561 len = num_arg(&user_buffer[i], 4, &value);
1562 if (len < 0) {
1563 return len;
1564 }
1565 i += len;
1566 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1567 pkt_dev->svlan_id = value; /* turn on SVLAN */
1568
1569 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001570 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001571
1572 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001573 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001574
1575 pkt_dev->nr_labels = 0; /* turn off MPLS */
1576 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1577 } else {
1578 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1579 pkt_dev->svlan_id = 0xffff;
1580
1581 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001582 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001583 }
1584 return count;
1585 }
1586
1587 if (!strcmp(name, "svlan_p")) {
1588 len = num_arg(&user_buffer[i], 1, &value);
1589 if (len < 0) {
1590 return len;
1591 }
1592 i += len;
1593 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1594 pkt_dev->svlan_p = value;
1595 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1596 } else {
1597 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1598 }
1599 return count;
1600 }
1601
1602 if (!strcmp(name, "svlan_cfi")) {
1603 len = num_arg(&user_buffer[i], 1, &value);
1604 if (len < 0) {
1605 return len;
1606 }
1607 i += len;
1608 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1609 pkt_dev->svlan_cfi = value;
1610 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1611 } else {
1612 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1613 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001614 return count;
1615 }
1616
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001617 if (!strcmp(name, "tos")) {
1618 __u32 tmp_value = 0;
1619 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1620 if (len < 0) {
1621 return len;
1622 }
1623 i += len;
1624 if (len == 2) {
1625 pkt_dev->tos = tmp_value;
1626 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1627 } else {
1628 sprintf(pg_result, "ERROR: tos must be 00-ff");
1629 }
1630 return count;
1631 }
1632
1633 if (!strcmp(name, "traffic_class")) {
1634 __u32 tmp_value = 0;
1635 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1636 if (len < 0) {
1637 return len;
1638 }
1639 i += len;
1640 if (len == 2) {
1641 pkt_dev->traffic_class = tmp_value;
1642 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1643 } else {
1644 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1645 }
1646 return count;
1647 }
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1650 return -EINVAL;
1651}
1652
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001653static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001655 return single_open(file, pktgen_if_show, PDE(inode)->data);
1656}
1657
Arjan van de Ven9a321442007-02-12 00:55:35 -08001658static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001659 .owner = THIS_MODULE,
1660 .open = pktgen_if_open,
1661 .read = seq_read,
1662 .llseek = seq_lseek,
1663 .write = pktgen_if_write,
1664 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001665};
1666
1667static int pktgen_thread_show(struct seq_file *seq, void *v)
1668{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001669 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001670 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001672 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Luiz Capitulino222f1802006-03-20 22:16:13 -08001674 seq_printf(seq, "Running: ");
1675
1676 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001677 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001678 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001679 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Luiz Capitulino222f1802006-03-20 22:16:13 -08001681 seq_printf(seq, "\nStopped: ");
1682
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001683 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001684 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001685 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 if (t->result[0])
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001688 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 else
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001690 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Luiz Capitulino222f1802006-03-20 22:16:13 -08001692 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001697static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001698 const char __user * user_buffer,
1699 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001701 struct seq_file *seq = (struct seq_file *)file->private_data;
1702 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int i = 0, max, len, ret;
1704 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001705 char *pg_result;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001708 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return -EINVAL;
1710 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001713 len = count_trail_chars(&user_buffer[i], max);
1714 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001715 return len;
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 i += len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /* Read variable name */
1720
1721 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001722 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001723 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 memset(name, 0, sizeof(name));
1726 if (copy_from_user(name, &user_buffer[i], len))
1727 return -EFAULT;
1728 i += len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001729
Luiz Capitulino222f1802006-03-20 22:16:13 -08001730 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001732 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001733 return len;
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 i += len;
1736
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001737 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001738 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1739 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Luiz Capitulino222f1802006-03-20 22:16:13 -08001741 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001742 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 ret = -EINVAL;
1744 goto out;
1745 }
1746
1747 pg_result = &(t->result[0]);
1748
Luiz Capitulino222f1802006-03-20 22:16:13 -08001749 if (!strcmp(name, "add_device")) {
1750 char f[32];
1751 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001753 if (len < 0) {
1754 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 goto out;
1756 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return -EFAULT;
1759 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001760 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001761 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001762 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001763 ret = count;
1764 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 goto out;
1766 }
1767
Luiz Capitulino222f1802006-03-20 22:16:13 -08001768 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001769 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001770 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001771 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001772 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 goto out;
1776 }
1777
Luiz Capitulino222f1802006-03-20 22:16:13 -08001778 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001779 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001780 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 goto out;
1782 }
1783
1784 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001785out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return ret;
1787}
1788
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001789static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001791 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
Arjan van de Ven9a321442007-02-12 00:55:35 -08001794static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001795 .owner = THIS_MODULE,
1796 .open = pktgen_thread_open,
1797 .read = seq_read,
1798 .llseek = seq_lseek,
1799 .write = pktgen_thread_write,
1800 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001801};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001804static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 struct pktgen_thread *t;
1807 struct pktgen_dev *pkt_dev = NULL;
1808
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001809 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 pkt_dev = pktgen_find_dev(t, ifname);
1811 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001812 if (remove) {
1813 if_lock(t);
1814 pkt_dev->removal_mark = 1;
1815 t->control |= T_REMDEV;
1816 if_unlock(t);
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 break;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001821 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001824/*
1825 * mark a device for removal
1826 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001827static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001830 const int max_tries = 10, msec_per_try = 125;
1831 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001832
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001833 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001834 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001835
Luiz Capitulino222f1802006-03-20 22:16:13 -08001836 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001837
1838 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001839 if (pkt_dev == NULL)
1840 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001841
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001842 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001843 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1844 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001845 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001846 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001847
1848 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001849 printk(KERN_ERR "pktgen_mark_device: timed out after "
1850 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001851 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001852 break;
1853 }
1854
1855 }
1856
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001857 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001858}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001859
Stephen Hemminger39df2322007-03-04 16:11:51 -08001860static void pktgen_change_name(struct net_device *dev)
1861{
1862 struct pktgen_thread *t;
1863
1864 list_for_each_entry(t, &pktgen_threads, th_list) {
1865 struct pktgen_dev *pkt_dev;
1866
1867 list_for_each_entry(pkt_dev, &t->if_list, list) {
1868 if (pkt_dev->odev != dev)
1869 continue;
1870
1871 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1872
1873 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1874 pg_proc_dir);
1875 if (!pkt_dev->entry)
1876 printk(KERN_ERR "pktgen: can't move proc "
1877 " entry for '%s'\n", dev->name);
1878 break;
1879 }
1880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Luiz Capitulino222f1802006-03-20 22:16:13 -08001883static int pktgen_device_event(struct notifier_block *unused,
1884 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001886 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001888 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001889 return NOTIFY_DONE;
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 /* It is OK that we do not hold the group lock right now,
1892 * as we run under the RTNL lock.
1893 */
1894
1895 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001896 case NETDEV_CHANGENAME:
1897 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001901 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 return NOTIFY_DONE;
1906}
1907
Robert Olssone6fce5b2008-08-07 02:23:01 -07001908static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev, const char *ifname)
1909{
1910 char b[IFNAMSIZ+5];
1911 int i = 0;
1912
1913 for(i=0; ifname[i] != '@'; i++) {
1914 if(i == IFNAMSIZ)
1915 break;
1916
1917 b[i] = ifname[i];
1918 }
1919 b[i] = 0;
1920
1921 return dev_get_by_name(&init_net, b);
1922}
1923
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925/* Associate pktgen_dev with a device. */
1926
Stephen Hemminger39df2322007-03-04 16:11:51 -08001927static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001928{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001930 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (pkt_dev->odev) {
1934 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001935 pkt_dev->odev = NULL;
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Robert Olssone6fce5b2008-08-07 02:23:01 -07001938 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001940 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001941 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001945 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001946 err = -EINVAL;
1947 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001948 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001949 err = -ENETDOWN;
1950 } else {
1951 pkt_dev->odev = odev;
1952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001956 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
1959/* Read pkt_dev from the interface and set up internal pktgen_dev
1960 * structure to have the right information to create/send packets
1961 */
1962static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1963{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001964 int ntxq;
1965
Luiz Capitulino222f1802006-03-20 22:16:13 -08001966 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001967 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1968 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001969 sprintf(pkt_dev->result,
1970 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1971 return;
1972 }
1973
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001974 /* make sure that we don't pick a non-existing transmit queue */
1975 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08001976
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001977 if (ntxq <= pkt_dev->queue_map_min) {
1978 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07001979 "queue_map_min (zero-based) (%d) exceeds valid range "
1980 "[0 - %d] for (%d) queues on %s, resetting\n",
1981 pkt_dev->queue_map_min, (ntxq ?: 1)- 1, ntxq,
1982 pkt_dev->odev->name);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001983 pkt_dev->queue_map_min = ntxq - 1;
1984 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07001985 if (pkt_dev->queue_map_max >= ntxq) {
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001986 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07001987 "queue_map_max (zero-based) (%d) exceeds valid range "
1988 "[0 - %d] for (%d) queues on %s, resetting\n",
1989 pkt_dev->queue_map_max, (ntxq ?: 1)- 1, ntxq,
1990 pkt_dev->odev->name);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001991 pkt_dev->queue_map_max = ntxq - 1;
1992 }
1993
Luiz Capitulino222f1802006-03-20 22:16:13 -08001994 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001996 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001997 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Luiz Capitulino222f1802006-03-20 22:16:13 -08001999 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002000 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Luiz Capitulino222f1802006-03-20 22:16:13 -08002002 /* Set up pkt size */
2003 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2004
2005 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002007 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 * gets exported
2009 */
2010
2011#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002012 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 struct inet6_dev *idev;
2014
Luiz Capitulino222f1802006-03-20 22:16:13 -08002015 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2016 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 set = 1;
2018 break;
2019 }
2020
Luiz Capitulino222f1802006-03-20 22:16:13 -08002021 if (!set) {
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /*
2024 * Use linklevel address if unconfigured.
2025 *
2026 * use ipv6_get_lladdr if/when it's get exported
2027 */
2028
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002029 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2031 struct inet6_ifaddr *ifp;
2032
2033 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034 for (ifp = idev->addr_list; ifp;
2035 ifp = ifp->if_next) {
2036 if (ifp->scope == IFA_LINK
2037 && !(ifp->
2038 flags & IFA_F_TENTATIVE)) {
2039 ipv6_addr_copy(&pkt_dev->
2040 cur_in6_saddr,
2041 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 err = 0;
2043 break;
2044 }
2045 }
2046 read_unlock_bh(&idev->lock);
2047 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002048 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002049 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002050 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2051 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 pkt_dev->saddr_min = 0;
2056 pkt_dev->saddr_max = 0;
2057 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002058
2059 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002062 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (in_dev) {
2064 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002065 pkt_dev->saddr_min =
2066 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 pkt_dev->saddr_max = pkt_dev->saddr_min;
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002071 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2073 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2074 }
2075
2076 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2077 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2078 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002079 /* Initialize current values. */
2080 pkt_dev->cur_dst_mac_offset = 0;
2081 pkt_dev->cur_src_mac_offset = 0;
2082 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2083 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2084 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2085 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 pkt_dev->nflows = 0;
2087}
2088
2089static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2090{
2091 __u64 start;
2092 __u64 now;
2093
2094 start = now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002096 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002097 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002098 schedule_timeout_interruptible(1);
2099 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (!pkt_dev->running)
2101 return;
2102 if (need_resched())
2103 schedule();
2104 }
2105
2106 now = getCurUs();
2107 }
2108
2109 pkt_dev->idle_acc += now - start;
2110}
2111
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002112static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2113{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002114 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002115 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2116 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2117 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2118}
2119
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002120static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2121{
2122
2123 if (pkt_dev->flows[flow].flags & F_INIT)
2124 return 1;
2125 else
2126 return 0;
2127}
2128
2129static inline int f_pick(struct pktgen_dev *pkt_dev)
2130{
2131 int flow = pkt_dev->curfl;
2132
2133 if (pkt_dev->flags & F_FLOW_SEQ) {
2134 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2135 /* reset time */
2136 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002137 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002138 pkt_dev->curfl += 1;
2139 if (pkt_dev->curfl >= pkt_dev->cflows)
2140 pkt_dev->curfl = 0; /*reset */
2141 }
2142 } else {
2143 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002144 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002145
Robert Olsson1211a642008-08-05 18:44:26 -07002146 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002147 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002148 pkt_dev->flows[flow].flags = 0;
2149 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002150 }
2151
2152 return pkt_dev->curfl;
2153}
2154
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002155
2156#ifdef CONFIG_XFRM
2157/* If there was already an IPSEC SA, we keep it as is, else
2158 * we go look for it ...
2159*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002160static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002161{
2162 struct xfrm_state *x = pkt_dev->flows[flow].x;
2163 if (!x) {
2164 /*slow path: we dont already have xfrm_state*/
2165 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2166 (xfrm_address_t *)&pkt_dev->cur_saddr,
2167 AF_INET,
2168 pkt_dev->ipsmode,
2169 pkt_dev->ipsproto, 0);
2170 if (x) {
2171 pkt_dev->flows[flow].x = x;
2172 set_pkt_overhead(pkt_dev);
2173 pkt_dev->pkt_overhead+=x->props.header_len;
2174 }
2175
2176 }
2177}
2178#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002179static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2180{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002181
2182 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2183 pkt_dev->cur_queue_map = smp_processor_id();
2184
2185 else if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002186 __u16 t;
2187 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2188 t = random32() %
2189 (pkt_dev->queue_map_max -
2190 pkt_dev->queue_map_min + 1)
2191 + pkt_dev->queue_map_min;
2192 } else {
2193 t = pkt_dev->cur_queue_map + 1;
2194 if (t > pkt_dev->queue_map_max)
2195 t = pkt_dev->queue_map_min;
2196 }
2197 pkt_dev->cur_queue_map = t;
2198 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002199 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 -07002200}
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202/* Increment/randomize headers according to flags and current values
2203 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2204 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002205static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2206{
2207 __u32 imn;
2208 __u32 imx;
2209 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002211 if (pkt_dev->cflows)
2212 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002215 if (pkt_dev->src_mac_count > 1) {
2216 __u32 mc;
2217 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Luiz Capitulino222f1802006-03-20 22:16:13 -08002219 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002220 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002221 else {
2222 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002223 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002224 pkt_dev->src_mac_count)
2225 pkt_dev->cur_src_mac_offset = 0;
2226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Luiz Capitulino222f1802006-03-20 22:16:13 -08002228 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2229 pkt_dev->hh[11] = tmp;
2230 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2231 pkt_dev->hh[10] = tmp;
2232 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2233 pkt_dev->hh[9] = tmp;
2234 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2235 pkt_dev->hh[8] = tmp;
2236 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2237 pkt_dev->hh[7] = tmp;
2238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Luiz Capitulino222f1802006-03-20 22:16:13 -08002240 /* Deal with Destination MAC */
2241 if (pkt_dev->dst_mac_count > 1) {
2242 __u32 mc;
2243 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Luiz Capitulino222f1802006-03-20 22:16:13 -08002245 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002246 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Luiz Capitulino222f1802006-03-20 22:16:13 -08002248 else {
2249 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002250 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002251 pkt_dev->dst_mac_count) {
2252 pkt_dev->cur_dst_mac_offset = 0;
2253 }
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Luiz Capitulino222f1802006-03-20 22:16:13 -08002256 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2257 pkt_dev->hh[5] = tmp;
2258 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2259 pkt_dev->hh[4] = tmp;
2260 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2261 pkt_dev->hh[3] = tmp;
2262 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2263 pkt_dev->hh[2] = tmp;
2264 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2265 pkt_dev->hh[1] = tmp;
2266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002268 if (pkt_dev->flags & F_MPLS_RND) {
2269 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002270 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002271 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2272 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002273 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002274 htonl(0x000fffff));
2275 }
2276
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002277 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002278 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002279 }
2280
2281 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002282 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002283 }
2284
Luiz Capitulino222f1802006-03-20 22:16:13 -08002285 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2286 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002287 pkt_dev->cur_udp_src = random32() %
2288 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2289 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Luiz Capitulino222f1802006-03-20 22:16:13 -08002291 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 pkt_dev->cur_udp_src++;
2293 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2294 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002295 }
2296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Luiz Capitulino222f1802006-03-20 22:16:13 -08002298 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2299 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002300 pkt_dev->cur_udp_dst = random32() %
2301 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2302 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002303 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002305 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307 }
2308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 if (!(pkt_dev->flags & F_IPV6)) {
2311
Luiz Capitulino222f1802006-03-20 22:16:13 -08002312 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2313 ntohl(pkt_dev->
2314 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002317 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 else {
2319 t = ntohl(pkt_dev->cur_saddr);
2320 t++;
2321 if (t > imx) {
2322 t = imn;
2323 }
2324 }
2325 pkt_dev->cur_saddr = htonl(t);
2326 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002327
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002328 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2330 } else {
Al Viro252e3342006-11-14 20:48:11 -08002331 imn = ntohl(pkt_dev->daddr_min);
2332 imx = ntohl(pkt_dev->daddr_max);
2333 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002335 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (pkt_dev->flags & F_IPDST_RND) {
2337
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002338 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002339 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Joe Perches21cf2252007-12-16 13:44:00 -08002341 while (ipv4_is_loopback(s) ||
2342 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002343 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002344 ipv4_is_zeronet(s) ||
2345 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002346 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002347 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
Al Viro252e3342006-11-14 20:48:11 -08002349 pkt_dev->cur_daddr = s;
2350 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 t = ntohl(pkt_dev->cur_daddr);
2352 t++;
2353 if (t > imx) {
2354 t = imn;
2355 }
2356 pkt_dev->cur_daddr = htonl(t);
2357 }
2358 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002359 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002360 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002361 pkt_dev->flows[flow].cur_daddr =
2362 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002363#ifdef CONFIG_XFRM
2364 if (pkt_dev->flags & F_IPSEC_ON)
2365 get_ipsec_sa(pkt_dev, flow);
2366#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 pkt_dev->nflows++;
2368 }
2369 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002370 } else { /* IPV6 * */
2371
2372 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2373 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2374 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2375 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 else {
2377 int i;
2378
2379 /* Only random destinations yet */
2380
Luiz Capitulino222f1802006-03-20 22:16:13 -08002381 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002383 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002384 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2385 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389
Luiz Capitulino222f1802006-03-20 22:16:13 -08002390 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2391 __u32 t;
2392 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002393 t = random32() %
2394 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2395 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002396 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002398 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 }
2401 pkt_dev->cur_pkt_size = t;
2402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002404 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 pkt_dev->flows[flow].count++;
2407}
2408
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002409
2410#ifdef CONFIG_XFRM
2411static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2412{
2413 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2414 int err = 0;
2415 struct iphdr *iph;
2416
2417 if (!x)
2418 return 0;
2419 /* XXX: we dont support tunnel mode for now until
2420 * we resolve the dst issue */
2421 if (x->props.mode != XFRM_MODE_TRANSPORT)
2422 return 0;
2423
2424 spin_lock(&x->lock);
2425 iph = ip_hdr(skb);
2426
Herbert Xu13996372007-10-17 21:35:51 -07002427 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002428 if (err)
2429 goto error;
2430 err = x->type->output(x, skb);
2431 if (err)
2432 goto error;
2433
2434 x->curlft.bytes +=skb->len;
2435 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002436error:
2437 spin_unlock(&x->lock);
2438 return err;
2439}
2440
2441static inline void free_SAs(struct pktgen_dev *pkt_dev)
2442{
2443 if (pkt_dev->cflows) {
2444 /* let go of the SAs if we have them */
2445 int i = 0;
2446 for (; i < pkt_dev->nflows; i++){
2447 struct xfrm_state *x = pkt_dev->flows[i].x;
2448 if (x) {
2449 xfrm_state_put(x);
2450 pkt_dev->flows[i].x = NULL;
2451 }
2452 }
2453 }
2454}
2455
2456static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2457 struct sk_buff *skb, __be16 protocol)
2458{
2459 if (pkt_dev->flags & F_IPSEC_ON) {
2460 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2461 int nhead = 0;
2462 if (x) {
2463 int ret;
2464 __u8 *eth;
2465 nhead = x->props.header_len - skb_headroom(skb);
2466 if (nhead >0) {
2467 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2468 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002469 printk(KERN_ERR "Error expanding "
2470 "ipsec packet %d\n",ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002471 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002472 }
2473 }
2474
2475 /* ipsec is not expecting ll header */
2476 skb_pull(skb, ETH_HLEN);
2477 ret = pktgen_output_ipsec(skb, pkt_dev);
2478 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002479 printk(KERN_ERR "Error creating ipsec "
2480 "packet %d\n",ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002481 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002482 }
2483 /* restore ll */
2484 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2485 memcpy(eth, pkt_dev->hh, 12);
2486 *(u16 *) & eth[12] = protocol;
2487 }
2488 }
2489 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002490err:
2491 kfree_skb(skb);
2492 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002493}
2494#endif
2495
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002496static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2497{
2498 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002499 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002500 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2501 }
2502 mpls--;
2503 *mpls |= MPLS_STACK_BOTTOM;
2504}
2505
Al Viro0f37c602006-11-03 03:49:56 -08002506static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2507 unsigned int prio)
2508{
2509 return htons(id | (cfi << 12) | (prio << 13));
2510}
2511
Luiz Capitulino222f1802006-03-20 22:16:13 -08002512static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2513 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 struct sk_buff *skb = NULL;
2516 __u8 *eth;
2517 struct udphdr *udph;
2518 int datalen, iplen;
2519 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002520 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002521 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002522 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002523 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2524 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2525 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2526 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002527 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002528
2529 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002530 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002531
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002532 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002533 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002534
Robert Olsson64053be2005-06-26 15:27:10 -07002535 /* Update any of the values, used when we're incrementing various
2536 * fields.
2537 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002538 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002539 mod_cur_headers(pkt_dev);
2540
David S. Miller7ac54592006-01-18 14:19:10 -08002541 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002542 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002543 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (!skb) {
2545 sprintf(pkt_dev->result, "No memory");
2546 return NULL;
2547 }
2548
David S. Miller7ac54592006-01-18 14:19:10 -08002549 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 /* Reserve for ethernet and IP header */
2552 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002553 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2554 if (pkt_dev->nr_labels)
2555 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002556
2557 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002558 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002559 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002560 *svlan_tci = build_tci(pkt_dev->svlan_id,
2561 pkt_dev->svlan_cfi,
2562 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002563 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002564 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002565 }
2566 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002567 *vlan_tci = build_tci(pkt_dev->vlan_id,
2568 pkt_dev->vlan_cfi,
2569 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002570 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002571 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002572 }
2573
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002574 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002575 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002576 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002577 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002578 iph = ip_hdr(skb);
2579 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002582 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002584 /* Eth + IPh + UDPh + mpls */
2585 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002586 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002587 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 udph->source = htons(pkt_dev->cur_udp_src);
2591 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002592 udph->len = htons(datalen + 8); /* DATA + udphdr */
2593 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 iph->ihl = 5;
2596 iph->version = 4;
2597 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002598 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002599 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 iph->saddr = pkt_dev->cur_saddr;
2601 iph->daddr = pkt_dev->cur_daddr;
2602 iph->frag_off = 0;
2603 iplen = 20 + 8 + datalen;
2604 iph->tot_len = htons(iplen);
2605 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002606 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002607 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002608 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002609 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 skb->dev = odev;
2611 skb->pkt_type = PACKET_HOST;
2612
Luiz Capitulino222f1802006-03-20 22:16:13 -08002613 if (pkt_dev->nfrags <= 0)
2614 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 else {
2616 int frags = pkt_dev->nfrags;
2617 int i;
2618
Luiz Capitulino222f1802006-03-20 22:16:13 -08002619 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (frags > MAX_SKB_FRAGS)
2622 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002623 if (datalen > frags * PAGE_SIZE) {
2624 skb_put(skb, datalen - frags * PAGE_SIZE);
2625 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627
2628 i = 0;
2629 while (datalen > 0) {
2630 struct page *page = alloc_pages(GFP_KERNEL, 0);
2631 skb_shinfo(skb)->frags[i].page = page;
2632 skb_shinfo(skb)->frags[i].page_offset = 0;
2633 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002634 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 datalen -= skb_shinfo(skb)->frags[i].size;
2636 skb->len += skb_shinfo(skb)->frags[i].size;
2637 skb->data_len += skb_shinfo(skb)->frags[i].size;
2638 i++;
2639 skb_shinfo(skb)->nr_frags = i;
2640 }
2641
2642 while (i < frags) {
2643 int rem;
2644
2645 if (i == 0)
2646 break;
2647
2648 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2649 if (rem == 0)
2650 break;
2651
2652 skb_shinfo(skb)->frags[i - 1].size -= rem;
2653
Luiz Capitulino222f1802006-03-20 22:16:13 -08002654 skb_shinfo(skb)->frags[i] =
2655 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002657 skb_shinfo(skb)->frags[i].page =
2658 skb_shinfo(skb)->frags[i - 1].page;
2659 skb_shinfo(skb)->frags[i].page_offset +=
2660 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 skb_shinfo(skb)->frags[i].size = rem;
2662 i++;
2663 skb_shinfo(skb)->nr_frags = i;
2664 }
2665 }
2666
Luiz Capitulino222f1802006-03-20 22:16:13 -08002667 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Luiz Capitulino222f1802006-03-20 22:16:13 -08002669 if (pgh) {
2670 struct timeval timestamp;
2671
2672 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2673 pgh->seq_num = htonl(pkt_dev->seq_num);
2674
2675 do_gettimeofday(&timestamp);
2676 pgh->tv_sec = htonl(timestamp.tv_sec);
2677 pgh->tv_usec = htonl(timestamp.tv_usec);
2678 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002679
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002680#ifdef CONFIG_XFRM
2681 if (!process_ipsec(pkt_dev, skb, protocol))
2682 return NULL;
2683#endif
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 return skb;
2686}
2687
2688/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002689 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2691 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002692 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 * Should be candidate for net/ipv4/utils.c
2694 * --ro
2695 */
2696
Luiz Capitulino222f1802006-03-20 22:16:13 -08002697static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002700 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 unsigned long u;
2702 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002703 unsigned int prefixlen = 0;
2704 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002705 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002706 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Luiz Capitulino222f1802006-03-20 22:16:13 -08002708 for (i = 0; i < 16; i++)
2709 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711 for (;;) {
2712 if (*s == ':') {
2713 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002714 if (s[1] == ':') { /* Found "::", skip to part 2 */
2715 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 len++;
2717 break;
2718 }
2719 s++;
2720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002722 u = simple_strtoul(s, &pos, 16);
2723 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002724 if (!i)
2725 return 0;
2726 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 /* the last 4 bytes may be written as IPv4 address */
2729
2730 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002731 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2732 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
2734 ip[prefixlen++] = (u >> 8);
2735 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002736 s += i;
2737 len += i;
2738 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return len;
2740 }
2741
2742/* part 2, after "::" */
2743 for (;;) {
2744 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002745 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 break;
2747 s++;
2748 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002749 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002751
2752 u = simple_strtol(s, &pos, 16);
2753 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002755 if (*s)
2756 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 break;
2758 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002759 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002761 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2762 sizeof(tmp));
2763 suffixlen += 4;
2764 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 break;
2766 }
2767 suffix[suffixlen++] = (u >> 8);
2768 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002769 s += i;
2770 len += i;
2771 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 break;
2773 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002774 for (i = 0; i < suffixlen; i++)
2775 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return len;
2777}
2778
Luiz Capitulino222f1802006-03-20 22:16:13 -08002779static char tohex(char hexdigit)
2780{
2781 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
2783
Luiz Capitulino222f1802006-03-20 22:16:13 -08002784static int fmt_xlong(char *s, unsigned int i)
2785{
2786 char *bak = s;
2787 *s = tohex((i >> 12) & 0xf);
2788 if (s != bak || *s != '0')
2789 ++s;
2790 *s = tohex((i >> 8) & 0xf);
2791 if (s != bak || *s != '0')
2792 ++s;
2793 *s = tohex((i >> 4) & 0xf);
2794 if (s != bak || *s != '0')
2795 ++s;
2796 *s = tohex(i & 0xf);
2797 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
Luiz Capitulino222f1802006-03-20 22:16:13 -08002800static unsigned int fmt_ip6(char *s, const char ip[16])
2801{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 unsigned int len;
2803 unsigned int i;
2804 unsigned int temp;
2805 unsigned int compressing;
2806 int j;
2807
Luiz Capitulino222f1802006-03-20 22:16:13 -08002808 len = 0;
2809 compressing = 0;
2810 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002813 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2814 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2815 temp = strlen(s);
2816 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
2818#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002819 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2820 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (temp == 0) {
2822 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002823 compressing = 1;
2824 if (j == 0) {
2825 *s++ = ':';
2826 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828 }
2829 } else {
2830 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002831 compressing = 0;
2832 *s++ = ':';
2833 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002835 i = fmt_xlong(s, temp);
2836 len += i;
2837 s += i;
2838 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 *s++ = ':';
2840 ++len;
2841 }
2842 }
2843 }
2844 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002845 *s++ = ':';
2846 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002848 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return len;
2850}
2851
Luiz Capitulino222f1802006-03-20 22:16:13 -08002852static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2853 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 struct sk_buff *skb = NULL;
2856 __u8 *eth;
2857 struct udphdr *udph;
2858 int datalen;
2859 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002860 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002861 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002862 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002863 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2864 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2865 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2866 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002867 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002868
2869 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002870 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002871
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002872 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002873 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002874
Robert Olsson64053be2005-06-26 15:27:10 -07002875 /* Update any of the values, used when we're incrementing various
2876 * fields.
2877 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002878 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002879 mod_cur_headers(pkt_dev);
2880
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002881 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002882 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (!skb) {
2884 sprintf(pkt_dev->result, "No memory");
2885 return NULL;
2886 }
2887
2888 skb_reserve(skb, 16);
2889
2890 /* Reserve for ethernet and IP header */
2891 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002892 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2893 if (pkt_dev->nr_labels)
2894 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002895
2896 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002897 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002898 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002899 *svlan_tci = build_tci(pkt_dev->svlan_id,
2900 pkt_dev->svlan_cfi,
2901 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002902 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002903 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002904 }
2905 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002906 *vlan_tci = build_tci(pkt_dev->vlan_id,
2907 pkt_dev->vlan_cfi,
2908 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002909 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002910 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002911 }
2912
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002913 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002914 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002915 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002916 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002917 iph = ipv6_hdr(skb);
2918 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002921 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002922
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002923 /* Eth + IPh + UDPh + mpls */
2924 datalen = pkt_dev->cur_pkt_size - 14 -
2925 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002926 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Luiz Capitulino222f1802006-03-20 22:16:13 -08002928 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 datalen = sizeof(struct pktgen_hdr);
2930 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002931 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2932 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
2934
2935 udph->source = htons(pkt_dev->cur_udp_src);
2936 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002937 udph->len = htons(datalen + sizeof(struct udphdr));
2938 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002940 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002942 if (pkt_dev->traffic_class) {
2943 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002944 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002945 }
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 iph->hop_limit = 32;
2948
2949 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2950 iph->nexthdr = IPPROTO_UDP;
2951
2952 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2953 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2954
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002955 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002956 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002957 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 skb->dev = odev;
2959 skb->pkt_type = PACKET_HOST;
2960
Luiz Capitulino222f1802006-03-20 22:16:13 -08002961 if (pkt_dev->nfrags <= 0)
2962 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 else {
2964 int frags = pkt_dev->nfrags;
2965 int i;
2966
Luiz Capitulino222f1802006-03-20 22:16:13 -08002967 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (frags > MAX_SKB_FRAGS)
2970 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002971 if (datalen > frags * PAGE_SIZE) {
2972 skb_put(skb, datalen - frags * PAGE_SIZE);
2973 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
2975
2976 i = 0;
2977 while (datalen > 0) {
2978 struct page *page = alloc_pages(GFP_KERNEL, 0);
2979 skb_shinfo(skb)->frags[i].page = page;
2980 skb_shinfo(skb)->frags[i].page_offset = 0;
2981 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002982 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 datalen -= skb_shinfo(skb)->frags[i].size;
2984 skb->len += skb_shinfo(skb)->frags[i].size;
2985 skb->data_len += skb_shinfo(skb)->frags[i].size;
2986 i++;
2987 skb_shinfo(skb)->nr_frags = i;
2988 }
2989
2990 while (i < frags) {
2991 int rem;
2992
2993 if (i == 0)
2994 break;
2995
2996 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2997 if (rem == 0)
2998 break;
2999
3000 skb_shinfo(skb)->frags[i - 1].size -= rem;
3001
Luiz Capitulino222f1802006-03-20 22:16:13 -08003002 skb_shinfo(skb)->frags[i] =
3003 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005 skb_shinfo(skb)->frags[i].page =
3006 skb_shinfo(skb)->frags[i - 1].page;
3007 skb_shinfo(skb)->frags[i].page_offset +=
3008 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 skb_shinfo(skb)->frags[i].size = rem;
3010 i++;
3011 skb_shinfo(skb)->nr_frags = i;
3012 }
3013 }
3014
Luiz Capitulino222f1802006-03-20 22:16:13 -08003015 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003017 if (pgh) {
3018 struct timeval timestamp;
3019
3020 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3021 pgh->seq_num = htonl(pkt_dev->seq_num);
3022
3023 do_gettimeofday(&timestamp);
3024 pgh->tv_sec = htonl(timestamp.tv_sec);
3025 pgh->tv_usec = htonl(timestamp.tv_usec);
3026 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003027 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return skb;
3030}
3031
Luiz Capitulino222f1802006-03-20 22:16:13 -08003032static inline struct sk_buff *fill_packet(struct net_device *odev,
3033 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003035 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return fill_packet_ipv6(odev, pkt_dev);
3037 else
3038 return fill_packet_ipv4(odev, pkt_dev);
3039}
3040
Luiz Capitulino222f1802006-03-20 22:16:13 -08003041static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003043 pkt_dev->seq_num = 1;
3044 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003046 pkt_dev->tx_bytes = 0;
3047 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
3050/* Set up structure for sending pkts, clear counters */
3051
3052static void pktgen_run(struct pktgen_thread *t)
3053{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003054 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 int started = 0;
3056
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003057 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003060 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 /*
3063 * setup odev and create initial packet.
3064 */
3065 pktgen_setup_inject(pkt_dev);
3066
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003069 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 pkt_dev->skb = NULL;
3071 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003072 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 pkt_dev->next_tx_ns = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003074 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 strcpy(pkt_dev->result, "Starting");
3077 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003078 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 strcpy(pkt_dev->result, "Error starting");
3080 }
3081 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003082 if (started)
3083 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
3086static void pktgen_stop_all_threads_ifs(void)
3087{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003088 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003090 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003092 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003093
3094 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003095 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003096
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003097 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098}
3099
Luiz Capitulino222f1802006-03-20 22:16:13 -08003100static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003102 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003103 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003105 list_for_each_entry(pkt_dev, &t->if_list, list)
3106 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 res = 1;
3108 break;
3109 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003110 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Luiz Capitulino222f1802006-03-20 22:16:13 -08003113static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003115 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Luiz Capitulino222f1802006-03-20 22:16:13 -08003117 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Luiz Capitulino222f1802006-03-20 22:16:13 -08003119 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Luiz Capitulino222f1802006-03-20 22:16:13 -08003121 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123 if (signal_pending(current))
3124 goto signal;
3125 if_lock(t);
3126 }
3127 if_unlock(t);
3128 return 1;
3129signal:
3130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
3133static int pktgen_wait_all_threads_run(void)
3134{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003135 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003137
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003138 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003139
3140 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003142 if (sig == 0)
3143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003145
3146 if (sig == 0)
3147 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003149
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003150 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return sig;
3152}
3153
3154static void pktgen_run_all_threads(void)
3155{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003156 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003158 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003160 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003162 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003164
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003165 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Luiz Capitulino222f1802006-03-20 22:16:13 -08003167 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 pktgen_wait_all_threads_run();
3170}
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3173{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003174 __u64 total_us, bps, mbps, pps, idle;
3175 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Luiz Capitulino222f1802006-03-20 22:16:13 -08003177 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Luiz Capitulino222f1802006-03-20 22:16:13 -08003179 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Luiz Capitulino222f1802006-03-20 22:16:13 -08003181 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3182 (unsigned long long)total_us,
3183 (unsigned long long)(total_us - idle),
3184 (unsigned long long)idle,
3185 (unsigned long long)pkt_dev->sofar,
3186 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Luiz Capitulino222f1802006-03-20 22:16:13 -08003188 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Luiz Capitulino222f1802006-03-20 22:16:13 -08003190 while ((total_us >> 32) != 0) {
3191 pps >>= 1;
3192 total_us >>= 1;
3193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Luiz Capitulino222f1802006-03-20 22:16:13 -08003197 bps = pps * 8 * pkt_dev->cur_pkt_size;
3198
3199 mbps = bps;
3200 do_div(mbps, 1000000);
3201 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3202 (unsigned long long)pps,
3203 (unsigned long long)mbps,
3204 (unsigned long long)bps,
3205 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208/* Set stopped-at timer, remove from running list, do counters & statistics */
3209
Luiz Capitulino222f1802006-03-20 22:16:13 -08003210static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003212 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003213
Luiz Capitulino222f1802006-03-20 22:16:13 -08003214 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003215 printk(KERN_WARNING "pktgen: interface: %s is already "
3216 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003217 return -EINVAL;
3218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Luiz Capitulino222f1802006-03-20 22:16:13 -08003220 pkt_dev->stopped_at = getCurUs();
3221 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003223 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Luiz Capitulino222f1802006-03-20 22:16:13 -08003225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
Luiz Capitulino222f1802006-03-20 22:16:13 -08003228static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003230 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if_lock(t);
3233
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003234 list_for_each_entry(pkt_dev, &t->if_list, list) {
3235 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003236 continue;
3237 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003238 best = pkt_dev;
3239 else if (pkt_dev->next_tx_us < best->next_tx_us)
3240 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
3242 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
Luiz Capitulino222f1802006-03-20 22:16:13 -08003246static void pktgen_stop(struct pktgen_thread *t)
3247{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003248 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003250 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Luiz Capitulino222f1802006-03-20 22:16:13 -08003252 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003254 list_for_each_entry(pkt_dev, &t->if_list, list) {
3255 pktgen_stop_device(pkt_dev);
3256 if (pkt_dev->skb)
3257 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003258
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003259 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Luiz Capitulino222f1802006-03-20 22:16:13 -08003262 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003265/*
3266 * one of our devices needs to be removed - find it
3267 * and remove it
3268 */
3269static void pktgen_rem_one_if(struct pktgen_thread *t)
3270{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003271 struct list_head *q, *n;
3272 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003273
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003274 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003275
3276 if_lock(t);
3277
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003278 list_for_each_safe(q, n, &t->if_list) {
3279 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003280
Luiz Capitulino222f1802006-03-20 22:16:13 -08003281 if (!cur->removal_mark)
3282 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003283
3284 if (cur->skb)
3285 kfree_skb(cur->skb);
3286 cur->skb = NULL;
3287
3288 pktgen_remove_device(t, cur);
3289
3290 break;
3291 }
3292
3293 if_unlock(t);
3294}
3295
Luiz Capitulino222f1802006-03-20 22:16:13 -08003296static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003298 struct list_head *q, *n;
3299 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003300
3301 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003302
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003303 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003304 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003306 list_for_each_safe(q, n, &t->if_list) {
3307 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003308
3309 if (cur->skb)
3310 kfree_skb(cur->skb);
3311 cur->skb = NULL;
3312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 pktgen_remove_device(t, cur);
3314 }
3315
Luiz Capitulino222f1802006-03-20 22:16:13 -08003316 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
Luiz Capitulino222f1802006-03-20 22:16:13 -08003319static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003321 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
David S. Milleree74baa2007-01-01 20:51:53 -08003323 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003325 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003327 list_del(&t->th_list);
3328
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003329 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
3332static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3333{
3334 struct net_device *odev = NULL;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003335 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 __u64 idle_start = 0;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003337 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 int ret;
3339
3340 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3343 u64 now;
3344
3345 now = getCurUs();
3346 if (now < pkt_dev->next_tx_us)
3347 spin(pkt_dev, pkt_dev->next_tx_us);
3348
3349 /* This is max DELAY, this has special meaning of
3350 * "never transmit"
3351 */
3352 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3353 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3354 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3355 goto out;
3356 }
3357 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003358
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003359 if (!pkt_dev->skb) {
3360 set_cur_queue_map(pkt_dev);
3361 queue_map = pkt_dev->cur_queue_map;
3362 } else {
3363 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3364 }
3365
3366 txq = netdev_get_tx_queue(odev, queue_map);
3367 if (netif_tx_queue_stopped(txq) ||
David S. Millerc3f26a22008-07-31 16:58:50 -07003368 netif_tx_queue_frozen(txq) ||
Robert Olsson378be2c2007-08-28 15:43:14 -07003369 need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 if (!netif_running(odev)) {
3373 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003374 if (pkt_dev->skb)
3375 kfree_skb(pkt_dev->skb);
3376 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 goto out;
3378 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003379 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003383
David S. Millerc3f26a22008-07-31 16:58:50 -07003384 if (netif_tx_queue_stopped(txq) ||
3385 netif_tx_queue_frozen(txq)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003386 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003388 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
3390 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003393 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3394 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003396 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 pkt_dev->skb = fill_packet(odev, pkt_dev);
3400 if (pkt_dev->skb == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003401 printk(KERN_ERR "pktgen: ERROR: couldn't "
3402 "allocate skb in fill_packet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003404 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 goto out;
3406 }
3407 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003408 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 }
3410 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003411
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003412 /* fill_packet() might have changed the queue */
3413 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3414 txq = netdev_get_tx_queue(odev, queue_map);
3415
3416 __netif_tx_lock_bh(txq);
David S. Millerc3f26a22008-07-31 16:58:50 -07003417 if (!netif_tx_queue_stopped(txq) &&
3418 !netif_tx_queue_frozen(txq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
3420 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003421 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3423 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003424 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 pkt_dev->sofar++;
3426 pkt_dev->seq_num++;
3427 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003428
3429 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 && (odev->features & NETIF_F_LLTX)) {
3431 cpu_relax();
3432 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003433 } else { /* Retry it next time */
3434
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 if (debug && net_ratelimit())
3438 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 pkt_dev->errors++;
3441 pkt_dev->last_ok = 0;
3442 }
3443
3444 pkt_dev->next_tx_us = getCurUs();
3445 pkt_dev->next_tx_ns = 0;
3446
3447 pkt_dev->next_tx_us += pkt_dev->delay_us;
3448 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3449
3450 if (pkt_dev->next_tx_ns > 1000) {
3451 pkt_dev->next_tx_us++;
3452 pkt_dev->next_tx_ns -= 1000;
3453 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Luiz Capitulino222f1802006-03-20 22:16:13 -08003456 else { /* Retry it next time */
3457 pkt_dev->last_ok = 0;
3458 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003462 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 /* If pkt_dev->count is zero, then run forever */
3465 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3466 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3467 idle_start = getCurUs();
3468 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3469 if (signal_pending(current)) {
3470 break;
3471 }
3472 schedule();
3473 }
3474 pkt_dev->idle_acc += getCurUs() - idle_start;
3475 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 /* Done with this */
3478 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003479 if (pkt_dev->skb)
3480 kfree_skb(pkt_dev->skb);
3481 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003482 }
3483out:;
3484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 * Main loop of the thread goes here
3488 */
3489
David S. Milleree74baa2007-01-01 20:51:53 -08003490static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003493 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003494 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
David S. Milleree74baa2007-01-01 20:51:53 -08003497 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
3499 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003500 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003502 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
David S. Milleree74baa2007-01-01 20:51:53 -08003504 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003505
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003506 set_freezable();
3507
David S. Milleree74baa2007-01-01 20:51:53 -08003508 while (!kthread_should_stop()) {
3509 pkt_dev = next_to_run(t);
3510
3511 if (!pkt_dev &&
3512 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3513 == 0) {
3514 prepare_to_wait(&(t->queue), &wait,
3515 TASK_INTERRUPTIBLE);
3516 schedule_timeout(HZ / 10);
3517 finish_wait(&(t->queue), &wait);
3518 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 __set_current_state(TASK_RUNNING);
3521
Robert Olssonb1639112007-08-28 15:46:58 -07003522 if (pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 pktgen_xmit(pkt_dev);
3524
Luiz Capitulino222f1802006-03-20 22:16:13 -08003525 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 pktgen_stop(t);
3527 t->control &= ~(T_STOP);
3528 }
3529
Luiz Capitulino222f1802006-03-20 22:16:13 -08003530 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 pktgen_run(t);
3532 t->control &= ~(T_RUN);
3533 }
3534
Luiz Capitulino222f1802006-03-20 22:16:13 -08003535 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003537 t->control &= ~(T_REMDEVALL);
3538 }
3539
Luiz Capitulino222f1802006-03-20 22:16:13 -08003540 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003541 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 t->control &= ~(T_REMDEV);
3543 }
3544
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003545 try_to_freeze();
3546
David S. Milleree74baa2007-01-01 20:51:53 -08003547 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003550 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003551 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003553 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003554 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003556 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003557 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003558
David S. Milleree74baa2007-01-01 20:51:53 -08003559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
Luiz Capitulino222f1802006-03-20 22:16:13 -08003562static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3563 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003565 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003568 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003569 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003570 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003571 break;
3572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Luiz Capitulino222f1802006-03-20 22:16:13 -08003574 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003575 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003576 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003579/*
3580 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 */
3582
Luiz Capitulino222f1802006-03-20 22:16:13 -08003583static int add_dev_to_thread(struct pktgen_thread *t,
3584 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585{
3586 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 if_lock(t);
3589
3590 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003591 printk(KERN_ERR "pktgen: ERROR: already assigned "
3592 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003593 rv = -EBUSY;
3594 goto out;
3595 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003596
3597 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003598 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 pkt_dev->running = 0;
3600
Luiz Capitulino222f1802006-03-20 22:16:13 -08003601out:
3602 if_unlock(t);
3603 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
3606/* Called under thread lock */
3607
Luiz Capitulino222f1802006-03-20 22:16:13 -08003608static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003610 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003611 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 /* We don't allow a device to be on several threads */
3614
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003615 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3616 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003617 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003618 return -EBUSY;
3619 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003620
3621 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3622 if (!pkt_dev)
3623 return -ENOMEM;
3624
Luiz Capitulino222f1802006-03-20 22:16:13 -08003625 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003626 if (pkt_dev->flows == NULL) {
3627 kfree(pkt_dev);
3628 return -ENOMEM;
3629 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003630 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003631
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003632 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003633 pkt_dev->min_pkt_size = ETH_ZLEN;
3634 pkt_dev->max_pkt_size = ETH_ZLEN;
3635 pkt_dev->nfrags = 0;
3636 pkt_dev->clone_skb = pg_clone_skb_d;
3637 pkt_dev->delay_us = pg_delay_d / 1000;
3638 pkt_dev->delay_ns = pg_delay_d % 1000;
3639 pkt_dev->count = pg_count_d;
3640 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003641 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003642 pkt_dev->udp_src_max = 9;
3643 pkt_dev->udp_dst_min = 9;
3644 pkt_dev->udp_dst_max = 9;
3645
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003646 pkt_dev->vlan_p = 0;
3647 pkt_dev->vlan_cfi = 0;
3648 pkt_dev->vlan_id = 0xffff;
3649 pkt_dev->svlan_p = 0;
3650 pkt_dev->svlan_cfi = 0;
3651 pkt_dev->svlan_id = 0xffff;
3652
Stephen Hemminger39df2322007-03-04 16:11:51 -08003653 err = pktgen_setup_dev(pkt_dev, ifname);
3654 if (err)
3655 goto out1;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003656
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003657 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3658 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003659 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003660 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003661 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003662 err = -EINVAL;
3663 goto out2;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003664 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003665#ifdef CONFIG_XFRM
3666 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3667 pkt_dev->ipsproto = IPPROTO_ESP;
3668#endif
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003669
3670 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003671out2:
3672 dev_put(pkt_dev->odev);
3673out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003674#ifdef CONFIG_XFRM
3675 free_SAs(pkt_dev);
3676#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -08003677 if (pkt_dev->flows)
3678 vfree(pkt_dev->flows);
3679 kfree(pkt_dev);
3680 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
David S. Milleree74baa2007-01-01 20:51:53 -08003683static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003685 struct pktgen_thread *t;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003686 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003687 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Luiz Capitulino222f1802006-03-20 22:16:13 -08003689 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3690 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003691 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3692 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003693 return -ENOMEM;
3694 }
3695
Luiz Capitulino222f1802006-03-20 22:16:13 -08003696 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003698
David S. Milleree74baa2007-01-01 20:51:53 -08003699 INIT_LIST_HEAD(&t->if_list);
3700
3701 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003702 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003703
3704 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3705 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003706 printk(KERN_ERR "pktgen: kernel_thread() failed "
3707 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003708 list_del(&t->th_list);
3709 kfree(t);
3710 return PTR_ERR(p);
3711 }
3712 kthread_bind(p, cpu);
3713 t->tsk = p;
3714
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003715 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3716 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003717 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003718 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003719 PG_PROC_DIR, t->tsk->comm);
3720 kthread_stop(p);
3721 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003722 kfree(t);
3723 return -EINVAL;
3724 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003725
David S. Milleree74baa2007-01-01 20:51:53 -08003726 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003727 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
3729 return 0;
3730}
3731
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003732/*
3733 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003735static void _rem_dev_from_if_list(struct pktgen_thread *t,
3736 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003738 struct list_head *q, *n;
3739 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003741 list_for_each_safe(q, n, &t->if_list) {
3742 p = list_entry(q, struct pktgen_dev, list);
3743 if (p == pkt_dev)
3744 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
3746}
3747
Luiz Capitulino222f1802006-03-20 22:16:13 -08003748static int pktgen_remove_device(struct pktgen_thread *t,
3749 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003752 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Luiz Capitulino222f1802006-03-20 22:16:13 -08003754 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003755 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3756 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003757 pktgen_stop_device(pkt_dev);
3758 }
3759
3760 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762 if (pkt_dev->odev) {
3763 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003764 pkt_dev->odev = NULL;
3765 }
3766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 /* And update the thread if_list */
3768
3769 _rem_dev_from_if_list(t, pkt_dev);
3770
Stephen Hemminger39df2322007-03-04 16:11:51 -08003771 if (pkt_dev->entry)
3772 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003774#ifdef CONFIG_XFRM
3775 free_SAs(pkt_dev);
3776#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 if (pkt_dev->flows)
3778 vfree(pkt_dev->flows);
3779 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
3782
Luiz Capitulino222f1802006-03-20 22:16:13 -08003783static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
3785 int cpu;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003786 struct proc_dir_entry *pe;
3787
David S. Miller25a8b252007-07-30 16:11:48 -07003788 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003790 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003791 if (!pg_proc_dir)
3792 return -ENODEV;
3793 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Wang Chen25296d52008-02-28 14:11:49 -08003795 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003796 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003797 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3798 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003799 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003800 return -EINVAL;
3801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 /* Register us to receive netdevice events */
3804 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003805
John Hawkes670c02c2005-10-13 09:30:31 -07003806 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003807 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
David S. Milleree74baa2007-01-01 20:51:53 -08003809 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003810 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003811 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3812 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003813 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003814
3815 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003816 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3817 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003818 unregister_netdevice_notifier(&pktgen_notifier_block);
3819 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003820 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003821 return -ENODEV;
3822 }
3823
Luiz Capitulino222f1802006-03-20 22:16:13 -08003824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825}
3826
3827static void __exit pg_cleanup(void)
3828{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003829 struct pktgen_thread *t;
3830 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 wait_queue_head_t queue;
3832 init_waitqueue_head(&queue);
3833
Luiz Capitulino222f1802006-03-20 22:16:13 -08003834 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003836 list_for_each_safe(q, n, &pktgen_threads) {
3837 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003838 kthread_stop(t->tsk);
3839 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
Luiz Capitulino222f1802006-03-20 22:16:13 -08003842 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 unregister_netdevice_notifier(&pktgen_notifier_block);
3844
Luiz Capitulino222f1802006-03-20 22:16:13 -08003845 /* Clean up proc file system */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003846 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003847 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848}
3849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850module_init(pg_init);
3851module_exit(pg_cleanup);
3852
3853MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3854MODULE_DESCRIPTION("Packet Generator tool");
3855MODULE_LICENSE("GPL");
3856module_param(pg_count_d, int, 0);
3857module_param(pg_delay_d, int, 0);
3858module_param(pg_clone_skb_d, int, 0);
3859module_param(debug, int, 0);