xref: /openbmc/linux/net/core/pktgen.c (revision b54c9d5bd6e38edac9ce3a3f95f14a1292b5268d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Authors:
41da177e4SLinus Torvalds  * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
51da177e4SLinus Torvalds  *                             Uppsala University and
61da177e4SLinus Torvalds  *                             Swedish University of Agricultural Sciences
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Alexey Kuznetsov  <kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds  * Ben Greear <greearb@candelatech.com>
1096de0e25SJan Engelhardt  * Jens Låås <jens.laas@data.slu.se>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * A tool for loading the network with preconfigurated packets.
131da177e4SLinus Torvalds  * The tool is implemented as a linux module.  Parameters are output
141da177e4SLinus Torvalds  * device, delay (to hard_xmit), number of packets, and whether
151da177e4SLinus Torvalds  * to use multiple SKBs or just the same one.
161da177e4SLinus Torvalds  * pktgen uses the installed interface's output routine.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * Additional hacking by:
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * Jens.Laas@data.slu.se
211da177e4SLinus Torvalds  * Improved by ANK. 010120.
221da177e4SLinus Torvalds  * Improved by ANK even more. 010212.
231da177e4SLinus Torvalds  * MAC address typo fixed. 010417 --ro
241da177e4SLinus Torvalds  * Integrated.  020301 --DaveM
251da177e4SLinus Torvalds  * Added multiskb option 020301 --DaveM
261da177e4SLinus Torvalds  * Scaling of results. 020417--sigurdur@linpro.no
271da177e4SLinus Torvalds  * Significant re-work of the module:
281da177e4SLinus Torvalds  *   *  Convert to threaded model to more efficiently be able to transmit
291da177e4SLinus Torvalds  *       and receive on multiple interfaces at once.
301da177e4SLinus Torvalds  *   *  Converted many counters to __u64 to allow longer runs.
311da177e4SLinus Torvalds  *   *  Allow configuration of ranges, like min/max IP address, MACs,
321da177e4SLinus Torvalds  *       and UDP-ports, for both source and destination, and can
331da177e4SLinus Torvalds  *       set to use a random distribution or sequentially walk the range.
341da177e4SLinus Torvalds  *   *  Can now change most values after starting.
351da177e4SLinus Torvalds  *   *  Place 12-byte packet in UDP payload with magic number,
361da177e4SLinus Torvalds  *       sequence number, and timestamp.
371da177e4SLinus Torvalds  *   *  Add receiver code that detects dropped pkts, re-ordered pkts, and
381da177e4SLinus Torvalds  *       latencies (with micro-second) precision.
391da177e4SLinus Torvalds  *   *  Add IOCTL interface to easily get counters & configuration.
401da177e4SLinus Torvalds  *   --Ben Greear <greearb@candelatech.com>
411da177e4SLinus Torvalds  *
421da177e4SLinus Torvalds  * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
431da177e4SLinus Torvalds  * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
441da177e4SLinus Torvalds  * as a "fastpath" with a configurable number of clones after alloc's.
451da177e4SLinus Torvalds  * clone_skb=0 means all packets are allocated this also means ranges time
461da177e4SLinus Torvalds  * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
471da177e4SLinus Torvalds  * clones.
481da177e4SLinus Torvalds  *
491da177e4SLinus Torvalds  * Also moved to /proc/net/pktgen/
501da177e4SLinus Torvalds  * --ro
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * Sept 10:  Fixed threading/locking.  Lots of bone-headed and more clever
531da177e4SLinus Torvalds  *    mistakes.  Also merged in DaveM's patch in the -pre6 patch.
541da177e4SLinus Torvalds  * --Ben Greear <greearb@candelatech.com>
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  * 021124 Finished major redesign and rewrite for new functionality.
591da177e4SLinus Torvalds  * See Documentation/networking/pktgen.txt for how to use this.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * The new operation:
621da177e4SLinus Torvalds  * For each CPU one thread/process is created at start. This process checks
631da177e4SLinus Torvalds  * for running devices in the if_list and sends packets until count is 0 it
641da177e4SLinus Torvalds  * also the thread checks the thread->control which is used for inter-process
651da177e4SLinus Torvalds  * communication. controlling process "posts" operations to the threads this
668788370aSJesper Dangaard Brouer  * way.
678788370aSJesper Dangaard Brouer  * The if_list is RCU protected, and the if_lock remains to protect updating
688788370aSJesper Dangaard Brouer  * of if_list, from "add_device" as it invoked from userspace (via proc write).
691da177e4SLinus Torvalds  *
701da177e4SLinus Torvalds  * By design there should only be *one* "controlling" process. In practice
711da177e4SLinus Torvalds  * multiple write accesses gives unpredictable result. Understood by "write"
721da177e4SLinus Torvalds  * to /proc gives result code thats should be read be the "writer".
73b4099fabSStephen Hemminger  * For practical use this should be no problem.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * Note when adding devices to a specific CPU there good idea to also assign
761da177e4SLinus Torvalds  * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
771da177e4SLinus Torvalds  * --ro
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  * Fix refcount off by one if first packet fails, potential null deref,
801da177e4SLinus Torvalds  * memleak 030710- KJP
811da177e4SLinus Torvalds  *
821da177e4SLinus Torvalds  * First "ranges" functionality for ipv6 030726 --ro
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * Included flow support. 030802 ANK.
851da177e4SLinus Torvalds  *
861da177e4SLinus Torvalds  * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
891da177e4SLinus Torvalds  * ia64 compilation fix from  Aron Griffis <aron@hp.com> 040604
901da177e4SLinus Torvalds  *
911da177e4SLinus Torvalds  * New xmit() return, do_div and misc clean up by Stephen Hemminger
921da177e4SLinus Torvalds  * <shemminger@osdl.org> 040923
931da177e4SLinus Torvalds  *
94ca9f1fd2SStephen Hemminger  * Randy Dunlap fixed u64 printk compiler warning
951da177e4SLinus Torvalds  *
961da177e4SLinus Torvalds  * Remove FCS from BW calculation.  Lennert Buytenhek <buytenh@wantstofly.org>
971da177e4SLinus Torvalds  * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * Corrections from Nikolai Malykh (nmalykh@bilim.com)
1001da177e4SLinus Torvalds  * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
1031da177e4SLinus Torvalds  * 050103
104ca6549afSSteven Whitehouse  *
105ca6549afSSteven Whitehouse  * MPLS support by Steven Whitehouse <steve@chygwyn.com>
106ca6549afSSteven Whitehouse  *
10734954ddcSFrancesco Fondelli  * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
10834954ddcSFrancesco Fondelli  *
109ce5d0b47SAdit Ranadive  * Fixed src_mac command to set source mac of packet to value specified in
110ce5d0b47SAdit Ranadive  * command by Adit Ranadive <adit.262@gmail.com>
1111da177e4SLinus Torvalds  */
112f9467eaeSJoe Perches 
113f9467eaeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
114f9467eaeSJoe Perches 
1151da177e4SLinus Torvalds #include <linux/sys.h>
1161da177e4SLinus Torvalds #include <linux/types.h>
1171da177e4SLinus Torvalds #include <linux/module.h>
1181da177e4SLinus Torvalds #include <linux/moduleparam.h>
1191da177e4SLinus Torvalds #include <linux/kernel.h>
120222fa076SLuiz Capitulino #include <linux/mutex.h>
1211da177e4SLinus Torvalds #include <linux/sched.h>
1221da177e4SLinus Torvalds #include <linux/slab.h>
1231da177e4SLinus Torvalds #include <linux/vmalloc.h>
1241da177e4SLinus Torvalds #include <linux/unistd.h>
1251da177e4SLinus Torvalds #include <linux/string.h>
1261da177e4SLinus Torvalds #include <linux/ptrace.h>
1271da177e4SLinus Torvalds #include <linux/errno.h>
1281da177e4SLinus Torvalds #include <linux/ioport.h>
1291da177e4SLinus Torvalds #include <linux/interrupt.h>
1304fc268d2SRandy Dunlap #include <linux/capability.h>
1312bc481cfSStephen Hemminger #include <linux/hrtimer.h>
13209fe3ef4SAndrew Morton #include <linux/freezer.h>
1331da177e4SLinus Torvalds #include <linux/delay.h>
1341da177e4SLinus Torvalds #include <linux/timer.h>
135cdcdbe0bSLuiz Capitulino #include <linux/list.h>
1361da177e4SLinus Torvalds #include <linux/init.h>
1371da177e4SLinus Torvalds #include <linux/skbuff.h>
1381da177e4SLinus Torvalds #include <linux/netdevice.h>
1391da177e4SLinus Torvalds #include <linux/inet.h>
1401da177e4SLinus Torvalds #include <linux/inetdevice.h>
1411da177e4SLinus Torvalds #include <linux/rtnetlink.h>
1421da177e4SLinus Torvalds #include <linux/if_arp.h>
14334954ddcSFrancesco Fondelli #include <linux/if_vlan.h>
1441da177e4SLinus Torvalds #include <linux/in.h>
1451da177e4SLinus Torvalds #include <linux/ip.h>
1461da177e4SLinus Torvalds #include <linux/ipv6.h>
1471da177e4SLinus Torvalds #include <linux/udp.h>
1481da177e4SLinus Torvalds #include <linux/proc_fs.h>
149d50a6b56SStephen Hemminger #include <linux/seq_file.h>
1501da177e4SLinus Torvalds #include <linux/wait.h>
151f404e9a6SKris Katterjohn #include <linux/etherdevice.h>
152ee74baa7SDavid S. Miller #include <linux/kthread.h>
153268bb0ceSLinus Torvalds #include <linux/prefetch.h>
15498fa15f3SAnshuman Khandual #include <linux/mmzone.h>
155457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1561da177e4SLinus Torvalds #include <net/checksum.h>
1571da177e4SLinus Torvalds #include <net/ipv6.h>
158c26bf4a5SThomas Graf #include <net/udp.h>
15973d94e94SStephen Rothwell #include <net/ip6_checksum.h>
1601da177e4SLinus Torvalds #include <net/addrconf.h>
161a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
162a553e4a6SJamal Hadi Salim #include <net/xfrm.h>
163a553e4a6SJamal Hadi Salim #endif
1644e58a027SCong Wang #include <net/netns/generic.h>
1651da177e4SLinus Torvalds #include <asm/byteorder.h>
1661da177e4SLinus Torvalds #include <linux/rcupdate.h>
1671977f032SJiri Slaby #include <linux/bitops.h>
16863adc6fbSStephen Hemminger #include <linux/io.h>
16963adc6fbSStephen Hemminger #include <linux/timex.h>
17063adc6fbSStephen Hemminger #include <linux/uaccess.h>
1711da177e4SLinus Torvalds #include <asm/dma.h>
1721da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1731da177e4SLinus Torvalds 
17440207264SJesper Dangaard Brouer #define VERSION	"2.75"
1751da177e4SLinus Torvalds #define IP_NAME_SZ 32
176ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
177d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1781da177e4SLinus Torvalds 
179f9467eaeSJoe Perches #define func_enter() pr_debug("entering %s\n", __func__);
180f9467eaeSJoe Perches 
1816f107c74SDmitry Safonov #define PKT_FLAGS							\
1826f107c74SDmitry Safonov 	pf(IPV6)		/* Interface in IPV6 Mode */		\
1836f107c74SDmitry Safonov 	pf(IPSRC_RND)		/* IP-Src Random  */			\
1846f107c74SDmitry Safonov 	pf(IPDST_RND)		/* IP-Dst Random  */			\
1856f107c74SDmitry Safonov 	pf(TXSIZE_RND)		/* Transmit size is random */		\
1866f107c74SDmitry Safonov 	pf(UDPSRC_RND)		/* UDP-Src Random */			\
1876f107c74SDmitry Safonov 	pf(UDPDST_RND)		/* UDP-Dst Random */			\
1886f107c74SDmitry Safonov 	pf(UDPCSUM)		/* Include UDP checksum */		\
1896f107c74SDmitry Safonov 	pf(NO_TIMESTAMP)	/* Don't timestamp packets (default TS) */ \
1906f107c74SDmitry Safonov 	pf(MPLS_RND)		/* Random MPLS labels */		\
1916f107c74SDmitry Safonov 	pf(QUEUE_MAP_RND)	/* queue map Random */			\
1926f107c74SDmitry Safonov 	pf(QUEUE_MAP_CPU)	/* queue map mirrors smp_processor_id() */ \
1936f107c74SDmitry Safonov 	pf(FLOW_SEQ)		/* Sequential flows */			\
1946f107c74SDmitry Safonov 	pf(IPSEC)		/* ipsec on for flows */		\
1956f107c74SDmitry Safonov 	pf(MACSRC_RND)		/* MAC-Src Random */			\
1966f107c74SDmitry Safonov 	pf(MACDST_RND)		/* MAC-Dst Random */			\
1976f107c74SDmitry Safonov 	pf(VID_RND)		/* Random VLAN ID */			\
1986f107c74SDmitry Safonov 	pf(SVID_RND)		/* Random SVLAN ID */			\
1996f107c74SDmitry Safonov 	pf(NODE)		/* Node memory alloc*/			\
2006f107c74SDmitry Safonov 
2016f107c74SDmitry Safonov #define pf(flag)		flag##_SHIFT,
2026f107c74SDmitry Safonov enum pkt_flags {
2036f107c74SDmitry Safonov 	PKT_FLAGS
2046f107c74SDmitry Safonov };
2056f107c74SDmitry Safonov #undef pf
2066f107c74SDmitry Safonov 
2071da177e4SLinus Torvalds /* Device flag bits */
2086f107c74SDmitry Safonov #define pf(flag)		static const __u32 F_##flag = (1<<flag##_SHIFT);
2096f107c74SDmitry Safonov PKT_FLAGS
2106f107c74SDmitry Safonov #undef pf
2111da177e4SLinus Torvalds 
21299c6d3d2SDmitry Safonov #define pf(flag)		__stringify(flag),
21399c6d3d2SDmitry Safonov static char *pkt_flag_names[] = {
21499c6d3d2SDmitry Safonov 	PKT_FLAGS
21599c6d3d2SDmitry Safonov };
21699c6d3d2SDmitry Safonov #undef pf
21799c6d3d2SDmitry Safonov 
21899c6d3d2SDmitry Safonov #define NR_PKT_FLAGS		ARRAY_SIZE(pkt_flag_names)
21999c6d3d2SDmitry Safonov 
2201da177e4SLinus Torvalds /* Thread control flag bits */
2216b80d6a6SStephen Hemminger #define T_STOP        (1<<0)	/* Stop run */
2226b80d6a6SStephen Hemminger #define T_RUN         (1<<1)	/* Start run */
2236b80d6a6SStephen Hemminger #define T_REMDEVALL   (1<<2)	/* Remove all devs */
2246b80d6a6SStephen Hemminger #define T_REMDEV      (1<<3)	/* Remove one dev */
2251da177e4SLinus Torvalds 
22662f64aedSAlexei Starovoitov /* Xmit modes */
22762f64aedSAlexei Starovoitov #define M_START_XMIT		0	/* Default normal TX */
22862f64aedSAlexei Starovoitov #define M_NETIF_RECEIVE 	1	/* Inject packets into stack */
2290967f244SJohn Fastabend #define M_QUEUE_XMIT		2	/* Inject packet into qdisc */
23062f64aedSAlexei Starovoitov 
2318788370aSJesper Dangaard Brouer /* If lock -- protects updating of if_list */
2329a0b1e8bSEric Dumazet #define   if_lock(t)           mutex_lock(&(t->if_lock));
2339a0b1e8bSEric Dumazet #define   if_unlock(t)           mutex_unlock(&(t->if_lock));
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2361da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
237d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
238d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds #define MAX_CFLOWS  65536
2411da177e4SLinus Torvalds 
24234954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
24334954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
24434954ddcSFrancesco Fondelli 
245222f1806SLuiz Capitulino struct flow_state {
246252e3346SAl Viro 	__be32 cur_daddr;
2471da177e4SLinus Torvalds 	int count;
248a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
249a553e4a6SJamal Hadi Salim 	struct xfrm_state *x;
250a553e4a6SJamal Hadi Salim #endif
251007a531bSJamal Hadi Salim 	__u32 flags;
2521da177e4SLinus Torvalds };
2531da177e4SLinus Torvalds 
254007a531bSJamal Hadi Salim /* flow flag bits */
255007a531bSJamal Hadi Salim #define F_INIT   (1<<0)		/* flow has been initialized */
256007a531bSJamal Hadi Salim 
2571da177e4SLinus Torvalds struct pktgen_dev {
2581da177e4SLinus Torvalds 	/*
2591da177e4SLinus Torvalds 	 * Try to keep frequent/infrequent used vars. separated.
2601da177e4SLinus Torvalds 	 */
26139df232fSStephen Hemminger 	struct proc_dir_entry *entry;	/* proc file */
2621da177e4SLinus Torvalds 	struct pktgen_thread *pg_thread;/* the owner */
26363adc6fbSStephen Hemminger 	struct list_head list;		/* chaining in the thread's run-queue */
2648788370aSJesper Dangaard Brouer 	struct rcu_head	 rcu;		/* freed by RCU */
2651da177e4SLinus Torvalds 
26663adc6fbSStephen Hemminger 	int running;		/* if false, the test will stop */
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	/* If min != max, then we will either do a linear iteration, or
2691da177e4SLinus Torvalds 	 * we will do a random selection from within the range.
2701da177e4SLinus Torvalds 	 */
2711da177e4SLinus Torvalds 	__u32 flags;
27262f64aedSAlexei Starovoitov 	int xmit_mode;
27368bf9f0bSAmerigo Wang 	int min_pkt_size;
27468bf9f0bSAmerigo Wang 	int max_pkt_size;
27516dab72fSJamal Hadi Salim 	int pkt_overhead;	/* overhead for MPLS, VLANs, IPSEC etc */
2761da177e4SLinus Torvalds 	int nfrags;
27762f64aedSAlexei Starovoitov 	int removal_mark;	/* non-zero => the device is marked for
27862f64aedSAlexei Starovoitov 				 * removal by worker thread */
27962f64aedSAlexei Starovoitov 
28026ad7879SEric Dumazet 	struct page *page;
281fd29cf72SStephen Hemminger 	u64 delay;		/* nano-seconds */
282fd29cf72SStephen Hemminger 
2831da177e4SLinus Torvalds 	__u64 count;		/* Default No packets to send */
2841da177e4SLinus Torvalds 	__u64 sofar;		/* How many pkts we've sent so far */
2851da177e4SLinus Torvalds 	__u64 tx_bytes;		/* How many bytes we've transmitted */
286f466dba1SJohn Fastabend 	__u64 errors;		/* Errors when trying to transmit, */
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	/* runtime counters relating to clone_skb */
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	__u32 clone_count;
2911da177e4SLinus Torvalds 	int last_ok;		/* Was last skb sent?
29263adc6fbSStephen Hemminger 				 * Or a failed transmit of some sort?
29363adc6fbSStephen Hemminger 				 * This will keep sequence numbers in order
2941da177e4SLinus Torvalds 				 */
295fd29cf72SStephen Hemminger 	ktime_t next_tx;
296fd29cf72SStephen Hemminger 	ktime_t started_at;
297fd29cf72SStephen Hemminger 	ktime_t stopped_at;
298fd29cf72SStephen Hemminger 	u64	idle_acc;	/* nano-seconds */
299fd29cf72SStephen Hemminger 
3001da177e4SLinus Torvalds 	__u32 seq_num;
3011da177e4SLinus Torvalds 
30263adc6fbSStephen Hemminger 	int clone_skb;		/*
30363adc6fbSStephen Hemminger 				 * Use multiple SKBs during packet gen.
30463adc6fbSStephen Hemminger 				 * If this number is greater than 1, then
30563adc6fbSStephen Hemminger 				 * that many copies of the same packet will be
30663adc6fbSStephen Hemminger 				 * sent before a new packet is allocated.
30763adc6fbSStephen Hemminger 				 * If you want to send 1024 identical packets
30863adc6fbSStephen Hemminger 				 * before creating a new packet,
30963adc6fbSStephen Hemminger 				 * set clone_skb to 1024.
3101da177e4SLinus Torvalds 				 */
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	char dst_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
3131da177e4SLinus Torvalds 	char dst_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
3141da177e4SLinus Torvalds 	char src_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
3151da177e4SLinus Torvalds 	char src_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	struct in6_addr in6_saddr;
3181da177e4SLinus Torvalds 	struct in6_addr in6_daddr;
3191da177e4SLinus Torvalds 	struct in6_addr cur_in6_daddr;
3201da177e4SLinus Torvalds 	struct in6_addr cur_in6_saddr;
3211da177e4SLinus Torvalds 	/* For ranges */
3221da177e4SLinus Torvalds 	struct in6_addr min_in6_daddr;
3231da177e4SLinus Torvalds 	struct in6_addr max_in6_daddr;
3241da177e4SLinus Torvalds 	struct in6_addr min_in6_saddr;
3251da177e4SLinus Torvalds 	struct in6_addr max_in6_saddr;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	/* If we're doing ranges, random or incremental, then this
3281da177e4SLinus Torvalds 	 * defines the min/max for those ranges.
3291da177e4SLinus Torvalds 	 */
330252e3346SAl Viro 	__be32 saddr_min;	/* inclusive, source IP address */
331252e3346SAl Viro 	__be32 saddr_max;	/* exclusive, source IP address */
332252e3346SAl Viro 	__be32 daddr_min;	/* inclusive, dest IP address */
333252e3346SAl Viro 	__be32 daddr_max;	/* exclusive, dest IP address */
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	__u16 udp_src_min;	/* inclusive, source UDP port */
3361da177e4SLinus Torvalds 	__u16 udp_src_max;	/* exclusive, source UDP port */
3371da177e4SLinus Torvalds 	__u16 udp_dst_min;	/* inclusive, dest UDP port */
3381da177e4SLinus Torvalds 	__u16 udp_dst_max;	/* exclusive, dest UDP port */
3391da177e4SLinus Torvalds 
3401ca7768cSFrancesco Fondelli 	/* DSCP + ECN */
34163adc6fbSStephen Hemminger 	__u8 tos;            /* six MSB of (former) IPv4 TOS
34263adc6fbSStephen Hemminger 				are for dscp codepoint */
34363adc6fbSStephen Hemminger 	__u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6
34463adc6fbSStephen Hemminger 				(see RFC 3260, sec. 4) */
3451ca7768cSFrancesco Fondelli 
346ca6549afSSteven Whitehouse 	/* MPLS */
34795c96174SEric Dumazet 	unsigned int nr_labels;	/* Depth of stack, 0 = no MPLS */
348ca6549afSSteven Whitehouse 	__be32 labels[MAX_MPLS_LABELS];
349ca6549afSSteven Whitehouse 
35034954ddcSFrancesco Fondelli 	/* VLAN/SVLAN (802.1Q/Q-in-Q) */
35134954ddcSFrancesco Fondelli 	__u8  vlan_p;
35234954ddcSFrancesco Fondelli 	__u8  vlan_cfi;
35334954ddcSFrancesco Fondelli 	__u16 vlan_id;  /* 0xffff means no vlan tag */
35434954ddcSFrancesco Fondelli 
35534954ddcSFrancesco Fondelli 	__u8  svlan_p;
35634954ddcSFrancesco Fondelli 	__u8  svlan_cfi;
35734954ddcSFrancesco Fondelli 	__u16 svlan_id; /* 0xffff means no svlan tag */
35834954ddcSFrancesco Fondelli 
3591da177e4SLinus Torvalds 	__u32 src_mac_count;	/* How many MACs to iterate through */
3601da177e4SLinus Torvalds 	__u32 dst_mac_count;	/* How many MACs to iterate through */
3611da177e4SLinus Torvalds 
362f404e9a6SKris Katterjohn 	unsigned char dst_mac[ETH_ALEN];
363f404e9a6SKris Katterjohn 	unsigned char src_mac[ETH_ALEN];
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	__u32 cur_dst_mac_offset;
3661da177e4SLinus Torvalds 	__u32 cur_src_mac_offset;
367252e3346SAl Viro 	__be32 cur_saddr;
368252e3346SAl Viro 	__be32 cur_daddr;
36966ed1e5eSEric Dumazet 	__u16 ip_id;
3701da177e4SLinus Torvalds 	__u16 cur_udp_dst;
3711da177e4SLinus Torvalds 	__u16 cur_udp_src;
37245b270f8SRobert Olsson 	__u16 cur_queue_map;
3731da177e4SLinus Torvalds 	__u32 cur_pkt_size;
374baac8564SEric Dumazet 	__u32 last_pkt_size;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	__u8 hh[14];
3771da177e4SLinus Torvalds 	/* = {
3781da177e4SLinus Torvalds 	   0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	   We fill in SRC address later
3811da177e4SLinus Torvalds 	   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3821da177e4SLinus Torvalds 	   0x08, 0x00
3831da177e4SLinus Torvalds 	   };
3841da177e4SLinus Torvalds 	 */
3851da177e4SLinus Torvalds 	__u16 pad;		/* pad out the hh struct to an even 16 bytes */
3861da177e4SLinus Torvalds 
38763adc6fbSStephen Hemminger 	struct sk_buff *skb;	/* skb we are to transmit next, used for when we
3881da177e4SLinus Torvalds 				 * are transmitting the same one multiple times
3891da177e4SLinus Torvalds 				 */
39063adc6fbSStephen Hemminger 	struct net_device *odev; /* The out-going device.
39163adc6fbSStephen Hemminger 				  * Note that the device should have it's
39263adc6fbSStephen Hemminger 				  * pg_info pointer pointing back to this
39363adc6fbSStephen Hemminger 				  * device.
39463adc6fbSStephen Hemminger 				  * Set when the user specifies the out-going
39563adc6fbSStephen Hemminger 				  * device name (not when the inject is
3961da177e4SLinus Torvalds 				  * started as it used to do.)
3971da177e4SLinus Torvalds 				  */
398593f63b0SEric Dumazet 	char odevname[32];
3991da177e4SLinus Torvalds 	struct flow_state *flows;
40095c96174SEric Dumazet 	unsigned int cflows;	/* Concurrent flows (config) */
40195c96174SEric Dumazet 	unsigned int lflow;		/* Flow length  (config) */
40295c96174SEric Dumazet 	unsigned int nflows;	/* accumulated flows (stats) */
40395c96174SEric Dumazet 	unsigned int curfl;		/* current sequenced flow (state)*/
40445b270f8SRobert Olsson 
40545b270f8SRobert Olsson 	u16 queue_map_min;
40645b270f8SRobert Olsson 	u16 queue_map_max;
4079e50e3acSJohn Fastabend 	__u32 skb_priority;	/* skb priority field */
40838b2cf29SAlexei Starovoitov 	unsigned int burst;	/* number of duplicated packets to burst */
409e99b99b4SRobert Olsson 	int node;               /* Memory node */
41045b270f8SRobert Olsson 
411a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
412a553e4a6SJamal Hadi Salim 	__u8	ipsmode;		/* IPSEC mode (config) */
413a553e4a6SJamal Hadi Salim 	__u8	ipsproto;		/* IPSEC type (config) */
414de4aee7dSFan Du 	__u32	spi;
415b6ca8bd5SDavid Miller 	struct xfrm_dst xdst;
416cf93d47eSFan Du 	struct dst_ops dstops;
417a553e4a6SJamal Hadi Salim #endif
41839df232fSStephen Hemminger 	char result[512];
4191da177e4SLinus Torvalds };
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds struct pktgen_hdr {
422252e3346SAl Viro 	__be32 pgh_magic;
423252e3346SAl Viro 	__be32 seq_num;
424252e3346SAl Viro 	__be32 tv_sec;
425252e3346SAl Viro 	__be32 tv_usec;
4261da177e4SLinus Torvalds };
4271da177e4SLinus Torvalds 
4284e58a027SCong Wang 
429c7d03a00SAlexey Dobriyan static unsigned int pg_net_id __read_mostly;
4304e58a027SCong Wang 
4314e58a027SCong Wang struct pktgen_net {
4324e58a027SCong Wang 	struct net		*net;
4334e58a027SCong Wang 	struct proc_dir_entry	*proc_dir;
4344e58a027SCong Wang 	struct list_head	pktgen_threads;
4354e58a027SCong Wang 	bool			pktgen_exiting;
4364e58a027SCong Wang };
437551eaff1SEric Dumazet 
4381da177e4SLinus Torvalds struct pktgen_thread {
4399a0b1e8bSEric Dumazet 	struct mutex if_lock;		/* for list of devices */
440c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
441cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
442ee74baa7SDavid S. Miller 	struct task_struct *tsk;
4431da177e4SLinus Torvalds 	char result[512];
4441da177e4SLinus Torvalds 
44563adc6fbSStephen Hemminger 	/* Field for thread to receive "posted" events terminate,
44663adc6fbSStephen Hemminger 	   stop ifs etc. */
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	u32 control;
4491da177e4SLinus Torvalds 	int cpu;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	wait_queue_head_t queue;
452d3ede327SDenis V. Lunev 	struct completion start_done;
4534e58a027SCong Wang 	struct pktgen_net *net;
4541da177e4SLinus Torvalds };
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds #define REMOVE 1
4571da177e4SLinus Torvalds #define FIND   0
4581da177e4SLinus Torvalds 
459c3d2f52dSStephen Hemminger static const char version[] =
460f9467eaeSJoe Perches 	"Packet Generator for packet performance testing. "
461f9467eaeSJoe Perches 	"Version: " VERSION "\n";
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4641da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
465222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
4663e984840SEric Dumazet 					  const char *ifname, bool exact);
4671da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
4684e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn);
4694e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn);
4704e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
4713bda06a3SStephen Hemminger 
4721da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4731da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
47439df232fSStephen Hemminger 
4751da177e4SLinus Torvalds /* Module parameters, defaults. */
47665c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
47765c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
47865c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
47965c5b786SStephen Hemminger static int debug  __read_mostly;
4801da177e4SLinus Torvalds 
481222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4841da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4851da177e4SLinus Torvalds };
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /*
4881da177e4SLinus Torvalds  * /proc handling functions
4891da177e4SLinus Torvalds  *
4901da177e4SLinus Torvalds  */
4911da177e4SLinus Torvalds 
492d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
493d50a6b56SStephen Hemminger {
494c3d2f52dSStephen Hemminger 	seq_puts(seq, version);
495d50a6b56SStephen Hemminger 	return 0;
496d50a6b56SStephen Hemminger }
4971da177e4SLinus Torvalds 
498d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user *buf,
4991da177e4SLinus Torvalds 			    size_t count, loff_t *ppos)
5001da177e4SLinus Torvalds {
501d50a6b56SStephen Hemminger 	char data[128];
5024e58a027SCong Wang 	struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
5031da177e4SLinus Torvalds 
50409455747SMathias Krause 	if (!capable(CAP_NET_ADMIN))
50509455747SMathias Krause 		return -EPERM;
5061da177e4SLinus Torvalds 
50720b0c718SMathias Krause 	if (count == 0)
50820b0c718SMathias Krause 		return -EINVAL;
50920b0c718SMathias Krause 
510d50a6b56SStephen Hemminger 	if (count > sizeof(data))
511d50a6b56SStephen Hemminger 		count = sizeof(data);
5121da177e4SLinus Torvalds 
51309455747SMathias Krause 	if (copy_from_user(data, buf, count))
51409455747SMathias Krause 		return -EFAULT;
51509455747SMathias Krause 
51620b0c718SMathias Krause 	data[count - 1] = 0;	/* Strip trailing '\n' and terminate string */
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
5194e58a027SCong Wang 		pktgen_stop_all_threads_ifs(pn);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
5224e58a027SCong Wang 		pktgen_run_all_threads(pn);
5231da177e4SLinus Torvalds 
524eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
5254e58a027SCong Wang 		pktgen_reset_all_threads(pn);
526eb37b41cSJesse Brandeburg 
5271da177e4SLinus Torvalds 	else
52840207264SJesper Dangaard Brouer 		return -EINVAL;
5291da177e4SLinus Torvalds 
53009455747SMathias Krause 	return count;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
533d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
5341da177e4SLinus Torvalds {
535d9dda78bSAl Viro 	return single_open(file, pgctrl_show, PDE_DATA(inode));
536d50a6b56SStephen Hemminger }
537d50a6b56SStephen Hemminger 
5389a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
539d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
540d50a6b56SStephen Hemminger 	.read    = seq_read,
541d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
542d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
543d50a6b56SStephen Hemminger 	.release = single_release,
544d50a6b56SStephen Hemminger };
545d50a6b56SStephen Hemminger 
546d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
547d50a6b56SStephen Hemminger {
548648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
549fd29cf72SStephen Hemminger 	ktime_t stopped;
55099c6d3d2SDmitry Safonov 	unsigned int i;
551fd29cf72SStephen Hemminger 	u64 idle;
5521da177e4SLinus Torvalds 
553222f1806SLuiz Capitulino 	seq_printf(seq,
554222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
555222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
556222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5571da177e4SLinus Torvalds 
558222f1806SLuiz Capitulino 	seq_printf(seq,
559fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
560fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
561593f63b0SEric Dumazet 		   pkt_dev->clone_skb, pkt_dev->odevname);
5621da177e4SLinus Torvalds 
563222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
564222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5651da177e4SLinus Torvalds 
56645b270f8SRobert Olsson 	seq_printf(seq,
56745b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
56845b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
56945b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
57045b270f8SRobert Olsson 
5719e50e3acSJohn Fastabend 	if (pkt_dev->skb_priority)
5729e50e3acSJohn Fastabend 		seq_printf(seq, "     skb_priority: %u\n",
5739e50e3acSJohn Fastabend 			   pkt_dev->skb_priority);
5749e50e3acSJohn Fastabend 
5751da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
576222f1806SLuiz Capitulino 		seq_printf(seq,
57747a0200dSAlexey Dobriyan 			   "     saddr: %pI6c  min_saddr: %pI6c  max_saddr: %pI6c\n"
57847a0200dSAlexey Dobriyan 			   "     daddr: %pI6c  min_daddr: %pI6c  max_daddr: %pI6c\n",
57947a0200dSAlexey Dobriyan 			   &pkt_dev->in6_saddr,
58047a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
58147a0200dSAlexey Dobriyan 			   &pkt_dev->in6_daddr,
58247a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
58363adc6fbSStephen Hemminger 	} else {
584222f1806SLuiz Capitulino 		seq_printf(seq,
58563adc6fbSStephen Hemminger 			   "     dst_min: %s  dst_max: %s\n",
58663adc6fbSStephen Hemminger 			   pkt_dev->dst_min, pkt_dev->dst_max);
58763adc6fbSStephen Hemminger 		seq_printf(seq,
58863adc6fbSStephen Hemminger 			   "     src_min: %s  src_max: %s\n",
58963adc6fbSStephen Hemminger 			   pkt_dev->src_min, pkt_dev->src_max);
59063adc6fbSStephen Hemminger 	}
5911da177e4SLinus Torvalds 
592d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5931da177e4SLinus Torvalds 
594e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
595e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
596e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5971da177e4SLinus Torvalds 
59897dc48e2SThomas Graf 	seq_puts(seq, "dst_mac: ");
599e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
6001da177e4SLinus Torvalds 
601222f1806SLuiz Capitulino 	seq_printf(seq,
60263adc6fbSStephen Hemminger 		   "     udp_src_min: %d  udp_src_max: %d"
60363adc6fbSStephen Hemminger 		   "  udp_dst_min: %d  udp_dst_max: %d\n",
604222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
605222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
6061da177e4SLinus Torvalds 
607222f1806SLuiz Capitulino 	seq_printf(seq,
608ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
6091da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
6101da177e4SLinus Torvalds 
611ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
61297dc48e2SThomas Graf 		seq_puts(seq, "     mpls: ");
613ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
614ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
615ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
616ca6549afSSteven Whitehouse 	}
617ca6549afSSteven Whitehouse 
61863adc6fbSStephen Hemminger 	if (pkt_dev->vlan_id != 0xffff)
61934954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
62063adc6fbSStephen Hemminger 			   pkt_dev->vlan_id, pkt_dev->vlan_p,
62163adc6fbSStephen Hemminger 			   pkt_dev->vlan_cfi);
62234954ddcSFrancesco Fondelli 
62363adc6fbSStephen Hemminger 	if (pkt_dev->svlan_id != 0xffff)
62434954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
62563adc6fbSStephen Hemminger 			   pkt_dev->svlan_id, pkt_dev->svlan_p,
62663adc6fbSStephen Hemminger 			   pkt_dev->svlan_cfi);
62734954ddcSFrancesco Fondelli 
62863adc6fbSStephen Hemminger 	if (pkt_dev->tos)
6291ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
6301ca7768cSFrancesco Fondelli 
63163adc6fbSStephen Hemminger 	if (pkt_dev->traffic_class)
6321ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
6331ca7768cSFrancesco Fondelli 
63438b2cf29SAlexei Starovoitov 	if (pkt_dev->burst > 1)
63538b2cf29SAlexei Starovoitov 		seq_printf(seq, "     burst: %d\n", pkt_dev->burst);
63638b2cf29SAlexei Starovoitov 
637e99b99b4SRobert Olsson 	if (pkt_dev->node >= 0)
638e99b99b4SRobert Olsson 		seq_printf(seq, "     node: %d\n", pkt_dev->node);
639e99b99b4SRobert Olsson 
64062f64aedSAlexei Starovoitov 	if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
64162f64aedSAlexei Starovoitov 		seq_puts(seq, "     xmit_mode: netif_receive\n");
6420967f244SJohn Fastabend 	else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
6430967f244SJohn Fastabend 		seq_puts(seq, "     xmit_mode: xmit_queue\n");
64462f64aedSAlexei Starovoitov 
64597dc48e2SThomas Graf 	seq_puts(seq, "     Flags: ");
646ca6549afSSteven Whitehouse 
64799c6d3d2SDmitry Safonov 	for (i = 0; i < NR_PKT_FLAGS; i++) {
64899c6d3d2SDmitry Safonov 		if (i == F_FLOW_SEQ)
64999c6d3d2SDmitry Safonov 			if (!pkt_dev->cflows)
65099c6d3d2SDmitry Safonov 				continue;
6511da177e4SLinus Torvalds 
65299c6d3d2SDmitry Safonov 		if (pkt_dev->flags & (1 << i))
65399c6d3d2SDmitry Safonov 			seq_printf(seq, "%s  ", pkt_flag_names[i]);
65499c6d3d2SDmitry Safonov 		else if (i == F_FLOW_SEQ)
65597dc48e2SThomas Graf 			seq_puts(seq, "FLOW_RND  ");
656007a531bSJamal Hadi Salim 
657a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
65899c6d3d2SDmitry Safonov 		if (i == F_IPSEC && pkt_dev->spi)
6598101328bSFan Du 			seq_printf(seq, "spi:%u", pkt_dev->spi);
660a553e4a6SJamal Hadi Salim #endif
66199c6d3d2SDmitry Safonov 	}
662e99b99b4SRobert Olsson 
663d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6641da177e4SLinus Torvalds 
665fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
666398f382cSDaniel Borkmann 	stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
667fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
668fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
6691da177e4SLinus Torvalds 
670222f1806SLuiz Capitulino 	seq_printf(seq,
671fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
6721da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
673fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
674fd29cf72SStephen Hemminger 
675fd29cf72SStephen Hemminger 	seq_printf(seq,
676fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
677fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
678fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
679fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
6801da177e4SLinus Torvalds 
681222f1806SLuiz Capitulino 	seq_printf(seq,
682222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
683d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
684d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
68747a0200dSAlexey Dobriyan 		seq_printf(seq, "     cur_saddr: %pI6c  cur_daddr: %pI6c\n",
68847a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_saddr,
68947a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_daddr);
690222f1806SLuiz Capitulino 	} else
6910373a946SAmerigo Wang 		seq_printf(seq, "     cur_saddr: %pI4  cur_daddr: %pI4\n",
6920373a946SAmerigo Wang 			   &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
6931da177e4SLinus Torvalds 
694d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
6951da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
6961da177e4SLinus Torvalds 
69745b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
69845b270f8SRobert Olsson 
699d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (pkt_dev->result[0])
702d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
7031da177e4SLinus Torvalds 	else
70497dc48e2SThomas Graf 		seq_puts(seq, "Result: Idle\n");
7051da177e4SLinus Torvalds 
706d50a6b56SStephen Hemminger 	return 0;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
709ca6549afSSteven Whitehouse 
71063adc6fbSStephen Hemminger static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
71163adc6fbSStephen Hemminger 		     __u32 *num)
712ca6549afSSteven Whitehouse {
713ca6549afSSteven Whitehouse 	int i = 0;
714ca6549afSSteven Whitehouse 	*num = 0;
715ca6549afSSteven Whitehouse 
7161ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
71782fd5b5dSAndy Shevchenko 		int value;
718ca6549afSSteven Whitehouse 		char c;
719ca6549afSSteven Whitehouse 		*num <<= 4;
720ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
721ca6549afSSteven Whitehouse 			return -EFAULT;
72282fd5b5dSAndy Shevchenko 		value = hex_to_bin(c);
72382fd5b5dSAndy Shevchenko 		if (value >= 0)
72482fd5b5dSAndy Shevchenko 			*num |= value;
725ca6549afSSteven Whitehouse 		else
726ca6549afSSteven Whitehouse 			break;
727ca6549afSSteven Whitehouse 	}
728ca6549afSSteven Whitehouse 	return i;
729ca6549afSSteven Whitehouse }
730ca6549afSSteven Whitehouse 
731222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
732222f1806SLuiz Capitulino 			     unsigned int maxlen)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	int i;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7371da177e4SLinus Torvalds 		char c;
7381da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7391da177e4SLinus Torvalds 			return -EFAULT;
7401da177e4SLinus Torvalds 		switch (c) {
7411da177e4SLinus Torvalds 		case '\"':
7421da177e4SLinus Torvalds 		case '\n':
7431da177e4SLinus Torvalds 		case '\r':
7441da177e4SLinus Torvalds 		case '\t':
7451da177e4SLinus Torvalds 		case ' ':
7461da177e4SLinus Torvalds 		case '=':
7471da177e4SLinus Torvalds 			break;
7481da177e4SLinus Torvalds 		default:
7491da177e4SLinus Torvalds 			goto done;
7503ff50b79SStephen Hemminger 		}
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds done:
7531da177e4SLinus Torvalds 	return i;
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
756bf0813bdSPaul Gortmaker static long num_arg(const char __user *user_buffer, unsigned long maxlen,
757bf0813bdSPaul Gortmaker 				unsigned long *num)
7581da177e4SLinus Torvalds {
759d6182223SPaul Gortmaker 	int i;
7601da177e4SLinus Torvalds 	*num = 0;
7611da177e4SLinus Torvalds 
762d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7631da177e4SLinus Torvalds 		char c;
7641da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7651da177e4SLinus Torvalds 			return -EFAULT;
7661da177e4SLinus Torvalds 		if ((c >= '0') && (c <= '9')) {
7671da177e4SLinus Torvalds 			*num *= 10;
7681da177e4SLinus Torvalds 			*num += c - '0';
7691da177e4SLinus Torvalds 		} else
7701da177e4SLinus Torvalds 			break;
7711da177e4SLinus Torvalds 	}
7721da177e4SLinus Torvalds 	return i;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
7761da177e4SLinus Torvalds {
777d6182223SPaul Gortmaker 	int i;
7781da177e4SLinus Torvalds 
779d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7801da177e4SLinus Torvalds 		char c;
7811da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7821da177e4SLinus Torvalds 			return -EFAULT;
7831da177e4SLinus Torvalds 		switch (c) {
7841da177e4SLinus Torvalds 		case '\"':
7851da177e4SLinus Torvalds 		case '\n':
7861da177e4SLinus Torvalds 		case '\r':
7871da177e4SLinus Torvalds 		case '\t':
7881da177e4SLinus Torvalds 		case ' ':
7891da177e4SLinus Torvalds 			goto done_str;
7901da177e4SLinus Torvalds 		default:
7911da177e4SLinus Torvalds 			break;
7923ff50b79SStephen Hemminger 		}
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds done_str:
7951da177e4SLinus Torvalds 	return i;
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
798ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
799ca6549afSSteven Whitehouse {
80095c96174SEric Dumazet 	unsigned int n = 0;
801ca6549afSSteven Whitehouse 	char c;
802ca6549afSSteven Whitehouse 	ssize_t i = 0;
803ca6549afSSteven Whitehouse 	int len;
804ca6549afSSteven Whitehouse 
805ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = 0;
806ca6549afSSteven Whitehouse 	do {
807ca6549afSSteven Whitehouse 		__u32 tmp;
8081ca7768cSFrancesco Fondelli 		len = hex32_arg(&buffer[i], 8, &tmp);
809ca6549afSSteven Whitehouse 		if (len <= 0)
810ca6549afSSteven Whitehouse 			return len;
811ca6549afSSteven Whitehouse 		pkt_dev->labels[n] = htonl(tmp);
812ca6549afSSteven Whitehouse 		if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
813ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
814ca6549afSSteven Whitehouse 		i += len;
815ca6549afSSteven Whitehouse 		if (get_user(c, &buffer[i]))
816ca6549afSSteven Whitehouse 			return -EFAULT;
817ca6549afSSteven Whitehouse 		i++;
818ca6549afSSteven Whitehouse 		n++;
819ca6549afSSteven Whitehouse 		if (n >= MAX_MPLS_LABELS)
820ca6549afSSteven Whitehouse 			return -E2BIG;
821ca6549afSSteven Whitehouse 	} while (c == ',');
822ca6549afSSteven Whitehouse 
823ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = n;
824ca6549afSSteven Whitehouse 	return i;
825ca6549afSSteven Whitehouse }
826ca6549afSSteven Whitehouse 
82752e12d5dSDmitry Safonov static __u32 pktgen_read_flag(const char *f, bool *disable)
82852e12d5dSDmitry Safonov {
82952e12d5dSDmitry Safonov 	__u32 i;
83052e12d5dSDmitry Safonov 
83152e12d5dSDmitry Safonov 	if (f[0] == '!') {
83252e12d5dSDmitry Safonov 		*disable = true;
83352e12d5dSDmitry Safonov 		f++;
83452e12d5dSDmitry Safonov 	}
83552e12d5dSDmitry Safonov 
83652e12d5dSDmitry Safonov 	for (i = 0; i < NR_PKT_FLAGS; i++) {
83752e12d5dSDmitry Safonov 		if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
83852e12d5dSDmitry Safonov 			continue;
83952e12d5dSDmitry Safonov 
84052e12d5dSDmitry Safonov 		/* allow only disabling ipv6 flag */
84152e12d5dSDmitry Safonov 		if (!*disable && i == IPV6_SHIFT)
84252e12d5dSDmitry Safonov 			continue;
84352e12d5dSDmitry Safonov 
84452e12d5dSDmitry Safonov 		if (strcmp(f, pkt_flag_names[i]) == 0)
84552e12d5dSDmitry Safonov 			return 1 << i;
84652e12d5dSDmitry Safonov 	}
84752e12d5dSDmitry Safonov 
84852e12d5dSDmitry Safonov 	if (strcmp(f, "FLOW_RND") == 0) {
84952e12d5dSDmitry Safonov 		*disable = !*disable;
85052e12d5dSDmitry Safonov 		return F_FLOW_SEQ;
85152e12d5dSDmitry Safonov 	}
85252e12d5dSDmitry Safonov 
85352e12d5dSDmitry Safonov 	return 0;
85452e12d5dSDmitry Safonov }
85552e12d5dSDmitry Safonov 
856222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file,
857222f1806SLuiz Capitulino 			       const char __user * user_buffer, size_t count,
858222f1806SLuiz Capitulino 			       loff_t * offset)
8591da177e4SLinus Torvalds {
8608a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
861d50a6b56SStephen Hemminger 	struct pktgen_dev *pkt_dev = seq->private;
862d6182223SPaul Gortmaker 	int i, max, len;
8631da177e4SLinus Torvalds 	char name[16], valstr[32];
8641da177e4SLinus Torvalds 	unsigned long value = 0;
8651da177e4SLinus Torvalds 	char *pg_result = NULL;
8661da177e4SLinus Torvalds 	int tmp = 0;
8671da177e4SLinus Torvalds 	char buf[128];
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	pg_result = &(pkt_dev->result[0]);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (count < 1) {
872294a0b7fSJoe Perches 		pr_warn("wrong command format\n");
8731da177e4SLinus Torvalds 		return -EINVAL;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 
876d6182223SPaul Gortmaker 	max = count;
877d6182223SPaul Gortmaker 	tmp = count_trail_chars(user_buffer, max);
8781da177e4SLinus Torvalds 	if (tmp < 0) {
879294a0b7fSJoe Perches 		pr_warn("illegal format\n");
8801da177e4SLinus Torvalds 		return tmp;
8811da177e4SLinus Torvalds 	}
882d6182223SPaul Gortmaker 	i = tmp;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	/* Read variable name */
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
88763adc6fbSStephen Hemminger 	if (len < 0)
888222f1806SLuiz Capitulino 		return len;
88963adc6fbSStephen Hemminger 
8901da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
8911da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
8921da177e4SLinus Torvalds 		return -EFAULT;
8931da177e4SLinus Torvalds 	i += len;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	max = count - i;
8961da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
8971da177e4SLinus Torvalds 	if (len < 0)
8981da177e4SLinus Torvalds 		return len;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	i += len;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	if (debug) {
903a870a02cSArnd Bergmann 		size_t copy = min_t(size_t, count + 1, 1024);
904a870a02cSArnd Bergmann 		char *tp = strndup_user(user_buffer, copy);
905a870a02cSArnd Bergmann 
906a870a02cSArnd Bergmann 		if (IS_ERR(tp))
907a870a02cSArnd Bergmann 			return PTR_ERR(tp);
908a870a02cSArnd Bergmann 
909a870a02cSArnd Bergmann 		pr_debug("%s,%zu  buffer -:%s:-\n", name, count, tp);
91029d1df72SGustavo A. R. Silva 		kfree(tp);
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	if (!strcmp(name, "min_pkt_size")) {
9141da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
91563adc6fbSStephen Hemminger 		if (len < 0)
916222f1806SLuiz Capitulino 			return len;
91763adc6fbSStephen Hemminger 
9181da177e4SLinus Torvalds 		i += len;
9191da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9201da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9211da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9221da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9231da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9241da177e4SLinus Torvalds 		}
925222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: min_pkt_size=%u",
926222f1806SLuiz Capitulino 			pkt_dev->min_pkt_size);
9271da177e4SLinus Torvalds 		return count;
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	if (!strcmp(name, "max_pkt_size")) {
9311da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
93263adc6fbSStephen Hemminger 		if (len < 0)
933222f1806SLuiz Capitulino 			return len;
93463adc6fbSStephen Hemminger 
9351da177e4SLinus Torvalds 		i += len;
9361da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9371da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9381da177e4SLinus Torvalds 		if (value != pkt_dev->max_pkt_size) {
9391da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9401da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9411da177e4SLinus Torvalds 		}
942222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: max_pkt_size=%u",
943222f1806SLuiz Capitulino 			pkt_dev->max_pkt_size);
9441da177e4SLinus Torvalds 		return count;
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	/* Shortcut for min = max */
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	if (!strcmp(name, "pkt_size")) {
9501da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
95163adc6fbSStephen Hemminger 		if (len < 0)
952222f1806SLuiz Capitulino 			return len;
95363adc6fbSStephen Hemminger 
9541da177e4SLinus Torvalds 		i += len;
9551da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9561da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9571da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9581da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9591da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9601da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9611da177e4SLinus Torvalds 		}
9621da177e4SLinus Torvalds 		sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
9631da177e4SLinus Torvalds 		return count;
9641da177e4SLinus Torvalds 	}
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	if (!strcmp(name, "debug")) {
9671da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
96863adc6fbSStephen Hemminger 		if (len < 0)
969222f1806SLuiz Capitulino 			return len;
97063adc6fbSStephen Hemminger 
9711da177e4SLinus Torvalds 		i += len;
9721da177e4SLinus Torvalds 		debug = value;
9731da177e4SLinus Torvalds 		sprintf(pg_result, "OK: debug=%u", debug);
9741da177e4SLinus Torvalds 		return count;
9751da177e4SLinus Torvalds 	}
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	if (!strcmp(name, "frags")) {
9781da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
97963adc6fbSStephen Hemminger 		if (len < 0)
980222f1806SLuiz Capitulino 			return len;
98163adc6fbSStephen Hemminger 
9821da177e4SLinus Torvalds 		i += len;
9831da177e4SLinus Torvalds 		pkt_dev->nfrags = value;
9841da177e4SLinus Torvalds 		sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
9851da177e4SLinus Torvalds 		return count;
9861da177e4SLinus Torvalds 	}
9871da177e4SLinus Torvalds 	if (!strcmp(name, "delay")) {
9881da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
98963adc6fbSStephen Hemminger 		if (len < 0)
990222f1806SLuiz Capitulino 			return len;
99163adc6fbSStephen Hemminger 
9921da177e4SLinus Torvalds 		i += len;
993fd29cf72SStephen Hemminger 		if (value == 0x7FFFFFFF)
994fd29cf72SStephen Hemminger 			pkt_dev->delay = ULLONG_MAX;
995fd29cf72SStephen Hemminger 		else
9969240d715SEric Dumazet 			pkt_dev->delay = (u64)value;
997fd29cf72SStephen Hemminger 
998fd29cf72SStephen Hemminger 		sprintf(pg_result, "OK: delay=%llu",
999fd29cf72SStephen Hemminger 			(unsigned long long) pkt_dev->delay);
10001da177e4SLinus Torvalds 		return count;
10011da177e4SLinus Torvalds 	}
100243d28b65SDaniel Turull 	if (!strcmp(name, "rate")) {
100343d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
100443d28b65SDaniel Turull 		if (len < 0)
100543d28b65SDaniel Turull 			return len;
100643d28b65SDaniel Turull 
100743d28b65SDaniel Turull 		i += len;
100843d28b65SDaniel Turull 		if (!value)
100943d28b65SDaniel Turull 			return len;
101043d28b65SDaniel Turull 		pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
101143d28b65SDaniel Turull 		if (debug)
1012f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
101343d28b65SDaniel Turull 
101443d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
101543d28b65SDaniel Turull 		return count;
101643d28b65SDaniel Turull 	}
101743d28b65SDaniel Turull 	if (!strcmp(name, "ratep")) {
101843d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
101943d28b65SDaniel Turull 		if (len < 0)
102043d28b65SDaniel Turull 			return len;
102143d28b65SDaniel Turull 
102243d28b65SDaniel Turull 		i += len;
102343d28b65SDaniel Turull 		if (!value)
102443d28b65SDaniel Turull 			return len;
102543d28b65SDaniel Turull 		pkt_dev->delay = NSEC_PER_SEC/value;
102643d28b65SDaniel Turull 		if (debug)
1027f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
102843d28b65SDaniel Turull 
102943d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
103043d28b65SDaniel Turull 		return count;
103143d28b65SDaniel Turull 	}
10321da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_min")) {
10331da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
103463adc6fbSStephen Hemminger 		if (len < 0)
1035222f1806SLuiz Capitulino 			return len;
103663adc6fbSStephen Hemminger 
10371da177e4SLinus Torvalds 		i += len;
10381da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_min) {
10391da177e4SLinus Torvalds 			pkt_dev->udp_src_min = value;
10401da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10411da177e4SLinus Torvalds 		}
10421da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
10431da177e4SLinus Torvalds 		return count;
10441da177e4SLinus Torvalds 	}
10451da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_min")) {
10461da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
104763adc6fbSStephen Hemminger 		if (len < 0)
1048222f1806SLuiz Capitulino 			return len;
104963adc6fbSStephen Hemminger 
10501da177e4SLinus Torvalds 		i += len;
10511da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_min) {
10521da177e4SLinus Torvalds 			pkt_dev->udp_dst_min = value;
10531da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10541da177e4SLinus Torvalds 		}
10551da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
10561da177e4SLinus Torvalds 		return count;
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_max")) {
10591da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
106063adc6fbSStephen Hemminger 		if (len < 0)
1061222f1806SLuiz Capitulino 			return len;
106263adc6fbSStephen Hemminger 
10631da177e4SLinus Torvalds 		i += len;
10641da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_max) {
10651da177e4SLinus Torvalds 			pkt_dev->udp_src_max = value;
10661da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10671da177e4SLinus Torvalds 		}
10681da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
10691da177e4SLinus Torvalds 		return count;
10701da177e4SLinus Torvalds 	}
10711da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_max")) {
10721da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
107363adc6fbSStephen Hemminger 		if (len < 0)
1074222f1806SLuiz Capitulino 			return len;
107563adc6fbSStephen Hemminger 
10761da177e4SLinus Torvalds 		i += len;
10771da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_max) {
10781da177e4SLinus Torvalds 			pkt_dev->udp_dst_max = value;
10791da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10801da177e4SLinus Torvalds 		}
10811da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
10821da177e4SLinus Torvalds 		return count;
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 	if (!strcmp(name, "clone_skb")) {
10851da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
108663adc6fbSStephen Hemminger 		if (len < 0)
1087222f1806SLuiz Capitulino 			return len;
1088d8873315SNeil Horman 		if ((value > 0) &&
108962f64aedSAlexei Starovoitov 		    ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
109062f64aedSAlexei Starovoitov 		     !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1091d8873315SNeil Horman 			return -ENOTSUPP;
10921da177e4SLinus Torvalds 		i += len;
10931da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
10961da177e4SLinus Torvalds 		return count;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
10991da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
110063adc6fbSStephen Hemminger 		if (len < 0)
1101222f1806SLuiz Capitulino 			return len;
110263adc6fbSStephen Hemminger 
11031da177e4SLinus Torvalds 		i += len;
11041da177e4SLinus Torvalds 		pkt_dev->count = value;
11051da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
11061da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
11071da177e4SLinus Torvalds 		return count;
11081da177e4SLinus Torvalds 	}
11091da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
11101da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
111163adc6fbSStephen Hemminger 		if (len < 0)
1112222f1806SLuiz Capitulino 			return len;
111363adc6fbSStephen Hemminger 
11141da177e4SLinus Torvalds 		i += len;
11151da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
11161da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
11171da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
11181da177e4SLinus Torvalds 		}
1119222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1120222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
11211da177e4SLinus Torvalds 		return count;
11221da177e4SLinus Torvalds 	}
11231da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
11241da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
112563adc6fbSStephen Hemminger 		if (len < 0)
1126222f1806SLuiz Capitulino 			return len;
112763adc6fbSStephen Hemminger 
11281da177e4SLinus Torvalds 		i += len;
11291da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
11301da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
11311da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
11321da177e4SLinus Torvalds 		}
1133222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1134222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
11351da177e4SLinus Torvalds 		return count;
11361da177e4SLinus Torvalds 	}
113738b2cf29SAlexei Starovoitov 	if (!strcmp(name, "burst")) {
113838b2cf29SAlexei Starovoitov 		len = num_arg(&user_buffer[i], 10, &value);
113938b2cf29SAlexei Starovoitov 		if (len < 0)
114038b2cf29SAlexei Starovoitov 			return len;
114138b2cf29SAlexei Starovoitov 
114238b2cf29SAlexei Starovoitov 		i += len;
11430967f244SJohn Fastabend 		if ((value > 1) &&
11440967f244SJohn Fastabend 		    ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
11450967f244SJohn Fastabend 		     ((pkt_dev->xmit_mode == M_START_XMIT) &&
11460967f244SJohn Fastabend 		     (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
114752d6c8c6SEric Dumazet 			return -ENOTSUPP;
114838b2cf29SAlexei Starovoitov 		pkt_dev->burst = value < 1 ? 1 : value;
114938b2cf29SAlexei Starovoitov 		sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
115038b2cf29SAlexei Starovoitov 		return count;
115138b2cf29SAlexei Starovoitov 	}
1152e99b99b4SRobert Olsson 	if (!strcmp(name, "node")) {
1153e99b99b4SRobert Olsson 		len = num_arg(&user_buffer[i], 10, &value);
1154e99b99b4SRobert Olsson 		if (len < 0)
1155e99b99b4SRobert Olsson 			return len;
1156e99b99b4SRobert Olsson 
1157e99b99b4SRobert Olsson 		i += len;
1158e99b99b4SRobert Olsson 
1159e99b99b4SRobert Olsson 		if (node_possible(value)) {
1160e99b99b4SRobert Olsson 			pkt_dev->node = value;
1161e99b99b4SRobert Olsson 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
116226ad7879SEric Dumazet 			if (pkt_dev->page) {
116326ad7879SEric Dumazet 				put_page(pkt_dev->page);
116426ad7879SEric Dumazet 				pkt_dev->page = NULL;
116526ad7879SEric Dumazet 			}
1166e99b99b4SRobert Olsson 		}
1167e99b99b4SRobert Olsson 		else
1168e99b99b4SRobert Olsson 			sprintf(pg_result, "ERROR: node not possible");
1169e99b99b4SRobert Olsson 		return count;
1170e99b99b4SRobert Olsson 	}
117162f64aedSAlexei Starovoitov 	if (!strcmp(name, "xmit_mode")) {
117262f64aedSAlexei Starovoitov 		char f[32];
117362f64aedSAlexei Starovoitov 
117462f64aedSAlexei Starovoitov 		memset(f, 0, 32);
117562f64aedSAlexei Starovoitov 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
117662f64aedSAlexei Starovoitov 		if (len < 0)
117762f64aedSAlexei Starovoitov 			return len;
117862f64aedSAlexei Starovoitov 
117962f64aedSAlexei Starovoitov 		if (copy_from_user(f, &user_buffer[i], len))
118062f64aedSAlexei Starovoitov 			return -EFAULT;
118162f64aedSAlexei Starovoitov 		i += len;
118262f64aedSAlexei Starovoitov 
118362f64aedSAlexei Starovoitov 		if (strcmp(f, "start_xmit") == 0) {
118462f64aedSAlexei Starovoitov 			pkt_dev->xmit_mode = M_START_XMIT;
118562f64aedSAlexei Starovoitov 		} else if (strcmp(f, "netif_receive") == 0) {
118662f64aedSAlexei Starovoitov 			/* clone_skb set earlier, not supported in this mode */
118762f64aedSAlexei Starovoitov 			if (pkt_dev->clone_skb > 0)
118862f64aedSAlexei Starovoitov 				return -ENOTSUPP;
118962f64aedSAlexei Starovoitov 
119062f64aedSAlexei Starovoitov 			pkt_dev->xmit_mode = M_NETIF_RECEIVE;
11919eea9222SAlexei Starovoitov 
11929eea9222SAlexei Starovoitov 			/* make sure new packet is allocated every time
11939eea9222SAlexei Starovoitov 			 * pktgen_xmit() is called
11949eea9222SAlexei Starovoitov 			 */
11959eea9222SAlexei Starovoitov 			pkt_dev->last_ok = 1;
11969eea9222SAlexei Starovoitov 
11979eea9222SAlexei Starovoitov 			/* override clone_skb if user passed default value
11989eea9222SAlexei Starovoitov 			 * at module loading time
11999eea9222SAlexei Starovoitov 			 */
12009eea9222SAlexei Starovoitov 			pkt_dev->clone_skb = 0;
12010967f244SJohn Fastabend 		} else if (strcmp(f, "queue_xmit") == 0) {
12020967f244SJohn Fastabend 			pkt_dev->xmit_mode = M_QUEUE_XMIT;
12030967f244SJohn Fastabend 			pkt_dev->last_ok = 1;
120462f64aedSAlexei Starovoitov 		} else {
120562f64aedSAlexei Starovoitov 			sprintf(pg_result,
120662f64aedSAlexei Starovoitov 				"xmit_mode -:%s:- unknown\nAvailable modes: %s",
120762f64aedSAlexei Starovoitov 				f, "start_xmit, netif_receive\n");
120862f64aedSAlexei Starovoitov 			return count;
120962f64aedSAlexei Starovoitov 		}
121062f64aedSAlexei Starovoitov 		sprintf(pg_result, "OK: xmit_mode=%s", f);
121162f64aedSAlexei Starovoitov 		return count;
121262f64aedSAlexei Starovoitov 	}
12131da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
121452e12d5dSDmitry Safonov 		__u32 flag;
12151da177e4SLinus Torvalds 		char f[32];
121652e12d5dSDmitry Safonov 		bool disable = false;
121752e12d5dSDmitry Safonov 
12181da177e4SLinus Torvalds 		memset(f, 0, 32);
12191da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
122063adc6fbSStephen Hemminger 		if (len < 0)
1221222f1806SLuiz Capitulino 			return len;
122263adc6fbSStephen Hemminger 
12231da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
12241da177e4SLinus Torvalds 			return -EFAULT;
12251da177e4SLinus Torvalds 		i += len;
12261da177e4SLinus Torvalds 
122752e12d5dSDmitry Safonov 		flag = pktgen_read_flag(f, &disable);
12281da177e4SLinus Torvalds 
122952e12d5dSDmitry Safonov 		if (flag) {
123052e12d5dSDmitry Safonov 			if (disable)
123152e12d5dSDmitry Safonov 				pkt_dev->flags &= ~flag;
123252e12d5dSDmitry Safonov 			else
123352e12d5dSDmitry Safonov 				pkt_dev->flags |= flag;
123452e12d5dSDmitry Safonov 		} else {
1235222f1806SLuiz Capitulino 			sprintf(pg_result,
1236222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
12371da177e4SLinus Torvalds 				f,
12381ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
123972f8e06fSMathias Krause 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
124072f8e06fSMathias Krause 				"MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
124172f8e06fSMathias Krause 				"QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
1242afb84b62SJesper Dangaard Brouer 				"NO_TIMESTAMP, "
124372f8e06fSMathias Krause #ifdef CONFIG_XFRM
124472f8e06fSMathias Krause 				"IPSEC, "
124572f8e06fSMathias Krause #endif
124672f8e06fSMathias Krause 				"NODE_ALLOC\n");
12471da177e4SLinus Torvalds 			return count;
12481da177e4SLinus Torvalds 		}
12491da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
12501da177e4SLinus Torvalds 		return count;
12511da177e4SLinus Torvalds 	}
12521da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
12531da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
125463adc6fbSStephen Hemminger 		if (len < 0)
1255222f1806SLuiz Capitulino 			return len;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12581da177e4SLinus Torvalds 			return -EFAULT;
12591da177e4SLinus Torvalds 		buf[len] = 0;
12601da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
12611da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1262f15f084fSJakub Kicinski 			strcpy(pkt_dev->dst_min, buf);
12631da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
12641da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
12651da177e4SLinus Torvalds 		}
12661da177e4SLinus Torvalds 		if (debug)
1267f342cda7SJoe Perches 			pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
12681da177e4SLinus Torvalds 		i += len;
12691da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
12701da177e4SLinus Torvalds 		return count;
12711da177e4SLinus Torvalds 	}
12721da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
12731da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
127463adc6fbSStephen Hemminger 		if (len < 0)
1275222f1806SLuiz Capitulino 			return len;
127663adc6fbSStephen Hemminger 
12771da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12781da177e4SLinus Torvalds 			return -EFAULT;
12791da177e4SLinus Torvalds 		buf[len] = 0;
12801da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
12811da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1282f15f084fSJakub Kicinski 			strcpy(pkt_dev->dst_max, buf);
12831da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
12841da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
12851da177e4SLinus Torvalds 		}
12861da177e4SLinus Torvalds 		if (debug)
1287f342cda7SJoe Perches 			pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
12881da177e4SLinus Torvalds 		i += len;
12891da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
12901da177e4SLinus Torvalds 		return count;
12911da177e4SLinus Torvalds 	}
12921da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
12931da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1294222f1806SLuiz Capitulino 		if (len < 0)
1295222f1806SLuiz Capitulino 			return len;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13001da177e4SLinus Torvalds 			return -EFAULT;
13011da177e4SLinus Torvalds 		buf[len] = 0;
13021da177e4SLinus Torvalds 
1303c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
130447a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
13051da177e4SLinus Torvalds 
13064e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 		if (debug)
1309f342cda7SJoe Perches 			pr_debug("dst6 set to: %s\n", buf);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 		i += len;
13121da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
13131da177e4SLinus Torvalds 		return count;
13141da177e4SLinus Torvalds 	}
13151da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
13161da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1317222f1806SLuiz Capitulino 		if (len < 0)
1318222f1806SLuiz Capitulino 			return len;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13231da177e4SLinus Torvalds 			return -EFAULT;
13241da177e4SLinus Torvalds 		buf[len] = 0;
13251da177e4SLinus Torvalds 
1326c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
132747a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
13281da177e4SLinus Torvalds 
13294e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
13301da177e4SLinus Torvalds 		if (debug)
1331f342cda7SJoe Perches 			pr_debug("dst6_min set to: %s\n", buf);
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 		i += len;
13341da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
13351da177e4SLinus Torvalds 		return count;
13361da177e4SLinus Torvalds 	}
13371da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
13381da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1339222f1806SLuiz Capitulino 		if (len < 0)
1340222f1806SLuiz Capitulino 			return len;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13451da177e4SLinus Torvalds 			return -EFAULT;
13461da177e4SLinus Torvalds 		buf[len] = 0;
13471da177e4SLinus Torvalds 
1348c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
134947a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 		if (debug)
1352f342cda7SJoe Perches 			pr_debug("dst6_max set to: %s\n", buf);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 		i += len;
13551da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
13561da177e4SLinus Torvalds 		return count;
13571da177e4SLinus Torvalds 	}
13581da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
13591da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1360222f1806SLuiz Capitulino 		if (len < 0)
1361222f1806SLuiz Capitulino 			return len;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13661da177e4SLinus Torvalds 			return -EFAULT;
13671da177e4SLinus Torvalds 		buf[len] = 0;
13681da177e4SLinus Torvalds 
1369c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
137047a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
13711da177e4SLinus Torvalds 
13724e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 		if (debug)
1375f342cda7SJoe Perches 			pr_debug("src6 set to: %s\n", buf);
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 		i += len;
13781da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
13791da177e4SLinus Torvalds 		return count;
13801da177e4SLinus Torvalds 	}
13811da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
13821da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
138363adc6fbSStephen Hemminger 		if (len < 0)
1384222f1806SLuiz Capitulino 			return len;
138563adc6fbSStephen Hemminger 
13861da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13871da177e4SLinus Torvalds 			return -EFAULT;
13881da177e4SLinus Torvalds 		buf[len] = 0;
13891da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
13901da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1391f15f084fSJakub Kicinski 			strcpy(pkt_dev->src_min, buf);
13921da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
13931da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
13941da177e4SLinus Torvalds 		}
13951da177e4SLinus Torvalds 		if (debug)
1396f342cda7SJoe Perches 			pr_debug("src_min set to: %s\n", pkt_dev->src_min);
13971da177e4SLinus Torvalds 		i += len;
13981da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
13991da177e4SLinus Torvalds 		return count;
14001da177e4SLinus Torvalds 	}
14011da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
14021da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
140363adc6fbSStephen Hemminger 		if (len < 0)
1404222f1806SLuiz Capitulino 			return len;
140563adc6fbSStephen Hemminger 
14061da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14071da177e4SLinus Torvalds 			return -EFAULT;
14081da177e4SLinus Torvalds 		buf[len] = 0;
14091da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
14101da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1411f15f084fSJakub Kicinski 			strcpy(pkt_dev->src_max, buf);
14121da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
14131da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
14141da177e4SLinus Torvalds 		}
14151da177e4SLinus Torvalds 		if (debug)
1416f342cda7SJoe Perches 			pr_debug("src_max set to: %s\n", pkt_dev->src_max);
14171da177e4SLinus Torvalds 		i += len;
14181da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
14191da177e4SLinus Torvalds 		return count;
14201da177e4SLinus Torvalds 	}
14211da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
14221da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
142363adc6fbSStephen Hemminger 		if (len < 0)
1424222f1806SLuiz Capitulino 			return len;
142563adc6fbSStephen Hemminger 
14261da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14271da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14281da177e4SLinus Torvalds 			return -EFAULT;
14291da177e4SLinus Torvalds 
14304940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->dst_mac))
14314940fc88SAlexey Dobriyan 			return -EINVAL;
14321da177e4SLinus Torvalds 		/* Set up Dest MAC */
14339ea08b12SJoe Perches 		ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
14341da177e4SLinus Torvalds 
14354940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
14361da177e4SLinus Torvalds 		return count;
14371da177e4SLinus Torvalds 	}
14381da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
14391da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
144063adc6fbSStephen Hemminger 		if (len < 0)
1441222f1806SLuiz Capitulino 			return len;
144263adc6fbSStephen Hemminger 
14431da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14441da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14451da177e4SLinus Torvalds 			return -EFAULT;
14461da177e4SLinus Torvalds 
14474940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->src_mac))
14484940fc88SAlexey Dobriyan 			return -EINVAL;
1449ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
14509ea08b12SJoe Perches 		ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
1451ce5d0b47SAdit Ranadive 
14524940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
14531da177e4SLinus Torvalds 		return count;
14541da177e4SLinus Torvalds 	}
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
14571da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
14581da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
14591da177e4SLinus Torvalds 		return count;
14601da177e4SLinus Torvalds 	}
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
14631da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
146463adc6fbSStephen Hemminger 		if (len < 0)
1465222f1806SLuiz Capitulino 			return len;
146663adc6fbSStephen Hemminger 
14671da177e4SLinus Torvalds 		i += len;
14681da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
14691da177e4SLinus Torvalds 			value = MAX_CFLOWS;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 		pkt_dev->cflows = value;
14721da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
14731da177e4SLinus Torvalds 		return count;
14741da177e4SLinus Torvalds 	}
14756bae9190SFan Du #ifdef CONFIG_XFRM
1476de4aee7dSFan Du 	if (!strcmp(name, "spi")) {
1477de4aee7dSFan Du 		len = num_arg(&user_buffer[i], 10, &value);
1478de4aee7dSFan Du 		if (len < 0)
1479de4aee7dSFan Du 			return len;
1480de4aee7dSFan Du 
1481de4aee7dSFan Du 		i += len;
1482de4aee7dSFan Du 		pkt_dev->spi = value;
1483de4aee7dSFan Du 		sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1484de4aee7dSFan Du 		return count;
1485de4aee7dSFan Du 	}
14866bae9190SFan Du #endif
14871da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
14881da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
148963adc6fbSStephen Hemminger 		if (len < 0)
1490222f1806SLuiz Capitulino 			return len;
149163adc6fbSStephen Hemminger 
14921da177e4SLinus Torvalds 		i += len;
14931da177e4SLinus Torvalds 		pkt_dev->lflow = value;
14941da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
14951da177e4SLinus Torvalds 		return count;
14961da177e4SLinus Torvalds 	}
14971da177e4SLinus Torvalds 
149845b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
149945b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
150063adc6fbSStephen Hemminger 		if (len < 0)
150145b270f8SRobert Olsson 			return len;
150263adc6fbSStephen Hemminger 
150345b270f8SRobert Olsson 		i += len;
150445b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
150545b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
150645b270f8SRobert Olsson 		return count;
150745b270f8SRobert Olsson 	}
150845b270f8SRobert Olsson 
150945b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
151045b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
151163adc6fbSStephen Hemminger 		if (len < 0)
151245b270f8SRobert Olsson 			return len;
151363adc6fbSStephen Hemminger 
151445b270f8SRobert Olsson 		i += len;
151545b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
151645b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
151745b270f8SRobert Olsson 		return count;
151845b270f8SRobert Olsson 	}
151945b270f8SRobert Olsson 
1520ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
152195c96174SEric Dumazet 		unsigned int n, cnt;
1522cfcabdccSStephen Hemminger 
1523ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1524cfcabdccSStephen Hemminger 		if (len < 0)
1525cfcabdccSStephen Hemminger 			return len;
1526ca6549afSSteven Whitehouse 		i += len;
1527cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1528ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1529cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1530ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1531ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
153234954ddcSFrancesco Fondelli 
153334954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
153434954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
153534954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
153634954ddcSFrancesco Fondelli 
153734954ddcSFrancesco Fondelli 			if (debug)
1538f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN auto turned off\n");
153934954ddcSFrancesco Fondelli 		}
154034954ddcSFrancesco Fondelli 		return count;
154134954ddcSFrancesco Fondelli 	}
154234954ddcSFrancesco Fondelli 
154334954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
154434954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
154563adc6fbSStephen Hemminger 		if (len < 0)
154634954ddcSFrancesco Fondelli 			return len;
154763adc6fbSStephen Hemminger 
154834954ddcSFrancesco Fondelli 		i += len;
154934954ddcSFrancesco Fondelli 		if (value <= 4095) {
155034954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
155134954ddcSFrancesco Fondelli 
155234954ddcSFrancesco Fondelli 			if (debug)
1553f342cda7SJoe Perches 				pr_debug("VLAN turned on\n");
155434954ddcSFrancesco Fondelli 
155534954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1556f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
155734954ddcSFrancesco Fondelli 
155834954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
155934954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
156034954ddcSFrancesco Fondelli 		} else {
156134954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
156234954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
156334954ddcSFrancesco Fondelli 
156434954ddcSFrancesco Fondelli 			if (debug)
1565f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
156634954ddcSFrancesco Fondelli 		}
156734954ddcSFrancesco Fondelli 		return count;
156834954ddcSFrancesco Fondelli 	}
156934954ddcSFrancesco Fondelli 
157034954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
157134954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
157263adc6fbSStephen Hemminger 		if (len < 0)
157334954ddcSFrancesco Fondelli 			return len;
157463adc6fbSStephen Hemminger 
157534954ddcSFrancesco Fondelli 		i += len;
157634954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
157734954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
157834954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
157934954ddcSFrancesco Fondelli 		} else {
158034954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
158134954ddcSFrancesco Fondelli 		}
158234954ddcSFrancesco Fondelli 		return count;
158334954ddcSFrancesco Fondelli 	}
158434954ddcSFrancesco Fondelli 
158534954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
158634954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
158763adc6fbSStephen Hemminger 		if (len < 0)
158834954ddcSFrancesco Fondelli 			return len;
158963adc6fbSStephen Hemminger 
159034954ddcSFrancesco Fondelli 		i += len;
159134954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
159234954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
159334954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
159434954ddcSFrancesco Fondelli 		} else {
159534954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
159634954ddcSFrancesco Fondelli 		}
159734954ddcSFrancesco Fondelli 		return count;
159834954ddcSFrancesco Fondelli 	}
159934954ddcSFrancesco Fondelli 
160034954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
160134954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
160263adc6fbSStephen Hemminger 		if (len < 0)
160334954ddcSFrancesco Fondelli 			return len;
160463adc6fbSStephen Hemminger 
160534954ddcSFrancesco Fondelli 		i += len;
160634954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
160734954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
160834954ddcSFrancesco Fondelli 
160934954ddcSFrancesco Fondelli 			if (debug)
1610f342cda7SJoe Perches 				pr_debug("SVLAN turned on\n");
161134954ddcSFrancesco Fondelli 
161234954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1613f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
161434954ddcSFrancesco Fondelli 
161534954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
161634954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
161734954ddcSFrancesco Fondelli 		} else {
161834954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
161934954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
162034954ddcSFrancesco Fondelli 
162134954ddcSFrancesco Fondelli 			if (debug)
1622f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
162334954ddcSFrancesco Fondelli 		}
162434954ddcSFrancesco Fondelli 		return count;
162534954ddcSFrancesco Fondelli 	}
162634954ddcSFrancesco Fondelli 
162734954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
162834954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
162963adc6fbSStephen Hemminger 		if (len < 0)
163034954ddcSFrancesco Fondelli 			return len;
163163adc6fbSStephen Hemminger 
163234954ddcSFrancesco Fondelli 		i += len;
163334954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
163434954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
163534954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
163634954ddcSFrancesco Fondelli 		} else {
163734954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
163834954ddcSFrancesco Fondelli 		}
163934954ddcSFrancesco Fondelli 		return count;
164034954ddcSFrancesco Fondelli 	}
164134954ddcSFrancesco Fondelli 
164234954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
164334954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
164463adc6fbSStephen Hemminger 		if (len < 0)
164534954ddcSFrancesco Fondelli 			return len;
164663adc6fbSStephen Hemminger 
164734954ddcSFrancesco Fondelli 		i += len;
164834954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
164934954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
165034954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
165134954ddcSFrancesco Fondelli 		} else {
165234954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
165334954ddcSFrancesco Fondelli 		}
1654ca6549afSSteven Whitehouse 		return count;
1655ca6549afSSteven Whitehouse 	}
1656ca6549afSSteven Whitehouse 
16571ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
16581ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16591ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
166063adc6fbSStephen Hemminger 		if (len < 0)
16611ca7768cSFrancesco Fondelli 			return len;
166263adc6fbSStephen Hemminger 
16631ca7768cSFrancesco Fondelli 		i += len;
16641ca7768cSFrancesco Fondelli 		if (len == 2) {
16651ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
16661ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
16671ca7768cSFrancesco Fondelli 		} else {
16681ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
16691ca7768cSFrancesco Fondelli 		}
16701ca7768cSFrancesco Fondelli 		return count;
16711ca7768cSFrancesco Fondelli 	}
16721ca7768cSFrancesco Fondelli 
16731ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
16741ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16751ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
167663adc6fbSStephen Hemminger 		if (len < 0)
16771ca7768cSFrancesco Fondelli 			return len;
167863adc6fbSStephen Hemminger 
16791ca7768cSFrancesco Fondelli 		i += len;
16801ca7768cSFrancesco Fondelli 		if (len == 2) {
16811ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
16821ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
16831ca7768cSFrancesco Fondelli 		} else {
16841ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
16851ca7768cSFrancesco Fondelli 		}
16861ca7768cSFrancesco Fondelli 		return count;
16871ca7768cSFrancesco Fondelli 	}
16881ca7768cSFrancesco Fondelli 
16899e50e3acSJohn Fastabend 	if (!strcmp(name, "skb_priority")) {
16909e50e3acSJohn Fastabend 		len = num_arg(&user_buffer[i], 9, &value);
16919e50e3acSJohn Fastabend 		if (len < 0)
16929e50e3acSJohn Fastabend 			return len;
16939e50e3acSJohn Fastabend 
16949e50e3acSJohn Fastabend 		i += len;
16959e50e3acSJohn Fastabend 		pkt_dev->skb_priority = value;
16969e50e3acSJohn Fastabend 		sprintf(pg_result, "OK: skb_priority=%i",
16979e50e3acSJohn Fastabend 			pkt_dev->skb_priority);
16989e50e3acSJohn Fastabend 		return count;
16999e50e3acSJohn Fastabend 	}
17009e50e3acSJohn Fastabend 
17011da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
17021da177e4SLinus Torvalds 	return -EINVAL;
17031da177e4SLinus Torvalds }
17041da177e4SLinus Torvalds 
1705d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
17061da177e4SLinus Torvalds {
1707d9dda78bSAl Viro 	return single_open(file, pktgen_if_show, PDE_DATA(inode));
17081da177e4SLinus Torvalds }
17091da177e4SLinus Torvalds 
17109a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1711d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1712d50a6b56SStephen Hemminger 	.read    = seq_read,
1713d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1714d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1715d50a6b56SStephen Hemminger 	.release = single_release,
1716d50a6b56SStephen Hemminger };
1717d50a6b56SStephen Hemminger 
1718d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1719d50a6b56SStephen Hemminger {
1720d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1721648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1722d50a6b56SStephen Hemminger 
1723d50a6b56SStephen Hemminger 	BUG_ON(!t);
1724d50a6b56SStephen Hemminger 
172597dc48e2SThomas Graf 	seq_puts(seq, "Running: ");
17261da177e4SLinus Torvalds 
17278788370aSJesper Dangaard Brouer 	rcu_read_lock();
17288788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
17291da177e4SLinus Torvalds 		if (pkt_dev->running)
1730593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17311da177e4SLinus Torvalds 
173297dc48e2SThomas Graf 	seq_puts(seq, "\nStopped: ");
17331da177e4SLinus Torvalds 
17348788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
17351da177e4SLinus Torvalds 		if (!pkt_dev->running)
1736593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	if (t->result[0])
1739d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
17401da177e4SLinus Torvalds 	else
174197dc48e2SThomas Graf 		seq_puts(seq, "\nResult: NA\n");
17421da177e4SLinus Torvalds 
17438788370aSJesper Dangaard Brouer 	rcu_read_unlock();
17441da177e4SLinus Torvalds 
1745d50a6b56SStephen Hemminger 	return 0;
17461da177e4SLinus Torvalds }
17471da177e4SLinus Torvalds 
1748d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1749d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1750d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
17511da177e4SLinus Torvalds {
17528a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
1753d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1754d6182223SPaul Gortmaker 	int i, max, len, ret;
17551da177e4SLinus Torvalds 	char name[40];
17561da177e4SLinus Torvalds 	char *pg_result;
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	if (count < 1) {
17591da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
17601da177e4SLinus Torvalds 		return -EINVAL;
17611da177e4SLinus Torvalds 	}
17621da177e4SLinus Torvalds 
1763d6182223SPaul Gortmaker 	max = count;
1764d6182223SPaul Gortmaker 	len = count_trail_chars(user_buffer, max);
17651da177e4SLinus Torvalds 	if (len < 0)
17661da177e4SLinus Torvalds 		return len;
17671da177e4SLinus Torvalds 
1768d6182223SPaul Gortmaker 	i = len;
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	/* Read variable name */
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
17731da177e4SLinus Torvalds 	if (len < 0)
17741da177e4SLinus Torvalds 		return len;
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
17771da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
17781da177e4SLinus Torvalds 		return -EFAULT;
17791da177e4SLinus Torvalds 	i += len;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	max = count - i;
17821da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
17831da177e4SLinus Torvalds 	if (len < 0)
17841da177e4SLinus Torvalds 		return len;
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 	i += len;
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	if (debug)
1789f342cda7SJoe Perches 		pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	if (!t) {
1792f9467eaeSJoe Perches 		pr_err("ERROR: No thread\n");
17931da177e4SLinus Torvalds 		ret = -EINVAL;
17941da177e4SLinus Torvalds 		goto out;
17951da177e4SLinus Torvalds 	}
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
18001da177e4SLinus Torvalds 		char f[32];
18011da177e4SLinus Torvalds 		memset(f, 0, 32);
18021da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
18031da177e4SLinus Torvalds 		if (len < 0) {
18041da177e4SLinus Torvalds 			ret = len;
18051da177e4SLinus Torvalds 			goto out;
18061da177e4SLinus Torvalds 		}
18071da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
18081da177e4SLinus Torvalds 			return -EFAULT;
18091da177e4SLinus Torvalds 		i += len;
18106146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
1811604dfd6eSCong Wang 		ret = pktgen_add_device(t, f);
18126146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1813604dfd6eSCong Wang 		if (!ret) {
18141da177e4SLinus Torvalds 			ret = count;
18151da177e4SLinus Torvalds 			sprintf(pg_result, "OK: add_device=%s", f);
1816604dfd6eSCong Wang 		} else
1817604dfd6eSCong Wang 			sprintf(pg_result, "ERROR: can not add device %s", f);
18181da177e4SLinus Torvalds 		goto out;
18191da177e4SLinus Torvalds 	}
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
18226146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
182395ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
18246146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1825121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
18261da177e4SLinus Torvalds 		ret = count;
18271da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
18281da177e4SLinus Torvalds 		goto out;
18291da177e4SLinus Torvalds 	}
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1832b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
18331da177e4SLinus Torvalds 		ret = count;
18341da177e4SLinus Torvalds 		goto out;
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	ret = -EINVAL;
18381da177e4SLinus Torvalds out:
18391da177e4SLinus Torvalds 	return ret;
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds 
1842d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
18431da177e4SLinus Torvalds {
1844d9dda78bSAl Viro 	return single_open(file, pktgen_thread_show, PDE_DATA(inode));
18451da177e4SLinus Torvalds }
18461da177e4SLinus Torvalds 
18479a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1848d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1849d50a6b56SStephen Hemminger 	.read    = seq_read,
1850d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1851d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1852d50a6b56SStephen Hemminger 	.release = single_release,
1853d50a6b56SStephen Hemminger };
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds /* Think find or remove for NN */
18564e58a027SCong Wang static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
18574e58a027SCong Wang 					      const char *ifname, int remove)
18581da177e4SLinus Torvalds {
18591da177e4SLinus Torvalds 	struct pktgen_thread *t;
18601da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
18613e984840SEric Dumazet 	bool exact = (remove == FIND);
18621da177e4SLinus Torvalds 
18634e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
18643e984840SEric Dumazet 		pkt_dev = pktgen_find_dev(t, ifname, exact);
18651da177e4SLinus Torvalds 		if (pkt_dev) {
18661da177e4SLinus Torvalds 			if (remove) {
186795ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
186895ed63f7SArthur Kepner 				t->control |= T_REMDEV;
18691da177e4SLinus Torvalds 			}
18701da177e4SLinus Torvalds 			break;
18711da177e4SLinus Torvalds 		}
18721da177e4SLinus Torvalds 	}
18731da177e4SLinus Torvalds 	return pkt_dev;
18741da177e4SLinus Torvalds }
18751da177e4SLinus Torvalds 
187695ed63f7SArthur Kepner /*
187795ed63f7SArthur Kepner  * mark a device for removal
187895ed63f7SArthur Kepner  */
18794e58a027SCong Wang static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
18801da177e4SLinus Torvalds {
18811da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
188295ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
188395ed63f7SArthur Kepner 	int i = 0;
188495ed63f7SArthur Kepner 
18856146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
1886f9467eaeSJoe Perches 	pr_debug("%s: marking %s for removal\n", __func__, ifname);
188795ed63f7SArthur Kepner 
188895ed63f7SArthur Kepner 	while (1) {
188995ed63f7SArthur Kepner 
18904e58a027SCong Wang 		pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1891222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1892222f1806SLuiz Capitulino 			break;	/* success */
189395ed63f7SArthur Kepner 
18946146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1895f9467eaeSJoe Perches 		pr_debug("%s: waiting for %s to disappear....\n",
1896f9467eaeSJoe Perches 			 __func__, ifname);
189795ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
18986146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
189995ed63f7SArthur Kepner 
190095ed63f7SArthur Kepner 		if (++i >= max_tries) {
1901f9467eaeSJoe Perches 			pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1902f9467eaeSJoe Perches 			       __func__, msec_per_try * i, ifname);
190395ed63f7SArthur Kepner 			break;
190495ed63f7SArthur Kepner 		}
190595ed63f7SArthur Kepner 
190695ed63f7SArthur Kepner 	}
190795ed63f7SArthur Kepner 
19086146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
190939df232fSStephen Hemminger }
191095ed63f7SArthur Kepner 
19114e58a027SCong Wang static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
191239df232fSStephen Hemminger {
191339df232fSStephen Hemminger 	struct pktgen_thread *t;
191439df232fSStephen Hemminger 
19159a0b1e8bSEric Dumazet 	mutex_lock(&pktgen_thread_lock);
19169a0b1e8bSEric Dumazet 
19174e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
191839df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
191939df232fSStephen Hemminger 
19209a0b1e8bSEric Dumazet 		if_lock(t);
19219a0b1e8bSEric Dumazet 		list_for_each_entry(pkt_dev, &t->if_list, list) {
192239df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
192339df232fSStephen Hemminger 				continue;
192439df232fSStephen Hemminger 
1925a8ca16eaSDavid Howells 			proc_remove(pkt_dev->entry);
192639df232fSStephen Hemminger 
19272975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
19284e58a027SCong Wang 							  pn->proc_dir,
19292975315bSAlexey Dobriyan 							  &pktgen_if_fops,
19302975315bSAlexey Dobriyan 							  pkt_dev);
193139df232fSStephen Hemminger 			if (!pkt_dev->entry)
1932f9467eaeSJoe Perches 				pr_err("can't move proc entry for '%s'\n",
1933f9467eaeSJoe Perches 				       dev->name);
193439df232fSStephen Hemminger 			break;
193539df232fSStephen Hemminger 		}
19369a0b1e8bSEric Dumazet 		if_unlock(t);
193739df232fSStephen Hemminger 	}
19389a0b1e8bSEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
19391da177e4SLinus Torvalds }
19401da177e4SLinus Torvalds 
1941222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
1942222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
19431da177e4SLinus Torvalds {
1944351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
19454e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
19461da177e4SLinus Torvalds 
19474e58a027SCong Wang 	if (pn->pktgen_exiting)
1948e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1949e9dc8653SEric W. Biederman 
19501da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
19511da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
19521da177e4SLinus Torvalds 	 */
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	switch (event) {
195539df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
19564e58a027SCong Wang 		pktgen_change_name(pn, dev);
19571da177e4SLinus Torvalds 		break;
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
19604e58a027SCong Wang 		pktgen_mark_device(pn, dev->name);
19611da177e4SLinus Torvalds 		break;
19623ff50b79SStephen Hemminger 	}
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	return NOTIFY_DONE;
19651da177e4SLinus Torvalds }
19661da177e4SLinus Torvalds 
19674e58a027SCong Wang static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
19684e58a027SCong Wang 						 struct pktgen_dev *pkt_dev,
196963adc6fbSStephen Hemminger 						 const char *ifname)
1970e6fce5b9SRobert Olsson {
1971e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
1972d6182223SPaul Gortmaker 	int i;
1973e6fce5b9SRobert Olsson 
1974e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
1975e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
1976e6fce5b9SRobert Olsson 			break;
1977e6fce5b9SRobert Olsson 
1978e6fce5b9SRobert Olsson 		b[i] = ifname[i];
1979e6fce5b9SRobert Olsson 	}
1980e6fce5b9SRobert Olsson 	b[i] = 0;
1981e6fce5b9SRobert Olsson 
19824e58a027SCong Wang 	return dev_get_by_name(pn->net, b);
1983e6fce5b9SRobert Olsson }
1984e6fce5b9SRobert Olsson 
1985e6fce5b9SRobert Olsson 
19861da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
19871da177e4SLinus Torvalds 
19884e58a027SCong Wang static int pktgen_setup_dev(const struct pktgen_net *pn,
19894e58a027SCong Wang 			    struct pktgen_dev *pkt_dev, const char *ifname)
1990222f1806SLuiz Capitulino {
19911da177e4SLinus Torvalds 	struct net_device *odev;
199239df232fSStephen Hemminger 	int err;
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	/* Clean old setups */
19951da177e4SLinus Torvalds 	if (pkt_dev->odev) {
19961da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
19971da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
19981da177e4SLinus Torvalds 	}
19991da177e4SLinus Torvalds 
20004e58a027SCong Wang 	odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
20011da177e4SLinus Torvalds 	if (!odev) {
2002f9467eaeSJoe Perches 		pr_err("no such netdevice: \"%s\"\n", ifname);
200339df232fSStephen Hemminger 		return -ENODEV;
20041da177e4SLinus Torvalds 	}
200539df232fSStephen Hemminger 
20061da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
2007f9467eaeSJoe Perches 		pr_err("not an ethernet device: \"%s\"\n", ifname);
200839df232fSStephen Hemminger 		err = -EINVAL;
200939df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
2010f9467eaeSJoe Perches 		pr_err("device is down: \"%s\"\n", ifname);
201139df232fSStephen Hemminger 		err = -ENETDOWN;
201239df232fSStephen Hemminger 	} else {
20131da177e4SLinus Torvalds 		pkt_dev->odev = odev;
201439df232fSStephen Hemminger 		return 0;
201539df232fSStephen Hemminger 	}
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	dev_put(odev);
201839df232fSStephen Hemminger 	return err;
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
20221da177e4SLinus Torvalds  * structure to have the right information to create/send packets
20231da177e4SLinus Torvalds  */
20241da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
20251da177e4SLinus Torvalds {
202664c00d81SAndrew Gallatin 	int ntxq;
202764c00d81SAndrew Gallatin 
20281da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
2029f9467eaeSJoe Perches 		pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2030222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
2031222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
20321da177e4SLinus Torvalds 		return;
20331da177e4SLinus Torvalds 	}
20341da177e4SLinus Torvalds 
203564c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
203664c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2037bfdbc0acSRobert Olsson 
203864c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
2039294a0b7fSJoe Perches 		pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
204088271660SJesse Brandeburg 			pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2041593f63b0SEric Dumazet 			pkt_dev->odevname);
204226e29eedSDan Carpenter 		pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
204364c00d81SAndrew Gallatin 	}
204488271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
2045294a0b7fSJoe Perches 		pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
204688271660SJesse Brandeburg 			pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2047593f63b0SEric Dumazet 			pkt_dev->odevname);
204826e29eedSDan Carpenter 		pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
204964c00d81SAndrew Gallatin 	}
205064c00d81SAndrew Gallatin 
20511da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
20521da177e4SLinus Torvalds 
2053f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
20549ea08b12SJoe Perches 		ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	/* Set up Dest MAC */
20579ea08b12SJoe Perches 	ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
20604c139b8cSAmerigo Wang 		int i, set = 0, err = 1;
20614c139b8cSAmerigo Wang 		struct inet6_dev *idev;
20624c139b8cSAmerigo Wang 
206368bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
206468bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
206568bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
206668bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
206768bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
206868bf9f0bSAmerigo Wang 		}
206968bf9f0bSAmerigo Wang 
2070df7e8e2eSEric Dumazet 		for (i = 0; i < sizeof(struct in6_addr); i++)
20711da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
20721da177e4SLinus Torvalds 				set = 1;
20731da177e4SLinus Torvalds 				break;
20741da177e4SLinus Torvalds 			}
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds 		if (!set) {
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 			/*
20791da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
20801da177e4SLinus Torvalds 			 *
20811da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
20821da177e4SLinus Torvalds 			 */
20831da177e4SLinus Torvalds 
20848814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
208563adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
208663adc6fbSStephen Hemminger 			if (idev) {
20871da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
20904c139b8cSAmerigo Wang 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
20914c139b8cSAmerigo Wang 					if ((ifp->scope & IFA_LINK) &&
2092f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
20934e3fd7a0SAlexey Dobriyan 						pkt_dev->cur_in6_saddr = ifp->addr;
20941da177e4SLinus Torvalds 						err = 0;
20951da177e4SLinus Torvalds 						break;
20961da177e4SLinus Torvalds 					}
20971da177e4SLinus Torvalds 				}
20981da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
20991da177e4SLinus Torvalds 			}
21008814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2101222f1806SLuiz Capitulino 			if (err)
2102f9467eaeSJoe Perches 				pr_err("ERROR: IPv6 link address not available\n");
21031da177e4SLinus Torvalds 		}
2104222f1806SLuiz Capitulino 	} else {
210568bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
210668bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
210768bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
210868bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
210968bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
211068bf9f0bSAmerigo Wang 		}
211168bf9f0bSAmerigo Wang 
21121da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
21131da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
21141da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 			struct in_device *in_dev;
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 			rcu_read_lock();
2119e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
21201da177e4SLinus Torvalds 			if (in_dev) {
21212638eb8bSFlorian Westphal 				const struct in_ifaddr *ifa;
21222638eb8bSFlorian Westphal 
21232638eb8bSFlorian Westphal 				ifa = rcu_dereference(in_dev->ifa_list);
21242638eb8bSFlorian Westphal 				if (ifa) {
21252638eb8bSFlorian Westphal 					pkt_dev->saddr_min = ifa->ifa_address;
21261da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
21271da177e4SLinus Torvalds 				}
21281da177e4SLinus Torvalds 			}
21291da177e4SLinus Torvalds 			rcu_read_unlock();
2130222f1806SLuiz Capitulino 		} else {
21311da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
21321da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
21331da177e4SLinus Torvalds 		}
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
21361da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
21371da177e4SLinus Torvalds 	}
21381da177e4SLinus Torvalds 	/* Initialize current values. */
213968bf9f0bSAmerigo Wang 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
214068bf9f0bSAmerigo Wang 	if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
214168bf9f0bSAmerigo Wang 		pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
214268bf9f0bSAmerigo Wang 
21431da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
21441da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
21451da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
21461da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
21471da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
21481da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
21491da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
21501da177e4SLinus Torvalds }
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 
2153fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2154fd29cf72SStephen Hemminger {
2155ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2156417bc4b8SEric Dumazet 	s64 remaining;
21572bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2158fd29cf72SStephen Hemminger 
21592bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
21602bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2161fd29cf72SStephen Hemminger 
216243d28b65SDaniel Turull 	remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2163bcf91bdbSGuenter Roeck 	if (remaining <= 0)
2164bcf91bdbSGuenter Roeck 		goto out;
21652bc481cfSStephen Hemminger 
2166398f382cSDaniel Borkmann 	start_time = ktime_get();
216733136d12SEric Dumazet 	if (remaining < 100000) {
216833136d12SEric Dumazet 		/* for small delays (<100us), just loop until limit is reached */
216933136d12SEric Dumazet 		do {
2170398f382cSDaniel Borkmann 			end_time = ktime_get();
2171398f382cSDaniel Borkmann 		} while (ktime_compare(end_time, spin_until) < 0);
217233136d12SEric Dumazet 	} else {
21732bc481cfSStephen Hemminger 		/* see do_nanosleep */
21742bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
21752bc481cfSStephen Hemminger 		do {
21762bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
21772bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
21782bc481cfSStephen Hemminger 
21792bc481cfSStephen Hemminger 			if (likely(t.task))
21801da177e4SLinus Torvalds 				schedule();
21811da177e4SLinus Torvalds 
21822bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
21832bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
21842bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
2185398f382cSDaniel Borkmann 		end_time = ktime_get();
218633136d12SEric Dumazet 	}
2187ef87979cSStephen Hemminger 
2188ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2189bcf91bdbSGuenter Roeck out:
219007a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2191bcf91bdbSGuenter Roeck 	destroy_hrtimer_on_stack(&t.timer);
21921da177e4SLinus Torvalds }
21931da177e4SLinus Torvalds 
219416dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
219516dab72fSJamal Hadi Salim {
219663d75463SPaolo Abeni 	pkt_dev->pkt_overhead = 0;
219716dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
219816dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
219916dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
220016dab72fSJamal Hadi Salim }
220116dab72fSJamal Hadi Salim 
2202648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2203007a531bSJamal Hadi Salim {
2204648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2205007a531bSJamal Hadi Salim }
2206007a531bSJamal Hadi Salim 
2207007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2208007a531bSJamal Hadi Salim {
2209007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2210007a531bSJamal Hadi Salim 
2211007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2212007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2213007a531bSJamal Hadi Salim 			/* reset time */
2214007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22151211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2216007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2217007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2218007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2219007a531bSJamal Hadi Salim 		}
2220007a531bSJamal Hadi Salim 	} else {
222133d7c5e5SAkinobu Mita 		flow = prandom_u32() % pkt_dev->cflows;
22221211a645SRobert Olsson 		pkt_dev->curfl = flow;
2223007a531bSJamal Hadi Salim 
22241211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2225007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22261211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
22271211a645SRobert Olsson 		}
2228007a531bSJamal Hadi Salim 	}
2229007a531bSJamal Hadi Salim 
2230007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2231007a531bSJamal Hadi Salim }
2232007a531bSJamal Hadi Salim 
2233a553e4a6SJamal Hadi Salim 
2234a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2235a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2236a553e4a6SJamal Hadi Salim  * we go look for it ...
2237a553e4a6SJamal Hadi Salim */
2238bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2239fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2240a553e4a6SJamal Hadi Salim {
2241a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
22424e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2243a553e4a6SJamal Hadi Salim 	if (!x) {
2244c454997eSFan Du 
2245c454997eSFan Du 		if (pkt_dev->spi) {
2246c454997eSFan Du 			/* We need as quick as possible to find the right SA
2247c454997eSFan Du 			 * Searching with minimum criteria to archieve this.
2248c454997eSFan Du 			 */
2249c454997eSFan Du 			x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2250c454997eSFan Du 		} else {
2251a553e4a6SJamal Hadi Salim 			/* slow path: we dont already have xfrm_state */
22527e652640SSteffen Klassert 			x = xfrm_stateonly_find(pn->net, DUMMY_MARK, 0,
22535447c5e4SAlexey Dobriyan 						(xfrm_address_t *)&pkt_dev->cur_daddr,
2254a553e4a6SJamal Hadi Salim 						(xfrm_address_t *)&pkt_dev->cur_saddr,
2255a553e4a6SJamal Hadi Salim 						AF_INET,
2256a553e4a6SJamal Hadi Salim 						pkt_dev->ipsmode,
2257a553e4a6SJamal Hadi Salim 						pkt_dev->ipsproto, 0);
2258c454997eSFan Du 		}
2259a553e4a6SJamal Hadi Salim 		if (x) {
2260a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2261a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2262a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2263a553e4a6SJamal Hadi Salim 		}
2264a553e4a6SJamal Hadi Salim 
2265a553e4a6SJamal Hadi Salim 	}
2266a553e4a6SJamal Hadi Salim }
2267a553e4a6SJamal Hadi Salim #endif
2268fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2269fd2ea0a7SDavid S. Miller {
2270e6fce5b9SRobert Olsson 
2271e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2272e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2273e6fce5b9SRobert Olsson 
2274896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2275fd2ea0a7SDavid S. Miller 		__u16 t;
2276fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
227733d7c5e5SAkinobu Mita 			t = prandom_u32() %
2278fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2279fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2280fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2281fd2ea0a7SDavid S. Miller 		} else {
2282fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2283fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2284fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2285fd2ea0a7SDavid S. Miller 		}
2286fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2287fd2ea0a7SDavid S. Miller 	}
2288bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2289fd2ea0a7SDavid S. Miller }
2290fd2ea0a7SDavid S. Miller 
22911da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
22921da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
22931da177e4SLinus Torvalds  */
2294222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2295222f1806SLuiz Capitulino {
22961da177e4SLinus Torvalds 	__u32 imn;
22971da177e4SLinus Torvalds 	__u32 imx;
22981da177e4SLinus Torvalds 	int flow = 0;
22991da177e4SLinus Torvalds 
2300007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2301007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds 	/*  Deal with source MAC */
23041da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
23051da177e4SLinus Torvalds 		__u32 mc;
23061da177e4SLinus Torvalds 		__u32 tmp;
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
230933d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->src_mac_count;
23101da177e4SLinus Torvalds 		else {
23111da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2312ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2313222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
23141da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
23151da177e4SLinus Torvalds 		}
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
23181da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
23191da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23201da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
23211da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23221da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
23231da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23241da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
23251da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
23261da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
23271da177e4SLinus Torvalds 	}
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
23301da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
23311da177e4SLinus Torvalds 		__u32 mc;
23321da177e4SLinus Torvalds 		__u32 tmp;
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
233533d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->dst_mac_count;
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 		else {
23381da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2339ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2340222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
23411da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
23421da177e4SLinus Torvalds 			}
23431da177e4SLinus Torvalds 		}
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
23461da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
23471da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23481da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
23491da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23501da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
23511da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23521da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
23531da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
23541da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
23551da177e4SLinus Torvalds 	}
23561da177e4SLinus Torvalds 
2357ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
235895c96174SEric Dumazet 		unsigned int i;
2359ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2360ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2361ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
236233d7c5e5SAkinobu Mita 					     ((__force __be32)prandom_u32() &
2363ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2364ca6549afSSteven Whitehouse 	}
2365ca6549afSSteven Whitehouse 
236634954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
236733d7c5e5SAkinobu Mita 		pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
236834954ddcSFrancesco Fondelli 	}
236934954ddcSFrancesco Fondelli 
237034954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
237133d7c5e5SAkinobu Mita 		pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
237234954ddcSFrancesco Fondelli 	}
237334954ddcSFrancesco Fondelli 
23741da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
23751da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
237633d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_src = prandom_u32() %
23775fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
23785fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 		else {
23811da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
23821da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
23831da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
23841da177e4SLinus Torvalds 		}
23851da177e4SLinus Torvalds 	}
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
23881da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
238933d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_dst = prandom_u32() %
23905fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
23915fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2392222f1806SLuiz Capitulino 		} else {
23931da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
23941da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
23951da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
23961da177e4SLinus Torvalds 		}
23971da177e4SLinus Torvalds 	}
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
24001da177e4SLinus Torvalds 
240163adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
240263adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
240363adc6fbSStephen Hemminger 		if (imn < imx) {
24041da177e4SLinus Torvalds 			__u32 t;
24051da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
240633d7c5e5SAkinobu Mita 				t = prandom_u32() % (imx - imn) + imn;
24071da177e4SLinus Torvalds 			else {
24081da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
24091da177e4SLinus Torvalds 				t++;
241063adc6fbSStephen Hemminger 				if (t > imx)
24111da177e4SLinus Torvalds 					t = imn;
241263adc6fbSStephen Hemminger 
24131da177e4SLinus Torvalds 			}
24141da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
24151da177e4SLinus Torvalds 		}
24161da177e4SLinus Torvalds 
2417007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
24181da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
24191da177e4SLinus Torvalds 		} else {
2420252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2421252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2422252e3346SAl Viro 			if (imn < imx) {
24231da177e4SLinus Torvalds 				__u32 t;
2424252e3346SAl Viro 				__be32 s;
24251da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
24261da177e4SLinus Torvalds 
242770e3ba72SAkinobu Mita 					do {
242833d7c5e5SAkinobu Mita 						t = prandom_u32() %
242933d7c5e5SAkinobu Mita 							(imx - imn) + imn;
2430252e3346SAl Viro 						s = htonl(t);
243170e3ba72SAkinobu Mita 					} while (ipv4_is_loopback(s) ||
243270e3ba72SAkinobu Mita 						ipv4_is_multicast(s) ||
243370e3ba72SAkinobu Mita 						ipv4_is_lbcast(s) ||
243470e3ba72SAkinobu Mita 						ipv4_is_zeronet(s) ||
243570e3ba72SAkinobu Mita 						ipv4_is_local_multicast(s));
2436252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2437252e3346SAl Viro 				} else {
24381da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
24391da177e4SLinus Torvalds 					t++;
24401da177e4SLinus Torvalds 					if (t > imx) {
24411da177e4SLinus Torvalds 						t = imn;
24421da177e4SLinus Torvalds 					}
24431da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
24441da177e4SLinus Torvalds 				}
24451da177e4SLinus Torvalds 			}
24461da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2447007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2448222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2449222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2450a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
24516f107c74SDmitry Safonov 				if (pkt_dev->flags & F_IPSEC)
2452a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2453a553e4a6SJamal Hadi Salim #endif
24541da177e4SLinus Torvalds 				pkt_dev->nflows++;
24551da177e4SLinus Torvalds 			}
24561da177e4SLinus Torvalds 		}
2457222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2458222f1806SLuiz Capitulino 
245906e30411SJoe Perches 		if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
24601da177e4SLinus Torvalds 			int i;
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 			/* Only random destinations yet */
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
24651da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
246633d7c5e5SAkinobu Mita 				    (((__force __be32)prandom_u32() |
24671da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
24681da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
24691da177e4SLinus Torvalds 			}
24701da177e4SLinus Torvalds 		}
24711da177e4SLinus Torvalds 	}
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
24741da177e4SLinus Torvalds 		__u32 t;
24751da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
247633d7c5e5SAkinobu Mita 			t = prandom_u32() %
24775fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
24785fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2479222f1806SLuiz Capitulino 		} else {
24801da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
24811da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
24821da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
24831da177e4SLinus Torvalds 		}
24841da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
24851da177e4SLinus Torvalds 	}
24861da177e4SLinus Torvalds 
2487fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
248845b270f8SRobert Olsson 
24891da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
24901da177e4SLinus Torvalds }
24911da177e4SLinus Torvalds 
2492a553e4a6SJamal Hadi Salim 
2493a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
24945537a055SFengguang Wu static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2495cf93d47eSFan Du 
2496cf93d47eSFan Du 	[RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2497cf93d47eSFan Du };
2498cf93d47eSFan Du 
2499a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2500a553e4a6SJamal Hadi Salim {
2501a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2502a553e4a6SJamal Hadi Salim 	int err = 0;
25036de9ace4SFan Du 	struct net *net = dev_net(pkt_dev->odev);
2504a553e4a6SJamal Hadi Salim 
2505a553e4a6SJamal Hadi Salim 	if (!x)
2506a553e4a6SJamal Hadi Salim 		return 0;
2507a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2508a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2509cf93d47eSFan Du 	if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2510a553e4a6SJamal Hadi Salim 		return 0;
2511a553e4a6SJamal Hadi Salim 
2512cf93d47eSFan Du 	/* But when user specify an valid SPI, transformation
2513cf93d47eSFan Du 	 * supports both transport/tunnel mode + ESP/AH type.
2514cf93d47eSFan Du 	 */
2515cf93d47eSFan Du 	if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2516b6ca8bd5SDavid Miller 		skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
2517cf93d47eSFan Du 
2518cf93d47eSFan Du 	rcu_read_lock_bh();
25190c620e97SFlorian Westphal 	err = pktgen_xfrm_outer_mode_output(x, skb);
2520cf93d47eSFan Du 	rcu_read_unlock_bh();
25216de9ace4SFan Du 	if (err) {
25226de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2523a553e4a6SJamal Hadi Salim 		goto error;
25246de9ace4SFan Du 	}
2525a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
25266de9ace4SFan Du 	if (err) {
25276de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2528a553e4a6SJamal Hadi Salim 		goto error;
25296de9ace4SFan Du 	}
25300af0a413SFan Du 	spin_lock_bh(&x->lock);
2531a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2532a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
25330af0a413SFan Du 	spin_unlock_bh(&x->lock);
2534a553e4a6SJamal Hadi Salim error:
2535a553e4a6SJamal Hadi Salim 	return err;
2536a553e4a6SJamal Hadi Salim }
2537a553e4a6SJamal Hadi Salim 
2538475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2539a553e4a6SJamal Hadi Salim {
2540a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2541a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2542d6182223SPaul Gortmaker 		int i;
2543d6182223SPaul Gortmaker 		for (i = 0; i < pkt_dev->cflows; i++) {
2544a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2545a553e4a6SJamal Hadi Salim 			if (x) {
2546a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2547a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2548a553e4a6SJamal Hadi Salim 			}
2549a553e4a6SJamal Hadi Salim 		}
2550a553e4a6SJamal Hadi Salim 	}
2551a553e4a6SJamal Hadi Salim }
2552a553e4a6SJamal Hadi Salim 
2553475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2554a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2555a553e4a6SJamal Hadi Salim {
25566f107c74SDmitry Safonov 	if (pkt_dev->flags & F_IPSEC) {
2557a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2558a553e4a6SJamal Hadi Salim 		int nhead = 0;
2559a553e4a6SJamal Hadi Salim 		if (x) {
2560d4969581SEric Dumazet 			struct ethhdr *eth;
25613868204dSfan.du 			struct iphdr *iph;
2562d4969581SEric Dumazet 			int ret;
25633868204dSfan.du 
2564a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2565a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2566a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2567a553e4a6SJamal Hadi Salim 				if (ret < 0) {
2568f9467eaeSJoe Perches 					pr_err("Error expanding ipsec packet %d\n",
2569f9467eaeSJoe Perches 					       ret);
2570b4bb4ac8SIlpo Järvinen 					goto err;
2571a553e4a6SJamal Hadi Salim 				}
2572a553e4a6SJamal Hadi Salim 			}
2573a553e4a6SJamal Hadi Salim 
2574a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2575a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2576a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2577a553e4a6SJamal Hadi Salim 			if (ret) {
2578f9467eaeSJoe Perches 				pr_err("Error creating ipsec packet %d\n", ret);
2579b4bb4ac8SIlpo Järvinen 				goto err;
2580a553e4a6SJamal Hadi Salim 			}
2581a553e4a6SJamal Hadi Salim 			/* restore ll */
2582d58ff351SJohannes Berg 			eth = skb_push(skb, ETH_HLEN);
2583d4969581SEric Dumazet 			memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2584d4969581SEric Dumazet 			eth->h_proto = protocol;
25853868204dSfan.du 
25863868204dSfan.du 			/* Update IPv4 header len as well as checksum value */
25873868204dSfan.du 			iph = ip_hdr(skb);
25883868204dSfan.du 			iph->tot_len = htons(skb->len - ETH_HLEN);
25893868204dSfan.du 			ip_send_check(iph);
2590a553e4a6SJamal Hadi Salim 		}
2591a553e4a6SJamal Hadi Salim 	}
2592a553e4a6SJamal Hadi Salim 	return 1;
2593b4bb4ac8SIlpo Järvinen err:
2594b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2595b4bb4ac8SIlpo Järvinen 	return 0;
2596a553e4a6SJamal Hadi Salim }
2597a553e4a6SJamal Hadi Salim #endif
2598a553e4a6SJamal Hadi Salim 
2599ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2600ca6549afSSteven Whitehouse {
260195c96174SEric Dumazet 	unsigned int i;
260263adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2603ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
260463adc6fbSStephen Hemminger 
2605ca6549afSSteven Whitehouse 	mpls--;
2606ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2607ca6549afSSteven Whitehouse }
2608ca6549afSSteven Whitehouse 
26090f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
26100f37c605SAl Viro 			       unsigned int prio)
26110f37c605SAl Viro {
26120f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
26130f37c605SAl Viro }
26140f37c605SAl Viro 
261526ad7879SEric Dumazet static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
261626ad7879SEric Dumazet 				int datalen)
261726ad7879SEric Dumazet {
26187f5d3f27SArnd Bergmann 	struct timespec64 timestamp;
261926ad7879SEric Dumazet 	struct pktgen_hdr *pgh;
262026ad7879SEric Dumazet 
26214df864c1SJohannes Berg 	pgh = skb_put(skb, sizeof(*pgh));
262226ad7879SEric Dumazet 	datalen -= sizeof(*pgh);
262326ad7879SEric Dumazet 
262426ad7879SEric Dumazet 	if (pkt_dev->nfrags <= 0) {
2625b080db58SJohannes Berg 		skb_put_zero(skb, datalen);
262626ad7879SEric Dumazet 	} else {
262726ad7879SEric Dumazet 		int frags = pkt_dev->nfrags;
262826ad7879SEric Dumazet 		int i, len;
26297d36a991Samit salecha 		int frag_len;
263026ad7879SEric Dumazet 
263126ad7879SEric Dumazet 
263226ad7879SEric Dumazet 		if (frags > MAX_SKB_FRAGS)
263326ad7879SEric Dumazet 			frags = MAX_SKB_FRAGS;
263426ad7879SEric Dumazet 		len = datalen - frags * PAGE_SIZE;
263526ad7879SEric Dumazet 		if (len > 0) {
2636b080db58SJohannes Berg 			skb_put_zero(skb, len);
263726ad7879SEric Dumazet 			datalen = frags * PAGE_SIZE;
263826ad7879SEric Dumazet 		}
263926ad7879SEric Dumazet 
264026ad7879SEric Dumazet 		i = 0;
26417d36a991Samit salecha 		frag_len = (datalen/frags) < PAGE_SIZE ?
26427d36a991Samit salecha 			   (datalen/frags) : PAGE_SIZE;
264326ad7879SEric Dumazet 		while (datalen > 0) {
264426ad7879SEric Dumazet 			if (unlikely(!pkt_dev->page)) {
264526ad7879SEric Dumazet 				int node = numa_node_id();
264626ad7879SEric Dumazet 
264726ad7879SEric Dumazet 				if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
264826ad7879SEric Dumazet 					node = pkt_dev->node;
264926ad7879SEric Dumazet 				pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
265026ad7879SEric Dumazet 				if (!pkt_dev->page)
265126ad7879SEric Dumazet 					break;
265226ad7879SEric Dumazet 			}
2653a0bec1cdSIan Campbell 			get_page(pkt_dev->page);
2654ea2ab693SIan Campbell 			skb_frag_set_page(skb, i, pkt_dev->page);
2655*b54c9d5bSJonathan Lemon 			skb_frag_off_set(&skb_shinfo(skb)->frags[i], 0);
26567d36a991Samit salecha 			/*last fragment, fill rest of data*/
26577d36a991Samit salecha 			if (i == (frags - 1))
26589e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i],
26599e903e08SEric Dumazet 				    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
26607d36a991Samit salecha 			else
26619e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
26629e903e08SEric Dumazet 			datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
26639e903e08SEric Dumazet 			skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
26649e903e08SEric Dumazet 			skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
266526ad7879SEric Dumazet 			i++;
266626ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
266726ad7879SEric Dumazet 		}
266826ad7879SEric Dumazet 	}
266926ad7879SEric Dumazet 
267026ad7879SEric Dumazet 	/* Stamp the time, and sequence number,
267126ad7879SEric Dumazet 	 * convert them to network byte order
267226ad7879SEric Dumazet 	 */
267326ad7879SEric Dumazet 	pgh->pgh_magic = htonl(PKTGEN_MAGIC);
267426ad7879SEric Dumazet 	pgh->seq_num = htonl(pkt_dev->seq_num);
267526ad7879SEric Dumazet 
2676afb84b62SJesper Dangaard Brouer 	if (pkt_dev->flags & F_NO_TIMESTAMP) {
2677afb84b62SJesper Dangaard Brouer 		pgh->tv_sec = 0;
2678afb84b62SJesper Dangaard Brouer 		pgh->tv_usec = 0;
2679afb84b62SJesper Dangaard Brouer 	} else {
26807f5d3f27SArnd Bergmann 		/*
26817f5d3f27SArnd Bergmann 		 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
26827f5d3f27SArnd Bergmann 		 * as done by wireshark, or y2038 when interpreted as signed.
26837f5d3f27SArnd Bergmann 		 * This is probably harmless, but if anyone wants to improve
26847f5d3f27SArnd Bergmann 		 * it, we could introduce a variant that puts 64-bit nanoseconds
26857f5d3f27SArnd Bergmann 		 * into the respective header bytes.
26867f5d3f27SArnd Bergmann 		 * This would also be slightly faster to read.
26877f5d3f27SArnd Bergmann 		 */
26887f5d3f27SArnd Bergmann 		ktime_get_real_ts64(&timestamp);
268926ad7879SEric Dumazet 		pgh->tv_sec = htonl(timestamp.tv_sec);
26907f5d3f27SArnd Bergmann 		pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
269126ad7879SEric Dumazet 	}
2692afb84b62SJesper Dangaard Brouer }
269326ad7879SEric Dumazet 
26947a6e288dSDaniel Borkmann static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
269563d75463SPaolo Abeni 					struct pktgen_dev *pkt_dev)
26967a6e288dSDaniel Borkmann {
269763d75463SPaolo Abeni 	unsigned int extralen = LL_RESERVED_SPACE(dev);
26987a6e288dSDaniel Borkmann 	struct sk_buff *skb = NULL;
269963d75463SPaolo Abeni 	unsigned int size;
27007a6e288dSDaniel Borkmann 
270163d75463SPaolo Abeni 	size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
27027a6e288dSDaniel Borkmann 	if (pkt_dev->flags & F_NODE) {
27037a6e288dSDaniel Borkmann 		int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
27047a6e288dSDaniel Borkmann 
27057a6e288dSDaniel Borkmann 		skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
27067a6e288dSDaniel Borkmann 		if (likely(skb)) {
27077a6e288dSDaniel Borkmann 			skb_reserve(skb, NET_SKB_PAD);
27087a6e288dSDaniel Borkmann 			skb->dev = dev;
27097a6e288dSDaniel Borkmann 		}
27107a6e288dSDaniel Borkmann 	} else {
27117a6e288dSDaniel Borkmann 		 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
27127a6e288dSDaniel Borkmann 	}
27133de03596SJohn Fastabend 
271463d75463SPaolo Abeni 	/* the caller pre-fetches from skb->data and reserves for the mac hdr */
27153de03596SJohn Fastabend 	if (likely(skb))
271663d75463SPaolo Abeni 		skb_reserve(skb, extralen - 16);
27177a6e288dSDaniel Borkmann 
27187a6e288dSDaniel Borkmann 	return skb;
27197a6e288dSDaniel Borkmann }
27207a6e288dSDaniel Borkmann 
27211da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
27221da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
27231da177e4SLinus Torvalds {
27241da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
27251da177e4SLinus Torvalds 	__u8 *eth;
27261da177e4SLinus Torvalds 	struct udphdr *udph;
27271da177e4SLinus Torvalds 	int datalen, iplen;
27281da177e4SLinus Torvalds 	struct iphdr *iph;
2729d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2730ca6549afSSteven Whitehouse 	__be32 *mpls;
273134954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
273234954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
273334954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
273434954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2735fd2ea0a7SDavid S. Miller 	u16 queue_map;
2736ca6549afSSteven Whitehouse 
2737ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2738d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
27391da177e4SLinus Torvalds 
274034954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2741d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
274234954ddcSFrancesco Fondelli 
274364053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
274464053beeSRobert Olsson 	 * fields.
274564053beeSRobert Olsson 	 */
274664053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2747eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
274864053beeSRobert Olsson 
274963d75463SPaolo Abeni 	skb = pktgen_alloc_skb(odev, pkt_dev);
27501da177e4SLinus Torvalds 	if (!skb) {
27511da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
27521da177e4SLinus Torvalds 		return NULL;
27531da177e4SLinus Torvalds 	}
27541da177e4SLinus Torvalds 
27557a6e288dSDaniel Borkmann 	prefetchw(skb->data);
275663d75463SPaolo Abeni 	skb_reserve(skb, 16);
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
2759d58ff351SJohannes Berg 	eth = skb_push(skb, 14);
27604df864c1SJohannes Berg 	mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2761ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2762ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
276334954ddcSFrancesco Fondelli 
276434954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
276534954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
27664df864c1SJohannes Berg 			svlan_tci = skb_put(skb, sizeof(__be16));
27670f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
27680f37c605SAl Viro 					       pkt_dev->svlan_cfi,
27690f37c605SAl Viro 					       pkt_dev->svlan_p);
27704df864c1SJohannes Berg 			svlan_encapsulated_proto = skb_put(skb,
27714df864c1SJohannes Berg 							   sizeof(__be16));
2772d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
277334954ddcSFrancesco Fondelli 		}
27744df864c1SJohannes Berg 		vlan_tci = skb_put(skb, sizeof(__be16));
27750f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
27760f37c605SAl Viro 				      pkt_dev->vlan_cfi,
27770f37c605SAl Viro 				      pkt_dev->vlan_p);
27784df864c1SJohannes Berg 		vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2779d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
278034954ddcSFrancesco Fondelli 	}
278134954ddcSFrancesco Fondelli 
2782c145aeb3SZhang Shengju 	skb_reset_mac_header(skb);
2783525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
27844df864c1SJohannes Berg 	iph = skb_put(skb, sizeof(struct iphdr));
2785525cebedSThomas Graf 
2786525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
27874df864c1SJohannes Berg 	udph = skb_put(skb, sizeof(struct udphdr));
2788fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
27899e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
27909e50e3acSJohn Fastabend 
27911da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2792252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
27931da177e4SLinus Torvalds 
2794ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2795ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
279616dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
27976af773e7SNishank Trivedi 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
27981da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
27991da177e4SLinus Torvalds 
28001da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
28011da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
28021da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
2803c26bf4a5SThomas Graf 	udph->check = 0;
28041da177e4SLinus Torvalds 
28051da177e4SLinus Torvalds 	iph->ihl = 5;
28061da177e4SLinus Torvalds 	iph->version = 4;
28071da177e4SLinus Torvalds 	iph->ttl = 32;
28081ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
28091da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
28101da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
28111da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
281266ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
281366ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
28141da177e4SLinus Torvalds 	iph->frag_off = 0;
28151da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
28161da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
281703c633e7SThomas Graf 	ip_send_check(iph);
2818ca6549afSSteven Whitehouse 	skb->protocol = protocol;
28191da177e4SLinus Torvalds 	skb->dev = odev;
28201da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
2821c26bf4a5SThomas Graf 
28227744b5f3SSabrina Dubroca 	pktgen_finalize_skb(pkt_dev, skb, datalen);
28237744b5f3SSabrina Dubroca 
2824c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2825c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2826c8cd0989STom Herbert 	} else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
2827c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2828c26bf4a5SThomas Graf 		skb->csum = 0;
28297744b5f3SSabrina Dubroca 		udp4_hwcsum(skb, iph->saddr, iph->daddr);
2830c26bf4a5SThomas Graf 	} else {
28317744b5f3SSabrina Dubroca 		__wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
2832c26bf4a5SThomas Graf 
2833c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
28347744b5f3SSabrina Dubroca 		udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
2835c26bf4a5SThomas Graf 						datalen + 8, IPPROTO_UDP, csum);
2836c26bf4a5SThomas Graf 
2837c26bf4a5SThomas Graf 		if (udph->check == 0)
2838c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2839c26bf4a5SThomas Graf 	}
2840c26bf4a5SThomas Graf 
2841a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2842a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2843a553e4a6SJamal Hadi Salim 		return NULL;
2844a553e4a6SJamal Hadi Salim #endif
2845a553e4a6SJamal Hadi Salim 
28461da177e4SLinus Torvalds 	return skb;
28471da177e4SLinus Torvalds }
28481da177e4SLinus Torvalds 
28491da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
28501da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
28511da177e4SLinus Torvalds {
28521da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
28531da177e4SLinus Torvalds 	__u8 *eth;
28541da177e4SLinus Torvalds 	struct udphdr *udph;
2855c26bf4a5SThomas Graf 	int datalen, udplen;
28561da177e4SLinus Torvalds 	struct ipv6hdr *iph;
2857d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2858ca6549afSSteven Whitehouse 	__be32 *mpls;
285934954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
286034954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
286134954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
286234954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2863fd2ea0a7SDavid S. Miller 	u16 queue_map;
2864ca6549afSSteven Whitehouse 
2865ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2866d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
28671da177e4SLinus Torvalds 
286834954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2869d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
287034954ddcSFrancesco Fondelli 
287164053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
287264053beeSRobert Olsson 	 * fields.
287364053beeSRobert Olsson 	 */
287464053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2875eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
287664053beeSRobert Olsson 
287763d75463SPaolo Abeni 	skb = pktgen_alloc_skb(odev, pkt_dev);
28781da177e4SLinus Torvalds 	if (!skb) {
28791da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
28801da177e4SLinus Torvalds 		return NULL;
28811da177e4SLinus Torvalds 	}
28821da177e4SLinus Torvalds 
28837a6e288dSDaniel Borkmann 	prefetchw(skb->data);
28841da177e4SLinus Torvalds 	skb_reserve(skb, 16);
28851da177e4SLinus Torvalds 
28861da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
2887d58ff351SJohannes Berg 	eth = skb_push(skb, 14);
28884df864c1SJohannes Berg 	mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2889ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2890ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
289134954ddcSFrancesco Fondelli 
289234954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
289334954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
28944df864c1SJohannes Berg 			svlan_tci = skb_put(skb, sizeof(__be16));
28950f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
28960f37c605SAl Viro 					       pkt_dev->svlan_cfi,
28970f37c605SAl Viro 					       pkt_dev->svlan_p);
28984df864c1SJohannes Berg 			svlan_encapsulated_proto = skb_put(skb,
28994df864c1SJohannes Berg 							   sizeof(__be16));
2900d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
290134954ddcSFrancesco Fondelli 		}
29024df864c1SJohannes Berg 		vlan_tci = skb_put(skb, sizeof(__be16));
29030f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
29040f37c605SAl Viro 				      pkt_dev->vlan_cfi,
29050f37c605SAl Viro 				      pkt_dev->vlan_p);
29064df864c1SJohannes Berg 		vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2907d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
290834954ddcSFrancesco Fondelli 	}
290934954ddcSFrancesco Fondelli 
2910c145aeb3SZhang Shengju 	skb_reset_mac_header(skb);
2911525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
29124df864c1SJohannes Berg 	iph = skb_put(skb, sizeof(struct ipv6hdr));
2913525cebedSThomas Graf 
2914525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
29154df864c1SJohannes Berg 	udph = skb_put(skb, sizeof(struct udphdr));
2916fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
29179e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2920252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
29211da177e4SLinus Torvalds 
2922ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2923ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
2924ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
292516dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
29261da177e4SLinus Torvalds 
29275aa8b572SAmerigo Wang 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
29281da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
2929e87cc472SJoe Perches 		net_info_ratelimited("increased datalen to %d\n", datalen);
29301da177e4SLinus Torvalds 	}
29311da177e4SLinus Torvalds 
2932c26bf4a5SThomas Graf 	udplen = datalen + sizeof(struct udphdr);
29331da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
29341da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
2935c26bf4a5SThomas Graf 	udph->len = htons(udplen);
2936c26bf4a5SThomas Graf 	udph->check = 0;
29371da177e4SLinus Torvalds 
2938d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
29391da177e4SLinus Torvalds 
29401ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
29411ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
2942252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
29431ca7768cSFrancesco Fondelli 	}
29441ca7768cSFrancesco Fondelli 
29451da177e4SLinus Torvalds 	iph->hop_limit = 32;
29461da177e4SLinus Torvalds 
2947c26bf4a5SThomas Graf 	iph->payload_len = htons(udplen);
29481da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
29491da177e4SLinus Torvalds 
29504e3fd7a0SAlexey Dobriyan 	iph->daddr = pkt_dev->cur_in6_daddr;
29514e3fd7a0SAlexey Dobriyan 	iph->saddr = pkt_dev->cur_in6_saddr;
29521da177e4SLinus Torvalds 
2953ca6549afSSteven Whitehouse 	skb->protocol = protocol;
29541da177e4SLinus Torvalds 	skb->dev = odev;
29551da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
29561da177e4SLinus Torvalds 
29577744b5f3SSabrina Dubroca 	pktgen_finalize_skb(pkt_dev, skb, datalen);
29587744b5f3SSabrina Dubroca 
2959c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2960c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2961c8cd0989STom Herbert 	} else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
2962c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2963c26bf4a5SThomas Graf 		skb->csum_start = skb_transport_header(skb) - skb->head;
2964c26bf4a5SThomas Graf 		skb->csum_offset = offsetof(struct udphdr, check);
2965c26bf4a5SThomas Graf 		udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2966c26bf4a5SThomas Graf 	} else {
29677744b5f3SSabrina Dubroca 		__wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
2968c26bf4a5SThomas Graf 
2969c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
2970c26bf4a5SThomas Graf 		udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2971c26bf4a5SThomas Graf 
2972c26bf4a5SThomas Graf 		if (udph->check == 0)
2973c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2974c26bf4a5SThomas Graf 	}
2975c26bf4a5SThomas Graf 
29761da177e4SLinus Torvalds 	return skb;
29771da177e4SLinus Torvalds }
29781da177e4SLinus Torvalds 
2979475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
29801da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
29811da177e4SLinus Torvalds {
29821da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
29831da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
29841da177e4SLinus Torvalds 	else
29851da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
29861da177e4SLinus Torvalds }
29871da177e4SLinus Torvalds 
29881da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
29891da177e4SLinus Torvalds {
29901da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
29911da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
29921da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
29931da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
29941da177e4SLinus Torvalds 	pkt_dev->errors = 0;
29951da177e4SLinus Torvalds }
29961da177e4SLinus Torvalds 
29971da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
29981da177e4SLinus Torvalds 
29991da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
30001da177e4SLinus Torvalds {
3001c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
30021da177e4SLinus Torvalds 	int started = 0;
30031da177e4SLinus Torvalds 
3004f9467eaeSJoe Perches 	func_enter();
30051da177e4SLinus Torvalds 
30068788370aSJesper Dangaard Brouer 	rcu_read_lock();
30078788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
30081da177e4SLinus Torvalds 
30091da177e4SLinus Torvalds 		/*
30101da177e4SLinus Torvalds 		 * setup odev and create initial packet.
30111da177e4SLinus Torvalds 		 */
30121da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds 		if (pkt_dev->odev) {
30151da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
30161da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
3017398f382cSDaniel Borkmann 			pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3018fd29cf72SStephen Hemminger 
301916dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
30228788370aSJesper Dangaard Brouer 			pkt_dev->running = 1;	/* Cranke yeself! */
30231da177e4SLinus Torvalds 			started++;
3024222f1806SLuiz Capitulino 		} else
30251da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
30261da177e4SLinus Torvalds 	}
30278788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3028222f1806SLuiz Capitulino 	if (started)
3029222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
30301da177e4SLinus Torvalds }
30311da177e4SLinus Torvalds 
30324e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
30331da177e4SLinus Torvalds {
3034cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30351da177e4SLinus Torvalds 
3036f9467eaeSJoe Perches 	func_enter();
30371da177e4SLinus Torvalds 
30386146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3039cdcdbe0bSLuiz Capitulino 
30404e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
304195ed63f7SArthur Kepner 		t->control |= T_STOP;
3042cdcdbe0bSLuiz Capitulino 
30436146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
30441da177e4SLinus Torvalds }
30451da177e4SLinus Torvalds 
3046648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
30471da177e4SLinus Torvalds {
3048648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
30491da177e4SLinus Torvalds 
30508788370aSJesper Dangaard Brouer 	rcu_read_lock();
30518788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
30528788370aSJesper Dangaard Brouer 		if (pkt_dev->running) {
30538788370aSJesper Dangaard Brouer 			rcu_read_unlock();
3054648fda74SStephen Hemminger 			return 1;
30558788370aSJesper Dangaard Brouer 		}
30568788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3057648fda74SStephen Hemminger 	return 0;
30581da177e4SLinus Torvalds }
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
30611da177e4SLinus Torvalds {
30621da177e4SLinus Torvalds 	while (thread_is_running(t)) {
30631da177e4SLinus Torvalds 
3064720f1de4SPaolo Abeni 		/* note: 't' will still be around even after the unlock/lock
3065720f1de4SPaolo Abeni 		 * cycle because pktgen_thread threads are only cleared at
3066720f1de4SPaolo Abeni 		 * net exit
3067720f1de4SPaolo Abeni 		 */
3068720f1de4SPaolo Abeni 		mutex_unlock(&pktgen_thread_lock);
30691da177e4SLinus Torvalds 		msleep_interruptible(100);
3070720f1de4SPaolo Abeni 		mutex_lock(&pktgen_thread_lock);
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds 		if (signal_pending(current))
30731da177e4SLinus Torvalds 			goto signal;
30741da177e4SLinus Torvalds 	}
30751da177e4SLinus Torvalds 	return 1;
30761da177e4SLinus Torvalds signal:
30771da177e4SLinus Torvalds 	return 0;
30781da177e4SLinus Torvalds }
30791da177e4SLinus Torvalds 
30804e58a027SCong Wang static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
30811da177e4SLinus Torvalds {
3082cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30831da177e4SLinus Torvalds 	int sig = 1;
30841da177e4SLinus Torvalds 
3085720f1de4SPaolo Abeni 	/* prevent from racing with rmmod */
3086720f1de4SPaolo Abeni 	if (!try_module_get(THIS_MODULE))
3087720f1de4SPaolo Abeni 		return sig;
3088720f1de4SPaolo Abeni 
30896146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3090cdcdbe0bSLuiz Capitulino 
30914e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
30921da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
3093222f1806SLuiz Capitulino 		if (sig == 0)
3094222f1806SLuiz Capitulino 			break;
30951da177e4SLinus Torvalds 	}
3096cdcdbe0bSLuiz Capitulino 
3097cdcdbe0bSLuiz Capitulino 	if (sig == 0)
30984e58a027SCong Wang 		list_for_each_entry(t, &pn->pktgen_threads, th_list)
30991da177e4SLinus Torvalds 			t->control |= (T_STOP);
3100cdcdbe0bSLuiz Capitulino 
31016146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
3102720f1de4SPaolo Abeni 	module_put(THIS_MODULE);
31031da177e4SLinus Torvalds 	return sig;
31041da177e4SLinus Torvalds }
31051da177e4SLinus Torvalds 
31064e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn)
31071da177e4SLinus Torvalds {
3108cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31091da177e4SLinus Torvalds 
3110f9467eaeSJoe Perches 	func_enter();
31111da177e4SLinus Torvalds 
31126146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
31131da177e4SLinus Torvalds 
31144e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
31151da177e4SLinus Torvalds 		t->control |= (T_RUN);
3116cdcdbe0bSLuiz Capitulino 
31176146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31181da177e4SLinus Torvalds 
311963adc6fbSStephen Hemminger 	/* Propagate thread->control  */
312063adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
31211da177e4SLinus Torvalds 
31224e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
31231da177e4SLinus Torvalds }
31241da177e4SLinus Torvalds 
31254e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn)
3126eb37b41cSJesse Brandeburg {
3127eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3128eb37b41cSJesse Brandeburg 
3129f9467eaeSJoe Perches 	func_enter();
3130eb37b41cSJesse Brandeburg 
3131eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3132eb37b41cSJesse Brandeburg 
31334e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
3134eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3135eb37b41cSJesse Brandeburg 
3136eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3137eb37b41cSJesse Brandeburg 
313863adc6fbSStephen Hemminger 	/* Propagate thread->control  */
313963adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3140eb37b41cSJesse Brandeburg 
31414e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
3142eb37b41cSJesse Brandeburg }
3143eb37b41cSJesse Brandeburg 
31441da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
31451da177e4SLinus Torvalds {
3146fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
31471da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3148fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3149fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3150fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
31511da177e4SLinus Torvalds 
315203a14ab1SDaniel Turull 	p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3153fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3154fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3155fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
31561da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
31571da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
31581da177e4SLinus Torvalds 
3159fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3160fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
31631da177e4SLinus Torvalds 
31641da177e4SLinus Torvalds 	mbps = bps;
31651da177e4SLinus Torvalds 	do_div(mbps, 1000000);
31661da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
31671da177e4SLinus Torvalds 		     (unsigned long long)pps,
31681da177e4SLinus Torvalds 		     (unsigned long long)mbps,
31691da177e4SLinus Torvalds 		     (unsigned long long)bps,
31701da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
31711da177e4SLinus Torvalds }
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
31741da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
31751da177e4SLinus Torvalds {
3176222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
31771da177e4SLinus Torvalds 
31781da177e4SLinus Torvalds 	if (!pkt_dev->running) {
3179294a0b7fSJoe Perches 		pr_warn("interface: %s is already stopped\n",
3180f9467eaeSJoe Perches 			pkt_dev->odevname);
31811da177e4SLinus Torvalds 		return -EINVAL;
31821da177e4SLinus Torvalds 	}
31831da177e4SLinus Torvalds 
31848788370aSJesper Dangaard Brouer 	pkt_dev->running = 0;
31853bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
31863bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3187398f382cSDaniel Borkmann 	pkt_dev->stopped_at = ktime_get();
31881da177e4SLinus Torvalds 
318995ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
31901da177e4SLinus Torvalds 
31911da177e4SLinus Torvalds 	return 0;
31921da177e4SLinus Torvalds }
31931da177e4SLinus Torvalds 
31941da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
31951da177e4SLinus Torvalds {
3196c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
31971da177e4SLinus Torvalds 
31988788370aSJesper Dangaard Brouer 	rcu_read_lock();
31998788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3200c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3201222f1806SLuiz Capitulino 			continue;
3202222f1806SLuiz Capitulino 		if (best == NULL)
3203c26a8016SLuiz Capitulino 			best = pkt_dev;
3204398f382cSDaniel Borkmann 		else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3205c26a8016SLuiz Capitulino 			best = pkt_dev;
32061da177e4SLinus Torvalds 	}
32078788370aSJesper Dangaard Brouer 	rcu_read_unlock();
32088788370aSJesper Dangaard Brouer 
32091da177e4SLinus Torvalds 	return best;
32101da177e4SLinus Torvalds }
32111da177e4SLinus Torvalds 
3212222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3213222f1806SLuiz Capitulino {
3214c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
32151da177e4SLinus Torvalds 
3216f9467eaeSJoe Perches 	func_enter();
32171da177e4SLinus Torvalds 
32188788370aSJesper Dangaard Brouer 	rcu_read_lock();
32191da177e4SLinus Torvalds 
32208788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3221c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
322295ed63f7SArthur Kepner 	}
322395ed63f7SArthur Kepner 
32248788370aSJesper Dangaard Brouer 	rcu_read_unlock();
322595ed63f7SArthur Kepner }
322695ed63f7SArthur Kepner 
322795ed63f7SArthur Kepner /*
322895ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
322995ed63f7SArthur Kepner  * and remove it
323095ed63f7SArthur Kepner  */
323195ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
323295ed63f7SArthur Kepner {
3233c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3234c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
323595ed63f7SArthur Kepner 
3236f9467eaeSJoe Perches 	func_enter();
323795ed63f7SArthur Kepner 
3238c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3239c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
324095ed63f7SArthur Kepner 
3241222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3242222f1806SLuiz Capitulino 			continue;
324395ed63f7SArthur Kepner 
324495ed63f7SArthur Kepner 		kfree_skb(cur->skb);
324595ed63f7SArthur Kepner 		cur->skb = NULL;
324695ed63f7SArthur Kepner 
324795ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
324895ed63f7SArthur Kepner 
324995ed63f7SArthur Kepner 		break;
325095ed63f7SArthur Kepner 	}
32511da177e4SLinus Torvalds }
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
32541da177e4SLinus Torvalds {
3255c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3256c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
32571da177e4SLinus Torvalds 
3258f9467eaeSJoe Perches 	func_enter();
3259f9467eaeSJoe Perches 
32601da177e4SLinus Torvalds 	/* Remove all devices, free mem */
32611da177e4SLinus Torvalds 
3262c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3263c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
326495ed63f7SArthur Kepner 
326595ed63f7SArthur Kepner 		kfree_skb(cur->skb);
326695ed63f7SArthur Kepner 		cur->skb = NULL;
326795ed63f7SArthur Kepner 
32681da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
32691da177e4SLinus Torvalds 	}
32701da177e4SLinus Torvalds }
32711da177e4SLinus Torvalds 
32721da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
32731da177e4SLinus Torvalds {
32741da177e4SLinus Torvalds 	/* Remove from the thread list */
32754e58a027SCong Wang 	remove_proc_entry(t->tsk->comm, t->net->proc_dir);
32761da177e4SLinus Torvalds }
32771da177e4SLinus Torvalds 
3278ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
32793791decbSStephen Hemminger {
3280398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
32813791decbSStephen Hemminger 	schedule();
3282398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
32833791decbSStephen Hemminger }
32843791decbSStephen Hemminger 
3285ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3286ef87979cSStephen Hemminger {
3287398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
3288ef87979cSStephen Hemminger 
328963354797SReshetova, Elena 	while (refcount_read(&(pkt_dev->skb->users)) != 1) {
3290ef87979cSStephen Hemminger 		if (signal_pending(current))
3291ef87979cSStephen Hemminger 			break;
3292ef87979cSStephen Hemminger 
3293ef87979cSStephen Hemminger 		if (need_resched())
3294ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3295ef87979cSStephen Hemminger 		else
3296ef87979cSStephen Hemminger 			cpu_relax();
3297ef87979cSStephen Hemminger 	}
3298398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3299ef87979cSStephen Hemminger }
3300fd29cf72SStephen Hemminger 
3301475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
33021da177e4SLinus Torvalds {
33036aa7de05SMark Rutland 	unsigned int burst = READ_ONCE(pkt_dev->burst);
330400829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
3305fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
330662f64aedSAlexei Starovoitov 	struct sk_buff *skb;
33071da177e4SLinus Torvalds 	int ret;
33081da177e4SLinus Torvalds 
3309ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3310ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
33111da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
33123791decbSStephen Hemminger 		return;
33131da177e4SLinus Torvalds 	}
33141da177e4SLinus Torvalds 
3315ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3316ef87979cSStephen Hemminger 	 * "never transmit"
3317ef87979cSStephen Hemminger 	 */
3318ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3319398f382cSDaniel Borkmann 		pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3320ef87979cSStephen Hemminger 		return;
3321ef87979cSStephen Hemminger 	}
3322ef87979cSStephen Hemminger 
3323ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
33247d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
33257d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
33261da177e4SLinus Torvalds 		/* build a new pkt */
33271da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
33281da177e4SLinus Torvalds 
33291da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
33301da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
3331f9467eaeSJoe Perches 			pr_err("ERROR: couldn't allocate skb in fill_packet\n");
33321da177e4SLinus Torvalds 			schedule();
33331da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
33343791decbSStephen Hemminger 			return;
33351da177e4SLinus Torvalds 		}
3336baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
33371da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
33381da177e4SLinus Torvalds 	}
33391da177e4SLinus Torvalds 
3340ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3341ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3342ef87979cSStephen Hemminger 
334362f64aedSAlexei Starovoitov 	if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
334462f64aedSAlexei Starovoitov 		skb = pkt_dev->skb;
334562f64aedSAlexei Starovoitov 		skb->protocol = eth_type_trans(skb, skb->dev);
334663354797SReshetova, Elena 		refcount_add(burst, &skb->users);
334762f64aedSAlexei Starovoitov 		local_bh_disable();
334862f64aedSAlexei Starovoitov 		do {
334962f64aedSAlexei Starovoitov 			ret = netif_receive_skb(skb);
335062f64aedSAlexei Starovoitov 			if (ret == NET_RX_DROP)
335162f64aedSAlexei Starovoitov 				pkt_dev->errors++;
335262f64aedSAlexei Starovoitov 			pkt_dev->sofar++;
335362f64aedSAlexei Starovoitov 			pkt_dev->seq_num++;
335463354797SReshetova, Elena 			if (refcount_read(&skb->users) != burst) {
335562f64aedSAlexei Starovoitov 				/* skb was queued by rps/rfs or taps,
335662f64aedSAlexei Starovoitov 				 * so cannot reuse this skb
335762f64aedSAlexei Starovoitov 				 */
335863354797SReshetova, Elena 				WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
335962f64aedSAlexei Starovoitov 				/* get out of the loop and wait
336062f64aedSAlexei Starovoitov 				 * until skb is consumed
336162f64aedSAlexei Starovoitov 				 */
336262f64aedSAlexei Starovoitov 				break;
336362f64aedSAlexei Starovoitov 			}
336462f64aedSAlexei Starovoitov 			/* skb was 'freed' by stack, so clean few
336562f64aedSAlexei Starovoitov 			 * bits and reuse it
336662f64aedSAlexei Starovoitov 			 */
3367a5135bcfSWillem de Bruijn 			skb_reset_tc(skb);
336862f64aedSAlexei Starovoitov 		} while (--burst > 0);
336962f64aedSAlexei Starovoitov 		goto out; /* Skips xmit_mode M_START_XMIT */
33700967f244SJohn Fastabend 	} else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
33710967f244SJohn Fastabend 		local_bh_disable();
337263354797SReshetova, Elena 		refcount_inc(&pkt_dev->skb->users);
33730967f244SJohn Fastabend 
33740967f244SJohn Fastabend 		ret = dev_queue_xmit(pkt_dev->skb);
33750967f244SJohn Fastabend 		switch (ret) {
33760967f244SJohn Fastabend 		case NET_XMIT_SUCCESS:
33770967f244SJohn Fastabend 			pkt_dev->sofar++;
33780967f244SJohn Fastabend 			pkt_dev->seq_num++;
33790967f244SJohn Fastabend 			pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
33800967f244SJohn Fastabend 			break;
33810967f244SJohn Fastabend 		case NET_XMIT_DROP:
33820967f244SJohn Fastabend 		case NET_XMIT_CN:
33830967f244SJohn Fastabend 		/* These are all valid return codes for a qdisc but
33840967f244SJohn Fastabend 		 * indicate packets are being dropped or will likely
33850967f244SJohn Fastabend 		 * be dropped soon.
33860967f244SJohn Fastabend 		 */
33870967f244SJohn Fastabend 		case NETDEV_TX_BUSY:
33880967f244SJohn Fastabend 		/* qdisc may call dev_hard_start_xmit directly in cases
33890967f244SJohn Fastabend 		 * where no queues exist e.g. loopback device, virtual
33900967f244SJohn Fastabend 		 * devices, etc. In this case we need to handle
33910967f244SJohn Fastabend 		 * NETDEV_TX_ codes.
33920967f244SJohn Fastabend 		 */
33930967f244SJohn Fastabend 		default:
33940967f244SJohn Fastabend 			pkt_dev->errors++;
33950967f244SJohn Fastabend 			net_info_ratelimited("%s xmit error: %d\n",
33960967f244SJohn Fastabend 					     pkt_dev->odevname, ret);
33970967f244SJohn Fastabend 			break;
33980967f244SJohn Fastabend 		}
33990967f244SJohn Fastabend 		goto out;
340062f64aedSAlexei Starovoitov 	}
340162f64aedSAlexei Starovoitov 
340210c51b56SDaniel Borkmann 	txq = skb_get_tx_queue(odev, pkt_dev->skb);
3403fd2ea0a7SDavid S. Miller 
34040f2eea4bSDaniel Borkmann 	local_bh_disable();
34050f2eea4bSDaniel Borkmann 
34060f2eea4bSDaniel Borkmann 	HARD_TX_LOCK(odev, txq, smp_processor_id());
34075b8db2f5SStephen Hemminger 
34086f25cd47SDaniel Borkmann 	if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3409ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
34100835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
34110835acfeSEric Dumazet 		goto unlock;
34120835acfeSEric Dumazet 	}
341363354797SReshetova, Elena 	refcount_add(burst, &pkt_dev->skb->users);
341438b2cf29SAlexei Starovoitov 
341538b2cf29SAlexei Starovoitov xmit_more:
341638b2cf29SAlexei Starovoitov 	ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
3417ef87979cSStephen Hemminger 
34185b8db2f5SStephen Hemminger 	switch (ret) {
34195b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
34201da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
34211da177e4SLinus Torvalds 		pkt_dev->sofar++;
34221da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3423baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
342438b2cf29SAlexei Starovoitov 		if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
342538b2cf29SAlexei Starovoitov 			goto xmit_more;
34265b8db2f5SStephen Hemminger 		break;
3427f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3428f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3429f466dba1SJohn Fastabend 		/* skb has been consumed */
3430f466dba1SJohn Fastabend 		pkt_dev->errors++;
3431f466dba1SJohn Fastabend 		break;
34325b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
3433e87cc472SJoe Perches 		net_info_ratelimited("%s xmit error: %d\n",
3434e87cc472SJoe Perches 				     pkt_dev->odevname, ret);
34351da177e4SLinus Torvalds 		pkt_dev->errors++;
3436f91845daSGustavo A. R. Silva 		/* fall through */
34375b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
34385b8db2f5SStephen Hemminger 		/* Retry it next time */
343963354797SReshetova, Elena 		refcount_dec(&(pkt_dev->skb->users));
34401da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
34411da177e4SLinus Torvalds 	}
344238b2cf29SAlexei Starovoitov 	if (unlikely(burst))
344363354797SReshetova, Elena 		WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
34440835acfeSEric Dumazet unlock:
34450f2eea4bSDaniel Borkmann 	HARD_TX_UNLOCK(odev, txq);
34460f2eea4bSDaniel Borkmann 
344762f64aedSAlexei Starovoitov out:
34480f2eea4bSDaniel Borkmann 	local_bh_enable();
34491da177e4SLinus Torvalds 
34501da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
34511da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3452ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
34531da177e4SLinus Torvalds 
34541da177e4SLinus Torvalds 		/* Done with this */
34551da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
34561da177e4SLinus Torvalds 	}
34571da177e4SLinus Torvalds }
34581da177e4SLinus Torvalds 
34591da177e4SLinus Torvalds /*
34601da177e4SLinus Torvalds  * Main loop of the thread goes here
34611da177e4SLinus Torvalds  */
34621da177e4SLinus Torvalds 
3463ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
34641da177e4SLinus Torvalds {
34651da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3466ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
34671da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
34681da177e4SLinus Torvalds 	int cpu = t->cpu;
34691da177e4SLinus Torvalds 
3470ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
34711da177e4SLinus Torvalds 
34721da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3473d3ede327SDenis V. Lunev 	complete(&t->start_done);
34741da177e4SLinus Torvalds 
3475f9467eaeSJoe Perches 	pr_debug("starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
34761da177e4SLinus Torvalds 
347783144186SRafael J. Wysocki 	set_freezable();
347883144186SRafael J. Wysocki 
3479ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3480ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3481ee74baa7SDavid S. Miller 
3482ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
34834e58a027SCong Wang 			if (t->net->pktgen_exiting)
3484551eaff1SEric Dumazet 				break;
3485ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3486ef87979cSStephen Hemminger 							 t->control != 0,
3487ef87979cSStephen Hemminger 							 HZ/10);
34881b3f720bSRafael J. Wysocki 			try_to_freeze();
3489ef87979cSStephen Hemminger 			continue;
3490ee74baa7SDavid S. Miller 		}
34911da177e4SLinus Torvalds 
3492ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
34931da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
34941da177e4SLinus Torvalds 
3495ef87979cSStephen Hemminger 			if (need_resched())
3496ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3497ef87979cSStephen Hemminger 			else
3498ef87979cSStephen Hemminger 				cpu_relax();
3499ef87979cSStephen Hemminger 		}
3500ef87979cSStephen Hemminger 
35011da177e4SLinus Torvalds 		if (t->control & T_STOP) {
35021da177e4SLinus Torvalds 			pktgen_stop(t);
35031da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
35041da177e4SLinus Torvalds 		}
35051da177e4SLinus Torvalds 
35061da177e4SLinus Torvalds 		if (t->control & T_RUN) {
35071da177e4SLinus Torvalds 			pktgen_run(t);
35081da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
35091da177e4SLinus Torvalds 		}
35101da177e4SLinus Torvalds 
351195ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
35121da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
351395ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
351495ed63f7SArthur Kepner 		}
351595ed63f7SArthur Kepner 
351695ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
351795ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
35181da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
35191da177e4SLinus Torvalds 		}
35201da177e4SLinus Torvalds 
352109fe3ef4SAndrew Morton 		try_to_freeze();
35221da177e4SLinus Torvalds 	}
35231da177e4SLinus Torvalds 
3524f9467eaeSJoe Perches 	pr_debug("%s stopping all device\n", t->tsk->comm);
35251da177e4SLinus Torvalds 	pktgen_stop(t);
35261da177e4SLinus Torvalds 
3527f9467eaeSJoe Perches 	pr_debug("%s removing all device\n", t->tsk->comm);
35281da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
35291da177e4SLinus Torvalds 
3530f9467eaeSJoe Perches 	pr_debug("%s removing thread\n", t->tsk->comm);
35311da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3532cdcdbe0bSLuiz Capitulino 
3533ee74baa7SDavid S. Miller 	return 0;
35341da177e4SLinus Torvalds }
35351da177e4SLinus Torvalds 
3536222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
35373e984840SEric Dumazet 					  const char *ifname, bool exact)
35381da177e4SLinus Torvalds {
3539c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
35403e984840SEric Dumazet 	size_t len = strlen(ifname);
35411da177e4SLinus Torvalds 
35428788370aSJesper Dangaard Brouer 	rcu_read_lock();
35438788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(p, &t->if_list, list)
35443e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
35453e984840SEric Dumazet 			if (p->odevname[len]) {
35463e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
35473e984840SEric Dumazet 					continue;
35483e984840SEric Dumazet 			}
3549c26a8016SLuiz Capitulino 			pkt_dev = p;
35501da177e4SLinus Torvalds 			break;
35511da177e4SLinus Torvalds 		}
35521da177e4SLinus Torvalds 
35538788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3554f9467eaeSJoe Perches 	pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
35551da177e4SLinus Torvalds 	return pkt_dev;
35561da177e4SLinus Torvalds }
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds /*
35591da177e4SLinus Torvalds  * Adds a dev at front of if_list.
35601da177e4SLinus Torvalds  */
35611da177e4SLinus Torvalds 
3562222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3563222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
35641da177e4SLinus Torvalds {
35651da177e4SLinus Torvalds 	int rv = 0;
35661da177e4SLinus Torvalds 
35678788370aSJesper Dangaard Brouer 	/* This function cannot be called concurrently, as its called
35688788370aSJesper Dangaard Brouer 	 * under pktgen_thread_lock mutex, but it can run from
35698788370aSJesper Dangaard Brouer 	 * userspace on another CPU than the kthread.  The if_lock()
35708788370aSJesper Dangaard Brouer 	 * is used here to sync with concurrent instances of
35718788370aSJesper Dangaard Brouer 	 * _rem_dev_from_if_list() invoked via kthread, which is also
35728788370aSJesper Dangaard Brouer 	 * updating the if_list */
35731da177e4SLinus Torvalds 	if_lock(t);
35741da177e4SLinus Torvalds 
35751da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
3576f9467eaeSJoe Perches 		pr_err("ERROR: already assigned to a thread\n");
35771da177e4SLinus Torvalds 		rv = -EBUSY;
35781da177e4SLinus Torvalds 		goto out;
35791da177e4SLinus Torvalds 	}
3580c26a8016SLuiz Capitulino 
35811da177e4SLinus Torvalds 	pkt_dev->running = 0;
35828788370aSJesper Dangaard Brouer 	pkt_dev->pg_thread = t;
35838788370aSJesper Dangaard Brouer 	list_add_rcu(&pkt_dev->list, &t->if_list);
35841da177e4SLinus Torvalds 
35851da177e4SLinus Torvalds out:
35861da177e4SLinus Torvalds 	if_unlock(t);
35871da177e4SLinus Torvalds 	return rv;
35881da177e4SLinus Torvalds }
35891da177e4SLinus Torvalds 
35901da177e4SLinus Torvalds /* Called under thread lock */
35911da177e4SLinus Torvalds 
35921da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
35931da177e4SLinus Torvalds {
35941da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
359539df232fSStephen Hemminger 	int err;
35963291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
35971da177e4SLinus Torvalds 
35981da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
35991da177e4SLinus Torvalds 
36004e58a027SCong Wang 	pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3601d50a6b56SStephen Hemminger 	if (pkt_dev) {
3602f9467eaeSJoe Perches 		pr_err("ERROR: interface already used\n");
3603d50a6b56SStephen Hemminger 		return -EBUSY;
3604d50a6b56SStephen Hemminger 	}
36051da177e4SLinus Torvalds 
36063291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
36071da177e4SLinus Torvalds 	if (!pkt_dev)
36081da177e4SLinus Torvalds 		return -ENOMEM;
36091da177e4SLinus Torvalds 
3610593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
3611fd7becedSKees Cook 	pkt_dev->flows = vzalloc_node(array_size(MAX_CFLOWS,
3612fd7becedSKees Cook 						 sizeof(struct flow_state)),
36133291b9dbSEric Dumazet 				      node);
36141da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
36151da177e4SLinus Torvalds 		kfree(pkt_dev);
36161da177e4SLinus Torvalds 		return -ENOMEM;
36171da177e4SLinus Torvalds 	}
36181da177e4SLinus Torvalds 
361995ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
36201da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
3621fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
36221da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
36231da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
36241da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
36251da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
36261da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
36271da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
362834954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
362934954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
363034954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
363134954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
363234954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
363334954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
363438b2cf29SAlexei Starovoitov 	pkt_dev->burst = 1;
363598fa15f3SAnshuman Khandual 	pkt_dev->node = NUMA_NO_NODE;
363634954ddcSFrancesco Fondelli 
36374e58a027SCong Wang 	err = pktgen_setup_dev(t->net, pkt_dev, ifname);
363839df232fSStephen Hemminger 	if (err)
363939df232fSStephen Hemminger 		goto out1;
3640d8873315SNeil Horman 	if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3641d8873315SNeil Horman 		pkt_dev->clone_skb = pg_clone_skb_d;
36421da177e4SLinus Torvalds 
36434e58a027SCong Wang 	pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
36445efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
364539df232fSStephen Hemminger 	if (!pkt_dev->entry) {
3646f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3647d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
364839df232fSStephen Hemminger 		err = -EINVAL;
364939df232fSStephen Hemminger 		goto out2;
365039df232fSStephen Hemminger 	}
3651a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3652a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3653a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3654cf93d47eSFan Du 
3655cf93d47eSFan Du 	/* xfrm tunnel mode needs additional dst to extract outter
3656cf93d47eSFan Du 	 * ip header protocol/ttl/id field, here creat a phony one.
3657cf93d47eSFan Du 	 * instead of looking for a valid rt, which definitely hurting
3658cf93d47eSFan Du 	 * performance under such circumstance.
3659cf93d47eSFan Du 	 */
3660cf93d47eSFan Du 	pkt_dev->dstops.family = AF_INET;
3661b6ca8bd5SDavid Miller 	pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3662b6ca8bd5SDavid Miller 	dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3663b6ca8bd5SDavid Miller 	pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3664b6ca8bd5SDavid Miller 	pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
3665a553e4a6SJamal Hadi Salim #endif
366639df232fSStephen Hemminger 
366739df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
366839df232fSStephen Hemminger out2:
366939df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
367039df232fSStephen Hemminger out1:
3671a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3672a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3673a553e4a6SJamal Hadi Salim #endif
36741da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
36751da177e4SLinus Torvalds 	kfree(pkt_dev);
367639df232fSStephen Hemminger 	return err;
36771da177e4SLinus Torvalds }
36781da177e4SLinus Torvalds 
36794e58a027SCong Wang static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
36801da177e4SLinus Torvalds {
3681cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3682d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3683ee74baa7SDavid S. Miller 	struct task_struct *p;
36841da177e4SLinus Torvalds 
36853291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
36863291b9dbSEric Dumazet 			 cpu_to_node(cpu));
36871da177e4SLinus Torvalds 	if (!t) {
3688f9467eaeSJoe Perches 		pr_err("ERROR: out of memory, can't create new thread\n");
36891da177e4SLinus Torvalds 		return -ENOMEM;
36901da177e4SLinus Torvalds 	}
36911da177e4SLinus Torvalds 
36929a0b1e8bSEric Dumazet 	mutex_init(&t->if_lock);
36931da177e4SLinus Torvalds 	t->cpu = cpu;
36941da177e4SLinus Torvalds 
3695ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3696ee74baa7SDavid S. Miller 
36974e58a027SCong Wang 	list_add_tail(&t->th_list, &pn->pktgen_threads);
3698d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3699ee74baa7SDavid S. Miller 
370094dcf29aSEric Dumazet 	p = kthread_create_on_node(pktgen_thread_worker,
370194dcf29aSEric Dumazet 				   t,
370294dcf29aSEric Dumazet 				   cpu_to_node(cpu),
370394dcf29aSEric Dumazet 				   "kpktgend_%d", cpu);
3704ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
3705f9467eaeSJoe Perches 		pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3706ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3707ee74baa7SDavid S. Miller 		kfree(t);
3708ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3709ee74baa7SDavid S. Miller 	}
3710ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3711ee74baa7SDavid S. Miller 	t->tsk = p;
3712ee74baa7SDavid S. Miller 
37134e58a027SCong Wang 	pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
37145efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3715d50a6b56SStephen Hemminger 	if (!pe) {
3716f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3717ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3718ee74baa7SDavid S. Miller 		kthread_stop(p);
3719ee74baa7SDavid S. Miller 		list_del(&t->th_list);
37201da177e4SLinus Torvalds 		kfree(t);
37211da177e4SLinus Torvalds 		return -EINVAL;
37221da177e4SLinus Torvalds 	}
3723d50a6b56SStephen Hemminger 
37244e58a027SCong Wang 	t->net = pn;
37251fbe4b46SOleg Nesterov 	get_task_struct(p);
3726ee74baa7SDavid S. Miller 	wake_up_process(p);
3727d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
37281da177e4SLinus Torvalds 
37291da177e4SLinus Torvalds 	return 0;
37301da177e4SLinus Torvalds }
37311da177e4SLinus Torvalds 
37321da177e4SLinus Torvalds /*
37331da177e4SLinus Torvalds  * Removes a device from the thread if_list.
37341da177e4SLinus Torvalds  */
3735222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3736222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
37371da177e4SLinus Torvalds {
3738c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3739c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
37401da177e4SLinus Torvalds 
37418788370aSJesper Dangaard Brouer 	if_lock(t);
3742c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3743c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3744c26a8016SLuiz Capitulino 		if (p == pkt_dev)
37458788370aSJesper Dangaard Brouer 			list_del_rcu(&p->list);
37461da177e4SLinus Torvalds 	}
37478788370aSJesper Dangaard Brouer 	if_unlock(t);
37481da177e4SLinus Torvalds }
37491da177e4SLinus Torvalds 
3750222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3751222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
37521da177e4SLinus Torvalds {
3753f9467eaeSJoe Perches 	pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
37541da177e4SLinus Torvalds 
37551da177e4SLinus Torvalds 	if (pkt_dev->running) {
3756294a0b7fSJoe Perches 		pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
37571da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
37581da177e4SLinus Torvalds 	}
37591da177e4SLinus Torvalds 
37601da177e4SLinus Torvalds 	/* Dis-associate from the interface */
37611da177e4SLinus Torvalds 
37621da177e4SLinus Torvalds 	if (pkt_dev->odev) {
37631da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
37641da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
37651da177e4SLinus Torvalds 	}
37661da177e4SLinus Torvalds 
37678788370aSJesper Dangaard Brouer 	/* Remove proc before if_list entry, because add_device uses
37688788370aSJesper Dangaard Brouer 	 * list to determine if interface already exist, avoid race
37698788370aSJesper Dangaard Brouer 	 * with proc_create_data() */
3770a8ca16eaSDavid Howells 	proc_remove(pkt_dev->entry);
37711da177e4SLinus Torvalds 
37728788370aSJesper Dangaard Brouer 	/* And update the thread if_list */
37738788370aSJesper Dangaard Brouer 	_rem_dev_from_if_list(t, pkt_dev);
37748788370aSJesper Dangaard Brouer 
3775a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3776a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3777a553e4a6SJamal Hadi Salim #endif
37781da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
377926ad7879SEric Dumazet 	if (pkt_dev->page)
378026ad7879SEric Dumazet 		put_page(pkt_dev->page);
37818788370aSJesper Dangaard Brouer 	kfree_rcu(pkt_dev, rcu);
37821da177e4SLinus Torvalds 	return 0;
37831da177e4SLinus Torvalds }
37841da177e4SLinus Torvalds 
37854e58a027SCong Wang static int __net_init pg_net_init(struct net *net)
37861da177e4SLinus Torvalds {
37874e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3788d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
37894e58a027SCong Wang 	int cpu, ret = 0;
3790d50a6b56SStephen Hemminger 
37914e58a027SCong Wang 	pn->net = net;
37924e58a027SCong Wang 	INIT_LIST_HEAD(&pn->pktgen_threads);
37934e58a027SCong Wang 	pn->pktgen_exiting = false;
37944e58a027SCong Wang 	pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
37954e58a027SCong Wang 	if (!pn->proc_dir) {
37964e58a027SCong Wang 		pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3797d50a6b56SStephen Hemminger 		return -ENODEV;
37981da177e4SLinus Torvalds 	}
37994e58a027SCong Wang 	pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
38004e58a027SCong Wang 	if (pe == NULL) {
38014e58a027SCong Wang 		pr_err("cannot create %s procfs entry\n", PGCTRL);
38024e58a027SCong Wang 		ret = -EINVAL;
38034e58a027SCong Wang 		goto remove;
38044e58a027SCong Wang 	}
38051da177e4SLinus Torvalds 
3806670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
38078024bb24SLuiz Capitulino 		int err;
38081da177e4SLinus Torvalds 
38094e58a027SCong Wang 		err = pktgen_create_thread(cpu, pn);
38108024bb24SLuiz Capitulino 		if (err)
38114e58a027SCong Wang 			pr_warn("Cannot create thread for cpu %d (%d)\n",
3812f9467eaeSJoe Perches 				   cpu, err);
38131da177e4SLinus Torvalds 	}
38148024bb24SLuiz Capitulino 
38154e58a027SCong Wang 	if (list_empty(&pn->pktgen_threads)) {
38164e58a027SCong Wang 		pr_err("Initialization failed for all threads\n");
3817ce14f894SWANG Cong 		ret = -ENODEV;
38184e58a027SCong Wang 		goto remove_entry;
38198024bb24SLuiz Capitulino 	}
38208024bb24SLuiz Capitulino 
38211da177e4SLinus Torvalds 	return 0;
3822ce14f894SWANG Cong 
38234e58a027SCong Wang remove_entry:
38244e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
38254e58a027SCong Wang remove:
3826ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3827ce14f894SWANG Cong 	return ret;
38281da177e4SLinus Torvalds }
38291da177e4SLinus Torvalds 
38304e58a027SCong Wang static void __net_exit pg_net_exit(struct net *net)
38311da177e4SLinus Torvalds {
38324e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3833cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3834cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
3835d4b11335SEric Dumazet 	LIST_HEAD(list);
38361da177e4SLinus Torvalds 
38371da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
38384e58a027SCong Wang 	pn->pktgen_exiting = true;
38391da177e4SLinus Torvalds 
3840c57b5468SEric Dumazet 	mutex_lock(&pktgen_thread_lock);
38414e58a027SCong Wang 	list_splice_init(&pn->pktgen_threads, &list);
3842c57b5468SEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
3843c57b5468SEric Dumazet 
3844c57b5468SEric Dumazet 	list_for_each_safe(q, n, &list) {
3845cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3846c57b5468SEric Dumazet 		list_del(&t->th_list);
3847ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
38481fbe4b46SOleg Nesterov 		put_task_struct(t->tsk);
3849ee74baa7SDavid S. Miller 		kfree(t);
38501da177e4SLinus Torvalds 	}
38511da177e4SLinus Torvalds 
38524e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
3853ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
38544e58a027SCong Wang }
38551da177e4SLinus Torvalds 
38564e58a027SCong Wang static struct pernet_operations pg_net_ops = {
38574e58a027SCong Wang 	.init = pg_net_init,
38584e58a027SCong Wang 	.exit = pg_net_exit,
38594e58a027SCong Wang 	.id   = &pg_net_id,
38604e58a027SCong Wang 	.size = sizeof(struct pktgen_net),
38614e58a027SCong Wang };
38624e58a027SCong Wang 
38634e58a027SCong Wang static int __init pg_init(void)
38644e58a027SCong Wang {
38654e58a027SCong Wang 	int ret = 0;
38664e58a027SCong Wang 
38674e58a027SCong Wang 	pr_info("%s", version);
38684e58a027SCong Wang 	ret = register_pernet_subsys(&pg_net_ops);
38694e58a027SCong Wang 	if (ret)
38704e58a027SCong Wang 		return ret;
38714e58a027SCong Wang 	ret = register_netdevice_notifier(&pktgen_notifier_block);
38724e58a027SCong Wang 	if (ret)
38734e58a027SCong Wang 		unregister_pernet_subsys(&pg_net_ops);
38744e58a027SCong Wang 
38754e58a027SCong Wang 	return ret;
38764e58a027SCong Wang }
38774e58a027SCong Wang 
38784e58a027SCong Wang static void __exit pg_cleanup(void)
38794e58a027SCong Wang {
38804e58a027SCong Wang 	unregister_netdevice_notifier(&pktgen_notifier_block);
38814e58a027SCong Wang 	unregister_pernet_subsys(&pg_net_ops);
38828788370aSJesper Dangaard Brouer 	/* Don't need rcu_barrier() due to use of kfree_rcu() */
38831da177e4SLinus Torvalds }
38841da177e4SLinus Torvalds 
38851da177e4SLinus Torvalds module_init(pg_init);
38861da177e4SLinus Torvalds module_exit(pg_cleanup);
38871da177e4SLinus Torvalds 
3888c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
38891da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
38901da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3891c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
38921da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3893c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
38941da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3895c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
38961da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3897c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
38981da177e4SLinus Torvalds module_param(debug, int, 0);
3899c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3900