blob: 19b8c20e98a4fdaf997a8d32c21bb1436e49915b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
Jan Engelhardt96de0e22007-10-19 23:21:04 +02009 * Jens Låås <jens.laas@data.slu.se>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090047 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090068 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
116 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118#include <linux/sys.h>
119#include <linux/types.h>
120#include <linux/module.h>
121#include <linux/moduleparam.h>
122#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800123#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <linux/sched.h>
125#include <linux/slab.h>
126#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <linux/unistd.h>
128#include <linux/string.h>
129#include <linux/ptrace.h>
130#include <linux/errno.h>
131#include <linux/ioport.h>
132#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800133#include <linux/capability.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700134#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#include <linux/delay.h>
136#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800137#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#include <linux/init.h>
139#include <linux/skbuff.h>
140#include <linux/netdevice.h>
141#include <linux/inet.h>
142#include <linux/inetdevice.h>
143#include <linux/rtnetlink.h>
144#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700145#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#include <linux/in.h>
147#include <linux/ip.h>
148#include <linux/ipv6.h>
149#include <linux/udp.h>
150#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700151#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800153#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800154#include <linux/kthread.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200155#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#include <net/checksum.h>
157#include <net/ipv6.h>
158#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700159#ifdef CONFIG_XFRM
160#include <net/xfrm.h>
161#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#include <asm/byteorder.h>
163#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700164#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#include <asm/io.h>
166#include <asm/dma.h>
167#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800168#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#include <asm/timex.h>
170
Robert Olssone6fce5b2008-08-07 02:23:01 -0700171#define VERSION "pktgen v2.70: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800174#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800175#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800178#define F_IPSRC_RND (1<<0) /* IP-Src Random */
179#define F_IPDST_RND (1<<1) /* IP-Dst Random */
180#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800186#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700187#define F_VID_RND (1<<9) /* Random VLAN ID */
188#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700189#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700190#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700191#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700192#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800195#define T_TERMINATE (1<<0)
196#define T_STOP (1<<1) /* Stop run */
197#define T_RUN (1<<2) /* Start run */
198#define T_REMDEVALL (1<<3) /* Remove all devs */
199#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/* If lock -- can be removed after some work */
202#define if_lock(t) spin_lock(&(t->if_lock));
203#define if_unlock(t) spin_unlock(&(t->if_lock));
204
205/* Used to help with determining the pkts on receive */
206#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700207#define PG_PROC_DIR "pktgen"
208#define PGCTRL "pgctrl"
209static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211#define MAX_CFLOWS 65536
212
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700213#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
214#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
215
Luiz Capitulino222f1802006-03-20 22:16:13 -0800216struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800217 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800218 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700219#ifdef CONFIG_XFRM
220 struct xfrm_state *x;
221#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700222 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700225/* flow flag bits */
226#define F_INIT (1<<0) /* flow has been initialized */
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /*
230 * Try to keep frequent/infrequent used vars. separated.
231 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800232 struct proc_dir_entry *entry; /* proc file */
233 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800234 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Luiz Capitulino222f1802006-03-20 22:16:13 -0800236 int running; /* if this changes to false, the test will stop */
237
238 /* If min != max, then we will either do a linear iteration, or
239 * we will do a random selection from within the range.
240 */
241 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800242 int removal_mark; /* non-zero => the device is marked for
243 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Luiz Capitulino222f1802006-03-20 22:16:13 -0800245 int min_pkt_size; /* = ETH_ZLEN; */
246 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700247 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800248 int nfrags;
249 __u32 delay_us; /* Default delay */
250 __u32 delay_ns;
251 __u64 count; /* Default No packets to send */
252 __u64 sofar; /* How many pkts we've sent so far */
253 __u64 tx_bytes; /* How many bytes we've transmitted */
254 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Luiz Capitulino222f1802006-03-20 22:16:13 -0800256 /* runtime counters relating to clone_skb */
257 __u64 next_tx_us; /* timestamp of when to tx next */
258 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Luiz Capitulino222f1802006-03-20 22:16:13 -0800260 __u64 allocated_skbs;
261 __u32 clone_count;
262 int last_ok; /* Was last skb sent?
263 * Or a failed transmit of some sort? This will keep
264 * sequence numbers in order, for example.
265 */
266 __u64 started_at; /* micro-seconds */
267 __u64 stopped_at; /* micro-seconds */
268 __u64 idle_acc; /* micro-seconds */
269 __u32 seq_num;
270
271 int clone_skb; /* Use multiple SKBs during packet gen. If this number
272 * is greater than 1, then that many copies of the same
273 * packet will be sent before a new packet is allocated.
274 * For instance, if you want to send 1024 identical packets
275 * before creating a new packet, set clone_skb to 1024.
276 */
277
278 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282
283 struct in6_addr in6_saddr;
284 struct in6_addr in6_daddr;
285 struct in6_addr cur_in6_daddr;
286 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800288 struct in6_addr min_in6_daddr;
289 struct in6_addr max_in6_daddr;
290 struct in6_addr min_in6_saddr;
291 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Luiz Capitulino222f1802006-03-20 22:16:13 -0800293 /* If we're doing ranges, random or incremental, then this
294 * defines the min/max for those ranges.
295 */
Al Viro252e33462006-11-14 20:48:11 -0800296 __be32 saddr_min; /* inclusive, source IP address */
297 __be32 saddr_max; /* exclusive, source IP address */
298 __be32 daddr_min; /* inclusive, dest IP address */
299 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Luiz Capitulino222f1802006-03-20 22:16:13 -0800301 __u16 udp_src_min; /* inclusive, source UDP port */
302 __u16 udp_src_max; /* exclusive, source UDP port */
303 __u16 udp_dst_min; /* inclusive, dest UDP port */
304 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700306 /* DSCP + ECN */
307 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
308 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
309
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800310 /* MPLS */
311 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
312 __be32 labels[MAX_MPLS_LABELS];
313
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700314 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
315 __u8 vlan_p;
316 __u8 vlan_cfi;
317 __u16 vlan_id; /* 0xffff means no vlan tag */
318
319 __u8 svlan_p;
320 __u8 svlan_cfi;
321 __u16 svlan_id; /* 0xffff means no svlan tag */
322
Luiz Capitulino222f1802006-03-20 22:16:13 -0800323 __u32 src_mac_count; /* How many MACs to iterate through */
324 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Luiz Capitulino222f1802006-03-20 22:16:13 -0800326 unsigned char dst_mac[ETH_ALEN];
327 unsigned char src_mac[ETH_ALEN];
328
329 __u32 cur_dst_mac_offset;
330 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800331 __be32 cur_saddr;
332 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800333 __u16 cur_udp_dst;
334 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700335 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800336 __u32 cur_pkt_size;
337
338 __u8 hh[14];
339 /* = {
340 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
341
342 We fill in SRC address later
343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
344 0x08, 0x00
345 };
346 */
347 __u16 pad; /* pad out the hh struct to an even 16 bytes */
348
349 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
350 * are transmitting the same one multiple times
351 */
352 struct net_device *odev; /* The out-going device. Note that the device should
353 * have it's pg_info pointer pointing back to this
354 * device. This will be set when the user specifies
355 * the out-going device name (not when the inject is
356 * started as it used to do.)
357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800359 unsigned cflows; /* Concurrent flows (config) */
360 unsigned lflow; /* Flow length (config) */
361 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700362 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700363
364 u16 queue_map_min;
365 u16 queue_map_max;
366
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700367#ifdef CONFIG_XFRM
368 __u8 ipsmode; /* IPSEC mode (config) */
369 __u8 ipsproto; /* IPSEC type (config) */
370#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800371 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373
374struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800375 __be32 pgh_magic;
376 __be32 seq_num;
377 __be32 tv_sec;
378 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379};
380
381struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800382 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800383 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800384 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800385 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800386 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Luiz Capitulino222f1802006-03-20 22:16:13 -0800388 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
389
390 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int cpu;
392
Luiz Capitulino222f1802006-03-20 22:16:13 -0800393 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700394 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
397#define REMOVE 1
398#define FIND 0
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800401static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800403 __u64 us = tv->tv_usec;
404 us += (__u64) tv->tv_sec * (__u64) 1000000;
405 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Ilpo Järvinen6db105d2008-01-12 03:25:00 -0800408static __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800410 struct timeval tv;
411 do_gettimeofday(&tv);
412 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/* old include end */
416
417static char version[] __initdata = VERSION;
418
Luiz Capitulino222f1802006-03-20 22:16:13 -0800419static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
420static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
421static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
422 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
424static void pktgen_run_all_threads(void);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800425static void pktgen_reset_all_threads(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static void pktgen_stop_all_threads_ifs(void);
427static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800428static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800430
Luiz Capitulino222f1802006-03-20 22:16:13 -0800431static unsigned int scan_ip6(const char *s, char ip[16]);
432static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800435static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb92005-12-22 12:51:46 -0800436static int pg_delay_d;
437static int pg_clone_skb_d;
438static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Luiz Capitulino222fa072006-03-20 22:24:27 -0800440static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800441static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static struct notifier_block pktgen_notifier_block = {
444 .notifier_call = pktgen_device_event,
445};
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900448 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 *
450 */
451
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700452static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800453{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700454 seq_puts(seq, VERSION);
455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Luiz Capitulino222f1802006-03-20 22:16:13 -0800458static ssize_t pgctrl_write(struct file *file, const char __user * buf,
459 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700462 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Luiz Capitulino222f1802006-03-20 22:16:13 -0800464 if (!capable(CAP_NET_ADMIN)) {
465 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700469 if (count > sizeof(data))
470 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700473 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700474 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800475 }
476 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Luiz Capitulino222f1802006-03-20 22:16:13 -0800478 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 pktgen_stop_all_threads_ifs();
480
Luiz Capitulino222f1802006-03-20 22:16:13 -0800481 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 pktgen_run_all_threads();
483
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800484 else if (!strcmp(data, "reset"))
485 pktgen_reset_all_threads();
486
Luiz Capitulino222f1802006-03-20 22:16:13 -0800487 else
David S. Miller25a8b252007-07-30 16:11:48 -0700488 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 err = count;
491
Luiz Capitulino222f1802006-03-20 22:16:13 -0800492out:
493 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700496static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700498 return single_open(file, pgctrl_show, PDE(inode)->data);
499}
500
Arjan van de Ven9a321442007-02-12 00:55:35 -0800501static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800502 .owner = THIS_MODULE,
503 .open = pgctrl_open,
504 .read = seq_read,
505 .llseek = seq_lseek,
506 .write = pgctrl_write,
507 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700508};
509
510static int pktgen_if_show(struct seq_file *seq, void *v)
511{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800512 struct pktgen_dev *pkt_dev = seq->private;
513 __u64 sa;
514 __u64 stopped;
515 __u64 now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Luiz Capitulino222f1802006-03-20 22:16:13 -0800517 seq_printf(seq,
518 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
519 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
520 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Luiz Capitulino222f1802006-03-20 22:16:13 -0800522 seq_printf(seq,
523 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
524 pkt_dev->nfrags,
525 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800526 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Luiz Capitulino222f1802006-03-20 22:16:13 -0800528 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
529 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Robert Olsson45b270f2007-08-28 15:45:55 -0700531 seq_printf(seq,
532 " queue_map_min: %u queue_map_max: %u\n",
533 pkt_dev->queue_map_min,
534 pkt_dev->queue_map_max);
535
Luiz Capitulino222f1802006-03-20 22:16:13 -0800536 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800538 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
539 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
540 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
541 seq_printf(seq,
542 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
543 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Luiz Capitulino222f1802006-03-20 22:16:13 -0800545 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
546 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
547 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
548 seq_printf(seq,
549 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
550 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Luiz Capitulino222f1802006-03-20 22:16:13 -0800552 } else
553 seq_printf(seq,
554 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
555 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
556 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700558 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Johannes Berge1749612008-10-27 15:59:26 -0700560 seq_printf(seq, "%pM ",
561 is_zero_ether_addr(pkt_dev->src_mac) ?
562 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Luiz Capitulino222f1802006-03-20 22:16:13 -0800564 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700565 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Luiz Capitulino222f1802006-03-20 22:16:13 -0800567 seq_printf(seq,
568 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
569 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
570 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Luiz Capitulino222f1802006-03-20 22:16:13 -0800572 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800573 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700574 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800576 if (pkt_dev->nr_labels) {
577 unsigned i;
578 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700579 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800580 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
581 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
582 }
583
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700584 if (pkt_dev->vlan_id != 0xffff) {
585 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
586 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
587 }
588
589 if (pkt_dev->svlan_id != 0xffff) {
590 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
591 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
592 }
593
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700594 if (pkt_dev->tos) {
595 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
596 }
597
598 if (pkt_dev->traffic_class) {
599 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
600 }
601
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800602 seq_printf(seq, " Flags: ");
603
Luiz Capitulino222f1802006-03-20 22:16:13 -0800604 if (pkt_dev->flags & F_IPV6)
605 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Luiz Capitulino222f1802006-03-20 22:16:13 -0800607 if (pkt_dev->flags & F_IPSRC_RND)
608 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Luiz Capitulino222f1802006-03-20 22:16:13 -0800610 if (pkt_dev->flags & F_IPDST_RND)
611 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Luiz Capitulino222f1802006-03-20 22:16:13 -0800613 if (pkt_dev->flags & F_TXSIZE_RND)
614 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Luiz Capitulino222f1802006-03-20 22:16:13 -0800616 if (pkt_dev->flags & F_UDPSRC_RND)
617 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Luiz Capitulino222f1802006-03-20 22:16:13 -0800619 if (pkt_dev->flags & F_UDPDST_RND)
620 seq_printf(seq, "UDPDST_RND ");
621
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800622 if (pkt_dev->flags & F_MPLS_RND)
623 seq_printf(seq, "MPLS_RND ");
624
Robert Olsson45b270f2007-08-28 15:45:55 -0700625 if (pkt_dev->flags & F_QUEUE_MAP_RND)
626 seq_printf(seq, "QUEUE_MAP_RND ");
627
Robert Olssone6fce5b2008-08-07 02:23:01 -0700628 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
629 seq_printf(seq, "QUEUE_MAP_CPU ");
630
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700631 if (pkt_dev->cflows) {
632 if (pkt_dev->flags & F_FLOW_SEQ)
633 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
634 else
635 seq_printf(seq, "FLOW_RND ");
636 }
637
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700638#ifdef CONFIG_XFRM
639 if (pkt_dev->flags & F_IPSEC_ON)
640 seq_printf(seq, "IPSEC ");
641#endif
642
Luiz Capitulino222f1802006-03-20 22:16:13 -0800643 if (pkt_dev->flags & F_MACSRC_RND)
644 seq_printf(seq, "MACSRC_RND ");
645
646 if (pkt_dev->flags & F_MACDST_RND)
647 seq_printf(seq, "MACDST_RND ");
648
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700649 if (pkt_dev->flags & F_VID_RND)
650 seq_printf(seq, "VID_RND ");
651
652 if (pkt_dev->flags & F_SVID_RND)
653 seq_printf(seq, "SVID_RND ");
654
Luiz Capitulino222f1802006-03-20 22:16:13 -0800655 seq_puts(seq, "\n");
656
657 sa = pkt_dev->started_at;
658 stopped = pkt_dev->stopped_at;
659 if (pkt_dev->running)
660 stopped = now; /* not really stopped, more like last-running-at */
661
662 seq_printf(seq,
663 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
664 (unsigned long long)pkt_dev->sofar,
665 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
666 (unsigned long long)stopped,
667 (unsigned long long)pkt_dev->idle_acc);
668
669 seq_printf(seq,
670 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700671 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
672 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Luiz Capitulino222f1802006-03-20 22:16:13 -0800674 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800676 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
677 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
678 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
679 } else
680 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700681 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Luiz Capitulino222f1802006-03-20 22:16:13 -0800683 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700684 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Robert Olsson45b270f2007-08-28 15:45:55 -0700686 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
687
Luiz Capitulino222f1802006-03-20 22:16:13 -0800688 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800691 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800693 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800698
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700699static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800700{
701 int i = 0;
702 *num = 0;
703
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700704 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800705 char c;
706 *num <<= 4;
707 if (get_user(c, &user_buffer[i]))
708 return -EFAULT;
709 if ((c >= '0') && (c <= '9'))
710 *num |= c - '0';
711 else if ((c >= 'a') && (c <= 'f'))
712 *num |= c - 'a' + 10;
713 else if ((c >= 'A') && (c <= 'F'))
714 *num |= c - 'A' + 10;
715 else
716 break;
717 }
718 return i;
719}
720
Luiz Capitulino222f1802006-03-20 22:16:13 -0800721static int count_trail_chars(const char __user * user_buffer,
722 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 int i;
725
726 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800727 char c;
728 if (get_user(c, &user_buffer[i]))
729 return -EFAULT;
730 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 case '\"':
732 case '\n':
733 case '\r':
734 case '\t':
735 case ' ':
736 case '=':
737 break;
738 default:
739 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742done:
743 return i;
744}
745
Luiz Capitulino222f1802006-03-20 22:16:13 -0800746static unsigned long num_arg(const char __user * user_buffer,
747 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 int i = 0;
750 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800751
752 for (; i < maxlen; i++) {
753 char c;
754 if (get_user(c, &user_buffer[i]))
755 return -EFAULT;
756 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800758 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 } else
760 break;
761 }
762 return i;
763}
764
Luiz Capitulino222f1802006-03-20 22:16:13 -0800765static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 int i = 0;
768
Luiz Capitulino222f1802006-03-20 22:16:13 -0800769 for (; i < maxlen; i++) {
770 char c;
771 if (get_user(c, &user_buffer[i]))
772 return -EFAULT;
773 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case '\"':
775 case '\n':
776 case '\r':
777 case '\t':
778 case ' ':
779 goto done_str;
780 break;
781 default:
782 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return i;
787}
788
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800789static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
790{
791 unsigned n = 0;
792 char c;
793 ssize_t i = 0;
794 int len;
795
796 pkt_dev->nr_labels = 0;
797 do {
798 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700799 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800800 if (len <= 0)
801 return len;
802 pkt_dev->labels[n] = htonl(tmp);
803 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
804 pkt_dev->flags |= F_MPLS_RND;
805 i += len;
806 if (get_user(c, &buffer[i]))
807 return -EFAULT;
808 i++;
809 n++;
810 if (n >= MAX_MPLS_LABELS)
811 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700812 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800813
814 pkt_dev->nr_labels = n;
815 return i;
816}
817
Luiz Capitulino222f1802006-03-20 22:16:13 -0800818static ssize_t pktgen_if_write(struct file *file,
819 const char __user * user_buffer, size_t count,
820 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800822 struct seq_file *seq = (struct seq_file *)file->private_data;
823 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int i = 0, max, len;
825 char name[16], valstr[32];
826 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800827 char *pg_result = NULL;
828 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800830
831 pg_result = &(pkt_dev->result[0]);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (count < 1) {
David S. Miller25a8b252007-07-30 16:11:48 -0700834 printk(KERN_WARNING "pktgen: wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return -EINVAL;
836 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 max = count - i;
839 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800840 if (tmp < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -0700841 printk(KERN_WARNING "pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800842 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800844 i += tmp;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* Read variable name */
847
848 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800849 if (len < 0) {
850 return len;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800853 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -EFAULT;
855 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800856
857 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800859 if (len < 0)
860 return len;
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 i += len;
863
864 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800865 char tb[count + 1];
866 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800868 tb[count] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700869 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800870 (unsigned long)count, tb);
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 if (!strcmp(name, "min_pkt_size")) {
874 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875 if (len < 0) {
876 return len;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800879 if (value < 14 + 20 + 8)
880 value = 14 + 20 + 8;
881 if (value != pkt_dev->min_pkt_size) {
882 pkt_dev->min_pkt_size = value;
883 pkt_dev->cur_pkt_size = value;
884 }
885 sprintf(pg_result, "OK: min_pkt_size=%u",
886 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return count;
888 }
889
Luiz Capitulino222f1802006-03-20 22:16:13 -0800890 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800892 if (len < 0) {
893 return len;
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800896 if (value < 14 + 20 + 8)
897 value = 14 + 20 + 8;
898 if (value != pkt_dev->max_pkt_size) {
899 pkt_dev->max_pkt_size = value;
900 pkt_dev->cur_pkt_size = value;
901 }
902 sprintf(pg_result, "OK: max_pkt_size=%u",
903 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return count;
905 }
906
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (!strcmp(name, "pkt_size")) {
910 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800911 if (len < 0) {
912 return len;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800915 if (value < 14 + 20 + 8)
916 value = 14 + 20 + 8;
917 if (value != pkt_dev->min_pkt_size) {
918 pkt_dev->min_pkt_size = value;
919 pkt_dev->max_pkt_size = value;
920 pkt_dev->cur_pkt_size = value;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
923 return count;
924 }
925
Luiz Capitulino222f1802006-03-20 22:16:13 -0800926 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800928 if (len < 0) {
929 return len;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800932 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 sprintf(pg_result, "OK: debug=%u", debug);
934 return count;
935 }
936
Luiz Capitulino222f1802006-03-20 22:16:13 -0800937 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800939 if (len < 0) {
940 return len;
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 i += len;
943 pkt_dev->nfrags = value;
944 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
945 return count;
946 }
947 if (!strcmp(name, "delay")) {
948 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800949 if (len < 0) {
950 return len;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 i += len;
953 if (value == 0x7FFFFFFF) {
954 pkt_dev->delay_us = 0x7FFFFFFF;
955 pkt_dev->delay_ns = 0;
956 } else {
957 pkt_dev->delay_us = value / 1000;
958 pkt_dev->delay_ns = value % 1000;
959 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 sprintf(pg_result, "OK: delay=%u",
961 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return count;
963 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800964 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800966 if (len < 0) {
967 return len;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800970 if (value != pkt_dev->udp_src_min) {
971 pkt_dev->udp_src_min = value;
972 pkt_dev->cur_udp_src = value;
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
975 return count;
976 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800977 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800979 if (len < 0) {
980 return len;
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800983 if (value != pkt_dev->udp_dst_min) {
984 pkt_dev->udp_dst_min = value;
985 pkt_dev->cur_udp_dst = value;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
988 return count;
989 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800990 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800992 if (len < 0) {
993 return len;
994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800996 if (value != pkt_dev->udp_src_max) {
997 pkt_dev->udp_src_max = value;
998 pkt_dev->cur_udp_src = value;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1001 return count;
1002 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001003 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001005 if (len < 0) {
1006 return len;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001009 if (value != pkt_dev->udp_dst_max) {
1010 pkt_dev->udp_dst_max = value;
1011 pkt_dev->cur_udp_dst = value;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1014 return count;
1015 }
1016 if (!strcmp(name, "clone_skb")) {
1017 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;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001022 pkt_dev->clone_skb = value;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1025 return count;
1026 }
1027 if (!strcmp(name, "count")) {
1028 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 if (len < 0) {
1030 return len;
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 i += len;
1033 pkt_dev->count = value;
1034 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001035 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return count;
1037 }
1038 if (!strcmp(name, "src_mac_count")) {
1039 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001040 if (len < 0) {
1041 return len;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 i += len;
1044 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 pkt_dev->src_mac_count = value;
1046 pkt_dev->cur_src_mac_offset = 0;
1047 }
1048 sprintf(pg_result, "OK: src_mac_count=%d",
1049 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return count;
1051 }
1052 if (!strcmp(name, "dst_mac_count")) {
1053 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 if (len < 0) {
1055 return len;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 i += len;
1058 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001059 pkt_dev->dst_mac_count = value;
1060 pkt_dev->cur_dst_mac_offset = 0;
1061 }
1062 sprintf(pg_result, "OK: dst_mac_count=%d",
1063 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return count;
1065 }
1066 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001067 char f[32];
1068 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001070 if (len < 0) {
1071 return len;
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (copy_from_user(f, &user_buffer[i], len))
1074 return -EFAULT;
1075 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001076 if (strcmp(f, "IPSRC_RND") == 0)
1077 pkt_dev->flags |= F_IPSRC_RND;
1078
1079 else if (strcmp(f, "!IPSRC_RND") == 0)
1080 pkt_dev->flags &= ~F_IPSRC_RND;
1081
1082 else if (strcmp(f, "TXSIZE_RND") == 0)
1083 pkt_dev->flags |= F_TXSIZE_RND;
1084
1085 else if (strcmp(f, "!TXSIZE_RND") == 0)
1086 pkt_dev->flags &= ~F_TXSIZE_RND;
1087
1088 else if (strcmp(f, "IPDST_RND") == 0)
1089 pkt_dev->flags |= F_IPDST_RND;
1090
1091 else if (strcmp(f, "!IPDST_RND") == 0)
1092 pkt_dev->flags &= ~F_IPDST_RND;
1093
1094 else if (strcmp(f, "UDPSRC_RND") == 0)
1095 pkt_dev->flags |= F_UDPSRC_RND;
1096
1097 else if (strcmp(f, "!UDPSRC_RND") == 0)
1098 pkt_dev->flags &= ~F_UDPSRC_RND;
1099
1100 else if (strcmp(f, "UDPDST_RND") == 0)
1101 pkt_dev->flags |= F_UDPDST_RND;
1102
1103 else if (strcmp(f, "!UDPDST_RND") == 0)
1104 pkt_dev->flags &= ~F_UDPDST_RND;
1105
1106 else if (strcmp(f, "MACSRC_RND") == 0)
1107 pkt_dev->flags |= F_MACSRC_RND;
1108
1109 else if (strcmp(f, "!MACSRC_RND") == 0)
1110 pkt_dev->flags &= ~F_MACSRC_RND;
1111
1112 else if (strcmp(f, "MACDST_RND") == 0)
1113 pkt_dev->flags |= F_MACDST_RND;
1114
1115 else if (strcmp(f, "!MACDST_RND") == 0)
1116 pkt_dev->flags &= ~F_MACDST_RND;
1117
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001118 else if (strcmp(f, "MPLS_RND") == 0)
1119 pkt_dev->flags |= F_MPLS_RND;
1120
1121 else if (strcmp(f, "!MPLS_RND") == 0)
1122 pkt_dev->flags &= ~F_MPLS_RND;
1123
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001124 else if (strcmp(f, "VID_RND") == 0)
1125 pkt_dev->flags |= F_VID_RND;
1126
1127 else if (strcmp(f, "!VID_RND") == 0)
1128 pkt_dev->flags &= ~F_VID_RND;
1129
1130 else if (strcmp(f, "SVID_RND") == 0)
1131 pkt_dev->flags |= F_SVID_RND;
1132
1133 else if (strcmp(f, "!SVID_RND") == 0)
1134 pkt_dev->flags &= ~F_SVID_RND;
1135
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001136 else if (strcmp(f, "FLOW_SEQ") == 0)
1137 pkt_dev->flags |= F_FLOW_SEQ;
1138
Robert Olsson45b270f2007-08-28 15:45:55 -07001139 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1140 pkt_dev->flags |= F_QUEUE_MAP_RND;
1141
1142 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1143 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001144
1145 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1146 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1147
1148 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1149 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001150#ifdef CONFIG_XFRM
1151 else if (strcmp(f, "IPSEC") == 0)
1152 pkt_dev->flags |= F_IPSEC_ON;
1153#endif
1154
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001155 else if (strcmp(f, "!IPV6") == 0)
1156 pkt_dev->flags &= ~F_IPV6;
1157
Luiz Capitulino222f1802006-03-20 22:16:13 -08001158 else {
1159 sprintf(pg_result,
1160 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1161 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001162 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001163 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001164 return count;
1165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1167 return count;
1168 }
1169 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1170 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001171 if (len < 0) {
1172 return len;
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Luiz Capitulino222f1802006-03-20 22:16:13 -08001175 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001177 buf[len] = 0;
1178 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1179 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1180 strncpy(pkt_dev->dst_min, buf, len);
1181 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1182 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1183 }
1184 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001185 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001186 pkt_dev->dst_min);
1187 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1189 return count;
1190 }
1191 if (!strcmp(name, "dst_max")) {
1192 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001193 if (len < 0) {
1194 return len;
1195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Luiz Capitulino222f1802006-03-20 22:16:13 -08001197 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return -EFAULT;
1199
Luiz Capitulino222f1802006-03-20 22:16:13 -08001200 buf[len] = 0;
1201 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1202 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1203 strncpy(pkt_dev->dst_max, buf, len);
1204 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1205 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1206 }
1207 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001208 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001209 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 i += len;
1211 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1212 return count;
1213 }
1214 if (!strcmp(name, "dst6")) {
1215 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001216 if (len < 0)
1217 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 pkt_dev->flags |= F_IPV6;
1220
Luiz Capitulino222f1802006-03-20 22:16:13 -08001221 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001223 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001226 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1229
Luiz Capitulino222f1802006-03-20 22:16:13 -08001230 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001231 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Luiz Capitulino222f1802006-03-20 22:16:13 -08001233 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sprintf(pg_result, "OK: dst6=%s", buf);
1235 return count;
1236 }
1237 if (!strcmp(name, "dst6_min")) {
1238 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001239 if (len < 0)
1240 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 pkt_dev->flags |= F_IPV6;
1243
Luiz Capitulino222f1802006-03-20 22:16:13 -08001244 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001246 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001249 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Luiz Capitulino222f1802006-03-20 22:16:13 -08001251 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1252 &pkt_dev->min_in6_daddr);
1253 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001254 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Luiz Capitulino222f1802006-03-20 22:16:13 -08001256 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 sprintf(pg_result, "OK: dst6_min=%s", buf);
1258 return count;
1259 }
1260 if (!strcmp(name, "dst6_max")) {
1261 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 if (len < 0)
1263 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 pkt_dev->flags |= F_IPV6;
1266
Luiz Capitulino222f1802006-03-20 22:16:13 -08001267 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001269 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001272 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Luiz Capitulino222f1802006-03-20 22:16:13 -08001274 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001275 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Luiz Capitulino222f1802006-03-20 22:16:13 -08001277 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 sprintf(pg_result, "OK: dst6_max=%s", buf);
1279 return count;
1280 }
1281 if (!strcmp(name, "src6")) {
1282 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001283 if (len < 0)
1284 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 pkt_dev->flags |= F_IPV6;
1287
Luiz Capitulino222f1802006-03-20 22:16:13 -08001288 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001290 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001293 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1296
Luiz Capitulino222f1802006-03-20 22:16:13 -08001297 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001298 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001299
1300 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 sprintf(pg_result, "OK: src6=%s", buf);
1302 return count;
1303 }
1304 if (!strcmp(name, "src_min")) {
1305 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 if (len < 0) {
1307 return len;
1308 }
1309 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001311 buf[len] = 0;
1312 if (strcmp(buf, pkt_dev->src_min) != 0) {
1313 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1314 strncpy(pkt_dev->src_min, buf, len);
1315 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1316 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1317 }
1318 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001319 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 i += len;
1322 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1323 return count;
1324 }
1325 if (!strcmp(name, "src_max")) {
1326 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001327 if (len < 0) {
1328 return len;
1329 }
1330 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001332 buf[len] = 0;
1333 if (strcmp(buf, pkt_dev->src_max) != 0) {
1334 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1335 strncpy(pkt_dev->src_max, buf, len);
1336 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1337 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1338 }
1339 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001340 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001341 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 i += len;
1343 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1344 return count;
1345 }
1346 if (!strcmp(name, "dst_mac")) {
1347 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001348 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001350 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001353 if (len < 0) {
1354 return len;
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001357 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EFAULT;
1359 i += len;
1360
Luiz Capitulino222f1802006-03-20 22:16:13 -08001361 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (*v >= '0' && *v <= '9') {
1363 *m *= 16;
1364 *m += *v - '0';
1365 }
1366 if (*v >= 'A' && *v <= 'F') {
1367 *m *= 16;
1368 *m += *v - 'A' + 10;
1369 }
1370 if (*v >= 'a' && *v <= 'f') {
1371 *m *= 16;
1372 *m += *v - 'a' + 10;
1373 }
1374 if (*v == ':') {
1375 m++;
1376 *m = 0;
1377 }
1378 }
1379
1380 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001381 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1382 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 sprintf(pg_result, "OK: dstmac");
1385 return count;
1386 }
1387 if (!strcmp(name, "src_mac")) {
1388 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001389 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 unsigned char *m = pkt_dev->src_mac;
1391
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001392 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001395 if (len < 0) {
1396 return len;
1397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001399 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return -EFAULT;
1401 i += len;
1402
Luiz Capitulino222f1802006-03-20 22:16:13 -08001403 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (*v >= '0' && *v <= '9') {
1405 *m *= 16;
1406 *m += *v - '0';
1407 }
1408 if (*v >= 'A' && *v <= 'F') {
1409 *m *= 16;
1410 *m += *v - 'A' + 10;
1411 }
1412 if (*v >= 'a' && *v <= 'f') {
1413 *m *= 16;
1414 *m += *v - 'a' + 10;
1415 }
1416 if (*v == ':') {
1417 m++;
1418 *m = 0;
1419 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001422 /* Set up Src MAC */
1423 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1424 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1425
Luiz Capitulino222f1802006-03-20 22:16:13 -08001426 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return count;
1428 }
1429
Luiz Capitulino222f1802006-03-20 22:16:13 -08001430 if (!strcmp(name, "clear_counters")) {
1431 pktgen_clear_counters(pkt_dev);
1432 sprintf(pg_result, "OK: Clearing counters.\n");
1433 return count;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 if (!strcmp(name, "flows")) {
1437 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001438 if (len < 0) {
1439 return len;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 i += len;
1442 if (value > MAX_CFLOWS)
1443 value = MAX_CFLOWS;
1444
1445 pkt_dev->cflows = value;
1446 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1447 return count;
1448 }
1449
1450 if (!strcmp(name, "flowlen")) {
1451 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001452 if (len < 0) {
1453 return len;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 i += len;
1456 pkt_dev->lflow = value;
1457 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1458 return count;
1459 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001460
Robert Olsson45b270f2007-08-28 15:45:55 -07001461 if (!strcmp(name, "queue_map_min")) {
1462 len = num_arg(&user_buffer[i], 5, &value);
1463 if (len < 0) {
1464 return len;
1465 }
1466 i += len;
1467 pkt_dev->queue_map_min = value;
1468 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1469 return count;
1470 }
1471
1472 if (!strcmp(name, "queue_map_max")) {
1473 len = num_arg(&user_buffer[i], 5, &value);
1474 if (len < 0) {
1475 return len;
1476 }
1477 i += len;
1478 pkt_dev->queue_map_max = value;
1479 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1480 return count;
1481 }
1482
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001483 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001484 unsigned n, cnt;
1485
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001486 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001487 if (len < 0)
1488 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001489 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001490 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001491 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001492 cnt += sprintf(pg_result + cnt,
1493 "%08x%s", ntohl(pkt_dev->labels[n]),
1494 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001495
1496 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1497 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1498 pkt_dev->svlan_id = 0xffff;
1499
1500 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001501 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001502 }
1503 return count;
1504 }
1505
1506 if (!strcmp(name, "vlan_id")) {
1507 len = num_arg(&user_buffer[i], 4, &value);
1508 if (len < 0) {
1509 return len;
1510 }
1511 i += len;
1512 if (value <= 4095) {
1513 pkt_dev->vlan_id = value; /* turn on VLAN */
1514
1515 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001516 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001517
1518 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001519 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001520
1521 pkt_dev->nr_labels = 0; /* turn off MPLS */
1522 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1523 } else {
1524 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1525 pkt_dev->svlan_id = 0xffff;
1526
1527 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001528 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001529 }
1530 return count;
1531 }
1532
1533 if (!strcmp(name, "vlan_p")) {
1534 len = num_arg(&user_buffer[i], 1, &value);
1535 if (len < 0) {
1536 return len;
1537 }
1538 i += len;
1539 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1540 pkt_dev->vlan_p = value;
1541 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1542 } else {
1543 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1544 }
1545 return count;
1546 }
1547
1548 if (!strcmp(name, "vlan_cfi")) {
1549 len = num_arg(&user_buffer[i], 1, &value);
1550 if (len < 0) {
1551 return len;
1552 }
1553 i += len;
1554 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1555 pkt_dev->vlan_cfi = value;
1556 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1557 } else {
1558 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1559 }
1560 return count;
1561 }
1562
1563 if (!strcmp(name, "svlan_id")) {
1564 len = num_arg(&user_buffer[i], 4, &value);
1565 if (len < 0) {
1566 return len;
1567 }
1568 i += len;
1569 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1570 pkt_dev->svlan_id = value; /* turn on SVLAN */
1571
1572 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001573 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001574
1575 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001576 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001577
1578 pkt_dev->nr_labels = 0; /* turn off MPLS */
1579 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1580 } else {
1581 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1582 pkt_dev->svlan_id = 0xffff;
1583
1584 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001585 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001586 }
1587 return count;
1588 }
1589
1590 if (!strcmp(name, "svlan_p")) {
1591 len = num_arg(&user_buffer[i], 1, &value);
1592 if (len < 0) {
1593 return len;
1594 }
1595 i += len;
1596 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1597 pkt_dev->svlan_p = value;
1598 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1599 } else {
1600 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1601 }
1602 return count;
1603 }
1604
1605 if (!strcmp(name, "svlan_cfi")) {
1606 len = num_arg(&user_buffer[i], 1, &value);
1607 if (len < 0) {
1608 return len;
1609 }
1610 i += len;
1611 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1612 pkt_dev->svlan_cfi = value;
1613 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1614 } else {
1615 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1616 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001617 return count;
1618 }
1619
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001620 if (!strcmp(name, "tos")) {
1621 __u32 tmp_value = 0;
1622 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1623 if (len < 0) {
1624 return len;
1625 }
1626 i += len;
1627 if (len == 2) {
1628 pkt_dev->tos = tmp_value;
1629 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1630 } else {
1631 sprintf(pg_result, "ERROR: tos must be 00-ff");
1632 }
1633 return count;
1634 }
1635
1636 if (!strcmp(name, "traffic_class")) {
1637 __u32 tmp_value = 0;
1638 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1639 if (len < 0) {
1640 return len;
1641 }
1642 i += len;
1643 if (len == 2) {
1644 pkt_dev->traffic_class = tmp_value;
1645 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1646 } else {
1647 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1648 }
1649 return count;
1650 }
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1653 return -EINVAL;
1654}
1655
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001656static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001658 return single_open(file, pktgen_if_show, PDE(inode)->data);
1659}
1660
Arjan van de Ven9a321442007-02-12 00:55:35 -08001661static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001662 .owner = THIS_MODULE,
1663 .open = pktgen_if_open,
1664 .read = seq_read,
1665 .llseek = seq_lseek,
1666 .write = pktgen_if_write,
1667 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001668};
1669
1670static int pktgen_thread_show(struct seq_file *seq, void *v)
1671{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001672 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001673 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001675 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Luiz Capitulino222f1802006-03-20 22:16:13 -08001677 seq_printf(seq, "Running: ");
1678
1679 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001680 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001681 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001682 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Luiz Capitulino222f1802006-03-20 22:16:13 -08001684 seq_printf(seq, "\nStopped: ");
1685
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001686 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001687 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001688 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001691 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001693 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Luiz Capitulino222f1802006-03-20 22:16:13 -08001695 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001700static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001701 const char __user * user_buffer,
1702 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001704 struct seq_file *seq = (struct seq_file *)file->private_data;
1705 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 int i = 0, max, len, ret;
1707 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001708 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001711 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return -EINVAL;
1713 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001716 len = count_trail_chars(&user_buffer[i], max);
1717 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001718 return len;
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /* Read variable name */
1723
1724 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001725 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001726 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 memset(name, 0, sizeof(name));
1729 if (copy_from_user(name, &user_buffer[i], len))
1730 return -EFAULT;
1731 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001732
Luiz Capitulino222f1802006-03-20 22:16:13 -08001733 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001736 return len;
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 i += len;
1739
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001740 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001741 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1742 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Luiz Capitulino222f1802006-03-20 22:16:13 -08001744 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07001745 printk(KERN_ERR "pktgen: ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 ret = -EINVAL;
1747 goto out;
1748 }
1749
1750 pg_result = &(t->result[0]);
1751
Luiz Capitulino222f1802006-03-20 22:16:13 -08001752 if (!strcmp(name, "add_device")) {
1753 char f[32];
1754 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001756 if (len < 0) {
1757 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 goto out;
1759 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001760 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return -EFAULT;
1762 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001763 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001765 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001766 ret = count;
1767 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto out;
1769 }
1770
Luiz Capitulino222f1802006-03-20 22:16:13 -08001771 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001772 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001773 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001774 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001775 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001777 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto out;
1779 }
1780
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001782 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto out;
1785 }
1786
1787 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001788out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return ret;
1790}
1791
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001792static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001794 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
Arjan van de Ven9a321442007-02-12 00:55:35 -08001797static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001798 .owner = THIS_MODULE,
1799 .open = pktgen_thread_open,
1800 .read = seq_read,
1801 .llseek = seq_lseek,
1802 .write = pktgen_thread_write,
1803 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001804};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001807static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 struct pktgen_thread *t;
1810 struct pktgen_dev *pkt_dev = NULL;
1811
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001812 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 pkt_dev = pktgen_find_dev(t, ifname);
1814 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001815 if (remove) {
1816 if_lock(t);
1817 pkt_dev->removal_mark = 1;
1818 t->control |= T_REMDEV;
1819 if_unlock(t);
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 break;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001824 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001827/*
1828 * mark a device for removal
1829 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001830static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001833 const int max_tries = 10, msec_per_try = 125;
1834 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001835
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001836 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001837 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001838
Luiz Capitulino222f1802006-03-20 22:16:13 -08001839 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001840
1841 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001842 if (pkt_dev == NULL)
1843 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001844
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001845 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001846 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1847 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001848 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001849 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001850
1851 if (++i >= max_tries) {
David S. Miller25a8b252007-07-30 16:11:48 -07001852 printk(KERN_ERR "pktgen_mark_device: timed out after "
1853 "waiting %d msec for device %s to be removed\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001854 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001855 break;
1856 }
1857
1858 }
1859
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001860 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001861}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001862
Stephen Hemminger39df2322007-03-04 16:11:51 -08001863static void pktgen_change_name(struct net_device *dev)
1864{
1865 struct pktgen_thread *t;
1866
1867 list_for_each_entry(t, &pktgen_threads, th_list) {
1868 struct pktgen_dev *pkt_dev;
1869
1870 list_for_each_entry(pkt_dev, &t->if_list, list) {
1871 if (pkt_dev->odev != dev)
1872 continue;
1873
1874 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1875
1876 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1877 pg_proc_dir);
1878 if (!pkt_dev->entry)
1879 printk(KERN_ERR "pktgen: can't move proc "
1880 " entry for '%s'\n", dev->name);
1881 break;
1882 }
1883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Luiz Capitulino222f1802006-03-20 22:16:13 -08001886static int pktgen_device_event(struct notifier_block *unused,
1887 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001889 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001891 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001892 return NOTIFY_DONE;
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /* It is OK that we do not hold the group lock right now,
1895 * as we run under the RTNL lock.
1896 */
1897
1898 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001899 case NETDEV_CHANGENAME:
1900 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001904 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 return NOTIFY_DONE;
1909}
1910
Robert Olssone6fce5b2008-08-07 02:23:01 -07001911static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev, const char *ifname)
1912{
1913 char b[IFNAMSIZ+5];
1914 int i = 0;
1915
1916 for(i=0; ifname[i] != '@'; i++) {
1917 if(i == IFNAMSIZ)
1918 break;
1919
1920 b[i] = ifname[i];
1921 }
1922 b[i] = 0;
1923
1924 return dev_get_by_name(&init_net, b);
1925}
1926
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928/* Associate pktgen_dev with a device. */
1929
Stephen Hemminger39df2322007-03-04 16:11:51 -08001930static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001933 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (pkt_dev->odev) {
1937 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001938 pkt_dev->odev = NULL;
1939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Robert Olssone6fce5b2008-08-07 02:23:01 -07001941 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (!odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001943 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001944 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (odev->type != ARPHRD_ETHER) {
David S. Miller25a8b252007-07-30 16:11:48 -07001948 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001949 err = -EINVAL;
1950 } else if (!netif_running(odev)) {
David S. Miller25a8b252007-07-30 16:11:48 -07001951 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001952 err = -ENETDOWN;
1953 } else {
1954 pkt_dev->odev = odev;
1955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
1962/* Read pkt_dev from the interface and set up internal pktgen_dev
1963 * structure to have the right information to create/send packets
1964 */
1965static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1966{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001967 int ntxq;
1968
Luiz Capitulino222f1802006-03-20 22:16:13 -08001969 if (!pkt_dev->odev) {
David S. Miller25a8b252007-07-30 16:11:48 -07001970 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1971 "setup_inject.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001972 sprintf(pkt_dev->result,
1973 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1974 return;
1975 }
1976
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001977 /* make sure that we don't pick a non-existing transmit queue */
1978 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08001979
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001980 if (ntxq <= pkt_dev->queue_map_min) {
1981 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07001982 "queue_map_min (zero-based) (%d) exceeds valid range "
1983 "[0 - %d] for (%d) queues on %s, resetting\n",
1984 pkt_dev->queue_map_min, (ntxq ?: 1)- 1, ntxq,
1985 pkt_dev->odev->name);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001986 pkt_dev->queue_map_min = ntxq - 1;
1987 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07001988 if (pkt_dev->queue_map_max >= ntxq) {
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001989 printk(KERN_WARNING "pktgen: WARNING: Requested "
Jesse Brandeburg88271662008-10-28 13:21:51 -07001990 "queue_map_max (zero-based) (%d) exceeds valid range "
1991 "[0 - %d] for (%d) queues on %s, resetting\n",
1992 pkt_dev->queue_map_max, (ntxq ?: 1)- 1, ntxq,
1993 pkt_dev->odev->name);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07001994 pkt_dev->queue_map_max = ntxq - 1;
1995 }
1996
Luiz Capitulino222f1802006-03-20 22:16:13 -08001997 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001999 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002000 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Luiz Capitulino222f1802006-03-20 22:16:13 -08002002 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002003 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Luiz Capitulino222f1802006-03-20 22:16:13 -08002005 /* Set up pkt size */
2006 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2007
2008 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002010 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 * gets exported
2012 */
2013
2014#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002015 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 struct inet6_dev *idev;
2017
Luiz Capitulino222f1802006-03-20 22:16:13 -08002018 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2019 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 set = 1;
2021 break;
2022 }
2023
Luiz Capitulino222f1802006-03-20 22:16:13 -08002024 if (!set) {
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /*
2027 * Use linklevel address if unconfigured.
2028 *
2029 * use ipv6_get_lladdr if/when it's get exported
2030 */
2031
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002032 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2034 struct inet6_ifaddr *ifp;
2035
2036 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002037 for (ifp = idev->addr_list; ifp;
2038 ifp = ifp->if_next) {
2039 if (ifp->scope == IFA_LINK
2040 && !(ifp->
2041 flags & IFA_F_TENTATIVE)) {
2042 ipv6_addr_copy(&pkt_dev->
2043 cur_in6_saddr,
2044 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 err = 0;
2046 break;
2047 }
2048 }
2049 read_unlock_bh(&idev->lock);
2050 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002051 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002052 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07002053 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2054 "address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002057 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 pkt_dev->saddr_min = 0;
2059 pkt_dev->saddr_max = 0;
2060 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002061
2062 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002065 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (in_dev) {
2067 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002068 pkt_dev->saddr_min =
2069 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 pkt_dev->saddr_max = pkt_dev->saddr_min;
2071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002074 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2076 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2077 }
2078
2079 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2080 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2081 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002082 /* Initialize current values. */
2083 pkt_dev->cur_dst_mac_offset = 0;
2084 pkt_dev->cur_src_mac_offset = 0;
2085 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2086 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2087 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2088 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 pkt_dev->nflows = 0;
2090}
2091
2092static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2093{
2094 __u64 start;
2095 __u64 now;
2096
2097 start = now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002099 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002100 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002101 schedule_timeout_interruptible(1);
2102 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (!pkt_dev->running)
2104 return;
2105 if (need_resched())
2106 schedule();
2107 }
2108
2109 now = getCurUs();
2110 }
2111
2112 pkt_dev->idle_acc += now - start;
2113}
2114
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002115static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2116{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002117 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002118 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2119 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2120 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2121}
2122
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002123static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2124{
2125
2126 if (pkt_dev->flows[flow].flags & F_INIT)
2127 return 1;
2128 else
2129 return 0;
2130}
2131
2132static inline int f_pick(struct pktgen_dev *pkt_dev)
2133{
2134 int flow = pkt_dev->curfl;
2135
2136 if (pkt_dev->flags & F_FLOW_SEQ) {
2137 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2138 /* reset time */
2139 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002140 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002141 pkt_dev->curfl += 1;
2142 if (pkt_dev->curfl >= pkt_dev->cflows)
2143 pkt_dev->curfl = 0; /*reset */
2144 }
2145 } else {
2146 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002147 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002148
Robert Olsson1211a642008-08-05 18:44:26 -07002149 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002150 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002151 pkt_dev->flows[flow].flags = 0;
2152 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002153 }
2154
2155 return pkt_dev->curfl;
2156}
2157
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002158
2159#ifdef CONFIG_XFRM
2160/* If there was already an IPSEC SA, we keep it as is, else
2161 * we go look for it ...
2162*/
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002163static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002164{
2165 struct xfrm_state *x = pkt_dev->flows[flow].x;
2166 if (!x) {
2167 /*slow path: we dont already have xfrm_state*/
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002168 x = xfrm_stateonly_find(&init_net,
2169 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002170 (xfrm_address_t *)&pkt_dev->cur_saddr,
2171 AF_INET,
2172 pkt_dev->ipsmode,
2173 pkt_dev->ipsproto, 0);
2174 if (x) {
2175 pkt_dev->flows[flow].x = x;
2176 set_pkt_overhead(pkt_dev);
2177 pkt_dev->pkt_overhead+=x->props.header_len;
2178 }
2179
2180 }
2181}
2182#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002183static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2184{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002185
2186 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2187 pkt_dev->cur_queue_map = smp_processor_id();
2188
2189 else if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002190 __u16 t;
2191 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2192 t = random32() %
2193 (pkt_dev->queue_map_max -
2194 pkt_dev->queue_map_min + 1)
2195 + pkt_dev->queue_map_min;
2196 } else {
2197 t = pkt_dev->cur_queue_map + 1;
2198 if (t > pkt_dev->queue_map_max)
2199 t = pkt_dev->queue_map_min;
2200 }
2201 pkt_dev->cur_queue_map = t;
2202 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002203 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002204}
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206/* Increment/randomize headers according to flags and current values
2207 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2208 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002209static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2210{
2211 __u32 imn;
2212 __u32 imx;
2213 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002215 if (pkt_dev->cflows)
2216 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002219 if (pkt_dev->src_mac_count > 1) {
2220 __u32 mc;
2221 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Luiz Capitulino222f1802006-03-20 22:16:13 -08002223 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002224 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002225 else {
2226 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002227 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002228 pkt_dev->src_mac_count)
2229 pkt_dev->cur_src_mac_offset = 0;
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Luiz Capitulino222f1802006-03-20 22:16:13 -08002232 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2233 pkt_dev->hh[11] = tmp;
2234 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2235 pkt_dev->hh[10] = tmp;
2236 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2237 pkt_dev->hh[9] = tmp;
2238 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2239 pkt_dev->hh[8] = tmp;
2240 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2241 pkt_dev->hh[7] = tmp;
2242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Luiz Capitulino222f1802006-03-20 22:16:13 -08002244 /* Deal with Destination MAC */
2245 if (pkt_dev->dst_mac_count > 1) {
2246 __u32 mc;
2247 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Luiz Capitulino222f1802006-03-20 22:16:13 -08002249 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002250 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Luiz Capitulino222f1802006-03-20 22:16:13 -08002252 else {
2253 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002254 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002255 pkt_dev->dst_mac_count) {
2256 pkt_dev->cur_dst_mac_offset = 0;
2257 }
2258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Luiz Capitulino222f1802006-03-20 22:16:13 -08002260 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2261 pkt_dev->hh[5] = tmp;
2262 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2263 pkt_dev->hh[4] = tmp;
2264 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2265 pkt_dev->hh[3] = tmp;
2266 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2267 pkt_dev->hh[2] = tmp;
2268 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2269 pkt_dev->hh[1] = tmp;
2270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002272 if (pkt_dev->flags & F_MPLS_RND) {
2273 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002274 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002275 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2276 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002277 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002278 htonl(0x000fffff));
2279 }
2280
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002281 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002282 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002283 }
2284
2285 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002286 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002287 }
2288
Luiz Capitulino222f1802006-03-20 22:16:13 -08002289 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2290 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002291 pkt_dev->cur_udp_src = random32() %
2292 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2293 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Luiz Capitulino222f1802006-03-20 22:16:13 -08002295 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 pkt_dev->cur_udp_src++;
2297 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2298 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002299 }
2300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Luiz Capitulino222f1802006-03-20 22:16:13 -08002302 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2303 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002304 pkt_dev->cur_udp_dst = random32() %
2305 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2306 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002309 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002311 }
2312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 if (!(pkt_dev->flags & F_IPV6)) {
2315
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2317 ntohl(pkt_dev->
2318 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002320 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002321 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 else {
2323 t = ntohl(pkt_dev->cur_saddr);
2324 t++;
2325 if (t > imx) {
2326 t = imn;
2327 }
2328 }
2329 pkt_dev->cur_saddr = htonl(t);
2330 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002331
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002332 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2334 } else {
Al Viro252e33462006-11-14 20:48:11 -08002335 imn = ntohl(pkt_dev->daddr_min);
2336 imx = ntohl(pkt_dev->daddr_max);
2337 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002339 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (pkt_dev->flags & F_IPDST_RND) {
2341
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002342 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002343 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Joe Perches21cf2252007-12-16 13:44:00 -08002345 while (ipv4_is_loopback(s) ||
2346 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002347 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002348 ipv4_is_zeronet(s) ||
2349 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002350 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002351 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
Al Viro252e33462006-11-14 20:48:11 -08002353 pkt_dev->cur_daddr = s;
2354 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 t = ntohl(pkt_dev->cur_daddr);
2356 t++;
2357 if (t > imx) {
2358 t = imn;
2359 }
2360 pkt_dev->cur_daddr = htonl(t);
2361 }
2362 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002363 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002364 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002365 pkt_dev->flows[flow].cur_daddr =
2366 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002367#ifdef CONFIG_XFRM
2368 if (pkt_dev->flags & F_IPSEC_ON)
2369 get_ipsec_sa(pkt_dev, flow);
2370#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 pkt_dev->nflows++;
2372 }
2373 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002374 } else { /* IPV6 * */
2375
2376 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2377 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2378 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2379 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 else {
2381 int i;
2382
2383 /* Only random destinations yet */
2384
Luiz Capitulino222f1802006-03-20 22:16:13 -08002385 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002387 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002388 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2389 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393
Luiz Capitulino222f1802006-03-20 22:16:13 -08002394 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2395 __u32 t;
2396 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002397 t = random32() %
2398 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2399 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002402 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002404 }
2405 pkt_dev->cur_pkt_size = t;
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002408 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 pkt_dev->flows[flow].count++;
2411}
2412
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002413
2414#ifdef CONFIG_XFRM
2415static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2416{
2417 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2418 int err = 0;
2419 struct iphdr *iph;
2420
2421 if (!x)
2422 return 0;
2423 /* XXX: we dont support tunnel mode for now until
2424 * we resolve the dst issue */
2425 if (x->props.mode != XFRM_MODE_TRANSPORT)
2426 return 0;
2427
2428 spin_lock(&x->lock);
2429 iph = ip_hdr(skb);
2430
Herbert Xu13996372007-10-17 21:35:51 -07002431 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002432 if (err)
2433 goto error;
2434 err = x->type->output(x, skb);
2435 if (err)
2436 goto error;
2437
2438 x->curlft.bytes +=skb->len;
2439 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002440error:
2441 spin_unlock(&x->lock);
2442 return err;
2443}
2444
2445static inline void free_SAs(struct pktgen_dev *pkt_dev)
2446{
2447 if (pkt_dev->cflows) {
2448 /* let go of the SAs if we have them */
2449 int i = 0;
Florian Westphal5b5f7922009-05-21 15:07:12 -07002450 for (; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002451 struct xfrm_state *x = pkt_dev->flows[i].x;
2452 if (x) {
2453 xfrm_state_put(x);
2454 pkt_dev->flows[i].x = NULL;
2455 }
2456 }
2457 }
2458}
2459
2460static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2461 struct sk_buff *skb, __be16 protocol)
2462{
2463 if (pkt_dev->flags & F_IPSEC_ON) {
2464 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2465 int nhead = 0;
2466 if (x) {
2467 int ret;
2468 __u8 *eth;
2469 nhead = x->props.header_len - skb_headroom(skb);
2470 if (nhead >0) {
2471 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2472 if (ret < 0) {
David S. Miller25a8b252007-07-30 16:11:48 -07002473 printk(KERN_ERR "Error expanding "
2474 "ipsec packet %d\n",ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002475 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002476 }
2477 }
2478
2479 /* ipsec is not expecting ll header */
2480 skb_pull(skb, ETH_HLEN);
2481 ret = pktgen_output_ipsec(skb, pkt_dev);
2482 if (ret) {
David S. Miller25a8b252007-07-30 16:11:48 -07002483 printk(KERN_ERR "Error creating ipsec "
2484 "packet %d\n",ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002485 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002486 }
2487 /* restore ll */
2488 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2489 memcpy(eth, pkt_dev->hh, 12);
2490 *(u16 *) & eth[12] = protocol;
2491 }
2492 }
2493 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002494err:
2495 kfree_skb(skb);
2496 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002497}
2498#endif
2499
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002500static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2501{
2502 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002503 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002504 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2505 }
2506 mpls--;
2507 *mpls |= MPLS_STACK_BOTTOM;
2508}
2509
Al Viro0f37c602006-11-03 03:49:56 -08002510static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2511 unsigned int prio)
2512{
2513 return htons(id | (cfi << 12) | (prio << 13));
2514}
2515
Luiz Capitulino222f1802006-03-20 22:16:13 -08002516static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2517 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
2519 struct sk_buff *skb = NULL;
2520 __u8 *eth;
2521 struct udphdr *udph;
2522 int datalen, iplen;
2523 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002524 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002525 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002526 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002527 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2528 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2529 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2530 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002531 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002532
2533 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002534 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002535
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002536 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002537 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002538
Robert Olsson64053be2005-06-26 15:27:10 -07002539 /* Update any of the values, used when we're incrementing various
2540 * fields.
2541 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002542 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002543 mod_cur_headers(pkt_dev);
2544
David S. Miller7ac54592006-01-18 14:19:10 -08002545 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002546 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002547 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (!skb) {
2549 sprintf(pkt_dev->result, "No memory");
2550 return NULL;
2551 }
2552
David S. Miller7ac54592006-01-18 14:19:10 -08002553 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 /* Reserve for ethernet and IP header */
2556 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002557 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2558 if (pkt_dev->nr_labels)
2559 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002560
2561 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002562 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002563 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002564 *svlan_tci = build_tci(pkt_dev->svlan_id,
2565 pkt_dev->svlan_cfi,
2566 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002567 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002568 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002569 }
2570 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002571 *vlan_tci = build_tci(pkt_dev->vlan_id,
2572 pkt_dev->vlan_cfi,
2573 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002574 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002575 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002576 }
2577
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002578 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002579 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002580 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002581 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002582 iph = ip_hdr(skb);
2583 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002586 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002588 /* Eth + IPh + UDPh + mpls */
2589 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002590 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002591 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 udph->source = htons(pkt_dev->cur_udp_src);
2595 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002596 udph->len = htons(datalen + 8); /* DATA + udphdr */
2597 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 iph->ihl = 5;
2600 iph->version = 4;
2601 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002602 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002603 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 iph->saddr = pkt_dev->cur_saddr;
2605 iph->daddr = pkt_dev->cur_daddr;
2606 iph->frag_off = 0;
2607 iplen = 20 + 8 + datalen;
2608 iph->tot_len = htons(iplen);
2609 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002610 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002611 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002612 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002613 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 skb->dev = odev;
2615 skb->pkt_type = PACKET_HOST;
2616
Luiz Capitulino222f1802006-03-20 22:16:13 -08002617 if (pkt_dev->nfrags <= 0)
2618 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 else {
2620 int frags = pkt_dev->nfrags;
2621 int i;
2622
Luiz Capitulino222f1802006-03-20 22:16:13 -08002623 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (frags > MAX_SKB_FRAGS)
2626 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002627 if (datalen > frags * PAGE_SIZE) {
2628 skb_put(skb, datalen - frags * PAGE_SIZE);
2629 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631
2632 i = 0;
2633 while (datalen > 0) {
2634 struct page *page = alloc_pages(GFP_KERNEL, 0);
2635 skb_shinfo(skb)->frags[i].page = page;
2636 skb_shinfo(skb)->frags[i].page_offset = 0;
2637 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002638 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 datalen -= skb_shinfo(skb)->frags[i].size;
2640 skb->len += skb_shinfo(skb)->frags[i].size;
2641 skb->data_len += skb_shinfo(skb)->frags[i].size;
2642 i++;
2643 skb_shinfo(skb)->nr_frags = i;
2644 }
2645
2646 while (i < frags) {
2647 int rem;
2648
2649 if (i == 0)
2650 break;
2651
2652 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2653 if (rem == 0)
2654 break;
2655
2656 skb_shinfo(skb)->frags[i - 1].size -= rem;
2657
Luiz Capitulino222f1802006-03-20 22:16:13 -08002658 skb_shinfo(skb)->frags[i] =
2659 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002661 skb_shinfo(skb)->frags[i].page =
2662 skb_shinfo(skb)->frags[i - 1].page;
2663 skb_shinfo(skb)->frags[i].page_offset +=
2664 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 skb_shinfo(skb)->frags[i].size = rem;
2666 i++;
2667 skb_shinfo(skb)->nr_frags = i;
2668 }
2669 }
2670
Luiz Capitulino222f1802006-03-20 22:16:13 -08002671 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Luiz Capitulino222f1802006-03-20 22:16:13 -08002673 if (pgh) {
2674 struct timeval timestamp;
2675
2676 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2677 pgh->seq_num = htonl(pkt_dev->seq_num);
2678
2679 do_gettimeofday(&timestamp);
2680 pgh->tv_sec = htonl(timestamp.tv_sec);
2681 pgh->tv_usec = htonl(timestamp.tv_usec);
2682 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002683
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002684#ifdef CONFIG_XFRM
2685 if (!process_ipsec(pkt_dev, skb, protocol))
2686 return NULL;
2687#endif
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return skb;
2690}
2691
2692/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002693 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2695 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002696 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 * Should be candidate for net/ipv4/utils.c
2698 * --ro
2699 */
2700
Luiz Capitulino222f1802006-03-20 22:16:13 -08002701static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
2703 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002704 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 unsigned long u;
2706 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002707 unsigned int prefixlen = 0;
2708 unsigned int suffixlen = 0;
Al Viro252e33462006-11-14 20:48:11 -08002709 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002710 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Luiz Capitulino222f1802006-03-20 22:16:13 -08002712 for (i = 0; i < 16; i++)
2713 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 for (;;) {
2716 if (*s == ':') {
2717 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002718 if (s[1] == ':') { /* Found "::", skip to part 2 */
2719 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 len++;
2721 break;
2722 }
2723 s++;
2724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002726 u = simple_strtoul(s, &pos, 16);
2727 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002728 if (!i)
2729 return 0;
2730 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 /* the last 4 bytes may be written as IPv4 address */
2733
2734 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002735 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2736 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
2738 ip[prefixlen++] = (u >> 8);
2739 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002740 s += i;
2741 len += i;
2742 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return len;
2744 }
2745
2746/* part 2, after "::" */
2747 for (;;) {
2748 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002749 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 break;
2751 s++;
2752 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002753 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002755
2756 u = simple_strtol(s, &pos, 16);
2757 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002759 if (*s)
2760 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 break;
2762 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002763 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002765 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2766 sizeof(tmp));
2767 suffixlen += 4;
2768 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 break;
2770 }
2771 suffix[suffixlen++] = (u >> 8);
2772 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002773 s += i;
2774 len += i;
2775 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 break;
2777 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002778 for (i = 0; i < suffixlen; i++)
2779 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return len;
2781}
2782
Luiz Capitulino222f1802006-03-20 22:16:13 -08002783static char tohex(char hexdigit)
2784{
2785 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}
2787
Luiz Capitulino222f1802006-03-20 22:16:13 -08002788static int fmt_xlong(char *s, unsigned int i)
2789{
2790 char *bak = s;
2791 *s = tohex((i >> 12) & 0xf);
2792 if (s != bak || *s != '0')
2793 ++s;
2794 *s = tohex((i >> 8) & 0xf);
2795 if (s != bak || *s != '0')
2796 ++s;
2797 *s = tohex((i >> 4) & 0xf);
2798 if (s != bak || *s != '0')
2799 ++s;
2800 *s = tohex(i & 0xf);
2801 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Luiz Capitulino222f1802006-03-20 22:16:13 -08002804static unsigned int fmt_ip6(char *s, const char ip[16])
2805{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 unsigned int len;
2807 unsigned int i;
2808 unsigned int temp;
2809 unsigned int compressing;
2810 int j;
2811
Luiz Capitulino222f1802006-03-20 22:16:13 -08002812 len = 0;
2813 compressing = 0;
2814 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
2816#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002817 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2818 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2819 temp = strlen(s);
2820 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
2822#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002823 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2824 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (temp == 0) {
2826 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002827 compressing = 1;
2828 if (j == 0) {
2829 *s++ = ':';
2830 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
2832 }
2833 } else {
2834 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002835 compressing = 0;
2836 *s++ = ':';
2837 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002839 i = fmt_xlong(s, temp);
2840 len += i;
2841 s += i;
2842 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 *s++ = ':';
2844 ++len;
2845 }
2846 }
2847 }
2848 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002849 *s++ = ':';
2850 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002852 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return len;
2854}
2855
Luiz Capitulino222f1802006-03-20 22:16:13 -08002856static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2857 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 struct sk_buff *skb = NULL;
2860 __u8 *eth;
2861 struct udphdr *udph;
2862 int datalen;
2863 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002864 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002865 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002866 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002867 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2868 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2869 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2870 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002871 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002872
2873 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002874 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002875
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002876 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002877 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002878
Robert Olsson64053be2005-06-26 15:27:10 -07002879 /* Update any of the values, used when we're incrementing various
2880 * fields.
2881 */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002882 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002883 mod_cur_headers(pkt_dev);
2884
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002885 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002886 pkt_dev->pkt_overhead, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (!skb) {
2888 sprintf(pkt_dev->result, "No memory");
2889 return NULL;
2890 }
2891
2892 skb_reserve(skb, 16);
2893
2894 /* Reserve for ethernet and IP header */
2895 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002896 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2897 if (pkt_dev->nr_labels)
2898 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002899
2900 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002901 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002902 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002903 *svlan_tci = build_tci(pkt_dev->svlan_id,
2904 pkt_dev->svlan_cfi,
2905 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002906 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002907 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002908 }
2909 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002910 *vlan_tci = build_tci(pkt_dev->vlan_id,
2911 pkt_dev->vlan_cfi,
2912 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002913 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002914 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002915 }
2916
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002917 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002918 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002919 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002920 skb_set_queue_mapping(skb, queue_map);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002921 iph = ipv6_hdr(skb);
2922 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002925 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002926
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002927 /* Eth + IPh + UDPh + mpls */
2928 datalen = pkt_dev->cur_pkt_size - 14 -
2929 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002930 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Luiz Capitulino222f1802006-03-20 22:16:13 -08002932 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 datalen = sizeof(struct pktgen_hdr);
2934 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002935 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2936 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938
2939 udph->source = htons(pkt_dev->cur_udp_src);
2940 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002941 udph->len = htons(datalen + sizeof(struct udphdr));
2942 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002944 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002946 if (pkt_dev->traffic_class) {
2947 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002948 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002949 }
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 iph->hop_limit = 32;
2952
2953 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2954 iph->nexthdr = IPPROTO_UDP;
2955
2956 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2957 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2958
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002959 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002960 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002961 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 skb->dev = odev;
2963 skb->pkt_type = PACKET_HOST;
2964
Luiz Capitulino222f1802006-03-20 22:16:13 -08002965 if (pkt_dev->nfrags <= 0)
2966 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 else {
2968 int frags = pkt_dev->nfrags;
2969 int i;
2970
Luiz Capitulino222f1802006-03-20 22:16:13 -08002971 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (frags > MAX_SKB_FRAGS)
2974 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002975 if (datalen > frags * PAGE_SIZE) {
2976 skb_put(skb, datalen - frags * PAGE_SIZE);
2977 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979
2980 i = 0;
2981 while (datalen > 0) {
2982 struct page *page = alloc_pages(GFP_KERNEL, 0);
2983 skb_shinfo(skb)->frags[i].page = page;
2984 skb_shinfo(skb)->frags[i].page_offset = 0;
2985 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002986 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 datalen -= skb_shinfo(skb)->frags[i].size;
2988 skb->len += skb_shinfo(skb)->frags[i].size;
2989 skb->data_len += skb_shinfo(skb)->frags[i].size;
2990 i++;
2991 skb_shinfo(skb)->nr_frags = i;
2992 }
2993
2994 while (i < frags) {
2995 int rem;
2996
2997 if (i == 0)
2998 break;
2999
3000 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3001 if (rem == 0)
3002 break;
3003
3004 skb_shinfo(skb)->frags[i - 1].size -= rem;
3005
Luiz Capitulino222f1802006-03-20 22:16:13 -08003006 skb_shinfo(skb)->frags[i] =
3007 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003009 skb_shinfo(skb)->frags[i].page =
3010 skb_shinfo(skb)->frags[i - 1].page;
3011 skb_shinfo(skb)->frags[i].page_offset +=
3012 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 skb_shinfo(skb)->frags[i].size = rem;
3014 i++;
3015 skb_shinfo(skb)->nr_frags = i;
3016 }
3017 }
3018
Luiz Capitulino222f1802006-03-20 22:16:13 -08003019 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003021 if (pgh) {
3022 struct timeval timestamp;
3023
3024 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3025 pgh->seq_num = htonl(pkt_dev->seq_num);
3026
3027 do_gettimeofday(&timestamp);
3028 pgh->tv_sec = htonl(timestamp.tv_sec);
3029 pgh->tv_usec = htonl(timestamp.tv_usec);
3030 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003031 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return skb;
3034}
3035
Luiz Capitulino222f1802006-03-20 22:16:13 -08003036static inline struct sk_buff *fill_packet(struct net_device *odev,
3037 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003039 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return fill_packet_ipv6(odev, pkt_dev);
3041 else
3042 return fill_packet_ipv4(odev, pkt_dev);
3043}
3044
Luiz Capitulino222f1802006-03-20 22:16:13 -08003045static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003047 pkt_dev->seq_num = 1;
3048 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003050 pkt_dev->tx_bytes = 0;
3051 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
3054/* Set up structure for sending pkts, clear counters */
3055
3056static void pktgen_run(struct pktgen_thread *t)
3057{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003058 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 int started = 0;
3060
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003061 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003064 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 /*
3067 * setup odev and create initial packet.
3068 */
3069 pktgen_setup_inject(pkt_dev);
3070
Luiz Capitulino222f1802006-03-20 22:16:13 -08003071 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003073 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 pkt_dev->skb = NULL;
3075 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003076 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 pkt_dev->next_tx_ns = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003078 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 strcpy(pkt_dev->result, "Starting");
3081 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003082 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 strcpy(pkt_dev->result, "Error starting");
3084 }
3085 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003086 if (started)
3087 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
3090static void pktgen_stop_all_threads_ifs(void)
3091{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003092 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003094 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003096 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003097
3098 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003099 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003100
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003101 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
Luiz Capitulino222f1802006-03-20 22:16:13 -08003104static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003106 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003107 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003109 list_for_each_entry(pkt_dev, &t->if_list, list)
3110 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 res = 1;
3112 break;
3113 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003114 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
Luiz Capitulino222f1802006-03-20 22:16:13 -08003117static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003119 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Luiz Capitulino222f1802006-03-20 22:16:13 -08003121 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Luiz Capitulino222f1802006-03-20 22:16:13 -08003125 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Luiz Capitulino222f1802006-03-20 22:16:13 -08003127 if (signal_pending(current))
3128 goto signal;
3129 if_lock(t);
3130 }
3131 if_unlock(t);
3132 return 1;
3133signal:
3134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
3137static int pktgen_wait_all_threads_run(void)
3138{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003139 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003141
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003142 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003143
3144 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003146 if (sig == 0)
3147 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003149
3150 if (sig == 0)
3151 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003153
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003154 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return sig;
3156}
3157
3158static void pktgen_run_all_threads(void)
3159{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003160 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003162 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003164 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003166 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003168
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003169 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 pktgen_wait_all_threads_run();
3174}
3175
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003176static void pktgen_reset_all_threads(void)
3177{
3178 struct pktgen_thread *t;
3179
3180 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3181
3182 mutex_lock(&pktgen_thread_lock);
3183
3184 list_for_each_entry(t, &pktgen_threads, th_list)
3185 t->control |= (T_REMDEVALL);
3186
3187 mutex_unlock(&pktgen_thread_lock);
3188
3189 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3190
3191 pktgen_wait_all_threads_run();
3192}
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3195{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003196 __u64 total_us, bps, mbps, pps, idle;
3197 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Luiz Capitulino222f1802006-03-20 22:16:13 -08003199 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Luiz Capitulino222f1802006-03-20 22:16:13 -08003201 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Luiz Capitulino222f1802006-03-20 22:16:13 -08003203 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3204 (unsigned long long)total_us,
3205 (unsigned long long)(total_us - idle),
3206 (unsigned long long)idle,
3207 (unsigned long long)pkt_dev->sofar,
3208 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Luiz Capitulino222f1802006-03-20 22:16:13 -08003210 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Luiz Capitulino222f1802006-03-20 22:16:13 -08003212 while ((total_us >> 32) != 0) {
3213 pps >>= 1;
3214 total_us >>= 1;
3215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Luiz Capitulino222f1802006-03-20 22:16:13 -08003217 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Luiz Capitulino222f1802006-03-20 22:16:13 -08003219 bps = pps * 8 * pkt_dev->cur_pkt_size;
3220
3221 mbps = bps;
3222 do_div(mbps, 1000000);
3223 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3224 (unsigned long long)pps,
3225 (unsigned long long)mbps,
3226 (unsigned long long)bps,
3227 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230/* Set stopped-at timer, remove from running list, do counters & statistics */
3231
Luiz Capitulino222f1802006-03-20 22:16:13 -08003232static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003234 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003235
Luiz Capitulino222f1802006-03-20 22:16:13 -08003236 if (!pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003237 printk(KERN_WARNING "pktgen: interface: %s is already "
3238 "stopped\n", pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003239 return -EINVAL;
3240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Luiz Capitulino222f1802006-03-20 22:16:13 -08003242 pkt_dev->stopped_at = getCurUs();
3243 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003245 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Luiz Capitulino222f1802006-03-20 22:16:13 -08003247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Luiz Capitulino222f1802006-03-20 22:16:13 -08003250static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003252 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if_lock(t);
3255
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003256 list_for_each_entry(pkt_dev, &t->if_list, list) {
3257 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003258 continue;
3259 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003260 best = pkt_dev;
3261 else if (pkt_dev->next_tx_us < best->next_tx_us)
3262 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
3264 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003265 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Luiz Capitulino222f1802006-03-20 22:16:13 -08003268static void pktgen_stop(struct pktgen_thread *t)
3269{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003270 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003272 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Luiz Capitulino222f1802006-03-20 22:16:13 -08003274 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003276 list_for_each_entry(pkt_dev, &t->if_list, list) {
3277 pktgen_stop_device(pkt_dev);
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003278 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003279
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003280 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Luiz Capitulino222f1802006-03-20 22:16:13 -08003283 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003286/*
3287 * one of our devices needs to be removed - find it
3288 * and remove it
3289 */
3290static void pktgen_rem_one_if(struct pktgen_thread *t)
3291{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003292 struct list_head *q, *n;
3293 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003294
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003295 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003296
3297 if_lock(t);
3298
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003299 list_for_each_safe(q, n, &t->if_list) {
3300 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003301
Luiz Capitulino222f1802006-03-20 22:16:13 -08003302 if (!cur->removal_mark)
3303 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003304
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003305 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003306 cur->skb = NULL;
3307
3308 pktgen_remove_device(t, cur);
3309
3310 break;
3311 }
3312
3313 if_unlock(t);
3314}
3315
Luiz Capitulino222f1802006-03-20 22:16:13 -08003316static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003318 struct list_head *q, *n;
3319 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003320
3321 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003322
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003323 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003324 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003326 list_for_each_safe(q, n, &t->if_list) {
3327 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003328
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003329 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003330 cur->skb = NULL;
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 pktgen_remove_device(t, cur);
3333 }
3334
Luiz Capitulino222f1802006-03-20 22:16:13 -08003335 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336}
3337
Luiz Capitulino222f1802006-03-20 22:16:13 -08003338static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003340 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
David S. Milleree74baa2007-01-01 20:51:53 -08003342 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003344 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003346 list_del(&t->th_list);
3347
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003348 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349}
3350
3351static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3352{
Stephen Hemminger00829822008-11-20 20:14:53 -08003353 struct net_device *odev = pkt_dev->odev;
3354 int (*xmit)(struct sk_buff *, struct net_device *)
3355 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003356 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 __u64 idle_start = 0;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003358 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 int ret;
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3362 u64 now;
3363
3364 now = getCurUs();
3365 if (now < pkt_dev->next_tx_us)
3366 spin(pkt_dev, pkt_dev->next_tx_us);
3367
3368 /* This is max DELAY, this has special meaning of
3369 * "never transmit"
3370 */
3371 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3372 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3373 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3374 goto out;
3375 }
3376 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003377
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003378 if (!pkt_dev->skb) {
3379 set_cur_queue_map(pkt_dev);
3380 queue_map = pkt_dev->cur_queue_map;
3381 } else {
3382 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3383 }
3384
3385 txq = netdev_get_tx_queue(odev, queue_map);
3386 if (netif_tx_queue_stopped(txq) ||
David S. Millerc3f26a22008-07-31 16:58:50 -07003387 netif_tx_queue_frozen(txq) ||
Robert Olsson378be2c2007-08-28 15:43:14 -07003388 need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 if (!netif_running(odev)) {
3392 pktgen_stop_device(pkt_dev);
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003393 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003394 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 goto out;
3396 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003397 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003401
David S. Millerc3f26a22008-07-31 16:58:50 -07003402 if (netif_tx_queue_stopped(txq) ||
3403 netif_tx_queue_frozen(txq)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003404 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003406 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
3408 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003411 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3412 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 /* build a new pkt */
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003414 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 pkt_dev->skb = fill_packet(odev, pkt_dev);
3417 if (pkt_dev->skb == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003418 printk(KERN_ERR "pktgen: ERROR: couldn't "
3419 "allocate skb in fill_packet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003421 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 goto out;
3423 }
3424 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003425 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
3427 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003428
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003429 /* fill_packet() might have changed the queue */
3430 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3431 txq = netdev_get_tx_queue(odev, queue_map);
3432
3433 __netif_tx_lock_bh(txq);
David S. Millerc3f26a22008-07-31 16:58:50 -07003434 if (!netif_tx_queue_stopped(txq) &&
3435 !netif_tx_queue_frozen(txq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003438 retry_now:
Stephen Hemminger00829822008-11-20 20:14:53 -08003439 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 if (likely(ret == NETDEV_TX_OK)) {
Eric Dumazet08baf562009-05-25 22:58:01 -07003441 txq_trans_update(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003442 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 pkt_dev->sofar++;
3444 pkt_dev->seq_num++;
3445 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003446
3447 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 && (odev->features & NETIF_F_LLTX)) {
3449 cpu_relax();
3450 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003451 } else { /* Retry it next time */
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if (debug && net_ratelimit())
3456 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 pkt_dev->errors++;
3459 pkt_dev->last_ok = 0;
3460 }
3461
3462 pkt_dev->next_tx_us = getCurUs();
3463 pkt_dev->next_tx_ns = 0;
3464
3465 pkt_dev->next_tx_us += pkt_dev->delay_us;
3466 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3467
3468 if (pkt_dev->next_tx_ns > 1000) {
3469 pkt_dev->next_tx_us++;
3470 pkt_dev->next_tx_ns -= 1000;
3471 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Luiz Capitulino222f1802006-03-20 22:16:13 -08003474 else { /* Retry it next time */
3475 pkt_dev->last_ok = 0;
3476 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003480 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 /* If pkt_dev->count is zero, then run forever */
3483 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3484 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3485 idle_start = getCurUs();
3486 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3487 if (signal_pending(current)) {
3488 break;
3489 }
3490 schedule();
3491 }
3492 pkt_dev->idle_acc += getCurUs() - idle_start;
3493 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 /* Done with this */
3496 pktgen_stop_device(pkt_dev);
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003497 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003498 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003499 }
3500out:;
3501}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003503/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 * Main loop of the thread goes here
3505 */
3506
David S. Milleree74baa2007-01-01 20:51:53 -08003507static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
3509 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003510 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003511 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
David S. Milleree74baa2007-01-01 20:51:53 -08003514 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
3516 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003517 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003519 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
David S. Milleree74baa2007-01-01 20:51:53 -08003521 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003522
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003523 set_freezable();
3524
David S. Milleree74baa2007-01-01 20:51:53 -08003525 while (!kthread_should_stop()) {
3526 pkt_dev = next_to_run(t);
3527
3528 if (!pkt_dev &&
3529 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3530 == 0) {
3531 prepare_to_wait(&(t->queue), &wait,
3532 TASK_INTERRUPTIBLE);
3533 schedule_timeout(HZ / 10);
3534 finish_wait(&(t->queue), &wait);
3535 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 __set_current_state(TASK_RUNNING);
3538
Robert Olssonb1639112007-08-28 15:46:58 -07003539 if (pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 pktgen_xmit(pkt_dev);
3541
Luiz Capitulino222f1802006-03-20 22:16:13 -08003542 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 pktgen_stop(t);
3544 t->control &= ~(T_STOP);
3545 }
3546
Luiz Capitulino222f1802006-03-20 22:16:13 -08003547 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 pktgen_run(t);
3549 t->control &= ~(T_RUN);
3550 }
3551
Luiz Capitulino222f1802006-03-20 22:16:13 -08003552 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003554 t->control &= ~(T_REMDEVALL);
3555 }
3556
Luiz Capitulino222f1802006-03-20 22:16:13 -08003557 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003558 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 t->control &= ~(T_REMDEV);
3560 }
3561
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003562 try_to_freeze();
3563
David S. Milleree74baa2007-01-01 20:51:53 -08003564 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003567 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003568 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003570 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003571 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003573 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003574 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003575
David S. Milleree74baa2007-01-01 20:51:53 -08003576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Luiz Capitulino222f1802006-03-20 22:16:13 -08003579static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3580 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003582 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003583 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003585 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003586 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003587 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 break;
3589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Luiz Capitulino222f1802006-03-20 22:16:13 -08003591 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003592 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003593 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003596/*
3597 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 */
3599
Luiz Capitulino222f1802006-03-20 22:16:13 -08003600static int add_dev_to_thread(struct pktgen_thread *t,
3601 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
3603 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Luiz Capitulino222f1802006-03-20 22:16:13 -08003605 if_lock(t);
3606
3607 if (pkt_dev->pg_thread) {
David S. Miller25a8b252007-07-30 16:11:48 -07003608 printk(KERN_ERR "pktgen: ERROR: already assigned "
3609 "to a thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003610 rv = -EBUSY;
3611 goto out;
3612 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003613
3614 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 pkt_dev->running = 0;
3617
Luiz Capitulino222f1802006-03-20 22:16:13 -08003618out:
3619 if_unlock(t);
3620 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
3623/* Called under thread lock */
3624
Luiz Capitulino222f1802006-03-20 22:16:13 -08003625static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003627 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003628 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 /* We don't allow a device to be on several threads */
3631
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003632 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3633 if (pkt_dev) {
David S. Miller25a8b252007-07-30 16:11:48 -07003634 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003635 return -EBUSY;
3636 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003637
3638 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3639 if (!pkt_dev)
3640 return -ENOMEM;
3641
Luiz Capitulino222f1802006-03-20 22:16:13 -08003642 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003643 if (pkt_dev->flows == NULL) {
3644 kfree(pkt_dev);
3645 return -ENOMEM;
3646 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003648
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003649 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003650 pkt_dev->min_pkt_size = ETH_ZLEN;
3651 pkt_dev->max_pkt_size = ETH_ZLEN;
3652 pkt_dev->nfrags = 0;
3653 pkt_dev->clone_skb = pg_clone_skb_d;
3654 pkt_dev->delay_us = pg_delay_d / 1000;
3655 pkt_dev->delay_ns = pg_delay_d % 1000;
3656 pkt_dev->count = pg_count_d;
3657 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003658 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003659 pkt_dev->udp_src_max = 9;
3660 pkt_dev->udp_dst_min = 9;
3661 pkt_dev->udp_dst_max = 9;
3662
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003663 pkt_dev->vlan_p = 0;
3664 pkt_dev->vlan_cfi = 0;
3665 pkt_dev->vlan_id = 0xffff;
3666 pkt_dev->svlan_p = 0;
3667 pkt_dev->svlan_cfi = 0;
3668 pkt_dev->svlan_id = 0xffff;
3669
Stephen Hemminger39df2322007-03-04 16:11:51 -08003670 err = pktgen_setup_dev(pkt_dev, ifname);
3671 if (err)
3672 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003673
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003674 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3675 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003676 if (!pkt_dev->entry) {
David S. Miller25a8b252007-07-30 16:11:48 -07003677 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003678 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003679 err = -EINVAL;
3680 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003681 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003682#ifdef CONFIG_XFRM
3683 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3684 pkt_dev->ipsproto = IPPROTO_ESP;
3685#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003686
3687 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003688out2:
3689 dev_put(pkt_dev->odev);
3690out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003691#ifdef CONFIG_XFRM
3692 free_SAs(pkt_dev);
3693#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003694 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003695 kfree(pkt_dev);
3696 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
3698
David S. Milleree74baa2007-01-01 20:51:53 -08003699static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003701 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003702 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003703 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Luiz Capitulino222f1802006-03-20 22:16:13 -08003705 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3706 if (!t) {
David S. Miller25a8b252007-07-30 16:11:48 -07003707 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3708 "create new thread.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003709 return -ENOMEM;
3710 }
3711
Luiz Capitulino222f1802006-03-20 22:16:13 -08003712 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003714
David S. Milleree74baa2007-01-01 20:51:53 -08003715 INIT_LIST_HEAD(&t->if_list);
3716
3717 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003718 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003719
3720 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3721 if (IS_ERR(p)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003722 printk(KERN_ERR "pktgen: kernel_thread() failed "
3723 "for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003724 list_del(&t->th_list);
3725 kfree(t);
3726 return PTR_ERR(p);
3727 }
3728 kthread_bind(p, cpu);
3729 t->tsk = p;
3730
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003731 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3732 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003733 if (!pe) {
David S. Miller25a8b252007-07-30 16:11:48 -07003734 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003735 PG_PROC_DIR, t->tsk->comm);
3736 kthread_stop(p);
3737 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003738 kfree(t);
3739 return -EINVAL;
3740 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003741
David S. Milleree74baa2007-01-01 20:51:53 -08003742 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003743 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745 return 0;
3746}
3747
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003748/*
3749 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003751static void _rem_dev_from_if_list(struct pktgen_thread *t,
3752 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003754 struct list_head *q, *n;
3755 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003757 list_for_each_safe(q, n, &t->if_list) {
3758 p = list_entry(q, struct pktgen_dev, list);
3759 if (p == pkt_dev)
3760 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 }
3762}
3763
Luiz Capitulino222f1802006-03-20 22:16:13 -08003764static int pktgen_remove_device(struct pktgen_thread *t,
3765 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
3767
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003768 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Luiz Capitulino222f1802006-03-20 22:16:13 -08003770 if (pkt_dev->running) {
David S. Miller25a8b252007-07-30 16:11:48 -07003771 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3772 "running interface, stopping it now.\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003773 pktgen_stop_device(pkt_dev);
3774 }
3775
3776 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
3778 if (pkt_dev->odev) {
3779 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003780 pkt_dev->odev = NULL;
3781 }
3782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 /* And update the thread if_list */
3784
3785 _rem_dev_from_if_list(t, pkt_dev);
3786
Stephen Hemminger39df2322007-03-04 16:11:51 -08003787 if (pkt_dev->entry)
3788 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003790#ifdef CONFIG_XFRM
3791 free_SAs(pkt_dev);
3792#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003793 vfree(pkt_dev->flows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
Luiz Capitulino222f1802006-03-20 22:16:13 -08003798static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
3800 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003801 struct proc_dir_entry *pe;
3802
David S. Miller25a8b252007-07-30 16:11:48 -07003803 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003805 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003806 if (!pg_proc_dir)
3807 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Wang Chen25296d52008-02-28 14:11:49 -08003809 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003810 if (pe == NULL) {
David S. Miller25a8b252007-07-30 16:11:48 -07003811 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3812 "procfs entry.\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003813 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003814 return -EINVAL;
3815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 /* Register us to receive netdevice events */
3818 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003819
John Hawkes670c02c2005-10-13 09:30:31 -07003820 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003821 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
David S. Milleree74baa2007-01-01 20:51:53 -08003823 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003824 if (err)
David S. Miller25a8b252007-07-30 16:11:48 -07003825 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3826 "thread for cpu %d (%d)\n", cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003827 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003828
3829 if (list_empty(&pktgen_threads)) {
David S. Miller25a8b252007-07-30 16:11:48 -07003830 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3831 "all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003832 unregister_netdevice_notifier(&pktgen_notifier_block);
3833 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003834 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003835 return -ENODEV;
3836 }
3837
Luiz Capitulino222f1802006-03-20 22:16:13 -08003838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839}
3840
3841static void __exit pg_cleanup(void)
3842{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003843 struct pktgen_thread *t;
3844 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 wait_queue_head_t queue;
3846 init_waitqueue_head(&queue);
3847
Luiz Capitulino222f1802006-03-20 22:16:13 -08003848 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003850 list_for_each_safe(q, n, &pktgen_threads) {
3851 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003852 kthread_stop(t->tsk);
3853 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Luiz Capitulino222f1802006-03-20 22:16:13 -08003856 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 unregister_netdevice_notifier(&pktgen_notifier_block);
3858
Luiz Capitulino222f1802006-03-20 22:16:13 -08003859 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003860 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003861 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862}
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864module_init(pg_init);
3865module_exit(pg_cleanup);
3866
3867MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3868MODULE_DESCRIPTION("Packet Generator tool");
3869MODULE_LICENSE("GPL");
3870module_param(pg_count_d, int, 0);
3871module_param(pg_delay_d, int, 0);
3872module_param(pg_clone_skb_d, int, 0);
3873module_param(debug, int, 0);