blob: be985f068e79c05d8fa2cc7feafba36a7af2744c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090047 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090068 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115#include <linux/sys.h>
116#include <linux/types.h>
117#include <linux/module.h>
118#include <linux/moduleparam.h>
119#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800120#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <linux/sched.h>
122#include <linux/slab.h>
123#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <linux/unistd.h>
125#include <linux/string.h>
126#include <linux/ptrace.h>
127#include <linux/errno.h>
128#include <linux/ioport.h>
129#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800130#include <linux/capability.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700131#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#include <linux/delay.h>
133#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800134#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#include <linux/init.h>
136#include <linux/skbuff.h>
137#include <linux/netdevice.h>
138#include <linux/inet.h>
139#include <linux/inetdevice.h>
140#include <linux/rtnetlink.h>
141#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700142#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#include <linux/in.h>
144#include <linux/ip.h>
145#include <linux/ipv6.h>
146#include <linux/udp.h>
147#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700148#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800150#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800151#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#include <net/checksum.h>
153#include <net/ipv6.h>
154#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700155#ifdef CONFIG_XFRM
156#include <net/xfrm.h>
157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#include <asm/byteorder.h>
159#include <linux/rcupdate.h>
160#include <asm/bitops.h>
161#include <asm/io.h>
162#include <asm/dma.h>
163#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800164#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#include <asm/timex.h>
166
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700167#define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/* The buckets are exponential in 'width' */
170#define LAT_BUCKETS_MAX 32
171#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800172#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800173#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800176#define F_IPSRC_RND (1<<0) /* IP-Src Random */
177#define F_IPDST_RND (1<<1) /* IP-Dst Random */
178#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
179#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
180#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
181#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
182#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
183#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800184#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700185#define F_VID_RND (1<<9) /* Random VLAN ID */
186#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700187#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700188#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800191#define T_TERMINATE (1<<0)
192#define T_STOP (1<<1) /* Stop run */
193#define T_RUN (1<<2) /* Start run */
194#define T_REMDEVALL (1<<3) /* Remove all devs */
195#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* If lock -- can be removed after some work */
198#define if_lock(t) spin_lock(&(t->if_lock));
199#define if_unlock(t) spin_unlock(&(t->if_lock));
200
201/* Used to help with determining the pkts on receive */
202#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700203#define PG_PROC_DIR "pktgen"
204#define PGCTRL "pgctrl"
205static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207#define MAX_CFLOWS 65536
208
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700209#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
210#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
211
Luiz Capitulino222f1802006-03-20 22:16:13 -0800212struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800213 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800214 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700215#ifdef CONFIG_XFRM
216 struct xfrm_state *x;
217#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700218 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700221/* flow flag bits */
222#define F_INIT (1<<0) /* flow has been initialized */
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /*
226 * Try to keep frequent/infrequent used vars. separated.
227 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800228 struct proc_dir_entry *entry; /* proc file */
229 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800230 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Luiz Capitulino222f1802006-03-20 22:16:13 -0800232 int running; /* if this changes to false, the test will stop */
233
234 /* If min != max, then we will either do a linear iteration, or
235 * we will do a random selection from within the range.
236 */
237 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800238 int removal_mark; /* non-zero => the device is marked for
239 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Luiz Capitulino222f1802006-03-20 22:16:13 -0800241 int min_pkt_size; /* = ETH_ZLEN; */
242 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700243 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800244 int nfrags;
245 __u32 delay_us; /* Default delay */
246 __u32 delay_ns;
247 __u64 count; /* Default No packets to send */
248 __u64 sofar; /* How many pkts we've sent so far */
249 __u64 tx_bytes; /* How many bytes we've transmitted */
250 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Luiz Capitulino222f1802006-03-20 22:16:13 -0800252 /* runtime counters relating to clone_skb */
253 __u64 next_tx_us; /* timestamp of when to tx next */
254 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Luiz Capitulino222f1802006-03-20 22:16:13 -0800256 __u64 allocated_skbs;
257 __u32 clone_count;
258 int last_ok; /* Was last skb sent?
259 * Or a failed transmit of some sort? This will keep
260 * sequence numbers in order, for example.
261 */
262 __u64 started_at; /* micro-seconds */
263 __u64 stopped_at; /* micro-seconds */
264 __u64 idle_acc; /* micro-seconds */
265 __u32 seq_num;
266
267 int clone_skb; /* Use multiple SKBs during packet gen. If this number
268 * is greater than 1, then that many copies of the same
269 * packet will be sent before a new packet is allocated.
270 * For instance, if you want to send 1024 identical packets
271 * before creating a new packet, set clone_skb to 1024.
272 */
273
274 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
275 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
276 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
277 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
278
279 struct in6_addr in6_saddr;
280 struct in6_addr in6_daddr;
281 struct in6_addr cur_in6_daddr;
282 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800284 struct in6_addr min_in6_daddr;
285 struct in6_addr max_in6_daddr;
286 struct in6_addr min_in6_saddr;
287 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Luiz Capitulino222f1802006-03-20 22:16:13 -0800289 /* If we're doing ranges, random or incremental, then this
290 * defines the min/max for those ranges.
291 */
Al Viro252e33462006-11-14 20:48:11 -0800292 __be32 saddr_min; /* inclusive, source IP address */
293 __be32 saddr_max; /* exclusive, source IP address */
294 __be32 daddr_min; /* inclusive, dest IP address */
295 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Luiz Capitulino222f1802006-03-20 22:16:13 -0800297 __u16 udp_src_min; /* inclusive, source UDP port */
298 __u16 udp_src_max; /* exclusive, source UDP port */
299 __u16 udp_dst_min; /* inclusive, dest UDP port */
300 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700302 /* DSCP + ECN */
303 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
304 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
305
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800306 /* MPLS */
307 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
308 __be32 labels[MAX_MPLS_LABELS];
309
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700310 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
311 __u8 vlan_p;
312 __u8 vlan_cfi;
313 __u16 vlan_id; /* 0xffff means no vlan tag */
314
315 __u8 svlan_p;
316 __u8 svlan_cfi;
317 __u16 svlan_id; /* 0xffff means no svlan tag */
318
Luiz Capitulino222f1802006-03-20 22:16:13 -0800319 __u32 src_mac_count; /* How many MACs to iterate through */
320 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Luiz Capitulino222f1802006-03-20 22:16:13 -0800322 unsigned char dst_mac[ETH_ALEN];
323 unsigned char src_mac[ETH_ALEN];
324
325 __u32 cur_dst_mac_offset;
326 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800327 __be32 cur_saddr;
328 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800329 __u16 cur_udp_dst;
330 __u16 cur_udp_src;
331 __u32 cur_pkt_size;
332
333 __u8 hh[14];
334 /* = {
335 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
336
337 We fill in SRC address later
338 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
339 0x08, 0x00
340 };
341 */
342 __u16 pad; /* pad out the hh struct to an even 16 bytes */
343
344 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
345 * are transmitting the same one multiple times
346 */
347 struct net_device *odev; /* The out-going device. Note that the device should
348 * have it's pg_info pointer pointing back to this
349 * device. This will be set when the user specifies
350 * the out-going device name (not when the inject is
351 * started as it used to do.)
352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800354 unsigned cflows; /* Concurrent flows (config) */
355 unsigned lflow; /* Flow length (config) */
356 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700357 unsigned curfl; /* current sequenced flow (state)*/
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700358#ifdef CONFIG_XFRM
359 __u8 ipsmode; /* IPSEC mode (config) */
360 __u8 ipsproto; /* IPSEC type (config) */
361#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800362 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363};
364
365struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800366 __be32 pgh_magic;
367 __be32 seq_num;
368 __be32 tv_sec;
369 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
372struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800373 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800374 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800375 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800376 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800377 char result[512];
378 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Luiz Capitulino222f1802006-03-20 22:16:13 -0800380 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
381
382 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int pid;
384 int cpu;
385
Luiz Capitulino222f1802006-03-20 22:16:13 -0800386 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387};
388
389#define REMOVE 1
390#define FIND 0
391
392/* This code works around the fact that do_div cannot handle two 64-bit
393 numbers, and regular 64-bit division doesn't work on x86 kernels.
394 --Ben
395*/
396
397#define PG_DIV 0
398
399/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
400 * Function copied/adapted/optimized from:
401 *
402 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
403 *
404 * Copyright 1994, University of Cambridge Computer Laboratory
405 * All Rights Reserved.
406 *
407 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700408static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800410 u64 a = (x < 0) ? -x : x;
411 u64 b = (y < 0) ? -y : y;
412 u64 res = 0, d = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Luiz Capitulino222f1802006-03-20 22:16:13 -0800414 if (b > 0) {
415 while (b < a) {
416 b <<= 1;
417 d <<= 1;
418 }
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Luiz Capitulino222f1802006-03-20 22:16:13 -0800421 do {
422 if (a >= b) {
423 a -= b;
424 res += d;
425 }
426 b >>= 1;
427 d >>= 1;
428 }
429 while (d);
430
431 if (PG_DIV == type) {
432 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
433 } else {
434 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438/* End of hacks to deal with 64-bit math on x86 */
439
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700440/** Convert to milliseconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800441static inline __u64 tv_to_ms(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800443 __u64 ms = tv->tv_usec / 1000;
444 ms += (__u64) tv->tv_sec * (__u64) 1000;
445 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800449static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800451 __u64 us = tv->tv_usec;
452 us += (__u64) tv->tv_sec * (__u64) 1000000;
453 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Luiz Capitulino222f1802006-03-20 22:16:13 -0800456static inline __u64 pg_div(__u64 n, __u32 base)
457{
458 __u64 tmp = n;
459 do_div(tmp, base);
460 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
461 n, base, tmp); */
462 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Luiz Capitulino222f1802006-03-20 22:16:13 -0800465static inline __u64 pg_div64(__u64 n, __u64 base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800467 __u64 tmp = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700469 * How do we know if the architecture we are running on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * supports division with 64 bit base?
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900471 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800473#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Luiz Capitulino222f1802006-03-20 22:16:13 -0800475 do_div(tmp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800477 tmp = divremdi3(n, base, PG_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -0800479 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Luiz Capitulino222f1802006-03-20 22:16:13 -0800482static inline __u64 getCurMs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800484 struct timeval tv;
485 do_gettimeofday(&tv);
486 return tv_to_ms(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Luiz Capitulino222f1802006-03-20 22:16:13 -0800489static inline __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800491 struct timeval tv;
492 do_gettimeofday(&tv);
493 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Luiz Capitulino222f1802006-03-20 22:16:13 -0800496static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498 return tv_to_us(a) - tv_to_us(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/* old include end */
502
503static char version[] __initdata = VERSION;
504
Luiz Capitulino222f1802006-03-20 22:16:13 -0800505static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
506static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
507static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
508 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
510static void pktgen_run_all_threads(void);
511static void pktgen_stop_all_threads_ifs(void);
512static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800513static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800515
Luiz Capitulino222f1802006-03-20 22:16:13 -0800516static unsigned int scan_ip6(const char *s, char ip[16]);
517static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800520static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb92005-12-22 12:51:46 -0800521static int pg_delay_d;
522static int pg_clone_skb_d;
523static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Luiz Capitulino222fa072006-03-20 22:24:27 -0800525static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800526static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528static struct notifier_block pktgen_notifier_block = {
529 .notifier_call = pktgen_device_event,
530};
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900533 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *
535 */
536
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700537static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800538{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700539 seq_puts(seq, VERSION);
540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Luiz Capitulino222f1802006-03-20 22:16:13 -0800543static ssize_t pgctrl_write(struct file *file, const char __user * buf,
544 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700547 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Luiz Capitulino222f1802006-03-20 22:16:13 -0800549 if (!capable(CAP_NET_ADMIN)) {
550 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700554 if (count > sizeof(data))
555 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700558 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700559 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800560 }
561 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Luiz Capitulino222f1802006-03-20 22:16:13 -0800563 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 pktgen_stop_all_threads_ifs();
565
Luiz Capitulino222f1802006-03-20 22:16:13 -0800566 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 pktgen_run_all_threads();
568
Luiz Capitulino222f1802006-03-20 22:16:13 -0800569 else
David S. Miller25a8b252007-07-30 16:11:48 -0700570 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 err = count;
573
Luiz Capitulino222f1802006-03-20 22:16:13 -0800574out:
575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700578static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700580 return single_open(file, pgctrl_show, PDE(inode)->data);
581}
582
Arjan van de Ven9a321442007-02-12 00:55:35 -0800583static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800584 .owner = THIS_MODULE,
585 .open = pgctrl_open,
586 .read = seq_read,
587 .llseek = seq_lseek,
588 .write = pgctrl_write,
589 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700590};
591
592static int pktgen_if_show(struct seq_file *seq, void *v)
593{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800595 struct pktgen_dev *pkt_dev = seq->private;
596 __u64 sa;
597 __u64 stopped;
598 __u64 now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Luiz Capitulino222f1802006-03-20 22:16:13 -0800600 seq_printf(seq,
601 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
602 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
603 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Luiz Capitulino222f1802006-03-20 22:16:13 -0800605 seq_printf(seq,
606 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
607 pkt_dev->nfrags,
608 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800609 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Luiz Capitulino222f1802006-03-20 22:16:13 -0800611 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
612 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Luiz Capitulino222f1802006-03-20 22:16:13 -0800614 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800616 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
617 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
618 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
619 seq_printf(seq,
620 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
621 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Luiz Capitulino222f1802006-03-20 22:16:13 -0800623 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
624 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
625 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
626 seq_printf(seq,
627 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
628 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Luiz Capitulino222f1802006-03-20 22:16:13 -0800630 } else
631 seq_printf(seq,
632 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
633 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
634 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700636 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800638 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -0800639 for (i = 0; i < 6; i++)
640 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
641 i == 5 ? " " : ":");
642 else
643 for (i = 0; i < 6; i++)
644 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
645 i == 5 ? " " : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Luiz Capitulino222f1802006-03-20 22:16:13 -0800647 seq_printf(seq, "dst_mac: ");
648 for (i = 0; i < 6; i++)
649 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
650 i == 5 ? "\n" : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Luiz Capitulino222f1802006-03-20 22:16:13 -0800652 seq_printf(seq,
653 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
654 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
655 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Luiz Capitulino222f1802006-03-20 22:16:13 -0800657 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800658 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700659 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800661 if (pkt_dev->nr_labels) {
662 unsigned i;
663 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700664 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800665 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
666 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
667 }
668
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700669 if (pkt_dev->vlan_id != 0xffff) {
670 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
671 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
672 }
673
674 if (pkt_dev->svlan_id != 0xffff) {
675 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
676 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
677 }
678
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700679 if (pkt_dev->tos) {
680 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
681 }
682
683 if (pkt_dev->traffic_class) {
684 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
685 }
686
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800687 seq_printf(seq, " Flags: ");
688
Luiz Capitulino222f1802006-03-20 22:16:13 -0800689 if (pkt_dev->flags & F_IPV6)
690 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Luiz Capitulino222f1802006-03-20 22:16:13 -0800692 if (pkt_dev->flags & F_IPSRC_RND)
693 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Luiz Capitulino222f1802006-03-20 22:16:13 -0800695 if (pkt_dev->flags & F_IPDST_RND)
696 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Luiz Capitulino222f1802006-03-20 22:16:13 -0800698 if (pkt_dev->flags & F_TXSIZE_RND)
699 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Luiz Capitulino222f1802006-03-20 22:16:13 -0800701 if (pkt_dev->flags & F_UDPSRC_RND)
702 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Luiz Capitulino222f1802006-03-20 22:16:13 -0800704 if (pkt_dev->flags & F_UDPDST_RND)
705 seq_printf(seq, "UDPDST_RND ");
706
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800707 if (pkt_dev->flags & F_MPLS_RND)
708 seq_printf(seq, "MPLS_RND ");
709
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700710 if (pkt_dev->cflows) {
711 if (pkt_dev->flags & F_FLOW_SEQ)
712 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
713 else
714 seq_printf(seq, "FLOW_RND ");
715 }
716
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700717#ifdef CONFIG_XFRM
718 if (pkt_dev->flags & F_IPSEC_ON)
719 seq_printf(seq, "IPSEC ");
720#endif
721
Luiz Capitulino222f1802006-03-20 22:16:13 -0800722 if (pkt_dev->flags & F_MACSRC_RND)
723 seq_printf(seq, "MACSRC_RND ");
724
725 if (pkt_dev->flags & F_MACDST_RND)
726 seq_printf(seq, "MACDST_RND ");
727
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700728 if (pkt_dev->flags & F_VID_RND)
729 seq_printf(seq, "VID_RND ");
730
731 if (pkt_dev->flags & F_SVID_RND)
732 seq_printf(seq, "SVID_RND ");
733
Luiz Capitulino222f1802006-03-20 22:16:13 -0800734 seq_puts(seq, "\n");
735
736 sa = pkt_dev->started_at;
737 stopped = pkt_dev->stopped_at;
738 if (pkt_dev->running)
739 stopped = now; /* not really stopped, more like last-running-at */
740
741 seq_printf(seq,
742 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
743 (unsigned long long)pkt_dev->sofar,
744 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
745 (unsigned long long)stopped,
746 (unsigned long long)pkt_dev->idle_acc);
747
748 seq_printf(seq,
749 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700750 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
751 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Luiz Capitulino222f1802006-03-20 22:16:13 -0800753 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800755 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
756 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
757 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
758 } else
759 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700760 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Luiz Capitulino222f1802006-03-20 22:16:13 -0800762 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700763 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Luiz Capitulino222f1802006-03-20 22:16:13 -0800765 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800768 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800770 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800775
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700776static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800777{
778 int i = 0;
779 *num = 0;
780
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700781 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800782 char c;
783 *num <<= 4;
784 if (get_user(c, &user_buffer[i]))
785 return -EFAULT;
786 if ((c >= '0') && (c <= '9'))
787 *num |= c - '0';
788 else if ((c >= 'a') && (c <= 'f'))
789 *num |= c - 'a' + 10;
790 else if ((c >= 'A') && (c <= 'F'))
791 *num |= c - 'A' + 10;
792 else
793 break;
794 }
795 return i;
796}
797
Luiz Capitulino222f1802006-03-20 22:16:13 -0800798static int count_trail_chars(const char __user * user_buffer,
799 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 int i;
802
803 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800804 char c;
805 if (get_user(c, &user_buffer[i]))
806 return -EFAULT;
807 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 case '\"':
809 case '\n':
810 case '\r':
811 case '\t':
812 case ' ':
813 case '=':
814 break;
815 default:
816 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819done:
820 return i;
821}
822
Luiz Capitulino222f1802006-03-20 22:16:13 -0800823static unsigned long num_arg(const char __user * user_buffer,
824 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 int i = 0;
827 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800828
829 for (; i < maxlen; i++) {
830 char c;
831 if (get_user(c, &user_buffer[i]))
832 return -EFAULT;
833 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800835 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 } else
837 break;
838 }
839 return i;
840}
841
Luiz Capitulino222f1802006-03-20 22:16:13 -0800842static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 int i = 0;
845
Luiz Capitulino222f1802006-03-20 22:16:13 -0800846 for (; i < maxlen; i++) {
847 char c;
848 if (get_user(c, &user_buffer[i]))
849 return -EFAULT;
850 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 case '\"':
852 case '\n':
853 case '\r':
854 case '\t':
855 case ' ':
856 goto done_str;
857 break;
858 default:
859 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return i;
864}
865
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800866static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
867{
868 unsigned n = 0;
869 char c;
870 ssize_t i = 0;
871 int len;
872
873 pkt_dev->nr_labels = 0;
874 do {
875 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700876 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800877 if (len <= 0)
878 return len;
879 pkt_dev->labels[n] = htonl(tmp);
880 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
881 pkt_dev->flags |= F_MPLS_RND;
882 i += len;
883 if (get_user(c, &buffer[i]))
884 return -EFAULT;
885 i++;
886 n++;
887 if (n >= MAX_MPLS_LABELS)
888 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700889 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800890
891 pkt_dev->nr_labels = n;
892 return i;
893}
894
Luiz Capitulino222f1802006-03-20 22:16:13 -0800895static ssize_t pktgen_if_write(struct file *file,
896 const char __user * user_buffer, size_t count,
897 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800899 struct seq_file *seq = (struct seq_file *)file->private_data;
900 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 int i = 0, max, len;
902 char name[16], valstr[32];
903 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 char *pg_result = NULL;
905 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907
908 pg_result = &(pkt_dev->result[0]);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700911 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -EINVAL;
913 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 max = count - i;
916 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800917 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700918 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800919 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800921 i += tmp;
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /* Read variable name */
924
925 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800926 if (len < 0) {
927 return len;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -EFAULT;
932 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800933
934 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800936 if (len < 0)
937 return len;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 i += len;
940
941 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800942 char tb[count + 1];
943 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800945 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700946 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800947 (unsigned long)count, tb);
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 if (!strcmp(name, "min_pkt_size")) {
951 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800952 if (len < 0) {
953 return len;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 if (value < 14 + 20 + 8)
957 value = 14 + 20 + 8;
958 if (value != pkt_dev->min_pkt_size) {
959 pkt_dev->min_pkt_size = value;
960 pkt_dev->cur_pkt_size = value;
961 }
962 sprintf(pg_result, "OK: min_pkt_size=%u",
963 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return count;
965 }
966
Luiz Capitulino222f1802006-03-20 22:16:13 -0800967 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800969 if (len < 0) {
970 return len;
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800973 if (value < 14 + 20 + 8)
974 value = 14 + 20 + 8;
975 if (value != pkt_dev->max_pkt_size) {
976 pkt_dev->max_pkt_size = value;
977 pkt_dev->cur_pkt_size = value;
978 }
979 sprintf(pg_result, "OK: max_pkt_size=%u",
980 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return count;
982 }
983
Luiz Capitulino222f1802006-03-20 22:16:13 -0800984 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (!strcmp(name, "pkt_size")) {
987 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800988 if (len < 0) {
989 return len;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800992 if (value < 14 + 20 + 8)
993 value = 14 + 20 + 8;
994 if (value != pkt_dev->min_pkt_size) {
995 pkt_dev->min_pkt_size = value;
996 pkt_dev->max_pkt_size = value;
997 pkt_dev->cur_pkt_size = value;
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
1000 return count;
1001 }
1002
Luiz Capitulino222f1802006-03-20 22:16:13 -08001003 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001005 if (len < 0) {
1006 return len;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001009 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 sprintf(pg_result, "OK: debug=%u", debug);
1011 return count;
1012 }
1013
Luiz Capitulino222f1802006-03-20 22:16:13 -08001014 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001016 if (len < 0) {
1017 return len;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 i += len;
1020 pkt_dev->nfrags = value;
1021 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1022 return count;
1023 }
1024 if (!strcmp(name, "delay")) {
1025 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001026 if (len < 0) {
1027 return len;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 i += len;
1030 if (value == 0x7FFFFFFF) {
1031 pkt_dev->delay_us = 0x7FFFFFFF;
1032 pkt_dev->delay_ns = 0;
1033 } else {
1034 pkt_dev->delay_us = value / 1000;
1035 pkt_dev->delay_ns = value % 1000;
1036 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001037 sprintf(pg_result, "OK: delay=%u",
1038 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return count;
1040 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001041 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001043 if (len < 0) {
1044 return len;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001047 if (value != pkt_dev->udp_src_min) {
1048 pkt_dev->udp_src_min = value;
1049 pkt_dev->cur_udp_src = value;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1052 return count;
1053 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001056 if (len < 0) {
1057 return len;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001060 if (value != pkt_dev->udp_dst_min) {
1061 pkt_dev->udp_dst_min = value;
1062 pkt_dev->cur_udp_dst = value;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1065 return count;
1066 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001067 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001069 if (len < 0) {
1070 return len;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001073 if (value != pkt_dev->udp_src_max) {
1074 pkt_dev->udp_src_max = value;
1075 pkt_dev->cur_udp_src = value;
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1078 return count;
1079 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001080 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001082 if (len < 0) {
1083 return len;
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001086 if (value != pkt_dev->udp_dst_max) {
1087 pkt_dev->udp_dst_max = value;
1088 pkt_dev->cur_udp_dst = value;
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1091 return count;
1092 }
1093 if (!strcmp(name, "clone_skb")) {
1094 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001095 if (len < 0) {
1096 return len;
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001099 pkt_dev->clone_skb = value;
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1102 return count;
1103 }
1104 if (!strcmp(name, "count")) {
1105 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001106 if (len < 0) {
1107 return len;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 i += len;
1110 pkt_dev->count = value;
1111 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001112 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return count;
1114 }
1115 if (!strcmp(name, "src_mac_count")) {
1116 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001117 if (len < 0) {
1118 return len;
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 i += len;
1121 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001122 pkt_dev->src_mac_count = value;
1123 pkt_dev->cur_src_mac_offset = 0;
1124 }
1125 sprintf(pg_result, "OK: src_mac_count=%d",
1126 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return count;
1128 }
1129 if (!strcmp(name, "dst_mac_count")) {
1130 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001131 if (len < 0) {
1132 return len;
1133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 i += len;
1135 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001136 pkt_dev->dst_mac_count = value;
1137 pkt_dev->cur_dst_mac_offset = 0;
1138 }
1139 sprintf(pg_result, "OK: dst_mac_count=%d",
1140 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return count;
1142 }
1143 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001144 char f[32];
1145 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001147 if (len < 0) {
1148 return len;
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (copy_from_user(f, &user_buffer[i], len))
1151 return -EFAULT;
1152 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001153 if (strcmp(f, "IPSRC_RND") == 0)
1154 pkt_dev->flags |= F_IPSRC_RND;
1155
1156 else if (strcmp(f, "!IPSRC_RND") == 0)
1157 pkt_dev->flags &= ~F_IPSRC_RND;
1158
1159 else if (strcmp(f, "TXSIZE_RND") == 0)
1160 pkt_dev->flags |= F_TXSIZE_RND;
1161
1162 else if (strcmp(f, "!TXSIZE_RND") == 0)
1163 pkt_dev->flags &= ~F_TXSIZE_RND;
1164
1165 else if (strcmp(f, "IPDST_RND") == 0)
1166 pkt_dev->flags |= F_IPDST_RND;
1167
1168 else if (strcmp(f, "!IPDST_RND") == 0)
1169 pkt_dev->flags &= ~F_IPDST_RND;
1170
1171 else if (strcmp(f, "UDPSRC_RND") == 0)
1172 pkt_dev->flags |= F_UDPSRC_RND;
1173
1174 else if (strcmp(f, "!UDPSRC_RND") == 0)
1175 pkt_dev->flags &= ~F_UDPSRC_RND;
1176
1177 else if (strcmp(f, "UDPDST_RND") == 0)
1178 pkt_dev->flags |= F_UDPDST_RND;
1179
1180 else if (strcmp(f, "!UDPDST_RND") == 0)
1181 pkt_dev->flags &= ~F_UDPDST_RND;
1182
1183 else if (strcmp(f, "MACSRC_RND") == 0)
1184 pkt_dev->flags |= F_MACSRC_RND;
1185
1186 else if (strcmp(f, "!MACSRC_RND") == 0)
1187 pkt_dev->flags &= ~F_MACSRC_RND;
1188
1189 else if (strcmp(f, "MACDST_RND") == 0)
1190 pkt_dev->flags |= F_MACDST_RND;
1191
1192 else if (strcmp(f, "!MACDST_RND") == 0)
1193 pkt_dev->flags &= ~F_MACDST_RND;
1194
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001195 else if (strcmp(f, "MPLS_RND") == 0)
1196 pkt_dev->flags |= F_MPLS_RND;
1197
1198 else if (strcmp(f, "!MPLS_RND") == 0)
1199 pkt_dev->flags &= ~F_MPLS_RND;
1200
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001201 else if (strcmp(f, "VID_RND") == 0)
1202 pkt_dev->flags |= F_VID_RND;
1203
1204 else if (strcmp(f, "!VID_RND") == 0)
1205 pkt_dev->flags &= ~F_VID_RND;
1206
1207 else if (strcmp(f, "SVID_RND") == 0)
1208 pkt_dev->flags |= F_SVID_RND;
1209
1210 else if (strcmp(f, "!SVID_RND") == 0)
1211 pkt_dev->flags &= ~F_SVID_RND;
1212
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001213 else if (strcmp(f, "FLOW_SEQ") == 0)
1214 pkt_dev->flags |= F_FLOW_SEQ;
1215
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001216#ifdef CONFIG_XFRM
1217 else if (strcmp(f, "IPSEC") == 0)
1218 pkt_dev->flags |= F_IPSEC_ON;
1219#endif
1220
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001221 else if (strcmp(f, "!IPV6") == 0)
1222 pkt_dev->flags &= ~F_IPV6;
1223
Luiz Capitulino222f1802006-03-20 22:16:13 -08001224 else {
1225 sprintf(pg_result,
1226 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1227 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001228 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001229 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001230 return count;
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1233 return count;
1234 }
1235 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1236 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001237 if (len < 0) {
1238 return len;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Luiz Capitulino222f1802006-03-20 22:16:13 -08001241 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001243 buf[len] = 0;
1244 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1245 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1246 strncpy(pkt_dev->dst_min, buf, len);
1247 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1248 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1249 }
1250 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001251 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001252 pkt_dev->dst_min);
1253 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1255 return count;
1256 }
1257 if (!strcmp(name, "dst_max")) {
1258 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 if (len < 0) {
1260 return len;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Luiz Capitulino222f1802006-03-20 22:16:13 -08001263 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return -EFAULT;
1265
Luiz Capitulino222f1802006-03-20 22:16:13 -08001266 buf[len] = 0;
1267 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1268 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1269 strncpy(pkt_dev->dst_max, buf, len);
1270 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1271 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1272 }
1273 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001274 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001275 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 i += len;
1277 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1278 return count;
1279 }
1280 if (!strcmp(name, "dst6")) {
1281 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001282 if (len < 0)
1283 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 pkt_dev->flags |= F_IPV6;
1286
Luiz Capitulino222f1802006-03-20 22:16:13 -08001287 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001289 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001292 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1295
Luiz Capitulino222f1802006-03-20 22:16:13 -08001296 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001297 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Luiz Capitulino222f1802006-03-20 22:16:13 -08001299 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 sprintf(pg_result, "OK: dst6=%s", buf);
1301 return count;
1302 }
1303 if (!strcmp(name, "dst6_min")) {
1304 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001305 if (len < 0)
1306 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 pkt_dev->flags |= F_IPV6;
1309
Luiz Capitulino222f1802006-03-20 22:16:13 -08001310 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001312 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001315 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1318 &pkt_dev->min_in6_daddr);
1319 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001320 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 sprintf(pg_result, "OK: dst6_min=%s", buf);
1324 return count;
1325 }
1326 if (!strcmp(name, "dst6_max")) {
1327 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001328 if (len < 0)
1329 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 pkt_dev->flags |= F_IPV6;
1332
Luiz Capitulino222f1802006-03-20 22:16:13 -08001333 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Luiz Capitulino222f1802006-03-20 22:16:13 -08001340 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001341 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Luiz Capitulino222f1802006-03-20 22:16:13 -08001343 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 sprintf(pg_result, "OK: dst6_max=%s", buf);
1345 return count;
1346 }
1347 if (!strcmp(name, "src6")) {
1348 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001349 if (len < 0)
1350 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 pkt_dev->flags |= F_IPV6;
1353
Luiz Capitulino222f1802006-03-20 22:16:13 -08001354 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001356 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1362
Luiz Capitulino222f1802006-03-20 22:16:13 -08001363 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001364 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001365
1366 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 sprintf(pg_result, "OK: src6=%s", buf);
1368 return count;
1369 }
1370 if (!strcmp(name, "src_min")) {
1371 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001372 if (len < 0) {
1373 return len;
1374 }
1375 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001377 buf[len] = 0;
1378 if (strcmp(buf, pkt_dev->src_min) != 0) {
1379 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1380 strncpy(pkt_dev->src_min, buf, len);
1381 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1382 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1383 }
1384 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001385 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001386 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 i += len;
1388 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1389 return count;
1390 }
1391 if (!strcmp(name, "src_max")) {
1392 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 if (len < 0) {
1394 return len;
1395 }
1396 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001398 buf[len] = 0;
1399 if (strcmp(buf, pkt_dev->src_max) != 0) {
1400 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1401 strncpy(pkt_dev->src_max, buf, len);
1402 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1403 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1404 }
1405 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001406 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001407 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 i += len;
1409 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1410 return count;
1411 }
1412 if (!strcmp(name, "dst_mac")) {
1413 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001414 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001416 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001419 if (len < 0) {
1420 return len;
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001423 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return -EFAULT;
1425 i += len;
1426
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (*v >= '0' && *v <= '9') {
1429 *m *= 16;
1430 *m += *v - '0';
1431 }
1432 if (*v >= 'A' && *v <= 'F') {
1433 *m *= 16;
1434 *m += *v - 'A' + 10;
1435 }
1436 if (*v >= 'a' && *v <= 'f') {
1437 *m *= 16;
1438 *m += *v - 'a' + 10;
1439 }
1440 if (*v == ':') {
1441 m++;
1442 *m = 0;
1443 }
1444 }
1445
1446 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001447 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1448 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 sprintf(pg_result, "OK: dstmac");
1451 return count;
1452 }
1453 if (!strcmp(name, "src_mac")) {
1454 char *v = valstr;
1455 unsigned char *m = pkt_dev->src_mac;
1456
1457 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001458 if (len < 0) {
1459 return len;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001462 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return -EFAULT;
1464 i += len;
1465
Luiz Capitulino222f1802006-03-20 22:16:13 -08001466 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (*v >= '0' && *v <= '9') {
1468 *m *= 16;
1469 *m += *v - '0';
1470 }
1471 if (*v >= 'A' && *v <= 'F') {
1472 *m *= 16;
1473 *m += *v - 'A' + 10;
1474 }
1475 if (*v >= 'a' && *v <= 'f') {
1476 *m *= 16;
1477 *m += *v - 'a' + 10;
1478 }
1479 if (*v == ':') {
1480 m++;
1481 *m = 0;
1482 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Luiz Capitulino222f1802006-03-20 22:16:13 -08001485 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return count;
1487 }
1488
Luiz Capitulino222f1802006-03-20 22:16:13 -08001489 if (!strcmp(name, "clear_counters")) {
1490 pktgen_clear_counters(pkt_dev);
1491 sprintf(pg_result, "OK: Clearing counters.\n");
1492 return count;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 if (!strcmp(name, "flows")) {
1496 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001497 if (len < 0) {
1498 return len;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 i += len;
1501 if (value > MAX_CFLOWS)
1502 value = MAX_CFLOWS;
1503
1504 pkt_dev->cflows = value;
1505 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1506 return count;
1507 }
1508
1509 if (!strcmp(name, "flowlen")) {
1510 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001511 if (len < 0) {
1512 return len;
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 i += len;
1515 pkt_dev->lflow = value;
1516 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1517 return count;
1518 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001519
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001520 if (!strcmp(name, "mpls")) {
1521 unsigned n, offset;
1522 len = get_labels(&user_buffer[i], pkt_dev);
1523 if (len < 0) { return len; }
1524 i += len;
1525 offset = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001526 for (n = 0; n < pkt_dev->nr_labels; n++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001527 offset += sprintf(pg_result + offset,
1528 "%08x%s", ntohl(pkt_dev->labels[n]),
1529 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001530
1531 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1532 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1533 pkt_dev->svlan_id = 0xffff;
1534
1535 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001536 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001537 }
1538 return count;
1539 }
1540
1541 if (!strcmp(name, "vlan_id")) {
1542 len = num_arg(&user_buffer[i], 4, &value);
1543 if (len < 0) {
1544 return len;
1545 }
1546 i += len;
1547 if (value <= 4095) {
1548 pkt_dev->vlan_id = value; /* turn on VLAN */
1549
1550 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001551 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001552
1553 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001554 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001555
1556 pkt_dev->nr_labels = 0; /* turn off MPLS */
1557 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1558 } else {
1559 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1560 pkt_dev->svlan_id = 0xffff;
1561
1562 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001563 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001564 }
1565 return count;
1566 }
1567
1568 if (!strcmp(name, "vlan_p")) {
1569 len = num_arg(&user_buffer[i], 1, &value);
1570 if (len < 0) {
1571 return len;
1572 }
1573 i += len;
1574 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1575 pkt_dev->vlan_p = value;
1576 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1577 } else {
1578 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1579 }
1580 return count;
1581 }
1582
1583 if (!strcmp(name, "vlan_cfi")) {
1584 len = num_arg(&user_buffer[i], 1, &value);
1585 if (len < 0) {
1586 return len;
1587 }
1588 i += len;
1589 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1590 pkt_dev->vlan_cfi = value;
1591 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1592 } else {
1593 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1594 }
1595 return count;
1596 }
1597
1598 if (!strcmp(name, "svlan_id")) {
1599 len = num_arg(&user_buffer[i], 4, &value);
1600 if (len < 0) {
1601 return len;
1602 }
1603 i += len;
1604 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1605 pkt_dev->svlan_id = value; /* turn on SVLAN */
1606
1607 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001608 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001609
1610 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001611 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001612
1613 pkt_dev->nr_labels = 0; /* turn off MPLS */
1614 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1615 } else {
1616 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1617 pkt_dev->svlan_id = 0xffff;
1618
1619 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001620 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001621 }
1622 return count;
1623 }
1624
1625 if (!strcmp(name, "svlan_p")) {
1626 len = num_arg(&user_buffer[i], 1, &value);
1627 if (len < 0) {
1628 return len;
1629 }
1630 i += len;
1631 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1632 pkt_dev->svlan_p = value;
1633 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1634 } else {
1635 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1636 }
1637 return count;
1638 }
1639
1640 if (!strcmp(name, "svlan_cfi")) {
1641 len = num_arg(&user_buffer[i], 1, &value);
1642 if (len < 0) {
1643 return len;
1644 }
1645 i += len;
1646 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1647 pkt_dev->svlan_cfi = value;
1648 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1649 } else {
1650 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1651 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001652 return count;
1653 }
1654
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001655 if (!strcmp(name, "tos")) {
1656 __u32 tmp_value = 0;
1657 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1658 if (len < 0) {
1659 return len;
1660 }
1661 i += len;
1662 if (len == 2) {
1663 pkt_dev->tos = tmp_value;
1664 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1665 } else {
1666 sprintf(pg_result, "ERROR: tos must be 00-ff");
1667 }
1668 return count;
1669 }
1670
1671 if (!strcmp(name, "traffic_class")) {
1672 __u32 tmp_value = 0;
1673 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1674 if (len < 0) {
1675 return len;
1676 }
1677 i += len;
1678 if (len == 2) {
1679 pkt_dev->traffic_class = tmp_value;
1680 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1681 } else {
1682 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1683 }
1684 return count;
1685 }
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1688 return -EINVAL;
1689}
1690
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001691static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001693 return single_open(file, pktgen_if_show, PDE(inode)->data);
1694}
1695
Arjan van de Ven9a321442007-02-12 00:55:35 -08001696static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001697 .owner = THIS_MODULE,
1698 .open = pktgen_if_open,
1699 .read = seq_read,
1700 .llseek = seq_lseek,
1701 .write = pktgen_if_write,
1702 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001703};
1704
1705static int pktgen_thread_show(struct seq_file *seq, void *v)
1706{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001707 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001708 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001710 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001712 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
David S. Milleree74baa2007-01-01 20:51:53 -08001713 t->tsk->comm, t->max_before_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Luiz Capitulino222f1802006-03-20 22:16:13 -08001715 seq_printf(seq, "Running: ");
1716
1717 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001718 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001719 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001720 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Luiz Capitulino222f1802006-03-20 22:16:13 -08001722 seq_printf(seq, "\nStopped: ");
1723
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001724 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001725 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001726 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001729 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001731 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Luiz Capitulino222f1802006-03-20 22:16:13 -08001733 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001738static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001739 const char __user * user_buffer,
1740 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001742 struct seq_file *seq = (struct seq_file *)file->private_data;
1743 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 int i = 0, max, len, ret;
1745 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001746 char *pg_result;
1747 unsigned long value = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001750 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return -EINVAL;
1752 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001755 len = count_trail_chars(&user_buffer[i], max);
1756 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001757 return len;
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* Read variable name */
1762
1763 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001765 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 memset(name, 0, sizeof(name));
1768 if (copy_from_user(name, &user_buffer[i], len))
1769 return -EFAULT;
1770 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001771
Luiz Capitulino222f1802006-03-20 22:16:13 -08001772 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001775 return len;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 i += len;
1778
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001779 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001780 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1781 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001784 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 ret = -EINVAL;
1786 goto out;
1787 }
1788
1789 pg_result = &(t->result[0]);
1790
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 if (!strcmp(name, "add_device")) {
1792 char f[32];
1793 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001795 if (len < 0) {
1796 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 goto out;
1798 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001799 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return -EFAULT;
1801 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001802 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001804 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001805 ret = count;
1806 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 goto out;
1808 }
1809
Luiz Capitulino222f1802006-03-20 22:16:13 -08001810 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001811 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001812 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001813 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001814 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001816 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto out;
1818 }
1819
Luiz Capitulino222f1802006-03-20 22:16:13 -08001820 if (!strcmp(name, "max_before_softirq")) {
1821 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001822 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001823 t->max_before_softirq = value;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001824 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001825 ret = count;
1826 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto out;
1828 }
1829
1830 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001831out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return ret;
1833}
1834
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001835static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001837 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Arjan van de Ven9a321442007-02-12 00:55:35 -08001840static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001841 .owner = THIS_MODULE,
1842 .open = pktgen_thread_open,
1843 .read = seq_read,
1844 .llseek = seq_lseek,
1845 .write = pktgen_thread_write,
1846 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001847};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001850static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 struct pktgen_thread *t;
1853 struct pktgen_dev *pkt_dev = NULL;
1854
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001855 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 pkt_dev = pktgen_find_dev(t, ifname);
1857 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001858 if (remove) {
1859 if_lock(t);
1860 pkt_dev->removal_mark = 1;
1861 t->control |= T_REMDEV;
1862 if_unlock(t);
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 break;
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001867 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001870/*
1871 * mark a device for removal
1872 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001873static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001876 const int max_tries = 10, msec_per_try = 125;
1877 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001878
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001879 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001880 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001881
Luiz Capitulino222f1802006-03-20 22:16:13 -08001882 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001883
1884 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001885 if (pkt_dev == NULL)
1886 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001887
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001888 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001889 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1890 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001891 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001892 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001893
1894 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001895 printk(KERN_ERR "pktgen_mark_device: timed out after "
1896 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001897 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001898 break;
1899 }
1900
1901 }
1902
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001903 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001904}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001905
Stephen Hemminger39df2322007-03-04 16:11:51 -08001906static void pktgen_change_name(struct net_device *dev)
1907{
1908 struct pktgen_thread *t;
1909
1910 list_for_each_entry(t, &pktgen_threads, th_list) {
1911 struct pktgen_dev *pkt_dev;
1912
1913 list_for_each_entry(pkt_dev, &t->if_list, list) {
1914 if (pkt_dev->odev != dev)
1915 continue;
1916
1917 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1918
1919 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1920 pg_proc_dir);
1921 if (!pkt_dev->entry)
1922 printk(KERN_ERR "pktgen: can't move proc "
1923 " entry for '%s'\n", dev->name);
1924 break;
1925 }
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Luiz Capitulino222f1802006-03-20 22:16:13 -08001929static int pktgen_device_event(struct notifier_block *unused,
1930 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001932 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /* It is OK that we do not hold the group lock right now,
1935 * as we run under the RTNL lock.
1936 */
1937
1938 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001939 case NETDEV_CHANGENAME:
1940 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001944 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 return NOTIFY_DONE;
1949}
1950
1951/* Associate pktgen_dev with a device. */
1952
Stephen Hemminger39df2322007-03-04 16:11:51 -08001953static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001954{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001956 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (pkt_dev->odev) {
1960 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001961 pkt_dev->odev = NULL;
1962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Stephen Hemminger39df2322007-03-04 16:11:51 -08001964 odev = dev_get_by_name(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001966 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001967 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001971 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001972 err = -EINVAL;
1973 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001974 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001975 err = -ENETDOWN;
1976 } else {
1977 pkt_dev->odev = odev;
1978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001982 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985/* Read pkt_dev from the interface and set up internal pktgen_dev
1986 * structure to have the right information to create/send packets
1987 */
1988static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1989{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001990 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001991 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1992 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001993 sprintf(pkt_dev->result,
1994 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1995 return;
1996 }
1997
1998 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002000 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002001 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Luiz Capitulino222f1802006-03-20 22:16:13 -08002003 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002004 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Luiz Capitulino222f1802006-03-20 22:16:13 -08002006 /* Set up pkt size */
2007 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2008
2009 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002011 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 * gets exported
2013 */
2014
2015#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002016 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct inet6_dev *idev;
2018
Luiz Capitulino222f1802006-03-20 22:16:13 -08002019 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2020 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 set = 1;
2022 break;
2023 }
2024
Luiz Capitulino222f1802006-03-20 22:16:13 -08002025 if (!set) {
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /*
2028 * Use linklevel address if unconfigured.
2029 *
2030 * use ipv6_get_lladdr if/when it's get exported
2031 */
2032
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002033 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2035 struct inet6_ifaddr *ifp;
2036
2037 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002038 for (ifp = idev->addr_list; ifp;
2039 ifp = ifp->if_next) {
2040 if (ifp->scope == IFA_LINK
2041 && !(ifp->
2042 flags & IFA_F_TENTATIVE)) {
2043 ipv6_addr_copy(&pkt_dev->
2044 cur_in6_saddr,
2045 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 err = 0;
2047 break;
2048 }
2049 }
2050 read_unlock_bh(&idev->lock);
2051 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002052 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002053 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002054 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2055 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002058 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 pkt_dev->saddr_min = 0;
2060 pkt_dev->saddr_max = 0;
2061 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002062
2063 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002066 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (in_dev) {
2068 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002069 pkt_dev->saddr_min =
2070 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 pkt_dev->saddr_max = pkt_dev->saddr_min;
2072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002075 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2077 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2078 }
2079
2080 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2081 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2082 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002083 /* Initialize current values. */
2084 pkt_dev->cur_dst_mac_offset = 0;
2085 pkt_dev->cur_src_mac_offset = 0;
2086 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2087 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2088 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2089 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 pkt_dev->nflows = 0;
2091}
2092
2093static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2094{
2095 __u64 start;
2096 __u64 now;
2097
2098 start = now = getCurUs();
2099 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2100 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002101 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002102 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002103 schedule_timeout_interruptible(1);
2104 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 do_softirq();
2106 if (!pkt_dev->running)
2107 return;
2108 if (need_resched())
2109 schedule();
2110 }
2111
2112 now = getCurUs();
2113 }
2114
2115 pkt_dev->idle_acc += now - start;
2116}
2117
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002118static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2119{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002120 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002121 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2122 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2123 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2124}
2125
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002126static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2127{
2128
2129 if (pkt_dev->flows[flow].flags & F_INIT)
2130 return 1;
2131 else
2132 return 0;
2133}
2134
2135static inline int f_pick(struct pktgen_dev *pkt_dev)
2136{
2137 int flow = pkt_dev->curfl;
2138
2139 if (pkt_dev->flags & F_FLOW_SEQ) {
2140 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2141 /* reset time */
2142 pkt_dev->flows[flow].count = 0;
2143 pkt_dev->curfl += 1;
2144 if (pkt_dev->curfl >= pkt_dev->cflows)
2145 pkt_dev->curfl = 0; /*reset */
2146 }
2147 } else {
2148 flow = random32() % pkt_dev->cflows;
2149
2150 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2151 pkt_dev->flows[flow].count = 0;
2152 }
2153
2154 return pkt_dev->curfl;
2155}
2156
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002157
2158#ifdef CONFIG_XFRM
2159/* If there was already an IPSEC SA, we keep it as is, else
2160 * we go look for it ...
2161*/
2162inline
2163void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2164{
2165 struct xfrm_state *x = pkt_dev->flows[flow].x;
2166 if (!x) {
2167 /*slow path: we dont already have xfrm_state*/
2168 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2169 (xfrm_address_t *)&pkt_dev->cur_saddr,
2170 AF_INET,
2171 pkt_dev->ipsmode,
2172 pkt_dev->ipsproto, 0);
2173 if (x) {
2174 pkt_dev->flows[flow].x = x;
2175 set_pkt_overhead(pkt_dev);
2176 pkt_dev->pkt_overhead+=x->props.header_len;
2177 }
2178
2179 }
2180}
2181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182/* Increment/randomize headers according to flags and current values
2183 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2184 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002185static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2186{
2187 __u32 imn;
2188 __u32 imx;
2189 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002191 if (pkt_dev->cflows)
2192 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002195 if (pkt_dev->src_mac_count > 1) {
2196 __u32 mc;
2197 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Luiz Capitulino222f1802006-03-20 22:16:13 -08002199 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002200 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002201 else {
2202 mc = pkt_dev->cur_src_mac_offset++;
2203 if (pkt_dev->cur_src_mac_offset >
2204 pkt_dev->src_mac_count)
2205 pkt_dev->cur_src_mac_offset = 0;
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Luiz Capitulino222f1802006-03-20 22:16:13 -08002208 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2209 pkt_dev->hh[11] = tmp;
2210 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2211 pkt_dev->hh[10] = tmp;
2212 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2213 pkt_dev->hh[9] = tmp;
2214 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2215 pkt_dev->hh[8] = tmp;
2216 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2217 pkt_dev->hh[7] = tmp;
2218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Luiz Capitulino222f1802006-03-20 22:16:13 -08002220 /* Deal with Destination MAC */
2221 if (pkt_dev->dst_mac_count > 1) {
2222 __u32 mc;
2223 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Luiz Capitulino222f1802006-03-20 22:16:13 -08002225 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002226 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Luiz Capitulino222f1802006-03-20 22:16:13 -08002228 else {
2229 mc = pkt_dev->cur_dst_mac_offset++;
2230 if (pkt_dev->cur_dst_mac_offset >
2231 pkt_dev->dst_mac_count) {
2232 pkt_dev->cur_dst_mac_offset = 0;
2233 }
2234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Luiz Capitulino222f1802006-03-20 22:16:13 -08002236 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2237 pkt_dev->hh[5] = tmp;
2238 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2239 pkt_dev->hh[4] = tmp;
2240 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2241 pkt_dev->hh[3] = tmp;
2242 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2243 pkt_dev->hh[2] = tmp;
2244 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2245 pkt_dev->hh[1] = tmp;
2246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002248 if (pkt_dev->flags & F_MPLS_RND) {
2249 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002250 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002251 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2252 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002253 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002254 htonl(0x000fffff));
2255 }
2256
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002257 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002258 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002259 }
2260
2261 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002262 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002263 }
2264
Luiz Capitulino222f1802006-03-20 22:16:13 -08002265 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2266 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002267 pkt_dev->cur_udp_src = random32() %
2268 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2269 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Luiz Capitulino222f1802006-03-20 22:16:13 -08002271 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 pkt_dev->cur_udp_src++;
2273 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2274 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002275 }
2276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Luiz Capitulino222f1802006-03-20 22:16:13 -08002278 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2279 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002280 pkt_dev->cur_udp_dst = random32() %
2281 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2282 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002283 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002285 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002287 }
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 if (!(pkt_dev->flags & F_IPV6)) {
2291
Luiz Capitulino222f1802006-03-20 22:16:13 -08002292 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2293 ntohl(pkt_dev->
2294 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002296 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002297 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 else {
2299 t = ntohl(pkt_dev->cur_saddr);
2300 t++;
2301 if (t > imx) {
2302 t = imn;
2303 }
2304 }
2305 pkt_dev->cur_saddr = htonl(t);
2306 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002308 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2310 } else {
Al Viro252e33462006-11-14 20:48:11 -08002311 imn = ntohl(pkt_dev->daddr_min);
2312 imx = ntohl(pkt_dev->daddr_max);
2313 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002315 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (pkt_dev->flags & F_IPDST_RND) {
2317
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002318 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002319 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Al Viro252e33462006-11-14 20:48:11 -08002321 while (LOOPBACK(s) || MULTICAST(s)
2322 || BADCLASS(s) || ZERONET(s)
2323 || LOCAL_MCAST(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002324 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002325 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
Al Viro252e33462006-11-14 20:48:11 -08002327 pkt_dev->cur_daddr = s;
2328 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 t = ntohl(pkt_dev->cur_daddr);
2330 t++;
2331 if (t > imx) {
2332 t = imn;
2333 }
2334 pkt_dev->cur_daddr = htonl(t);
2335 }
2336 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002337 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002338 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002339 pkt_dev->flows[flow].cur_daddr =
2340 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002341#ifdef CONFIG_XFRM
2342 if (pkt_dev->flags & F_IPSEC_ON)
2343 get_ipsec_sa(pkt_dev, flow);
2344#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 pkt_dev->nflows++;
2346 }
2347 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002348 } else { /* IPV6 * */
2349
2350 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2351 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2352 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2353 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 else {
2355 int i;
2356
2357 /* Only random destinations yet */
2358
Luiz Capitulino222f1802006-03-20 22:16:13 -08002359 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002361 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002362 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2363 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367
Luiz Capitulino222f1802006-03-20 22:16:13 -08002368 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2369 __u32 t;
2370 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002371 t = random32() %
2372 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2373 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002374 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002376 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002378 }
2379 pkt_dev->cur_pkt_size = t;
2380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 pkt_dev->flows[flow].count++;
2383}
2384
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002385
2386#ifdef CONFIG_XFRM
2387static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2388{
2389 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2390 int err = 0;
2391 struct iphdr *iph;
2392
2393 if (!x)
2394 return 0;
2395 /* XXX: we dont support tunnel mode for now until
2396 * we resolve the dst issue */
2397 if (x->props.mode != XFRM_MODE_TRANSPORT)
2398 return 0;
2399
2400 spin_lock(&x->lock);
2401 iph = ip_hdr(skb);
2402
2403 err = x->mode->output(x, skb);
2404 if (err)
2405 goto error;
2406 err = x->type->output(x, skb);
2407 if (err)
2408 goto error;
2409
2410 x->curlft.bytes +=skb->len;
2411 x->curlft.packets++;
2412 spin_unlock(&x->lock);
2413
2414error:
2415 spin_unlock(&x->lock);
2416 return err;
2417}
2418
2419static inline void free_SAs(struct pktgen_dev *pkt_dev)
2420{
2421 if (pkt_dev->cflows) {
2422 /* let go of the SAs if we have them */
2423 int i = 0;
2424 for (; i < pkt_dev->nflows; i++){
2425 struct xfrm_state *x = pkt_dev->flows[i].x;
2426 if (x) {
2427 xfrm_state_put(x);
2428 pkt_dev->flows[i].x = NULL;
2429 }
2430 }
2431 }
2432}
2433
2434static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2435 struct sk_buff *skb, __be16 protocol)
2436{
2437 if (pkt_dev->flags & F_IPSEC_ON) {
2438 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2439 int nhead = 0;
2440 if (x) {
2441 int ret;
2442 __u8 *eth;
2443 nhead = x->props.header_len - skb_headroom(skb);
2444 if (nhead >0) {
2445 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2446 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002447 printk(KERN_ERR "Error expanding "
2448 "ipsec packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002449 return 0;
2450 }
2451 }
2452
2453 /* ipsec is not expecting ll header */
2454 skb_pull(skb, ETH_HLEN);
2455 ret = pktgen_output_ipsec(skb, pkt_dev);
2456 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002457 printk(KERN_ERR "Error creating ipsec "
2458 "packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002459 kfree_skb(skb);
2460 return 0;
2461 }
2462 /* restore ll */
2463 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2464 memcpy(eth, pkt_dev->hh, 12);
2465 *(u16 *) & eth[12] = protocol;
2466 }
2467 }
2468 return 1;
2469}
2470#endif
2471
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002472static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2473{
2474 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002475 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002476 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2477 }
2478 mpls--;
2479 *mpls |= MPLS_STACK_BOTTOM;
2480}
2481
Al Viro0f37c602006-11-03 03:49:56 -08002482static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2483 unsigned int prio)
2484{
2485 return htons(id | (cfi << 12) | (prio << 13));
2486}
2487
Luiz Capitulino222f1802006-03-20 22:16:13 -08002488static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2489 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 struct sk_buff *skb = NULL;
2492 __u8 *eth;
2493 struct udphdr *udph;
2494 int datalen, iplen;
2495 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002496 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002497 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002498 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002499 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2500 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2501 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2502 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2503
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002504
2505 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002506 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002507
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002508 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002509 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002510
Robert Olsson64053be2005-06-26 15:27:10 -07002511 /* Update any of the values, used when we're incrementing various
2512 * fields.
2513 */
2514 mod_cur_headers(pkt_dev);
2515
David S. Miller7ac54592006-01-18 14:19:10 -08002516 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002517 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002518 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (!skb) {
2520 sprintf(pkt_dev->result, "No memory");
2521 return NULL;
2522 }
2523
David S. Miller7ac54592006-01-18 14:19:10 -08002524 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 /* Reserve for ethernet and IP header */
2527 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002528 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2529 if (pkt_dev->nr_labels)
2530 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002531
2532 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002533 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002534 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002535 *svlan_tci = build_tci(pkt_dev->svlan_id,
2536 pkt_dev->svlan_cfi,
2537 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002538 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002539 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002540 }
2541 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002542 *vlan_tci = build_tci(pkt_dev->vlan_id,
2543 pkt_dev->vlan_cfi,
2544 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002545 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002546 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002547 }
2548
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002549 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002550 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002551 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2552
2553 iph = ip_hdr(skb);
2554 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002557 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002559 /* Eth + IPh + UDPh + mpls */
2560 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002561 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002562 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 udph->source = htons(pkt_dev->cur_udp_src);
2566 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002567 udph->len = htons(datalen + 8); /* DATA + udphdr */
2568 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 iph->ihl = 5;
2571 iph->version = 4;
2572 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002573 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002574 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 iph->saddr = pkt_dev->cur_saddr;
2576 iph->daddr = pkt_dev->cur_daddr;
2577 iph->frag_off = 0;
2578 iplen = 20 + 8 + datalen;
2579 iph->tot_len = htons(iplen);
2580 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002581 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002582 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002583 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002584 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 skb->dev = odev;
2586 skb->pkt_type = PACKET_HOST;
2587
Luiz Capitulino222f1802006-03-20 22:16:13 -08002588 if (pkt_dev->nfrags <= 0)
2589 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 else {
2591 int frags = pkt_dev->nfrags;
2592 int i;
2593
Luiz Capitulino222f1802006-03-20 22:16:13 -08002594 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (frags > MAX_SKB_FRAGS)
2597 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002598 if (datalen > frags * PAGE_SIZE) {
2599 skb_put(skb, datalen - frags * PAGE_SIZE);
2600 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
2602
2603 i = 0;
2604 while (datalen > 0) {
2605 struct page *page = alloc_pages(GFP_KERNEL, 0);
2606 skb_shinfo(skb)->frags[i].page = page;
2607 skb_shinfo(skb)->frags[i].page_offset = 0;
2608 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002609 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 datalen -= skb_shinfo(skb)->frags[i].size;
2611 skb->len += skb_shinfo(skb)->frags[i].size;
2612 skb->data_len += skb_shinfo(skb)->frags[i].size;
2613 i++;
2614 skb_shinfo(skb)->nr_frags = i;
2615 }
2616
2617 while (i < frags) {
2618 int rem;
2619
2620 if (i == 0)
2621 break;
2622
2623 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2624 if (rem == 0)
2625 break;
2626
2627 skb_shinfo(skb)->frags[i - 1].size -= rem;
2628
Luiz Capitulino222f1802006-03-20 22:16:13 -08002629 skb_shinfo(skb)->frags[i] =
2630 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002632 skb_shinfo(skb)->frags[i].page =
2633 skb_shinfo(skb)->frags[i - 1].page;
2634 skb_shinfo(skb)->frags[i].page_offset +=
2635 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 skb_shinfo(skb)->frags[i].size = rem;
2637 i++;
2638 skb_shinfo(skb)->nr_frags = i;
2639 }
2640 }
2641
Luiz Capitulino222f1802006-03-20 22:16:13 -08002642 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Luiz Capitulino222f1802006-03-20 22:16:13 -08002644 if (pgh) {
2645 struct timeval timestamp;
2646
2647 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2648 pgh->seq_num = htonl(pkt_dev->seq_num);
2649
2650 do_gettimeofday(&timestamp);
2651 pgh->tv_sec = htonl(timestamp.tv_sec);
2652 pgh->tv_usec = htonl(timestamp.tv_usec);
2653 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002654
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002655#ifdef CONFIG_XFRM
2656 if (!process_ipsec(pkt_dev, skb, protocol))
2657 return NULL;
2658#endif
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 return skb;
2661}
2662
2663/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002664 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2666 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002667 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 * Should be candidate for net/ipv4/utils.c
2669 * --ro
2670 */
2671
Luiz Capitulino222f1802006-03-20 22:16:13 -08002672static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
2674 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002675 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 unsigned long u;
2677 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002678 unsigned int prefixlen = 0;
2679 unsigned int suffixlen = 0;
Al Viro252e33462006-11-14 20:48:11 -08002680 __be32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Luiz Capitulino222f1802006-03-20 22:16:13 -08002682 for (i = 0; i < 16; i++)
2683 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 for (;;) {
2686 if (*s == ':') {
2687 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002688 if (s[1] == ':') { /* Found "::", skip to part 2 */
2689 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 len++;
2691 break;
2692 }
2693 s++;
2694 }
2695 {
2696 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002697 u = simple_strtoul(s, &tmp, 16);
2698 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
2700
Luiz Capitulino222f1802006-03-20 22:16:13 -08002701 if (!i)
2702 return 0;
2703 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 /* the last 4 bytes may be written as IPv4 address */
2706
2707 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002708 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2709 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711 ip[prefixlen++] = (u >> 8);
2712 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002713 s += i;
2714 len += i;
2715 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return len;
2717 }
2718
2719/* part 2, after "::" */
2720 for (;;) {
2721 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002722 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 break;
2724 s++;
2725 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002726 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 break;
2728 {
2729 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002730 u = simple_strtol(s, &tmp, 16);
2731 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
2733 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002734 if (*s)
2735 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 break;
2737 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002738 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002740 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2741 sizeof(tmp));
2742 suffixlen += 4;
2743 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 break;
2745 }
2746 suffix[suffixlen++] = (u >> 8);
2747 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002748 s += i;
2749 len += i;
2750 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 break;
2752 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002753 for (i = 0; i < suffixlen; i++)
2754 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return len;
2756}
2757
Luiz Capitulino222f1802006-03-20 22:16:13 -08002758static char tohex(char hexdigit)
2759{
2760 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Luiz Capitulino222f1802006-03-20 22:16:13 -08002763static int fmt_xlong(char *s, unsigned int i)
2764{
2765 char *bak = s;
2766 *s = tohex((i >> 12) & 0xf);
2767 if (s != bak || *s != '0')
2768 ++s;
2769 *s = tohex((i >> 8) & 0xf);
2770 if (s != bak || *s != '0')
2771 ++s;
2772 *s = tohex((i >> 4) & 0xf);
2773 if (s != bak || *s != '0')
2774 ++s;
2775 *s = tohex(i & 0xf);
2776 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
Luiz Capitulino222f1802006-03-20 22:16:13 -08002779static unsigned int fmt_ip6(char *s, const char ip[16])
2780{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 unsigned int len;
2782 unsigned int i;
2783 unsigned int temp;
2784 unsigned int compressing;
2785 int j;
2786
Luiz Capitulino222f1802006-03-20 22:16:13 -08002787 len = 0;
2788 compressing = 0;
2789 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002792 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2793 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2794 temp = strlen(s);
2795 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002798 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2799 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (temp == 0) {
2801 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002802 compressing = 1;
2803 if (j == 0) {
2804 *s++ = ':';
2805 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
2807 }
2808 } else {
2809 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002810 compressing = 0;
2811 *s++ = ':';
2812 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002814 i = fmt_xlong(s, temp);
2815 len += i;
2816 s += i;
2817 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 *s++ = ':';
2819 ++len;
2820 }
2821 }
2822 }
2823 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002824 *s++ = ':';
2825 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002827 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return len;
2829}
2830
Luiz Capitulino222f1802006-03-20 22:16:13 -08002831static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2832 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
2834 struct sk_buff *skb = NULL;
2835 __u8 *eth;
2836 struct udphdr *udph;
2837 int datalen;
2838 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002839 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002840 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002841 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002842 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2843 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2844 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2845 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002846
2847 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002848 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002849
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002850 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002851 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002852
Robert Olsson64053be2005-06-26 15:27:10 -07002853 /* Update any of the values, used when we're incrementing various
2854 * fields.
2855 */
2856 mod_cur_headers(pkt_dev);
2857
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002858 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002859 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (!skb) {
2861 sprintf(pkt_dev->result, "No memory");
2862 return NULL;
2863 }
2864
2865 skb_reserve(skb, 16);
2866
2867 /* Reserve for ethernet and IP header */
2868 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002869 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2870 if (pkt_dev->nr_labels)
2871 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002872
2873 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002874 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002875 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002876 *svlan_tci = build_tci(pkt_dev->svlan_id,
2877 pkt_dev->svlan_cfi,
2878 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002879 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002880 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002881 }
2882 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002883 *vlan_tci = build_tci(pkt_dev->vlan_id,
2884 pkt_dev->vlan_cfi,
2885 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002886 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002887 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002888 }
2889
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002890 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002891 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002892 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2893
2894 iph = ipv6_hdr(skb);
2895 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002898 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002899
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002900 /* Eth + IPh + UDPh + mpls */
2901 datalen = pkt_dev->cur_pkt_size - 14 -
2902 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002903 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Luiz Capitulino222f1802006-03-20 22:16:13 -08002905 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 datalen = sizeof(struct pktgen_hdr);
2907 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002908 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2909 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911
2912 udph->source = htons(pkt_dev->cur_udp_src);
2913 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002914 udph->len = htons(datalen + sizeof(struct udphdr));
2915 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002917 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002919 if (pkt_dev->traffic_class) {
2920 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002921 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002922 }
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 iph->hop_limit = 32;
2925
2926 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2927 iph->nexthdr = IPPROTO_UDP;
2928
2929 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2930 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2931
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002932 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002933 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002934 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 skb->dev = odev;
2936 skb->pkt_type = PACKET_HOST;
2937
Luiz Capitulino222f1802006-03-20 22:16:13 -08002938 if (pkt_dev->nfrags <= 0)
2939 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 else {
2941 int frags = pkt_dev->nfrags;
2942 int i;
2943
Luiz Capitulino222f1802006-03-20 22:16:13 -08002944 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (frags > MAX_SKB_FRAGS)
2947 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002948 if (datalen > frags * PAGE_SIZE) {
2949 skb_put(skb, datalen - frags * PAGE_SIZE);
2950 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
2952
2953 i = 0;
2954 while (datalen > 0) {
2955 struct page *page = alloc_pages(GFP_KERNEL, 0);
2956 skb_shinfo(skb)->frags[i].page = page;
2957 skb_shinfo(skb)->frags[i].page_offset = 0;
2958 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002959 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 datalen -= skb_shinfo(skb)->frags[i].size;
2961 skb->len += skb_shinfo(skb)->frags[i].size;
2962 skb->data_len += skb_shinfo(skb)->frags[i].size;
2963 i++;
2964 skb_shinfo(skb)->nr_frags = i;
2965 }
2966
2967 while (i < frags) {
2968 int rem;
2969
2970 if (i == 0)
2971 break;
2972
2973 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2974 if (rem == 0)
2975 break;
2976
2977 skb_shinfo(skb)->frags[i - 1].size -= rem;
2978
Luiz Capitulino222f1802006-03-20 22:16:13 -08002979 skb_shinfo(skb)->frags[i] =
2980 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002982 skb_shinfo(skb)->frags[i].page =
2983 skb_shinfo(skb)->frags[i - 1].page;
2984 skb_shinfo(skb)->frags[i].page_offset +=
2985 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 skb_shinfo(skb)->frags[i].size = rem;
2987 i++;
2988 skb_shinfo(skb)->nr_frags = i;
2989 }
2990 }
2991
Luiz Capitulino222f1802006-03-20 22:16:13 -08002992 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002994 if (pgh) {
2995 struct timeval timestamp;
2996
2997 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2998 pgh->seq_num = htonl(pkt_dev->seq_num);
2999
3000 do_gettimeofday(&timestamp);
3001 pgh->tv_sec = htonl(timestamp.tv_sec);
3002 pgh->tv_usec = htonl(timestamp.tv_usec);
3003 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003004 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return skb;
3007}
3008
Luiz Capitulino222f1802006-03-20 22:16:13 -08003009static inline struct sk_buff *fill_packet(struct net_device *odev,
3010 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003012 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return fill_packet_ipv6(odev, pkt_dev);
3014 else
3015 return fill_packet_ipv4(odev, pkt_dev);
3016}
3017
Luiz Capitulino222f1802006-03-20 22:16:13 -08003018static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003020 pkt_dev->seq_num = 1;
3021 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003023 pkt_dev->tx_bytes = 0;
3024 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
3027/* Set up structure for sending pkts, clear counters */
3028
3029static void pktgen_run(struct pktgen_thread *t)
3030{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003031 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 int started = 0;
3033
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003034 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003037 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
3039 /*
3040 * setup odev and create initial packet.
3041 */
3042 pktgen_setup_inject(pkt_dev);
3043
Luiz Capitulino222f1802006-03-20 22:16:13 -08003044 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003046 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 pkt_dev->skb = NULL;
3048 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003049 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 pkt_dev->next_tx_ns = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003051 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 strcpy(pkt_dev->result, "Starting");
3054 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003055 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 strcpy(pkt_dev->result, "Error starting");
3057 }
3058 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003059 if (started)
3060 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
3063static void pktgen_stop_all_threads_ifs(void)
3064{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003065 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003067 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003069 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003070
3071 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003072 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003073
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003074 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075}
3076
Luiz Capitulino222f1802006-03-20 22:16:13 -08003077static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003079 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003080 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003082 list_for_each_entry(pkt_dev, &t->if_list, list)
3083 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 res = 1;
3085 break;
3086 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003087 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Luiz Capitulino222f1802006-03-20 22:16:13 -08003090static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003092 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Luiz Capitulino222f1802006-03-20 22:16:13 -08003094 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Luiz Capitulino222f1802006-03-20 22:16:13 -08003096 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Luiz Capitulino222f1802006-03-20 22:16:13 -08003098 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Luiz Capitulino222f1802006-03-20 22:16:13 -08003100 if (signal_pending(current))
3101 goto signal;
3102 if_lock(t);
3103 }
3104 if_unlock(t);
3105 return 1;
3106signal:
3107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
3110static int pktgen_wait_all_threads_run(void)
3111{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003112 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003114
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003115 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003116
3117 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003119 if (sig == 0)
3120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003122
3123 if (sig == 0)
3124 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003126
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003127 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return sig;
3129}
3130
3131static void pktgen_run_all_threads(void)
3132{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003133 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003135 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003137 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003139 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003141
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003142 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 pktgen_wait_all_threads_run();
3147}
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3150{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151 __u64 total_us, bps, mbps, pps, idle;
3152 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3159 (unsigned long long)total_us,
3160 (unsigned long long)(total_us - idle),
3161 (unsigned long long)idle,
3162 (unsigned long long)pkt_dev->sofar,
3163 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Luiz Capitulino222f1802006-03-20 22:16:13 -08003165 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Luiz Capitulino222f1802006-03-20 22:16:13 -08003167 while ((total_us >> 32) != 0) {
3168 pps >>= 1;
3169 total_us >>= 1;
3170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Luiz Capitulino222f1802006-03-20 22:16:13 -08003172 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Luiz Capitulino222f1802006-03-20 22:16:13 -08003174 bps = pps * 8 * pkt_dev->cur_pkt_size;
3175
3176 mbps = bps;
3177 do_div(mbps, 1000000);
3178 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3179 (unsigned long long)pps,
3180 (unsigned long long)mbps,
3181 (unsigned long long)bps,
3182 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185/* Set stopped-at timer, remove from running list, do counters & statistics */
3186
Luiz Capitulino222f1802006-03-20 22:16:13 -08003187static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003189 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003190
Luiz Capitulino222f1802006-03-20 22:16:13 -08003191 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003192 printk(KERN_WARNING "pktgen: interface: %s is already "
3193 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003194 return -EINVAL;
3195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Luiz Capitulino222f1802006-03-20 22:16:13 -08003197 pkt_dev->stopped_at = getCurUs();
3198 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003200 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Luiz Capitulino222f1802006-03-20 22:16:13 -08003202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003207 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 if_lock(t);
3210
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003211 list_for_each_entry(pkt_dev, &t->if_list, list) {
3212 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003213 continue;
3214 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003215 best = pkt_dev;
3216 else if (pkt_dev->next_tx_us < best->next_tx_us)
3217 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
3219 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003220 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Luiz Capitulino222f1802006-03-20 22:16:13 -08003223static void pktgen_stop(struct pktgen_thread *t)
3224{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003225 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003227 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Luiz Capitulino222f1802006-03-20 22:16:13 -08003229 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003231 list_for_each_entry(pkt_dev, &t->if_list, list) {
3232 pktgen_stop_device(pkt_dev);
3233 if (pkt_dev->skb)
3234 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003235
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003236 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Luiz Capitulino222f1802006-03-20 22:16:13 -08003239 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003242/*
3243 * one of our devices needs to be removed - find it
3244 * and remove it
3245 */
3246static void pktgen_rem_one_if(struct pktgen_thread *t)
3247{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003248 struct list_head *q, *n;
3249 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003250
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003251 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003252
3253 if_lock(t);
3254
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003255 list_for_each_safe(q, n, &t->if_list) {
3256 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003257
Luiz Capitulino222f1802006-03-20 22:16:13 -08003258 if (!cur->removal_mark)
3259 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003260
3261 if (cur->skb)
3262 kfree_skb(cur->skb);
3263 cur->skb = NULL;
3264
3265 pktgen_remove_device(t, cur);
3266
3267 break;
3268 }
3269
3270 if_unlock(t);
3271}
3272
Luiz Capitulino222f1802006-03-20 22:16:13 -08003273static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003275 struct list_head *q, *n;
3276 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003277
3278 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003279
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003280 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003281 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003283 list_for_each_safe(q, n, &t->if_list) {
3284 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003285
3286 if (cur->skb)
3287 kfree_skb(cur->skb);
3288 cur->skb = NULL;
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 pktgen_remove_device(t, cur);
3291 }
3292
Luiz Capitulino222f1802006-03-20 22:16:13 -08003293 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Luiz Capitulino222f1802006-03-20 22:16:13 -08003296static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003298 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
David S. Milleree74baa2007-01-01 20:51:53 -08003300 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003302 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003304 list_del(&t->th_list);
3305
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003306 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
3309static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3310{
3311 struct net_device *odev = NULL;
3312 __u64 idle_start = 0;
3313 int ret;
3314
3315 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3318 u64 now;
3319
3320 now = getCurUs();
3321 if (now < pkt_dev->next_tx_us)
3322 spin(pkt_dev, pkt_dev->next_tx_us);
3323
3324 /* This is max DELAY, this has special meaning of
3325 * "never transmit"
3326 */
3327 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3328 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3329 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3330 goto out;
3331 }
3332 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003333
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003334 if ((netif_queue_stopped(odev) ||
3335 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) ||
3336 need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 if (!netif_running(odev)) {
3340 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003341 if (pkt_dev->skb)
3342 kfree_skb(pkt_dev->skb);
3343 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 goto out;
3345 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003346 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003350
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003351 if (netif_queue_stopped(odev) ||
3352 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003353 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003355 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
3357 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003360 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3361 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003363 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 pkt_dev->skb = fill_packet(odev, pkt_dev);
3367 if (pkt_dev->skb == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003368 printk(KERN_ERR "pktgen: ERROR: couldn't "
3369 "allocate skb in fill_packet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003371 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 goto out;
3373 }
3374 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003375 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
3377 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003378
Herbert Xu932ff272006-06-09 12:20:56 -07003379 netif_tx_lock_bh(odev);
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003380 if (!netif_queue_stopped(odev) &&
3381 !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
3383 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003384 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3386 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003387 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 pkt_dev->sofar++;
3389 pkt_dev->seq_num++;
3390 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003391
3392 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 && (odev->features & NETIF_F_LLTX)) {
3394 cpu_relax();
3395 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003396 } else { /* Retry it next time */
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 if (debug && net_ratelimit())
3401 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 pkt_dev->errors++;
3404 pkt_dev->last_ok = 0;
3405 }
3406
3407 pkt_dev->next_tx_us = getCurUs();
3408 pkt_dev->next_tx_ns = 0;
3409
3410 pkt_dev->next_tx_us += pkt_dev->delay_us;
3411 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3412
3413 if (pkt_dev->next_tx_ns > 1000) {
3414 pkt_dev->next_tx_us++;
3415 pkt_dev->next_tx_ns -= 1000;
3416 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Luiz Capitulino222f1802006-03-20 22:16:13 -08003419 else { /* Retry it next time */
3420 pkt_dev->last_ok = 0;
3421 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Herbert Xu932ff272006-06-09 12:20:56 -07003425 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 /* If pkt_dev->count is zero, then run forever */
3428 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3429 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3430 idle_start = getCurUs();
3431 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3432 if (signal_pending(current)) {
3433 break;
3434 }
3435 schedule();
3436 }
3437 pkt_dev->idle_acc += getCurUs() - idle_start;
3438 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 /* Done with this */
3441 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003442 if (pkt_dev->skb)
3443 kfree_skb(pkt_dev->skb);
3444 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003445 }
3446out:;
3447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003449/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 * Main loop of the thread goes here
3451 */
3452
David S. Milleree74baa2007-01-01 20:51:53 -08003453static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
3455 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003456 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003457 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 u32 max_before_softirq;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003460 u32 tx_since_softirq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
David S. Milleree74baa2007-01-01 20:51:53 -08003462 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464 init_waitqueue_head(&t->queue);
3465
Luiz Capitulino222f1802006-03-20 22:16:13 -08003466 t->pid = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003468 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 max_before_softirq = t->max_before_softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
David S. Milleree74baa2007-01-01 20:51:53 -08003472 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003473
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003474 set_freezable();
3475
David S. Milleree74baa2007-01-01 20:51:53 -08003476 while (!kthread_should_stop()) {
3477 pkt_dev = next_to_run(t);
3478
3479 if (!pkt_dev &&
3480 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3481 == 0) {
3482 prepare_to_wait(&(t->queue), &wait,
3483 TASK_INTERRUPTIBLE);
3484 schedule_timeout(HZ / 10);
3485 finish_wait(&(t->queue), &wait);
3486 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 __set_current_state(TASK_RUNNING);
3489
Luiz Capitulino222f1802006-03-20 22:16:13 -08003490 if (pkt_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 pktgen_xmit(pkt_dev);
3493
3494 /*
3495 * We like to stay RUNNING but must also give
3496 * others fair share.
3497 */
3498
3499 tx_since_softirq += pkt_dev->last_ok;
3500
3501 if (tx_since_softirq > max_before_softirq) {
3502 if (local_softirq_pending())
3503 do_softirq();
3504 tx_since_softirq = 0;
3505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
3507
Luiz Capitulino222f1802006-03-20 22:16:13 -08003508 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 pktgen_stop(t);
3510 t->control &= ~(T_STOP);
3511 }
3512
Luiz Capitulino222f1802006-03-20 22:16:13 -08003513 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 pktgen_run(t);
3515 t->control &= ~(T_RUN);
3516 }
3517
Luiz Capitulino222f1802006-03-20 22:16:13 -08003518 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003520 t->control &= ~(T_REMDEVALL);
3521 }
3522
Luiz Capitulino222f1802006-03-20 22:16:13 -08003523 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003524 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 t->control &= ~(T_REMDEV);
3526 }
3527
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003528 try_to_freeze();
3529
David S. Milleree74baa2007-01-01 20:51:53 -08003530 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003533 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003534 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003536 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003537 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003539 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003540 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003541
David S. Milleree74baa2007-01-01 20:51:53 -08003542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
Luiz Capitulino222f1802006-03-20 22:16:13 -08003545static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3546 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003548 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003549 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003551 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003552 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003553 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003554 break;
3555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Luiz Capitulino222f1802006-03-20 22:16:13 -08003557 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003558 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003559 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003562/*
3563 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 */
3565
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566static int add_dev_to_thread(struct pktgen_thread *t,
3567 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
3569 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Luiz Capitulino222f1802006-03-20 22:16:13 -08003571 if_lock(t);
3572
3573 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003574 printk(KERN_ERR "pktgen: ERROR: already assigned "
3575 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003576 rv = -EBUSY;
3577 goto out;
3578 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003579
3580 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003581 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 pkt_dev->running = 0;
3583
Luiz Capitulino222f1802006-03-20 22:16:13 -08003584out:
3585 if_unlock(t);
3586 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
3589/* Called under thread lock */
3590
Luiz Capitulino222f1802006-03-20 22:16:13 -08003591static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003593 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003594 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 /* We don't allow a device to be on several threads */
3597
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003598 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3599 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003600 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003601 return -EBUSY;
3602 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003603
3604 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3605 if (!pkt_dev)
3606 return -ENOMEM;
3607
Luiz Capitulino222f1802006-03-20 22:16:13 -08003608 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003609 if (pkt_dev->flows == NULL) {
3610 kfree(pkt_dev);
3611 return -ENOMEM;
3612 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003613 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003614
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003615 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003616 pkt_dev->min_pkt_size = ETH_ZLEN;
3617 pkt_dev->max_pkt_size = ETH_ZLEN;
3618 pkt_dev->nfrags = 0;
3619 pkt_dev->clone_skb = pg_clone_skb_d;
3620 pkt_dev->delay_us = pg_delay_d / 1000;
3621 pkt_dev->delay_ns = pg_delay_d % 1000;
3622 pkt_dev->count = pg_count_d;
3623 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003624 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003625 pkt_dev->udp_src_max = 9;
3626 pkt_dev->udp_dst_min = 9;
3627 pkt_dev->udp_dst_max = 9;
3628
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003629 pkt_dev->vlan_p = 0;
3630 pkt_dev->vlan_cfi = 0;
3631 pkt_dev->vlan_id = 0xffff;
3632 pkt_dev->svlan_p = 0;
3633 pkt_dev->svlan_cfi = 0;
3634 pkt_dev->svlan_id = 0xffff;
3635
Stephen Hemminger39df2322007-03-04 16:11:51 -08003636 err = pktgen_setup_dev(pkt_dev, ifname);
3637 if (err)
3638 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003639
Stephen Hemminger39df2322007-03-04 16:11:51 -08003640 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3641 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003642 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003643 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003644 err = -EINVAL;
3645 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003646 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08003647 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3648 pkt_dev->entry->data = pkt_dev;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003649#ifdef CONFIG_XFRM
3650 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3651 pkt_dev->ipsproto = IPPROTO_ESP;
3652#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003653
3654 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003655out2:
3656 dev_put(pkt_dev->odev);
3657out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003658#ifdef CONFIG_XFRM
3659 free_SAs(pkt_dev);
3660#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -08003661 if (pkt_dev->flows)
3662 vfree(pkt_dev->flows);
3663 kfree(pkt_dev);
3664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665}
3666
David S. Milleree74baa2007-01-01 20:51:53 -08003667static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003669 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003670 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003671 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Luiz Capitulino222f1802006-03-20 22:16:13 -08003673 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3674 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003675 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3676 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003677 return -ENOMEM;
3678 }
3679
Luiz Capitulino222f1802006-03-20 22:16:13 -08003680 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003682
David S. Milleree74baa2007-01-01 20:51:53 -08003683 INIT_LIST_HEAD(&t->if_list);
3684
3685 list_add_tail(&t->th_list, &pktgen_threads);
3686
3687 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3688 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003689 printk(KERN_ERR "pktgen: kernel_thread() failed "
3690 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003691 list_del(&t->th_list);
3692 kfree(t);
3693 return PTR_ERR(p);
3694 }
3695 kthread_bind(p, cpu);
3696 t->tsk = p;
3697
3698 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003699 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003700 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003701 PG_PROC_DIR, t->tsk->comm);
3702 kthread_stop(p);
3703 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003704 kfree(t);
3705 return -EINVAL;
3706 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003707
3708 pe->proc_fops = &pktgen_thread_fops;
3709 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
David S. Milleree74baa2007-01-01 20:51:53 -08003711 wake_up_process(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
3713 return 0;
3714}
3715
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003716/*
3717 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003719static void _rem_dev_from_if_list(struct pktgen_thread *t,
3720 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003722 struct list_head *q, *n;
3723 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003725 list_for_each_safe(q, n, &t->if_list) {
3726 p = list_entry(q, struct pktgen_dev, list);
3727 if (p == pkt_dev)
3728 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 }
3730}
3731
Luiz Capitulino222f1802006-03-20 22:16:13 -08003732static int pktgen_remove_device(struct pktgen_thread *t,
3733 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734{
3735
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003736 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Luiz Capitulino222f1802006-03-20 22:16:13 -08003738 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003739 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3740 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003741 pktgen_stop_device(pkt_dev);
3742 }
3743
3744 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
3746 if (pkt_dev->odev) {
3747 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003748 pkt_dev->odev = NULL;
3749 }
3750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 /* And update the thread if_list */
3752
3753 _rem_dev_from_if_list(t, pkt_dev);
3754
Stephen Hemminger39df2322007-03-04 16:11:51 -08003755 if (pkt_dev->entry)
3756 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003758#ifdef CONFIG_XFRM
3759 free_SAs(pkt_dev);
3760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 if (pkt_dev->flows)
3762 vfree(pkt_dev->flows);
3763 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765}
3766
Luiz Capitulino222f1802006-03-20 22:16:13 -08003767static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
3769 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003770 struct proc_dir_entry *pe;
3771
David S. Miller25a8b252007-07-30 16:11:48 -07003772 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003774 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3775 if (!pg_proc_dir)
3776 return -ENODEV;
3777 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003779 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003780 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003781 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3782 "procfs entry.\n", PGCTRL);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003783 proc_net_remove(PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003784 return -EINVAL;
3785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
Luiz Capitulino222f1802006-03-20 22:16:13 -08003787 pe->proc_fops = &pktgen_fops;
3788 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 /* Register us to receive netdevice events */
3791 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003792
John Hawkes670c02c2005-10-13 09:30:31 -07003793 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003794 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
David S. Milleree74baa2007-01-01 20:51:53 -08003796 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003797 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003798 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3799 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003800 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003801
3802 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003803 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3804 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003805 unregister_netdevice_notifier(&pktgen_notifier_block);
3806 remove_proc_entry(PGCTRL, pg_proc_dir);
3807 proc_net_remove(PG_PROC_DIR);
3808 return -ENODEV;
3809 }
3810
Luiz Capitulino222f1802006-03-20 22:16:13 -08003811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
3813
3814static void __exit pg_cleanup(void)
3815{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003816 struct pktgen_thread *t;
3817 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 wait_queue_head_t queue;
3819 init_waitqueue_head(&queue);
3820
Luiz Capitulino222f1802006-03-20 22:16:13 -08003821 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003823 list_for_each_safe(q, n, &pktgen_threads) {
3824 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003825 kthread_stop(t->tsk);
3826 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Luiz Capitulino222f1802006-03-20 22:16:13 -08003829 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 unregister_netdevice_notifier(&pktgen_notifier_block);
3831
Luiz Capitulino222f1802006-03-20 22:16:13 -08003832 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003833 remove_proc_entry(PGCTRL, pg_proc_dir);
3834 proc_net_remove(PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
3836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837module_init(pg_init);
3838module_exit(pg_cleanup);
3839
3840MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3841MODULE_DESCRIPTION("Packet Generator tool");
3842MODULE_LICENSE("GPL");
3843module_param(pg_count_d, int, 0);
3844module_param(pg_delay_d, int, 0);
3845module_param(pg_clone_skb_d, int, 0);
3846module_param(debug, int, 0);