blob: 803d0c8826af006047c19e198e7d242192d02b81 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090047 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090068 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
116 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118#include <linux/sys.h>
119#include <linux/types.h>
120#include <linux/module.h>
121#include <linux/moduleparam.h>
122#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800123#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <linux/sched.h>
125#include <linux/slab.h>
126#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <linux/unistd.h>
128#include <linux/string.h>
129#include <linux/ptrace.h>
130#include <linux/errno.h>
131#include <linux/ioport.h>
132#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800133#include <linux/capability.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700134#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#include <linux/delay.h>
136#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800137#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#include <linux/init.h>
139#include <linux/skbuff.h>
140#include <linux/netdevice.h>
141#include <linux/inet.h>
142#include <linux/inetdevice.h>
143#include <linux/rtnetlink.h>
144#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700145#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#include <linux/in.h>
147#include <linux/ip.h>
148#include <linux/ipv6.h>
149#include <linux/udp.h>
150#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700151#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800153#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800154#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#include <net/checksum.h>
156#include <net/ipv6.h>
157#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700158#ifdef CONFIG_XFRM
159#include <net/xfrm.h>
160#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#include <asm/byteorder.h>
162#include <linux/rcupdate.h>
163#include <asm/bitops.h>
164#include <asm/io.h>
165#include <asm/dma.h>
166#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800167#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#include <asm/timex.h>
169
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700170#define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/* The buckets are exponential in 'width' */
173#define LAT_BUCKETS_MAX 32
174#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800175#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800176#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800179#define F_IPSRC_RND (1<<0) /* IP-Src Random */
180#define F_IPDST_RND (1<<1) /* IP-Dst Random */
181#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
182#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
183#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
184#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
185#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
186#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800187#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700188#define F_VID_RND (1<<9) /* Random VLAN ID */
189#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700190#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700191#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800194#define T_TERMINATE (1<<0)
195#define T_STOP (1<<1) /* Stop run */
196#define T_RUN (1<<2) /* Start run */
197#define T_REMDEVALL (1<<3) /* Remove all devs */
198#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* If lock -- can be removed after some work */
201#define if_lock(t) spin_lock(&(t->if_lock));
202#define if_unlock(t) spin_unlock(&(t->if_lock));
203
204/* Used to help with determining the pkts on receive */
205#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700206#define PG_PROC_DIR "pktgen"
207#define PGCTRL "pgctrl"
208static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210#define MAX_CFLOWS 65536
211
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700212#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
213#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
214
Luiz Capitulino222f1802006-03-20 22:16:13 -0800215struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800216 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800217 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700218#ifdef CONFIG_XFRM
219 struct xfrm_state *x;
220#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700221 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700224/* flow flag bits */
225#define F_INIT (1<<0) /* flow has been initialized */
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * Try to keep frequent/infrequent used vars. separated.
230 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800231 struct proc_dir_entry *entry; /* proc file */
232 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800233 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Luiz Capitulino222f1802006-03-20 22:16:13 -0800235 int running; /* if this changes to false, the test will stop */
236
237 /* If min != max, then we will either do a linear iteration, or
238 * we will do a random selection from within the range.
239 */
240 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800241 int removal_mark; /* non-zero => the device is marked for
242 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Luiz Capitulino222f1802006-03-20 22:16:13 -0800244 int min_pkt_size; /* = ETH_ZLEN; */
245 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700246 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800247 int nfrags;
248 __u32 delay_us; /* Default delay */
249 __u32 delay_ns;
250 __u64 count; /* Default No packets to send */
251 __u64 sofar; /* How many pkts we've sent so far */
252 __u64 tx_bytes; /* How many bytes we've transmitted */
253 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Luiz Capitulino222f1802006-03-20 22:16:13 -0800255 /* runtime counters relating to clone_skb */
256 __u64 next_tx_us; /* timestamp of when to tx next */
257 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Luiz Capitulino222f1802006-03-20 22:16:13 -0800259 __u64 allocated_skbs;
260 __u32 clone_count;
261 int last_ok; /* Was last skb sent?
262 * Or a failed transmit of some sort? This will keep
263 * sequence numbers in order, for example.
264 */
265 __u64 started_at; /* micro-seconds */
266 __u64 stopped_at; /* micro-seconds */
267 __u64 idle_acc; /* micro-seconds */
268 __u32 seq_num;
269
270 int clone_skb; /* Use multiple SKBs during packet gen. If this number
271 * is greater than 1, then that many copies of the same
272 * packet will be sent before a new packet is allocated.
273 * For instance, if you want to send 1024 identical packets
274 * before creating a new packet, set clone_skb to 1024.
275 */
276
277 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
278 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281
282 struct in6_addr in6_saddr;
283 struct in6_addr in6_daddr;
284 struct in6_addr cur_in6_daddr;
285 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800287 struct in6_addr min_in6_daddr;
288 struct in6_addr max_in6_daddr;
289 struct in6_addr min_in6_saddr;
290 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Luiz Capitulino222f1802006-03-20 22:16:13 -0800292 /* If we're doing ranges, random or incremental, then this
293 * defines the min/max for those ranges.
294 */
Al Viro252e3342006-11-14 20:48:11 -0800295 __be32 saddr_min; /* inclusive, source IP address */
296 __be32 saddr_max; /* exclusive, source IP address */
297 __be32 daddr_min; /* inclusive, dest IP address */
298 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Luiz Capitulino222f1802006-03-20 22:16:13 -0800300 __u16 udp_src_min; /* inclusive, source UDP port */
301 __u16 udp_src_max; /* exclusive, source UDP port */
302 __u16 udp_dst_min; /* inclusive, dest UDP port */
303 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700305 /* DSCP + ECN */
306 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
307 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
308
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800309 /* MPLS */
310 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
311 __be32 labels[MAX_MPLS_LABELS];
312
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700313 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
314 __u8 vlan_p;
315 __u8 vlan_cfi;
316 __u16 vlan_id; /* 0xffff means no vlan tag */
317
318 __u8 svlan_p;
319 __u8 svlan_cfi;
320 __u16 svlan_id; /* 0xffff means no svlan tag */
321
Luiz Capitulino222f1802006-03-20 22:16:13 -0800322 __u32 src_mac_count; /* How many MACs to iterate through */
323 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Luiz Capitulino222f1802006-03-20 22:16:13 -0800325 unsigned char dst_mac[ETH_ALEN];
326 unsigned char src_mac[ETH_ALEN];
327
328 __u32 cur_dst_mac_offset;
329 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800330 __be32 cur_saddr;
331 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800332 __u16 cur_udp_dst;
333 __u16 cur_udp_src;
334 __u32 cur_pkt_size;
335
336 __u8 hh[14];
337 /* = {
338 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
339
340 We fill in SRC address later
341 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
342 0x08, 0x00
343 };
344 */
345 __u16 pad; /* pad out the hh struct to an even 16 bytes */
346
347 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
348 * are transmitting the same one multiple times
349 */
350 struct net_device *odev; /* The out-going device. Note that the device should
351 * have it's pg_info pointer pointing back to this
352 * device. This will be set when the user specifies
353 * the out-going device name (not when the inject is
354 * started as it used to do.)
355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800357 unsigned cflows; /* Concurrent flows (config) */
358 unsigned lflow; /* Flow length (config) */
359 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700360 unsigned curfl; /* current sequenced flow (state)*/
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700361#ifdef CONFIG_XFRM
362 __u8 ipsmode; /* IPSEC mode (config) */
363 __u8 ipsproto; /* IPSEC type (config) */
364#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800365 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366};
367
368struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800369 __be32 pgh_magic;
370 __be32 seq_num;
371 __be32 tv_sec;
372 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
375struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800376 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800377 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800378 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800379 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800380 char result[512];
381 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Luiz Capitulino222f1802006-03-20 22:16:13 -0800383 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
384
385 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int cpu;
387
Luiz Capitulino222f1802006-03-20 22:16:13 -0800388 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389};
390
391#define REMOVE 1
392#define FIND 0
393
394/* This code works around the fact that do_div cannot handle two 64-bit
395 numbers, and regular 64-bit division doesn't work on x86 kernels.
396 --Ben
397*/
398
399#define PG_DIV 0
400
401/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
402 * Function copied/adapted/optimized from:
403 *
404 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
405 *
406 * Copyright 1994, University of Cambridge Computer Laboratory
407 * All Rights Reserved.
408 *
409 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700410static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800412 u64 a = (x < 0) ? -x : x;
413 u64 b = (y < 0) ? -y : y;
414 u64 res = 0, d = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Luiz Capitulino222f1802006-03-20 22:16:13 -0800416 if (b > 0) {
417 while (b < a) {
418 b <<= 1;
419 d <<= 1;
420 }
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Luiz Capitulino222f1802006-03-20 22:16:13 -0800423 do {
424 if (a >= b) {
425 a -= b;
426 res += d;
427 }
428 b >>= 1;
429 d >>= 1;
430 }
431 while (d);
432
433 if (PG_DIV == type) {
434 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
435 } else {
436 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440/* End of hacks to deal with 64-bit math on x86 */
441
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700442/** Convert to milliseconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800443static inline __u64 tv_to_ms(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800445 __u64 ms = tv->tv_usec / 1000;
446 ms += (__u64) tv->tv_sec * (__u64) 1000;
447 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800451static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800453 __u64 us = tv->tv_usec;
454 us += (__u64) tv->tv_sec * (__u64) 1000000;
455 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Luiz Capitulino222f1802006-03-20 22:16:13 -0800458static inline __u64 pg_div(__u64 n, __u32 base)
459{
460 __u64 tmp = n;
461 do_div(tmp, base);
462 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
463 n, base, tmp); */
464 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Luiz Capitulino222f1802006-03-20 22:16:13 -0800467static inline __u64 pg_div64(__u64 n, __u64 base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800469 __u64 tmp = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700471 * How do we know if the architecture we are running on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * supports division with 64 bit base?
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900473 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800475#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Luiz Capitulino222f1802006-03-20 22:16:13 -0800477 do_div(tmp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800479 tmp = divremdi3(n, base, PG_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -0800481 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Luiz Capitulino222f1802006-03-20 22:16:13 -0800484static inline __u64 getCurMs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800486 struct timeval tv;
487 do_gettimeofday(&tv);
488 return tv_to_ms(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Luiz Capitulino222f1802006-03-20 22:16:13 -0800491static inline __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800493 struct timeval tv;
494 do_gettimeofday(&tv);
495 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800500 return tv_to_us(a) - tv_to_us(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/* old include end */
504
505static char version[] __initdata = VERSION;
506
Luiz Capitulino222f1802006-03-20 22:16:13 -0800507static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
508static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
509static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
510 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
512static void pktgen_run_all_threads(void);
513static void pktgen_stop_all_threads_ifs(void);
514static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800515static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800517
Luiz Capitulino222f1802006-03-20 22:16:13 -0800518static unsigned int scan_ip6(const char *s, char ip[16]);
519static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800522static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb92005-12-22 12:51:46 -0800523static int pg_delay_d;
524static int pg_clone_skb_d;
525static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Luiz Capitulino222fa072006-03-20 22:24:27 -0800527static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800528static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530static struct notifier_block pktgen_notifier_block = {
531 .notifier_call = pktgen_device_event,
532};
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900535 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 *
537 */
538
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700539static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800540{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700541 seq_puts(seq, VERSION);
542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Luiz Capitulino222f1802006-03-20 22:16:13 -0800545static ssize_t pgctrl_write(struct file *file, const char __user * buf,
546 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int err = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700549 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Luiz Capitulino222f1802006-03-20 22:16:13 -0800551 if (!capable(CAP_NET_ADMIN)) {
552 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700556 if (count > sizeof(data))
557 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700560 err = -EFAULT;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700561 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800562 }
563 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Luiz Capitulino222f1802006-03-20 22:16:13 -0800565 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 pktgen_stop_all_threads_ifs();
567
Luiz Capitulino222f1802006-03-20 22:16:13 -0800568 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 pktgen_run_all_threads();
570
Luiz Capitulino222f1802006-03-20 22:16:13 -0800571 else
David S. Miller25a8b252007-07-30 16:11:48 -0700572 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 err = count;
575
Luiz Capitulino222f1802006-03-20 22:16:13 -0800576out:
577 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700580static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700582 return single_open(file, pgctrl_show, PDE(inode)->data);
583}
584
Arjan van de Ven9a321442007-02-12 00:55:35 -0800585static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800586 .owner = THIS_MODULE,
587 .open = pgctrl_open,
588 .read = seq_read,
589 .llseek = seq_lseek,
590 .write = pgctrl_write,
591 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700592};
593
594static int pktgen_if_show(struct seq_file *seq, void *v)
595{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800597 struct pktgen_dev *pkt_dev = seq->private;
598 __u64 sa;
599 __u64 stopped;
600 __u64 now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Luiz Capitulino222f1802006-03-20 22:16:13 -0800602 seq_printf(seq,
603 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
604 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
605 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Luiz Capitulino222f1802006-03-20 22:16:13 -0800607 seq_printf(seq,
608 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
609 pkt_dev->nfrags,
610 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800611 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Luiz Capitulino222f1802006-03-20 22:16:13 -0800613 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
614 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Luiz Capitulino222f1802006-03-20 22:16:13 -0800616 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800618 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
619 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
620 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
621 seq_printf(seq,
622 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
623 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Luiz Capitulino222f1802006-03-20 22:16:13 -0800625 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
626 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
627 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
628 seq_printf(seq,
629 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
630 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Luiz Capitulino222f1802006-03-20 22:16:13 -0800632 } else
633 seq_printf(seq,
634 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
635 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
636 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700638 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800640 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -0800641 for (i = 0; i < 6; i++)
642 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
643 i == 5 ? " " : ":");
644 else
645 for (i = 0; i < 6; i++)
646 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
647 i == 5 ? " " : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Luiz Capitulino222f1802006-03-20 22:16:13 -0800649 seq_printf(seq, "dst_mac: ");
650 for (i = 0; i < 6; i++)
651 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
652 i == 5 ? "\n" : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Luiz Capitulino222f1802006-03-20 22:16:13 -0800654 seq_printf(seq,
655 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
656 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
657 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Luiz Capitulino222f1802006-03-20 22:16:13 -0800659 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800660 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700661 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800663 if (pkt_dev->nr_labels) {
664 unsigned i;
665 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700666 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800667 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
668 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
669 }
670
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700671 if (pkt_dev->vlan_id != 0xffff) {
672 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
673 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
674 }
675
676 if (pkt_dev->svlan_id != 0xffff) {
677 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
678 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
679 }
680
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700681 if (pkt_dev->tos) {
682 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
683 }
684
685 if (pkt_dev->traffic_class) {
686 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
687 }
688
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800689 seq_printf(seq, " Flags: ");
690
Luiz Capitulino222f1802006-03-20 22:16:13 -0800691 if (pkt_dev->flags & F_IPV6)
692 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Luiz Capitulino222f1802006-03-20 22:16:13 -0800694 if (pkt_dev->flags & F_IPSRC_RND)
695 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Luiz Capitulino222f1802006-03-20 22:16:13 -0800697 if (pkt_dev->flags & F_IPDST_RND)
698 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Luiz Capitulino222f1802006-03-20 22:16:13 -0800700 if (pkt_dev->flags & F_TXSIZE_RND)
701 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Luiz Capitulino222f1802006-03-20 22:16:13 -0800703 if (pkt_dev->flags & F_UDPSRC_RND)
704 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Luiz Capitulino222f1802006-03-20 22:16:13 -0800706 if (pkt_dev->flags & F_UDPDST_RND)
707 seq_printf(seq, "UDPDST_RND ");
708
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800709 if (pkt_dev->flags & F_MPLS_RND)
710 seq_printf(seq, "MPLS_RND ");
711
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700712 if (pkt_dev->cflows) {
713 if (pkt_dev->flags & F_FLOW_SEQ)
714 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
715 else
716 seq_printf(seq, "FLOW_RND ");
717 }
718
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700719#ifdef CONFIG_XFRM
720 if (pkt_dev->flags & F_IPSEC_ON)
721 seq_printf(seq, "IPSEC ");
722#endif
723
Luiz Capitulino222f1802006-03-20 22:16:13 -0800724 if (pkt_dev->flags & F_MACSRC_RND)
725 seq_printf(seq, "MACSRC_RND ");
726
727 if (pkt_dev->flags & F_MACDST_RND)
728 seq_printf(seq, "MACDST_RND ");
729
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700730 if (pkt_dev->flags & F_VID_RND)
731 seq_printf(seq, "VID_RND ");
732
733 if (pkt_dev->flags & F_SVID_RND)
734 seq_printf(seq, "SVID_RND ");
735
Luiz Capitulino222f1802006-03-20 22:16:13 -0800736 seq_puts(seq, "\n");
737
738 sa = pkt_dev->started_at;
739 stopped = pkt_dev->stopped_at;
740 if (pkt_dev->running)
741 stopped = now; /* not really stopped, more like last-running-at */
742
743 seq_printf(seq,
744 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
745 (unsigned long long)pkt_dev->sofar,
746 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
747 (unsigned long long)stopped,
748 (unsigned long long)pkt_dev->idle_acc);
749
750 seq_printf(seq,
751 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700752 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
753 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Luiz Capitulino222f1802006-03-20 22:16:13 -0800755 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800757 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
758 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
759 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
760 } else
761 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700762 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Luiz Capitulino222f1802006-03-20 22:16:13 -0800764 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700765 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Luiz Capitulino222f1802006-03-20 22:16:13 -0800767 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800770 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800772 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -0700774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800777
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700778static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800779{
780 int i = 0;
781 *num = 0;
782
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700783 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800784 char c;
785 *num <<= 4;
786 if (get_user(c, &user_buffer[i]))
787 return -EFAULT;
788 if ((c >= '0') && (c <= '9'))
789 *num |= c - '0';
790 else if ((c >= 'a') && (c <= 'f'))
791 *num |= c - 'a' + 10;
792 else if ((c >= 'A') && (c <= 'F'))
793 *num |= c - 'A' + 10;
794 else
795 break;
796 }
797 return i;
798}
799
Luiz Capitulino222f1802006-03-20 22:16:13 -0800800static int count_trail_chars(const char __user * user_buffer,
801 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 int i;
804
805 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800806 char c;
807 if (get_user(c, &user_buffer[i]))
808 return -EFAULT;
809 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 case '\"':
811 case '\n':
812 case '\r':
813 case '\t':
814 case ' ':
815 case '=':
816 break;
817 default:
818 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821done:
822 return i;
823}
824
Luiz Capitulino222f1802006-03-20 22:16:13 -0800825static unsigned long num_arg(const char __user * user_buffer,
826 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 int i = 0;
829 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800830
831 for (; i < maxlen; i++) {
832 char c;
833 if (get_user(c, &user_buffer[i]))
834 return -EFAULT;
835 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800837 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 } else
839 break;
840 }
841 return i;
842}
843
Luiz Capitulino222f1802006-03-20 22:16:13 -0800844static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 int i = 0;
847
Luiz Capitulino222f1802006-03-20 22:16:13 -0800848 for (; i < maxlen; i++) {
849 char c;
850 if (get_user(c, &user_buffer[i]))
851 return -EFAULT;
852 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 case '\"':
854 case '\n':
855 case '\r':
856 case '\t':
857 case ' ':
858 goto done_str;
859 break;
860 default:
861 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return i;
866}
867
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800868static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
869{
870 unsigned n = 0;
871 char c;
872 ssize_t i = 0;
873 int len;
874
875 pkt_dev->nr_labels = 0;
876 do {
877 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700878 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800879 if (len <= 0)
880 return len;
881 pkt_dev->labels[n] = htonl(tmp);
882 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
883 pkt_dev->flags |= F_MPLS_RND;
884 i += len;
885 if (get_user(c, &buffer[i]))
886 return -EFAULT;
887 i++;
888 n++;
889 if (n >= MAX_MPLS_LABELS)
890 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700891 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800892
893 pkt_dev->nr_labels = n;
894 return i;
895}
896
Luiz Capitulino222f1802006-03-20 22:16:13 -0800897static ssize_t pktgen_if_write(struct file *file,
898 const char __user * user_buffer, size_t count,
899 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800901 struct seq_file *seq = (struct seq_file *)file->private_data;
902 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 int i = 0, max, len;
904 char name[16], valstr[32];
905 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800906 char *pg_result = NULL;
907 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800909
910 pg_result = &(pkt_dev->result[0]);
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700913 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -EINVAL;
915 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 max = count - i;
918 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800919 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700920 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800921 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800923 i += tmp;
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* Read variable name */
926
927 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800928 if (len < 0) {
929 return len;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800932 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return -EFAULT;
934 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800935
936 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800938 if (len < 0)
939 return len;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 i += len;
942
943 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800944 char tb[count + 1];
945 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800947 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700948 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800949 (unsigned long)count, tb);
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (!strcmp(name, "min_pkt_size")) {
953 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800954 if (len < 0) {
955 return len;
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800958 if (value < 14 + 20 + 8)
959 value = 14 + 20 + 8;
960 if (value != pkt_dev->min_pkt_size) {
961 pkt_dev->min_pkt_size = value;
962 pkt_dev->cur_pkt_size = value;
963 }
964 sprintf(pg_result, "OK: min_pkt_size=%u",
965 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return count;
967 }
968
Luiz Capitulino222f1802006-03-20 22:16:13 -0800969 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800971 if (len < 0) {
972 return len;
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975 if (value < 14 + 20 + 8)
976 value = 14 + 20 + 8;
977 if (value != pkt_dev->max_pkt_size) {
978 pkt_dev->max_pkt_size = value;
979 pkt_dev->cur_pkt_size = value;
980 }
981 sprintf(pg_result, "OK: max_pkt_size=%u",
982 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return count;
984 }
985
Luiz Capitulino222f1802006-03-20 22:16:13 -0800986 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 if (!strcmp(name, "pkt_size")) {
989 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800990 if (len < 0) {
991 return len;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800994 if (value < 14 + 20 + 8)
995 value = 14 + 20 + 8;
996 if (value != pkt_dev->min_pkt_size) {
997 pkt_dev->min_pkt_size = value;
998 pkt_dev->max_pkt_size = value;
999 pkt_dev->cur_pkt_size = value;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
1002 return count;
1003 }
1004
Luiz Capitulino222f1802006-03-20 22:16:13 -08001005 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001007 if (len < 0) {
1008 return len;
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001011 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 sprintf(pg_result, "OK: debug=%u", debug);
1013 return count;
1014 }
1015
Luiz Capitulino222f1802006-03-20 22:16:13 -08001016 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001018 if (len < 0) {
1019 return len;
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 i += len;
1022 pkt_dev->nfrags = value;
1023 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1024 return count;
1025 }
1026 if (!strcmp(name, "delay")) {
1027 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001028 if (len < 0) {
1029 return len;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 i += len;
1032 if (value == 0x7FFFFFFF) {
1033 pkt_dev->delay_us = 0x7FFFFFFF;
1034 pkt_dev->delay_ns = 0;
1035 } else {
1036 pkt_dev->delay_us = value / 1000;
1037 pkt_dev->delay_ns = value % 1000;
1038 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 sprintf(pg_result, "OK: delay=%u",
1040 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return count;
1042 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001043 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 if (len < 0) {
1046 return len;
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001049 if (value != pkt_dev->udp_src_min) {
1050 pkt_dev->udp_src_min = value;
1051 pkt_dev->cur_udp_src = value;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1054 return count;
1055 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001056 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001058 if (len < 0) {
1059 return len;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001062 if (value != pkt_dev->udp_dst_min) {
1063 pkt_dev->udp_dst_min = value;
1064 pkt_dev->cur_udp_dst = value;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1067 return count;
1068 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001069 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001071 if (len < 0) {
1072 return len;
1073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001075 if (value != pkt_dev->udp_src_max) {
1076 pkt_dev->udp_src_max = value;
1077 pkt_dev->cur_udp_src = value;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1080 return count;
1081 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001082 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001084 if (len < 0) {
1085 return len;
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001088 if (value != pkt_dev->udp_dst_max) {
1089 pkt_dev->udp_dst_max = value;
1090 pkt_dev->cur_udp_dst = value;
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1093 return count;
1094 }
1095 if (!strcmp(name, "clone_skb")) {
1096 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001097 if (len < 0) {
1098 return len;
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001101 pkt_dev->clone_skb = value;
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1104 return count;
1105 }
1106 if (!strcmp(name, "count")) {
1107 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001108 if (len < 0) {
1109 return len;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 i += len;
1112 pkt_dev->count = value;
1113 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001114 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return count;
1116 }
1117 if (!strcmp(name, "src_mac_count")) {
1118 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001119 if (len < 0) {
1120 return len;
1121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 i += len;
1123 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001124 pkt_dev->src_mac_count = value;
1125 pkt_dev->cur_src_mac_offset = 0;
1126 }
1127 sprintf(pg_result, "OK: src_mac_count=%d",
1128 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return count;
1130 }
1131 if (!strcmp(name, "dst_mac_count")) {
1132 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001133 if (len < 0) {
1134 return len;
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 i += len;
1137 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001138 pkt_dev->dst_mac_count = value;
1139 pkt_dev->cur_dst_mac_offset = 0;
1140 }
1141 sprintf(pg_result, "OK: dst_mac_count=%d",
1142 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return count;
1144 }
1145 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001146 char f[32];
1147 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001149 if (len < 0) {
1150 return len;
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (copy_from_user(f, &user_buffer[i], len))
1153 return -EFAULT;
1154 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001155 if (strcmp(f, "IPSRC_RND") == 0)
1156 pkt_dev->flags |= F_IPSRC_RND;
1157
1158 else if (strcmp(f, "!IPSRC_RND") == 0)
1159 pkt_dev->flags &= ~F_IPSRC_RND;
1160
1161 else if (strcmp(f, "TXSIZE_RND") == 0)
1162 pkt_dev->flags |= F_TXSIZE_RND;
1163
1164 else if (strcmp(f, "!TXSIZE_RND") == 0)
1165 pkt_dev->flags &= ~F_TXSIZE_RND;
1166
1167 else if (strcmp(f, "IPDST_RND") == 0)
1168 pkt_dev->flags |= F_IPDST_RND;
1169
1170 else if (strcmp(f, "!IPDST_RND") == 0)
1171 pkt_dev->flags &= ~F_IPDST_RND;
1172
1173 else if (strcmp(f, "UDPSRC_RND") == 0)
1174 pkt_dev->flags |= F_UDPSRC_RND;
1175
1176 else if (strcmp(f, "!UDPSRC_RND") == 0)
1177 pkt_dev->flags &= ~F_UDPSRC_RND;
1178
1179 else if (strcmp(f, "UDPDST_RND") == 0)
1180 pkt_dev->flags |= F_UDPDST_RND;
1181
1182 else if (strcmp(f, "!UDPDST_RND") == 0)
1183 pkt_dev->flags &= ~F_UDPDST_RND;
1184
1185 else if (strcmp(f, "MACSRC_RND") == 0)
1186 pkt_dev->flags |= F_MACSRC_RND;
1187
1188 else if (strcmp(f, "!MACSRC_RND") == 0)
1189 pkt_dev->flags &= ~F_MACSRC_RND;
1190
1191 else if (strcmp(f, "MACDST_RND") == 0)
1192 pkt_dev->flags |= F_MACDST_RND;
1193
1194 else if (strcmp(f, "!MACDST_RND") == 0)
1195 pkt_dev->flags &= ~F_MACDST_RND;
1196
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001197 else if (strcmp(f, "MPLS_RND") == 0)
1198 pkt_dev->flags |= F_MPLS_RND;
1199
1200 else if (strcmp(f, "!MPLS_RND") == 0)
1201 pkt_dev->flags &= ~F_MPLS_RND;
1202
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001203 else if (strcmp(f, "VID_RND") == 0)
1204 pkt_dev->flags |= F_VID_RND;
1205
1206 else if (strcmp(f, "!VID_RND") == 0)
1207 pkt_dev->flags &= ~F_VID_RND;
1208
1209 else if (strcmp(f, "SVID_RND") == 0)
1210 pkt_dev->flags |= F_SVID_RND;
1211
1212 else if (strcmp(f, "!SVID_RND") == 0)
1213 pkt_dev->flags &= ~F_SVID_RND;
1214
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001215 else if (strcmp(f, "FLOW_SEQ") == 0)
1216 pkt_dev->flags |= F_FLOW_SEQ;
1217
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001218#ifdef CONFIG_XFRM
1219 else if (strcmp(f, "IPSEC") == 0)
1220 pkt_dev->flags |= F_IPSEC_ON;
1221#endif
1222
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001223 else if (strcmp(f, "!IPV6") == 0)
1224 pkt_dev->flags &= ~F_IPV6;
1225
Luiz Capitulino222f1802006-03-20 22:16:13 -08001226 else {
1227 sprintf(pg_result,
1228 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1229 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001230 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001231 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001232 return count;
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1235 return count;
1236 }
1237 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1238 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001239 if (len < 0) {
1240 return len;
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Luiz Capitulino222f1802006-03-20 22:16:13 -08001243 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001245 buf[len] = 0;
1246 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1247 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1248 strncpy(pkt_dev->dst_min, buf, len);
1249 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1250 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1251 }
1252 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001253 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001254 pkt_dev->dst_min);
1255 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1257 return count;
1258 }
1259 if (!strcmp(name, "dst_max")) {
1260 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001261 if (len < 0) {
1262 return len;
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Luiz Capitulino222f1802006-03-20 22:16:13 -08001265 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return -EFAULT;
1267
Luiz Capitulino222f1802006-03-20 22:16:13 -08001268 buf[len] = 0;
1269 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1270 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1271 strncpy(pkt_dev->dst_max, buf, len);
1272 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1273 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1274 }
1275 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001276 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001277 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 i += len;
1279 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1280 return count;
1281 }
1282 if (!strcmp(name, "dst6")) {
1283 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001284 if (len < 0)
1285 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 pkt_dev->flags |= F_IPV6;
1288
Luiz Capitulino222f1802006-03-20 22:16:13 -08001289 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001291 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001294 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1297
Luiz Capitulino222f1802006-03-20 22:16:13 -08001298 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001299 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Luiz Capitulino222f1802006-03-20 22:16:13 -08001301 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 sprintf(pg_result, "OK: dst6=%s", buf);
1303 return count;
1304 }
1305 if (!strcmp(name, "dst6_min")) {
1306 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 if (len < 0)
1308 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 pkt_dev->flags |= F_IPV6;
1311
Luiz Capitulino222f1802006-03-20 22:16:13 -08001312 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001314 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1320 &pkt_dev->min_in6_daddr);
1321 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001322 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Luiz Capitulino222f1802006-03-20 22:16:13 -08001324 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 sprintf(pg_result, "OK: dst6_min=%s", buf);
1326 return count;
1327 }
1328 if (!strcmp(name, "dst6_max")) {
1329 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001330 if (len < 0)
1331 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 pkt_dev->flags |= F_IPV6;
1334
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001340 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Luiz Capitulino222f1802006-03-20 22:16:13 -08001342 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001343 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Luiz Capitulino222f1802006-03-20 22:16:13 -08001345 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 sprintf(pg_result, "OK: dst6_max=%s", buf);
1347 return count;
1348 }
1349 if (!strcmp(name, "src6")) {
1350 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 if (len < 0)
1352 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 pkt_dev->flags |= F_IPV6;
1355
Luiz Capitulino222f1802006-03-20 22:16:13 -08001356 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001358 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001361 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1364
Luiz Capitulino222f1802006-03-20 22:16:13 -08001365 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001366 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001367
1368 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 sprintf(pg_result, "OK: src6=%s", buf);
1370 return count;
1371 }
1372 if (!strcmp(name, "src_min")) {
1373 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001374 if (len < 0) {
1375 return len;
1376 }
1377 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001379 buf[len] = 0;
1380 if (strcmp(buf, pkt_dev->src_min) != 0) {
1381 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1382 strncpy(pkt_dev->src_min, buf, len);
1383 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1384 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1385 }
1386 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001387 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001388 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 i += len;
1390 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1391 return count;
1392 }
1393 if (!strcmp(name, "src_max")) {
1394 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001395 if (len < 0) {
1396 return len;
1397 }
1398 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001400 buf[len] = 0;
1401 if (strcmp(buf, pkt_dev->src_max) != 0) {
1402 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1403 strncpy(pkt_dev->src_max, buf, len);
1404 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1405 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1406 }
1407 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001408 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001409 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 i += len;
1411 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1412 return count;
1413 }
1414 if (!strcmp(name, "dst_mac")) {
1415 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001416 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001418 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001421 if (len < 0) {
1422 return len;
1423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001425 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -EFAULT;
1427 i += len;
1428
Luiz Capitulino222f1802006-03-20 22:16:13 -08001429 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (*v >= '0' && *v <= '9') {
1431 *m *= 16;
1432 *m += *v - '0';
1433 }
1434 if (*v >= 'A' && *v <= 'F') {
1435 *m *= 16;
1436 *m += *v - 'A' + 10;
1437 }
1438 if (*v >= 'a' && *v <= 'f') {
1439 *m *= 16;
1440 *m += *v - 'a' + 10;
1441 }
1442 if (*v == ':') {
1443 m++;
1444 *m = 0;
1445 }
1446 }
1447
1448 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001449 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1450 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 sprintf(pg_result, "OK: dstmac");
1453 return count;
1454 }
1455 if (!strcmp(name, "src_mac")) {
1456 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001457 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 unsigned char *m = pkt_dev->src_mac;
1459
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001460 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001463 if (len < 0) {
1464 return len;
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001467 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return -EFAULT;
1469 i += len;
1470
Luiz Capitulino222f1802006-03-20 22:16:13 -08001471 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (*v >= '0' && *v <= '9') {
1473 *m *= 16;
1474 *m += *v - '0';
1475 }
1476 if (*v >= 'A' && *v <= 'F') {
1477 *m *= 16;
1478 *m += *v - 'A' + 10;
1479 }
1480 if (*v >= 'a' && *v <= 'f') {
1481 *m *= 16;
1482 *m += *v - 'a' + 10;
1483 }
1484 if (*v == ':') {
1485 m++;
1486 *m = 0;
1487 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001490 /* Set up Src MAC */
1491 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1492 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1493
Luiz Capitulino222f1802006-03-20 22:16:13 -08001494 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return count;
1496 }
1497
Luiz Capitulino222f1802006-03-20 22:16:13 -08001498 if (!strcmp(name, "clear_counters")) {
1499 pktgen_clear_counters(pkt_dev);
1500 sprintf(pg_result, "OK: Clearing counters.\n");
1501 return count;
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 if (!strcmp(name, "flows")) {
1505 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001506 if (len < 0) {
1507 return len;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 i += len;
1510 if (value > MAX_CFLOWS)
1511 value = MAX_CFLOWS;
1512
1513 pkt_dev->cflows = value;
1514 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1515 return count;
1516 }
1517
1518 if (!strcmp(name, "flowlen")) {
1519 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001520 if (len < 0) {
1521 return len;
1522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 i += len;
1524 pkt_dev->lflow = value;
1525 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1526 return count;
1527 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001528
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001529 if (!strcmp(name, "mpls")) {
1530 unsigned n, offset;
1531 len = get_labels(&user_buffer[i], pkt_dev);
1532 if (len < 0) { return len; }
1533 i += len;
1534 offset = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001535 for (n = 0; n < pkt_dev->nr_labels; n++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001536 offset += sprintf(pg_result + offset,
1537 "%08x%s", ntohl(pkt_dev->labels[n]),
1538 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001539
1540 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1541 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1542 pkt_dev->svlan_id = 0xffff;
1543
1544 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001545 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001546 }
1547 return count;
1548 }
1549
1550 if (!strcmp(name, "vlan_id")) {
1551 len = num_arg(&user_buffer[i], 4, &value);
1552 if (len < 0) {
1553 return len;
1554 }
1555 i += len;
1556 if (value <= 4095) {
1557 pkt_dev->vlan_id = value; /* turn on VLAN */
1558
1559 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001560 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001561
1562 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001563 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001564
1565 pkt_dev->nr_labels = 0; /* turn off MPLS */
1566 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1567 } else {
1568 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1569 pkt_dev->svlan_id = 0xffff;
1570
1571 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001572 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001573 }
1574 return count;
1575 }
1576
1577 if (!strcmp(name, "vlan_p")) {
1578 len = num_arg(&user_buffer[i], 1, &value);
1579 if (len < 0) {
1580 return len;
1581 }
1582 i += len;
1583 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1584 pkt_dev->vlan_p = value;
1585 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1586 } else {
1587 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1588 }
1589 return count;
1590 }
1591
1592 if (!strcmp(name, "vlan_cfi")) {
1593 len = num_arg(&user_buffer[i], 1, &value);
1594 if (len < 0) {
1595 return len;
1596 }
1597 i += len;
1598 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1599 pkt_dev->vlan_cfi = value;
1600 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1601 } else {
1602 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1603 }
1604 return count;
1605 }
1606
1607 if (!strcmp(name, "svlan_id")) {
1608 len = num_arg(&user_buffer[i], 4, &value);
1609 if (len < 0) {
1610 return len;
1611 }
1612 i += len;
1613 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1614 pkt_dev->svlan_id = value; /* turn on SVLAN */
1615
1616 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001617 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001618
1619 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001620 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001621
1622 pkt_dev->nr_labels = 0; /* turn off MPLS */
1623 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1624 } else {
1625 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1626 pkt_dev->svlan_id = 0xffff;
1627
1628 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001629 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001630 }
1631 return count;
1632 }
1633
1634 if (!strcmp(name, "svlan_p")) {
1635 len = num_arg(&user_buffer[i], 1, &value);
1636 if (len < 0) {
1637 return len;
1638 }
1639 i += len;
1640 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1641 pkt_dev->svlan_p = value;
1642 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1643 } else {
1644 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1645 }
1646 return count;
1647 }
1648
1649 if (!strcmp(name, "svlan_cfi")) {
1650 len = num_arg(&user_buffer[i], 1, &value);
1651 if (len < 0) {
1652 return len;
1653 }
1654 i += len;
1655 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1656 pkt_dev->svlan_cfi = value;
1657 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1658 } else {
1659 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1660 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001661 return count;
1662 }
1663
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001664 if (!strcmp(name, "tos")) {
1665 __u32 tmp_value = 0;
1666 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1667 if (len < 0) {
1668 return len;
1669 }
1670 i += len;
1671 if (len == 2) {
1672 pkt_dev->tos = tmp_value;
1673 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1674 } else {
1675 sprintf(pg_result, "ERROR: tos must be 00-ff");
1676 }
1677 return count;
1678 }
1679
1680 if (!strcmp(name, "traffic_class")) {
1681 __u32 tmp_value = 0;
1682 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1683 if (len < 0) {
1684 return len;
1685 }
1686 i += len;
1687 if (len == 2) {
1688 pkt_dev->traffic_class = tmp_value;
1689 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1690 } else {
1691 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1692 }
1693 return count;
1694 }
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1697 return -EINVAL;
1698}
1699
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001700static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001702 return single_open(file, pktgen_if_show, PDE(inode)->data);
1703}
1704
Arjan van de Ven9a321442007-02-12 00:55:35 -08001705static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001706 .owner = THIS_MODULE,
1707 .open = pktgen_if_open,
1708 .read = seq_read,
1709 .llseek = seq_lseek,
1710 .write = pktgen_if_write,
1711 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001712};
1713
1714static int pktgen_thread_show(struct seq_file *seq, void *v)
1715{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001716 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001717 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001719 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001721 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
David S. Milleree74baa2007-01-01 20:51:53 -08001722 t->tsk->comm, t->max_before_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Luiz Capitulino222f1802006-03-20 22:16:13 -08001724 seq_printf(seq, "Running: ");
1725
1726 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001727 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001728 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001729 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Luiz Capitulino222f1802006-03-20 22:16:13 -08001731 seq_printf(seq, "\nStopped: ");
1732
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001733 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001734 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001735 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 if (t->result[0])
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001738 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 else
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001740 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Luiz Capitulino222f1802006-03-20 22:16:13 -08001742 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001747static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001748 const char __user * user_buffer,
1749 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001751 struct seq_file *seq = (struct seq_file *)file->private_data;
1752 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int i = 0, max, len, ret;
1754 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001755 char *pg_result;
1756 unsigned long value = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001759 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return -EINVAL;
1761 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 len = count_trail_chars(&user_buffer[i], max);
1765 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001766 return len;
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 i += len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /* Read variable name */
1771
1772 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001774 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 memset(name, 0, sizeof(name));
1777 if (copy_from_user(name, &user_buffer[i], len))
1778 return -EFAULT;
1779 i += len;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001780
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783 if (len < 0)
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001784 return len;
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 i += len;
1787
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001788 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001789 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1790 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Luiz Capitulino222f1802006-03-20 22:16:13 -08001792 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001793 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 ret = -EINVAL;
1795 goto out;
1796 }
1797
1798 pg_result = &(t->result[0]);
1799
Luiz Capitulino222f1802006-03-20 22:16:13 -08001800 if (!strcmp(name, "add_device")) {
1801 char f[32];
1802 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001804 if (len < 0) {
1805 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto out;
1807 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001808 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return -EFAULT;
1810 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001811 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001812 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001813 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001814 ret = count;
1815 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 goto out;
1817 }
1818
Luiz Capitulino222f1802006-03-20 22:16:13 -08001819 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001820 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001821 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001822 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001823 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001825 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto out;
1827 }
1828
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829 if (!strcmp(name, "max_before_softirq")) {
1830 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001831 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001832 t->max_before_softirq = value;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001833 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001834 ret = count;
1835 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto out;
1837 }
1838
1839 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001840out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return ret;
1842}
1843
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001844static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001846 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Arjan van de Ven9a321442007-02-12 00:55:35 -08001849static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001850 .owner = THIS_MODULE,
1851 .open = pktgen_thread_open,
1852 .read = seq_read,
1853 .llseek = seq_lseek,
1854 .write = pktgen_thread_write,
1855 .release = single_release,
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07001856};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001859static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 struct pktgen_thread *t;
1862 struct pktgen_dev *pkt_dev = NULL;
1863
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001864 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 pkt_dev = pktgen_find_dev(t, ifname);
1866 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001867 if (remove) {
1868 if_lock(t);
1869 pkt_dev->removal_mark = 1;
1870 t->control |= T_REMDEV;
1871 if_unlock(t);
1872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 break;
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001876 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001879/*
1880 * mark a device for removal
1881 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001882static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001885 const int max_tries = 10, msec_per_try = 125;
1886 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001887
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001888 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001889 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001890
Luiz Capitulino222f1802006-03-20 22:16:13 -08001891 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001892
1893 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001894 if (pkt_dev == NULL)
1895 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001896
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001897 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001898 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1899 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001900 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001901 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001902
1903 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001904 printk(KERN_ERR "pktgen_mark_device: timed out after "
1905 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001906 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001907 break;
1908 }
1909
1910 }
1911
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001912 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001913}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001914
Stephen Hemminger39df2322007-03-04 16:11:51 -08001915static void pktgen_change_name(struct net_device *dev)
1916{
1917 struct pktgen_thread *t;
1918
1919 list_for_each_entry(t, &pktgen_threads, th_list) {
1920 struct pktgen_dev *pkt_dev;
1921
1922 list_for_each_entry(pkt_dev, &t->if_list, list) {
1923 if (pkt_dev->odev != dev)
1924 continue;
1925
1926 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1927
1928 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1929 pg_proc_dir);
1930 if (!pkt_dev->entry)
1931 printk(KERN_ERR "pktgen: can't move proc "
1932 " entry for '%s'\n", dev->name);
1933 break;
1934 }
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Luiz Capitulino222f1802006-03-20 22:16:13 -08001938static int pktgen_device_event(struct notifier_block *unused,
1939 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001941 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 /* It is OK that we do not hold the group lock right now,
1944 * as we run under the RTNL lock.
1945 */
1946
1947 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001948 case NETDEV_CHANGENAME:
1949 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001953 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 return NOTIFY_DONE;
1958}
1959
1960/* Associate pktgen_dev with a device. */
1961
Stephen Hemminger39df2322007-03-04 16:11:51 -08001962static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001963{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001965 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (pkt_dev->odev) {
1969 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001970 pkt_dev->odev = NULL;
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Stephen Hemminger39df2322007-03-04 16:11:51 -08001973 odev = dev_get_by_name(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001975 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001976 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001980 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001981 err = -EINVAL;
1982 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001983 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001984 err = -ENETDOWN;
1985 } else {
1986 pkt_dev->odev = odev;
1987 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001991 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994/* Read pkt_dev from the interface and set up internal pktgen_dev
1995 * structure to have the right information to create/send packets
1996 */
1997static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1998{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001999 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07002000 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
2001 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002002 sprintf(pkt_dev->result,
2003 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2004 return;
2005 }
2006
2007 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002009 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002010 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Luiz Capitulino222f1802006-03-20 22:16:13 -08002012 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002013 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Luiz Capitulino222f1802006-03-20 22:16:13 -08002015 /* Set up pkt size */
2016 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2017
2018 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002020 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 * gets exported
2022 */
2023
2024#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002025 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 struct inet6_dev *idev;
2027
Luiz Capitulino222f1802006-03-20 22:16:13 -08002028 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2029 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 set = 1;
2031 break;
2032 }
2033
Luiz Capitulino222f1802006-03-20 22:16:13 -08002034 if (!set) {
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /*
2037 * Use linklevel address if unconfigured.
2038 *
2039 * use ipv6_get_lladdr if/when it's get exported
2040 */
2041
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002042 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2044 struct inet6_ifaddr *ifp;
2045
2046 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002047 for (ifp = idev->addr_list; ifp;
2048 ifp = ifp->if_next) {
2049 if (ifp->scope == IFA_LINK
2050 && !(ifp->
2051 flags & IFA_F_TENTATIVE)) {
2052 ipv6_addr_copy(&pkt_dev->
2053 cur_in6_saddr,
2054 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 err = 0;
2056 break;
2057 }
2058 }
2059 read_unlock_bh(&idev->lock);
2060 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002061 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002062 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002063 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2064 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002067 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 pkt_dev->saddr_min = 0;
2069 pkt_dev->saddr_max = 0;
2070 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002071
2072 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002075 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (in_dev) {
2077 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002078 pkt_dev->saddr_min =
2079 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 pkt_dev->saddr_max = pkt_dev->saddr_min;
2081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
2083 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002084 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2086 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2087 }
2088
2089 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2090 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2091 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002092 /* Initialize current values. */
2093 pkt_dev->cur_dst_mac_offset = 0;
2094 pkt_dev->cur_src_mac_offset = 0;
2095 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2096 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2097 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2098 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 pkt_dev->nflows = 0;
2100}
2101
2102static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2103{
2104 __u64 start;
2105 __u64 now;
2106
2107 start = now = getCurUs();
2108 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2109 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002110 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002111 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002112 schedule_timeout_interruptible(1);
2113 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 do_softirq();
2115 if (!pkt_dev->running)
2116 return;
2117 if (need_resched())
2118 schedule();
2119 }
2120
2121 now = getCurUs();
2122 }
2123
2124 pkt_dev->idle_acc += now - start;
2125}
2126
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002127static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2128{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002129 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002130 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2131 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2132 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2133}
2134
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002135static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2136{
2137
2138 if (pkt_dev->flows[flow].flags & F_INIT)
2139 return 1;
2140 else
2141 return 0;
2142}
2143
2144static inline int f_pick(struct pktgen_dev *pkt_dev)
2145{
2146 int flow = pkt_dev->curfl;
2147
2148 if (pkt_dev->flags & F_FLOW_SEQ) {
2149 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2150 /* reset time */
2151 pkt_dev->flows[flow].count = 0;
2152 pkt_dev->curfl += 1;
2153 if (pkt_dev->curfl >= pkt_dev->cflows)
2154 pkt_dev->curfl = 0; /*reset */
2155 }
2156 } else {
2157 flow = random32() % pkt_dev->cflows;
2158
2159 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2160 pkt_dev->flows[flow].count = 0;
2161 }
2162
2163 return pkt_dev->curfl;
2164}
2165
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002166
2167#ifdef CONFIG_XFRM
2168/* If there was already an IPSEC SA, we keep it as is, else
2169 * we go look for it ...
2170*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002171static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002172{
2173 struct xfrm_state *x = pkt_dev->flows[flow].x;
2174 if (!x) {
2175 /*slow path: we dont already have xfrm_state*/
2176 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2177 (xfrm_address_t *)&pkt_dev->cur_saddr,
2178 AF_INET,
2179 pkt_dev->ipsmode,
2180 pkt_dev->ipsproto, 0);
2181 if (x) {
2182 pkt_dev->flows[flow].x = x;
2183 set_pkt_overhead(pkt_dev);
2184 pkt_dev->pkt_overhead+=x->props.header_len;
2185 }
2186
2187 }
2188}
2189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190/* Increment/randomize headers according to flags and current values
2191 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2192 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002193static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2194{
2195 __u32 imn;
2196 __u32 imx;
2197 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002199 if (pkt_dev->cflows)
2200 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002203 if (pkt_dev->src_mac_count > 1) {
2204 __u32 mc;
2205 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Luiz Capitulino222f1802006-03-20 22:16:13 -08002207 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002208 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002209 else {
2210 mc = pkt_dev->cur_src_mac_offset++;
2211 if (pkt_dev->cur_src_mac_offset >
2212 pkt_dev->src_mac_count)
2213 pkt_dev->cur_src_mac_offset = 0;
2214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Luiz Capitulino222f1802006-03-20 22:16:13 -08002216 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2217 pkt_dev->hh[11] = tmp;
2218 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2219 pkt_dev->hh[10] = tmp;
2220 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2221 pkt_dev->hh[9] = tmp;
2222 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2223 pkt_dev->hh[8] = tmp;
2224 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2225 pkt_dev->hh[7] = tmp;
2226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Luiz Capitulino222f1802006-03-20 22:16:13 -08002228 /* Deal with Destination MAC */
2229 if (pkt_dev->dst_mac_count > 1) {
2230 __u32 mc;
2231 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Luiz Capitulino222f1802006-03-20 22:16:13 -08002233 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002234 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Luiz Capitulino222f1802006-03-20 22:16:13 -08002236 else {
2237 mc = pkt_dev->cur_dst_mac_offset++;
2238 if (pkt_dev->cur_dst_mac_offset >
2239 pkt_dev->dst_mac_count) {
2240 pkt_dev->cur_dst_mac_offset = 0;
2241 }
2242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Luiz Capitulino222f1802006-03-20 22:16:13 -08002244 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2245 pkt_dev->hh[5] = tmp;
2246 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2247 pkt_dev->hh[4] = tmp;
2248 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2249 pkt_dev->hh[3] = tmp;
2250 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2251 pkt_dev->hh[2] = tmp;
2252 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2253 pkt_dev->hh[1] = tmp;
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002256 if (pkt_dev->flags & F_MPLS_RND) {
2257 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002258 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002259 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2260 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002261 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002262 htonl(0x000fffff));
2263 }
2264
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002265 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002266 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002267 }
2268
2269 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002270 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002271 }
2272
Luiz Capitulino222f1802006-03-20 22:16:13 -08002273 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2274 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002275 pkt_dev->cur_udp_src = random32() %
2276 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2277 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Luiz Capitulino222f1802006-03-20 22:16:13 -08002279 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 pkt_dev->cur_udp_src++;
2281 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2282 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002283 }
2284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Luiz Capitulino222f1802006-03-20 22:16:13 -08002286 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2287 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002288 pkt_dev->cur_udp_dst = random32() %
2289 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2290 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002291 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002293 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002295 }
2296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 if (!(pkt_dev->flags & F_IPV6)) {
2299
Luiz Capitulino222f1802006-03-20 22:16:13 -08002300 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2301 ntohl(pkt_dev->
2302 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002304 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002305 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 else {
2307 t = ntohl(pkt_dev->cur_saddr);
2308 t++;
2309 if (t > imx) {
2310 t = imn;
2311 }
2312 }
2313 pkt_dev->cur_saddr = htonl(t);
2314 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002315
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002316 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2318 } else {
Al Viro252e3342006-11-14 20:48:11 -08002319 imn = ntohl(pkt_dev->daddr_min);
2320 imx = ntohl(pkt_dev->daddr_max);
2321 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002323 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (pkt_dev->flags & F_IPDST_RND) {
2325
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002326 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002327 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Al Viro252e3342006-11-14 20:48:11 -08002329 while (LOOPBACK(s) || MULTICAST(s)
2330 || BADCLASS(s) || ZERONET(s)
2331 || LOCAL_MCAST(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002332 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002333 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
Al Viro252e3342006-11-14 20:48:11 -08002335 pkt_dev->cur_daddr = s;
2336 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 t = ntohl(pkt_dev->cur_daddr);
2338 t++;
2339 if (t > imx) {
2340 t = imn;
2341 }
2342 pkt_dev->cur_daddr = htonl(t);
2343 }
2344 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002345 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002346 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002347 pkt_dev->flows[flow].cur_daddr =
2348 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002349#ifdef CONFIG_XFRM
2350 if (pkt_dev->flags & F_IPSEC_ON)
2351 get_ipsec_sa(pkt_dev, flow);
2352#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 pkt_dev->nflows++;
2354 }
2355 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002356 } else { /* IPV6 * */
2357
2358 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2359 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2360 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2361 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 else {
2363 int i;
2364
2365 /* Only random destinations yet */
2366
Luiz Capitulino222f1802006-03-20 22:16:13 -08002367 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002369 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002370 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2371 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
Luiz Capitulino222f1802006-03-20 22:16:13 -08002376 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2377 __u32 t;
2378 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002379 t = random32() %
2380 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2381 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002382 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002384 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002386 }
2387 pkt_dev->cur_pkt_size = t;
2388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 pkt_dev->flows[flow].count++;
2391}
2392
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002393
2394#ifdef CONFIG_XFRM
2395static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2396{
2397 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2398 int err = 0;
2399 struct iphdr *iph;
2400
2401 if (!x)
2402 return 0;
2403 /* XXX: we dont support tunnel mode for now until
2404 * we resolve the dst issue */
2405 if (x->props.mode != XFRM_MODE_TRANSPORT)
2406 return 0;
2407
2408 spin_lock(&x->lock);
2409 iph = ip_hdr(skb);
2410
2411 err = x->mode->output(x, skb);
2412 if (err)
2413 goto error;
2414 err = x->type->output(x, skb);
2415 if (err)
2416 goto error;
2417
2418 x->curlft.bytes +=skb->len;
2419 x->curlft.packets++;
2420 spin_unlock(&x->lock);
2421
2422error:
2423 spin_unlock(&x->lock);
2424 return err;
2425}
2426
2427static inline void free_SAs(struct pktgen_dev *pkt_dev)
2428{
2429 if (pkt_dev->cflows) {
2430 /* let go of the SAs if we have them */
2431 int i = 0;
2432 for (; i < pkt_dev->nflows; i++){
2433 struct xfrm_state *x = pkt_dev->flows[i].x;
2434 if (x) {
2435 xfrm_state_put(x);
2436 pkt_dev->flows[i].x = NULL;
2437 }
2438 }
2439 }
2440}
2441
2442static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2443 struct sk_buff *skb, __be16 protocol)
2444{
2445 if (pkt_dev->flags & F_IPSEC_ON) {
2446 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2447 int nhead = 0;
2448 if (x) {
2449 int ret;
2450 __u8 *eth;
2451 nhead = x->props.header_len - skb_headroom(skb);
2452 if (nhead >0) {
2453 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2454 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002455 printk(KERN_ERR "Error expanding "
2456 "ipsec packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002457 return 0;
2458 }
2459 }
2460
2461 /* ipsec is not expecting ll header */
2462 skb_pull(skb, ETH_HLEN);
2463 ret = pktgen_output_ipsec(skb, pkt_dev);
2464 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002465 printk(KERN_ERR "Error creating ipsec "
2466 "packet %d\n",ret);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002467 kfree_skb(skb);
2468 return 0;
2469 }
2470 /* restore ll */
2471 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2472 memcpy(eth, pkt_dev->hh, 12);
2473 *(u16 *) & eth[12] = protocol;
2474 }
2475 }
2476 return 1;
2477}
2478#endif
2479
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002480static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2481{
2482 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002483 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002484 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2485 }
2486 mpls--;
2487 *mpls |= MPLS_STACK_BOTTOM;
2488}
2489
Al Viro0f37c602006-11-03 03:49:56 -08002490static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2491 unsigned int prio)
2492{
2493 return htons(id | (cfi << 12) | (prio << 13));
2494}
2495
Luiz Capitulino222f1802006-03-20 22:16:13 -08002496static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2497 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 struct sk_buff *skb = NULL;
2500 __u8 *eth;
2501 struct udphdr *udph;
2502 int datalen, iplen;
2503 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002504 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002505 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002506 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002507 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2508 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2509 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2510 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2511
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002512
2513 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002514 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002515
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002516 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002517 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002518
Robert Olsson64053be2005-06-26 15:27:10 -07002519 /* Update any of the values, used when we're incrementing various
2520 * fields.
2521 */
2522 mod_cur_headers(pkt_dev);
2523
David S. Miller7ac54592006-01-18 14:19:10 -08002524 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002525 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002526 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (!skb) {
2528 sprintf(pkt_dev->result, "No memory");
2529 return NULL;
2530 }
2531
David S. Miller7ac54592006-01-18 14:19:10 -08002532 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 /* Reserve for ethernet and IP header */
2535 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002536 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2537 if (pkt_dev->nr_labels)
2538 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002539
2540 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002541 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002542 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002543 *svlan_tci = build_tci(pkt_dev->svlan_id,
2544 pkt_dev->svlan_cfi,
2545 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002546 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002547 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002548 }
2549 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002550 *vlan_tci = build_tci(pkt_dev->vlan_id,
2551 pkt_dev->vlan_cfi,
2552 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002553 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002554 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002555 }
2556
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002557 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002558 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002559 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2560
2561 iph = ip_hdr(skb);
2562 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002565 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002567 /* Eth + IPh + UDPh + mpls */
2568 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002569 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002570 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 udph->source = htons(pkt_dev->cur_udp_src);
2574 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002575 udph->len = htons(datalen + 8); /* DATA + udphdr */
2576 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 iph->ihl = 5;
2579 iph->version = 4;
2580 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002581 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002582 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 iph->saddr = pkt_dev->cur_saddr;
2584 iph->daddr = pkt_dev->cur_daddr;
2585 iph->frag_off = 0;
2586 iplen = 20 + 8 + datalen;
2587 iph->tot_len = htons(iplen);
2588 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002589 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002590 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002591 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002592 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 skb->dev = odev;
2594 skb->pkt_type = PACKET_HOST;
2595
Luiz Capitulino222f1802006-03-20 22:16:13 -08002596 if (pkt_dev->nfrags <= 0)
2597 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 else {
2599 int frags = pkt_dev->nfrags;
2600 int i;
2601
Luiz Capitulino222f1802006-03-20 22:16:13 -08002602 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (frags > MAX_SKB_FRAGS)
2605 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002606 if (datalen > frags * PAGE_SIZE) {
2607 skb_put(skb, datalen - frags * PAGE_SIZE);
2608 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610
2611 i = 0;
2612 while (datalen > 0) {
2613 struct page *page = alloc_pages(GFP_KERNEL, 0);
2614 skb_shinfo(skb)->frags[i].page = page;
2615 skb_shinfo(skb)->frags[i].page_offset = 0;
2616 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002617 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 datalen -= skb_shinfo(skb)->frags[i].size;
2619 skb->len += skb_shinfo(skb)->frags[i].size;
2620 skb->data_len += skb_shinfo(skb)->frags[i].size;
2621 i++;
2622 skb_shinfo(skb)->nr_frags = i;
2623 }
2624
2625 while (i < frags) {
2626 int rem;
2627
2628 if (i == 0)
2629 break;
2630
2631 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2632 if (rem == 0)
2633 break;
2634
2635 skb_shinfo(skb)->frags[i - 1].size -= rem;
2636
Luiz Capitulino222f1802006-03-20 22:16:13 -08002637 skb_shinfo(skb)->frags[i] =
2638 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002640 skb_shinfo(skb)->frags[i].page =
2641 skb_shinfo(skb)->frags[i - 1].page;
2642 skb_shinfo(skb)->frags[i].page_offset +=
2643 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 skb_shinfo(skb)->frags[i].size = rem;
2645 i++;
2646 skb_shinfo(skb)->nr_frags = i;
2647 }
2648 }
2649
Luiz Capitulino222f1802006-03-20 22:16:13 -08002650 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Luiz Capitulino222f1802006-03-20 22:16:13 -08002652 if (pgh) {
2653 struct timeval timestamp;
2654
2655 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2656 pgh->seq_num = htonl(pkt_dev->seq_num);
2657
2658 do_gettimeofday(&timestamp);
2659 pgh->tv_sec = htonl(timestamp.tv_sec);
2660 pgh->tv_usec = htonl(timestamp.tv_usec);
2661 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002662
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002663#ifdef CONFIG_XFRM
2664 if (!process_ipsec(pkt_dev, skb, protocol))
2665 return NULL;
2666#endif
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 return skb;
2669}
2670
2671/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002672 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2674 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002675 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 * Should be candidate for net/ipv4/utils.c
2677 * --ro
2678 */
2679
Luiz Capitulino222f1802006-03-20 22:16:13 -08002680static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002683 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 unsigned long u;
2685 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002686 unsigned int prefixlen = 0;
2687 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002688 __be32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Luiz Capitulino222f1802006-03-20 22:16:13 -08002690 for (i = 0; i < 16; i++)
2691 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693 for (;;) {
2694 if (*s == ':') {
2695 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002696 if (s[1] == ':') { /* Found "::", skip to part 2 */
2697 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 len++;
2699 break;
2700 }
2701 s++;
2702 }
2703 {
2704 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002705 u = simple_strtoul(s, &tmp, 16);
2706 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
2708
Luiz Capitulino222f1802006-03-20 22:16:13 -08002709 if (!i)
2710 return 0;
2711 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 /* the last 4 bytes may be written as IPv4 address */
2714
2715 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002716 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2717 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719 ip[prefixlen++] = (u >> 8);
2720 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002721 s += i;
2722 len += i;
2723 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return len;
2725 }
2726
2727/* part 2, after "::" */
2728 for (;;) {
2729 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002730 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 break;
2732 s++;
2733 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002734 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 break;
2736 {
2737 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002738 u = simple_strtol(s, &tmp, 16);
2739 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
2741 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002742 if (*s)
2743 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 break;
2745 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002746 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002748 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2749 sizeof(tmp));
2750 suffixlen += 4;
2751 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 break;
2753 }
2754 suffix[suffixlen++] = (u >> 8);
2755 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002756 s += i;
2757 len += i;
2758 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 break;
2760 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002761 for (i = 0; i < suffixlen; i++)
2762 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return len;
2764}
2765
Luiz Capitulino222f1802006-03-20 22:16:13 -08002766static char tohex(char hexdigit)
2767{
2768 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
Luiz Capitulino222f1802006-03-20 22:16:13 -08002771static int fmt_xlong(char *s, unsigned int i)
2772{
2773 char *bak = s;
2774 *s = tohex((i >> 12) & 0xf);
2775 if (s != bak || *s != '0')
2776 ++s;
2777 *s = tohex((i >> 8) & 0xf);
2778 if (s != bak || *s != '0')
2779 ++s;
2780 *s = tohex((i >> 4) & 0xf);
2781 if (s != bak || *s != '0')
2782 ++s;
2783 *s = tohex(i & 0xf);
2784 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
2786
Luiz Capitulino222f1802006-03-20 22:16:13 -08002787static unsigned int fmt_ip6(char *s, const char ip[16])
2788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 unsigned int len;
2790 unsigned int i;
2791 unsigned int temp;
2792 unsigned int compressing;
2793 int j;
2794
Luiz Capitulino222f1802006-03-20 22:16:13 -08002795 len = 0;
2796 compressing = 0;
2797 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002800 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2801 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2802 temp = strlen(s);
2803 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002806 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2807 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (temp == 0) {
2809 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002810 compressing = 1;
2811 if (j == 0) {
2812 *s++ = ':';
2813 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
2815 }
2816 } else {
2817 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002818 compressing = 0;
2819 *s++ = ':';
2820 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002822 i = fmt_xlong(s, temp);
2823 len += i;
2824 s += i;
2825 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 *s++ = ':';
2827 ++len;
2828 }
2829 }
2830 }
2831 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002832 *s++ = ':';
2833 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002835 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return len;
2837}
2838
Luiz Capitulino222f1802006-03-20 22:16:13 -08002839static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2840 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
2842 struct sk_buff *skb = NULL;
2843 __u8 *eth;
2844 struct udphdr *udph;
2845 int datalen;
2846 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002847 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002848 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002849 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002850 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2851 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2852 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2853 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002854
2855 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002856 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002857
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002858 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002859 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002860
Robert Olsson64053be2005-06-26 15:27:10 -07002861 /* Update any of the values, used when we're incrementing various
2862 * fields.
2863 */
2864 mod_cur_headers(pkt_dev);
2865
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002866 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002867 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (!skb) {
2869 sprintf(pkt_dev->result, "No memory");
2870 return NULL;
2871 }
2872
2873 skb_reserve(skb, 16);
2874
2875 /* Reserve for ethernet and IP header */
2876 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002877 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2878 if (pkt_dev->nr_labels)
2879 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002880
2881 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002882 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002883 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002884 *svlan_tci = build_tci(pkt_dev->svlan_id,
2885 pkt_dev->svlan_cfi,
2886 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002887 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002888 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002889 }
2890 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002891 *vlan_tci = build_tci(pkt_dev->vlan_id,
2892 pkt_dev->vlan_cfi,
2893 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002894 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002895 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002896 }
2897
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002898 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002899 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002900 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2901
2902 iph = ipv6_hdr(skb);
2903 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002906 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002907
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002908 /* Eth + IPh + UDPh + mpls */
2909 datalen = pkt_dev->cur_pkt_size - 14 -
2910 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002911 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Luiz Capitulino222f1802006-03-20 22:16:13 -08002913 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 datalen = sizeof(struct pktgen_hdr);
2915 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002916 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2917 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
2919
2920 udph->source = htons(pkt_dev->cur_udp_src);
2921 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002922 udph->len = htons(datalen + sizeof(struct udphdr));
2923 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002925 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002927 if (pkt_dev->traffic_class) {
2928 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002929 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002930 }
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 iph->hop_limit = 32;
2933
2934 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2935 iph->nexthdr = IPPROTO_UDP;
2936
2937 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2938 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2939
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002940 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002941 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002942 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 skb->dev = odev;
2944 skb->pkt_type = PACKET_HOST;
2945
Luiz Capitulino222f1802006-03-20 22:16:13 -08002946 if (pkt_dev->nfrags <= 0)
2947 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 else {
2949 int frags = pkt_dev->nfrags;
2950 int i;
2951
Luiz Capitulino222f1802006-03-20 22:16:13 -08002952 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (frags > MAX_SKB_FRAGS)
2955 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002956 if (datalen > frags * PAGE_SIZE) {
2957 skb_put(skb, datalen - frags * PAGE_SIZE);
2958 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
2960
2961 i = 0;
2962 while (datalen > 0) {
2963 struct page *page = alloc_pages(GFP_KERNEL, 0);
2964 skb_shinfo(skb)->frags[i].page = page;
2965 skb_shinfo(skb)->frags[i].page_offset = 0;
2966 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002967 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 datalen -= skb_shinfo(skb)->frags[i].size;
2969 skb->len += skb_shinfo(skb)->frags[i].size;
2970 skb->data_len += skb_shinfo(skb)->frags[i].size;
2971 i++;
2972 skb_shinfo(skb)->nr_frags = i;
2973 }
2974
2975 while (i < frags) {
2976 int rem;
2977
2978 if (i == 0)
2979 break;
2980
2981 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2982 if (rem == 0)
2983 break;
2984
2985 skb_shinfo(skb)->frags[i - 1].size -= rem;
2986
Luiz Capitulino222f1802006-03-20 22:16:13 -08002987 skb_shinfo(skb)->frags[i] =
2988 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002990 skb_shinfo(skb)->frags[i].page =
2991 skb_shinfo(skb)->frags[i - 1].page;
2992 skb_shinfo(skb)->frags[i].page_offset +=
2993 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 skb_shinfo(skb)->frags[i].size = rem;
2995 i++;
2996 skb_shinfo(skb)->nr_frags = i;
2997 }
2998 }
2999
Luiz Capitulino222f1802006-03-20 22:16:13 -08003000 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003002 if (pgh) {
3003 struct timeval timestamp;
3004
3005 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3006 pgh->seq_num = htonl(pkt_dev->seq_num);
3007
3008 do_gettimeofday(&timestamp);
3009 pgh->tv_sec = htonl(timestamp.tv_sec);
3010 pgh->tv_usec = htonl(timestamp.tv_usec);
3011 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003012 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return skb;
3015}
3016
Luiz Capitulino222f1802006-03-20 22:16:13 -08003017static inline struct sk_buff *fill_packet(struct net_device *odev,
3018 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003020 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return fill_packet_ipv6(odev, pkt_dev);
3022 else
3023 return fill_packet_ipv4(odev, pkt_dev);
3024}
3025
Luiz Capitulino222f1802006-03-20 22:16:13 -08003026static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003028 pkt_dev->seq_num = 1;
3029 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003031 pkt_dev->tx_bytes = 0;
3032 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
3035/* Set up structure for sending pkts, clear counters */
3036
3037static void pktgen_run(struct pktgen_thread *t)
3038{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003039 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 int started = 0;
3041
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003042 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003045 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047 /*
3048 * setup odev and create initial packet.
3049 */
3050 pktgen_setup_inject(pkt_dev);
3051
Luiz Capitulino222f1802006-03-20 22:16:13 -08003052 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003054 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 pkt_dev->skb = NULL;
3056 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003057 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 pkt_dev->next_tx_ns = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003059 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 strcpy(pkt_dev->result, "Starting");
3062 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003063 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 strcpy(pkt_dev->result, "Error starting");
3065 }
3066 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067 if (started)
3068 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069}
3070
3071static void pktgen_stop_all_threads_ifs(void)
3072{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003073 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003075 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003077 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003078
3079 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003080 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003081
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003082 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
3084
Luiz Capitulino222f1802006-03-20 22:16:13 -08003085static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003087 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003088 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003090 list_for_each_entry(pkt_dev, &t->if_list, list)
3091 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 res = 1;
3093 break;
3094 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003095 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Luiz Capitulino222f1802006-03-20 22:16:13 -08003098static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003100 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Luiz Capitulino222f1802006-03-20 22:16:13 -08003102 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Luiz Capitulino222f1802006-03-20 22:16:13 -08003104 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Luiz Capitulino222f1802006-03-20 22:16:13 -08003106 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Luiz Capitulino222f1802006-03-20 22:16:13 -08003108 if (signal_pending(current))
3109 goto signal;
3110 if_lock(t);
3111 }
3112 if_unlock(t);
3113 return 1;
3114signal:
3115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
3118static int pktgen_wait_all_threads_run(void)
3119{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003120 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003122
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003123 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003124
3125 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003127 if (sig == 0)
3128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003130
3131 if (sig == 0)
3132 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003134
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003135 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 return sig;
3137}
3138
3139static void pktgen_run_all_threads(void)
3140{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003141 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003143 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003145 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003147 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003149
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003150 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 pktgen_wait_all_threads_run();
3155}
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3158{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 __u64 total_us, bps, mbps, pps, idle;
3160 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Luiz Capitulino222f1802006-03-20 22:16:13 -08003162 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Luiz Capitulino222f1802006-03-20 22:16:13 -08003164 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3167 (unsigned long long)total_us,
3168 (unsigned long long)(total_us - idle),
3169 (unsigned long long)idle,
3170 (unsigned long long)pkt_dev->sofar,
3171 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Luiz Capitulino222f1802006-03-20 22:16:13 -08003173 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 while ((total_us >> 32) != 0) {
3176 pps >>= 1;
3177 total_us >>= 1;
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Luiz Capitulino222f1802006-03-20 22:16:13 -08003180 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Luiz Capitulino222f1802006-03-20 22:16:13 -08003182 bps = pps * 8 * pkt_dev->cur_pkt_size;
3183
3184 mbps = bps;
3185 do_div(mbps, 1000000);
3186 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3187 (unsigned long long)pps,
3188 (unsigned long long)mbps,
3189 (unsigned long long)bps,
3190 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193/* Set stopped-at timer, remove from running list, do counters & statistics */
3194
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003197 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003198
Luiz Capitulino222f1802006-03-20 22:16:13 -08003199 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003200 printk(KERN_WARNING "pktgen: interface: %s is already "
3201 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003202 return -EINVAL;
3203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205 pkt_dev->stopped_at = getCurUs();
3206 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003208 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Luiz Capitulino222f1802006-03-20 22:16:13 -08003210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212
Luiz Capitulino222f1802006-03-20 22:16:13 -08003213static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003215 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if_lock(t);
3218
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003219 list_for_each_entry(pkt_dev, &t->if_list, list) {
3220 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003221 continue;
3222 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003223 best = pkt_dev;
3224 else if (pkt_dev->next_tx_us < best->next_tx_us)
3225 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003228 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Luiz Capitulino222f1802006-03-20 22:16:13 -08003231static void pktgen_stop(struct pktgen_thread *t)
3232{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003233 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003235 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003239 list_for_each_entry(pkt_dev, &t->if_list, list) {
3240 pktgen_stop_device(pkt_dev);
3241 if (pkt_dev->skb)
3242 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003243
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003244 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Luiz Capitulino222f1802006-03-20 22:16:13 -08003247 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003250/*
3251 * one of our devices needs to be removed - find it
3252 * and remove it
3253 */
3254static void pktgen_rem_one_if(struct pktgen_thread *t)
3255{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003256 struct list_head *q, *n;
3257 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003258
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003259 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003260
3261 if_lock(t);
3262
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003263 list_for_each_safe(q, n, &t->if_list) {
3264 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003265
Luiz Capitulino222f1802006-03-20 22:16:13 -08003266 if (!cur->removal_mark)
3267 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003268
3269 if (cur->skb)
3270 kfree_skb(cur->skb);
3271 cur->skb = NULL;
3272
3273 pktgen_remove_device(t, cur);
3274
3275 break;
3276 }
3277
3278 if_unlock(t);
3279}
3280
Luiz Capitulino222f1802006-03-20 22:16:13 -08003281static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003283 struct list_head *q, *n;
3284 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003285
3286 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003287
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003288 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003289 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003291 list_for_each_safe(q, n, &t->if_list) {
3292 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003293
3294 if (cur->skb)
3295 kfree_skb(cur->skb);
3296 cur->skb = NULL;
3297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 pktgen_remove_device(t, cur);
3299 }
3300
Luiz Capitulino222f1802006-03-20 22:16:13 -08003301 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303
Luiz Capitulino222f1802006-03-20 22:16:13 -08003304static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003306 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
David S. Milleree74baa2007-01-01 20:51:53 -08003308 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003310 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003312 list_del(&t->th_list);
3313
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003314 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3318{
3319 struct net_device *odev = NULL;
3320 __u64 idle_start = 0;
3321 int ret;
3322
3323 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3326 u64 now;
3327
3328 now = getCurUs();
3329 if (now < pkt_dev->next_tx_us)
3330 spin(pkt_dev, pkt_dev->next_tx_us);
3331
3332 /* This is max DELAY, this has special meaning of
3333 * "never transmit"
3334 */
3335 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3336 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3337 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3338 goto out;
3339 }
3340 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003341
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003342 if ((netif_queue_stopped(odev) ||
Robert Olsson378be2c2007-08-28 15:43:14 -07003343 (pkt_dev->skb &&
3344 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping))) ||
3345 need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (!netif_running(odev)) {
3349 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003350 if (pkt_dev->skb)
3351 kfree_skb(pkt_dev->skb);
3352 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 goto out;
3354 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003355 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003359
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003360 if (netif_queue_stopped(odev) ||
3361 netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003362 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003364 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
3366 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003369 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3370 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003372 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 pkt_dev->skb = fill_packet(odev, pkt_dev);
3376 if (pkt_dev->skb == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003377 printk(KERN_ERR "pktgen: ERROR: couldn't "
3378 "allocate skb in fill_packet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003380 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 goto out;
3382 }
3383 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003384 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
3386 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003387
Herbert Xu932ff272006-06-09 12:20:56 -07003388 netif_tx_lock_bh(odev);
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003389 if (!netif_queue_stopped(odev) &&
3390 !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
3392 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003393 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3395 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003396 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 pkt_dev->sofar++;
3398 pkt_dev->seq_num++;
3399 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003400
3401 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 && (odev->features & NETIF_F_LLTX)) {
3403 cpu_relax();
3404 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003405 } else { /* Retry it next time */
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 if (debug && net_ratelimit())
3410 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 pkt_dev->errors++;
3413 pkt_dev->last_ok = 0;
3414 }
3415
3416 pkt_dev->next_tx_us = getCurUs();
3417 pkt_dev->next_tx_ns = 0;
3418
3419 pkt_dev->next_tx_us += pkt_dev->delay_us;
3420 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3421
3422 if (pkt_dev->next_tx_ns > 1000) {
3423 pkt_dev->next_tx_us++;
3424 pkt_dev->next_tx_ns -= 1000;
3425 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Luiz Capitulino222f1802006-03-20 22:16:13 -08003428 else { /* Retry it next time */
3429 pkt_dev->last_ok = 0;
3430 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Herbert Xu932ff272006-06-09 12:20:56 -07003434 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 /* If pkt_dev->count is zero, then run forever */
3437 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3438 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3439 idle_start = getCurUs();
3440 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3441 if (signal_pending(current)) {
3442 break;
3443 }
3444 schedule();
3445 }
3446 pkt_dev->idle_acc += getCurUs() - idle_start;
3447 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 /* Done with this */
3450 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003451 if (pkt_dev->skb)
3452 kfree_skb(pkt_dev->skb);
3453 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003454 }
3455out:;
3456}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003458/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 * Main loop of the thread goes here
3460 */
3461
David S. Milleree74baa2007-01-01 20:51:53 -08003462static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
3464 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003465 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003466 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 u32 max_before_softirq;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003469 u32 tx_since_softirq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
David S. Milleree74baa2007-01-01 20:51:53 -08003471 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
3473 init_waitqueue_head(&t->queue);
3474
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003475 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 max_before_softirq = t->max_before_softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
David S. Milleree74baa2007-01-01 20:51:53 -08003479 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003480
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003481 set_freezable();
3482
David S. Milleree74baa2007-01-01 20:51:53 -08003483 while (!kthread_should_stop()) {
3484 pkt_dev = next_to_run(t);
3485
3486 if (!pkt_dev &&
3487 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3488 == 0) {
3489 prepare_to_wait(&(t->queue), &wait,
3490 TASK_INTERRUPTIBLE);
3491 schedule_timeout(HZ / 10);
3492 finish_wait(&(t->queue), &wait);
3493 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 __set_current_state(TASK_RUNNING);
3496
Luiz Capitulino222f1802006-03-20 22:16:13 -08003497 if (pkt_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
3499 pktgen_xmit(pkt_dev);
3500
3501 /*
3502 * We like to stay RUNNING but must also give
3503 * others fair share.
3504 */
3505
3506 tx_since_softirq += pkt_dev->last_ok;
3507
3508 if (tx_since_softirq > max_before_softirq) {
3509 if (local_softirq_pending())
3510 do_softirq();
3511 tx_since_softirq = 0;
3512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 }
3514
Luiz Capitulino222f1802006-03-20 22:16:13 -08003515 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 pktgen_stop(t);
3517 t->control &= ~(T_STOP);
3518 }
3519
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 pktgen_run(t);
3522 t->control &= ~(T_RUN);
3523 }
3524
Luiz Capitulino222f1802006-03-20 22:16:13 -08003525 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003527 t->control &= ~(T_REMDEVALL);
3528 }
3529
Luiz Capitulino222f1802006-03-20 22:16:13 -08003530 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003531 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 t->control &= ~(T_REMDEV);
3533 }
3534
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003535 try_to_freeze();
3536
David S. Milleree74baa2007-01-01 20:51:53 -08003537 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003540 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003541 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003543 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003544 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003546 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003547 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003548
David S. Milleree74baa2007-01-01 20:51:53 -08003549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550}
3551
Luiz Capitulino222f1802006-03-20 22:16:13 -08003552static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3553 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003555 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003556 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003558 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003559 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003560 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003561 break;
3562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Luiz Capitulino222f1802006-03-20 22:16:13 -08003564 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003565 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003569/*
3570 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 */
3572
Luiz Capitulino222f1802006-03-20 22:16:13 -08003573static int add_dev_to_thread(struct pktgen_thread *t,
3574 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
3576 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Luiz Capitulino222f1802006-03-20 22:16:13 -08003578 if_lock(t);
3579
3580 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003581 printk(KERN_ERR "pktgen: ERROR: already assigned "
3582 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003583 rv = -EBUSY;
3584 goto out;
3585 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003586
3587 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 pkt_dev->running = 0;
3590
Luiz Capitulino222f1802006-03-20 22:16:13 -08003591out:
3592 if_unlock(t);
3593 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
3596/* Called under thread lock */
3597
Luiz Capitulino222f1802006-03-20 22:16:13 -08003598static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003600 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003601 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 /* We don't allow a device to be on several threads */
3604
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003605 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3606 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003607 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003608 return -EBUSY;
3609 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003610
3611 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3612 if (!pkt_dev)
3613 return -ENOMEM;
3614
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003616 if (pkt_dev->flows == NULL) {
3617 kfree(pkt_dev);
3618 return -ENOMEM;
3619 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003620 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003621
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003622 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003623 pkt_dev->min_pkt_size = ETH_ZLEN;
3624 pkt_dev->max_pkt_size = ETH_ZLEN;
3625 pkt_dev->nfrags = 0;
3626 pkt_dev->clone_skb = pg_clone_skb_d;
3627 pkt_dev->delay_us = pg_delay_d / 1000;
3628 pkt_dev->delay_ns = pg_delay_d % 1000;
3629 pkt_dev->count = pg_count_d;
3630 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003631 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003632 pkt_dev->udp_src_max = 9;
3633 pkt_dev->udp_dst_min = 9;
3634 pkt_dev->udp_dst_max = 9;
3635
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003636 pkt_dev->vlan_p = 0;
3637 pkt_dev->vlan_cfi = 0;
3638 pkt_dev->vlan_id = 0xffff;
3639 pkt_dev->svlan_p = 0;
3640 pkt_dev->svlan_cfi = 0;
3641 pkt_dev->svlan_id = 0xffff;
3642
Stephen Hemminger39df2322007-03-04 16:11:51 -08003643 err = pktgen_setup_dev(pkt_dev, ifname);
3644 if (err)
3645 goto out1;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003646
Stephen Hemminger39df2322007-03-04 16:11:51 -08003647 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3648 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003649 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003650 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003651 err = -EINVAL;
3652 goto out2;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003653 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08003654 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3655 pkt_dev->entry->data = pkt_dev;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003656#ifdef CONFIG_XFRM
3657 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3658 pkt_dev->ipsproto = IPPROTO_ESP;
3659#endif
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003660
3661 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003662out2:
3663 dev_put(pkt_dev->odev);
3664out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003665#ifdef CONFIG_XFRM
3666 free_SAs(pkt_dev);
3667#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -08003668 if (pkt_dev->flows)
3669 vfree(pkt_dev->flows);
3670 kfree(pkt_dev);
3671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672}
3673
David S. Milleree74baa2007-01-01 20:51:53 -08003674static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003676 struct pktgen_thread *t;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003677 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003678 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Luiz Capitulino222f1802006-03-20 22:16:13 -08003680 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3681 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003682 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3683 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003684 return -ENOMEM;
3685 }
3686
Luiz Capitulino222f1802006-03-20 22:16:13 -08003687 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003689
David S. Milleree74baa2007-01-01 20:51:53 -08003690 INIT_LIST_HEAD(&t->if_list);
3691
3692 list_add_tail(&t->th_list, &pktgen_threads);
3693
3694 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3695 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003696 printk(KERN_ERR "pktgen: kernel_thread() failed "
3697 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003698 list_del(&t->th_list);
3699 kfree(t);
3700 return PTR_ERR(p);
3701 }
3702 kthread_bind(p, cpu);
3703 t->tsk = p;
3704
3705 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003706 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003707 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003708 PG_PROC_DIR, t->tsk->comm);
3709 kthread_stop(p);
3710 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003711 kfree(t);
3712 return -EINVAL;
3713 }
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003714
3715 pe->proc_fops = &pktgen_thread_fops;
3716 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
David S. Milleree74baa2007-01-01 20:51:53 -08003718 wake_up_process(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
3720 return 0;
3721}
3722
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003723/*
3724 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003726static void _rem_dev_from_if_list(struct pktgen_thread *t,
3727 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003729 struct list_head *q, *n;
3730 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003732 list_for_each_safe(q, n, &t->if_list) {
3733 p = list_entry(q, struct pktgen_dev, list);
3734 if (p == pkt_dev)
3735 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 }
3737}
3738
Luiz Capitulino222f1802006-03-20 22:16:13 -08003739static int pktgen_remove_device(struct pktgen_thread *t,
3740 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
3742
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003743 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Luiz Capitulino222f1802006-03-20 22:16:13 -08003745 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003746 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3747 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003748 pktgen_stop_device(pkt_dev);
3749 }
3750
3751 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
3753 if (pkt_dev->odev) {
3754 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003755 pkt_dev->odev = NULL;
3756 }
3757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 /* And update the thread if_list */
3759
3760 _rem_dev_from_if_list(t, pkt_dev);
3761
Stephen Hemminger39df2322007-03-04 16:11:51 -08003762 if (pkt_dev->entry)
3763 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003765#ifdef CONFIG_XFRM
3766 free_SAs(pkt_dev);
3767#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 if (pkt_dev->flows)
3769 vfree(pkt_dev->flows);
3770 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
Luiz Capitulino222f1802006-03-20 22:16:13 -08003774static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
3776 int cpu;
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003777 struct proc_dir_entry *pe;
3778
David S. Miller25a8b252007-07-30 16:11:48 -07003779 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003781 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3782 if (!pg_proc_dir)
3783 return -ENODEV;
3784 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003786 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003787 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003788 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3789 "procfs entry.\n", PGCTRL);
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003790 proc_net_remove(PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003791 return -EINVAL;
3792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Luiz Capitulino222f1802006-03-20 22:16:13 -08003794 pe->proc_fops = &pktgen_fops;
3795 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
3797 /* Register us to receive netdevice events */
3798 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003799
John Hawkes670c02c2005-10-13 09:30:31 -07003800 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003801 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
David S. Milleree74baa2007-01-01 20:51:53 -08003803 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003804 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003805 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3806 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003807 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003808
3809 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003810 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3811 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003812 unregister_netdevice_notifier(&pktgen_notifier_block);
3813 remove_proc_entry(PGCTRL, pg_proc_dir);
3814 proc_net_remove(PG_PROC_DIR);
3815 return -ENODEV;
3816 }
3817
Luiz Capitulino222f1802006-03-20 22:16:13 -08003818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819}
3820
3821static void __exit pg_cleanup(void)
3822{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003823 struct pktgen_thread *t;
3824 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 wait_queue_head_t queue;
3826 init_waitqueue_head(&queue);
3827
Luiz Capitulino222f1802006-03-20 22:16:13 -08003828 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003830 list_for_each_safe(q, n, &pktgen_threads) {
3831 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003832 kthread_stop(t->tsk);
3833 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Luiz Capitulino222f1802006-03-20 22:16:13 -08003836 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 unregister_netdevice_notifier(&pktgen_notifier_block);
3838
Luiz Capitulino222f1802006-03-20 22:16:13 -08003839 /* Clean up proc file system */
Stephen Hemmingerd50a6b52005-10-14 15:42:33 -07003840 remove_proc_entry(PGCTRL, pg_proc_dir);
3841 proc_net_remove(PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842}
3843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844module_init(pg_init);
3845module_exit(pg_cleanup);
3846
3847MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3848MODULE_DESCRIPTION("Packet Generator tool");
3849MODULE_LICENSE("GPL");
3850module_param(pg_count_d, int, 0);
3851module_param(pg_delay_d, int, 0);
3852module_param(pg_clone_skb_d, int, 0);
3853module_param(debug, int, 0);