xref: /openbmc/linux/net/core/pktgen.c (revision a5135bcfba7345031df45e02cd150a45add47cf8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Authors:
31da177e4SLinus Torvalds  * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
41da177e4SLinus Torvalds  *                             Uppsala University and
51da177e4SLinus Torvalds  *                             Swedish University of Agricultural Sciences
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Alexey Kuznetsov  <kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  * Ben Greear <greearb@candelatech.com>
996de0e25SJan Engelhardt  * Jens Låås <jens.laas@data.slu.se>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * A tool for loading the network with preconfigurated packets.
181da177e4SLinus Torvalds  * The tool is implemented as a linux module.  Parameters are output
191da177e4SLinus Torvalds  * device, delay (to hard_xmit), number of packets, and whether
201da177e4SLinus Torvalds  * to use multiple SKBs or just the same one.
211da177e4SLinus Torvalds  * pktgen uses the installed interface's output routine.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * Additional hacking by:
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Jens.Laas@data.slu.se
261da177e4SLinus Torvalds  * Improved by ANK. 010120.
271da177e4SLinus Torvalds  * Improved by ANK even more. 010212.
281da177e4SLinus Torvalds  * MAC address typo fixed. 010417 --ro
291da177e4SLinus Torvalds  * Integrated.  020301 --DaveM
301da177e4SLinus Torvalds  * Added multiskb option 020301 --DaveM
311da177e4SLinus Torvalds  * Scaling of results. 020417--sigurdur@linpro.no
321da177e4SLinus Torvalds  * Significant re-work of the module:
331da177e4SLinus Torvalds  *   *  Convert to threaded model to more efficiently be able to transmit
341da177e4SLinus Torvalds  *       and receive on multiple interfaces at once.
351da177e4SLinus Torvalds  *   *  Converted many counters to __u64 to allow longer runs.
361da177e4SLinus Torvalds  *   *  Allow configuration of ranges, like min/max IP address, MACs,
371da177e4SLinus Torvalds  *       and UDP-ports, for both source and destination, and can
381da177e4SLinus Torvalds  *       set to use a random distribution or sequentially walk the range.
391da177e4SLinus Torvalds  *   *  Can now change most values after starting.
401da177e4SLinus Torvalds  *   *  Place 12-byte packet in UDP payload with magic number,
411da177e4SLinus Torvalds  *       sequence number, and timestamp.
421da177e4SLinus Torvalds  *   *  Add receiver code that detects dropped pkts, re-ordered pkts, and
431da177e4SLinus Torvalds  *       latencies (with micro-second) precision.
441da177e4SLinus Torvalds  *   *  Add IOCTL interface to easily get counters & configuration.
451da177e4SLinus Torvalds  *   --Ben Greear <greearb@candelatech.com>
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
481da177e4SLinus Torvalds  * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
491da177e4SLinus Torvalds  * as a "fastpath" with a configurable number of clones after alloc's.
501da177e4SLinus Torvalds  * clone_skb=0 means all packets are allocated this also means ranges time
511da177e4SLinus Torvalds  * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
521da177e4SLinus Torvalds  * clones.
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  * Also moved to /proc/net/pktgen/
551da177e4SLinus Torvalds  * --ro
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * Sept 10:  Fixed threading/locking.  Lots of bone-headed and more clever
581da177e4SLinus Torvalds  *    mistakes.  Also merged in DaveM's patch in the -pre6 patch.
591da177e4SLinus Torvalds  * --Ben Greear <greearb@candelatech.com>
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  *
641da177e4SLinus Torvalds  * 021124 Finished major redesign and rewrite for new functionality.
651da177e4SLinus Torvalds  * See Documentation/networking/pktgen.txt for how to use this.
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  * The new operation:
681da177e4SLinus Torvalds  * For each CPU one thread/process is created at start. This process checks
691da177e4SLinus Torvalds  * for running devices in the if_list and sends packets until count is 0 it
701da177e4SLinus Torvalds  * also the thread checks the thread->control which is used for inter-process
711da177e4SLinus Torvalds  * communication. controlling process "posts" operations to the threads this
728788370aSJesper Dangaard Brouer  * way.
738788370aSJesper Dangaard Brouer  * The if_list is RCU protected, and the if_lock remains to protect updating
748788370aSJesper Dangaard Brouer  * of if_list, from "add_device" as it invoked from userspace (via proc write).
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  * By design there should only be *one* "controlling" process. In practice
771da177e4SLinus Torvalds  * multiple write accesses gives unpredictable result. Understood by "write"
781da177e4SLinus Torvalds  * to /proc gives result code thats should be read be the "writer".
79b4099fabSStephen Hemminger  * For practical use this should be no problem.
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  * Note when adding devices to a specific CPU there good idea to also assign
821da177e4SLinus Torvalds  * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
831da177e4SLinus Torvalds  * --ro
841da177e4SLinus Torvalds  *
851da177e4SLinus Torvalds  * Fix refcount off by one if first packet fails, potential null deref,
861da177e4SLinus Torvalds  * memleak 030710- KJP
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * First "ranges" functionality for ipv6 030726 --ro
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * Included flow support. 030802 ANK.
911da177e4SLinus Torvalds  *
921da177e4SLinus Torvalds  * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
931da177e4SLinus Torvalds  *
941da177e4SLinus Torvalds  * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
951da177e4SLinus Torvalds  * ia64 compilation fix from  Aron Griffis <aron@hp.com> 040604
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  * New xmit() return, do_div and misc clean up by Stephen Hemminger
981da177e4SLinus Torvalds  * <shemminger@osdl.org> 040923
991da177e4SLinus Torvalds  *
100ca9f1fd2SStephen Hemminger  * Randy Dunlap fixed u64 printk compiler warning
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  * Remove FCS from BW calculation.  Lennert Buytenhek <buytenh@wantstofly.org>
1031da177e4SLinus Torvalds  * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
1041da177e4SLinus Torvalds  *
1051da177e4SLinus Torvalds  * Corrections from Nikolai Malykh (nmalykh@bilim.com)
1061da177e4SLinus Torvalds  * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
1071da177e4SLinus Torvalds  *
1081da177e4SLinus Torvalds  * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
1091da177e4SLinus Torvalds  * 050103
110ca6549afSSteven Whitehouse  *
111ca6549afSSteven Whitehouse  * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112ca6549afSSteven Whitehouse  *
11334954ddcSFrancesco Fondelli  * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
11434954ddcSFrancesco Fondelli  *
115ce5d0b47SAdit Ranadive  * Fixed src_mac command to set source mac of packet to value specified in
116ce5d0b47SAdit Ranadive  * command by Adit Ranadive <adit.262@gmail.com>
117ce5d0b47SAdit Ranadive  *
1181da177e4SLinus Torvalds  */
119f9467eaeSJoe Perches 
120f9467eaeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
121f9467eaeSJoe Perches 
1221da177e4SLinus Torvalds #include <linux/sys.h>
1231da177e4SLinus Torvalds #include <linux/types.h>
1241da177e4SLinus Torvalds #include <linux/module.h>
1251da177e4SLinus Torvalds #include <linux/moduleparam.h>
1261da177e4SLinus Torvalds #include <linux/kernel.h>
127222fa076SLuiz Capitulino #include <linux/mutex.h>
1281da177e4SLinus Torvalds #include <linux/sched.h>
1291da177e4SLinus Torvalds #include <linux/slab.h>
1301da177e4SLinus Torvalds #include <linux/vmalloc.h>
1311da177e4SLinus Torvalds #include <linux/unistd.h>
1321da177e4SLinus Torvalds #include <linux/string.h>
1331da177e4SLinus Torvalds #include <linux/ptrace.h>
1341da177e4SLinus Torvalds #include <linux/errno.h>
1351da177e4SLinus Torvalds #include <linux/ioport.h>
1361da177e4SLinus Torvalds #include <linux/interrupt.h>
1374fc268d2SRandy Dunlap #include <linux/capability.h>
1382bc481cfSStephen Hemminger #include <linux/hrtimer.h>
13909fe3ef4SAndrew Morton #include <linux/freezer.h>
1401da177e4SLinus Torvalds #include <linux/delay.h>
1411da177e4SLinus Torvalds #include <linux/timer.h>
142cdcdbe0bSLuiz Capitulino #include <linux/list.h>
1431da177e4SLinus Torvalds #include <linux/init.h>
1441da177e4SLinus Torvalds #include <linux/skbuff.h>
1451da177e4SLinus Torvalds #include <linux/netdevice.h>
1461da177e4SLinus Torvalds #include <linux/inet.h>
1471da177e4SLinus Torvalds #include <linux/inetdevice.h>
1481da177e4SLinus Torvalds #include <linux/rtnetlink.h>
1491da177e4SLinus Torvalds #include <linux/if_arp.h>
15034954ddcSFrancesco Fondelli #include <linux/if_vlan.h>
1511da177e4SLinus Torvalds #include <linux/in.h>
1521da177e4SLinus Torvalds #include <linux/ip.h>
1531da177e4SLinus Torvalds #include <linux/ipv6.h>
1541da177e4SLinus Torvalds #include <linux/udp.h>
1551da177e4SLinus Torvalds #include <linux/proc_fs.h>
156d50a6b56SStephen Hemminger #include <linux/seq_file.h>
1571da177e4SLinus Torvalds #include <linux/wait.h>
158f404e9a6SKris Katterjohn #include <linux/etherdevice.h>
159ee74baa7SDavid S. Miller #include <linux/kthread.h>
160268bb0ceSLinus Torvalds #include <linux/prefetch.h>
161457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1621da177e4SLinus Torvalds #include <net/checksum.h>
1631da177e4SLinus Torvalds #include <net/ipv6.h>
164c26bf4a5SThomas Graf #include <net/udp.h>
16573d94e94SStephen Rothwell #include <net/ip6_checksum.h>
1661da177e4SLinus Torvalds #include <net/addrconf.h>
167a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
168a553e4a6SJamal Hadi Salim #include <net/xfrm.h>
169a553e4a6SJamal Hadi Salim #endif
1704e58a027SCong Wang #include <net/netns/generic.h>
1711da177e4SLinus Torvalds #include <asm/byteorder.h>
1721da177e4SLinus Torvalds #include <linux/rcupdate.h>
1731977f032SJiri Slaby #include <linux/bitops.h>
17463adc6fbSStephen Hemminger #include <linux/io.h>
17563adc6fbSStephen Hemminger #include <linux/timex.h>
17663adc6fbSStephen Hemminger #include <linux/uaccess.h>
1771da177e4SLinus Torvalds #include <asm/dma.h>
1781da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1791da177e4SLinus Torvalds 
18040207264SJesper Dangaard Brouer #define VERSION	"2.75"
1811da177e4SLinus Torvalds #define IP_NAME_SZ 32
182ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
183d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1841da177e4SLinus Torvalds 
185f9467eaeSJoe Perches #define func_enter() pr_debug("entering %s\n", __func__);
186f9467eaeSJoe Perches 
1871da177e4SLinus Torvalds /* Device flag bits */
1881da177e4SLinus Torvalds #define F_IPSRC_RND   (1<<0)	/* IP-Src Random  */
1891da177e4SLinus Torvalds #define F_IPDST_RND   (1<<1)	/* IP-Dst Random  */
1901da177e4SLinus Torvalds #define F_UDPSRC_RND  (1<<2)	/* UDP-Src Random */
1911da177e4SLinus Torvalds #define F_UDPDST_RND  (1<<3)	/* UDP-Dst Random */
1921da177e4SLinus Torvalds #define F_MACSRC_RND  (1<<4)	/* MAC-Src Random */
1931da177e4SLinus Torvalds #define F_MACDST_RND  (1<<5)	/* MAC-Dst Random */
1941da177e4SLinus Torvalds #define F_TXSIZE_RND  (1<<6)	/* Transmit size is random */
1951da177e4SLinus Torvalds #define F_IPV6        (1<<7)	/* Interface in IPV6 Mode */
196ca6549afSSteven Whitehouse #define F_MPLS_RND    (1<<8)	/* Random MPLS labels */
19734954ddcSFrancesco Fondelli #define F_VID_RND     (1<<9)	/* Random VLAN ID */
19834954ddcSFrancesco Fondelli #define F_SVID_RND    (1<<10)	/* Random SVLAN ID */
199007a531bSJamal Hadi Salim #define F_FLOW_SEQ    (1<<11)	/* Sequential flows */
200a553e4a6SJamal Hadi Salim #define F_IPSEC_ON    (1<<12)	/* ipsec on for flows */
20145b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13)	/* queue map Random */
202e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14)	/* queue map mirrors smp_processor_id() */
203e99b99b4SRobert Olsson #define F_NODE          (1<<15)	/* Node memory alloc*/
204c26bf4a5SThomas Graf #define F_UDPCSUM       (1<<16)	/* Include UDP checksum */
205afb84b62SJesper Dangaard Brouer #define F_NO_TIMESTAMP  (1<<17)	/* Don't timestamp packets (default TS) */
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /* Thread control flag bits */
2086b80d6a6SStephen Hemminger #define T_STOP        (1<<0)	/* Stop run */
2096b80d6a6SStephen Hemminger #define T_RUN         (1<<1)	/* Start run */
2106b80d6a6SStephen Hemminger #define T_REMDEVALL   (1<<2)	/* Remove all devs */
2116b80d6a6SStephen Hemminger #define T_REMDEV      (1<<3)	/* Remove one dev */
2121da177e4SLinus Torvalds 
21362f64aedSAlexei Starovoitov /* Xmit modes */
21462f64aedSAlexei Starovoitov #define M_START_XMIT		0	/* Default normal TX */
21562f64aedSAlexei Starovoitov #define M_NETIF_RECEIVE 	1	/* Inject packets into stack */
2160967f244SJohn Fastabend #define M_QUEUE_XMIT		2	/* Inject packet into qdisc */
21762f64aedSAlexei Starovoitov 
2188788370aSJesper Dangaard Brouer /* If lock -- protects updating of if_list */
2199a0b1e8bSEric Dumazet #define   if_lock(t)           mutex_lock(&(t->if_lock));
2209a0b1e8bSEric Dumazet #define   if_unlock(t)           mutex_unlock(&(t->if_lock));
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2231da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
224d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
225d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds #define MAX_CFLOWS  65536
2281da177e4SLinus Torvalds 
22934954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
23034954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
23134954ddcSFrancesco Fondelli 
232222f1806SLuiz Capitulino struct flow_state {
233252e3346SAl Viro 	__be32 cur_daddr;
2341da177e4SLinus Torvalds 	int count;
235a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
236a553e4a6SJamal Hadi Salim 	struct xfrm_state *x;
237a553e4a6SJamal Hadi Salim #endif
238007a531bSJamal Hadi Salim 	__u32 flags;
2391da177e4SLinus Torvalds };
2401da177e4SLinus Torvalds 
241007a531bSJamal Hadi Salim /* flow flag bits */
242007a531bSJamal Hadi Salim #define F_INIT   (1<<0)		/* flow has been initialized */
243007a531bSJamal Hadi Salim 
2441da177e4SLinus Torvalds struct pktgen_dev {
2451da177e4SLinus Torvalds 	/*
2461da177e4SLinus Torvalds 	 * Try to keep frequent/infrequent used vars. separated.
2471da177e4SLinus Torvalds 	 */
24839df232fSStephen Hemminger 	struct proc_dir_entry *entry;	/* proc file */
2491da177e4SLinus Torvalds 	struct pktgen_thread *pg_thread;/* the owner */
25063adc6fbSStephen Hemminger 	struct list_head list;		/* chaining in the thread's run-queue */
2518788370aSJesper Dangaard Brouer 	struct rcu_head	 rcu;		/* freed by RCU */
2521da177e4SLinus Torvalds 
25363adc6fbSStephen Hemminger 	int running;		/* if false, the test will stop */
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	/* If min != max, then we will either do a linear iteration, or
2561da177e4SLinus Torvalds 	 * we will do a random selection from within the range.
2571da177e4SLinus Torvalds 	 */
2581da177e4SLinus Torvalds 	__u32 flags;
25962f64aedSAlexei Starovoitov 	int xmit_mode;
26068bf9f0bSAmerigo Wang 	int min_pkt_size;
26168bf9f0bSAmerigo Wang 	int max_pkt_size;
26216dab72fSJamal Hadi Salim 	int pkt_overhead;	/* overhead for MPLS, VLANs, IPSEC etc */
2631da177e4SLinus Torvalds 	int nfrags;
26462f64aedSAlexei Starovoitov 	int removal_mark;	/* non-zero => the device is marked for
26562f64aedSAlexei Starovoitov 				 * removal by worker thread */
26662f64aedSAlexei Starovoitov 
26726ad7879SEric Dumazet 	struct page *page;
268fd29cf72SStephen Hemminger 	u64 delay;		/* nano-seconds */
269fd29cf72SStephen Hemminger 
2701da177e4SLinus Torvalds 	__u64 count;		/* Default No packets to send */
2711da177e4SLinus Torvalds 	__u64 sofar;		/* How many pkts we've sent so far */
2721da177e4SLinus Torvalds 	__u64 tx_bytes;		/* How many bytes we've transmitted */
273f466dba1SJohn Fastabend 	__u64 errors;		/* Errors when trying to transmit, */
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/* runtime counters relating to clone_skb */
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	__u32 clone_count;
2781da177e4SLinus Torvalds 	int last_ok;		/* Was last skb sent?
27963adc6fbSStephen Hemminger 				 * Or a failed transmit of some sort?
28063adc6fbSStephen Hemminger 				 * This will keep sequence numbers in order
2811da177e4SLinus Torvalds 				 */
282fd29cf72SStephen Hemminger 	ktime_t next_tx;
283fd29cf72SStephen Hemminger 	ktime_t started_at;
284fd29cf72SStephen Hemminger 	ktime_t stopped_at;
285fd29cf72SStephen Hemminger 	u64	idle_acc;	/* nano-seconds */
286fd29cf72SStephen Hemminger 
2871da177e4SLinus Torvalds 	__u32 seq_num;
2881da177e4SLinus Torvalds 
28963adc6fbSStephen Hemminger 	int clone_skb;		/*
29063adc6fbSStephen Hemminger 				 * Use multiple SKBs during packet gen.
29163adc6fbSStephen Hemminger 				 * If this number is greater than 1, then
29263adc6fbSStephen Hemminger 				 * that many copies of the same packet will be
29363adc6fbSStephen Hemminger 				 * sent before a new packet is allocated.
29463adc6fbSStephen Hemminger 				 * If you want to send 1024 identical packets
29563adc6fbSStephen Hemminger 				 * before creating a new packet,
29663adc6fbSStephen Hemminger 				 * set clone_skb to 1024.
2971da177e4SLinus Torvalds 				 */
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	char dst_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
3001da177e4SLinus Torvalds 	char dst_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
3011da177e4SLinus Torvalds 	char src_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
3021da177e4SLinus Torvalds 	char src_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	struct in6_addr in6_saddr;
3051da177e4SLinus Torvalds 	struct in6_addr in6_daddr;
3061da177e4SLinus Torvalds 	struct in6_addr cur_in6_daddr;
3071da177e4SLinus Torvalds 	struct in6_addr cur_in6_saddr;
3081da177e4SLinus Torvalds 	/* For ranges */
3091da177e4SLinus Torvalds 	struct in6_addr min_in6_daddr;
3101da177e4SLinus Torvalds 	struct in6_addr max_in6_daddr;
3111da177e4SLinus Torvalds 	struct in6_addr min_in6_saddr;
3121da177e4SLinus Torvalds 	struct in6_addr max_in6_saddr;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	/* If we're doing ranges, random or incremental, then this
3151da177e4SLinus Torvalds 	 * defines the min/max for those ranges.
3161da177e4SLinus Torvalds 	 */
317252e3346SAl Viro 	__be32 saddr_min;	/* inclusive, source IP address */
318252e3346SAl Viro 	__be32 saddr_max;	/* exclusive, source IP address */
319252e3346SAl Viro 	__be32 daddr_min;	/* inclusive, dest IP address */
320252e3346SAl Viro 	__be32 daddr_max;	/* exclusive, dest IP address */
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	__u16 udp_src_min;	/* inclusive, source UDP port */
3231da177e4SLinus Torvalds 	__u16 udp_src_max;	/* exclusive, source UDP port */
3241da177e4SLinus Torvalds 	__u16 udp_dst_min;	/* inclusive, dest UDP port */
3251da177e4SLinus Torvalds 	__u16 udp_dst_max;	/* exclusive, dest UDP port */
3261da177e4SLinus Torvalds 
3271ca7768cSFrancesco Fondelli 	/* DSCP + ECN */
32863adc6fbSStephen Hemminger 	__u8 tos;            /* six MSB of (former) IPv4 TOS
32963adc6fbSStephen Hemminger 				are for dscp codepoint */
33063adc6fbSStephen Hemminger 	__u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6
33163adc6fbSStephen Hemminger 				(see RFC 3260, sec. 4) */
3321ca7768cSFrancesco Fondelli 
333ca6549afSSteven Whitehouse 	/* MPLS */
33495c96174SEric Dumazet 	unsigned int nr_labels;	/* Depth of stack, 0 = no MPLS */
335ca6549afSSteven Whitehouse 	__be32 labels[MAX_MPLS_LABELS];
336ca6549afSSteven Whitehouse 
33734954ddcSFrancesco Fondelli 	/* VLAN/SVLAN (802.1Q/Q-in-Q) */
33834954ddcSFrancesco Fondelli 	__u8  vlan_p;
33934954ddcSFrancesco Fondelli 	__u8  vlan_cfi;
34034954ddcSFrancesco Fondelli 	__u16 vlan_id;  /* 0xffff means no vlan tag */
34134954ddcSFrancesco Fondelli 
34234954ddcSFrancesco Fondelli 	__u8  svlan_p;
34334954ddcSFrancesco Fondelli 	__u8  svlan_cfi;
34434954ddcSFrancesco Fondelli 	__u16 svlan_id; /* 0xffff means no svlan tag */
34534954ddcSFrancesco Fondelli 
3461da177e4SLinus Torvalds 	__u32 src_mac_count;	/* How many MACs to iterate through */
3471da177e4SLinus Torvalds 	__u32 dst_mac_count;	/* How many MACs to iterate through */
3481da177e4SLinus Torvalds 
349f404e9a6SKris Katterjohn 	unsigned char dst_mac[ETH_ALEN];
350f404e9a6SKris Katterjohn 	unsigned char src_mac[ETH_ALEN];
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	__u32 cur_dst_mac_offset;
3531da177e4SLinus Torvalds 	__u32 cur_src_mac_offset;
354252e3346SAl Viro 	__be32 cur_saddr;
355252e3346SAl Viro 	__be32 cur_daddr;
35666ed1e5eSEric Dumazet 	__u16 ip_id;
3571da177e4SLinus Torvalds 	__u16 cur_udp_dst;
3581da177e4SLinus Torvalds 	__u16 cur_udp_src;
35945b270f8SRobert Olsson 	__u16 cur_queue_map;
3601da177e4SLinus Torvalds 	__u32 cur_pkt_size;
361baac8564SEric Dumazet 	__u32 last_pkt_size;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	__u8 hh[14];
3641da177e4SLinus Torvalds 	/* = {
3651da177e4SLinus Torvalds 	   0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	   We fill in SRC address later
3681da177e4SLinus Torvalds 	   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3691da177e4SLinus Torvalds 	   0x08, 0x00
3701da177e4SLinus Torvalds 	   };
3711da177e4SLinus Torvalds 	 */
3721da177e4SLinus Torvalds 	__u16 pad;		/* pad out the hh struct to an even 16 bytes */
3731da177e4SLinus Torvalds 
37463adc6fbSStephen Hemminger 	struct sk_buff *skb;	/* skb we are to transmit next, used for when we
3751da177e4SLinus Torvalds 				 * are transmitting the same one multiple times
3761da177e4SLinus Torvalds 				 */
37763adc6fbSStephen Hemminger 	struct net_device *odev; /* The out-going device.
37863adc6fbSStephen Hemminger 				  * Note that the device should have it's
37963adc6fbSStephen Hemminger 				  * pg_info pointer pointing back to this
38063adc6fbSStephen Hemminger 				  * device.
38163adc6fbSStephen Hemminger 				  * Set when the user specifies the out-going
38263adc6fbSStephen Hemminger 				  * device name (not when the inject is
3831da177e4SLinus Torvalds 				  * started as it used to do.)
3841da177e4SLinus Torvalds 				  */
385593f63b0SEric Dumazet 	char odevname[32];
3861da177e4SLinus Torvalds 	struct flow_state *flows;
38795c96174SEric Dumazet 	unsigned int cflows;	/* Concurrent flows (config) */
38895c96174SEric Dumazet 	unsigned int lflow;		/* Flow length  (config) */
38995c96174SEric Dumazet 	unsigned int nflows;	/* accumulated flows (stats) */
39095c96174SEric Dumazet 	unsigned int curfl;		/* current sequenced flow (state)*/
39145b270f8SRobert Olsson 
39245b270f8SRobert Olsson 	u16 queue_map_min;
39345b270f8SRobert Olsson 	u16 queue_map_max;
3949e50e3acSJohn Fastabend 	__u32 skb_priority;	/* skb priority field */
39538b2cf29SAlexei Starovoitov 	unsigned int burst;	/* number of duplicated packets to burst */
396e99b99b4SRobert Olsson 	int node;               /* Memory node */
39745b270f8SRobert Olsson 
398a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
399a553e4a6SJamal Hadi Salim 	__u8	ipsmode;		/* IPSEC mode (config) */
400a553e4a6SJamal Hadi Salim 	__u8	ipsproto;		/* IPSEC type (config) */
401de4aee7dSFan Du 	__u32	spi;
402cf93d47eSFan Du 	struct dst_entry dst;
403cf93d47eSFan Du 	struct dst_ops dstops;
404a553e4a6SJamal Hadi Salim #endif
40539df232fSStephen Hemminger 	char result[512];
4061da177e4SLinus Torvalds };
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds struct pktgen_hdr {
409252e3346SAl Viro 	__be32 pgh_magic;
410252e3346SAl Viro 	__be32 seq_num;
411252e3346SAl Viro 	__be32 tv_sec;
412252e3346SAl Viro 	__be32 tv_usec;
4131da177e4SLinus Torvalds };
4141da177e4SLinus Torvalds 
4154e58a027SCong Wang 
416c7d03a00SAlexey Dobriyan static unsigned int pg_net_id __read_mostly;
4174e58a027SCong Wang 
4184e58a027SCong Wang struct pktgen_net {
4194e58a027SCong Wang 	struct net		*net;
4204e58a027SCong Wang 	struct proc_dir_entry	*proc_dir;
4214e58a027SCong Wang 	struct list_head	pktgen_threads;
4224e58a027SCong Wang 	bool			pktgen_exiting;
4234e58a027SCong Wang };
424551eaff1SEric Dumazet 
4251da177e4SLinus Torvalds struct pktgen_thread {
4269a0b1e8bSEric Dumazet 	struct mutex if_lock;		/* for list of devices */
427c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
428cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
429ee74baa7SDavid S. Miller 	struct task_struct *tsk;
4301da177e4SLinus Torvalds 	char result[512];
4311da177e4SLinus Torvalds 
43263adc6fbSStephen Hemminger 	/* Field for thread to receive "posted" events terminate,
43363adc6fbSStephen Hemminger 	   stop ifs etc. */
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	u32 control;
4361da177e4SLinus Torvalds 	int cpu;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	wait_queue_head_t queue;
439d3ede327SDenis V. Lunev 	struct completion start_done;
4404e58a027SCong Wang 	struct pktgen_net *net;
4411da177e4SLinus Torvalds };
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds #define REMOVE 1
4441da177e4SLinus Torvalds #define FIND   0
4451da177e4SLinus Torvalds 
446c3d2f52dSStephen Hemminger static const char version[] =
447f9467eaeSJoe Perches 	"Packet Generator for packet performance testing. "
448f9467eaeSJoe Perches 	"Version: " VERSION "\n";
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4511da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
452222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
4533e984840SEric Dumazet 					  const char *ifname, bool exact);
4541da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
4554e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn);
4564e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn);
4574e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
4583bda06a3SStephen Hemminger 
4591da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4601da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
46139df232fSStephen Hemminger 
4621da177e4SLinus Torvalds /* Module parameters, defaults. */
46365c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
46465c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
46565c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
46665c5b786SStephen Hemminger static int debug  __read_mostly;
4671da177e4SLinus Torvalds 
468222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4711da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4721da177e4SLinus Torvalds };
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds /*
4751da177e4SLinus Torvalds  * /proc handling functions
4761da177e4SLinus Torvalds  *
4771da177e4SLinus Torvalds  */
4781da177e4SLinus Torvalds 
479d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
480d50a6b56SStephen Hemminger {
481c3d2f52dSStephen Hemminger 	seq_puts(seq, version);
482d50a6b56SStephen Hemminger 	return 0;
483d50a6b56SStephen Hemminger }
4841da177e4SLinus Torvalds 
485d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user *buf,
4861da177e4SLinus Torvalds 			    size_t count, loff_t *ppos)
4871da177e4SLinus Torvalds {
488d50a6b56SStephen Hemminger 	char data[128];
4894e58a027SCong Wang 	struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
4901da177e4SLinus Torvalds 
49109455747SMathias Krause 	if (!capable(CAP_NET_ADMIN))
49209455747SMathias Krause 		return -EPERM;
4931da177e4SLinus Torvalds 
49420b0c718SMathias Krause 	if (count == 0)
49520b0c718SMathias Krause 		return -EINVAL;
49620b0c718SMathias Krause 
497d50a6b56SStephen Hemminger 	if (count > sizeof(data))
498d50a6b56SStephen Hemminger 		count = sizeof(data);
4991da177e4SLinus Torvalds 
50009455747SMathias Krause 	if (copy_from_user(data, buf, count))
50109455747SMathias Krause 		return -EFAULT;
50209455747SMathias Krause 
50320b0c718SMathias Krause 	data[count - 1] = 0;	/* Strip trailing '\n' and terminate string */
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
5064e58a027SCong Wang 		pktgen_stop_all_threads_ifs(pn);
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
5094e58a027SCong Wang 		pktgen_run_all_threads(pn);
5101da177e4SLinus Torvalds 
511eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
5124e58a027SCong Wang 		pktgen_reset_all_threads(pn);
513eb37b41cSJesse Brandeburg 
5141da177e4SLinus Torvalds 	else
51540207264SJesper Dangaard Brouer 		return -EINVAL;
5161da177e4SLinus Torvalds 
51709455747SMathias Krause 	return count;
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
520d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
5211da177e4SLinus Torvalds {
522d9dda78bSAl Viro 	return single_open(file, pgctrl_show, PDE_DATA(inode));
523d50a6b56SStephen Hemminger }
524d50a6b56SStephen Hemminger 
5259a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
526d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
527d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
528d50a6b56SStephen Hemminger 	.read    = seq_read,
529d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
530d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
531d50a6b56SStephen Hemminger 	.release = single_release,
532d50a6b56SStephen Hemminger };
533d50a6b56SStephen Hemminger 
534d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
535d50a6b56SStephen Hemminger {
536648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
537fd29cf72SStephen Hemminger 	ktime_t stopped;
538fd29cf72SStephen Hemminger 	u64 idle;
5391da177e4SLinus Torvalds 
540222f1806SLuiz Capitulino 	seq_printf(seq,
541222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
542222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
543222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5441da177e4SLinus Torvalds 
545222f1806SLuiz Capitulino 	seq_printf(seq,
546fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
547fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
548593f63b0SEric Dumazet 		   pkt_dev->clone_skb, pkt_dev->odevname);
5491da177e4SLinus Torvalds 
550222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
551222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5521da177e4SLinus Torvalds 
55345b270f8SRobert Olsson 	seq_printf(seq,
55445b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
55545b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
55645b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
55745b270f8SRobert Olsson 
5589e50e3acSJohn Fastabend 	if (pkt_dev->skb_priority)
5599e50e3acSJohn Fastabend 		seq_printf(seq, "     skb_priority: %u\n",
5609e50e3acSJohn Fastabend 			   pkt_dev->skb_priority);
5619e50e3acSJohn Fastabend 
5621da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
563222f1806SLuiz Capitulino 		seq_printf(seq,
56447a0200dSAlexey Dobriyan 			   "     saddr: %pI6c  min_saddr: %pI6c  max_saddr: %pI6c\n"
56547a0200dSAlexey Dobriyan 			   "     daddr: %pI6c  min_daddr: %pI6c  max_daddr: %pI6c\n",
56647a0200dSAlexey Dobriyan 			   &pkt_dev->in6_saddr,
56747a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
56847a0200dSAlexey Dobriyan 			   &pkt_dev->in6_daddr,
56947a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
57063adc6fbSStephen Hemminger 	} else {
571222f1806SLuiz Capitulino 		seq_printf(seq,
57263adc6fbSStephen Hemminger 			   "     dst_min: %s  dst_max: %s\n",
57363adc6fbSStephen Hemminger 			   pkt_dev->dst_min, pkt_dev->dst_max);
57463adc6fbSStephen Hemminger 		seq_printf(seq,
57563adc6fbSStephen Hemminger 			   "     src_min: %s  src_max: %s\n",
57663adc6fbSStephen Hemminger 			   pkt_dev->src_min, pkt_dev->src_max);
57763adc6fbSStephen Hemminger 	}
5781da177e4SLinus Torvalds 
579d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5801da177e4SLinus Torvalds 
581e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
582e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
583e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5841da177e4SLinus Torvalds 
58597dc48e2SThomas Graf 	seq_puts(seq, "dst_mac: ");
586e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
5871da177e4SLinus Torvalds 
588222f1806SLuiz Capitulino 	seq_printf(seq,
58963adc6fbSStephen Hemminger 		   "     udp_src_min: %d  udp_src_max: %d"
59063adc6fbSStephen Hemminger 		   "  udp_dst_min: %d  udp_dst_max: %d\n",
591222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
592222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
5931da177e4SLinus Torvalds 
594222f1806SLuiz Capitulino 	seq_printf(seq,
595ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
5961da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
5971da177e4SLinus Torvalds 
598ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
59995c96174SEric Dumazet 		unsigned int i;
60097dc48e2SThomas Graf 		seq_puts(seq, "     mpls: ");
601ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
602ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
603ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
604ca6549afSSteven Whitehouse 	}
605ca6549afSSteven Whitehouse 
60663adc6fbSStephen Hemminger 	if (pkt_dev->vlan_id != 0xffff)
60734954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
60863adc6fbSStephen Hemminger 			   pkt_dev->vlan_id, pkt_dev->vlan_p,
60963adc6fbSStephen Hemminger 			   pkt_dev->vlan_cfi);
61034954ddcSFrancesco Fondelli 
61163adc6fbSStephen Hemminger 	if (pkt_dev->svlan_id != 0xffff)
61234954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
61363adc6fbSStephen Hemminger 			   pkt_dev->svlan_id, pkt_dev->svlan_p,
61463adc6fbSStephen Hemminger 			   pkt_dev->svlan_cfi);
61534954ddcSFrancesco Fondelli 
61663adc6fbSStephen Hemminger 	if (pkt_dev->tos)
6171ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
6181ca7768cSFrancesco Fondelli 
61963adc6fbSStephen Hemminger 	if (pkt_dev->traffic_class)
6201ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
6211ca7768cSFrancesco Fondelli 
62238b2cf29SAlexei Starovoitov 	if (pkt_dev->burst > 1)
62338b2cf29SAlexei Starovoitov 		seq_printf(seq, "     burst: %d\n", pkt_dev->burst);
62438b2cf29SAlexei Starovoitov 
625e99b99b4SRobert Olsson 	if (pkt_dev->node >= 0)
626e99b99b4SRobert Olsson 		seq_printf(seq, "     node: %d\n", pkt_dev->node);
627e99b99b4SRobert Olsson 
62862f64aedSAlexei Starovoitov 	if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
62962f64aedSAlexei Starovoitov 		seq_puts(seq, "     xmit_mode: netif_receive\n");
6300967f244SJohn Fastabend 	else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
6310967f244SJohn Fastabend 		seq_puts(seq, "     xmit_mode: xmit_queue\n");
63262f64aedSAlexei Starovoitov 
63397dc48e2SThomas Graf 	seq_puts(seq, "     Flags: ");
634ca6549afSSteven Whitehouse 
6351da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
63697dc48e2SThomas Graf 		seq_puts(seq, "IPV6  ");
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPSRC_RND)
63997dc48e2SThomas Graf 		seq_puts(seq, "IPSRC_RND  ");
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPDST_RND)
64297dc48e2SThomas Graf 		seq_puts(seq, "IPDST_RND  ");
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	if (pkt_dev->flags & F_TXSIZE_RND)
64597dc48e2SThomas Graf 		seq_puts(seq, "TXSIZE_RND  ");
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPSRC_RND)
64897dc48e2SThomas Graf 		seq_puts(seq, "UDPSRC_RND  ");
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPDST_RND)
65197dc48e2SThomas Graf 		seq_puts(seq, "UDPDST_RND  ");
6521da177e4SLinus Torvalds 
653c26bf4a5SThomas Graf 	if (pkt_dev->flags & F_UDPCSUM)
65497dc48e2SThomas Graf 		seq_puts(seq, "UDPCSUM  ");
655c26bf4a5SThomas Graf 
656afb84b62SJesper Dangaard Brouer 	if (pkt_dev->flags & F_NO_TIMESTAMP)
657afb84b62SJesper Dangaard Brouer 		seq_puts(seq, "NO_TIMESTAMP  ");
658afb84b62SJesper Dangaard Brouer 
659ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND)
66097dc48e2SThomas Graf 		seq_puts(seq,  "MPLS_RND  ");
661ca6549afSSteven Whitehouse 
66245b270f8SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_RND)
66397dc48e2SThomas Graf 		seq_puts(seq,  "QUEUE_MAP_RND  ");
66445b270f8SRobert Olsson 
665e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
66697dc48e2SThomas Graf 		seq_puts(seq,  "QUEUE_MAP_CPU  ");
667e6fce5b9SRobert Olsson 
668007a531bSJamal Hadi Salim 	if (pkt_dev->cflows) {
669007a531bSJamal Hadi Salim 		if (pkt_dev->flags & F_FLOW_SEQ)
67097dc48e2SThomas Graf 			seq_puts(seq,  "FLOW_SEQ  "); /*in sequence flows*/
671007a531bSJamal Hadi Salim 		else
67297dc48e2SThomas Graf 			seq_puts(seq,  "FLOW_RND  ");
673007a531bSJamal Hadi Salim 	}
674007a531bSJamal Hadi Salim 
675a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
6768101328bSFan Du 	if (pkt_dev->flags & F_IPSEC_ON) {
67797dc48e2SThomas Graf 		seq_puts(seq,  "IPSEC  ");
6788101328bSFan Du 		if (pkt_dev->spi)
6798101328bSFan Du 			seq_printf(seq, "spi:%u", pkt_dev->spi);
6808101328bSFan Du 	}
681a553e4a6SJamal Hadi Salim #endif
682a553e4a6SJamal Hadi Salim 
6831da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACSRC_RND)
68497dc48e2SThomas Graf 		seq_puts(seq, "MACSRC_RND  ");
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACDST_RND)
68797dc48e2SThomas Graf 		seq_puts(seq, "MACDST_RND  ");
6881da177e4SLinus Torvalds 
68934954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_VID_RND)
69097dc48e2SThomas Graf 		seq_puts(seq, "VID_RND  ");
69134954ddcSFrancesco Fondelli 
69234954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_SVID_RND)
69397dc48e2SThomas Graf 		seq_puts(seq, "SVID_RND  ");
69434954ddcSFrancesco Fondelli 
695e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE)
69697dc48e2SThomas Graf 		seq_puts(seq, "NODE_ALLOC  ");
697e99b99b4SRobert Olsson 
698d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6991da177e4SLinus Torvalds 
700fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
701398f382cSDaniel Borkmann 	stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
702fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
703fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
7041da177e4SLinus Torvalds 
705222f1806SLuiz Capitulino 	seq_printf(seq,
706fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
7071da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
708fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
709fd29cf72SStephen Hemminger 
710fd29cf72SStephen Hemminger 	seq_printf(seq,
711fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
712fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
713fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
714fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
7151da177e4SLinus Torvalds 
716222f1806SLuiz Capitulino 	seq_printf(seq,
717222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
718d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
719d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
72247a0200dSAlexey Dobriyan 		seq_printf(seq, "     cur_saddr: %pI6c  cur_daddr: %pI6c\n",
72347a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_saddr,
72447a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_daddr);
725222f1806SLuiz Capitulino 	} else
7260373a946SAmerigo Wang 		seq_printf(seq, "     cur_saddr: %pI4  cur_daddr: %pI4\n",
7270373a946SAmerigo Wang 			   &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
7281da177e4SLinus Torvalds 
729d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
7301da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
7311da177e4SLinus Torvalds 
73245b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
73345b270f8SRobert Olsson 
734d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	if (pkt_dev->result[0])
737d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
7381da177e4SLinus Torvalds 	else
73997dc48e2SThomas Graf 		seq_puts(seq, "Result: Idle\n");
7401da177e4SLinus Torvalds 
741d50a6b56SStephen Hemminger 	return 0;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744ca6549afSSteven Whitehouse 
74563adc6fbSStephen Hemminger static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
74663adc6fbSStephen Hemminger 		     __u32 *num)
747ca6549afSSteven Whitehouse {
748ca6549afSSteven Whitehouse 	int i = 0;
749ca6549afSSteven Whitehouse 	*num = 0;
750ca6549afSSteven Whitehouse 
7511ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
75282fd5b5dSAndy Shevchenko 		int value;
753ca6549afSSteven Whitehouse 		char c;
754ca6549afSSteven Whitehouse 		*num <<= 4;
755ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
756ca6549afSSteven Whitehouse 			return -EFAULT;
75782fd5b5dSAndy Shevchenko 		value = hex_to_bin(c);
75882fd5b5dSAndy Shevchenko 		if (value >= 0)
75982fd5b5dSAndy Shevchenko 			*num |= value;
760ca6549afSSteven Whitehouse 		else
761ca6549afSSteven Whitehouse 			break;
762ca6549afSSteven Whitehouse 	}
763ca6549afSSteven Whitehouse 	return i;
764ca6549afSSteven Whitehouse }
765ca6549afSSteven Whitehouse 
766222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
767222f1806SLuiz Capitulino 			     unsigned int maxlen)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 	int i;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7721da177e4SLinus Torvalds 		char c;
7731da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7741da177e4SLinus Torvalds 			return -EFAULT;
7751da177e4SLinus Torvalds 		switch (c) {
7761da177e4SLinus Torvalds 		case '\"':
7771da177e4SLinus Torvalds 		case '\n':
7781da177e4SLinus Torvalds 		case '\r':
7791da177e4SLinus Torvalds 		case '\t':
7801da177e4SLinus Torvalds 		case ' ':
7811da177e4SLinus Torvalds 		case '=':
7821da177e4SLinus Torvalds 			break;
7831da177e4SLinus Torvalds 		default:
7841da177e4SLinus Torvalds 			goto done;
7853ff50b79SStephen Hemminger 		}
7861da177e4SLinus Torvalds 	}
7871da177e4SLinus Torvalds done:
7881da177e4SLinus Torvalds 	return i;
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
791bf0813bdSPaul Gortmaker static long num_arg(const char __user *user_buffer, unsigned long maxlen,
792bf0813bdSPaul Gortmaker 				unsigned long *num)
7931da177e4SLinus Torvalds {
794d6182223SPaul Gortmaker 	int i;
7951da177e4SLinus Torvalds 	*num = 0;
7961da177e4SLinus Torvalds 
797d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7981da177e4SLinus Torvalds 		char c;
7991da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
8001da177e4SLinus Torvalds 			return -EFAULT;
8011da177e4SLinus Torvalds 		if ((c >= '0') && (c <= '9')) {
8021da177e4SLinus Torvalds 			*num *= 10;
8031da177e4SLinus Torvalds 			*num += c - '0';
8041da177e4SLinus Torvalds 		} else
8051da177e4SLinus Torvalds 			break;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 	return i;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
8111da177e4SLinus Torvalds {
812d6182223SPaul Gortmaker 	int i;
8131da177e4SLinus Torvalds 
814d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
8151da177e4SLinus Torvalds 		char c;
8161da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
8171da177e4SLinus Torvalds 			return -EFAULT;
8181da177e4SLinus Torvalds 		switch (c) {
8191da177e4SLinus Torvalds 		case '\"':
8201da177e4SLinus Torvalds 		case '\n':
8211da177e4SLinus Torvalds 		case '\r':
8221da177e4SLinus Torvalds 		case '\t':
8231da177e4SLinus Torvalds 		case ' ':
8241da177e4SLinus Torvalds 			goto done_str;
8251da177e4SLinus Torvalds 		default:
8261da177e4SLinus Torvalds 			break;
8273ff50b79SStephen Hemminger 		}
8281da177e4SLinus Torvalds 	}
8291da177e4SLinus Torvalds done_str:
8301da177e4SLinus Torvalds 	return i;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
833ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
834ca6549afSSteven Whitehouse {
83595c96174SEric Dumazet 	unsigned int n = 0;
836ca6549afSSteven Whitehouse 	char c;
837ca6549afSSteven Whitehouse 	ssize_t i = 0;
838ca6549afSSteven Whitehouse 	int len;
839ca6549afSSteven Whitehouse 
840ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = 0;
841ca6549afSSteven Whitehouse 	do {
842ca6549afSSteven Whitehouse 		__u32 tmp;
8431ca7768cSFrancesco Fondelli 		len = hex32_arg(&buffer[i], 8, &tmp);
844ca6549afSSteven Whitehouse 		if (len <= 0)
845ca6549afSSteven Whitehouse 			return len;
846ca6549afSSteven Whitehouse 		pkt_dev->labels[n] = htonl(tmp);
847ca6549afSSteven Whitehouse 		if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
848ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
849ca6549afSSteven Whitehouse 		i += len;
850ca6549afSSteven Whitehouse 		if (get_user(c, &buffer[i]))
851ca6549afSSteven Whitehouse 			return -EFAULT;
852ca6549afSSteven Whitehouse 		i++;
853ca6549afSSteven Whitehouse 		n++;
854ca6549afSSteven Whitehouse 		if (n >= MAX_MPLS_LABELS)
855ca6549afSSteven Whitehouse 			return -E2BIG;
856ca6549afSSteven Whitehouse 	} while (c == ',');
857ca6549afSSteven Whitehouse 
858ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = n;
859ca6549afSSteven Whitehouse 	return i;
860ca6549afSSteven Whitehouse }
861ca6549afSSteven Whitehouse 
862222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file,
863222f1806SLuiz Capitulino 			       const char __user * user_buffer, size_t count,
864222f1806SLuiz Capitulino 			       loff_t * offset)
8651da177e4SLinus Torvalds {
8668a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
867d50a6b56SStephen Hemminger 	struct pktgen_dev *pkt_dev = seq->private;
868d6182223SPaul Gortmaker 	int i, max, len;
8691da177e4SLinus Torvalds 	char name[16], valstr[32];
8701da177e4SLinus Torvalds 	unsigned long value = 0;
8711da177e4SLinus Torvalds 	char *pg_result = NULL;
8721da177e4SLinus Torvalds 	int tmp = 0;
8731da177e4SLinus Torvalds 	char buf[128];
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	pg_result = &(pkt_dev->result[0]);
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	if (count < 1) {
878294a0b7fSJoe Perches 		pr_warn("wrong command format\n");
8791da177e4SLinus Torvalds 		return -EINVAL;
8801da177e4SLinus Torvalds 	}
8811da177e4SLinus Torvalds 
882d6182223SPaul Gortmaker 	max = count;
883d6182223SPaul Gortmaker 	tmp = count_trail_chars(user_buffer, max);
8841da177e4SLinus Torvalds 	if (tmp < 0) {
885294a0b7fSJoe Perches 		pr_warn("illegal format\n");
8861da177e4SLinus Torvalds 		return tmp;
8871da177e4SLinus Torvalds 	}
888d6182223SPaul Gortmaker 	i = tmp;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	/* Read variable name */
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
89363adc6fbSStephen Hemminger 	if (len < 0)
894222f1806SLuiz Capitulino 		return len;
89563adc6fbSStephen Hemminger 
8961da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
8971da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
8981da177e4SLinus Torvalds 		return -EFAULT;
8991da177e4SLinus Torvalds 	i += len;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	max = count - i;
9021da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
9031da177e4SLinus Torvalds 	if (len < 0)
9041da177e4SLinus Torvalds 		return len;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	i += len;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	if (debug) {
90986c2c0a8SDmitry Torokhov 		size_t copy = min_t(size_t, count, 1023);
910448d7b5dSNelson Elhage 		char tb[copy + 1];
911448d7b5dSNelson Elhage 		if (copy_from_user(tb, user_buffer, copy))
9121da177e4SLinus Torvalds 			return -EFAULT;
913448d7b5dSNelson Elhage 		tb[copy] = 0;
914f342cda7SJoe Perches 		pr_debug("%s,%lu  buffer -:%s:-\n",
915f342cda7SJoe Perches 			 name, (unsigned long)count, tb);
9161da177e4SLinus Torvalds 	}
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	if (!strcmp(name, "min_pkt_size")) {
9191da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
92063adc6fbSStephen Hemminger 		if (len < 0)
921222f1806SLuiz Capitulino 			return len;
92263adc6fbSStephen Hemminger 
9231da177e4SLinus Torvalds 		i += len;
9241da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9251da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9261da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9271da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9281da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9291da177e4SLinus Torvalds 		}
930222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: min_pkt_size=%u",
931222f1806SLuiz Capitulino 			pkt_dev->min_pkt_size);
9321da177e4SLinus Torvalds 		return count;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	if (!strcmp(name, "max_pkt_size")) {
9361da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
93763adc6fbSStephen Hemminger 		if (len < 0)
938222f1806SLuiz Capitulino 			return len;
93963adc6fbSStephen Hemminger 
9401da177e4SLinus Torvalds 		i += len;
9411da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9421da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9431da177e4SLinus Torvalds 		if (value != pkt_dev->max_pkt_size) {
9441da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9451da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9461da177e4SLinus Torvalds 		}
947222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: max_pkt_size=%u",
948222f1806SLuiz Capitulino 			pkt_dev->max_pkt_size);
9491da177e4SLinus Torvalds 		return count;
9501da177e4SLinus Torvalds 	}
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	/* Shortcut for min = max */
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	if (!strcmp(name, "pkt_size")) {
9551da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
95663adc6fbSStephen Hemminger 		if (len < 0)
957222f1806SLuiz Capitulino 			return len;
95863adc6fbSStephen Hemminger 
9591da177e4SLinus Torvalds 		i += len;
9601da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9611da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9621da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9631da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9641da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9651da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 		sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
9681da177e4SLinus Torvalds 		return count;
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	if (!strcmp(name, "debug")) {
9721da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
97363adc6fbSStephen Hemminger 		if (len < 0)
974222f1806SLuiz Capitulino 			return len;
97563adc6fbSStephen Hemminger 
9761da177e4SLinus Torvalds 		i += len;
9771da177e4SLinus Torvalds 		debug = value;
9781da177e4SLinus Torvalds 		sprintf(pg_result, "OK: debug=%u", debug);
9791da177e4SLinus Torvalds 		return count;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	if (!strcmp(name, "frags")) {
9831da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
98463adc6fbSStephen Hemminger 		if (len < 0)
985222f1806SLuiz Capitulino 			return len;
98663adc6fbSStephen Hemminger 
9871da177e4SLinus Torvalds 		i += len;
9881da177e4SLinus Torvalds 		pkt_dev->nfrags = value;
9891da177e4SLinus Torvalds 		sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
9901da177e4SLinus Torvalds 		return count;
9911da177e4SLinus Torvalds 	}
9921da177e4SLinus Torvalds 	if (!strcmp(name, "delay")) {
9931da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
99463adc6fbSStephen Hemminger 		if (len < 0)
995222f1806SLuiz Capitulino 			return len;
99663adc6fbSStephen Hemminger 
9971da177e4SLinus Torvalds 		i += len;
998fd29cf72SStephen Hemminger 		if (value == 0x7FFFFFFF)
999fd29cf72SStephen Hemminger 			pkt_dev->delay = ULLONG_MAX;
1000fd29cf72SStephen Hemminger 		else
10019240d715SEric Dumazet 			pkt_dev->delay = (u64)value;
1002fd29cf72SStephen Hemminger 
1003fd29cf72SStephen Hemminger 		sprintf(pg_result, "OK: delay=%llu",
1004fd29cf72SStephen Hemminger 			(unsigned long long) pkt_dev->delay);
10051da177e4SLinus Torvalds 		return count;
10061da177e4SLinus Torvalds 	}
100743d28b65SDaniel Turull 	if (!strcmp(name, "rate")) {
100843d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
100943d28b65SDaniel Turull 		if (len < 0)
101043d28b65SDaniel Turull 			return len;
101143d28b65SDaniel Turull 
101243d28b65SDaniel Turull 		i += len;
101343d28b65SDaniel Turull 		if (!value)
101443d28b65SDaniel Turull 			return len;
101543d28b65SDaniel Turull 		pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
101643d28b65SDaniel Turull 		if (debug)
1017f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
101843d28b65SDaniel Turull 
101943d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
102043d28b65SDaniel Turull 		return count;
102143d28b65SDaniel Turull 	}
102243d28b65SDaniel Turull 	if (!strcmp(name, "ratep")) {
102343d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
102443d28b65SDaniel Turull 		if (len < 0)
102543d28b65SDaniel Turull 			return len;
102643d28b65SDaniel Turull 
102743d28b65SDaniel Turull 		i += len;
102843d28b65SDaniel Turull 		if (!value)
102943d28b65SDaniel Turull 			return len;
103043d28b65SDaniel Turull 		pkt_dev->delay = NSEC_PER_SEC/value;
103143d28b65SDaniel Turull 		if (debug)
1032f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
103343d28b65SDaniel Turull 
103443d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
103543d28b65SDaniel Turull 		return count;
103643d28b65SDaniel Turull 	}
10371da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_min")) {
10381da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
103963adc6fbSStephen Hemminger 		if (len < 0)
1040222f1806SLuiz Capitulino 			return len;
104163adc6fbSStephen Hemminger 
10421da177e4SLinus Torvalds 		i += len;
10431da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_min) {
10441da177e4SLinus Torvalds 			pkt_dev->udp_src_min = value;
10451da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10461da177e4SLinus Torvalds 		}
10471da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
10481da177e4SLinus Torvalds 		return count;
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_min")) {
10511da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
105263adc6fbSStephen Hemminger 		if (len < 0)
1053222f1806SLuiz Capitulino 			return len;
105463adc6fbSStephen Hemminger 
10551da177e4SLinus Torvalds 		i += len;
10561da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_min) {
10571da177e4SLinus Torvalds 			pkt_dev->udp_dst_min = value;
10581da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10591da177e4SLinus Torvalds 		}
10601da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
10611da177e4SLinus Torvalds 		return count;
10621da177e4SLinus Torvalds 	}
10631da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_max")) {
10641da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
106563adc6fbSStephen Hemminger 		if (len < 0)
1066222f1806SLuiz Capitulino 			return len;
106763adc6fbSStephen Hemminger 
10681da177e4SLinus Torvalds 		i += len;
10691da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_max) {
10701da177e4SLinus Torvalds 			pkt_dev->udp_src_max = value;
10711da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10721da177e4SLinus Torvalds 		}
10731da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
10741da177e4SLinus Torvalds 		return count;
10751da177e4SLinus Torvalds 	}
10761da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_max")) {
10771da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
107863adc6fbSStephen Hemminger 		if (len < 0)
1079222f1806SLuiz Capitulino 			return len;
108063adc6fbSStephen Hemminger 
10811da177e4SLinus Torvalds 		i += len;
10821da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_max) {
10831da177e4SLinus Torvalds 			pkt_dev->udp_dst_max = value;
10841da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10851da177e4SLinus Torvalds 		}
10861da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
10871da177e4SLinus Torvalds 		return count;
10881da177e4SLinus Torvalds 	}
10891da177e4SLinus Torvalds 	if (!strcmp(name, "clone_skb")) {
10901da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
109163adc6fbSStephen Hemminger 		if (len < 0)
1092222f1806SLuiz Capitulino 			return len;
1093d8873315SNeil Horman 		if ((value > 0) &&
109462f64aedSAlexei Starovoitov 		    ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
109562f64aedSAlexei Starovoitov 		     !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1096d8873315SNeil Horman 			return -ENOTSUPP;
10971da177e4SLinus Torvalds 		i += len;
10981da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
11011da177e4SLinus Torvalds 		return count;
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
11041da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
110563adc6fbSStephen Hemminger 		if (len < 0)
1106222f1806SLuiz Capitulino 			return len;
110763adc6fbSStephen Hemminger 
11081da177e4SLinus Torvalds 		i += len;
11091da177e4SLinus Torvalds 		pkt_dev->count = value;
11101da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
11111da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
11121da177e4SLinus Torvalds 		return count;
11131da177e4SLinus Torvalds 	}
11141da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
11151da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
111663adc6fbSStephen Hemminger 		if (len < 0)
1117222f1806SLuiz Capitulino 			return len;
111863adc6fbSStephen Hemminger 
11191da177e4SLinus Torvalds 		i += len;
11201da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
11211da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
11221da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
11231da177e4SLinus Torvalds 		}
1124222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1125222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
11261da177e4SLinus Torvalds 		return count;
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
11291da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
113063adc6fbSStephen Hemminger 		if (len < 0)
1131222f1806SLuiz Capitulino 			return len;
113263adc6fbSStephen Hemminger 
11331da177e4SLinus Torvalds 		i += len;
11341da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
11351da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
11361da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
11371da177e4SLinus Torvalds 		}
1138222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1139222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
11401da177e4SLinus Torvalds 		return count;
11411da177e4SLinus Torvalds 	}
114238b2cf29SAlexei Starovoitov 	if (!strcmp(name, "burst")) {
114338b2cf29SAlexei Starovoitov 		len = num_arg(&user_buffer[i], 10, &value);
114438b2cf29SAlexei Starovoitov 		if (len < 0)
114538b2cf29SAlexei Starovoitov 			return len;
114638b2cf29SAlexei Starovoitov 
114738b2cf29SAlexei Starovoitov 		i += len;
11480967f244SJohn Fastabend 		if ((value > 1) &&
11490967f244SJohn Fastabend 		    ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
11500967f244SJohn Fastabend 		     ((pkt_dev->xmit_mode == M_START_XMIT) &&
11510967f244SJohn Fastabend 		     (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
115252d6c8c6SEric Dumazet 			return -ENOTSUPP;
115338b2cf29SAlexei Starovoitov 		pkt_dev->burst = value < 1 ? 1 : value;
115438b2cf29SAlexei Starovoitov 		sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
115538b2cf29SAlexei Starovoitov 		return count;
115638b2cf29SAlexei Starovoitov 	}
1157e99b99b4SRobert Olsson 	if (!strcmp(name, "node")) {
1158e99b99b4SRobert Olsson 		len = num_arg(&user_buffer[i], 10, &value);
1159e99b99b4SRobert Olsson 		if (len < 0)
1160e99b99b4SRobert Olsson 			return len;
1161e99b99b4SRobert Olsson 
1162e99b99b4SRobert Olsson 		i += len;
1163e99b99b4SRobert Olsson 
1164e99b99b4SRobert Olsson 		if (node_possible(value)) {
1165e99b99b4SRobert Olsson 			pkt_dev->node = value;
1166e99b99b4SRobert Olsson 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
116726ad7879SEric Dumazet 			if (pkt_dev->page) {
116826ad7879SEric Dumazet 				put_page(pkt_dev->page);
116926ad7879SEric Dumazet 				pkt_dev->page = NULL;
117026ad7879SEric Dumazet 			}
1171e99b99b4SRobert Olsson 		}
1172e99b99b4SRobert Olsson 		else
1173e99b99b4SRobert Olsson 			sprintf(pg_result, "ERROR: node not possible");
1174e99b99b4SRobert Olsson 		return count;
1175e99b99b4SRobert Olsson 	}
117662f64aedSAlexei Starovoitov 	if (!strcmp(name, "xmit_mode")) {
117762f64aedSAlexei Starovoitov 		char f[32];
117862f64aedSAlexei Starovoitov 
117962f64aedSAlexei Starovoitov 		memset(f, 0, 32);
118062f64aedSAlexei Starovoitov 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
118162f64aedSAlexei Starovoitov 		if (len < 0)
118262f64aedSAlexei Starovoitov 			return len;
118362f64aedSAlexei Starovoitov 
118462f64aedSAlexei Starovoitov 		if (copy_from_user(f, &user_buffer[i], len))
118562f64aedSAlexei Starovoitov 			return -EFAULT;
118662f64aedSAlexei Starovoitov 		i += len;
118762f64aedSAlexei Starovoitov 
118862f64aedSAlexei Starovoitov 		if (strcmp(f, "start_xmit") == 0) {
118962f64aedSAlexei Starovoitov 			pkt_dev->xmit_mode = M_START_XMIT;
119062f64aedSAlexei Starovoitov 		} else if (strcmp(f, "netif_receive") == 0) {
119162f64aedSAlexei Starovoitov 			/* clone_skb set earlier, not supported in this mode */
119262f64aedSAlexei Starovoitov 			if (pkt_dev->clone_skb > 0)
119362f64aedSAlexei Starovoitov 				return -ENOTSUPP;
119462f64aedSAlexei Starovoitov 
119562f64aedSAlexei Starovoitov 			pkt_dev->xmit_mode = M_NETIF_RECEIVE;
11969eea9222SAlexei Starovoitov 
11979eea9222SAlexei Starovoitov 			/* make sure new packet is allocated every time
11989eea9222SAlexei Starovoitov 			 * pktgen_xmit() is called
11999eea9222SAlexei Starovoitov 			 */
12009eea9222SAlexei Starovoitov 			pkt_dev->last_ok = 1;
12019eea9222SAlexei Starovoitov 
12029eea9222SAlexei Starovoitov 			/* override clone_skb if user passed default value
12039eea9222SAlexei Starovoitov 			 * at module loading time
12049eea9222SAlexei Starovoitov 			 */
12059eea9222SAlexei Starovoitov 			pkt_dev->clone_skb = 0;
12060967f244SJohn Fastabend 		} else if (strcmp(f, "queue_xmit") == 0) {
12070967f244SJohn Fastabend 			pkt_dev->xmit_mode = M_QUEUE_XMIT;
12080967f244SJohn Fastabend 			pkt_dev->last_ok = 1;
120962f64aedSAlexei Starovoitov 		} else {
121062f64aedSAlexei Starovoitov 			sprintf(pg_result,
121162f64aedSAlexei Starovoitov 				"xmit_mode -:%s:- unknown\nAvailable modes: %s",
121262f64aedSAlexei Starovoitov 				f, "start_xmit, netif_receive\n");
121362f64aedSAlexei Starovoitov 			return count;
121462f64aedSAlexei Starovoitov 		}
121562f64aedSAlexei Starovoitov 		sprintf(pg_result, "OK: xmit_mode=%s", f);
121662f64aedSAlexei Starovoitov 		return count;
121762f64aedSAlexei Starovoitov 	}
12181da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
12191da177e4SLinus Torvalds 		char f[32];
12201da177e4SLinus Torvalds 		memset(f, 0, 32);
12211da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
122263adc6fbSStephen Hemminger 		if (len < 0)
1223222f1806SLuiz Capitulino 			return len;
122463adc6fbSStephen Hemminger 
12251da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
12261da177e4SLinus Torvalds 			return -EFAULT;
12271da177e4SLinus Torvalds 		i += len;
12281da177e4SLinus Torvalds 		if (strcmp(f, "IPSRC_RND") == 0)
12291da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPSRC_RND;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 		else if (strcmp(f, "!IPSRC_RND") == 0)
12321da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPSRC_RND;
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 		else if (strcmp(f, "TXSIZE_RND") == 0)
12351da177e4SLinus Torvalds 			pkt_dev->flags |= F_TXSIZE_RND;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 		else if (strcmp(f, "!TXSIZE_RND") == 0)
12381da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_TXSIZE_RND;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 		else if (strcmp(f, "IPDST_RND") == 0)
12411da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPDST_RND;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 		else if (strcmp(f, "!IPDST_RND") == 0)
12441da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPDST_RND;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 		else if (strcmp(f, "UDPSRC_RND") == 0)
12471da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPSRC_RND;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPSRC_RND") == 0)
12501da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPSRC_RND;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 		else if (strcmp(f, "UDPDST_RND") == 0)
12531da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPDST_RND;
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPDST_RND") == 0)
12561da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPDST_RND;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 		else if (strcmp(f, "MACSRC_RND") == 0)
12591da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACSRC_RND;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 		else if (strcmp(f, "!MACSRC_RND") == 0)
12621da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACSRC_RND;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 		else if (strcmp(f, "MACDST_RND") == 0)
12651da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACDST_RND;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 		else if (strcmp(f, "!MACDST_RND") == 0)
12681da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACDST_RND;
12691da177e4SLinus Torvalds 
1270ca6549afSSteven Whitehouse 		else if (strcmp(f, "MPLS_RND") == 0)
1271ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
1272ca6549afSSteven Whitehouse 
1273ca6549afSSteven Whitehouse 		else if (strcmp(f, "!MPLS_RND") == 0)
1274ca6549afSSteven Whitehouse 			pkt_dev->flags &= ~F_MPLS_RND;
1275ca6549afSSteven Whitehouse 
127634954ddcSFrancesco Fondelli 		else if (strcmp(f, "VID_RND") == 0)
127734954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_VID_RND;
127834954ddcSFrancesco Fondelli 
127934954ddcSFrancesco Fondelli 		else if (strcmp(f, "!VID_RND") == 0)
128034954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_VID_RND;
128134954ddcSFrancesco Fondelli 
128234954ddcSFrancesco Fondelli 		else if (strcmp(f, "SVID_RND") == 0)
128334954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_SVID_RND;
128434954ddcSFrancesco Fondelli 
128534954ddcSFrancesco Fondelli 		else if (strcmp(f, "!SVID_RND") == 0)
128634954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_SVID_RND;
128734954ddcSFrancesco Fondelli 
1288007a531bSJamal Hadi Salim 		else if (strcmp(f, "FLOW_SEQ") == 0)
1289007a531bSJamal Hadi Salim 			pkt_dev->flags |= F_FLOW_SEQ;
1290007a531bSJamal Hadi Salim 
129145b270f8SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
129245b270f8SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_RND;
129345b270f8SRobert Olsson 
129445b270f8SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
129545b270f8SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1296e6fce5b9SRobert Olsson 
1297e6fce5b9SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1298e6fce5b9SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1299e6fce5b9SRobert Olsson 
1300e6fce5b9SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1301e6fce5b9SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1302a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
1303a553e4a6SJamal Hadi Salim 		else if (strcmp(f, "IPSEC") == 0)
1304a553e4a6SJamal Hadi Salim 			pkt_dev->flags |= F_IPSEC_ON;
1305a553e4a6SJamal Hadi Salim #endif
1306a553e4a6SJamal Hadi Salim 
13071ca7768cSFrancesco Fondelli 		else if (strcmp(f, "!IPV6") == 0)
13081ca7768cSFrancesco Fondelli 			pkt_dev->flags &= ~F_IPV6;
13091ca7768cSFrancesco Fondelli 
1310e99b99b4SRobert Olsson 		else if (strcmp(f, "NODE_ALLOC") == 0)
1311e99b99b4SRobert Olsson 			pkt_dev->flags |= F_NODE;
1312e99b99b4SRobert Olsson 
1313e99b99b4SRobert Olsson 		else if (strcmp(f, "!NODE_ALLOC") == 0)
1314e99b99b4SRobert Olsson 			pkt_dev->flags &= ~F_NODE;
1315e99b99b4SRobert Olsson 
1316c26bf4a5SThomas Graf 		else if (strcmp(f, "UDPCSUM") == 0)
1317c26bf4a5SThomas Graf 			pkt_dev->flags |= F_UDPCSUM;
1318c26bf4a5SThomas Graf 
1319c26bf4a5SThomas Graf 		else if (strcmp(f, "!UDPCSUM") == 0)
1320c26bf4a5SThomas Graf 			pkt_dev->flags &= ~F_UDPCSUM;
1321c26bf4a5SThomas Graf 
1322afb84b62SJesper Dangaard Brouer 		else if (strcmp(f, "NO_TIMESTAMP") == 0)
1323afb84b62SJesper Dangaard Brouer 			pkt_dev->flags |= F_NO_TIMESTAMP;
1324afb84b62SJesper Dangaard Brouer 
1325f1f00d8fSJesper Dangaard Brouer 		else if (strcmp(f, "!NO_TIMESTAMP") == 0)
1326f1f00d8fSJesper Dangaard Brouer 			pkt_dev->flags &= ~F_NO_TIMESTAMP;
1327f1f00d8fSJesper Dangaard Brouer 
13281da177e4SLinus Torvalds 		else {
1329222f1806SLuiz Capitulino 			sprintf(pg_result,
1330222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
13311da177e4SLinus Torvalds 				f,
13321ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
133372f8e06fSMathias Krause 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
133472f8e06fSMathias Krause 				"MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
133572f8e06fSMathias Krause 				"QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
1336afb84b62SJesper Dangaard Brouer 				"NO_TIMESTAMP, "
133772f8e06fSMathias Krause #ifdef CONFIG_XFRM
133872f8e06fSMathias Krause 				"IPSEC, "
133972f8e06fSMathias Krause #endif
134072f8e06fSMathias Krause 				"NODE_ALLOC\n");
13411da177e4SLinus Torvalds 			return count;
13421da177e4SLinus Torvalds 		}
13431da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
13441da177e4SLinus Torvalds 		return count;
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
13471da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
134863adc6fbSStephen Hemminger 		if (len < 0)
1349222f1806SLuiz Capitulino 			return len;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13521da177e4SLinus Torvalds 			return -EFAULT;
13531da177e4SLinus Torvalds 		buf[len] = 0;
13541da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
13551da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
13561da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_min, buf, len);
13571da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
13581da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
13591da177e4SLinus Torvalds 		}
13601da177e4SLinus Torvalds 		if (debug)
1361f342cda7SJoe Perches 			pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
13621da177e4SLinus Torvalds 		i += len;
13631da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
13641da177e4SLinus Torvalds 		return count;
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
13671da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
136863adc6fbSStephen Hemminger 		if (len < 0)
1369222f1806SLuiz Capitulino 			return len;
137063adc6fbSStephen Hemminger 
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13731da177e4SLinus Torvalds 			return -EFAULT;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 		buf[len] = 0;
13761da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
13771da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
13781da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_max, buf, len);
13791da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
13801da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
13811da177e4SLinus Torvalds 		}
13821da177e4SLinus Torvalds 		if (debug)
1383f342cda7SJoe Perches 			pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
13841da177e4SLinus Torvalds 		i += len;
13851da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
13861da177e4SLinus Torvalds 		return count;
13871da177e4SLinus Torvalds 	}
13881da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
13891da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1390222f1806SLuiz Capitulino 		if (len < 0)
1391222f1806SLuiz Capitulino 			return len;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13961da177e4SLinus Torvalds 			return -EFAULT;
13971da177e4SLinus Torvalds 		buf[len] = 0;
13981da177e4SLinus Torvalds 
1399c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
140047a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
14011da177e4SLinus Torvalds 
14024e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 		if (debug)
1405f342cda7SJoe Perches 			pr_debug("dst6 set to: %s\n", buf);
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 		i += len;
14081da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
14091da177e4SLinus Torvalds 		return count;
14101da177e4SLinus Torvalds 	}
14111da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
14121da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1413222f1806SLuiz Capitulino 		if (len < 0)
1414222f1806SLuiz Capitulino 			return len;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14191da177e4SLinus Torvalds 			return -EFAULT;
14201da177e4SLinus Torvalds 		buf[len] = 0;
14211da177e4SLinus Torvalds 
1422c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
142347a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
14241da177e4SLinus Torvalds 
14254e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
14261da177e4SLinus Torvalds 		if (debug)
1427f342cda7SJoe Perches 			pr_debug("dst6_min set to: %s\n", buf);
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 		i += len;
14301da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
14311da177e4SLinus Torvalds 		return count;
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
14341da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1435222f1806SLuiz Capitulino 		if (len < 0)
1436222f1806SLuiz Capitulino 			return len;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14411da177e4SLinus Torvalds 			return -EFAULT;
14421da177e4SLinus Torvalds 		buf[len] = 0;
14431da177e4SLinus Torvalds 
1444c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
144547a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 		if (debug)
1448f342cda7SJoe Perches 			pr_debug("dst6_max set to: %s\n", buf);
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 		i += len;
14511da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
14521da177e4SLinus Torvalds 		return count;
14531da177e4SLinus Torvalds 	}
14541da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
14551da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1456222f1806SLuiz Capitulino 		if (len < 0)
1457222f1806SLuiz Capitulino 			return len;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14621da177e4SLinus Torvalds 			return -EFAULT;
14631da177e4SLinus Torvalds 		buf[len] = 0;
14641da177e4SLinus Torvalds 
1465c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
146647a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
14671da177e4SLinus Torvalds 
14684e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 		if (debug)
1471f342cda7SJoe Perches 			pr_debug("src6 set to: %s\n", buf);
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 		i += len;
14741da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
14751da177e4SLinus Torvalds 		return count;
14761da177e4SLinus Torvalds 	}
14771da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
14781da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
147963adc6fbSStephen Hemminger 		if (len < 0)
1480222f1806SLuiz Capitulino 			return len;
148163adc6fbSStephen Hemminger 
14821da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14831da177e4SLinus Torvalds 			return -EFAULT;
14841da177e4SLinus Torvalds 		buf[len] = 0;
14851da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
14861da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
14871da177e4SLinus Torvalds 			strncpy(pkt_dev->src_min, buf, len);
14881da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
14891da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
14901da177e4SLinus Torvalds 		}
14911da177e4SLinus Torvalds 		if (debug)
1492f342cda7SJoe Perches 			pr_debug("src_min set to: %s\n", pkt_dev->src_min);
14931da177e4SLinus Torvalds 		i += len;
14941da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
14951da177e4SLinus Torvalds 		return count;
14961da177e4SLinus Torvalds 	}
14971da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
14981da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
149963adc6fbSStephen Hemminger 		if (len < 0)
1500222f1806SLuiz Capitulino 			return len;
150163adc6fbSStephen Hemminger 
15021da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
15031da177e4SLinus Torvalds 			return -EFAULT;
15041da177e4SLinus Torvalds 		buf[len] = 0;
15051da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
15061da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
15071da177e4SLinus Torvalds 			strncpy(pkt_dev->src_max, buf, len);
15081da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
15091da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
15101da177e4SLinus Torvalds 		}
15111da177e4SLinus Torvalds 		if (debug)
1512f342cda7SJoe Perches 			pr_debug("src_max set to: %s\n", pkt_dev->src_max);
15131da177e4SLinus Torvalds 		i += len;
15141da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
15151da177e4SLinus Torvalds 		return count;
15161da177e4SLinus Torvalds 	}
15171da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
15181da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
151963adc6fbSStephen Hemminger 		if (len < 0)
1520222f1806SLuiz Capitulino 			return len;
152163adc6fbSStephen Hemminger 
15221da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
15231da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
15241da177e4SLinus Torvalds 			return -EFAULT;
15251da177e4SLinus Torvalds 
15264940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->dst_mac))
15274940fc88SAlexey Dobriyan 			return -EINVAL;
15281da177e4SLinus Torvalds 		/* Set up Dest MAC */
15299ea08b12SJoe Perches 		ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
15301da177e4SLinus Torvalds 
15314940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
15321da177e4SLinus Torvalds 		return count;
15331da177e4SLinus Torvalds 	}
15341da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
15351da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
153663adc6fbSStephen Hemminger 		if (len < 0)
1537222f1806SLuiz Capitulino 			return len;
153863adc6fbSStephen Hemminger 
15391da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
15401da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
15411da177e4SLinus Torvalds 			return -EFAULT;
15421da177e4SLinus Torvalds 
15434940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->src_mac))
15444940fc88SAlexey Dobriyan 			return -EINVAL;
1545ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
15469ea08b12SJoe Perches 		ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
1547ce5d0b47SAdit Ranadive 
15484940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
15491da177e4SLinus Torvalds 		return count;
15501da177e4SLinus Torvalds 	}
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
15531da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
15541da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
15551da177e4SLinus Torvalds 		return count;
15561da177e4SLinus Torvalds 	}
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
15591da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
156063adc6fbSStephen Hemminger 		if (len < 0)
1561222f1806SLuiz Capitulino 			return len;
156263adc6fbSStephen Hemminger 
15631da177e4SLinus Torvalds 		i += len;
15641da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
15651da177e4SLinus Torvalds 			value = MAX_CFLOWS;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 		pkt_dev->cflows = value;
15681da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
15691da177e4SLinus Torvalds 		return count;
15701da177e4SLinus Torvalds 	}
15716bae9190SFan Du #ifdef CONFIG_XFRM
1572de4aee7dSFan Du 	if (!strcmp(name, "spi")) {
1573de4aee7dSFan Du 		len = num_arg(&user_buffer[i], 10, &value);
1574de4aee7dSFan Du 		if (len < 0)
1575de4aee7dSFan Du 			return len;
1576de4aee7dSFan Du 
1577de4aee7dSFan Du 		i += len;
1578de4aee7dSFan Du 		pkt_dev->spi = value;
1579de4aee7dSFan Du 		sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1580de4aee7dSFan Du 		return count;
1581de4aee7dSFan Du 	}
15826bae9190SFan Du #endif
15831da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
15841da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
158563adc6fbSStephen Hemminger 		if (len < 0)
1586222f1806SLuiz Capitulino 			return len;
158763adc6fbSStephen Hemminger 
15881da177e4SLinus Torvalds 		i += len;
15891da177e4SLinus Torvalds 		pkt_dev->lflow = value;
15901da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
15911da177e4SLinus Torvalds 		return count;
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 
159445b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
159545b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
159663adc6fbSStephen Hemminger 		if (len < 0)
159745b270f8SRobert Olsson 			return len;
159863adc6fbSStephen Hemminger 
159945b270f8SRobert Olsson 		i += len;
160045b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
160145b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
160245b270f8SRobert Olsson 		return count;
160345b270f8SRobert Olsson 	}
160445b270f8SRobert Olsson 
160545b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
160645b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
160763adc6fbSStephen Hemminger 		if (len < 0)
160845b270f8SRobert Olsson 			return len;
160963adc6fbSStephen Hemminger 
161045b270f8SRobert Olsson 		i += len;
161145b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
161245b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
161345b270f8SRobert Olsson 		return count;
161445b270f8SRobert Olsson 	}
161545b270f8SRobert Olsson 
1616ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
161795c96174SEric Dumazet 		unsigned int n, cnt;
1618cfcabdccSStephen Hemminger 
1619ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1620cfcabdccSStephen Hemminger 		if (len < 0)
1621cfcabdccSStephen Hemminger 			return len;
1622ca6549afSSteven Whitehouse 		i += len;
1623cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1624ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1625cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1626ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1627ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
162834954ddcSFrancesco Fondelli 
162934954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
163034954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
163134954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
163234954ddcSFrancesco Fondelli 
163334954ddcSFrancesco Fondelli 			if (debug)
1634f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN auto turned off\n");
163534954ddcSFrancesco Fondelli 		}
163634954ddcSFrancesco Fondelli 		return count;
163734954ddcSFrancesco Fondelli 	}
163834954ddcSFrancesco Fondelli 
163934954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
164034954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
164163adc6fbSStephen Hemminger 		if (len < 0)
164234954ddcSFrancesco Fondelli 			return len;
164363adc6fbSStephen Hemminger 
164434954ddcSFrancesco Fondelli 		i += len;
164534954ddcSFrancesco Fondelli 		if (value <= 4095) {
164634954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
164734954ddcSFrancesco Fondelli 
164834954ddcSFrancesco Fondelli 			if (debug)
1649f342cda7SJoe Perches 				pr_debug("VLAN turned on\n");
165034954ddcSFrancesco Fondelli 
165134954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1652f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
165334954ddcSFrancesco Fondelli 
165434954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
165534954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
165634954ddcSFrancesco Fondelli 		} else {
165734954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
165834954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
165934954ddcSFrancesco Fondelli 
166034954ddcSFrancesco Fondelli 			if (debug)
1661f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
166234954ddcSFrancesco Fondelli 		}
166334954ddcSFrancesco Fondelli 		return count;
166434954ddcSFrancesco Fondelli 	}
166534954ddcSFrancesco Fondelli 
166634954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
166734954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
166863adc6fbSStephen Hemminger 		if (len < 0)
166934954ddcSFrancesco Fondelli 			return len;
167063adc6fbSStephen Hemminger 
167134954ddcSFrancesco Fondelli 		i += len;
167234954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
167334954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
167434954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
167534954ddcSFrancesco Fondelli 		} else {
167634954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
167734954ddcSFrancesco Fondelli 		}
167834954ddcSFrancesco Fondelli 		return count;
167934954ddcSFrancesco Fondelli 	}
168034954ddcSFrancesco Fondelli 
168134954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
168234954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
168363adc6fbSStephen Hemminger 		if (len < 0)
168434954ddcSFrancesco Fondelli 			return len;
168563adc6fbSStephen Hemminger 
168634954ddcSFrancesco Fondelli 		i += len;
168734954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
168834954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
168934954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
169034954ddcSFrancesco Fondelli 		} else {
169134954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
169234954ddcSFrancesco Fondelli 		}
169334954ddcSFrancesco Fondelli 		return count;
169434954ddcSFrancesco Fondelli 	}
169534954ddcSFrancesco Fondelli 
169634954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
169734954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
169863adc6fbSStephen Hemminger 		if (len < 0)
169934954ddcSFrancesco Fondelli 			return len;
170063adc6fbSStephen Hemminger 
170134954ddcSFrancesco Fondelli 		i += len;
170234954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
170334954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
170434954ddcSFrancesco Fondelli 
170534954ddcSFrancesco Fondelli 			if (debug)
1706f342cda7SJoe Perches 				pr_debug("SVLAN turned on\n");
170734954ddcSFrancesco Fondelli 
170834954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1709f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
171034954ddcSFrancesco Fondelli 
171134954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
171234954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
171334954ddcSFrancesco Fondelli 		} else {
171434954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
171534954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
171634954ddcSFrancesco Fondelli 
171734954ddcSFrancesco Fondelli 			if (debug)
1718f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
171934954ddcSFrancesco Fondelli 		}
172034954ddcSFrancesco Fondelli 		return count;
172134954ddcSFrancesco Fondelli 	}
172234954ddcSFrancesco Fondelli 
172334954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
172434954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
172563adc6fbSStephen Hemminger 		if (len < 0)
172634954ddcSFrancesco Fondelli 			return len;
172763adc6fbSStephen Hemminger 
172834954ddcSFrancesco Fondelli 		i += len;
172934954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
173034954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
173134954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
173234954ddcSFrancesco Fondelli 		} else {
173334954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
173434954ddcSFrancesco Fondelli 		}
173534954ddcSFrancesco Fondelli 		return count;
173634954ddcSFrancesco Fondelli 	}
173734954ddcSFrancesco Fondelli 
173834954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
173934954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
174063adc6fbSStephen Hemminger 		if (len < 0)
174134954ddcSFrancesco Fondelli 			return len;
174263adc6fbSStephen Hemminger 
174334954ddcSFrancesco Fondelli 		i += len;
174434954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
174534954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
174634954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
174734954ddcSFrancesco Fondelli 		} else {
174834954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
174934954ddcSFrancesco Fondelli 		}
1750ca6549afSSteven Whitehouse 		return count;
1751ca6549afSSteven Whitehouse 	}
1752ca6549afSSteven Whitehouse 
17531ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
17541ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
17551ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
175663adc6fbSStephen Hemminger 		if (len < 0)
17571ca7768cSFrancesco Fondelli 			return len;
175863adc6fbSStephen Hemminger 
17591ca7768cSFrancesco Fondelli 		i += len;
17601ca7768cSFrancesco Fondelli 		if (len == 2) {
17611ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
17621ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
17631ca7768cSFrancesco Fondelli 		} else {
17641ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
17651ca7768cSFrancesco Fondelli 		}
17661ca7768cSFrancesco Fondelli 		return count;
17671ca7768cSFrancesco Fondelli 	}
17681ca7768cSFrancesco Fondelli 
17691ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
17701ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
17711ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
177263adc6fbSStephen Hemminger 		if (len < 0)
17731ca7768cSFrancesco Fondelli 			return len;
177463adc6fbSStephen Hemminger 
17751ca7768cSFrancesco Fondelli 		i += len;
17761ca7768cSFrancesco Fondelli 		if (len == 2) {
17771ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
17781ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
17791ca7768cSFrancesco Fondelli 		} else {
17801ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
17811ca7768cSFrancesco Fondelli 		}
17821ca7768cSFrancesco Fondelli 		return count;
17831ca7768cSFrancesco Fondelli 	}
17841ca7768cSFrancesco Fondelli 
17859e50e3acSJohn Fastabend 	if (!strcmp(name, "skb_priority")) {
17869e50e3acSJohn Fastabend 		len = num_arg(&user_buffer[i], 9, &value);
17879e50e3acSJohn Fastabend 		if (len < 0)
17889e50e3acSJohn Fastabend 			return len;
17899e50e3acSJohn Fastabend 
17909e50e3acSJohn Fastabend 		i += len;
17919e50e3acSJohn Fastabend 		pkt_dev->skb_priority = value;
17929e50e3acSJohn Fastabend 		sprintf(pg_result, "OK: skb_priority=%i",
17939e50e3acSJohn Fastabend 			pkt_dev->skb_priority);
17949e50e3acSJohn Fastabend 		return count;
17959e50e3acSJohn Fastabend 	}
17969e50e3acSJohn Fastabend 
17971da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
17981da177e4SLinus Torvalds 	return -EINVAL;
17991da177e4SLinus Torvalds }
18001da177e4SLinus Torvalds 
1801d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
18021da177e4SLinus Torvalds {
1803d9dda78bSAl Viro 	return single_open(file, pktgen_if_show, PDE_DATA(inode));
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
18069a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1807d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1808d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1809d50a6b56SStephen Hemminger 	.read    = seq_read,
1810d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1811d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1812d50a6b56SStephen Hemminger 	.release = single_release,
1813d50a6b56SStephen Hemminger };
1814d50a6b56SStephen Hemminger 
1815d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1816d50a6b56SStephen Hemminger {
1817d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1818648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1819d50a6b56SStephen Hemminger 
1820d50a6b56SStephen Hemminger 	BUG_ON(!t);
1821d50a6b56SStephen Hemminger 
182297dc48e2SThomas Graf 	seq_puts(seq, "Running: ");
18231da177e4SLinus Torvalds 
18248788370aSJesper Dangaard Brouer 	rcu_read_lock();
18258788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
18261da177e4SLinus Torvalds 		if (pkt_dev->running)
1827593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
18281da177e4SLinus Torvalds 
182997dc48e2SThomas Graf 	seq_puts(seq, "\nStopped: ");
18301da177e4SLinus Torvalds 
18318788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
18321da177e4SLinus Torvalds 		if (!pkt_dev->running)
1833593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	if (t->result[0])
1836d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
18371da177e4SLinus Torvalds 	else
183897dc48e2SThomas Graf 		seq_puts(seq, "\nResult: NA\n");
18391da177e4SLinus Torvalds 
18408788370aSJesper Dangaard Brouer 	rcu_read_unlock();
18411da177e4SLinus Torvalds 
1842d50a6b56SStephen Hemminger 	return 0;
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
1845d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1846d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1847d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
18481da177e4SLinus Torvalds {
18498a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
1850d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1851d6182223SPaul Gortmaker 	int i, max, len, ret;
18521da177e4SLinus Torvalds 	char name[40];
18531da177e4SLinus Torvalds 	char *pg_result;
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 	if (count < 1) {
18561da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
18571da177e4SLinus Torvalds 		return -EINVAL;
18581da177e4SLinus Torvalds 	}
18591da177e4SLinus Torvalds 
1860d6182223SPaul Gortmaker 	max = count;
1861d6182223SPaul Gortmaker 	len = count_trail_chars(user_buffer, max);
18621da177e4SLinus Torvalds 	if (len < 0)
18631da177e4SLinus Torvalds 		return len;
18641da177e4SLinus Torvalds 
1865d6182223SPaul Gortmaker 	i = len;
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	/* Read variable name */
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
18701da177e4SLinus Torvalds 	if (len < 0)
18711da177e4SLinus Torvalds 		return len;
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
18741da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
18751da177e4SLinus Torvalds 		return -EFAULT;
18761da177e4SLinus Torvalds 	i += len;
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds 	max = count - i;
18791da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
18801da177e4SLinus Torvalds 	if (len < 0)
18811da177e4SLinus Torvalds 		return len;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	i += len;
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 	if (debug)
1886f342cda7SJoe Perches 		pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 	if (!t) {
1889f9467eaeSJoe Perches 		pr_err("ERROR: No thread\n");
18901da177e4SLinus Torvalds 		ret = -EINVAL;
18911da177e4SLinus Torvalds 		goto out;
18921da177e4SLinus Torvalds 	}
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
18971da177e4SLinus Torvalds 		char f[32];
18981da177e4SLinus Torvalds 		memset(f, 0, 32);
18991da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
19001da177e4SLinus Torvalds 		if (len < 0) {
19011da177e4SLinus Torvalds 			ret = len;
19021da177e4SLinus Torvalds 			goto out;
19031da177e4SLinus Torvalds 		}
19041da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
19051da177e4SLinus Torvalds 			return -EFAULT;
19061da177e4SLinus Torvalds 		i += len;
19076146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
1908604dfd6eSCong Wang 		ret = pktgen_add_device(t, f);
19096146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1910604dfd6eSCong Wang 		if (!ret) {
19111da177e4SLinus Torvalds 			ret = count;
19121da177e4SLinus Torvalds 			sprintf(pg_result, "OK: add_device=%s", f);
1913604dfd6eSCong Wang 		} else
1914604dfd6eSCong Wang 			sprintf(pg_result, "ERROR: can not add device %s", f);
19151da177e4SLinus Torvalds 		goto out;
19161da177e4SLinus Torvalds 	}
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
19196146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
192095ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
19216146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1922121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
19231da177e4SLinus Torvalds 		ret = count;
19241da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
19251da177e4SLinus Torvalds 		goto out;
19261da177e4SLinus Torvalds 	}
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1929b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
19301da177e4SLinus Torvalds 		ret = count;
19311da177e4SLinus Torvalds 		goto out;
19321da177e4SLinus Torvalds 	}
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 	ret = -EINVAL;
19351da177e4SLinus Torvalds out:
19361da177e4SLinus Torvalds 	return ret;
19371da177e4SLinus Torvalds }
19381da177e4SLinus Torvalds 
1939d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
19401da177e4SLinus Torvalds {
1941d9dda78bSAl Viro 	return single_open(file, pktgen_thread_show, PDE_DATA(inode));
19421da177e4SLinus Torvalds }
19431da177e4SLinus Torvalds 
19449a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1945d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1946d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1947d50a6b56SStephen Hemminger 	.read    = seq_read,
1948d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1949d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1950d50a6b56SStephen Hemminger 	.release = single_release,
1951d50a6b56SStephen Hemminger };
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds /* Think find or remove for NN */
19544e58a027SCong Wang static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
19554e58a027SCong Wang 					      const char *ifname, int remove)
19561da177e4SLinus Torvalds {
19571da177e4SLinus Torvalds 	struct pktgen_thread *t;
19581da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
19593e984840SEric Dumazet 	bool exact = (remove == FIND);
19601da177e4SLinus Torvalds 
19614e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
19623e984840SEric Dumazet 		pkt_dev = pktgen_find_dev(t, ifname, exact);
19631da177e4SLinus Torvalds 		if (pkt_dev) {
19641da177e4SLinus Torvalds 			if (remove) {
196595ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
196695ed63f7SArthur Kepner 				t->control |= T_REMDEV;
19671da177e4SLinus Torvalds 			}
19681da177e4SLinus Torvalds 			break;
19691da177e4SLinus Torvalds 		}
19701da177e4SLinus Torvalds 	}
19711da177e4SLinus Torvalds 	return pkt_dev;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
197495ed63f7SArthur Kepner /*
197595ed63f7SArthur Kepner  * mark a device for removal
197695ed63f7SArthur Kepner  */
19774e58a027SCong Wang static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
19781da177e4SLinus Torvalds {
19791da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
198095ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
198195ed63f7SArthur Kepner 	int i = 0;
198295ed63f7SArthur Kepner 
19836146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
1984f9467eaeSJoe Perches 	pr_debug("%s: marking %s for removal\n", __func__, ifname);
198595ed63f7SArthur Kepner 
198695ed63f7SArthur Kepner 	while (1) {
198795ed63f7SArthur Kepner 
19884e58a027SCong Wang 		pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1989222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1990222f1806SLuiz Capitulino 			break;	/* success */
199195ed63f7SArthur Kepner 
19926146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1993f9467eaeSJoe Perches 		pr_debug("%s: waiting for %s to disappear....\n",
1994f9467eaeSJoe Perches 			 __func__, ifname);
199595ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
19966146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
199795ed63f7SArthur Kepner 
199895ed63f7SArthur Kepner 		if (++i >= max_tries) {
1999f9467eaeSJoe Perches 			pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
2000f9467eaeSJoe Perches 			       __func__, msec_per_try * i, ifname);
200195ed63f7SArthur Kepner 			break;
200295ed63f7SArthur Kepner 		}
200395ed63f7SArthur Kepner 
200495ed63f7SArthur Kepner 	}
200595ed63f7SArthur Kepner 
20066146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
200739df232fSStephen Hemminger }
200895ed63f7SArthur Kepner 
20094e58a027SCong Wang static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
201039df232fSStephen Hemminger {
201139df232fSStephen Hemminger 	struct pktgen_thread *t;
201239df232fSStephen Hemminger 
20139a0b1e8bSEric Dumazet 	mutex_lock(&pktgen_thread_lock);
20149a0b1e8bSEric Dumazet 
20154e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
201639df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
201739df232fSStephen Hemminger 
20189a0b1e8bSEric Dumazet 		if_lock(t);
20199a0b1e8bSEric Dumazet 		list_for_each_entry(pkt_dev, &t->if_list, list) {
202039df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
202139df232fSStephen Hemminger 				continue;
202239df232fSStephen Hemminger 
2023a8ca16eaSDavid Howells 			proc_remove(pkt_dev->entry);
202439df232fSStephen Hemminger 
20252975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
20264e58a027SCong Wang 							  pn->proc_dir,
20272975315bSAlexey Dobriyan 							  &pktgen_if_fops,
20282975315bSAlexey Dobriyan 							  pkt_dev);
202939df232fSStephen Hemminger 			if (!pkt_dev->entry)
2030f9467eaeSJoe Perches 				pr_err("can't move proc entry for '%s'\n",
2031f9467eaeSJoe Perches 				       dev->name);
203239df232fSStephen Hemminger 			break;
203339df232fSStephen Hemminger 		}
20349a0b1e8bSEric Dumazet 		if_unlock(t);
203539df232fSStephen Hemminger 	}
20369a0b1e8bSEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
2039222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
2040222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
20411da177e4SLinus Torvalds {
2042351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
20434e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
20441da177e4SLinus Torvalds 
20454e58a027SCong Wang 	if (pn->pktgen_exiting)
2046e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
2047e9dc8653SEric W. Biederman 
20481da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
20491da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
20501da177e4SLinus Torvalds 	 */
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	switch (event) {
205339df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
20544e58a027SCong Wang 		pktgen_change_name(pn, dev);
20551da177e4SLinus Torvalds 		break;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
20584e58a027SCong Wang 		pktgen_mark_device(pn, dev->name);
20591da177e4SLinus Torvalds 		break;
20603ff50b79SStephen Hemminger 	}
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	return NOTIFY_DONE;
20631da177e4SLinus Torvalds }
20641da177e4SLinus Torvalds 
20654e58a027SCong Wang static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
20664e58a027SCong Wang 						 struct pktgen_dev *pkt_dev,
206763adc6fbSStephen Hemminger 						 const char *ifname)
2068e6fce5b9SRobert Olsson {
2069e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
2070d6182223SPaul Gortmaker 	int i;
2071e6fce5b9SRobert Olsson 
2072e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
2073e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
2074e6fce5b9SRobert Olsson 			break;
2075e6fce5b9SRobert Olsson 
2076e6fce5b9SRobert Olsson 		b[i] = ifname[i];
2077e6fce5b9SRobert Olsson 	}
2078e6fce5b9SRobert Olsson 	b[i] = 0;
2079e6fce5b9SRobert Olsson 
20804e58a027SCong Wang 	return dev_get_by_name(pn->net, b);
2081e6fce5b9SRobert Olsson }
2082e6fce5b9SRobert Olsson 
2083e6fce5b9SRobert Olsson 
20841da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
20851da177e4SLinus Torvalds 
20864e58a027SCong Wang static int pktgen_setup_dev(const struct pktgen_net *pn,
20874e58a027SCong Wang 			    struct pktgen_dev *pkt_dev, const char *ifname)
2088222f1806SLuiz Capitulino {
20891da177e4SLinus Torvalds 	struct net_device *odev;
209039df232fSStephen Hemminger 	int err;
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	/* Clean old setups */
20931da177e4SLinus Torvalds 	if (pkt_dev->odev) {
20941da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
20951da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
20961da177e4SLinus Torvalds 	}
20971da177e4SLinus Torvalds 
20984e58a027SCong Wang 	odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
20991da177e4SLinus Torvalds 	if (!odev) {
2100f9467eaeSJoe Perches 		pr_err("no such netdevice: \"%s\"\n", ifname);
210139df232fSStephen Hemminger 		return -ENODEV;
21021da177e4SLinus Torvalds 	}
210339df232fSStephen Hemminger 
21041da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
2105f9467eaeSJoe Perches 		pr_err("not an ethernet device: \"%s\"\n", ifname);
210639df232fSStephen Hemminger 		err = -EINVAL;
210739df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
2108f9467eaeSJoe Perches 		pr_err("device is down: \"%s\"\n", ifname);
210939df232fSStephen Hemminger 		err = -ENETDOWN;
211039df232fSStephen Hemminger 	} else {
21111da177e4SLinus Torvalds 		pkt_dev->odev = odev;
211239df232fSStephen Hemminger 		return 0;
211339df232fSStephen Hemminger 	}
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	dev_put(odev);
211639df232fSStephen Hemminger 	return err;
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
21201da177e4SLinus Torvalds  * structure to have the right information to create/send packets
21211da177e4SLinus Torvalds  */
21221da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
21231da177e4SLinus Torvalds {
212464c00d81SAndrew Gallatin 	int ntxq;
212564c00d81SAndrew Gallatin 
21261da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
2127f9467eaeSJoe Perches 		pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2128222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
2129222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
21301da177e4SLinus Torvalds 		return;
21311da177e4SLinus Torvalds 	}
21321da177e4SLinus Torvalds 
213364c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
213464c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2135bfdbc0acSRobert Olsson 
213664c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
2137294a0b7fSJoe Perches 		pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
213888271660SJesse Brandeburg 			pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2139593f63b0SEric Dumazet 			pkt_dev->odevname);
214026e29eedSDan Carpenter 		pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
214164c00d81SAndrew Gallatin 	}
214288271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
2143294a0b7fSJoe Perches 		pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
214488271660SJesse Brandeburg 			pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2145593f63b0SEric Dumazet 			pkt_dev->odevname);
214626e29eedSDan Carpenter 		pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
214764c00d81SAndrew Gallatin 	}
214864c00d81SAndrew Gallatin 
21491da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
21501da177e4SLinus Torvalds 
2151f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
21529ea08b12SJoe Perches 		ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	/* Set up Dest MAC */
21559ea08b12SJoe Perches 	ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
21584c139b8cSAmerigo Wang 		int i, set = 0, err = 1;
21594c139b8cSAmerigo Wang 		struct inet6_dev *idev;
21604c139b8cSAmerigo Wang 
216168bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
216268bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
216368bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
216468bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
216568bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
216668bf9f0bSAmerigo Wang 		}
216768bf9f0bSAmerigo Wang 
21681da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
21691da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
21701da177e4SLinus Torvalds 				set = 1;
21711da177e4SLinus Torvalds 				break;
21721da177e4SLinus Torvalds 			}
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds 		if (!set) {
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 			/*
21771da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
21781da177e4SLinus Torvalds 			 *
21791da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
21801da177e4SLinus Torvalds 			 */
21811da177e4SLinus Torvalds 
21828814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
218363adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
218463adc6fbSStephen Hemminger 			if (idev) {
21851da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
21884c139b8cSAmerigo Wang 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
21894c139b8cSAmerigo Wang 					if ((ifp->scope & IFA_LINK) &&
2190f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
21914e3fd7a0SAlexey Dobriyan 						pkt_dev->cur_in6_saddr = ifp->addr;
21921da177e4SLinus Torvalds 						err = 0;
21931da177e4SLinus Torvalds 						break;
21941da177e4SLinus Torvalds 					}
21951da177e4SLinus Torvalds 				}
21961da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
21971da177e4SLinus Torvalds 			}
21988814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2199222f1806SLuiz Capitulino 			if (err)
2200f9467eaeSJoe Perches 				pr_err("ERROR: IPv6 link address not available\n");
22011da177e4SLinus Torvalds 		}
2202222f1806SLuiz Capitulino 	} else {
220368bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
220468bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
220568bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
220668bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
220768bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
220868bf9f0bSAmerigo Wang 		}
220968bf9f0bSAmerigo Wang 
22101da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
22111da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
22121da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds 			struct in_device *in_dev;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 			rcu_read_lock();
2217e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
22181da177e4SLinus Torvalds 			if (in_dev) {
22191da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2220222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2221222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
22221da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
22231da177e4SLinus Torvalds 				}
22241da177e4SLinus Torvalds 			}
22251da177e4SLinus Torvalds 			rcu_read_unlock();
2226222f1806SLuiz Capitulino 		} else {
22271da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
22281da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
22291da177e4SLinus Torvalds 		}
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
22321da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
22331da177e4SLinus Torvalds 	}
22341da177e4SLinus Torvalds 	/* Initialize current values. */
223568bf9f0bSAmerigo Wang 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
223668bf9f0bSAmerigo Wang 	if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
223768bf9f0bSAmerigo Wang 		pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
223868bf9f0bSAmerigo Wang 
22391da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
22401da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
22411da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
22421da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
22431da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
22441da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
22451da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
22461da177e4SLinus Torvalds }
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds 
2249fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2250fd29cf72SStephen Hemminger {
2251ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2252417bc4b8SEric Dumazet 	s64 remaining;
22532bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2254fd29cf72SStephen Hemminger 
22552bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
22562bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2257fd29cf72SStephen Hemminger 
225843d28b65SDaniel Turull 	remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2259bcf91bdbSGuenter Roeck 	if (remaining <= 0)
2260bcf91bdbSGuenter Roeck 		goto out;
22612bc481cfSStephen Hemminger 
2262398f382cSDaniel Borkmann 	start_time = ktime_get();
226333136d12SEric Dumazet 	if (remaining < 100000) {
226433136d12SEric Dumazet 		/* for small delays (<100us), just loop until limit is reached */
226533136d12SEric Dumazet 		do {
2266398f382cSDaniel Borkmann 			end_time = ktime_get();
2267398f382cSDaniel Borkmann 		} while (ktime_compare(end_time, spin_until) < 0);
226833136d12SEric Dumazet 	} else {
22692bc481cfSStephen Hemminger 		/* see do_nanosleep */
22702bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
22712bc481cfSStephen Hemminger 		do {
22722bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
22732bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
22742bc481cfSStephen Hemminger 
22752bc481cfSStephen Hemminger 			if (likely(t.task))
22761da177e4SLinus Torvalds 				schedule();
22771da177e4SLinus Torvalds 
22782bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
22792bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
22802bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
2281398f382cSDaniel Borkmann 		end_time = ktime_get();
228233136d12SEric Dumazet 	}
2283ef87979cSStephen Hemminger 
2284ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2285bcf91bdbSGuenter Roeck out:
228607a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2287bcf91bdbSGuenter Roeck 	destroy_hrtimer_on_stack(&t.timer);
22881da177e4SLinus Torvalds }
22891da177e4SLinus Torvalds 
229016dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
229116dab72fSJamal Hadi Salim {
229263d75463SPaolo Abeni 	pkt_dev->pkt_overhead = 0;
229316dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
229416dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
229516dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
229616dab72fSJamal Hadi Salim }
229716dab72fSJamal Hadi Salim 
2298648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2299007a531bSJamal Hadi Salim {
2300648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2301007a531bSJamal Hadi Salim }
2302007a531bSJamal Hadi Salim 
2303007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2304007a531bSJamal Hadi Salim {
2305007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2306007a531bSJamal Hadi Salim 
2307007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2308007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2309007a531bSJamal Hadi Salim 			/* reset time */
2310007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
23111211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2312007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2313007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2314007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2315007a531bSJamal Hadi Salim 		}
2316007a531bSJamal Hadi Salim 	} else {
231733d7c5e5SAkinobu Mita 		flow = prandom_u32() % pkt_dev->cflows;
23181211a645SRobert Olsson 		pkt_dev->curfl = flow;
2319007a531bSJamal Hadi Salim 
23201211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2321007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
23221211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
23231211a645SRobert Olsson 		}
2324007a531bSJamal Hadi Salim 	}
2325007a531bSJamal Hadi Salim 
2326007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2327007a531bSJamal Hadi Salim }
2328007a531bSJamal Hadi Salim 
2329a553e4a6SJamal Hadi Salim 
2330a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2331a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2332a553e4a6SJamal Hadi Salim  * we go look for it ...
2333a553e4a6SJamal Hadi Salim */
2334bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2335fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2336a553e4a6SJamal Hadi Salim {
2337a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
23384e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2339a553e4a6SJamal Hadi Salim 	if (!x) {
2340c454997eSFan Du 
2341c454997eSFan Du 		if (pkt_dev->spi) {
2342c454997eSFan Du 			/* We need as quick as possible to find the right SA
2343c454997eSFan Du 			 * Searching with minimum criteria to archieve this.
2344c454997eSFan Du 			 */
2345c454997eSFan Du 			x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2346c454997eSFan Du 		} else {
2347a553e4a6SJamal Hadi Salim 			/* slow path: we dont already have xfrm_state */
23484e58a027SCong Wang 			x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
23495447c5e4SAlexey Dobriyan 						(xfrm_address_t *)&pkt_dev->cur_daddr,
2350a553e4a6SJamal Hadi Salim 						(xfrm_address_t *)&pkt_dev->cur_saddr,
2351a553e4a6SJamal Hadi Salim 						AF_INET,
2352a553e4a6SJamal Hadi Salim 						pkt_dev->ipsmode,
2353a553e4a6SJamal Hadi Salim 						pkt_dev->ipsproto, 0);
2354c454997eSFan Du 		}
2355a553e4a6SJamal Hadi Salim 		if (x) {
2356a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2357a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2358a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2359a553e4a6SJamal Hadi Salim 		}
2360a553e4a6SJamal Hadi Salim 
2361a553e4a6SJamal Hadi Salim 	}
2362a553e4a6SJamal Hadi Salim }
2363a553e4a6SJamal Hadi Salim #endif
2364fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2365fd2ea0a7SDavid S. Miller {
2366e6fce5b9SRobert Olsson 
2367e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2368e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2369e6fce5b9SRobert Olsson 
2370896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2371fd2ea0a7SDavid S. Miller 		__u16 t;
2372fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
237333d7c5e5SAkinobu Mita 			t = prandom_u32() %
2374fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2375fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2376fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2377fd2ea0a7SDavid S. Miller 		} else {
2378fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2379fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2380fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2381fd2ea0a7SDavid S. Miller 		}
2382fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2383fd2ea0a7SDavid S. Miller 	}
2384bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2385fd2ea0a7SDavid S. Miller }
2386fd2ea0a7SDavid S. Miller 
23871da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
23881da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
23891da177e4SLinus Torvalds  */
2390222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2391222f1806SLuiz Capitulino {
23921da177e4SLinus Torvalds 	__u32 imn;
23931da177e4SLinus Torvalds 	__u32 imx;
23941da177e4SLinus Torvalds 	int flow = 0;
23951da177e4SLinus Torvalds 
2396007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2397007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds 	/*  Deal with source MAC */
24001da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
24011da177e4SLinus Torvalds 		__u32 mc;
24021da177e4SLinus Torvalds 		__u32 tmp;
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
240533d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->src_mac_count;
24061da177e4SLinus Torvalds 		else {
24071da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2408ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2409222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
24101da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
24111da177e4SLinus Torvalds 		}
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
24141da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
24151da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
24161da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
24171da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
24181da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
24191da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
24201da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
24211da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
24221da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
24231da177e4SLinus Torvalds 	}
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
24261da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
24271da177e4SLinus Torvalds 		__u32 mc;
24281da177e4SLinus Torvalds 		__u32 tmp;
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
243133d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->dst_mac_count;
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 		else {
24341da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2435ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2436222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
24371da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
24381da177e4SLinus Torvalds 			}
24391da177e4SLinus Torvalds 		}
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
24421da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
24431da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
24441da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
24451da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
24461da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
24471da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
24481da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
24491da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
24501da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
24511da177e4SLinus Torvalds 	}
24521da177e4SLinus Torvalds 
2453ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
245495c96174SEric Dumazet 		unsigned int i;
2455ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2456ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2457ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
245833d7c5e5SAkinobu Mita 					     ((__force __be32)prandom_u32() &
2459ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2460ca6549afSSteven Whitehouse 	}
2461ca6549afSSteven Whitehouse 
246234954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
246333d7c5e5SAkinobu Mita 		pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
246434954ddcSFrancesco Fondelli 	}
246534954ddcSFrancesco Fondelli 
246634954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
246733d7c5e5SAkinobu Mita 		pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
246834954ddcSFrancesco Fondelli 	}
246934954ddcSFrancesco Fondelli 
24701da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
24711da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
247233d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_src = prandom_u32() %
24735fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
24745fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds 		else {
24771da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
24781da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
24791da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
24801da177e4SLinus Torvalds 		}
24811da177e4SLinus Torvalds 	}
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
24841da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
248533d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_dst = prandom_u32() %
24865fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
24875fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2488222f1806SLuiz Capitulino 		} else {
24891da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
24901da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
24911da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
24921da177e4SLinus Torvalds 		}
24931da177e4SLinus Torvalds 	}
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
24961da177e4SLinus Torvalds 
249763adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
249863adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
249963adc6fbSStephen Hemminger 		if (imn < imx) {
25001da177e4SLinus Torvalds 			__u32 t;
25011da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
250233d7c5e5SAkinobu Mita 				t = prandom_u32() % (imx - imn) + imn;
25031da177e4SLinus Torvalds 			else {
25041da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
25051da177e4SLinus Torvalds 				t++;
250663adc6fbSStephen Hemminger 				if (t > imx)
25071da177e4SLinus Torvalds 					t = imn;
250863adc6fbSStephen Hemminger 
25091da177e4SLinus Torvalds 			}
25101da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
25111da177e4SLinus Torvalds 		}
25121da177e4SLinus Torvalds 
2513007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
25141da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
25151da177e4SLinus Torvalds 		} else {
2516252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2517252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2518252e3346SAl Viro 			if (imn < imx) {
25191da177e4SLinus Torvalds 				__u32 t;
2520252e3346SAl Viro 				__be32 s;
25211da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
25221da177e4SLinus Torvalds 
252370e3ba72SAkinobu Mita 					do {
252433d7c5e5SAkinobu Mita 						t = prandom_u32() %
252533d7c5e5SAkinobu Mita 							(imx - imn) + imn;
2526252e3346SAl Viro 						s = htonl(t);
252770e3ba72SAkinobu Mita 					} while (ipv4_is_loopback(s) ||
252870e3ba72SAkinobu Mita 						ipv4_is_multicast(s) ||
252970e3ba72SAkinobu Mita 						ipv4_is_lbcast(s) ||
253070e3ba72SAkinobu Mita 						ipv4_is_zeronet(s) ||
253170e3ba72SAkinobu Mita 						ipv4_is_local_multicast(s));
2532252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2533252e3346SAl Viro 				} else {
25341da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
25351da177e4SLinus Torvalds 					t++;
25361da177e4SLinus Torvalds 					if (t > imx) {
25371da177e4SLinus Torvalds 						t = imn;
25381da177e4SLinus Torvalds 					}
25391da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
25401da177e4SLinus Torvalds 				}
25411da177e4SLinus Torvalds 			}
25421da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2543007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2544222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2545222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2546a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2547a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2548a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2549a553e4a6SJamal Hadi Salim #endif
25501da177e4SLinus Torvalds 				pkt_dev->nflows++;
25511da177e4SLinus Torvalds 			}
25521da177e4SLinus Torvalds 		}
2553222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2554222f1806SLuiz Capitulino 
255506e30411SJoe Perches 		if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
25561da177e4SLinus Torvalds 			int i;
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 			/* Only random destinations yet */
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
25611da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
256233d7c5e5SAkinobu Mita 				    (((__force __be32)prandom_u32() |
25631da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
25641da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
25651da177e4SLinus Torvalds 			}
25661da177e4SLinus Torvalds 		}
25671da177e4SLinus Torvalds 	}
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
25701da177e4SLinus Torvalds 		__u32 t;
25711da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
257233d7c5e5SAkinobu Mita 			t = prandom_u32() %
25735fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
25745fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2575222f1806SLuiz Capitulino 		} else {
25761da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
25771da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
25781da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
25791da177e4SLinus Torvalds 		}
25801da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
25811da177e4SLinus Torvalds 	}
25821da177e4SLinus Torvalds 
2583fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
258445b270f8SRobert Olsson 
25851da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
25861da177e4SLinus Torvalds }
25871da177e4SLinus Torvalds 
2588a553e4a6SJamal Hadi Salim 
2589a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
25905537a055SFengguang Wu static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2591cf93d47eSFan Du 
2592cf93d47eSFan Du 	[RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2593cf93d47eSFan Du };
2594cf93d47eSFan Du 
2595a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2596a553e4a6SJamal Hadi Salim {
2597a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2598a553e4a6SJamal Hadi Salim 	int err = 0;
25996de9ace4SFan Du 	struct net *net = dev_net(pkt_dev->odev);
2600a553e4a6SJamal Hadi Salim 
2601a553e4a6SJamal Hadi Salim 	if (!x)
2602a553e4a6SJamal Hadi Salim 		return 0;
2603a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2604a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2605cf93d47eSFan Du 	if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2606a553e4a6SJamal Hadi Salim 		return 0;
2607a553e4a6SJamal Hadi Salim 
2608cf93d47eSFan Du 	/* But when user specify an valid SPI, transformation
2609cf93d47eSFan Du 	 * supports both transport/tunnel mode + ESP/AH type.
2610cf93d47eSFan Du 	 */
2611cf93d47eSFan Du 	if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2612cf93d47eSFan Du 		skb->_skb_refdst = (unsigned long)&pkt_dev->dst | SKB_DST_NOREF;
2613cf93d47eSFan Du 
2614cf93d47eSFan Du 	rcu_read_lock_bh();
261513996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2616cf93d47eSFan Du 	rcu_read_unlock_bh();
26176de9ace4SFan Du 	if (err) {
26186de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2619a553e4a6SJamal Hadi Salim 		goto error;
26206de9ace4SFan Du 	}
2621a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
26226de9ace4SFan Du 	if (err) {
26236de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2624a553e4a6SJamal Hadi Salim 		goto error;
26256de9ace4SFan Du 	}
26260af0a413SFan Du 	spin_lock_bh(&x->lock);
2627a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2628a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
26290af0a413SFan Du 	spin_unlock_bh(&x->lock);
2630a553e4a6SJamal Hadi Salim error:
2631a553e4a6SJamal Hadi Salim 	return err;
2632a553e4a6SJamal Hadi Salim }
2633a553e4a6SJamal Hadi Salim 
2634475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2635a553e4a6SJamal Hadi Salim {
2636a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2637a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2638d6182223SPaul Gortmaker 		int i;
2639d6182223SPaul Gortmaker 		for (i = 0; i < pkt_dev->cflows; i++) {
2640a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2641a553e4a6SJamal Hadi Salim 			if (x) {
2642a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2643a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2644a553e4a6SJamal Hadi Salim 			}
2645a553e4a6SJamal Hadi Salim 		}
2646a553e4a6SJamal Hadi Salim 	}
2647a553e4a6SJamal Hadi Salim }
2648a553e4a6SJamal Hadi Salim 
2649475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2650a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2651a553e4a6SJamal Hadi Salim {
2652a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2653a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2654a553e4a6SJamal Hadi Salim 		int nhead = 0;
2655a553e4a6SJamal Hadi Salim 		if (x) {
2656d4969581SEric Dumazet 			struct ethhdr *eth;
26573868204dSfan.du 			struct iphdr *iph;
2658d4969581SEric Dumazet 			int ret;
26593868204dSfan.du 
2660a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2661a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2662a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2663a553e4a6SJamal Hadi Salim 				if (ret < 0) {
2664f9467eaeSJoe Perches 					pr_err("Error expanding ipsec packet %d\n",
2665f9467eaeSJoe Perches 					       ret);
2666b4bb4ac8SIlpo Järvinen 					goto err;
2667a553e4a6SJamal Hadi Salim 				}
2668a553e4a6SJamal Hadi Salim 			}
2669a553e4a6SJamal Hadi Salim 
2670a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2671a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2672a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2673a553e4a6SJamal Hadi Salim 			if (ret) {
2674f9467eaeSJoe Perches 				pr_err("Error creating ipsec packet %d\n", ret);
2675b4bb4ac8SIlpo Järvinen 				goto err;
2676a553e4a6SJamal Hadi Salim 			}
2677a553e4a6SJamal Hadi Salim 			/* restore ll */
2678d4969581SEric Dumazet 			eth = (struct ethhdr *)skb_push(skb, ETH_HLEN);
2679d4969581SEric Dumazet 			memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2680d4969581SEric Dumazet 			eth->h_proto = protocol;
26813868204dSfan.du 
26823868204dSfan.du 			/* Update IPv4 header len as well as checksum value */
26833868204dSfan.du 			iph = ip_hdr(skb);
26843868204dSfan.du 			iph->tot_len = htons(skb->len - ETH_HLEN);
26853868204dSfan.du 			ip_send_check(iph);
2686a553e4a6SJamal Hadi Salim 		}
2687a553e4a6SJamal Hadi Salim 	}
2688a553e4a6SJamal Hadi Salim 	return 1;
2689b4bb4ac8SIlpo Järvinen err:
2690b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2691b4bb4ac8SIlpo Järvinen 	return 0;
2692a553e4a6SJamal Hadi Salim }
2693a553e4a6SJamal Hadi Salim #endif
2694a553e4a6SJamal Hadi Salim 
2695ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2696ca6549afSSteven Whitehouse {
269795c96174SEric Dumazet 	unsigned int i;
269863adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2699ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
270063adc6fbSStephen Hemminger 
2701ca6549afSSteven Whitehouse 	mpls--;
2702ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2703ca6549afSSteven Whitehouse }
2704ca6549afSSteven Whitehouse 
27050f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
27060f37c605SAl Viro 			       unsigned int prio)
27070f37c605SAl Viro {
27080f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
27090f37c605SAl Viro }
27100f37c605SAl Viro 
271126ad7879SEric Dumazet static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
271226ad7879SEric Dumazet 				int datalen)
271326ad7879SEric Dumazet {
271426ad7879SEric Dumazet 	struct timeval timestamp;
271526ad7879SEric Dumazet 	struct pktgen_hdr *pgh;
271626ad7879SEric Dumazet 
271726ad7879SEric Dumazet 	pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
271826ad7879SEric Dumazet 	datalen -= sizeof(*pgh);
271926ad7879SEric Dumazet 
272026ad7879SEric Dumazet 	if (pkt_dev->nfrags <= 0) {
272105aebe2eSDaniel Turull 		memset(skb_put(skb, datalen), 0, datalen);
272226ad7879SEric Dumazet 	} else {
272326ad7879SEric Dumazet 		int frags = pkt_dev->nfrags;
272426ad7879SEric Dumazet 		int i, len;
27257d36a991Samit salecha 		int frag_len;
272626ad7879SEric Dumazet 
272726ad7879SEric Dumazet 
272826ad7879SEric Dumazet 		if (frags > MAX_SKB_FRAGS)
272926ad7879SEric Dumazet 			frags = MAX_SKB_FRAGS;
273026ad7879SEric Dumazet 		len = datalen - frags * PAGE_SIZE;
273126ad7879SEric Dumazet 		if (len > 0) {
273226ad7879SEric Dumazet 			memset(skb_put(skb, len), 0, len);
273326ad7879SEric Dumazet 			datalen = frags * PAGE_SIZE;
273426ad7879SEric Dumazet 		}
273526ad7879SEric Dumazet 
273626ad7879SEric Dumazet 		i = 0;
27377d36a991Samit salecha 		frag_len = (datalen/frags) < PAGE_SIZE ?
27387d36a991Samit salecha 			   (datalen/frags) : PAGE_SIZE;
273926ad7879SEric Dumazet 		while (datalen > 0) {
274026ad7879SEric Dumazet 			if (unlikely(!pkt_dev->page)) {
274126ad7879SEric Dumazet 				int node = numa_node_id();
274226ad7879SEric Dumazet 
274326ad7879SEric Dumazet 				if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
274426ad7879SEric Dumazet 					node = pkt_dev->node;
274526ad7879SEric Dumazet 				pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
274626ad7879SEric Dumazet 				if (!pkt_dev->page)
274726ad7879SEric Dumazet 					break;
274826ad7879SEric Dumazet 			}
2749a0bec1cdSIan Campbell 			get_page(pkt_dev->page);
2750ea2ab693SIan Campbell 			skb_frag_set_page(skb, i, pkt_dev->page);
275126ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page_offset = 0;
27527d36a991Samit salecha 			/*last fragment, fill rest of data*/
27537d36a991Samit salecha 			if (i == (frags - 1))
27549e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i],
27559e903e08SEric Dumazet 				    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
27567d36a991Samit salecha 			else
27579e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
27589e903e08SEric Dumazet 			datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
27599e903e08SEric Dumazet 			skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
27609e903e08SEric Dumazet 			skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
276126ad7879SEric Dumazet 			i++;
276226ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
276326ad7879SEric Dumazet 		}
276426ad7879SEric Dumazet 	}
276526ad7879SEric Dumazet 
276626ad7879SEric Dumazet 	/* Stamp the time, and sequence number,
276726ad7879SEric Dumazet 	 * convert them to network byte order
276826ad7879SEric Dumazet 	 */
276926ad7879SEric Dumazet 	pgh->pgh_magic = htonl(PKTGEN_MAGIC);
277026ad7879SEric Dumazet 	pgh->seq_num = htonl(pkt_dev->seq_num);
277126ad7879SEric Dumazet 
2772afb84b62SJesper Dangaard Brouer 	if (pkt_dev->flags & F_NO_TIMESTAMP) {
2773afb84b62SJesper Dangaard Brouer 		pgh->tv_sec = 0;
2774afb84b62SJesper Dangaard Brouer 		pgh->tv_usec = 0;
2775afb84b62SJesper Dangaard Brouer 	} else {
277626ad7879SEric Dumazet 		do_gettimeofday(&timestamp);
277726ad7879SEric Dumazet 		pgh->tv_sec = htonl(timestamp.tv_sec);
277826ad7879SEric Dumazet 		pgh->tv_usec = htonl(timestamp.tv_usec);
277926ad7879SEric Dumazet 	}
2780afb84b62SJesper Dangaard Brouer }
278126ad7879SEric Dumazet 
27827a6e288dSDaniel Borkmann static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
278363d75463SPaolo Abeni 					struct pktgen_dev *pkt_dev)
27847a6e288dSDaniel Borkmann {
278563d75463SPaolo Abeni 	unsigned int extralen = LL_RESERVED_SPACE(dev);
27867a6e288dSDaniel Borkmann 	struct sk_buff *skb = NULL;
278763d75463SPaolo Abeni 	unsigned int size;
27887a6e288dSDaniel Borkmann 
278963d75463SPaolo Abeni 	size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
27907a6e288dSDaniel Borkmann 	if (pkt_dev->flags & F_NODE) {
27917a6e288dSDaniel Borkmann 		int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
27927a6e288dSDaniel Borkmann 
27937a6e288dSDaniel Borkmann 		skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
27947a6e288dSDaniel Borkmann 		if (likely(skb)) {
27957a6e288dSDaniel Borkmann 			skb_reserve(skb, NET_SKB_PAD);
27967a6e288dSDaniel Borkmann 			skb->dev = dev;
27977a6e288dSDaniel Borkmann 		}
27987a6e288dSDaniel Borkmann 	} else {
27997a6e288dSDaniel Borkmann 		 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
28007a6e288dSDaniel Borkmann 	}
28013de03596SJohn Fastabend 
280263d75463SPaolo Abeni 	/* the caller pre-fetches from skb->data and reserves for the mac hdr */
28033de03596SJohn Fastabend 	if (likely(skb))
280463d75463SPaolo Abeni 		skb_reserve(skb, extralen - 16);
28057a6e288dSDaniel Borkmann 
28067a6e288dSDaniel Borkmann 	return skb;
28077a6e288dSDaniel Borkmann }
28087a6e288dSDaniel Borkmann 
28091da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
28101da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
28111da177e4SLinus Torvalds {
28121da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
28131da177e4SLinus Torvalds 	__u8 *eth;
28141da177e4SLinus Torvalds 	struct udphdr *udph;
28151da177e4SLinus Torvalds 	int datalen, iplen;
28161da177e4SLinus Torvalds 	struct iphdr *iph;
2817d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2818ca6549afSSteven Whitehouse 	__be32 *mpls;
281934954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
282034954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
282134954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
282234954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2823fd2ea0a7SDavid S. Miller 	u16 queue_map;
2824ca6549afSSteven Whitehouse 
2825ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2826d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
28271da177e4SLinus Torvalds 
282834954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2829d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
283034954ddcSFrancesco Fondelli 
283164053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
283264053beeSRobert Olsson 	 * fields.
283364053beeSRobert Olsson 	 */
283464053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2835eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
283664053beeSRobert Olsson 
283763d75463SPaolo Abeni 	skb = pktgen_alloc_skb(odev, pkt_dev);
28381da177e4SLinus Torvalds 	if (!skb) {
28391da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
28401da177e4SLinus Torvalds 		return NULL;
28411da177e4SLinus Torvalds 	}
28421da177e4SLinus Torvalds 
28437a6e288dSDaniel Borkmann 	prefetchw(skb->data);
284463d75463SPaolo Abeni 	skb_reserve(skb, 16);
28451da177e4SLinus Torvalds 
28461da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
28471da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2848ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2849ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2850ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
285134954ddcSFrancesco Fondelli 
285234954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
285334954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
285434954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28550f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
28560f37c605SAl Viro 					       pkt_dev->svlan_cfi,
28570f37c605SAl Viro 					       pkt_dev->svlan_p);
285834954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2859d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
286034954ddcSFrancesco Fondelli 		}
286134954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28620f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
28630f37c605SAl Viro 				      pkt_dev->vlan_cfi,
28640f37c605SAl Viro 				      pkt_dev->vlan_p);
286534954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2866d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
286734954ddcSFrancesco Fondelli 	}
286834954ddcSFrancesco Fondelli 
2869c145aeb3SZhang Shengju 	skb_reset_mac_header(skb);
2870525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2871525cebedSThomas Graf 	iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2872525cebedSThomas Graf 
2873525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
2874525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
2875fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
28769e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
28779e50e3acSJohn Fastabend 
28781da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2879252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
28801da177e4SLinus Torvalds 
2881ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2882ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
288316dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
28846af773e7SNishank Trivedi 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
28851da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
28881da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
28891da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
2890c26bf4a5SThomas Graf 	udph->check = 0;
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds 	iph->ihl = 5;
28931da177e4SLinus Torvalds 	iph->version = 4;
28941da177e4SLinus Torvalds 	iph->ttl = 32;
28951ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
28961da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
28971da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
28981da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
289966ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
290066ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
29011da177e4SLinus Torvalds 	iph->frag_off = 0;
29021da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
29031da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
290403c633e7SThomas Graf 	ip_send_check(iph);
2905ca6549afSSteven Whitehouse 	skb->protocol = protocol;
29061da177e4SLinus Torvalds 	skb->dev = odev;
29071da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
2908c26bf4a5SThomas Graf 
29097744b5f3SSabrina Dubroca 	pktgen_finalize_skb(pkt_dev, skb, datalen);
29107744b5f3SSabrina Dubroca 
2911c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2912c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2913c8cd0989STom Herbert 	} else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
2914c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2915c26bf4a5SThomas Graf 		skb->csum = 0;
29167744b5f3SSabrina Dubroca 		udp4_hwcsum(skb, iph->saddr, iph->daddr);
2917c26bf4a5SThomas Graf 	} else {
29187744b5f3SSabrina Dubroca 		__wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
2919c26bf4a5SThomas Graf 
2920c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
29217744b5f3SSabrina Dubroca 		udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
2922c26bf4a5SThomas Graf 						datalen + 8, IPPROTO_UDP, csum);
2923c26bf4a5SThomas Graf 
2924c26bf4a5SThomas Graf 		if (udph->check == 0)
2925c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2926c26bf4a5SThomas Graf 	}
2927c26bf4a5SThomas Graf 
2928a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2929a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2930a553e4a6SJamal Hadi Salim 		return NULL;
2931a553e4a6SJamal Hadi Salim #endif
2932a553e4a6SJamal Hadi Salim 
29331da177e4SLinus Torvalds 	return skb;
29341da177e4SLinus Torvalds }
29351da177e4SLinus Torvalds 
29361da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
29371da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
29381da177e4SLinus Torvalds {
29391da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
29401da177e4SLinus Torvalds 	__u8 *eth;
29411da177e4SLinus Torvalds 	struct udphdr *udph;
2942c26bf4a5SThomas Graf 	int datalen, udplen;
29431da177e4SLinus Torvalds 	struct ipv6hdr *iph;
2944d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2945ca6549afSSteven Whitehouse 	__be32 *mpls;
294634954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
294734954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
294834954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
294934954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2950fd2ea0a7SDavid S. Miller 	u16 queue_map;
2951ca6549afSSteven Whitehouse 
2952ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2953d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
29541da177e4SLinus Torvalds 
295534954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2956d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
295734954ddcSFrancesco Fondelli 
295864053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
295964053beeSRobert Olsson 	 * fields.
296064053beeSRobert Olsson 	 */
296164053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2962eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
296364053beeSRobert Olsson 
296463d75463SPaolo Abeni 	skb = pktgen_alloc_skb(odev, pkt_dev);
29651da177e4SLinus Torvalds 	if (!skb) {
29661da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
29671da177e4SLinus Torvalds 		return NULL;
29681da177e4SLinus Torvalds 	}
29691da177e4SLinus Torvalds 
29707a6e288dSDaniel Borkmann 	prefetchw(skb->data);
29711da177e4SLinus Torvalds 	skb_reserve(skb, 16);
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
29741da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2975ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2976ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2977ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
297834954ddcSFrancesco Fondelli 
297934954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
298034954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
298134954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29820f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
29830f37c605SAl Viro 					       pkt_dev->svlan_cfi,
29840f37c605SAl Viro 					       pkt_dev->svlan_p);
298534954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2986d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
298734954ddcSFrancesco Fondelli 		}
298834954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29890f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
29900f37c605SAl Viro 				      pkt_dev->vlan_cfi,
29910f37c605SAl Viro 				      pkt_dev->vlan_p);
299234954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2993d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
299434954ddcSFrancesco Fondelli 	}
299534954ddcSFrancesco Fondelli 
2996c145aeb3SZhang Shengju 	skb_reset_mac_header(skb);
2997525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2998525cebedSThomas Graf 	iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2999525cebedSThomas Graf 
3000525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
3001525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
3002fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
30039e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
30041da177e4SLinus Torvalds 
30051da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
3006252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
30071da177e4SLinus Torvalds 
3008ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
3009ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
3010ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
301116dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
30121da177e4SLinus Torvalds 
30135aa8b572SAmerigo Wang 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
30141da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
3015e87cc472SJoe Perches 		net_info_ratelimited("increased datalen to %d\n", datalen);
30161da177e4SLinus Torvalds 	}
30171da177e4SLinus Torvalds 
3018c26bf4a5SThomas Graf 	udplen = datalen + sizeof(struct udphdr);
30191da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
30201da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
3021c26bf4a5SThomas Graf 	udph->len = htons(udplen);
3022c26bf4a5SThomas Graf 	udph->check = 0;
30231da177e4SLinus Torvalds 
3024d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
30251da177e4SLinus Torvalds 
30261ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
30271ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
3028252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
30291ca7768cSFrancesco Fondelli 	}
30301ca7768cSFrancesco Fondelli 
30311da177e4SLinus Torvalds 	iph->hop_limit = 32;
30321da177e4SLinus Torvalds 
3033c26bf4a5SThomas Graf 	iph->payload_len = htons(udplen);
30341da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
30351da177e4SLinus Torvalds 
30364e3fd7a0SAlexey Dobriyan 	iph->daddr = pkt_dev->cur_in6_daddr;
30374e3fd7a0SAlexey Dobriyan 	iph->saddr = pkt_dev->cur_in6_saddr;
30381da177e4SLinus Torvalds 
3039ca6549afSSteven Whitehouse 	skb->protocol = protocol;
30401da177e4SLinus Torvalds 	skb->dev = odev;
30411da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
30421da177e4SLinus Torvalds 
30437744b5f3SSabrina Dubroca 	pktgen_finalize_skb(pkt_dev, skb, datalen);
30447744b5f3SSabrina Dubroca 
3045c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
3046c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
3047c8cd0989STom Herbert 	} else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
3048c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
3049c26bf4a5SThomas Graf 		skb->csum_start = skb_transport_header(skb) - skb->head;
3050c26bf4a5SThomas Graf 		skb->csum_offset = offsetof(struct udphdr, check);
3051c26bf4a5SThomas Graf 		udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
3052c26bf4a5SThomas Graf 	} else {
30537744b5f3SSabrina Dubroca 		__wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
3054c26bf4a5SThomas Graf 
3055c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
3056c26bf4a5SThomas Graf 		udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
3057c26bf4a5SThomas Graf 
3058c26bf4a5SThomas Graf 		if (udph->check == 0)
3059c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
3060c26bf4a5SThomas Graf 	}
3061c26bf4a5SThomas Graf 
30621da177e4SLinus Torvalds 	return skb;
30631da177e4SLinus Torvalds }
30641da177e4SLinus Torvalds 
3065475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
30661da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
30671da177e4SLinus Torvalds {
30681da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
30691da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
30701da177e4SLinus Torvalds 	else
30711da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
30721da177e4SLinus Torvalds }
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
30751da177e4SLinus Torvalds {
30761da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
30771da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
30781da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
30791da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
30801da177e4SLinus Torvalds 	pkt_dev->errors = 0;
30811da177e4SLinus Torvalds }
30821da177e4SLinus Torvalds 
30831da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
30841da177e4SLinus Torvalds 
30851da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
30861da177e4SLinus Torvalds {
3087c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
30881da177e4SLinus Torvalds 	int started = 0;
30891da177e4SLinus Torvalds 
3090f9467eaeSJoe Perches 	func_enter();
30911da177e4SLinus Torvalds 
30928788370aSJesper Dangaard Brouer 	rcu_read_lock();
30938788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds 		/*
30961da177e4SLinus Torvalds 		 * setup odev and create initial packet.
30971da177e4SLinus Torvalds 		 */
30981da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
30991da177e4SLinus Torvalds 
31001da177e4SLinus Torvalds 		if (pkt_dev->odev) {
31011da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
31021da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
3103398f382cSDaniel Borkmann 			pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3104fd29cf72SStephen Hemminger 
310516dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
31061da177e4SLinus Torvalds 
31071da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
31088788370aSJesper Dangaard Brouer 			pkt_dev->running = 1;	/* Cranke yeself! */
31091da177e4SLinus Torvalds 			started++;
3110222f1806SLuiz Capitulino 		} else
31111da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
31121da177e4SLinus Torvalds 	}
31138788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3114222f1806SLuiz Capitulino 	if (started)
3115222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
31161da177e4SLinus Torvalds }
31171da177e4SLinus Torvalds 
31184e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
31191da177e4SLinus Torvalds {
3120cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31211da177e4SLinus Torvalds 
3122f9467eaeSJoe Perches 	func_enter();
31231da177e4SLinus Torvalds 
31246146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3125cdcdbe0bSLuiz Capitulino 
31264e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
312795ed63f7SArthur Kepner 		t->control |= T_STOP;
3128cdcdbe0bSLuiz Capitulino 
31296146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31301da177e4SLinus Torvalds }
31311da177e4SLinus Torvalds 
3132648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
31331da177e4SLinus Torvalds {
3134648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
31351da177e4SLinus Torvalds 
31368788370aSJesper Dangaard Brouer 	rcu_read_lock();
31378788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
31388788370aSJesper Dangaard Brouer 		if (pkt_dev->running) {
31398788370aSJesper Dangaard Brouer 			rcu_read_unlock();
3140648fda74SStephen Hemminger 			return 1;
31418788370aSJesper Dangaard Brouer 		}
31428788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3143648fda74SStephen Hemminger 	return 0;
31441da177e4SLinus Torvalds }
31451da177e4SLinus Torvalds 
31461da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
31471da177e4SLinus Torvalds {
31481da177e4SLinus Torvalds 	while (thread_is_running(t)) {
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds 		msleep_interruptible(100);
31511da177e4SLinus Torvalds 
31521da177e4SLinus Torvalds 		if (signal_pending(current))
31531da177e4SLinus Torvalds 			goto signal;
31541da177e4SLinus Torvalds 	}
31551da177e4SLinus Torvalds 	return 1;
31561da177e4SLinus Torvalds signal:
31571da177e4SLinus Torvalds 	return 0;
31581da177e4SLinus Torvalds }
31591da177e4SLinus Torvalds 
31604e58a027SCong Wang static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
31611da177e4SLinus Torvalds {
3162cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31631da177e4SLinus Torvalds 	int sig = 1;
31641da177e4SLinus Torvalds 
31656146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3166cdcdbe0bSLuiz Capitulino 
31674e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
31681da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
3169222f1806SLuiz Capitulino 		if (sig == 0)
3170222f1806SLuiz Capitulino 			break;
31711da177e4SLinus Torvalds 	}
3172cdcdbe0bSLuiz Capitulino 
3173cdcdbe0bSLuiz Capitulino 	if (sig == 0)
31744e58a027SCong Wang 		list_for_each_entry(t, &pn->pktgen_threads, th_list)
31751da177e4SLinus Torvalds 			t->control |= (T_STOP);
3176cdcdbe0bSLuiz Capitulino 
31776146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31781da177e4SLinus Torvalds 	return sig;
31791da177e4SLinus Torvalds }
31801da177e4SLinus Torvalds 
31814e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn)
31821da177e4SLinus Torvalds {
3183cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31841da177e4SLinus Torvalds 
3185f9467eaeSJoe Perches 	func_enter();
31861da177e4SLinus Torvalds 
31876146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
31881da177e4SLinus Torvalds 
31894e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
31901da177e4SLinus Torvalds 		t->control |= (T_RUN);
3191cdcdbe0bSLuiz Capitulino 
31926146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31931da177e4SLinus Torvalds 
319463adc6fbSStephen Hemminger 	/* Propagate thread->control  */
319563adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
31961da177e4SLinus Torvalds 
31974e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
31981da177e4SLinus Torvalds }
31991da177e4SLinus Torvalds 
32004e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn)
3201eb37b41cSJesse Brandeburg {
3202eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3203eb37b41cSJesse Brandeburg 
3204f9467eaeSJoe Perches 	func_enter();
3205eb37b41cSJesse Brandeburg 
3206eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3207eb37b41cSJesse Brandeburg 
32084e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
3209eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3210eb37b41cSJesse Brandeburg 
3211eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3212eb37b41cSJesse Brandeburg 
321363adc6fbSStephen Hemminger 	/* Propagate thread->control  */
321463adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3215eb37b41cSJesse Brandeburg 
32164e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
3217eb37b41cSJesse Brandeburg }
3218eb37b41cSJesse Brandeburg 
32191da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
32201da177e4SLinus Torvalds {
3221fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
32221da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3223fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3224fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3225fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
32261da177e4SLinus Torvalds 
322703a14ab1SDaniel Turull 	p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3228fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3229fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3230fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
32311da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
32321da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
32331da177e4SLinus Torvalds 
3234fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3235fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
32361da177e4SLinus Torvalds 
32371da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds 	mbps = bps;
32401da177e4SLinus Torvalds 	do_div(mbps, 1000000);
32411da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
32421da177e4SLinus Torvalds 		     (unsigned long long)pps,
32431da177e4SLinus Torvalds 		     (unsigned long long)mbps,
32441da177e4SLinus Torvalds 		     (unsigned long long)bps,
32451da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
32461da177e4SLinus Torvalds }
32471da177e4SLinus Torvalds 
32481da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
32491da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
32501da177e4SLinus Torvalds {
3251222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds 	if (!pkt_dev->running) {
3254294a0b7fSJoe Perches 		pr_warn("interface: %s is already stopped\n",
3255f9467eaeSJoe Perches 			pkt_dev->odevname);
32561da177e4SLinus Torvalds 		return -EINVAL;
32571da177e4SLinus Torvalds 	}
32581da177e4SLinus Torvalds 
32598788370aSJesper Dangaard Brouer 	pkt_dev->running = 0;
32603bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
32613bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3262398f382cSDaniel Borkmann 	pkt_dev->stopped_at = ktime_get();
32631da177e4SLinus Torvalds 
326495ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
32651da177e4SLinus Torvalds 
32661da177e4SLinus Torvalds 	return 0;
32671da177e4SLinus Torvalds }
32681da177e4SLinus Torvalds 
32691da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
32701da177e4SLinus Torvalds {
3271c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
32721da177e4SLinus Torvalds 
32738788370aSJesper Dangaard Brouer 	rcu_read_lock();
32748788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3275c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3276222f1806SLuiz Capitulino 			continue;
3277222f1806SLuiz Capitulino 		if (best == NULL)
3278c26a8016SLuiz Capitulino 			best = pkt_dev;
3279398f382cSDaniel Borkmann 		else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3280c26a8016SLuiz Capitulino 			best = pkt_dev;
32811da177e4SLinus Torvalds 	}
32828788370aSJesper Dangaard Brouer 	rcu_read_unlock();
32838788370aSJesper Dangaard Brouer 
32841da177e4SLinus Torvalds 	return best;
32851da177e4SLinus Torvalds }
32861da177e4SLinus Torvalds 
3287222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3288222f1806SLuiz Capitulino {
3289c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
32901da177e4SLinus Torvalds 
3291f9467eaeSJoe Perches 	func_enter();
32921da177e4SLinus Torvalds 
32938788370aSJesper Dangaard Brouer 	rcu_read_lock();
32941da177e4SLinus Torvalds 
32958788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3296c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
329795ed63f7SArthur Kepner 	}
329895ed63f7SArthur Kepner 
32998788370aSJesper Dangaard Brouer 	rcu_read_unlock();
330095ed63f7SArthur Kepner }
330195ed63f7SArthur Kepner 
330295ed63f7SArthur Kepner /*
330395ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
330495ed63f7SArthur Kepner  * and remove it
330595ed63f7SArthur Kepner  */
330695ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
330795ed63f7SArthur Kepner {
3308c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3309c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
331095ed63f7SArthur Kepner 
3311f9467eaeSJoe Perches 	func_enter();
331295ed63f7SArthur Kepner 
3313c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3314c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
331595ed63f7SArthur Kepner 
3316222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3317222f1806SLuiz Capitulino 			continue;
331895ed63f7SArthur Kepner 
331995ed63f7SArthur Kepner 		kfree_skb(cur->skb);
332095ed63f7SArthur Kepner 		cur->skb = NULL;
332195ed63f7SArthur Kepner 
332295ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
332395ed63f7SArthur Kepner 
332495ed63f7SArthur Kepner 		break;
332595ed63f7SArthur Kepner 	}
33261da177e4SLinus Torvalds }
33271da177e4SLinus Torvalds 
33281da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
33291da177e4SLinus Torvalds {
3330c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3331c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
33321da177e4SLinus Torvalds 
3333f9467eaeSJoe Perches 	func_enter();
3334f9467eaeSJoe Perches 
33351da177e4SLinus Torvalds 	/* Remove all devices, free mem */
33361da177e4SLinus Torvalds 
3337c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3338c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
333995ed63f7SArthur Kepner 
334095ed63f7SArthur Kepner 		kfree_skb(cur->skb);
334195ed63f7SArthur Kepner 		cur->skb = NULL;
334295ed63f7SArthur Kepner 
33431da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
33441da177e4SLinus Torvalds 	}
33451da177e4SLinus Torvalds }
33461da177e4SLinus Torvalds 
33471da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
33481da177e4SLinus Torvalds {
33491da177e4SLinus Torvalds 	/* Remove from the thread list */
33504e58a027SCong Wang 	remove_proc_entry(t->tsk->comm, t->net->proc_dir);
33511da177e4SLinus Torvalds }
33521da177e4SLinus Torvalds 
3353ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
33543791decbSStephen Hemminger {
3355398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
33563791decbSStephen Hemminger 	schedule();
3357398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
33583791decbSStephen Hemminger }
33593791decbSStephen Hemminger 
3360ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3361ef87979cSStephen Hemminger {
3362398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
3363ef87979cSStephen Hemminger 
3364ef87979cSStephen Hemminger 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3365ef87979cSStephen Hemminger 		if (signal_pending(current))
3366ef87979cSStephen Hemminger 			break;
3367ef87979cSStephen Hemminger 
3368ef87979cSStephen Hemminger 		if (need_resched())
3369ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3370ef87979cSStephen Hemminger 		else
3371ef87979cSStephen Hemminger 			cpu_relax();
3372ef87979cSStephen Hemminger 	}
3373398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3374ef87979cSStephen Hemminger }
3375fd29cf72SStephen Hemminger 
3376475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
33771da177e4SLinus Torvalds {
337838b2cf29SAlexei Starovoitov 	unsigned int burst = ACCESS_ONCE(pkt_dev->burst);
337900829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
3380fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
338162f64aedSAlexei Starovoitov 	struct sk_buff *skb;
33821da177e4SLinus Torvalds 	int ret;
33831da177e4SLinus Torvalds 
3384ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3385ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
33861da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
33873791decbSStephen Hemminger 		return;
33881da177e4SLinus Torvalds 	}
33891da177e4SLinus Torvalds 
3390ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3391ef87979cSStephen Hemminger 	 * "never transmit"
3392ef87979cSStephen Hemminger 	 */
3393ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3394398f382cSDaniel Borkmann 		pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3395ef87979cSStephen Hemminger 		return;
3396ef87979cSStephen Hemminger 	}
3397ef87979cSStephen Hemminger 
3398ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
33997d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
34007d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
34011da177e4SLinus Torvalds 		/* build a new pkt */
34021da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
34031da177e4SLinus Torvalds 
34041da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
34051da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
3406f9467eaeSJoe Perches 			pr_err("ERROR: couldn't allocate skb in fill_packet\n");
34071da177e4SLinus Torvalds 			schedule();
34081da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
34093791decbSStephen Hemminger 			return;
34101da177e4SLinus Torvalds 		}
3411baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
34121da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
34131da177e4SLinus Torvalds 	}
34141da177e4SLinus Torvalds 
3415ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3416ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3417ef87979cSStephen Hemminger 
341862f64aedSAlexei Starovoitov 	if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
341962f64aedSAlexei Starovoitov 		skb = pkt_dev->skb;
342062f64aedSAlexei Starovoitov 		skb->protocol = eth_type_trans(skb, skb->dev);
342162f64aedSAlexei Starovoitov 		atomic_add(burst, &skb->users);
342262f64aedSAlexei Starovoitov 		local_bh_disable();
342362f64aedSAlexei Starovoitov 		do {
342462f64aedSAlexei Starovoitov 			ret = netif_receive_skb(skb);
342562f64aedSAlexei Starovoitov 			if (ret == NET_RX_DROP)
342662f64aedSAlexei Starovoitov 				pkt_dev->errors++;
342762f64aedSAlexei Starovoitov 			pkt_dev->sofar++;
342862f64aedSAlexei Starovoitov 			pkt_dev->seq_num++;
342962f64aedSAlexei Starovoitov 			if (atomic_read(&skb->users) != burst) {
343062f64aedSAlexei Starovoitov 				/* skb was queued by rps/rfs or taps,
343162f64aedSAlexei Starovoitov 				 * so cannot reuse this skb
343262f64aedSAlexei Starovoitov 				 */
343362f64aedSAlexei Starovoitov 				atomic_sub(burst - 1, &skb->users);
343462f64aedSAlexei Starovoitov 				/* get out of the loop and wait
343562f64aedSAlexei Starovoitov 				 * until skb is consumed
343662f64aedSAlexei Starovoitov 				 */
343762f64aedSAlexei Starovoitov 				break;
343862f64aedSAlexei Starovoitov 			}
343962f64aedSAlexei Starovoitov 			/* skb was 'freed' by stack, so clean few
344062f64aedSAlexei Starovoitov 			 * bits and reuse it
344162f64aedSAlexei Starovoitov 			 */
3442*a5135bcfSWillem de Bruijn 			skb_reset_tc(skb);
344362f64aedSAlexei Starovoitov 		} while (--burst > 0);
344462f64aedSAlexei Starovoitov 		goto out; /* Skips xmit_mode M_START_XMIT */
34450967f244SJohn Fastabend 	} else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
34460967f244SJohn Fastabend 		local_bh_disable();
34470967f244SJohn Fastabend 		atomic_inc(&pkt_dev->skb->users);
34480967f244SJohn Fastabend 
34490967f244SJohn Fastabend 		ret = dev_queue_xmit(pkt_dev->skb);
34500967f244SJohn Fastabend 		switch (ret) {
34510967f244SJohn Fastabend 		case NET_XMIT_SUCCESS:
34520967f244SJohn Fastabend 			pkt_dev->sofar++;
34530967f244SJohn Fastabend 			pkt_dev->seq_num++;
34540967f244SJohn Fastabend 			pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
34550967f244SJohn Fastabend 			break;
34560967f244SJohn Fastabend 		case NET_XMIT_DROP:
34570967f244SJohn Fastabend 		case NET_XMIT_CN:
34580967f244SJohn Fastabend 		/* These are all valid return codes for a qdisc but
34590967f244SJohn Fastabend 		 * indicate packets are being dropped or will likely
34600967f244SJohn Fastabend 		 * be dropped soon.
34610967f244SJohn Fastabend 		 */
34620967f244SJohn Fastabend 		case NETDEV_TX_BUSY:
34630967f244SJohn Fastabend 		/* qdisc may call dev_hard_start_xmit directly in cases
34640967f244SJohn Fastabend 		 * where no queues exist e.g. loopback device, virtual
34650967f244SJohn Fastabend 		 * devices, etc. In this case we need to handle
34660967f244SJohn Fastabend 		 * NETDEV_TX_ codes.
34670967f244SJohn Fastabend 		 */
34680967f244SJohn Fastabend 		default:
34690967f244SJohn Fastabend 			pkt_dev->errors++;
34700967f244SJohn Fastabend 			net_info_ratelimited("%s xmit error: %d\n",
34710967f244SJohn Fastabend 					     pkt_dev->odevname, ret);
34720967f244SJohn Fastabend 			break;
34730967f244SJohn Fastabend 		}
34740967f244SJohn Fastabend 		goto out;
347562f64aedSAlexei Starovoitov 	}
347662f64aedSAlexei Starovoitov 
347710c51b56SDaniel Borkmann 	txq = skb_get_tx_queue(odev, pkt_dev->skb);
3478fd2ea0a7SDavid S. Miller 
34790f2eea4bSDaniel Borkmann 	local_bh_disable();
34800f2eea4bSDaniel Borkmann 
34810f2eea4bSDaniel Borkmann 	HARD_TX_LOCK(odev, txq, smp_processor_id());
34825b8db2f5SStephen Hemminger 
34836f25cd47SDaniel Borkmann 	if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3484ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
34850835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
34860835acfeSEric Dumazet 		goto unlock;
34870835acfeSEric Dumazet 	}
348838b2cf29SAlexei Starovoitov 	atomic_add(burst, &pkt_dev->skb->users);
348938b2cf29SAlexei Starovoitov 
349038b2cf29SAlexei Starovoitov xmit_more:
349138b2cf29SAlexei Starovoitov 	ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
3492ef87979cSStephen Hemminger 
34935b8db2f5SStephen Hemminger 	switch (ret) {
34945b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
34951da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
34961da177e4SLinus Torvalds 		pkt_dev->sofar++;
34971da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3498baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
349938b2cf29SAlexei Starovoitov 		if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
350038b2cf29SAlexei Starovoitov 			goto xmit_more;
35015b8db2f5SStephen Hemminger 		break;
3502f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3503f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3504f466dba1SJohn Fastabend 		/* skb has been consumed */
3505f466dba1SJohn Fastabend 		pkt_dev->errors++;
3506f466dba1SJohn Fastabend 		break;
35075b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
3508e87cc472SJoe Perches 		net_info_ratelimited("%s xmit error: %d\n",
3509e87cc472SJoe Perches 				     pkt_dev->odevname, ret);
35101da177e4SLinus Torvalds 		pkt_dev->errors++;
35115b8db2f5SStephen Hemminger 		/* fallthru */
35125b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
35135b8db2f5SStephen Hemminger 		/* Retry it next time */
35145b8db2f5SStephen Hemminger 		atomic_dec(&(pkt_dev->skb->users));
35151da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
35161da177e4SLinus Torvalds 	}
351738b2cf29SAlexei Starovoitov 	if (unlikely(burst))
351838b2cf29SAlexei Starovoitov 		atomic_sub(burst, &pkt_dev->skb->users);
35190835acfeSEric Dumazet unlock:
35200f2eea4bSDaniel Borkmann 	HARD_TX_UNLOCK(odev, txq);
35210f2eea4bSDaniel Borkmann 
352262f64aedSAlexei Starovoitov out:
35230f2eea4bSDaniel Borkmann 	local_bh_enable();
35241da177e4SLinus Torvalds 
35251da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
35261da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3527ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
35281da177e4SLinus Torvalds 
35291da177e4SLinus Torvalds 		/* Done with this */
35301da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
35311da177e4SLinus Torvalds 	}
35321da177e4SLinus Torvalds }
35331da177e4SLinus Torvalds 
35341da177e4SLinus Torvalds /*
35351da177e4SLinus Torvalds  * Main loop of the thread goes here
35361da177e4SLinus Torvalds  */
35371da177e4SLinus Torvalds 
3538ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
35391da177e4SLinus Torvalds {
35401da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3541ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
35421da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
35431da177e4SLinus Torvalds 	int cpu = t->cpu;
35441da177e4SLinus Torvalds 
3545ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3548d3ede327SDenis V. Lunev 	complete(&t->start_done);
35491da177e4SLinus Torvalds 
3550f9467eaeSJoe Perches 	pr_debug("starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
35511da177e4SLinus Torvalds 
355283144186SRafael J. Wysocki 	set_freezable();
355383144186SRafael J. Wysocki 
3554ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3555ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3556ee74baa7SDavid S. Miller 
3557ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
35584e58a027SCong Wang 			if (t->net->pktgen_exiting)
3559551eaff1SEric Dumazet 				break;
3560ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3561ef87979cSStephen Hemminger 							 t->control != 0,
3562ef87979cSStephen Hemminger 							 HZ/10);
35631b3f720bSRafael J. Wysocki 			try_to_freeze();
3564ef87979cSStephen Hemminger 			continue;
3565ee74baa7SDavid S. Miller 		}
35661da177e4SLinus Torvalds 
3567ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
35681da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
35691da177e4SLinus Torvalds 
3570ef87979cSStephen Hemminger 			if (need_resched())
3571ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3572ef87979cSStephen Hemminger 			else
3573ef87979cSStephen Hemminger 				cpu_relax();
3574ef87979cSStephen Hemminger 		}
3575ef87979cSStephen Hemminger 
35761da177e4SLinus Torvalds 		if (t->control & T_STOP) {
35771da177e4SLinus Torvalds 			pktgen_stop(t);
35781da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
35791da177e4SLinus Torvalds 		}
35801da177e4SLinus Torvalds 
35811da177e4SLinus Torvalds 		if (t->control & T_RUN) {
35821da177e4SLinus Torvalds 			pktgen_run(t);
35831da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
35841da177e4SLinus Torvalds 		}
35851da177e4SLinus Torvalds 
358695ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
35871da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
358895ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
358995ed63f7SArthur Kepner 		}
359095ed63f7SArthur Kepner 
359195ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
359295ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
35931da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
35941da177e4SLinus Torvalds 		}
35951da177e4SLinus Torvalds 
359609fe3ef4SAndrew Morton 		try_to_freeze();
35971da177e4SLinus Torvalds 	}
35981da177e4SLinus Torvalds 
3599f9467eaeSJoe Perches 	pr_debug("%s stopping all device\n", t->tsk->comm);
36001da177e4SLinus Torvalds 	pktgen_stop(t);
36011da177e4SLinus Torvalds 
3602f9467eaeSJoe Perches 	pr_debug("%s removing all device\n", t->tsk->comm);
36031da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
36041da177e4SLinus Torvalds 
3605f9467eaeSJoe Perches 	pr_debug("%s removing thread\n", t->tsk->comm);
36061da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3607cdcdbe0bSLuiz Capitulino 
3608ee74baa7SDavid S. Miller 	return 0;
36091da177e4SLinus Torvalds }
36101da177e4SLinus Torvalds 
3611222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
36123e984840SEric Dumazet 					  const char *ifname, bool exact)
36131da177e4SLinus Torvalds {
3614c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
36153e984840SEric Dumazet 	size_t len = strlen(ifname);
36161da177e4SLinus Torvalds 
36178788370aSJesper Dangaard Brouer 	rcu_read_lock();
36188788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(p, &t->if_list, list)
36193e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
36203e984840SEric Dumazet 			if (p->odevname[len]) {
36213e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
36223e984840SEric Dumazet 					continue;
36233e984840SEric Dumazet 			}
3624c26a8016SLuiz Capitulino 			pkt_dev = p;
36251da177e4SLinus Torvalds 			break;
36261da177e4SLinus Torvalds 		}
36271da177e4SLinus Torvalds 
36288788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3629f9467eaeSJoe Perches 	pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
36301da177e4SLinus Torvalds 	return pkt_dev;
36311da177e4SLinus Torvalds }
36321da177e4SLinus Torvalds 
36331da177e4SLinus Torvalds /*
36341da177e4SLinus Torvalds  * Adds a dev at front of if_list.
36351da177e4SLinus Torvalds  */
36361da177e4SLinus Torvalds 
3637222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3638222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
36391da177e4SLinus Torvalds {
36401da177e4SLinus Torvalds 	int rv = 0;
36411da177e4SLinus Torvalds 
36428788370aSJesper Dangaard Brouer 	/* This function cannot be called concurrently, as its called
36438788370aSJesper Dangaard Brouer 	 * under pktgen_thread_lock mutex, but it can run from
36448788370aSJesper Dangaard Brouer 	 * userspace on another CPU than the kthread.  The if_lock()
36458788370aSJesper Dangaard Brouer 	 * is used here to sync with concurrent instances of
36468788370aSJesper Dangaard Brouer 	 * _rem_dev_from_if_list() invoked via kthread, which is also
36478788370aSJesper Dangaard Brouer 	 * updating the if_list */
36481da177e4SLinus Torvalds 	if_lock(t);
36491da177e4SLinus Torvalds 
36501da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
3651f9467eaeSJoe Perches 		pr_err("ERROR: already assigned to a thread\n");
36521da177e4SLinus Torvalds 		rv = -EBUSY;
36531da177e4SLinus Torvalds 		goto out;
36541da177e4SLinus Torvalds 	}
3655c26a8016SLuiz Capitulino 
36561da177e4SLinus Torvalds 	pkt_dev->running = 0;
36578788370aSJesper Dangaard Brouer 	pkt_dev->pg_thread = t;
36588788370aSJesper Dangaard Brouer 	list_add_rcu(&pkt_dev->list, &t->if_list);
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds out:
36611da177e4SLinus Torvalds 	if_unlock(t);
36621da177e4SLinus Torvalds 	return rv;
36631da177e4SLinus Torvalds }
36641da177e4SLinus Torvalds 
36651da177e4SLinus Torvalds /* Called under thread lock */
36661da177e4SLinus Torvalds 
36671da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
36681da177e4SLinus Torvalds {
36691da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
367039df232fSStephen Hemminger 	int err;
36713291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
36741da177e4SLinus Torvalds 
36754e58a027SCong Wang 	pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3676d50a6b56SStephen Hemminger 	if (pkt_dev) {
3677f9467eaeSJoe Perches 		pr_err("ERROR: interface already used\n");
3678d50a6b56SStephen Hemminger 		return -EBUSY;
3679d50a6b56SStephen Hemminger 	}
36801da177e4SLinus Torvalds 
36813291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
36821da177e4SLinus Torvalds 	if (!pkt_dev)
36831da177e4SLinus Torvalds 		return -ENOMEM;
36841da177e4SLinus Torvalds 
3685593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
368668d5ac2eSWANG Cong 	pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
36873291b9dbSEric Dumazet 				      node);
36881da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
36891da177e4SLinus Torvalds 		kfree(pkt_dev);
36901da177e4SLinus Torvalds 		return -ENOMEM;
36911da177e4SLinus Torvalds 	}
36921da177e4SLinus Torvalds 
369395ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
36941da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
3695fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
36961da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
36971da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
36981da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
36991da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
37001da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
37011da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
370234954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
370334954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
370434954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
370534954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
370634954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
370734954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
370838b2cf29SAlexei Starovoitov 	pkt_dev->burst = 1;
3709e99b99b4SRobert Olsson 	pkt_dev->node = -1;
371034954ddcSFrancesco Fondelli 
37114e58a027SCong Wang 	err = pktgen_setup_dev(t->net, pkt_dev, ifname);
371239df232fSStephen Hemminger 	if (err)
371339df232fSStephen Hemminger 		goto out1;
3714d8873315SNeil Horman 	if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3715d8873315SNeil Horman 		pkt_dev->clone_skb = pg_clone_skb_d;
37161da177e4SLinus Torvalds 
37174e58a027SCong Wang 	pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
37185efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
371939df232fSStephen Hemminger 	if (!pkt_dev->entry) {
3720f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3721d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
372239df232fSStephen Hemminger 		err = -EINVAL;
372339df232fSStephen Hemminger 		goto out2;
372439df232fSStephen Hemminger 	}
3725a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3726a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3727a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3728cf93d47eSFan Du 
3729cf93d47eSFan Du 	/* xfrm tunnel mode needs additional dst to extract outter
3730cf93d47eSFan Du 	 * ip header protocol/ttl/id field, here creat a phony one.
3731cf93d47eSFan Du 	 * instead of looking for a valid rt, which definitely hurting
3732cf93d47eSFan Du 	 * performance under such circumstance.
3733cf93d47eSFan Du 	 */
3734cf93d47eSFan Du 	pkt_dev->dstops.family = AF_INET;
3735cf93d47eSFan Du 	pkt_dev->dst.dev = pkt_dev->odev;
3736cf93d47eSFan Du 	dst_init_metrics(&pkt_dev->dst, pktgen_dst_metrics, false);
3737cf93d47eSFan Du 	pkt_dev->dst.child = &pkt_dev->dst;
3738cf93d47eSFan Du 	pkt_dev->dst.ops = &pkt_dev->dstops;
3739a553e4a6SJamal Hadi Salim #endif
374039df232fSStephen Hemminger 
374139df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
374239df232fSStephen Hemminger out2:
374339df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
374439df232fSStephen Hemminger out1:
3745a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3746a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3747a553e4a6SJamal Hadi Salim #endif
37481da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
37491da177e4SLinus Torvalds 	kfree(pkt_dev);
375039df232fSStephen Hemminger 	return err;
37511da177e4SLinus Torvalds }
37521da177e4SLinus Torvalds 
37534e58a027SCong Wang static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
37541da177e4SLinus Torvalds {
3755cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3756d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3757ee74baa7SDavid S. Miller 	struct task_struct *p;
37581da177e4SLinus Torvalds 
37593291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
37603291b9dbSEric Dumazet 			 cpu_to_node(cpu));
37611da177e4SLinus Torvalds 	if (!t) {
3762f9467eaeSJoe Perches 		pr_err("ERROR: out of memory, can't create new thread\n");
37631da177e4SLinus Torvalds 		return -ENOMEM;
37641da177e4SLinus Torvalds 	}
37651da177e4SLinus Torvalds 
37669a0b1e8bSEric Dumazet 	mutex_init(&t->if_lock);
37671da177e4SLinus Torvalds 	t->cpu = cpu;
37681da177e4SLinus Torvalds 
3769ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3770ee74baa7SDavid S. Miller 
37714e58a027SCong Wang 	list_add_tail(&t->th_list, &pn->pktgen_threads);
3772d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3773ee74baa7SDavid S. Miller 
377494dcf29aSEric Dumazet 	p = kthread_create_on_node(pktgen_thread_worker,
377594dcf29aSEric Dumazet 				   t,
377694dcf29aSEric Dumazet 				   cpu_to_node(cpu),
377794dcf29aSEric Dumazet 				   "kpktgend_%d", cpu);
3778ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
3779f9467eaeSJoe Perches 		pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3780ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3781ee74baa7SDavid S. Miller 		kfree(t);
3782ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3783ee74baa7SDavid S. Miller 	}
3784ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3785ee74baa7SDavid S. Miller 	t->tsk = p;
3786ee74baa7SDavid S. Miller 
37874e58a027SCong Wang 	pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
37885efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3789d50a6b56SStephen Hemminger 	if (!pe) {
3790f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3791ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3792ee74baa7SDavid S. Miller 		kthread_stop(p);
3793ee74baa7SDavid S. Miller 		list_del(&t->th_list);
37941da177e4SLinus Torvalds 		kfree(t);
37951da177e4SLinus Torvalds 		return -EINVAL;
37961da177e4SLinus Torvalds 	}
3797d50a6b56SStephen Hemminger 
37984e58a027SCong Wang 	t->net = pn;
37991fbe4b46SOleg Nesterov 	get_task_struct(p);
3800ee74baa7SDavid S. Miller 	wake_up_process(p);
3801d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
38021da177e4SLinus Torvalds 
38031da177e4SLinus Torvalds 	return 0;
38041da177e4SLinus Torvalds }
38051da177e4SLinus Torvalds 
38061da177e4SLinus Torvalds /*
38071da177e4SLinus Torvalds  * Removes a device from the thread if_list.
38081da177e4SLinus Torvalds  */
3809222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3810222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
38111da177e4SLinus Torvalds {
3812c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3813c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
38141da177e4SLinus Torvalds 
38158788370aSJesper Dangaard Brouer 	if_lock(t);
3816c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3817c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3818c26a8016SLuiz Capitulino 		if (p == pkt_dev)
38198788370aSJesper Dangaard Brouer 			list_del_rcu(&p->list);
38201da177e4SLinus Torvalds 	}
38218788370aSJesper Dangaard Brouer 	if_unlock(t);
38221da177e4SLinus Torvalds }
38231da177e4SLinus Torvalds 
3824222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3825222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
38261da177e4SLinus Torvalds {
3827f9467eaeSJoe Perches 	pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
38281da177e4SLinus Torvalds 
38291da177e4SLinus Torvalds 	if (pkt_dev->running) {
3830294a0b7fSJoe Perches 		pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
38311da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
38321da177e4SLinus Torvalds 	}
38331da177e4SLinus Torvalds 
38341da177e4SLinus Torvalds 	/* Dis-associate from the interface */
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds 	if (pkt_dev->odev) {
38371da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
38381da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
38391da177e4SLinus Torvalds 	}
38401da177e4SLinus Torvalds 
38418788370aSJesper Dangaard Brouer 	/* Remove proc before if_list entry, because add_device uses
38428788370aSJesper Dangaard Brouer 	 * list to determine if interface already exist, avoid race
38438788370aSJesper Dangaard Brouer 	 * with proc_create_data() */
3844a8ca16eaSDavid Howells 	proc_remove(pkt_dev->entry);
38451da177e4SLinus Torvalds 
38468788370aSJesper Dangaard Brouer 	/* And update the thread if_list */
38478788370aSJesper Dangaard Brouer 	_rem_dev_from_if_list(t, pkt_dev);
38488788370aSJesper Dangaard Brouer 
3849a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3850a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3851a553e4a6SJamal Hadi Salim #endif
38521da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
385326ad7879SEric Dumazet 	if (pkt_dev->page)
385426ad7879SEric Dumazet 		put_page(pkt_dev->page);
38558788370aSJesper Dangaard Brouer 	kfree_rcu(pkt_dev, rcu);
38561da177e4SLinus Torvalds 	return 0;
38571da177e4SLinus Torvalds }
38581da177e4SLinus Torvalds 
38594e58a027SCong Wang static int __net_init pg_net_init(struct net *net)
38601da177e4SLinus Torvalds {
38614e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3862d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
38634e58a027SCong Wang 	int cpu, ret = 0;
3864d50a6b56SStephen Hemminger 
38654e58a027SCong Wang 	pn->net = net;
38664e58a027SCong Wang 	INIT_LIST_HEAD(&pn->pktgen_threads);
38674e58a027SCong Wang 	pn->pktgen_exiting = false;
38684e58a027SCong Wang 	pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
38694e58a027SCong Wang 	if (!pn->proc_dir) {
38704e58a027SCong Wang 		pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3871d50a6b56SStephen Hemminger 		return -ENODEV;
38721da177e4SLinus Torvalds 	}
38734e58a027SCong Wang 	pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
38744e58a027SCong Wang 	if (pe == NULL) {
38754e58a027SCong Wang 		pr_err("cannot create %s procfs entry\n", PGCTRL);
38764e58a027SCong Wang 		ret = -EINVAL;
38774e58a027SCong Wang 		goto remove;
38784e58a027SCong Wang 	}
38791da177e4SLinus Torvalds 
3880670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
38818024bb24SLuiz Capitulino 		int err;
38821da177e4SLinus Torvalds 
38834e58a027SCong Wang 		err = pktgen_create_thread(cpu, pn);
38848024bb24SLuiz Capitulino 		if (err)
38854e58a027SCong Wang 			pr_warn("Cannot create thread for cpu %d (%d)\n",
3886f9467eaeSJoe Perches 				   cpu, err);
38871da177e4SLinus Torvalds 	}
38888024bb24SLuiz Capitulino 
38894e58a027SCong Wang 	if (list_empty(&pn->pktgen_threads)) {
38904e58a027SCong Wang 		pr_err("Initialization failed for all threads\n");
3891ce14f894SWANG Cong 		ret = -ENODEV;
38924e58a027SCong Wang 		goto remove_entry;
38938024bb24SLuiz Capitulino 	}
38948024bb24SLuiz Capitulino 
38951da177e4SLinus Torvalds 	return 0;
3896ce14f894SWANG Cong 
38974e58a027SCong Wang remove_entry:
38984e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
38994e58a027SCong Wang remove:
3900ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3901ce14f894SWANG Cong 	return ret;
39021da177e4SLinus Torvalds }
39031da177e4SLinus Torvalds 
39044e58a027SCong Wang static void __net_exit pg_net_exit(struct net *net)
39051da177e4SLinus Torvalds {
39064e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3907cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3908cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
3909d4b11335SEric Dumazet 	LIST_HEAD(list);
39101da177e4SLinus Torvalds 
39111da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
39124e58a027SCong Wang 	pn->pktgen_exiting = true;
39131da177e4SLinus Torvalds 
3914c57b5468SEric Dumazet 	mutex_lock(&pktgen_thread_lock);
39154e58a027SCong Wang 	list_splice_init(&pn->pktgen_threads, &list);
3916c57b5468SEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
3917c57b5468SEric Dumazet 
3918c57b5468SEric Dumazet 	list_for_each_safe(q, n, &list) {
3919cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3920c57b5468SEric Dumazet 		list_del(&t->th_list);
3921ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
39221fbe4b46SOleg Nesterov 		put_task_struct(t->tsk);
3923ee74baa7SDavid S. Miller 		kfree(t);
39241da177e4SLinus Torvalds 	}
39251da177e4SLinus Torvalds 
39264e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
3927ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
39284e58a027SCong Wang }
39291da177e4SLinus Torvalds 
39304e58a027SCong Wang static struct pernet_operations pg_net_ops = {
39314e58a027SCong Wang 	.init = pg_net_init,
39324e58a027SCong Wang 	.exit = pg_net_exit,
39334e58a027SCong Wang 	.id   = &pg_net_id,
39344e58a027SCong Wang 	.size = sizeof(struct pktgen_net),
39354e58a027SCong Wang };
39364e58a027SCong Wang 
39374e58a027SCong Wang static int __init pg_init(void)
39384e58a027SCong Wang {
39394e58a027SCong Wang 	int ret = 0;
39404e58a027SCong Wang 
39414e58a027SCong Wang 	pr_info("%s", version);
39424e58a027SCong Wang 	ret = register_pernet_subsys(&pg_net_ops);
39434e58a027SCong Wang 	if (ret)
39444e58a027SCong Wang 		return ret;
39454e58a027SCong Wang 	ret = register_netdevice_notifier(&pktgen_notifier_block);
39464e58a027SCong Wang 	if (ret)
39474e58a027SCong Wang 		unregister_pernet_subsys(&pg_net_ops);
39484e58a027SCong Wang 
39494e58a027SCong Wang 	return ret;
39504e58a027SCong Wang }
39514e58a027SCong Wang 
39524e58a027SCong Wang static void __exit pg_cleanup(void)
39534e58a027SCong Wang {
39544e58a027SCong Wang 	unregister_netdevice_notifier(&pktgen_notifier_block);
39554e58a027SCong Wang 	unregister_pernet_subsys(&pg_net_ops);
39568788370aSJesper Dangaard Brouer 	/* Don't need rcu_barrier() due to use of kfree_rcu() */
39571da177e4SLinus Torvalds }
39581da177e4SLinus Torvalds 
39591da177e4SLinus Torvalds module_init(pg_init);
39601da177e4SLinus Torvalds module_exit(pg_cleanup);
39611da177e4SLinus Torvalds 
3962c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
39631da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
39641da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3965c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
39661da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3967c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
39681da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3969c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
39701da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3971c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
39721da177e4SLinus Torvalds module_param(debug, int, 0);
3973c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3974