blob: 20e63b302ba613ea7112365376d0773f4a7a8fbf [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#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800174#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800175#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800178#define F_IPSRC_RND (1<<0) /* IP-Src Random */
179#define F_IPDST_RND (1<<1) /* IP-Dst Random */
180#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800186#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700187#define F_VID_RND (1<<9) /* Random VLAN ID */
188#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700189#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700190#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700191#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800194#define T_TERMINATE (1<<0)
195#define T_STOP (1<<1) /* Stop run */
196#define T_RUN (1<<2) /* Start run */
197#define T_REMDEVALL (1<<3) /* Remove all devs */
198#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* If lock -- can be removed after some work */
201#define if_lock(t) spin_lock(&(t->if_lock));
202#define if_unlock(t) spin_unlock(&(t->if_lock));
203
204/* Used to help with determining the pkts on receive */
205#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700206#define PG_PROC_DIR "pktgen"
207#define PGCTRL "pgctrl"
208static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210#define MAX_CFLOWS 65536
211
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700212#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
213#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
214
Luiz Capitulino222f1802006-03-20 22:16:13 -0800215struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800216 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800217 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700218#ifdef CONFIG_XFRM
219 struct xfrm_state *x;
220#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700221 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700224/* flow flag bits */
225#define F_INIT (1<<0) /* flow has been initialized */
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * Try to keep frequent/infrequent used vars. separated.
230 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800231 struct proc_dir_entry *entry; /* proc file */
232 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800233 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Luiz Capitulino222f1802006-03-20 22:16:13 -0800235 int running; /* if this changes to false, the test will stop */
236
237 /* If min != max, then we will either do a linear iteration, or
238 * we will do a random selection from within the range.
239 */
240 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800241 int removal_mark; /* non-zero => the device is marked for
242 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Luiz Capitulino222f1802006-03-20 22:16:13 -0800244 int min_pkt_size; /* = ETH_ZLEN; */
245 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700246 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800247 int nfrags;
248 __u32 delay_us; /* Default delay */
249 __u32 delay_ns;
250 __u64 count; /* Default No packets to send */
251 __u64 sofar; /* How many pkts we've sent so far */
252 __u64 tx_bytes; /* How many bytes we've transmitted */
253 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Luiz Capitulino222f1802006-03-20 22:16:13 -0800255 /* runtime counters relating to clone_skb */
256 __u64 next_tx_us; /* timestamp of when to tx next */
257 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Luiz Capitulino222f1802006-03-20 22:16:13 -0800259 __u64 allocated_skbs;
260 __u32 clone_count;
261 int last_ok; /* Was last skb sent?
262 * Or a failed transmit of some sort? This will keep
263 * sequence numbers in order, for example.
264 */
265 __u64 started_at; /* micro-seconds */
266 __u64 stopped_at; /* micro-seconds */
267 __u64 idle_acc; /* micro-seconds */
268 __u32 seq_num;
269
270 int clone_skb; /* Use multiple SKBs during packet gen. If this number
271 * is greater than 1, then that many copies of the same
272 * packet will be sent before a new packet is allocated.
273 * For instance, if you want to send 1024 identical packets
274 * before creating a new packet, set clone_skb to 1024.
275 */
276
277 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
278 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281
282 struct in6_addr in6_saddr;
283 struct in6_addr in6_daddr;
284 struct in6_addr cur_in6_daddr;
285 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800287 struct in6_addr min_in6_daddr;
288 struct in6_addr max_in6_daddr;
289 struct in6_addr min_in6_saddr;
290 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Luiz Capitulino222f1802006-03-20 22:16:13 -0800292 /* If we're doing ranges, random or incremental, then this
293 * defines the min/max for those ranges.
294 */
Al Viro252e3342006-11-14 20:48:11 -0800295 __be32 saddr_min; /* inclusive, source IP address */
296 __be32 saddr_max; /* exclusive, source IP address */
297 __be32 daddr_min; /* inclusive, dest IP address */
298 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Luiz Capitulino222f1802006-03-20 22:16:13 -0800300 __u16 udp_src_min; /* inclusive, source UDP port */
301 __u16 udp_src_max; /* exclusive, source UDP port */
302 __u16 udp_dst_min; /* inclusive, dest UDP port */
303 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700305 /* DSCP + ECN */
306 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
307 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
308
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800309 /* MPLS */
310 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
311 __be32 labels[MAX_MPLS_LABELS];
312
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700313 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
314 __u8 vlan_p;
315 __u8 vlan_cfi;
316 __u16 vlan_id; /* 0xffff means no vlan tag */
317
318 __u8 svlan_p;
319 __u8 svlan_cfi;
320 __u16 svlan_id; /* 0xffff means no svlan tag */
321
Luiz Capitulino222f1802006-03-20 22:16:13 -0800322 __u32 src_mac_count; /* How many MACs to iterate through */
323 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Luiz Capitulino222f1802006-03-20 22:16:13 -0800325 unsigned char dst_mac[ETH_ALEN];
326 unsigned char src_mac[ETH_ALEN];
327
328 __u32 cur_dst_mac_offset;
329 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800330 __be32 cur_saddr;
331 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800332 __u16 cur_udp_dst;
333 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700334 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800335 __u32 cur_pkt_size;
336
337 __u8 hh[14];
338 /* = {
339 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
340
341 We fill in SRC address later
342 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343 0x08, 0x00
344 };
345 */
346 __u16 pad; /* pad out the hh struct to an even 16 bytes */
347
348 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
349 * are transmitting the same one multiple times
350 */
351 struct net_device *odev; /* The out-going device. Note that the device should
352 * have it's pg_info pointer pointing back to this
353 * device. This will be set when the user specifies
354 * the out-going device name (not when the inject is
355 * started as it used to do.)
356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800358 unsigned cflows; /* Concurrent flows (config) */
359 unsigned lflow; /* Flow length (config) */
360 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700361 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700362
363 u16 queue_map_min;
364 u16 queue_map_max;
365
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700366#ifdef CONFIG_XFRM
367 __u8 ipsmode; /* IPSEC mode (config) */
368 __u8 ipsproto; /* IPSEC type (config) */
369#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800370 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
373struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800374 __be32 pgh_magic;
375 __be32 seq_num;
376 __be32 tv_sec;
377 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
380struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800381 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800382 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800383 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800384 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800385 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Luiz Capitulino222f1802006-03-20 22:16:13 -0800387 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
388
389 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int cpu;
391
Luiz Capitulino222f1802006-03-20 22:16:13 -0800392 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393};
394
395#define REMOVE 1
396#define FIND 0
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800399static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800401 __u64 us = tv->tv_usec;
402 us += (__u64) tv->tv_sec * (__u64) 1000000;
403 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Ilpo Järvinen6db105d2008-01-12 03:25:00 -0800406static __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800408 struct timeval tv;
409 do_gettimeofday(&tv);
410 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/* old include end */
414
415static char version[] __initdata = VERSION;
416
Luiz Capitulino222f1802006-03-20 22:16:13 -0800417static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
418static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
419static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
420 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
422static void pktgen_run_all_threads(void);
423static void pktgen_stop_all_threads_ifs(void);
424static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800425static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800427
Luiz Capitulino222f1802006-03-20 22:16:13 -0800428static unsigned int scan_ip6(const char *s, char ip[16]);
429static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800432static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb972005-12-22 12:51:46 -0800433static int pg_delay_d;
434static int pg_clone_skb_d;
435static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Luiz Capitulino222fa072006-03-20 22:24:27 -0800437static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800438static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static struct notifier_block pktgen_notifier_block = {
441 .notifier_call = pktgen_device_event,
442};
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900445 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
447 */
448
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700449static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800450{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700451 seq_puts(seq, VERSION);
452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Luiz Capitulino222f1802006-03-20 22:16:13 -0800455static ssize_t pgctrl_write(struct file *file, const char __user * buf,
456 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700459 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Luiz Capitulino222f1802006-03-20 22:16:13 -0800461 if (!capable(CAP_NET_ADMIN)) {
462 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700466 if (count > sizeof(data))
467 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700470 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700471 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800472 }
473 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Luiz Capitulino222f1802006-03-20 22:16:13 -0800475 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 pktgen_stop_all_threads_ifs();
477
Luiz Capitulino222f1802006-03-20 22:16:13 -0800478 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 pktgen_run_all_threads();
480
Luiz Capitulino222f1802006-03-20 22:16:13 -0800481 else
David S. Miller25a8b252007-07-30 16:11:48 -0700482 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 err = count;
485
Luiz Capitulino222f1802006-03-20 22:16:13 -0800486out:
487 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700490static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700492 return single_open(file, pgctrl_show, PDE(inode)->data);
493}
494
Arjan van de Ven9a321442007-02-12 00:55:35 -0800495static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800496 .owner = THIS_MODULE,
497 .open = pgctrl_open,
498 .read = seq_read,
499 .llseek = seq_lseek,
500 .write = pgctrl_write,
501 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700502};
503
504static int pktgen_if_show(struct seq_file *seq, void *v)
505{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800506 struct pktgen_dev *pkt_dev = seq->private;
507 __u64 sa;
508 __u64 stopped;
509 __u64 now = getCurUs();
Joe Perches0795af52007-10-03 17:59:30 -0700510 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Luiz Capitulino222f1802006-03-20 22:16:13 -0800512 seq_printf(seq,
513 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
514 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
515 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Luiz Capitulino222f1802006-03-20 22:16:13 -0800517 seq_printf(seq,
518 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
519 pkt_dev->nfrags,
520 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800521 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Luiz Capitulino222f1802006-03-20 22:16:13 -0800523 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
524 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Robert Olsson45b270f2007-08-28 15:45:55 -0700526 seq_printf(seq,
527 " queue_map_min: %u queue_map_max: %u\n",
528 pkt_dev->queue_map_min,
529 pkt_dev->queue_map_max);
530
Luiz Capitulino222f1802006-03-20 22:16:13 -0800531 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800533 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
534 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
535 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
536 seq_printf(seq,
537 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
538 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Luiz Capitulino222f1802006-03-20 22:16:13 -0800540 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
541 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
542 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
543 seq_printf(seq,
544 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
545 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Luiz Capitulino222f1802006-03-20 22:16:13 -0800547 } else
548 seq_printf(seq,
549 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
550 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
551 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700553 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Joe Perches0795af52007-10-03 17:59:30 -0700555 seq_printf(seq, "%s ",
556 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
557 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Luiz Capitulino222f1802006-03-20 22:16:13 -0800559 seq_printf(seq, "dst_mac: ");
Joe Perches0795af52007-10-03 17:59:30 -0700560 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Luiz Capitulino222f1802006-03-20 22:16:13 -0800562 seq_printf(seq,
563 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
564 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
565 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Luiz Capitulino222f1802006-03-20 22:16:13 -0800567 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800568 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700569 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800571 if (pkt_dev->nr_labels) {
572 unsigned i;
573 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700574 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800575 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
576 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
577 }
578
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700579 if (pkt_dev->vlan_id != 0xffff) {
580 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
581 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
582 }
583
584 if (pkt_dev->svlan_id != 0xffff) {
585 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
586 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
587 }
588
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700589 if (pkt_dev->tos) {
590 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
591 }
592
593 if (pkt_dev->traffic_class) {
594 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
595 }
596
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800597 seq_printf(seq, " Flags: ");
598
Luiz Capitulino222f1802006-03-20 22:16:13 -0800599 if (pkt_dev->flags & F_IPV6)
600 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Luiz Capitulino222f1802006-03-20 22:16:13 -0800602 if (pkt_dev->flags & F_IPSRC_RND)
603 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Luiz Capitulino222f1802006-03-20 22:16:13 -0800605 if (pkt_dev->flags & F_IPDST_RND)
606 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Luiz Capitulino222f1802006-03-20 22:16:13 -0800608 if (pkt_dev->flags & F_TXSIZE_RND)
609 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Luiz Capitulino222f1802006-03-20 22:16:13 -0800611 if (pkt_dev->flags & F_UDPSRC_RND)
612 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Luiz Capitulino222f1802006-03-20 22:16:13 -0800614 if (pkt_dev->flags & F_UDPDST_RND)
615 seq_printf(seq, "UDPDST_RND ");
616
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800617 if (pkt_dev->flags & F_MPLS_RND)
618 seq_printf(seq, "MPLS_RND ");
619
Robert Olsson45b270f2007-08-28 15:45:55 -0700620 if (pkt_dev->flags & F_QUEUE_MAP_RND)
621 seq_printf(seq, "QUEUE_MAP_RND ");
622
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700623 if (pkt_dev->cflows) {
624 if (pkt_dev->flags & F_FLOW_SEQ)
625 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
626 else
627 seq_printf(seq, "FLOW_RND ");
628 }
629
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700630#ifdef CONFIG_XFRM
631 if (pkt_dev->flags & F_IPSEC_ON)
632 seq_printf(seq, "IPSEC ");
633#endif
634
Luiz Capitulino222f1802006-03-20 22:16:13 -0800635 if (pkt_dev->flags & F_MACSRC_RND)
636 seq_printf(seq, "MACSRC_RND ");
637
638 if (pkt_dev->flags & F_MACDST_RND)
639 seq_printf(seq, "MACDST_RND ");
640
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700641 if (pkt_dev->flags & F_VID_RND)
642 seq_printf(seq, "VID_RND ");
643
644 if (pkt_dev->flags & F_SVID_RND)
645 seq_printf(seq, "SVID_RND ");
646
Luiz Capitulino222f1802006-03-20 22:16:13 -0800647 seq_puts(seq, "\n");
648
649 sa = pkt_dev->started_at;
650 stopped = pkt_dev->stopped_at;
651 if (pkt_dev->running)
652 stopped = now; /* not really stopped, more like last-running-at */
653
654 seq_printf(seq,
655 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
656 (unsigned long long)pkt_dev->sofar,
657 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
658 (unsigned long long)stopped,
659 (unsigned long long)pkt_dev->idle_acc);
660
661 seq_printf(seq,
662 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700663 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
664 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Luiz Capitulino222f1802006-03-20 22:16:13 -0800666 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800668 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
669 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
670 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
671 } else
672 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700673 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Luiz Capitulino222f1802006-03-20 22:16:13 -0800675 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700676 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Robert Olsson45b270f2007-08-28 15:45:55 -0700678 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
679
Luiz Capitulino222f1802006-03-20 22:16:13 -0800680 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800683 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800685 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800690
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700691static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800692{
693 int i = 0;
694 *num = 0;
695
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700696 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800697 char c;
698 *num <<= 4;
699 if (get_user(c, &user_buffer[i]))
700 return -EFAULT;
701 if ((c >= '0') && (c <= '9'))
702 *num |= c - '0';
703 else if ((c >= 'a') && (c <= 'f'))
704 *num |= c - 'a' + 10;
705 else if ((c >= 'A') && (c <= 'F'))
706 *num |= c - 'A' + 10;
707 else
708 break;
709 }
710 return i;
711}
712
Luiz Capitulino222f1802006-03-20 22:16:13 -0800713static int count_trail_chars(const char __user * user_buffer,
714 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 int i;
717
718 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800719 char c;
720 if (get_user(c, &user_buffer[i]))
721 return -EFAULT;
722 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 case '\"':
724 case '\n':
725 case '\r':
726 case '\t':
727 case ' ':
728 case '=':
729 break;
730 default:
731 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734done:
735 return i;
736}
737
Luiz Capitulino222f1802006-03-20 22:16:13 -0800738static unsigned long num_arg(const char __user * user_buffer,
739 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int i = 0;
742 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800743
744 for (; i < maxlen; i++) {
745 char c;
746 if (get_user(c, &user_buffer[i]))
747 return -EFAULT;
748 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800750 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 } else
752 break;
753 }
754 return i;
755}
756
Luiz Capitulino222f1802006-03-20 22:16:13 -0800757static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 int i = 0;
760
Luiz Capitulino222f1802006-03-20 22:16:13 -0800761 for (; i < maxlen; i++) {
762 char c;
763 if (get_user(c, &user_buffer[i]))
764 return -EFAULT;
765 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 case '\"':
767 case '\n':
768 case '\r':
769 case '\t':
770 case ' ':
771 goto done_str;
772 break;
773 default:
774 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return i;
779}
780
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800781static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
782{
783 unsigned n = 0;
784 char c;
785 ssize_t i = 0;
786 int len;
787
788 pkt_dev->nr_labels = 0;
789 do {
790 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700791 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800792 if (len <= 0)
793 return len;
794 pkt_dev->labels[n] = htonl(tmp);
795 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
796 pkt_dev->flags |= F_MPLS_RND;
797 i += len;
798 if (get_user(c, &buffer[i]))
799 return -EFAULT;
800 i++;
801 n++;
802 if (n >= MAX_MPLS_LABELS)
803 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700804 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800805
806 pkt_dev->nr_labels = n;
807 return i;
808}
809
Luiz Capitulino222f1802006-03-20 22:16:13 -0800810static ssize_t pktgen_if_write(struct file *file,
811 const char __user * user_buffer, size_t count,
812 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800814 struct seq_file *seq = (struct seq_file *)file->private_data;
815 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int i = 0, max, len;
817 char name[16], valstr[32];
818 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800819 char *pg_result = NULL;
820 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800822
823 pg_result = &(pkt_dev->result[0]);
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700826 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return -EINVAL;
828 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 max = count - i;
831 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800832 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700833 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800834 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800836 i += tmp;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Read variable name */
839
840 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800841 if (len < 0) {
842 return len;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800845 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return -EFAULT;
847 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800848
849 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800851 if (len < 0)
852 return len;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 i += len;
855
856 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800857 char tb[count + 1];
858 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800860 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700861 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800862 (unsigned long)count, tb);
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 if (!strcmp(name, "min_pkt_size")) {
866 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867 if (len < 0) {
868 return len;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800871 if (value < 14 + 20 + 8)
872 value = 14 + 20 + 8;
873 if (value != pkt_dev->min_pkt_size) {
874 pkt_dev->min_pkt_size = value;
875 pkt_dev->cur_pkt_size = value;
876 }
877 sprintf(pg_result, "OK: min_pkt_size=%u",
878 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return count;
880 }
881
Luiz Capitulino222f1802006-03-20 22:16:13 -0800882 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800884 if (len < 0) {
885 return len;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800888 if (value < 14 + 20 + 8)
889 value = 14 + 20 + 8;
890 if (value != pkt_dev->max_pkt_size) {
891 pkt_dev->max_pkt_size = value;
892 pkt_dev->cur_pkt_size = value;
893 }
894 sprintf(pg_result, "OK: max_pkt_size=%u",
895 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return count;
897 }
898
Luiz Capitulino222f1802006-03-20 22:16:13 -0800899 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (!strcmp(name, "pkt_size")) {
902 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800903 if (len < 0) {
904 return len;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907 if (value < 14 + 20 + 8)
908 value = 14 + 20 + 8;
909 if (value != pkt_dev->min_pkt_size) {
910 pkt_dev->min_pkt_size = value;
911 pkt_dev->max_pkt_size = value;
912 pkt_dev->cur_pkt_size = value;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
915 return count;
916 }
917
Luiz Capitulino222f1802006-03-20 22:16:13 -0800918 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800920 if (len < 0) {
921 return len;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 sprintf(pg_result, "OK: debug=%u", debug);
926 return count;
927 }
928
Luiz Capitulino222f1802006-03-20 22:16:13 -0800929 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800931 if (len < 0) {
932 return len;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 i += len;
935 pkt_dev->nfrags = value;
936 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
937 return count;
938 }
939 if (!strcmp(name, "delay")) {
940 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800941 if (len < 0) {
942 return len;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 i += len;
945 if (value == 0x7FFFFFFF) {
946 pkt_dev->delay_us = 0x7FFFFFFF;
947 pkt_dev->delay_ns = 0;
948 } else {
949 pkt_dev->delay_us = value / 1000;
950 pkt_dev->delay_ns = value % 1000;
951 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800952 sprintf(pg_result, "OK: delay=%u",
953 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return count;
955 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800958 if (len < 0) {
959 return len;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800962 if (value != pkt_dev->udp_src_min) {
963 pkt_dev->udp_src_min = value;
964 pkt_dev->cur_udp_src = value;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
967 return count;
968 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800969 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800971 if (len < 0) {
972 return len;
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975 if (value != pkt_dev->udp_dst_min) {
976 pkt_dev->udp_dst_min = value;
977 pkt_dev->cur_udp_dst = value;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
980 return count;
981 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800982 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800984 if (len < 0) {
985 return len;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800988 if (value != pkt_dev->udp_src_max) {
989 pkt_dev->udp_src_max = value;
990 pkt_dev->cur_udp_src = value;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
993 return count;
994 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800995 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800997 if (len < 0) {
998 return len;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001001 if (value != pkt_dev->udp_dst_max) {
1002 pkt_dev->udp_dst_max = value;
1003 pkt_dev->cur_udp_dst = value;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1006 return count;
1007 }
1008 if (!strcmp(name, "clone_skb")) {
1009 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001010 if (len < 0) {
1011 return len;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001014 pkt_dev->clone_skb = value;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1017 return count;
1018 }
1019 if (!strcmp(name, "count")) {
1020 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001021 if (len < 0) {
1022 return len;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 i += len;
1025 pkt_dev->count = value;
1026 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001027 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return count;
1029 }
1030 if (!strcmp(name, "src_mac_count")) {
1031 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001032 if (len < 0) {
1033 return len;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 i += len;
1036 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001037 pkt_dev->src_mac_count = value;
1038 pkt_dev->cur_src_mac_offset = 0;
1039 }
1040 sprintf(pg_result, "OK: src_mac_count=%d",
1041 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return count;
1043 }
1044 if (!strcmp(name, "dst_mac_count")) {
1045 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001046 if (len < 0) {
1047 return len;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 i += len;
1050 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001051 pkt_dev->dst_mac_count = value;
1052 pkt_dev->cur_dst_mac_offset = 0;
1053 }
1054 sprintf(pg_result, "OK: dst_mac_count=%d",
1055 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return count;
1057 }
1058 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001059 char f[32];
1060 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001062 if (len < 0) {
1063 return len;
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (copy_from_user(f, &user_buffer[i], len))
1066 return -EFAULT;
1067 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001068 if (strcmp(f, "IPSRC_RND") == 0)
1069 pkt_dev->flags |= F_IPSRC_RND;
1070
1071 else if (strcmp(f, "!IPSRC_RND") == 0)
1072 pkt_dev->flags &= ~F_IPSRC_RND;
1073
1074 else if (strcmp(f, "TXSIZE_RND") == 0)
1075 pkt_dev->flags |= F_TXSIZE_RND;
1076
1077 else if (strcmp(f, "!TXSIZE_RND") == 0)
1078 pkt_dev->flags &= ~F_TXSIZE_RND;
1079
1080 else if (strcmp(f, "IPDST_RND") == 0)
1081 pkt_dev->flags |= F_IPDST_RND;
1082
1083 else if (strcmp(f, "!IPDST_RND") == 0)
1084 pkt_dev->flags &= ~F_IPDST_RND;
1085
1086 else if (strcmp(f, "UDPSRC_RND") == 0)
1087 pkt_dev->flags |= F_UDPSRC_RND;
1088
1089 else if (strcmp(f, "!UDPSRC_RND") == 0)
1090 pkt_dev->flags &= ~F_UDPSRC_RND;
1091
1092 else if (strcmp(f, "UDPDST_RND") == 0)
1093 pkt_dev->flags |= F_UDPDST_RND;
1094
1095 else if (strcmp(f, "!UDPDST_RND") == 0)
1096 pkt_dev->flags &= ~F_UDPDST_RND;
1097
1098 else if (strcmp(f, "MACSRC_RND") == 0)
1099 pkt_dev->flags |= F_MACSRC_RND;
1100
1101 else if (strcmp(f, "!MACSRC_RND") == 0)
1102 pkt_dev->flags &= ~F_MACSRC_RND;
1103
1104 else if (strcmp(f, "MACDST_RND") == 0)
1105 pkt_dev->flags |= F_MACDST_RND;
1106
1107 else if (strcmp(f, "!MACDST_RND") == 0)
1108 pkt_dev->flags &= ~F_MACDST_RND;
1109
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001110 else if (strcmp(f, "MPLS_RND") == 0)
1111 pkt_dev->flags |= F_MPLS_RND;
1112
1113 else if (strcmp(f, "!MPLS_RND") == 0)
1114 pkt_dev->flags &= ~F_MPLS_RND;
1115
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001116 else if (strcmp(f, "VID_RND") == 0)
1117 pkt_dev->flags |= F_VID_RND;
1118
1119 else if (strcmp(f, "!VID_RND") == 0)
1120 pkt_dev->flags &= ~F_VID_RND;
1121
1122 else if (strcmp(f, "SVID_RND") == 0)
1123 pkt_dev->flags |= F_SVID_RND;
1124
1125 else if (strcmp(f, "!SVID_RND") == 0)
1126 pkt_dev->flags &= ~F_SVID_RND;
1127
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001128 else if (strcmp(f, "FLOW_SEQ") == 0)
1129 pkt_dev->flags |= F_FLOW_SEQ;
1130
Robert Olsson45b270f2007-08-28 15:45:55 -07001131 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1132 pkt_dev->flags |= F_QUEUE_MAP_RND;
1133
1134 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1135 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001136#ifdef CONFIG_XFRM
1137 else if (strcmp(f, "IPSEC") == 0)
1138 pkt_dev->flags |= F_IPSEC_ON;
1139#endif
1140
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001141 else if (strcmp(f, "!IPV6") == 0)
1142 pkt_dev->flags &= ~F_IPV6;
1143
Luiz Capitulino222f1802006-03-20 22:16:13 -08001144 else {
1145 sprintf(pg_result,
1146 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1147 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001148 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001149 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001150 return count;
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1153 return count;
1154 }
1155 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1156 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001157 if (len < 0) {
1158 return len;
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Luiz Capitulino222f1802006-03-20 22:16:13 -08001161 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001163 buf[len] = 0;
1164 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1165 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1166 strncpy(pkt_dev->dst_min, buf, len);
1167 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1168 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1169 }
1170 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001171 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001172 pkt_dev->dst_min);
1173 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1175 return count;
1176 }
1177 if (!strcmp(name, "dst_max")) {
1178 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001179 if (len < 0) {
1180 return len;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Luiz Capitulino222f1802006-03-20 22:16:13 -08001183 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return -EFAULT;
1185
Luiz Capitulino222f1802006-03-20 22:16:13 -08001186 buf[len] = 0;
1187 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1188 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1189 strncpy(pkt_dev->dst_max, buf, len);
1190 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1191 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1192 }
1193 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001194 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001195 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 i += len;
1197 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1198 return count;
1199 }
1200 if (!strcmp(name, "dst6")) {
1201 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001202 if (len < 0)
1203 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 pkt_dev->flags |= F_IPV6;
1206
Luiz Capitulino222f1802006-03-20 22:16:13 -08001207 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001209 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001212 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1215
Luiz Capitulino222f1802006-03-20 22:16:13 -08001216 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001217 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Luiz Capitulino222f1802006-03-20 22:16:13 -08001219 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sprintf(pg_result, "OK: dst6=%s", buf);
1221 return count;
1222 }
1223 if (!strcmp(name, "dst6_min")) {
1224 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001225 if (len < 0)
1226 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 pkt_dev->flags |= F_IPV6;
1229
Luiz Capitulino222f1802006-03-20 22:16:13 -08001230 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001232 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001235 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Luiz Capitulino222f1802006-03-20 22:16:13 -08001237 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1238 &pkt_dev->min_in6_daddr);
1239 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001240 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Luiz Capitulino222f1802006-03-20 22:16:13 -08001242 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 sprintf(pg_result, "OK: dst6_min=%s", buf);
1244 return count;
1245 }
1246 if (!strcmp(name, "dst6_max")) {
1247 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001248 if (len < 0)
1249 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 pkt_dev->flags |= F_IPV6;
1252
Luiz Capitulino222f1802006-03-20 22:16:13 -08001253 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001255 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001258 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001261 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Luiz Capitulino222f1802006-03-20 22:16:13 -08001263 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 sprintf(pg_result, "OK: dst6_max=%s", buf);
1265 return count;
1266 }
1267 if (!strcmp(name, "src6")) {
1268 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001269 if (len < 0)
1270 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 pkt_dev->flags |= F_IPV6;
1273
Luiz Capitulino222f1802006-03-20 22:16:13 -08001274 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001276 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001279 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1282
Luiz Capitulino222f1802006-03-20 22:16:13 -08001283 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001284 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001285
1286 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 sprintf(pg_result, "OK: src6=%s", buf);
1288 return count;
1289 }
1290 if (!strcmp(name, "src_min")) {
1291 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001292 if (len < 0) {
1293 return len;
1294 }
1295 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001297 buf[len] = 0;
1298 if (strcmp(buf, pkt_dev->src_min) != 0) {
1299 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1300 strncpy(pkt_dev->src_min, buf, len);
1301 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1302 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1303 }
1304 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001305 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 i += len;
1308 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1309 return count;
1310 }
1311 if (!strcmp(name, "src_max")) {
1312 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001313 if (len < 0) {
1314 return len;
1315 }
1316 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001318 buf[len] = 0;
1319 if (strcmp(buf, pkt_dev->src_max) != 0) {
1320 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1321 strncpy(pkt_dev->src_max, buf, len);
1322 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1323 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1324 }
1325 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001326 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001327 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 i += len;
1329 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1330 return count;
1331 }
1332 if (!strcmp(name, "dst_mac")) {
1333 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001334 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001336 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001339 if (len < 0) {
1340 return len;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001343 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return -EFAULT;
1345 i += len;
1346
Luiz Capitulino222f1802006-03-20 22:16:13 -08001347 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (*v >= '0' && *v <= '9') {
1349 *m *= 16;
1350 *m += *v - '0';
1351 }
1352 if (*v >= 'A' && *v <= 'F') {
1353 *m *= 16;
1354 *m += *v - 'A' + 10;
1355 }
1356 if (*v >= 'a' && *v <= 'f') {
1357 *m *= 16;
1358 *m += *v - 'a' + 10;
1359 }
1360 if (*v == ':') {
1361 m++;
1362 *m = 0;
1363 }
1364 }
1365
1366 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001367 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1368 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 sprintf(pg_result, "OK: dstmac");
1371 return count;
1372 }
1373 if (!strcmp(name, "src_mac")) {
1374 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001375 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 unsigned char *m = pkt_dev->src_mac;
1377
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001378 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381 if (len < 0) {
1382 return len;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001385 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return -EFAULT;
1387 i += len;
1388
Luiz Capitulino222f1802006-03-20 22:16:13 -08001389 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (*v >= '0' && *v <= '9') {
1391 *m *= 16;
1392 *m += *v - '0';
1393 }
1394 if (*v >= 'A' && *v <= 'F') {
1395 *m *= 16;
1396 *m += *v - 'A' + 10;
1397 }
1398 if (*v >= 'a' && *v <= 'f') {
1399 *m *= 16;
1400 *m += *v - 'a' + 10;
1401 }
1402 if (*v == ':') {
1403 m++;
1404 *m = 0;
1405 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001408 /* Set up Src MAC */
1409 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1410 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1411
Luiz Capitulino222f1802006-03-20 22:16:13 -08001412 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return count;
1414 }
1415
Luiz Capitulino222f1802006-03-20 22:16:13 -08001416 if (!strcmp(name, "clear_counters")) {
1417 pktgen_clear_counters(pkt_dev);
1418 sprintf(pg_result, "OK: Clearing counters.\n");
1419 return count;
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 if (!strcmp(name, "flows")) {
1423 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001424 if (len < 0) {
1425 return len;
1426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 i += len;
1428 if (value > MAX_CFLOWS)
1429 value = MAX_CFLOWS;
1430
1431 pkt_dev->cflows = value;
1432 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1433 return count;
1434 }
1435
1436 if (!strcmp(name, "flowlen")) {
1437 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001438 if (len < 0) {
1439 return len;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 i += len;
1442 pkt_dev->lflow = value;
1443 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1444 return count;
1445 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001446
Robert Olsson45b270f2007-08-28 15:45:55 -07001447 if (!strcmp(name, "queue_map_min")) {
1448 len = num_arg(&user_buffer[i], 5, &value);
1449 if (len < 0) {
1450 return len;
1451 }
1452 i += len;
1453 pkt_dev->queue_map_min = value;
1454 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1455 return count;
1456 }
1457
1458 if (!strcmp(name, "queue_map_max")) {
1459 len = num_arg(&user_buffer[i], 5, &value);
1460 if (len < 0) {
1461 return len;
1462 }
1463 i += len;
1464 pkt_dev->queue_map_max = value;
1465 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1466 return count;
1467 }
1468
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001469 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001470 unsigned n, cnt;
1471
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001472 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001473 if (len < 0)
1474 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001475 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001476 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001477 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001478 cnt += sprintf(pg_result + cnt,
1479 "%08x%s", ntohl(pkt_dev->labels[n]),
1480 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001481
1482 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1483 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1484 pkt_dev->svlan_id = 0xffff;
1485
1486 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001487 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001488 }
1489 return count;
1490 }
1491
1492 if (!strcmp(name, "vlan_id")) {
1493 len = num_arg(&user_buffer[i], 4, &value);
1494 if (len < 0) {
1495 return len;
1496 }
1497 i += len;
1498 if (value <= 4095) {
1499 pkt_dev->vlan_id = value; /* turn on VLAN */
1500
1501 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001502 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001503
1504 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001505 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001506
1507 pkt_dev->nr_labels = 0; /* turn off MPLS */
1508 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1509 } else {
1510 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1511 pkt_dev->svlan_id = 0xffff;
1512
1513 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001514 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001515 }
1516 return count;
1517 }
1518
1519 if (!strcmp(name, "vlan_p")) {
1520 len = num_arg(&user_buffer[i], 1, &value);
1521 if (len < 0) {
1522 return len;
1523 }
1524 i += len;
1525 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1526 pkt_dev->vlan_p = value;
1527 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1528 } else {
1529 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1530 }
1531 return count;
1532 }
1533
1534 if (!strcmp(name, "vlan_cfi")) {
1535 len = num_arg(&user_buffer[i], 1, &value);
1536 if (len < 0) {
1537 return len;
1538 }
1539 i += len;
1540 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1541 pkt_dev->vlan_cfi = value;
1542 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1543 } else {
1544 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1545 }
1546 return count;
1547 }
1548
1549 if (!strcmp(name, "svlan_id")) {
1550 len = num_arg(&user_buffer[i], 4, &value);
1551 if (len < 0) {
1552 return len;
1553 }
1554 i += len;
1555 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1556 pkt_dev->svlan_id = value; /* turn on SVLAN */
1557
1558 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001559 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001560
1561 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001562 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001563
1564 pkt_dev->nr_labels = 0; /* turn off MPLS */
1565 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1566 } else {
1567 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1568 pkt_dev->svlan_id = 0xffff;
1569
1570 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001571 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001572 }
1573 return count;
1574 }
1575
1576 if (!strcmp(name, "svlan_p")) {
1577 len = num_arg(&user_buffer[i], 1, &value);
1578 if (len < 0) {
1579 return len;
1580 }
1581 i += len;
1582 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1583 pkt_dev->svlan_p = value;
1584 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1585 } else {
1586 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1587 }
1588 return count;
1589 }
1590
1591 if (!strcmp(name, "svlan_cfi")) {
1592 len = num_arg(&user_buffer[i], 1, &value);
1593 if (len < 0) {
1594 return len;
1595 }
1596 i += len;
1597 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1598 pkt_dev->svlan_cfi = value;
1599 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1600 } else {
1601 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1602 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001603 return count;
1604 }
1605
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001606 if (!strcmp(name, "tos")) {
1607 __u32 tmp_value = 0;
1608 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1609 if (len < 0) {
1610 return len;
1611 }
1612 i += len;
1613 if (len == 2) {
1614 pkt_dev->tos = tmp_value;
1615 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1616 } else {
1617 sprintf(pg_result, "ERROR: tos must be 00-ff");
1618 }
1619 return count;
1620 }
1621
1622 if (!strcmp(name, "traffic_class")) {
1623 __u32 tmp_value = 0;
1624 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1625 if (len < 0) {
1626 return len;
1627 }
1628 i += len;
1629 if (len == 2) {
1630 pkt_dev->traffic_class = tmp_value;
1631 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1632 } else {
1633 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1634 }
1635 return count;
1636 }
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1639 return -EINVAL;
1640}
1641
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001642static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001644 return single_open(file, pktgen_if_show, PDE(inode)->data);
1645}
1646
Arjan van de Ven9a321442007-02-12 00:55:35 -08001647static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001648 .owner = THIS_MODULE,
1649 .open = pktgen_if_open,
1650 .read = seq_read,
1651 .llseek = seq_lseek,
1652 .write = pktgen_if_write,
1653 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001654};
1655
1656static int pktgen_thread_show(struct seq_file *seq, void *v)
1657{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001658 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001659 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001661 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Luiz Capitulino222f1802006-03-20 22:16:13 -08001663 seq_printf(seq, "Running: ");
1664
1665 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001666 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001667 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001668 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Luiz Capitulino222f1802006-03-20 22:16:13 -08001670 seq_printf(seq, "\nStopped: ");
1671
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001672 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001673 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001674 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001677 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001679 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Luiz Capitulino222f1802006-03-20 22:16:13 -08001681 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001686static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001687 const char __user * user_buffer,
1688 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001690 struct seq_file *seq = (struct seq_file *)file->private_data;
1691 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 int i = 0, max, len, ret;
1693 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001694 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001697 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return -EINVAL;
1699 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001702 len = count_trail_chars(&user_buffer[i], max);
1703 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001704 return len;
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /* Read variable name */
1709
1710 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001711 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001712 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 memset(name, 0, sizeof(name));
1715 if (copy_from_user(name, &user_buffer[i], len))
1716 return -EFAULT;
1717 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001718
Luiz Capitulino222f1802006-03-20 22:16:13 -08001719 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001721 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001722 return len;
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 i += len;
1725
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001726 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001727 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1728 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Luiz Capitulino222f1802006-03-20 22:16:13 -08001730 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001731 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 ret = -EINVAL;
1733 goto out;
1734 }
1735
1736 pg_result = &(t->result[0]);
1737
Luiz Capitulino222f1802006-03-20 22:16:13 -08001738 if (!strcmp(name, "add_device")) {
1739 char f[32];
1740 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001742 if (len < 0) {
1743 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 goto out;
1745 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001746 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return -EFAULT;
1748 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001749 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001750 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001751 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001752 ret = count;
1753 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto out;
1755 }
1756
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001758 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001759 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001760 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001761 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001763 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 goto out;
1765 }
1766
Luiz Capitulino222f1802006-03-20 22:16:13 -08001767 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001768 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001769 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto out;
1771 }
1772
1773 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return ret;
1776}
1777
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001778static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001780 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Arjan van de Ven9a321442007-02-12 00:55:35 -08001783static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001784 .owner = THIS_MODULE,
1785 .open = pktgen_thread_open,
1786 .read = seq_read,
1787 .llseek = seq_lseek,
1788 .write = pktgen_thread_write,
1789 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001790};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001793static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 struct pktgen_thread *t;
1796 struct pktgen_dev *pkt_dev = NULL;
1797
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001798 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 pkt_dev = pktgen_find_dev(t, ifname);
1800 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001801 if (remove) {
1802 if_lock(t);
1803 pkt_dev->removal_mark = 1;
1804 t->control |= T_REMDEV;
1805 if_unlock(t);
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 break;
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001810 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001813/*
1814 * mark a device for removal
1815 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001816static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001819 const int max_tries = 10, msec_per_try = 125;
1820 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001821
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001822 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001823 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001824
Luiz Capitulino222f1802006-03-20 22:16:13 -08001825 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001826
1827 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001828 if (pkt_dev == NULL)
1829 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001830
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001831 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001832 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1833 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001834 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001835 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001836
1837 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001838 printk(KERN_ERR "pktgen_mark_device: timed out after "
1839 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001840 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001841 break;
1842 }
1843
1844 }
1845
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001846 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001847}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001848
Stephen Hemminger39df2322007-03-04 16:11:51 -08001849static void pktgen_change_name(struct net_device *dev)
1850{
1851 struct pktgen_thread *t;
1852
1853 list_for_each_entry(t, &pktgen_threads, th_list) {
1854 struct pktgen_dev *pkt_dev;
1855
1856 list_for_each_entry(pkt_dev, &t->if_list, list) {
1857 if (pkt_dev->odev != dev)
1858 continue;
1859
1860 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1861
1862 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1863 pg_proc_dir);
1864 if (!pkt_dev->entry)
1865 printk(KERN_ERR "pktgen: can't move proc "
1866 " entry for '%s'\n", dev->name);
1867 break;
1868 }
1869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Luiz Capitulino222f1802006-03-20 22:16:13 -08001872static int pktgen_device_event(struct notifier_block *unused,
1873 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001875 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001877 if (dev->nd_net != &init_net)
1878 return NOTIFY_DONE;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /* It is OK that we do not hold the group lock right now,
1881 * as we run under the RTNL lock.
1882 */
1883
1884 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001885 case NETDEV_CHANGENAME:
1886 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001890 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 return NOTIFY_DONE;
1895}
1896
1897/* Associate pktgen_dev with a device. */
1898
Stephen Hemminger39df2322007-03-04 16:11:51 -08001899static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001902 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (pkt_dev->odev) {
1906 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001907 pkt_dev->odev = NULL;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Eric W. Biederman881d9662007-09-17 11:56:21 -07001910 odev = dev_get_by_name(&init_net, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001912 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001913 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001917 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001918 err = -EINVAL;
1919 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001920 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001921 err = -ENETDOWN;
1922 } else {
1923 pkt_dev->odev = odev;
1924 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931/* Read pkt_dev from the interface and set up internal pktgen_dev
1932 * structure to have the right information to create/send packets
1933 */
1934static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1935{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001936 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001937 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1938 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001939 sprintf(pkt_dev->result,
1940 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1941 return;
1942 }
1943
1944 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001946 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001947 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Luiz Capitulino222f1802006-03-20 22:16:13 -08001949 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001950 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Luiz Capitulino222f1802006-03-20 22:16:13 -08001952 /* Set up pkt size */
1953 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1954
1955 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001957 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 * gets exported
1959 */
1960
1961#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08001962 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 struct inet6_dev *idev;
1964
Luiz Capitulino222f1802006-03-20 22:16:13 -08001965 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1966 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 set = 1;
1968 break;
1969 }
1970
Luiz Capitulino222f1802006-03-20 22:16:13 -08001971 if (!set) {
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /*
1974 * Use linklevel address if unconfigured.
1975 *
1976 * use ipv6_get_lladdr if/when it's get exported
1977 */
1978
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001979 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1981 struct inet6_ifaddr *ifp;
1982
1983 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001984 for (ifp = idev->addr_list; ifp;
1985 ifp = ifp->if_next) {
1986 if (ifp->scope == IFA_LINK
1987 && !(ifp->
1988 flags & IFA_F_TENTATIVE)) {
1989 ipv6_addr_copy(&pkt_dev->
1990 cur_in6_saddr,
1991 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 err = 0;
1993 break;
1994 }
1995 }
1996 read_unlock_bh(&idev->lock);
1997 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001998 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001999 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002000 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2001 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
2003#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002004 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 pkt_dev->saddr_min = 0;
2006 pkt_dev->saddr_max = 0;
2007 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002008
2009 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002012 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (in_dev) {
2014 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002015 pkt_dev->saddr_min =
2016 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 pkt_dev->saddr_max = pkt_dev->saddr_min;
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002021 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2023 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2024 }
2025
2026 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2027 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2028 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002029 /* Initialize current values. */
2030 pkt_dev->cur_dst_mac_offset = 0;
2031 pkt_dev->cur_src_mac_offset = 0;
2032 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2033 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2034 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2035 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 pkt_dev->nflows = 0;
2037}
2038
2039static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2040{
2041 __u64 start;
2042 __u64 now;
2043
2044 start = now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002046 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002047 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002048 schedule_timeout_interruptible(1);
2049 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (!pkt_dev->running)
2051 return;
2052 if (need_resched())
2053 schedule();
2054 }
2055
2056 now = getCurUs();
2057 }
2058
2059 pkt_dev->idle_acc += now - start;
2060}
2061
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002062static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2063{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002064 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002065 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2066 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2067 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2068}
2069
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002070static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2071{
2072
2073 if (pkt_dev->flows[flow].flags & F_INIT)
2074 return 1;
2075 else
2076 return 0;
2077}
2078
2079static inline int f_pick(struct pktgen_dev *pkt_dev)
2080{
2081 int flow = pkt_dev->curfl;
2082
2083 if (pkt_dev->flags & F_FLOW_SEQ) {
2084 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2085 /* reset time */
2086 pkt_dev->flows[flow].count = 0;
2087 pkt_dev->curfl += 1;
2088 if (pkt_dev->curfl >= pkt_dev->cflows)
2089 pkt_dev->curfl = 0; /*reset */
2090 }
2091 } else {
2092 flow = random32() % pkt_dev->cflows;
2093
2094 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2095 pkt_dev->flows[flow].count = 0;
2096 }
2097
2098 return pkt_dev->curfl;
2099}
2100
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002101
2102#ifdef CONFIG_XFRM
2103/* If there was already an IPSEC SA, we keep it as is, else
2104 * we go look for it ...
2105*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002106static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002107{
2108 struct xfrm_state *x = pkt_dev->flows[flow].x;
2109 if (!x) {
2110 /*slow path: we dont already have xfrm_state*/
2111 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2112 (xfrm_address_t *)&pkt_dev->cur_saddr,
2113 AF_INET,
2114 pkt_dev->ipsmode,
2115 pkt_dev->ipsproto, 0);
2116 if (x) {
2117 pkt_dev->flows[flow].x = x;
2118 set_pkt_overhead(pkt_dev);
2119 pkt_dev->pkt_overhead+=x->props.header_len;
2120 }
2121
2122 }
2123}
2124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125/* Increment/randomize headers according to flags and current values
2126 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2127 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002128static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2129{
2130 __u32 imn;
2131 __u32 imx;
2132 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002134 if (pkt_dev->cflows)
2135 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002138 if (pkt_dev->src_mac_count > 1) {
2139 __u32 mc;
2140 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Luiz Capitulino222f1802006-03-20 22:16:13 -08002142 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002143 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002144 else {
2145 mc = pkt_dev->cur_src_mac_offset++;
2146 if (pkt_dev->cur_src_mac_offset >
2147 pkt_dev->src_mac_count)
2148 pkt_dev->cur_src_mac_offset = 0;
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Luiz Capitulino222f1802006-03-20 22:16:13 -08002151 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2152 pkt_dev->hh[11] = tmp;
2153 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2154 pkt_dev->hh[10] = tmp;
2155 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2156 pkt_dev->hh[9] = tmp;
2157 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2158 pkt_dev->hh[8] = tmp;
2159 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2160 pkt_dev->hh[7] = tmp;
2161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Luiz Capitulino222f1802006-03-20 22:16:13 -08002163 /* Deal with Destination MAC */
2164 if (pkt_dev->dst_mac_count > 1) {
2165 __u32 mc;
2166 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Luiz Capitulino222f1802006-03-20 22:16:13 -08002168 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002169 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Luiz Capitulino222f1802006-03-20 22:16:13 -08002171 else {
2172 mc = pkt_dev->cur_dst_mac_offset++;
2173 if (pkt_dev->cur_dst_mac_offset >
2174 pkt_dev->dst_mac_count) {
2175 pkt_dev->cur_dst_mac_offset = 0;
2176 }
2177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Luiz Capitulino222f1802006-03-20 22:16:13 -08002179 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2180 pkt_dev->hh[5] = tmp;
2181 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2182 pkt_dev->hh[4] = tmp;
2183 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2184 pkt_dev->hh[3] = tmp;
2185 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2186 pkt_dev->hh[2] = tmp;
2187 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2188 pkt_dev->hh[1] = tmp;
2189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002191 if (pkt_dev->flags & F_MPLS_RND) {
2192 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002193 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002194 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2195 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002196 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002197 htonl(0x000fffff));
2198 }
2199
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002200 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002201 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002202 }
2203
2204 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002205 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002206 }
2207
Luiz Capitulino222f1802006-03-20 22:16:13 -08002208 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2209 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002210 pkt_dev->cur_udp_src = random32() %
2211 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2212 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Luiz Capitulino222f1802006-03-20 22:16:13 -08002214 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 pkt_dev->cur_udp_src++;
2216 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2217 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002218 }
2219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Luiz Capitulino222f1802006-03-20 22:16:13 -08002221 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2222 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002223 pkt_dev->cur_udp_dst = random32() %
2224 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2225 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002226 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002228 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002230 }
2231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 if (!(pkt_dev->flags & F_IPV6)) {
2234
Luiz Capitulino222f1802006-03-20 22:16:13 -08002235 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2236 ntohl(pkt_dev->
2237 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002239 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002240 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 else {
2242 t = ntohl(pkt_dev->cur_saddr);
2243 t++;
2244 if (t > imx) {
2245 t = imn;
2246 }
2247 }
2248 pkt_dev->cur_saddr = htonl(t);
2249 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002250
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002251 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2253 } else {
Al Viro252e3342006-11-14 20:48:11 -08002254 imn = ntohl(pkt_dev->daddr_min);
2255 imx = ntohl(pkt_dev->daddr_max);
2256 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002258 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (pkt_dev->flags & F_IPDST_RND) {
2260
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002261 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002262 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Joe Perches21cf2252007-12-16 13:44:00 -08002264 while (ipv4_is_loopback(s) ||
2265 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002266 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002267 ipv4_is_zeronet(s) ||
2268 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002269 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002270 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Al Viro252e3342006-11-14 20:48:11 -08002272 pkt_dev->cur_daddr = s;
2273 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 t = ntohl(pkt_dev->cur_daddr);
2275 t++;
2276 if (t > imx) {
2277 t = imn;
2278 }
2279 pkt_dev->cur_daddr = htonl(t);
2280 }
2281 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002282 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002283 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002284 pkt_dev->flows[flow].cur_daddr =
2285 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002286#ifdef CONFIG_XFRM
2287 if (pkt_dev->flags & F_IPSEC_ON)
2288 get_ipsec_sa(pkt_dev, flow);
2289#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 pkt_dev->nflows++;
2291 }
2292 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002293 } else { /* IPV6 * */
2294
2295 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2296 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2297 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2298 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 else {
2300 int i;
2301
2302 /* Only random destinations yet */
2303
Luiz Capitulino222f1802006-03-20 22:16:13 -08002304 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002306 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2308 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312
Luiz Capitulino222f1802006-03-20 22:16:13 -08002313 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2314 __u32 t;
2315 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002316 t = random32() %
2317 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2318 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002319 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002321 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 }
2324 pkt_dev->cur_pkt_size = t;
2325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Robert Olsson45b270f2007-08-28 15:45:55 -07002327 if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2328 __u16 t;
2329 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2330 t = random32() %
2331 (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1)
2332 + pkt_dev->queue_map_min;
2333 } else {
2334 t = pkt_dev->cur_queue_map + 1;
2335 if (t > pkt_dev->queue_map_max)
2336 t = pkt_dev->queue_map_min;
2337 }
2338 pkt_dev->cur_queue_map = t;
2339 }
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 pkt_dev->flows[flow].count++;
2342}
2343
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002344
2345#ifdef CONFIG_XFRM
2346static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2347{
2348 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2349 int err = 0;
2350 struct iphdr *iph;
2351
2352 if (!x)
2353 return 0;
2354 /* XXX: we dont support tunnel mode for now until
2355 * we resolve the dst issue */
2356 if (x->props.mode != XFRM_MODE_TRANSPORT)
2357 return 0;
2358
2359 spin_lock(&x->lock);
2360 iph = ip_hdr(skb);
2361
Herbert Xu13996372007-10-17 21:35:51 -07002362 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002363 if (err)
2364 goto error;
2365 err = x->type->output(x, skb);
2366 if (err)
2367 goto error;
2368
2369 x->curlft.bytes +=skb->len;
2370 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002371error:
2372 spin_unlock(&x->lock);
2373 return err;
2374}
2375
2376static inline void free_SAs(struct pktgen_dev *pkt_dev)
2377{
2378 if (pkt_dev->cflows) {
2379 /* let go of the SAs if we have them */
2380 int i = 0;
2381 for (; i < pkt_dev->nflows; i++){
2382 struct xfrm_state *x = pkt_dev->flows[i].x;
2383 if (x) {
2384 xfrm_state_put(x);
2385 pkt_dev->flows[i].x = NULL;
2386 }
2387 }
2388 }
2389}
2390
2391static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2392 struct sk_buff *skb, __be16 protocol)
2393{
2394 if (pkt_dev->flags & F_IPSEC_ON) {
2395 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2396 int nhead = 0;
2397 if (x) {
2398 int ret;
2399 __u8 *eth;
2400 nhead = x->props.header_len - skb_headroom(skb);
2401 if (nhead >0) {
2402 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2403 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002404 printk(KERN_ERR "Error expanding "
2405 "ipsec packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002406 return 0;
2407 }
2408 }
2409
2410 /* ipsec is not expecting ll header */
2411 skb_pull(skb, ETH_HLEN);
2412 ret = pktgen_output_ipsec(skb, pkt_dev);
2413 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002414 printk(KERN_ERR "Error creating ipsec "
2415 "packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002416 kfree_skb(skb);
2417 return 0;
2418 }
2419 /* restore ll */
2420 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2421 memcpy(eth, pkt_dev->hh, 12);
2422 *(u16 *) & eth[12] = protocol;
2423 }
2424 }
2425 return 1;
2426}
2427#endif
2428
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002429static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2430{
2431 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002432 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002433 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2434 }
2435 mpls--;
2436 *mpls |= MPLS_STACK_BOTTOM;
2437}
2438
Al Viro0f37c602006-11-03 03:49:56 -08002439static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2440 unsigned int prio)
2441{
2442 return htons(id | (cfi << 12) | (prio << 13));
2443}
2444
Luiz Capitulino222f1802006-03-20 22:16:13 -08002445static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2446 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
2448 struct sk_buff *skb = NULL;
2449 __u8 *eth;
2450 struct udphdr *udph;
2451 int datalen, iplen;
2452 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002453 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002454 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002455 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002456 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2457 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2458 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2459 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2460
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002461
2462 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002463 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002464
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002465 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002466 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002467
Robert Olsson64053be2005-06-26 15:27:10 -07002468 /* Update any of the values, used when we're incrementing various
2469 * fields.
2470 */
2471 mod_cur_headers(pkt_dev);
2472
David S. Miller7ac54592006-01-18 14:19:10 -08002473 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002474 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002475 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (!skb) {
2477 sprintf(pkt_dev->result, "No memory");
2478 return NULL;
2479 }
2480
David S. Miller7ac54592006-01-18 14:19:10 -08002481 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 /* Reserve for ethernet and IP header */
2484 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002485 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2486 if (pkt_dev->nr_labels)
2487 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002488
2489 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002490 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002491 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002492 *svlan_tci = build_tci(pkt_dev->svlan_id,
2493 pkt_dev->svlan_cfi,
2494 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002495 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002496 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002497 }
2498 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002499 *vlan_tci = build_tci(pkt_dev->vlan_id,
2500 pkt_dev->vlan_cfi,
2501 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002502 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002503 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002504 }
2505
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002506 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002507 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002508 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
Pavel Emelyanovdfa40912007-10-21 16:57:55 -07002509 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002510 iph = ip_hdr(skb);
2511 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002514 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002516 /* Eth + IPh + UDPh + mpls */
2517 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002518 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002519 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 udph->source = htons(pkt_dev->cur_udp_src);
2523 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002524 udph->len = htons(datalen + 8); /* DATA + udphdr */
2525 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 iph->ihl = 5;
2528 iph->version = 4;
2529 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002530 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002531 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 iph->saddr = pkt_dev->cur_saddr;
2533 iph->daddr = pkt_dev->cur_daddr;
2534 iph->frag_off = 0;
2535 iplen = 20 + 8 + datalen;
2536 iph->tot_len = htons(iplen);
2537 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002538 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002539 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002540 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002541 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 skb->dev = odev;
2543 skb->pkt_type = PACKET_HOST;
2544
Luiz Capitulino222f1802006-03-20 22:16:13 -08002545 if (pkt_dev->nfrags <= 0)
2546 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 else {
2548 int frags = pkt_dev->nfrags;
2549 int i;
2550
Luiz Capitulino222f1802006-03-20 22:16:13 -08002551 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (frags > MAX_SKB_FRAGS)
2554 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002555 if (datalen > frags * PAGE_SIZE) {
2556 skb_put(skb, datalen - frags * PAGE_SIZE);
2557 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 }
2559
2560 i = 0;
2561 while (datalen > 0) {
2562 struct page *page = alloc_pages(GFP_KERNEL, 0);
2563 skb_shinfo(skb)->frags[i].page = page;
2564 skb_shinfo(skb)->frags[i].page_offset = 0;
2565 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002566 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 datalen -= skb_shinfo(skb)->frags[i].size;
2568 skb->len += skb_shinfo(skb)->frags[i].size;
2569 skb->data_len += skb_shinfo(skb)->frags[i].size;
2570 i++;
2571 skb_shinfo(skb)->nr_frags = i;
2572 }
2573
2574 while (i < frags) {
2575 int rem;
2576
2577 if (i == 0)
2578 break;
2579
2580 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2581 if (rem == 0)
2582 break;
2583
2584 skb_shinfo(skb)->frags[i - 1].size -= rem;
2585
Luiz Capitulino222f1802006-03-20 22:16:13 -08002586 skb_shinfo(skb)->frags[i] =
2587 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002589 skb_shinfo(skb)->frags[i].page =
2590 skb_shinfo(skb)->frags[i - 1].page;
2591 skb_shinfo(skb)->frags[i].page_offset +=
2592 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 skb_shinfo(skb)->frags[i].size = rem;
2594 i++;
2595 skb_shinfo(skb)->nr_frags = i;
2596 }
2597 }
2598
Luiz Capitulino222f1802006-03-20 22:16:13 -08002599 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Luiz Capitulino222f1802006-03-20 22:16:13 -08002601 if (pgh) {
2602 struct timeval timestamp;
2603
2604 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2605 pgh->seq_num = htonl(pkt_dev->seq_num);
2606
2607 do_gettimeofday(&timestamp);
2608 pgh->tv_sec = htonl(timestamp.tv_sec);
2609 pgh->tv_usec = htonl(timestamp.tv_usec);
2610 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002611
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002612#ifdef CONFIG_XFRM
2613 if (!process_ipsec(pkt_dev, skb, protocol))
2614 return NULL;
2615#endif
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return skb;
2618}
2619
2620/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002621 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2623 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002624 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 * Should be candidate for net/ipv4/utils.c
2626 * --ro
2627 */
2628
Luiz Capitulino222f1802006-03-20 22:16:13 -08002629static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002632 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 unsigned long u;
2634 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002635 unsigned int prefixlen = 0;
2636 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002637 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002638 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Luiz Capitulino222f1802006-03-20 22:16:13 -08002640 for (i = 0; i < 16; i++)
2641 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 for (;;) {
2644 if (*s == ':') {
2645 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002646 if (s[1] == ':') { /* Found "::", skip to part 2 */
2647 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 len++;
2649 break;
2650 }
2651 s++;
2652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002654 u = simple_strtoul(s, &pos, 16);
2655 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002656 if (!i)
2657 return 0;
2658 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 /* the last 4 bytes may be written as IPv4 address */
2661
2662 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002663 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2664 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666 ip[prefixlen++] = (u >> 8);
2667 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002668 s += i;
2669 len += i;
2670 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return len;
2672 }
2673
2674/* part 2, after "::" */
2675 for (;;) {
2676 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002677 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 break;
2679 s++;
2680 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002681 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002683
2684 u = simple_strtol(s, &pos, 16);
2685 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002687 if (*s)
2688 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 break;
2690 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002691 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002693 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2694 sizeof(tmp));
2695 suffixlen += 4;
2696 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 break;
2698 }
2699 suffix[suffixlen++] = (u >> 8);
2700 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002701 s += i;
2702 len += i;
2703 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 break;
2705 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002706 for (i = 0; i < suffixlen; i++)
2707 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return len;
2709}
2710
Luiz Capitulino222f1802006-03-20 22:16:13 -08002711static char tohex(char hexdigit)
2712{
2713 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Luiz Capitulino222f1802006-03-20 22:16:13 -08002716static int fmt_xlong(char *s, unsigned int i)
2717{
2718 char *bak = s;
2719 *s = tohex((i >> 12) & 0xf);
2720 if (s != bak || *s != '0')
2721 ++s;
2722 *s = tohex((i >> 8) & 0xf);
2723 if (s != bak || *s != '0')
2724 ++s;
2725 *s = tohex((i >> 4) & 0xf);
2726 if (s != bak || *s != '0')
2727 ++s;
2728 *s = tohex(i & 0xf);
2729 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Luiz Capitulino222f1802006-03-20 22:16:13 -08002732static unsigned int fmt_ip6(char *s, const char ip[16])
2733{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 unsigned int len;
2735 unsigned int i;
2736 unsigned int temp;
2737 unsigned int compressing;
2738 int j;
2739
Luiz Capitulino222f1802006-03-20 22:16:13 -08002740 len = 0;
2741 compressing = 0;
2742 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002745 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2746 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2747 temp = strlen(s);
2748 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
2750#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002751 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2752 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (temp == 0) {
2754 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002755 compressing = 1;
2756 if (j == 0) {
2757 *s++ = ':';
2758 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
2760 }
2761 } else {
2762 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002763 compressing = 0;
2764 *s++ = ':';
2765 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002767 i = fmt_xlong(s, temp);
2768 len += i;
2769 s += i;
2770 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 *s++ = ':';
2772 ++len;
2773 }
2774 }
2775 }
2776 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002777 *s++ = ':';
2778 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002780 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return len;
2782}
2783
Luiz Capitulino222f1802006-03-20 22:16:13 -08002784static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2785 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
2787 struct sk_buff *skb = NULL;
2788 __u8 *eth;
2789 struct udphdr *udph;
2790 int datalen;
2791 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002792 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002793 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002794 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002795 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2796 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2797 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2798 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002799
2800 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002801 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002802
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002803 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002804 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002805
Robert Olsson64053be2005-06-26 15:27:10 -07002806 /* Update any of the values, used when we're incrementing various
2807 * fields.
2808 */
2809 mod_cur_headers(pkt_dev);
2810
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002811 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002812 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (!skb) {
2814 sprintf(pkt_dev->result, "No memory");
2815 return NULL;
2816 }
2817
2818 skb_reserve(skb, 16);
2819
2820 /* Reserve for ethernet and IP header */
2821 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002822 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2823 if (pkt_dev->nr_labels)
2824 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002825
2826 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002827 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002828 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002829 *svlan_tci = build_tci(pkt_dev->svlan_id,
2830 pkt_dev->svlan_cfi,
2831 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002832 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002833 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002834 }
2835 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002836 *vlan_tci = build_tci(pkt_dev->vlan_id,
2837 pkt_dev->vlan_cfi,
2838 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002839 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002840 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002841 }
2842
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002843 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002844 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002845 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
Pavel Emelyanovdfa40912007-10-21 16:57:55 -07002846 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002847 iph = ipv6_hdr(skb);
2848 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002851 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002852
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002853 /* Eth + IPh + UDPh + mpls */
2854 datalen = pkt_dev->cur_pkt_size - 14 -
2855 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002856 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Luiz Capitulino222f1802006-03-20 22:16:13 -08002858 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 datalen = sizeof(struct pktgen_hdr);
2860 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002861 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2862 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
2864
2865 udph->source = htons(pkt_dev->cur_udp_src);
2866 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002867 udph->len = htons(datalen + sizeof(struct udphdr));
2868 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002870 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002872 if (pkt_dev->traffic_class) {
2873 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002874 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002875 }
2876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 iph->hop_limit = 32;
2878
2879 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2880 iph->nexthdr = IPPROTO_UDP;
2881
2882 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2883 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2884
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002885 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002886 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002887 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 skb->dev = odev;
2889 skb->pkt_type = PACKET_HOST;
2890
Luiz Capitulino222f1802006-03-20 22:16:13 -08002891 if (pkt_dev->nfrags <= 0)
2892 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 else {
2894 int frags = pkt_dev->nfrags;
2895 int i;
2896
Luiz Capitulino222f1802006-03-20 22:16:13 -08002897 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 if (frags > MAX_SKB_FRAGS)
2900 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002901 if (datalen > frags * PAGE_SIZE) {
2902 skb_put(skb, datalen - frags * PAGE_SIZE);
2903 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905
2906 i = 0;
2907 while (datalen > 0) {
2908 struct page *page = alloc_pages(GFP_KERNEL, 0);
2909 skb_shinfo(skb)->frags[i].page = page;
2910 skb_shinfo(skb)->frags[i].page_offset = 0;
2911 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002912 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 datalen -= skb_shinfo(skb)->frags[i].size;
2914 skb->len += skb_shinfo(skb)->frags[i].size;
2915 skb->data_len += skb_shinfo(skb)->frags[i].size;
2916 i++;
2917 skb_shinfo(skb)->nr_frags = i;
2918 }
2919
2920 while (i < frags) {
2921 int rem;
2922
2923 if (i == 0)
2924 break;
2925
2926 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2927 if (rem == 0)
2928 break;
2929
2930 skb_shinfo(skb)->frags[i - 1].size -= rem;
2931
Luiz Capitulino222f1802006-03-20 22:16:13 -08002932 skb_shinfo(skb)->frags[i] =
2933 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002935 skb_shinfo(skb)->frags[i].page =
2936 skb_shinfo(skb)->frags[i - 1].page;
2937 skb_shinfo(skb)->frags[i].page_offset +=
2938 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 skb_shinfo(skb)->frags[i].size = rem;
2940 i++;
2941 skb_shinfo(skb)->nr_frags = i;
2942 }
2943 }
2944
Luiz Capitulino222f1802006-03-20 22:16:13 -08002945 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002947 if (pgh) {
2948 struct timeval timestamp;
2949
2950 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2951 pgh->seq_num = htonl(pkt_dev->seq_num);
2952
2953 do_gettimeofday(&timestamp);
2954 pgh->tv_sec = htonl(timestamp.tv_sec);
2955 pgh->tv_usec = htonl(timestamp.tv_usec);
2956 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002957 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return skb;
2960}
2961
Luiz Capitulino222f1802006-03-20 22:16:13 -08002962static inline struct sk_buff *fill_packet(struct net_device *odev,
2963 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002965 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return fill_packet_ipv6(odev, pkt_dev);
2967 else
2968 return fill_packet_ipv4(odev, pkt_dev);
2969}
2970
Luiz Capitulino222f1802006-03-20 22:16:13 -08002971static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002973 pkt_dev->seq_num = 1;
2974 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002976 pkt_dev->tx_bytes = 0;
2977 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980/* Set up structure for sending pkts, clear counters */
2981
2982static void pktgen_run(struct pktgen_thread *t)
2983{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002984 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 int started = 0;
2986
Stephen Hemminger25c4e532007-03-04 16:06:47 -08002987 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002990 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 /*
2993 * setup odev and create initial packet.
2994 */
2995 pktgen_setup_inject(pkt_dev);
2996
Luiz Capitulino222f1802006-03-20 22:16:13 -08002997 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002999 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 pkt_dev->skb = NULL;
3001 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003002 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 pkt_dev->next_tx_ns = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003004 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 strcpy(pkt_dev->result, "Starting");
3007 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003008 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 strcpy(pkt_dev->result, "Error starting");
3010 }
3011 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003012 if (started)
3013 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
3016static void pktgen_stop_all_threads_ifs(void)
3017{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003018 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003020 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003022 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003023
3024 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003025 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003026
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003027 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
Luiz Capitulino222f1802006-03-20 22:16:13 -08003030static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003032 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003033 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003035 list_for_each_entry(pkt_dev, &t->if_list, list)
3036 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 res = 1;
3038 break;
3039 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003040 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Luiz Capitulino222f1802006-03-20 22:16:13 -08003043static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003045 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Luiz Capitulino222f1802006-03-20 22:16:13 -08003047 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Luiz Capitulino222f1802006-03-20 22:16:13 -08003049 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Luiz Capitulino222f1802006-03-20 22:16:13 -08003051 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Luiz Capitulino222f1802006-03-20 22:16:13 -08003053 if (signal_pending(current))
3054 goto signal;
3055 if_lock(t);
3056 }
3057 if_unlock(t);
3058 return 1;
3059signal:
3060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
3063static int pktgen_wait_all_threads_run(void)
3064{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003065 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003068 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003069
3070 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003072 if (sig == 0)
3073 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003075
3076 if (sig == 0)
3077 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003079
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003080 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return sig;
3082}
3083
3084static void pktgen_run_all_threads(void)
3085{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003086 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003088 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003090 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003092 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003094
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003095 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Luiz Capitulino222f1802006-03-20 22:16:13 -08003097 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 pktgen_wait_all_threads_run();
3100}
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3103{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003104 __u64 total_us, bps, mbps, pps, idle;
3105 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Luiz Capitulino222f1802006-03-20 22:16:13 -08003107 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Luiz Capitulino222f1802006-03-20 22:16:13 -08003109 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Luiz Capitulino222f1802006-03-20 22:16:13 -08003111 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3112 (unsigned long long)total_us,
3113 (unsigned long long)(total_us - idle),
3114 (unsigned long long)idle,
3115 (unsigned long long)pkt_dev->sofar,
3116 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Luiz Capitulino222f1802006-03-20 22:16:13 -08003118 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Luiz Capitulino222f1802006-03-20 22:16:13 -08003120 while ((total_us >> 32) != 0) {
3121 pps >>= 1;
3122 total_us >>= 1;
3123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Luiz Capitulino222f1802006-03-20 22:16:13 -08003125 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Luiz Capitulino222f1802006-03-20 22:16:13 -08003127 bps = pps * 8 * pkt_dev->cur_pkt_size;
3128
3129 mbps = bps;
3130 do_div(mbps, 1000000);
3131 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3132 (unsigned long long)pps,
3133 (unsigned long long)mbps,
3134 (unsigned long long)bps,
3135 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138/* Set stopped-at timer, remove from running list, do counters & statistics */
3139
Luiz Capitulino222f1802006-03-20 22:16:13 -08003140static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003142 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003143
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003145 printk(KERN_WARNING "pktgen: interface: %s is already "
3146 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003147 return -EINVAL;
3148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Luiz Capitulino222f1802006-03-20 22:16:13 -08003150 pkt_dev->stopped_at = getCurUs();
3151 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003153 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Luiz Capitulino222f1802006-03-20 22:16:13 -08003155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003160 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if_lock(t);
3163
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003164 list_for_each_entry(pkt_dev, &t->if_list, list) {
3165 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 continue;
3167 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003168 best = pkt_dev;
3169 else if (pkt_dev->next_tx_us < best->next_tx_us)
3170 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
3172 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003173 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
Luiz Capitulino222f1802006-03-20 22:16:13 -08003176static void pktgen_stop(struct pktgen_thread *t)
3177{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003178 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003180 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Luiz Capitulino222f1802006-03-20 22:16:13 -08003182 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003184 list_for_each_entry(pkt_dev, &t->if_list, list) {
3185 pktgen_stop_device(pkt_dev);
3186 if (pkt_dev->skb)
3187 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003188
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003189 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Luiz Capitulino222f1802006-03-20 22:16:13 -08003192 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003195/*
3196 * one of our devices needs to be removed - find it
3197 * and remove it
3198 */
3199static void pktgen_rem_one_if(struct pktgen_thread *t)
3200{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003201 struct list_head *q, *n;
3202 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003203
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003204 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003205
3206 if_lock(t);
3207
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003208 list_for_each_safe(q, n, &t->if_list) {
3209 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003210
Luiz Capitulino222f1802006-03-20 22:16:13 -08003211 if (!cur->removal_mark)
3212 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003213
3214 if (cur->skb)
3215 kfree_skb(cur->skb);
3216 cur->skb = NULL;
3217
3218 pktgen_remove_device(t, cur);
3219
3220 break;
3221 }
3222
3223 if_unlock(t);
3224}
3225
Luiz Capitulino222f1802006-03-20 22:16:13 -08003226static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003228 struct list_head *q, *n;
3229 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003230
3231 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003232
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003233 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003234 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003236 list_for_each_safe(q, n, &t->if_list) {
3237 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003238
3239 if (cur->skb)
3240 kfree_skb(cur->skb);
3241 cur->skb = NULL;
3242
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 pktgen_remove_device(t, cur);
3244 }
3245
Luiz Capitulino222f1802006-03-20 22:16:13 -08003246 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Luiz Capitulino222f1802006-03-20 22:16:13 -08003249static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003251 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
David S. Milleree74baa2007-01-01 20:51:53 -08003253 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003255 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003257 list_del(&t->th_list);
3258
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003259 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
3262static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3263{
3264 struct net_device *odev = NULL;
3265 __u64 idle_start = 0;
3266 int ret;
3267
3268 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3271 u64 now;
3272
3273 now = getCurUs();
3274 if (now < pkt_dev->next_tx_us)
3275 spin(pkt_dev, pkt_dev->next_tx_us);
3276
3277 /* This is max DELAY, this has special meaning of
3278 * "never transmit"
3279 */
3280 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3281 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3282 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3283 goto out;
3284 }
3285 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003286
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003287 if ((netif_queue_stopped(odev) ||
Robert Olsson378be2c2007-08-28 15:43:14 -07003288 (pkt_dev->skb &&
Pavel Emelyanov668f8952007-10-21 17:01:56 -07003289 netif_subqueue_stopped(odev, pkt_dev->skb))) ||
Robert Olsson378be2c2007-08-28 15:43:14 -07003290 need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 if (!netif_running(odev)) {
3294 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003295 if (pkt_dev->skb)
3296 kfree_skb(pkt_dev->skb);
3297 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 goto out;
3299 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003300 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003304
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003305 if (netif_queue_stopped(odev) ||
Pavel Emelyanov668f8952007-10-21 17:01:56 -07003306 netif_subqueue_stopped(odev, pkt_dev->skb)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003307 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003309 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
3311 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003314 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3315 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003317 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 pkt_dev->skb = fill_packet(odev, pkt_dev);
3321 if (pkt_dev->skb == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003322 printk(KERN_ERR "pktgen: ERROR: couldn't "
3323 "allocate skb in fill_packet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003325 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 goto out;
3327 }
3328 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003329 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
3331 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003332
Herbert Xu932ff272006-06-09 12:20:56 -07003333 netif_tx_lock_bh(odev);
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003334 if (!netif_queue_stopped(odev) &&
Pavel Emelyanov668f8952007-10-21 17:01:56 -07003335 !netif_subqueue_stopped(odev, pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003338 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3340 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003341 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 pkt_dev->sofar++;
3343 pkt_dev->seq_num++;
3344 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003345
3346 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 && (odev->features & NETIF_F_LLTX)) {
3348 cpu_relax();
3349 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003350 } else { /* Retry it next time */
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (debug && net_ratelimit())
3355 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 pkt_dev->errors++;
3358 pkt_dev->last_ok = 0;
3359 }
3360
3361 pkt_dev->next_tx_us = getCurUs();
3362 pkt_dev->next_tx_ns = 0;
3363
3364 pkt_dev->next_tx_us += pkt_dev->delay_us;
3365 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3366
3367 if (pkt_dev->next_tx_ns > 1000) {
3368 pkt_dev->next_tx_us++;
3369 pkt_dev->next_tx_ns -= 1000;
3370 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Luiz Capitulino222f1802006-03-20 22:16:13 -08003373 else { /* Retry it next time */
3374 pkt_dev->last_ok = 0;
3375 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Herbert Xu932ff272006-06-09 12:20:56 -07003379 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 /* If pkt_dev->count is zero, then run forever */
3382 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3383 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3384 idle_start = getCurUs();
3385 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3386 if (signal_pending(current)) {
3387 break;
3388 }
3389 schedule();
3390 }
3391 pkt_dev->idle_acc += getCurUs() - idle_start;
3392 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 /* Done with this */
3395 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003396 if (pkt_dev->skb)
3397 kfree_skb(pkt_dev->skb);
3398 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399 }
3400out:;
3401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003403/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 * Main loop of the thread goes here
3405 */
3406
David S. Milleree74baa2007-01-01 20:51:53 -08003407static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
3409 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003410 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003411 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
David S. Milleree74baa2007-01-01 20:51:53 -08003414 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416 init_waitqueue_head(&t->queue);
3417
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003418 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
David S. Milleree74baa2007-01-01 20:51:53 -08003420 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003421
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003422 set_freezable();
3423
David S. Milleree74baa2007-01-01 20:51:53 -08003424 while (!kthread_should_stop()) {
3425 pkt_dev = next_to_run(t);
3426
3427 if (!pkt_dev &&
3428 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3429 == 0) {
3430 prepare_to_wait(&(t->queue), &wait,
3431 TASK_INTERRUPTIBLE);
3432 schedule_timeout(HZ / 10);
3433 finish_wait(&(t->queue), &wait);
3434 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 __set_current_state(TASK_RUNNING);
3437
Robert Olssonb1639112007-08-28 15:46:58 -07003438 if (pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 pktgen_xmit(pkt_dev);
3440
Luiz Capitulino222f1802006-03-20 22:16:13 -08003441 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 pktgen_stop(t);
3443 t->control &= ~(T_STOP);
3444 }
3445
Luiz Capitulino222f1802006-03-20 22:16:13 -08003446 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 pktgen_run(t);
3448 t->control &= ~(T_RUN);
3449 }
3450
Luiz Capitulino222f1802006-03-20 22:16:13 -08003451 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003453 t->control &= ~(T_REMDEVALL);
3454 }
3455
Luiz Capitulino222f1802006-03-20 22:16:13 -08003456 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003457 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 t->control &= ~(T_REMDEV);
3459 }
3460
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003461 try_to_freeze();
3462
David S. Milleree74baa2007-01-01 20:51:53 -08003463 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003466 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003467 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003469 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003470 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003472 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003473 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003474
David S. Milleree74baa2007-01-01 20:51:53 -08003475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Luiz Capitulino222f1802006-03-20 22:16:13 -08003478static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3479 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003481 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003482 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003484 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003485 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003486 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003487 break;
3488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Luiz Capitulino222f1802006-03-20 22:16:13 -08003490 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003491 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003492 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493}
3494
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003495/*
3496 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 */
3498
Luiz Capitulino222f1802006-03-20 22:16:13 -08003499static int add_dev_to_thread(struct pktgen_thread *t,
3500 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
3502 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Luiz Capitulino222f1802006-03-20 22:16:13 -08003504 if_lock(t);
3505
3506 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003507 printk(KERN_ERR "pktgen: ERROR: already assigned "
3508 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003509 rv = -EBUSY;
3510 goto out;
3511 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003512
3513 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003514 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 pkt_dev->running = 0;
3516
Luiz Capitulino222f1802006-03-20 22:16:13 -08003517out:
3518 if_unlock(t);
3519 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520}
3521
3522/* Called under thread lock */
3523
Luiz Capitulino222f1802006-03-20 22:16:13 -08003524static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003526 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003527 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /* We don't allow a device to be on several threads */
3530
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003531 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3532 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003533 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003534 return -EBUSY;
3535 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003536
3537 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3538 if (!pkt_dev)
3539 return -ENOMEM;
3540
Luiz Capitulino222f1802006-03-20 22:16:13 -08003541 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003542 if (pkt_dev->flows == NULL) {
3543 kfree(pkt_dev);
3544 return -ENOMEM;
3545 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003546 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003547
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003548 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003549 pkt_dev->min_pkt_size = ETH_ZLEN;
3550 pkt_dev->max_pkt_size = ETH_ZLEN;
3551 pkt_dev->nfrags = 0;
3552 pkt_dev->clone_skb = pg_clone_skb_d;
3553 pkt_dev->delay_us = pg_delay_d / 1000;
3554 pkt_dev->delay_ns = pg_delay_d % 1000;
3555 pkt_dev->count = pg_count_d;
3556 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003557 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003558 pkt_dev->udp_src_max = 9;
3559 pkt_dev->udp_dst_min = 9;
3560 pkt_dev->udp_dst_max = 9;
3561
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003562 pkt_dev->vlan_p = 0;
3563 pkt_dev->vlan_cfi = 0;
3564 pkt_dev->vlan_id = 0xffff;
3565 pkt_dev->svlan_p = 0;
3566 pkt_dev->svlan_cfi = 0;
3567 pkt_dev->svlan_id = 0xffff;
3568
Stephen Hemminger39df2322007-03-04 16:11:51 -08003569 err = pktgen_setup_dev(pkt_dev, ifname);
3570 if (err)
3571 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003572
Wang Chen25296d52008-02-28 14:11:49 -08003573 pkt_dev->entry = proc_create(ifname, 0600,
3574 pg_proc_dir, &pktgen_if_fops);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003575 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003576 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003577 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003578 err = -EINVAL;
3579 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003580 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08003581 pkt_dev->entry->data = pkt_dev;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003582#ifdef CONFIG_XFRM
3583 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3584 pkt_dev->ipsproto = IPPROTO_ESP;
3585#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003586
3587 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003588out2:
3589 dev_put(pkt_dev->odev);
3590out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003591#ifdef CONFIG_XFRM
3592 free_SAs(pkt_dev);
3593#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -08003594 if (pkt_dev->flows)
3595 vfree(pkt_dev->flows);
3596 kfree(pkt_dev);
3597 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
David S. Milleree74baa2007-01-01 20:51:53 -08003600static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003602 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003603 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003604 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Luiz Capitulino222f1802006-03-20 22:16:13 -08003606 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3607 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003608 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3609 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003610 return -ENOMEM;
3611 }
3612
Luiz Capitulino222f1802006-03-20 22:16:13 -08003613 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615
David S. Milleree74baa2007-01-01 20:51:53 -08003616 INIT_LIST_HEAD(&t->if_list);
3617
3618 list_add_tail(&t->th_list, &pktgen_threads);
3619
3620 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3621 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003622 printk(KERN_ERR "pktgen: kernel_thread() failed "
3623 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003624 list_del(&t->th_list);
3625 kfree(t);
3626 return PTR_ERR(p);
3627 }
3628 kthread_bind(p, cpu);
3629 t->tsk = p;
3630
Wang Chen25296d52008-02-28 14:11:49 -08003631 pe = proc_create(t->tsk->comm, 0600, pg_proc_dir, &pktgen_thread_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003632 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003633 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003634 PG_PROC_DIR, t->tsk->comm);
3635 kthread_stop(p);
3636 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003637 kfree(t);
3638 return -EINVAL;
3639 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003640
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003641 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
David S. Milleree74baa2007-01-01 20:51:53 -08003643 wake_up_process(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
3645 return 0;
3646}
3647
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003648/*
3649 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003651static void _rem_dev_from_if_list(struct pktgen_thread *t,
3652 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003654 struct list_head *q, *n;
3655 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003657 list_for_each_safe(q, n, &t->if_list) {
3658 p = list_entry(q, struct pktgen_dev, list);
3659 if (p == pkt_dev)
3660 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
3662}
3663
Luiz Capitulino222f1802006-03-20 22:16:13 -08003664static int pktgen_remove_device(struct pktgen_thread *t,
3665 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
3667
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003668 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Luiz Capitulino222f1802006-03-20 22:16:13 -08003670 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003671 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3672 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003673 pktgen_stop_device(pkt_dev);
3674 }
3675
3676 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678 if (pkt_dev->odev) {
3679 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003680 pkt_dev->odev = NULL;
3681 }
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 /* And update the thread if_list */
3684
3685 _rem_dev_from_if_list(t, pkt_dev);
3686
Stephen Hemminger39df2322007-03-04 16:11:51 -08003687 if (pkt_dev->entry)
3688 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003690#ifdef CONFIG_XFRM
3691 free_SAs(pkt_dev);
3692#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 if (pkt_dev->flows)
3694 vfree(pkt_dev->flows);
3695 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
3698
Luiz Capitulino222f1802006-03-20 22:16:13 -08003699static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
3701 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003702 struct proc_dir_entry *pe;
3703
David S. Miller25a8b252007-07-30 16:11:48 -07003704 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003706 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003707 if (!pg_proc_dir)
3708 return -ENODEV;
3709 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Wang Chen25296d52008-02-28 14:11:49 -08003711 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003712 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003713 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3714 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003715 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003716 return -EINVAL;
3717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Luiz Capitulino222f1802006-03-20 22:16:13 -08003719 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 /* Register us to receive netdevice events */
3722 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003723
John Hawkes670c02c2005-10-13 09:30:31 -07003724 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003725 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
David S. Milleree74baa2007-01-01 20:51:53 -08003727 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003728 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003729 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3730 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003731 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003732
3733 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003734 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3735 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003736 unregister_netdevice_notifier(&pktgen_notifier_block);
3737 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003738 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003739 return -ENODEV;
3740 }
3741
Luiz Capitulino222f1802006-03-20 22:16:13 -08003742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
3745static void __exit pg_cleanup(void)
3746{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003747 struct pktgen_thread *t;
3748 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 wait_queue_head_t queue;
3750 init_waitqueue_head(&queue);
3751
Luiz Capitulino222f1802006-03-20 22:16:13 -08003752 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003754 list_for_each_safe(q, n, &pktgen_threads) {
3755 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003756 kthread_stop(t->tsk);
3757 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Luiz Capitulino222f1802006-03-20 22:16:13 -08003760 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 unregister_netdevice_notifier(&pktgen_notifier_block);
3762
Luiz Capitulino222f1802006-03-20 22:16:13 -08003763 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003764 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003765 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768module_init(pg_init);
3769module_exit(pg_cleanup);
3770
3771MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3772MODULE_DESCRIPTION("Packet Generator tool");
3773MODULE_LICENSE("GPL");
3774module_param(pg_count_d, int, 0);
3775module_param(pg_delay_d, int, 0);
3776module_param(pg_clone_skb_d, int, 0);
3777module_param(debug, int, 0);