blob: 2dec6f1b0cd7c2d1e933047df38942cac1e68c06 [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>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700159#include <linux/prefetch.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200160#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#include <net/checksum.h>
162#include <net/ipv6.h>
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200163#include <net/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700165#ifdef CONFIG_XFRM
166#include <net/xfrm.h>
167#endif
Cong Wang4e58a022013-01-28 19:55:53 +0000168#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#include <asm/byteorder.h>
170#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700171#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000172#include <linux/io.h>
173#include <linux/timex.h>
174#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800176#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Daniel Turull43d28b62010-06-09 22:49:57 +0000178#define VERSION "2.74"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800180#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800181#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Joe Perchesf9467ea2010-06-21 12:29:14 +0000183#define func_enter() pr_debug("entering %s\n", __func__);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800186#define F_IPSRC_RND (1<<0) /* IP-Src Random */
187#define F_IPDST_RND (1<<1) /* IP-Dst Random */
188#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
189#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
190#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
191#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
192#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
193#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800194#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700195#define F_VID_RND (1<<9) /* Random VLAN ID */
196#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700197#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700198#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700199#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700200#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000201#define F_NODE (1<<15) /* Node memory alloc*/
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200202#define F_UDPCSUM (1<<16) /* Include UDP checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000205#define T_STOP (1<<0) /* Stop run */
206#define T_RUN (1<<1) /* Start run */
207#define T_REMDEVALL (1<<2) /* Remove all devs */
208#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/* If lock -- can be removed after some work */
211#define if_lock(t) spin_lock(&(t->if_lock));
212#define if_unlock(t) spin_unlock(&(t->if_lock));
213
214/* Used to help with determining the pkts on receive */
215#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700216#define PG_PROC_DIR "pktgen"
217#define PGCTRL "pgctrl"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219#define MAX_CFLOWS 65536
220
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700221#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
222#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
223
Luiz Capitulino222f1802006-03-20 22:16:13 -0800224struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800225 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800226 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700227#ifdef CONFIG_XFRM
228 struct xfrm_state *x;
229#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700230 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700233/* flow flag bits */
234#define F_INIT (1<<0) /* flow has been initialized */
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /*
238 * Try to keep frequent/infrequent used vars. separated.
239 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800240 struct proc_dir_entry *entry; /* proc file */
241 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000242 struct list_head list; /* chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000244 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800245
246 /* If min != max, then we will either do a linear iteration, or
247 * we will do a random selection from within the range.
248 */
249 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800250 int removal_mark; /* non-zero => the device is marked for
251 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000253 int min_pkt_size;
254 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700255 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800256 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800257 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000258 u64 delay; /* nano-seconds */
259
Luiz Capitulino222f1802006-03-20 22:16:13 -0800260 __u64 count; /* Default No packets to send */
261 __u64 sofar; /* How many pkts we've sent so far */
262 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800263 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Luiz Capitulino222f1802006-03-20 22:16:13 -0800265 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Luiz Capitulino222f1802006-03-20 22:16:13 -0800267 __u64 allocated_skbs;
268 __u32 clone_count;
269 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000270 * Or a failed transmit of some sort?
271 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800272 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000273 ktime_t next_tx;
274 ktime_t started_at;
275 ktime_t stopped_at;
276 u64 idle_acc; /* nano-seconds */
277
Luiz Capitulino222f1802006-03-20 22:16:13 -0800278 __u32 seq_num;
279
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000280 int clone_skb; /*
281 * Use multiple SKBs during packet gen.
282 * If this number is greater than 1, then
283 * that many copies of the same packet will be
284 * sent before a new packet is allocated.
285 * If you want to send 1024 identical packets
286 * before creating a new packet,
287 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800288 */
289
290 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
291 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
292 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
293 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
294
295 struct in6_addr in6_saddr;
296 struct in6_addr in6_daddr;
297 struct in6_addr cur_in6_daddr;
298 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800300 struct in6_addr min_in6_daddr;
301 struct in6_addr max_in6_daddr;
302 struct in6_addr min_in6_saddr;
303 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Luiz Capitulino222f1802006-03-20 22:16:13 -0800305 /* If we're doing ranges, random or incremental, then this
306 * defines the min/max for those ranges.
307 */
Al Viro252e33462006-11-14 20:48:11 -0800308 __be32 saddr_min; /* inclusive, source IP address */
309 __be32 saddr_max; /* exclusive, source IP address */
310 __be32 daddr_min; /* inclusive, dest IP address */
311 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Luiz Capitulino222f1802006-03-20 22:16:13 -0800313 __u16 udp_src_min; /* inclusive, source UDP port */
314 __u16 udp_src_max; /* exclusive, source UDP port */
315 __u16 udp_dst_min; /* inclusive, dest UDP port */
316 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700318 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000319 __u8 tos; /* six MSB of (former) IPv4 TOS
320 are for dscp codepoint */
321 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
322 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700323
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800324 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000325 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800326 __be32 labels[MAX_MPLS_LABELS];
327
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700328 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
329 __u8 vlan_p;
330 __u8 vlan_cfi;
331 __u16 vlan_id; /* 0xffff means no vlan tag */
332
333 __u8 svlan_p;
334 __u8 svlan_cfi;
335 __u16 svlan_id; /* 0xffff means no svlan tag */
336
Luiz Capitulino222f1802006-03-20 22:16:13 -0800337 __u32 src_mac_count; /* How many MACs to iterate through */
338 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Luiz Capitulino222f1802006-03-20 22:16:13 -0800340 unsigned char dst_mac[ETH_ALEN];
341 unsigned char src_mac[ETH_ALEN];
342
343 __u32 cur_dst_mac_offset;
344 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800345 __be32 cur_saddr;
346 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700347 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800348 __u16 cur_udp_dst;
349 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700350 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800351 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800352 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800353
354 __u8 hh[14];
355 /* = {
356 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
357
358 We fill in SRC address later
359 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
360 0x08, 0x00
361 };
362 */
363 __u16 pad; /* pad out the hh struct to an even 16 bytes */
364
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000365 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800366 * are transmitting the same one multiple times
367 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000368 struct net_device *odev; /* The out-going device.
369 * Note that the device should have it's
370 * pg_info pointer pointing back to this
371 * device.
372 * Set when the user specifies the out-going
373 * device name (not when the inject is
374 * started as it used to do.)
375 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000376 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000378 unsigned int cflows; /* Concurrent flows (config) */
379 unsigned int lflow; /* Flow length (config) */
380 unsigned int nflows; /* accumulated flows (stats) */
381 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700382
383 u16 queue_map_min;
384 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000385 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000386 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700387
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700388#ifdef CONFIG_XFRM
389 __u8 ipsmode; /* IPSEC mode (config) */
390 __u8 ipsproto; /* IPSEC type (config) */
391#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800392 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393};
394
395struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800396 __be32 pgh_magic;
397 __be32 seq_num;
398 __be32 tv_sec;
399 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
Cong Wang4e58a022013-01-28 19:55:53 +0000402
403static int pg_net_id __read_mostly;
404
405struct pktgen_net {
406 struct net *net;
407 struct proc_dir_entry *proc_dir;
408 struct list_head pktgen_threads;
409 bool pktgen_exiting;
410};
Eric Dumazet551eaff2010-11-21 10:26:44 -0800411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000413 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a80162006-03-20 22:18:16 -0800414 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800415 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800416 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800417 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000419 /* Field for thread to receive "posted" events terminate,
420 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800421
422 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int cpu;
424
Luiz Capitulino222f1802006-03-20 22:16:13 -0800425 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700426 struct completion start_done;
Cong Wang4e58a022013-01-28 19:55:53 +0000427 struct pktgen_net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428};
429
430#define REMOVE 1
431#define FIND 0
432
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000433static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000434 "Packet Generator for packet performance testing. "
435 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Luiz Capitulino222f1802006-03-20 22:16:13 -0800437static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
438static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
439static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800440 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
Cong Wang4e58a022013-01-28 19:55:53 +0000442static void pktgen_run_all_threads(struct pktgen_net *pn);
443static void pktgen_reset_all_threads(struct pktgen_net *pn);
444static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000445
Luiz Capitulino222f1802006-03-20 22:16:13 -0800446static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000450static int pg_count_d __read_mostly = 1000;
451static int pg_delay_d __read_mostly;
452static int pg_clone_skb_d __read_mostly;
453static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Luiz Capitulino222fa072006-03-20 22:24:27 -0800455static DEFINE_MUTEX(pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457static struct notifier_block pktgen_notifier_block = {
458 .notifier_call = pktgen_device_event,
459};
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900462 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
464 */
465
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700466static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800467{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000468 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000472static ssize_t pgctrl_write(struct file *file, const char __user *buf,
473 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700476 char data[128];
Cong Wang4e58a022013-01-28 19:55:53 +0000477 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Luiz Capitulino222f1802006-03-20 22:16:13 -0800479 if (!capable(CAP_NET_ADMIN)) {
480 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700484 if (count > sizeof(data))
485 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700488 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700489 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800490 }
491 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Luiz Capitulino222f1802006-03-20 22:16:13 -0800493 if (!strcmp(data, "stop"))
Cong Wang4e58a022013-01-28 19:55:53 +0000494 pktgen_stop_all_threads_ifs(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Luiz Capitulino222f1802006-03-20 22:16:13 -0800496 else if (!strcmp(data, "start"))
Cong Wang4e58a022013-01-28 19:55:53 +0000497 pktgen_run_all_threads(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800499 else if (!strcmp(data, "reset"))
Cong Wang4e58a022013-01-28 19:55:53 +0000500 pktgen_reset_all_threads(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800501
Luiz Capitulino222f1802006-03-20 22:16:13 -0800502 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000503 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 err = count;
506
Luiz Capitulino222f1802006-03-20 22:16:13 -0800507out:
508 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700511static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Al Virod9dda782013-03-31 18:16:14 -0400513 return single_open(file, pgctrl_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700514}
515
Arjan van de Ven9a321442007-02-12 00:55:35 -0800516static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800517 .owner = THIS_MODULE,
518 .open = pgctrl_open,
519 .read = seq_read,
520 .llseek = seq_lseek,
521 .write = pgctrl_write,
522 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700523};
524
525static int pktgen_if_show(struct seq_file *seq, void *v)
526{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000527 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000528 ktime_t stopped;
529 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Luiz Capitulino222f1802006-03-20 22:16:13 -0800531 seq_printf(seq,
532 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
533 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
534 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Luiz Capitulino222f1802006-03-20 22:16:13 -0800536 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000537 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
538 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000539 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Luiz Capitulino222f1802006-03-20 22:16:13 -0800541 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
542 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Robert Olsson45b270f2007-08-28 15:45:55 -0700544 seq_printf(seq,
545 " queue_map_min: %u queue_map_max: %u\n",
546 pkt_dev->queue_map_min,
547 pkt_dev->queue_map_max);
548
John Fastabend9e50e3a2010-11-16 19:12:28 +0000549 if (pkt_dev->skb_priority)
550 seq_printf(seq, " skb_priority: %u\n",
551 pkt_dev->skb_priority);
552
Luiz Capitulino222f1802006-03-20 22:16:13 -0800553 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800554 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000555 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
556 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
557 &pkt_dev->in6_saddr,
558 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
559 &pkt_dev->in6_daddr,
560 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000561 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800562 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000563 " dst_min: %s dst_max: %s\n",
564 pkt_dev->dst_min, pkt_dev->dst_max);
565 seq_printf(seq,
566 " src_min: %s src_max: %s\n",
567 pkt_dev->src_min, pkt_dev->src_max);
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700570 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Johannes Berge1749612008-10-27 15:59:26 -0700572 seq_printf(seq, "%pM ",
573 is_zero_ether_addr(pkt_dev->src_mac) ?
574 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Luiz Capitulino222f1802006-03-20 22:16:13 -0800576 seq_printf(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700577 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Luiz Capitulino222f1802006-03-20 22:16:13 -0800579 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000580 " udp_src_min: %d udp_src_max: %d"
581 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800582 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
583 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Luiz Capitulino222f1802006-03-20 22:16:13 -0800585 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800586 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700587 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800589 if (pkt_dev->nr_labels) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000590 unsigned int i;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800591 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700592 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800593 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
594 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
595 }
596
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000597 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700598 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000599 pkt_dev->vlan_id, pkt_dev->vlan_p,
600 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700601
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000602 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700603 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000604 pkt_dev->svlan_id, pkt_dev->svlan_p,
605 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700606
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000607 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700608 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700609
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000610 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700611 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700612
Robert Olssone99b99b2010-03-18 22:44:30 +0000613 if (pkt_dev->node >= 0)
614 seq_printf(seq, " node: %d\n", pkt_dev->node);
615
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800616 seq_printf(seq, " Flags: ");
617
Luiz Capitulino222f1802006-03-20 22:16:13 -0800618 if (pkt_dev->flags & F_IPV6)
619 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Luiz Capitulino222f1802006-03-20 22:16:13 -0800621 if (pkt_dev->flags & F_IPSRC_RND)
622 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Luiz Capitulino222f1802006-03-20 22:16:13 -0800624 if (pkt_dev->flags & F_IPDST_RND)
625 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Luiz Capitulino222f1802006-03-20 22:16:13 -0800627 if (pkt_dev->flags & F_TXSIZE_RND)
628 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Luiz Capitulino222f1802006-03-20 22:16:13 -0800630 if (pkt_dev->flags & F_UDPSRC_RND)
631 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Luiz Capitulino222f1802006-03-20 22:16:13 -0800633 if (pkt_dev->flags & F_UDPDST_RND)
634 seq_printf(seq, "UDPDST_RND ");
635
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200636 if (pkt_dev->flags & F_UDPCSUM)
637 seq_printf(seq, "UDPCSUM ");
638
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800639 if (pkt_dev->flags & F_MPLS_RND)
640 seq_printf(seq, "MPLS_RND ");
641
Robert Olsson45b270f2007-08-28 15:45:55 -0700642 if (pkt_dev->flags & F_QUEUE_MAP_RND)
643 seq_printf(seq, "QUEUE_MAP_RND ");
644
Robert Olssone6fce5b2008-08-07 02:23:01 -0700645 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
646 seq_printf(seq, "QUEUE_MAP_CPU ");
647
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700648 if (pkt_dev->cflows) {
649 if (pkt_dev->flags & F_FLOW_SEQ)
650 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
651 else
652 seq_printf(seq, "FLOW_RND ");
653 }
654
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700655#ifdef CONFIG_XFRM
656 if (pkt_dev->flags & F_IPSEC_ON)
657 seq_printf(seq, "IPSEC ");
658#endif
659
Luiz Capitulino222f1802006-03-20 22:16:13 -0800660 if (pkt_dev->flags & F_MACSRC_RND)
661 seq_printf(seq, "MACSRC_RND ");
662
663 if (pkt_dev->flags & F_MACDST_RND)
664 seq_printf(seq, "MACDST_RND ");
665
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700666 if (pkt_dev->flags & F_VID_RND)
667 seq_printf(seq, "VID_RND ");
668
669 if (pkt_dev->flags & F_SVID_RND)
670 seq_printf(seq, "SVID_RND ");
671
Robert Olssone99b99b2010-03-18 22:44:30 +0000672 if (pkt_dev->flags & F_NODE)
673 seq_printf(seq, "NODE_ALLOC ");
674
Luiz Capitulino222f1802006-03-20 22:16:13 -0800675 seq_puts(seq, "\n");
676
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000677 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000678 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000679 idle = pkt_dev->idle_acc;
680 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800681
682 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000683 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800684 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000685 (unsigned long long)pkt_dev->errors);
686
687 seq_printf(seq,
688 " started: %lluus stopped: %lluus idle: %lluus\n",
689 (unsigned long long) ktime_to_us(pkt_dev->started_at),
690 (unsigned long long) ktime_to_us(stopped),
691 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800692
693 seq_printf(seq,
694 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700695 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
696 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Luiz Capitulino222f1802006-03-20 22:16:13 -0800698 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000699 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
700 &pkt_dev->cur_in6_saddr,
701 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800702 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000703 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
704 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Luiz Capitulino222f1802006-03-20 22:16:13 -0800706 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700707 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Robert Olsson45b270f2007-08-28 15:45:55 -0700709 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
710
Luiz Capitulino222f1802006-03-20 22:16:13 -0800711 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800714 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800716 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800721
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000722static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
723 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800724{
725 int i = 0;
726 *num = 0;
727
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700728 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000729 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800730 char c;
731 *num <<= 4;
732 if (get_user(c, &user_buffer[i]))
733 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000734 value = hex_to_bin(c);
735 if (value >= 0)
736 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800737 else
738 break;
739 }
740 return i;
741}
742
Luiz Capitulino222f1802006-03-20 22:16:13 -0800743static int count_trail_chars(const char __user * user_buffer,
744 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 int i;
747
748 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800749 char c;
750 if (get_user(c, &user_buffer[i]))
751 return -EFAULT;
752 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 case '\"':
754 case '\n':
755 case '\r':
756 case '\t':
757 case ' ':
758 case '=':
759 break;
760 default:
761 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764done:
765 return i;
766}
767
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000768static long num_arg(const char __user *user_buffer, unsigned long maxlen,
769 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000771 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800773
Paul Gortmakerd6182222010-10-18 12:14:44 +0000774 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800775 char c;
776 if (get_user(c, &user_buffer[i]))
777 return -EFAULT;
778 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800780 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 } else
782 break;
783 }
784 return i;
785}
786
Luiz Capitulino222f1802006-03-20 22:16:13 -0800787static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000789 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Paul Gortmakerd6182222010-10-18 12:14:44 +0000791 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800792 char c;
793 if (get_user(c, &user_buffer[i]))
794 return -EFAULT;
795 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 case '\"':
797 case '\n':
798 case '\r':
799 case '\t':
800 case ' ':
801 goto done_str;
802 break;
803 default:
804 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return i;
809}
810
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800811static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
812{
Eric Dumazet95c96172012-04-15 05:58:06 +0000813 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800814 char c;
815 ssize_t i = 0;
816 int len;
817
818 pkt_dev->nr_labels = 0;
819 do {
820 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700821 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800822 if (len <= 0)
823 return len;
824 pkt_dev->labels[n] = htonl(tmp);
825 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
826 pkt_dev->flags |= F_MPLS_RND;
827 i += len;
828 if (get_user(c, &buffer[i]))
829 return -EFAULT;
830 i++;
831 n++;
832 if (n >= MAX_MPLS_LABELS)
833 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700834 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800835
836 pkt_dev->nr_labels = n;
837 return i;
838}
839
Luiz Capitulino222f1802006-03-20 22:16:13 -0800840static ssize_t pktgen_if_write(struct file *file,
841 const char __user * user_buffer, size_t count,
842 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Joe Perches8a994a72010-07-12 10:50:23 +0000844 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800845 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000846 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 char name[16], valstr[32];
848 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800849 char *pg_result = NULL;
850 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800852
853 pg_result = &(pkt_dev->result[0]);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000856 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -EINVAL;
858 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800859
Paul Gortmakerd6182222010-10-18 12:14:44 +0000860 max = count;
861 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800862 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000863 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800864 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000866 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* Read variable name */
869
870 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000871 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800872 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -EFAULT;
877 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800878
879 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800881 if (len < 0)
882 return len;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 i += len;
885
886 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000887 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700888 char tb[copy + 1];
889 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700891 tb[copy] = 0;
Joe Perchesf342cda2012-05-16 17:50:41 +0000892 pr_debug("%s,%lu buffer -:%s:-\n",
893 name, (unsigned long)count, tb);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (!strcmp(name, "min_pkt_size")) {
897 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000898 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800899 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800902 if (value < 14 + 20 + 8)
903 value = 14 + 20 + 8;
904 if (value != pkt_dev->min_pkt_size) {
905 pkt_dev->min_pkt_size = value;
906 pkt_dev->cur_pkt_size = value;
907 }
908 sprintf(pg_result, "OK: min_pkt_size=%u",
909 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return count;
911 }
912
Luiz Capitulino222f1802006-03-20 22:16:13 -0800913 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000915 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800916 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800919 if (value < 14 + 20 + 8)
920 value = 14 + 20 + 8;
921 if (value != pkt_dev->max_pkt_size) {
922 pkt_dev->max_pkt_size = value;
923 pkt_dev->cur_pkt_size = value;
924 }
925 sprintf(pg_result, "OK: max_pkt_size=%u",
926 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return count;
928 }
929
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (!strcmp(name, "pkt_size")) {
933 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000934 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800935 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800938 if (value < 14 + 20 + 8)
939 value = 14 + 20 + 8;
940 if (value != pkt_dev->min_pkt_size) {
941 pkt_dev->min_pkt_size = value;
942 pkt_dev->max_pkt_size = value;
943 pkt_dev->cur_pkt_size = value;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
946 return count;
947 }
948
Luiz Capitulino222f1802006-03-20 22:16:13 -0800949 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000951 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800952 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800955 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 sprintf(pg_result, "OK: debug=%u", debug);
957 return count;
958 }
959
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 if (!strcmp(name, "frags")) {
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;
966 pkt_dev->nfrags = value;
967 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
968 return count;
969 }
970 if (!strcmp(name, "delay")) {
971 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000972 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800973 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000976 if (value == 0x7FFFFFFF)
977 pkt_dev->delay = ULLONG_MAX;
978 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000979 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000980
981 sprintf(pg_result, "OK: delay=%llu",
982 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return count;
984 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000985 if (!strcmp(name, "rate")) {
986 len = num_arg(&user_buffer[i], 10, &value);
987 if (len < 0)
988 return len;
989
990 i += len;
991 if (!value)
992 return len;
993 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
994 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +0000995 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +0000996
997 sprintf(pg_result, "OK: rate=%lu", value);
998 return count;
999 }
1000 if (!strcmp(name, "ratep")) {
1001 len = num_arg(&user_buffer[i], 10, &value);
1002 if (len < 0)
1003 return len;
1004
1005 i += len;
1006 if (!value)
1007 return len;
1008 pkt_dev->delay = NSEC_PER_SEC/value;
1009 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001010 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001011
1012 sprintf(pg_result, "OK: rate=%lu", value);
1013 return count;
1014 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001015 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001017 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001018 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001021 if (value != pkt_dev->udp_src_min) {
1022 pkt_dev->udp_src_min = value;
1023 pkt_dev->cur_udp_src = value;
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1026 return count;
1027 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001028 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001030 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001031 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001034 if (value != pkt_dev->udp_dst_min) {
1035 pkt_dev->udp_dst_min = value;
1036 pkt_dev->cur_udp_dst = value;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1039 return count;
1040 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001041 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001043 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001044 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001047 if (value != pkt_dev->udp_src_max) {
1048 pkt_dev->udp_src_max = value;
1049 pkt_dev->cur_udp_src = value;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1052 return count;
1053 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001056 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001057 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001060 if (value != pkt_dev->udp_dst_max) {
1061 pkt_dev->udp_dst_max = value;
1062 pkt_dev->cur_udp_dst = value;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1065 return count;
1066 }
1067 if (!strcmp(name, "clone_skb")) {
1068 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001069 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001070 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001071 if ((value > 0) &&
1072 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1073 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001075 pkt_dev->clone_skb = value;
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1078 return count;
1079 }
1080 if (!strcmp(name, "count")) {
1081 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001082 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 i += len;
1086 pkt_dev->count = value;
1087 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001088 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return count;
1090 }
1091 if (!strcmp(name, "src_mac_count")) {
1092 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001093 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001094 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 i += len;
1097 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001098 pkt_dev->src_mac_count = value;
1099 pkt_dev->cur_src_mac_offset = 0;
1100 }
1101 sprintf(pg_result, "OK: src_mac_count=%d",
1102 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return count;
1104 }
1105 if (!strcmp(name, "dst_mac_count")) {
1106 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001107 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001108 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 i += len;
1111 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001112 pkt_dev->dst_mac_count = value;
1113 pkt_dev->cur_dst_mac_offset = 0;
1114 }
1115 sprintf(pg_result, "OK: dst_mac_count=%d",
1116 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return count;
1118 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001119 if (!strcmp(name, "node")) {
1120 len = num_arg(&user_buffer[i], 10, &value);
1121 if (len < 0)
1122 return len;
1123
1124 i += len;
1125
1126 if (node_possible(value)) {
1127 pkt_dev->node = value;
1128 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001129 if (pkt_dev->page) {
1130 put_page(pkt_dev->page);
1131 pkt_dev->page = NULL;
1132 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001133 }
1134 else
1135 sprintf(pg_result, "ERROR: node not possible");
1136 return count;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001139 char f[32];
1140 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001142 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001143 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (copy_from_user(f, &user_buffer[i], len))
1146 return -EFAULT;
1147 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001148 if (strcmp(f, "IPSRC_RND") == 0)
1149 pkt_dev->flags |= F_IPSRC_RND;
1150
1151 else if (strcmp(f, "!IPSRC_RND") == 0)
1152 pkt_dev->flags &= ~F_IPSRC_RND;
1153
1154 else if (strcmp(f, "TXSIZE_RND") == 0)
1155 pkt_dev->flags |= F_TXSIZE_RND;
1156
1157 else if (strcmp(f, "!TXSIZE_RND") == 0)
1158 pkt_dev->flags &= ~F_TXSIZE_RND;
1159
1160 else if (strcmp(f, "IPDST_RND") == 0)
1161 pkt_dev->flags |= F_IPDST_RND;
1162
1163 else if (strcmp(f, "!IPDST_RND") == 0)
1164 pkt_dev->flags &= ~F_IPDST_RND;
1165
1166 else if (strcmp(f, "UDPSRC_RND") == 0)
1167 pkt_dev->flags |= F_UDPSRC_RND;
1168
1169 else if (strcmp(f, "!UDPSRC_RND") == 0)
1170 pkt_dev->flags &= ~F_UDPSRC_RND;
1171
1172 else if (strcmp(f, "UDPDST_RND") == 0)
1173 pkt_dev->flags |= F_UDPDST_RND;
1174
1175 else if (strcmp(f, "!UDPDST_RND") == 0)
1176 pkt_dev->flags &= ~F_UDPDST_RND;
1177
1178 else if (strcmp(f, "MACSRC_RND") == 0)
1179 pkt_dev->flags |= F_MACSRC_RND;
1180
1181 else if (strcmp(f, "!MACSRC_RND") == 0)
1182 pkt_dev->flags &= ~F_MACSRC_RND;
1183
1184 else if (strcmp(f, "MACDST_RND") == 0)
1185 pkt_dev->flags |= F_MACDST_RND;
1186
1187 else if (strcmp(f, "!MACDST_RND") == 0)
1188 pkt_dev->flags &= ~F_MACDST_RND;
1189
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001190 else if (strcmp(f, "MPLS_RND") == 0)
1191 pkt_dev->flags |= F_MPLS_RND;
1192
1193 else if (strcmp(f, "!MPLS_RND") == 0)
1194 pkt_dev->flags &= ~F_MPLS_RND;
1195
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001196 else if (strcmp(f, "VID_RND") == 0)
1197 pkt_dev->flags |= F_VID_RND;
1198
1199 else if (strcmp(f, "!VID_RND") == 0)
1200 pkt_dev->flags &= ~F_VID_RND;
1201
1202 else if (strcmp(f, "SVID_RND") == 0)
1203 pkt_dev->flags |= F_SVID_RND;
1204
1205 else if (strcmp(f, "!SVID_RND") == 0)
1206 pkt_dev->flags &= ~F_SVID_RND;
1207
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001208 else if (strcmp(f, "FLOW_SEQ") == 0)
1209 pkt_dev->flags |= F_FLOW_SEQ;
1210
Robert Olsson45b270f2007-08-28 15:45:55 -07001211 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1212 pkt_dev->flags |= F_QUEUE_MAP_RND;
1213
1214 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1215 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001216
1217 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1218 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1219
1220 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1221 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001222#ifdef CONFIG_XFRM
1223 else if (strcmp(f, "IPSEC") == 0)
1224 pkt_dev->flags |= F_IPSEC_ON;
1225#endif
1226
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001227 else if (strcmp(f, "!IPV6") == 0)
1228 pkt_dev->flags &= ~F_IPV6;
1229
Robert Olssone99b99b2010-03-18 22:44:30 +00001230 else if (strcmp(f, "NODE_ALLOC") == 0)
1231 pkt_dev->flags |= F_NODE;
1232
1233 else if (strcmp(f, "!NODE_ALLOC") == 0)
1234 pkt_dev->flags &= ~F_NODE;
1235
Thomas Grafc26bf4a2013-07-25 18:12:18 +02001236 else if (strcmp(f, "UDPCSUM") == 0)
1237 pkt_dev->flags |= F_UDPCSUM;
1238
1239 else if (strcmp(f, "!UDPCSUM") == 0)
1240 pkt_dev->flags &= ~F_UDPCSUM;
1241
Luiz Capitulino222f1802006-03-20 22:16:13 -08001242 else {
1243 sprintf(pg_result,
1244 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1245 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001246 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Robert Olssone99b99b2010-03-18 22:44:30 +00001247 "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 -08001248 return count;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1251 return count;
1252 }
1253 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1254 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001255 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001256 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Luiz Capitulino222f1802006-03-20 22:16:13 -08001258 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 buf[len] = 0;
1261 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1262 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1263 strncpy(pkt_dev->dst_min, buf, len);
1264 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1265 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1266 }
1267 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001268 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001269 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1271 return count;
1272 }
1273 if (!strcmp(name, "dst_max")) {
1274 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001275 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001276 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Luiz Capitulino222f1802006-03-20 22:16:13 -08001279 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return -EFAULT;
1281
Luiz Capitulino222f1802006-03-20 22:16:13 -08001282 buf[len] = 0;
1283 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1284 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1285 strncpy(pkt_dev->dst_max, buf, len);
1286 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1287 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1288 }
1289 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001290 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 i += len;
1292 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1293 return count;
1294 }
1295 if (!strcmp(name, "dst6")) {
1296 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001297 if (len < 0)
1298 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 pkt_dev->flags |= F_IPV6;
1301
Luiz Capitulino222f1802006-03-20 22:16:13 -08001302 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001304 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Amerigo Wangc468fb12012-10-09 17:48:20 +00001306 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001307 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001309 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Luiz Capitulino222f1802006-03-20 22:16:13 -08001311 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001312 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Luiz Capitulino222f1802006-03-20 22:16:13 -08001314 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 sprintf(pg_result, "OK: dst6=%s", buf);
1316 return count;
1317 }
1318 if (!strcmp(name, "dst6_min")) {
1319 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 if (len < 0)
1321 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 pkt_dev->flags |= F_IPV6;
1324
Luiz Capitulino222f1802006-03-20 22:16:13 -08001325 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001327 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Amerigo Wangc468fb12012-10-09 17:48:20 +00001329 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001330 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001332 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001333 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001334 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Luiz Capitulino222f1802006-03-20 22:16:13 -08001336 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 sprintf(pg_result, "OK: dst6_min=%s", buf);
1338 return count;
1339 }
1340 if (!strcmp(name, "dst6_max")) {
1341 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001342 if (len < 0)
1343 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 pkt_dev->flags |= F_IPV6;
1346
Luiz Capitulino222f1802006-03-20 22:16:13 -08001347 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001349 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Amerigo Wangc468fb12012-10-09 17:48:20 +00001351 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001352 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Luiz Capitulino222f1802006-03-20 22:16:13 -08001354 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001355 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Luiz Capitulino222f1802006-03-20 22:16:13 -08001357 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 sprintf(pg_result, "OK: dst6_max=%s", buf);
1359 return count;
1360 }
1361 if (!strcmp(name, "src6")) {
1362 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001363 if (len < 0)
1364 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 pkt_dev->flags |= F_IPV6;
1367
Luiz Capitulino222f1802006-03-20 22:16:13 -08001368 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001370 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Amerigo Wangc468fb12012-10-09 17:48:20 +00001372 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001373 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001375 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Luiz Capitulino222f1802006-03-20 22:16:13 -08001377 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001378 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001379
1380 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 sprintf(pg_result, "OK: src6=%s", buf);
1382 return count;
1383 }
1384 if (!strcmp(name, "src_min")) {
1385 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001386 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001387 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001388
Luiz Capitulino222f1802006-03-20 22:16:13 -08001389 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001391 buf[len] = 0;
1392 if (strcmp(buf, pkt_dev->src_min) != 0) {
1393 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1394 strncpy(pkt_dev->src_min, buf, len);
1395 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1396 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1397 }
1398 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001399 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 i += len;
1401 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1402 return count;
1403 }
1404 if (!strcmp(name, "src_max")) {
1405 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001406 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001407 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001408
Luiz Capitulino222f1802006-03-20 22:16:13 -08001409 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001411 buf[len] = 0;
1412 if (strcmp(buf, pkt_dev->src_max) != 0) {
1413 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1414 strncpy(pkt_dev->src_max, buf, len);
1415 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1416 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1417 }
1418 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001419 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 i += len;
1421 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1422 return count;
1423 }
1424 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001426 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001427 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001430 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001433 if (!mac_pton(valstr, pkt_dev->dst_mac))
1434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* Set up Dest MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001436 memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001437
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001438 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return count;
1440 }
1441 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001443 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001444 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001447 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001450 if (!mac_pton(valstr, pkt_dev->src_mac))
1451 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001452 /* Set up Src MAC */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001453 memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001454
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001455 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return count;
1457 }
1458
Luiz Capitulino222f1802006-03-20 22:16:13 -08001459 if (!strcmp(name, "clear_counters")) {
1460 pktgen_clear_counters(pkt_dev);
1461 sprintf(pg_result, "OK: Clearing counters.\n");
1462 return count;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 if (!strcmp(name, "flows")) {
1466 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001467 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001468 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 i += len;
1471 if (value > MAX_CFLOWS)
1472 value = MAX_CFLOWS;
1473
1474 pkt_dev->cflows = value;
1475 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1476 return count;
1477 }
1478
1479 if (!strcmp(name, "flowlen")) {
1480 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001481 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001482 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 i += len;
1485 pkt_dev->lflow = value;
1486 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1487 return count;
1488 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001489
Robert Olsson45b270f2007-08-28 15:45:55 -07001490 if (!strcmp(name, "queue_map_min")) {
1491 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001492 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001493 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001494
Robert Olsson45b270f2007-08-28 15:45:55 -07001495 i += len;
1496 pkt_dev->queue_map_min = value;
1497 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1498 return count;
1499 }
1500
1501 if (!strcmp(name, "queue_map_max")) {
1502 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001503 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001504 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001505
Robert Olsson45b270f2007-08-28 15:45:55 -07001506 i += len;
1507 pkt_dev->queue_map_max = value;
1508 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1509 return count;
1510 }
1511
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001512 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001513 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001514
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001515 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001516 if (len < 0)
1517 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001518 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001519 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001520 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001521 cnt += sprintf(pg_result + cnt,
1522 "%08x%s", ntohl(pkt_dev->labels[n]),
1523 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001524
1525 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1526 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1527 pkt_dev->svlan_id = 0xffff;
1528
1529 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001530 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001531 }
1532 return count;
1533 }
1534
1535 if (!strcmp(name, "vlan_id")) {
1536 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001537 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001538 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001539
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001540 i += len;
1541 if (value <= 4095) {
1542 pkt_dev->vlan_id = value; /* turn on VLAN */
1543
1544 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001545 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001546
1547 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001548 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001549
1550 pkt_dev->nr_labels = 0; /* turn off MPLS */
1551 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1552 } else {
1553 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1554 pkt_dev->svlan_id = 0xffff;
1555
1556 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001557 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001558 }
1559 return count;
1560 }
1561
1562 if (!strcmp(name, "vlan_p")) {
1563 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001564 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001565 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001566
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001567 i += len;
1568 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1569 pkt_dev->vlan_p = value;
1570 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1571 } else {
1572 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1573 }
1574 return count;
1575 }
1576
1577 if (!strcmp(name, "vlan_cfi")) {
1578 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001579 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001580 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001581
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001582 i += len;
1583 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1584 pkt_dev->vlan_cfi = value;
1585 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1586 } else {
1587 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1588 }
1589 return count;
1590 }
1591
1592 if (!strcmp(name, "svlan_id")) {
1593 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001594 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001595 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001596
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001597 i += len;
1598 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1599 pkt_dev->svlan_id = value; /* turn on SVLAN */
1600
1601 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001602 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001603
1604 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001605 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001606
1607 pkt_dev->nr_labels = 0; /* turn off MPLS */
1608 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1609 } else {
1610 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1611 pkt_dev->svlan_id = 0xffff;
1612
1613 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001614 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001615 }
1616 return count;
1617 }
1618
1619 if (!strcmp(name, "svlan_p")) {
1620 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001621 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001622 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001623
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001624 i += len;
1625 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1626 pkt_dev->svlan_p = value;
1627 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1628 } else {
1629 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1630 }
1631 return count;
1632 }
1633
1634 if (!strcmp(name, "svlan_cfi")) {
1635 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001636 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001637 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001638
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001639 i += len;
1640 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1641 pkt_dev->svlan_cfi = value;
1642 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1643 } else {
1644 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1645 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001646 return count;
1647 }
1648
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001649 if (!strcmp(name, "tos")) {
1650 __u32 tmp_value = 0;
1651 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001652 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001653 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001654
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001655 i += len;
1656 if (len == 2) {
1657 pkt_dev->tos = tmp_value;
1658 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1659 } else {
1660 sprintf(pg_result, "ERROR: tos must be 00-ff");
1661 }
1662 return count;
1663 }
1664
1665 if (!strcmp(name, "traffic_class")) {
1666 __u32 tmp_value = 0;
1667 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001668 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001669 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001670
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001671 i += len;
1672 if (len == 2) {
1673 pkt_dev->traffic_class = tmp_value;
1674 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1675 } else {
1676 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1677 }
1678 return count;
1679 }
1680
John Fastabend9e50e3a2010-11-16 19:12:28 +00001681 if (!strcmp(name, "skb_priority")) {
1682 len = num_arg(&user_buffer[i], 9, &value);
1683 if (len < 0)
1684 return len;
1685
1686 i += len;
1687 pkt_dev->skb_priority = value;
1688 sprintf(pg_result, "OK: skb_priority=%i",
1689 pkt_dev->skb_priority);
1690 return count;
1691 }
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1694 return -EINVAL;
1695}
1696
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001697static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Al Virod9dda782013-03-31 18:16:14 -04001699 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001700}
1701
Arjan van de Ven9a321442007-02-12 00:55:35 -08001702static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001703 .owner = THIS_MODULE,
1704 .open = pktgen_if_open,
1705 .read = seq_read,
1706 .llseek = seq_lseek,
1707 .write = pktgen_if_write,
1708 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001709};
1710
1711static int pktgen_thread_show(struct seq_file *seq, void *v)
1712{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001713 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001714 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001716 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Luiz Capitulino222f1802006-03-20 22:16:13 -08001718 seq_printf(seq, "Running: ");
1719
1720 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08001721 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001722 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001723 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Luiz Capitulino222f1802006-03-20 22:16:13 -08001725 seq_printf(seq, "\nStopped: ");
1726
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08001727 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001728 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001729 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001732 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001734 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Luiz Capitulino222f1802006-03-20 22:16:13 -08001736 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001741static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001742 const char __user * user_buffer,
1743 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Joe Perches8a994a72010-07-12 10:50:23 +00001745 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001746 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001747 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001749 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001752 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return -EINVAL;
1754 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001755
Paul Gortmakerd6182222010-10-18 12:14:44 +00001756 max = count;
1757 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001758 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001759 return len;
1760
Paul Gortmakerd6182222010-10-18 12:14:44 +00001761 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /* Read variable name */
1764
1765 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001766 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001767 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 memset(name, 0, sizeof(name));
1770 if (copy_from_user(name, &user_buffer[i], len))
1771 return -EFAULT;
1772 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001773
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001776 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001777 return len;
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 i += len;
1780
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001781 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001782 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Luiz Capitulino222f1802006-03-20 22:16:13 -08001784 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001785 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 ret = -EINVAL;
1787 goto out;
1788 }
1789
1790 pg_result = &(t->result[0]);
1791
Luiz Capitulino222f1802006-03-20 22:16:13 -08001792 if (!strcmp(name, "add_device")) {
1793 char f[32];
1794 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001796 if (len < 0) {
1797 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto out;
1799 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001800 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return -EFAULT;
1802 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001803 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001804 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001805 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001806 if (!ret) {
1807 ret = count;
1808 sprintf(pg_result, "OK: add_device=%s", f);
1809 } else
1810 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto out;
1812 }
1813
Luiz Capitulino222f1802006-03-20 22:16:13 -08001814 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001815 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001816 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001817 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001818 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001820 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto out;
1822 }
1823
Luiz Capitulino222f1802006-03-20 22:16:13 -08001824 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001825 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001826 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto out;
1828 }
1829
1830 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001831out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return ret;
1833}
1834
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001835static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Al Virod9dda782013-03-31 18:16:14 -04001837 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Arjan van de Ven9a321442007-02-12 00:55:35 -08001840static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001841 .owner = THIS_MODULE,
1842 .open = pktgen_thread_open,
1843 .read = seq_read,
1844 .llseek = seq_lseek,
1845 .write = pktgen_thread_write,
1846 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001847};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001850static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1851 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 struct pktgen_thread *t;
1854 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001855 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Cong Wang4e58a022013-01-28 19:55:53 +00001857 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001858 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001860 if (remove) {
1861 if_lock(t);
1862 pkt_dev->removal_mark = 1;
1863 t->control |= T_REMDEV;
1864 if_unlock(t);
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 break;
1867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001869 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001872/*
1873 * mark a device for removal
1874 */
Cong Wang4e58a022013-01-28 19:55:53 +00001875static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
1877 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001878 const int max_tries = 10, msec_per_try = 125;
1879 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001880
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001881 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001882 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001883
Luiz Capitulino222f1802006-03-20 22:16:13 -08001884 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001885
Cong Wang4e58a022013-01-28 19:55:53 +00001886 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001887 if (pkt_dev == NULL)
1888 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001889
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001890 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001891 pr_debug("%s: waiting for %s to disappear....\n",
1892 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001893 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001894 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001895
1896 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001897 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1898 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001899 break;
1900 }
1901
1902 }
1903
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001904 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001905}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001906
Cong Wang4e58a022013-01-28 19:55:53 +00001907static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001908{
1909 struct pktgen_thread *t;
1910
Cong Wang4e58a022013-01-28 19:55:53 +00001911 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001912 struct pktgen_dev *pkt_dev;
1913
1914 list_for_each_entry(pkt_dev, &t->if_list, list) {
1915 if (pkt_dev->odev != dev)
1916 continue;
1917
David Howellsa8ca16e2013-04-12 17:27:28 +01001918 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001919
Alexey Dobriyan29753152009-08-28 23:34:43 -07001920 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00001921 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07001922 &pktgen_if_fops,
1923 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001924 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001925 pr_err("can't move proc entry for '%s'\n",
1926 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001927 break;
1928 }
1929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Luiz Capitulino222f1802006-03-20 22:16:13 -08001932static int pktgen_device_event(struct notifier_block *unused,
1933 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Jiri Pirko351638e2013-05-28 01:30:21 +00001935 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00001936 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Cong Wang4e58a022013-01-28 19:55:53 +00001938 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001939 return NOTIFY_DONE;
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* It is OK that we do not hold the group lock right now,
1942 * as we run under the RTNL lock.
1943 */
1944
1945 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001946 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00001947 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00001951 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 return NOTIFY_DONE;
1956}
1957
Cong Wang4e58a022013-01-28 19:55:53 +00001958static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1959 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001960 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001961{
1962 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001963 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001964
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001965 for (i = 0; ifname[i] != '@'; i++) {
1966 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001967 break;
1968
1969 b[i] = ifname[i];
1970 }
1971 b[i] = 0;
1972
Cong Wang4e58a022013-01-28 19:55:53 +00001973 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07001974}
1975
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/* Associate pktgen_dev with a device. */
1978
Cong Wang4e58a022013-01-28 19:55:53 +00001979static int pktgen_setup_dev(const struct pktgen_net *pn,
1980 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001981{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001983 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (pkt_dev->odev) {
1987 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001988 pkt_dev->odev = NULL;
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Cong Wang4e58a022013-01-28 19:55:53 +00001991 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001993 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001994 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001998 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001999 err = -EINVAL;
2000 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002001 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002002 err = -ENETDOWN;
2003 } else {
2004 pkt_dev->odev = odev;
2005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
2012/* Read pkt_dev from the interface and set up internal pktgen_dev
2013 * structure to have the right information to create/send packets
2014 */
2015static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2016{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002017 int ntxq;
2018
Luiz Capitulino222f1802006-03-20 22:16:13 -08002019 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002020 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002021 sprintf(pkt_dev->result,
2022 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2023 return;
2024 }
2025
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002026 /* make sure that we don't pick a non-existing transmit queue */
2027 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002028
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002029 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002030 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2031 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2032 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002033 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002034 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002035 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002036 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2037 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2038 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002039 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002040 }
2041
Luiz Capitulino222f1802006-03-20 22:16:13 -08002042 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002044 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08002045 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Luiz Capitulino222f1802006-03-20 22:16:13 -08002047 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002048 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Luiz Capitulino222f1802006-03-20 22:16:13 -08002050 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002051 int i, set = 0, err = 1;
2052 struct inet6_dev *idev;
2053
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002054 if (pkt_dev->min_pkt_size == 0) {
2055 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2056 + sizeof(struct udphdr)
2057 + sizeof(struct pktgen_hdr)
2058 + pkt_dev->pkt_overhead;
2059 }
2060
Luiz Capitulino222f1802006-03-20 22:16:13 -08002061 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2062 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 set = 1;
2064 break;
2065 }
2066
Luiz Capitulino222f1802006-03-20 22:16:13 -08002067 if (!set) {
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 /*
2070 * Use linklevel address if unconfigured.
2071 *
2072 * use ipv6_get_lladdr if/when it's get exported
2073 */
2074
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002075 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002076 idev = __in6_dev_get(pkt_dev->odev);
2077 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct inet6_ifaddr *ifp;
2079
2080 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002081 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2082 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002083 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002084 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 err = 0;
2086 break;
2087 }
2088 }
2089 read_unlock_bh(&idev->lock);
2090 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002091 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002092 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002093 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002095 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002096 if (pkt_dev->min_pkt_size == 0) {
2097 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2098 + sizeof(struct udphdr)
2099 + sizeof(struct pktgen_hdr)
2100 + pkt_dev->pkt_overhead;
2101 }
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 pkt_dev->saddr_min = 0;
2104 pkt_dev->saddr_max = 0;
2105 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002106
2107 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002110 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (in_dev) {
2112 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002113 pkt_dev->saddr_min =
2114 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 pkt_dev->saddr_max = pkt_dev->saddr_min;
2116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002119 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2121 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2122 }
2123
2124 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2125 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2126 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002127 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002128 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2129 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2130 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2131
Luiz Capitulino222f1802006-03-20 22:16:13 -08002132 pkt_dev->cur_dst_mac_offset = 0;
2133 pkt_dev->cur_src_mac_offset = 0;
2134 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2135 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2136 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2137 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 pkt_dev->nflows = 0;
2139}
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002142static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2143{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002144 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002145 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002146 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002147
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002148 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2149 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002150
Daniel Turull43d28b62010-06-09 22:49:57 +00002151 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002152 if (remaining <= 0) {
2153 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002154 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002155 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002156
Daniel Borkmann398f3822012-10-28 08:27:19 +00002157 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002158 if (remaining < 100000) {
2159 /* for small delays (<100us), just loop until limit is reached */
2160 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002161 end_time = ktime_get();
2162 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002163 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002164 /* see do_nanosleep */
2165 hrtimer_init_sleeper(&t, current);
2166 do {
2167 set_current_state(TASK_INTERRUPTIBLE);
2168 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2169 if (!hrtimer_active(&t.timer))
2170 t.task = NULL;
2171
2172 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002175 hrtimer_cancel(&t.timer);
2176 } while (t.task && pkt_dev->running && !signal_pending(current));
2177 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002178 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002180
2181 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002182 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002185static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2186{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002187 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002188 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2189 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2190 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2191}
2192
Stephen Hemminger648fda72009-08-27 13:55:07 +00002193static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002194{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002195 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002196}
2197
2198static inline int f_pick(struct pktgen_dev *pkt_dev)
2199{
2200 int flow = pkt_dev->curfl;
2201
2202 if (pkt_dev->flags & F_FLOW_SEQ) {
2203 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2204 /* reset time */
2205 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002206 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002207 pkt_dev->curfl += 1;
2208 if (pkt_dev->curfl >= pkt_dev->cflows)
2209 pkt_dev->curfl = 0; /*reset */
2210 }
2211 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002212 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002213 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002214
Robert Olsson1211a642008-08-05 18:44:26 -07002215 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002216 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002217 pkt_dev->flows[flow].flags = 0;
2218 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002219 }
2220
2221 return pkt_dev->curfl;
2222}
2223
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002224
2225#ifdef CONFIG_XFRM
2226/* If there was already an IPSEC SA, we keep it as is, else
2227 * we go look for it ...
2228*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002229#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002230static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002231{
2232 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002233 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002234 if (!x) {
2235 /*slow path: we dont already have xfrm_state*/
Cong Wang4e58a022013-01-28 19:55:53 +00002236 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08002237 (xfrm_address_t *)&pkt_dev->cur_daddr,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002238 (xfrm_address_t *)&pkt_dev->cur_saddr,
2239 AF_INET,
2240 pkt_dev->ipsmode,
2241 pkt_dev->ipsproto, 0);
2242 if (x) {
2243 pkt_dev->flows[flow].x = x;
2244 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002245 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002246 }
2247
2248 }
2249}
2250#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002251static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2252{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002253
2254 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2255 pkt_dev->cur_queue_map = smp_processor_id();
2256
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002257 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002258 __u16 t;
2259 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002260 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002261 (pkt_dev->queue_map_max -
2262 pkt_dev->queue_map_min + 1)
2263 + pkt_dev->queue_map_min;
2264 } else {
2265 t = pkt_dev->cur_queue_map + 1;
2266 if (t > pkt_dev->queue_map_max)
2267 t = pkt_dev->queue_map_min;
2268 }
2269 pkt_dev->cur_queue_map = t;
2270 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002271 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 -07002272}
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274/* Increment/randomize headers according to flags and current values
2275 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2276 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002277static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2278{
2279 __u32 imn;
2280 __u32 imx;
2281 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002283 if (pkt_dev->cflows)
2284 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002287 if (pkt_dev->src_mac_count > 1) {
2288 __u32 mc;
2289 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Luiz Capitulino222f1802006-03-20 22:16:13 -08002291 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002292 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002293 else {
2294 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002295 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002296 pkt_dev->src_mac_count)
2297 pkt_dev->cur_src_mac_offset = 0;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Luiz Capitulino222f1802006-03-20 22:16:13 -08002300 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2301 pkt_dev->hh[11] = tmp;
2302 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2303 pkt_dev->hh[10] = tmp;
2304 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2305 pkt_dev->hh[9] = tmp;
2306 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2307 pkt_dev->hh[8] = tmp;
2308 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2309 pkt_dev->hh[7] = tmp;
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Luiz Capitulino222f1802006-03-20 22:16:13 -08002312 /* Deal with Destination MAC */
2313 if (pkt_dev->dst_mac_count > 1) {
2314 __u32 mc;
2315 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Luiz Capitulino222f1802006-03-20 22:16:13 -08002317 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002318 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Luiz Capitulino222f1802006-03-20 22:16:13 -08002320 else {
2321 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002322 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 pkt_dev->dst_mac_count) {
2324 pkt_dev->cur_dst_mac_offset = 0;
2325 }
2326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Luiz Capitulino222f1802006-03-20 22:16:13 -08002328 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2329 pkt_dev->hh[5] = tmp;
2330 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2331 pkt_dev->hh[4] = tmp;
2332 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2333 pkt_dev->hh[3] = tmp;
2334 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2335 pkt_dev->hh[2] = tmp;
2336 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2337 pkt_dev->hh[1] = tmp;
2338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002340 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002341 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002342 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002343 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2344 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002345 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002346 htonl(0x000fffff));
2347 }
2348
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002349 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002350 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002351 }
2352
2353 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002354 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002355 }
2356
Luiz Capitulino222f1802006-03-20 22:16:13 -08002357 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2358 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002359 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002360 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2361 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Luiz Capitulino222f1802006-03-20 22:16:13 -08002363 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 pkt_dev->cur_udp_src++;
2365 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2366 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002367 }
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Luiz Capitulino222f1802006-03-20 22:16:13 -08002370 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2371 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002372 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002373 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2374 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002375 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002377 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002379 }
2380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (!(pkt_dev->flags & F_IPV6)) {
2383
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002384 imn = ntohl(pkt_dev->saddr_min);
2385 imx = ntohl(pkt_dev->saddr_max);
2386 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002388 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002389 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 else {
2391 t = ntohl(pkt_dev->cur_saddr);
2392 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002393 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397 pkt_dev->cur_saddr = htonl(t);
2398 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002399
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002400 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2402 } else {
Al Viro252e33462006-11-14 20:48:11 -08002403 imn = ntohl(pkt_dev->daddr_min);
2404 imx = ntohl(pkt_dev->daddr_max);
2405 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002407 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (pkt_dev->flags & F_IPDST_RND) {
2409
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002410 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002411 t = prandom_u32() %
2412 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002413 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002414 } while (ipv4_is_loopback(s) ||
2415 ipv4_is_multicast(s) ||
2416 ipv4_is_lbcast(s) ||
2417 ipv4_is_zeronet(s) ||
2418 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002419 pkt_dev->cur_daddr = s;
2420 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 t = ntohl(pkt_dev->cur_daddr);
2422 t++;
2423 if (t > imx) {
2424 t = imn;
2425 }
2426 pkt_dev->cur_daddr = htonl(t);
2427 }
2428 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002429 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002430 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002431 pkt_dev->flows[flow].cur_daddr =
2432 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002433#ifdef CONFIG_XFRM
2434 if (pkt_dev->flags & F_IPSEC_ON)
2435 get_ipsec_sa(pkt_dev, flow);
2436#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 pkt_dev->nflows++;
2438 }
2439 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002440 } else { /* IPV6 * */
2441
Joe Perches06e30412012-10-18 17:55:31 +00002442 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int i;
2444
2445 /* Only random destinations yet */
2446
Luiz Capitulino222f1802006-03-20 22:16:13 -08002447 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002449 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002450 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2451 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
2455
Luiz Capitulino222f1802006-03-20 22:16:13 -08002456 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2457 __u32 t;
2458 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002459 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002460 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2461 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002462 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002464 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002466 }
2467 pkt_dev->cur_pkt_size = t;
2468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002470 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 pkt_dev->flows[flow].count++;
2473}
2474
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002475
2476#ifdef CONFIG_XFRM
2477static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2478{
2479 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2480 int err = 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002481
2482 if (!x)
2483 return 0;
2484 /* XXX: we dont support tunnel mode for now until
2485 * we resolve the dst issue */
2486 if (x->props.mode != XFRM_MODE_TRANSPORT)
2487 return 0;
2488
2489 spin_lock(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002490
Herbert Xu13996372007-10-17 21:35:51 -07002491 err = x->outer_mode->output(x, skb);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002492 if (err)
2493 goto error;
2494 err = x->type->output(x, skb);
2495 if (err)
2496 goto error;
2497
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002498 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002499 x->curlft.packets++;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002500error:
2501 spin_unlock(&x->lock);
2502 return err;
2503}
2504
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002505static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002506{
2507 if (pkt_dev->cflows) {
2508 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002509 int i;
2510 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002511 struct xfrm_state *x = pkt_dev->flows[i].x;
2512 if (x) {
2513 xfrm_state_put(x);
2514 pkt_dev->flows[i].x = NULL;
2515 }
2516 }
2517 }
2518}
2519
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002520static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002521 struct sk_buff *skb, __be16 protocol)
2522{
2523 if (pkt_dev->flags & F_IPSEC_ON) {
2524 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2525 int nhead = 0;
2526 if (x) {
2527 int ret;
2528 __u8 *eth;
2529 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002530 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002531 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2532 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002533 pr_err("Error expanding ipsec packet %d\n",
2534 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002535 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002536 }
2537 }
2538
2539 /* ipsec is not expecting ll header */
2540 skb_pull(skb, ETH_HLEN);
2541 ret = pktgen_output_ipsec(skb, pkt_dev);
2542 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002543 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002544 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002545 }
2546 /* restore ll */
2547 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2548 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002549 *(u16 *) &eth[12] = protocol;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002550 }
2551 }
2552 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002553err:
2554 kfree_skb(skb);
2555 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002556}
2557#endif
2558
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002559static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2560{
Eric Dumazet95c96172012-04-15 05:58:06 +00002561 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002562 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002563 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002564
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002565 mpls--;
2566 *mpls |= MPLS_STACK_BOTTOM;
2567}
2568
Al Viro0f37c602006-11-03 03:49:56 -08002569static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2570 unsigned int prio)
2571{
2572 return htons(id | (cfi << 12) | (prio << 13));
2573}
2574
Eric Dumazet26ad7872011-01-25 13:26:05 -08002575static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2576 int datalen)
2577{
2578 struct timeval timestamp;
2579 struct pktgen_hdr *pgh;
2580
2581 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2582 datalen -= sizeof(*pgh);
2583
2584 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002585 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002586 } else {
2587 int frags = pkt_dev->nfrags;
2588 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002589 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002590
2591
2592 if (frags > MAX_SKB_FRAGS)
2593 frags = MAX_SKB_FRAGS;
2594 len = datalen - frags * PAGE_SIZE;
2595 if (len > 0) {
2596 memset(skb_put(skb, len), 0, len);
2597 datalen = frags * PAGE_SIZE;
2598 }
2599
2600 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002601 frag_len = (datalen/frags) < PAGE_SIZE ?
2602 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002603 while (datalen > 0) {
2604 if (unlikely(!pkt_dev->page)) {
2605 int node = numa_node_id();
2606
2607 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2608 node = pkt_dev->node;
2609 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2610 if (!pkt_dev->page)
2611 break;
2612 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002613 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002614 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002615 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002616 /*last fragment, fill rest of data*/
2617 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002618 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2619 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002620 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002621 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2622 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2623 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2624 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002625 i++;
2626 skb_shinfo(skb)->nr_frags = i;
2627 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002628 }
2629
2630 /* Stamp the time, and sequence number,
2631 * convert them to network byte order
2632 */
2633 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2634 pgh->seq_num = htonl(pkt_dev->seq_num);
2635
2636 do_gettimeofday(&timestamp);
2637 pgh->tv_sec = htonl(timestamp.tv_sec);
2638 pgh->tv_usec = htonl(timestamp.tv_usec);
2639}
2640
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002641static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2642 struct pktgen_dev *pkt_dev,
2643 unsigned int extralen)
2644{
2645 struct sk_buff *skb = NULL;
2646 unsigned int size = pkt_dev->cur_pkt_size + 64 + extralen +
2647 pkt_dev->pkt_overhead;
2648
2649 if (pkt_dev->flags & F_NODE) {
2650 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2651
2652 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2653 if (likely(skb)) {
2654 skb_reserve(skb, NET_SKB_PAD);
2655 skb->dev = dev;
2656 }
2657 } else {
2658 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2659 }
2660
2661 return skb;
2662}
2663
Luiz Capitulino222f1802006-03-20 22:16:13 -08002664static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2665 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
2667 struct sk_buff *skb = NULL;
2668 __u8 *eth;
2669 struct udphdr *udph;
2670 int datalen, iplen;
2671 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002672 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002673 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002674 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2675 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2676 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2677 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002678 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002679
2680 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002681 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002682
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002683 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002684 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002685
Robert Olsson64053be2005-06-26 15:27:10 -07002686 /* Update any of the values, used when we're incrementing various
2687 * fields.
2688 */
2689 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002690 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002691
David S. Miller7ac54592006-01-18 14:19:10 -08002692 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002693
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002694 skb = pktgen_alloc_skb(odev, pkt_dev, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (!skb) {
2696 sprintf(pkt_dev->result, "No memory");
2697 return NULL;
2698 }
2699
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002700 prefetchw(skb->data);
David S. Miller7ac54592006-01-18 14:19:10 -08002701 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 /* Reserve for ethernet and IP header */
2704 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002705 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2706 if (pkt_dev->nr_labels)
2707 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002708
2709 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002710 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002711 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002712 *svlan_tci = build_tci(pkt_dev->svlan_id,
2713 pkt_dev->svlan_cfi,
2714 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002715 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002716 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002717 }
2718 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002719 *vlan_tci = build_tci(pkt_dev->vlan_id,
2720 pkt_dev->vlan_cfi,
2721 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002722 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002723 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002724 }
2725
Thomas Graf525cebe2013-06-03 11:49:23 +00002726 skb_set_mac_header(skb, 0);
2727 skb_set_network_header(skb, skb->len);
2728 iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2729
2730 skb_set_transport_header(skb, skb->len);
2731 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002732 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002733 skb->priority = pkt_dev->skb_priority;
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002736 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002738 /* Eth + IPh + UDPh + mpls */
2739 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002740 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002741 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 udph->source = htons(pkt_dev->cur_udp_src);
2745 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002746 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002747 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 iph->ihl = 5;
2750 iph->version = 4;
2751 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002752 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002753 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 iph->saddr = pkt_dev->cur_saddr;
2755 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002756 iph->id = htons(pkt_dev->ip_id);
2757 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 iph->frag_off = 0;
2759 iplen = 20 + 8 + datalen;
2760 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002761 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002762 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 skb->dev = odev;
2764 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002765
2766 if (!(pkt_dev->flags & F_UDPCSUM)) {
2767 skb->ip_summed = CHECKSUM_NONE;
2768 } else if (odev->features & NETIF_F_V4_CSUM) {
2769 skb->ip_summed = CHECKSUM_PARTIAL;
2770 skb->csum = 0;
2771 udp4_hwcsum(skb, udph->source, udph->dest);
2772 } else {
2773 __wsum csum = udp_csum(skb);
2774
2775 /* add protocol-dependent pseudo-header */
2776 udph->check = csum_tcpudp_magic(udph->source, udph->dest,
2777 datalen + 8, IPPROTO_UDP, csum);
2778
2779 if (udph->check == 0)
2780 udph->check = CSUM_MANGLED_0;
2781 }
2782
Eric Dumazet26ad7872011-01-25 13:26:05 -08002783 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002784
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002785#ifdef CONFIG_XFRM
2786 if (!process_ipsec(pkt_dev, skb, protocol))
2787 return NULL;
2788#endif
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return skb;
2791}
2792
Luiz Capitulino222f1802006-03-20 22:16:13 -08002793static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2794 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
2796 struct sk_buff *skb = NULL;
2797 __u8 *eth;
2798 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002799 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002801 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002802 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002803 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2804 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2805 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2806 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002807 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002808
2809 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002810 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002811
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002812 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002813 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002814
Robert Olsson64053be2005-06-26 15:27:10 -07002815 /* Update any of the values, used when we're incrementing various
2816 * fields.
2817 */
2818 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002819 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002820
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002821 skb = pktgen_alloc_skb(odev, pkt_dev, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (!skb) {
2823 sprintf(pkt_dev->result, "No memory");
2824 return NULL;
2825 }
2826
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002827 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 skb_reserve(skb, 16);
2829
2830 /* Reserve for ethernet and IP header */
2831 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002832 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2833 if (pkt_dev->nr_labels)
2834 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002835
2836 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002837 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002838 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002839 *svlan_tci = build_tci(pkt_dev->svlan_id,
2840 pkt_dev->svlan_cfi,
2841 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002842 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002843 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002844 }
2845 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002846 *vlan_tci = build_tci(pkt_dev->vlan_id,
2847 pkt_dev->vlan_cfi,
2848 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002849 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002850 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002851 }
2852
Thomas Graf525cebe2013-06-03 11:49:23 +00002853 skb_set_mac_header(skb, 0);
2854 skb_set_network_header(skb, skb->len);
2855 iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2856
2857 skb_set_transport_header(skb, skb->len);
2858 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002859 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002860 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002863 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002864
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002865 /* Eth + IPh + UDPh + mpls */
2866 datalen = pkt_dev->cur_pkt_size - 14 -
2867 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002868 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002870 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002872 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
2874
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002875 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 udph->source = htons(pkt_dev->cur_udp_src);
2877 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002878 udph->len = htons(udplen);
2879 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002881 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002883 if (pkt_dev->traffic_class) {
2884 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002885 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002886 }
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 iph->hop_limit = 32;
2889
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002890 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 iph->nexthdr = IPPROTO_UDP;
2892
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002893 iph->daddr = pkt_dev->cur_in6_daddr;
2894 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002896 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 skb->dev = odev;
2898 skb->pkt_type = PACKET_HOST;
2899
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002900 if (!(pkt_dev->flags & F_UDPCSUM)) {
2901 skb->ip_summed = CHECKSUM_NONE;
2902 } else if (odev->features & NETIF_F_V6_CSUM) {
2903 skb->ip_summed = CHECKSUM_PARTIAL;
2904 skb->csum_start = skb_transport_header(skb) - skb->head;
2905 skb->csum_offset = offsetof(struct udphdr, check);
2906 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2907 } else {
2908 __wsum csum = udp_csum(skb);
2909
2910 /* add protocol-dependent pseudo-header */
2911 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2912
2913 if (udph->check == 0)
2914 udph->check = CSUM_MANGLED_0;
2915 }
2916
Eric Dumazet26ad7872011-01-25 13:26:05 -08002917 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return skb;
2920}
2921
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002922static struct sk_buff *fill_packet(struct net_device *odev,
2923 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002925 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return fill_packet_ipv6(odev, pkt_dev);
2927 else
2928 return fill_packet_ipv4(odev, pkt_dev);
2929}
2930
Luiz Capitulino222f1802006-03-20 22:16:13 -08002931static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002933 pkt_dev->seq_num = 1;
2934 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002936 pkt_dev->tx_bytes = 0;
2937 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
2940/* Set up structure for sending pkts, clear counters */
2941
2942static void pktgen_run(struct pktgen_thread *t)
2943{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08002944 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 int started = 0;
2946
Joe Perchesf9467ea2010-06-21 12:29:14 +00002947 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08002950 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952 /*
2953 * setup odev and create initial packet.
2954 */
2955 pktgen_setup_inject(pkt_dev);
2956
Luiz Capitulino222f1802006-03-20 22:16:13 -08002957 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002959 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00002961 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002962
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002963 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 strcpy(pkt_dev->result, "Starting");
2966 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002967 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 strcpy(pkt_dev->result, "Error starting");
2969 }
2970 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002971 if (started)
2972 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
Cong Wang4e58a022013-01-28 19:55:53 +00002975static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002977 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Joe Perchesf9467ea2010-06-21 12:29:14 +00002979 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002981 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002982
Cong Wang4e58a022013-01-28 19:55:53 +00002983 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002984 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002985
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002986 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
Stephen Hemminger648fda72009-08-27 13:55:07 +00002989static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002991 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08002993 list_for_each_entry(pkt_dev, &t->if_list, list)
Stephen Hemminger648fda72009-08-27 13:55:07 +00002994 if (pkt_dev->running)
2995 return 1;
2996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
Luiz Capitulino222f1802006-03-20 22:16:13 -08002999static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003001 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Luiz Capitulino222f1802006-03-20 22:16:13 -08003003 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Luiz Capitulino222f1802006-03-20 22:16:13 -08003007 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Luiz Capitulino222f1802006-03-20 22:16:13 -08003009 if (signal_pending(current))
3010 goto signal;
3011 if_lock(t);
3012 }
3013 if_unlock(t);
3014 return 1;
3015signal:
3016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
Cong Wang4e58a022013-01-28 19:55:53 +00003019static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003021 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003023
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003024 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003025
Cong Wang4e58a022013-01-28 19:55:53 +00003026 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003028 if (sig == 0)
3029 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003031
3032 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003033 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003035
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003036 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return sig;
3038}
3039
Cong Wang4e58a022013-01-28 19:55:53 +00003040static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003042 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Joe Perchesf9467ea2010-06-21 12:29:14 +00003044 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003046 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Cong Wang4e58a022013-01-28 19:55:53 +00003048 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003050
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003051 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003053 /* Propagate thread->control */
3054 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003055
Cong Wang4e58a022013-01-28 19:55:53 +00003056 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
Cong Wang4e58a022013-01-28 19:55:53 +00003059static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003060{
3061 struct pktgen_thread *t;
3062
Joe Perchesf9467ea2010-06-21 12:29:14 +00003063 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003064
3065 mutex_lock(&pktgen_thread_lock);
3066
Cong Wang4e58a022013-01-28 19:55:53 +00003067 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003068 t->control |= (T_REMDEVALL);
3069
3070 mutex_unlock(&pktgen_thread_lock);
3071
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003072 /* Propagate thread->control */
3073 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003074
Cong Wang4e58a022013-01-28 19:55:53 +00003075 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003076}
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3079{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003080 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003081 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003082 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3083 pkt_dev->started_at);
3084 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Daniel Turull03a14ab2011-03-09 14:11:00 -08003086 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003087 (unsigned long long)ktime_to_us(elapsed),
3088 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3089 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003090 (unsigned long long)pkt_dev->sofar,
3091 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003093 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3094 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Luiz Capitulino222f1802006-03-20 22:16:13 -08003096 bps = pps * 8 * pkt_dev->cur_pkt_size;
3097
3098 mbps = bps;
3099 do_div(mbps, 1000000);
3100 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3101 (unsigned long long)pps,
3102 (unsigned long long)mbps,
3103 (unsigned long long)bps,
3104 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003108static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003110 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003111
Luiz Capitulino222f1802006-03-20 22:16:13 -08003112 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003113 pr_warning("interface: %s is already stopped\n",
3114 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003115 return -EINVAL;
3116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003118 kfree_skb(pkt_dev->skb);
3119 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003120 pkt_dev->stopped_at = ktime_get();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003121 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003123 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Luiz Capitulino222f1802006-03-20 22:16:13 -08003125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
Luiz Capitulino222f1802006-03-20 22:16:13 -08003128static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003130 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if_lock(t);
3133
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003134 list_for_each_entry(pkt_dev, &t->if_list, list) {
3135 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003136 continue;
3137 if (best == NULL)
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003138 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003139 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003140 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
3142 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003143 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Luiz Capitulino222f1802006-03-20 22:16:13 -08003146static void pktgen_stop(struct pktgen_thread *t)
3147{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003148 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Joe Perchesf9467ea2010-06-21 12:29:14 +00003150 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003154 list_for_each_entry(pkt_dev, &t->if_list, list) {
3155 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003161/*
3162 * one of our devices needs to be removed - find it
3163 * and remove it
3164 */
3165static void pktgen_rem_one_if(struct pktgen_thread *t)
3166{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003167 struct list_head *q, *n;
3168 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003169
Joe Perchesf9467ea2010-06-21 12:29:14 +00003170 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003171
3172 if_lock(t);
3173
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003174 list_for_each_safe(q, n, &t->if_list) {
3175 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003176
Luiz Capitulino222f1802006-03-20 22:16:13 -08003177 if (!cur->removal_mark)
3178 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003179
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003180 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003181 cur->skb = NULL;
3182
3183 pktgen_remove_device(t, cur);
3184
3185 break;
3186 }
3187
3188 if_unlock(t);
3189}
3190
Luiz Capitulino222f1802006-03-20 22:16:13 -08003191static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003193 struct list_head *q, *n;
3194 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195
Joe Perchesf9467ea2010-06-21 12:29:14 +00003196 func_enter();
3197
Luiz Capitulino222f1802006-03-20 22:16:13 -08003198 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003199
Luiz Capitulino222f1802006-03-20 22:16:13 -08003200 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003202 list_for_each_safe(q, n, &t->if_list) {
3203 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003204
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003205 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003206 cur->skb = NULL;
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 pktgen_remove_device(t, cur);
3209 }
3210
Luiz Capitulino222f1802006-03-20 22:16:13 -08003211 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
Luiz Capitulino222f1802006-03-20 22:16:13 -08003214static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003216 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003217 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
Stephen Hemmingeref879792009-09-22 19:41:43 +00003220static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003221{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003222 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003223 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003224 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003225}
3226
Stephen Hemmingeref879792009-09-22 19:41:43 +00003227static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3228{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003229 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003230
3231 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3232 if (signal_pending(current))
3233 break;
3234
3235 if (need_resched())
3236 pktgen_resched(pkt_dev);
3237 else
3238 cpu_relax();
3239 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003240 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003241}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003242
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003243static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Stephen Hemminger00829822008-11-20 20:14:53 -08003245 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003246 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003247 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003248 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003249 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 int ret;
3251
Stephen Hemmingeref879792009-09-22 19:41:43 +00003252 /* If device is offline, then don't send */
3253 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3254 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003255 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003257
Stephen Hemmingeref879792009-09-22 19:41:43 +00003258 /* This is max DELAY, this has special meaning of
3259 * "never transmit"
3260 */
3261 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003262 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003263 return;
3264 }
3265
3266 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003267 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3268 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3269 /* build a new pkt */
3270 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003271
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003272 pkt_dev->skb = fill_packet(odev, pkt_dev);
3273 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003274 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003275 schedule();
3276 pkt_dev->clone_count--; /* back out increment, OOM */
3277 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003279 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003280 pkt_dev->allocated_skbs++;
3281 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003283
Stephen Hemmingeref879792009-09-22 19:41:43 +00003284 if (pkt_dev->delay && pkt_dev->last_ok)
3285 spin(pkt_dev, pkt_dev->next_tx);
3286
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003287 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3288 txq = netdev_get_tx_queue(odev, queue_map);
3289
3290 __netif_tx_lock_bh(txq);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003291
Tom Herbert734664982011-11-28 16:32:44 +00003292 if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003293 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003294 pkt_dev->last_ok = 0;
3295 goto unlock;
3296 }
3297 atomic_inc(&(pkt_dev->skb->users));
3298 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Stephen Hemmingeref879792009-09-22 19:41:43 +00003300 switch (ret) {
3301 case NETDEV_TX_OK:
3302 txq_trans_update(txq);
3303 pkt_dev->last_ok = 1;
3304 pkt_dev->sofar++;
3305 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003306 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003307 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003308 case NET_XMIT_DROP:
3309 case NET_XMIT_CN:
3310 case NET_XMIT_POLICED:
3311 /* skb has been consumed */
3312 pkt_dev->errors++;
3313 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003314 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003315 net_info_ratelimited("%s xmit error: %d\n",
3316 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003317 pkt_dev->errors++;
3318 /* fallthru */
3319 case NETDEV_TX_LOCKED:
3320 case NETDEV_TX_BUSY:
3321 /* Retry it next time */
3322 atomic_dec(&(pkt_dev->skb->users));
3323 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003324 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003325unlock:
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003326 __netif_tx_unlock_bh(txq);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 /* If pkt_dev->count is zero, then run forever */
3329 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003330 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 /* Done with this */
3333 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003334 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003335}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003337/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 * Main loop of the thread goes here
3339 */
3340
David S. Milleree74baa2007-01-01 20:51:53 -08003341static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
3343 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003344 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003345 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
David S. Milleree74baa2007-01-01 20:51:53 -08003348 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003351 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Joe Perchesf9467ea2010-06-21 12:29:14 +00003353 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
David S. Milleree74baa2007-01-01 20:51:53 -08003355 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003356
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003357 set_freezable();
3358
David S. Milleree74baa2007-01-01 20:51:53 -08003359 while (!kthread_should_stop()) {
3360 pkt_dev = next_to_run(t);
3361
Stephen Hemmingeref879792009-09-22 19:41:43 +00003362 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003363 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003364 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003365 wait_event_interruptible_timeout(t->queue,
3366 t->control != 0,
3367 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003368 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003369 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003370 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 __set_current_state(TASK_RUNNING);
3373
Stephen Hemmingeref879792009-09-22 19:41:43 +00003374 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 pktgen_xmit(pkt_dev);
3376
Stephen Hemmingeref879792009-09-22 19:41:43 +00003377 if (need_resched())
3378 pktgen_resched(pkt_dev);
3379 else
3380 cpu_relax();
3381 }
3382
Luiz Capitulino222f1802006-03-20 22:16:13 -08003383 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 pktgen_stop(t);
3385 t->control &= ~(T_STOP);
3386 }
3387
Luiz Capitulino222f1802006-03-20 22:16:13 -08003388 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 pktgen_run(t);
3390 t->control &= ~(T_RUN);
3391 }
3392
Luiz Capitulino222f1802006-03-20 22:16:13 -08003393 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003395 t->control &= ~(T_REMDEVALL);
3396 }
3397
Luiz Capitulino222f1802006-03-20 22:16:13 -08003398 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003399 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 t->control &= ~(T_REMDEV);
3401 }
3402
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003403 try_to_freeze();
3404
David S. Milleree74baa2007-01-01 20:51:53 -08003405 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Joe Perchesf9467ea2010-06-21 12:29:14 +00003408 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003409 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Joe Perchesf9467ea2010-06-21 12:29:14 +00003411 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003412 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Joe Perchesf9467ea2010-06-21 12:29:14 +00003414 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003415 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003416
Eric Dumazet551eaff2010-11-21 10:26:44 -08003417 /* Wait for kthread_stop */
3418 while (!kthread_should_stop()) {
3419 set_current_state(TASK_INTERRUPTIBLE);
3420 schedule();
3421 }
3422 __set_current_state(TASK_RUNNING);
3423
David S. Milleree74baa2007-01-01 20:51:53 -08003424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
Luiz Capitulino222f1802006-03-20 22:16:13 -08003427static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003428 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003430 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003431 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Eric Dumazet3e984842009-11-24 14:50:53 -08003433 if_lock(t);
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003434 list_for_each_entry(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003435 if (strncmp(p->odevname, ifname, len) == 0) {
3436 if (p->odevname[len]) {
3437 if (exact || p->odevname[len] != '@')
3438 continue;
3439 }
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003440 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003441 break;
3442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Luiz Capitulino222f1802006-03-20 22:16:13 -08003444 if_unlock(t);
Joe Perchesf9467ea2010-06-21 12:29:14 +00003445 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003446 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003449/*
3450 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 */
3452
Luiz Capitulino222f1802006-03-20 22:16:13 -08003453static int add_dev_to_thread(struct pktgen_thread *t,
3454 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
3456 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Luiz Capitulino222f1802006-03-20 22:16:13 -08003458 if_lock(t);
3459
3460 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003461 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003462 rv = -EBUSY;
3463 goto out;
3464 }
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003465
3466 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003467 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 pkt_dev->running = 0;
3469
Luiz Capitulino222f1802006-03-20 22:16:13 -08003470out:
3471 if_unlock(t);
3472 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
3475/* Called under thread lock */
3476
Luiz Capitulino222f1802006-03-20 22:16:13 -08003477static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003479 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003480 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003481 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 /* We don't allow a device to be on several threads */
3484
Cong Wang4e58a022013-01-28 19:55:53 +00003485 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003486 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003487 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003488 return -EBUSY;
3489 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003490
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003491 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003492 if (!pkt_dev)
3493 return -ENOMEM;
3494
Eric Dumazet593f63b2009-11-23 01:44:37 +00003495 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003496 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003497 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003498 if (pkt_dev->flows == NULL) {
3499 kfree(pkt_dev);
3500 return -ENOMEM;
3501 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003502
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003503 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003504 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003505 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003506 pkt_dev->count = pg_count_d;
3507 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003508 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003509 pkt_dev->udp_src_max = 9;
3510 pkt_dev->udp_dst_min = 9;
3511 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003512 pkt_dev->vlan_p = 0;
3513 pkt_dev->vlan_cfi = 0;
3514 pkt_dev->vlan_id = 0xffff;
3515 pkt_dev->svlan_p = 0;
3516 pkt_dev->svlan_cfi = 0;
3517 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003518 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003519
Cong Wang4e58a022013-01-28 19:55:53 +00003520 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003521 if (err)
3522 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003523 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3524 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003525
Cong Wang4e58a022013-01-28 19:55:53 +00003526 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003527 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003528 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003529 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003530 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003531 err = -EINVAL;
3532 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003533 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003534#ifdef CONFIG_XFRM
3535 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3536 pkt_dev->ipsproto = IPPROTO_ESP;
3537#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003538
3539 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003540out2:
3541 dev_put(pkt_dev->odev);
3542out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003543#ifdef CONFIG_XFRM
3544 free_SAs(pkt_dev);
3545#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003546 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003547 kfree(pkt_dev);
3548 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
Cong Wang4e58a022013-01-28 19:55:53 +00003551static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003553 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003554 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003555 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003557 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3558 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003559 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003560 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003561 return -ENOMEM;
3562 }
3563
Luiz Capitulino222f1802006-03-20 22:16:13 -08003564 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003566
David S. Milleree74baa2007-01-01 20:51:53 -08003567 INIT_LIST_HEAD(&t->if_list);
3568
Cong Wang4e58a022013-01-28 19:55:53 +00003569 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003570 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003571
Eric Dumazet94dcf292011-03-22 16:30:45 -07003572 p = kthread_create_on_node(pktgen_thread_worker,
3573 t,
3574 cpu_to_node(cpu),
3575 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003576 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003577 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003578 list_del(&t->th_list);
3579 kfree(t);
3580 return PTR_ERR(p);
3581 }
3582 kthread_bind(p, cpu);
3583 t->tsk = p;
3584
Cong Wang4e58a022013-01-28 19:55:53 +00003585 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003586 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003587 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003588 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003589 PG_PROC_DIR, t->tsk->comm);
3590 kthread_stop(p);
3591 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003592 kfree(t);
3593 return -EINVAL;
3594 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003595
Cong Wang4e58a022013-01-28 19:55:53 +00003596 t->net = pn;
David S. Milleree74baa2007-01-01 20:51:53 -08003597 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003598 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
3600 return 0;
3601}
3602
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003603/*
3604 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003606static void _rem_dev_from_if_list(struct pktgen_thread *t,
3607 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003609 struct list_head *q, *n;
3610 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Luiz Capitulinoc26a80162006-03-20 22:18:16 -08003612 list_for_each_safe(q, n, &t->if_list) {
3613 p = list_entry(q, struct pktgen_dev, list);
3614 if (p == pkt_dev)
3615 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
3617}
3618
Luiz Capitulino222f1802006-03-20 22:16:13 -08003619static int pktgen_remove_device(struct pktgen_thread *t,
3620 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003622 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Luiz Capitulino222f1802006-03-20 22:16:13 -08003624 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003625 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003626 pktgen_stop_device(pkt_dev);
3627 }
3628
3629 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
3631 if (pkt_dev->odev) {
3632 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003633 pkt_dev->odev = NULL;
3634 }
3635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 /* And update the thread if_list */
3637
3638 _rem_dev_from_if_list(t, pkt_dev);
3639
Stephen Hemminger39df2322007-03-04 16:11:51 -08003640 if (pkt_dev->entry)
David Howellsa8ca16e2013-04-12 17:27:28 +01003641 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003643#ifdef CONFIG_XFRM
3644 free_SAs(pkt_dev);
3645#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003646 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003647 if (pkt_dev->page)
3648 put_page(pkt_dev->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
3652
Cong Wang4e58a022013-01-28 19:55:53 +00003653static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
Cong Wang4e58a022013-01-28 19:55:53 +00003655 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003656 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003657 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003658
Cong Wang4e58a022013-01-28 19:55:53 +00003659 pn->net = net;
3660 INIT_LIST_HEAD(&pn->pktgen_threads);
3661 pn->pktgen_exiting = false;
3662 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3663 if (!pn->proc_dir) {
3664 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003665 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003666 }
Cong Wang4e58a022013-01-28 19:55:53 +00003667 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3668 if (pe == NULL) {
3669 pr_err("cannot create %s procfs entry\n", PGCTRL);
3670 ret = -EINVAL;
3671 goto remove;
3672 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003673
John Hawkes670c02c2005-10-13 09:30:31 -07003674 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003675 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Cong Wang4e58a022013-01-28 19:55:53 +00003677 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003678 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003679 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003680 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003681 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003682
Cong Wang4e58a022013-01-28 19:55:53 +00003683 if (list_empty(&pn->pktgen_threads)) {
3684 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003685 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003686 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003687 }
3688
Luiz Capitulino222f1802006-03-20 22:16:13 -08003689 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003690
Cong Wang4e58a022013-01-28 19:55:53 +00003691remove_entry:
3692 remove_proc_entry(PGCTRL, pn->proc_dir);
3693remove:
Gao fengece31ff2013-02-18 01:34:56 +00003694 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003695 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696}
3697
Cong Wang4e58a022013-01-28 19:55:53 +00003698static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
Cong Wang4e58a022013-01-28 19:55:53 +00003700 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003701 struct pktgen_thread *t;
3702 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003703 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Luiz Capitulino222f1802006-03-20 22:16:13 -08003705 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003706 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Eric Dumazetc57b5462012-05-09 13:29:51 +00003708 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003709 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003710 mutex_unlock(&pktgen_thread_lock);
3711
3712 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003713 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003714 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003715 kthread_stop(t->tsk);
3716 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Cong Wang4e58a022013-01-28 19:55:53 +00003719 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003720 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Cong Wang4e58a022013-01-28 19:55:53 +00003723static struct pernet_operations pg_net_ops = {
3724 .init = pg_net_init,
3725 .exit = pg_net_exit,
3726 .id = &pg_net_id,
3727 .size = sizeof(struct pktgen_net),
3728};
3729
3730static int __init pg_init(void)
3731{
3732 int ret = 0;
3733
3734 pr_info("%s", version);
3735 ret = register_pernet_subsys(&pg_net_ops);
3736 if (ret)
3737 return ret;
3738 ret = register_netdevice_notifier(&pktgen_notifier_block);
3739 if (ret)
3740 unregister_pernet_subsys(&pg_net_ops);
3741
3742 return ret;
3743}
3744
3745static void __exit pg_cleanup(void)
3746{
3747 unregister_netdevice_notifier(&pktgen_notifier_block);
3748 unregister_pernet_subsys(&pg_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749}
3750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751module_init(pg_init);
3752module_exit(pg_cleanup);
3753
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003754MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755MODULE_DESCRIPTION("Packet Generator tool");
3756MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003757MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003759MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003761MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003763MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003765MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");