blob: f07bd590f8f9f8c3b59027a1b811271f28227790 [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>
9 * Jens Låås <jens.laas@data.slu.se>
10 *
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 Hemmingerd50a6b562005-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>
164#include <asm/bitops.h>
165#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 Olssonb1639112007-08-28 15:46:58 -0700171#define VERSION "pktgen v2.69: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* The buckets are exponential in 'width' */
174#define LAT_BUCKETS_MAX 32
175#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800176#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800177#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800180#define F_IPSRC_RND (1<<0) /* IP-Src Random */
181#define F_IPDST_RND (1<<1) /* IP-Dst Random */
182#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
183#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
184#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
185#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
186#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
187#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800188#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700189#define F_VID_RND (1<<9) /* Random VLAN ID */
190#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700191#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700192#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700193#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800196#define T_TERMINATE (1<<0)
197#define T_STOP (1<<1) /* Stop run */
198#define T_RUN (1<<2) /* Start run */
199#define T_REMDEVALL (1<<3) /* Remove all devs */
200#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* If lock -- can be removed after some work */
203#define if_lock(t) spin_lock(&(t->if_lock));
204#define if_unlock(t) spin_unlock(&(t->if_lock));
205
206/* Used to help with determining the pkts on receive */
207#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700208#define PG_PROC_DIR "pktgen"
209#define PGCTRL "pgctrl"
210static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212#define MAX_CFLOWS 65536
213
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700214#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
215#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
216
Luiz Capitulino222f1802006-03-20 22:16:13 -0800217struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800218 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800219 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700220#ifdef CONFIG_XFRM
221 struct xfrm_state *x;
222#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700223 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700226/* flow flag bits */
227#define F_INIT (1<<0) /* flow has been initialized */
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /*
231 * Try to keep frequent/infrequent used vars. separated.
232 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800233 struct proc_dir_entry *entry; /* proc file */
234 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800235 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Luiz Capitulino222f1802006-03-20 22:16:13 -0800237 int running; /* if this changes to false, the test will stop */
238
239 /* If min != max, then we will either do a linear iteration, or
240 * we will do a random selection from within the range.
241 */
242 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800243 int removal_mark; /* non-zero => the device is marked for
244 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Luiz Capitulino222f1802006-03-20 22:16:13 -0800246 int min_pkt_size; /* = ETH_ZLEN; */
247 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700248 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800249 int nfrags;
250 __u32 delay_us; /* Default delay */
251 __u32 delay_ns;
252 __u64 count; /* Default No packets to send */
253 __u64 sofar; /* How many pkts we've sent so far */
254 __u64 tx_bytes; /* How many bytes we've transmitted */
255 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Luiz Capitulino222f1802006-03-20 22:16:13 -0800257 /* runtime counters relating to clone_skb */
258 __u64 next_tx_us; /* timestamp of when to tx next */
259 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Luiz Capitulino222f1802006-03-20 22:16:13 -0800261 __u64 allocated_skbs;
262 __u32 clone_count;
263 int last_ok; /* Was last skb sent?
264 * Or a failed transmit of some sort? This will keep
265 * sequence numbers in order, for example.
266 */
267 __u64 started_at; /* micro-seconds */
268 __u64 stopped_at; /* micro-seconds */
269 __u64 idle_acc; /* micro-seconds */
270 __u32 seq_num;
271
272 int clone_skb; /* Use multiple SKBs during packet gen. If this number
273 * is greater than 1, then that many copies of the same
274 * packet will be sent before a new packet is allocated.
275 * For instance, if you want to send 1024 identical packets
276 * before creating a new packet, set clone_skb to 1024.
277 */
278
279 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
283
284 struct in6_addr in6_saddr;
285 struct in6_addr in6_daddr;
286 struct in6_addr cur_in6_daddr;
287 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800289 struct in6_addr min_in6_daddr;
290 struct in6_addr max_in6_daddr;
291 struct in6_addr min_in6_saddr;
292 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Luiz Capitulino222f1802006-03-20 22:16:13 -0800294 /* If we're doing ranges, random or incremental, then this
295 * defines the min/max for those ranges.
296 */
Al Viro252e3342006-11-14 20:48:11 -0800297 __be32 saddr_min; /* inclusive, source IP address */
298 __be32 saddr_max; /* exclusive, source IP address */
299 __be32 daddr_min; /* inclusive, dest IP address */
300 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Luiz Capitulino222f1802006-03-20 22:16:13 -0800302 __u16 udp_src_min; /* inclusive, source UDP port */
303 __u16 udp_src_max; /* exclusive, source UDP port */
304 __u16 udp_dst_min; /* inclusive, dest UDP port */
305 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700307 /* DSCP + ECN */
308 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
309 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
310
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800311 /* MPLS */
312 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
313 __be32 labels[MAX_MPLS_LABELS];
314
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700315 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
316 __u8 vlan_p;
317 __u8 vlan_cfi;
318 __u16 vlan_id; /* 0xffff means no vlan tag */
319
320 __u8 svlan_p;
321 __u8 svlan_cfi;
322 __u16 svlan_id; /* 0xffff means no svlan tag */
323
Luiz Capitulino222f1802006-03-20 22:16:13 -0800324 __u32 src_mac_count; /* How many MACs to iterate through */
325 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Luiz Capitulino222f1802006-03-20 22:16:13 -0800327 unsigned char dst_mac[ETH_ALEN];
328 unsigned char src_mac[ETH_ALEN];
329
330 __u32 cur_dst_mac_offset;
331 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800332 __be32 cur_saddr;
333 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800334 __u16 cur_udp_dst;
335 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700336 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800337 __u32 cur_pkt_size;
338
339 __u8 hh[14];
340 /* = {
341 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
342
343 We fill in SRC address later
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x08, 0x00
346 };
347 */
348 __u16 pad; /* pad out the hh struct to an even 16 bytes */
349
350 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
351 * are transmitting the same one multiple times
352 */
353 struct net_device *odev; /* The out-going device. Note that the device should
354 * have it's pg_info pointer pointing back to this
355 * device. This will be set when the user specifies
356 * the out-going device name (not when the inject is
357 * started as it used to do.)
358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800360 unsigned cflows; /* Concurrent flows (config) */
361 unsigned lflow; /* Flow length (config) */
362 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700363 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700364
365 u16 queue_map_min;
366 u16 queue_map_max;
367
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700368#ifdef CONFIG_XFRM
369 __u8 ipsmode; /* IPSEC mode (config) */
370 __u8 ipsproto; /* IPSEC type (config) */
371#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800372 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
375struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800376 __be32 pgh_magic;
377 __be32 seq_num;
378 __be32 tv_sec;
379 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380};
381
382struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800383 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800384 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800385 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800386 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800387 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Luiz Capitulino222f1802006-03-20 22:16:13 -0800389 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
390
391 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int cpu;
393
Luiz Capitulino222f1802006-03-20 22:16:13 -0800394 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
397#define REMOVE 1
398#define FIND 0
399
400/* This code works around the fact that do_div cannot handle two 64-bit
401 numbers, and regular 64-bit division doesn't work on x86 kernels.
402 --Ben
403*/
404
405#define PG_DIV 0
406
407/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
408 * Function copied/adapted/optimized from:
409 *
410 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
411 *
412 * Copyright 1994, University of Cambridge Computer Laboratory
413 * All Rights Reserved.
414 *
415 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700416static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800418 u64 a = (x < 0) ? -x : x;
419 u64 b = (y < 0) ? -y : y;
420 u64 res = 0, d = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Luiz Capitulino222f1802006-03-20 22:16:13 -0800422 if (b > 0) {
423 while (b < a) {
424 b <<= 1;
425 d <<= 1;
426 }
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Luiz Capitulino222f1802006-03-20 22:16:13 -0800429 do {
430 if (a >= b) {
431 a -= b;
432 res += d;
433 }
434 b >>= 1;
435 d >>= 1;
436 }
437 while (d);
438
439 if (PG_DIV == type) {
440 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
441 } else {
442 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/* End of hacks to deal with 64-bit math on x86 */
447
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700448/** Convert to milliseconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800449static inline __u64 tv_to_ms(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800451 __u64 ms = tv->tv_usec / 1000;
452 ms += (__u64) tv->tv_sec * (__u64) 1000;
453 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800457static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800459 __u64 us = tv->tv_usec;
460 us += (__u64) tv->tv_sec * (__u64) 1000000;
461 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Luiz Capitulino222f1802006-03-20 22:16:13 -0800464static inline __u64 pg_div(__u64 n, __u32 base)
465{
466 __u64 tmp = n;
467 do_div(tmp, base);
468 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
469 n, base, tmp); */
470 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Luiz Capitulino222f1802006-03-20 22:16:13 -0800473static inline __u64 pg_div64(__u64 n, __u64 base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800475 __u64 tmp = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/*
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700477 * How do we know if the architecture we are running on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * supports division with 64 bit base?
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900479 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800481#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Luiz Capitulino222f1802006-03-20 22:16:13 -0800483 do_div(tmp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800485 tmp = divremdi3(n, base, PG_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -0800487 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Luiz Capitulino222f1802006-03-20 22:16:13 -0800490static inline __u64 getCurMs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800492 struct timeval tv;
493 do_gettimeofday(&tv);
494 return tv_to_ms(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Luiz Capitulino222f1802006-03-20 22:16:13 -0800497static inline __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800499 struct timeval tv;
500 do_gettimeofday(&tv);
501 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Luiz Capitulino222f1802006-03-20 22:16:13 -0800504static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800506 return tv_to_us(a) - tv_to_us(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/* old include end */
510
511static char version[] __initdata = VERSION;
512
Luiz Capitulino222f1802006-03-20 22:16:13 -0800513static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
514static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
515static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
516 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
518static void pktgen_run_all_threads(void);
519static void pktgen_stop_all_threads_ifs(void);
520static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800521static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800523
Luiz Capitulino222f1802006-03-20 22:16:13 -0800524static unsigned int scan_ip6(const char *s, char ip[16]);
525static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800528static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb972005-12-22 12:51:46 -0800529static int pg_delay_d;
530static int pg_clone_skb_d;
531static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Luiz Capitulino222fa072006-03-20 22:24:27 -0800533static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800534static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536static struct notifier_block pktgen_notifier_block = {
537 .notifier_call = pktgen_device_event,
538};
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900541 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *
543 */
544
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700545static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800546{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700547 seq_puts(seq, VERSION);
548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Luiz Capitulino222f1802006-03-20 22:16:13 -0800551static ssize_t pgctrl_write(struct file *file, const char __user * buf,
552 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700555 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Luiz Capitulino222f1802006-03-20 22:16:13 -0800557 if (!capable(CAP_NET_ADMIN)) {
558 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700562 if (count > sizeof(data))
563 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700566 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700567 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800568 }
569 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Luiz Capitulino222f1802006-03-20 22:16:13 -0800571 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 pktgen_stop_all_threads_ifs();
573
Luiz Capitulino222f1802006-03-20 22:16:13 -0800574 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 pktgen_run_all_threads();
576
Luiz Capitulino222f1802006-03-20 22:16:13 -0800577 else
David S. Miller25a8b252007-07-30 16:11:48 -0700578 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 err = count;
581
Luiz Capitulino222f1802006-03-20 22:16:13 -0800582out:
583 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700586static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700588 return single_open(file, pgctrl_show, PDE(inode)->data);
589}
590
Arjan van de Ven9a321442007-02-12 00:55:35 -0800591static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800592 .owner = THIS_MODULE,
593 .open = pgctrl_open,
594 .read = seq_read,
595 .llseek = seq_lseek,
596 .write = pgctrl_write,
597 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700598};
599
600static int pktgen_if_show(struct seq_file *seq, void *v)
601{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800602 struct pktgen_dev *pkt_dev = seq->private;
603 __u64 sa;
604 __u64 stopped;
605 __u64 now = getCurUs();
Joe Perches0795af52007-10-03 17:59:30 -0700606 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Luiz Capitulino222f1802006-03-20 22:16:13 -0800608 seq_printf(seq,
609 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
610 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
611 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Luiz Capitulino222f1802006-03-20 22:16:13 -0800613 seq_printf(seq,
614 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
615 pkt_dev->nfrags,
616 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800617 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Luiz Capitulino222f1802006-03-20 22:16:13 -0800619 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
620 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Robert Olsson45b270f2007-08-28 15:45:55 -0700622 seq_printf(seq,
623 " queue_map_min: %u queue_map_max: %u\n",
624 pkt_dev->queue_map_min,
625 pkt_dev->queue_map_max);
626
Luiz Capitulino222f1802006-03-20 22:16:13 -0800627 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800629 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
630 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
631 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
632 seq_printf(seq,
633 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
634 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Luiz Capitulino222f1802006-03-20 22:16:13 -0800636 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
637 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
638 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
639 seq_printf(seq,
640 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
641 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Luiz Capitulino222f1802006-03-20 22:16:13 -0800643 } else
644 seq_printf(seq,
645 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
646 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
647 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700649 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Joe Perches0795af52007-10-03 17:59:30 -0700651 seq_printf(seq, "%s ",
652 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
653 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Luiz Capitulino222f1802006-03-20 22:16:13 -0800655 seq_printf(seq, "dst_mac: ");
Joe Perches0795af52007-10-03 17:59:30 -0700656 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Luiz Capitulino222f1802006-03-20 22:16:13 -0800658 seq_printf(seq,
659 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
660 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
661 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Luiz Capitulino222f1802006-03-20 22:16:13 -0800663 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800664 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700665 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800667 if (pkt_dev->nr_labels) {
668 unsigned i;
669 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700670 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800671 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
672 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
673 }
674
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700675 if (pkt_dev->vlan_id != 0xffff) {
676 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
677 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
678 }
679
680 if (pkt_dev->svlan_id != 0xffff) {
681 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
682 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
683 }
684
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700685 if (pkt_dev->tos) {
686 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
687 }
688
689 if (pkt_dev->traffic_class) {
690 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
691 }
692
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800693 seq_printf(seq, " Flags: ");
694
Luiz Capitulino222f1802006-03-20 22:16:13 -0800695 if (pkt_dev->flags & F_IPV6)
696 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Luiz Capitulino222f1802006-03-20 22:16:13 -0800698 if (pkt_dev->flags & F_IPSRC_RND)
699 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Luiz Capitulino222f1802006-03-20 22:16:13 -0800701 if (pkt_dev->flags & F_IPDST_RND)
702 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Luiz Capitulino222f1802006-03-20 22:16:13 -0800704 if (pkt_dev->flags & F_TXSIZE_RND)
705 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Luiz Capitulino222f1802006-03-20 22:16:13 -0800707 if (pkt_dev->flags & F_UDPSRC_RND)
708 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Luiz Capitulino222f1802006-03-20 22:16:13 -0800710 if (pkt_dev->flags & F_UDPDST_RND)
711 seq_printf(seq, "UDPDST_RND ");
712
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800713 if (pkt_dev->flags & F_MPLS_RND)
714 seq_printf(seq, "MPLS_RND ");
715
Robert Olsson45b270f2007-08-28 15:45:55 -0700716 if (pkt_dev->flags & F_QUEUE_MAP_RND)
717 seq_printf(seq, "QUEUE_MAP_RND ");
718
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700719 if (pkt_dev->cflows) {
720 if (pkt_dev->flags & F_FLOW_SEQ)
721 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
722 else
723 seq_printf(seq, "FLOW_RND ");
724 }
725
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700726#ifdef CONFIG_XFRM
727 if (pkt_dev->flags & F_IPSEC_ON)
728 seq_printf(seq, "IPSEC ");
729#endif
730
Luiz Capitulino222f1802006-03-20 22:16:13 -0800731 if (pkt_dev->flags & F_MACSRC_RND)
732 seq_printf(seq, "MACSRC_RND ");
733
734 if (pkt_dev->flags & F_MACDST_RND)
735 seq_printf(seq, "MACDST_RND ");
736
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700737 if (pkt_dev->flags & F_VID_RND)
738 seq_printf(seq, "VID_RND ");
739
740 if (pkt_dev->flags & F_SVID_RND)
741 seq_printf(seq, "SVID_RND ");
742
Luiz Capitulino222f1802006-03-20 22:16:13 -0800743 seq_puts(seq, "\n");
744
745 sa = pkt_dev->started_at;
746 stopped = pkt_dev->stopped_at;
747 if (pkt_dev->running)
748 stopped = now; /* not really stopped, more like last-running-at */
749
750 seq_printf(seq,
751 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
752 (unsigned long long)pkt_dev->sofar,
753 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
754 (unsigned long long)stopped,
755 (unsigned long long)pkt_dev->idle_acc);
756
757 seq_printf(seq,
758 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700759 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
760 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Luiz Capitulino222f1802006-03-20 22:16:13 -0800762 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800764 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
765 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
766 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
767 } else
768 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700769 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Luiz Capitulino222f1802006-03-20 22:16:13 -0800771 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700772 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Robert Olsson45b270f2007-08-28 15:45:55 -0700774 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
775
Luiz Capitulino222f1802006-03-20 22:16:13 -0800776 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800779 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800781 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800786
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700787static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800788{
789 int i = 0;
790 *num = 0;
791
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700792 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800793 char c;
794 *num <<= 4;
795 if (get_user(c, &user_buffer[i]))
796 return -EFAULT;
797 if ((c >= '0') && (c <= '9'))
798 *num |= c - '0';
799 else if ((c >= 'a') && (c <= 'f'))
800 *num |= c - 'a' + 10;
801 else if ((c >= 'A') && (c <= 'F'))
802 *num |= c - 'A' + 10;
803 else
804 break;
805 }
806 return i;
807}
808
Luiz Capitulino222f1802006-03-20 22:16:13 -0800809static int count_trail_chars(const char __user * user_buffer,
810 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 int i;
813
814 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800815 char c;
816 if (get_user(c, &user_buffer[i]))
817 return -EFAULT;
818 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 case '\"':
820 case '\n':
821 case '\r':
822 case '\t':
823 case ' ':
824 case '=':
825 break;
826 default:
827 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830done:
831 return i;
832}
833
Luiz Capitulino222f1802006-03-20 22:16:13 -0800834static unsigned long num_arg(const char __user * user_buffer,
835 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 int i = 0;
838 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800839
840 for (; i < maxlen; i++) {
841 char c;
842 if (get_user(c, &user_buffer[i]))
843 return -EFAULT;
844 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800846 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 } else
848 break;
849 }
850 return i;
851}
852
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 int i = 0;
856
Luiz Capitulino222f1802006-03-20 22:16:13 -0800857 for (; i < maxlen; i++) {
858 char c;
859 if (get_user(c, &user_buffer[i]))
860 return -EFAULT;
861 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 case '\"':
863 case '\n':
864 case '\r':
865 case '\t':
866 case ' ':
867 goto done_str;
868 break;
869 default:
870 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return i;
875}
876
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800877static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
878{
879 unsigned n = 0;
880 char c;
881 ssize_t i = 0;
882 int len;
883
884 pkt_dev->nr_labels = 0;
885 do {
886 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700887 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800888 if (len <= 0)
889 return len;
890 pkt_dev->labels[n] = htonl(tmp);
891 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
892 pkt_dev->flags |= F_MPLS_RND;
893 i += len;
894 if (get_user(c, &buffer[i]))
895 return -EFAULT;
896 i++;
897 n++;
898 if (n >= MAX_MPLS_LABELS)
899 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700900 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800901
902 pkt_dev->nr_labels = n;
903 return i;
904}
905
Luiz Capitulino222f1802006-03-20 22:16:13 -0800906static ssize_t pktgen_if_write(struct file *file,
907 const char __user * user_buffer, size_t count,
908 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800910 struct seq_file *seq = (struct seq_file *)file->private_data;
911 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int i = 0, max, len;
913 char name[16], valstr[32];
914 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800915 char *pg_result = NULL;
916 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800918
919 pg_result = &(pkt_dev->result[0]);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700922 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -EINVAL;
924 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 max = count - i;
927 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800928 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700929 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800932 i += tmp;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* Read variable name */
935
936 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800937 if (len < 0) {
938 return len;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800941 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return -EFAULT;
943 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800944
945 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800947 if (len < 0)
948 return len;
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 i += len;
951
952 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800953 char tb[count + 1];
954 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700957 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800958 (unsigned long)count, tb);
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (!strcmp(name, "min_pkt_size")) {
962 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 < 14 + 20 + 8)
968 value = 14 + 20 + 8;
969 if (value != pkt_dev->min_pkt_size) {
970 pkt_dev->min_pkt_size = value;
971 pkt_dev->cur_pkt_size = value;
972 }
973 sprintf(pg_result, "OK: min_pkt_size=%u",
974 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return count;
976 }
977
Luiz Capitulino222f1802006-03-20 22:16:13 -0800978 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800980 if (len < 0) {
981 return len;
982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800984 if (value < 14 + 20 + 8)
985 value = 14 + 20 + 8;
986 if (value != pkt_dev->max_pkt_size) {
987 pkt_dev->max_pkt_size = value;
988 pkt_dev->cur_pkt_size = value;
989 }
990 sprintf(pg_result, "OK: max_pkt_size=%u",
991 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return count;
993 }
994
Luiz Capitulino222f1802006-03-20 22:16:13 -0800995 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 if (!strcmp(name, "pkt_size")) {
998 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800999 if (len < 0) {
1000 return len;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001003 if (value < 14 + 20 + 8)
1004 value = 14 + 20 + 8;
1005 if (value != pkt_dev->min_pkt_size) {
1006 pkt_dev->min_pkt_size = value;
1007 pkt_dev->max_pkt_size = value;
1008 pkt_dev->cur_pkt_size = value;
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
1011 return count;
1012 }
1013
Luiz Capitulino222f1802006-03-20 22:16:13 -08001014 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001016 if (len < 0) {
1017 return len;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001020 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 sprintf(pg_result, "OK: debug=%u", debug);
1022 return count;
1023 }
1024
Luiz Capitulino222f1802006-03-20 22:16:13 -08001025 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001027 if (len < 0) {
1028 return len;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 i += len;
1031 pkt_dev->nfrags = value;
1032 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1033 return count;
1034 }
1035 if (!strcmp(name, "delay")) {
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 (value == 0x7FFFFFFF) {
1042 pkt_dev->delay_us = 0x7FFFFFFF;
1043 pkt_dev->delay_ns = 0;
1044 } else {
1045 pkt_dev->delay_us = value / 1000;
1046 pkt_dev->delay_ns = value % 1000;
1047 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001048 sprintf(pg_result, "OK: delay=%u",
1049 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return count;
1051 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 if (len < 0) {
1055 return len;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001058 if (value != pkt_dev->udp_src_min) {
1059 pkt_dev->udp_src_min = value;
1060 pkt_dev->cur_udp_src = value;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1063 return count;
1064 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001065 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001067 if (len < 0) {
1068 return len;
1069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001071 if (value != pkt_dev->udp_dst_min) {
1072 pkt_dev->udp_dst_min = value;
1073 pkt_dev->cur_udp_dst = value;
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1076 return count;
1077 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001078 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001080 if (len < 0) {
1081 return len;
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001084 if (value != pkt_dev->udp_src_max) {
1085 pkt_dev->udp_src_max = value;
1086 pkt_dev->cur_udp_src = value;
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1089 return count;
1090 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001091 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001093 if (len < 0) {
1094 return len;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001097 if (value != pkt_dev->udp_dst_max) {
1098 pkt_dev->udp_dst_max = value;
1099 pkt_dev->cur_udp_dst = value;
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1102 return count;
1103 }
1104 if (!strcmp(name, "clone_skb")) {
1105 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001106 if (len < 0) {
1107 return len;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001110 pkt_dev->clone_skb = value;
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1113 return count;
1114 }
1115 if (!strcmp(name, "count")) {
1116 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001117 if (len < 0) {
1118 return len;
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 i += len;
1121 pkt_dev->count = value;
1122 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001123 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return count;
1125 }
1126 if (!strcmp(name, "src_mac_count")) {
1127 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001128 if (len < 0) {
1129 return len;
1130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 i += len;
1132 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001133 pkt_dev->src_mac_count = value;
1134 pkt_dev->cur_src_mac_offset = 0;
1135 }
1136 sprintf(pg_result, "OK: src_mac_count=%d",
1137 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return count;
1139 }
1140 if (!strcmp(name, "dst_mac_count")) {
1141 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001142 if (len < 0) {
1143 return len;
1144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 i += len;
1146 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001147 pkt_dev->dst_mac_count = value;
1148 pkt_dev->cur_dst_mac_offset = 0;
1149 }
1150 sprintf(pg_result, "OK: dst_mac_count=%d",
1151 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return count;
1153 }
1154 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001155 char f[32];
1156 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001158 if (len < 0) {
1159 return len;
1160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (copy_from_user(f, &user_buffer[i], len))
1162 return -EFAULT;
1163 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001164 if (strcmp(f, "IPSRC_RND") == 0)
1165 pkt_dev->flags |= F_IPSRC_RND;
1166
1167 else if (strcmp(f, "!IPSRC_RND") == 0)
1168 pkt_dev->flags &= ~F_IPSRC_RND;
1169
1170 else if (strcmp(f, "TXSIZE_RND") == 0)
1171 pkt_dev->flags |= F_TXSIZE_RND;
1172
1173 else if (strcmp(f, "!TXSIZE_RND") == 0)
1174 pkt_dev->flags &= ~F_TXSIZE_RND;
1175
1176 else if (strcmp(f, "IPDST_RND") == 0)
1177 pkt_dev->flags |= F_IPDST_RND;
1178
1179 else if (strcmp(f, "!IPDST_RND") == 0)
1180 pkt_dev->flags &= ~F_IPDST_RND;
1181
1182 else if (strcmp(f, "UDPSRC_RND") == 0)
1183 pkt_dev->flags |= F_UDPSRC_RND;
1184
1185 else if (strcmp(f, "!UDPSRC_RND") == 0)
1186 pkt_dev->flags &= ~F_UDPSRC_RND;
1187
1188 else if (strcmp(f, "UDPDST_RND") == 0)
1189 pkt_dev->flags |= F_UDPDST_RND;
1190
1191 else if (strcmp(f, "!UDPDST_RND") == 0)
1192 pkt_dev->flags &= ~F_UDPDST_RND;
1193
1194 else if (strcmp(f, "MACSRC_RND") == 0)
1195 pkt_dev->flags |= F_MACSRC_RND;
1196
1197 else if (strcmp(f, "!MACSRC_RND") == 0)
1198 pkt_dev->flags &= ~F_MACSRC_RND;
1199
1200 else if (strcmp(f, "MACDST_RND") == 0)
1201 pkt_dev->flags |= F_MACDST_RND;
1202
1203 else if (strcmp(f, "!MACDST_RND") == 0)
1204 pkt_dev->flags &= ~F_MACDST_RND;
1205
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001206 else if (strcmp(f, "MPLS_RND") == 0)
1207 pkt_dev->flags |= F_MPLS_RND;
1208
1209 else if (strcmp(f, "!MPLS_RND") == 0)
1210 pkt_dev->flags &= ~F_MPLS_RND;
1211
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001212 else if (strcmp(f, "VID_RND") == 0)
1213 pkt_dev->flags |= F_VID_RND;
1214
1215 else if (strcmp(f, "!VID_RND") == 0)
1216 pkt_dev->flags &= ~F_VID_RND;
1217
1218 else if (strcmp(f, "SVID_RND") == 0)
1219 pkt_dev->flags |= F_SVID_RND;
1220
1221 else if (strcmp(f, "!SVID_RND") == 0)
1222 pkt_dev->flags &= ~F_SVID_RND;
1223
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001224 else if (strcmp(f, "FLOW_SEQ") == 0)
1225 pkt_dev->flags |= F_FLOW_SEQ;
1226
Robert Olsson45b270f2007-08-28 15:45:55 -07001227 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1228 pkt_dev->flags |= F_QUEUE_MAP_RND;
1229
1230 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1231 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001232#ifdef CONFIG_XFRM
1233 else if (strcmp(f, "IPSEC") == 0)
1234 pkt_dev->flags |= F_IPSEC_ON;
1235#endif
1236
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001237 else if (strcmp(f, "!IPV6") == 0)
1238 pkt_dev->flags &= ~F_IPV6;
1239
Luiz Capitulino222f1802006-03-20 22:16:13 -08001240 else {
1241 sprintf(pg_result,
1242 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1243 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001244 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001245 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001246 return count;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1249 return count;
1250 }
1251 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1252 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001253 if (len < 0) {
1254 return len;
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Luiz Capitulino222f1802006-03-20 22:16:13 -08001257 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 buf[len] = 0;
1260 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1261 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1262 strncpy(pkt_dev->dst_min, buf, len);
1263 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1264 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1265 }
1266 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001267 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001268 pkt_dev->dst_min);
1269 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1271 return count;
1272 }
1273 if (!strcmp(name, "dst_max")) {
1274 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001275 if (len < 0) {
1276 return len;
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Luiz Capitulino222f1802006-03-20 22:16:13 -08001279 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return -EFAULT;
1281
Luiz Capitulino222f1802006-03-20 22:16:13 -08001282 buf[len] = 0;
1283 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1284 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1285 strncpy(pkt_dev->dst_max, buf, len);
1286 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1287 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1288 }
1289 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001290 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001291 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 i += len;
1293 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1294 return count;
1295 }
1296 if (!strcmp(name, "dst6")) {
1297 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001298 if (len < 0)
1299 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 pkt_dev->flags |= F_IPV6;
1302
Luiz Capitulino222f1802006-03-20 22:16:13 -08001303 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001305 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1311
Luiz Capitulino222f1802006-03-20 22:16:13 -08001312 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001313 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Luiz Capitulino222f1802006-03-20 22:16:13 -08001315 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 sprintf(pg_result, "OK: dst6=%s", buf);
1317 return count;
1318 }
1319 if (!strcmp(name, "dst6_min")) {
1320 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001321 if (len < 0)
1322 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 pkt_dev->flags |= F_IPV6;
1325
Luiz Capitulino222f1802006-03-20 22:16:13 -08001326 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001328 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001331 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Luiz Capitulino222f1802006-03-20 22:16:13 -08001333 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1334 &pkt_dev->min_in6_daddr);
1335 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001336 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 sprintf(pg_result, "OK: dst6_min=%s", buf);
1340 return count;
1341 }
1342 if (!strcmp(name, "dst6_max")) {
1343 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001344 if (len < 0)
1345 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 pkt_dev->flags |= F_IPV6;
1348
Luiz Capitulino222f1802006-03-20 22:16:13 -08001349 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001354 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Luiz Capitulino222f1802006-03-20 22:16:13 -08001356 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001357 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 sprintf(pg_result, "OK: dst6_max=%s", buf);
1361 return count;
1362 }
1363 if (!strcmp(name, "src6")) {
1364 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001365 if (len < 0)
1366 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 pkt_dev->flags |= F_IPV6;
1369
Luiz Capitulino222f1802006-03-20 22:16:13 -08001370 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001372 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001375 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1378
Luiz Capitulino222f1802006-03-20 22:16:13 -08001379 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001380 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381
1382 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 sprintf(pg_result, "OK: src6=%s", buf);
1384 return count;
1385 }
1386 if (!strcmp(name, "src_min")) {
1387 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001388 if (len < 0) {
1389 return len;
1390 }
1391 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 buf[len] = 0;
1394 if (strcmp(buf, pkt_dev->src_min) != 0) {
1395 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1396 strncpy(pkt_dev->src_min, buf, len);
1397 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1398 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1399 }
1400 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001401 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001402 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 i += len;
1404 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1405 return count;
1406 }
1407 if (!strcmp(name, "src_max")) {
1408 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001409 if (len < 0) {
1410 return len;
1411 }
1412 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001414 buf[len] = 0;
1415 if (strcmp(buf, pkt_dev->src_max) != 0) {
1416 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1417 strncpy(pkt_dev->src_max, buf, len);
1418 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1419 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1420 }
1421 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001422 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001423 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 i += len;
1425 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1426 return count;
1427 }
1428 if (!strcmp(name, "dst_mac")) {
1429 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001430 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001432 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001435 if (len < 0) {
1436 return len;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001439 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return -EFAULT;
1441 i += len;
1442
Luiz Capitulino222f1802006-03-20 22:16:13 -08001443 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (*v >= '0' && *v <= '9') {
1445 *m *= 16;
1446 *m += *v - '0';
1447 }
1448 if (*v >= 'A' && *v <= 'F') {
1449 *m *= 16;
1450 *m += *v - 'A' + 10;
1451 }
1452 if (*v >= 'a' && *v <= 'f') {
1453 *m *= 16;
1454 *m += *v - 'a' + 10;
1455 }
1456 if (*v == ':') {
1457 m++;
1458 *m = 0;
1459 }
1460 }
1461
1462 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001463 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1464 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 sprintf(pg_result, "OK: dstmac");
1467 return count;
1468 }
1469 if (!strcmp(name, "src_mac")) {
1470 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001471 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 unsigned char *m = pkt_dev->src_mac;
1473
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001474 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001477 if (len < 0) {
1478 return len;
1479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001481 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EFAULT;
1483 i += len;
1484
Luiz Capitulino222f1802006-03-20 22:16:13 -08001485 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (*v >= '0' && *v <= '9') {
1487 *m *= 16;
1488 *m += *v - '0';
1489 }
1490 if (*v >= 'A' && *v <= 'F') {
1491 *m *= 16;
1492 *m += *v - 'A' + 10;
1493 }
1494 if (*v >= 'a' && *v <= 'f') {
1495 *m *= 16;
1496 *m += *v - 'a' + 10;
1497 }
1498 if (*v == ':') {
1499 m++;
1500 *m = 0;
1501 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001504 /* Set up Src MAC */
1505 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1506 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1507
Luiz Capitulino222f1802006-03-20 22:16:13 -08001508 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return count;
1510 }
1511
Luiz Capitulino222f1802006-03-20 22:16:13 -08001512 if (!strcmp(name, "clear_counters")) {
1513 pktgen_clear_counters(pkt_dev);
1514 sprintf(pg_result, "OK: Clearing counters.\n");
1515 return count;
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 if (!strcmp(name, "flows")) {
1519 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001520 if (len < 0) {
1521 return len;
1522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 i += len;
1524 if (value > MAX_CFLOWS)
1525 value = MAX_CFLOWS;
1526
1527 pkt_dev->cflows = value;
1528 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1529 return count;
1530 }
1531
1532 if (!strcmp(name, "flowlen")) {
1533 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001534 if (len < 0) {
1535 return len;
1536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 i += len;
1538 pkt_dev->lflow = value;
1539 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1540 return count;
1541 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001542
Robert Olsson45b270f2007-08-28 15:45:55 -07001543 if (!strcmp(name, "queue_map_min")) {
1544 len = num_arg(&user_buffer[i], 5, &value);
1545 if (len < 0) {
1546 return len;
1547 }
1548 i += len;
1549 pkt_dev->queue_map_min = value;
1550 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1551 return count;
1552 }
1553
1554 if (!strcmp(name, "queue_map_max")) {
1555 len = num_arg(&user_buffer[i], 5, &value);
1556 if (len < 0) {
1557 return len;
1558 }
1559 i += len;
1560 pkt_dev->queue_map_max = value;
1561 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1562 return count;
1563 }
1564
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001565 if (!strcmp(name, "mpls")) {
1566 unsigned n, offset;
1567 len = get_labels(&user_buffer[i], pkt_dev);
1568 if (len < 0) { return len; }
1569 i += len;
1570 offset = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001571 for (n = 0; n < pkt_dev->nr_labels; n++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001572 offset += sprintf(pg_result + offset,
1573 "%08x%s", ntohl(pkt_dev->labels[n]),
1574 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001575
1576 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1577 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1578 pkt_dev->svlan_id = 0xffff;
1579
1580 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001581 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001582 }
1583 return count;
1584 }
1585
1586 if (!strcmp(name, "vlan_id")) {
1587 len = num_arg(&user_buffer[i], 4, &value);
1588 if (len < 0) {
1589 return len;
1590 }
1591 i += len;
1592 if (value <= 4095) {
1593 pkt_dev->vlan_id = value; /* turn on VLAN */
1594
1595 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001596 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001597
1598 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001599 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001600
1601 pkt_dev->nr_labels = 0; /* turn off MPLS */
1602 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1603 } else {
1604 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1605 pkt_dev->svlan_id = 0xffff;
1606
1607 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001608 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001609 }
1610 return count;
1611 }
1612
1613 if (!strcmp(name, "vlan_p")) {
1614 len = num_arg(&user_buffer[i], 1, &value);
1615 if (len < 0) {
1616 return len;
1617 }
1618 i += len;
1619 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1620 pkt_dev->vlan_p = value;
1621 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1622 } else {
1623 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1624 }
1625 return count;
1626 }
1627
1628 if (!strcmp(name, "vlan_cfi")) {
1629 len = num_arg(&user_buffer[i], 1, &value);
1630 if (len < 0) {
1631 return len;
1632 }
1633 i += len;
1634 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1635 pkt_dev->vlan_cfi = value;
1636 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1637 } else {
1638 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1639 }
1640 return count;
1641 }
1642
1643 if (!strcmp(name, "svlan_id")) {
1644 len = num_arg(&user_buffer[i], 4, &value);
1645 if (len < 0) {
1646 return len;
1647 }
1648 i += len;
1649 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1650 pkt_dev->svlan_id = value; /* turn on SVLAN */
1651
1652 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001653 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001654
1655 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001656 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001657
1658 pkt_dev->nr_labels = 0; /* turn off MPLS */
1659 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1660 } else {
1661 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1662 pkt_dev->svlan_id = 0xffff;
1663
1664 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001665 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001666 }
1667 return count;
1668 }
1669
1670 if (!strcmp(name, "svlan_p")) {
1671 len = num_arg(&user_buffer[i], 1, &value);
1672 if (len < 0) {
1673 return len;
1674 }
1675 i += len;
1676 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1677 pkt_dev->svlan_p = value;
1678 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1679 } else {
1680 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1681 }
1682 return count;
1683 }
1684
1685 if (!strcmp(name, "svlan_cfi")) {
1686 len = num_arg(&user_buffer[i], 1, &value);
1687 if (len < 0) {
1688 return len;
1689 }
1690 i += len;
1691 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1692 pkt_dev->svlan_cfi = value;
1693 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1694 } else {
1695 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1696 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001697 return count;
1698 }
1699
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001700 if (!strcmp(name, "tos")) {
1701 __u32 tmp_value = 0;
1702 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1703 if (len < 0) {
1704 return len;
1705 }
1706 i += len;
1707 if (len == 2) {
1708 pkt_dev->tos = tmp_value;
1709 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1710 } else {
1711 sprintf(pg_result, "ERROR: tos must be 00-ff");
1712 }
1713 return count;
1714 }
1715
1716 if (!strcmp(name, "traffic_class")) {
1717 __u32 tmp_value = 0;
1718 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1719 if (len < 0) {
1720 return len;
1721 }
1722 i += len;
1723 if (len == 2) {
1724 pkt_dev->traffic_class = tmp_value;
1725 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1726 } else {
1727 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1728 }
1729 return count;
1730 }
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1733 return -EINVAL;
1734}
1735
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001736static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001738 return single_open(file, pktgen_if_show, PDE(inode)->data);
1739}
1740
Arjan van de Ven9a321442007-02-12 00:55:35 -08001741static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001742 .owner = THIS_MODULE,
1743 .open = pktgen_if_open,
1744 .read = seq_read,
1745 .llseek = seq_lseek,
1746 .write = pktgen_if_write,
1747 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001748};
1749
1750static int pktgen_thread_show(struct seq_file *seq, void *v)
1751{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001752 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001753 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001755 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 seq_printf(seq, "Running: ");
1758
1759 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001760 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001761 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001762 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 seq_printf(seq, "\nStopped: ");
1765
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001766 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001767 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001768 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001771 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001773 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Luiz Capitulino222f1802006-03-20 22:16:13 -08001775 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001780static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 const char __user * user_buffer,
1782 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001784 struct seq_file *seq = (struct seq_file *)file->private_data;
1785 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 int i = 0, max, len, ret;
1787 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001788 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return -EINVAL;
1793 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001796 len = count_trail_chars(&user_buffer[i], max);
1797 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001798 return len;
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /* Read variable name */
1803
1804 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001806 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 memset(name, 0, sizeof(name));
1809 if (copy_from_user(name, &user_buffer[i], len))
1810 return -EFAULT;
1811 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001812
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001815 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001816 return len;
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 i += len;
1819
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001820 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001821 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1822 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Luiz Capitulino222f1802006-03-20 22:16:13 -08001824 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001825 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 ret = -EINVAL;
1827 goto out;
1828 }
1829
1830 pg_result = &(t->result[0]);
1831
Luiz Capitulino222f1802006-03-20 22:16:13 -08001832 if (!strcmp(name, "add_device")) {
1833 char f[32];
1834 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001836 if (len < 0) {
1837 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 goto out;
1839 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001840 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return -EFAULT;
1842 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001843 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001844 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001845 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846 ret = count;
1847 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto out;
1849 }
1850
Luiz Capitulino222f1802006-03-20 22:16:13 -08001851 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001852 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001853 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001854 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001855 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001857 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 goto out;
1859 }
1860
Luiz Capitulino222f1802006-03-20 22:16:13 -08001861 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001862 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001863 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 goto out;
1865 }
1866
1867 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001868out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return ret;
1870}
1871
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001872static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001874 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
Arjan van de Ven9a321442007-02-12 00:55:35 -08001877static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001878 .owner = THIS_MODULE,
1879 .open = pktgen_thread_open,
1880 .read = seq_read,
1881 .llseek = seq_lseek,
1882 .write = pktgen_thread_write,
1883 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001884};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001887static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 struct pktgen_thread *t;
1890 struct pktgen_dev *pkt_dev = NULL;
1891
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001892 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 pkt_dev = pktgen_find_dev(t, ifname);
1894 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001895 if (remove) {
1896 if_lock(t);
1897 pkt_dev->removal_mark = 1;
1898 t->control |= T_REMDEV;
1899 if_unlock(t);
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 break;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001904 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001907/*
1908 * mark a device for removal
1909 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001910static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001913 const int max_tries = 10, msec_per_try = 125;
1914 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001915
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001916 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001917 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001918
Luiz Capitulino222f1802006-03-20 22:16:13 -08001919 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001920
1921 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001922 if (pkt_dev == NULL)
1923 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001924
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001925 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001926 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1927 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001928 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001929 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001930
1931 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001932 printk(KERN_ERR "pktgen_mark_device: timed out after "
1933 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001934 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001935 break;
1936 }
1937
1938 }
1939
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001940 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001941}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001942
Stephen Hemminger39df2322007-03-04 16:11:51 -08001943static void pktgen_change_name(struct net_device *dev)
1944{
1945 struct pktgen_thread *t;
1946
1947 list_for_each_entry(t, &pktgen_threads, th_list) {
1948 struct pktgen_dev *pkt_dev;
1949
1950 list_for_each_entry(pkt_dev, &t->if_list, list) {
1951 if (pkt_dev->odev != dev)
1952 continue;
1953
1954 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1955
1956 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1957 pg_proc_dir);
1958 if (!pkt_dev->entry)
1959 printk(KERN_ERR "pktgen: can't move proc "
1960 " entry for '%s'\n", dev->name);
1961 break;
1962 }
1963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Luiz Capitulino222f1802006-03-20 22:16:13 -08001966static int pktgen_device_event(struct notifier_block *unused,
1967 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001969 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001971 if (dev->nd_net != &init_net)
1972 return NOTIFY_DONE;
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /* It is OK that we do not hold the group lock right now,
1975 * as we run under the RTNL lock.
1976 */
1977
1978 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001979 case NETDEV_CHANGENAME:
1980 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001984 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 return NOTIFY_DONE;
1989}
1990
1991/* Associate pktgen_dev with a device. */
1992
Stephen Hemminger39df2322007-03-04 16:11:51 -08001993static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001996 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (pkt_dev->odev) {
2000 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002001 pkt_dev->odev = NULL;
2002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Eric W. Biederman881d9662007-09-17 11:56:21 -07002004 odev = dev_get_by_name(&init_net, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07002006 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002007 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07002011 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002012 err = -EINVAL;
2013 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07002014 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002015 err = -ENETDOWN;
2016 } else {
2017 pkt_dev->odev = odev;
2018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002022 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
2025/* Read pkt_dev from the interface and set up internal pktgen_dev
2026 * structure to have the right information to create/send packets
2027 */
2028static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2029{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002030 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07002031 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
2032 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002033 sprintf(pkt_dev->result,
2034 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2035 return;
2036 }
2037
2038 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002040 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002041 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Luiz Capitulino222f1802006-03-20 22:16:13 -08002043 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002044 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Luiz Capitulino222f1802006-03-20 22:16:13 -08002046 /* Set up pkt size */
2047 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2048
2049 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002051 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 * gets exported
2053 */
2054
2055#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002056 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct inet6_dev *idev;
2058
Luiz Capitulino222f1802006-03-20 22:16:13 -08002059 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2060 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 set = 1;
2062 break;
2063 }
2064
Luiz Capitulino222f1802006-03-20 22:16:13 -08002065 if (!set) {
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 /*
2068 * Use linklevel address if unconfigured.
2069 *
2070 * use ipv6_get_lladdr if/when it's get exported
2071 */
2072
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002073 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2075 struct inet6_ifaddr *ifp;
2076
2077 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002078 for (ifp = idev->addr_list; ifp;
2079 ifp = ifp->if_next) {
2080 if (ifp->scope == IFA_LINK
2081 && !(ifp->
2082 flags & IFA_F_TENTATIVE)) {
2083 ipv6_addr_copy(&pkt_dev->
2084 cur_in6_saddr,
2085 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 err = 0;
2087 break;
2088 }
2089 }
2090 read_unlock_bh(&idev->lock);
2091 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002092 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002093 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002094 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2095 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002098 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 pkt_dev->saddr_min = 0;
2100 pkt_dev->saddr_max = 0;
2101 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002102
2103 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002106 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (in_dev) {
2108 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002109 pkt_dev->saddr_min =
2110 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 pkt_dev->saddr_max = pkt_dev->saddr_min;
2112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002115 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2117 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2118 }
2119
2120 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2121 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2122 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002123 /* Initialize current values. */
2124 pkt_dev->cur_dst_mac_offset = 0;
2125 pkt_dev->cur_src_mac_offset = 0;
2126 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2127 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2128 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2129 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 pkt_dev->nflows = 0;
2131}
2132
2133static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2134{
2135 __u64 start;
2136 __u64 now;
2137
2138 start = now = getCurUs();
2139 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2140 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002141 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002142 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002143 schedule_timeout_interruptible(1);
2144 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (!pkt_dev->running)
2146 return;
2147 if (need_resched())
2148 schedule();
2149 }
2150
2151 now = getCurUs();
2152 }
2153
2154 pkt_dev->idle_acc += now - start;
2155}
2156
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002157static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2158{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002159 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002160 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2161 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2162 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2163}
2164
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002165static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2166{
2167
2168 if (pkt_dev->flows[flow].flags & F_INIT)
2169 return 1;
2170 else
2171 return 0;
2172}
2173
2174static inline int f_pick(struct pktgen_dev *pkt_dev)
2175{
2176 int flow = pkt_dev->curfl;
2177
2178 if (pkt_dev->flags & F_FLOW_SEQ) {
2179 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2180 /* reset time */
2181 pkt_dev->flows[flow].count = 0;
2182 pkt_dev->curfl += 1;
2183 if (pkt_dev->curfl >= pkt_dev->cflows)
2184 pkt_dev->curfl = 0; /*reset */
2185 }
2186 } else {
2187 flow = random32() % pkt_dev->cflows;
2188
2189 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2190 pkt_dev->flows[flow].count = 0;
2191 }
2192
2193 return pkt_dev->curfl;
2194}
2195
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002196
2197#ifdef CONFIG_XFRM
2198/* If there was already an IPSEC SA, we keep it as is, else
2199 * we go look for it ...
2200*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002201static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002202{
2203 struct xfrm_state *x = pkt_dev->flows[flow].x;
2204 if (!x) {
2205 /*slow path: we dont already have xfrm_state*/
2206 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2207 (xfrm_address_t *)&pkt_dev->cur_saddr,
2208 AF_INET,
2209 pkt_dev->ipsmode,
2210 pkt_dev->ipsproto, 0);
2211 if (x) {
2212 pkt_dev->flows[flow].x = x;
2213 set_pkt_overhead(pkt_dev);
2214 pkt_dev->pkt_overhead+=x->props.header_len;
2215 }
2216
2217 }
2218}
2219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220/* Increment/randomize headers according to flags and current values
2221 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2222 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002223static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2224{
2225 __u32 imn;
2226 __u32 imx;
2227 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002229 if (pkt_dev->cflows)
2230 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002233 if (pkt_dev->src_mac_count > 1) {
2234 __u32 mc;
2235 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Luiz Capitulino222f1802006-03-20 22:16:13 -08002237 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002238 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002239 else {
2240 mc = pkt_dev->cur_src_mac_offset++;
2241 if (pkt_dev->cur_src_mac_offset >
2242 pkt_dev->src_mac_count)
2243 pkt_dev->cur_src_mac_offset = 0;
2244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Luiz Capitulino222f1802006-03-20 22:16:13 -08002246 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2247 pkt_dev->hh[11] = tmp;
2248 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2249 pkt_dev->hh[10] = tmp;
2250 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2251 pkt_dev->hh[9] = tmp;
2252 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2253 pkt_dev->hh[8] = tmp;
2254 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2255 pkt_dev->hh[7] = tmp;
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Luiz Capitulino222f1802006-03-20 22:16:13 -08002258 /* Deal with Destination MAC */
2259 if (pkt_dev->dst_mac_count > 1) {
2260 __u32 mc;
2261 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Luiz Capitulino222f1802006-03-20 22:16:13 -08002263 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002264 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Luiz Capitulino222f1802006-03-20 22:16:13 -08002266 else {
2267 mc = pkt_dev->cur_dst_mac_offset++;
2268 if (pkt_dev->cur_dst_mac_offset >
2269 pkt_dev->dst_mac_count) {
2270 pkt_dev->cur_dst_mac_offset = 0;
2271 }
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Luiz Capitulino222f1802006-03-20 22:16:13 -08002274 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2275 pkt_dev->hh[5] = tmp;
2276 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2277 pkt_dev->hh[4] = tmp;
2278 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2279 pkt_dev->hh[3] = tmp;
2280 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2281 pkt_dev->hh[2] = tmp;
2282 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2283 pkt_dev->hh[1] = tmp;
2284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002286 if (pkt_dev->flags & F_MPLS_RND) {
2287 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002288 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002289 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2290 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002291 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002292 htonl(0x000fffff));
2293 }
2294
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002295 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002296 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002297 }
2298
2299 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002300 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002301 }
2302
Luiz Capitulino222f1802006-03-20 22:16:13 -08002303 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2304 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002305 pkt_dev->cur_udp_src = random32() %
2306 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2307 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Luiz Capitulino222f1802006-03-20 22:16:13 -08002309 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 pkt_dev->cur_udp_src++;
2311 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2312 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002313 }
2314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2317 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002318 pkt_dev->cur_udp_dst = random32() %
2319 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2320 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002321 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002325 }
2326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 if (!(pkt_dev->flags & F_IPV6)) {
2329
Luiz Capitulino222f1802006-03-20 22:16:13 -08002330 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2331 ntohl(pkt_dev->
2332 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002334 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002335 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 else {
2337 t = ntohl(pkt_dev->cur_saddr);
2338 t++;
2339 if (t > imx) {
2340 t = imn;
2341 }
2342 }
2343 pkt_dev->cur_saddr = htonl(t);
2344 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002345
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002346 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2348 } else {
Al Viro252e3342006-11-14 20:48:11 -08002349 imn = ntohl(pkt_dev->daddr_min);
2350 imx = ntohl(pkt_dev->daddr_max);
2351 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002353 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (pkt_dev->flags & F_IPDST_RND) {
2355
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002356 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002357 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Al Viro252e3342006-11-14 20:48:11 -08002359 while (LOOPBACK(s) || MULTICAST(s)
2360 || BADCLASS(s) || ZERONET(s)
2361 || LOCAL_MCAST(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002362 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002363 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
Al Viro252e3342006-11-14 20:48:11 -08002365 pkt_dev->cur_daddr = s;
2366 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 t = ntohl(pkt_dev->cur_daddr);
2368 t++;
2369 if (t > imx) {
2370 t = imn;
2371 }
2372 pkt_dev->cur_daddr = htonl(t);
2373 }
2374 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002375 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002376 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002377 pkt_dev->flows[flow].cur_daddr =
2378 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002379#ifdef CONFIG_XFRM
2380 if (pkt_dev->flags & F_IPSEC_ON)
2381 get_ipsec_sa(pkt_dev, flow);
2382#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 pkt_dev->nflows++;
2384 }
2385 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002386 } else { /* IPV6 * */
2387
2388 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2389 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2390 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2391 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 else {
2393 int i;
2394
2395 /* Only random destinations yet */
2396
Luiz Capitulino222f1802006-03-20 22:16:13 -08002397 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002399 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2401 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405
Luiz Capitulino222f1802006-03-20 22:16:13 -08002406 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2407 __u32 t;
2408 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002409 t = random32() %
2410 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2411 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002412 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002414 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002416 }
2417 pkt_dev->cur_pkt_size = t;
2418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Robert Olsson45b270f2007-08-28 15:45:55 -07002420 if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2421 __u16 t;
2422 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2423 t = random32() %
2424 (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1)
2425 + pkt_dev->queue_map_min;
2426 } else {
2427 t = pkt_dev->cur_queue_map + 1;
2428 if (t > pkt_dev->queue_map_max)
2429 t = pkt_dev->queue_map_min;
2430 }
2431 pkt_dev->cur_queue_map = t;
2432 }
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 pkt_dev->flows[flow].count++;
2435}
2436
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002437
2438#ifdef CONFIG_XFRM
2439static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2440{
2441 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2442 int err = 0;
2443 struct iphdr *iph;
2444
2445 if (!x)
2446 return 0;
2447 /* XXX: we dont support tunnel mode for now until
2448 * we resolve the dst issue */
2449 if (x->props.mode != XFRM_MODE_TRANSPORT)
2450 return 0;
2451
2452 spin_lock(&x->lock);
2453 iph = ip_hdr(skb);
2454
2455 err = x->mode->output(x, skb);
2456 if (err)
2457 goto error;
2458 err = x->type->output(x, skb);
2459 if (err)
2460 goto error;
2461
2462 x->curlft.bytes +=skb->len;
2463 x->curlft.packets++;
2464 spin_unlock(&x->lock);
2465
2466error:
2467 spin_unlock(&x->lock);
2468 return err;
2469}
2470
2471static inline void free_SAs(struct pktgen_dev *pkt_dev)
2472{
2473 if (pkt_dev->cflows) {
2474 /* let go of the SAs if we have them */
2475 int i = 0;
2476 for (; i < pkt_dev->nflows; i++){
2477 struct xfrm_state *x = pkt_dev->flows[i].x;
2478 if (x) {
2479 xfrm_state_put(x);
2480 pkt_dev->flows[i].x = NULL;
2481 }
2482 }
2483 }
2484}
2485
2486static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2487 struct sk_buff *skb, __be16 protocol)
2488{
2489 if (pkt_dev->flags & F_IPSEC_ON) {
2490 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2491 int nhead = 0;
2492 if (x) {
2493 int ret;
2494 __u8 *eth;
2495 nhead = x->props.header_len - skb_headroom(skb);
2496 if (nhead >0) {
2497 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2498 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002499 printk(KERN_ERR "Error expanding "
2500 "ipsec packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002501 return 0;
2502 }
2503 }
2504
2505 /* ipsec is not expecting ll header */
2506 skb_pull(skb, ETH_HLEN);
2507 ret = pktgen_output_ipsec(skb, pkt_dev);
2508 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002509 printk(KERN_ERR "Error creating ipsec "
2510 "packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002511 kfree_skb(skb);
2512 return 0;
2513 }
2514 /* restore ll */
2515 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2516 memcpy(eth, pkt_dev->hh, 12);
2517 *(u16 *) & eth[12] = protocol;
2518 }
2519 }
2520 return 1;
2521}
2522#endif
2523
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002524static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2525{
2526 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002527 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002528 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2529 }
2530 mpls--;
2531 *mpls |= MPLS_STACK_BOTTOM;
2532}
2533
Al Viro0f37c602006-11-03 03:49:56 -08002534static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2535 unsigned int prio)
2536{
2537 return htons(id | (cfi << 12) | (prio << 13));
2538}
2539
Luiz Capitulino222f1802006-03-20 22:16:13 -08002540static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2541 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 struct sk_buff *skb = NULL;
2544 __u8 *eth;
2545 struct udphdr *udph;
2546 int datalen, iplen;
2547 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002548 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002549 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002550 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002551 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2552 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2553 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2554 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2555
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002556
2557 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002558 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002559
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002560 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002561 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002562
Robert Olsson64053be2005-06-26 15:27:10 -07002563 /* Update any of the values, used when we're incrementing various
2564 * fields.
2565 */
2566 mod_cur_headers(pkt_dev);
2567
David S. Miller7ac54592006-01-18 14:19:10 -08002568 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002569 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002570 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (!skb) {
2572 sprintf(pkt_dev->result, "No memory");
2573 return NULL;
2574 }
2575
David S. Miller7ac54592006-01-18 14:19:10 -08002576 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 /* Reserve for ethernet and IP header */
2579 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002580 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2581 if (pkt_dev->nr_labels)
2582 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002583
2584 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002585 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002586 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002587 *svlan_tci = build_tci(pkt_dev->svlan_id,
2588 pkt_dev->svlan_cfi,
2589 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002590 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002591 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002592 }
2593 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002594 *vlan_tci = build_tci(pkt_dev->vlan_id,
2595 pkt_dev->vlan_cfi,
2596 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002597 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002598 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002599 }
2600
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002601 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002602 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002603 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
Robert Olsson45b270f2007-08-28 15:45:55 -07002604 skb->queue_mapping = pkt_dev->cur_queue_map;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002605
2606 iph = ip_hdr(skb);
2607 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002610 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002612 /* Eth + IPh + UDPh + mpls */
2613 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002614 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002615 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 udph->source = htons(pkt_dev->cur_udp_src);
2619 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002620 udph->len = htons(datalen + 8); /* DATA + udphdr */
2621 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 iph->ihl = 5;
2624 iph->version = 4;
2625 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002626 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002627 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 iph->saddr = pkt_dev->cur_saddr;
2629 iph->daddr = pkt_dev->cur_daddr;
2630 iph->frag_off = 0;
2631 iplen = 20 + 8 + datalen;
2632 iph->tot_len = htons(iplen);
2633 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002634 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002635 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002636 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002637 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 skb->dev = odev;
2639 skb->pkt_type = PACKET_HOST;
2640
Luiz Capitulino222f1802006-03-20 22:16:13 -08002641 if (pkt_dev->nfrags <= 0)
2642 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 else {
2644 int frags = pkt_dev->nfrags;
2645 int i;
2646
Luiz Capitulino222f1802006-03-20 22:16:13 -08002647 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (frags > MAX_SKB_FRAGS)
2650 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002651 if (datalen > frags * PAGE_SIZE) {
2652 skb_put(skb, datalen - frags * PAGE_SIZE);
2653 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
2655
2656 i = 0;
2657 while (datalen > 0) {
2658 struct page *page = alloc_pages(GFP_KERNEL, 0);
2659 skb_shinfo(skb)->frags[i].page = page;
2660 skb_shinfo(skb)->frags[i].page_offset = 0;
2661 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002662 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 datalen -= skb_shinfo(skb)->frags[i].size;
2664 skb->len += skb_shinfo(skb)->frags[i].size;
2665 skb->data_len += skb_shinfo(skb)->frags[i].size;
2666 i++;
2667 skb_shinfo(skb)->nr_frags = i;
2668 }
2669
2670 while (i < frags) {
2671 int rem;
2672
2673 if (i == 0)
2674 break;
2675
2676 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2677 if (rem == 0)
2678 break;
2679
2680 skb_shinfo(skb)->frags[i - 1].size -= rem;
2681
Luiz Capitulino222f1802006-03-20 22:16:13 -08002682 skb_shinfo(skb)->frags[i] =
2683 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002685 skb_shinfo(skb)->frags[i].page =
2686 skb_shinfo(skb)->frags[i - 1].page;
2687 skb_shinfo(skb)->frags[i].page_offset +=
2688 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 skb_shinfo(skb)->frags[i].size = rem;
2690 i++;
2691 skb_shinfo(skb)->nr_frags = i;
2692 }
2693 }
2694
Luiz Capitulino222f1802006-03-20 22:16:13 -08002695 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Luiz Capitulino222f1802006-03-20 22:16:13 -08002697 if (pgh) {
2698 struct timeval timestamp;
2699
2700 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2701 pgh->seq_num = htonl(pkt_dev->seq_num);
2702
2703 do_gettimeofday(&timestamp);
2704 pgh->tv_sec = htonl(timestamp.tv_sec);
2705 pgh->tv_usec = htonl(timestamp.tv_usec);
2706 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002707
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002708#ifdef CONFIG_XFRM
2709 if (!process_ipsec(pkt_dev, skb, protocol))
2710 return NULL;
2711#endif
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return skb;
2714}
2715
2716/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002717 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2719 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002720 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 * Should be candidate for net/ipv4/utils.c
2722 * --ro
2723 */
2724
Luiz Capitulino222f1802006-03-20 22:16:13 -08002725static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002728 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 unsigned long u;
2730 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002731 unsigned int prefixlen = 0;
2732 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002733 __be32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Luiz Capitulino222f1802006-03-20 22:16:13 -08002735 for (i = 0; i < 16; i++)
2736 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 for (;;) {
2739 if (*s == ':') {
2740 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002741 if (s[1] == ':') { /* Found "::", skip to part 2 */
2742 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 len++;
2744 break;
2745 }
2746 s++;
2747 }
2748 {
2749 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002750 u = simple_strtoul(s, &tmp, 16);
2751 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753
Luiz Capitulino222f1802006-03-20 22:16:13 -08002754 if (!i)
2755 return 0;
2756 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 /* the last 4 bytes may be written as IPv4 address */
2759
2760 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002761 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2762 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
2764 ip[prefixlen++] = (u >> 8);
2765 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002766 s += i;
2767 len += i;
2768 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return len;
2770 }
2771
2772/* part 2, after "::" */
2773 for (;;) {
2774 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002775 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 break;
2777 s++;
2778 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002779 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 break;
2781 {
2782 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002783 u = simple_strtol(s, &tmp, 16);
2784 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002787 if (*s)
2788 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 break;
2790 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002791 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002793 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2794 sizeof(tmp));
2795 suffixlen += 4;
2796 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 break;
2798 }
2799 suffix[suffixlen++] = (u >> 8);
2800 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002801 s += i;
2802 len += i;
2803 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 break;
2805 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002806 for (i = 0; i < suffixlen; i++)
2807 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return len;
2809}
2810
Luiz Capitulino222f1802006-03-20 22:16:13 -08002811static char tohex(char hexdigit)
2812{
2813 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
Luiz Capitulino222f1802006-03-20 22:16:13 -08002816static int fmt_xlong(char *s, unsigned int i)
2817{
2818 char *bak = s;
2819 *s = tohex((i >> 12) & 0xf);
2820 if (s != bak || *s != '0')
2821 ++s;
2822 *s = tohex((i >> 8) & 0xf);
2823 if (s != bak || *s != '0')
2824 ++s;
2825 *s = tohex((i >> 4) & 0xf);
2826 if (s != bak || *s != '0')
2827 ++s;
2828 *s = tohex(i & 0xf);
2829 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
2831
Luiz Capitulino222f1802006-03-20 22:16:13 -08002832static unsigned int fmt_ip6(char *s, const char ip[16])
2833{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 unsigned int len;
2835 unsigned int i;
2836 unsigned int temp;
2837 unsigned int compressing;
2838 int j;
2839
Luiz Capitulino222f1802006-03-20 22:16:13 -08002840 len = 0;
2841 compressing = 0;
2842 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002845 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2846 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2847 temp = strlen(s);
2848 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002851 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2852 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (temp == 0) {
2854 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002855 compressing = 1;
2856 if (j == 0) {
2857 *s++ = ':';
2858 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 }
2860 }
2861 } else {
2862 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002863 compressing = 0;
2864 *s++ = ':';
2865 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002867 i = fmt_xlong(s, temp);
2868 len += i;
2869 s += i;
2870 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 *s++ = ':';
2872 ++len;
2873 }
2874 }
2875 }
2876 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002877 *s++ = ':';
2878 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002880 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return len;
2882}
2883
Luiz Capitulino222f1802006-03-20 22:16:13 -08002884static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2885 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 struct sk_buff *skb = NULL;
2888 __u8 *eth;
2889 struct udphdr *udph;
2890 int datalen;
2891 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002892 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002893 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002894 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002895 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2896 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2897 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2898 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002899
2900 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002901 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002902
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002903 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002904 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002905
Robert Olsson64053be2005-06-26 15:27:10 -07002906 /* Update any of the values, used when we're incrementing various
2907 * fields.
2908 */
2909 mod_cur_headers(pkt_dev);
2910
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002911 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002912 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (!skb) {
2914 sprintf(pkt_dev->result, "No memory");
2915 return NULL;
2916 }
2917
2918 skb_reserve(skb, 16);
2919
2920 /* Reserve for ethernet and IP header */
2921 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002922 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2923 if (pkt_dev->nr_labels)
2924 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002925
2926 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002927 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002928 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002929 *svlan_tci = build_tci(pkt_dev->svlan_id,
2930 pkt_dev->svlan_cfi,
2931 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002932 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002933 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002934 }
2935 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002936 *vlan_tci = build_tci(pkt_dev->vlan_id,
2937 pkt_dev->vlan_cfi,
2938 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002939 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002940 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002941 }
2942
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002943 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002944 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002945 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
Robert Olsson45b270f2007-08-28 15:45:55 -07002946 skb->queue_mapping = pkt_dev->cur_queue_map;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002947
2948 iph = ipv6_hdr(skb);
2949 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002952 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002953
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002954 /* Eth + IPh + UDPh + mpls */
2955 datalen = pkt_dev->cur_pkt_size - 14 -
2956 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002957 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Luiz Capitulino222f1802006-03-20 22:16:13 -08002959 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 datalen = sizeof(struct pktgen_hdr);
2961 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002962 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2963 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965
2966 udph->source = htons(pkt_dev->cur_udp_src);
2967 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002968 udph->len = htons(datalen + sizeof(struct udphdr));
2969 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002971 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002973 if (pkt_dev->traffic_class) {
2974 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002975 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002976 }
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 iph->hop_limit = 32;
2979
2980 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2981 iph->nexthdr = IPPROTO_UDP;
2982
2983 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2984 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2985
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002986 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002987 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002988 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 skb->dev = odev;
2990 skb->pkt_type = PACKET_HOST;
2991
Luiz Capitulino222f1802006-03-20 22:16:13 -08002992 if (pkt_dev->nfrags <= 0)
2993 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 else {
2995 int frags = pkt_dev->nfrags;
2996 int i;
2997
Luiz Capitulino222f1802006-03-20 22:16:13 -08002998 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (frags > MAX_SKB_FRAGS)
3001 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003002 if (datalen > frags * PAGE_SIZE) {
3003 skb_put(skb, datalen - frags * PAGE_SIZE);
3004 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
3006
3007 i = 0;
3008 while (datalen > 0) {
3009 struct page *page = alloc_pages(GFP_KERNEL, 0);
3010 skb_shinfo(skb)->frags[i].page = page;
3011 skb_shinfo(skb)->frags[i].page_offset = 0;
3012 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08003013 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 datalen -= skb_shinfo(skb)->frags[i].size;
3015 skb->len += skb_shinfo(skb)->frags[i].size;
3016 skb->data_len += skb_shinfo(skb)->frags[i].size;
3017 i++;
3018 skb_shinfo(skb)->nr_frags = i;
3019 }
3020
3021 while (i < frags) {
3022 int rem;
3023
3024 if (i == 0)
3025 break;
3026
3027 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3028 if (rem == 0)
3029 break;
3030
3031 skb_shinfo(skb)->frags[i - 1].size -= rem;
3032
Luiz Capitulino222f1802006-03-20 22:16:13 -08003033 skb_shinfo(skb)->frags[i] =
3034 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003036 skb_shinfo(skb)->frags[i].page =
3037 skb_shinfo(skb)->frags[i - 1].page;
3038 skb_shinfo(skb)->frags[i].page_offset +=
3039 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 skb_shinfo(skb)->frags[i].size = rem;
3041 i++;
3042 skb_shinfo(skb)->nr_frags = i;
3043 }
3044 }
3045
Luiz Capitulino222f1802006-03-20 22:16:13 -08003046 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048 if (pgh) {
3049 struct timeval timestamp;
3050
3051 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3052 pgh->seq_num = htonl(pkt_dev->seq_num);
3053
3054 do_gettimeofday(&timestamp);
3055 pgh->tv_sec = htonl(timestamp.tv_sec);
3056 pgh->tv_usec = htonl(timestamp.tv_usec);
3057 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003058 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return skb;
3061}
3062
Luiz Capitulino222f1802006-03-20 22:16:13 -08003063static inline struct sk_buff *fill_packet(struct net_device *odev,
3064 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003066 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return fill_packet_ipv6(odev, pkt_dev);
3068 else
3069 return fill_packet_ipv4(odev, pkt_dev);
3070}
3071
Luiz Capitulino222f1802006-03-20 22:16:13 -08003072static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074 pkt_dev->seq_num = 1;
3075 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003077 pkt_dev->tx_bytes = 0;
3078 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
3081/* Set up structure for sending pkts, clear counters */
3082
3083static void pktgen_run(struct pktgen_thread *t)
3084{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003085 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 int started = 0;
3087
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003088 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
3090 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003091 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 /*
3094 * setup odev and create initial packet.
3095 */
3096 pktgen_setup_inject(pkt_dev);
3097
Luiz Capitulino222f1802006-03-20 22:16:13 -08003098 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003100 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 pkt_dev->skb = NULL;
3102 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003103 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 pkt_dev->next_tx_ns = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003105 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 strcpy(pkt_dev->result, "Starting");
3108 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003109 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 strcpy(pkt_dev->result, "Error starting");
3111 }
3112 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003113 if (started)
3114 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
3117static void pktgen_stop_all_threads_ifs(void)
3118{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003119 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003121 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003123 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003124
3125 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003126 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003127
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003128 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Luiz Capitulino222f1802006-03-20 22:16:13 -08003131static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003133 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003134 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003136 list_for_each_entry(pkt_dev, &t->if_list, list)
3137 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 res = 1;
3139 break;
3140 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003141 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003146 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Luiz Capitulino222f1802006-03-20 22:16:13 -08003148 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Luiz Capitulino222f1802006-03-20 22:16:13 -08003150 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154 if (signal_pending(current))
3155 goto signal;
3156 if_lock(t);
3157 }
3158 if_unlock(t);
3159 return 1;
3160signal:
3161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
3164static int pktgen_wait_all_threads_run(void)
3165{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003166 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003168
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003169 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003170
3171 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003173 if (sig == 0)
3174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003176
3177 if (sig == 0)
3178 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003180
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003181 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return sig;
3183}
3184
3185static void pktgen_run_all_threads(void)
3186{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003187 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003189 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003191 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003193 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003195
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003196 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Luiz Capitulino222f1802006-03-20 22:16:13 -08003198 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 pktgen_wait_all_threads_run();
3201}
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3204{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205 __u64 total_us, bps, mbps, pps, idle;
3206 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Luiz Capitulino222f1802006-03-20 22:16:13 -08003208 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Luiz Capitulino222f1802006-03-20 22:16:13 -08003210 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Luiz Capitulino222f1802006-03-20 22:16:13 -08003212 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3213 (unsigned long long)total_us,
3214 (unsigned long long)(total_us - idle),
3215 (unsigned long long)idle,
3216 (unsigned long long)pkt_dev->sofar,
3217 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Luiz Capitulino222f1802006-03-20 22:16:13 -08003219 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Luiz Capitulino222f1802006-03-20 22:16:13 -08003221 while ((total_us >> 32) != 0) {
3222 pps >>= 1;
3223 total_us >>= 1;
3224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Luiz Capitulino222f1802006-03-20 22:16:13 -08003226 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Luiz Capitulino222f1802006-03-20 22:16:13 -08003228 bps = pps * 8 * pkt_dev->cur_pkt_size;
3229
3230 mbps = bps;
3231 do_div(mbps, 1000000);
3232 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3233 (unsigned long long)pps,
3234 (unsigned long long)mbps,
3235 (unsigned long long)bps,
3236 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239/* Set stopped-at timer, remove from running list, do counters & statistics */
3240
Luiz Capitulino222f1802006-03-20 22:16:13 -08003241static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003244
Luiz Capitulino222f1802006-03-20 22:16:13 -08003245 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003246 printk(KERN_WARNING "pktgen: interface: %s is already "
3247 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003248 return -EINVAL;
3249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Luiz Capitulino222f1802006-03-20 22:16:13 -08003251 pkt_dev->stopped_at = getCurUs();
3252 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003254 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Luiz Capitulino222f1802006-03-20 22:16:13 -08003256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
3258
Luiz Capitulino222f1802006-03-20 22:16:13 -08003259static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003261 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if_lock(t);
3264
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003265 list_for_each_entry(pkt_dev, &t->if_list, list) {
3266 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003267 continue;
3268 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003269 best = pkt_dev;
3270 else if (pkt_dev->next_tx_us < best->next_tx_us)
3271 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 }
3273 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003274 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
Luiz Capitulino222f1802006-03-20 22:16:13 -08003277static void pktgen_stop(struct pktgen_thread *t)
3278{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003279 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003281 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Luiz Capitulino222f1802006-03-20 22:16:13 -08003283 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003285 list_for_each_entry(pkt_dev, &t->if_list, list) {
3286 pktgen_stop_device(pkt_dev);
3287 if (pkt_dev->skb)
3288 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003289
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003290 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Luiz Capitulino222f1802006-03-20 22:16:13 -08003293 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003296/*
3297 * one of our devices needs to be removed - find it
3298 * and remove it
3299 */
3300static void pktgen_rem_one_if(struct pktgen_thread *t)
3301{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003302 struct list_head *q, *n;
3303 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003304
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003305 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003306
3307 if_lock(t);
3308
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003309 list_for_each_safe(q, n, &t->if_list) {
3310 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003311
Luiz Capitulino222f1802006-03-20 22:16:13 -08003312 if (!cur->removal_mark)
3313 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003314
3315 if (cur->skb)
3316 kfree_skb(cur->skb);
3317 cur->skb = NULL;
3318
3319 pktgen_remove_device(t, cur);
3320
3321 break;
3322 }
3323
3324 if_unlock(t);
3325}
3326
Luiz Capitulino222f1802006-03-20 22:16:13 -08003327static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003329 struct list_head *q, *n;
3330 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003331
3332 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003333
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003334 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003335 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003337 list_for_each_safe(q, n, &t->if_list) {
3338 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003339
3340 if (cur->skb)
3341 kfree_skb(cur->skb);
3342 cur->skb = NULL;
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 pktgen_remove_device(t, cur);
3345 }
3346
Luiz Capitulino222f1802006-03-20 22:16:13 -08003347 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348}
3349
Luiz Capitulino222f1802006-03-20 22:16:13 -08003350static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003352 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
David S. Milleree74baa2007-01-01 20:51:53 -08003354 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003356 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003358 list_del(&t->th_list);
3359
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003360 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
3363static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3364{
3365 struct net_device *odev = NULL;
3366 __u64 idle_start = 0;
3367 int ret;
3368
3369 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3372 u64 now;
3373
3374 now = getCurUs();
3375 if (now < pkt_dev->next_tx_us)
3376 spin(pkt_dev, pkt_dev->next_tx_us);
3377
3378 /* This is max DELAY, this has special meaning of
3379 * "never transmit"
3380 */
3381 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3382 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3383 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3384 goto out;
3385 }
3386 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003387
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003388 if ((netif_queue_stopped(odev) ||
Robert Olsson378be2c2007-08-28 15:43:14 -07003389 (pkt_dev->skb &&
3390 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping))) ||
3391 need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 if (!netif_running(odev)) {
3395 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003396 if (pkt_dev->skb)
3397 kfree_skb(pkt_dev->skb);
3398 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 goto out;
3400 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003401 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003405
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003406 if (netif_queue_stopped(odev) ||
3407 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003408 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003410 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 }
3412 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003415 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3416 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003418 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 pkt_dev->skb = fill_packet(odev, pkt_dev);
3422 if (pkt_dev->skb == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003423 printk(KERN_ERR "pktgen: ERROR: couldn't "
3424 "allocate skb in fill_packet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003426 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 goto out;
3428 }
3429 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003430 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
3432 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003433
Herbert Xu932ff272006-06-09 12:20:56 -07003434 netif_tx_lock_bh(odev);
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003435 if (!netif_queue_stopped(odev) &&
3436 !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
3438 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003439 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3441 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003442 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 pkt_dev->sofar++;
3444 pkt_dev->seq_num++;
3445 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003446
3447 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 && (odev->features & NETIF_F_LLTX)) {
3449 cpu_relax();
3450 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003451 } else { /* Retry it next time */
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if (debug && net_ratelimit())
3456 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 pkt_dev->errors++;
3459 pkt_dev->last_ok = 0;
3460 }
3461
3462 pkt_dev->next_tx_us = getCurUs();
3463 pkt_dev->next_tx_ns = 0;
3464
3465 pkt_dev->next_tx_us += pkt_dev->delay_us;
3466 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3467
3468 if (pkt_dev->next_tx_ns > 1000) {
3469 pkt_dev->next_tx_us++;
3470 pkt_dev->next_tx_ns -= 1000;
3471 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Luiz Capitulino222f1802006-03-20 22:16:13 -08003474 else { /* Retry it next time */
3475 pkt_dev->last_ok = 0;
3476 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Herbert Xu932ff272006-06-09 12:20:56 -07003480 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 /* If pkt_dev->count is zero, then run forever */
3483 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3484 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3485 idle_start = getCurUs();
3486 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3487 if (signal_pending(current)) {
3488 break;
3489 }
3490 schedule();
3491 }
3492 pkt_dev->idle_acc += getCurUs() - idle_start;
3493 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 /* Done with this */
3496 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003497 if (pkt_dev->skb)
3498 kfree_skb(pkt_dev->skb);
3499 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003500 }
3501out:;
3502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003504/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 * Main loop of the thread goes here
3506 */
3507
David S. Milleree74baa2007-01-01 20:51:53 -08003508static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
3510 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003511 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003512 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
David S. Milleree74baa2007-01-01 20:51:53 -08003515 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 init_waitqueue_head(&t->queue);
3518
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003519 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
David S. Milleree74baa2007-01-01 20:51:53 -08003521 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003522
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003523 set_freezable();
3524
David S. Milleree74baa2007-01-01 20:51:53 -08003525 while (!kthread_should_stop()) {
3526 pkt_dev = next_to_run(t);
3527
3528 if (!pkt_dev &&
3529 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3530 == 0) {
3531 prepare_to_wait(&(t->queue), &wait,
3532 TASK_INTERRUPTIBLE);
3533 schedule_timeout(HZ / 10);
3534 finish_wait(&(t->queue), &wait);
3535 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 __set_current_state(TASK_RUNNING);
3538
Robert Olssonb1639112007-08-28 15:46:58 -07003539 if (pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 pktgen_xmit(pkt_dev);
3541
Luiz Capitulino222f1802006-03-20 22:16:13 -08003542 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 pktgen_stop(t);
3544 t->control &= ~(T_STOP);
3545 }
3546
Luiz Capitulino222f1802006-03-20 22:16:13 -08003547 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 pktgen_run(t);
3549 t->control &= ~(T_RUN);
3550 }
3551
Luiz Capitulino222f1802006-03-20 22:16:13 -08003552 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003554 t->control &= ~(T_REMDEVALL);
3555 }
3556
Luiz Capitulino222f1802006-03-20 22:16:13 -08003557 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003558 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 t->control &= ~(T_REMDEV);
3560 }
3561
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003562 try_to_freeze();
3563
David S. Milleree74baa2007-01-01 20:51:53 -08003564 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003567 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003568 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003570 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003571 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003573 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003574 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003575
David S. Milleree74baa2007-01-01 20:51:53 -08003576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Luiz Capitulino222f1802006-03-20 22:16:13 -08003579static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3580 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003582 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003583 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003585 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003586 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003587 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 break;
3589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Luiz Capitulino222f1802006-03-20 22:16:13 -08003591 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003592 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003593 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003596/*
3597 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 */
3599
Luiz Capitulino222f1802006-03-20 22:16:13 -08003600static int add_dev_to_thread(struct pktgen_thread *t,
3601 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
3603 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Luiz Capitulino222f1802006-03-20 22:16:13 -08003605 if_lock(t);
3606
3607 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003608 printk(KERN_ERR "pktgen: ERROR: already assigned "
3609 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003610 rv = -EBUSY;
3611 goto out;
3612 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003613
3614 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 pkt_dev->running = 0;
3617
Luiz Capitulino222f1802006-03-20 22:16:13 -08003618out:
3619 if_unlock(t);
3620 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
3623/* Called under thread lock */
3624
Luiz Capitulino222f1802006-03-20 22:16:13 -08003625static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003627 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003628 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 /* We don't allow a device to be on several threads */
3631
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003632 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3633 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003634 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003635 return -EBUSY;
3636 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003637
3638 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3639 if (!pkt_dev)
3640 return -ENOMEM;
3641
Luiz Capitulino222f1802006-03-20 22:16:13 -08003642 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003643 if (pkt_dev->flows == NULL) {
3644 kfree(pkt_dev);
3645 return -ENOMEM;
3646 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003648
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003649 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003650 pkt_dev->min_pkt_size = ETH_ZLEN;
3651 pkt_dev->max_pkt_size = ETH_ZLEN;
3652 pkt_dev->nfrags = 0;
3653 pkt_dev->clone_skb = pg_clone_skb_d;
3654 pkt_dev->delay_us = pg_delay_d / 1000;
3655 pkt_dev->delay_ns = pg_delay_d % 1000;
3656 pkt_dev->count = pg_count_d;
3657 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003658 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003659 pkt_dev->udp_src_max = 9;
3660 pkt_dev->udp_dst_min = 9;
3661 pkt_dev->udp_dst_max = 9;
3662
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003663 pkt_dev->vlan_p = 0;
3664 pkt_dev->vlan_cfi = 0;
3665 pkt_dev->vlan_id = 0xffff;
3666 pkt_dev->svlan_p = 0;
3667 pkt_dev->svlan_cfi = 0;
3668 pkt_dev->svlan_id = 0xffff;
3669
Stephen Hemminger39df2322007-03-04 16:11:51 -08003670 err = pktgen_setup_dev(pkt_dev, ifname);
3671 if (err)
3672 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003673
Stephen Hemminger39df2322007-03-04 16:11:51 -08003674 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3675 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003676 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003677 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003678 err = -EINVAL;
3679 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003680 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08003681 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3682 pkt_dev->entry->data = pkt_dev;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003683#ifdef CONFIG_XFRM
3684 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3685 pkt_dev->ipsproto = IPPROTO_ESP;
3686#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003687
3688 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003689out2:
3690 dev_put(pkt_dev->odev);
3691out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003692#ifdef CONFIG_XFRM
3693 free_SAs(pkt_dev);
3694#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -08003695 if (pkt_dev->flows)
3696 vfree(pkt_dev->flows);
3697 kfree(pkt_dev);
3698 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699}
3700
David S. Milleree74baa2007-01-01 20:51:53 -08003701static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003703 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003704 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003705 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Luiz Capitulino222f1802006-03-20 22:16:13 -08003707 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3708 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003709 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3710 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003711 return -ENOMEM;
3712 }
3713
Luiz Capitulino222f1802006-03-20 22:16:13 -08003714 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003716
David S. Milleree74baa2007-01-01 20:51:53 -08003717 INIT_LIST_HEAD(&t->if_list);
3718
3719 list_add_tail(&t->th_list, &pktgen_threads);
3720
3721 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3722 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003723 printk(KERN_ERR "pktgen: kernel_thread() failed "
3724 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003725 list_del(&t->th_list);
3726 kfree(t);
3727 return PTR_ERR(p);
3728 }
3729 kthread_bind(p, cpu);
3730 t->tsk = p;
3731
3732 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003733 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003734 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003735 PG_PROC_DIR, t->tsk->comm);
3736 kthread_stop(p);
3737 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003738 kfree(t);
3739 return -EINVAL;
3740 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003741
3742 pe->proc_fops = &pktgen_thread_fops;
3743 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
David S. Milleree74baa2007-01-01 20:51:53 -08003745 wake_up_process(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
3747 return 0;
3748}
3749
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003750/*
3751 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003753static void _rem_dev_from_if_list(struct pktgen_thread *t,
3754 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003756 struct list_head *q, *n;
3757 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003759 list_for_each_safe(q, n, &t->if_list) {
3760 p = list_entry(q, struct pktgen_dev, list);
3761 if (p == pkt_dev)
3762 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 }
3764}
3765
Luiz Capitulino222f1802006-03-20 22:16:13 -08003766static int pktgen_remove_device(struct pktgen_thread *t,
3767 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
3769
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003770 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Luiz Capitulino222f1802006-03-20 22:16:13 -08003772 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003773 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3774 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003775 pktgen_stop_device(pkt_dev);
3776 }
3777
3778 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
3780 if (pkt_dev->odev) {
3781 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003782 pkt_dev->odev = NULL;
3783 }
3784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 /* And update the thread if_list */
3786
3787 _rem_dev_from_if_list(t, pkt_dev);
3788
Stephen Hemminger39df2322007-03-04 16:11:51 -08003789 if (pkt_dev->entry)
3790 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003792#ifdef CONFIG_XFRM
3793 free_SAs(pkt_dev);
3794#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 if (pkt_dev->flows)
3796 vfree(pkt_dev->flows);
3797 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799}
3800
Luiz Capitulino222f1802006-03-20 22:16:13 -08003801static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
3803 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003804 struct proc_dir_entry *pe;
3805
David S. Miller25a8b252007-07-30 16:11:48 -07003806 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003808 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003809 if (!pg_proc_dir)
3810 return -ENODEV;
3811 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003813 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003814 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003815 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3816 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003817 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003818 return -EINVAL;
3819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
Luiz Capitulino222f1802006-03-20 22:16:13 -08003821 pe->proc_fops = &pktgen_fops;
3822 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 /* Register us to receive netdevice events */
3825 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003826
John Hawkes670c02c2005-10-13 09:30:31 -07003827 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003828 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
David S. Milleree74baa2007-01-01 20:51:53 -08003830 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003831 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003832 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3833 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003834 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003835
3836 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003837 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3838 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003839 unregister_netdevice_notifier(&pktgen_notifier_block);
3840 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003841 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003842 return -ENODEV;
3843 }
3844
Luiz Capitulino222f1802006-03-20 22:16:13 -08003845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846}
3847
3848static void __exit pg_cleanup(void)
3849{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003850 struct pktgen_thread *t;
3851 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 wait_queue_head_t queue;
3853 init_waitqueue_head(&queue);
3854
Luiz Capitulino222f1802006-03-20 22:16:13 -08003855 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003857 list_for_each_safe(q, n, &pktgen_threads) {
3858 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003859 kthread_stop(t->tsk);
3860 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Luiz Capitulino222f1802006-03-20 22:16:13 -08003863 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 unregister_netdevice_notifier(&pktgen_notifier_block);
3865
Luiz Capitulino222f1802006-03-20 22:16:13 -08003866 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003867 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003868 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869}
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871module_init(pg_init);
3872module_exit(pg_cleanup);
3873
3874MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3875MODULE_DESCRIPTION("Packet Generator tool");
3876MODULE_LICENSE("GPL");
3877module_param(pg_count_d, int, 0);
3878module_param(pg_delay_d, int, 0);
3879module_param(pg_clone_skb_d, int, 0);
3880module_param(debug, int, 0);