blob: 5b05e364b8aedb24eb4936fb49e4c6a408596d15 [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
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +020072 * way.
73 * The if_list is RCU protected, and the if_lock remains to protect updating
74 * of if_list, from "add_device" as it invoked from userspace (via proc write).
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090076 * By design there should only be *one* "controlling" process. In practice
77 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070079 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090081 * Note when adding devices to a specific CPU there good idea to also assign
82 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * --ro
84 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090085 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * memleak 030710- KJP
87 *
88 * First "ranges" functionality for ipv6 030726 --ro
89 *
90 * Included flow support. 030802 ANK.
91 *
92 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090093 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
95 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090097 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * <shemminger@osdl.org> 040923
99 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900100 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
102 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
103 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900105 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900108 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800110 *
111 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700113 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 *
Adit Ranadivece5d0b42007-09-16 14:52:15 -0700115 * Fixed src_mac command to set source mac of packet to value specified in
116 * command by Adit Ranadive <adit.262@gmail.com>
117 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
Joe Perchesf9467ea2010-06-21 12:29:14 +0000119
120#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <linux/sys.h>
123#include <linux/types.h>
124#include <linux/module.h>
125#include <linux/moduleparam.h>
126#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800127#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#include <linux/sched.h>
129#include <linux/slab.h>
130#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#include <linux/unistd.h>
132#include <linux/string.h>
133#include <linux/ptrace.h>
134#include <linux/errno.h>
135#include <linux/ioport.h>
136#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800137#include <linux/capability.h>
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700138#include <linux/hrtimer.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700139#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#include <linux/delay.h>
141#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800142#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#include <linux/init.h>
144#include <linux/skbuff.h>
145#include <linux/netdevice.h>
146#include <linux/inet.h>
147#include <linux/inetdevice.h>
148#include <linux/rtnetlink.h>
149#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700150#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#include <linux/in.h>
152#include <linux/ip.h>
153#include <linux/ipv6.h>
154#include <linux/udp.h>
155#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700156#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800158#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800159#include <linux/kthread.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700160#include <linux/prefetch.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200161#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#include <net/checksum.h>
163#include <net/ipv6.h>
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200164#include <net/udp.h>
Stephen Rothwell73d94e92013-07-29 16:21:26 +1000165#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#include <net/addrconf.h>
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700167#ifdef CONFIG_XFRM
168#include <net/xfrm.h>
169#endif
Cong Wang4e58a022013-01-28 19:55:53 +0000170#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#include <asm/byteorder.h>
172#include <linux/rcupdate.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700173#include <linux/bitops.h>
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000174#include <linux/io.h>
175#include <linux/timex.h>
176#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#include <asm/dma.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800178#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Daniel Turull43d28b62010-06-09 22:49:57 +0000180#define VERSION "2.74"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800182#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800183#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Joe Perchesf9467ea2010-06-21 12:29:14 +0000185#define func_enter() pr_debug("entering %s\n", __func__);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800188#define F_IPSRC_RND (1<<0) /* IP-Src Random */
189#define F_IPDST_RND (1<<1) /* IP-Dst Random */
190#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
191#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
192#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
193#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
194#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
195#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800196#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700197#define F_VID_RND (1<<9) /* Random VLAN ID */
198#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700199#define F_FLOW_SEQ (1<<11) /* Sequential flows */
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700200#define F_IPSEC_ON (1<<12) /* ipsec on for flows */
Robert Olsson45b270f2007-08-28 15:45:55 -0700201#define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
Robert Olssone6fce5b2008-08-07 02:23:01 -0700202#define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
Robert Olssone99b99b2010-03-18 22:44:30 +0000203#define F_NODE (1<<15) /* Node memory alloc*/
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200204#define F_UDPCSUM (1<<16) /* Include UDP checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/* Thread control flag bits */
Stephen Hemminger6b80d6a2009-09-22 19:41:42 +0000207#define T_STOP (1<<0) /* Stop run */
208#define T_RUN (1<<1) /* Start run */
209#define T_REMDEVALL (1<<2) /* Remove all devs */
210#define T_REMDEV (1<<3) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200212/* If lock -- protects updating of if_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define if_lock(t) spin_lock(&(t->if_lock));
214#define if_unlock(t) spin_unlock(&(t->if_lock));
215
216/* Used to help with determining the pkts on receive */
217#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700218#define PG_PROC_DIR "pktgen"
219#define PGCTRL "pgctrl"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221#define MAX_CFLOWS 65536
222
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700223#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
224#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
225
Luiz Capitulino222f1802006-03-20 22:16:13 -0800226struct flow_state {
Al Viro252e33462006-11-14 20:48:11 -0800227 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800228 int count;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700229#ifdef CONFIG_XFRM
230 struct xfrm_state *x;
231#endif
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700232 __u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700235/* flow flag bits */
236#define F_INIT (1<<0) /* flow has been initialized */
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /*
240 * Try to keep frequent/infrequent used vars. separated.
241 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800242 struct proc_dir_entry *entry; /* proc file */
243 struct pktgen_thread *pg_thread;/* the owner */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000244 struct list_head list; /* chaining in the thread's run-queue */
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +0200245 struct rcu_head rcu; /* freed by RCU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000247 int running; /* if false, the test will stop */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800248
249 /* If min != max, then we will either do a linear iteration, or
250 * we will do a random selection from within the range.
251 */
252 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800253 int removal_mark; /* non-zero => the device is marked for
254 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Amerigo Wang68bf9f02012-10-09 17:48:17 +0000256 int min_pkt_size;
257 int max_pkt_size;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -0700258 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800259 int nfrags;
Eric Dumazet26ad7872011-01-25 13:26:05 -0800260 struct page *page;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000261 u64 delay; /* nano-seconds */
262
Luiz Capitulino222f1802006-03-20 22:16:13 -0800263 __u64 count; /* Default No packets to send */
264 __u64 sofar; /* How many pkts we've sent so far */
265 __u64 tx_bytes; /* How many bytes we've transmitted */
John Fastabendf466dba2009-12-23 22:02:57 -0800266 __u64 errors; /* Errors when trying to transmit, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Luiz Capitulino222f1802006-03-20 22:16:13 -0800268 /* runtime counters relating to clone_skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Luiz Capitulino222f1802006-03-20 22:16:13 -0800270 __u64 allocated_skbs;
271 __u32 clone_count;
272 int last_ok; /* Was last skb sent?
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000273 * Or a failed transmit of some sort?
274 * This will keep sequence numbers in order
Luiz Capitulino222f1802006-03-20 22:16:13 -0800275 */
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000276 ktime_t next_tx;
277 ktime_t started_at;
278 ktime_t stopped_at;
279 u64 idle_acc; /* nano-seconds */
280
Luiz Capitulino222f1802006-03-20 22:16:13 -0800281 __u32 seq_num;
282
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000283 int clone_skb; /*
284 * Use multiple SKBs during packet gen.
285 * If this number is greater than 1, then
286 * that many copies of the same packet will be
287 * sent before a new packet is allocated.
288 * If you want to send 1024 identical packets
289 * before creating a new packet,
290 * set clone_skb to 1024.
Luiz Capitulino222f1802006-03-20 22:16:13 -0800291 */
292
293 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
294 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
295 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
296 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
297
298 struct in6_addr in6_saddr;
299 struct in6_addr in6_daddr;
300 struct in6_addr cur_in6_daddr;
301 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800303 struct in6_addr min_in6_daddr;
304 struct in6_addr max_in6_daddr;
305 struct in6_addr min_in6_saddr;
306 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Luiz Capitulino222f1802006-03-20 22:16:13 -0800308 /* If we're doing ranges, random or incremental, then this
309 * defines the min/max for those ranges.
310 */
Al Viro252e33462006-11-14 20:48:11 -0800311 __be32 saddr_min; /* inclusive, source IP address */
312 __be32 saddr_max; /* exclusive, source IP address */
313 __be32 daddr_min; /* inclusive, dest IP address */
314 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Luiz Capitulino222f1802006-03-20 22:16:13 -0800316 __u16 udp_src_min; /* inclusive, source UDP port */
317 __u16 udp_src_max; /* exclusive, source UDP port */
318 __u16 udp_dst_min; /* inclusive, dest UDP port */
319 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700321 /* DSCP + ECN */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000322 __u8 tos; /* six MSB of (former) IPv4 TOS
323 are for dscp codepoint */
324 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
325 (see RFC 3260, sec. 4) */
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700326
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800327 /* MPLS */
Eric Dumazet95c96172012-04-15 05:58:06 +0000328 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800329 __be32 labels[MAX_MPLS_LABELS];
330
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700331 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
332 __u8 vlan_p;
333 __u8 vlan_cfi;
334 __u16 vlan_id; /* 0xffff means no vlan tag */
335
336 __u8 svlan_p;
337 __u8 svlan_cfi;
338 __u16 svlan_id; /* 0xffff means no svlan tag */
339
Luiz Capitulino222f1802006-03-20 22:16:13 -0800340 __u32 src_mac_count; /* How many MACs to iterate through */
341 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Luiz Capitulino222f1802006-03-20 22:16:13 -0800343 unsigned char dst_mac[ETH_ALEN];
344 unsigned char src_mac[ETH_ALEN];
345
346 __u32 cur_dst_mac_offset;
347 __u32 cur_src_mac_offset;
Al Viro252e33462006-11-14 20:48:11 -0800348 __be32 cur_saddr;
349 __be32 cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -0700350 __u16 ip_id;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800351 __u16 cur_udp_dst;
352 __u16 cur_udp_src;
Robert Olsson45b270f2007-08-28 15:45:55 -0700353 __u16 cur_queue_map;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800354 __u32 cur_pkt_size;
Eric Dumazetbaac8562009-11-05 21:04:32 -0800355 __u32 last_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800356
357 __u8 hh[14];
358 /* = {
359 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
360
361 We fill in SRC address later
362 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
363 0x08, 0x00
364 };
365 */
366 __u16 pad; /* pad out the hh struct to an even 16 bytes */
367
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000368 struct sk_buff *skb; /* skb we are to transmit next, used for when we
Luiz Capitulino222f1802006-03-20 22:16:13 -0800369 * are transmitting the same one multiple times
370 */
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000371 struct net_device *odev; /* The out-going device.
372 * Note that the device should have it's
373 * pg_info pointer pointing back to this
374 * device.
375 * Set when the user specifies the out-going
376 * device name (not when the inject is
377 * started as it used to do.)
378 */
Eric Dumazet593f63b2009-11-23 01:44:37 +0000379 char odevname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 struct flow_state *flows;
Eric Dumazet95c96172012-04-15 05:58:06 +0000381 unsigned int cflows; /* Concurrent flows (config) */
382 unsigned int lflow; /* Flow length (config) */
383 unsigned int nflows; /* accumulated flows (stats) */
384 unsigned int curfl; /* current sequenced flow (state)*/
Robert Olsson45b270f2007-08-28 15:45:55 -0700385
386 u16 queue_map_min;
387 u16 queue_map_max;
John Fastabend9e50e3a2010-11-16 19:12:28 +0000388 __u32 skb_priority; /* skb priority field */
Robert Olssone99b99b2010-03-18 22:44:30 +0000389 int node; /* Memory node */
Robert Olsson45b270f2007-08-28 15:45:55 -0700390
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700391#ifdef CONFIG_XFRM
392 __u8 ipsmode; /* IPSEC mode (config) */
393 __u8 ipsproto; /* IPSEC type (config) */
Fan Dude4aee72014-01-03 11:18:30 +0800394 __u32 spi;
Fan Ducf93d47e2014-01-03 11:18:31 +0800395 struct dst_entry dst;
396 struct dst_ops dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700397#endif
Stephen Hemminger39df2322007-03-04 16:11:51 -0800398 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
400
401struct pktgen_hdr {
Al Viro252e33462006-11-14 20:48:11 -0800402 __be32 pgh_magic;
403 __be32 seq_num;
404 __be32 tv_sec;
405 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406};
407
Cong Wang4e58a022013-01-28 19:55:53 +0000408
409static int pg_net_id __read_mostly;
410
411struct pktgen_net {
412 struct net *net;
413 struct proc_dir_entry *proc_dir;
414 struct list_head pktgen_threads;
415 bool pktgen_exiting;
416};
Eric Dumazet551eaff2010-11-21 10:26:44 -0800417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418struct pktgen_thread {
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000419 spinlock_t if_lock; /* for list of devices */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800420 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800421 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800422 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800423 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000425 /* Field for thread to receive "posted" events terminate,
426 stop ifs etc. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800427
428 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int cpu;
430
Luiz Capitulino222f1802006-03-20 22:16:13 -0800431 wait_queue_head_t queue;
Denis V. Lunevd3ede322008-05-20 15:12:44 -0700432 struct completion start_done;
Cong Wang4e58a022013-01-28 19:55:53 +0000433 struct pktgen_net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434};
435
436#define REMOVE 1
437#define FIND 0
438
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000439static const char version[] =
Joe Perchesf9467ea2010-06-21 12:29:14 +0000440 "Packet Generator for packet performance testing. "
441 "Version: " VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Luiz Capitulino222f1802006-03-20 22:16:13 -0800443static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
444static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
445static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -0800446 const char *ifname, bool exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
Cong Wang4e58a022013-01-28 19:55:53 +0000448static void pktgen_run_all_threads(struct pktgen_net *pn);
449static void pktgen_reset_all_threads(struct pktgen_net *pn);
450static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
Stephen Hemminger3bda06a2009-08-27 13:55:10 +0000451
Luiz Capitulino222f1802006-03-20 22:16:13 -0800452static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/* Module parameters, defaults. */
Stephen Hemminger65c5b782009-08-27 13:55:09 +0000456static int pg_count_d __read_mostly = 1000;
457static int pg_delay_d __read_mostly;
458static int pg_clone_skb_d __read_mostly;
459static int debug __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Luiz Capitulino222fa072006-03-20 22:24:27 -0800461static DEFINE_MUTEX(pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static struct notifier_block pktgen_notifier_block = {
464 .notifier_call = pktgen_device_event,
465};
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900468 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
470 */
471
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700472static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800473{
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +0000474 seq_puts(seq, version);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000478static ssize_t pgctrl_write(struct file *file, const char __user *buf,
479 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700481 char data[128];
Cong Wang4e58a022013-01-28 19:55:53 +0000482 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Mathias Krause09455742014-02-21 21:38:35 +0100484 if (!capable(CAP_NET_ADMIN))
485 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Mathias Krause20b0c712014-02-21 21:38:34 +0100487 if (count == 0)
488 return -EINVAL;
489
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700490 if (count > sizeof(data))
491 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Mathias Krause09455742014-02-21 21:38:35 +0100493 if (copy_from_user(data, buf, count))
494 return -EFAULT;
495
Mathias Krause20b0c712014-02-21 21:38:34 +0100496 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498 if (!strcmp(data, "stop"))
Cong Wang4e58a022013-01-28 19:55:53 +0000499 pktgen_stop_all_threads_ifs(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Luiz Capitulino222f1802006-03-20 22:16:13 -0800501 else if (!strcmp(data, "start"))
Cong Wang4e58a022013-01-28 19:55:53 +0000502 pktgen_run_all_threads(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800504 else if (!strcmp(data, "reset"))
Cong Wang4e58a022013-01-28 19:55:53 +0000505 pktgen_reset_all_threads(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -0800506
Luiz Capitulino222f1802006-03-20 22:16:13 -0800507 else
Joe Perchesf9467ea2010-06-21 12:29:14 +0000508 pr_warning("Unknown command: %s\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Mathias Krause09455742014-02-21 21:38:35 +0100510 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700513static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Al Virod9dda782013-03-31 18:16:14 -0400515 return single_open(file, pgctrl_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700516}
517
Arjan van de Ven9a321442007-02-12 00:55:35 -0800518static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800519 .owner = THIS_MODULE,
520 .open = pgctrl_open,
521 .read = seq_read,
522 .llseek = seq_lseek,
523 .write = pgctrl_write,
524 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700525};
526
527static int pktgen_if_show(struct seq_file *seq, void *v)
528{
Stephen Hemminger648fda72009-08-27 13:55:07 +0000529 const struct pktgen_dev *pkt_dev = seq->private;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000530 ktime_t stopped;
531 u64 idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Luiz Capitulino222f1802006-03-20 22:16:13 -0800533 seq_printf(seq,
534 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
535 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
536 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Luiz Capitulino222f1802006-03-20 22:16:13 -0800538 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000539 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
540 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
Eric Dumazet593f63b2009-11-23 01:44:37 +0000541 pkt_dev->clone_skb, pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Luiz Capitulino222f1802006-03-20 22:16:13 -0800543 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
544 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Robert Olsson45b270f2007-08-28 15:45:55 -0700546 seq_printf(seq,
547 " queue_map_min: %u queue_map_max: %u\n",
548 pkt_dev->queue_map_min,
549 pkt_dev->queue_map_max);
550
John Fastabend9e50e3a2010-11-16 19:12:28 +0000551 if (pkt_dev->skb_priority)
552 seq_printf(seq, " skb_priority: %u\n",
553 pkt_dev->skb_priority);
554
Luiz Capitulino222f1802006-03-20 22:16:13 -0800555 if (pkt_dev->flags & F_IPV6) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800556 seq_printf(seq,
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000557 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
558 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
559 &pkt_dev->in6_saddr,
560 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
561 &pkt_dev->in6_daddr,
562 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000563 } else {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800564 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000565 " dst_min: %s dst_max: %s\n",
566 pkt_dev->dst_min, pkt_dev->dst_max);
567 seq_printf(seq,
568 " src_min: %s src_max: %s\n",
569 pkt_dev->src_min, pkt_dev->src_max);
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700572 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Johannes Berge1749612008-10-27 15:59:26 -0700574 seq_printf(seq, "%pM ",
575 is_zero_ether_addr(pkt_dev->src_mac) ?
576 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Thomas Graf97dc48e2014-05-16 23:28:54 +0200578 seq_puts(seq, "dst_mac: ");
Johannes Berge1749612008-10-27 15:59:26 -0700579 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Luiz Capitulino222f1802006-03-20 22:16:13 -0800581 seq_printf(seq,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000582 " udp_src_min: %d udp_src_max: %d"
583 " udp_dst_min: %d udp_dst_max: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800584 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
585 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Luiz Capitulino222f1802006-03-20 22:16:13 -0800587 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800588 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700589 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800591 if (pkt_dev->nr_labels) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000592 unsigned int i;
Thomas Graf97dc48e2014-05-16 23:28:54 +0200593 seq_puts(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700594 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800595 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
596 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
597 }
598
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000599 if (pkt_dev->vlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700600 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000601 pkt_dev->vlan_id, pkt_dev->vlan_p,
602 pkt_dev->vlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700603
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000604 if (pkt_dev->svlan_id != 0xffff)
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700605 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000606 pkt_dev->svlan_id, pkt_dev->svlan_p,
607 pkt_dev->svlan_cfi);
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700608
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000609 if (pkt_dev->tos)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700610 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700611
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000612 if (pkt_dev->traffic_class)
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700613 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700614
Robert Olssone99b99b2010-03-18 22:44:30 +0000615 if (pkt_dev->node >= 0)
616 seq_printf(seq, " node: %d\n", pkt_dev->node);
617
Thomas Graf97dc48e2014-05-16 23:28:54 +0200618 seq_puts(seq, " Flags: ");
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800619
Luiz Capitulino222f1802006-03-20 22:16:13 -0800620 if (pkt_dev->flags & F_IPV6)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200621 seq_puts(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Luiz Capitulino222f1802006-03-20 22:16:13 -0800623 if (pkt_dev->flags & F_IPSRC_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200624 seq_puts(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Luiz Capitulino222f1802006-03-20 22:16:13 -0800626 if (pkt_dev->flags & F_IPDST_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200627 seq_puts(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Luiz Capitulino222f1802006-03-20 22:16:13 -0800629 if (pkt_dev->flags & F_TXSIZE_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200630 seq_puts(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Luiz Capitulino222f1802006-03-20 22:16:13 -0800632 if (pkt_dev->flags & F_UDPSRC_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200633 seq_puts(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Luiz Capitulino222f1802006-03-20 22:16:13 -0800635 if (pkt_dev->flags & F_UDPDST_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200636 seq_puts(seq, "UDPDST_RND ");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800637
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200638 if (pkt_dev->flags & F_UDPCSUM)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200639 seq_puts(seq, "UDPCSUM ");
Thomas Grafc26bf4a2013-07-25 18:12:18 +0200640
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800641 if (pkt_dev->flags & F_MPLS_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200642 seq_puts(seq, "MPLS_RND ");
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800643
Robert Olsson45b270f2007-08-28 15:45:55 -0700644 if (pkt_dev->flags & F_QUEUE_MAP_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200645 seq_puts(seq, "QUEUE_MAP_RND ");
Robert Olsson45b270f2007-08-28 15:45:55 -0700646
Robert Olssone6fce5b2008-08-07 02:23:01 -0700647 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200648 seq_puts(seq, "QUEUE_MAP_CPU ");
Robert Olssone6fce5b2008-08-07 02:23:01 -0700649
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700650 if (pkt_dev->cflows) {
651 if (pkt_dev->flags & F_FLOW_SEQ)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200652 seq_puts(seq, "FLOW_SEQ "); /*in sequence flows*/
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700653 else
Thomas Graf97dc48e2014-05-16 23:28:54 +0200654 seq_puts(seq, "FLOW_RND ");
Jamal Hadi Salim007a5312007-07-02 22:40:36 -0700655 }
656
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700657#ifdef CONFIG_XFRM
Fan Du81013282014-01-03 11:18:33 +0800658 if (pkt_dev->flags & F_IPSEC_ON) {
Thomas Graf97dc48e2014-05-16 23:28:54 +0200659 seq_puts(seq, "IPSEC ");
Fan Du81013282014-01-03 11:18:33 +0800660 if (pkt_dev->spi)
661 seq_printf(seq, "spi:%u", pkt_dev->spi);
662 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -0700663#endif
664
Luiz Capitulino222f1802006-03-20 22:16:13 -0800665 if (pkt_dev->flags & F_MACSRC_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200666 seq_puts(seq, "MACSRC_RND ");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800667
668 if (pkt_dev->flags & F_MACDST_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200669 seq_puts(seq, "MACDST_RND ");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800670
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700671 if (pkt_dev->flags & F_VID_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200672 seq_puts(seq, "VID_RND ");
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700673
674 if (pkt_dev->flags & F_SVID_RND)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200675 seq_puts(seq, "SVID_RND ");
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700676
Robert Olssone99b99b2010-03-18 22:44:30 +0000677 if (pkt_dev->flags & F_NODE)
Thomas Graf97dc48e2014-05-16 23:28:54 +0200678 seq_puts(seq, "NODE_ALLOC ");
Robert Olssone99b99b2010-03-18 22:44:30 +0000679
Luiz Capitulino222f1802006-03-20 22:16:13 -0800680 seq_puts(seq, "\n");
681
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000682 /* not really stopped, more like last-running-at */
Daniel Borkmann398f3822012-10-28 08:27:19 +0000683 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000684 idle = pkt_dev->idle_acc;
685 do_div(idle, NSEC_PER_USEC);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800686
687 seq_printf(seq,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000688 "Current:\n pkts-sofar: %llu errors: %llu\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800689 (unsigned long long)pkt_dev->sofar,
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000690 (unsigned long long)pkt_dev->errors);
691
692 seq_printf(seq,
693 " started: %lluus stopped: %lluus idle: %lluus\n",
694 (unsigned long long) ktime_to_us(pkt_dev->started_at),
695 (unsigned long long) ktime_to_us(stopped),
696 (unsigned long long) idle);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800697
698 seq_printf(seq,
699 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700700 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
701 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Luiz Capitulino222f1802006-03-20 22:16:13 -0800703 if (pkt_dev->flags & F_IPV6) {
Alexey Dobriyan47a02002011-05-03 11:23:40 +0000704 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
705 &pkt_dev->cur_in6_saddr,
706 &pkt_dev->cur_in6_daddr);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800707 } else
Amerigo Wang0373a942012-10-09 17:48:18 +0000708 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
709 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Luiz Capitulino222f1802006-03-20 22:16:13 -0800711 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700712 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Robert Olsson45b270f2007-08-28 15:45:55 -0700714 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
715
Luiz Capitulino222f1802006-03-20 22:16:13 -0800716 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800719 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 else
Thomas Graf97dc48e2014-05-16 23:28:54 +0200721 seq_puts(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800726
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000727static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
728 __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800729{
730 int i = 0;
731 *num = 0;
732
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700733 for (; i < maxlen; i++) {
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000734 int value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800735 char c;
736 *num <<= 4;
737 if (get_user(c, &user_buffer[i]))
738 return -EFAULT;
Andy Shevchenko82fd5b52010-09-20 20:40:26 +0000739 value = hex_to_bin(c);
740 if (value >= 0)
741 *num |= value;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800742 else
743 break;
744 }
745 return i;
746}
747
Luiz Capitulino222f1802006-03-20 22:16:13 -0800748static int count_trail_chars(const char __user * user_buffer,
749 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 int i;
752
753 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800754 char c;
755 if (get_user(c, &user_buffer[i]))
756 return -EFAULT;
757 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 case '\"':
759 case '\n':
760 case '\r':
761 case '\t':
762 case ' ':
763 case '=':
764 break;
765 default:
766 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769done:
770 return i;
771}
772
Paul Gortmakerbf0813b2012-01-19 15:40:06 +0000773static long num_arg(const char __user *user_buffer, unsigned long maxlen,
774 unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000776 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800778
Paul Gortmakerd6182222010-10-18 12:14:44 +0000779 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800780 char c;
781 if (get_user(c, &user_buffer[i]))
782 return -EFAULT;
783 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800785 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 } else
787 break;
788 }
789 return i;
790}
791
Luiz Capitulino222f1802006-03-20 22:16:13 -0800792static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Paul Gortmakerd6182222010-10-18 12:14:44 +0000794 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Paul Gortmakerd6182222010-10-18 12:14:44 +0000796 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800797 char c;
798 if (get_user(c, &user_buffer[i]))
799 return -EFAULT;
800 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 case '\"':
802 case '\n':
803 case '\r':
804 case '\t':
805 case ' ':
806 goto done_str;
807 break;
808 default:
809 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return i;
814}
815
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800816static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
817{
Eric Dumazet95c96172012-04-15 05:58:06 +0000818 unsigned int n = 0;
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800819 char c;
820 ssize_t i = 0;
821 int len;
822
823 pkt_dev->nr_labels = 0;
824 do {
825 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700826 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800827 if (len <= 0)
828 return len;
829 pkt_dev->labels[n] = htonl(tmp);
830 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
831 pkt_dev->flags |= F_MPLS_RND;
832 i += len;
833 if (get_user(c, &buffer[i]))
834 return -EFAULT;
835 i++;
836 n++;
837 if (n >= MAX_MPLS_LABELS)
838 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700839 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800840
841 pkt_dev->nr_labels = n;
842 return i;
843}
844
Luiz Capitulino222f1802006-03-20 22:16:13 -0800845static ssize_t pktgen_if_write(struct file *file,
846 const char __user * user_buffer, size_t count,
847 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Joe Perches8a994a72010-07-12 10:50:23 +0000849 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800850 struct pktgen_dev *pkt_dev = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +0000851 int i, max, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 char name[16], valstr[32];
853 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800854 char *pg_result = NULL;
855 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800857
858 pg_result = &(pkt_dev->result[0]);
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (count < 1) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000861 pr_warning("wrong command format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return -EINVAL;
863 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800864
Paul Gortmakerd6182222010-10-18 12:14:44 +0000865 max = count;
866 tmp = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867 if (tmp < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +0000868 pr_warning("illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800869 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Paul Gortmakerd6182222010-10-18 12:14:44 +0000871 i = tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /* Read variable name */
874
875 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000876 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800877 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800880 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EFAULT;
882 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800883
884 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886 if (len < 0)
887 return len;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 i += len;
890
891 if (debug) {
Dmitry Torokhov86c2c0a2010-11-06 20:11:38 +0000892 size_t copy = min_t(size_t, count, 1023);
Nelson Elhage448d7b52010-10-28 11:31:07 -0700893 char tb[copy + 1];
894 if (copy_from_user(tb, user_buffer, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -EFAULT;
Nelson Elhage448d7b52010-10-28 11:31:07 -0700896 tb[copy] = 0;
Joe Perchesf342cda2012-05-16 17:50:41 +0000897 pr_debug("%s,%lu buffer -:%s:-\n",
898 name, (unsigned long)count, tb);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (!strcmp(name, "min_pkt_size")) {
902 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000903 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907 if (value < 14 + 20 + 8)
908 value = 14 + 20 + 8;
909 if (value != pkt_dev->min_pkt_size) {
910 pkt_dev->min_pkt_size = value;
911 pkt_dev->cur_pkt_size = value;
912 }
913 sprintf(pg_result, "OK: min_pkt_size=%u",
914 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return count;
916 }
917
Luiz Capitulino222f1802006-03-20 22:16:13 -0800918 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000920 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800921 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 if (value < 14 + 20 + 8)
925 value = 14 + 20 + 8;
926 if (value != pkt_dev->max_pkt_size) {
927 pkt_dev->max_pkt_size = value;
928 pkt_dev->cur_pkt_size = value;
929 }
930 sprintf(pg_result, "OK: max_pkt_size=%u",
931 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return count;
933 }
934
Luiz Capitulino222f1802006-03-20 22:16:13 -0800935 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (!strcmp(name, "pkt_size")) {
938 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000939 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800940 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800943 if (value < 14 + 20 + 8)
944 value = 14 + 20 + 8;
945 if (value != pkt_dev->min_pkt_size) {
946 pkt_dev->min_pkt_size = value;
947 pkt_dev->max_pkt_size = value;
948 pkt_dev->cur_pkt_size = value;
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
951 return count;
952 }
953
Luiz Capitulino222f1802006-03-20 22:16:13 -0800954 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000956 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800957 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 sprintf(pg_result, "OK: debug=%u", debug);
962 return count;
963 }
964
Luiz Capitulino222f1802006-03-20 22:16:13 -0800965 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000967 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800968 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 i += len;
971 pkt_dev->nfrags = value;
972 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
973 return count;
974 }
975 if (!strcmp(name, "delay")) {
976 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000977 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800978 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +0000979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 i += len;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000981 if (value == 0x7FFFFFFF)
982 pkt_dev->delay = ULLONG_MAX;
983 else
Eric Dumazet9240d712009-10-03 01:39:18 +0000984 pkt_dev->delay = (u64)value;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +0000985
986 sprintf(pg_result, "OK: delay=%llu",
987 (unsigned long long) pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return count;
989 }
Daniel Turull43d28b62010-06-09 22:49:57 +0000990 if (!strcmp(name, "rate")) {
991 len = num_arg(&user_buffer[i], 10, &value);
992 if (len < 0)
993 return len;
994
995 i += len;
996 if (!value)
997 return len;
998 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
999 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001000 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001001
1002 sprintf(pg_result, "OK: rate=%lu", value);
1003 return count;
1004 }
1005 if (!strcmp(name, "ratep")) {
1006 len = num_arg(&user_buffer[i], 10, &value);
1007 if (len < 0)
1008 return len;
1009
1010 i += len;
1011 if (!value)
1012 return len;
1013 pkt_dev->delay = NSEC_PER_SEC/value;
1014 if (debug)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001015 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
Daniel Turull43d28b62010-06-09 22:49:57 +00001016
1017 sprintf(pg_result, "OK: rate=%lu", value);
1018 return count;
1019 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001020 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001022 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001023 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001026 if (value != pkt_dev->udp_src_min) {
1027 pkt_dev->udp_src_min = value;
1028 pkt_dev->cur_udp_src = value;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1031 return count;
1032 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001033 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001035 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001036 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 if (value != pkt_dev->udp_dst_min) {
1040 pkt_dev->udp_dst_min = value;
1041 pkt_dev->cur_udp_dst = value;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1044 return count;
1045 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001046 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001048 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001049 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 if (value != pkt_dev->udp_src_max) {
1053 pkt_dev->udp_src_max = value;
1054 pkt_dev->cur_udp_src = value;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1057 return count;
1058 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001059 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001061 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001062 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001065 if (value != pkt_dev->udp_dst_max) {
1066 pkt_dev->udp_dst_max = value;
1067 pkt_dev->cur_udp_dst = value;
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1070 return count;
1071 }
1072 if (!strcmp(name, "clone_skb")) {
1073 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001074 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001075 return len;
Neil Hormand8873312011-07-26 06:05:37 +00001076 if ((value > 0) &&
1077 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1078 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001080 pkt_dev->clone_skb = value;
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1083 return count;
1084 }
1085 if (!strcmp(name, "count")) {
1086 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001087 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001088 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 i += len;
1091 pkt_dev->count = value;
1092 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001093 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return count;
1095 }
1096 if (!strcmp(name, "src_mac_count")) {
1097 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001098 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001099 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 i += len;
1102 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001103 pkt_dev->src_mac_count = value;
1104 pkt_dev->cur_src_mac_offset = 0;
1105 }
1106 sprintf(pg_result, "OK: src_mac_count=%d",
1107 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return count;
1109 }
1110 if (!strcmp(name, "dst_mac_count")) {
1111 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001112 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001113 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 i += len;
1116 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001117 pkt_dev->dst_mac_count = value;
1118 pkt_dev->cur_dst_mac_offset = 0;
1119 }
1120 sprintf(pg_result, "OK: dst_mac_count=%d",
1121 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return count;
1123 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001124 if (!strcmp(name, "node")) {
1125 len = num_arg(&user_buffer[i], 10, &value);
1126 if (len < 0)
1127 return len;
1128
1129 i += len;
1130
1131 if (node_possible(value)) {
1132 pkt_dev->node = value;
1133 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
Eric Dumazet26ad7872011-01-25 13:26:05 -08001134 if (pkt_dev->page) {
1135 put_page(pkt_dev->page);
1136 pkt_dev->page = NULL;
1137 }
Robert Olssone99b99b2010-03-18 22:44:30 +00001138 }
1139 else
1140 sprintf(pg_result, "ERROR: node not possible");
1141 return count;
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001144 char f[32];
1145 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001147 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001148 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (copy_from_user(f, &user_buffer[i], len))
1151 return -EFAULT;
1152 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001153 if (strcmp(f, "IPSRC_RND") == 0)
1154 pkt_dev->flags |= F_IPSRC_RND;
1155
1156 else if (strcmp(f, "!IPSRC_RND") == 0)
1157 pkt_dev->flags &= ~F_IPSRC_RND;
1158
1159 else if (strcmp(f, "TXSIZE_RND") == 0)
1160 pkt_dev->flags |= F_TXSIZE_RND;
1161
1162 else if (strcmp(f, "!TXSIZE_RND") == 0)
1163 pkt_dev->flags &= ~F_TXSIZE_RND;
1164
1165 else if (strcmp(f, "IPDST_RND") == 0)
1166 pkt_dev->flags |= F_IPDST_RND;
1167
1168 else if (strcmp(f, "!IPDST_RND") == 0)
1169 pkt_dev->flags &= ~F_IPDST_RND;
1170
1171 else if (strcmp(f, "UDPSRC_RND") == 0)
1172 pkt_dev->flags |= F_UDPSRC_RND;
1173
1174 else if (strcmp(f, "!UDPSRC_RND") == 0)
1175 pkt_dev->flags &= ~F_UDPSRC_RND;
1176
1177 else if (strcmp(f, "UDPDST_RND") == 0)
1178 pkt_dev->flags |= F_UDPDST_RND;
1179
1180 else if (strcmp(f, "!UDPDST_RND") == 0)
1181 pkt_dev->flags &= ~F_UDPDST_RND;
1182
1183 else if (strcmp(f, "MACSRC_RND") == 0)
1184 pkt_dev->flags |= F_MACSRC_RND;
1185
1186 else if (strcmp(f, "!MACSRC_RND") == 0)
1187 pkt_dev->flags &= ~F_MACSRC_RND;
1188
1189 else if (strcmp(f, "MACDST_RND") == 0)
1190 pkt_dev->flags |= F_MACDST_RND;
1191
1192 else if (strcmp(f, "!MACDST_RND") == 0)
1193 pkt_dev->flags &= ~F_MACDST_RND;
1194
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001195 else if (strcmp(f, "MPLS_RND") == 0)
1196 pkt_dev->flags |= F_MPLS_RND;
1197
1198 else if (strcmp(f, "!MPLS_RND") == 0)
1199 pkt_dev->flags &= ~F_MPLS_RND;
1200
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001201 else if (strcmp(f, "VID_RND") == 0)
1202 pkt_dev->flags |= F_VID_RND;
1203
1204 else if (strcmp(f, "!VID_RND") == 0)
1205 pkt_dev->flags &= ~F_VID_RND;
1206
1207 else if (strcmp(f, "SVID_RND") == 0)
1208 pkt_dev->flags |= F_SVID_RND;
1209
1210 else if (strcmp(f, "!SVID_RND") == 0)
1211 pkt_dev->flags &= ~F_SVID_RND;
1212
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07001213 else if (strcmp(f, "FLOW_SEQ") == 0)
1214 pkt_dev->flags |= F_FLOW_SEQ;
1215
Robert Olsson45b270f2007-08-28 15:45:55 -07001216 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1217 pkt_dev->flags |= F_QUEUE_MAP_RND;
1218
1219 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1220 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001221
1222 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1223 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1224
1225 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1226 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07001227#ifdef CONFIG_XFRM
1228 else if (strcmp(f, "IPSEC") == 0)
1229 pkt_dev->flags |= F_IPSEC_ON;
1230#endif
1231
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001232 else if (strcmp(f, "!IPV6") == 0)
1233 pkt_dev->flags &= ~F_IPV6;
1234
Robert Olssone99b99b2010-03-18 22:44:30 +00001235 else if (strcmp(f, "NODE_ALLOC") == 0)
1236 pkt_dev->flags |= F_NODE;
1237
1238 else if (strcmp(f, "!NODE_ALLOC") == 0)
1239 pkt_dev->flags &= ~F_NODE;
1240
Thomas Grafc26bf4a2013-07-25 18:12:18 +02001241 else if (strcmp(f, "UDPCSUM") == 0)
1242 pkt_dev->flags |= F_UDPCSUM;
1243
1244 else if (strcmp(f, "!UDPCSUM") == 0)
1245 pkt_dev->flags &= ~F_UDPCSUM;
1246
Luiz Capitulino222f1802006-03-20 22:16:13 -08001247 else {
1248 sprintf(pg_result,
1249 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1250 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001251 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
Mathias Krause72f8e062014-02-21 21:38:36 +01001252 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1253 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1254 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
1255#ifdef CONFIG_XFRM
1256 "IPSEC, "
1257#endif
1258 "NODE_ALLOC\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 return count;
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1262 return count;
1263 }
1264 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1265 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001266 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001267 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Luiz Capitulino222f1802006-03-20 22:16:13 -08001269 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001271 buf[len] = 0;
1272 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1273 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1274 strncpy(pkt_dev->dst_min, buf, len);
1275 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1276 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1277 }
1278 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001279 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001280 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1282 return count;
1283 }
1284 if (!strcmp(name, "dst_max")) {
1285 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001286 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001287 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Luiz Capitulino222f1802006-03-20 22:16:13 -08001290 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return -EFAULT;
1292
Luiz Capitulino222f1802006-03-20 22:16:13 -08001293 buf[len] = 0;
1294 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1295 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1296 strncpy(pkt_dev->dst_max, buf, len);
1297 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1298 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1299 }
1300 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001301 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 i += len;
1303 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1304 return count;
1305 }
1306 if (!strcmp(name, "dst6")) {
1307 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 if (len < 0)
1309 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 pkt_dev->flags |= F_IPV6;
1312
Luiz Capitulino222f1802006-03-20 22:16:13 -08001313 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001315 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Amerigo Wangc468fb12012-10-09 17:48:20 +00001317 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001318 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001320 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001323 pr_debug("dst6 set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Luiz Capitulino222f1802006-03-20 22:16:13 -08001325 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 sprintf(pg_result, "OK: dst6=%s", buf);
1327 return count;
1328 }
1329 if (!strcmp(name, "dst6_min")) {
1330 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001331 if (len < 0)
1332 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 pkt_dev->flags |= F_IPV6;
1335
Luiz Capitulino222f1802006-03-20 22:16:13 -08001336 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Amerigo Wangc468fb12012-10-09 17:48:20 +00001340 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001341 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001343 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001344 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001345 pr_debug("dst6_min set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Luiz Capitulino222f1802006-03-20 22:16:13 -08001347 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 sprintf(pg_result, "OK: dst6_min=%s", buf);
1349 return count;
1350 }
1351 if (!strcmp(name, "dst6_max")) {
1352 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001353 if (len < 0)
1354 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 pkt_dev->flags |= F_IPV6;
1357
Luiz Capitulino222f1802006-03-20 22:16:13 -08001358 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001360 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Amerigo Wangc468fb12012-10-09 17:48:20 +00001362 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001363 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Luiz Capitulino222f1802006-03-20 22:16:13 -08001365 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001366 pr_debug("dst6_max set to: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Luiz Capitulino222f1802006-03-20 22:16:13 -08001368 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 sprintf(pg_result, "OK: dst6_max=%s", buf);
1370 return count;
1371 }
1372 if (!strcmp(name, "src6")) {
1373 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001374 if (len < 0)
1375 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 pkt_dev->flags |= F_IPV6;
1378
Luiz Capitulino222f1802006-03-20 22:16:13 -08001379 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Amerigo Wangc468fb12012-10-09 17:48:20 +00001383 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
Alexey Dobriyan47a02002011-05-03 11:23:40 +00001384 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001386 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Luiz Capitulino222f1802006-03-20 22:16:13 -08001388 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001389 pr_debug("src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001390
1391 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 sprintf(pg_result, "OK: src6=%s", buf);
1393 return count;
1394 }
1395 if (!strcmp(name, "src_min")) {
1396 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001397 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001398 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001399
Luiz Capitulino222f1802006-03-20 22:16:13 -08001400 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001402 buf[len] = 0;
1403 if (strcmp(buf, pkt_dev->src_min) != 0) {
1404 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1405 strncpy(pkt_dev->src_min, buf, len);
1406 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1407 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1408 }
1409 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001410 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 i += len;
1412 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1413 return count;
1414 }
1415 if (!strcmp(name, "src_max")) {
1416 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001417 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001418 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001419
Luiz Capitulino222f1802006-03-20 22:16:13 -08001420 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001422 buf[len] = 0;
1423 if (strcmp(buf, pkt_dev->src_max) != 0) {
1424 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1425 strncpy(pkt_dev->src_max, buf, len);
1426 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1427 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1428 }
1429 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001430 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 i += len;
1432 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1433 return count;
1434 }
1435 if (!strcmp(name, "dst_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001437 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001438 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001441 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001444 if (!mac_pton(valstr, pkt_dev->dst_mac))
1445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001447 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001449 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return count;
1451 }
1452 if (!strcmp(name, "src_mac")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001454 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001455 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001458 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001461 if (!mac_pton(valstr, pkt_dev->src_mac))
1462 return -EINVAL;
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001463 /* Set up Src MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08001464 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
Adit Ranadivece5d0b42007-09-16 14:52:15 -07001465
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001466 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return count;
1468 }
1469
Luiz Capitulino222f1802006-03-20 22:16:13 -08001470 if (!strcmp(name, "clear_counters")) {
1471 pktgen_clear_counters(pkt_dev);
1472 sprintf(pg_result, "OK: Clearing counters.\n");
1473 return count;
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 if (!strcmp(name, "flows")) {
1477 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001478 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001479 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 i += len;
1482 if (value > MAX_CFLOWS)
1483 value = MAX_CFLOWS;
1484
1485 pkt_dev->cflows = value;
1486 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1487 return count;
1488 }
Fan Du6bae9192014-01-10 14:39:13 +08001489#ifdef CONFIG_XFRM
Fan Dude4aee72014-01-03 11:18:30 +08001490 if (!strcmp(name, "spi")) {
1491 len = num_arg(&user_buffer[i], 10, &value);
1492 if (len < 0)
1493 return len;
1494
1495 i += len;
1496 pkt_dev->spi = value;
1497 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1498 return count;
1499 }
Fan Du6bae9192014-01-10 14:39:13 +08001500#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (!strcmp(name, "flowlen")) {
1502 len = num_arg(&user_buffer[i], 10, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001503 if (len < 0)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001504 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 i += len;
1507 pkt_dev->lflow = value;
1508 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1509 return count;
1510 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001511
Robert Olsson45b270f2007-08-28 15:45:55 -07001512 if (!strcmp(name, "queue_map_min")) {
1513 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001514 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001515 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001516
Robert Olsson45b270f2007-08-28 15:45:55 -07001517 i += len;
1518 pkt_dev->queue_map_min = value;
1519 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1520 return count;
1521 }
1522
1523 if (!strcmp(name, "queue_map_max")) {
1524 len = num_arg(&user_buffer[i], 5, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001525 if (len < 0)
Robert Olsson45b270f2007-08-28 15:45:55 -07001526 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001527
Robert Olsson45b270f2007-08-28 15:45:55 -07001528 i += len;
1529 pkt_dev->queue_map_max = value;
1530 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1531 return count;
1532 }
1533
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001534 if (!strcmp(name, "mpls")) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001535 unsigned int n, cnt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001536
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001537 len = get_labels(&user_buffer[i], pkt_dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001538 if (len < 0)
1539 return len;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001540 i += len;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001541 cnt = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001542 for (n = 0; n < pkt_dev->nr_labels; n++)
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001543 cnt += sprintf(pg_result + cnt,
1544 "%08x%s", ntohl(pkt_dev->labels[n]),
1545 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001546
1547 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1548 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1549 pkt_dev->svlan_id = 0xffff;
1550
1551 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001552 pr_debug("VLAN/SVLAN auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001553 }
1554 return count;
1555 }
1556
1557 if (!strcmp(name, "vlan_id")) {
1558 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001559 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001560 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001561
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001562 i += len;
1563 if (value <= 4095) {
1564 pkt_dev->vlan_id = value; /* turn on VLAN */
1565
1566 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001567 pr_debug("VLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001568
1569 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001570 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001571
1572 pkt_dev->nr_labels = 0; /* turn off MPLS */
1573 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1574 } else {
1575 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1576 pkt_dev->svlan_id = 0xffff;
1577
1578 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001579 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001580 }
1581 return count;
1582 }
1583
1584 if (!strcmp(name, "vlan_p")) {
1585 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001586 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001587 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001588
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001589 i += len;
1590 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1591 pkt_dev->vlan_p = value;
1592 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1593 } else {
1594 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1595 }
1596 return count;
1597 }
1598
1599 if (!strcmp(name, "vlan_cfi")) {
1600 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001601 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001602 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001603
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001604 i += len;
1605 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1606 pkt_dev->vlan_cfi = value;
1607 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1608 } else {
1609 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1610 }
1611 return count;
1612 }
1613
1614 if (!strcmp(name, "svlan_id")) {
1615 len = num_arg(&user_buffer[i], 4, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001616 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001617 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001618
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001619 i += len;
1620 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1621 pkt_dev->svlan_id = value; /* turn on SVLAN */
1622
1623 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001624 pr_debug("SVLAN turned on\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001625
1626 if (debug && pkt_dev->nr_labels)
Joe Perchesf342cda2012-05-16 17:50:41 +00001627 pr_debug("MPLS auto turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001628
1629 pkt_dev->nr_labels = 0; /* turn off MPLS */
1630 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1631 } else {
1632 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1633 pkt_dev->svlan_id = 0xffff;
1634
1635 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001636 pr_debug("VLAN/SVLAN turned off\n");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001637 }
1638 return count;
1639 }
1640
1641 if (!strcmp(name, "svlan_p")) {
1642 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001643 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001644 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001645
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001646 i += len;
1647 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1648 pkt_dev->svlan_p = value;
1649 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1650 } else {
1651 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1652 }
1653 return count;
1654 }
1655
1656 if (!strcmp(name, "svlan_cfi")) {
1657 len = num_arg(&user_buffer[i], 1, &value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001658 if (len < 0)
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001659 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001660
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001661 i += len;
1662 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1663 pkt_dev->svlan_cfi = value;
1664 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1665 } else {
1666 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1667 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001668 return count;
1669 }
1670
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001671 if (!strcmp(name, "tos")) {
1672 __u32 tmp_value = 0;
1673 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001674 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001675 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001676
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001677 i += len;
1678 if (len == 2) {
1679 pkt_dev->tos = tmp_value;
1680 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1681 } else {
1682 sprintf(pg_result, "ERROR: tos must be 00-ff");
1683 }
1684 return count;
1685 }
1686
1687 if (!strcmp(name, "traffic_class")) {
1688 __u32 tmp_value = 0;
1689 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001690 if (len < 0)
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001691 return len;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001692
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001693 i += len;
1694 if (len == 2) {
1695 pkt_dev->traffic_class = tmp_value;
1696 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1697 } else {
1698 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1699 }
1700 return count;
1701 }
1702
John Fastabend9e50e3a2010-11-16 19:12:28 +00001703 if (!strcmp(name, "skb_priority")) {
1704 len = num_arg(&user_buffer[i], 9, &value);
1705 if (len < 0)
1706 return len;
1707
1708 i += len;
1709 pkt_dev->skb_priority = value;
1710 sprintf(pg_result, "OK: skb_priority=%i",
1711 pkt_dev->skb_priority);
1712 return count;
1713 }
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1716 return -EINVAL;
1717}
1718
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001719static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Al Virod9dda782013-03-31 18:16:14 -04001721 return single_open(file, pktgen_if_show, PDE_DATA(inode));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001722}
1723
Arjan van de Ven9a321442007-02-12 00:55:35 -08001724static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001725 .owner = THIS_MODULE,
1726 .open = pktgen_if_open,
1727 .read = seq_read,
1728 .llseek = seq_lseek,
1729 .write = pktgen_if_write,
1730 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001731};
1732
1733static int pktgen_thread_show(struct seq_file *seq, void *v)
1734{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 struct pktgen_thread *t = seq->private;
Stephen Hemminger648fda72009-08-27 13:55:07 +00001736 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001738 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Thomas Graf97dc48e2014-05-16 23:28:54 +02001740 seq_puts(seq, "Running: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001741
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001742 rcu_read_lock();
1743 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001744 if (pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001745 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Thomas Graf97dc48e2014-05-16 23:28:54 +02001747 seq_puts(seq, "\nStopped: ");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001748
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001749 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001750 if (!pkt_dev->running)
Eric Dumazet593f63b2009-11-23 01:44:37 +00001751 seq_printf(seq, "%s ", pkt_dev->odevname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001754 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 else
Thomas Graf97dc48e2014-05-16 23:28:54 +02001756 seq_puts(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001758 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001763static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001764 const char __user * user_buffer,
1765 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Joe Perches8a994a72010-07-12 10:50:23 +00001767 struct seq_file *seq = file->private_data;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001768 struct pktgen_thread *t = seq->private;
Paul Gortmakerd6182222010-10-18 12:14:44 +00001769 int i, max, len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001771 char *pg_result;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return -EINVAL;
1776 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001777
Paul Gortmakerd6182222010-10-18 12:14:44 +00001778 max = count;
1779 len = count_trail_chars(user_buffer, max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001780 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001781 return len;
1782
Paul Gortmakerd6182222010-10-18 12:14:44 +00001783 i = len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* Read variable name */
1786
1787 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001788 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001789 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 memset(name, 0, sizeof(name));
1792 if (copy_from_user(name, &user_buffer[i], len))
1793 return -EFAULT;
1794 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001795
Luiz Capitulino222f1802006-03-20 22:16:13 -08001796 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001798 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001799 return len;
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 i += len;
1802
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001803 if (debug)
Joe Perchesf342cda2012-05-16 17:50:41 +00001804 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Luiz Capitulino222f1802006-03-20 22:16:13 -08001806 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001807 pr_err("ERROR: No thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 ret = -EINVAL;
1809 goto out;
1810 }
1811
1812 pg_result = &(t->result[0]);
1813
Luiz Capitulino222f1802006-03-20 22:16:13 -08001814 if (!strcmp(name, "add_device")) {
1815 char f[32];
1816 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001818 if (len < 0) {
1819 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 goto out;
1821 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001822 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return -EFAULT;
1824 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001825 mutex_lock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001826 ret = pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001827 mutex_unlock(&pktgen_thread_lock);
Cong Wang604dfd62013-01-27 21:14:08 +00001828 if (!ret) {
1829 ret = count;
1830 sprintf(pg_result, "OK: add_device=%s", f);
1831 } else
1832 sprintf(pg_result, "ERROR: can not add device %s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 goto out;
1834 }
1835
Luiz Capitulino222f1802006-03-20 22:16:13 -08001836 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001837 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001838 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001839 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001840 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001842 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 goto out;
1844 }
1845
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846 if (!strcmp(name, "max_before_softirq")) {
Robert Olssonb1639112007-08-28 15:46:58 -07001847 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001848 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto out;
1850 }
1851
1852 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001853out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return ret;
1855}
1856
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001857static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Al Virod9dda782013-03-31 18:16:14 -04001859 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Arjan van de Ven9a321442007-02-12 00:55:35 -08001862static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001863 .owner = THIS_MODULE,
1864 .open = pktgen_thread_open,
1865 .read = seq_read,
1866 .llseek = seq_lseek,
1867 .write = pktgen_thread_write,
1868 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001869};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871/* Think find or remove for NN */
Cong Wang4e58a022013-01-28 19:55:53 +00001872static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1873 const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 struct pktgen_thread *t;
1876 struct pktgen_dev *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08001877 bool exact = (remove == FIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Cong Wang4e58a022013-01-28 19:55:53 +00001879 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Eric Dumazet3e984842009-11-24 14:50:53 -08001880 pkt_dev = pktgen_find_dev(t, ifname, exact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001882 if (remove) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001883 pkt_dev->removal_mark = 1;
1884 t->control |= T_REMDEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 break;
1887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001889 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001892/*
1893 * mark a device for removal
1894 */
Cong Wang4e58a022013-01-28 19:55:53 +00001895static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001898 const int max_tries = 10, msec_per_try = 125;
1899 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001900
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001901 mutex_lock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001902 pr_debug("%s: marking %s for removal\n", __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001903
Luiz Capitulino222f1802006-03-20 22:16:13 -08001904 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001905
Cong Wang4e58a022013-01-28 19:55:53 +00001906 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001907 if (pkt_dev == NULL)
1908 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001909
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001910 mutex_unlock(&pktgen_thread_lock);
Joe Perchesf9467ea2010-06-21 12:29:14 +00001911 pr_debug("%s: waiting for %s to disappear....\n",
1912 __func__, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001913 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001914 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001915
1916 if (++i >= max_tries) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00001917 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1918 __func__, msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001919 break;
1920 }
1921
1922 }
1923
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001924 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001925}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001926
Cong Wang4e58a022013-01-28 19:55:53 +00001927static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001928{
1929 struct pktgen_thread *t;
1930
Cong Wang4e58a022013-01-28 19:55:53 +00001931 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001932 struct pktgen_dev *pkt_dev;
1933
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001934 rcu_read_lock();
1935 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001936 if (pkt_dev->odev != dev)
1937 continue;
1938
David Howellsa8ca16e2013-04-12 17:27:28 +01001939 proc_remove(pkt_dev->entry);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001940
Alexey Dobriyan29753152009-08-28 23:34:43 -07001941 pkt_dev->entry = proc_create_data(dev->name, 0600,
Cong Wang4e58a022013-01-28 19:55:53 +00001942 pn->proc_dir,
Alexey Dobriyan29753152009-08-28 23:34:43 -07001943 &pktgen_if_fops,
1944 pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001945 if (!pkt_dev->entry)
Joe Perchesf9467ea2010-06-21 12:29:14 +00001946 pr_err("can't move proc entry for '%s'\n",
1947 dev->name);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001948 break;
1949 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02001950 rcu_read_unlock();
Stephen Hemminger39df2322007-03-04 16:11:51 -08001951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Luiz Capitulino222f1802006-03-20 22:16:13 -08001954static int pktgen_device_event(struct notifier_block *unused,
1955 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Jiri Pirko351638e2013-05-28 01:30:21 +00001957 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Cong Wang4e58a022013-01-28 19:55:53 +00001958 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Cong Wang4e58a022013-01-28 19:55:53 +00001960 if (pn->pktgen_exiting)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001961 return NOTIFY_DONE;
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /* It is OK that we do not hold the group lock right now,
1964 * as we run under the RTNL lock.
1965 */
1966
1967 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001968 case NETDEV_CHANGENAME:
Cong Wang4e58a022013-01-28 19:55:53 +00001969 pktgen_change_name(pn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 case NETDEV_UNREGISTER:
Cong Wang4e58a022013-01-28 19:55:53 +00001973 pktgen_mark_device(pn, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 return NOTIFY_DONE;
1978}
1979
Cong Wang4e58a022013-01-28 19:55:53 +00001980static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1981 struct pktgen_dev *pkt_dev,
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001982 const char *ifname)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001983{
1984 char b[IFNAMSIZ+5];
Paul Gortmakerd6182222010-10-18 12:14:44 +00001985 int i;
Robert Olssone6fce5b2008-08-07 02:23:01 -07001986
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00001987 for (i = 0; ifname[i] != '@'; i++) {
1988 if (i == IFNAMSIZ)
Robert Olssone6fce5b2008-08-07 02:23:01 -07001989 break;
1990
1991 b[i] = ifname[i];
1992 }
1993 b[i] = 0;
1994
Cong Wang4e58a022013-01-28 19:55:53 +00001995 return dev_get_by_name(pn->net, b);
Robert Olssone6fce5b2008-08-07 02:23:01 -07001996}
1997
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999/* Associate pktgen_dev with a device. */
2000
Cong Wang4e58a022013-01-28 19:55:53 +00002001static int pktgen_setup_dev(const struct pktgen_net *pn,
2002 struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08002005 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (pkt_dev->odev) {
2009 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002010 pkt_dev->odev = NULL;
2011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Cong Wang4e58a022013-01-28 19:55:53 +00002013 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (!odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002015 pr_err("no such netdevice: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002016 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (odev->type != ARPHRD_ETHER) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002020 pr_err("not an ethernet device: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002021 err = -EINVAL;
2022 } else if (!netif_running(odev)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002023 pr_err("device is down: \"%s\"\n", ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002024 err = -ENETDOWN;
2025 } else {
2026 pkt_dev->odev = odev;
2027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08002031 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034/* Read pkt_dev from the interface and set up internal pktgen_dev
2035 * structure to have the right information to create/send packets
2036 */
2037static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2038{
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002039 int ntxq;
2040
Luiz Capitulino222f1802006-03-20 22:16:13 -08002041 if (!pkt_dev->odev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002042 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002043 sprintf(pkt_dev->result,
2044 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2045 return;
2046 }
2047
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002048 /* make sure that we don't pick a non-existing transmit queue */
2049 ntxq = pkt_dev->odev->real_num_tx_queues;
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002050
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002051 if (ntxq <= pkt_dev->queue_map_min) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002052 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2053 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2054 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002055 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002056 }
Jesse Brandeburg88271662008-10-28 13:21:51 -07002057 if (pkt_dev->queue_map_max >= ntxq) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002058 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2059 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2060 pkt_dev->odevname);
Dan Carpenter26e29ee2012-01-06 03:13:47 +00002061 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
Andrew Gallatin64c00d82008-08-13 15:16:00 -07002062 }
2063
Luiz Capitulino222f1802006-03-20 22:16:13 -08002064 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08002066 if (is_zero_ether_addr(pkt_dev->src_mac))
Joe Perches9ea08b12014-01-20 09:52:19 -08002067 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Luiz Capitulino222f1802006-03-20 22:16:13 -08002069 /* Set up Dest MAC */
Joe Perches9ea08b12014-01-20 09:52:19 -08002070 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Luiz Capitulino222f1802006-03-20 22:16:13 -08002072 if (pkt_dev->flags & F_IPV6) {
Amerigo Wang4c139b82012-10-09 17:48:19 +00002073 int i, set = 0, err = 1;
2074 struct inet6_dev *idev;
2075
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002076 if (pkt_dev->min_pkt_size == 0) {
2077 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2078 + sizeof(struct udphdr)
2079 + sizeof(struct pktgen_hdr)
2080 + pkt_dev->pkt_overhead;
2081 }
2082
Luiz Capitulino222f1802006-03-20 22:16:13 -08002083 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2084 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 set = 1;
2086 break;
2087 }
2088
Luiz Capitulino222f1802006-03-20 22:16:13 -08002089 if (!set) {
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /*
2092 * Use linklevel address if unconfigured.
2093 *
2094 * use ipv6_get_lladdr if/when it's get exported
2095 */
2096
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002097 rcu_read_lock();
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002098 idev = __in6_dev_get(pkt_dev->odev);
2099 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct inet6_ifaddr *ifp;
2101
2102 read_lock_bh(&idev->lock);
Amerigo Wang4c139b82012-10-09 17:48:19 +00002103 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2104 if ((ifp->scope & IFA_LINK) &&
Joe Perchesf64f9e72009-11-29 16:55:45 -08002105 !(ifp->flags & IFA_F_TENTATIVE)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002106 pkt_dev->cur_in6_saddr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 err = 0;
2108 break;
2109 }
2110 }
2111 read_unlock_bh(&idev->lock);
2112 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002113 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002114 if (err)
Joe Perchesf9467ea2010-06-21 12:29:14 +00002115 pr_err("ERROR: IPv6 link address not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002117 } else {
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002118 if (pkt_dev->min_pkt_size == 0) {
2119 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2120 + sizeof(struct udphdr)
2121 + sizeof(struct pktgen_hdr)
2122 + pkt_dev->pkt_overhead;
2123 }
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 pkt_dev->saddr_min = 0;
2126 pkt_dev->saddr_max = 0;
2127 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002128
2129 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002132 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (in_dev) {
2134 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002135 pkt_dev->saddr_min =
2136 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 pkt_dev->saddr_max = pkt_dev->saddr_min;
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002141 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2143 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2144 }
2145
2146 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2147 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2148 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002149 /* Initialize current values. */
Amerigo Wang68bf9f02012-10-09 17:48:17 +00002150 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2151 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2152 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2153
Luiz Capitulino222f1802006-03-20 22:16:13 -08002154 pkt_dev->cur_dst_mac_offset = 0;
2155 pkt_dev->cur_src_mac_offset = 0;
2156 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2157 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2158 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2159 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 pkt_dev->nflows = 0;
2161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002164static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2165{
Stephen Hemmingeref879792009-09-22 19:41:43 +00002166 ktime_t start_time, end_time;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002167 s64 remaining;
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002168 struct hrtimer_sleeper t;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002169
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002170 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2171 hrtimer_set_expires(&t.timer, spin_until);
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00002172
Daniel Turull43d28b62010-06-09 22:49:57 +00002173 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002174 if (remaining <= 0) {
2175 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002176 return;
Eric Dumazet417bc4b2009-10-01 09:29:45 -07002177 }
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002178
Daniel Borkmann398f3822012-10-28 08:27:19 +00002179 start_time = ktime_get();
Eric Dumazet33136d12011-10-20 17:00:21 -04002180 if (remaining < 100000) {
2181 /* for small delays (<100us), just loop until limit is reached */
2182 do {
Daniel Borkmann398f3822012-10-28 08:27:19 +00002183 end_time = ktime_get();
2184 } while (ktime_compare(end_time, spin_until) < 0);
Eric Dumazet33136d12011-10-20 17:00:21 -04002185 } else {
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002186 /* see do_nanosleep */
2187 hrtimer_init_sleeper(&t, current);
2188 do {
2189 set_current_state(TASK_INTERRUPTIBLE);
2190 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2191 if (!hrtimer_active(&t.timer))
2192 t.task = NULL;
2193
2194 if (likely(t.task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07002197 hrtimer_cancel(&t.timer);
2198 } while (t.task && pkt_dev->running && !signal_pending(current));
2199 __set_current_state(TASK_RUNNING);
Daniel Borkmann398f3822012-10-28 08:27:19 +00002200 end_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
Stephen Hemmingeref879792009-09-22 19:41:43 +00002202
2203 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
Daniel Turull07a0f0f2010-06-10 23:08:11 -07002204 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002207static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2208{
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002209 pkt_dev->pkt_overhead = 0;
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002210 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2211 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2212 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2213}
2214
Stephen Hemminger648fda72009-08-27 13:55:07 +00002215static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002216{
Stephen Hemminger648fda72009-08-27 13:55:07 +00002217 return !!(pkt_dev->flows[flow].flags & F_INIT);
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002218}
2219
2220static inline int f_pick(struct pktgen_dev *pkt_dev)
2221{
2222 int flow = pkt_dev->curfl;
2223
2224 if (pkt_dev->flags & F_FLOW_SEQ) {
2225 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2226 /* reset time */
2227 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002228 pkt_dev->flows[flow].flags = 0;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002229 pkt_dev->curfl += 1;
2230 if (pkt_dev->curfl >= pkt_dev->cflows)
2231 pkt_dev->curfl = 0; /*reset */
2232 }
2233 } else {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002234 flow = prandom_u32() % pkt_dev->cflows;
Robert Olsson1211a642008-08-05 18:44:26 -07002235 pkt_dev->curfl = flow;
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002236
Robert Olsson1211a642008-08-05 18:44:26 -07002237 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002238 pkt_dev->flows[flow].count = 0;
Robert Olsson1211a642008-08-05 18:44:26 -07002239 pkt_dev->flows[flow].flags = 0;
2240 }
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002241 }
2242
2243 return pkt_dev->curfl;
2244}
2245
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002246
2247#ifdef CONFIG_XFRM
2248/* If there was already an IPSEC SA, we keep it as is, else
2249 * we go look for it ...
2250*/
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002251#define DUMMY_MARK 0
Adrian Bunkfea1ab02007-07-30 18:04:09 -07002252static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002253{
2254 struct xfrm_state *x = pkt_dev->flows[flow].x;
Cong Wang4e58a022013-01-28 19:55:53 +00002255 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002256 if (!x) {
Fan Duc4549972014-01-03 11:18:32 +08002257
2258 if (pkt_dev->spi) {
2259 /* We need as quick as possible to find the right SA
2260 * Searching with minimum criteria to archieve this.
2261 */
2262 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2263 } else {
2264 /* slow path: we dont already have xfrm_state */
2265 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
2266 (xfrm_address_t *)&pkt_dev->cur_daddr,
2267 (xfrm_address_t *)&pkt_dev->cur_saddr,
2268 AF_INET,
2269 pkt_dev->ipsmode,
2270 pkt_dev->ipsproto, 0);
2271 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002272 if (x) {
2273 pkt_dev->flows[flow].x = x;
2274 set_pkt_overhead(pkt_dev);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002275 pkt_dev->pkt_overhead += x->props.header_len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002276 }
2277
2278 }
2279}
2280#endif
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002281static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2282{
Robert Olssone6fce5b2008-08-07 02:23:01 -07002283
2284 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2285 pkt_dev->cur_queue_map = smp_processor_id();
2286
Eric Dumazet896a7cf2009-10-02 20:24:59 +00002287 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002288 __u16 t;
2289 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002290 t = prandom_u32() %
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002291 (pkt_dev->queue_map_max -
2292 pkt_dev->queue_map_min + 1)
2293 + pkt_dev->queue_map_min;
2294 } else {
2295 t = pkt_dev->cur_queue_map + 1;
2296 if (t > pkt_dev->queue_map_max)
2297 t = pkt_dev->queue_map_min;
2298 }
2299 pkt_dev->cur_queue_map = t;
2300 }
Robert Olssonbfdbc0a2008-11-19 14:09:47 -08002301 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002302}
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304/* Increment/randomize headers according to flags and current values
2305 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2306 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2308{
2309 __u32 imn;
2310 __u32 imx;
2311 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002313 if (pkt_dev->cflows)
2314 flow = f_pick(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002317 if (pkt_dev->src_mac_count > 1) {
2318 __u32 mc;
2319 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Luiz Capitulino222f1802006-03-20 22:16:13 -08002321 if (pkt_dev->flags & F_MACSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002322 mc = prandom_u32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002323 else {
2324 mc = pkt_dev->cur_src_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002325 if (pkt_dev->cur_src_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002326 pkt_dev->src_mac_count)
2327 pkt_dev->cur_src_mac_offset = 0;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Luiz Capitulino222f1802006-03-20 22:16:13 -08002330 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2331 pkt_dev->hh[11] = tmp;
2332 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2333 pkt_dev->hh[10] = tmp;
2334 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2335 pkt_dev->hh[9] = tmp;
2336 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2337 pkt_dev->hh[8] = tmp;
2338 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2339 pkt_dev->hh[7] = tmp;
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Luiz Capitulino222f1802006-03-20 22:16:13 -08002342 /* Deal with Destination MAC */
2343 if (pkt_dev->dst_mac_count > 1) {
2344 __u32 mc;
2345 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Luiz Capitulino222f1802006-03-20 22:16:13 -08002347 if (pkt_dev->flags & F_MACDST_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002348 mc = prandom_u32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Luiz Capitulino222f1802006-03-20 22:16:13 -08002350 else {
2351 mc = pkt_dev->cur_dst_mac_offset++;
Robert Olssonff2a79a2008-08-05 18:45:05 -07002352 if (pkt_dev->cur_dst_mac_offset >=
Luiz Capitulino222f1802006-03-20 22:16:13 -08002353 pkt_dev->dst_mac_count) {
2354 pkt_dev->cur_dst_mac_offset = 0;
2355 }
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Luiz Capitulino222f1802006-03-20 22:16:13 -08002358 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2359 pkt_dev->hh[5] = tmp;
2360 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2361 pkt_dev->hh[4] = tmp;
2362 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2363 pkt_dev->hh[3] = tmp;
2364 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2365 pkt_dev->hh[2] = tmp;
2366 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2367 pkt_dev->hh[1] = tmp;
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002370 if (pkt_dev->flags & F_MPLS_RND) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002371 unsigned int i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002372 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002373 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2374 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002375 ((__force __be32)prandom_u32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002376 htonl(0x000fffff));
2377 }
2378
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002379 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002380 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002381 }
2382
2383 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002384 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002385 }
2386
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2388 if (pkt_dev->flags & F_UDPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002389 pkt_dev->cur_udp_src = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002390 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2391 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Luiz Capitulino222f1802006-03-20 22:16:13 -08002393 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 pkt_dev->cur_udp_src++;
2395 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2396 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002397 }
2398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2401 if (pkt_dev->flags & F_UDPDST_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002402 pkt_dev->cur_udp_dst = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002403 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2404 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002405 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002407 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002409 }
2410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 if (!(pkt_dev->flags & F_IPV6)) {
2413
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002414 imn = ntohl(pkt_dev->saddr_min);
2415 imx = ntohl(pkt_dev->saddr_max);
2416 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002418 if (pkt_dev->flags & F_IPSRC_RND)
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002419 t = prandom_u32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 else {
2421 t = ntohl(pkt_dev->cur_saddr);
2422 t++;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002423 if (t > imx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 t = imn;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
2427 pkt_dev->cur_saddr = htonl(t);
2428 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002429
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002430 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2432 } else {
Al Viro252e33462006-11-14 20:48:11 -08002433 imn = ntohl(pkt_dev->daddr_min);
2434 imx = ntohl(pkt_dev->daddr_max);
2435 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 __u32 t;
Al Viro252e33462006-11-14 20:48:11 -08002437 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (pkt_dev->flags & F_IPDST_RND) {
2439
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002440 do {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002441 t = prandom_u32() %
2442 (imx - imn) + imn;
Al Viro252e33462006-11-14 20:48:11 -08002443 s = htonl(t);
Akinobu Mita70e3ba72013-04-29 16:21:41 -07002444 } while (ipv4_is_loopback(s) ||
2445 ipv4_is_multicast(s) ||
2446 ipv4_is_lbcast(s) ||
2447 ipv4_is_zeronet(s) ||
2448 ipv4_is_local_multicast(s));
Al Viro252e33462006-11-14 20:48:11 -08002449 pkt_dev->cur_daddr = s;
2450 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 t = ntohl(pkt_dev->cur_daddr);
2452 t++;
2453 if (t > imx) {
2454 t = imn;
2455 }
2456 pkt_dev->cur_daddr = htonl(t);
2457 }
2458 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002459 if (pkt_dev->cflows) {
Jamal Hadi Salim007a5312007-07-02 22:40:36 -07002460 pkt_dev->flows[flow].flags |= F_INIT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002461 pkt_dev->flows[flow].cur_daddr =
2462 pkt_dev->cur_daddr;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002463#ifdef CONFIG_XFRM
2464 if (pkt_dev->flags & F_IPSEC_ON)
2465 get_ipsec_sa(pkt_dev, flow);
2466#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 pkt_dev->nflows++;
2468 }
2469 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002470 } else { /* IPV6 * */
2471
Joe Perches06e30412012-10-18 17:55:31 +00002472 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 int i;
2474
2475 /* Only random destinations yet */
2476
Luiz Capitulino222f1802006-03-20 22:16:13 -08002477 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002479 (((__force __be32)prandom_u32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002480 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2481 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485
Luiz Capitulino222f1802006-03-20 22:16:13 -08002486 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2487 __u32 t;
2488 if (pkt_dev->flags & F_TXSIZE_RND) {
Akinobu Mita33d7c5e2013-04-29 16:21:40 -07002489 t = prandom_u32() %
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002490 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2491 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002492 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002494 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002496 }
2497 pkt_dev->cur_pkt_size = t;
2498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002500 set_cur_queue_map(pkt_dev);
Robert Olsson45b270f2007-08-28 15:45:55 -07002501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 pkt_dev->flows[flow].count++;
2503}
2504
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002505
2506#ifdef CONFIG_XFRM
Fengguang Wu5537a052014-01-06 11:00:07 +01002507static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
Fan Ducf93d47e2014-01-03 11:18:31 +08002508
2509 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2510};
2511
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002512static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2513{
2514 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2515 int err = 0;
Fan Du6de9ace2014-01-03 11:18:28 +08002516 struct net *net = dev_net(pkt_dev->odev);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002517
2518 if (!x)
2519 return 0;
2520 /* XXX: we dont support tunnel mode for now until
2521 * we resolve the dst issue */
Fan Ducf93d47e2014-01-03 11:18:31 +08002522 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002523 return 0;
2524
Fan Ducf93d47e2014-01-03 11:18:31 +08002525 /* But when user specify an valid SPI, transformation
2526 * supports both transport/tunnel mode + ESP/AH type.
2527 */
2528 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2529 skb->_skb_refdst = (unsigned long)&pkt_dev->dst | SKB_DST_NOREF;
2530
2531 rcu_read_lock_bh();
Herbert Xu13996372007-10-17 21:35:51 -07002532 err = x->outer_mode->output(x, skb);
Fan Ducf93d47e2014-01-03 11:18:31 +08002533 rcu_read_unlock_bh();
Fan Du6de9ace2014-01-03 11:18:28 +08002534 if (err) {
2535 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002536 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002537 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002538 err = x->type->output(x, skb);
Fan Du6de9ace2014-01-03 11:18:28 +08002539 if (err) {
2540 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002541 goto error;
Fan Du6de9ace2014-01-03 11:18:28 +08002542 }
Fan Du0af0a412014-01-03 11:18:27 +08002543 spin_lock_bh(&x->lock);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002544 x->curlft.bytes += skb->len;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002545 x->curlft.packets++;
Fan Du0af0a412014-01-03 11:18:27 +08002546 spin_unlock_bh(&x->lock);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002547error:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002548 return err;
2549}
2550
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002551static void free_SAs(struct pktgen_dev *pkt_dev)
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002552{
2553 if (pkt_dev->cflows) {
2554 /* let go of the SAs if we have them */
Paul Gortmakerd6182222010-10-18 12:14:44 +00002555 int i;
2556 for (i = 0; i < pkt_dev->cflows; i++) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002557 struct xfrm_state *x = pkt_dev->flows[i].x;
2558 if (x) {
2559 xfrm_state_put(x);
2560 pkt_dev->flows[i].x = NULL;
2561 }
2562 }
2563 }
2564}
2565
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002566static int process_ipsec(struct pktgen_dev *pkt_dev,
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002567 struct sk_buff *skb, __be16 protocol)
2568{
2569 if (pkt_dev->flags & F_IPSEC_ON) {
2570 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2571 int nhead = 0;
2572 if (x) {
2573 int ret;
2574 __u8 *eth;
fan.du38682042013-12-01 16:28:48 +08002575 struct iphdr *iph;
2576
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002577 nhead = x->props.header_len - skb_headroom(skb);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002578 if (nhead > 0) {
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002579 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2580 if (ret < 0) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002581 pr_err("Error expanding ipsec packet %d\n",
2582 ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002583 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002584 }
2585 }
2586
2587 /* ipsec is not expecting ll header */
2588 skb_pull(skb, ETH_HLEN);
2589 ret = pktgen_output_ipsec(skb, pkt_dev);
2590 if (ret) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00002591 pr_err("Error creating ipsec packet %d\n", ret);
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002592 goto err;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002593 }
2594 /* restore ll */
2595 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2596 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002597 *(u16 *) &eth[12] = protocol;
fan.du38682042013-12-01 16:28:48 +08002598
2599 /* Update IPv4 header len as well as checksum value */
2600 iph = ip_hdr(skb);
2601 iph->tot_len = htons(skb->len - ETH_HLEN);
2602 ip_send_check(iph);
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002603 }
2604 }
2605 return 1;
Ilpo Järvinenb4bb4ac2008-10-13 18:43:59 -07002606err:
2607 kfree_skb(skb);
2608 return 0;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002609}
2610#endif
2611
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002612static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2613{
Eric Dumazet95c96172012-04-15 05:58:06 +00002614 unsigned int i;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002615 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002616 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002617
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002618 mpls--;
2619 *mpls |= MPLS_STACK_BOTTOM;
2620}
2621
Al Viro0f37c602006-11-03 03:49:56 -08002622static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2623 unsigned int prio)
2624{
2625 return htons(id | (cfi << 12) | (prio << 13));
2626}
2627
Eric Dumazet26ad7872011-01-25 13:26:05 -08002628static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2629 int datalen)
2630{
2631 struct timeval timestamp;
2632 struct pktgen_hdr *pgh;
2633
2634 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2635 datalen -= sizeof(*pgh);
2636
2637 if (pkt_dev->nfrags <= 0) {
Daniel Turull05aebe22011-03-14 13:47:40 -07002638 memset(skb_put(skb, datalen), 0, datalen);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002639 } else {
2640 int frags = pkt_dev->nfrags;
2641 int i, len;
amit salecha7d36a992011-04-22 16:22:20 +00002642 int frag_len;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002643
2644
2645 if (frags > MAX_SKB_FRAGS)
2646 frags = MAX_SKB_FRAGS;
2647 len = datalen - frags * PAGE_SIZE;
2648 if (len > 0) {
2649 memset(skb_put(skb, len), 0, len);
2650 datalen = frags * PAGE_SIZE;
2651 }
2652
2653 i = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002654 frag_len = (datalen/frags) < PAGE_SIZE ?
2655 (datalen/frags) : PAGE_SIZE;
Eric Dumazet26ad7872011-01-25 13:26:05 -08002656 while (datalen > 0) {
2657 if (unlikely(!pkt_dev->page)) {
2658 int node = numa_node_id();
2659
2660 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2661 node = pkt_dev->node;
2662 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2663 if (!pkt_dev->page)
2664 break;
2665 }
Ian Campbella0bec1c2011-10-18 22:55:11 +00002666 get_page(pkt_dev->page);
Ian Campbellea2ab692011-08-22 23:44:58 +00002667 skb_frag_set_page(skb, i, pkt_dev->page);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002668 skb_shinfo(skb)->frags[i].page_offset = 0;
amit salecha7d36a992011-04-22 16:22:20 +00002669 /*last fragment, fill rest of data*/
2670 if (i == (frags - 1))
Eric Dumazet9e903e02011-10-18 21:00:24 +00002671 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2672 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
amit salecha7d36a992011-04-22 16:22:20 +00002673 else
Eric Dumazet9e903e02011-10-18 21:00:24 +00002674 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2675 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2676 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2677 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Eric Dumazet26ad7872011-01-25 13:26:05 -08002678 i++;
2679 skb_shinfo(skb)->nr_frags = i;
2680 }
Eric Dumazet26ad7872011-01-25 13:26:05 -08002681 }
2682
2683 /* Stamp the time, and sequence number,
2684 * convert them to network byte order
2685 */
2686 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2687 pgh->seq_num = htonl(pkt_dev->seq_num);
2688
2689 do_gettimeofday(&timestamp);
2690 pgh->tv_sec = htonl(timestamp.tv_sec);
2691 pgh->tv_usec = htonl(timestamp.tv_usec);
2692}
2693
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002694static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2695 struct pktgen_dev *pkt_dev,
2696 unsigned int extralen)
2697{
2698 struct sk_buff *skb = NULL;
2699 unsigned int size = pkt_dev->cur_pkt_size + 64 + extralen +
2700 pkt_dev->pkt_overhead;
2701
2702 if (pkt_dev->flags & F_NODE) {
2703 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2704
2705 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2706 if (likely(skb)) {
2707 skb_reserve(skb, NET_SKB_PAD);
2708 skb->dev = dev;
2709 }
2710 } else {
2711 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2712 }
2713
2714 return skb;
2715}
2716
Luiz Capitulino222f1802006-03-20 22:16:13 -08002717static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2718 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 struct sk_buff *skb = NULL;
2721 __u8 *eth;
2722 struct udphdr *udph;
2723 int datalen, iplen;
2724 struct iphdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002725 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002726 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002727 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2728 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2729 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2730 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002731 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002732
2733 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002734 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002735
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002736 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002737 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002738
Robert Olsson64053be2005-06-26 15:27:10 -07002739 /* Update any of the values, used when we're incrementing various
2740 * fields.
2741 */
2742 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002743 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002744
David S. Miller7ac54592006-01-18 14:19:10 -08002745 datalen = (odev->hard_header_len + 16) & ~0xf;
Robert Olssone99b99b2010-03-18 22:44:30 +00002746
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002747 skb = pktgen_alloc_skb(odev, pkt_dev, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (!skb) {
2749 sprintf(pkt_dev->result, "No memory");
2750 return NULL;
2751 }
2752
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002753 prefetchw(skb->data);
David S. Miller7ac54592006-01-18 14:19:10 -08002754 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 /* Reserve for ethernet and IP header */
2757 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002758 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2759 if (pkt_dev->nr_labels)
2760 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002761
2762 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002763 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002764 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002765 *svlan_tci = build_tci(pkt_dev->svlan_id,
2766 pkt_dev->svlan_cfi,
2767 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002768 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002769 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002770 }
2771 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002772 *vlan_tci = build_tci(pkt_dev->vlan_id,
2773 pkt_dev->vlan_cfi,
2774 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002775 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002776 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002777 }
2778
Thomas Graf525cebe2013-06-03 11:49:23 +00002779 skb_set_mac_header(skb, 0);
2780 skb_set_network_header(skb, skb->len);
2781 iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2782
2783 skb_set_transport_header(skb, skb->len);
2784 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002785 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002786 skb->priority = pkt_dev->skb_priority;
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e33462006-11-14 20:48:11 -08002789 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002791 /* Eth + IPh + UDPh + mpls */
2792 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002793 pkt_dev->pkt_overhead;
Nishank Trivedi6af773e2012-09-12 13:32:49 +00002794 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 udph->source = htons(pkt_dev->cur_udp_src);
2798 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002799 udph->len = htons(datalen + 8); /* DATA + udphdr */
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002800 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 iph->ihl = 5;
2803 iph->version = 4;
2804 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002805 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002806 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 iph->saddr = pkt_dev->cur_saddr;
2808 iph->daddr = pkt_dev->cur_daddr;
Eric Dumazet66ed1e52009-10-24 06:55:20 -07002809 iph->id = htons(pkt_dev->ip_id);
2810 pkt_dev->ip_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 iph->frag_off = 0;
2812 iplen = 20 + 8 + datalen;
2813 iph->tot_len = htons(iplen);
Thomas Graf03c633e2013-07-25 14:08:04 +02002814 ip_send_check(iph);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002815 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 skb->dev = odev;
2817 skb->pkt_type = PACKET_HOST;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002818
2819 if (!(pkt_dev->flags & F_UDPCSUM)) {
2820 skb->ip_summed = CHECKSUM_NONE;
2821 } else if (odev->features & NETIF_F_V4_CSUM) {
2822 skb->ip_summed = CHECKSUM_PARTIAL;
2823 skb->csum = 0;
2824 udp4_hwcsum(skb, udph->source, udph->dest);
2825 } else {
2826 __wsum csum = udp_csum(skb);
2827
2828 /* add protocol-dependent pseudo-header */
2829 udph->check = csum_tcpudp_magic(udph->source, udph->dest,
2830 datalen + 8, IPPROTO_UDP, csum);
2831
2832 if (udph->check == 0)
2833 udph->check = CSUM_MANGLED_0;
2834 }
2835
Eric Dumazet26ad7872011-01-25 13:26:05 -08002836 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002837
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07002838#ifdef CONFIG_XFRM
2839 if (!process_ipsec(pkt_dev, skb, protocol))
2840 return NULL;
2841#endif
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return skb;
2844}
2845
Luiz Capitulino222f1802006-03-20 22:16:13 -08002846static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2847 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 struct sk_buff *skb = NULL;
2850 __u8 *eth;
2851 struct udphdr *udph;
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002852 int datalen, udplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 struct ipv6hdr *iph;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002854 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002855 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002856 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2857 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2858 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2859 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002860 u16 queue_map;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002861
2862 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002863 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002864
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002865 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002866 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002867
Robert Olsson64053be2005-06-26 15:27:10 -07002868 /* Update any of the values, used when we're incrementing various
2869 * fields.
2870 */
2871 mod_cur_headers(pkt_dev);
Junchang Wangeb589062010-11-07 23:19:43 +00002872 queue_map = pkt_dev->cur_queue_map;
Robert Olsson64053be2005-06-26 15:27:10 -07002873
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002874 skb = pktgen_alloc_skb(odev, pkt_dev, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (!skb) {
2876 sprintf(pkt_dev->result, "No memory");
2877 return NULL;
2878 }
2879
Daniel Borkmann7a6e2882013-06-08 14:18:16 +02002880 prefetchw(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 skb_reserve(skb, 16);
2882
2883 /* Reserve for ethernet and IP header */
2884 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002885 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2886 if (pkt_dev->nr_labels)
2887 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002888
2889 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002890 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002891 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002892 *svlan_tci = build_tci(pkt_dev->svlan_id,
2893 pkt_dev->svlan_cfi,
2894 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002895 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002896 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002897 }
2898 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002899 *vlan_tci = build_tci(pkt_dev->vlan_id,
2900 pkt_dev->vlan_cfi,
2901 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002902 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002903 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002904 }
2905
Thomas Graf525cebe2013-06-03 11:49:23 +00002906 skb_set_mac_header(skb, 0);
2907 skb_set_network_header(skb, skb->len);
2908 iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2909
2910 skb_set_transport_header(skb, skb->len);
2911 udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002912 skb_set_queue_mapping(skb, queue_map);
John Fastabend9e50e3a2010-11-16 19:12:28 +00002913 skb->priority = pkt_dev->skb_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 memcpy(eth, pkt_dev->hh, 12);
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00002916 *(__be16 *) &eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002917
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002918 /* Eth + IPh + UDPh + mpls */
2919 datalen = pkt_dev->cur_pkt_size - 14 -
2920 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07002921 pkt_dev->pkt_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Amerigo Wang5aa8b572012-10-09 17:48:16 +00002923 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 datalen = sizeof(struct pktgen_hdr);
Joe Perchese87cc472012-05-13 21:56:26 +00002925 net_info_ratelimited("increased datalen to %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002928 udplen = datalen + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 udph->source = htons(pkt_dev->cur_udp_src);
2930 udph->dest = htons(pkt_dev->cur_udp_dst);
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002931 udph->len = htons(udplen);
2932 udph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002934 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002936 if (pkt_dev->traffic_class) {
2937 /* Version + traffic class + flow (0) */
Al Viro252e33462006-11-14 20:48:11 -08002938 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002939 }
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 iph->hop_limit = 32;
2942
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002943 iph->payload_len = htons(udplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 iph->nexthdr = IPPROTO_UDP;
2945
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002946 iph->daddr = pkt_dev->cur_in6_daddr;
2947 iph->saddr = pkt_dev->cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002949 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 skb->dev = odev;
2951 skb->pkt_type = PACKET_HOST;
2952
Thomas Grafc26bf4a2013-07-25 18:12:18 +02002953 if (!(pkt_dev->flags & F_UDPCSUM)) {
2954 skb->ip_summed = CHECKSUM_NONE;
2955 } else if (odev->features & NETIF_F_V6_CSUM) {
2956 skb->ip_summed = CHECKSUM_PARTIAL;
2957 skb->csum_start = skb_transport_header(skb) - skb->head;
2958 skb->csum_offset = offsetof(struct udphdr, check);
2959 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2960 } else {
2961 __wsum csum = udp_csum(skb);
2962
2963 /* add protocol-dependent pseudo-header */
2964 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2965
2966 if (udph->check == 0)
2967 udph->check = CSUM_MANGLED_0;
2968 }
2969
Eric Dumazet26ad7872011-01-25 13:26:05 -08002970 pktgen_finalize_skb(pkt_dev, skb, datalen);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return skb;
2973}
2974
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00002975static struct sk_buff *fill_packet(struct net_device *odev,
2976 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002978 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return fill_packet_ipv6(odev, pkt_dev);
2980 else
2981 return fill_packet_ipv4(odev, pkt_dev);
2982}
2983
Luiz Capitulino222f1802006-03-20 22:16:13 -08002984static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002986 pkt_dev->seq_num = 1;
2987 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002989 pkt_dev->tx_bytes = 0;
2990 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
2993/* Set up structure for sending pkts, clear counters */
2994
2995static void pktgen_run(struct pktgen_thread *t)
2996{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002997 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 int started = 0;
2999
Joe Perchesf9467ea2010-06-21 12:29:14 +00003000 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003002 rcu_read_lock();
3003 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005 /*
3006 * setup odev and create initial packet.
3007 */
3008 pktgen_setup_inject(pkt_dev);
3009
Luiz Capitulino222f1802006-03-20 22:16:13 -08003010 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 pktgen_clear_counters(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003013 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003014
Jamal Hadi Salim16dab722007-07-02 22:39:50 -07003015 set_pkt_overhead(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 strcpy(pkt_dev->result, "Starting");
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003018 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003020 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 strcpy(pkt_dev->result, "Error starting");
3022 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003023 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003024 if (started)
3025 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Cong Wang4e58a022013-01-28 19:55:53 +00003028static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003030 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Joe Perchesf9467ea2010-06-21 12:29:14 +00003032 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003034 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003035
Cong Wang4e58a022013-01-28 19:55:53 +00003036 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003037 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003038
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003039 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Stephen Hemminger648fda72009-08-27 13:55:07 +00003042static int thread_is_running(const struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Stephen Hemminger648fda72009-08-27 13:55:07 +00003044 const struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003046 rcu_read_lock();
3047 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3048 if (pkt_dev->running) {
3049 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003050 return 1;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003051 }
3052 rcu_read_unlock();
Stephen Hemminger648fda72009-08-27 13:55:07 +00003053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Luiz Capitulino222f1802006-03-20 22:16:13 -08003056static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003058 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Luiz Capitulino222f1802006-03-20 22:16:13 -08003060 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Luiz Capitulino222f1802006-03-20 22:16:13 -08003062 if (signal_pending(current))
3063 goto signal;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003064 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003065 return 1;
3066signal:
3067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
Cong Wang4e58a022013-01-28 19:55:53 +00003070static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003072 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003075 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003076
Cong Wang4e58a022013-01-28 19:55:53 +00003077 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003079 if (sig == 0)
3080 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003082
3083 if (sig == 0)
Cong Wang4e58a022013-01-28 19:55:53 +00003084 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003086
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003087 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return sig;
3089}
3090
Cong Wang4e58a022013-01-28 19:55:53 +00003091static void pktgen_run_all_threads(struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003093 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Joe Perchesf9467ea2010-06-21 12:29:14 +00003095 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003097 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Cong Wang4e58a022013-01-28 19:55:53 +00003099 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003101
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003102 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003104 /* Propagate thread->control */
3105 schedule_timeout_interruptible(msecs_to_jiffies(125));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003106
Cong Wang4e58a022013-01-28 19:55:53 +00003107 pktgen_wait_all_threads_run(pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Cong Wang4e58a022013-01-28 19:55:53 +00003110static void pktgen_reset_all_threads(struct pktgen_net *pn)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003111{
3112 struct pktgen_thread *t;
3113
Joe Perchesf9467ea2010-06-21 12:29:14 +00003114 func_enter();
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003115
3116 mutex_lock(&pktgen_thread_lock);
3117
Cong Wang4e58a022013-01-28 19:55:53 +00003118 list_for_each_entry(t, &pn->pktgen_threads, th_list)
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003119 t->control |= (T_REMDEVALL);
3120
3121 mutex_unlock(&pktgen_thread_lock);
3122
Stephen Hemminger63adc6f2009-08-27 13:55:19 +00003123 /* Propagate thread->control */
3124 schedule_timeout_interruptible(msecs_to_jiffies(125));
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003125
Cong Wang4e58a022013-01-28 19:55:53 +00003126 pktgen_wait_all_threads_run(pn);
Jesse Brandeburgeb37b412008-11-10 16:48:03 -08003127}
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3130{
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003131 __u64 bps, mbps, pps;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003132 char *p = pkt_dev->result;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003133 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3134 pkt_dev->started_at);
3135 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Daniel Turull03a14ab2011-03-09 14:11:00 -08003137 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003138 (unsigned long long)ktime_to_us(elapsed),
3139 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3140 (unsigned long long)ktime_to_us(idle),
Luiz Capitulino222f1802006-03-20 22:16:13 -08003141 (unsigned long long)pkt_dev->sofar,
3142 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003144 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3145 ktime_to_ns(elapsed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Luiz Capitulino222f1802006-03-20 22:16:13 -08003147 bps = pps * 8 * pkt_dev->cur_pkt_size;
3148
3149 mbps = bps;
3150 do_div(mbps, 1000000);
3151 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3152 (unsigned long long)pps,
3153 (unsigned long long)mbps,
3154 (unsigned long long)bps,
3155 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158/* Set stopped-at timer, remove from running list, do counters & statistics */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003161 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003162
Luiz Capitulino222f1802006-03-20 22:16:13 -08003163 if (!pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003164 pr_warning("interface: %s is already stopped\n",
3165 pkt_dev->odevname);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 return -EINVAL;
3167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003169 pkt_dev->running = 0;
Stephen Hemminger3bda06a2009-08-27 13:55:10 +00003170 kfree_skb(pkt_dev->skb);
3171 pkt_dev->skb = NULL;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003172 pkt_dev->stopped_at = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003174 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Luiz Capitulino222f1802006-03-20 22:16:13 -08003176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
3178
Luiz Capitulino222f1802006-03-20 22:16:13 -08003179static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003181 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003182
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003183 rcu_read_lock();
3184 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003185 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003186 continue;
3187 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003188 best = pkt_dev;
Daniel Borkmann398f3822012-10-28 08:27:19 +00003189 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003190 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003192 rcu_read_unlock();
3193
Luiz Capitulino222f1802006-03-20 22:16:13 -08003194 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
Luiz Capitulino222f1802006-03-20 22:16:13 -08003197static void pktgen_stop(struct pktgen_thread *t)
3198{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003199 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Joe Perchesf9467ea2010-06-21 12:29:14 +00003201 func_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003203 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003205 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003206 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003209 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003212/*
3213 * one of our devices needs to be removed - find it
3214 * and remove it
3215 */
3216static void pktgen_rem_one_if(struct pktgen_thread *t)
3217{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003218 struct list_head *q, *n;
3219 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003220
Joe Perchesf9467ea2010-06-21 12:29:14 +00003221 func_enter();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003222
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003223 list_for_each_safe(q, n, &t->if_list) {
3224 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003225
Luiz Capitulino222f1802006-03-20 22:16:13 -08003226 if (!cur->removal_mark)
3227 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003228
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003229 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003230 cur->skb = NULL;
3231
3232 pktgen_remove_device(t, cur);
3233
3234 break;
3235 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003236}
3237
Luiz Capitulino222f1802006-03-20 22:16:13 -08003238static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003240 struct list_head *q, *n;
3241 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003242
Joe Perchesf9467ea2010-06-21 12:29:14 +00003243 func_enter();
3244
Luiz Capitulino222f1802006-03-20 22:16:13 -08003245 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003246
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003247 list_for_each_safe(q, n, &t->if_list) {
3248 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003249
Wei Yongjun86dc1ad2009-02-25 00:31:54 +00003250 kfree_skb(cur->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003251 cur->skb = NULL;
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 pktgen_remove_device(t, cur);
3254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Luiz Capitulino222f1802006-03-20 22:16:13 -08003257static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003259 /* Remove from the thread list */
Cong Wang4e58a022013-01-28 19:55:53 +00003260 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Stephen Hemmingeref879792009-09-22 19:41:43 +00003263static void pktgen_resched(struct pktgen_dev *pkt_dev)
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003264{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003265 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003266 schedule();
Daniel Borkmann398f3822012-10-28 08:27:19 +00003267 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003268}
3269
Stephen Hemmingeref879792009-09-22 19:41:43 +00003270static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3271{
Daniel Borkmann398f3822012-10-28 08:27:19 +00003272 ktime_t idle_start = ktime_get();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003273
3274 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3275 if (signal_pending(current))
3276 break;
3277
3278 if (need_resched())
3279 pktgen_resched(pkt_dev);
3280 else
3281 cpu_relax();
3282 }
Daniel Borkmann398f3822012-10-28 08:27:19 +00003283 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
Stephen Hemmingeref879792009-09-22 19:41:43 +00003284}
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003285
Stephen Hemminger475ac1e2009-08-27 13:55:08 +00003286static void pktgen_xmit(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Stephen Hemminger00829822008-11-20 20:14:53 -08003288 struct net_device *odev = pkt_dev->odev;
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00003289 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
Stephen Hemminger00829822008-11-20 20:14:53 -08003290 = odev->netdev_ops->ndo_start_xmit;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003291 struct netdev_queue *txq;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003292 u16 queue_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 int ret;
3294
Stephen Hemmingeref879792009-09-22 19:41:43 +00003295 /* If device is offline, then don't send */
3296 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3297 pktgen_stop_device(pkt_dev);
Stephen Hemminger3791dec2009-08-27 13:55:11 +00003298 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003300
Stephen Hemmingeref879792009-09-22 19:41:43 +00003301 /* This is max DELAY, this has special meaning of
3302 * "never transmit"
3303 */
3304 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
Daniel Borkmann398f3822012-10-28 08:27:19 +00003305 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003306 return;
3307 }
3308
3309 /* If no skb or clone count exhausted then get new one */
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003310 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3311 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3312 /* build a new pkt */
3313 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003314
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003315 pkt_dev->skb = fill_packet(odev, pkt_dev);
3316 if (pkt_dev->skb == NULL) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003317 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003318 schedule();
3319 pkt_dev->clone_count--; /* back out increment, OOM */
3320 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
Eric Dumazetbaac8562009-11-05 21:04:32 -08003322 pkt_dev->last_pkt_size = pkt_dev->skb->len;
Stephen Hemminger7d7bb1c2009-08-27 13:55:12 +00003323 pkt_dev->allocated_skbs++;
3324 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003326
Stephen Hemmingeref879792009-09-22 19:41:43 +00003327 if (pkt_dev->delay && pkt_dev->last_ok)
3328 spin(pkt_dev, pkt_dev->next_tx);
3329
David S. Millerfd2ea0a2008-07-17 01:56:23 -07003330 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3331 txq = netdev_get_tx_queue(odev, queue_map);
3332
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003333 local_bh_disable();
3334
3335 HARD_TX_LOCK(odev, txq, smp_processor_id());
Stephen Hemmingeref879792009-09-22 19:41:43 +00003336
Daniel Borkmann6f25cd42014-04-07 17:18:30 +02003337 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003338 ret = NETDEV_TX_BUSY;
Eric Dumazet0835acf2009-09-30 13:03:33 +00003339 pkt_dev->last_ok = 0;
3340 goto unlock;
3341 }
3342 atomic_inc(&(pkt_dev->skb->users));
3343 ret = (*xmit)(pkt_dev->skb, odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Stephen Hemmingeref879792009-09-22 19:41:43 +00003345 switch (ret) {
3346 case NETDEV_TX_OK:
3347 txq_trans_update(txq);
3348 pkt_dev->last_ok = 1;
3349 pkt_dev->sofar++;
3350 pkt_dev->seq_num++;
Eric Dumazetbaac8562009-11-05 21:04:32 -08003351 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003352 break;
John Fastabendf466dba2009-12-23 22:02:57 -08003353 case NET_XMIT_DROP:
3354 case NET_XMIT_CN:
3355 case NET_XMIT_POLICED:
3356 /* skb has been consumed */
3357 pkt_dev->errors++;
3358 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003359 default: /* Drivers are not supposed to return other values! */
Joe Perchese87cc472012-05-13 21:56:26 +00003360 net_info_ratelimited("%s xmit error: %d\n",
3361 pkt_dev->odevname, ret);
Stephen Hemmingeref879792009-09-22 19:41:43 +00003362 pkt_dev->errors++;
3363 /* fallthru */
3364 case NETDEV_TX_LOCKED:
3365 case NETDEV_TX_BUSY:
3366 /* Retry it next time */
3367 atomic_dec(&(pkt_dev->skb->users));
3368 pkt_dev->last_ok = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003369 }
Eric Dumazet0835acf2009-09-30 13:03:33 +00003370unlock:
Daniel Borkmann0f2eea42014-04-11 13:22:00 +02003371 HARD_TX_UNLOCK(odev, txq);
3372
3373 local_bh_enable();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 /* If pkt_dev->count is zero, then run forever */
3376 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
Stephen Hemmingeref879792009-09-22 19:41:43 +00003377 pktgen_wait_for_skb(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /* Done with this */
3380 pktgen_stop_device(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003381 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003384/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 * Main loop of the thread goes here
3386 */
3387
David S. Milleree74baa2007-01-01 20:51:53 -08003388static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
3390 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003391 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003392 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
David S. Milleree74baa2007-01-01 20:51:53 -08003395 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 init_waitqueue_head(&t->queue);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003398 complete(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Joe Perchesf9467ea2010-06-21 12:29:14 +00003400 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003402 set_freezable();
3403
Jesper Dangaard Brouerbaac1672014-06-26 13:16:49 +02003404 __set_current_state(TASK_RUNNING);
3405
David S. Milleree74baa2007-01-01 20:51:53 -08003406 while (!kthread_should_stop()) {
3407 pkt_dev = next_to_run(t);
3408
Stephen Hemmingeref879792009-09-22 19:41:43 +00003409 if (unlikely(!pkt_dev && t->control == 0)) {
Cong Wang4e58a022013-01-28 19:55:53 +00003410 if (t->net->pktgen_exiting)
Eric Dumazet551eaff2010-11-21 10:26:44 -08003411 break;
Stephen Hemmingeref879792009-09-22 19:41:43 +00003412 wait_event_interruptible_timeout(t->queue,
3413 t->control != 0,
3414 HZ/10);
Rafael J. Wysocki1b3f7202010-02-04 14:00:41 -08003415 try_to_freeze();
Stephen Hemmingeref879792009-09-22 19:41:43 +00003416 continue;
David S. Milleree74baa2007-01-01 20:51:53 -08003417 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003418
Stephen Hemmingeref879792009-09-22 19:41:43 +00003419 if (likely(pkt_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 pktgen_xmit(pkt_dev);
3421
Stephen Hemmingeref879792009-09-22 19:41:43 +00003422 if (need_resched())
3423 pktgen_resched(pkt_dev);
3424 else
3425 cpu_relax();
3426 }
3427
Luiz Capitulino222f1802006-03-20 22:16:13 -08003428 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 pktgen_stop(t);
3430 t->control &= ~(T_STOP);
3431 }
3432
Luiz Capitulino222f1802006-03-20 22:16:13 -08003433 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 pktgen_run(t);
3435 t->control &= ~(T_RUN);
3436 }
3437
Luiz Capitulino222f1802006-03-20 22:16:13 -08003438 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003440 t->control &= ~(T_REMDEVALL);
3441 }
3442
Luiz Capitulino222f1802006-03-20 22:16:13 -08003443 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003444 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 t->control &= ~(T_REMDEV);
3446 }
3447
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003448 try_to_freeze();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003449 }
Jesper Dangaard Brouerbaac1672014-06-26 13:16:49 +02003450 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Joe Perchesf9467ea2010-06-21 12:29:14 +00003452 pr_debug("%s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003453 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Joe Perchesf9467ea2010-06-21 12:29:14 +00003455 pr_debug("%s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003456 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Joe Perchesf9467ea2010-06-21 12:29:14 +00003458 pr_debug("%s removing thread\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003459 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003460
Eric Dumazet551eaff2010-11-21 10:26:44 -08003461 /* Wait for kthread_stop */
3462 while (!kthread_should_stop()) {
3463 set_current_state(TASK_INTERRUPTIBLE);
3464 schedule();
3465 }
3466 __set_current_state(TASK_RUNNING);
3467
David S. Milleree74baa2007-01-01 20:51:53 -08003468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
Luiz Capitulino222f1802006-03-20 22:16:13 -08003471static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
Eric Dumazet3e984842009-11-24 14:50:53 -08003472 const char *ifname, bool exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003474 struct pktgen_dev *p, *pkt_dev = NULL;
Eric Dumazet3e984842009-11-24 14:50:53 -08003475 size_t len = strlen(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003477 rcu_read_lock();
3478 list_for_each_entry_rcu(p, &t->if_list, list)
Eric Dumazet3e984842009-11-24 14:50:53 -08003479 if (strncmp(p->odevname, ifname, len) == 0) {
3480 if (p->odevname[len]) {
3481 if (exact || p->odevname[len] != '@')
3482 continue;
3483 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003484 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003485 break;
3486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003488 rcu_read_unlock();
Joe Perchesf9467ea2010-06-21 12:29:14 +00003489 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003490 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003493/*
3494 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 */
3496
Luiz Capitulino222f1802006-03-20 22:16:13 -08003497static int add_dev_to_thread(struct pktgen_thread *t,
3498 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
3500 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003502 /* This function cannot be called concurrently, as its called
3503 * under pktgen_thread_lock mutex, but it can run from
3504 * userspace on another CPU than the kthread. The if_lock()
3505 * is used here to sync with concurrent instances of
3506 * _rem_dev_from_if_list() invoked via kthread, which is also
3507 * updating the if_list */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003508 if_lock(t);
3509
3510 if (pkt_dev->pg_thread) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003511 pr_err("ERROR: already assigned to a thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003512 rv = -EBUSY;
3513 goto out;
3514 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 pkt_dev->running = 0;
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003517 pkt_dev->pg_thread = t;
3518 list_add_rcu(&pkt_dev->list, &t->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Luiz Capitulino222f1802006-03-20 22:16:13 -08003520out:
3521 if_unlock(t);
3522 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
3525/* Called under thread lock */
3526
Luiz Capitulino222f1802006-03-20 22:16:13 -08003527static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003529 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003530 int err;
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003531 int node = cpu_to_node(t->cpu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 /* We don't allow a device to be on several threads */
3534
Cong Wang4e58a022013-01-28 19:55:53 +00003535 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003536 if (pkt_dev) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003537 pr_err("ERROR: interface already used\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003538 return -EBUSY;
3539 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003540
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003541 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003542 if (!pkt_dev)
3543 return -ENOMEM;
3544
Eric Dumazet593f63b2009-11-23 01:44:37 +00003545 strcpy(pkt_dev->odevname, ifname);
WANG Cong68d5ac22011-05-22 00:17:11 +00003546 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003547 node);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003548 if (pkt_dev->flows == NULL) {
3549 kfree(pkt_dev);
3550 return -ENOMEM;
3551 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003552
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003553 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003554 pkt_dev->nfrags = 0;
Stephen Hemmingerfd29cf72009-08-27 13:55:16 +00003555 pkt_dev->delay = pg_delay_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003556 pkt_dev->count = pg_count_d;
3557 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003558 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003559 pkt_dev->udp_src_max = 9;
3560 pkt_dev->udp_dst_min = 9;
3561 pkt_dev->udp_dst_max = 9;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003562 pkt_dev->vlan_p = 0;
3563 pkt_dev->vlan_cfi = 0;
3564 pkt_dev->vlan_id = 0xffff;
3565 pkt_dev->svlan_p = 0;
3566 pkt_dev->svlan_cfi = 0;
3567 pkt_dev->svlan_id = 0xffff;
Robert Olssone99b99b2010-03-18 22:44:30 +00003568 pkt_dev->node = -1;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003569
Cong Wang4e58a022013-01-28 19:55:53 +00003570 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003571 if (err)
3572 goto out1;
Neil Hormand8873312011-07-26 06:05:37 +00003573 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3574 pkt_dev->clone_skb = pg_clone_skb_d;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003575
Cong Wang4e58a022013-01-28 19:55:53 +00003576 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003577 &pktgen_if_fops, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003578 if (!pkt_dev->entry) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003579 pr_err("cannot create %s/%s procfs entry\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003580 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003581 err = -EINVAL;
3582 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003583 }
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003584#ifdef CONFIG_XFRM
3585 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3586 pkt_dev->ipsproto = IPPROTO_ESP;
Fan Ducf93d47e2014-01-03 11:18:31 +08003587
3588 /* xfrm tunnel mode needs additional dst to extract outter
3589 * ip header protocol/ttl/id field, here creat a phony one.
3590 * instead of looking for a valid rt, which definitely hurting
3591 * performance under such circumstance.
3592 */
3593 pkt_dev->dstops.family = AF_INET;
3594 pkt_dev->dst.dev = pkt_dev->odev;
3595 dst_init_metrics(&pkt_dev->dst, pktgen_dst_metrics, false);
3596 pkt_dev->dst.child = &pkt_dev->dst;
3597 pkt_dev->dst.ops = &pkt_dev->dstops;
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003598#endif
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003599
3600 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003601out2:
3602 dev_put(pkt_dev->odev);
3603out1:
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003604#ifdef CONFIG_XFRM
3605 free_SAs(pkt_dev);
3606#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003607 vfree(pkt_dev->flows);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003608 kfree(pkt_dev);
3609 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
Cong Wang4e58a022013-01-28 19:55:53 +00003612static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003614 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003615 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003616 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Eric Dumazet3291b9d2009-11-29 00:44:33 -08003618 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3619 cpu_to_node(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003620 if (!t) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003621 pr_err("ERROR: out of memory, can't create new thread\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003622 return -ENOMEM;
3623 }
3624
Luiz Capitulino222f1802006-03-20 22:16:13 -08003625 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003627
David S. Milleree74baa2007-01-01 20:51:53 -08003628 INIT_LIST_HEAD(&t->if_list);
3629
Cong Wang4e58a022013-01-28 19:55:53 +00003630 list_add_tail(&t->th_list, &pn->pktgen_threads);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003631 init_completion(&t->start_done);
David S. Milleree74baa2007-01-01 20:51:53 -08003632
Eric Dumazet94dcf292011-03-22 16:30:45 -07003633 p = kthread_create_on_node(pktgen_thread_worker,
3634 t,
3635 cpu_to_node(cpu),
3636 "kpktgend_%d", cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003637 if (IS_ERR(p)) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003638 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
David S. Milleree74baa2007-01-01 20:51:53 -08003639 list_del(&t->th_list);
3640 kfree(t);
3641 return PTR_ERR(p);
3642 }
3643 kthread_bind(p, cpu);
3644 t->tsk = p;
3645
Cong Wang4e58a022013-01-28 19:55:53 +00003646 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
Denis V. Lunev5efdccb2008-05-02 02:46:22 -07003647 &pktgen_thread_fops, t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003648 if (!pe) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003649 pr_err("cannot create %s/%s procfs entry\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003650 PG_PROC_DIR, t->tsk->comm);
3651 kthread_stop(p);
3652 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003653 kfree(t);
3654 return -EINVAL;
3655 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003656
Cong Wang4e58a022013-01-28 19:55:53 +00003657 t->net = pn;
David S. Milleree74baa2007-01-01 20:51:53 -08003658 wake_up_process(p);
Denis V. Lunevd3ede322008-05-20 15:12:44 -07003659 wait_for_completion(&t->start_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
3661 return 0;
3662}
3663
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003664/*
3665 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003667static void _rem_dev_from_if_list(struct pktgen_thread *t,
3668 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003670 struct list_head *q, *n;
3671 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003673 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003674 list_for_each_safe(q, n, &t->if_list) {
3675 p = list_entry(q, struct pktgen_dev, list);
3676 if (p == pkt_dev)
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003677 list_del_rcu(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003679 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681
Luiz Capitulino222f1802006-03-20 22:16:13 -08003682static int pktgen_remove_device(struct pktgen_thread *t,
3683 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
Joe Perchesf9467ea2010-06-21 12:29:14 +00003685 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
Luiz Capitulino222f1802006-03-20 22:16:13 -08003687 if (pkt_dev->running) {
Joe Perchesf9467ea2010-06-21 12:29:14 +00003688 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003689 pktgen_stop_device(pkt_dev);
3690 }
3691
3692 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
3694 if (pkt_dev->odev) {
3695 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003696 pkt_dev->odev = NULL;
3697 }
3698
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003699 /* Remove proc before if_list entry, because add_device uses
3700 * list to determine if interface already exist, avoid race
3701 * with proc_create_data() */
Stephen Hemminger39df2322007-03-04 16:11:51 -08003702 if (pkt_dev->entry)
David Howellsa8ca16e2013-04-12 17:27:28 +01003703 proc_remove(pkt_dev->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003705 /* And update the thread if_list */
3706 _rem_dev_from_if_list(t, pkt_dev);
3707
Jamal Hadi Salima553e4a2007-07-02 22:41:59 -07003708#ifdef CONFIG_XFRM
3709 free_SAs(pkt_dev);
3710#endif
Figo.zhang1d0ebfe2009-06-08 00:40:35 -07003711 vfree(pkt_dev->flows);
Eric Dumazet26ad7872011-01-25 13:26:05 -08003712 if (pkt_dev->page)
3713 put_page(pkt_dev->page);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003714 kfree_rcu(pkt_dev, rcu);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
3717
Cong Wang4e58a022013-01-28 19:55:53 +00003718static int __net_init pg_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719{
Cong Wang4e58a022013-01-28 19:55:53 +00003720 struct pktgen_net *pn = net_generic(net, pg_net_id);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003721 struct proc_dir_entry *pe;
Cong Wang4e58a022013-01-28 19:55:53 +00003722 int cpu, ret = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003723
Cong Wang4e58a022013-01-28 19:55:53 +00003724 pn->net = net;
3725 INIT_LIST_HEAD(&pn->pktgen_threads);
3726 pn->pktgen_exiting = false;
3727 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3728 if (!pn->proc_dir) {
3729 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003730 return -ENODEV;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003731 }
Cong Wang4e58a022013-01-28 19:55:53 +00003732 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3733 if (pe == NULL) {
3734 pr_err("cannot create %s procfs entry\n", PGCTRL);
3735 ret = -EINVAL;
3736 goto remove;
3737 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003738
John Hawkes670c02c2005-10-13 09:30:31 -07003739 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003740 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Cong Wang4e58a022013-01-28 19:55:53 +00003742 err = pktgen_create_thread(cpu, pn);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003743 if (err)
Cong Wang4e58a022013-01-28 19:55:53 +00003744 pr_warn("Cannot create thread for cpu %d (%d)\n",
Joe Perchesf9467ea2010-06-21 12:29:14 +00003745 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003746 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003747
Cong Wang4e58a022013-01-28 19:55:53 +00003748 if (list_empty(&pn->pktgen_threads)) {
3749 pr_err("Initialization failed for all threads\n");
WANG Congce14f892011-05-22 00:52:08 +00003750 ret = -ENODEV;
Cong Wang4e58a022013-01-28 19:55:53 +00003751 goto remove_entry;
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003752 }
3753
Luiz Capitulino222f1802006-03-20 22:16:13 -08003754 return 0;
WANG Congce14f892011-05-22 00:52:08 +00003755
Cong Wang4e58a022013-01-28 19:55:53 +00003756remove_entry:
3757 remove_proc_entry(PGCTRL, pn->proc_dir);
3758remove:
Gao fengece31ff2013-02-18 01:34:56 +00003759 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
WANG Congce14f892011-05-22 00:52:08 +00003760 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761}
3762
Cong Wang4e58a022013-01-28 19:55:53 +00003763static void __net_exit pg_net_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
Cong Wang4e58a022013-01-28 19:55:53 +00003765 struct pktgen_net *pn = net_generic(net, pg_net_id);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003766 struct pktgen_thread *t;
3767 struct list_head *q, *n;
Eric Dumazetd4b11332012-05-17 23:52:26 +00003768 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Luiz Capitulino222f1802006-03-20 22:16:13 -08003770 /* Stop all interfaces & threads */
Cong Wang4e58a022013-01-28 19:55:53 +00003771 pn->pktgen_exiting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Eric Dumazetc57b5462012-05-09 13:29:51 +00003773 mutex_lock(&pktgen_thread_lock);
Cong Wang4e58a022013-01-28 19:55:53 +00003774 list_splice_init(&pn->pktgen_threads, &list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003775 mutex_unlock(&pktgen_thread_lock);
3776
3777 list_for_each_safe(q, n, &list) {
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003778 t = list_entry(q, struct pktgen_thread, th_list);
Eric Dumazetc57b5462012-05-09 13:29:51 +00003779 list_del(&t->th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003780 kthread_stop(t->tsk);
3781 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Cong Wang4e58a022013-01-28 19:55:53 +00003784 remove_proc_entry(PGCTRL, pn->proc_dir);
Gao fengece31ff2013-02-18 01:34:56 +00003785 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
Cong Wang4e58a022013-01-28 19:55:53 +00003786}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Cong Wang4e58a022013-01-28 19:55:53 +00003788static struct pernet_operations pg_net_ops = {
3789 .init = pg_net_init,
3790 .exit = pg_net_exit,
3791 .id = &pg_net_id,
3792 .size = sizeof(struct pktgen_net),
3793};
3794
3795static int __init pg_init(void)
3796{
3797 int ret = 0;
3798
3799 pr_info("%s", version);
3800 ret = register_pernet_subsys(&pg_net_ops);
3801 if (ret)
3802 return ret;
3803 ret = register_netdevice_notifier(&pktgen_notifier_block);
3804 if (ret)
3805 unregister_pernet_subsys(&pg_net_ops);
3806
3807 return ret;
3808}
3809
3810static void __exit pg_cleanup(void)
3811{
3812 unregister_netdevice_notifier(&pktgen_notifier_block);
3813 unregister_pernet_subsys(&pg_net_ops);
Jesper Dangaard Brouer87883702014-06-26 13:16:59 +02003814 /* Don't need rcu_barrier() due to use of kfree_rcu() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815}
3816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817module_init(pg_init);
3818module_exit(pg_cleanup);
3819
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003820MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821MODULE_DESCRIPTION("Packet Generator tool");
3822MODULE_LICENSE("GPL");
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003823MODULE_VERSION(VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824module_param(pg_count_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003825MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826module_param(pg_delay_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003827MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828module_param(pg_clone_skb_d, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003829MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830module_param(debug, int, 0);
Stephen Hemmingerc3d2f522009-08-27 13:55:20 +00003831MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");