blob: ff79d94b5944348256ff45fee2fa16d9b406586e [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 */
Joe Perchesf9467ea2010-06-21 12:29:14 +0000118
119#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <linux/sys.h>
122#include <linux/types.h>
123#include <linux/module.h>
124#include <linux/moduleparam.h>
125#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800126#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <linux/sched.h>
128#include <linux/slab.h>
129#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#include <linux/unistd.h>
131#include <linux/string.h>
132#include <linux/ptrace.h>
133#include <linux/errno.h>
134#include <linux/ioport.h>
135#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800136#include <linux/capability.h>
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700137#include <linux/hrtimer.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700138#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#include <linux/delay.h>
140#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800141#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#include <linux/init.h>
143#include <linux/skbuff.h>
144#include <linux/netdevice.h>
145#include <linux/inet.h>
146#include <linux/inetdevice.h>
147#include <linux/rtnetlink.h>
148#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700149#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#include <linux/in.h>
151#include <linux/ip.h>
152#include <linux/ipv6.h>
153#include <linux/udp.h>
154#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700155#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800157#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800158#include <linux/kthread.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200159#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#include <net/checksum.h>
161#include <net/ipv6.h>
162#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700163#ifdef CONFIG_XFRM
164#include <net/xfrm.h>
165#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#include <asm/byteorder.h>
167#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700168#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000169#include <linux/io.h>
170#include <linux/timex.h>
171#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800173#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Daniel Turull43d28b62010-06-09 22:49:57 +0000175#define VERSION "2.74"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800177#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800178#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Joe Perchesf9467ea2010-06-21 12:29:14 +0000180#define func_enter() pr_debug("entering %s\n", __func__);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800183#define F_IPSRC_RND (1<<0) /* IP-Src Random */
184#define F_IPDST_RND (1<<1) /* IP-Dst Random */
185#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
186#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
187#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
188#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
189#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
190#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800191#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700192#define F_VID_RND (1<<9) /* Random VLAN ID */
193#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700194#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700195#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700196#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700197#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000198#define F_NODE (1<<15) /* Node memory alloc*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000201#define T_STOP (1<<0) /* Stop run */
202#define T_RUN (1<<1) /* Start run */
203#define T_REMDEVALL (1<<2) /* Remove all devs */
204#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* If lock -- can be removed after some work */
207#define if_lock(t) spin_lock(&(t->if_lock));
208#define if_unlock(t) spin_unlock(&(t->if_lock));
209
210/* Used to help with determining the pkts on receive */
211#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700212#define PG_PROC_DIR "pktgen"
213#define PGCTRL "pgctrl"
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000214static struct proc_dir_entry *pg_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216#define MAX_CFLOWS 65536
217
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700218#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
219#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
220
Luiz Capitulino222f1802006-03-20 22:16:13 -0800221struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800222 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800223 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700224#ifdef CONFIG_XFRM
225 struct xfrm_state *x;
226#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700227 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
229
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700230/* flow flag bits */
231#define F_INIT (1<<0) /* flow has been initialized */
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /*
235 * Try to keep frequent/infrequent used vars. separated.
236 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800237 struct proc_dir_entry *entry; /* proc file */
238 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000239 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000241 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800242
243 /* If min != max, then we will either do a linear iteration, or
244 * we will do a random selection from within the range.
245 */
246 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800247 int removal_mark; /* non-zero => the device is marked for
248 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Luiz Capitulino222f1802006-03-20 22:16:13 -0800250 int min_pkt_size; /* = ETH_ZLEN; */
251 int max_pkt_size; /* = ETH_ZLEN; */
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700252 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800253 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800254 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000255 u64 delay; /* nano-seconds */
256
Luiz Capitulino222f1802006-03-20 22:16:13 -0800257 __u64 count; /* Default No packets to send */
258 __u64 sofar; /* How many pkts we've sent so far */
259 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800260 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Luiz Capitulino222f1802006-03-20 22:16:13 -0800262 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Luiz Capitulino222f1802006-03-20 22:16:13 -0800264 __u64 allocated_skbs;
265 __u32 clone_count;
266 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000267 * Or a failed transmit of some sort?
268 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800269 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000270 ktime_t next_tx;
271 ktime_t started_at;
272 ktime_t stopped_at;
273 u64 idle_acc; /* nano-seconds */
274
Luiz Capitulino222f1802006-03-20 22:16:13 -0800275 __u32 seq_num;
276
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000277 int clone_skb; /*
278 * Use multiple SKBs during packet gen.
279 * If this number is greater than 1, then
280 * that many copies of the same packet will be
281 * sent before a new packet is allocated.
282 * If you want to send 1024 identical packets
283 * before creating a new packet,
284 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800285 */
286
287 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
288 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
289 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
290 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
291
292 struct in6_addr in6_saddr;
293 struct in6_addr in6_daddr;
294 struct in6_addr cur_in6_daddr;
295 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800297 struct in6_addr min_in6_daddr;
298 struct in6_addr max_in6_daddr;
299 struct in6_addr min_in6_saddr;
300 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Luiz Capitulino222f1802006-03-20 22:16:13 -0800302 /* If we're doing ranges, random or incremental, then this
303 * defines the min/max for those ranges.
304 */
Al Viro252e33462006-11-14 20:48:11 -0800305 __be32 saddr_min; /* inclusive, source IP address */
306 __be32 saddr_max; /* exclusive, source IP address */
307 __be32 daddr_min; /* inclusive, dest IP address */
308 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Luiz Capitulino222f1802006-03-20 22:16:13 -0800310 __u16 udp_src_min; /* inclusive, source UDP port */
311 __u16 udp_src_max; /* exclusive, source UDP port */
312 __u16 udp_dst_min; /* inclusive, dest UDP port */
313 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700315 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000316 __u8 tos; /* six MSB of (former) IPv4 TOS
317 are for dscp codepoint */
318 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
319 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700320
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800321 /* MPLS */
322 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
323 __be32 labels[MAX_MPLS_LABELS];
324
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700325 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
326 __u8 vlan_p;
327 __u8 vlan_cfi;
328 __u16 vlan_id; /* 0xffff means no vlan tag */
329
330 __u8 svlan_p;
331 __u8 svlan_cfi;
332 __u16 svlan_id; /* 0xffff means no svlan tag */
333
Luiz Capitulino222f1802006-03-20 22:16:13 -0800334 __u32 src_mac_count; /* How many MACs to iterate through */
335 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Luiz Capitulino222f1802006-03-20 22:16:13 -0800337 unsigned char dst_mac[ETH_ALEN];
338 unsigned char src_mac[ETH_ALEN];
339
340 __u32 cur_dst_mac_offset;
341 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800342 __be32 cur_saddr;
343 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700344 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800345 __u16 cur_udp_dst;
346 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700347 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800348 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800349 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800350
351 __u8 hh[14];
352 /* = {
353 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
354
355 We fill in SRC address later
356 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
357 0x08, 0x00
358 };
359 */
360 __u16 pad; /* pad out the hh struct to an even 16 bytes */
361
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000362 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800363 * are transmitting the same one multiple times
364 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000365 struct net_device *odev; /* The out-going device.
366 * Note that the device should have it's
367 * pg_info pointer pointing back to this
368 * device.
369 * Set when the user specifies the out-going
370 * device name (not when the inject is
371 * started as it used to do.)
372 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000373 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800375 unsigned cflows; /* Concurrent flows (config) */
376 unsigned lflow; /* Flow length (config) */
377 unsigned nflows; /* accumulated flows (stats) */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700378 unsigned curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700379
380 u16 queue_map_min;
381 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000382 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000383 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700384
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700385#ifdef CONFIG_XFRM
386 __u8 ipsmode; /* IPSEC mode (config) */
387 __u8 ipsproto; /* IPSEC type (config) */
388#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800389 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390};
391
392struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800393 __be32 pgh_magic;
394 __be32 seq_num;
395 __be32 tv_sec;
396 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};
398
Eric Dumazet551eaff2010-11-21 10:26:44 -0800399static bool pktgen_exiting __read_mostly;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000402 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800403 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800404 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800405 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800406 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000408 /* Field for thread to receive "posted" events terminate,
409 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800410
411 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int cpu;
413
Luiz Capitulino222f1802006-03-20 22:16:13 -0800414 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700415 struct completion start_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416};
417
418#define REMOVE 1
419#define FIND 0
420
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000421static inline ktime_t ktime_now(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000423 struct timespec ts;
424 ktime_get_ts(&ts);
425
426 return timespec_to_ktime(ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000429/* This works even if 32 bit because of careful byte order choice */
430static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000432 return cmp1.tv64 < cmp2.tv64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000435static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000436 "Packet Generator for packet performance testing. "
437 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Luiz Capitulino222f1802006-03-20 22:16:13 -0800439static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
440static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
441static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800442 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
444static void pktgen_run_all_threads(void);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800445static void pktgen_reset_all_threads(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446static void pktgen_stop_all_threads_ifs(void);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000447
Luiz Capitulino222f1802006-03-20 22:16:13 -0800448static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800450
Luiz Capitulino222f1802006-03-20 22:16:13 -0800451static unsigned int scan_ip6(const char *s, char ip[16]);
452static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000455static int pg_count_d __read_mostly = 1000;
456static int pg_delay_d __read_mostly;
457static int pg_clone_skb_d __read_mostly;
458static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Luiz Capitulino222fa072006-03-20 22:24:27 -0800460static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800461static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static struct notifier_block pktgen_notifier_block = {
464 .notifier_call = pktgen_device_event,
465};
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900468 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
470 */
471
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700472static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800473{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000474 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000478static ssize_t pgctrl_write(struct file *file, const char __user *buf,
479 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700482 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Luiz Capitulino222f1802006-03-20 22:16:13 -0800484 if (!capable(CAP_NET_ADMIN)) {
485 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700489 if (count > sizeof(data))
490 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700493 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700494 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800495 }
496 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 pktgen_stop_all_threads_ifs();
500
Luiz Capitulino222f1802006-03-20 22:16:13 -0800501 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 pktgen_run_all_threads();
503
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800504 else if (!strcmp(data, "reset"))
505 pktgen_reset_all_threads();
506
Luiz Capitulino222f1802006-03-20 22:16:13 -0800507 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000508 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 err = count;
511
Luiz Capitulino222f1802006-03-20 22:16:13 -0800512out:
513 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700516static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700518 return single_open(file, pgctrl_show, PDE(inode)->data);
519}
520
Arjan van de Ven9a321442007-02-12 00:55:35 -0800521static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800522 .owner = THIS_MODULE,
523 .open = pgctrl_open,
524 .read = seq_read,
525 .llseek = seq_lseek,
526 .write = pgctrl_write,
527 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700528};
529
530static int pktgen_if_show(struct seq_file *seq, void *v)
531{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000532 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000533 ktime_t stopped;
534 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Luiz Capitulino222f1802006-03-20 22:16:13 -0800536 seq_printf(seq,
537 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
538 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
539 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Luiz Capitulino222f1802006-03-20 22:16:13 -0800541 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000542 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
543 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000544 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Luiz Capitulino222f1802006-03-20 22:16:13 -0800546 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
547 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Robert Olsson45b270f2007-08-28 15:45:55 -0700549 seq_printf(seq,
550 " queue_map_min: %u queue_map_max: %u\n",
551 pkt_dev->queue_map_min,
552 pkt_dev->queue_map_max);
553
John Fastabend9e50e3a2010-11-16 19:12:28 +0000554 if (pkt_dev->skb_priority)
555 seq_printf(seq, " skb_priority: %u\n",
556 pkt_dev->skb_priority);
557
Luiz Capitulino222f1802006-03-20 22:16:13 -0800558 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800560 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
561 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
562 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
563 seq_printf(seq,
564 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
565 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Luiz Capitulino222f1802006-03-20 22:16:13 -0800567 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
568 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
569 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
570 seq_printf(seq,
571 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
572 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000574 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800575 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000576 " dst_min: %s dst_max: %s\n",
577 pkt_dev->dst_min, pkt_dev->dst_max);
578 seq_printf(seq,
579 " src_min: %s src_max: %s\n",
580 pkt_dev->src_min, pkt_dev->src_max);
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700583 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Johannes Berge1749612008-10-27 15:59:26 -0700585 seq_printf(seq, "%pM ",
586 is_zero_ether_addr(pkt_dev->src_mac) ?
587 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Luiz Capitulino222f1802006-03-20 22:16:13 -0800589 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700590 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Luiz Capitulino222f1802006-03-20 22:16:13 -0800592 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000593 " udp_src_min: %d udp_src_max: %d"
594 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800595 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
596 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Luiz Capitulino222f1802006-03-20 22:16:13 -0800598 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800599 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700600 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800602 if (pkt_dev->nr_labels) {
603 unsigned i;
604 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700605 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800606 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
607 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
608 }
609
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000610 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700611 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000612 pkt_dev->vlan_id, pkt_dev->vlan_p,
613 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700614
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000615 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700616 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000617 pkt_dev->svlan_id, pkt_dev->svlan_p,
618 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700619
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000620 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700621 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700622
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000623 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700624 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700625
Robert Olssone99b99b2010-03-18 22:44:30 +0000626 if (pkt_dev->node >= 0)
627 seq_printf(seq, " node: %d\n", pkt_dev->node);
628
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800629 seq_printf(seq, " Flags: ");
630
Luiz Capitulino222f1802006-03-20 22:16:13 -0800631 if (pkt_dev->flags & F_IPV6)
632 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Luiz Capitulino222f1802006-03-20 22:16:13 -0800634 if (pkt_dev->flags & F_IPSRC_RND)
635 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Luiz Capitulino222f1802006-03-20 22:16:13 -0800637 if (pkt_dev->flags & F_IPDST_RND)
638 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Luiz Capitulino222f1802006-03-20 22:16:13 -0800640 if (pkt_dev->flags & F_TXSIZE_RND)
641 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Luiz Capitulino222f1802006-03-20 22:16:13 -0800643 if (pkt_dev->flags & F_UDPSRC_RND)
644 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Luiz Capitulino222f1802006-03-20 22:16:13 -0800646 if (pkt_dev->flags & F_UDPDST_RND)
647 seq_printf(seq, "UDPDST_RND ");
648
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800649 if (pkt_dev->flags & F_MPLS_RND)
650 seq_printf(seq, "MPLS_RND ");
651
Robert Olsson45b270f2007-08-28 15:45:55 -0700652 if (pkt_dev->flags & F_QUEUE_MAP_RND)
653 seq_printf(seq, "QUEUE_MAP_RND ");
654
Robert Olssone6fce5b2008-08-07 02:23:01 -0700655 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
656 seq_printf(seq, "QUEUE_MAP_CPU ");
657
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700658 if (pkt_dev->cflows) {
659 if (pkt_dev->flags & F_FLOW_SEQ)
660 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
661 else
662 seq_printf(seq, "FLOW_RND ");
663 }
664
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700665#ifdef CONFIG_XFRM
666 if (pkt_dev->flags & F_IPSEC_ON)
667 seq_printf(seq, "IPSEC ");
668#endif
669
Luiz Capitulino222f1802006-03-20 22:16:13 -0800670 if (pkt_dev->flags & F_MACSRC_RND)
671 seq_printf(seq, "MACSRC_RND ");
672
673 if (pkt_dev->flags & F_MACDST_RND)
674 seq_printf(seq, "MACDST_RND ");
675
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700676 if (pkt_dev->flags & F_VID_RND)
677 seq_printf(seq, "VID_RND ");
678
679 if (pkt_dev->flags & F_SVID_RND)
680 seq_printf(seq, "SVID_RND ");
681
Robert Olssone99b99b2010-03-18 22:44:30 +0000682 if (pkt_dev->flags & F_NODE)
683 seq_printf(seq, "NODE_ALLOC ");
684
Luiz Capitulino222f1802006-03-20 22:16:13 -0800685 seq_puts(seq, "\n");
686
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000687 /* not really stopped, more like last-running-at */
688 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
689 idle = pkt_dev->idle_acc;
690 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800691
692 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000693 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800694 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000695 (unsigned long long)pkt_dev->errors);
696
697 seq_printf(seq,
698 " started: %lluus stopped: %lluus idle: %lluus\n",
699 (unsigned long long) ktime_to_us(pkt_dev->started_at),
700 (unsigned long long) ktime_to_us(stopped),
701 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800702
703 seq_printf(seq,
704 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700705 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
706 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Luiz Capitulino222f1802006-03-20 22:16:13 -0800708 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800710 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
711 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
712 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
713 } else
714 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700715 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Luiz Capitulino222f1802006-03-20 22:16:13 -0800717 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700718 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Robert Olsson45b270f2007-08-28 15:45:55 -0700720 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
721
Luiz Capitulino222f1802006-03-20 22:16:13 -0800722 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800725 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800727 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800732
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000733static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
734 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800735{
736 int i = 0;
737 *num = 0;
738
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700739 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000740 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800741 char c;
742 *num <<= 4;
743 if (get_user(c, &user_buffer[i]))
744 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000745 value = hex_to_bin(c);
746 if (value >= 0)
747 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800748 else
749 break;
750 }
751 return i;
752}
753
Luiz Capitulino222f1802006-03-20 22:16:13 -0800754static int count_trail_chars(const char __user * user_buffer,
755 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 int i;
758
759 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800760 char c;
761 if (get_user(c, &user_buffer[i]))
762 return -EFAULT;
763 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 case '\"':
765 case '\n':
766 case '\r':
767 case '\t':
768 case ' ':
769 case '=':
770 break;
771 default:
772 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775done:
776 return i;
777}
778
Luiz Capitulino222f1802006-03-20 22:16:13 -0800779static unsigned long num_arg(const char __user * user_buffer,
780 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000782 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800784
Paul Gortmakerd6182222010-10-18 12:14:44 +0000785 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800786 char c;
787 if (get_user(c, &user_buffer[i]))
788 return -EFAULT;
789 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800791 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 } else
793 break;
794 }
795 return i;
796}
797
Luiz Capitulino222f1802006-03-20 22:16:13 -0800798static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000800 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Paul Gortmakerd6182222010-10-18 12:14:44 +0000802 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800803 char c;
804 if (get_user(c, &user_buffer[i]))
805 return -EFAULT;
806 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 case '\"':
808 case '\n':
809 case '\r':
810 case '\t':
811 case ' ':
812 goto done_str;
813 break;
814 default:
815 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return i;
820}
821
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800822static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
823{
824 unsigned n = 0;
825 char c;
826 ssize_t i = 0;
827 int len;
828
829 pkt_dev->nr_labels = 0;
830 do {
831 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700832 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800833 if (len <= 0)
834 return len;
835 pkt_dev->labels[n] = htonl(tmp);
836 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
837 pkt_dev->flags |= F_MPLS_RND;
838 i += len;
839 if (get_user(c, &buffer[i]))
840 return -EFAULT;
841 i++;
842 n++;
843 if (n >= MAX_MPLS_LABELS)
844 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700845 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800846
847 pkt_dev->nr_labels = n;
848 return i;
849}
850
Luiz Capitulino222f1802006-03-20 22:16:13 -0800851static ssize_t pktgen_if_write(struct file *file,
852 const char __user * user_buffer, size_t count,
853 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Joe Perches8a994a72010-07-12 10:50:23 +0000855 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800856 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000857 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 char name[16], valstr[32];
859 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800860 char *pg_result = NULL;
861 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863
864 pg_result = &(pkt_dev->result[0]);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000867 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return -EINVAL;
869 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800870
Paul Gortmakerd6182222010-10-18 12:14:44 +0000871 max = count;
872 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800873 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000874 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000877 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* Read variable name */
880
881 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000882 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800883 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -EFAULT;
888 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889
890 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 len = count_trail_chars(&user_buffer[i], max);
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;
896
897 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000898 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700899 char tb[copy + 1];
900 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700902 tb[copy] = 0;
David S. Miller25a8b252007-07-30 16:11:48 -0700903 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 (unsigned long)count, tb);
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (!strcmp(name, "min_pkt_size")) {
908 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000909 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800910 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800913 if (value < 14 + 20 + 8)
914 value = 14 + 20 + 8;
915 if (value != pkt_dev->min_pkt_size) {
916 pkt_dev->min_pkt_size = value;
917 pkt_dev->cur_pkt_size = value;
918 }
919 sprintf(pg_result, "OK: min_pkt_size=%u",
920 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return count;
922 }
923
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000926 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800927 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 if (value < 14 + 20 + 8)
931 value = 14 + 20 + 8;
932 if (value != pkt_dev->max_pkt_size) {
933 pkt_dev->max_pkt_size = value;
934 pkt_dev->cur_pkt_size = value;
935 }
936 sprintf(pg_result, "OK: max_pkt_size=%u",
937 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return count;
939 }
940
Luiz Capitulino222f1802006-03-20 22:16:13 -0800941 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (!strcmp(name, "pkt_size")) {
944 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000945 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800946 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800949 if (value < 14 + 20 + 8)
950 value = 14 + 20 + 8;
951 if (value != pkt_dev->min_pkt_size) {
952 pkt_dev->min_pkt_size = value;
953 pkt_dev->max_pkt_size = value;
954 pkt_dev->cur_pkt_size = value;
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
957 return count;
958 }
959
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000962 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800963 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800966 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 sprintf(pg_result, "OK: debug=%u", debug);
968 return count;
969 }
970
Luiz Capitulino222f1802006-03-20 22:16:13 -0800971 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000973 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800974 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 i += len;
977 pkt_dev->nfrags = value;
978 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
979 return count;
980 }
981 if (!strcmp(name, "delay")) {
982 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000983 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800984 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000987 if (value == 0x7FFFFFFF)
988 pkt_dev->delay = ULLONG_MAX;
989 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000990 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000991
992 sprintf(pg_result, "OK: delay=%llu",
993 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return count;
995 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000996 if (!strcmp(name, "rate")) {
997 len = num_arg(&user_buffer[i], 10, &value);
998 if (len < 0)
999 return len;
1000
1001 i += len;
1002 if (!value)
1003 return len;
1004 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1005 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001006 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001007
1008 sprintf(pg_result, "OK: rate=%lu", value);
1009 return count;
1010 }
1011 if (!strcmp(name, "ratep")) {
1012 len = num_arg(&user_buffer[i], 10, &value);
1013 if (len < 0)
1014 return len;
1015
1016 i += len;
1017 if (!value)
1018 return len;
1019 pkt_dev->delay = NSEC_PER_SEC/value;
1020 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001021 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001022
1023 sprintf(pg_result, "OK: rate=%lu", value);
1024 return count;
1025 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001026 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001028 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001032 if (value != pkt_dev->udp_src_min) {
1033 pkt_dev->udp_src_min = value;
1034 pkt_dev->cur_udp_src = value;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1037 return count;
1038 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001041 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001042 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 if (value != pkt_dev->udp_dst_min) {
1046 pkt_dev->udp_dst_min = value;
1047 pkt_dev->cur_udp_dst = value;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1050 return count;
1051 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001054 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001055 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001058 if (value != pkt_dev->udp_src_max) {
1059 pkt_dev->udp_src_max = value;
1060 pkt_dev->cur_udp_src = value;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1063 return count;
1064 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001065 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001067 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001068 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001071 if (value != pkt_dev->udp_dst_max) {
1072 pkt_dev->udp_dst_max = value;
1073 pkt_dev->cur_udp_dst = value;
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1076 return count;
1077 }
1078 if (!strcmp(name, "clone_skb")) {
1079 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001080 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001081 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001084 pkt_dev->clone_skb = value;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1087 return count;
1088 }
1089 if (!strcmp(name, "count")) {
1090 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001091 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001092 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 i += len;
1095 pkt_dev->count = value;
1096 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001097 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return count;
1099 }
1100 if (!strcmp(name, "src_mac_count")) {
1101 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001102 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001103 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 i += len;
1106 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001107 pkt_dev->src_mac_count = value;
1108 pkt_dev->cur_src_mac_offset = 0;
1109 }
1110 sprintf(pg_result, "OK: src_mac_count=%d",
1111 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return count;
1113 }
1114 if (!strcmp(name, "dst_mac_count")) {
1115 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001116 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001117 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 i += len;
1120 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001121 pkt_dev->dst_mac_count = value;
1122 pkt_dev->cur_dst_mac_offset = 0;
1123 }
1124 sprintf(pg_result, "OK: dst_mac_count=%d",
1125 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return count;
1127 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001128 if (!strcmp(name, "node")) {
1129 len = num_arg(&user_buffer[i], 10, &value);
1130 if (len < 0)
1131 return len;
1132
1133 i += len;
1134
1135 if (node_possible(value)) {
1136 pkt_dev->node = value;
1137 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001138 if (pkt_dev->page) {
1139 put_page(pkt_dev->page);
1140 pkt_dev->page = NULL;
1141 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001142 }
1143 else
1144 sprintf(pg_result, "ERROR: node not possible");
1145 return count;
1146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001148 char f[32];
1149 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001151 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001152 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (copy_from_user(f, &user_buffer[i], len))
1155 return -EFAULT;
1156 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001157 if (strcmp(f, "IPSRC_RND") == 0)
1158 pkt_dev->flags |= F_IPSRC_RND;
1159
1160 else if (strcmp(f, "!IPSRC_RND") == 0)
1161 pkt_dev->flags &= ~F_IPSRC_RND;
1162
1163 else if (strcmp(f, "TXSIZE_RND") == 0)
1164 pkt_dev->flags |= F_TXSIZE_RND;
1165
1166 else if (strcmp(f, "!TXSIZE_RND") == 0)
1167 pkt_dev->flags &= ~F_TXSIZE_RND;
1168
1169 else if (strcmp(f, "IPDST_RND") == 0)
1170 pkt_dev->flags |= F_IPDST_RND;
1171
1172 else if (strcmp(f, "!IPDST_RND") == 0)
1173 pkt_dev->flags &= ~F_IPDST_RND;
1174
1175 else if (strcmp(f, "UDPSRC_RND") == 0)
1176 pkt_dev->flags |= F_UDPSRC_RND;
1177
1178 else if (strcmp(f, "!UDPSRC_RND") == 0)
1179 pkt_dev->flags &= ~F_UDPSRC_RND;
1180
1181 else if (strcmp(f, "UDPDST_RND") == 0)
1182 pkt_dev->flags |= F_UDPDST_RND;
1183
1184 else if (strcmp(f, "!UDPDST_RND") == 0)
1185 pkt_dev->flags &= ~F_UDPDST_RND;
1186
1187 else if (strcmp(f, "MACSRC_RND") == 0)
1188 pkt_dev->flags |= F_MACSRC_RND;
1189
1190 else if (strcmp(f, "!MACSRC_RND") == 0)
1191 pkt_dev->flags &= ~F_MACSRC_RND;
1192
1193 else if (strcmp(f, "MACDST_RND") == 0)
1194 pkt_dev->flags |= F_MACDST_RND;
1195
1196 else if (strcmp(f, "!MACDST_RND") == 0)
1197 pkt_dev->flags &= ~F_MACDST_RND;
1198
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001199 else if (strcmp(f, "MPLS_RND") == 0)
1200 pkt_dev->flags |= F_MPLS_RND;
1201
1202 else if (strcmp(f, "!MPLS_RND") == 0)
1203 pkt_dev->flags &= ~F_MPLS_RND;
1204
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001205 else if (strcmp(f, "VID_RND") == 0)
1206 pkt_dev->flags |= F_VID_RND;
1207
1208 else if (strcmp(f, "!VID_RND") == 0)
1209 pkt_dev->flags &= ~F_VID_RND;
1210
1211 else if (strcmp(f, "SVID_RND") == 0)
1212 pkt_dev->flags |= F_SVID_RND;
1213
1214 else if (strcmp(f, "!SVID_RND") == 0)
1215 pkt_dev->flags &= ~F_SVID_RND;
1216
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001217 else if (strcmp(f, "FLOW_SEQ") == 0)
1218 pkt_dev->flags |= F_FLOW_SEQ;
1219
Robert Olsson45b270f2007-08-28 15:45:55 -07001220 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1221 pkt_dev->flags |= F_QUEUE_MAP_RND;
1222
1223 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1224 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001225
1226 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1227 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1228
1229 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1230 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001231#ifdef CONFIG_XFRM
1232 else if (strcmp(f, "IPSEC") == 0)
1233 pkt_dev->flags |= F_IPSEC_ON;
1234#endif
1235
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001236 else if (strcmp(f, "!IPV6") == 0)
1237 pkt_dev->flags &= ~F_IPV6;
1238
Robert Olssone99b99b2010-03-18 22:44:30 +00001239 else if (strcmp(f, "NODE_ALLOC") == 0)
1240 pkt_dev->flags |= F_NODE;
1241
1242 else if (strcmp(f, "!NODE_ALLOC") == 0)
1243 pkt_dev->flags &= ~F_NODE;
1244
Luiz Capitulino222f1802006-03-20 22:16:13 -08001245 else {
1246 sprintf(pg_result,
1247 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1248 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001249 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001250 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001251 return count;
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1254 return count;
1255 }
1256 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1257 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001258 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Luiz Capitulino222f1802006-03-20 22:16:13 -08001261 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001263 buf[len] = 0;
1264 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1265 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1266 strncpy(pkt_dev->dst_min, buf, len);
1267 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1268 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1269 }
1270 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001271 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001272 pkt_dev->dst_min);
1273 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1275 return count;
1276 }
1277 if (!strcmp(name, "dst_max")) {
1278 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001279 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001280 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Luiz Capitulino222f1802006-03-20 22:16:13 -08001283 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EFAULT;
1285
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286 buf[len] = 0;
1287 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1288 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1289 strncpy(pkt_dev->dst_max, buf, len);
1290 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1291 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1292 }
1293 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001294 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001295 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 i += len;
1297 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1298 return count;
1299 }
1300 if (!strcmp(name, "dst6")) {
1301 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001302 if (len < 0)
1303 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 pkt_dev->flags |= F_IPV6;
1306
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001309 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001312 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1315
Luiz Capitulino222f1802006-03-20 22:16:13 -08001316 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001317 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sprintf(pg_result, "OK: dst6=%s", buf);
1321 return count;
1322 }
1323 if (!strcmp(name, "dst6_min")) {
1324 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001325 if (len < 0)
1326 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 pkt_dev->flags |= F_IPV6;
1329
Luiz Capitulino222f1802006-03-20 22:16:13 -08001330 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1338 &pkt_dev->min_in6_daddr);
1339 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001340 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Luiz Capitulino222f1802006-03-20 22:16:13 -08001342 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 sprintf(pg_result, "OK: dst6_min=%s", buf);
1344 return count;
1345 }
1346 if (!strcmp(name, "dst6_max")) {
1347 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001348 if (len < 0)
1349 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 pkt_dev->flags |= F_IPV6;
1352
Luiz Capitulino222f1802006-03-20 22:16:13 -08001353 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001355 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001358 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Luiz Capitulino222f1802006-03-20 22:16:13 -08001360 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001361 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Luiz Capitulino222f1802006-03-20 22:16:13 -08001363 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 sprintf(pg_result, "OK: dst6_max=%s", buf);
1365 return count;
1366 }
1367 if (!strcmp(name, "src6")) {
1368 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001369 if (len < 0)
1370 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 pkt_dev->flags |= F_IPV6;
1373
Luiz Capitulino222f1802006-03-20 22:16:13 -08001374 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001376 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001379 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1382
Luiz Capitulino222f1802006-03-20 22:16:13 -08001383 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001384 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001385
1386 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 sprintf(pg_result, "OK: src6=%s", buf);
1388 return count;
1389 }
1390 if (!strcmp(name, "src_min")) {
1391 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001392 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001393 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001394
Luiz Capitulino222f1802006-03-20 22:16:13 -08001395 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001397 buf[len] = 0;
1398 if (strcmp(buf, pkt_dev->src_min) != 0) {
1399 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1400 strncpy(pkt_dev->src_min, buf, len);
1401 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1402 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1403 }
1404 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001405 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001406 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 i += len;
1408 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1409 return count;
1410 }
1411 if (!strcmp(name, "src_max")) {
1412 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001413 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001414 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001415
Luiz Capitulino222f1802006-03-20 22:16:13 -08001416 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001418 buf[len] = 0;
1419 if (strcmp(buf, pkt_dev->src_max) != 0) {
1420 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1421 strncpy(pkt_dev->src_max, buf, len);
1422 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1423 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1424 }
1425 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001426 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 i += len;
1429 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1430 return count;
1431 }
1432 if (!strcmp(name, "dst_mac")) {
1433 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001434 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001436 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001439 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001440 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001443 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -EFAULT;
1445 i += len;
1446
Luiz Capitulino222f1802006-03-20 22:16:13 -08001447 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Andy Shevchenko451e07a2010-07-23 03:18:10 +00001448 int value;
1449
1450 value = hex_to_bin(*v);
1451 if (value >= 0)
1452 *m = *m * 16 + value;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (*v == ':') {
1455 m++;
1456 *m = 0;
1457 }
1458 }
1459
1460 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001461 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1462 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 sprintf(pg_result, "OK: dstmac");
1465 return count;
1466 }
1467 if (!strcmp(name, "src_mac")) {
1468 char *v = valstr;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001469 unsigned char old_smac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 unsigned char *m = pkt_dev->src_mac;
1471
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001472 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001475 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001476 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001479 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -EFAULT;
1481 i += len;
1482
Luiz Capitulino222f1802006-03-20 22:16:13 -08001483 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Andy Shevchenko451e07a2010-07-23 03:18:10 +00001484 int value;
1485
1486 value = hex_to_bin(*v);
1487 if (value >= 0)
1488 *m = *m * 16 + value;
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (*v == ':') {
1491 m++;
1492 *m = 0;
1493 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001496 /* Set up Src MAC */
1497 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1498 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1499
Luiz Capitulino222f1802006-03-20 22:16:13 -08001500 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return count;
1502 }
1503
Luiz Capitulino222f1802006-03-20 22:16:13 -08001504 if (!strcmp(name, "clear_counters")) {
1505 pktgen_clear_counters(pkt_dev);
1506 sprintf(pg_result, "OK: Clearing counters.\n");
1507 return count;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 if (!strcmp(name, "flows")) {
1511 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001512 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001513 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 i += len;
1516 if (value > MAX_CFLOWS)
1517 value = MAX_CFLOWS;
1518
1519 pkt_dev->cflows = value;
1520 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1521 return count;
1522 }
1523
1524 if (!strcmp(name, "flowlen")) {
1525 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001526 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001527 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 i += len;
1530 pkt_dev->lflow = value;
1531 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1532 return count;
1533 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001534
Robert Olsson45b270f2007-08-28 15:45:55 -07001535 if (!strcmp(name, "queue_map_min")) {
1536 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001537 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001538 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001539
Robert Olsson45b270f2007-08-28 15:45:55 -07001540 i += len;
1541 pkt_dev->queue_map_min = value;
1542 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1543 return count;
1544 }
1545
1546 if (!strcmp(name, "queue_map_max")) {
1547 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001548 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001549 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001550
Robert Olsson45b270f2007-08-28 15:45:55 -07001551 i += len;
1552 pkt_dev->queue_map_max = value;
1553 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1554 return count;
1555 }
1556
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001557 if (!strcmp(name, "mpls")) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001558 unsigned n, cnt;
1559
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001560 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001561 if (len < 0)
1562 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001563 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001564 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001565 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001566 cnt += sprintf(pg_result + cnt,
1567 "%08x%s", ntohl(pkt_dev->labels[n]),
1568 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001569
1570 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1571 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1572 pkt_dev->svlan_id = 0xffff;
1573
1574 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001575 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001576 }
1577 return count;
1578 }
1579
1580 if (!strcmp(name, "vlan_id")) {
1581 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001582 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001583 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001584
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001585 i += len;
1586 if (value <= 4095) {
1587 pkt_dev->vlan_id = value; /* turn on VLAN */
1588
1589 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001590 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001591
1592 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001593 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001594
1595 pkt_dev->nr_labels = 0; /* turn off MPLS */
1596 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1597 } else {
1598 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1599 pkt_dev->svlan_id = 0xffff;
1600
1601 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001602 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001603 }
1604 return count;
1605 }
1606
1607 if (!strcmp(name, "vlan_p")) {
1608 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001609 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001610 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001611
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001612 i += len;
1613 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1614 pkt_dev->vlan_p = value;
1615 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1616 } else {
1617 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1618 }
1619 return count;
1620 }
1621
1622 if (!strcmp(name, "vlan_cfi")) {
1623 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001624 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001625 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001626
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001627 i += len;
1628 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1629 pkt_dev->vlan_cfi = value;
1630 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1631 } else {
1632 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1633 }
1634 return count;
1635 }
1636
1637 if (!strcmp(name, "svlan_id")) {
1638 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001639 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001640 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001641
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001642 i += len;
1643 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1644 pkt_dev->svlan_id = value; /* turn on SVLAN */
1645
1646 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001647 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001648
1649 if (debug && pkt_dev->nr_labels)
David S. Miller25a8b252007-07-30 16:11:48 -07001650 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001651
1652 pkt_dev->nr_labels = 0; /* turn off MPLS */
1653 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1654 } else {
1655 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1656 pkt_dev->svlan_id = 0xffff;
1657
1658 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001659 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001660 }
1661 return count;
1662 }
1663
1664 if (!strcmp(name, "svlan_p")) {
1665 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001666 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001667 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001668
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001669 i += len;
1670 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1671 pkt_dev->svlan_p = value;
1672 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1673 } else {
1674 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1675 }
1676 return count;
1677 }
1678
1679 if (!strcmp(name, "svlan_cfi")) {
1680 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001681 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001682 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001683
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001684 i += len;
1685 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1686 pkt_dev->svlan_cfi = value;
1687 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1688 } else {
1689 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1690 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001691 return count;
1692 }
1693
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001694 if (!strcmp(name, "tos")) {
1695 __u32 tmp_value = 0;
1696 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001697 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001698 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001699
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001700 i += len;
1701 if (len == 2) {
1702 pkt_dev->tos = tmp_value;
1703 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1704 } else {
1705 sprintf(pg_result, "ERROR: tos must be 00-ff");
1706 }
1707 return count;
1708 }
1709
1710 if (!strcmp(name, "traffic_class")) {
1711 __u32 tmp_value = 0;
1712 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001713 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001714 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001715
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001716 i += len;
1717 if (len == 2) {
1718 pkt_dev->traffic_class = tmp_value;
1719 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1720 } else {
1721 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1722 }
1723 return count;
1724 }
1725
John Fastabend9e50e3a2010-11-16 19:12:28 +00001726 if (!strcmp(name, "skb_priority")) {
1727 len = num_arg(&user_buffer[i], 9, &value);
1728 if (len < 0)
1729 return len;
1730
1731 i += len;
1732 pkt_dev->skb_priority = value;
1733 sprintf(pg_result, "OK: skb_priority=%i",
1734 pkt_dev->skb_priority);
1735 return count;
1736 }
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1739 return -EINVAL;
1740}
1741
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001742static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001744 return single_open(file, pktgen_if_show, PDE(inode)->data);
1745}
1746
Arjan van de Ven9a321442007-02-12 00:55:35 -08001747static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001748 .owner = THIS_MODULE,
1749 .open = pktgen_if_open,
1750 .read = seq_read,
1751 .llseek = seq_lseek,
1752 .write = pktgen_if_write,
1753 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001754};
1755
1756static int pktgen_thread_show(struct seq_file *seq, void *v)
1757{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001758 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001759 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001761 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Luiz Capitulino222f1802006-03-20 22:16:13 -08001763 seq_printf(seq, "Running: ");
1764
1765 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001766 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001767 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001768 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Luiz Capitulino222f1802006-03-20 22:16:13 -08001770 seq_printf(seq, "\nStopped: ");
1771
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001772 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001774 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001777 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001779 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001786static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001787 const char __user * user_buffer,
1788 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Joe Perches8a994a72010-07-12 10:50:23 +00001790 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001792 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001794 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001797 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return -EINVAL;
1799 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001800
Paul Gortmakerd6182222010-10-18 12:14:44 +00001801 max = count;
1802 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001804 return len;
1805
Paul Gortmakerd6182222010-10-18 12:14:44 +00001806 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /* Read variable name */
1809
1810 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001811 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001812 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 memset(name, 0, sizeof(name));
1815 if (copy_from_user(name, &user_buffer[i], len))
1816 return -EFAULT;
1817 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001818
Luiz Capitulino222f1802006-03-20 22:16:13 -08001819 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001821 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001822 return len;
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 i += len;
1825
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001826 if (debug)
David S. Miller25a8b252007-07-30 16:11:48 -07001827 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1828 name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Luiz Capitulino222f1802006-03-20 22:16:13 -08001830 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001831 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 ret = -EINVAL;
1833 goto out;
1834 }
1835
1836 pg_result = &(t->result[0]);
1837
Luiz Capitulino222f1802006-03-20 22:16:13 -08001838 if (!strcmp(name, "add_device")) {
1839 char f[32];
1840 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001842 if (len < 0) {
1843 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto out;
1845 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return -EFAULT;
1848 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001849 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001850 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001851 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001852 ret = count;
1853 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto out;
1855 }
1856
Luiz Capitulino222f1802006-03-20 22:16:13 -08001857 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001858 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001859 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001860 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001861 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001863 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 goto out;
1865 }
1866
Luiz Capitulino222f1802006-03-20 22:16:13 -08001867 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001868 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001869 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto out;
1871 }
1872
1873 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001874out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return ret;
1876}
1877
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001878static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001880 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Arjan van de Ven9a321442007-02-12 00:55:35 -08001883static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001884 .owner = THIS_MODULE,
1885 .open = pktgen_thread_open,
1886 .read = seq_read,
1887 .llseek = seq_lseek,
1888 .write = pktgen_thread_write,
1889 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001890};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001893static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct pktgen_thread *t;
1896 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001897 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001899 list_for_each_entry(t, &pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001900 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001902 if (remove) {
1903 if_lock(t);
1904 pkt_dev->removal_mark = 1;
1905 t->control |= T_REMDEV;
1906 if_unlock(t);
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 break;
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001911 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001914/*
1915 * mark a device for removal
1916 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001917static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001920 const int max_tries = 10, msec_per_try = 125;
1921 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001922
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001923 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001924 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001925
Luiz Capitulino222f1802006-03-20 22:16:13 -08001926 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001927
1928 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001929 if (pkt_dev == NULL)
1930 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001931
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001932 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001933 pr_debug("%s: waiting for %s to disappear....\n",
1934 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001935 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001936 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001937
1938 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001939 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1940 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001941 break;
1942 }
1943
1944 }
1945
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001946 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001947}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001948
Stephen Hemminger39df2322007-03-04 16:11:51 -08001949static void pktgen_change_name(struct net_device *dev)
1950{
1951 struct pktgen_thread *t;
1952
1953 list_for_each_entry(t, &pktgen_threads, th_list) {
1954 struct pktgen_dev *pkt_dev;
1955
1956 list_for_each_entry(pkt_dev, &t->if_list, list) {
1957 if (pkt_dev->odev != dev)
1958 continue;
1959
1960 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1961
Alexey Dobriyan29753152009-08-28 23:34:43 -07001962 pkt_dev->entry = proc_create_data(dev->name, 0600,
1963 pg_proc_dir,
1964 &pktgen_if_fops,
1965 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001966 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001967 pr_err("can't move proc entry for '%s'\n",
1968 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001969 break;
1970 }
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Luiz Capitulino222f1802006-03-20 22:16:13 -08001974static int pktgen_device_event(struct notifier_block *unused,
1975 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001977 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001979 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001980 return NOTIFY_DONE;
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /* It is OK that we do not hold the group lock right now,
1983 * as we run under the RTNL lock.
1984 */
1985
1986 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001987 case NETDEV_CHANGENAME:
1988 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001992 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 return NOTIFY_DONE;
1997}
1998
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001999static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
2000 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002001{
2002 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00002003 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07002004
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002005 for (i = 0; ifname[i] != '@'; i++) {
2006 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07002007 break;
2008
2009 b[i] = ifname[i];
2010 }
2011 b[i] = 0;
2012
2013 return dev_get_by_name(&init_net, b);
2014}
2015
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017/* Associate pktgen_dev with a device. */
2018
Stephen Hemminger39df2322007-03-04 16:11:51 -08002019static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002022 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (pkt_dev->odev) {
2026 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002027 pkt_dev->odev = NULL;
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Robert Olssone6fce5b2008-08-07 02:23:01 -07002030 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002032 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002033 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002037 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002038 err = -EINVAL;
2039 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002040 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002041 err = -ENETDOWN;
2042 } else {
2043 pkt_dev->odev = odev;
2044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002048 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
2051/* Read pkt_dev from the interface and set up internal pktgen_dev
2052 * structure to have the right information to create/send packets
2053 */
2054static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2055{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002056 int ntxq;
2057
Luiz Capitulino222f1802006-03-20 22:16:13 -08002058 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002059 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002060 sprintf(pkt_dev->result,
2061 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2062 return;
2063 }
2064
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002065 /* make sure that we don't pick a non-existing transmit queue */
2066 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002067
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002068 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002069 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2070 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2071 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002072 pkt_dev->queue_map_min = ntxq - 1;
2073 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002074 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002075 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2076 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2077 pkt_dev->odevname);
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002078 pkt_dev->queue_map_max = ntxq - 1;
2079 }
2080
Luiz Capitulino222f1802006-03-20 22:16:13 -08002081 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002083 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002084 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Luiz Capitulino222f1802006-03-20 22:16:13 -08002086 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002087 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Luiz Capitulino222f1802006-03-20 22:16:13 -08002089 /* Set up pkt size */
2090 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2091
2092 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002094 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 * gets exported
2096 */
2097
2098#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08002099 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct inet6_dev *idev;
2101
Luiz Capitulino222f1802006-03-20 22:16:13 -08002102 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2103 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 set = 1;
2105 break;
2106 }
2107
Luiz Capitulino222f1802006-03-20 22:16:13 -08002108 if (!set) {
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /*
2111 * Use linklevel address if unconfigured.
2112 *
2113 * use ipv6_get_lladdr if/when it's get exported
2114 */
2115
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002116 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002117 idev = __in6_dev_get(pkt_dev->odev);
2118 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 struct inet6_ifaddr *ifp;
2120
2121 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002122 for (ifp = idev->addr_list; ifp;
2123 ifp = ifp->if_next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002124 if (ifp->scope == IFA_LINK &&
2125 !(ifp->flags & IFA_F_TENTATIVE)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002126 ipv6_addr_copy(&pkt_dev->
2127 cur_in6_saddr,
2128 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 err = 0;
2130 break;
2131 }
2132 }
2133 read_unlock_bh(&idev->lock);
2134 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002135 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002136 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002137 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002140 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 pkt_dev->saddr_min = 0;
2142 pkt_dev->saddr_max = 0;
2143 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002144
2145 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002148 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (in_dev) {
2150 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002151 pkt_dev->saddr_min =
2152 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 pkt_dev->saddr_max = pkt_dev->saddr_min;
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002157 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2159 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2160 }
2161
2162 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2163 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2164 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002165 /* Initialize current values. */
2166 pkt_dev->cur_dst_mac_offset = 0;
2167 pkt_dev->cur_src_mac_offset = 0;
2168 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2169 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2170 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2171 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 pkt_dev->nflows = 0;
2173}
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002176static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2177{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002178 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002179 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002180 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002181
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002182 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2183 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002184
Daniel Turull43d28b62010-06-09 22:49:57 +00002185 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002186 if (remaining <= 0) {
2187 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002188 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002189 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002190
Stephen Hemmingeref879792009-09-22 19:41:43 +00002191 start_time = ktime_now();
Daniel Turull43d28b62010-06-09 22:49:57 +00002192 if (remaining < 100000)
2193 ndelay(remaining); /* really small just spin */
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002194 else {
2195 /* see do_nanosleep */
2196 hrtimer_init_sleeper(&t, current);
2197 do {
2198 set_current_state(TASK_INTERRUPTIBLE);
2199 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2200 if (!hrtimer_active(&t.timer))
2201 t.task = NULL;
2202
2203 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002206 hrtimer_cancel(&t.timer);
2207 } while (t.task && pkt_dev->running && !signal_pending(current));
2208 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002210 end_time = ktime_now();
2211
2212 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002213 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002216static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2217{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002218 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002219 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2220 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2221 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2222}
2223
Stephen Hemminger648fda72009-08-27 13:55:07 +00002224static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002225{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002226 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002227}
2228
2229static inline int f_pick(struct pktgen_dev *pkt_dev)
2230{
2231 int flow = pkt_dev->curfl;
2232
2233 if (pkt_dev->flags & F_FLOW_SEQ) {
2234 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2235 /* reset time */
2236 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002237 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002238 pkt_dev->curfl += 1;
2239 if (pkt_dev->curfl >= pkt_dev->cflows)
2240 pkt_dev->curfl = 0; /*reset */
2241 }
2242 } else {
2243 flow = random32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002244 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002245
Robert Olsson1211a642008-08-05 18:44:26 -07002246 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002247 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002248 pkt_dev->flows[flow].flags = 0;
2249 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002250 }
2251
2252 return pkt_dev->curfl;
2253}
2254
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002255
2256#ifdef CONFIG_XFRM
2257/* If there was already an IPSEC SA, we keep it as is, else
2258 * we go look for it ...
2259*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002260#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002261static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002262{
2263 struct xfrm_state *x = pkt_dev->flows[flow].x;
2264 if (!x) {
2265 /*slow path: we dont already have xfrm_state*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002266 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002267 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002268 (xfrm_address_t *)&pkt_dev->cur_saddr,
2269 AF_INET,
2270 pkt_dev->ipsmode,
2271 pkt_dev->ipsproto, 0);
2272 if (x) {
2273 pkt_dev->flows[flow].x = x;
2274 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002275 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002276 }
2277
2278 }
2279}
2280#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002281static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2282{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002283
2284 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2285 pkt_dev->cur_queue_map = smp_processor_id();
2286
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002287 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002288 __u16 t;
2289 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2290 t = random32() %
2291 (pkt_dev->queue_map_max -
2292 pkt_dev->queue_map_min + 1)
2293 + pkt_dev->queue_map_min;
2294 } else {
2295 t = pkt_dev->cur_queue_map + 1;
2296 if (t > pkt_dev->queue_map_max)
2297 t = pkt_dev->queue_map_min;
2298 }
2299 pkt_dev->cur_queue_map = t;
2300 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002301 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 -07002302}
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304/* Increment/randomize headers according to flags and current values
2305 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2306 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2308{
2309 __u32 imn;
2310 __u32 imx;
2311 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002313 if (pkt_dev->cflows)
2314 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002317 if (pkt_dev->src_mac_count > 1) {
2318 __u32 mc;
2319 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Luiz Capitulino222f1802006-03-20 22:16:13 -08002321 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002322 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 else {
2324 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002325 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002326 pkt_dev->src_mac_count)
2327 pkt_dev->cur_src_mac_offset = 0;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Luiz Capitulino222f1802006-03-20 22:16:13 -08002330 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2331 pkt_dev->hh[11] = tmp;
2332 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2333 pkt_dev->hh[10] = tmp;
2334 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2335 pkt_dev->hh[9] = tmp;
2336 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2337 pkt_dev->hh[8] = tmp;
2338 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2339 pkt_dev->hh[7] = tmp;
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Luiz Capitulino222f1802006-03-20 22:16:13 -08002342 /* Deal with Destination MAC */
2343 if (pkt_dev->dst_mac_count > 1) {
2344 __u32 mc;
2345 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Luiz Capitulino222f1802006-03-20 22:16:13 -08002347 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002348 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Luiz Capitulino222f1802006-03-20 22:16:13 -08002350 else {
2351 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002352 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002353 pkt_dev->dst_mac_count) {
2354 pkt_dev->cur_dst_mac_offset = 0;
2355 }
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Luiz Capitulino222f1802006-03-20 22:16:13 -08002358 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2359 pkt_dev->hh[5] = tmp;
2360 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2361 pkt_dev->hh[4] = tmp;
2362 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2363 pkt_dev->hh[3] = tmp;
2364 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2365 pkt_dev->hh[2] = tmp;
2366 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2367 pkt_dev->hh[1] = tmp;
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002370 if (pkt_dev->flags & F_MPLS_RND) {
2371 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002372 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002373 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2374 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002375 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002376 htonl(0x000fffff));
2377 }
2378
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002379 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002380 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002381 }
2382
2383 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002384 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002385 }
2386
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2388 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002389 pkt_dev->cur_udp_src = random32() %
2390 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2391 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Luiz Capitulino222f1802006-03-20 22:16:13 -08002393 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 pkt_dev->cur_udp_src++;
2395 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2396 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002397 }
2398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2401 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002402 pkt_dev->cur_udp_dst = random32() %
2403 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2404 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002405 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002407 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002409 }
2410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 if (!(pkt_dev->flags & F_IPV6)) {
2413
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002414 imn = ntohl(pkt_dev->saddr_min);
2415 imx = ntohl(pkt_dev->saddr_max);
2416 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002418 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002419 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 else {
2421 t = ntohl(pkt_dev->cur_saddr);
2422 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002423 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
2427 pkt_dev->cur_saddr = htonl(t);
2428 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002429
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002430 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2432 } else {
Al Viro252e33462006-11-14 20:48:11 -08002433 imn = ntohl(pkt_dev->daddr_min);
2434 imx = ntohl(pkt_dev->daddr_max);
2435 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002437 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (pkt_dev->flags & F_IPDST_RND) {
2439
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002440 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002441 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Joe Perches21cf2252007-12-16 13:44:00 -08002443 while (ipv4_is_loopback(s) ||
2444 ipv4_is_multicast(s) ||
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002445 ipv4_is_lbcast(s) ||
Joe Perches21cf2252007-12-16 13:44:00 -08002446 ipv4_is_zeronet(s) ||
2447 ipv4_is_local_multicast(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002448 t = random32() % (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002449 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
Al Viro252e33462006-11-14 20:48:11 -08002451 pkt_dev->cur_daddr = s;
2452 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 t = ntohl(pkt_dev->cur_daddr);
2454 t++;
2455 if (t > imx) {
2456 t = imn;
2457 }
2458 pkt_dev->cur_daddr = htonl(t);
2459 }
2460 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002461 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002462 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002463 pkt_dev->flows[flow].cur_daddr =
2464 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002465#ifdef CONFIG_XFRM
2466 if (pkt_dev->flags & F_IPSEC_ON)
2467 get_ipsec_sa(pkt_dev, flow);
2468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 pkt_dev->nflows++;
2470 }
2471 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002472 } else { /* IPV6 * */
2473
2474 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2475 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2476 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2477 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 else {
2479 int i;
2480
2481 /* Only random destinations yet */
2482
Luiz Capitulino222f1802006-03-20 22:16:13 -08002483 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002485 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002486 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2487 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491
Luiz Capitulino222f1802006-03-20 22:16:13 -08002492 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2493 __u32 t;
2494 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002495 t = random32() %
2496 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2497 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002498 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002500 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002502 }
2503 pkt_dev->cur_pkt_size = t;
2504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002506 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 pkt_dev->flows[flow].count++;
2509}
2510
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002511
2512#ifdef CONFIG_XFRM
2513static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2514{
2515 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2516 int err = 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002517
2518 if (!x)
2519 return 0;
2520 /* XXX: we dont support tunnel mode for now until
2521 * we resolve the dst issue */
2522 if (x->props.mode != XFRM_MODE_TRANSPORT)
2523 return 0;
2524
2525 spin_lock(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002526
Herbert Xu13996372007-10-17 21:35:51 -07002527 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002528 if (err)
2529 goto error;
2530 err = x->type->output(x, skb);
2531 if (err)
2532 goto error;
2533
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002534 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002535 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002536error:
2537 spin_unlock(&x->lock);
2538 return err;
2539}
2540
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002541static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002542{
2543 if (pkt_dev->cflows) {
2544 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002545 int i;
2546 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002547 struct xfrm_state *x = pkt_dev->flows[i].x;
2548 if (x) {
2549 xfrm_state_put(x);
2550 pkt_dev->flows[i].x = NULL;
2551 }
2552 }
2553 }
2554}
2555
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002556static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002557 struct sk_buff *skb, __be16 protocol)
2558{
2559 if (pkt_dev->flags & F_IPSEC_ON) {
2560 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2561 int nhead = 0;
2562 if (x) {
2563 int ret;
2564 __u8 *eth;
2565 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002566 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002567 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2568 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002569 pr_err("Error expanding ipsec packet %d\n",
2570 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002571 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002572 }
2573 }
2574
2575 /* ipsec is not expecting ll header */
2576 skb_pull(skb, ETH_HLEN);
2577 ret = pktgen_output_ipsec(skb, pkt_dev);
2578 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002579 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002580 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002581 }
2582 /* restore ll */
2583 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2584 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002585 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002586 }
2587 }
2588 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002589err:
2590 kfree_skb(skb);
2591 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002592}
2593#endif
2594
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002595static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2596{
2597 unsigned i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002598 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002599 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002600
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002601 mpls--;
2602 *mpls |= MPLS_STACK_BOTTOM;
2603}
2604
Al Viro0f37c602006-11-03 03:49:56 -08002605static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2606 unsigned int prio)
2607{
2608 return htons(id | (cfi << 12) | (prio << 13));
2609}
2610
Eric Dumazet26ad7872011-01-25 13:26:05 -08002611static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2612 int datalen)
2613{
2614 struct timeval timestamp;
2615 struct pktgen_hdr *pgh;
2616
2617 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2618 datalen -= sizeof(*pgh);
2619
2620 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002621 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002622 } else {
2623 int frags = pkt_dev->nfrags;
2624 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002625 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002626
2627
2628 if (frags > MAX_SKB_FRAGS)
2629 frags = MAX_SKB_FRAGS;
2630 len = datalen - frags * PAGE_SIZE;
2631 if (len > 0) {
2632 memset(skb_put(skb, len), 0, len);
2633 datalen = frags * PAGE_SIZE;
2634 }
2635
2636 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002637 frag_len = (datalen/frags) < PAGE_SIZE ?
2638 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002639 while (datalen > 0) {
2640 if (unlikely(!pkt_dev->page)) {
2641 int node = numa_node_id();
2642
2643 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2644 node = pkt_dev->node;
2645 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2646 if (!pkt_dev->page)
2647 break;
2648 }
2649 skb_shinfo(skb)->frags[i].page = pkt_dev->page;
2650 get_page(pkt_dev->page);
2651 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002652 /*last fragment, fill rest of data*/
2653 if (i == (frags - 1))
2654 skb_shinfo(skb)->frags[i].size =
2655 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2656 else
2657 skb_shinfo(skb)->frags[i].size = frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002658 datalen -= skb_shinfo(skb)->frags[i].size;
2659 skb->len += skb_shinfo(skb)->frags[i].size;
2660 skb->data_len += skb_shinfo(skb)->frags[i].size;
2661 i++;
2662 skb_shinfo(skb)->nr_frags = i;
2663 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002664 }
2665
2666 /* Stamp the time, and sequence number,
2667 * convert them to network byte order
2668 */
2669 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2670 pgh->seq_num = htonl(pkt_dev->seq_num);
2671
2672 do_gettimeofday(&timestamp);
2673 pgh->tv_sec = htonl(timestamp.tv_sec);
2674 pgh->tv_usec = htonl(timestamp.tv_usec);
2675}
2676
Luiz Capitulino222f1802006-03-20 22:16:13 -08002677static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2678 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 struct sk_buff *skb = NULL;
2681 __u8 *eth;
2682 struct udphdr *udph;
2683 int datalen, iplen;
2684 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002685 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002686 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002687 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2688 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2689 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2690 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002691 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002692
2693 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002694 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002695
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002696 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002697 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002698
Robert Olsson64053be2005-06-26 15:27:10 -07002699 /* Update any of the values, used when we're incrementing various
2700 * fields.
2701 */
2702 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002703 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002704
David S. Miller7ac54592006-01-18 14:19:10 -08002705 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002706
2707 if (pkt_dev->flags & F_NODE) {
2708 int node;
2709
2710 if (pkt_dev->node >= 0)
2711 node = pkt_dev->node;
2712 else
2713 node = numa_node_id();
2714
2715 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2716 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2717 if (likely(skb)) {
2718 skb_reserve(skb, NET_SKB_PAD);
2719 skb->dev = odev;
2720 }
2721 }
2722 else
2723 skb = __netdev_alloc_skb(odev,
2724 pkt_dev->cur_pkt_size + 64
2725 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 if (!skb) {
2728 sprintf(pkt_dev->result, "No memory");
2729 return NULL;
2730 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00002731 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
David S. Miller7ac54592006-01-18 14:19:10 -08002733 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735 /* Reserve for ethernet and IP header */
2736 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002737 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2738 if (pkt_dev->nr_labels)
2739 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002740
2741 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002742 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002743 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002744 *svlan_tci = build_tci(pkt_dev->svlan_id,
2745 pkt_dev->svlan_cfi,
2746 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002747 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002748 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002749 }
2750 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002751 *vlan_tci = build_tci(pkt_dev->vlan_id,
2752 pkt_dev->vlan_cfi,
2753 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002754 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002755 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002756 }
2757
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002758 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002759 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002760 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002761 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002762 skb->priority = pkt_dev->skb_priority;
2763
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002764 iph = ip_hdr(skb);
2765 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002768 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002770 /* Eth + IPh + UDPh + mpls */
2771 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002772 pkt_dev->pkt_overhead;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002773 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 udph->source = htons(pkt_dev->cur_udp_src);
2777 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002778 udph->len = htons(datalen + 8); /* DATA + udphdr */
2779 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 iph->ihl = 5;
2782 iph->version = 4;
2783 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002784 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002785 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 iph->saddr = pkt_dev->cur_saddr;
2787 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002788 iph->id = htons(pkt_dev->ip_id);
2789 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 iph->frag_off = 0;
2791 iplen = 20 + 8 + datalen;
2792 iph->tot_len = htons(iplen);
2793 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002794 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002795 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002796 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002797 pkt_dev->pkt_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 skb->dev = odev;
2799 skb->pkt_type = PACKET_HOST;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002800 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002801
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002802#ifdef CONFIG_XFRM
2803 if (!process_ipsec(pkt_dev, skb, protocol))
2804 return NULL;
2805#endif
2806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return skb;
2808}
2809
2810/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002811 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2813 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002814 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 * Should be candidate for net/ipv4/utils.c
2816 * --ro
2817 */
2818
Luiz Capitulino222f1802006-03-20 22:16:13 -08002819static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002822 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 unsigned long u;
2824 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002825 unsigned int prefixlen = 0;
2826 unsigned int suffixlen = 0;
Al Viro252e33462006-11-14 20:48:11 -08002827 __be32 tmp;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002828 char *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Luiz Capitulino222f1802006-03-20 22:16:13 -08002830 for (i = 0; i < 16; i++)
2831 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 for (;;) {
2834 if (*s == ':') {
2835 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002836 if (s[1] == ':') { /* Found "::", skip to part 2 */
2837 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 len++;
2839 break;
2840 }
2841 s++;
2842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002844 u = simple_strtoul(s, &pos, 16);
2845 i = pos - s;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002846 if (!i)
2847 return 0;
2848 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /* the last 4 bytes may be written as IPv4 address */
2851
2852 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002853 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2854 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856 ip[prefixlen++] = (u >> 8);
2857 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002858 s += i;
2859 len += i;
2860 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return len;
2862 }
2863
2864/* part 2, after "::" */
2865 for (;;) {
2866 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002867 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 break;
2869 s++;
2870 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002871 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 break;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002873
2874 u = simple_strtol(s, &pos, 16);
2875 i = pos - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002877 if (*s)
2878 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 break;
2880 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002881 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002883 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2884 sizeof(tmp));
2885 suffixlen += 4;
2886 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 break;
2888 }
2889 suffix[suffixlen++] = (u >> 8);
2890 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002891 s += i;
2892 len += i;
2893 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 break;
2895 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002896 for (i = 0; i < suffixlen; i++)
2897 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return len;
2899}
2900
Luiz Capitulino222f1802006-03-20 22:16:13 -08002901static char tohex(char hexdigit)
2902{
2903 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
2905
Luiz Capitulino222f1802006-03-20 22:16:13 -08002906static int fmt_xlong(char *s, unsigned int i)
2907{
2908 char *bak = s;
2909 *s = tohex((i >> 12) & 0xf);
2910 if (s != bak || *s != '0')
2911 ++s;
2912 *s = tohex((i >> 8) & 0xf);
2913 if (s != bak || *s != '0')
2914 ++s;
2915 *s = tohex((i >> 4) & 0xf);
2916 if (s != bak || *s != '0')
2917 ++s;
2918 *s = tohex(i & 0xf);
2919 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
Luiz Capitulino222f1802006-03-20 22:16:13 -08002922static unsigned int fmt_ip6(char *s, const char ip[16])
2923{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 unsigned int len;
2925 unsigned int i;
2926 unsigned int temp;
2927 unsigned int compressing;
2928 int j;
2929
Luiz Capitulino222f1802006-03-20 22:16:13 -08002930 len = 0;
2931 compressing = 0;
2932 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002935 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2936 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2937 temp = strlen(s);
2938 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
2940#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002941 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2942 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if (temp == 0) {
2944 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002945 compressing = 1;
2946 if (j == 0) {
2947 *s++ = ':';
2948 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950 }
2951 } else {
2952 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002953 compressing = 0;
2954 *s++ = ':';
2955 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002957 i = fmt_xlong(s, temp);
2958 len += i;
2959 s += i;
2960 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 *s++ = ':';
2962 ++len;
2963 }
2964 }
2965 }
2966 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002967 *s++ = ':';
2968 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002970 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 return len;
2972}
2973
Luiz Capitulino222f1802006-03-20 22:16:13 -08002974static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2975 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
2977 struct sk_buff *skb = NULL;
2978 __u8 *eth;
2979 struct udphdr *udph;
2980 int datalen;
2981 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002982 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002983 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002984 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2985 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2986 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2987 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002988 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002989
2990 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002991 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002992
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002993 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002994 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002995
Robert Olsson64053be2005-06-26 15:27:10 -07002996 /* Update any of the values, used when we're incrementing various
2997 * fields.
2998 */
2999 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00003000 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07003001
Stephen Hemmingere4707572009-08-27 13:55:13 +00003002 skb = __netdev_alloc_skb(odev,
3003 pkt_dev->cur_pkt_size + 64
3004 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (!skb) {
3006 sprintf(pkt_dev->result, "No memory");
3007 return NULL;
3008 }
Junchang Wanga8d764b2010-12-08 16:55:16 +00003009 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011 skb_reserve(skb, 16);
3012
3013 /* Reserve for ethernet and IP header */
3014 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003015 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
3016 if (pkt_dev->nr_labels)
3017 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003018
3019 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07003020 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003021 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003022 *svlan_tci = build_tci(pkt_dev->svlan_id,
3023 pkt_dev->svlan_cfi,
3024 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003025 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003026 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003027 }
3028 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08003029 *vlan_tci = build_tci(pkt_dev->vlan_id,
3030 pkt_dev->vlan_cfi,
3031 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003032 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003033 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003034 }
3035
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003036 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003037 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003038 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003039 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00003040 skb->priority = pkt_dev->skb_priority;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03003041 iph = ipv6_hdr(skb);
3042 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003045 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07003046
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003047 /* Eth + IPh + UDPh + mpls */
3048 datalen = pkt_dev->cur_pkt_size - 14 -
3049 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003050 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Luiz Capitulino222f1802006-03-20 22:16:13 -08003052 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 datalen = sizeof(struct pktgen_hdr);
3054 if (net_ratelimit())
Joe Perchesf9467ea2010-06-21 12:29:14 +00003055 pr_info("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 }
3057
3058 udph->source = htons(pkt_dev->cur_udp_src);
3059 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003060 udph->len = htons(datalen + sizeof(struct udphdr));
3061 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08003063 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003065 if (pkt_dev->traffic_class) {
3066 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08003067 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07003068 }
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 iph->hop_limit = 32;
3071
3072 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3073 iph->nexthdr = IPPROTO_UDP;
3074
3075 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3076 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3077
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07003078 skb->mac_header = (skb->network_header - ETH_HLEN -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003079 pkt_dev->pkt_overhead);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08003080 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 skb->dev = odev;
3082 skb->pkt_type = PACKET_HOST;
3083
Eric Dumazet26ad7872011-01-25 13:26:05 -08003084 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return skb;
3087}
3088
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003089static struct sk_buff *fill_packet(struct net_device *odev,
3090 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003092 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return fill_packet_ipv6(odev, pkt_dev);
3094 else
3095 return fill_packet_ipv4(odev, pkt_dev);
3096}
3097
Luiz Capitulino222f1802006-03-20 22:16:13 -08003098static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003100 pkt_dev->seq_num = 1;
3101 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003103 pkt_dev->tx_bytes = 0;
3104 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105}
3106
3107/* Set up structure for sending pkts, clear counters */
3108
3109static void pktgen_run(struct pktgen_thread *t)
3110{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003111 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 int started = 0;
3113
Joe Perchesf9467ea2010-06-21 12:29:14 +00003114 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
3116 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003117 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 /*
3120 * setup odev and create initial packet.
3121 */
3122 pktgen_setup_inject(pkt_dev);
3123
Luiz Capitulino222f1802006-03-20 22:16:13 -08003124 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003126 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003128 pkt_dev->started_at =
3129 pkt_dev->next_tx = ktime_now();
3130
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003131 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 strcpy(pkt_dev->result, "Starting");
3134 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003135 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 strcpy(pkt_dev->result, "Error starting");
3137 }
3138 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003139 if (started)
3140 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
3143static void pktgen_stop_all_threads_ifs(void)
3144{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003145 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Joe Perchesf9467ea2010-06-21 12:29:14 +00003147 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003149 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003150
3151 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003152 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}
3156
Stephen Hemminger648fda72009-08-27 13:55:07 +00003157static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003159 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003161 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00003162 if (pkt_dev->running)
3163 return 1;
3164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Luiz Capitulino222f1802006-03-20 22:16:13 -08003167static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Luiz Capitulino222f1802006-03-20 22:16:13 -08003173 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Luiz Capitulino222f1802006-03-20 22:16:13 -08003177 if (signal_pending(current))
3178 goto signal;
3179 if_lock(t);
3180 }
3181 if_unlock(t);
3182 return 1;
3183signal:
3184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
3187static int pktgen_wait_all_threads_run(void)
3188{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003189 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003191
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003192 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003193
3194 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003196 if (sig == 0)
3197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003199
3200 if (sig == 0)
3201 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003203
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003204 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return sig;
3206}
3207
3208static void pktgen_run_all_threads(void)
3209{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003210 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Joe Perchesf9467ea2010-06-21 12:29:14 +00003212 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003214 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003216 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003218
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003219 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003221 /* Propagate thread->control */
3222 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 pktgen_wait_all_threads_run();
3225}
3226
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003227static void pktgen_reset_all_threads(void)
3228{
3229 struct pktgen_thread *t;
3230
Joe Perchesf9467ea2010-06-21 12:29:14 +00003231 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003232
3233 mutex_lock(&pktgen_thread_lock);
3234
3235 list_for_each_entry(t, &pktgen_threads, th_list)
3236 t->control |= (T_REMDEVALL);
3237
3238 mutex_unlock(&pktgen_thread_lock);
3239
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003240 /* Propagate thread->control */
3241 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003242
3243 pktgen_wait_all_threads_run();
3244}
3245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3247{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003248 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003249 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003250 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3251 pkt_dev->started_at);
3252 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Daniel Turull03a14ab2011-03-09 14:11:00 -08003254 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003255 (unsigned long long)ktime_to_us(elapsed),
3256 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3257 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003258 (unsigned long long)pkt_dev->sofar,
3259 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003261 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3262 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Luiz Capitulino222f1802006-03-20 22:16:13 -08003264 bps = pps * 8 * pkt_dev->cur_pkt_size;
3265
3266 mbps = bps;
3267 do_div(mbps, 1000000);
3268 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3269 (unsigned long long)pps,
3270 (unsigned long long)mbps,
3271 (unsigned long long)bps,
3272 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003276static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003278 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003279
Luiz Capitulino222f1802006-03-20 22:16:13 -08003280 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003281 pr_warning("interface: %s is already stopped\n",
3282 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003283 return -EINVAL;
3284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003286 kfree_skb(pkt_dev->skb);
3287 pkt_dev->skb = NULL;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003288 pkt_dev->stopped_at = ktime_now();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003289 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003291 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Luiz Capitulino222f1802006-03-20 22:16:13 -08003293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Luiz Capitulino222f1802006-03-20 22:16:13 -08003296static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003298 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if_lock(t);
3301
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003302 list_for_each_entry(pkt_dev, &t->if_list, list) {
3303 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003304 continue;
3305 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003306 best = pkt_dev;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003307 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003308 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 }
3310 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003311 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
Luiz Capitulino222f1802006-03-20 22:16:13 -08003314static void pktgen_stop(struct pktgen_thread *t)
3315{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003316 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Joe Perchesf9467ea2010-06-21 12:29:14 +00003318 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Luiz Capitulino222f1802006-03-20 22:16:13 -08003320 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003322 list_for_each_entry(pkt_dev, &t->if_list, list) {
3323 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Luiz Capitulino222f1802006-03-20 22:16:13 -08003326 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003329/*
3330 * one of our devices needs to be removed - find it
3331 * and remove it
3332 */
3333static void pktgen_rem_one_if(struct pktgen_thread *t)
3334{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003335 struct list_head *q, *n;
3336 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003337
Joe Perchesf9467ea2010-06-21 12:29:14 +00003338 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003339
3340 if_lock(t);
3341
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003342 list_for_each_safe(q, n, &t->if_list) {
3343 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003344
Luiz Capitulino222f1802006-03-20 22:16:13 -08003345 if (!cur->removal_mark)
3346 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003347
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003348 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003349 cur->skb = NULL;
3350
3351 pktgen_remove_device(t, cur);
3352
3353 break;
3354 }
3355
3356 if_unlock(t);
3357}
3358
Luiz Capitulino222f1802006-03-20 22:16:13 -08003359static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003361 struct list_head *q, *n;
3362 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003363
Joe Perchesf9467ea2010-06-21 12:29:14 +00003364 func_enter();
3365
Luiz Capitulino222f1802006-03-20 22:16:13 -08003366 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003367
Luiz Capitulino222f1802006-03-20 22:16:13 -08003368 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003370 list_for_each_safe(q, n, &t->if_list) {
3371 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003372
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003373 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003374 cur->skb = NULL;
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 pktgen_remove_device(t, cur);
3377 }
3378
Luiz Capitulino222f1802006-03-20 22:16:13 -08003379 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
3381
Luiz Capitulino222f1802006-03-20 22:16:13 -08003382static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003384 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
David S. Milleree74baa2007-01-01 20:51:53 -08003386 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
Stephen Hemmingeref879792009-09-22 19:41:43 +00003390static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003391{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003392 ktime_t idle_start = ktime_now();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003393 schedule();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003394 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003395}
3396
Stephen Hemmingeref879792009-09-22 19:41:43 +00003397static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3398{
3399 ktime_t idle_start = ktime_now();
3400
3401 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3402 if (signal_pending(current))
3403 break;
3404
3405 if (need_resched())
3406 pktgen_resched(pkt_dev);
3407 else
3408 cpu_relax();
3409 }
3410 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3411}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003412
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003413static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Stephen Hemminger00829822008-11-20 20:14:53 -08003415 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003416 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003417 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003418 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003419 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 int ret;
3421
Stephen Hemmingeref879792009-09-22 19:41:43 +00003422 /* If device is offline, then don't send */
3423 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3424 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003425 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003427
Stephen Hemmingeref879792009-09-22 19:41:43 +00003428 /* This is max DELAY, this has special meaning of
3429 * "never transmit"
3430 */
3431 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3432 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3433 return;
3434 }
3435
3436 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003437 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3438 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3439 /* build a new pkt */
3440 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003441
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003442 pkt_dev->skb = fill_packet(odev, pkt_dev);
3443 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003444 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003445 schedule();
3446 pkt_dev->clone_count--; /* back out increment, OOM */
3447 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003449 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003450 pkt_dev->allocated_skbs++;
3451 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003453
Stephen Hemmingeref879792009-09-22 19:41:43 +00003454 if (pkt_dev->delay && pkt_dev->last_ok)
3455 spin(pkt_dev, pkt_dev->next_tx);
3456
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003457 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3458 txq = netdev_get_tx_queue(odev, queue_map);
3459
3460 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003461
Eric Dumazet5a0d2262010-11-23 10:42:02 +00003462 if (unlikely(netif_tx_queue_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003463 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003464 pkt_dev->last_ok = 0;
3465 goto unlock;
3466 }
3467 atomic_inc(&(pkt_dev->skb->users));
3468 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Stephen Hemmingeref879792009-09-22 19:41:43 +00003470 switch (ret) {
3471 case NETDEV_TX_OK:
3472 txq_trans_update(txq);
3473 pkt_dev->last_ok = 1;
3474 pkt_dev->sofar++;
3475 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003476 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003477 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003478 case NET_XMIT_DROP:
3479 case NET_XMIT_CN:
3480 case NET_XMIT_POLICED:
3481 /* skb has been consumed */
3482 pkt_dev->errors++;
3483 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003484 default: /* Drivers are not supposed to return other values! */
3485 if (net_ratelimit())
Joe Perches58231182010-12-20 10:30:06 -08003486 pr_info("%s xmit error: %d\n", pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003487 pkt_dev->errors++;
3488 /* fallthru */
3489 case NETDEV_TX_LOCKED:
3490 case NETDEV_TX_BUSY:
3491 /* Retry it next time */
3492 atomic_dec(&(pkt_dev->skb->users));
3493 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003494 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003495unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003496 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 /* If pkt_dev->count is zero, then run forever */
3499 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003500 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 /* Done with this */
3503 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003504 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003507/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 * Main loop of the thread goes here
3509 */
3510
David S. Milleree74baa2007-01-01 20:51:53 -08003511static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
3513 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003514 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003515 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
David S. Milleree74baa2007-01-01 20:51:53 -08003518 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
3520 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003521 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Joe Perchesf9467ea2010-06-21 12:29:14 +00003523 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
David S. Milleree74baa2007-01-01 20:51:53 -08003525 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003526
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003527 set_freezable();
3528
David S. Milleree74baa2007-01-01 20:51:53 -08003529 while (!kthread_should_stop()) {
3530 pkt_dev = next_to_run(t);
3531
Stephen Hemmingeref879792009-09-22 19:41:43 +00003532 if (unlikely(!pkt_dev && t->control == 0)) {
Eric Dumazet551eaff2010-11-21 10:26:44 -08003533 if (pktgen_exiting)
3534 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003535 wait_event_interruptible_timeout(t->queue,
3536 t->control != 0,
3537 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003538 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003539 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003540 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 __set_current_state(TASK_RUNNING);
3543
Stephen Hemmingeref879792009-09-22 19:41:43 +00003544 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 pktgen_xmit(pkt_dev);
3546
Stephen Hemmingeref879792009-09-22 19:41:43 +00003547 if (need_resched())
3548 pktgen_resched(pkt_dev);
3549 else
3550 cpu_relax();
3551 }
3552
Luiz Capitulino222f1802006-03-20 22:16:13 -08003553 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 pktgen_stop(t);
3555 t->control &= ~(T_STOP);
3556 }
3557
Luiz Capitulino222f1802006-03-20 22:16:13 -08003558 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 pktgen_run(t);
3560 t->control &= ~(T_RUN);
3561 }
3562
Luiz Capitulino222f1802006-03-20 22:16:13 -08003563 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003565 t->control &= ~(T_REMDEVALL);
3566 }
3567
Luiz Capitulino222f1802006-03-20 22:16:13 -08003568 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003569 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 t->control &= ~(T_REMDEV);
3571 }
3572
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003573 try_to_freeze();
3574
David S. Milleree74baa2007-01-01 20:51:53 -08003575 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Joe Perchesf9467ea2010-06-21 12:29:14 +00003578 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003579 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Joe Perchesf9467ea2010-06-21 12:29:14 +00003581 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003582 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Joe Perchesf9467ea2010-06-21 12:29:14 +00003584 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003585 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003586
Eric Dumazet551eaff2010-11-21 10:26:44 -08003587 /* Wait for kthread_stop */
3588 while (!kthread_should_stop()) {
3589 set_current_state(TASK_INTERRUPTIBLE);
3590 schedule();
3591 }
3592 __set_current_state(TASK_RUNNING);
3593
David S. Milleree74baa2007-01-01 20:51:53 -08003594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Luiz Capitulino222f1802006-03-20 22:16:13 -08003597static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003598 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003600 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003601 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Eric Dumazet3e984842009-11-24 14:50:53 -08003603 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003604 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003605 if (strncmp(p->odevname, ifname, len) == 0) {
3606 if (p->odevname[len]) {
3607 if (exact || p->odevname[len] != '@')
3608 continue;
3609 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003610 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003611 break;
3612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Luiz Capitulino222f1802006-03-20 22:16:13 -08003614 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003615 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003616 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
3618
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003619/*
3620 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 */
3622
Luiz Capitulino222f1802006-03-20 22:16:13 -08003623static int add_dev_to_thread(struct pktgen_thread *t,
3624 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Luiz Capitulino222f1802006-03-20 22:16:13 -08003628 if_lock(t);
3629
3630 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003631 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003632 rv = -EBUSY;
3633 goto out;
3634 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003635
3636 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003637 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 pkt_dev->running = 0;
3639
Luiz Capitulino222f1802006-03-20 22:16:13 -08003640out:
3641 if_unlock(t);
3642 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
3645/* Called under thread lock */
3646
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003649 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003650 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003651 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003652
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 /* We don't allow a device to be on several threads */
3654
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003655 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3656 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003657 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003658 return -EBUSY;
3659 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003660
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003661 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003662 if (!pkt_dev)
3663 return -ENOMEM;
3664
Eric Dumazet593f63b2009-11-23 01:44:37 +00003665 strcpy(pkt_dev->odevname, ifname);
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003666 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3667 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003668 if (pkt_dev->flows == NULL) {
3669 kfree(pkt_dev);
3670 return -ENOMEM;
3671 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003672 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003673
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003674 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003675 pkt_dev->min_pkt_size = ETH_ZLEN;
3676 pkt_dev->max_pkt_size = ETH_ZLEN;
3677 pkt_dev->nfrags = 0;
3678 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003679 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003680 pkt_dev->count = pg_count_d;
3681 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003682 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003683 pkt_dev->udp_src_max = 9;
3684 pkt_dev->udp_dst_min = 9;
3685 pkt_dev->udp_dst_max = 9;
3686
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003687 pkt_dev->vlan_p = 0;
3688 pkt_dev->vlan_cfi = 0;
3689 pkt_dev->vlan_id = 0xffff;
3690 pkt_dev->svlan_p = 0;
3691 pkt_dev->svlan_cfi = 0;
3692 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003693 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003694
Stephen Hemminger39df2322007-03-04 16:11:51 -08003695 err = pktgen_setup_dev(pkt_dev, ifname);
3696 if (err)
3697 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003698
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003699 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3700 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003701 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003702 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003703 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003704 err = -EINVAL;
3705 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003706 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003707#ifdef CONFIG_XFRM
3708 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3709 pkt_dev->ipsproto = IPPROTO_ESP;
3710#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003711
3712 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003713out2:
3714 dev_put(pkt_dev->odev);
3715out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003716#ifdef CONFIG_XFRM
3717 free_SAs(pkt_dev);
3718#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003719 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003720 kfree(pkt_dev);
3721 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722}
3723
David S. Milleree74baa2007-01-01 20:51:53 -08003724static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003726 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003727 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003728 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003730 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3731 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003732 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003733 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003734 return -ENOMEM;
3735 }
3736
Luiz Capitulino222f1802006-03-20 22:16:13 -08003737 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003739
David S. Milleree74baa2007-01-01 20:51:53 -08003740 INIT_LIST_HEAD(&t->if_list);
3741
3742 list_add_tail(&t->th_list, &pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003743 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003744
Eric Dumazet94dcf292011-03-22 16:30:45 -07003745 p = kthread_create_on_node(pktgen_thread_worker,
3746 t,
3747 cpu_to_node(cpu),
3748 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003749 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003750 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003751 list_del(&t->th_list);
3752 kfree(t);
3753 return PTR_ERR(p);
3754 }
3755 kthread_bind(p, cpu);
3756 t->tsk = p;
3757
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003758 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3759 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003760 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003761 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003762 PG_PROC_DIR, t->tsk->comm);
3763 kthread_stop(p);
3764 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003765 kfree(t);
3766 return -EINVAL;
3767 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003768
David S. Milleree74baa2007-01-01 20:51:53 -08003769 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003770 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
3772 return 0;
3773}
3774
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003775/*
3776 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003778static void _rem_dev_from_if_list(struct pktgen_thread *t,
3779 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003781 struct list_head *q, *n;
3782 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003784 list_for_each_safe(q, n, &t->if_list) {
3785 p = list_entry(q, struct pktgen_dev, list);
3786 if (p == pkt_dev)
3787 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
3789}
3790
Luiz Capitulino222f1802006-03-20 22:16:13 -08003791static int pktgen_remove_device(struct pktgen_thread *t,
3792 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794
Joe Perchesf9467ea2010-06-21 12:29:14 +00003795 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Luiz Capitulino222f1802006-03-20 22:16:13 -08003797 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003798 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003799 pktgen_stop_device(pkt_dev);
3800 }
3801
3802 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
3804 if (pkt_dev->odev) {
3805 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003806 pkt_dev->odev = NULL;
3807 }
3808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 /* And update the thread if_list */
3810
3811 _rem_dev_from_if_list(t, pkt_dev);
3812
Stephen Hemminger39df2322007-03-04 16:11:51 -08003813 if (pkt_dev->entry)
3814 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003816#ifdef CONFIG_XFRM
3817 free_SAs(pkt_dev);
3818#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003819 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003820 if (pkt_dev->page)
3821 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
Luiz Capitulino222f1802006-03-20 22:16:13 -08003826static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
3828 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003829 struct proc_dir_entry *pe;
3830
Joe Perchesf9467ea2010-06-21 12:29:14 +00003831 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003833 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003834 if (!pg_proc_dir)
3835 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Wang Chen25296d52008-02-28 14:11:49 -08003837 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003838 if (pe == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003839 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003840 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003841 return -EINVAL;
3842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 /* Register us to receive netdevice events */
3845 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003846
John Hawkes670c02c2005-10-13 09:30:31 -07003847 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003848 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
David S. Milleree74baa2007-01-01 20:51:53 -08003850 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003851 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00003852 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3853 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003854 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003855
3856 if (list_empty(&pktgen_threads)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003857 pr_err("ERROR: Initialization failed for all threads\n");
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003858 unregister_netdevice_notifier(&pktgen_notifier_block);
3859 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003860 proc_net_remove(&init_net, PG_PROC_DIR);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003861 return -ENODEV;
3862 }
3863
Luiz Capitulino222f1802006-03-20 22:16:13 -08003864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865}
3866
3867static void __exit pg_cleanup(void)
3868{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003869 struct pktgen_thread *t;
3870 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Luiz Capitulino222f1802006-03-20 22:16:13 -08003872 /* Stop all interfaces & threads */
Eric Dumazet551eaff2010-11-21 10:26:44 -08003873 pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003875 list_for_each_safe(q, n, &pktgen_threads) {
3876 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003877 kthread_stop(t->tsk);
3878 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Luiz Capitulino222f1802006-03-20 22:16:13 -08003881 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 unregister_netdevice_notifier(&pktgen_notifier_block);
3883
Luiz Capitulino222f1802006-03-20 22:16:13 -08003884 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003885 remove_proc_entry(PGCTRL, pg_proc_dir);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003886 proc_net_remove(&init_net, PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887}
3888
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889module_init(pg_init);
3890module_exit(pg_cleanup);
3891
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003892MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893MODULE_DESCRIPTION("Packet Generator tool");
3894MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003895MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003897MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003899MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003901MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003903MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");