blob: eebccdbdbacafb1c913be9dc6ec51b57875afc46 [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 Hemmingerd50a6b562005-10-14 15:42:33 -0700151#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800153#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800154#include <linux/kthread.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200155#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#include <net/checksum.h>
157#include <net/ipv6.h>
158#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700159#ifdef CONFIG_XFRM
160#include <net/xfrm.h>
161#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#include <asm/byteorder.h>
163#include <linux/rcupdate.h>
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/* The buckets are exponential in 'width' */
174#define LAT_BUCKETS_MAX 32
175#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800176#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800177#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800180#define F_IPSRC_RND (1<<0) /* IP-Src Random */
181#define F_IPDST_RND (1<<1) /* IP-Dst Random */
182#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
183#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
184#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
185#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
186#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
187#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800188#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700189#define F_VID_RND (1<<9) /* Random VLAN ID */
190#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700191#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700192#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700193#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800196#define T_TERMINATE (1<<0)
197#define T_STOP (1<<1) /* Stop run */
198#define T_RUN (1<<2) /* Start run */
199#define T_REMDEVALL (1<<3) /* Remove all devs */
200#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* If lock -- can be removed after some work */
203#define if_lock(t) spin_lock(&(t->if_lock));
204#define if_unlock(t) spin_unlock(&(t->if_lock));
205
206/* Used to help with determining the pkts on receive */
207#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700208#define PG_PROC_DIR "pktgen"
209#define PGCTRL "pgctrl"
210static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212#define MAX_CFLOWS 65536
213
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700214#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
215#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
216
Luiz Capitulino222f1802006-03-20 22:16:13 -0800217struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800218 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800219 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700220#ifdef CONFIG_XFRM
221 struct xfrm_state *x;
222#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700223 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700226/* flow flag bits */
227#define F_INIT (1<<0) /* flow has been initialized */
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /*
231 * Try to keep frequent/infrequent used vars. separated.
232 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800233 struct proc_dir_entry *entry; /* proc file */
234 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800235 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Luiz Capitulino222f1802006-03-20 22:16:13 -0800237 int running; /* if this changes to false, the test will stop */
238
239 /* If min != max, then we will either do a linear iteration, or
240 * we will do a random selection from within the range.
241 */
242 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800243 int removal_mark; /* non-zero => the device is marked for
244 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Luiz Capitulino222f1802006-03-20 22:16:13 -0800246 int min_pkt_size; /* = ETH_ZLEN; */
247 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700248 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800249 int nfrags;
250 __u32 delay_us; /* Default delay */
251 __u32 delay_ns;
252 __u64 count; /* Default No packets to send */
253 __u64 sofar; /* How many pkts we've sent so far */
254 __u64 tx_bytes; /* How many bytes we've transmitted */
255 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Luiz Capitulino222f1802006-03-20 22:16:13 -0800257 /* runtime counters relating to clone_skb */
258 __u64 next_tx_us; /* timestamp of when to tx next */
259 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Luiz Capitulino222f1802006-03-20 22:16:13 -0800261 __u64 allocated_skbs;
262 __u32 clone_count;
263 int last_ok; /* Was last skb sent?
264 * Or a failed transmit of some sort? This will keep
265 * sequence numbers in order, for example.
266 */
267 __u64 started_at; /* micro-seconds */
268 __u64 stopped_at; /* micro-seconds */
269 __u64 idle_acc; /* micro-seconds */
270 __u32 seq_num;
271
272 int clone_skb; /* Use multiple SKBs during packet gen. If this number
273 * is greater than 1, then that many copies of the same
274 * packet will be sent before a new packet is allocated.
275 * For instance, if you want to send 1024 identical packets
276 * before creating a new packet, set clone_skb to 1024.
277 */
278
279 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
283
284 struct in6_addr in6_saddr;
285 struct in6_addr in6_daddr;
286 struct in6_addr cur_in6_daddr;
287 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800289 struct in6_addr min_in6_daddr;
290 struct in6_addr max_in6_daddr;
291 struct in6_addr min_in6_saddr;
292 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Luiz Capitulino222f1802006-03-20 22:16:13 -0800294 /* If we're doing ranges, random or incremental, then this
295 * defines the min/max for those ranges.
296 */
Al Viro252e3342006-11-14 20:48:11 -0800297 __be32 saddr_min; /* inclusive, source IP address */
298 __be32 saddr_max; /* exclusive, source IP address */
299 __be32 daddr_min; /* inclusive, dest IP address */
300 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Luiz Capitulino222f1802006-03-20 22:16:13 -0800302 __u16 udp_src_min; /* inclusive, source UDP port */
303 __u16 udp_src_max; /* exclusive, source UDP port */
304 __u16 udp_dst_min; /* inclusive, dest UDP port */
305 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700307 /* DSCP + ECN */
308 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
309 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
310
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800311 /* MPLS */
312 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
313 __be32 labels[MAX_MPLS_LABELS];
314
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700315 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
316 __u8 vlan_p;
317 __u8 vlan_cfi;
318 __u16 vlan_id; /* 0xffff means no vlan tag */
319
320 __u8 svlan_p;
321 __u8 svlan_cfi;
322 __u16 svlan_id; /* 0xffff means no svlan tag */
323
Luiz Capitulino222f1802006-03-20 22:16:13 -0800324 __u32 src_mac_count; /* How many MACs to iterate through */
325 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Luiz Capitulino222f1802006-03-20 22:16:13 -0800327 unsigned char dst_mac[ETH_ALEN];
328 unsigned char src_mac[ETH_ALEN];
329
330 __u32 cur_dst_mac_offset;
331 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800332 __be32 cur_saddr;
333 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800334 __u16 cur_udp_dst;
335 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700336 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800337 __u32 cur_pkt_size;
338
339 __u8 hh[14];
340 /* = {
341 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
342
343 We fill in SRC address later
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x08, 0x00
346 };
347 */
348 __u16 pad; /* pad out the hh struct to an even 16 bytes */
349
350 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
351 * are transmitting the same one multiple times
352 */
353 struct net_device *odev; /* The out-going device. Note that the device should
354 * have it's pg_info pointer pointing back to this
355 * device. This will be set when the user specifies
356 * the out-going device name (not when the inject is
357 * started as it used to do.)
358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800360 unsigned cflows; /* Concurrent flows (config) */
361 unsigned lflow; /* Flow length (config) */
362 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700363 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700364
365 u16 queue_map_min;
366 u16 queue_map_max;
367
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700368#ifdef CONFIG_XFRM
369 __u8 ipsmode; /* IPSEC mode (config) */
370 __u8 ipsproto; /* IPSEC type (config) */
371#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800372 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
375struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800376 __be32 pgh_magic;
377 __be32 seq_num;
378 __be32 tv_sec;
379 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380};
381
382struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800383 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800384 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800385 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800386 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800387 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Luiz Capitulino222f1802006-03-20 22:16:13 -0800389 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
390
391 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int cpu;
393
Luiz Capitulino222f1802006-03-20 22:16:13 -0800394 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
397#define REMOVE 1
398#define FIND 0
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800401static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800403 __u64 us = tv->tv_usec;
404 us += (__u64) tv->tv_sec * (__u64) 1000000;
405 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Ilpo Järvinen6db105d2008-01-12 03:25:00 -0800408static __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800410 struct timeval tv;
411 do_gettimeofday(&tv);
412 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/* old include end */
416
417static char version[] __initdata = VERSION;
418
Luiz Capitulino222f1802006-03-20 22:16:13 -0800419static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
420static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
421static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
422 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
424static void pktgen_run_all_threads(void);
425static void pktgen_stop_all_threads_ifs(void);
426static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800427static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800429
Luiz Capitulino222f1802006-03-20 22:16:13 -0800430static unsigned int scan_ip6(const char *s, char ip[16]);
431static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800434static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb972005-12-22 12:51:46 -0800435static int pg_delay_d;
436static int pg_clone_skb_d;
437static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Luiz Capitulino222fa072006-03-20 22:24:27 -0800439static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800440static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static struct notifier_block pktgen_notifier_block = {
443 .notifier_call = pktgen_device_event,
444};
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900447 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 *
449 */
450
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700451static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800452{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700453 seq_puts(seq, VERSION);
454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Luiz Capitulino222f1802006-03-20 22:16:13 -0800457static ssize_t pgctrl_write(struct file *file, const char __user * buf,
458 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700461 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Luiz Capitulino222f1802006-03-20 22:16:13 -0800463 if (!capable(CAP_NET_ADMIN)) {
464 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700468 if (count > sizeof(data))
469 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700472 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700473 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800474 }
475 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Luiz Capitulino222f1802006-03-20 22:16:13 -0800477 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 pktgen_stop_all_threads_ifs();
479
Luiz Capitulino222f1802006-03-20 22:16:13 -0800480 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 pktgen_run_all_threads();
482
Luiz Capitulino222f1802006-03-20 22:16:13 -0800483 else
David S. Miller25a8b252007-07-30 16:11:48 -0700484 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 err = count;
487
Luiz Capitulino222f1802006-03-20 22:16:13 -0800488out:
489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700492static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700494 return single_open(file, pgctrl_show, PDE(inode)->data);
495}
496
Arjan van de Ven9a321442007-02-12 00:55:35 -0800497static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498 .owner = THIS_MODULE,
499 .open = pgctrl_open,
500 .read = seq_read,
501 .llseek = seq_lseek,
502 .write = pgctrl_write,
503 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700504};
505
506static int pktgen_if_show(struct seq_file *seq, void *v)
507{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800508 struct pktgen_dev *pkt_dev = seq->private;
509 __u64 sa;
510 __u64 stopped;
511 __u64 now = getCurUs();
Joe Perches0795af52007-10-03 17:59:30 -0700512 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Luiz Capitulino222f1802006-03-20 22:16:13 -0800514 seq_printf(seq,
515 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
516 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
517 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Luiz Capitulino222f1802006-03-20 22:16:13 -0800519 seq_printf(seq,
520 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
521 pkt_dev->nfrags,
522 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800523 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Luiz Capitulino222f1802006-03-20 22:16:13 -0800525 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
526 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Robert Olsson45b270f2007-08-28 15:45:55 -0700528 seq_printf(seq,
529 " queue_map_min: %u queue_map_max: %u\n",
530 pkt_dev->queue_map_min,
531 pkt_dev->queue_map_max);
532
Luiz Capitulino222f1802006-03-20 22:16:13 -0800533 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800535 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
536 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
537 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
538 seq_printf(seq,
539 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
540 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Luiz Capitulino222f1802006-03-20 22:16:13 -0800542 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
543 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
544 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
545 seq_printf(seq,
546 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
547 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Luiz Capitulino222f1802006-03-20 22:16:13 -0800549 } else
550 seq_printf(seq,
551 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
552 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
553 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700555 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Joe Perches0795af52007-10-03 17:59:30 -0700557 seq_printf(seq, "%s ",
558 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
559 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Luiz Capitulino222f1802006-03-20 22:16:13 -0800561 seq_printf(seq, "dst_mac: ");
Joe Perches0795af52007-10-03 17:59:30 -0700562 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Luiz Capitulino222f1802006-03-20 22:16:13 -0800564 seq_printf(seq,
565 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
566 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
567 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Luiz Capitulino222f1802006-03-20 22:16:13 -0800569 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800570 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700571 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800573 if (pkt_dev->nr_labels) {
574 unsigned i;
575 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700576 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800577 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
578 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
579 }
580
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700581 if (pkt_dev->vlan_id != 0xffff) {
582 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
583 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
584 }
585
586 if (pkt_dev->svlan_id != 0xffff) {
587 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
588 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
589 }
590
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700591 if (pkt_dev->tos) {
592 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
593 }
594
595 if (pkt_dev->traffic_class) {
596 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
597 }
598
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800599 seq_printf(seq, " Flags: ");
600
Luiz Capitulino222f1802006-03-20 22:16:13 -0800601 if (pkt_dev->flags & F_IPV6)
602 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Luiz Capitulino222f1802006-03-20 22:16:13 -0800604 if (pkt_dev->flags & F_IPSRC_RND)
605 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Luiz Capitulino222f1802006-03-20 22:16:13 -0800607 if (pkt_dev->flags & F_IPDST_RND)
608 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Luiz Capitulino222f1802006-03-20 22:16:13 -0800610 if (pkt_dev->flags & F_TXSIZE_RND)
611 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Luiz Capitulino222f1802006-03-20 22:16:13 -0800613 if (pkt_dev->flags & F_UDPSRC_RND)
614 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Luiz Capitulino222f1802006-03-20 22:16:13 -0800616 if (pkt_dev->flags & F_UDPDST_RND)
617 seq_printf(seq, "UDPDST_RND ");
618
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800619 if (pkt_dev->flags & F_MPLS_RND)
620 seq_printf(seq, "MPLS_RND ");
621
Robert Olsson45b270f2007-08-28 15:45:55 -0700622 if (pkt_dev->flags & F_QUEUE_MAP_RND)
623 seq_printf(seq, "QUEUE_MAP_RND ");
624
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700625 if (pkt_dev->cflows) {
626 if (pkt_dev->flags & F_FLOW_SEQ)
627 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
628 else
629 seq_printf(seq, "FLOW_RND ");
630 }
631
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700632#ifdef CONFIG_XFRM
633 if (pkt_dev->flags & F_IPSEC_ON)
634 seq_printf(seq, "IPSEC ");
635#endif
636
Luiz Capitulino222f1802006-03-20 22:16:13 -0800637 if (pkt_dev->flags & F_MACSRC_RND)
638 seq_printf(seq, "MACSRC_RND ");
639
640 if (pkt_dev->flags & F_MACDST_RND)
641 seq_printf(seq, "MACDST_RND ");
642
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700643 if (pkt_dev->flags & F_VID_RND)
644 seq_printf(seq, "VID_RND ");
645
646 if (pkt_dev->flags & F_SVID_RND)
647 seq_printf(seq, "SVID_RND ");
648
Luiz Capitulino222f1802006-03-20 22:16:13 -0800649 seq_puts(seq, "\n");
650
651 sa = pkt_dev->started_at;
652 stopped = pkt_dev->stopped_at;
653 if (pkt_dev->running)
654 stopped = now; /* not really stopped, more like last-running-at */
655
656 seq_printf(seq,
657 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
658 (unsigned long long)pkt_dev->sofar,
659 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
660 (unsigned long long)stopped,
661 (unsigned long long)pkt_dev->idle_acc);
662
663 seq_printf(seq,
664 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700665 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
666 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Luiz Capitulino222f1802006-03-20 22:16:13 -0800668 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800670 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
671 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
672 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
673 } else
674 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700675 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Luiz Capitulino222f1802006-03-20 22:16:13 -0800677 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700678 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Robert Olsson45b270f2007-08-28 15:45:55 -0700680 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
681
Luiz Capitulino222f1802006-03-20 22:16:13 -0800682 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800685 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800687 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800692
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700693static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800694{
695 int i = 0;
696 *num = 0;
697
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700698 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800699 char c;
700 *num <<= 4;
701 if (get_user(c, &user_buffer[i]))
702 return -EFAULT;
703 if ((c >= '0') && (c <= '9'))
704 *num |= c - '0';
705 else if ((c >= 'a') && (c <= 'f'))
706 *num |= c - 'a' + 10;
707 else if ((c >= 'A') && (c <= 'F'))
708 *num |= c - 'A' + 10;
709 else
710 break;
711 }
712 return i;
713}
714
Luiz Capitulino222f1802006-03-20 22:16:13 -0800715static int count_trail_chars(const char __user * user_buffer,
716 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 int i;
719
720 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800721 char c;
722 if (get_user(c, &user_buffer[i]))
723 return -EFAULT;
724 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 case '\"':
726 case '\n':
727 case '\r':
728 case '\t':
729 case ' ':
730 case '=':
731 break;
732 default:
733 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736done:
737 return i;
738}
739
Luiz Capitulino222f1802006-03-20 22:16:13 -0800740static unsigned long num_arg(const char __user * user_buffer,
741 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 int i = 0;
744 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800745
746 for (; i < maxlen; i++) {
747 char c;
748 if (get_user(c, &user_buffer[i]))
749 return -EFAULT;
750 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800752 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 } else
754 break;
755 }
756 return i;
757}
758
Luiz Capitulino222f1802006-03-20 22:16:13 -0800759static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 int i = 0;
762
Luiz Capitulino222f1802006-03-20 22:16:13 -0800763 for (; i < maxlen; i++) {
764 char c;
765 if (get_user(c, &user_buffer[i]))
766 return -EFAULT;
767 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 case '\"':
769 case '\n':
770 case '\r':
771 case '\t':
772 case ' ':
773 goto done_str;
774 break;
775 default:
776 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return i;
781}
782
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800783static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
784{
785 unsigned n = 0;
786 char c;
787 ssize_t i = 0;
788 int len;
789
790 pkt_dev->nr_labels = 0;
791 do {
792 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700793 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800794 if (len <= 0)
795 return len;
796 pkt_dev->labels[n] = htonl(tmp);
797 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
798 pkt_dev->flags |= F_MPLS_RND;
799 i += len;
800 if (get_user(c, &buffer[i]))
801 return -EFAULT;
802 i++;
803 n++;
804 if (n >= MAX_MPLS_LABELS)
805 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700806 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800807
808 pkt_dev->nr_labels = n;
809 return i;
810}
811
Luiz Capitulino222f1802006-03-20 22:16:13 -0800812static ssize_t pktgen_if_write(struct file *file,
813 const char __user * user_buffer, size_t count,
814 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800816 struct seq_file *seq = (struct seq_file *)file->private_data;
817 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int i = 0, max, len;
819 char name[16], valstr[32];
820 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800821 char *pg_result = NULL;
822 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800824
825 pg_result = &(pkt_dev->result[0]);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700828 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -EINVAL;
830 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 max = count - i;
833 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800834 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700835 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800836 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800838 i += tmp;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /* Read variable name */
841
842 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800843 if (len < 0) {
844 return len;
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800847 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -EFAULT;
849 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800850
851 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853 if (len < 0)
854 return len;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 i += len;
857
858 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800859 char tb[count + 1];
860 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800862 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700863 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800864 (unsigned long)count, tb);
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 if (!strcmp(name, "min_pkt_size")) {
868 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800869 if (len < 0) {
870 return len;
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800873 if (value < 14 + 20 + 8)
874 value = 14 + 20 + 8;
875 if (value != pkt_dev->min_pkt_size) {
876 pkt_dev->min_pkt_size = value;
877 pkt_dev->cur_pkt_size = value;
878 }
879 sprintf(pg_result, "OK: min_pkt_size=%u",
880 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return count;
882 }
883
Luiz Capitulino222f1802006-03-20 22:16:13 -0800884 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886 if (len < 0) {
887 return len;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800890 if (value < 14 + 20 + 8)
891 value = 14 + 20 + 8;
892 if (value != pkt_dev->max_pkt_size) {
893 pkt_dev->max_pkt_size = value;
894 pkt_dev->cur_pkt_size = value;
895 }
896 sprintf(pg_result, "OK: max_pkt_size=%u",
897 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return count;
899 }
900
Luiz Capitulino222f1802006-03-20 22:16:13 -0800901 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (!strcmp(name, "pkt_size")) {
904 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800905 if (len < 0) {
906 return len;
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800909 if (value < 14 + 20 + 8)
910 value = 14 + 20 + 8;
911 if (value != pkt_dev->min_pkt_size) {
912 pkt_dev->min_pkt_size = value;
913 pkt_dev->max_pkt_size = value;
914 pkt_dev->cur_pkt_size = value;
915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
917 return count;
918 }
919
Luiz Capitulino222f1802006-03-20 22:16:13 -0800920 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800922 if (len < 0) {
923 return len;
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800926 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 sprintf(pg_result, "OK: debug=%u", debug);
928 return count;
929 }
930
Luiz Capitulino222f1802006-03-20 22:16:13 -0800931 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800933 if (len < 0) {
934 return len;
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 i += len;
937 pkt_dev->nfrags = value;
938 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
939 return count;
940 }
941 if (!strcmp(name, "delay")) {
942 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800943 if (len < 0) {
944 return len;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 i += len;
947 if (value == 0x7FFFFFFF) {
948 pkt_dev->delay_us = 0x7FFFFFFF;
949 pkt_dev->delay_ns = 0;
950 } else {
951 pkt_dev->delay_us = value / 1000;
952 pkt_dev->delay_ns = value % 1000;
953 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800954 sprintf(pg_result, "OK: delay=%u",
955 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return count;
957 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800958 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 if (len < 0) {
961 return len;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800964 if (value != pkt_dev->udp_src_min) {
965 pkt_dev->udp_src_min = value;
966 pkt_dev->cur_udp_src = value;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
969 return count;
970 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800971 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800973 if (len < 0) {
974 return len;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800977 if (value != pkt_dev->udp_dst_min) {
978 pkt_dev->udp_dst_min = value;
979 pkt_dev->cur_udp_dst = value;
980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
982 return count;
983 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800984 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800986 if (len < 0) {
987 return len;
988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800990 if (value != pkt_dev->udp_src_max) {
991 pkt_dev->udp_src_max = value;
992 pkt_dev->cur_udp_src = value;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
995 return count;
996 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800997 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800999 if (len < 0) {
1000 return len;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001003 if (value != pkt_dev->udp_dst_max) {
1004 pkt_dev->udp_dst_max = value;
1005 pkt_dev->cur_udp_dst = value;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1008 return count;
1009 }
1010 if (!strcmp(name, "clone_skb")) {
1011 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001012 if (len < 0) {
1013 return len;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001016 pkt_dev->clone_skb = value;
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1019 return count;
1020 }
1021 if (!strcmp(name, "count")) {
1022 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001023 if (len < 0) {
1024 return len;
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 i += len;
1027 pkt_dev->count = value;
1028 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return count;
1031 }
1032 if (!strcmp(name, "src_mac_count")) {
1033 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001034 if (len < 0) {
1035 return len;
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 i += len;
1038 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 pkt_dev->src_mac_count = value;
1040 pkt_dev->cur_src_mac_offset = 0;
1041 }
1042 sprintf(pg_result, "OK: src_mac_count=%d",
1043 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return count;
1045 }
1046 if (!strcmp(name, "dst_mac_count")) {
1047 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001048 if (len < 0) {
1049 return len;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 i += len;
1052 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001053 pkt_dev->dst_mac_count = value;
1054 pkt_dev->cur_dst_mac_offset = 0;
1055 }
1056 sprintf(pg_result, "OK: dst_mac_count=%d",
1057 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return count;
1059 }
1060 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001061 char f[32];
1062 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001064 if (len < 0) {
1065 return len;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (copy_from_user(f, &user_buffer[i], len))
1068 return -EFAULT;
1069 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001070 if (strcmp(f, "IPSRC_RND") == 0)
1071 pkt_dev->flags |= F_IPSRC_RND;
1072
1073 else if (strcmp(f, "!IPSRC_RND") == 0)
1074 pkt_dev->flags &= ~F_IPSRC_RND;
1075
1076 else if (strcmp(f, "TXSIZE_RND") == 0)
1077 pkt_dev->flags |= F_TXSIZE_RND;
1078
1079 else if (strcmp(f, "!TXSIZE_RND") == 0)
1080 pkt_dev->flags &= ~F_TXSIZE_RND;
1081
1082 else if (strcmp(f, "IPDST_RND") == 0)
1083 pkt_dev->flags |= F_IPDST_RND;
1084
1085 else if (strcmp(f, "!IPDST_RND") == 0)
1086 pkt_dev->flags &= ~F_IPDST_RND;
1087
1088 else if (strcmp(f, "UDPSRC_RND") == 0)
1089 pkt_dev->flags |= F_UDPSRC_RND;
1090
1091 else if (strcmp(f, "!UDPSRC_RND") == 0)
1092 pkt_dev->flags &= ~F_UDPSRC_RND;
1093
1094 else if (strcmp(f, "UDPDST_RND") == 0)
1095 pkt_dev->flags |= F_UDPDST_RND;
1096
1097 else if (strcmp(f, "!UDPDST_RND") == 0)
1098 pkt_dev->flags &= ~F_UDPDST_RND;
1099
1100 else if (strcmp(f, "MACSRC_RND") == 0)
1101 pkt_dev->flags |= F_MACSRC_RND;
1102
1103 else if (strcmp(f, "!MACSRC_RND") == 0)
1104 pkt_dev->flags &= ~F_MACSRC_RND;
1105
1106 else if (strcmp(f, "MACDST_RND") == 0)
1107 pkt_dev->flags |= F_MACDST_RND;
1108
1109 else if (strcmp(f, "!MACDST_RND") == 0)
1110 pkt_dev->flags &= ~F_MACDST_RND;
1111
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001112 else if (strcmp(f, "MPLS_RND") == 0)
1113 pkt_dev->flags |= F_MPLS_RND;
1114
1115 else if (strcmp(f, "!MPLS_RND") == 0)
1116 pkt_dev->flags &= ~F_MPLS_RND;
1117
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001118 else if (strcmp(f, "VID_RND") == 0)
1119 pkt_dev->flags |= F_VID_RND;
1120
1121 else if (strcmp(f, "!VID_RND") == 0)
1122 pkt_dev->flags &= ~F_VID_RND;
1123
1124 else if (strcmp(f, "SVID_RND") == 0)
1125 pkt_dev->flags |= F_SVID_RND;
1126
1127 else if (strcmp(f, "!SVID_RND") == 0)
1128 pkt_dev->flags &= ~F_SVID_RND;
1129
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001130 else if (strcmp(f, "FLOW_SEQ") == 0)
1131 pkt_dev->flags |= F_FLOW_SEQ;
1132
Robert Olsson45b270f2007-08-28 15:45:55 -07001133 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1134 pkt_dev->flags |= F_QUEUE_MAP_RND;
1135
1136 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1137 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001138#ifdef CONFIG_XFRM
1139 else if (strcmp(f, "IPSEC") == 0)
1140 pkt_dev->flags |= F_IPSEC_ON;
1141#endif
1142
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001143 else if (strcmp(f, "!IPV6") == 0)
1144 pkt_dev->flags &= ~F_IPV6;
1145
Luiz Capitulino222f1802006-03-20 22:16:13 -08001146 else {
1147 sprintf(pg_result,
1148 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1149 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001150 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001151 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001152 return count;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1155 return count;
1156 }
1157 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1158 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001159 if (len < 0) {
1160 return len;
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Luiz Capitulino222f1802006-03-20 22:16:13 -08001163 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001165 buf[len] = 0;
1166 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1167 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1168 strncpy(pkt_dev->dst_min, buf, len);
1169 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1170 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1171 }
1172 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001173 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001174 pkt_dev->dst_min);
1175 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1177 return count;
1178 }
1179 if (!strcmp(name, "dst_max")) {
1180 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001181 if (len < 0) {
1182 return len;
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Luiz Capitulino222f1802006-03-20 22:16:13 -08001185 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return -EFAULT;
1187
Luiz Capitulino222f1802006-03-20 22:16:13 -08001188 buf[len] = 0;
1189 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1190 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1191 strncpy(pkt_dev->dst_max, buf, len);
1192 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1193 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1194 }
1195 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001196 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001197 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 i += len;
1199 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1200 return count;
1201 }
1202 if (!strcmp(name, "dst6")) {
1203 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001204 if (len < 0)
1205 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 pkt_dev->flags |= F_IPV6;
1208
Luiz Capitulino222f1802006-03-20 22:16:13 -08001209 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001211 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001214 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1217
Luiz Capitulino222f1802006-03-20 22:16:13 -08001218 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001219 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Luiz Capitulino222f1802006-03-20 22:16:13 -08001221 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 sprintf(pg_result, "OK: dst6=%s", buf);
1223 return count;
1224 }
1225 if (!strcmp(name, "dst6_min")) {
1226 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001227 if (len < 0)
1228 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 pkt_dev->flags |= F_IPV6;
1231
Luiz Capitulino222f1802006-03-20 22:16:13 -08001232 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001234 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001237 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Luiz Capitulino222f1802006-03-20 22:16:13 -08001239 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1240 &pkt_dev->min_in6_daddr);
1241 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001242 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Luiz Capitulino222f1802006-03-20 22:16:13 -08001244 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 sprintf(pg_result, "OK: dst6_min=%s", buf);
1246 return count;
1247 }
1248 if (!strcmp(name, "dst6_max")) {
1249 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001250 if (len < 0)
1251 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 pkt_dev->flags |= F_IPV6;
1254
Luiz Capitulino222f1802006-03-20 22:16:13 -08001255 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001257 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001263 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Luiz Capitulino222f1802006-03-20 22:16:13 -08001265 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 sprintf(pg_result, "OK: dst6_max=%s", buf);
1267 return count;
1268 }
1269 if (!strcmp(name, "src6")) {
1270 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001271 if (len < 0)
1272 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 pkt_dev->flags |= F_IPV6;
1275
Luiz Capitulino222f1802006-03-20 22:16:13 -08001276 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001278 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001281 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1284
Luiz Capitulino222f1802006-03-20 22:16:13 -08001285 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001286 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001287
1288 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 sprintf(pg_result, "OK: src6=%s", buf);
1290 return count;
1291 }
1292 if (!strcmp(name, "src_min")) {
1293 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001294 if (len < 0) {
1295 return len;
1296 }
1297 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001299 buf[len] = 0;
1300 if (strcmp(buf, pkt_dev->src_min) != 0) {
1301 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1302 strncpy(pkt_dev->src_min, buf, len);
1303 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1304 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1305 }
1306 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001307 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 i += len;
1310 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1311 return count;
1312 }
1313 if (!strcmp(name, "src_max")) {
1314 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001315 if (len < 0) {
1316 return len;
1317 }
1318 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 buf[len] = 0;
1321 if (strcmp(buf, pkt_dev->src_max) != 0) {
1322 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1323 strncpy(pkt_dev->src_max, buf, len);
1324 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1325 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1326 }
1327 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001328 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 i += len;
1331 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1332 return count;
1333 }
1334 if (!strcmp(name, "dst_mac")) {
1335 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001336 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001338 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001341 if (len < 0) {
1342 return len;
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001345 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return -EFAULT;
1347 i += len;
1348
Luiz Capitulino222f1802006-03-20 22:16:13 -08001349 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (*v >= '0' && *v <= '9') {
1351 *m *= 16;
1352 *m += *v - '0';
1353 }
1354 if (*v >= 'A' && *v <= 'F') {
1355 *m *= 16;
1356 *m += *v - 'A' + 10;
1357 }
1358 if (*v >= 'a' && *v <= 'f') {
1359 *m *= 16;
1360 *m += *v - 'a' + 10;
1361 }
1362 if (*v == ':') {
1363 m++;
1364 *m = 0;
1365 }
1366 }
1367
1368 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001369 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1370 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 sprintf(pg_result, "OK: dstmac");
1373 return count;
1374 }
1375 if (!strcmp(name, "src_mac")) {
1376 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001377 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 unsigned char *m = pkt_dev->src_mac;
1379
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001380 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001383 if (len < 0) {
1384 return len;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001387 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return -EFAULT;
1389 i += len;
1390
Luiz Capitulino222f1802006-03-20 22:16:13 -08001391 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (*v >= '0' && *v <= '9') {
1393 *m *= 16;
1394 *m += *v - '0';
1395 }
1396 if (*v >= 'A' && *v <= 'F') {
1397 *m *= 16;
1398 *m += *v - 'A' + 10;
1399 }
1400 if (*v >= 'a' && *v <= 'f') {
1401 *m *= 16;
1402 *m += *v - 'a' + 10;
1403 }
1404 if (*v == ':') {
1405 m++;
1406 *m = 0;
1407 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001410 /* Set up Src MAC */
1411 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1412 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1413
Luiz Capitulino222f1802006-03-20 22:16:13 -08001414 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return count;
1416 }
1417
Luiz Capitulino222f1802006-03-20 22:16:13 -08001418 if (!strcmp(name, "clear_counters")) {
1419 pktgen_clear_counters(pkt_dev);
1420 sprintf(pg_result, "OK: Clearing counters.\n");
1421 return count;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 if (!strcmp(name, "flows")) {
1425 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001426 if (len < 0) {
1427 return len;
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 i += len;
1430 if (value > MAX_CFLOWS)
1431 value = MAX_CFLOWS;
1432
1433 pkt_dev->cflows = value;
1434 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1435 return count;
1436 }
1437
1438 if (!strcmp(name, "flowlen")) {
1439 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001440 if (len < 0) {
1441 return len;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 i += len;
1444 pkt_dev->lflow = value;
1445 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1446 return count;
1447 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448
Robert Olsson45b270f2007-08-28 15:45:55 -07001449 if (!strcmp(name, "queue_map_min")) {
1450 len = num_arg(&user_buffer[i], 5, &value);
1451 if (len < 0) {
1452 return len;
1453 }
1454 i += len;
1455 pkt_dev->queue_map_min = value;
1456 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1457 return count;
1458 }
1459
1460 if (!strcmp(name, "queue_map_max")) {
1461 len = num_arg(&user_buffer[i], 5, &value);
1462 if (len < 0) {
1463 return len;
1464 }
1465 i += len;
1466 pkt_dev->queue_map_max = value;
1467 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1468 return count;
1469 }
1470
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001471 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001472 unsigned n, cnt;
1473
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001474 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001475 if (len < 0)
1476 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001477 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001478 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001479 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001480 cnt += sprintf(pg_result + cnt,
1481 "%08x%s", ntohl(pkt_dev->labels[n]),
1482 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001483
1484 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1485 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1486 pkt_dev->svlan_id = 0xffff;
1487
1488 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001489 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001490 }
1491 return count;
1492 }
1493
1494 if (!strcmp(name, "vlan_id")) {
1495 len = num_arg(&user_buffer[i], 4, &value);
1496 if (len < 0) {
1497 return len;
1498 }
1499 i += len;
1500 if (value <= 4095) {
1501 pkt_dev->vlan_id = value; /* turn on VLAN */
1502
1503 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001504 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001505
1506 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001507 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001508
1509 pkt_dev->nr_labels = 0; /* turn off MPLS */
1510 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1511 } else {
1512 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1513 pkt_dev->svlan_id = 0xffff;
1514
1515 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001516 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001517 }
1518 return count;
1519 }
1520
1521 if (!strcmp(name, "vlan_p")) {
1522 len = num_arg(&user_buffer[i], 1, &value);
1523 if (len < 0) {
1524 return len;
1525 }
1526 i += len;
1527 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1528 pkt_dev->vlan_p = value;
1529 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1530 } else {
1531 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1532 }
1533 return count;
1534 }
1535
1536 if (!strcmp(name, "vlan_cfi")) {
1537 len = num_arg(&user_buffer[i], 1, &value);
1538 if (len < 0) {
1539 return len;
1540 }
1541 i += len;
1542 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1543 pkt_dev->vlan_cfi = value;
1544 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1545 } else {
1546 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1547 }
1548 return count;
1549 }
1550
1551 if (!strcmp(name, "svlan_id")) {
1552 len = num_arg(&user_buffer[i], 4, &value);
1553 if (len < 0) {
1554 return len;
1555 }
1556 i += len;
1557 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1558 pkt_dev->svlan_id = value; /* turn on SVLAN */
1559
1560 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001561 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001562
1563 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001564 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001565
1566 pkt_dev->nr_labels = 0; /* turn off MPLS */
1567 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1568 } else {
1569 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1570 pkt_dev->svlan_id = 0xffff;
1571
1572 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001573 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001574 }
1575 return count;
1576 }
1577
1578 if (!strcmp(name, "svlan_p")) {
1579 len = num_arg(&user_buffer[i], 1, &value);
1580 if (len < 0) {
1581 return len;
1582 }
1583 i += len;
1584 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1585 pkt_dev->svlan_p = value;
1586 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1587 } else {
1588 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1589 }
1590 return count;
1591 }
1592
1593 if (!strcmp(name, "svlan_cfi")) {
1594 len = num_arg(&user_buffer[i], 1, &value);
1595 if (len < 0) {
1596 return len;
1597 }
1598 i += len;
1599 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1600 pkt_dev->svlan_cfi = value;
1601 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1602 } else {
1603 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1604 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001605 return count;
1606 }
1607
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001608 if (!strcmp(name, "tos")) {
1609 __u32 tmp_value = 0;
1610 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1611 if (len < 0) {
1612 return len;
1613 }
1614 i += len;
1615 if (len == 2) {
1616 pkt_dev->tos = tmp_value;
1617 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1618 } else {
1619 sprintf(pg_result, "ERROR: tos must be 00-ff");
1620 }
1621 return count;
1622 }
1623
1624 if (!strcmp(name, "traffic_class")) {
1625 __u32 tmp_value = 0;
1626 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1627 if (len < 0) {
1628 return len;
1629 }
1630 i += len;
1631 if (len == 2) {
1632 pkt_dev->traffic_class = tmp_value;
1633 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1634 } else {
1635 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1636 }
1637 return count;
1638 }
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1641 return -EINVAL;
1642}
1643
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001644static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001646 return single_open(file, pktgen_if_show, PDE(inode)->data);
1647}
1648
Arjan van de Ven9a321442007-02-12 00:55:35 -08001649static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001650 .owner = THIS_MODULE,
1651 .open = pktgen_if_open,
1652 .read = seq_read,
1653 .llseek = seq_lseek,
1654 .write = pktgen_if_write,
1655 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001656};
1657
1658static int pktgen_thread_show(struct seq_file *seq, void *v)
1659{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001660 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001661 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001663 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Luiz Capitulino222f1802006-03-20 22:16:13 -08001665 seq_printf(seq, "Running: ");
1666
1667 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001668 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001669 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001670 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Luiz Capitulino222f1802006-03-20 22:16:13 -08001672 seq_printf(seq, "\nStopped: ");
1673
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001674 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001675 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001676 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001679 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001681 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Luiz Capitulino222f1802006-03-20 22:16:13 -08001683 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001688static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001689 const char __user * user_buffer,
1690 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001692 struct seq_file *seq = (struct seq_file *)file->private_data;
1693 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 int i = 0, max, len, ret;
1695 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001696 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001699 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return -EINVAL;
1701 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001704 len = count_trail_chars(&user_buffer[i], max);
1705 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001706 return len;
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 /* Read variable name */
1711
1712 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001713 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001714 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 memset(name, 0, sizeof(name));
1717 if (copy_from_user(name, &user_buffer[i], len))
1718 return -EFAULT;
1719 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001720
Luiz Capitulino222f1802006-03-20 22:16:13 -08001721 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001723 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001724 return len;
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 i += len;
1727
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001728 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001729 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1730 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Luiz Capitulino222f1802006-03-20 22:16:13 -08001732 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001733 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 ret = -EINVAL;
1735 goto out;
1736 }
1737
1738 pg_result = &(t->result[0]);
1739
Luiz Capitulino222f1802006-03-20 22:16:13 -08001740 if (!strcmp(name, "add_device")) {
1741 char f[32];
1742 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001744 if (len < 0) {
1745 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto out;
1747 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001748 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return -EFAULT;
1750 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001751 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001752 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001753 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001754 ret = count;
1755 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 goto out;
1757 }
1758
Luiz Capitulino222f1802006-03-20 22:16:13 -08001759 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001760 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001761 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001762 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001763 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001765 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto out;
1767 }
1768
Luiz Capitulino222f1802006-03-20 22:16:13 -08001769 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001770 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001771 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 goto out;
1773 }
1774
1775 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001776out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return ret;
1778}
1779
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001780static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001782 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Arjan van de Ven9a321442007-02-12 00:55:35 -08001785static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001786 .owner = THIS_MODULE,
1787 .open = pktgen_thread_open,
1788 .read = seq_read,
1789 .llseek = seq_lseek,
1790 .write = pktgen_thread_write,
1791 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001792};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001795static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 struct pktgen_thread *t;
1798 struct pktgen_dev *pkt_dev = NULL;
1799
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001800 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 pkt_dev = pktgen_find_dev(t, ifname);
1802 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 if (remove) {
1804 if_lock(t);
1805 pkt_dev->removal_mark = 1;
1806 t->control |= T_REMDEV;
1807 if_unlock(t);
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 break;
1810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001812 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001815/*
1816 * mark a device for removal
1817 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001818static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001821 const int max_tries = 10, msec_per_try = 125;
1822 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001823
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001824 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001825 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001826
Luiz Capitulino222f1802006-03-20 22:16:13 -08001827 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001828
1829 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001830 if (pkt_dev == NULL)
1831 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001832
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001833 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001834 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1835 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001836 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001837 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001838
1839 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001840 printk(KERN_ERR "pktgen_mark_device: timed out after "
1841 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001842 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001843 break;
1844 }
1845
1846 }
1847
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001848 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001849}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001850
Stephen Hemminger39df2322007-03-04 16:11:51 -08001851static void pktgen_change_name(struct net_device *dev)
1852{
1853 struct pktgen_thread *t;
1854
1855 list_for_each_entry(t, &pktgen_threads, th_list) {
1856 struct pktgen_dev *pkt_dev;
1857
1858 list_for_each_entry(pkt_dev, &t->if_list, list) {
1859 if (pkt_dev->odev != dev)
1860 continue;
1861
1862 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1863
1864 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1865 pg_proc_dir);
1866 if (!pkt_dev->entry)
1867 printk(KERN_ERR "pktgen: can't move proc "
1868 " entry for '%s'\n", dev->name);
1869 break;
1870 }
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Luiz Capitulino222f1802006-03-20 22:16:13 -08001874static int pktgen_device_event(struct notifier_block *unused,
1875 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001877 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001879 if (dev->nd_net != &init_net)
1880 return NOTIFY_DONE;
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /* It is OK that we do not hold the group lock right now,
1883 * as we run under the RTNL lock.
1884 */
1885
1886 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001887 case NETDEV_CHANGENAME:
1888 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001892 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 return NOTIFY_DONE;
1897}
1898
1899/* Associate pktgen_dev with a device. */
1900
Stephen Hemminger39df2322007-03-04 16:11:51 -08001901static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001904 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (pkt_dev->odev) {
1908 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001909 pkt_dev->odev = NULL;
1910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Eric W. Biederman881d9662007-09-17 11:56:21 -07001912 odev = dev_get_by_name(&init_net, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001914 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001915 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001919 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001920 err = -EINVAL;
1921 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001922 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001923 err = -ENETDOWN;
1924 } else {
1925 pkt_dev->odev = odev;
1926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001930 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
1933/* Read pkt_dev from the interface and set up internal pktgen_dev
1934 * structure to have the right information to create/send packets
1935 */
1936static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1937{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001938 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001939 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1940 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001941 sprintf(pkt_dev->result,
1942 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1943 return;
1944 }
1945
1946 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001948 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001949 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Luiz Capitulino222f1802006-03-20 22:16:13 -08001951 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001952 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Luiz Capitulino222f1802006-03-20 22:16:13 -08001954 /* Set up pkt size */
1955 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1956
1957 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001959 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 * gets exported
1961 */
1962
1963#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08001964 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct inet6_dev *idev;
1966
Luiz Capitulino222f1802006-03-20 22:16:13 -08001967 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1968 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 set = 1;
1970 break;
1971 }
1972
Luiz Capitulino222f1802006-03-20 22:16:13 -08001973 if (!set) {
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /*
1976 * Use linklevel address if unconfigured.
1977 *
1978 * use ipv6_get_lladdr if/when it's get exported
1979 */
1980
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001981 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1983 struct inet6_ifaddr *ifp;
1984
1985 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001986 for (ifp = idev->addr_list; ifp;
1987 ifp = ifp->if_next) {
1988 if (ifp->scope == IFA_LINK
1989 && !(ifp->
1990 flags & IFA_F_TENTATIVE)) {
1991 ipv6_addr_copy(&pkt_dev->
1992 cur_in6_saddr,
1993 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 err = 0;
1995 break;
1996 }
1997 }
1998 read_unlock_bh(&idev->lock);
1999 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002000 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002001 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002002 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2003 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002006 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 pkt_dev->saddr_min = 0;
2008 pkt_dev->saddr_max = 0;
2009 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002010
2011 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002014 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (in_dev) {
2016 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002017 pkt_dev->saddr_min =
2018 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 pkt_dev->saddr_max = pkt_dev->saddr_min;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002023 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2025 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2026 }
2027
2028 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2029 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2030 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002031 /* Initialize current values. */
2032 pkt_dev->cur_dst_mac_offset = 0;
2033 pkt_dev->cur_src_mac_offset = 0;
2034 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2035 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2036 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2037 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 pkt_dev->nflows = 0;
2039}
2040
2041static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2042{
2043 __u64 start;
2044 __u64 now;
2045
2046 start = now = getCurUs();
2047 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2048 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002049 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002050 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002051 schedule_timeout_interruptible(1);
2052 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (!pkt_dev->running)
2054 return;
2055 if (need_resched())
2056 schedule();
2057 }
2058
2059 now = getCurUs();
2060 }
2061
2062 pkt_dev->idle_acc += now - start;
2063}
2064
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002065static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2066{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002067 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002068 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2069 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2070 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2071}
2072
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002073static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2074{
2075
2076 if (pkt_dev->flows[flow].flags & F_INIT)
2077 return 1;
2078 else
2079 return 0;
2080}
2081
2082static inline int f_pick(struct pktgen_dev *pkt_dev)
2083{
2084 int flow = pkt_dev->curfl;
2085
2086 if (pkt_dev->flags & F_FLOW_SEQ) {
2087 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2088 /* reset time */
2089 pkt_dev->flows[flow].count = 0;
2090 pkt_dev->curfl += 1;
2091 if (pkt_dev->curfl >= pkt_dev->cflows)
2092 pkt_dev->curfl = 0; /*reset */
2093 }
2094 } else {
2095 flow = random32() % pkt_dev->cflows;
2096
2097 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2098 pkt_dev->flows[flow].count = 0;
2099 }
2100
2101 return pkt_dev->curfl;
2102}
2103
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002104
2105#ifdef CONFIG_XFRM
2106/* If there was already an IPSEC SA, we keep it as is, else
2107 * we go look for it ...
2108*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002109static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002110{
2111 struct xfrm_state *x = pkt_dev->flows[flow].x;
2112 if (!x) {
2113 /*slow path: we dont already have xfrm_state*/
2114 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2115 (xfrm_address_t *)&pkt_dev->cur_saddr,
2116 AF_INET,
2117 pkt_dev->ipsmode,
2118 pkt_dev->ipsproto, 0);
2119 if (x) {
2120 pkt_dev->flows[flow].x = x;
2121 set_pkt_overhead(pkt_dev);
2122 pkt_dev->pkt_overhead+=x->props.header_len;
2123 }
2124
2125 }
2126}
2127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128/* Increment/randomize headers according to flags and current values
2129 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2130 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002131static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2132{
2133 __u32 imn;
2134 __u32 imx;
2135 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002137 if (pkt_dev->cflows)
2138 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002141 if (pkt_dev->src_mac_count > 1) {
2142 __u32 mc;
2143 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Luiz Capitulino222f1802006-03-20 22:16:13 -08002145 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002146 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002147 else {
2148 mc = pkt_dev->cur_src_mac_offset++;
2149 if (pkt_dev->cur_src_mac_offset >
2150 pkt_dev->src_mac_count)
2151 pkt_dev->cur_src_mac_offset = 0;
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Luiz Capitulino222f1802006-03-20 22:16:13 -08002154 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2155 pkt_dev->hh[11] = tmp;
2156 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2157 pkt_dev->hh[10] = tmp;
2158 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2159 pkt_dev->hh[9] = tmp;
2160 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2161 pkt_dev->hh[8] = tmp;
2162 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2163 pkt_dev->hh[7] = tmp;
2164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Luiz Capitulino222f1802006-03-20 22:16:13 -08002166 /* Deal with Destination MAC */
2167 if (pkt_dev->dst_mac_count > 1) {
2168 __u32 mc;
2169 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Luiz Capitulino222f1802006-03-20 22:16:13 -08002171 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002172 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Luiz Capitulino222f1802006-03-20 22:16:13 -08002174 else {
2175 mc = pkt_dev->cur_dst_mac_offset++;
2176 if (pkt_dev->cur_dst_mac_offset >
2177 pkt_dev->dst_mac_count) {
2178 pkt_dev->cur_dst_mac_offset = 0;
2179 }
2180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Luiz Capitulino222f1802006-03-20 22:16:13 -08002182 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2183 pkt_dev->hh[5] = tmp;
2184 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2185 pkt_dev->hh[4] = tmp;
2186 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2187 pkt_dev->hh[3] = tmp;
2188 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2189 pkt_dev->hh[2] = tmp;
2190 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2191 pkt_dev->hh[1] = tmp;
2192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002194 if (pkt_dev->flags & F_MPLS_RND) {
2195 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002196 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002197 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2198 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002199 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002200 htonl(0x000fffff));
2201 }
2202
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002203 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002204 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002205 }
2206
2207 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002208 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002209 }
2210
Luiz Capitulino222f1802006-03-20 22:16:13 -08002211 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2212 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002213 pkt_dev->cur_udp_src = random32() %
2214 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2215 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Luiz Capitulino222f1802006-03-20 22:16:13 -08002217 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 pkt_dev->cur_udp_src++;
2219 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2220 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002221 }
2222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Luiz Capitulino222f1802006-03-20 22:16:13 -08002224 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2225 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002226 pkt_dev->cur_udp_dst = random32() %
2227 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2228 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002229 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002231 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002233 }
2234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 if (!(pkt_dev->flags & F_IPV6)) {
2237
Luiz Capitulino222f1802006-03-20 22:16:13 -08002238 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2239 ntohl(pkt_dev->
2240 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002242 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002243 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 else {
2245 t = ntohl(pkt_dev->cur_saddr);
2246 t++;
2247 if (t > imx) {
2248 t = imn;
2249 }
2250 }
2251 pkt_dev->cur_saddr = htonl(t);
2252 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002253
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002254 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2256 } else {
Al Viro252e3342006-11-14 20:48:11 -08002257 imn = ntohl(pkt_dev->daddr_min);
2258 imx = ntohl(pkt_dev->daddr_max);
2259 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002261 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (pkt_dev->flags & F_IPDST_RND) {
2263
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002264 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002265 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Joe Perches21cf2252007-12-16 13:44:00 -08002267 while (ipv4_is_loopback(s) ||
2268 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002269 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002270 ipv4_is_zeronet(s) ||
2271 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002272 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002273 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Al Viro252e3342006-11-14 20:48:11 -08002275 pkt_dev->cur_daddr = s;
2276 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 t = ntohl(pkt_dev->cur_daddr);
2278 t++;
2279 if (t > imx) {
2280 t = imn;
2281 }
2282 pkt_dev->cur_daddr = htonl(t);
2283 }
2284 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002285 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002286 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002287 pkt_dev->flows[flow].cur_daddr =
2288 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002289#ifdef CONFIG_XFRM
2290 if (pkt_dev->flags & F_IPSEC_ON)
2291 get_ipsec_sa(pkt_dev, flow);
2292#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 pkt_dev->nflows++;
2294 }
2295 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002296 } else { /* IPV6 * */
2297
2298 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2299 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2300 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2301 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 else {
2303 int i;
2304
2305 /* Only random destinations yet */
2306
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002309 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002310 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2311 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2317 __u32 t;
2318 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002319 t = random32() %
2320 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2321 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002322 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002324 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002326 }
2327 pkt_dev->cur_pkt_size = t;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Robert Olsson45b270f2007-08-28 15:45:55 -07002330 if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2331 __u16 t;
2332 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2333 t = random32() %
2334 (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1)
2335 + pkt_dev->queue_map_min;
2336 } else {
2337 t = pkt_dev->cur_queue_map + 1;
2338 if (t > pkt_dev->queue_map_max)
2339 t = pkt_dev->queue_map_min;
2340 }
2341 pkt_dev->cur_queue_map = t;
2342 }
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 pkt_dev->flows[flow].count++;
2345}
2346
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002347
2348#ifdef CONFIG_XFRM
2349static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2350{
2351 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2352 int err = 0;
2353 struct iphdr *iph;
2354
2355 if (!x)
2356 return 0;
2357 /* XXX: we dont support tunnel mode for now until
2358 * we resolve the dst issue */
2359 if (x->props.mode != XFRM_MODE_TRANSPORT)
2360 return 0;
2361
2362 spin_lock(&x->lock);
2363 iph = ip_hdr(skb);
2364
Herbert Xu13996372007-10-17 21:35:51 -07002365 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002366 if (err)
2367 goto error;
2368 err = x->type->output(x, skb);
2369 if (err)
2370 goto error;
2371
2372 x->curlft.bytes +=skb->len;
2373 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002374error:
2375 spin_unlock(&x->lock);
2376 return err;
2377}
2378
2379static inline void free_SAs(struct pktgen_dev *pkt_dev)
2380{
2381 if (pkt_dev->cflows) {
2382 /* let go of the SAs if we have them */
2383 int i = 0;
2384 for (; i < pkt_dev->nflows; i++){
2385 struct xfrm_state *x = pkt_dev->flows[i].x;
2386 if (x) {
2387 xfrm_state_put(x);
2388 pkt_dev->flows[i].x = NULL;
2389 }
2390 }
2391 }
2392}
2393
2394static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2395 struct sk_buff *skb, __be16 protocol)
2396{
2397 if (pkt_dev->flags & F_IPSEC_ON) {
2398 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2399 int nhead = 0;
2400 if (x) {
2401 int ret;
2402 __u8 *eth;
2403 nhead = x->props.header_len - skb_headroom(skb);
2404 if (nhead >0) {
2405 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2406 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002407 printk(KERN_ERR "Error expanding "
2408 "ipsec packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002409 return 0;
2410 }
2411 }
2412
2413 /* ipsec is not expecting ll header */
2414 skb_pull(skb, ETH_HLEN);
2415 ret = pktgen_output_ipsec(skb, pkt_dev);
2416 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002417 printk(KERN_ERR "Error creating ipsec "
2418 "packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002419 kfree_skb(skb);
2420 return 0;
2421 }
2422 /* restore ll */
2423 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2424 memcpy(eth, pkt_dev->hh, 12);
2425 *(u16 *) & eth[12] = protocol;
2426 }
2427 }
2428 return 1;
2429}
2430#endif
2431
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002432static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2433{
2434 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002435 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002436 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2437 }
2438 mpls--;
2439 *mpls |= MPLS_STACK_BOTTOM;
2440}
2441
Al Viro0f37c602006-11-03 03:49:56 -08002442static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2443 unsigned int prio)
2444{
2445 return htons(id | (cfi << 12) | (prio << 13));
2446}
2447
Luiz Capitulino222f1802006-03-20 22:16:13 -08002448static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2449 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
2451 struct sk_buff *skb = NULL;
2452 __u8 *eth;
2453 struct udphdr *udph;
2454 int datalen, iplen;
2455 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002456 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002457 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002458 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002459 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2460 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2461 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2462 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2463
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002464
2465 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002466 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002467
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002468 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002469 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002470
Robert Olsson64053be2005-06-26 15:27:10 -07002471 /* Update any of the values, used when we're incrementing various
2472 * fields.
2473 */
2474 mod_cur_headers(pkt_dev);
2475
David S. Miller7ac54592006-01-18 14:19:10 -08002476 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002477 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002478 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (!skb) {
2480 sprintf(pkt_dev->result, "No memory");
2481 return NULL;
2482 }
2483
David S. Miller7ac54592006-01-18 14:19:10 -08002484 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 /* Reserve for ethernet and IP header */
2487 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002488 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2489 if (pkt_dev->nr_labels)
2490 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002491
2492 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002493 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002494 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002495 *svlan_tci = build_tci(pkt_dev->svlan_id,
2496 pkt_dev->svlan_cfi,
2497 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002498 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002499 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002500 }
2501 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002502 *vlan_tci = build_tci(pkt_dev->vlan_id,
2503 pkt_dev->vlan_cfi,
2504 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002505 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002506 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002507 }
2508
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002509 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002510 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002511 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
Pavel Emelyanovdfa40912007-10-21 16:57:55 -07002512 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002513 iph = ip_hdr(skb);
2514 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002517 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002519 /* Eth + IPh + UDPh + mpls */
2520 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002521 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002522 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 udph->source = htons(pkt_dev->cur_udp_src);
2526 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002527 udph->len = htons(datalen + 8); /* DATA + udphdr */
2528 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 iph->ihl = 5;
2531 iph->version = 4;
2532 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002533 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002534 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 iph->saddr = pkt_dev->cur_saddr;
2536 iph->daddr = pkt_dev->cur_daddr;
2537 iph->frag_off = 0;
2538 iplen = 20 + 8 + datalen;
2539 iph->tot_len = htons(iplen);
2540 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002541 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002542 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002543 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002544 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 skb->dev = odev;
2546 skb->pkt_type = PACKET_HOST;
2547
Luiz Capitulino222f1802006-03-20 22:16:13 -08002548 if (pkt_dev->nfrags <= 0)
2549 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 else {
2551 int frags = pkt_dev->nfrags;
2552 int i;
2553
Luiz Capitulino222f1802006-03-20 22:16:13 -08002554 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (frags > MAX_SKB_FRAGS)
2557 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002558 if (datalen > frags * PAGE_SIZE) {
2559 skb_put(skb, datalen - frags * PAGE_SIZE);
2560 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
2562
2563 i = 0;
2564 while (datalen > 0) {
2565 struct page *page = alloc_pages(GFP_KERNEL, 0);
2566 skb_shinfo(skb)->frags[i].page = page;
2567 skb_shinfo(skb)->frags[i].page_offset = 0;
2568 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002569 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 datalen -= skb_shinfo(skb)->frags[i].size;
2571 skb->len += skb_shinfo(skb)->frags[i].size;
2572 skb->data_len += skb_shinfo(skb)->frags[i].size;
2573 i++;
2574 skb_shinfo(skb)->nr_frags = i;
2575 }
2576
2577 while (i < frags) {
2578 int rem;
2579
2580 if (i == 0)
2581 break;
2582
2583 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2584 if (rem == 0)
2585 break;
2586
2587 skb_shinfo(skb)->frags[i - 1].size -= rem;
2588
Luiz Capitulino222f1802006-03-20 22:16:13 -08002589 skb_shinfo(skb)->frags[i] =
2590 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002592 skb_shinfo(skb)->frags[i].page =
2593 skb_shinfo(skb)->frags[i - 1].page;
2594 skb_shinfo(skb)->frags[i].page_offset +=
2595 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 skb_shinfo(skb)->frags[i].size = rem;
2597 i++;
2598 skb_shinfo(skb)->nr_frags = i;
2599 }
2600 }
2601
Luiz Capitulino222f1802006-03-20 22:16:13 -08002602 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Luiz Capitulino222f1802006-03-20 22:16:13 -08002604 if (pgh) {
2605 struct timeval timestamp;
2606
2607 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2608 pgh->seq_num = htonl(pkt_dev->seq_num);
2609
2610 do_gettimeofday(&timestamp);
2611 pgh->tv_sec = htonl(timestamp.tv_sec);
2612 pgh->tv_usec = htonl(timestamp.tv_usec);
2613 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002614
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002615#ifdef CONFIG_XFRM
2616 if (!process_ipsec(pkt_dev, skb, protocol))
2617 return NULL;
2618#endif
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 return skb;
2621}
2622
2623/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002624 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2626 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002627 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 * Should be candidate for net/ipv4/utils.c
2629 * --ro
2630 */
2631
Luiz Capitulino222f1802006-03-20 22:16:13 -08002632static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002635 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 unsigned long u;
2637 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002638 unsigned int prefixlen = 0;
2639 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002640 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002641 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Luiz Capitulino222f1802006-03-20 22:16:13 -08002643 for (i = 0; i < 16; i++)
2644 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 for (;;) {
2647 if (*s == ':') {
2648 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002649 if (s[1] == ':') { /* Found "::", skip to part 2 */
2650 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 len++;
2652 break;
2653 }
2654 s++;
2655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002657 u = simple_strtoul(s, &pos, 16);
2658 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002659 if (!i)
2660 return 0;
2661 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 /* the last 4 bytes may be written as IPv4 address */
2664
2665 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002666 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2667 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
2669 ip[prefixlen++] = (u >> 8);
2670 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002671 s += i;
2672 len += i;
2673 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return len;
2675 }
2676
2677/* part 2, after "::" */
2678 for (;;) {
2679 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002680 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 break;
2682 s++;
2683 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002684 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002686
2687 u = simple_strtol(s, &pos, 16);
2688 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002690 if (*s)
2691 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 break;
2693 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002694 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002696 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2697 sizeof(tmp));
2698 suffixlen += 4;
2699 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 break;
2701 }
2702 suffix[suffixlen++] = (u >> 8);
2703 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002704 s += i;
2705 len += i;
2706 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
2708 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002709 for (i = 0; i < suffixlen; i++)
2710 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return len;
2712}
2713
Luiz Capitulino222f1802006-03-20 22:16:13 -08002714static char tohex(char hexdigit)
2715{
2716 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
Luiz Capitulino222f1802006-03-20 22:16:13 -08002719static int fmt_xlong(char *s, unsigned int i)
2720{
2721 char *bak = s;
2722 *s = tohex((i >> 12) & 0xf);
2723 if (s != bak || *s != '0')
2724 ++s;
2725 *s = tohex((i >> 8) & 0xf);
2726 if (s != bak || *s != '0')
2727 ++s;
2728 *s = tohex((i >> 4) & 0xf);
2729 if (s != bak || *s != '0')
2730 ++s;
2731 *s = tohex(i & 0xf);
2732 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
Luiz Capitulino222f1802006-03-20 22:16:13 -08002735static unsigned int fmt_ip6(char *s, const char ip[16])
2736{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 unsigned int len;
2738 unsigned int i;
2739 unsigned int temp;
2740 unsigned int compressing;
2741 int j;
2742
Luiz Capitulino222f1802006-03-20 22:16:13 -08002743 len = 0;
2744 compressing = 0;
2745 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
2747#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002748 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2749 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2750 temp = strlen(s);
2751 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002754 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2755 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (temp == 0) {
2757 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002758 compressing = 1;
2759 if (j == 0) {
2760 *s++ = ':';
2761 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763 }
2764 } else {
2765 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002766 compressing = 0;
2767 *s++ = ':';
2768 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002770 i = fmt_xlong(s, temp);
2771 len += i;
2772 s += i;
2773 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 *s++ = ':';
2775 ++len;
2776 }
2777 }
2778 }
2779 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002780 *s++ = ':';
2781 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002783 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return len;
2785}
2786
Luiz Capitulino222f1802006-03-20 22:16:13 -08002787static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2788 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
2790 struct sk_buff *skb = NULL;
2791 __u8 *eth;
2792 struct udphdr *udph;
2793 int datalen;
2794 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002795 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002796 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002797 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002798 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2799 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2800 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2801 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002802
2803 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002804 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002805
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002806 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002807 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002808
Robert Olsson64053be2005-06-26 15:27:10 -07002809 /* Update any of the values, used when we're incrementing various
2810 * fields.
2811 */
2812 mod_cur_headers(pkt_dev);
2813
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002814 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002815 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (!skb) {
2817 sprintf(pkt_dev->result, "No memory");
2818 return NULL;
2819 }
2820
2821 skb_reserve(skb, 16);
2822
2823 /* Reserve for ethernet and IP header */
2824 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002825 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2826 if (pkt_dev->nr_labels)
2827 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002828
2829 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002830 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002831 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002832 *svlan_tci = build_tci(pkt_dev->svlan_id,
2833 pkt_dev->svlan_cfi,
2834 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002835 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002836 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002837 }
2838 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002839 *vlan_tci = build_tci(pkt_dev->vlan_id,
2840 pkt_dev->vlan_cfi,
2841 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002842 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002843 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002844 }
2845
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002846 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002847 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002848 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
Pavel Emelyanovdfa40912007-10-21 16:57:55 -07002849 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002850 iph = ipv6_hdr(skb);
2851 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002854 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002855
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002856 /* Eth + IPh + UDPh + mpls */
2857 datalen = pkt_dev->cur_pkt_size - 14 -
2858 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002859 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Luiz Capitulino222f1802006-03-20 22:16:13 -08002861 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 datalen = sizeof(struct pktgen_hdr);
2863 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002864 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2865 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
2867
2868 udph->source = htons(pkt_dev->cur_udp_src);
2869 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002870 udph->len = htons(datalen + sizeof(struct udphdr));
2871 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002873 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002875 if (pkt_dev->traffic_class) {
2876 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002877 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002878 }
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 iph->hop_limit = 32;
2881
2882 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2883 iph->nexthdr = IPPROTO_UDP;
2884
2885 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2886 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2887
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002888 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002889 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002890 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 skb->dev = odev;
2892 skb->pkt_type = PACKET_HOST;
2893
Luiz Capitulino222f1802006-03-20 22:16:13 -08002894 if (pkt_dev->nfrags <= 0)
2895 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 else {
2897 int frags = pkt_dev->nfrags;
2898 int i;
2899
Luiz Capitulino222f1802006-03-20 22:16:13 -08002900 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 if (frags > MAX_SKB_FRAGS)
2903 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002904 if (datalen > frags * PAGE_SIZE) {
2905 skb_put(skb, datalen - frags * PAGE_SIZE);
2906 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
2908
2909 i = 0;
2910 while (datalen > 0) {
2911 struct page *page = alloc_pages(GFP_KERNEL, 0);
2912 skb_shinfo(skb)->frags[i].page = page;
2913 skb_shinfo(skb)->frags[i].page_offset = 0;
2914 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002915 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 datalen -= skb_shinfo(skb)->frags[i].size;
2917 skb->len += skb_shinfo(skb)->frags[i].size;
2918 skb->data_len += skb_shinfo(skb)->frags[i].size;
2919 i++;
2920 skb_shinfo(skb)->nr_frags = i;
2921 }
2922
2923 while (i < frags) {
2924 int rem;
2925
2926 if (i == 0)
2927 break;
2928
2929 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2930 if (rem == 0)
2931 break;
2932
2933 skb_shinfo(skb)->frags[i - 1].size -= rem;
2934
Luiz Capitulino222f1802006-03-20 22:16:13 -08002935 skb_shinfo(skb)->frags[i] =
2936 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002938 skb_shinfo(skb)->frags[i].page =
2939 skb_shinfo(skb)->frags[i - 1].page;
2940 skb_shinfo(skb)->frags[i].page_offset +=
2941 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 skb_shinfo(skb)->frags[i].size = rem;
2943 i++;
2944 skb_shinfo(skb)->nr_frags = i;
2945 }
2946 }
2947
Luiz Capitulino222f1802006-03-20 22:16:13 -08002948 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002950 if (pgh) {
2951 struct timeval timestamp;
2952
2953 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2954 pgh->seq_num = htonl(pkt_dev->seq_num);
2955
2956 do_gettimeofday(&timestamp);
2957 pgh->tv_sec = htonl(timestamp.tv_sec);
2958 pgh->tv_usec = htonl(timestamp.tv_usec);
2959 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002960 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return skb;
2963}
2964
Luiz Capitulino222f1802006-03-20 22:16:13 -08002965static inline struct sk_buff *fill_packet(struct net_device *odev,
2966 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002968 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return fill_packet_ipv6(odev, pkt_dev);
2970 else
2971 return fill_packet_ipv4(odev, pkt_dev);
2972}
2973
Luiz Capitulino222f1802006-03-20 22:16:13 -08002974static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002976 pkt_dev->seq_num = 1;
2977 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002979 pkt_dev->tx_bytes = 0;
2980 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
2983/* Set up structure for sending pkts, clear counters */
2984
2985static void pktgen_run(struct pktgen_thread *t)
2986{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002987 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 int started = 0;
2989
Stephen Hemminger25c4e532007-03-04 16:06:47 -08002990 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002993 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 /*
2996 * setup odev and create initial packet.
2997 */
2998 pktgen_setup_inject(pkt_dev);
2999
Luiz Capitulino222f1802006-03-20 22:16:13 -08003000 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003002 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 pkt_dev->skb = NULL;
3004 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 pkt_dev->next_tx_ns = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003007 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 strcpy(pkt_dev->result, "Starting");
3010 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003011 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 strcpy(pkt_dev->result, "Error starting");
3013 }
3014 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003015 if (started)
3016 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
3019static void pktgen_stop_all_threads_ifs(void)
3020{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003021 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003023 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003025 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003026
3027 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003028 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003029
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003030 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
Luiz Capitulino222f1802006-03-20 22:16:13 -08003033static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003035 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003036 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003038 list_for_each_entry(pkt_dev, &t->if_list, list)
3039 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 res = 1;
3041 break;
3042 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003043 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
Luiz Capitulino222f1802006-03-20 22:16:13 -08003046static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Luiz Capitulino222f1802006-03-20 22:16:13 -08003050 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Luiz Capitulino222f1802006-03-20 22:16:13 -08003052 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Luiz Capitulino222f1802006-03-20 22:16:13 -08003054 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Luiz Capitulino222f1802006-03-20 22:16:13 -08003056 if (signal_pending(current))
3057 goto signal;
3058 if_lock(t);
3059 }
3060 if_unlock(t);
3061 return 1;
3062signal:
3063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
3066static int pktgen_wait_all_threads_run(void)
3067{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003068 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003070
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003071 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003072
3073 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003075 if (sig == 0)
3076 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003078
3079 if (sig == 0)
3080 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003082
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003083 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return sig;
3085}
3086
3087static void pktgen_run_all_threads(void)
3088{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003089 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003091 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003093 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003095 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003097
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003098 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Luiz Capitulino222f1802006-03-20 22:16:13 -08003100 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 pktgen_wait_all_threads_run();
3103}
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3106{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003107 __u64 total_us, bps, mbps, pps, idle;
3108 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Luiz Capitulino222f1802006-03-20 22:16:13 -08003110 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Luiz Capitulino222f1802006-03-20 22:16:13 -08003112 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Luiz Capitulino222f1802006-03-20 22:16:13 -08003114 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3115 (unsigned long long)total_us,
3116 (unsigned long long)(total_us - idle),
3117 (unsigned long long)idle,
3118 (unsigned long long)pkt_dev->sofar,
3119 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Luiz Capitulino222f1802006-03-20 22:16:13 -08003121 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123 while ((total_us >> 32) != 0) {
3124 pps >>= 1;
3125 total_us >>= 1;
3126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Luiz Capitulino222f1802006-03-20 22:16:13 -08003128 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Luiz Capitulino222f1802006-03-20 22:16:13 -08003130 bps = pps * 8 * pkt_dev->cur_pkt_size;
3131
3132 mbps = bps;
3133 do_div(mbps, 1000000);
3134 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3135 (unsigned long long)pps,
3136 (unsigned long long)mbps,
3137 (unsigned long long)bps,
3138 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141/* Set stopped-at timer, remove from running list, do counters & statistics */
3142
Luiz Capitulino222f1802006-03-20 22:16:13 -08003143static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003145 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003146
Luiz Capitulino222f1802006-03-20 22:16:13 -08003147 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003148 printk(KERN_WARNING "pktgen: interface: %s is already "
3149 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003150 return -EINVAL;
3151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Luiz Capitulino222f1802006-03-20 22:16:13 -08003153 pkt_dev->stopped_at = getCurUs();
3154 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003156 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
Luiz Capitulino222f1802006-03-20 22:16:13 -08003161static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003163 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if_lock(t);
3166
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003167 list_for_each_entry(pkt_dev, &t->if_list, list) {
3168 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169 continue;
3170 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003171 best = pkt_dev;
3172 else if (pkt_dev->next_tx_us < best->next_tx_us)
3173 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003176 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
3178
Luiz Capitulino222f1802006-03-20 22:16:13 -08003179static void pktgen_stop(struct pktgen_thread *t)
3180{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003181 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003183 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Luiz Capitulino222f1802006-03-20 22:16:13 -08003185 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003187 list_for_each_entry(pkt_dev, &t->if_list, list) {
3188 pktgen_stop_device(pkt_dev);
3189 if (pkt_dev->skb)
3190 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003191
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003192 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003198/*
3199 * one of our devices needs to be removed - find it
3200 * and remove it
3201 */
3202static void pktgen_rem_one_if(struct pktgen_thread *t)
3203{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003204 struct list_head *q, *n;
3205 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003206
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003207 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003208
3209 if_lock(t);
3210
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003211 list_for_each_safe(q, n, &t->if_list) {
3212 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003213
Luiz Capitulino222f1802006-03-20 22:16:13 -08003214 if (!cur->removal_mark)
3215 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003216
3217 if (cur->skb)
3218 kfree_skb(cur->skb);
3219 cur->skb = NULL;
3220
3221 pktgen_remove_device(t, cur);
3222
3223 break;
3224 }
3225
3226 if_unlock(t);
3227}
3228
Luiz Capitulino222f1802006-03-20 22:16:13 -08003229static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003231 struct list_head *q, *n;
3232 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003233
3234 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003235
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003236 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003239 list_for_each_safe(q, n, &t->if_list) {
3240 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003241
3242 if (cur->skb)
3243 kfree_skb(cur->skb);
3244 cur->skb = NULL;
3245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 pktgen_remove_device(t, cur);
3247 }
3248
Luiz Capitulino222f1802006-03-20 22:16:13 -08003249 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
Luiz Capitulino222f1802006-03-20 22:16:13 -08003252static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003254 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
David S. Milleree74baa2007-01-01 20:51:53 -08003256 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003258 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003260 list_del(&t->th_list);
3261
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003262 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
3265static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3266{
3267 struct net_device *odev = NULL;
3268 __u64 idle_start = 0;
3269 int ret;
3270
3271 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3274 u64 now;
3275
3276 now = getCurUs();
3277 if (now < pkt_dev->next_tx_us)
3278 spin(pkt_dev, pkt_dev->next_tx_us);
3279
3280 /* This is max DELAY, this has special meaning of
3281 * "never transmit"
3282 */
3283 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3284 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3285 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3286 goto out;
3287 }
3288 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003289
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003290 if ((netif_queue_stopped(odev) ||
Robert Olsson378be2c2007-08-28 15:43:14 -07003291 (pkt_dev->skb &&
Pavel Emelyanov668f8952007-10-21 17:01:56 -07003292 netif_subqueue_stopped(odev, pkt_dev->skb))) ||
Robert Olsson378be2c2007-08-28 15:43:14 -07003293 need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 if (!netif_running(odev)) {
3297 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003298 if (pkt_dev->skb)
3299 kfree_skb(pkt_dev->skb);
3300 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 goto out;
3302 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003303 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003307
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003308 if (netif_queue_stopped(odev) ||
Pavel Emelyanov668f8952007-10-21 17:01:56 -07003309 netif_subqueue_stopped(odev, pkt_dev->skb)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003310 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003312 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003317 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3318 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003320 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 pkt_dev->skb = fill_packet(odev, pkt_dev);
3324 if (pkt_dev->skb == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003325 printk(KERN_ERR "pktgen: ERROR: couldn't "
3326 "allocate skb in fill_packet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003328 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 goto out;
3330 }
3331 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003332 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 }
3334 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003335
Herbert Xu932ff272006-06-09 12:20:56 -07003336 netif_tx_lock_bh(odev);
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003337 if (!netif_queue_stopped(odev) &&
Pavel Emelyanov668f8952007-10-21 17:01:56 -07003338 !netif_subqueue_stopped(odev, pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
3340 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003341 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3343 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003344 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 pkt_dev->sofar++;
3346 pkt_dev->seq_num++;
3347 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003348
3349 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 && (odev->features & NETIF_F_LLTX)) {
3351 cpu_relax();
3352 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003353 } else { /* Retry it next time */
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (debug && net_ratelimit())
3358 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 pkt_dev->errors++;
3361 pkt_dev->last_ok = 0;
3362 }
3363
3364 pkt_dev->next_tx_us = getCurUs();
3365 pkt_dev->next_tx_ns = 0;
3366
3367 pkt_dev->next_tx_us += pkt_dev->delay_us;
3368 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3369
3370 if (pkt_dev->next_tx_ns > 1000) {
3371 pkt_dev->next_tx_us++;
3372 pkt_dev->next_tx_ns -= 1000;
3373 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Luiz Capitulino222f1802006-03-20 22:16:13 -08003376 else { /* Retry it next time */
3377 pkt_dev->last_ok = 0;
3378 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Herbert Xu932ff272006-06-09 12:20:56 -07003382 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 /* If pkt_dev->count is zero, then run forever */
3385 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3386 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3387 idle_start = getCurUs();
3388 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3389 if (signal_pending(current)) {
3390 break;
3391 }
3392 schedule();
3393 }
3394 pkt_dev->idle_acc += getCurUs() - idle_start;
3395 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 /* Done with this */
3398 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003399 if (pkt_dev->skb)
3400 kfree_skb(pkt_dev->skb);
3401 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003402 }
3403out:;
3404}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003406/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 * Main loop of the thread goes here
3408 */
3409
David S. Milleree74baa2007-01-01 20:51:53 -08003410static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
3412 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003413 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003414 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
David S. Milleree74baa2007-01-01 20:51:53 -08003417 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
3419 init_waitqueue_head(&t->queue);
3420
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003421 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
David S. Milleree74baa2007-01-01 20:51:53 -08003423 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003424
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003425 set_freezable();
3426
David S. Milleree74baa2007-01-01 20:51:53 -08003427 while (!kthread_should_stop()) {
3428 pkt_dev = next_to_run(t);
3429
3430 if (!pkt_dev &&
3431 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3432 == 0) {
3433 prepare_to_wait(&(t->queue), &wait,
3434 TASK_INTERRUPTIBLE);
3435 schedule_timeout(HZ / 10);
3436 finish_wait(&(t->queue), &wait);
3437 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 __set_current_state(TASK_RUNNING);
3440
Robert Olssonb1639112007-08-28 15:46:58 -07003441 if (pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 pktgen_xmit(pkt_dev);
3443
Luiz Capitulino222f1802006-03-20 22:16:13 -08003444 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 pktgen_stop(t);
3446 t->control &= ~(T_STOP);
3447 }
3448
Luiz Capitulino222f1802006-03-20 22:16:13 -08003449 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 pktgen_run(t);
3451 t->control &= ~(T_RUN);
3452 }
3453
Luiz Capitulino222f1802006-03-20 22:16:13 -08003454 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003456 t->control &= ~(T_REMDEVALL);
3457 }
3458
Luiz Capitulino222f1802006-03-20 22:16:13 -08003459 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003460 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 t->control &= ~(T_REMDEV);
3462 }
3463
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003464 try_to_freeze();
3465
David S. Milleree74baa2007-01-01 20:51:53 -08003466 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003469 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003470 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003472 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003473 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003475 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003476 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003477
David S. Milleree74baa2007-01-01 20:51:53 -08003478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
3480
Luiz Capitulino222f1802006-03-20 22:16:13 -08003481static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3482 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003484 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003485 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003487 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003488 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003489 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003490 break;
3491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Luiz Capitulino222f1802006-03-20 22:16:13 -08003493 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003494 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003495 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003498/*
3499 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 */
3501
Luiz Capitulino222f1802006-03-20 22:16:13 -08003502static int add_dev_to_thread(struct pktgen_thread *t,
3503 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
3505 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Luiz Capitulino222f1802006-03-20 22:16:13 -08003507 if_lock(t);
3508
3509 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003510 printk(KERN_ERR "pktgen: ERROR: already assigned "
3511 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003512 rv = -EBUSY;
3513 goto out;
3514 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003515
3516 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 pkt_dev->running = 0;
3519
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520out:
3521 if_unlock(t);
3522 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
3525/* Called under thread lock */
3526
Luiz Capitulino222f1802006-03-20 22:16:13 -08003527static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003529 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003530 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 /* We don't allow a device to be on several threads */
3533
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003534 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3535 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003536 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003537 return -EBUSY;
3538 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003539
3540 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3541 if (!pkt_dev)
3542 return -ENOMEM;
3543
Luiz Capitulino222f1802006-03-20 22:16:13 -08003544 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003545 if (pkt_dev->flows == NULL) {
3546 kfree(pkt_dev);
3547 return -ENOMEM;
3548 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003549 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003550
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003551 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003552 pkt_dev->min_pkt_size = ETH_ZLEN;
3553 pkt_dev->max_pkt_size = ETH_ZLEN;
3554 pkt_dev->nfrags = 0;
3555 pkt_dev->clone_skb = pg_clone_skb_d;
3556 pkt_dev->delay_us = pg_delay_d / 1000;
3557 pkt_dev->delay_ns = pg_delay_d % 1000;
3558 pkt_dev->count = pg_count_d;
3559 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003560 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003561 pkt_dev->udp_src_max = 9;
3562 pkt_dev->udp_dst_min = 9;
3563 pkt_dev->udp_dst_max = 9;
3564
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003565 pkt_dev->vlan_p = 0;
3566 pkt_dev->vlan_cfi = 0;
3567 pkt_dev->vlan_id = 0xffff;
3568 pkt_dev->svlan_p = 0;
3569 pkt_dev->svlan_cfi = 0;
3570 pkt_dev->svlan_id = 0xffff;
3571
Stephen Hemminger39df2322007-03-04 16:11:51 -08003572 err = pktgen_setup_dev(pkt_dev, ifname);
3573 if (err)
3574 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003575
Stephen Hemminger39df2322007-03-04 16:11:51 -08003576 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3577 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003578 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003579 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003580 err = -EINVAL;
3581 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003582 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08003583 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3584 pkt_dev->entry->data = pkt_dev;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003585#ifdef CONFIG_XFRM
3586 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3587 pkt_dev->ipsproto = IPPROTO_ESP;
3588#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003589
3590 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003591out2:
3592 dev_put(pkt_dev->odev);
3593out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003594#ifdef CONFIG_XFRM
3595 free_SAs(pkt_dev);
3596#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -08003597 if (pkt_dev->flows)
3598 vfree(pkt_dev->flows);
3599 kfree(pkt_dev);
3600 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
David S. Milleree74baa2007-01-01 20:51:53 -08003603static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003605 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003606 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003607 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Luiz Capitulino222f1802006-03-20 22:16:13 -08003609 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3610 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003611 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3612 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003613 return -ENOMEM;
3614 }
3615
Luiz Capitulino222f1802006-03-20 22:16:13 -08003616 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003618
David S. Milleree74baa2007-01-01 20:51:53 -08003619 INIT_LIST_HEAD(&t->if_list);
3620
3621 list_add_tail(&t->th_list, &pktgen_threads);
3622
3623 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3624 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003625 printk(KERN_ERR "pktgen: kernel_thread() failed "
3626 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003627 list_del(&t->th_list);
3628 kfree(t);
3629 return PTR_ERR(p);
3630 }
3631 kthread_bind(p, cpu);
3632 t->tsk = p;
3633
3634 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003635 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003636 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003637 PG_PROC_DIR, t->tsk->comm);
3638 kthread_stop(p);
3639 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003640 kfree(t);
3641 return -EINVAL;
3642 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003643
3644 pe->proc_fops = &pktgen_thread_fops;
3645 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
David S. Milleree74baa2007-01-01 20:51:53 -08003647 wake_up_process(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
3649 return 0;
3650}
3651
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003652/*
3653 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003655static void _rem_dev_from_if_list(struct pktgen_thread *t,
3656 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003658 struct list_head *q, *n;
3659 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003661 list_for_each_safe(q, n, &t->if_list) {
3662 p = list_entry(q, struct pktgen_dev, list);
3663 if (p == pkt_dev)
3664 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666}
3667
Luiz Capitulino222f1802006-03-20 22:16:13 -08003668static int pktgen_remove_device(struct pktgen_thread *t,
3669 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670{
3671
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003672 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Luiz Capitulino222f1802006-03-20 22:16:13 -08003674 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003675 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3676 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003677 pktgen_stop_device(pkt_dev);
3678 }
3679
3680 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
3682 if (pkt_dev->odev) {
3683 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003684 pkt_dev->odev = NULL;
3685 }
3686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 /* And update the thread if_list */
3688
3689 _rem_dev_from_if_list(t, pkt_dev);
3690
Stephen Hemminger39df2322007-03-04 16:11:51 -08003691 if (pkt_dev->entry)
3692 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003694#ifdef CONFIG_XFRM
3695 free_SAs(pkt_dev);
3696#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 if (pkt_dev->flows)
3698 vfree(pkt_dev->flows);
3699 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
Luiz Capitulino222f1802006-03-20 22:16:13 -08003703static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
3705 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003706 struct proc_dir_entry *pe;
3707
David S. Miller25a8b252007-07-30 16:11:48 -07003708 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003710 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003711 if (!pg_proc_dir)
3712 return -ENODEV;
3713 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003715 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003716 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003717 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3718 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003719 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003720 return -EINVAL;
3721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Luiz Capitulino222f1802006-03-20 22:16:13 -08003723 pe->proc_fops = &pktgen_fops;
3724 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
3726 /* Register us to receive netdevice events */
3727 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003728
John Hawkes670c02c2005-10-13 09:30:31 -07003729 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003730 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
David S. Milleree74baa2007-01-01 20:51:53 -08003732 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003733 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003734 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3735 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003736 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003737
3738 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003739 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3740 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003741 unregister_netdevice_notifier(&pktgen_notifier_block);
3742 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003743 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003744 return -ENODEV;
3745 }
3746
Luiz Capitulino222f1802006-03-20 22:16:13 -08003747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
3750static void __exit pg_cleanup(void)
3751{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003752 struct pktgen_thread *t;
3753 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 wait_queue_head_t queue;
3755 init_waitqueue_head(&queue);
3756
Luiz Capitulino222f1802006-03-20 22:16:13 -08003757 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003759 list_for_each_safe(q, n, &pktgen_threads) {
3760 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003761 kthread_stop(t->tsk);
3762 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Luiz Capitulino222f1802006-03-20 22:16:13 -08003765 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 unregister_netdevice_notifier(&pktgen_notifier_block);
3767
Luiz Capitulino222f1802006-03-20 22:16:13 -08003768 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003769 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003770 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771}
3772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773module_init(pg_init);
3774module_exit(pg_cleanup);
3775
3776MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3777MODULE_DESCRIPTION("Packet Generator tool");
3778MODULE_LICENSE("GPL");
3779module_param(pg_count_d, int, 0);
3780module_param(pg_delay_d, int, 0);
3781module_param(pg_clone_skb_d, int, 0);
3782module_param(debug, int, 0);