blob: 2498cdaf8cbe9d8e7511c29e8d8cee121fe51d40 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
Jan Engelhardt96de0e22007-10-19 23:21:04 +02009 * Jens Låås <jens.laas@data.slu.se>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090047 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090068 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
116 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118#include <linux/sys.h>
119#include <linux/types.h>
120#include <linux/module.h>
121#include <linux/moduleparam.h>
122#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800123#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <linux/sched.h>
125#include <linux/slab.h>
126#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <linux/unistd.h>
128#include <linux/string.h>
129#include <linux/ptrace.h>
130#include <linux/errno.h>
131#include <linux/ioport.h>
132#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800133#include <linux/capability.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700134#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#include <linux/delay.h>
136#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800137#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#include <linux/init.h>
139#include <linux/skbuff.h>
140#include <linux/netdevice.h>
141#include <linux/inet.h>
142#include <linux/inetdevice.h>
143#include <linux/rtnetlink.h>
144#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700145#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#include <linux/in.h>
147#include <linux/ip.h>
148#include <linux/ipv6.h>
149#include <linux/udp.h>
150#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700151#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800153#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800154#include <linux/kthread.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200155#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#include <net/checksum.h>
157#include <net/ipv6.h>
158#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700159#ifdef CONFIG_XFRM
160#include <net/xfrm.h>
161#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#include <asm/byteorder.h>
163#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700164#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#include <asm/io.h>
166#include <asm/dma.h>
167#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800168#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#include <asm/timex.h>
170
Robert 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#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800174#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800175#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800178#define F_IPSRC_RND (1<<0) /* IP-Src Random */
179#define F_IPDST_RND (1<<1) /* IP-Dst Random */
180#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800186#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700187#define F_VID_RND (1<<9) /* Random VLAN ID */
188#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700189#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700190#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700191#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800194#define T_TERMINATE (1<<0)
195#define T_STOP (1<<1) /* Stop run */
196#define T_RUN (1<<2) /* Start run */
197#define T_REMDEVALL (1<<3) /* Remove all devs */
198#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* If lock -- can be removed after some work */
201#define if_lock(t) spin_lock(&(t->if_lock));
202#define if_unlock(t) spin_unlock(&(t->if_lock));
203
204/* Used to help with determining the pkts on receive */
205#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700206#define PG_PROC_DIR "pktgen"
207#define PGCTRL "pgctrl"
208static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210#define MAX_CFLOWS 65536
211
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700212#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
213#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
214
Luiz Capitulino222f1802006-03-20 22:16:13 -0800215struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800216 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800217 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700218#ifdef CONFIG_XFRM
219 struct xfrm_state *x;
220#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700221 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700224/* flow flag bits */
225#define F_INIT (1<<0) /* flow has been initialized */
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * Try to keep frequent/infrequent used vars. separated.
230 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800231 struct proc_dir_entry *entry; /* proc file */
232 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800233 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Luiz Capitulino222f1802006-03-20 22:16:13 -0800235 int running; /* if this changes to false, the test will stop */
236
237 /* If min != max, then we will either do a linear iteration, or
238 * we will do a random selection from within the range.
239 */
240 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800241 int removal_mark; /* non-zero => the device is marked for
242 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Luiz Capitulino222f1802006-03-20 22:16:13 -0800244 int min_pkt_size; /* = ETH_ZLEN; */
245 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700246 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800247 int nfrags;
248 __u32 delay_us; /* Default delay */
249 __u32 delay_ns;
250 __u64 count; /* Default No packets to send */
251 __u64 sofar; /* How many pkts we've sent so far */
252 __u64 tx_bytes; /* How many bytes we've transmitted */
253 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Luiz Capitulino222f1802006-03-20 22:16:13 -0800255 /* runtime counters relating to clone_skb */
256 __u64 next_tx_us; /* timestamp of when to tx next */
257 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Luiz Capitulino222f1802006-03-20 22:16:13 -0800259 __u64 allocated_skbs;
260 __u32 clone_count;
261 int last_ok; /* Was last skb sent?
262 * Or a failed transmit of some sort? This will keep
263 * sequence numbers in order, for example.
264 */
265 __u64 started_at; /* micro-seconds */
266 __u64 stopped_at; /* micro-seconds */
267 __u64 idle_acc; /* micro-seconds */
268 __u32 seq_num;
269
270 int clone_skb; /* Use multiple SKBs during packet gen. If this number
271 * is greater than 1, then that many copies of the same
272 * packet will be sent before a new packet is allocated.
273 * For instance, if you want to send 1024 identical packets
274 * before creating a new packet, set clone_skb to 1024.
275 */
276
277 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
278 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281
282 struct in6_addr in6_saddr;
283 struct in6_addr in6_daddr;
284 struct in6_addr cur_in6_daddr;
285 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800287 struct in6_addr min_in6_daddr;
288 struct in6_addr max_in6_daddr;
289 struct in6_addr min_in6_saddr;
290 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Luiz Capitulino222f1802006-03-20 22:16:13 -0800292 /* If we're doing ranges, random or incremental, then this
293 * defines the min/max for those ranges.
294 */
Al Viro252e3342006-11-14 20:48:11 -0800295 __be32 saddr_min; /* inclusive, source IP address */
296 __be32 saddr_max; /* exclusive, source IP address */
297 __be32 daddr_min; /* inclusive, dest IP address */
298 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Luiz Capitulino222f1802006-03-20 22:16:13 -0800300 __u16 udp_src_min; /* inclusive, source UDP port */
301 __u16 udp_src_max; /* exclusive, source UDP port */
302 __u16 udp_dst_min; /* inclusive, dest UDP port */
303 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700305 /* DSCP + ECN */
306 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
307 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
308
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800309 /* MPLS */
310 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
311 __be32 labels[MAX_MPLS_LABELS];
312
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700313 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
314 __u8 vlan_p;
315 __u8 vlan_cfi;
316 __u16 vlan_id; /* 0xffff means no vlan tag */
317
318 __u8 svlan_p;
319 __u8 svlan_cfi;
320 __u16 svlan_id; /* 0xffff means no svlan tag */
321
Luiz Capitulino222f1802006-03-20 22:16:13 -0800322 __u32 src_mac_count; /* How many MACs to iterate through */
323 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Luiz Capitulino222f1802006-03-20 22:16:13 -0800325 unsigned char dst_mac[ETH_ALEN];
326 unsigned char src_mac[ETH_ALEN];
327
328 __u32 cur_dst_mac_offset;
329 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800330 __be32 cur_saddr;
331 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800332 __u16 cur_udp_dst;
333 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700334 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800335 __u32 cur_pkt_size;
336
337 __u8 hh[14];
338 /* = {
339 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
340
341 We fill in SRC address later
342 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343 0x08, 0x00
344 };
345 */
346 __u16 pad; /* pad out the hh struct to an even 16 bytes */
347
348 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
349 * are transmitting the same one multiple times
350 */
351 struct net_device *odev; /* The out-going device. Note that the device should
352 * have it's pg_info pointer pointing back to this
353 * device. This will be set when the user specifies
354 * the out-going device name (not when the inject is
355 * started as it used to do.)
356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800358 unsigned cflows; /* Concurrent flows (config) */
359 unsigned lflow; /* Flow length (config) */
360 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700361 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700362
363 u16 queue_map_min;
364 u16 queue_map_max;
365
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700366#ifdef CONFIG_XFRM
367 __u8 ipsmode; /* IPSEC mode (config) */
368 __u8 ipsproto; /* IPSEC type (config) */
369#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800370 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
373struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800374 __be32 pgh_magic;
375 __be32 seq_num;
376 __be32 tv_sec;
377 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
380struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800381 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800382 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800383 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800384 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800385 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Luiz Capitulino222f1802006-03-20 22:16:13 -0800387 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
388
389 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int cpu;
391
Luiz Capitulino222f1802006-03-20 22:16:13 -0800392 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700393 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394};
395
396#define REMOVE 1
397#define FIND 0
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800400static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800402 __u64 us = tv->tv_usec;
403 us += (__u64) tv->tv_sec * (__u64) 1000000;
404 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Ilpo Järvinen6db105d2008-01-12 03:25:00 -0800407static __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800409 struct timeval tv;
410 do_gettimeofday(&tv);
411 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/* old include end */
415
416static char version[] __initdata = VERSION;
417
Luiz Capitulino222f1802006-03-20 22:16:13 -0800418static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
419static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
420static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
421 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
423static void pktgen_run_all_threads(void);
424static void pktgen_stop_all_threads_ifs(void);
425static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800426static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800428
Luiz Capitulino222f1802006-03-20 22:16:13 -0800429static unsigned int scan_ip6(const char *s, char ip[16]);
430static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800433static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb92005-12-22 12:51:46 -0800434static int pg_delay_d;
435static int pg_clone_skb_d;
436static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Luiz Capitulino222fa072006-03-20 22:24:27 -0800438static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800439static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static struct notifier_block pktgen_notifier_block = {
442 .notifier_call = pktgen_device_event,
443};
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900446 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *
448 */
449
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700450static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800451{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700452 seq_puts(seq, VERSION);
453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Luiz Capitulino222f1802006-03-20 22:16:13 -0800456static ssize_t pgctrl_write(struct file *file, const char __user * buf,
457 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int err = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700460 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Luiz Capitulino222f1802006-03-20 22:16:13 -0800462 if (!capable(CAP_NET_ADMIN)) {
463 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700467 if (count > sizeof(data))
468 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700471 err = -EFAULT;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700472 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800473 }
474 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Luiz Capitulino222f1802006-03-20 22:16:13 -0800476 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 pktgen_stop_all_threads_ifs();
478
Luiz Capitulino222f1802006-03-20 22:16:13 -0800479 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 pktgen_run_all_threads();
481
Luiz Capitulino222f1802006-03-20 22:16:13 -0800482 else
David S. Miller25a8b252007-07-30 16:11:48 -0700483 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 err = count;
486
Luiz Capitulino222f1802006-03-20 22:16:13 -0800487out:
488 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700491static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700493 return single_open(file, pgctrl_show, PDE(inode)->data);
494}
495
Arjan van de Ven9a321442007-02-12 00:55:35 -0800496static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800497 .owner = THIS_MODULE,
498 .open = pgctrl_open,
499 .read = seq_read,
500 .llseek = seq_lseek,
501 .write = pgctrl_write,
502 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700503};
504
505static int pktgen_if_show(struct seq_file *seq, void *v)
506{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800507 struct pktgen_dev *pkt_dev = seq->private;
508 __u64 sa;
509 __u64 stopped;
510 __u64 now = getCurUs();
Joe Perches0795af52007-10-03 17:59:30 -0700511 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Luiz Capitulino222f1802006-03-20 22:16:13 -0800513 seq_printf(seq,
514 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
515 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
516 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Luiz Capitulino222f1802006-03-20 22:16:13 -0800518 seq_printf(seq,
519 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
520 pkt_dev->nfrags,
521 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800522 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Luiz Capitulino222f1802006-03-20 22:16:13 -0800524 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
525 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Robert Olsson45b270f2007-08-28 15:45:55 -0700527 seq_printf(seq,
528 " queue_map_min: %u queue_map_max: %u\n",
529 pkt_dev->queue_map_min,
530 pkt_dev->queue_map_max);
531
Luiz Capitulino222f1802006-03-20 22:16:13 -0800532 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800534 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
535 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
536 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
537 seq_printf(seq,
538 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
539 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Luiz Capitulino222f1802006-03-20 22:16:13 -0800541 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
542 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
543 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
544 seq_printf(seq,
545 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
546 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Luiz Capitulino222f1802006-03-20 22:16:13 -0800548 } else
549 seq_printf(seq,
550 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
551 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
552 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700554 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Joe Perches0795af52007-10-03 17:59:30 -0700556 seq_printf(seq, "%s ",
557 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
558 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Luiz Capitulino222f1802006-03-20 22:16:13 -0800560 seq_printf(seq, "dst_mac: ");
Joe Perches0795af52007-10-03 17:59:30 -0700561 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Luiz Capitulino222f1802006-03-20 22:16:13 -0800563 seq_printf(seq,
564 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
565 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
566 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Luiz Capitulino222f1802006-03-20 22:16:13 -0800568 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800569 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700570 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800572 if (pkt_dev->nr_labels) {
573 unsigned i;
574 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700575 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800576 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
577 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
578 }
579
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700580 if (pkt_dev->vlan_id != 0xffff) {
581 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
582 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
583 }
584
585 if (pkt_dev->svlan_id != 0xffff) {
586 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
587 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
588 }
589
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700590 if (pkt_dev->tos) {
591 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
592 }
593
594 if (pkt_dev->traffic_class) {
595 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
596 }
597
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800598 seq_printf(seq, " Flags: ");
599
Luiz Capitulino222f1802006-03-20 22:16:13 -0800600 if (pkt_dev->flags & F_IPV6)
601 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Luiz Capitulino222f1802006-03-20 22:16:13 -0800603 if (pkt_dev->flags & F_IPSRC_RND)
604 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Luiz Capitulino222f1802006-03-20 22:16:13 -0800606 if (pkt_dev->flags & F_IPDST_RND)
607 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Luiz Capitulino222f1802006-03-20 22:16:13 -0800609 if (pkt_dev->flags & F_TXSIZE_RND)
610 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Luiz Capitulino222f1802006-03-20 22:16:13 -0800612 if (pkt_dev->flags & F_UDPSRC_RND)
613 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Luiz Capitulino222f1802006-03-20 22:16:13 -0800615 if (pkt_dev->flags & F_UDPDST_RND)
616 seq_printf(seq, "UDPDST_RND ");
617
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800618 if (pkt_dev->flags & F_MPLS_RND)
619 seq_printf(seq, "MPLS_RND ");
620
Robert Olsson45b270f2007-08-28 15:45:55 -0700621 if (pkt_dev->flags & F_QUEUE_MAP_RND)
622 seq_printf(seq, "QUEUE_MAP_RND ");
623
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700624 if (pkt_dev->cflows) {
625 if (pkt_dev->flags & F_FLOW_SEQ)
626 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
627 else
628 seq_printf(seq, "FLOW_RND ");
629 }
630
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700631#ifdef CONFIG_XFRM
632 if (pkt_dev->flags & F_IPSEC_ON)
633 seq_printf(seq, "IPSEC ");
634#endif
635
Luiz Capitulino222f1802006-03-20 22:16:13 -0800636 if (pkt_dev->flags & F_MACSRC_RND)
637 seq_printf(seq, "MACSRC_RND ");
638
639 if (pkt_dev->flags & F_MACDST_RND)
640 seq_printf(seq, "MACDST_RND ");
641
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700642 if (pkt_dev->flags & F_VID_RND)
643 seq_printf(seq, "VID_RND ");
644
645 if (pkt_dev->flags & F_SVID_RND)
646 seq_printf(seq, "SVID_RND ");
647
Luiz Capitulino222f1802006-03-20 22:16:13 -0800648 seq_puts(seq, "\n");
649
650 sa = pkt_dev->started_at;
651 stopped = pkt_dev->stopped_at;
652 if (pkt_dev->running)
653 stopped = now; /* not really stopped, more like last-running-at */
654
655 seq_printf(seq,
656 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
657 (unsigned long long)pkt_dev->sofar,
658 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
659 (unsigned long long)stopped,
660 (unsigned long long)pkt_dev->idle_acc);
661
662 seq_printf(seq,
663 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700664 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
665 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Luiz Capitulino222f1802006-03-20 22:16:13 -0800667 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800669 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
670 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
671 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
672 } else
673 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700674 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Luiz Capitulino222f1802006-03-20 22:16:13 -0800676 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700677 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Robert Olsson45b270f2007-08-28 15:45:55 -0700679 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
680
Luiz Capitulino222f1802006-03-20 22:16:13 -0800681 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800684 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800686 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800691
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700692static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800693{
694 int i = 0;
695 *num = 0;
696
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700697 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800698 char c;
699 *num <<= 4;
700 if (get_user(c, &user_buffer[i]))
701 return -EFAULT;
702 if ((c >= '0') && (c <= '9'))
703 *num |= c - '0';
704 else if ((c >= 'a') && (c <= 'f'))
705 *num |= c - 'a' + 10;
706 else if ((c >= 'A') && (c <= 'F'))
707 *num |= c - 'A' + 10;
708 else
709 break;
710 }
711 return i;
712}
713
Luiz Capitulino222f1802006-03-20 22:16:13 -0800714static int count_trail_chars(const char __user * user_buffer,
715 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 int i;
718
719 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800720 char c;
721 if (get_user(c, &user_buffer[i]))
722 return -EFAULT;
723 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 case '\"':
725 case '\n':
726 case '\r':
727 case '\t':
728 case ' ':
729 case '=':
730 break;
731 default:
732 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735done:
736 return i;
737}
738
Luiz Capitulino222f1802006-03-20 22:16:13 -0800739static unsigned long num_arg(const char __user * user_buffer,
740 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 int i = 0;
743 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800744
745 for (; i < maxlen; i++) {
746 char c;
747 if (get_user(c, &user_buffer[i]))
748 return -EFAULT;
749 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800751 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 } else
753 break;
754 }
755 return i;
756}
757
Luiz Capitulino222f1802006-03-20 22:16:13 -0800758static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 int i = 0;
761
Luiz Capitulino222f1802006-03-20 22:16:13 -0800762 for (; i < maxlen; i++) {
763 char c;
764 if (get_user(c, &user_buffer[i]))
765 return -EFAULT;
766 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 case '\"':
768 case '\n':
769 case '\r':
770 case '\t':
771 case ' ':
772 goto done_str;
773 break;
774 default:
775 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return i;
780}
781
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800782static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
783{
784 unsigned n = 0;
785 char c;
786 ssize_t i = 0;
787 int len;
788
789 pkt_dev->nr_labels = 0;
790 do {
791 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700792 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800793 if (len <= 0)
794 return len;
795 pkt_dev->labels[n] = htonl(tmp);
796 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
797 pkt_dev->flags |= F_MPLS_RND;
798 i += len;
799 if (get_user(c, &buffer[i]))
800 return -EFAULT;
801 i++;
802 n++;
803 if (n >= MAX_MPLS_LABELS)
804 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700805 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800806
807 pkt_dev->nr_labels = n;
808 return i;
809}
810
Luiz Capitulino222f1802006-03-20 22:16:13 -0800811static ssize_t pktgen_if_write(struct file *file,
812 const char __user * user_buffer, size_t count,
813 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800815 struct seq_file *seq = (struct seq_file *)file->private_data;
816 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int i = 0, max, len;
818 char name[16], valstr[32];
819 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800820 char *pg_result = NULL;
821 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800823
824 pg_result = &(pkt_dev->result[0]);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700827 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EINVAL;
829 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 max = count - i;
832 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800833 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700834 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800835 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800837 i += tmp;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* Read variable name */
840
841 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800842 if (len < 0) {
843 return len;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800846 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return -EFAULT;
848 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800849
850 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800852 if (len < 0)
853 return len;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 i += len;
856
857 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800858 char tb[count + 1];
859 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800861 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700862 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863 (unsigned long)count, tb);
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 if (!strcmp(name, "min_pkt_size")) {
867 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800868 if (len < 0) {
869 return len;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800872 if (value < 14 + 20 + 8)
873 value = 14 + 20 + 8;
874 if (value != pkt_dev->min_pkt_size) {
875 pkt_dev->min_pkt_size = value;
876 pkt_dev->cur_pkt_size = value;
877 }
878 sprintf(pg_result, "OK: min_pkt_size=%u",
879 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return count;
881 }
882
Luiz Capitulino222f1802006-03-20 22:16:13 -0800883 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800885 if (len < 0) {
886 return len;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889 if (value < 14 + 20 + 8)
890 value = 14 + 20 + 8;
891 if (value != pkt_dev->max_pkt_size) {
892 pkt_dev->max_pkt_size = value;
893 pkt_dev->cur_pkt_size = value;
894 }
895 sprintf(pg_result, "OK: max_pkt_size=%u",
896 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return count;
898 }
899
Luiz Capitulino222f1802006-03-20 22:16:13 -0800900 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (!strcmp(name, "pkt_size")) {
903 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 if (len < 0) {
905 return len;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800908 if (value < 14 + 20 + 8)
909 value = 14 + 20 + 8;
910 if (value != pkt_dev->min_pkt_size) {
911 pkt_dev->min_pkt_size = value;
912 pkt_dev->max_pkt_size = value;
913 pkt_dev->cur_pkt_size = value;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
916 return count;
917 }
918
Luiz Capitulino222f1802006-03-20 22:16:13 -0800919 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800921 if (len < 0) {
922 return len;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800925 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 sprintf(pg_result, "OK: debug=%u", debug);
927 return count;
928 }
929
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800932 if (len < 0) {
933 return len;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 i += len;
936 pkt_dev->nfrags = value;
937 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
938 return count;
939 }
940 if (!strcmp(name, "delay")) {
941 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800942 if (len < 0) {
943 return len;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 i += len;
946 if (value == 0x7FFFFFFF) {
947 pkt_dev->delay_us = 0x7FFFFFFF;
948 pkt_dev->delay_ns = 0;
949 } else {
950 pkt_dev->delay_us = value / 1000;
951 pkt_dev->delay_ns = value % 1000;
952 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800953 sprintf(pg_result, "OK: delay=%u",
954 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return count;
956 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800957 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800959 if (len < 0) {
960 return len;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800963 if (value != pkt_dev->udp_src_min) {
964 pkt_dev->udp_src_min = value;
965 pkt_dev->cur_udp_src = value;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
968 return count;
969 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800970 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800972 if (len < 0) {
973 return len;
974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800976 if (value != pkt_dev->udp_dst_min) {
977 pkt_dev->udp_dst_min = value;
978 pkt_dev->cur_udp_dst = value;
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
981 return count;
982 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800983 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800985 if (len < 0) {
986 return len;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800989 if (value != pkt_dev->udp_src_max) {
990 pkt_dev->udp_src_max = value;
991 pkt_dev->cur_udp_src = value;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
994 return count;
995 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800996 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800998 if (len < 0) {
999 return len;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001002 if (value != pkt_dev->udp_dst_max) {
1003 pkt_dev->udp_dst_max = value;
1004 pkt_dev->cur_udp_dst = value;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1007 return count;
1008 }
1009 if (!strcmp(name, "clone_skb")) {
1010 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001011 if (len < 0) {
1012 return len;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001015 pkt_dev->clone_skb = value;
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1018 return count;
1019 }
1020 if (!strcmp(name, "count")) {
1021 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001022 if (len < 0) {
1023 return len;
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 i += len;
1026 pkt_dev->count = value;
1027 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001028 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return count;
1030 }
1031 if (!strcmp(name, "src_mac_count")) {
1032 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001033 if (len < 0) {
1034 return len;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 i += len;
1037 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001038 pkt_dev->src_mac_count = value;
1039 pkt_dev->cur_src_mac_offset = 0;
1040 }
1041 sprintf(pg_result, "OK: src_mac_count=%d",
1042 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return count;
1044 }
1045 if (!strcmp(name, "dst_mac_count")) {
1046 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001047 if (len < 0) {
1048 return len;
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 i += len;
1051 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 pkt_dev->dst_mac_count = value;
1053 pkt_dev->cur_dst_mac_offset = 0;
1054 }
1055 sprintf(pg_result, "OK: dst_mac_count=%d",
1056 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return count;
1058 }
1059 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001060 char f[32];
1061 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001063 if (len < 0) {
1064 return len;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (copy_from_user(f, &user_buffer[i], len))
1067 return -EFAULT;
1068 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001069 if (strcmp(f, "IPSRC_RND") == 0)
1070 pkt_dev->flags |= F_IPSRC_RND;
1071
1072 else if (strcmp(f, "!IPSRC_RND") == 0)
1073 pkt_dev->flags &= ~F_IPSRC_RND;
1074
1075 else if (strcmp(f, "TXSIZE_RND") == 0)
1076 pkt_dev->flags |= F_TXSIZE_RND;
1077
1078 else if (strcmp(f, "!TXSIZE_RND") == 0)
1079 pkt_dev->flags &= ~F_TXSIZE_RND;
1080
1081 else if (strcmp(f, "IPDST_RND") == 0)
1082 pkt_dev->flags |= F_IPDST_RND;
1083
1084 else if (strcmp(f, "!IPDST_RND") == 0)
1085 pkt_dev->flags &= ~F_IPDST_RND;
1086
1087 else if (strcmp(f, "UDPSRC_RND") == 0)
1088 pkt_dev->flags |= F_UDPSRC_RND;
1089
1090 else if (strcmp(f, "!UDPSRC_RND") == 0)
1091 pkt_dev->flags &= ~F_UDPSRC_RND;
1092
1093 else if (strcmp(f, "UDPDST_RND") == 0)
1094 pkt_dev->flags |= F_UDPDST_RND;
1095
1096 else if (strcmp(f, "!UDPDST_RND") == 0)
1097 pkt_dev->flags &= ~F_UDPDST_RND;
1098
1099 else if (strcmp(f, "MACSRC_RND") == 0)
1100 pkt_dev->flags |= F_MACSRC_RND;
1101
1102 else if (strcmp(f, "!MACSRC_RND") == 0)
1103 pkt_dev->flags &= ~F_MACSRC_RND;
1104
1105 else if (strcmp(f, "MACDST_RND") == 0)
1106 pkt_dev->flags |= F_MACDST_RND;
1107
1108 else if (strcmp(f, "!MACDST_RND") == 0)
1109 pkt_dev->flags &= ~F_MACDST_RND;
1110
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001111 else if (strcmp(f, "MPLS_RND") == 0)
1112 pkt_dev->flags |= F_MPLS_RND;
1113
1114 else if (strcmp(f, "!MPLS_RND") == 0)
1115 pkt_dev->flags &= ~F_MPLS_RND;
1116
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001117 else if (strcmp(f, "VID_RND") == 0)
1118 pkt_dev->flags |= F_VID_RND;
1119
1120 else if (strcmp(f, "!VID_RND") == 0)
1121 pkt_dev->flags &= ~F_VID_RND;
1122
1123 else if (strcmp(f, "SVID_RND") == 0)
1124 pkt_dev->flags |= F_SVID_RND;
1125
1126 else if (strcmp(f, "!SVID_RND") == 0)
1127 pkt_dev->flags &= ~F_SVID_RND;
1128
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001129 else if (strcmp(f, "FLOW_SEQ") == 0)
1130 pkt_dev->flags |= F_FLOW_SEQ;
1131
Robert Olsson45b270f2007-08-28 15:45:55 -07001132 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1133 pkt_dev->flags |= F_QUEUE_MAP_RND;
1134
1135 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1136 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001137#ifdef CONFIG_XFRM
1138 else if (strcmp(f, "IPSEC") == 0)
1139 pkt_dev->flags |= F_IPSEC_ON;
1140#endif
1141
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001142 else if (strcmp(f, "!IPV6") == 0)
1143 pkt_dev->flags &= ~F_IPV6;
1144
Luiz Capitulino222f1802006-03-20 22:16:13 -08001145 else {
1146 sprintf(pg_result,
1147 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1148 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001149 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001150 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001151 return count;
1152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1154 return count;
1155 }
1156 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1157 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001158 if (len < 0) {
1159 return len;
1160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Luiz Capitulino222f1802006-03-20 22:16:13 -08001162 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001164 buf[len] = 0;
1165 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1166 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1167 strncpy(pkt_dev->dst_min, buf, len);
1168 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1169 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1170 }
1171 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001172 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001173 pkt_dev->dst_min);
1174 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1176 return count;
1177 }
1178 if (!strcmp(name, "dst_max")) {
1179 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001180 if (len < 0) {
1181 return len;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Luiz Capitulino222f1802006-03-20 22:16:13 -08001184 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -EFAULT;
1186
Luiz Capitulino222f1802006-03-20 22:16:13 -08001187 buf[len] = 0;
1188 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1189 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1190 strncpy(pkt_dev->dst_max, buf, len);
1191 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1192 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1193 }
1194 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001195 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001196 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 i += len;
1198 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1199 return count;
1200 }
1201 if (!strcmp(name, "dst6")) {
1202 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001203 if (len < 0)
1204 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 pkt_dev->flags |= F_IPV6;
1207
Luiz Capitulino222f1802006-03-20 22:16:13 -08001208 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001210 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001213 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1216
Luiz Capitulino222f1802006-03-20 22:16:13 -08001217 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001218 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Luiz Capitulino222f1802006-03-20 22:16:13 -08001220 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 sprintf(pg_result, "OK: dst6=%s", buf);
1222 return count;
1223 }
1224 if (!strcmp(name, "dst6_min")) {
1225 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001226 if (len < 0)
1227 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 pkt_dev->flags |= F_IPV6;
1230
Luiz Capitulino222f1802006-03-20 22:16:13 -08001231 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001233 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001236 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Luiz Capitulino222f1802006-03-20 22:16:13 -08001238 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1239 &pkt_dev->min_in6_daddr);
1240 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001241 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Luiz Capitulino222f1802006-03-20 22:16:13 -08001243 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 sprintf(pg_result, "OK: dst6_min=%s", buf);
1245 return count;
1246 }
1247 if (!strcmp(name, "dst6_max")) {
1248 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001249 if (len < 0)
1250 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 pkt_dev->flags |= F_IPV6;
1253
Luiz Capitulino222f1802006-03-20 22:16:13 -08001254 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001256 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Luiz Capitulino222f1802006-03-20 22:16:13 -08001261 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001262 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Luiz Capitulino222f1802006-03-20 22:16:13 -08001264 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 sprintf(pg_result, "OK: dst6_max=%s", buf);
1266 return count;
1267 }
1268 if (!strcmp(name, "src6")) {
1269 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001270 if (len < 0)
1271 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 pkt_dev->flags |= F_IPV6;
1274
Luiz Capitulino222f1802006-03-20 22:16:13 -08001275 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001277 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001280 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1283
Luiz Capitulino222f1802006-03-20 22:16:13 -08001284 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001285 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286
1287 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 sprintf(pg_result, "OK: src6=%s", buf);
1289 return count;
1290 }
1291 if (!strcmp(name, "src_min")) {
1292 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001293 if (len < 0) {
1294 return len;
1295 }
1296 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001298 buf[len] = 0;
1299 if (strcmp(buf, pkt_dev->src_min) != 0) {
1300 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1301 strncpy(pkt_dev->src_min, buf, len);
1302 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1303 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1304 }
1305 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001306 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 i += len;
1309 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1310 return count;
1311 }
1312 if (!strcmp(name, "src_max")) {
1313 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001314 if (len < 0) {
1315 return len;
1316 }
1317 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 buf[len] = 0;
1320 if (strcmp(buf, pkt_dev->src_max) != 0) {
1321 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1322 strncpy(pkt_dev->src_max, buf, len);
1323 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1324 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1325 }
1326 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001327 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001328 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 i += len;
1330 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1331 return count;
1332 }
1333 if (!strcmp(name, "dst_mac")) {
1334 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001335 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001337 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001340 if (len < 0) {
1341 return len;
1342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001344 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return -EFAULT;
1346 i += len;
1347
Luiz Capitulino222f1802006-03-20 22:16:13 -08001348 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (*v >= '0' && *v <= '9') {
1350 *m *= 16;
1351 *m += *v - '0';
1352 }
1353 if (*v >= 'A' && *v <= 'F') {
1354 *m *= 16;
1355 *m += *v - 'A' + 10;
1356 }
1357 if (*v >= 'a' && *v <= 'f') {
1358 *m *= 16;
1359 *m += *v - 'a' + 10;
1360 }
1361 if (*v == ':') {
1362 m++;
1363 *m = 0;
1364 }
1365 }
1366
1367 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001368 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1369 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 sprintf(pg_result, "OK: dstmac");
1372 return count;
1373 }
1374 if (!strcmp(name, "src_mac")) {
1375 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001376 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 unsigned char *m = pkt_dev->src_mac;
1378
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001379 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001382 if (len < 0) {
1383 return len;
1384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001386 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EFAULT;
1388 i += len;
1389
Luiz Capitulino222f1802006-03-20 22:16:13 -08001390 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (*v >= '0' && *v <= '9') {
1392 *m *= 16;
1393 *m += *v - '0';
1394 }
1395 if (*v >= 'A' && *v <= 'F') {
1396 *m *= 16;
1397 *m += *v - 'A' + 10;
1398 }
1399 if (*v >= 'a' && *v <= 'f') {
1400 *m *= 16;
1401 *m += *v - 'a' + 10;
1402 }
1403 if (*v == ':') {
1404 m++;
1405 *m = 0;
1406 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001409 /* Set up Src MAC */
1410 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1411 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1412
Luiz Capitulino222f1802006-03-20 22:16:13 -08001413 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return count;
1415 }
1416
Luiz Capitulino222f1802006-03-20 22:16:13 -08001417 if (!strcmp(name, "clear_counters")) {
1418 pktgen_clear_counters(pkt_dev);
1419 sprintf(pg_result, "OK: Clearing counters.\n");
1420 return count;
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 if (!strcmp(name, "flows")) {
1424 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001425 if (len < 0) {
1426 return len;
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 i += len;
1429 if (value > MAX_CFLOWS)
1430 value = MAX_CFLOWS;
1431
1432 pkt_dev->cflows = value;
1433 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1434 return count;
1435 }
1436
1437 if (!strcmp(name, "flowlen")) {
1438 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001439 if (len < 0) {
1440 return len;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 i += len;
1443 pkt_dev->lflow = value;
1444 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1445 return count;
1446 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001447
Robert Olsson45b270f2007-08-28 15:45:55 -07001448 if (!strcmp(name, "queue_map_min")) {
1449 len = num_arg(&user_buffer[i], 5, &value);
1450 if (len < 0) {
1451 return len;
1452 }
1453 i += len;
1454 pkt_dev->queue_map_min = value;
1455 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1456 return count;
1457 }
1458
1459 if (!strcmp(name, "queue_map_max")) {
1460 len = num_arg(&user_buffer[i], 5, &value);
1461 if (len < 0) {
1462 return len;
1463 }
1464 i += len;
1465 pkt_dev->queue_map_max = value;
1466 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1467 return count;
1468 }
1469
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001470 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001471 unsigned n, cnt;
1472
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001473 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001474 if (len < 0)
1475 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001476 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001477 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001478 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001479 cnt += sprintf(pg_result + cnt,
1480 "%08x%s", ntohl(pkt_dev->labels[n]),
1481 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001482
1483 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1484 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1485 pkt_dev->svlan_id = 0xffff;
1486
1487 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001488 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001489 }
1490 return count;
1491 }
1492
1493 if (!strcmp(name, "vlan_id")) {
1494 len = num_arg(&user_buffer[i], 4, &value);
1495 if (len < 0) {
1496 return len;
1497 }
1498 i += len;
1499 if (value <= 4095) {
1500 pkt_dev->vlan_id = value; /* turn on VLAN */
1501
1502 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001503 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001504
1505 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001506 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001507
1508 pkt_dev->nr_labels = 0; /* turn off MPLS */
1509 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1510 } else {
1511 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1512 pkt_dev->svlan_id = 0xffff;
1513
1514 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001515 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001516 }
1517 return count;
1518 }
1519
1520 if (!strcmp(name, "vlan_p")) {
1521 len = num_arg(&user_buffer[i], 1, &value);
1522 if (len < 0) {
1523 return len;
1524 }
1525 i += len;
1526 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1527 pkt_dev->vlan_p = value;
1528 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1529 } else {
1530 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1531 }
1532 return count;
1533 }
1534
1535 if (!strcmp(name, "vlan_cfi")) {
1536 len = num_arg(&user_buffer[i], 1, &value);
1537 if (len < 0) {
1538 return len;
1539 }
1540 i += len;
1541 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1542 pkt_dev->vlan_cfi = value;
1543 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1544 } else {
1545 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1546 }
1547 return count;
1548 }
1549
1550 if (!strcmp(name, "svlan_id")) {
1551 len = num_arg(&user_buffer[i], 4, &value);
1552 if (len < 0) {
1553 return len;
1554 }
1555 i += len;
1556 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1557 pkt_dev->svlan_id = value; /* turn on SVLAN */
1558
1559 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001560 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001561
1562 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001563 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001564
1565 pkt_dev->nr_labels = 0; /* turn off MPLS */
1566 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1567 } else {
1568 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1569 pkt_dev->svlan_id = 0xffff;
1570
1571 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001572 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001573 }
1574 return count;
1575 }
1576
1577 if (!strcmp(name, "svlan_p")) {
1578 len = num_arg(&user_buffer[i], 1, &value);
1579 if (len < 0) {
1580 return len;
1581 }
1582 i += len;
1583 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1584 pkt_dev->svlan_p = value;
1585 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1586 } else {
1587 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1588 }
1589 return count;
1590 }
1591
1592 if (!strcmp(name, "svlan_cfi")) {
1593 len = num_arg(&user_buffer[i], 1, &value);
1594 if (len < 0) {
1595 return len;
1596 }
1597 i += len;
1598 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1599 pkt_dev->svlan_cfi = value;
1600 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1601 } else {
1602 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1603 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001604 return count;
1605 }
1606
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001607 if (!strcmp(name, "tos")) {
1608 __u32 tmp_value = 0;
1609 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1610 if (len < 0) {
1611 return len;
1612 }
1613 i += len;
1614 if (len == 2) {
1615 pkt_dev->tos = tmp_value;
1616 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1617 } else {
1618 sprintf(pg_result, "ERROR: tos must be 00-ff");
1619 }
1620 return count;
1621 }
1622
1623 if (!strcmp(name, "traffic_class")) {
1624 __u32 tmp_value = 0;
1625 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1626 if (len < 0) {
1627 return len;
1628 }
1629 i += len;
1630 if (len == 2) {
1631 pkt_dev->traffic_class = tmp_value;
1632 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1633 } else {
1634 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1635 }
1636 return count;
1637 }
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1640 return -EINVAL;
1641}
1642
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001643static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001645 return single_open(file, pktgen_if_show, PDE(inode)->data);
1646}
1647
Arjan van de Ven9a321442007-02-12 00:55:35 -08001648static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001649 .owner = THIS_MODULE,
1650 .open = pktgen_if_open,
1651 .read = seq_read,
1652 .llseek = seq_lseek,
1653 .write = pktgen_if_write,
1654 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001655};
1656
1657static int pktgen_thread_show(struct seq_file *seq, void *v)
1658{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001659 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001660 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001662 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Luiz Capitulino222f1802006-03-20 22:16:13 -08001664 seq_printf(seq, "Running: ");
1665
1666 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001667 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001668 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001669 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Luiz Capitulino222f1802006-03-20 22:16:13 -08001671 seq_printf(seq, "\nStopped: ");
1672
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001673 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001674 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001675 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 if (t->result[0])
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001678 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 else
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001680 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Luiz Capitulino222f1802006-03-20 22:16:13 -08001682 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001687static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001688 const char __user * user_buffer,
1689 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001691 struct seq_file *seq = (struct seq_file *)file->private_data;
1692 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 int i = 0, max, len, ret;
1694 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001695 char *pg_result;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001698 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return -EINVAL;
1700 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001703 len = count_trail_chars(&user_buffer[i], max);
1704 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001705 return len;
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 i += len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /* Read variable name */
1710
1711 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001712 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001713 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 memset(name, 0, sizeof(name));
1716 if (copy_from_user(name, &user_buffer[i], len))
1717 return -EFAULT;
1718 i += len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001719
Luiz Capitulino222f1802006-03-20 22:16:13 -08001720 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001722 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001723 return len;
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 i += len;
1726
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001727 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001728 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1729 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Luiz Capitulino222f1802006-03-20 22:16:13 -08001731 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001732 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 ret = -EINVAL;
1734 goto out;
1735 }
1736
1737 pg_result = &(t->result[0]);
1738
Luiz Capitulino222f1802006-03-20 22:16:13 -08001739 if (!strcmp(name, "add_device")) {
1740 char f[32];
1741 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001743 if (len < 0) {
1744 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto out;
1746 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001747 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return -EFAULT;
1749 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001750 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001751 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001752 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001753 ret = count;
1754 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 goto out;
1756 }
1757
Luiz Capitulino222f1802006-03-20 22:16:13 -08001758 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001759 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001760 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001761 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 goto out;
1766 }
1767
Luiz Capitulino222f1802006-03-20 22:16:13 -08001768 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001769 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001770 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 goto out;
1772 }
1773
1774 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001775out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return ret;
1777}
1778
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001779static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001781 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Arjan van de Ven9a321442007-02-12 00:55:35 -08001784static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001785 .owner = THIS_MODULE,
1786 .open = pktgen_thread_open,
1787 .read = seq_read,
1788 .llseek = seq_lseek,
1789 .write = pktgen_thread_write,
1790 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001791};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001794static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 struct pktgen_thread *t;
1797 struct pktgen_dev *pkt_dev = NULL;
1798
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001799 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 pkt_dev = pktgen_find_dev(t, ifname);
1801 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001802 if (remove) {
1803 if_lock(t);
1804 pkt_dev->removal_mark = 1;
1805 t->control |= T_REMDEV;
1806 if_unlock(t);
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 break;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001811 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001814/*
1815 * mark a device for removal
1816 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001817static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001820 const int max_tries = 10, msec_per_try = 125;
1821 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001822
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001823 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001824 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001825
Luiz Capitulino222f1802006-03-20 22:16:13 -08001826 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001827
1828 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829 if (pkt_dev == NULL)
1830 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001831
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001832 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001833 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1834 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001835 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001836 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001837
1838 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001839 printk(KERN_ERR "pktgen_mark_device: timed out after "
1840 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001841 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001842 break;
1843 }
1844
1845 }
1846
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001847 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001848}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001849
Stephen Hemminger39df2322007-03-04 16:11:51 -08001850static void pktgen_change_name(struct net_device *dev)
1851{
1852 struct pktgen_thread *t;
1853
1854 list_for_each_entry(t, &pktgen_threads, th_list) {
1855 struct pktgen_dev *pkt_dev;
1856
1857 list_for_each_entry(pkt_dev, &t->if_list, list) {
1858 if (pkt_dev->odev != dev)
1859 continue;
1860
1861 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1862
1863 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1864 pg_proc_dir);
1865 if (!pkt_dev->entry)
1866 printk(KERN_ERR "pktgen: can't move proc "
1867 " entry for '%s'\n", dev->name);
1868 break;
1869 }
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Luiz Capitulino222f1802006-03-20 22:16:13 -08001873static int pktgen_device_event(struct notifier_block *unused,
1874 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001876 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001878 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001879 return NOTIFY_DONE;
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /* It is OK that we do not hold the group lock right now,
1882 * as we run under the RTNL lock.
1883 */
1884
1885 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001886 case NETDEV_CHANGENAME:
1887 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001891 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 return NOTIFY_DONE;
1896}
1897
1898/* Associate pktgen_dev with a device. */
1899
Stephen Hemminger39df2322007-03-04 16:11:51 -08001900static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001901{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001903 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (pkt_dev->odev) {
1907 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001908 pkt_dev->odev = NULL;
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Eric W. Biederman881d9662007-09-17 11:56:21 -07001911 odev = dev_get_by_name(&init_net, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001913 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001914 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001918 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001919 err = -EINVAL;
1920 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001921 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001922 err = -ENETDOWN;
1923 } else {
1924 pkt_dev->odev = odev;
1925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001929 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
1932/* Read pkt_dev from the interface and set up internal pktgen_dev
1933 * structure to have the right information to create/send packets
1934 */
1935static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1936{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001937 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001938 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1939 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001940 sprintf(pkt_dev->result,
1941 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1942 return;
1943 }
1944
1945 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001947 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001948 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Luiz Capitulino222f1802006-03-20 22:16:13 -08001950 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001951 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Luiz Capitulino222f1802006-03-20 22:16:13 -08001953 /* Set up pkt size */
1954 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1955
1956 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001958 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 * gets exported
1960 */
1961
1962#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08001963 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct inet6_dev *idev;
1965
Luiz Capitulino222f1802006-03-20 22:16:13 -08001966 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1967 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 set = 1;
1969 break;
1970 }
1971
Luiz Capitulino222f1802006-03-20 22:16:13 -08001972 if (!set) {
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * Use linklevel address if unconfigured.
1976 *
1977 * use ipv6_get_lladdr if/when it's get exported
1978 */
1979
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001980 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1982 struct inet6_ifaddr *ifp;
1983
1984 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001985 for (ifp = idev->addr_list; ifp;
1986 ifp = ifp->if_next) {
1987 if (ifp->scope == IFA_LINK
1988 && !(ifp->
1989 flags & IFA_F_TENTATIVE)) {
1990 ipv6_addr_copy(&pkt_dev->
1991 cur_in6_saddr,
1992 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 err = 0;
1994 break;
1995 }
1996 }
1997 read_unlock_bh(&idev->lock);
1998 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001999 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002000 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002001 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2002 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002005 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 pkt_dev->saddr_min = 0;
2007 pkt_dev->saddr_max = 0;
2008 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002009
2010 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002013 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (in_dev) {
2015 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002016 pkt_dev->saddr_min =
2017 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 pkt_dev->saddr_max = pkt_dev->saddr_min;
2019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002022 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2024 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2025 }
2026
2027 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2028 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2029 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002030 /* Initialize current values. */
2031 pkt_dev->cur_dst_mac_offset = 0;
2032 pkt_dev->cur_src_mac_offset = 0;
2033 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2034 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2035 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2036 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 pkt_dev->nflows = 0;
2038}
2039
2040static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2041{
2042 __u64 start;
2043 __u64 now;
2044
2045 start = now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002047 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002048 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002049 schedule_timeout_interruptible(1);
2050 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (!pkt_dev->running)
2052 return;
2053 if (need_resched())
2054 schedule();
2055 }
2056
2057 now = getCurUs();
2058 }
2059
2060 pkt_dev->idle_acc += now - start;
2061}
2062
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002063static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2064{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002065 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002066 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2067 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2068 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2069}
2070
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002071static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2072{
2073
2074 if (pkt_dev->flows[flow].flags & F_INIT)
2075 return 1;
2076 else
2077 return 0;
2078}
2079
2080static inline int f_pick(struct pktgen_dev *pkt_dev)
2081{
2082 int flow = pkt_dev->curfl;
2083
2084 if (pkt_dev->flags & F_FLOW_SEQ) {
2085 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2086 /* reset time */
2087 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002088 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002089 pkt_dev->curfl += 1;
2090 if (pkt_dev->curfl >= pkt_dev->cflows)
2091 pkt_dev->curfl = 0; /*reset */
2092 }
2093 } else {
2094 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002095 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002096
Robert Olsson1211a642008-08-05 18:44:26 -07002097 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002098 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002099 pkt_dev->flows[flow].flags = 0;
2100 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002101 }
2102
2103 return pkt_dev->curfl;
2104}
2105
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002106
2107#ifdef CONFIG_XFRM
2108/* If there was already an IPSEC SA, we keep it as is, else
2109 * we go look for it ...
2110*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002111static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002112{
2113 struct xfrm_state *x = pkt_dev->flows[flow].x;
2114 if (!x) {
2115 /*slow path: we dont already have xfrm_state*/
2116 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2117 (xfrm_address_t *)&pkt_dev->cur_saddr,
2118 AF_INET,
2119 pkt_dev->ipsmode,
2120 pkt_dev->ipsproto, 0);
2121 if (x) {
2122 pkt_dev->flows[flow].x = x;
2123 set_pkt_overhead(pkt_dev);
2124 pkt_dev->pkt_overhead+=x->props.header_len;
2125 }
2126
2127 }
2128}
2129#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002130static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2131{
2132 if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2133 __u16 t;
2134 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2135 t = random32() %
2136 (pkt_dev->queue_map_max -
2137 pkt_dev->queue_map_min + 1)
2138 + pkt_dev->queue_map_min;
2139 } else {
2140 t = pkt_dev->cur_queue_map + 1;
2141 if (t > pkt_dev->queue_map_max)
2142 t = pkt_dev->queue_map_min;
2143 }
2144 pkt_dev->cur_queue_map = t;
2145 }
2146}
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/* Increment/randomize headers according to flags and current values
2149 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2150 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002151static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2152{
2153 __u32 imn;
2154 __u32 imx;
2155 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002157 if (pkt_dev->cflows)
2158 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002161 if (pkt_dev->src_mac_count > 1) {
2162 __u32 mc;
2163 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Luiz Capitulino222f1802006-03-20 22:16:13 -08002165 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002166 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002167 else {
2168 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002169 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002170 pkt_dev->src_mac_count)
2171 pkt_dev->cur_src_mac_offset = 0;
2172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Luiz Capitulino222f1802006-03-20 22:16:13 -08002174 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2175 pkt_dev->hh[11] = tmp;
2176 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2177 pkt_dev->hh[10] = tmp;
2178 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2179 pkt_dev->hh[9] = tmp;
2180 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2181 pkt_dev->hh[8] = tmp;
2182 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2183 pkt_dev->hh[7] = tmp;
2184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Luiz Capitulino222f1802006-03-20 22:16:13 -08002186 /* Deal with Destination MAC */
2187 if (pkt_dev->dst_mac_count > 1) {
2188 __u32 mc;
2189 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Luiz Capitulino222f1802006-03-20 22:16:13 -08002191 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002192 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Luiz Capitulino222f1802006-03-20 22:16:13 -08002194 else {
2195 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002196 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002197 pkt_dev->dst_mac_count) {
2198 pkt_dev->cur_dst_mac_offset = 0;
2199 }
2200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Luiz Capitulino222f1802006-03-20 22:16:13 -08002202 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2203 pkt_dev->hh[5] = tmp;
2204 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2205 pkt_dev->hh[4] = tmp;
2206 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2207 pkt_dev->hh[3] = tmp;
2208 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2209 pkt_dev->hh[2] = tmp;
2210 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2211 pkt_dev->hh[1] = tmp;
2212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002214 if (pkt_dev->flags & F_MPLS_RND) {
2215 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002216 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002217 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2218 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002219 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002220 htonl(0x000fffff));
2221 }
2222
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002223 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002224 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002225 }
2226
2227 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002228 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002229 }
2230
Luiz Capitulino222f1802006-03-20 22:16:13 -08002231 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2232 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002233 pkt_dev->cur_udp_src = random32() %
2234 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2235 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Luiz Capitulino222f1802006-03-20 22:16:13 -08002237 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 pkt_dev->cur_udp_src++;
2239 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2240 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002241 }
2242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Luiz Capitulino222f1802006-03-20 22:16:13 -08002244 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2245 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002246 pkt_dev->cur_udp_dst = random32() %
2247 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2248 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002249 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002251 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002253 }
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 if (!(pkt_dev->flags & F_IPV6)) {
2257
Luiz Capitulino222f1802006-03-20 22:16:13 -08002258 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2259 ntohl(pkt_dev->
2260 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002262 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002263 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 else {
2265 t = ntohl(pkt_dev->cur_saddr);
2266 t++;
2267 if (t > imx) {
2268 t = imn;
2269 }
2270 }
2271 pkt_dev->cur_saddr = htonl(t);
2272 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002273
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002274 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2276 } else {
Al Viro252e3342006-11-14 20:48:11 -08002277 imn = ntohl(pkt_dev->daddr_min);
2278 imx = ntohl(pkt_dev->daddr_max);
2279 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002281 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (pkt_dev->flags & F_IPDST_RND) {
2283
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002284 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002285 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Joe Perches21cf2252007-12-16 13:44:00 -08002287 while (ipv4_is_loopback(s) ||
2288 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002289 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002290 ipv4_is_zeronet(s) ||
2291 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002292 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002293 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
Al Viro252e3342006-11-14 20:48:11 -08002295 pkt_dev->cur_daddr = s;
2296 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 t = ntohl(pkt_dev->cur_daddr);
2298 t++;
2299 if (t > imx) {
2300 t = imn;
2301 }
2302 pkt_dev->cur_daddr = htonl(t);
2303 }
2304 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002305 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002306 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307 pkt_dev->flows[flow].cur_daddr =
2308 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002309#ifdef CONFIG_XFRM
2310 if (pkt_dev->flags & F_IPSEC_ON)
2311 get_ipsec_sa(pkt_dev, flow);
2312#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 pkt_dev->nflows++;
2314 }
2315 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 } else { /* IPV6 * */
2317
2318 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2319 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2320 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2321 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 else {
2323 int i;
2324
2325 /* Only random destinations yet */
2326
Luiz Capitulino222f1802006-03-20 22:16:13 -08002327 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002329 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002330 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2331 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
Luiz Capitulino222f1802006-03-20 22:16:13 -08002336 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2337 __u32 t;
2338 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002339 t = random32() %
2340 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2341 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002342 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002344 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002346 }
2347 pkt_dev->cur_pkt_size = t;
2348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002350 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 pkt_dev->flows[flow].count++;
2353}
2354
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002355
2356#ifdef CONFIG_XFRM
2357static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2358{
2359 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2360 int err = 0;
2361 struct iphdr *iph;
2362
2363 if (!x)
2364 return 0;
2365 /* XXX: we dont support tunnel mode for now until
2366 * we resolve the dst issue */
2367 if (x->props.mode != XFRM_MODE_TRANSPORT)
2368 return 0;
2369
2370 spin_lock(&x->lock);
2371 iph = ip_hdr(skb);
2372
Herbert Xu13996372007-10-17 21:35:51 -07002373 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002374 if (err)
2375 goto error;
2376 err = x->type->output(x, skb);
2377 if (err)
2378 goto error;
2379
2380 x->curlft.bytes +=skb->len;
2381 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002382error:
2383 spin_unlock(&x->lock);
2384 return err;
2385}
2386
2387static inline void free_SAs(struct pktgen_dev *pkt_dev)
2388{
2389 if (pkt_dev->cflows) {
2390 /* let go of the SAs if we have them */
2391 int i = 0;
2392 for (; i < pkt_dev->nflows; i++){
2393 struct xfrm_state *x = pkt_dev->flows[i].x;
2394 if (x) {
2395 xfrm_state_put(x);
2396 pkt_dev->flows[i].x = NULL;
2397 }
2398 }
2399 }
2400}
2401
2402static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2403 struct sk_buff *skb, __be16 protocol)
2404{
2405 if (pkt_dev->flags & F_IPSEC_ON) {
2406 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2407 int nhead = 0;
2408 if (x) {
2409 int ret;
2410 __u8 *eth;
2411 nhead = x->props.header_len - skb_headroom(skb);
2412 if (nhead >0) {
2413 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2414 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002415 printk(KERN_ERR "Error expanding "
2416 "ipsec packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002417 return 0;
2418 }
2419 }
2420
2421 /* ipsec is not expecting ll header */
2422 skb_pull(skb, ETH_HLEN);
2423 ret = pktgen_output_ipsec(skb, pkt_dev);
2424 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002425 printk(KERN_ERR "Error creating ipsec "
2426 "packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002427 kfree_skb(skb);
2428 return 0;
2429 }
2430 /* restore ll */
2431 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2432 memcpy(eth, pkt_dev->hh, 12);
2433 *(u16 *) & eth[12] = protocol;
2434 }
2435 }
2436 return 1;
2437}
2438#endif
2439
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002440static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2441{
2442 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002443 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002444 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2445 }
2446 mpls--;
2447 *mpls |= MPLS_STACK_BOTTOM;
2448}
2449
Al Viro0f37c602006-11-03 03:49:56 -08002450static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2451 unsigned int prio)
2452{
2453 return htons(id | (cfi << 12) | (prio << 13));
2454}
2455
Luiz Capitulino222f1802006-03-20 22:16:13 -08002456static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2457 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 struct sk_buff *skb = NULL;
2460 __u8 *eth;
2461 struct udphdr *udph;
2462 int datalen, iplen;
2463 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002464 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002465 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002466 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002467 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2468 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2469 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2470 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002471 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002472
2473 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002474 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002475
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002476 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002477 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002478
Robert Olsson64053be2005-06-26 15:27:10 -07002479 /* Update any of the values, used when we're incrementing various
2480 * fields.
2481 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002482 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002483 mod_cur_headers(pkt_dev);
2484
David S. Miller7ac54592006-01-18 14:19:10 -08002485 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002486 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002487 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (!skb) {
2489 sprintf(pkt_dev->result, "No memory");
2490 return NULL;
2491 }
2492
David S. Miller7ac54592006-01-18 14:19:10 -08002493 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 /* Reserve for ethernet and IP header */
2496 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002497 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2498 if (pkt_dev->nr_labels)
2499 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002500
2501 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002502 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002503 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002504 *svlan_tci = build_tci(pkt_dev->svlan_id,
2505 pkt_dev->svlan_cfi,
2506 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002507 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002508 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002509 }
2510 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002511 *vlan_tci = build_tci(pkt_dev->vlan_id,
2512 pkt_dev->vlan_cfi,
2513 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002514 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002515 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002516 }
2517
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002518 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002519 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002520 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002521 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002522 iph = ip_hdr(skb);
2523 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002526 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002528 /* Eth + IPh + UDPh + mpls */
2529 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002530 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002531 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 udph->source = htons(pkt_dev->cur_udp_src);
2535 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002536 udph->len = htons(datalen + 8); /* DATA + udphdr */
2537 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 iph->ihl = 5;
2540 iph->version = 4;
2541 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002542 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002543 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 iph->saddr = pkt_dev->cur_saddr;
2545 iph->daddr = pkt_dev->cur_daddr;
2546 iph->frag_off = 0;
2547 iplen = 20 + 8 + datalen;
2548 iph->tot_len = htons(iplen);
2549 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002550 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002551 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002552 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002553 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 skb->dev = odev;
2555 skb->pkt_type = PACKET_HOST;
2556
Luiz Capitulino222f1802006-03-20 22:16:13 -08002557 if (pkt_dev->nfrags <= 0)
2558 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 else {
2560 int frags = pkt_dev->nfrags;
2561 int i;
2562
Luiz Capitulino222f1802006-03-20 22:16:13 -08002563 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (frags > MAX_SKB_FRAGS)
2566 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002567 if (datalen > frags * PAGE_SIZE) {
2568 skb_put(skb, datalen - frags * PAGE_SIZE);
2569 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571
2572 i = 0;
2573 while (datalen > 0) {
2574 struct page *page = alloc_pages(GFP_KERNEL, 0);
2575 skb_shinfo(skb)->frags[i].page = page;
2576 skb_shinfo(skb)->frags[i].page_offset = 0;
2577 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002578 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 datalen -= skb_shinfo(skb)->frags[i].size;
2580 skb->len += skb_shinfo(skb)->frags[i].size;
2581 skb->data_len += skb_shinfo(skb)->frags[i].size;
2582 i++;
2583 skb_shinfo(skb)->nr_frags = i;
2584 }
2585
2586 while (i < frags) {
2587 int rem;
2588
2589 if (i == 0)
2590 break;
2591
2592 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2593 if (rem == 0)
2594 break;
2595
2596 skb_shinfo(skb)->frags[i - 1].size -= rem;
2597
Luiz Capitulino222f1802006-03-20 22:16:13 -08002598 skb_shinfo(skb)->frags[i] =
2599 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002601 skb_shinfo(skb)->frags[i].page =
2602 skb_shinfo(skb)->frags[i - 1].page;
2603 skb_shinfo(skb)->frags[i].page_offset +=
2604 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 skb_shinfo(skb)->frags[i].size = rem;
2606 i++;
2607 skb_shinfo(skb)->nr_frags = i;
2608 }
2609 }
2610
Luiz Capitulino222f1802006-03-20 22:16:13 -08002611 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Luiz Capitulino222f1802006-03-20 22:16:13 -08002613 if (pgh) {
2614 struct timeval timestamp;
2615
2616 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2617 pgh->seq_num = htonl(pkt_dev->seq_num);
2618
2619 do_gettimeofday(&timestamp);
2620 pgh->tv_sec = htonl(timestamp.tv_sec);
2621 pgh->tv_usec = htonl(timestamp.tv_usec);
2622 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002623
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002624#ifdef CONFIG_XFRM
2625 if (!process_ipsec(pkt_dev, skb, protocol))
2626 return NULL;
2627#endif
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return skb;
2630}
2631
2632/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002633 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2635 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002636 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 * Should be candidate for net/ipv4/utils.c
2638 * --ro
2639 */
2640
Luiz Capitulino222f1802006-03-20 22:16:13 -08002641static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
2643 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002644 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 unsigned long u;
2646 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002647 unsigned int prefixlen = 0;
2648 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002649 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002650 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Luiz Capitulino222f1802006-03-20 22:16:13 -08002652 for (i = 0; i < 16; i++)
2653 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 for (;;) {
2656 if (*s == ':') {
2657 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002658 if (s[1] == ':') { /* Found "::", skip to part 2 */
2659 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 len++;
2661 break;
2662 }
2663 s++;
2664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002666 u = simple_strtoul(s, &pos, 16);
2667 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002668 if (!i)
2669 return 0;
2670 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 /* the last 4 bytes may be written as IPv4 address */
2673
2674 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002675 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2676 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
2678 ip[prefixlen++] = (u >> 8);
2679 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002680 s += i;
2681 len += i;
2682 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return len;
2684 }
2685
2686/* part 2, after "::" */
2687 for (;;) {
2688 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002689 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 break;
2691 s++;
2692 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002693 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002695
2696 u = simple_strtol(s, &pos, 16);
2697 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002699 if (*s)
2700 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 break;
2702 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002703 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002705 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2706 sizeof(tmp));
2707 suffixlen += 4;
2708 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 break;
2710 }
2711 suffix[suffixlen++] = (u >> 8);
2712 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002713 s += i;
2714 len += i;
2715 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 break;
2717 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002718 for (i = 0; i < suffixlen; i++)
2719 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return len;
2721}
2722
Luiz Capitulino222f1802006-03-20 22:16:13 -08002723static char tohex(char hexdigit)
2724{
2725 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
Luiz Capitulino222f1802006-03-20 22:16:13 -08002728static int fmt_xlong(char *s, unsigned int i)
2729{
2730 char *bak = s;
2731 *s = tohex((i >> 12) & 0xf);
2732 if (s != bak || *s != '0')
2733 ++s;
2734 *s = tohex((i >> 8) & 0xf);
2735 if (s != bak || *s != '0')
2736 ++s;
2737 *s = tohex((i >> 4) & 0xf);
2738 if (s != bak || *s != '0')
2739 ++s;
2740 *s = tohex(i & 0xf);
2741 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
Luiz Capitulino222f1802006-03-20 22:16:13 -08002744static unsigned int fmt_ip6(char *s, const char ip[16])
2745{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 unsigned int len;
2747 unsigned int i;
2748 unsigned int temp;
2749 unsigned int compressing;
2750 int j;
2751
Luiz Capitulino222f1802006-03-20 22:16:13 -08002752 len = 0;
2753 compressing = 0;
2754 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002757 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2758 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2759 temp = strlen(s);
2760 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
2762#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002763 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2764 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (temp == 0) {
2766 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002767 compressing = 1;
2768 if (j == 0) {
2769 *s++ = ':';
2770 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
2772 }
2773 } else {
2774 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002775 compressing = 0;
2776 *s++ = ':';
2777 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002779 i = fmt_xlong(s, temp);
2780 len += i;
2781 s += i;
2782 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 *s++ = ':';
2784 ++len;
2785 }
2786 }
2787 }
2788 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002789 *s++ = ':';
2790 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002792 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return len;
2794}
2795
Luiz Capitulino222f1802006-03-20 22:16:13 -08002796static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2797 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
2799 struct sk_buff *skb = NULL;
2800 __u8 *eth;
2801 struct udphdr *udph;
2802 int datalen;
2803 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002804 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002805 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002806 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002807 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2808 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2809 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2810 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002811 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002812
2813 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002814 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002815
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002816 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002817 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002818
Robert Olsson64053be2005-06-26 15:27:10 -07002819 /* Update any of the values, used when we're incrementing various
2820 * fields.
2821 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002822 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002823 mod_cur_headers(pkt_dev);
2824
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002825 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002826 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (!skb) {
2828 sprintf(pkt_dev->result, "No memory");
2829 return NULL;
2830 }
2831
2832 skb_reserve(skb, 16);
2833
2834 /* Reserve for ethernet and IP header */
2835 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002836 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2837 if (pkt_dev->nr_labels)
2838 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002839
2840 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002841 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002842 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002843 *svlan_tci = build_tci(pkt_dev->svlan_id,
2844 pkt_dev->svlan_cfi,
2845 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002846 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002847 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002848 }
2849 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002850 *vlan_tci = build_tci(pkt_dev->vlan_id,
2851 pkt_dev->vlan_cfi,
2852 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002853 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002854 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002855 }
2856
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002857 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002858 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002859 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002860 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002861 iph = ipv6_hdr(skb);
2862 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002865 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002866
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002867 /* Eth + IPh + UDPh + mpls */
2868 datalen = pkt_dev->cur_pkt_size - 14 -
2869 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002870 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Luiz Capitulino222f1802006-03-20 22:16:13 -08002872 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 datalen = sizeof(struct pktgen_hdr);
2874 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002875 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2876 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878
2879 udph->source = htons(pkt_dev->cur_udp_src);
2880 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002881 udph->len = htons(datalen + sizeof(struct udphdr));
2882 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002884 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002886 if (pkt_dev->traffic_class) {
2887 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002888 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002889 }
2890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 iph->hop_limit = 32;
2892
2893 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2894 iph->nexthdr = IPPROTO_UDP;
2895
2896 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2897 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2898
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002899 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002900 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002901 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 skb->dev = odev;
2903 skb->pkt_type = PACKET_HOST;
2904
Luiz Capitulino222f1802006-03-20 22:16:13 -08002905 if (pkt_dev->nfrags <= 0)
2906 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 else {
2908 int frags = pkt_dev->nfrags;
2909 int i;
2910
Luiz Capitulino222f1802006-03-20 22:16:13 -08002911 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (frags > MAX_SKB_FRAGS)
2914 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002915 if (datalen > frags * PAGE_SIZE) {
2916 skb_put(skb, datalen - frags * PAGE_SIZE);
2917 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
2919
2920 i = 0;
2921 while (datalen > 0) {
2922 struct page *page = alloc_pages(GFP_KERNEL, 0);
2923 skb_shinfo(skb)->frags[i].page = page;
2924 skb_shinfo(skb)->frags[i].page_offset = 0;
2925 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002926 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 datalen -= skb_shinfo(skb)->frags[i].size;
2928 skb->len += skb_shinfo(skb)->frags[i].size;
2929 skb->data_len += skb_shinfo(skb)->frags[i].size;
2930 i++;
2931 skb_shinfo(skb)->nr_frags = i;
2932 }
2933
2934 while (i < frags) {
2935 int rem;
2936
2937 if (i == 0)
2938 break;
2939
2940 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2941 if (rem == 0)
2942 break;
2943
2944 skb_shinfo(skb)->frags[i - 1].size -= rem;
2945
Luiz Capitulino222f1802006-03-20 22:16:13 -08002946 skb_shinfo(skb)->frags[i] =
2947 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002949 skb_shinfo(skb)->frags[i].page =
2950 skb_shinfo(skb)->frags[i - 1].page;
2951 skb_shinfo(skb)->frags[i].page_offset +=
2952 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 skb_shinfo(skb)->frags[i].size = rem;
2954 i++;
2955 skb_shinfo(skb)->nr_frags = i;
2956 }
2957 }
2958
Luiz Capitulino222f1802006-03-20 22:16:13 -08002959 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002961 if (pgh) {
2962 struct timeval timestamp;
2963
2964 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2965 pgh->seq_num = htonl(pkt_dev->seq_num);
2966
2967 do_gettimeofday(&timestamp);
2968 pgh->tv_sec = htonl(timestamp.tv_sec);
2969 pgh->tv_usec = htonl(timestamp.tv_usec);
2970 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002971 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return skb;
2974}
2975
Luiz Capitulino222f1802006-03-20 22:16:13 -08002976static inline struct sk_buff *fill_packet(struct net_device *odev,
2977 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002979 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return fill_packet_ipv6(odev, pkt_dev);
2981 else
2982 return fill_packet_ipv4(odev, pkt_dev);
2983}
2984
Luiz Capitulino222f1802006-03-20 22:16:13 -08002985static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002987 pkt_dev->seq_num = 1;
2988 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002990 pkt_dev->tx_bytes = 0;
2991 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
2993
2994/* Set up structure for sending pkts, clear counters */
2995
2996static void pktgen_run(struct pktgen_thread *t)
2997{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002998 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 int started = 0;
3000
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003001 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003004 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
3006 /*
3007 * setup odev and create initial packet.
3008 */
3009 pktgen_setup_inject(pkt_dev);
3010
Luiz Capitulino222f1802006-03-20 22:16:13 -08003011 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003013 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 pkt_dev->skb = NULL;
3015 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003016 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 pkt_dev->next_tx_ns = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003018 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 strcpy(pkt_dev->result, "Starting");
3021 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003022 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 strcpy(pkt_dev->result, "Error starting");
3024 }
3025 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003026 if (started)
3027 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
3030static void pktgen_stop_all_threads_ifs(void)
3031{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003032 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003034 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003036 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003037
3038 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003039 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003040
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003041 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
Luiz Capitulino222f1802006-03-20 22:16:13 -08003044static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003046 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003047 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003049 list_for_each_entry(pkt_dev, &t->if_list, list)
3050 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 res = 1;
3052 break;
3053 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003054 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
3056
Luiz Capitulino222f1802006-03-20 22:16:13 -08003057static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003059 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Luiz Capitulino222f1802006-03-20 22:16:13 -08003061 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Luiz Capitulino222f1802006-03-20 22:16:13 -08003063 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Luiz Capitulino222f1802006-03-20 22:16:13 -08003065 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067 if (signal_pending(current))
3068 goto signal;
3069 if_lock(t);
3070 }
3071 if_unlock(t);
3072 return 1;
3073signal:
3074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075}
3076
3077static int pktgen_wait_all_threads_run(void)
3078{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003079 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003081
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003082 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003083
3084 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003086 if (sig == 0)
3087 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003089
3090 if (sig == 0)
3091 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003093
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003094 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return sig;
3096}
3097
3098static void pktgen_run_all_threads(void)
3099{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003100 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003102 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003104 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003106 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003108
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003109 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Luiz Capitulino222f1802006-03-20 22:16:13 -08003111 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 pktgen_wait_all_threads_run();
3114}
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3117{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003118 __u64 total_us, bps, mbps, pps, idle;
3119 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Luiz Capitulino222f1802006-03-20 22:16:13 -08003121 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Luiz Capitulino222f1802006-03-20 22:16:13 -08003125 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3126 (unsigned long long)total_us,
3127 (unsigned long long)(total_us - idle),
3128 (unsigned long long)idle,
3129 (unsigned long long)pkt_dev->sofar,
3130 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Luiz Capitulino222f1802006-03-20 22:16:13 -08003132 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Luiz Capitulino222f1802006-03-20 22:16:13 -08003134 while ((total_us >> 32) != 0) {
3135 pps >>= 1;
3136 total_us >>= 1;
3137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Luiz Capitulino222f1802006-03-20 22:16:13 -08003139 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Luiz Capitulino222f1802006-03-20 22:16:13 -08003141 bps = pps * 8 * pkt_dev->cur_pkt_size;
3142
3143 mbps = bps;
3144 do_div(mbps, 1000000);
3145 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3146 (unsigned long long)pps,
3147 (unsigned long long)mbps,
3148 (unsigned long long)bps,
3149 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152/* Set stopped-at timer, remove from running list, do counters & statistics */
3153
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003157
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003159 printk(KERN_WARNING "pktgen: interface: %s is already "
3160 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003161 return -EINVAL;
3162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Luiz Capitulino222f1802006-03-20 22:16:13 -08003164 pkt_dev->stopped_at = getCurUs();
3165 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003167 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Luiz Capitulino222f1802006-03-20 22:16:13 -08003172static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003174 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 if_lock(t);
3177
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003178 list_for_each_entry(pkt_dev, &t->if_list, list) {
3179 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003180 continue;
3181 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003182 best = pkt_dev;
3183 else if (pkt_dev->next_tx_us < best->next_tx_us)
3184 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 }
3186 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003187 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
Luiz Capitulino222f1802006-03-20 22:16:13 -08003190static void pktgen_stop(struct pktgen_thread *t)
3191{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003192 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003194 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Luiz Capitulino222f1802006-03-20 22:16:13 -08003196 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003198 list_for_each_entry(pkt_dev, &t->if_list, list) {
3199 pktgen_stop_device(pkt_dev);
3200 if (pkt_dev->skb)
3201 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003202
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003203 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Luiz Capitulino222f1802006-03-20 22:16:13 -08003206 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003209/*
3210 * one of our devices needs to be removed - find it
3211 * and remove it
3212 */
3213static void pktgen_rem_one_if(struct pktgen_thread *t)
3214{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003215 struct list_head *q, *n;
3216 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003217
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003218 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003219
3220 if_lock(t);
3221
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003222 list_for_each_safe(q, n, &t->if_list) {
3223 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003224
Luiz Capitulino222f1802006-03-20 22:16:13 -08003225 if (!cur->removal_mark)
3226 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003227
3228 if (cur->skb)
3229 kfree_skb(cur->skb);
3230 cur->skb = NULL;
3231
3232 pktgen_remove_device(t, cur);
3233
3234 break;
3235 }
3236
3237 if_unlock(t);
3238}
3239
Luiz Capitulino222f1802006-03-20 22:16:13 -08003240static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003242 struct list_head *q, *n;
3243 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003244
3245 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003246
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003247 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003248 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003250 list_for_each_safe(q, n, &t->if_list) {
3251 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003252
3253 if (cur->skb)
3254 kfree_skb(cur->skb);
3255 cur->skb = NULL;
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 pktgen_remove_device(t, cur);
3258 }
3259
Luiz Capitulino222f1802006-03-20 22:16:13 -08003260 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Luiz Capitulino222f1802006-03-20 22:16:13 -08003263static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003265 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
David S. Milleree74baa2007-01-01 20:51:53 -08003267 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003269 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003271 list_del(&t->th_list);
3272
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003273 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
3276static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3277{
3278 struct net_device *odev = NULL;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003279 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 __u64 idle_start = 0;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003281 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 int ret;
3283
3284 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3287 u64 now;
3288
3289 now = getCurUs();
3290 if (now < pkt_dev->next_tx_us)
3291 spin(pkt_dev, pkt_dev->next_tx_us);
3292
3293 /* This is max DELAY, this has special meaning of
3294 * "never transmit"
3295 */
3296 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3297 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3298 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3299 goto out;
3300 }
3301 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003302
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003303 if (!pkt_dev->skb) {
3304 set_cur_queue_map(pkt_dev);
3305 queue_map = pkt_dev->cur_queue_map;
3306 } else {
3307 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3308 }
3309
3310 txq = netdev_get_tx_queue(odev, queue_map);
3311 if (netif_tx_queue_stopped(txq) ||
David S. Millerc3f26a22008-07-31 16:58:50 -07003312 netif_tx_queue_frozen(txq) ||
Robert Olsson378be2c2007-08-28 15:43:14 -07003313 need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (!netif_running(odev)) {
3317 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003318 if (pkt_dev->skb)
3319 kfree_skb(pkt_dev->skb);
3320 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 goto out;
3322 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003323 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003327
David S. Millerc3f26a22008-07-31 16:58:50 -07003328 if (netif_tx_queue_stopped(txq) ||
3329 netif_tx_queue_frozen(txq)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003330 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003332 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 }
3334 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003337 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3338 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003340 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 pkt_dev->skb = fill_packet(odev, pkt_dev);
3344 if (pkt_dev->skb == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003345 printk(KERN_ERR "pktgen: ERROR: couldn't "
3346 "allocate skb in fill_packet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003348 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 goto out;
3350 }
3351 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003352 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
3354 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003355
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003356 /* fill_packet() might have changed the queue */
3357 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3358 txq = netdev_get_tx_queue(odev, queue_map);
3359
3360 __netif_tx_lock_bh(txq);
David S. Millerc3f26a22008-07-31 16:58:50 -07003361 if (!netif_tx_queue_stopped(txq) &&
3362 !netif_tx_queue_frozen(txq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
3364 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003365 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3367 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003368 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 pkt_dev->sofar++;
3370 pkt_dev->seq_num++;
3371 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003372
3373 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 && (odev->features & NETIF_F_LLTX)) {
3375 cpu_relax();
3376 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003377 } else { /* Retry it next time */
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (debug && net_ratelimit())
3382 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 pkt_dev->errors++;
3385 pkt_dev->last_ok = 0;
3386 }
3387
3388 pkt_dev->next_tx_us = getCurUs();
3389 pkt_dev->next_tx_ns = 0;
3390
3391 pkt_dev->next_tx_us += pkt_dev->delay_us;
3392 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3393
3394 if (pkt_dev->next_tx_ns > 1000) {
3395 pkt_dev->next_tx_us++;
3396 pkt_dev->next_tx_ns -= 1000;
3397 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Luiz Capitulino222f1802006-03-20 22:16:13 -08003400 else { /* Retry it next time */
3401 pkt_dev->last_ok = 0;
3402 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003406 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 /* If pkt_dev->count is zero, then run forever */
3409 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3410 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3411 idle_start = getCurUs();
3412 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3413 if (signal_pending(current)) {
3414 break;
3415 }
3416 schedule();
3417 }
3418 pkt_dev->idle_acc += getCurUs() - idle_start;
3419 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 /* Done with this */
3422 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003423 if (pkt_dev->skb)
3424 kfree_skb(pkt_dev->skb);
3425 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003426 }
3427out:;
3428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003430/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 * Main loop of the thread goes here
3432 */
3433
David S. Milleree74baa2007-01-01 20:51:53 -08003434static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
3436 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003437 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003438 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
David S. Milleree74baa2007-01-01 20:51:53 -08003441 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
3443 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003444 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003446 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
David S. Milleree74baa2007-01-01 20:51:53 -08003448 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003449
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003450 set_freezable();
3451
David S. Milleree74baa2007-01-01 20:51:53 -08003452 while (!kthread_should_stop()) {
3453 pkt_dev = next_to_run(t);
3454
3455 if (!pkt_dev &&
3456 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3457 == 0) {
3458 prepare_to_wait(&(t->queue), &wait,
3459 TASK_INTERRUPTIBLE);
3460 schedule_timeout(HZ / 10);
3461 finish_wait(&(t->queue), &wait);
3462 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 __set_current_state(TASK_RUNNING);
3465
Robert Olssonb1639112007-08-28 15:46:58 -07003466 if (pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 pktgen_xmit(pkt_dev);
3468
Luiz Capitulino222f1802006-03-20 22:16:13 -08003469 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 pktgen_stop(t);
3471 t->control &= ~(T_STOP);
3472 }
3473
Luiz Capitulino222f1802006-03-20 22:16:13 -08003474 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 pktgen_run(t);
3476 t->control &= ~(T_RUN);
3477 }
3478
Luiz Capitulino222f1802006-03-20 22:16:13 -08003479 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003481 t->control &= ~(T_REMDEVALL);
3482 }
3483
Luiz Capitulino222f1802006-03-20 22:16:13 -08003484 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003485 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 t->control &= ~(T_REMDEV);
3487 }
3488
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003489 try_to_freeze();
3490
David S. Milleree74baa2007-01-01 20:51:53 -08003491 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003494 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003495 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003497 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003498 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003500 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003501 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003502
David S. Milleree74baa2007-01-01 20:51:53 -08003503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
3505
Luiz Capitulino222f1802006-03-20 22:16:13 -08003506static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3507 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003509 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003510 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003512 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003513 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003514 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003515 break;
3516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Luiz Capitulino222f1802006-03-20 22:16:13 -08003518 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003519 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003523/*
3524 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 */
3526
Luiz Capitulino222f1802006-03-20 22:16:13 -08003527static int add_dev_to_thread(struct pktgen_thread *t,
3528 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
3530 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Luiz Capitulino222f1802006-03-20 22:16:13 -08003532 if_lock(t);
3533
3534 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003535 printk(KERN_ERR "pktgen: ERROR: already assigned "
3536 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003537 rv = -EBUSY;
3538 goto out;
3539 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003540
3541 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003542 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 pkt_dev->running = 0;
3544
Luiz Capitulino222f1802006-03-20 22:16:13 -08003545out:
3546 if_unlock(t);
3547 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
3550/* Called under thread lock */
3551
Luiz Capitulino222f1802006-03-20 22:16:13 -08003552static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003554 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003555 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 /* We don't allow a device to be on several threads */
3558
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003559 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3560 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003561 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003562 return -EBUSY;
3563 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003564
3565 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3566 if (!pkt_dev)
3567 return -ENOMEM;
3568
Luiz Capitulino222f1802006-03-20 22:16:13 -08003569 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003570 if (pkt_dev->flows == NULL) {
3571 kfree(pkt_dev);
3572 return -ENOMEM;
3573 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003574 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003575
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003576 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003577 pkt_dev->min_pkt_size = ETH_ZLEN;
3578 pkt_dev->max_pkt_size = ETH_ZLEN;
3579 pkt_dev->nfrags = 0;
3580 pkt_dev->clone_skb = pg_clone_skb_d;
3581 pkt_dev->delay_us = pg_delay_d / 1000;
3582 pkt_dev->delay_ns = pg_delay_d % 1000;
3583 pkt_dev->count = pg_count_d;
3584 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003585 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003586 pkt_dev->udp_src_max = 9;
3587 pkt_dev->udp_dst_min = 9;
3588 pkt_dev->udp_dst_max = 9;
3589
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003590 pkt_dev->vlan_p = 0;
3591 pkt_dev->vlan_cfi = 0;
3592 pkt_dev->vlan_id = 0xffff;
3593 pkt_dev->svlan_p = 0;
3594 pkt_dev->svlan_cfi = 0;
3595 pkt_dev->svlan_id = 0xffff;
3596
Stephen Hemminger39df2322007-03-04 16:11:51 -08003597 err = pktgen_setup_dev(pkt_dev, ifname);
3598 if (err)
3599 goto out1;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003600
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003601 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3602 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003603 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003604 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003605 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003606 err = -EINVAL;
3607 goto out2;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003608 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003609#ifdef CONFIG_XFRM
3610 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3611 pkt_dev->ipsproto = IPPROTO_ESP;
3612#endif
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003613
3614 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003615out2:
3616 dev_put(pkt_dev->odev);
3617out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003618#ifdef CONFIG_XFRM
3619 free_SAs(pkt_dev);
3620#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -08003621 if (pkt_dev->flows)
3622 vfree(pkt_dev->flows);
3623 kfree(pkt_dev);
3624 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
David S. Milleree74baa2007-01-01 20:51:53 -08003627static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003629 struct pktgen_thread *t;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003630 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003631 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Luiz Capitulino222f1802006-03-20 22:16:13 -08003633 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3634 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003635 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3636 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003637 return -ENOMEM;
3638 }
3639
Luiz Capitulino222f1802006-03-20 22:16:13 -08003640 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003642
David S. Milleree74baa2007-01-01 20:51:53 -08003643 INIT_LIST_HEAD(&t->if_list);
3644
3645 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003646 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003647
3648 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3649 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003650 printk(KERN_ERR "pktgen: kernel_thread() failed "
3651 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003652 list_del(&t->th_list);
3653 kfree(t);
3654 return PTR_ERR(p);
3655 }
3656 kthread_bind(p, cpu);
3657 t->tsk = p;
3658
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003659 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3660 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003661 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003662 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003663 PG_PROC_DIR, t->tsk->comm);
3664 kthread_stop(p);
3665 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003666 kfree(t);
3667 return -EINVAL;
3668 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003669
David S. Milleree74baa2007-01-01 20:51:53 -08003670 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003671 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
3673 return 0;
3674}
3675
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003676/*
3677 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003679static void _rem_dev_from_if_list(struct pktgen_thread *t,
3680 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003682 struct list_head *q, *n;
3683 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003685 list_for_each_safe(q, n, &t->if_list) {
3686 p = list_entry(q, struct pktgen_dev, list);
3687 if (p == pkt_dev)
3688 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 }
3690}
3691
Luiz Capitulino222f1802006-03-20 22:16:13 -08003692static int pktgen_remove_device(struct pktgen_thread *t,
3693 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
3695
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003696 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Luiz Capitulino222f1802006-03-20 22:16:13 -08003698 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003699 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3700 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003701 pktgen_stop_device(pkt_dev);
3702 }
3703
3704 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
3706 if (pkt_dev->odev) {
3707 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003708 pkt_dev->odev = NULL;
3709 }
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 /* And update the thread if_list */
3712
3713 _rem_dev_from_if_list(t, pkt_dev);
3714
Stephen Hemminger39df2322007-03-04 16:11:51 -08003715 if (pkt_dev->entry)
3716 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003718#ifdef CONFIG_XFRM
3719 free_SAs(pkt_dev);
3720#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 if (pkt_dev->flows)
3722 vfree(pkt_dev->flows);
3723 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
Luiz Capitulino222f1802006-03-20 22:16:13 -08003727static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
3729 int cpu;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003730 struct proc_dir_entry *pe;
3731
David S. Miller25a8b252007-07-30 16:11:48 -07003732 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003734 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003735 if (!pg_proc_dir)
3736 return -ENODEV;
3737 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Wang Chen25296d52008-02-28 14:11:49 -08003739 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003740 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003741 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3742 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003743 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003744 return -EINVAL;
3745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 /* Register us to receive netdevice events */
3748 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003749
John Hawkes670c02c2005-10-13 09:30:31 -07003750 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003751 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
David S. Milleree74baa2007-01-01 20:51:53 -08003753 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003754 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003755 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3756 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003757 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003758
3759 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003760 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3761 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003762 unregister_netdevice_notifier(&pktgen_notifier_block);
3763 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003764 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003765 return -ENODEV;
3766 }
3767
Luiz Capitulino222f1802006-03-20 22:16:13 -08003768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769}
3770
3771static void __exit pg_cleanup(void)
3772{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003773 struct pktgen_thread *t;
3774 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 wait_queue_head_t queue;
3776 init_waitqueue_head(&queue);
3777
Luiz Capitulino222f1802006-03-20 22:16:13 -08003778 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003780 list_for_each_safe(q, n, &pktgen_threads) {
3781 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003782 kthread_stop(t->tsk);
3783 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Luiz Capitulino222f1802006-03-20 22:16:13 -08003786 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 unregister_netdevice_notifier(&pktgen_notifier_block);
3788
Luiz Capitulino222f1802006-03-20 22:16:13 -08003789 /* Clean up proc file system */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003790 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003791 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794module_init(pg_init);
3795module_exit(pg_cleanup);
3796
3797MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3798MODULE_DESCRIPTION("Packet Generator tool");
3799MODULE_LICENSE("GPL");
3800module_param(pg_count_d, int, 0);
3801module_param(pg_delay_d, int, 0);
3802module_param(pg_clone_skb_d, int, 0);
3803module_param(debug, int, 0);