xref: /openbmc/linux/net/core/pktgen.c (revision 63d75463c91a5b5be7c0aca11ceb45ea5a0ae81d)
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 */
2191da177e4SLinus Torvalds #define   if_lock(t)           spin_lock(&(t->if_lock));
2201da177e4SLinus Torvalds #define   if_unlock(t)           spin_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 
4164e58a027SCong Wang static 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 {
42663adc6fbSStephen Hemminger 	spinlock_t 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 
20134e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
201439df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
201539df232fSStephen Hemminger 
20168788370aSJesper Dangaard Brouer 		rcu_read_lock();
20178788370aSJesper Dangaard Brouer 		list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
201839df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
201939df232fSStephen Hemminger 				continue;
202039df232fSStephen Hemminger 
2021a8ca16eaSDavid Howells 			proc_remove(pkt_dev->entry);
202239df232fSStephen Hemminger 
20232975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
20244e58a027SCong Wang 							  pn->proc_dir,
20252975315bSAlexey Dobriyan 							  &pktgen_if_fops,
20262975315bSAlexey Dobriyan 							  pkt_dev);
202739df232fSStephen Hemminger 			if (!pkt_dev->entry)
2028f9467eaeSJoe Perches 				pr_err("can't move proc entry for '%s'\n",
2029f9467eaeSJoe Perches 				       dev->name);
203039df232fSStephen Hemminger 			break;
203139df232fSStephen Hemminger 		}
20328788370aSJesper Dangaard Brouer 		rcu_read_unlock();
203339df232fSStephen Hemminger 	}
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
2036222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
2037222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
20381da177e4SLinus Torvalds {
2039351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
20404e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
20411da177e4SLinus Torvalds 
20424e58a027SCong Wang 	if (pn->pktgen_exiting)
2043e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
2044e9dc8653SEric W. Biederman 
20451da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
20461da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
20471da177e4SLinus Torvalds 	 */
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 	switch (event) {
205039df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
20514e58a027SCong Wang 		pktgen_change_name(pn, dev);
20521da177e4SLinus Torvalds 		break;
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
20554e58a027SCong Wang 		pktgen_mark_device(pn, dev->name);
20561da177e4SLinus Torvalds 		break;
20573ff50b79SStephen Hemminger 	}
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	return NOTIFY_DONE;
20601da177e4SLinus Torvalds }
20611da177e4SLinus Torvalds 
20624e58a027SCong Wang static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
20634e58a027SCong Wang 						 struct pktgen_dev *pkt_dev,
206463adc6fbSStephen Hemminger 						 const char *ifname)
2065e6fce5b9SRobert Olsson {
2066e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
2067d6182223SPaul Gortmaker 	int i;
2068e6fce5b9SRobert Olsson 
2069e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
2070e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
2071e6fce5b9SRobert Olsson 			break;
2072e6fce5b9SRobert Olsson 
2073e6fce5b9SRobert Olsson 		b[i] = ifname[i];
2074e6fce5b9SRobert Olsson 	}
2075e6fce5b9SRobert Olsson 	b[i] = 0;
2076e6fce5b9SRobert Olsson 
20774e58a027SCong Wang 	return dev_get_by_name(pn->net, b);
2078e6fce5b9SRobert Olsson }
2079e6fce5b9SRobert Olsson 
2080e6fce5b9SRobert Olsson 
20811da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
20821da177e4SLinus Torvalds 
20834e58a027SCong Wang static int pktgen_setup_dev(const struct pktgen_net *pn,
20844e58a027SCong Wang 			    struct pktgen_dev *pkt_dev, const char *ifname)
2085222f1806SLuiz Capitulino {
20861da177e4SLinus Torvalds 	struct net_device *odev;
208739df232fSStephen Hemminger 	int err;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	/* Clean old setups */
20901da177e4SLinus Torvalds 	if (pkt_dev->odev) {
20911da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
20921da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
20931da177e4SLinus Torvalds 	}
20941da177e4SLinus Torvalds 
20954e58a027SCong Wang 	odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
20961da177e4SLinus Torvalds 	if (!odev) {
2097f9467eaeSJoe Perches 		pr_err("no such netdevice: \"%s\"\n", ifname);
209839df232fSStephen Hemminger 		return -ENODEV;
20991da177e4SLinus Torvalds 	}
210039df232fSStephen Hemminger 
21011da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
2102f9467eaeSJoe Perches 		pr_err("not an ethernet device: \"%s\"\n", ifname);
210339df232fSStephen Hemminger 		err = -EINVAL;
210439df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
2105f9467eaeSJoe Perches 		pr_err("device is down: \"%s\"\n", ifname);
210639df232fSStephen Hemminger 		err = -ENETDOWN;
210739df232fSStephen Hemminger 	} else {
21081da177e4SLinus Torvalds 		pkt_dev->odev = odev;
210939df232fSStephen Hemminger 		return 0;
211039df232fSStephen Hemminger 	}
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 	dev_put(odev);
211339df232fSStephen Hemminger 	return err;
21141da177e4SLinus Torvalds }
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
21171da177e4SLinus Torvalds  * structure to have the right information to create/send packets
21181da177e4SLinus Torvalds  */
21191da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
21201da177e4SLinus Torvalds {
212164c00d81SAndrew Gallatin 	int ntxq;
212264c00d81SAndrew Gallatin 
21231da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
2124f9467eaeSJoe Perches 		pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2125222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
2126222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
21271da177e4SLinus Torvalds 		return;
21281da177e4SLinus Torvalds 	}
21291da177e4SLinus Torvalds 
213064c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
213164c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2132bfdbc0acSRobert Olsson 
213364c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
2134294a0b7fSJoe Perches 		pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
213588271660SJesse Brandeburg 			pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2136593f63b0SEric Dumazet 			pkt_dev->odevname);
213726e29eedSDan Carpenter 		pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
213864c00d81SAndrew Gallatin 	}
213988271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
2140294a0b7fSJoe Perches 		pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
214188271660SJesse Brandeburg 			pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2142593f63b0SEric Dumazet 			pkt_dev->odevname);
214326e29eedSDan Carpenter 		pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
214464c00d81SAndrew Gallatin 	}
214564c00d81SAndrew Gallatin 
21461da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
21471da177e4SLinus Torvalds 
2148f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
21499ea08b12SJoe Perches 		ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds 	/* Set up Dest MAC */
21529ea08b12SJoe Perches 	ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
21554c139b8cSAmerigo Wang 		int i, set = 0, err = 1;
21564c139b8cSAmerigo Wang 		struct inet6_dev *idev;
21574c139b8cSAmerigo Wang 
215868bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
215968bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
216068bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
216168bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
216268bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
216368bf9f0bSAmerigo Wang 		}
216468bf9f0bSAmerigo Wang 
21651da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
21661da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
21671da177e4SLinus Torvalds 				set = 1;
21681da177e4SLinus Torvalds 				break;
21691da177e4SLinus Torvalds 			}
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 		if (!set) {
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds 			/*
21741da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
21751da177e4SLinus Torvalds 			 *
21761da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
21771da177e4SLinus Torvalds 			 */
21781da177e4SLinus Torvalds 
21798814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
218063adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
218163adc6fbSStephen Hemminger 			if (idev) {
21821da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
21854c139b8cSAmerigo Wang 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
21864c139b8cSAmerigo Wang 					if ((ifp->scope & IFA_LINK) &&
2187f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
21884e3fd7a0SAlexey Dobriyan 						pkt_dev->cur_in6_saddr = ifp->addr;
21891da177e4SLinus Torvalds 						err = 0;
21901da177e4SLinus Torvalds 						break;
21911da177e4SLinus Torvalds 					}
21921da177e4SLinus Torvalds 				}
21931da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
21941da177e4SLinus Torvalds 			}
21958814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2196222f1806SLuiz Capitulino 			if (err)
2197f9467eaeSJoe Perches 				pr_err("ERROR: IPv6 link address not available\n");
21981da177e4SLinus Torvalds 		}
2199222f1806SLuiz Capitulino 	} else {
220068bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
220168bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
220268bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
220368bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
220468bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
220568bf9f0bSAmerigo Wang 		}
220668bf9f0bSAmerigo Wang 
22071da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
22081da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
22091da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
22101da177e4SLinus Torvalds 
22111da177e4SLinus Torvalds 			struct in_device *in_dev;
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 			rcu_read_lock();
2214e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
22151da177e4SLinus Torvalds 			if (in_dev) {
22161da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2217222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2218222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
22191da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
22201da177e4SLinus Torvalds 				}
22211da177e4SLinus Torvalds 			}
22221da177e4SLinus Torvalds 			rcu_read_unlock();
2223222f1806SLuiz Capitulino 		} else {
22241da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
22251da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
22261da177e4SLinus Torvalds 		}
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
22291da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
22301da177e4SLinus Torvalds 	}
22311da177e4SLinus Torvalds 	/* Initialize current values. */
223268bf9f0bSAmerigo Wang 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
223368bf9f0bSAmerigo Wang 	if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
223468bf9f0bSAmerigo Wang 		pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
223568bf9f0bSAmerigo Wang 
22361da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
22371da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
22381da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
22391da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
22401da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
22411da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
22421da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
22431da177e4SLinus Torvalds }
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 
2246fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2247fd29cf72SStephen Hemminger {
2248ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2249417bc4b8SEric Dumazet 	s64 remaining;
22502bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2251fd29cf72SStephen Hemminger 
22522bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
22532bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2254fd29cf72SStephen Hemminger 
225543d28b65SDaniel Turull 	remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2256bcf91bdbSGuenter Roeck 	if (remaining <= 0)
2257bcf91bdbSGuenter Roeck 		goto out;
22582bc481cfSStephen Hemminger 
2259398f382cSDaniel Borkmann 	start_time = ktime_get();
226033136d12SEric Dumazet 	if (remaining < 100000) {
226133136d12SEric Dumazet 		/* for small delays (<100us), just loop until limit is reached */
226233136d12SEric Dumazet 		do {
2263398f382cSDaniel Borkmann 			end_time = ktime_get();
2264398f382cSDaniel Borkmann 		} while (ktime_compare(end_time, spin_until) < 0);
226533136d12SEric Dumazet 	} else {
22662bc481cfSStephen Hemminger 		/* see do_nanosleep */
22672bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
22682bc481cfSStephen Hemminger 		do {
22692bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
22702bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
22712bc481cfSStephen Hemminger 
22722bc481cfSStephen Hemminger 			if (likely(t.task))
22731da177e4SLinus Torvalds 				schedule();
22741da177e4SLinus Torvalds 
22752bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
22762bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
22772bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
2278398f382cSDaniel Borkmann 		end_time = ktime_get();
227933136d12SEric Dumazet 	}
2280ef87979cSStephen Hemminger 
2281ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2282bcf91bdbSGuenter Roeck out:
228307a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2284bcf91bdbSGuenter Roeck 	destroy_hrtimer_on_stack(&t.timer);
22851da177e4SLinus Torvalds }
22861da177e4SLinus Torvalds 
228716dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
228816dab72fSJamal Hadi Salim {
2289*63d75463SPaolo Abeni 	pkt_dev->pkt_overhead = 0;
229016dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
229116dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
229216dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
229316dab72fSJamal Hadi Salim }
229416dab72fSJamal Hadi Salim 
2295648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2296007a531bSJamal Hadi Salim {
2297648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2298007a531bSJamal Hadi Salim }
2299007a531bSJamal Hadi Salim 
2300007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2301007a531bSJamal Hadi Salim {
2302007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2303007a531bSJamal Hadi Salim 
2304007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2305007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2306007a531bSJamal Hadi Salim 			/* reset time */
2307007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
23081211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2309007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2310007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2311007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2312007a531bSJamal Hadi Salim 		}
2313007a531bSJamal Hadi Salim 	} else {
231433d7c5e5SAkinobu Mita 		flow = prandom_u32() % pkt_dev->cflows;
23151211a645SRobert Olsson 		pkt_dev->curfl = flow;
2316007a531bSJamal Hadi Salim 
23171211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2318007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
23191211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
23201211a645SRobert Olsson 		}
2321007a531bSJamal Hadi Salim 	}
2322007a531bSJamal Hadi Salim 
2323007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2324007a531bSJamal Hadi Salim }
2325007a531bSJamal Hadi Salim 
2326a553e4a6SJamal Hadi Salim 
2327a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2328a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2329a553e4a6SJamal Hadi Salim  * we go look for it ...
2330a553e4a6SJamal Hadi Salim */
2331bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2332fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2333a553e4a6SJamal Hadi Salim {
2334a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
23354e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2336a553e4a6SJamal Hadi Salim 	if (!x) {
2337c454997eSFan Du 
2338c454997eSFan Du 		if (pkt_dev->spi) {
2339c454997eSFan Du 			/* We need as quick as possible to find the right SA
2340c454997eSFan Du 			 * Searching with minimum criteria to archieve this.
2341c454997eSFan Du 			 */
2342c454997eSFan Du 			x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2343c454997eSFan Du 		} else {
2344a553e4a6SJamal Hadi Salim 			/* slow path: we dont already have xfrm_state */
23454e58a027SCong Wang 			x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
23465447c5e4SAlexey Dobriyan 						(xfrm_address_t *)&pkt_dev->cur_daddr,
2347a553e4a6SJamal Hadi Salim 						(xfrm_address_t *)&pkt_dev->cur_saddr,
2348a553e4a6SJamal Hadi Salim 						AF_INET,
2349a553e4a6SJamal Hadi Salim 						pkt_dev->ipsmode,
2350a553e4a6SJamal Hadi Salim 						pkt_dev->ipsproto, 0);
2351c454997eSFan Du 		}
2352a553e4a6SJamal Hadi Salim 		if (x) {
2353a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2354a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2355a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2356a553e4a6SJamal Hadi Salim 		}
2357a553e4a6SJamal Hadi Salim 
2358a553e4a6SJamal Hadi Salim 	}
2359a553e4a6SJamal Hadi Salim }
2360a553e4a6SJamal Hadi Salim #endif
2361fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2362fd2ea0a7SDavid S. Miller {
2363e6fce5b9SRobert Olsson 
2364e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2365e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2366e6fce5b9SRobert Olsson 
2367896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2368fd2ea0a7SDavid S. Miller 		__u16 t;
2369fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
237033d7c5e5SAkinobu Mita 			t = prandom_u32() %
2371fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2372fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2373fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2374fd2ea0a7SDavid S. Miller 		} else {
2375fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2376fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2377fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2378fd2ea0a7SDavid S. Miller 		}
2379fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2380fd2ea0a7SDavid S. Miller 	}
2381bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2382fd2ea0a7SDavid S. Miller }
2383fd2ea0a7SDavid S. Miller 
23841da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
23851da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
23861da177e4SLinus Torvalds  */
2387222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2388222f1806SLuiz Capitulino {
23891da177e4SLinus Torvalds 	__u32 imn;
23901da177e4SLinus Torvalds 	__u32 imx;
23911da177e4SLinus Torvalds 	int flow = 0;
23921da177e4SLinus Torvalds 
2393007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2394007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 	/*  Deal with source MAC */
23971da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
23981da177e4SLinus Torvalds 		__u32 mc;
23991da177e4SLinus Torvalds 		__u32 tmp;
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
240233d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->src_mac_count;
24031da177e4SLinus Torvalds 		else {
24041da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2405ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2406222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
24071da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
24081da177e4SLinus Torvalds 		}
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
24111da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
24121da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
24131da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
24141da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
24151da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
24161da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
24171da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
24181da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
24191da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
24201da177e4SLinus Torvalds 	}
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
24231da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
24241da177e4SLinus Torvalds 		__u32 mc;
24251da177e4SLinus Torvalds 		__u32 tmp;
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
242833d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->dst_mac_count;
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 		else {
24311da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2432ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2433222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
24341da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
24351da177e4SLinus Torvalds 			}
24361da177e4SLinus Torvalds 		}
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
24391da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
24401da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
24411da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
24421da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
24431da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
24441da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
24451da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
24461da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
24471da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
24481da177e4SLinus Torvalds 	}
24491da177e4SLinus Torvalds 
2450ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
245195c96174SEric Dumazet 		unsigned int i;
2452ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2453ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2454ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
245533d7c5e5SAkinobu Mita 					     ((__force __be32)prandom_u32() &
2456ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2457ca6549afSSteven Whitehouse 	}
2458ca6549afSSteven Whitehouse 
245934954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
246033d7c5e5SAkinobu Mita 		pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
246134954ddcSFrancesco Fondelli 	}
246234954ddcSFrancesco Fondelli 
246334954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
246433d7c5e5SAkinobu Mita 		pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
246534954ddcSFrancesco Fondelli 	}
246634954ddcSFrancesco Fondelli 
24671da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
24681da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
246933d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_src = prandom_u32() %
24705fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
24715fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 		else {
24741da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
24751da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
24761da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
24771da177e4SLinus Torvalds 		}
24781da177e4SLinus Torvalds 	}
24791da177e4SLinus Torvalds 
24801da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
24811da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
248233d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_dst = prandom_u32() %
24835fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
24845fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2485222f1806SLuiz Capitulino 		} else {
24861da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
24871da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
24881da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
24891da177e4SLinus Torvalds 		}
24901da177e4SLinus Torvalds 	}
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
24931da177e4SLinus Torvalds 
249463adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
249563adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
249663adc6fbSStephen Hemminger 		if (imn < imx) {
24971da177e4SLinus Torvalds 			__u32 t;
24981da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
249933d7c5e5SAkinobu Mita 				t = prandom_u32() % (imx - imn) + imn;
25001da177e4SLinus Torvalds 			else {
25011da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
25021da177e4SLinus Torvalds 				t++;
250363adc6fbSStephen Hemminger 				if (t > imx)
25041da177e4SLinus Torvalds 					t = imn;
250563adc6fbSStephen Hemminger 
25061da177e4SLinus Torvalds 			}
25071da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
25081da177e4SLinus Torvalds 		}
25091da177e4SLinus Torvalds 
2510007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
25111da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
25121da177e4SLinus Torvalds 		} else {
2513252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2514252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2515252e3346SAl Viro 			if (imn < imx) {
25161da177e4SLinus Torvalds 				__u32 t;
2517252e3346SAl Viro 				__be32 s;
25181da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
25191da177e4SLinus Torvalds 
252070e3ba72SAkinobu Mita 					do {
252133d7c5e5SAkinobu Mita 						t = prandom_u32() %
252233d7c5e5SAkinobu Mita 							(imx - imn) + imn;
2523252e3346SAl Viro 						s = htonl(t);
252470e3ba72SAkinobu Mita 					} while (ipv4_is_loopback(s) ||
252570e3ba72SAkinobu Mita 						ipv4_is_multicast(s) ||
252670e3ba72SAkinobu Mita 						ipv4_is_lbcast(s) ||
252770e3ba72SAkinobu Mita 						ipv4_is_zeronet(s) ||
252870e3ba72SAkinobu Mita 						ipv4_is_local_multicast(s));
2529252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2530252e3346SAl Viro 				} else {
25311da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
25321da177e4SLinus Torvalds 					t++;
25331da177e4SLinus Torvalds 					if (t > imx) {
25341da177e4SLinus Torvalds 						t = imn;
25351da177e4SLinus Torvalds 					}
25361da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
25371da177e4SLinus Torvalds 				}
25381da177e4SLinus Torvalds 			}
25391da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2540007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2541222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2542222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2543a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2544a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2545a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2546a553e4a6SJamal Hadi Salim #endif
25471da177e4SLinus Torvalds 				pkt_dev->nflows++;
25481da177e4SLinus Torvalds 			}
25491da177e4SLinus Torvalds 		}
2550222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2551222f1806SLuiz Capitulino 
255206e30411SJoe Perches 		if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
25531da177e4SLinus Torvalds 			int i;
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 			/* Only random destinations yet */
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
25581da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
255933d7c5e5SAkinobu Mita 				    (((__force __be32)prandom_u32() |
25601da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
25611da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
25621da177e4SLinus Torvalds 			}
25631da177e4SLinus Torvalds 		}
25641da177e4SLinus Torvalds 	}
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
25671da177e4SLinus Torvalds 		__u32 t;
25681da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
256933d7c5e5SAkinobu Mita 			t = prandom_u32() %
25705fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
25715fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2572222f1806SLuiz Capitulino 		} else {
25731da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
25741da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
25751da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
25761da177e4SLinus Torvalds 		}
25771da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
25781da177e4SLinus Torvalds 	}
25791da177e4SLinus Torvalds 
2580fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
258145b270f8SRobert Olsson 
25821da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
25831da177e4SLinus Torvalds }
25841da177e4SLinus Torvalds 
2585a553e4a6SJamal Hadi Salim 
2586a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
25875537a055SFengguang Wu static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2588cf93d47eSFan Du 
2589cf93d47eSFan Du 	[RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2590cf93d47eSFan Du };
2591cf93d47eSFan Du 
2592a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2593a553e4a6SJamal Hadi Salim {
2594a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2595a553e4a6SJamal Hadi Salim 	int err = 0;
25966de9ace4SFan Du 	struct net *net = dev_net(pkt_dev->odev);
2597a553e4a6SJamal Hadi Salim 
2598a553e4a6SJamal Hadi Salim 	if (!x)
2599a553e4a6SJamal Hadi Salim 		return 0;
2600a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2601a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2602cf93d47eSFan Du 	if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2603a553e4a6SJamal Hadi Salim 		return 0;
2604a553e4a6SJamal Hadi Salim 
2605cf93d47eSFan Du 	/* But when user specify an valid SPI, transformation
2606cf93d47eSFan Du 	 * supports both transport/tunnel mode + ESP/AH type.
2607cf93d47eSFan Du 	 */
2608cf93d47eSFan Du 	if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2609cf93d47eSFan Du 		skb->_skb_refdst = (unsigned long)&pkt_dev->dst | SKB_DST_NOREF;
2610cf93d47eSFan Du 
2611cf93d47eSFan Du 	rcu_read_lock_bh();
261213996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2613cf93d47eSFan Du 	rcu_read_unlock_bh();
26146de9ace4SFan Du 	if (err) {
26156de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2616a553e4a6SJamal Hadi Salim 		goto error;
26176de9ace4SFan Du 	}
2618a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
26196de9ace4SFan Du 	if (err) {
26206de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2621a553e4a6SJamal Hadi Salim 		goto error;
26226de9ace4SFan Du 	}
26230af0a413SFan Du 	spin_lock_bh(&x->lock);
2624a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2625a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
26260af0a413SFan Du 	spin_unlock_bh(&x->lock);
2627a553e4a6SJamal Hadi Salim error:
2628a553e4a6SJamal Hadi Salim 	return err;
2629a553e4a6SJamal Hadi Salim }
2630a553e4a6SJamal Hadi Salim 
2631475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2632a553e4a6SJamal Hadi Salim {
2633a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2634a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2635d6182223SPaul Gortmaker 		int i;
2636d6182223SPaul Gortmaker 		for (i = 0; i < pkt_dev->cflows; i++) {
2637a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2638a553e4a6SJamal Hadi Salim 			if (x) {
2639a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2640a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2641a553e4a6SJamal Hadi Salim 			}
2642a553e4a6SJamal Hadi Salim 		}
2643a553e4a6SJamal Hadi Salim 	}
2644a553e4a6SJamal Hadi Salim }
2645a553e4a6SJamal Hadi Salim 
2646475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2647a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2648a553e4a6SJamal Hadi Salim {
2649a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2650a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2651a553e4a6SJamal Hadi Salim 		int nhead = 0;
2652a553e4a6SJamal Hadi Salim 		if (x) {
2653d4969581SEric Dumazet 			struct ethhdr *eth;
26543868204dSfan.du 			struct iphdr *iph;
2655d4969581SEric Dumazet 			int ret;
26563868204dSfan.du 
2657a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2658a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2659a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2660a553e4a6SJamal Hadi Salim 				if (ret < 0) {
2661f9467eaeSJoe Perches 					pr_err("Error expanding ipsec packet %d\n",
2662f9467eaeSJoe Perches 					       ret);
2663b4bb4ac8SIlpo Järvinen 					goto err;
2664a553e4a6SJamal Hadi Salim 				}
2665a553e4a6SJamal Hadi Salim 			}
2666a553e4a6SJamal Hadi Salim 
2667a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2668a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2669a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2670a553e4a6SJamal Hadi Salim 			if (ret) {
2671f9467eaeSJoe Perches 				pr_err("Error creating ipsec packet %d\n", ret);
2672b4bb4ac8SIlpo Järvinen 				goto err;
2673a553e4a6SJamal Hadi Salim 			}
2674a553e4a6SJamal Hadi Salim 			/* restore ll */
2675d4969581SEric Dumazet 			eth = (struct ethhdr *)skb_push(skb, ETH_HLEN);
2676d4969581SEric Dumazet 			memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2677d4969581SEric Dumazet 			eth->h_proto = protocol;
26783868204dSfan.du 
26793868204dSfan.du 			/* Update IPv4 header len as well as checksum value */
26803868204dSfan.du 			iph = ip_hdr(skb);
26813868204dSfan.du 			iph->tot_len = htons(skb->len - ETH_HLEN);
26823868204dSfan.du 			ip_send_check(iph);
2683a553e4a6SJamal Hadi Salim 		}
2684a553e4a6SJamal Hadi Salim 	}
2685a553e4a6SJamal Hadi Salim 	return 1;
2686b4bb4ac8SIlpo Järvinen err:
2687b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2688b4bb4ac8SIlpo Järvinen 	return 0;
2689a553e4a6SJamal Hadi Salim }
2690a553e4a6SJamal Hadi Salim #endif
2691a553e4a6SJamal Hadi Salim 
2692ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2693ca6549afSSteven Whitehouse {
269495c96174SEric Dumazet 	unsigned int i;
269563adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2696ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
269763adc6fbSStephen Hemminger 
2698ca6549afSSteven Whitehouse 	mpls--;
2699ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2700ca6549afSSteven Whitehouse }
2701ca6549afSSteven Whitehouse 
27020f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
27030f37c605SAl Viro 			       unsigned int prio)
27040f37c605SAl Viro {
27050f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
27060f37c605SAl Viro }
27070f37c605SAl Viro 
270826ad7879SEric Dumazet static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
270926ad7879SEric Dumazet 				int datalen)
271026ad7879SEric Dumazet {
271126ad7879SEric Dumazet 	struct timeval timestamp;
271226ad7879SEric Dumazet 	struct pktgen_hdr *pgh;
271326ad7879SEric Dumazet 
271426ad7879SEric Dumazet 	pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
271526ad7879SEric Dumazet 	datalen -= sizeof(*pgh);
271626ad7879SEric Dumazet 
271726ad7879SEric Dumazet 	if (pkt_dev->nfrags <= 0) {
271805aebe2eSDaniel Turull 		memset(skb_put(skb, datalen), 0, datalen);
271926ad7879SEric Dumazet 	} else {
272026ad7879SEric Dumazet 		int frags = pkt_dev->nfrags;
272126ad7879SEric Dumazet 		int i, len;
27227d36a991Samit salecha 		int frag_len;
272326ad7879SEric Dumazet 
272426ad7879SEric Dumazet 
272526ad7879SEric Dumazet 		if (frags > MAX_SKB_FRAGS)
272626ad7879SEric Dumazet 			frags = MAX_SKB_FRAGS;
272726ad7879SEric Dumazet 		len = datalen - frags * PAGE_SIZE;
272826ad7879SEric Dumazet 		if (len > 0) {
272926ad7879SEric Dumazet 			memset(skb_put(skb, len), 0, len);
273026ad7879SEric Dumazet 			datalen = frags * PAGE_SIZE;
273126ad7879SEric Dumazet 		}
273226ad7879SEric Dumazet 
273326ad7879SEric Dumazet 		i = 0;
27347d36a991Samit salecha 		frag_len = (datalen/frags) < PAGE_SIZE ?
27357d36a991Samit salecha 			   (datalen/frags) : PAGE_SIZE;
273626ad7879SEric Dumazet 		while (datalen > 0) {
273726ad7879SEric Dumazet 			if (unlikely(!pkt_dev->page)) {
273826ad7879SEric Dumazet 				int node = numa_node_id();
273926ad7879SEric Dumazet 
274026ad7879SEric Dumazet 				if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
274126ad7879SEric Dumazet 					node = pkt_dev->node;
274226ad7879SEric Dumazet 				pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
274326ad7879SEric Dumazet 				if (!pkt_dev->page)
274426ad7879SEric Dumazet 					break;
274526ad7879SEric Dumazet 			}
2746a0bec1cdSIan Campbell 			get_page(pkt_dev->page);
2747ea2ab693SIan Campbell 			skb_frag_set_page(skb, i, pkt_dev->page);
274826ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page_offset = 0;
27497d36a991Samit salecha 			/*last fragment, fill rest of data*/
27507d36a991Samit salecha 			if (i == (frags - 1))
27519e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i],
27529e903e08SEric Dumazet 				    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
27537d36a991Samit salecha 			else
27549e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
27559e903e08SEric Dumazet 			datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
27569e903e08SEric Dumazet 			skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
27579e903e08SEric Dumazet 			skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
275826ad7879SEric Dumazet 			i++;
275926ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
276026ad7879SEric Dumazet 		}
276126ad7879SEric Dumazet 	}
276226ad7879SEric Dumazet 
276326ad7879SEric Dumazet 	/* Stamp the time, and sequence number,
276426ad7879SEric Dumazet 	 * convert them to network byte order
276526ad7879SEric Dumazet 	 */
276626ad7879SEric Dumazet 	pgh->pgh_magic = htonl(PKTGEN_MAGIC);
276726ad7879SEric Dumazet 	pgh->seq_num = htonl(pkt_dev->seq_num);
276826ad7879SEric Dumazet 
2769afb84b62SJesper Dangaard Brouer 	if (pkt_dev->flags & F_NO_TIMESTAMP) {
2770afb84b62SJesper Dangaard Brouer 		pgh->tv_sec = 0;
2771afb84b62SJesper Dangaard Brouer 		pgh->tv_usec = 0;
2772afb84b62SJesper Dangaard Brouer 	} else {
277326ad7879SEric Dumazet 		do_gettimeofday(&timestamp);
277426ad7879SEric Dumazet 		pgh->tv_sec = htonl(timestamp.tv_sec);
277526ad7879SEric Dumazet 		pgh->tv_usec = htonl(timestamp.tv_usec);
277626ad7879SEric Dumazet 	}
2777afb84b62SJesper Dangaard Brouer }
277826ad7879SEric Dumazet 
27797a6e288dSDaniel Borkmann static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2780*63d75463SPaolo Abeni 					struct pktgen_dev *pkt_dev)
27817a6e288dSDaniel Borkmann {
2782*63d75463SPaolo Abeni 	unsigned int extralen = LL_RESERVED_SPACE(dev);
27837a6e288dSDaniel Borkmann 	struct sk_buff *skb = NULL;
2784*63d75463SPaolo Abeni 	unsigned int size;
27857a6e288dSDaniel Borkmann 
2786*63d75463SPaolo Abeni 	size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
27877a6e288dSDaniel Borkmann 	if (pkt_dev->flags & F_NODE) {
27887a6e288dSDaniel Borkmann 		int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
27897a6e288dSDaniel Borkmann 
27907a6e288dSDaniel Borkmann 		skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
27917a6e288dSDaniel Borkmann 		if (likely(skb)) {
27927a6e288dSDaniel Borkmann 			skb_reserve(skb, NET_SKB_PAD);
27937a6e288dSDaniel Borkmann 			skb->dev = dev;
27947a6e288dSDaniel Borkmann 		}
27957a6e288dSDaniel Borkmann 	} else {
27967a6e288dSDaniel Borkmann 		 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
27977a6e288dSDaniel Borkmann 	}
27983de03596SJohn Fastabend 
2799*63d75463SPaolo Abeni 	/* the caller pre-fetches from skb->data and reserves for the mac hdr */
28003de03596SJohn Fastabend 	if (likely(skb))
2801*63d75463SPaolo Abeni 		skb_reserve(skb, extralen - 16);
28027a6e288dSDaniel Borkmann 
28037a6e288dSDaniel Borkmann 	return skb;
28047a6e288dSDaniel Borkmann }
28057a6e288dSDaniel Borkmann 
28061da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
28071da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
28081da177e4SLinus Torvalds {
28091da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
28101da177e4SLinus Torvalds 	__u8 *eth;
28111da177e4SLinus Torvalds 	struct udphdr *udph;
28121da177e4SLinus Torvalds 	int datalen, iplen;
28131da177e4SLinus Torvalds 	struct iphdr *iph;
2814d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2815ca6549afSSteven Whitehouse 	__be32 *mpls;
281634954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
281734954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
281834954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
281934954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2820fd2ea0a7SDavid S. Miller 	u16 queue_map;
2821ca6549afSSteven Whitehouse 
2822ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2823d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
28241da177e4SLinus Torvalds 
282534954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2826d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
282734954ddcSFrancesco Fondelli 
282864053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
282964053beeSRobert Olsson 	 * fields.
283064053beeSRobert Olsson 	 */
283164053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2832eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
283364053beeSRobert Olsson 
2834*63d75463SPaolo Abeni 	skb = pktgen_alloc_skb(odev, pkt_dev);
28351da177e4SLinus Torvalds 	if (!skb) {
28361da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
28371da177e4SLinus Torvalds 		return NULL;
28381da177e4SLinus Torvalds 	}
28391da177e4SLinus Torvalds 
28407a6e288dSDaniel Borkmann 	prefetchw(skb->data);
2841*63d75463SPaolo Abeni 	skb_reserve(skb, 16);
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
28441da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2845ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2846ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2847ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
284834954ddcSFrancesco Fondelli 
284934954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
285034954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
285134954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28520f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
28530f37c605SAl Viro 					       pkt_dev->svlan_cfi,
28540f37c605SAl Viro 					       pkt_dev->svlan_p);
285534954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2856d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
285734954ddcSFrancesco Fondelli 		}
285834954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28590f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
28600f37c605SAl Viro 				      pkt_dev->vlan_cfi,
28610f37c605SAl Viro 				      pkt_dev->vlan_p);
286234954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2863d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
286434954ddcSFrancesco Fondelli 	}
286534954ddcSFrancesco Fondelli 
2866c145aeb3SZhang Shengju 	skb_reset_mac_header(skb);
2867525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2868525cebedSThomas Graf 	iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2869525cebedSThomas Graf 
2870525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
2871525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
2872fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
28739e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
28749e50e3acSJohn Fastabend 
28751da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2876252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
28771da177e4SLinus Torvalds 
2878ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2879ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
288016dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
28816af773e7SNishank Trivedi 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
28821da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
28851da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
28861da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
2887c26bf4a5SThomas Graf 	udph->check = 0;
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	iph->ihl = 5;
28901da177e4SLinus Torvalds 	iph->version = 4;
28911da177e4SLinus Torvalds 	iph->ttl = 32;
28921ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
28931da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
28941da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
28951da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
289666ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
289766ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
28981da177e4SLinus Torvalds 	iph->frag_off = 0;
28991da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
29001da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
290103c633e7SThomas Graf 	ip_send_check(iph);
2902ca6549afSSteven Whitehouse 	skb->protocol = protocol;
29031da177e4SLinus Torvalds 	skb->dev = odev;
29041da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
2905c26bf4a5SThomas Graf 
29067744b5f3SSabrina Dubroca 	pktgen_finalize_skb(pkt_dev, skb, datalen);
29077744b5f3SSabrina Dubroca 
2908c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2909c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2910c8cd0989STom Herbert 	} else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
2911c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2912c26bf4a5SThomas Graf 		skb->csum = 0;
29137744b5f3SSabrina Dubroca 		udp4_hwcsum(skb, iph->saddr, iph->daddr);
2914c26bf4a5SThomas Graf 	} else {
29157744b5f3SSabrina Dubroca 		__wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
2916c26bf4a5SThomas Graf 
2917c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
29187744b5f3SSabrina Dubroca 		udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
2919c26bf4a5SThomas Graf 						datalen + 8, IPPROTO_UDP, csum);
2920c26bf4a5SThomas Graf 
2921c26bf4a5SThomas Graf 		if (udph->check == 0)
2922c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2923c26bf4a5SThomas Graf 	}
2924c26bf4a5SThomas Graf 
2925a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2926a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2927a553e4a6SJamal Hadi Salim 		return NULL;
2928a553e4a6SJamal Hadi Salim #endif
2929a553e4a6SJamal Hadi Salim 
29301da177e4SLinus Torvalds 	return skb;
29311da177e4SLinus Torvalds }
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
29341da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
29351da177e4SLinus Torvalds {
29361da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
29371da177e4SLinus Torvalds 	__u8 *eth;
29381da177e4SLinus Torvalds 	struct udphdr *udph;
2939c26bf4a5SThomas Graf 	int datalen, udplen;
29401da177e4SLinus Torvalds 	struct ipv6hdr *iph;
2941d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2942ca6549afSSteven Whitehouse 	__be32 *mpls;
294334954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
294434954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
294534954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
294634954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2947fd2ea0a7SDavid S. Miller 	u16 queue_map;
2948ca6549afSSteven Whitehouse 
2949ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2950d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
29511da177e4SLinus Torvalds 
295234954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2953d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
295434954ddcSFrancesco Fondelli 
295564053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
295664053beeSRobert Olsson 	 * fields.
295764053beeSRobert Olsson 	 */
295864053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2959eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
296064053beeSRobert Olsson 
2961*63d75463SPaolo Abeni 	skb = pktgen_alloc_skb(odev, pkt_dev);
29621da177e4SLinus Torvalds 	if (!skb) {
29631da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
29641da177e4SLinus Torvalds 		return NULL;
29651da177e4SLinus Torvalds 	}
29661da177e4SLinus Torvalds 
29677a6e288dSDaniel Borkmann 	prefetchw(skb->data);
29681da177e4SLinus Torvalds 	skb_reserve(skb, 16);
29691da177e4SLinus Torvalds 
29701da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
29711da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2972ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2973ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2974ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
297534954ddcSFrancesco Fondelli 
297634954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
297734954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
297834954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29790f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
29800f37c605SAl Viro 					       pkt_dev->svlan_cfi,
29810f37c605SAl Viro 					       pkt_dev->svlan_p);
298234954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2983d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
298434954ddcSFrancesco Fondelli 		}
298534954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29860f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
29870f37c605SAl Viro 				      pkt_dev->vlan_cfi,
29880f37c605SAl Viro 				      pkt_dev->vlan_p);
298934954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2990d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
299134954ddcSFrancesco Fondelli 	}
299234954ddcSFrancesco Fondelli 
2993c145aeb3SZhang Shengju 	skb_reset_mac_header(skb);
2994525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2995525cebedSThomas Graf 	iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2996525cebedSThomas Graf 
2997525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
2998525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
2999fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
30009e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
3003252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
30041da177e4SLinus Torvalds 
3005ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
3006ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
3007ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
300816dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
30091da177e4SLinus Torvalds 
30105aa8b572SAmerigo Wang 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
30111da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
3012e87cc472SJoe Perches 		net_info_ratelimited("increased datalen to %d\n", datalen);
30131da177e4SLinus Torvalds 	}
30141da177e4SLinus Torvalds 
3015c26bf4a5SThomas Graf 	udplen = datalen + sizeof(struct udphdr);
30161da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
30171da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
3018c26bf4a5SThomas Graf 	udph->len = htons(udplen);
3019c26bf4a5SThomas Graf 	udph->check = 0;
30201da177e4SLinus Torvalds 
3021d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
30221da177e4SLinus Torvalds 
30231ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
30241ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
3025252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
30261ca7768cSFrancesco Fondelli 	}
30271ca7768cSFrancesco Fondelli 
30281da177e4SLinus Torvalds 	iph->hop_limit = 32;
30291da177e4SLinus Torvalds 
3030c26bf4a5SThomas Graf 	iph->payload_len = htons(udplen);
30311da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
30321da177e4SLinus Torvalds 
30334e3fd7a0SAlexey Dobriyan 	iph->daddr = pkt_dev->cur_in6_daddr;
30344e3fd7a0SAlexey Dobriyan 	iph->saddr = pkt_dev->cur_in6_saddr;
30351da177e4SLinus Torvalds 
3036ca6549afSSteven Whitehouse 	skb->protocol = protocol;
30371da177e4SLinus Torvalds 	skb->dev = odev;
30381da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
30391da177e4SLinus Torvalds 
30407744b5f3SSabrina Dubroca 	pktgen_finalize_skb(pkt_dev, skb, datalen);
30417744b5f3SSabrina Dubroca 
3042c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
3043c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
3044c8cd0989STom Herbert 	} else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
3045c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
3046c26bf4a5SThomas Graf 		skb->csum_start = skb_transport_header(skb) - skb->head;
3047c26bf4a5SThomas Graf 		skb->csum_offset = offsetof(struct udphdr, check);
3048c26bf4a5SThomas Graf 		udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
3049c26bf4a5SThomas Graf 	} else {
30507744b5f3SSabrina Dubroca 		__wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
3051c26bf4a5SThomas Graf 
3052c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
3053c26bf4a5SThomas Graf 		udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
3054c26bf4a5SThomas Graf 
3055c26bf4a5SThomas Graf 		if (udph->check == 0)
3056c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
3057c26bf4a5SThomas Graf 	}
3058c26bf4a5SThomas Graf 
30591da177e4SLinus Torvalds 	return skb;
30601da177e4SLinus Torvalds }
30611da177e4SLinus Torvalds 
3062475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
30631da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
30641da177e4SLinus Torvalds {
30651da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
30661da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
30671da177e4SLinus Torvalds 	else
30681da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
30691da177e4SLinus Torvalds }
30701da177e4SLinus Torvalds 
30711da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
30721da177e4SLinus Torvalds {
30731da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
30741da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
30751da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
30761da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
30771da177e4SLinus Torvalds 	pkt_dev->errors = 0;
30781da177e4SLinus Torvalds }
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
30831da177e4SLinus Torvalds {
3084c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
30851da177e4SLinus Torvalds 	int started = 0;
30861da177e4SLinus Torvalds 
3087f9467eaeSJoe Perches 	func_enter();
30881da177e4SLinus Torvalds 
30898788370aSJesper Dangaard Brouer 	rcu_read_lock();
30908788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
30911da177e4SLinus Torvalds 
30921da177e4SLinus Torvalds 		/*
30931da177e4SLinus Torvalds 		 * setup odev and create initial packet.
30941da177e4SLinus Torvalds 		 */
30951da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds 		if (pkt_dev->odev) {
30981da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
30991da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
3100398f382cSDaniel Borkmann 			pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3101fd29cf72SStephen Hemminger 
310216dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
31031da177e4SLinus Torvalds 
31041da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
31058788370aSJesper Dangaard Brouer 			pkt_dev->running = 1;	/* Cranke yeself! */
31061da177e4SLinus Torvalds 			started++;
3107222f1806SLuiz Capitulino 		} else
31081da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
31091da177e4SLinus Torvalds 	}
31108788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3111222f1806SLuiz Capitulino 	if (started)
3112222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
31131da177e4SLinus Torvalds }
31141da177e4SLinus Torvalds 
31154e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
31161da177e4SLinus Torvalds {
3117cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31181da177e4SLinus Torvalds 
3119f9467eaeSJoe Perches 	func_enter();
31201da177e4SLinus Torvalds 
31216146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3122cdcdbe0bSLuiz Capitulino 
31234e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
312495ed63f7SArthur Kepner 		t->control |= T_STOP;
3125cdcdbe0bSLuiz Capitulino 
31266146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31271da177e4SLinus Torvalds }
31281da177e4SLinus Torvalds 
3129648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
31301da177e4SLinus Torvalds {
3131648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
31321da177e4SLinus Torvalds 
31338788370aSJesper Dangaard Brouer 	rcu_read_lock();
31348788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
31358788370aSJesper Dangaard Brouer 		if (pkt_dev->running) {
31368788370aSJesper Dangaard Brouer 			rcu_read_unlock();
3137648fda74SStephen Hemminger 			return 1;
31388788370aSJesper Dangaard Brouer 		}
31398788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3140648fda74SStephen Hemminger 	return 0;
31411da177e4SLinus Torvalds }
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
31441da177e4SLinus Torvalds {
31451da177e4SLinus Torvalds 	while (thread_is_running(t)) {
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 		msleep_interruptible(100);
31481da177e4SLinus Torvalds 
31491da177e4SLinus Torvalds 		if (signal_pending(current))
31501da177e4SLinus Torvalds 			goto signal;
31511da177e4SLinus Torvalds 	}
31521da177e4SLinus Torvalds 	return 1;
31531da177e4SLinus Torvalds signal:
31541da177e4SLinus Torvalds 	return 0;
31551da177e4SLinus Torvalds }
31561da177e4SLinus Torvalds 
31574e58a027SCong Wang static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
31581da177e4SLinus Torvalds {
3159cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31601da177e4SLinus Torvalds 	int sig = 1;
31611da177e4SLinus Torvalds 
31626146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3163cdcdbe0bSLuiz Capitulino 
31644e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
31651da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
3166222f1806SLuiz Capitulino 		if (sig == 0)
3167222f1806SLuiz Capitulino 			break;
31681da177e4SLinus Torvalds 	}
3169cdcdbe0bSLuiz Capitulino 
3170cdcdbe0bSLuiz Capitulino 	if (sig == 0)
31714e58a027SCong Wang 		list_for_each_entry(t, &pn->pktgen_threads, th_list)
31721da177e4SLinus Torvalds 			t->control |= (T_STOP);
3173cdcdbe0bSLuiz Capitulino 
31746146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31751da177e4SLinus Torvalds 	return sig;
31761da177e4SLinus Torvalds }
31771da177e4SLinus Torvalds 
31784e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn)
31791da177e4SLinus Torvalds {
3180cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31811da177e4SLinus Torvalds 
3182f9467eaeSJoe Perches 	func_enter();
31831da177e4SLinus Torvalds 
31846146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
31851da177e4SLinus Torvalds 
31864e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
31871da177e4SLinus Torvalds 		t->control |= (T_RUN);
3188cdcdbe0bSLuiz Capitulino 
31896146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31901da177e4SLinus Torvalds 
319163adc6fbSStephen Hemminger 	/* Propagate thread->control  */
319263adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
31931da177e4SLinus Torvalds 
31944e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
31951da177e4SLinus Torvalds }
31961da177e4SLinus Torvalds 
31974e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn)
3198eb37b41cSJesse Brandeburg {
3199eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3200eb37b41cSJesse Brandeburg 
3201f9467eaeSJoe Perches 	func_enter();
3202eb37b41cSJesse Brandeburg 
3203eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3204eb37b41cSJesse Brandeburg 
32054e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
3206eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3207eb37b41cSJesse Brandeburg 
3208eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3209eb37b41cSJesse Brandeburg 
321063adc6fbSStephen Hemminger 	/* Propagate thread->control  */
321163adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3212eb37b41cSJesse Brandeburg 
32134e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
3214eb37b41cSJesse Brandeburg }
3215eb37b41cSJesse Brandeburg 
32161da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
32171da177e4SLinus Torvalds {
3218fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
32191da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3220fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3221fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3222fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
32231da177e4SLinus Torvalds 
322403a14ab1SDaniel Turull 	p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3225fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3226fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3227fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
32281da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
32291da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
32301da177e4SLinus Torvalds 
3231fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3232fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
32331da177e4SLinus Torvalds 
32341da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds 	mbps = bps;
32371da177e4SLinus Torvalds 	do_div(mbps, 1000000);
32381da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
32391da177e4SLinus Torvalds 		     (unsigned long long)pps,
32401da177e4SLinus Torvalds 		     (unsigned long long)mbps,
32411da177e4SLinus Torvalds 		     (unsigned long long)bps,
32421da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
32431da177e4SLinus Torvalds }
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
32461da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
32471da177e4SLinus Torvalds {
3248222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
32491da177e4SLinus Torvalds 
32501da177e4SLinus Torvalds 	if (!pkt_dev->running) {
3251294a0b7fSJoe Perches 		pr_warn("interface: %s is already stopped\n",
3252f9467eaeSJoe Perches 			pkt_dev->odevname);
32531da177e4SLinus Torvalds 		return -EINVAL;
32541da177e4SLinus Torvalds 	}
32551da177e4SLinus Torvalds 
32568788370aSJesper Dangaard Brouer 	pkt_dev->running = 0;
32573bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
32583bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3259398f382cSDaniel Borkmann 	pkt_dev->stopped_at = ktime_get();
32601da177e4SLinus Torvalds 
326195ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
32621da177e4SLinus Torvalds 
32631da177e4SLinus Torvalds 	return 0;
32641da177e4SLinus Torvalds }
32651da177e4SLinus Torvalds 
32661da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
32671da177e4SLinus Torvalds {
3268c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
32691da177e4SLinus Torvalds 
32708788370aSJesper Dangaard Brouer 	rcu_read_lock();
32718788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3272c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3273222f1806SLuiz Capitulino 			continue;
3274222f1806SLuiz Capitulino 		if (best == NULL)
3275c26a8016SLuiz Capitulino 			best = pkt_dev;
3276398f382cSDaniel Borkmann 		else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3277c26a8016SLuiz Capitulino 			best = pkt_dev;
32781da177e4SLinus Torvalds 	}
32798788370aSJesper Dangaard Brouer 	rcu_read_unlock();
32808788370aSJesper Dangaard Brouer 
32811da177e4SLinus Torvalds 	return best;
32821da177e4SLinus Torvalds }
32831da177e4SLinus Torvalds 
3284222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3285222f1806SLuiz Capitulino {
3286c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
32871da177e4SLinus Torvalds 
3288f9467eaeSJoe Perches 	func_enter();
32891da177e4SLinus Torvalds 
32908788370aSJesper Dangaard Brouer 	rcu_read_lock();
32911da177e4SLinus Torvalds 
32928788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3293c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
329495ed63f7SArthur Kepner 	}
329595ed63f7SArthur Kepner 
32968788370aSJesper Dangaard Brouer 	rcu_read_unlock();
329795ed63f7SArthur Kepner }
329895ed63f7SArthur Kepner 
329995ed63f7SArthur Kepner /*
330095ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
330195ed63f7SArthur Kepner  * and remove it
330295ed63f7SArthur Kepner  */
330395ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
330495ed63f7SArthur Kepner {
3305c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3306c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
330795ed63f7SArthur Kepner 
3308f9467eaeSJoe Perches 	func_enter();
330995ed63f7SArthur Kepner 
3310c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3311c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
331295ed63f7SArthur Kepner 
3313222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3314222f1806SLuiz Capitulino 			continue;
331595ed63f7SArthur Kepner 
331695ed63f7SArthur Kepner 		kfree_skb(cur->skb);
331795ed63f7SArthur Kepner 		cur->skb = NULL;
331895ed63f7SArthur Kepner 
331995ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
332095ed63f7SArthur Kepner 
332195ed63f7SArthur Kepner 		break;
332295ed63f7SArthur Kepner 	}
33231da177e4SLinus Torvalds }
33241da177e4SLinus Torvalds 
33251da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
33261da177e4SLinus Torvalds {
3327c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3328c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
33291da177e4SLinus Torvalds 
3330f9467eaeSJoe Perches 	func_enter();
3331f9467eaeSJoe Perches 
33321da177e4SLinus Torvalds 	/* Remove all devices, free mem */
33331da177e4SLinus Torvalds 
3334c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3335c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
333695ed63f7SArthur Kepner 
333795ed63f7SArthur Kepner 		kfree_skb(cur->skb);
333895ed63f7SArthur Kepner 		cur->skb = NULL;
333995ed63f7SArthur Kepner 
33401da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
33411da177e4SLinus Torvalds 	}
33421da177e4SLinus Torvalds }
33431da177e4SLinus Torvalds 
33441da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
33451da177e4SLinus Torvalds {
33461da177e4SLinus Torvalds 	/* Remove from the thread list */
33474e58a027SCong Wang 	remove_proc_entry(t->tsk->comm, t->net->proc_dir);
33481da177e4SLinus Torvalds }
33491da177e4SLinus Torvalds 
3350ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
33513791decbSStephen Hemminger {
3352398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
33533791decbSStephen Hemminger 	schedule();
3354398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
33553791decbSStephen Hemminger }
33563791decbSStephen Hemminger 
3357ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3358ef87979cSStephen Hemminger {
3359398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
3360ef87979cSStephen Hemminger 
3361ef87979cSStephen Hemminger 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3362ef87979cSStephen Hemminger 		if (signal_pending(current))
3363ef87979cSStephen Hemminger 			break;
3364ef87979cSStephen Hemminger 
3365ef87979cSStephen Hemminger 		if (need_resched())
3366ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3367ef87979cSStephen Hemminger 		else
3368ef87979cSStephen Hemminger 			cpu_relax();
3369ef87979cSStephen Hemminger 	}
3370398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3371ef87979cSStephen Hemminger }
3372fd29cf72SStephen Hemminger 
3373475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
33741da177e4SLinus Torvalds {
337538b2cf29SAlexei Starovoitov 	unsigned int burst = ACCESS_ONCE(pkt_dev->burst);
337600829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
3377fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
337862f64aedSAlexei Starovoitov 	struct sk_buff *skb;
33791da177e4SLinus Torvalds 	int ret;
33801da177e4SLinus Torvalds 
3381ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3382ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
33831da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
33843791decbSStephen Hemminger 		return;
33851da177e4SLinus Torvalds 	}
33861da177e4SLinus Torvalds 
3387ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3388ef87979cSStephen Hemminger 	 * "never transmit"
3389ef87979cSStephen Hemminger 	 */
3390ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3391398f382cSDaniel Borkmann 		pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3392ef87979cSStephen Hemminger 		return;
3393ef87979cSStephen Hemminger 	}
3394ef87979cSStephen Hemminger 
3395ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
33967d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
33977d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
33981da177e4SLinus Torvalds 		/* build a new pkt */
33991da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
34001da177e4SLinus Torvalds 
34011da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
34021da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
3403f9467eaeSJoe Perches 			pr_err("ERROR: couldn't allocate skb in fill_packet\n");
34041da177e4SLinus Torvalds 			schedule();
34051da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
34063791decbSStephen Hemminger 			return;
34071da177e4SLinus Torvalds 		}
3408baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
34091da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
34101da177e4SLinus Torvalds 	}
34111da177e4SLinus Torvalds 
3412ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3413ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3414ef87979cSStephen Hemminger 
341562f64aedSAlexei Starovoitov 	if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
341662f64aedSAlexei Starovoitov 		skb = pkt_dev->skb;
341762f64aedSAlexei Starovoitov 		skb->protocol = eth_type_trans(skb, skb->dev);
341862f64aedSAlexei Starovoitov 		atomic_add(burst, &skb->users);
341962f64aedSAlexei Starovoitov 		local_bh_disable();
342062f64aedSAlexei Starovoitov 		do {
342162f64aedSAlexei Starovoitov 			ret = netif_receive_skb(skb);
342262f64aedSAlexei Starovoitov 			if (ret == NET_RX_DROP)
342362f64aedSAlexei Starovoitov 				pkt_dev->errors++;
342462f64aedSAlexei Starovoitov 			pkt_dev->sofar++;
342562f64aedSAlexei Starovoitov 			pkt_dev->seq_num++;
342662f64aedSAlexei Starovoitov 			if (atomic_read(&skb->users) != burst) {
342762f64aedSAlexei Starovoitov 				/* skb was queued by rps/rfs or taps,
342862f64aedSAlexei Starovoitov 				 * so cannot reuse this skb
342962f64aedSAlexei Starovoitov 				 */
343062f64aedSAlexei Starovoitov 				atomic_sub(burst - 1, &skb->users);
343162f64aedSAlexei Starovoitov 				/* get out of the loop and wait
343262f64aedSAlexei Starovoitov 				 * until skb is consumed
343362f64aedSAlexei Starovoitov 				 */
343462f64aedSAlexei Starovoitov 				break;
343562f64aedSAlexei Starovoitov 			}
343662f64aedSAlexei Starovoitov 			/* skb was 'freed' by stack, so clean few
343762f64aedSAlexei Starovoitov 			 * bits and reuse it
343862f64aedSAlexei Starovoitov 			 */
343962f64aedSAlexei Starovoitov #ifdef CONFIG_NET_CLS_ACT
344062f64aedSAlexei Starovoitov 			skb->tc_verd = 0; /* reset reclass/redir ttl */
344162f64aedSAlexei Starovoitov #endif
344262f64aedSAlexei Starovoitov 		} while (--burst > 0);
344362f64aedSAlexei Starovoitov 		goto out; /* Skips xmit_mode M_START_XMIT */
34440967f244SJohn Fastabend 	} else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
34450967f244SJohn Fastabend 		local_bh_disable();
34460967f244SJohn Fastabend 		atomic_inc(&pkt_dev->skb->users);
34470967f244SJohn Fastabend 
34480967f244SJohn Fastabend 		ret = dev_queue_xmit(pkt_dev->skb);
34490967f244SJohn Fastabend 		switch (ret) {
34500967f244SJohn Fastabend 		case NET_XMIT_SUCCESS:
34510967f244SJohn Fastabend 			pkt_dev->sofar++;
34520967f244SJohn Fastabend 			pkt_dev->seq_num++;
34530967f244SJohn Fastabend 			pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
34540967f244SJohn Fastabend 			break;
34550967f244SJohn Fastabend 		case NET_XMIT_DROP:
34560967f244SJohn Fastabend 		case NET_XMIT_CN:
34570967f244SJohn Fastabend 		/* These are all valid return codes for a qdisc but
34580967f244SJohn Fastabend 		 * indicate packets are being dropped or will likely
34590967f244SJohn Fastabend 		 * be dropped soon.
34600967f244SJohn Fastabend 		 */
34610967f244SJohn Fastabend 		case NETDEV_TX_BUSY:
34620967f244SJohn Fastabend 		/* qdisc may call dev_hard_start_xmit directly in cases
34630967f244SJohn Fastabend 		 * where no queues exist e.g. loopback device, virtual
34640967f244SJohn Fastabend 		 * devices, etc. In this case we need to handle
34650967f244SJohn Fastabend 		 * NETDEV_TX_ codes.
34660967f244SJohn Fastabend 		 */
34670967f244SJohn Fastabend 		default:
34680967f244SJohn Fastabend 			pkt_dev->errors++;
34690967f244SJohn Fastabend 			net_info_ratelimited("%s xmit error: %d\n",
34700967f244SJohn Fastabend 					     pkt_dev->odevname, ret);
34710967f244SJohn Fastabend 			break;
34720967f244SJohn Fastabend 		}
34730967f244SJohn Fastabend 		goto out;
347462f64aedSAlexei Starovoitov 	}
347562f64aedSAlexei Starovoitov 
347610c51b56SDaniel Borkmann 	txq = skb_get_tx_queue(odev, pkt_dev->skb);
3477fd2ea0a7SDavid S. Miller 
34780f2eea4bSDaniel Borkmann 	local_bh_disable();
34790f2eea4bSDaniel Borkmann 
34800f2eea4bSDaniel Borkmann 	HARD_TX_LOCK(odev, txq, smp_processor_id());
34815b8db2f5SStephen Hemminger 
34826f25cd47SDaniel Borkmann 	if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3483ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
34840835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
34850835acfeSEric Dumazet 		goto unlock;
34860835acfeSEric Dumazet 	}
348738b2cf29SAlexei Starovoitov 	atomic_add(burst, &pkt_dev->skb->users);
348838b2cf29SAlexei Starovoitov 
348938b2cf29SAlexei Starovoitov xmit_more:
349038b2cf29SAlexei Starovoitov 	ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
3491ef87979cSStephen Hemminger 
34925b8db2f5SStephen Hemminger 	switch (ret) {
34935b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
34941da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
34951da177e4SLinus Torvalds 		pkt_dev->sofar++;
34961da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3497baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
349838b2cf29SAlexei Starovoitov 		if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
349938b2cf29SAlexei Starovoitov 			goto xmit_more;
35005b8db2f5SStephen Hemminger 		break;
3501f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3502f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3503f466dba1SJohn Fastabend 		/* skb has been consumed */
3504f466dba1SJohn Fastabend 		pkt_dev->errors++;
3505f466dba1SJohn Fastabend 		break;
35065b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
3507e87cc472SJoe Perches 		net_info_ratelimited("%s xmit error: %d\n",
3508e87cc472SJoe Perches 				     pkt_dev->odevname, ret);
35091da177e4SLinus Torvalds 		pkt_dev->errors++;
35105b8db2f5SStephen Hemminger 		/* fallthru */
35115b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
35125b8db2f5SStephen Hemminger 		/* Retry it next time */
35135b8db2f5SStephen Hemminger 		atomic_dec(&(pkt_dev->skb->users));
35141da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
35151da177e4SLinus Torvalds 	}
351638b2cf29SAlexei Starovoitov 	if (unlikely(burst))
351738b2cf29SAlexei Starovoitov 		atomic_sub(burst, &pkt_dev->skb->users);
35180835acfeSEric Dumazet unlock:
35190f2eea4bSDaniel Borkmann 	HARD_TX_UNLOCK(odev, txq);
35200f2eea4bSDaniel Borkmann 
352162f64aedSAlexei Starovoitov out:
35220f2eea4bSDaniel Borkmann 	local_bh_enable();
35231da177e4SLinus Torvalds 
35241da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
35251da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3526ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
35271da177e4SLinus Torvalds 
35281da177e4SLinus Torvalds 		/* Done with this */
35291da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
35301da177e4SLinus Torvalds 	}
35311da177e4SLinus Torvalds }
35321da177e4SLinus Torvalds 
35331da177e4SLinus Torvalds /*
35341da177e4SLinus Torvalds  * Main loop of the thread goes here
35351da177e4SLinus Torvalds  */
35361da177e4SLinus Torvalds 
3537ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
35381da177e4SLinus Torvalds {
35391da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3540ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
35411da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
35421da177e4SLinus Torvalds 	int cpu = t->cpu;
35431da177e4SLinus Torvalds 
3544ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
35451da177e4SLinus Torvalds 
35461da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3547d3ede327SDenis V. Lunev 	complete(&t->start_done);
35481da177e4SLinus Torvalds 
3549f9467eaeSJoe Perches 	pr_debug("starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
35501da177e4SLinus Torvalds 
355183144186SRafael J. Wysocki 	set_freezable();
355283144186SRafael J. Wysocki 
3553ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3554ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3555ee74baa7SDavid S. Miller 
3556ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
35574e58a027SCong Wang 			if (t->net->pktgen_exiting)
3558551eaff1SEric Dumazet 				break;
3559ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3560ef87979cSStephen Hemminger 							 t->control != 0,
3561ef87979cSStephen Hemminger 							 HZ/10);
35621b3f720bSRafael J. Wysocki 			try_to_freeze();
3563ef87979cSStephen Hemminger 			continue;
3564ee74baa7SDavid S. Miller 		}
35651da177e4SLinus Torvalds 
3566ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
35671da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
35681da177e4SLinus Torvalds 
3569ef87979cSStephen Hemminger 			if (need_resched())
3570ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3571ef87979cSStephen Hemminger 			else
3572ef87979cSStephen Hemminger 				cpu_relax();
3573ef87979cSStephen Hemminger 		}
3574ef87979cSStephen Hemminger 
35751da177e4SLinus Torvalds 		if (t->control & T_STOP) {
35761da177e4SLinus Torvalds 			pktgen_stop(t);
35771da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
35781da177e4SLinus Torvalds 		}
35791da177e4SLinus Torvalds 
35801da177e4SLinus Torvalds 		if (t->control & T_RUN) {
35811da177e4SLinus Torvalds 			pktgen_run(t);
35821da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
35831da177e4SLinus Torvalds 		}
35841da177e4SLinus Torvalds 
358595ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
35861da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
358795ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
358895ed63f7SArthur Kepner 		}
358995ed63f7SArthur Kepner 
359095ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
359195ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
35921da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
35931da177e4SLinus Torvalds 		}
35941da177e4SLinus Torvalds 
359509fe3ef4SAndrew Morton 		try_to_freeze();
35961da177e4SLinus Torvalds 	}
35971da177e4SLinus Torvalds 
3598f9467eaeSJoe Perches 	pr_debug("%s stopping all device\n", t->tsk->comm);
35991da177e4SLinus Torvalds 	pktgen_stop(t);
36001da177e4SLinus Torvalds 
3601f9467eaeSJoe Perches 	pr_debug("%s removing all device\n", t->tsk->comm);
36021da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
36031da177e4SLinus Torvalds 
3604f9467eaeSJoe Perches 	pr_debug("%s removing thread\n", t->tsk->comm);
36051da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3606cdcdbe0bSLuiz Capitulino 
3607ee74baa7SDavid S. Miller 	return 0;
36081da177e4SLinus Torvalds }
36091da177e4SLinus Torvalds 
3610222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
36113e984840SEric Dumazet 					  const char *ifname, bool exact)
36121da177e4SLinus Torvalds {
3613c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
36143e984840SEric Dumazet 	size_t len = strlen(ifname);
36151da177e4SLinus Torvalds 
36168788370aSJesper Dangaard Brouer 	rcu_read_lock();
36178788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(p, &t->if_list, list)
36183e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
36193e984840SEric Dumazet 			if (p->odevname[len]) {
36203e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
36213e984840SEric Dumazet 					continue;
36223e984840SEric Dumazet 			}
3623c26a8016SLuiz Capitulino 			pkt_dev = p;
36241da177e4SLinus Torvalds 			break;
36251da177e4SLinus Torvalds 		}
36261da177e4SLinus Torvalds 
36278788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3628f9467eaeSJoe Perches 	pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
36291da177e4SLinus Torvalds 	return pkt_dev;
36301da177e4SLinus Torvalds }
36311da177e4SLinus Torvalds 
36321da177e4SLinus Torvalds /*
36331da177e4SLinus Torvalds  * Adds a dev at front of if_list.
36341da177e4SLinus Torvalds  */
36351da177e4SLinus Torvalds 
3636222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3637222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
36381da177e4SLinus Torvalds {
36391da177e4SLinus Torvalds 	int rv = 0;
36401da177e4SLinus Torvalds 
36418788370aSJesper Dangaard Brouer 	/* This function cannot be called concurrently, as its called
36428788370aSJesper Dangaard Brouer 	 * under pktgen_thread_lock mutex, but it can run from
36438788370aSJesper Dangaard Brouer 	 * userspace on another CPU than the kthread.  The if_lock()
36448788370aSJesper Dangaard Brouer 	 * is used here to sync with concurrent instances of
36458788370aSJesper Dangaard Brouer 	 * _rem_dev_from_if_list() invoked via kthread, which is also
36468788370aSJesper Dangaard Brouer 	 * updating the if_list */
36471da177e4SLinus Torvalds 	if_lock(t);
36481da177e4SLinus Torvalds 
36491da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
3650f9467eaeSJoe Perches 		pr_err("ERROR: already assigned to a thread\n");
36511da177e4SLinus Torvalds 		rv = -EBUSY;
36521da177e4SLinus Torvalds 		goto out;
36531da177e4SLinus Torvalds 	}
3654c26a8016SLuiz Capitulino 
36551da177e4SLinus Torvalds 	pkt_dev->running = 0;
36568788370aSJesper Dangaard Brouer 	pkt_dev->pg_thread = t;
36578788370aSJesper Dangaard Brouer 	list_add_rcu(&pkt_dev->list, &t->if_list);
36581da177e4SLinus Torvalds 
36591da177e4SLinus Torvalds out:
36601da177e4SLinus Torvalds 	if_unlock(t);
36611da177e4SLinus Torvalds 	return rv;
36621da177e4SLinus Torvalds }
36631da177e4SLinus Torvalds 
36641da177e4SLinus Torvalds /* Called under thread lock */
36651da177e4SLinus Torvalds 
36661da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
36671da177e4SLinus Torvalds {
36681da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
366939df232fSStephen Hemminger 	int err;
36703291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
36711da177e4SLinus Torvalds 
36721da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
36731da177e4SLinus Torvalds 
36744e58a027SCong Wang 	pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3675d50a6b56SStephen Hemminger 	if (pkt_dev) {
3676f9467eaeSJoe Perches 		pr_err("ERROR: interface already used\n");
3677d50a6b56SStephen Hemminger 		return -EBUSY;
3678d50a6b56SStephen Hemminger 	}
36791da177e4SLinus Torvalds 
36803291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
36811da177e4SLinus Torvalds 	if (!pkt_dev)
36821da177e4SLinus Torvalds 		return -ENOMEM;
36831da177e4SLinus Torvalds 
3684593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
368568d5ac2eSWANG Cong 	pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
36863291b9dbSEric Dumazet 				      node);
36871da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
36881da177e4SLinus Torvalds 		kfree(pkt_dev);
36891da177e4SLinus Torvalds 		return -ENOMEM;
36901da177e4SLinus Torvalds 	}
36911da177e4SLinus Torvalds 
369295ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
36931da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
3694fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
36951da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
36961da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
36971da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
36981da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
36991da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
37001da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
370134954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
370234954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
370334954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
370434954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
370534954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
370634954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
370738b2cf29SAlexei Starovoitov 	pkt_dev->burst = 1;
3708e99b99b4SRobert Olsson 	pkt_dev->node = -1;
370934954ddcSFrancesco Fondelli 
37104e58a027SCong Wang 	err = pktgen_setup_dev(t->net, pkt_dev, ifname);
371139df232fSStephen Hemminger 	if (err)
371239df232fSStephen Hemminger 		goto out1;
3713d8873315SNeil Horman 	if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3714d8873315SNeil Horman 		pkt_dev->clone_skb = pg_clone_skb_d;
37151da177e4SLinus Torvalds 
37164e58a027SCong Wang 	pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
37175efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
371839df232fSStephen Hemminger 	if (!pkt_dev->entry) {
3719f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3720d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
372139df232fSStephen Hemminger 		err = -EINVAL;
372239df232fSStephen Hemminger 		goto out2;
372339df232fSStephen Hemminger 	}
3724a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3725a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3726a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3727cf93d47eSFan Du 
3728cf93d47eSFan Du 	/* xfrm tunnel mode needs additional dst to extract outter
3729cf93d47eSFan Du 	 * ip header protocol/ttl/id field, here creat a phony one.
3730cf93d47eSFan Du 	 * instead of looking for a valid rt, which definitely hurting
3731cf93d47eSFan Du 	 * performance under such circumstance.
3732cf93d47eSFan Du 	 */
3733cf93d47eSFan Du 	pkt_dev->dstops.family = AF_INET;
3734cf93d47eSFan Du 	pkt_dev->dst.dev = pkt_dev->odev;
3735cf93d47eSFan Du 	dst_init_metrics(&pkt_dev->dst, pktgen_dst_metrics, false);
3736cf93d47eSFan Du 	pkt_dev->dst.child = &pkt_dev->dst;
3737cf93d47eSFan Du 	pkt_dev->dst.ops = &pkt_dev->dstops;
3738a553e4a6SJamal Hadi Salim #endif
373939df232fSStephen Hemminger 
374039df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
374139df232fSStephen Hemminger out2:
374239df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
374339df232fSStephen Hemminger out1:
3744a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3745a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3746a553e4a6SJamal Hadi Salim #endif
37471da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
37481da177e4SLinus Torvalds 	kfree(pkt_dev);
374939df232fSStephen Hemminger 	return err;
37501da177e4SLinus Torvalds }
37511da177e4SLinus Torvalds 
37524e58a027SCong Wang static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
37531da177e4SLinus Torvalds {
3754cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3755d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3756ee74baa7SDavid S. Miller 	struct task_struct *p;
37571da177e4SLinus Torvalds 
37583291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
37593291b9dbSEric Dumazet 			 cpu_to_node(cpu));
37601da177e4SLinus Torvalds 	if (!t) {
3761f9467eaeSJoe Perches 		pr_err("ERROR: out of memory, can't create new thread\n");
37621da177e4SLinus Torvalds 		return -ENOMEM;
37631da177e4SLinus Torvalds 	}
37641da177e4SLinus Torvalds 
37651da177e4SLinus Torvalds 	spin_lock_init(&t->if_lock);
37661da177e4SLinus Torvalds 	t->cpu = cpu;
37671da177e4SLinus Torvalds 
3768ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3769ee74baa7SDavid S. Miller 
37704e58a027SCong Wang 	list_add_tail(&t->th_list, &pn->pktgen_threads);
3771d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3772ee74baa7SDavid S. Miller 
377394dcf29aSEric Dumazet 	p = kthread_create_on_node(pktgen_thread_worker,
377494dcf29aSEric Dumazet 				   t,
377594dcf29aSEric Dumazet 				   cpu_to_node(cpu),
377694dcf29aSEric Dumazet 				   "kpktgend_%d", cpu);
3777ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
3778f9467eaeSJoe Perches 		pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3779ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3780ee74baa7SDavid S. Miller 		kfree(t);
3781ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3782ee74baa7SDavid S. Miller 	}
3783ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3784ee74baa7SDavid S. Miller 	t->tsk = p;
3785ee74baa7SDavid S. Miller 
37864e58a027SCong Wang 	pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
37875efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3788d50a6b56SStephen Hemminger 	if (!pe) {
3789f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3790ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3791ee74baa7SDavid S. Miller 		kthread_stop(p);
3792ee74baa7SDavid S. Miller 		list_del(&t->th_list);
37931da177e4SLinus Torvalds 		kfree(t);
37941da177e4SLinus Torvalds 		return -EINVAL;
37951da177e4SLinus Torvalds 	}
3796d50a6b56SStephen Hemminger 
37974e58a027SCong Wang 	t->net = pn;
37981fbe4b46SOleg Nesterov 	get_task_struct(p);
3799ee74baa7SDavid S. Miller 	wake_up_process(p);
3800d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
38011da177e4SLinus Torvalds 
38021da177e4SLinus Torvalds 	return 0;
38031da177e4SLinus Torvalds }
38041da177e4SLinus Torvalds 
38051da177e4SLinus Torvalds /*
38061da177e4SLinus Torvalds  * Removes a device from the thread if_list.
38071da177e4SLinus Torvalds  */
3808222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3809222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
38101da177e4SLinus Torvalds {
3811c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3812c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
38131da177e4SLinus Torvalds 
38148788370aSJesper Dangaard Brouer 	if_lock(t);
3815c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3816c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3817c26a8016SLuiz Capitulino 		if (p == pkt_dev)
38188788370aSJesper Dangaard Brouer 			list_del_rcu(&p->list);
38191da177e4SLinus Torvalds 	}
38208788370aSJesper Dangaard Brouer 	if_unlock(t);
38211da177e4SLinus Torvalds }
38221da177e4SLinus Torvalds 
3823222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3824222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
38251da177e4SLinus Torvalds {
3826f9467eaeSJoe Perches 	pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
38271da177e4SLinus Torvalds 
38281da177e4SLinus Torvalds 	if (pkt_dev->running) {
3829294a0b7fSJoe Perches 		pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
38301da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
38311da177e4SLinus Torvalds 	}
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds 	/* Dis-associate from the interface */
38341da177e4SLinus Torvalds 
38351da177e4SLinus Torvalds 	if (pkt_dev->odev) {
38361da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
38371da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
38381da177e4SLinus Torvalds 	}
38391da177e4SLinus Torvalds 
38408788370aSJesper Dangaard Brouer 	/* Remove proc before if_list entry, because add_device uses
38418788370aSJesper Dangaard Brouer 	 * list to determine if interface already exist, avoid race
38428788370aSJesper Dangaard Brouer 	 * with proc_create_data() */
3843a8ca16eaSDavid Howells 	proc_remove(pkt_dev->entry);
38441da177e4SLinus Torvalds 
38458788370aSJesper Dangaard Brouer 	/* And update the thread if_list */
38468788370aSJesper Dangaard Brouer 	_rem_dev_from_if_list(t, pkt_dev);
38478788370aSJesper Dangaard Brouer 
3848a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3849a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3850a553e4a6SJamal Hadi Salim #endif
38511da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
385226ad7879SEric Dumazet 	if (pkt_dev->page)
385326ad7879SEric Dumazet 		put_page(pkt_dev->page);
38548788370aSJesper Dangaard Brouer 	kfree_rcu(pkt_dev, rcu);
38551da177e4SLinus Torvalds 	return 0;
38561da177e4SLinus Torvalds }
38571da177e4SLinus Torvalds 
38584e58a027SCong Wang static int __net_init pg_net_init(struct net *net)
38591da177e4SLinus Torvalds {
38604e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3861d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
38624e58a027SCong Wang 	int cpu, ret = 0;
3863d50a6b56SStephen Hemminger 
38644e58a027SCong Wang 	pn->net = net;
38654e58a027SCong Wang 	INIT_LIST_HEAD(&pn->pktgen_threads);
38664e58a027SCong Wang 	pn->pktgen_exiting = false;
38674e58a027SCong Wang 	pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
38684e58a027SCong Wang 	if (!pn->proc_dir) {
38694e58a027SCong Wang 		pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3870d50a6b56SStephen Hemminger 		return -ENODEV;
38711da177e4SLinus Torvalds 	}
38724e58a027SCong Wang 	pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
38734e58a027SCong Wang 	if (pe == NULL) {
38744e58a027SCong Wang 		pr_err("cannot create %s procfs entry\n", PGCTRL);
38754e58a027SCong Wang 		ret = -EINVAL;
38764e58a027SCong Wang 		goto remove;
38774e58a027SCong Wang 	}
38781da177e4SLinus Torvalds 
3879670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
38808024bb24SLuiz Capitulino 		int err;
38811da177e4SLinus Torvalds 
38824e58a027SCong Wang 		err = pktgen_create_thread(cpu, pn);
38838024bb24SLuiz Capitulino 		if (err)
38844e58a027SCong Wang 			pr_warn("Cannot create thread for cpu %d (%d)\n",
3885f9467eaeSJoe Perches 				   cpu, err);
38861da177e4SLinus Torvalds 	}
38878024bb24SLuiz Capitulino 
38884e58a027SCong Wang 	if (list_empty(&pn->pktgen_threads)) {
38894e58a027SCong Wang 		pr_err("Initialization failed for all threads\n");
3890ce14f894SWANG Cong 		ret = -ENODEV;
38914e58a027SCong Wang 		goto remove_entry;
38928024bb24SLuiz Capitulino 	}
38938024bb24SLuiz Capitulino 
38941da177e4SLinus Torvalds 	return 0;
3895ce14f894SWANG Cong 
38964e58a027SCong Wang remove_entry:
38974e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
38984e58a027SCong Wang remove:
3899ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3900ce14f894SWANG Cong 	return ret;
39011da177e4SLinus Torvalds }
39021da177e4SLinus Torvalds 
39034e58a027SCong Wang static void __net_exit pg_net_exit(struct net *net)
39041da177e4SLinus Torvalds {
39054e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3906cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3907cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
3908d4b11335SEric Dumazet 	LIST_HEAD(list);
39091da177e4SLinus Torvalds 
39101da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
39114e58a027SCong Wang 	pn->pktgen_exiting = true;
39121da177e4SLinus Torvalds 
3913c57b5468SEric Dumazet 	mutex_lock(&pktgen_thread_lock);
39144e58a027SCong Wang 	list_splice_init(&pn->pktgen_threads, &list);
3915c57b5468SEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
3916c57b5468SEric Dumazet 
3917c57b5468SEric Dumazet 	list_for_each_safe(q, n, &list) {
3918cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3919c57b5468SEric Dumazet 		list_del(&t->th_list);
3920ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
39211fbe4b46SOleg Nesterov 		put_task_struct(t->tsk);
3922ee74baa7SDavid S. Miller 		kfree(t);
39231da177e4SLinus Torvalds 	}
39241da177e4SLinus Torvalds 
39254e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
3926ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
39274e58a027SCong Wang }
39281da177e4SLinus Torvalds 
39294e58a027SCong Wang static struct pernet_operations pg_net_ops = {
39304e58a027SCong Wang 	.init = pg_net_init,
39314e58a027SCong Wang 	.exit = pg_net_exit,
39324e58a027SCong Wang 	.id   = &pg_net_id,
39334e58a027SCong Wang 	.size = sizeof(struct pktgen_net),
39344e58a027SCong Wang };
39354e58a027SCong Wang 
39364e58a027SCong Wang static int __init pg_init(void)
39374e58a027SCong Wang {
39384e58a027SCong Wang 	int ret = 0;
39394e58a027SCong Wang 
39404e58a027SCong Wang 	pr_info("%s", version);
39414e58a027SCong Wang 	ret = register_pernet_subsys(&pg_net_ops);
39424e58a027SCong Wang 	if (ret)
39434e58a027SCong Wang 		return ret;
39444e58a027SCong Wang 	ret = register_netdevice_notifier(&pktgen_notifier_block);
39454e58a027SCong Wang 	if (ret)
39464e58a027SCong Wang 		unregister_pernet_subsys(&pg_net_ops);
39474e58a027SCong Wang 
39484e58a027SCong Wang 	return ret;
39494e58a027SCong Wang }
39504e58a027SCong Wang 
39514e58a027SCong Wang static void __exit pg_cleanup(void)
39524e58a027SCong Wang {
39534e58a027SCong Wang 	unregister_netdevice_notifier(&pktgen_notifier_block);
39544e58a027SCong Wang 	unregister_pernet_subsys(&pg_net_ops);
39558788370aSJesper Dangaard Brouer 	/* Don't need rcu_barrier() due to use of kfree_rcu() */
39561da177e4SLinus Torvalds }
39571da177e4SLinus Torvalds 
39581da177e4SLinus Torvalds module_init(pg_init);
39591da177e4SLinus Torvalds module_exit(pg_cleanup);
39601da177e4SLinus Torvalds 
3961c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
39621da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
39631da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3964c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
39651da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3966c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
39671da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3968c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
39691da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3970c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
39711da177e4SLinus Torvalds module_param(debug, int, 0);
3972c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3973