xref: /openbmc/linux/net/core/pktgen.c (revision cf93d47ed4489cc349678f397290b3ecba868a3b)
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
721da177e4SLinus Torvalds  * way. The if_lock should be possible to remove when add/rem_device is merged
731da177e4SLinus Torvalds  * into this too.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * By design there should only be *one* "controlling" process. In practice
761da177e4SLinus Torvalds  * multiple write accesses gives unpredictable result. Understood by "write"
771da177e4SLinus Torvalds  * to /proc gives result code thats should be read be the "writer".
78b4099fabSStephen Hemminger  * For practical use this should be no problem.
791da177e4SLinus Torvalds  *
801da177e4SLinus Torvalds  * Note when adding devices to a specific CPU there good idea to also assign
811da177e4SLinus Torvalds  * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
821da177e4SLinus Torvalds  * --ro
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * Fix refcount off by one if first packet fails, potential null deref,
851da177e4SLinus Torvalds  * memleak 030710- KJP
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * First "ranges" functionality for ipv6 030726 --ro
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  * Included flow support. 030802 ANK.
901da177e4SLinus Torvalds  *
911da177e4SLinus Torvalds  * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
921da177e4SLinus Torvalds  *
931da177e4SLinus Torvalds  * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
941da177e4SLinus Torvalds  * ia64 compilation fix from  Aron Griffis <aron@hp.com> 040604
951da177e4SLinus Torvalds  *
961da177e4SLinus Torvalds  * New xmit() return, do_div and misc clean up by Stephen Hemminger
971da177e4SLinus Torvalds  * <shemminger@osdl.org> 040923
981da177e4SLinus Torvalds  *
99b4099fabSStephen Hemminger  * Randy Dunlap fixed u64 printk compiler waring
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * Remove FCS from BW calculation.  Lennert Buytenhek <buytenh@wantstofly.org>
1021da177e4SLinus Torvalds  * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  * Corrections from Nikolai Malykh (nmalykh@bilim.com)
1051da177e4SLinus Torvalds  * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
1081da177e4SLinus Torvalds  * 050103
109ca6549afSSteven Whitehouse  *
110ca6549afSSteven Whitehouse  * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111ca6549afSSteven Whitehouse  *
11234954ddcSFrancesco Fondelli  * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
11334954ddcSFrancesco Fondelli  *
114ce5d0b47SAdit Ranadive  * Fixed src_mac command to set source mac of packet to value specified in
115ce5d0b47SAdit Ranadive  * command by Adit Ranadive <adit.262@gmail.com>
116ce5d0b47SAdit Ranadive  *
1171da177e4SLinus Torvalds  */
118f9467eaeSJoe Perches 
119f9467eaeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
120f9467eaeSJoe Perches 
1211da177e4SLinus Torvalds #include <linux/sys.h>
1221da177e4SLinus Torvalds #include <linux/types.h>
1231da177e4SLinus Torvalds #include <linux/module.h>
1241da177e4SLinus Torvalds #include <linux/moduleparam.h>
1251da177e4SLinus Torvalds #include <linux/kernel.h>
126222fa076SLuiz Capitulino #include <linux/mutex.h>
1271da177e4SLinus Torvalds #include <linux/sched.h>
1281da177e4SLinus Torvalds #include <linux/slab.h>
1291da177e4SLinus Torvalds #include <linux/vmalloc.h>
1301da177e4SLinus Torvalds #include <linux/unistd.h>
1311da177e4SLinus Torvalds #include <linux/string.h>
1321da177e4SLinus Torvalds #include <linux/ptrace.h>
1331da177e4SLinus Torvalds #include <linux/errno.h>
1341da177e4SLinus Torvalds #include <linux/ioport.h>
1351da177e4SLinus Torvalds #include <linux/interrupt.h>
1364fc268d2SRandy Dunlap #include <linux/capability.h>
1372bc481cfSStephen Hemminger #include <linux/hrtimer.h>
13809fe3ef4SAndrew Morton #include <linux/freezer.h>
1391da177e4SLinus Torvalds #include <linux/delay.h>
1401da177e4SLinus Torvalds #include <linux/timer.h>
141cdcdbe0bSLuiz Capitulino #include <linux/list.h>
1421da177e4SLinus Torvalds #include <linux/init.h>
1431da177e4SLinus Torvalds #include <linux/skbuff.h>
1441da177e4SLinus Torvalds #include <linux/netdevice.h>
1451da177e4SLinus Torvalds #include <linux/inet.h>
1461da177e4SLinus Torvalds #include <linux/inetdevice.h>
1471da177e4SLinus Torvalds #include <linux/rtnetlink.h>
1481da177e4SLinus Torvalds #include <linux/if_arp.h>
14934954ddcSFrancesco Fondelli #include <linux/if_vlan.h>
1501da177e4SLinus Torvalds #include <linux/in.h>
1511da177e4SLinus Torvalds #include <linux/ip.h>
1521da177e4SLinus Torvalds #include <linux/ipv6.h>
1531da177e4SLinus Torvalds #include <linux/udp.h>
1541da177e4SLinus Torvalds #include <linux/proc_fs.h>
155d50a6b56SStephen Hemminger #include <linux/seq_file.h>
1561da177e4SLinus Torvalds #include <linux/wait.h>
157f404e9a6SKris Katterjohn #include <linux/etherdevice.h>
158ee74baa7SDavid S. Miller #include <linux/kthread.h>
159268bb0ceSLinus Torvalds #include <linux/prefetch.h>
160457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1611da177e4SLinus Torvalds #include <net/checksum.h>
1621da177e4SLinus Torvalds #include <net/ipv6.h>
163c26bf4a5SThomas Graf #include <net/udp.h>
16473d94e94SStephen Rothwell #include <net/ip6_checksum.h>
1651da177e4SLinus Torvalds #include <net/addrconf.h>
166a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
167a553e4a6SJamal Hadi Salim #include <net/xfrm.h>
168a553e4a6SJamal Hadi Salim #endif
1694e58a027SCong Wang #include <net/netns/generic.h>
1701da177e4SLinus Torvalds #include <asm/byteorder.h>
1711da177e4SLinus Torvalds #include <linux/rcupdate.h>
1721977f032SJiri Slaby #include <linux/bitops.h>
17363adc6fbSStephen Hemminger #include <linux/io.h>
17463adc6fbSStephen Hemminger #include <linux/timex.h>
17563adc6fbSStephen Hemminger #include <linux/uaccess.h>
1761da177e4SLinus Torvalds #include <asm/dma.h>
1771da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1781da177e4SLinus Torvalds 
17943d28b65SDaniel Turull #define VERSION	"2.74"
1801da177e4SLinus Torvalds #define IP_NAME_SZ 32
181ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
182d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1831da177e4SLinus Torvalds 
184f9467eaeSJoe Perches #define func_enter() pr_debug("entering %s\n", __func__);
185f9467eaeSJoe Perches 
1861da177e4SLinus Torvalds /* Device flag bits */
1871da177e4SLinus Torvalds #define F_IPSRC_RND   (1<<0)	/* IP-Src Random  */
1881da177e4SLinus Torvalds #define F_IPDST_RND   (1<<1)	/* IP-Dst Random  */
1891da177e4SLinus Torvalds #define F_UDPSRC_RND  (1<<2)	/* UDP-Src Random */
1901da177e4SLinus Torvalds #define F_UDPDST_RND  (1<<3)	/* UDP-Dst Random */
1911da177e4SLinus Torvalds #define F_MACSRC_RND  (1<<4)	/* MAC-Src Random */
1921da177e4SLinus Torvalds #define F_MACDST_RND  (1<<5)	/* MAC-Dst Random */
1931da177e4SLinus Torvalds #define F_TXSIZE_RND  (1<<6)	/* Transmit size is random */
1941da177e4SLinus Torvalds #define F_IPV6        (1<<7)	/* Interface in IPV6 Mode */
195ca6549afSSteven Whitehouse #define F_MPLS_RND    (1<<8)	/* Random MPLS labels */
19634954ddcSFrancesco Fondelli #define F_VID_RND     (1<<9)	/* Random VLAN ID */
19734954ddcSFrancesco Fondelli #define F_SVID_RND    (1<<10)	/* Random SVLAN ID */
198007a531bSJamal Hadi Salim #define F_FLOW_SEQ    (1<<11)	/* Sequential flows */
199a553e4a6SJamal Hadi Salim #define F_IPSEC_ON    (1<<12)	/* ipsec on for flows */
20045b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13)	/* queue map Random */
201e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14)	/* queue map mirrors smp_processor_id() */
202e99b99b4SRobert Olsson #define F_NODE          (1<<15)	/* Node memory alloc*/
203c26bf4a5SThomas Graf #define F_UDPCSUM       (1<<16)	/* Include UDP checksum */
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /* Thread control flag bits */
2066b80d6a6SStephen Hemminger #define T_STOP        (1<<0)	/* Stop run */
2076b80d6a6SStephen Hemminger #define T_RUN         (1<<1)	/* Start run */
2086b80d6a6SStephen Hemminger #define T_REMDEVALL   (1<<2)	/* Remove all devs */
2096b80d6a6SStephen Hemminger #define T_REMDEV      (1<<3)	/* Remove one dev */
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* If lock -- can be removed after some work */
2121da177e4SLinus Torvalds #define   if_lock(t)           spin_lock(&(t->if_lock));
2131da177e4SLinus Torvalds #define   if_unlock(t)           spin_unlock(&(t->if_lock));
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2161da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
217d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
218d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds #define MAX_CFLOWS  65536
2211da177e4SLinus Torvalds 
22234954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
22334954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
22434954ddcSFrancesco Fondelli 
225222f1806SLuiz Capitulino struct flow_state {
226252e3346SAl Viro 	__be32 cur_daddr;
2271da177e4SLinus Torvalds 	int count;
228a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
229a553e4a6SJamal Hadi Salim 	struct xfrm_state *x;
230a553e4a6SJamal Hadi Salim #endif
231007a531bSJamal Hadi Salim 	__u32 flags;
2321da177e4SLinus Torvalds };
2331da177e4SLinus Torvalds 
234007a531bSJamal Hadi Salim /* flow flag bits */
235007a531bSJamal Hadi Salim #define F_INIT   (1<<0)		/* flow has been initialized */
236007a531bSJamal Hadi Salim 
2371da177e4SLinus Torvalds struct pktgen_dev {
2381da177e4SLinus Torvalds 	/*
2391da177e4SLinus Torvalds 	 * Try to keep frequent/infrequent used vars. separated.
2401da177e4SLinus Torvalds 	 */
24139df232fSStephen Hemminger 	struct proc_dir_entry *entry;	/* proc file */
2421da177e4SLinus Torvalds 	struct pktgen_thread *pg_thread;/* the owner */
24363adc6fbSStephen Hemminger 	struct list_head list;		/* chaining in the thread's run-queue */
2441da177e4SLinus Torvalds 
24563adc6fbSStephen Hemminger 	int running;		/* if false, the test will stop */
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/* If min != max, then we will either do a linear iteration, or
2481da177e4SLinus Torvalds 	 * we will do a random selection from within the range.
2491da177e4SLinus Torvalds 	 */
2501da177e4SLinus Torvalds 	__u32 flags;
25195ed63f7SArthur Kepner 	int removal_mark;	/* non-zero => the device is marked for
25295ed63f7SArthur Kepner 				 * removal by worker thread */
2531da177e4SLinus Torvalds 
25468bf9f0bSAmerigo Wang 	int min_pkt_size;
25568bf9f0bSAmerigo Wang 	int max_pkt_size;
25616dab72fSJamal Hadi Salim 	int pkt_overhead;	/* overhead for MPLS, VLANs, IPSEC etc */
2571da177e4SLinus Torvalds 	int nfrags;
25826ad7879SEric Dumazet 	struct page *page;
259fd29cf72SStephen Hemminger 	u64 delay;		/* nano-seconds */
260fd29cf72SStephen Hemminger 
2611da177e4SLinus Torvalds 	__u64 count;		/* Default No packets to send */
2621da177e4SLinus Torvalds 	__u64 sofar;		/* How many pkts we've sent so far */
2631da177e4SLinus Torvalds 	__u64 tx_bytes;		/* How many bytes we've transmitted */
264f466dba1SJohn Fastabend 	__u64 errors;		/* Errors when trying to transmit, */
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	/* runtime counters relating to clone_skb */
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	__u64 allocated_skbs;
2691da177e4SLinus Torvalds 	__u32 clone_count;
2701da177e4SLinus Torvalds 	int last_ok;		/* Was last skb sent?
27163adc6fbSStephen Hemminger 				 * Or a failed transmit of some sort?
27263adc6fbSStephen Hemminger 				 * This will keep sequence numbers in order
2731da177e4SLinus Torvalds 				 */
274fd29cf72SStephen Hemminger 	ktime_t next_tx;
275fd29cf72SStephen Hemminger 	ktime_t started_at;
276fd29cf72SStephen Hemminger 	ktime_t stopped_at;
277fd29cf72SStephen Hemminger 	u64	idle_acc;	/* nano-seconds */
278fd29cf72SStephen Hemminger 
2791da177e4SLinus Torvalds 	__u32 seq_num;
2801da177e4SLinus Torvalds 
28163adc6fbSStephen Hemminger 	int clone_skb;		/*
28263adc6fbSStephen Hemminger 				 * Use multiple SKBs during packet gen.
28363adc6fbSStephen Hemminger 				 * If this number is greater than 1, then
28463adc6fbSStephen Hemminger 				 * that many copies of the same packet will be
28563adc6fbSStephen Hemminger 				 * sent before a new packet is allocated.
28663adc6fbSStephen Hemminger 				 * If you want to send 1024 identical packets
28763adc6fbSStephen Hemminger 				 * before creating a new packet,
28863adc6fbSStephen Hemminger 				 * set clone_skb to 1024.
2891da177e4SLinus Torvalds 				 */
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	char dst_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2921da177e4SLinus Torvalds 	char dst_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2931da177e4SLinus Torvalds 	char src_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2941da177e4SLinus Torvalds 	char src_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	struct in6_addr in6_saddr;
2971da177e4SLinus Torvalds 	struct in6_addr in6_daddr;
2981da177e4SLinus Torvalds 	struct in6_addr cur_in6_daddr;
2991da177e4SLinus Torvalds 	struct in6_addr cur_in6_saddr;
3001da177e4SLinus Torvalds 	/* For ranges */
3011da177e4SLinus Torvalds 	struct in6_addr min_in6_daddr;
3021da177e4SLinus Torvalds 	struct in6_addr max_in6_daddr;
3031da177e4SLinus Torvalds 	struct in6_addr min_in6_saddr;
3041da177e4SLinus Torvalds 	struct in6_addr max_in6_saddr;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	/* If we're doing ranges, random or incremental, then this
3071da177e4SLinus Torvalds 	 * defines the min/max for those ranges.
3081da177e4SLinus Torvalds 	 */
309252e3346SAl Viro 	__be32 saddr_min;	/* inclusive, source IP address */
310252e3346SAl Viro 	__be32 saddr_max;	/* exclusive, source IP address */
311252e3346SAl Viro 	__be32 daddr_min;	/* inclusive, dest IP address */
312252e3346SAl Viro 	__be32 daddr_max;	/* exclusive, dest IP address */
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	__u16 udp_src_min;	/* inclusive, source UDP port */
3151da177e4SLinus Torvalds 	__u16 udp_src_max;	/* exclusive, source UDP port */
3161da177e4SLinus Torvalds 	__u16 udp_dst_min;	/* inclusive, dest UDP port */
3171da177e4SLinus Torvalds 	__u16 udp_dst_max;	/* exclusive, dest UDP port */
3181da177e4SLinus Torvalds 
3191ca7768cSFrancesco Fondelli 	/* DSCP + ECN */
32063adc6fbSStephen Hemminger 	__u8 tos;            /* six MSB of (former) IPv4 TOS
32163adc6fbSStephen Hemminger 				are for dscp codepoint */
32263adc6fbSStephen Hemminger 	__u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6
32363adc6fbSStephen Hemminger 				(see RFC 3260, sec. 4) */
3241ca7768cSFrancesco Fondelli 
325ca6549afSSteven Whitehouse 	/* MPLS */
32695c96174SEric Dumazet 	unsigned int nr_labels;	/* Depth of stack, 0 = no MPLS */
327ca6549afSSteven Whitehouse 	__be32 labels[MAX_MPLS_LABELS];
328ca6549afSSteven Whitehouse 
32934954ddcSFrancesco Fondelli 	/* VLAN/SVLAN (802.1Q/Q-in-Q) */
33034954ddcSFrancesco Fondelli 	__u8  vlan_p;
33134954ddcSFrancesco Fondelli 	__u8  vlan_cfi;
33234954ddcSFrancesco Fondelli 	__u16 vlan_id;  /* 0xffff means no vlan tag */
33334954ddcSFrancesco Fondelli 
33434954ddcSFrancesco Fondelli 	__u8  svlan_p;
33534954ddcSFrancesco Fondelli 	__u8  svlan_cfi;
33634954ddcSFrancesco Fondelli 	__u16 svlan_id; /* 0xffff means no svlan tag */
33734954ddcSFrancesco Fondelli 
3381da177e4SLinus Torvalds 	__u32 src_mac_count;	/* How many MACs to iterate through */
3391da177e4SLinus Torvalds 	__u32 dst_mac_count;	/* How many MACs to iterate through */
3401da177e4SLinus Torvalds 
341f404e9a6SKris Katterjohn 	unsigned char dst_mac[ETH_ALEN];
342f404e9a6SKris Katterjohn 	unsigned char src_mac[ETH_ALEN];
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	__u32 cur_dst_mac_offset;
3451da177e4SLinus Torvalds 	__u32 cur_src_mac_offset;
346252e3346SAl Viro 	__be32 cur_saddr;
347252e3346SAl Viro 	__be32 cur_daddr;
34866ed1e5eSEric Dumazet 	__u16 ip_id;
3491da177e4SLinus Torvalds 	__u16 cur_udp_dst;
3501da177e4SLinus Torvalds 	__u16 cur_udp_src;
35145b270f8SRobert Olsson 	__u16 cur_queue_map;
3521da177e4SLinus Torvalds 	__u32 cur_pkt_size;
353baac8564SEric Dumazet 	__u32 last_pkt_size;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	__u8 hh[14];
3561da177e4SLinus Torvalds 	/* = {
3571da177e4SLinus Torvalds 	   0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	   We fill in SRC address later
3601da177e4SLinus Torvalds 	   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3611da177e4SLinus Torvalds 	   0x08, 0x00
3621da177e4SLinus Torvalds 	   };
3631da177e4SLinus Torvalds 	 */
3641da177e4SLinus Torvalds 	__u16 pad;		/* pad out the hh struct to an even 16 bytes */
3651da177e4SLinus Torvalds 
36663adc6fbSStephen Hemminger 	struct sk_buff *skb;	/* skb we are to transmit next, used for when we
3671da177e4SLinus Torvalds 				 * are transmitting the same one multiple times
3681da177e4SLinus Torvalds 				 */
36963adc6fbSStephen Hemminger 	struct net_device *odev; /* The out-going device.
37063adc6fbSStephen Hemminger 				  * Note that the device should have it's
37163adc6fbSStephen Hemminger 				  * pg_info pointer pointing back to this
37263adc6fbSStephen Hemminger 				  * device.
37363adc6fbSStephen Hemminger 				  * Set when the user specifies the out-going
37463adc6fbSStephen Hemminger 				  * device name (not when the inject is
3751da177e4SLinus Torvalds 				  * started as it used to do.)
3761da177e4SLinus Torvalds 				  */
377593f63b0SEric Dumazet 	char odevname[32];
3781da177e4SLinus Torvalds 	struct flow_state *flows;
37995c96174SEric Dumazet 	unsigned int cflows;	/* Concurrent flows (config) */
38095c96174SEric Dumazet 	unsigned int lflow;		/* Flow length  (config) */
38195c96174SEric Dumazet 	unsigned int nflows;	/* accumulated flows (stats) */
38295c96174SEric Dumazet 	unsigned int curfl;		/* current sequenced flow (state)*/
38345b270f8SRobert Olsson 
38445b270f8SRobert Olsson 	u16 queue_map_min;
38545b270f8SRobert Olsson 	u16 queue_map_max;
3869e50e3acSJohn Fastabend 	__u32 skb_priority;	/* skb priority field */
387e99b99b4SRobert Olsson 	int node;               /* Memory node */
38845b270f8SRobert Olsson 
389a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
390a553e4a6SJamal Hadi Salim 	__u8	ipsmode;		/* IPSEC mode (config) */
391a553e4a6SJamal Hadi Salim 	__u8	ipsproto;		/* IPSEC type (config) */
392de4aee7dSFan Du 	__u32	spi;
393*cf93d47eSFan Du 	struct dst_entry dst;
394*cf93d47eSFan Du 	struct dst_ops dstops;
395a553e4a6SJamal Hadi Salim #endif
39639df232fSStephen Hemminger 	char result[512];
3971da177e4SLinus Torvalds };
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds struct pktgen_hdr {
400252e3346SAl Viro 	__be32 pgh_magic;
401252e3346SAl Viro 	__be32 seq_num;
402252e3346SAl Viro 	__be32 tv_sec;
403252e3346SAl Viro 	__be32 tv_usec;
4041da177e4SLinus Torvalds };
4051da177e4SLinus Torvalds 
4064e58a027SCong Wang 
4074e58a027SCong Wang static int pg_net_id __read_mostly;
4084e58a027SCong Wang 
4094e58a027SCong Wang struct pktgen_net {
4104e58a027SCong Wang 	struct net		*net;
4114e58a027SCong Wang 	struct proc_dir_entry	*proc_dir;
4124e58a027SCong Wang 	struct list_head	pktgen_threads;
4134e58a027SCong Wang 	bool			pktgen_exiting;
4144e58a027SCong Wang };
415551eaff1SEric Dumazet 
4161da177e4SLinus Torvalds struct pktgen_thread {
41763adc6fbSStephen Hemminger 	spinlock_t if_lock;		/* for list of devices */
418c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
419cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
420ee74baa7SDavid S. Miller 	struct task_struct *tsk;
4211da177e4SLinus Torvalds 	char result[512];
4221da177e4SLinus Torvalds 
42363adc6fbSStephen Hemminger 	/* Field for thread to receive "posted" events terminate,
42463adc6fbSStephen Hemminger 	   stop ifs etc. */
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	u32 control;
4271da177e4SLinus Torvalds 	int cpu;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	wait_queue_head_t queue;
430d3ede327SDenis V. Lunev 	struct completion start_done;
4314e58a027SCong Wang 	struct pktgen_net *net;
4321da177e4SLinus Torvalds };
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds #define REMOVE 1
4351da177e4SLinus Torvalds #define FIND   0
4361da177e4SLinus Torvalds 
437c3d2f52dSStephen Hemminger static const char version[] =
438f9467eaeSJoe Perches 	"Packet Generator for packet performance testing. "
439f9467eaeSJoe Perches 	"Version: " VERSION "\n";
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4421da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
443222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
4443e984840SEric Dumazet 					  const char *ifname, bool exact);
4451da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
4464e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn);
4474e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn);
4484e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
4493bda06a3SStephen Hemminger 
4501da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4511da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
45239df232fSStephen Hemminger 
4531da177e4SLinus Torvalds /* Module parameters, defaults. */
45465c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
45565c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
45665c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
45765c5b786SStephen Hemminger static int debug  __read_mostly;
4581da177e4SLinus Torvalds 
459222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4621da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4631da177e4SLinus Torvalds };
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds /*
4661da177e4SLinus Torvalds  * /proc handling functions
4671da177e4SLinus Torvalds  *
4681da177e4SLinus Torvalds  */
4691da177e4SLinus Torvalds 
470d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
471d50a6b56SStephen Hemminger {
472c3d2f52dSStephen Hemminger 	seq_puts(seq, version);
473d50a6b56SStephen Hemminger 	return 0;
474d50a6b56SStephen Hemminger }
4751da177e4SLinus Torvalds 
476d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user *buf,
4771da177e4SLinus Torvalds 			    size_t count, loff_t *ppos)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	int err = 0;
480d50a6b56SStephen Hemminger 	char data[128];
4814e58a027SCong Wang 	struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN)) {
4841da177e4SLinus Torvalds 		err = -EPERM;
4851da177e4SLinus Torvalds 		goto out;
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 
488d50a6b56SStephen Hemminger 	if (count > sizeof(data))
489d50a6b56SStephen Hemminger 		count = sizeof(data);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count)) {
4921da177e4SLinus Torvalds 		err = -EFAULT;
493d50a6b56SStephen Hemminger 		goto out;
4941da177e4SLinus Torvalds 	}
4951da177e4SLinus Torvalds 	data[count - 1] = 0;	/* Make string */
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
4984e58a027SCong Wang 		pktgen_stop_all_threads_ifs(pn);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
5014e58a027SCong Wang 		pktgen_run_all_threads(pn);
5021da177e4SLinus Torvalds 
503eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
5044e58a027SCong Wang 		pktgen_reset_all_threads(pn);
505eb37b41cSJesse Brandeburg 
5061da177e4SLinus Torvalds 	else
507f9467eaeSJoe Perches 		pr_warning("Unknown command: %s\n", data);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	err = count;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds out:
5121da177e4SLinus Torvalds 	return err;
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
515d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
5161da177e4SLinus Torvalds {
517d9dda78bSAl Viro 	return single_open(file, pgctrl_show, PDE_DATA(inode));
518d50a6b56SStephen Hemminger }
519d50a6b56SStephen Hemminger 
5209a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
521d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
522d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
523d50a6b56SStephen Hemminger 	.read    = seq_read,
524d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
525d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
526d50a6b56SStephen Hemminger 	.release = single_release,
527d50a6b56SStephen Hemminger };
528d50a6b56SStephen Hemminger 
529d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
530d50a6b56SStephen Hemminger {
531648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
532fd29cf72SStephen Hemminger 	ktime_t stopped;
533fd29cf72SStephen Hemminger 	u64 idle;
5341da177e4SLinus Torvalds 
535222f1806SLuiz Capitulino 	seq_printf(seq,
536222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
537222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
538222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5391da177e4SLinus Torvalds 
540222f1806SLuiz Capitulino 	seq_printf(seq,
541fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
542fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
543593f63b0SEric Dumazet 		   pkt_dev->clone_skb, pkt_dev->odevname);
5441da177e4SLinus Torvalds 
545222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
546222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5471da177e4SLinus Torvalds 
54845b270f8SRobert Olsson 	seq_printf(seq,
54945b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
55045b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
55145b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
55245b270f8SRobert Olsson 
5539e50e3acSJohn Fastabend 	if (pkt_dev->skb_priority)
5549e50e3acSJohn Fastabend 		seq_printf(seq, "     skb_priority: %u\n",
5559e50e3acSJohn Fastabend 			   pkt_dev->skb_priority);
5569e50e3acSJohn Fastabend 
5571da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
558222f1806SLuiz Capitulino 		seq_printf(seq,
55947a0200dSAlexey Dobriyan 			   "     saddr: %pI6c  min_saddr: %pI6c  max_saddr: %pI6c\n"
56047a0200dSAlexey Dobriyan 			   "     daddr: %pI6c  min_daddr: %pI6c  max_daddr: %pI6c\n",
56147a0200dSAlexey Dobriyan 			   &pkt_dev->in6_saddr,
56247a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
56347a0200dSAlexey Dobriyan 			   &pkt_dev->in6_daddr,
56447a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
56563adc6fbSStephen Hemminger 	} else {
566222f1806SLuiz Capitulino 		seq_printf(seq,
56763adc6fbSStephen Hemminger 			   "     dst_min: %s  dst_max: %s\n",
56863adc6fbSStephen Hemminger 			   pkt_dev->dst_min, pkt_dev->dst_max);
56963adc6fbSStephen Hemminger 		seq_printf(seq,
57063adc6fbSStephen Hemminger 			   "        src_min: %s  src_max: %s\n",
57163adc6fbSStephen Hemminger 			   pkt_dev->src_min, pkt_dev->src_max);
57263adc6fbSStephen Hemminger 	}
5731da177e4SLinus Torvalds 
574d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5751da177e4SLinus Torvalds 
576e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
577e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
578e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5791da177e4SLinus Torvalds 
580d50a6b56SStephen Hemminger 	seq_printf(seq, "dst_mac: ");
581e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
5821da177e4SLinus Torvalds 
583222f1806SLuiz Capitulino 	seq_printf(seq,
58463adc6fbSStephen Hemminger 		   "     udp_src_min: %d  udp_src_max: %d"
58563adc6fbSStephen Hemminger 		   "  udp_dst_min: %d  udp_dst_max: %d\n",
586222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
587222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
5881da177e4SLinus Torvalds 
589222f1806SLuiz Capitulino 	seq_printf(seq,
590ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
5911da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
5921da177e4SLinus Torvalds 
593ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
59495c96174SEric Dumazet 		unsigned int i;
595ca6549afSSteven Whitehouse 		seq_printf(seq, "     mpls: ");
596ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
597ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
598ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
599ca6549afSSteven Whitehouse 	}
600ca6549afSSteven Whitehouse 
60163adc6fbSStephen Hemminger 	if (pkt_dev->vlan_id != 0xffff)
60234954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
60363adc6fbSStephen Hemminger 			   pkt_dev->vlan_id, pkt_dev->vlan_p,
60463adc6fbSStephen Hemminger 			   pkt_dev->vlan_cfi);
60534954ddcSFrancesco Fondelli 
60663adc6fbSStephen Hemminger 	if (pkt_dev->svlan_id != 0xffff)
60734954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
60863adc6fbSStephen Hemminger 			   pkt_dev->svlan_id, pkt_dev->svlan_p,
60963adc6fbSStephen Hemminger 			   pkt_dev->svlan_cfi);
61034954ddcSFrancesco Fondelli 
61163adc6fbSStephen Hemminger 	if (pkt_dev->tos)
6121ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
6131ca7768cSFrancesco Fondelli 
61463adc6fbSStephen Hemminger 	if (pkt_dev->traffic_class)
6151ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
6161ca7768cSFrancesco Fondelli 
617e99b99b4SRobert Olsson 	if (pkt_dev->node >= 0)
618e99b99b4SRobert Olsson 		seq_printf(seq, "     node: %d\n", pkt_dev->node);
619e99b99b4SRobert Olsson 
620ca6549afSSteven Whitehouse 	seq_printf(seq, "     Flags: ");
621ca6549afSSteven Whitehouse 
6221da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
623d50a6b56SStephen Hemminger 		seq_printf(seq, "IPV6  ");
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPSRC_RND)
626d50a6b56SStephen Hemminger 		seq_printf(seq, "IPSRC_RND  ");
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPDST_RND)
629d50a6b56SStephen Hemminger 		seq_printf(seq, "IPDST_RND  ");
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if (pkt_dev->flags & F_TXSIZE_RND)
632d50a6b56SStephen Hemminger 		seq_printf(seq, "TXSIZE_RND  ");
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPSRC_RND)
635d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPSRC_RND  ");
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPDST_RND)
638d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPDST_RND  ");
6391da177e4SLinus Torvalds 
640c26bf4a5SThomas Graf 	if (pkt_dev->flags & F_UDPCSUM)
641c26bf4a5SThomas Graf 		seq_printf(seq, "UDPCSUM  ");
642c26bf4a5SThomas Graf 
643ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND)
644ca6549afSSteven Whitehouse 		seq_printf(seq,  "MPLS_RND  ");
645ca6549afSSteven Whitehouse 
64645b270f8SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_RND)
64745b270f8SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_RND  ");
64845b270f8SRobert Olsson 
649e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
650e6fce5b9SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_CPU  ");
651e6fce5b9SRobert Olsson 
652007a531bSJamal Hadi Salim 	if (pkt_dev->cflows) {
653007a531bSJamal Hadi Salim 		if (pkt_dev->flags & F_FLOW_SEQ)
654007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_SEQ  "); /*in sequence flows*/
655007a531bSJamal Hadi Salim 		else
656007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_RND  ");
657007a531bSJamal Hadi Salim 	}
658007a531bSJamal Hadi Salim 
659a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
660a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON)
661a553e4a6SJamal Hadi Salim 		seq_printf(seq,  "IPSEC  ");
662a553e4a6SJamal Hadi Salim #endif
663a553e4a6SJamal Hadi Salim 
6641da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACSRC_RND)
665d50a6b56SStephen Hemminger 		seq_printf(seq, "MACSRC_RND  ");
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACDST_RND)
668d50a6b56SStephen Hemminger 		seq_printf(seq, "MACDST_RND  ");
6691da177e4SLinus Torvalds 
67034954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_VID_RND)
67134954ddcSFrancesco Fondelli 		seq_printf(seq, "VID_RND  ");
67234954ddcSFrancesco Fondelli 
67334954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_SVID_RND)
67434954ddcSFrancesco Fondelli 		seq_printf(seq, "SVID_RND  ");
67534954ddcSFrancesco Fondelli 
676e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE)
677e99b99b4SRobert Olsson 		seq_printf(seq, "NODE_ALLOC  ");
678e99b99b4SRobert Olsson 
679d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6801da177e4SLinus Torvalds 
681fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
682398f382cSDaniel Borkmann 	stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
683fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
684fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
6851da177e4SLinus Torvalds 
686222f1806SLuiz Capitulino 	seq_printf(seq,
687fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
6881da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
689fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
690fd29cf72SStephen Hemminger 
691fd29cf72SStephen Hemminger 	seq_printf(seq,
692fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
693fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
694fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
695fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
6961da177e4SLinus Torvalds 
697222f1806SLuiz Capitulino 	seq_printf(seq,
698222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
699d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
700d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
70347a0200dSAlexey Dobriyan 		seq_printf(seq, "     cur_saddr: %pI6c  cur_daddr: %pI6c\n",
70447a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_saddr,
70547a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_daddr);
706222f1806SLuiz Capitulino 	} else
7070373a946SAmerigo Wang 		seq_printf(seq, "     cur_saddr: %pI4  cur_daddr: %pI4\n",
7080373a946SAmerigo Wang 			   &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
7091da177e4SLinus Torvalds 
710d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
7111da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
7121da177e4SLinus Torvalds 
71345b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
71445b270f8SRobert Olsson 
715d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	if (pkt_dev->result[0])
718d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
7191da177e4SLinus Torvalds 	else
720d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: Idle\n");
7211da177e4SLinus Torvalds 
722d50a6b56SStephen Hemminger 	return 0;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
725ca6549afSSteven Whitehouse 
72663adc6fbSStephen Hemminger static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
72763adc6fbSStephen Hemminger 		     __u32 *num)
728ca6549afSSteven Whitehouse {
729ca6549afSSteven Whitehouse 	int i = 0;
730ca6549afSSteven Whitehouse 	*num = 0;
731ca6549afSSteven Whitehouse 
7321ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
73382fd5b5dSAndy Shevchenko 		int value;
734ca6549afSSteven Whitehouse 		char c;
735ca6549afSSteven Whitehouse 		*num <<= 4;
736ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
737ca6549afSSteven Whitehouse 			return -EFAULT;
73882fd5b5dSAndy Shevchenko 		value = hex_to_bin(c);
73982fd5b5dSAndy Shevchenko 		if (value >= 0)
74082fd5b5dSAndy Shevchenko 			*num |= value;
741ca6549afSSteven Whitehouse 		else
742ca6549afSSteven Whitehouse 			break;
743ca6549afSSteven Whitehouse 	}
744ca6549afSSteven Whitehouse 	return i;
745ca6549afSSteven Whitehouse }
746ca6549afSSteven Whitehouse 
747222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
748222f1806SLuiz Capitulino 			     unsigned int maxlen)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	int i;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7531da177e4SLinus Torvalds 		char c;
7541da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7551da177e4SLinus Torvalds 			return -EFAULT;
7561da177e4SLinus Torvalds 		switch (c) {
7571da177e4SLinus Torvalds 		case '\"':
7581da177e4SLinus Torvalds 		case '\n':
7591da177e4SLinus Torvalds 		case '\r':
7601da177e4SLinus Torvalds 		case '\t':
7611da177e4SLinus Torvalds 		case ' ':
7621da177e4SLinus Torvalds 		case '=':
7631da177e4SLinus Torvalds 			break;
7641da177e4SLinus Torvalds 		default:
7651da177e4SLinus Torvalds 			goto done;
7663ff50b79SStephen Hemminger 		}
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds done:
7691da177e4SLinus Torvalds 	return i;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
772bf0813bdSPaul Gortmaker static long num_arg(const char __user *user_buffer, unsigned long maxlen,
773bf0813bdSPaul Gortmaker 				unsigned long *num)
7741da177e4SLinus Torvalds {
775d6182223SPaul Gortmaker 	int i;
7761da177e4SLinus Torvalds 	*num = 0;
7771da177e4SLinus Torvalds 
778d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7791da177e4SLinus Torvalds 		char c;
7801da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7811da177e4SLinus Torvalds 			return -EFAULT;
7821da177e4SLinus Torvalds 		if ((c >= '0') && (c <= '9')) {
7831da177e4SLinus Torvalds 			*num *= 10;
7841da177e4SLinus Torvalds 			*num += c - '0';
7851da177e4SLinus Torvalds 		} else
7861da177e4SLinus Torvalds 			break;
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 	return i;
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
7921da177e4SLinus Torvalds {
793d6182223SPaul Gortmaker 	int i;
7941da177e4SLinus Torvalds 
795d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7961da177e4SLinus Torvalds 		char c;
7971da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7981da177e4SLinus Torvalds 			return -EFAULT;
7991da177e4SLinus Torvalds 		switch (c) {
8001da177e4SLinus Torvalds 		case '\"':
8011da177e4SLinus Torvalds 		case '\n':
8021da177e4SLinus Torvalds 		case '\r':
8031da177e4SLinus Torvalds 		case '\t':
8041da177e4SLinus Torvalds 		case ' ':
8051da177e4SLinus Torvalds 			goto done_str;
8061da177e4SLinus Torvalds 			break;
8071da177e4SLinus Torvalds 		default:
8081da177e4SLinus Torvalds 			break;
8093ff50b79SStephen Hemminger 		}
8101da177e4SLinus Torvalds 	}
8111da177e4SLinus Torvalds done_str:
8121da177e4SLinus Torvalds 	return i;
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
815ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
816ca6549afSSteven Whitehouse {
81795c96174SEric Dumazet 	unsigned int n = 0;
818ca6549afSSteven Whitehouse 	char c;
819ca6549afSSteven Whitehouse 	ssize_t i = 0;
820ca6549afSSteven Whitehouse 	int len;
821ca6549afSSteven Whitehouse 
822ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = 0;
823ca6549afSSteven Whitehouse 	do {
824ca6549afSSteven Whitehouse 		__u32 tmp;
8251ca7768cSFrancesco Fondelli 		len = hex32_arg(&buffer[i], 8, &tmp);
826ca6549afSSteven Whitehouse 		if (len <= 0)
827ca6549afSSteven Whitehouse 			return len;
828ca6549afSSteven Whitehouse 		pkt_dev->labels[n] = htonl(tmp);
829ca6549afSSteven Whitehouse 		if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
830ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
831ca6549afSSteven Whitehouse 		i += len;
832ca6549afSSteven Whitehouse 		if (get_user(c, &buffer[i]))
833ca6549afSSteven Whitehouse 			return -EFAULT;
834ca6549afSSteven Whitehouse 		i++;
835ca6549afSSteven Whitehouse 		n++;
836ca6549afSSteven Whitehouse 		if (n >= MAX_MPLS_LABELS)
837ca6549afSSteven Whitehouse 			return -E2BIG;
838ca6549afSSteven Whitehouse 	} while (c == ',');
839ca6549afSSteven Whitehouse 
840ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = n;
841ca6549afSSteven Whitehouse 	return i;
842ca6549afSSteven Whitehouse }
843ca6549afSSteven Whitehouse 
844222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file,
845222f1806SLuiz Capitulino 			       const char __user * user_buffer, size_t count,
846222f1806SLuiz Capitulino 			       loff_t * offset)
8471da177e4SLinus Torvalds {
8488a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
849d50a6b56SStephen Hemminger 	struct pktgen_dev *pkt_dev = seq->private;
850d6182223SPaul Gortmaker 	int i, max, len;
8511da177e4SLinus Torvalds 	char name[16], valstr[32];
8521da177e4SLinus Torvalds 	unsigned long value = 0;
8531da177e4SLinus Torvalds 	char *pg_result = NULL;
8541da177e4SLinus Torvalds 	int tmp = 0;
8551da177e4SLinus Torvalds 	char buf[128];
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	pg_result = &(pkt_dev->result[0]);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	if (count < 1) {
860f9467eaeSJoe Perches 		pr_warning("wrong command format\n");
8611da177e4SLinus Torvalds 		return -EINVAL;
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 
864d6182223SPaul Gortmaker 	max = count;
865d6182223SPaul Gortmaker 	tmp = count_trail_chars(user_buffer, max);
8661da177e4SLinus Torvalds 	if (tmp < 0) {
867f9467eaeSJoe Perches 		pr_warning("illegal format\n");
8681da177e4SLinus Torvalds 		return tmp;
8691da177e4SLinus Torvalds 	}
870d6182223SPaul Gortmaker 	i = tmp;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	/* Read variable name */
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
87563adc6fbSStephen Hemminger 	if (len < 0)
876222f1806SLuiz Capitulino 		return len;
87763adc6fbSStephen Hemminger 
8781da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
8791da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
8801da177e4SLinus Torvalds 		return -EFAULT;
8811da177e4SLinus Torvalds 	i += len;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	max = count - i;
8841da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
8851da177e4SLinus Torvalds 	if (len < 0)
8861da177e4SLinus Torvalds 		return len;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	i += len;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	if (debug) {
89186c2c0a8SDmitry Torokhov 		size_t copy = min_t(size_t, count, 1023);
892448d7b5dSNelson Elhage 		char tb[copy + 1];
893448d7b5dSNelson Elhage 		if (copy_from_user(tb, user_buffer, copy))
8941da177e4SLinus Torvalds 			return -EFAULT;
895448d7b5dSNelson Elhage 		tb[copy] = 0;
896f342cda7SJoe Perches 		pr_debug("%s,%lu  buffer -:%s:-\n",
897f342cda7SJoe Perches 			 name, (unsigned long)count, tb);
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	if (!strcmp(name, "min_pkt_size")) {
9011da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
90263adc6fbSStephen Hemminger 		if (len < 0)
903222f1806SLuiz Capitulino 			return len;
90463adc6fbSStephen Hemminger 
9051da177e4SLinus Torvalds 		i += len;
9061da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9071da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9081da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9091da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9101da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9111da177e4SLinus Torvalds 		}
912222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: min_pkt_size=%u",
913222f1806SLuiz Capitulino 			pkt_dev->min_pkt_size);
9141da177e4SLinus Torvalds 		return count;
9151da177e4SLinus Torvalds 	}
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	if (!strcmp(name, "max_pkt_size")) {
9181da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
91963adc6fbSStephen Hemminger 		if (len < 0)
920222f1806SLuiz Capitulino 			return len;
92163adc6fbSStephen Hemminger 
9221da177e4SLinus Torvalds 		i += len;
9231da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9241da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9251da177e4SLinus Torvalds 		if (value != pkt_dev->max_pkt_size) {
9261da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9271da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9281da177e4SLinus Torvalds 		}
929222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: max_pkt_size=%u",
930222f1806SLuiz Capitulino 			pkt_dev->max_pkt_size);
9311da177e4SLinus Torvalds 		return count;
9321da177e4SLinus Torvalds 	}
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	/* Shortcut for min = max */
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	if (!strcmp(name, "pkt_size")) {
9371da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
93863adc6fbSStephen Hemminger 		if (len < 0)
939222f1806SLuiz Capitulino 			return len;
94063adc6fbSStephen Hemminger 
9411da177e4SLinus Torvalds 		i += len;
9421da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9431da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9441da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9451da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9461da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9471da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9481da177e4SLinus Torvalds 		}
9491da177e4SLinus Torvalds 		sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
9501da177e4SLinus Torvalds 		return count;
9511da177e4SLinus Torvalds 	}
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	if (!strcmp(name, "debug")) {
9541da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
95563adc6fbSStephen Hemminger 		if (len < 0)
956222f1806SLuiz Capitulino 			return len;
95763adc6fbSStephen Hemminger 
9581da177e4SLinus Torvalds 		i += len;
9591da177e4SLinus Torvalds 		debug = value;
9601da177e4SLinus Torvalds 		sprintf(pg_result, "OK: debug=%u", debug);
9611da177e4SLinus Torvalds 		return count;
9621da177e4SLinus Torvalds 	}
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	if (!strcmp(name, "frags")) {
9651da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
96663adc6fbSStephen Hemminger 		if (len < 0)
967222f1806SLuiz Capitulino 			return len;
96863adc6fbSStephen Hemminger 
9691da177e4SLinus Torvalds 		i += len;
9701da177e4SLinus Torvalds 		pkt_dev->nfrags = value;
9711da177e4SLinus Torvalds 		sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
9721da177e4SLinus Torvalds 		return count;
9731da177e4SLinus Torvalds 	}
9741da177e4SLinus Torvalds 	if (!strcmp(name, "delay")) {
9751da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
97663adc6fbSStephen Hemminger 		if (len < 0)
977222f1806SLuiz Capitulino 			return len;
97863adc6fbSStephen Hemminger 
9791da177e4SLinus Torvalds 		i += len;
980fd29cf72SStephen Hemminger 		if (value == 0x7FFFFFFF)
981fd29cf72SStephen Hemminger 			pkt_dev->delay = ULLONG_MAX;
982fd29cf72SStephen Hemminger 		else
9839240d715SEric Dumazet 			pkt_dev->delay = (u64)value;
984fd29cf72SStephen Hemminger 
985fd29cf72SStephen Hemminger 		sprintf(pg_result, "OK: delay=%llu",
986fd29cf72SStephen Hemminger 			(unsigned long long) pkt_dev->delay);
9871da177e4SLinus Torvalds 		return count;
9881da177e4SLinus Torvalds 	}
98943d28b65SDaniel Turull 	if (!strcmp(name, "rate")) {
99043d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
99143d28b65SDaniel Turull 		if (len < 0)
99243d28b65SDaniel Turull 			return len;
99343d28b65SDaniel Turull 
99443d28b65SDaniel Turull 		i += len;
99543d28b65SDaniel Turull 		if (!value)
99643d28b65SDaniel Turull 			return len;
99743d28b65SDaniel Turull 		pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
99843d28b65SDaniel Turull 		if (debug)
999f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
100043d28b65SDaniel Turull 
100143d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
100243d28b65SDaniel Turull 		return count;
100343d28b65SDaniel Turull 	}
100443d28b65SDaniel Turull 	if (!strcmp(name, "ratep")) {
100543d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
100643d28b65SDaniel Turull 		if (len < 0)
100743d28b65SDaniel Turull 			return len;
100843d28b65SDaniel Turull 
100943d28b65SDaniel Turull 		i += len;
101043d28b65SDaniel Turull 		if (!value)
101143d28b65SDaniel Turull 			return len;
101243d28b65SDaniel Turull 		pkt_dev->delay = NSEC_PER_SEC/value;
101343d28b65SDaniel Turull 		if (debug)
1014f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
101543d28b65SDaniel Turull 
101643d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
101743d28b65SDaniel Turull 		return count;
101843d28b65SDaniel Turull 	}
10191da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_min")) {
10201da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
102163adc6fbSStephen Hemminger 		if (len < 0)
1022222f1806SLuiz Capitulino 			return len;
102363adc6fbSStephen Hemminger 
10241da177e4SLinus Torvalds 		i += len;
10251da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_min) {
10261da177e4SLinus Torvalds 			pkt_dev->udp_src_min = value;
10271da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10281da177e4SLinus Torvalds 		}
10291da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
10301da177e4SLinus Torvalds 		return count;
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_min")) {
10331da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
103463adc6fbSStephen Hemminger 		if (len < 0)
1035222f1806SLuiz Capitulino 			return len;
103663adc6fbSStephen Hemminger 
10371da177e4SLinus Torvalds 		i += len;
10381da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_min) {
10391da177e4SLinus Torvalds 			pkt_dev->udp_dst_min = value;
10401da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10411da177e4SLinus Torvalds 		}
10421da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
10431da177e4SLinus Torvalds 		return count;
10441da177e4SLinus Torvalds 	}
10451da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_max")) {
10461da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
104763adc6fbSStephen Hemminger 		if (len < 0)
1048222f1806SLuiz Capitulino 			return len;
104963adc6fbSStephen Hemminger 
10501da177e4SLinus Torvalds 		i += len;
10511da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_max) {
10521da177e4SLinus Torvalds 			pkt_dev->udp_src_max = value;
10531da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10541da177e4SLinus Torvalds 		}
10551da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
10561da177e4SLinus Torvalds 		return count;
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_max")) {
10591da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
106063adc6fbSStephen Hemminger 		if (len < 0)
1061222f1806SLuiz Capitulino 			return len;
106263adc6fbSStephen Hemminger 
10631da177e4SLinus Torvalds 		i += len;
10641da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_max) {
10651da177e4SLinus Torvalds 			pkt_dev->udp_dst_max = value;
10661da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10671da177e4SLinus Torvalds 		}
10681da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
10691da177e4SLinus Torvalds 		return count;
10701da177e4SLinus Torvalds 	}
10711da177e4SLinus Torvalds 	if (!strcmp(name, "clone_skb")) {
10721da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
107363adc6fbSStephen Hemminger 		if (len < 0)
1074222f1806SLuiz Capitulino 			return len;
1075d8873315SNeil Horman 		if ((value > 0) &&
1076d8873315SNeil Horman 		    (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1077d8873315SNeil Horman 			return -ENOTSUPP;
10781da177e4SLinus Torvalds 		i += len;
10791da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
10821da177e4SLinus Torvalds 		return count;
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
10851da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
108663adc6fbSStephen Hemminger 		if (len < 0)
1087222f1806SLuiz Capitulino 			return len;
108863adc6fbSStephen Hemminger 
10891da177e4SLinus Torvalds 		i += len;
10901da177e4SLinus Torvalds 		pkt_dev->count = value;
10911da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
10921da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
10931da177e4SLinus Torvalds 		return count;
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
10961da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
109763adc6fbSStephen Hemminger 		if (len < 0)
1098222f1806SLuiz Capitulino 			return len;
109963adc6fbSStephen Hemminger 
11001da177e4SLinus Torvalds 		i += len;
11011da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
11021da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
11031da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
11041da177e4SLinus Torvalds 		}
1105222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1106222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
11071da177e4SLinus Torvalds 		return count;
11081da177e4SLinus Torvalds 	}
11091da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
11101da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
111163adc6fbSStephen Hemminger 		if (len < 0)
1112222f1806SLuiz Capitulino 			return len;
111363adc6fbSStephen Hemminger 
11141da177e4SLinus Torvalds 		i += len;
11151da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
11161da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
11171da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
11181da177e4SLinus Torvalds 		}
1119222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1120222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
11211da177e4SLinus Torvalds 		return count;
11221da177e4SLinus Torvalds 	}
1123e99b99b4SRobert Olsson 	if (!strcmp(name, "node")) {
1124e99b99b4SRobert Olsson 		len = num_arg(&user_buffer[i], 10, &value);
1125e99b99b4SRobert Olsson 		if (len < 0)
1126e99b99b4SRobert Olsson 			return len;
1127e99b99b4SRobert Olsson 
1128e99b99b4SRobert Olsson 		i += len;
1129e99b99b4SRobert Olsson 
1130e99b99b4SRobert Olsson 		if (node_possible(value)) {
1131e99b99b4SRobert Olsson 			pkt_dev->node = value;
1132e99b99b4SRobert Olsson 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
113326ad7879SEric Dumazet 			if (pkt_dev->page) {
113426ad7879SEric Dumazet 				put_page(pkt_dev->page);
113526ad7879SEric Dumazet 				pkt_dev->page = NULL;
113626ad7879SEric Dumazet 			}
1137e99b99b4SRobert Olsson 		}
1138e99b99b4SRobert Olsson 		else
1139e99b99b4SRobert Olsson 			sprintf(pg_result, "ERROR: node not possible");
1140e99b99b4SRobert Olsson 		return count;
1141e99b99b4SRobert Olsson 	}
11421da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
11431da177e4SLinus Torvalds 		char f[32];
11441da177e4SLinus Torvalds 		memset(f, 0, 32);
11451da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
114663adc6fbSStephen Hemminger 		if (len < 0)
1147222f1806SLuiz Capitulino 			return len;
114863adc6fbSStephen Hemminger 
11491da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
11501da177e4SLinus Torvalds 			return -EFAULT;
11511da177e4SLinus Torvalds 		i += len;
11521da177e4SLinus Torvalds 		if (strcmp(f, "IPSRC_RND") == 0)
11531da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPSRC_RND;
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 		else if (strcmp(f, "!IPSRC_RND") == 0)
11561da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPSRC_RND;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 		else if (strcmp(f, "TXSIZE_RND") == 0)
11591da177e4SLinus Torvalds 			pkt_dev->flags |= F_TXSIZE_RND;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 		else if (strcmp(f, "!TXSIZE_RND") == 0)
11621da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_TXSIZE_RND;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 		else if (strcmp(f, "IPDST_RND") == 0)
11651da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPDST_RND;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 		else if (strcmp(f, "!IPDST_RND") == 0)
11681da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPDST_RND;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		else if (strcmp(f, "UDPSRC_RND") == 0)
11711da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPSRC_RND;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPSRC_RND") == 0)
11741da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPSRC_RND;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 		else if (strcmp(f, "UDPDST_RND") == 0)
11771da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPDST_RND;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPDST_RND") == 0)
11801da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPDST_RND;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 		else if (strcmp(f, "MACSRC_RND") == 0)
11831da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACSRC_RND;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 		else if (strcmp(f, "!MACSRC_RND") == 0)
11861da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACSRC_RND;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 		else if (strcmp(f, "MACDST_RND") == 0)
11891da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACDST_RND;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 		else if (strcmp(f, "!MACDST_RND") == 0)
11921da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACDST_RND;
11931da177e4SLinus Torvalds 
1194ca6549afSSteven Whitehouse 		else if (strcmp(f, "MPLS_RND") == 0)
1195ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
1196ca6549afSSteven Whitehouse 
1197ca6549afSSteven Whitehouse 		else if (strcmp(f, "!MPLS_RND") == 0)
1198ca6549afSSteven Whitehouse 			pkt_dev->flags &= ~F_MPLS_RND;
1199ca6549afSSteven Whitehouse 
120034954ddcSFrancesco Fondelli 		else if (strcmp(f, "VID_RND") == 0)
120134954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_VID_RND;
120234954ddcSFrancesco Fondelli 
120334954ddcSFrancesco Fondelli 		else if (strcmp(f, "!VID_RND") == 0)
120434954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_VID_RND;
120534954ddcSFrancesco Fondelli 
120634954ddcSFrancesco Fondelli 		else if (strcmp(f, "SVID_RND") == 0)
120734954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_SVID_RND;
120834954ddcSFrancesco Fondelli 
120934954ddcSFrancesco Fondelli 		else if (strcmp(f, "!SVID_RND") == 0)
121034954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_SVID_RND;
121134954ddcSFrancesco Fondelli 
1212007a531bSJamal Hadi Salim 		else if (strcmp(f, "FLOW_SEQ") == 0)
1213007a531bSJamal Hadi Salim 			pkt_dev->flags |= F_FLOW_SEQ;
1214007a531bSJamal Hadi Salim 
121545b270f8SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
121645b270f8SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_RND;
121745b270f8SRobert Olsson 
121845b270f8SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
121945b270f8SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1220e6fce5b9SRobert Olsson 
1221e6fce5b9SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1222e6fce5b9SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1223e6fce5b9SRobert Olsson 
1224e6fce5b9SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1225e6fce5b9SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1226a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
1227a553e4a6SJamal Hadi Salim 		else if (strcmp(f, "IPSEC") == 0)
1228a553e4a6SJamal Hadi Salim 			pkt_dev->flags |= F_IPSEC_ON;
1229a553e4a6SJamal Hadi Salim #endif
1230a553e4a6SJamal Hadi Salim 
12311ca7768cSFrancesco Fondelli 		else if (strcmp(f, "!IPV6") == 0)
12321ca7768cSFrancesco Fondelli 			pkt_dev->flags &= ~F_IPV6;
12331ca7768cSFrancesco Fondelli 
1234e99b99b4SRobert Olsson 		else if (strcmp(f, "NODE_ALLOC") == 0)
1235e99b99b4SRobert Olsson 			pkt_dev->flags |= F_NODE;
1236e99b99b4SRobert Olsson 
1237e99b99b4SRobert Olsson 		else if (strcmp(f, "!NODE_ALLOC") == 0)
1238e99b99b4SRobert Olsson 			pkt_dev->flags &= ~F_NODE;
1239e99b99b4SRobert Olsson 
1240c26bf4a5SThomas Graf 		else if (strcmp(f, "UDPCSUM") == 0)
1241c26bf4a5SThomas Graf 			pkt_dev->flags |= F_UDPCSUM;
1242c26bf4a5SThomas Graf 
1243c26bf4a5SThomas Graf 		else if (strcmp(f, "!UDPCSUM") == 0)
1244c26bf4a5SThomas Graf 			pkt_dev->flags &= ~F_UDPCSUM;
1245c26bf4a5SThomas Graf 
12461da177e4SLinus Torvalds 		else {
1247222f1806SLuiz Capitulino 			sprintf(pg_result,
1248222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
12491da177e4SLinus Torvalds 				f,
12501ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1251e99b99b4SRobert Olsson 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
12521da177e4SLinus Torvalds 			return count;
12531da177e4SLinus Torvalds 		}
12541da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
12551da177e4SLinus Torvalds 		return count;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
12581da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
125963adc6fbSStephen Hemminger 		if (len < 0)
1260222f1806SLuiz Capitulino 			return len;
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12631da177e4SLinus Torvalds 			return -EFAULT;
12641da177e4SLinus Torvalds 		buf[len] = 0;
12651da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
12661da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
12671da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_min, buf, len);
12681da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
12691da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
12701da177e4SLinus Torvalds 		}
12711da177e4SLinus Torvalds 		if (debug)
1272f342cda7SJoe Perches 			pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
12731da177e4SLinus Torvalds 		i += len;
12741da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
12751da177e4SLinus Torvalds 		return count;
12761da177e4SLinus Torvalds 	}
12771da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
12781da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
127963adc6fbSStephen Hemminger 		if (len < 0)
1280222f1806SLuiz Capitulino 			return len;
128163adc6fbSStephen Hemminger 
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12841da177e4SLinus Torvalds 			return -EFAULT;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 		buf[len] = 0;
12871da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
12881da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
12891da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_max, buf, len);
12901da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
12911da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
12921da177e4SLinus Torvalds 		}
12931da177e4SLinus Torvalds 		if (debug)
1294f342cda7SJoe Perches 			pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
12951da177e4SLinus Torvalds 		i += len;
12961da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
12971da177e4SLinus Torvalds 		return count;
12981da177e4SLinus Torvalds 	}
12991da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
13001da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1301222f1806SLuiz Capitulino 		if (len < 0)
1302222f1806SLuiz Capitulino 			return len;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13071da177e4SLinus Torvalds 			return -EFAULT;
13081da177e4SLinus Torvalds 		buf[len] = 0;
13091da177e4SLinus Torvalds 
1310c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
131147a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
13121da177e4SLinus Torvalds 
13134e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 		if (debug)
1316f342cda7SJoe Perches 			pr_debug("dst6 set to: %s\n", buf);
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 		i += len;
13191da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
13201da177e4SLinus Torvalds 		return count;
13211da177e4SLinus Torvalds 	}
13221da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
13231da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1324222f1806SLuiz Capitulino 		if (len < 0)
1325222f1806SLuiz Capitulino 			return len;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13301da177e4SLinus Torvalds 			return -EFAULT;
13311da177e4SLinus Torvalds 		buf[len] = 0;
13321da177e4SLinus Torvalds 
1333c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
133447a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
13351da177e4SLinus Torvalds 
13364e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
13371da177e4SLinus Torvalds 		if (debug)
1338f342cda7SJoe Perches 			pr_debug("dst6_min set to: %s\n", buf);
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 		i += len;
13411da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
13421da177e4SLinus Torvalds 		return count;
13431da177e4SLinus Torvalds 	}
13441da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
13451da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1346222f1806SLuiz Capitulino 		if (len < 0)
1347222f1806SLuiz Capitulino 			return len;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13521da177e4SLinus Torvalds 			return -EFAULT;
13531da177e4SLinus Torvalds 		buf[len] = 0;
13541da177e4SLinus Torvalds 
1355c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
135647a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 		if (debug)
1359f342cda7SJoe Perches 			pr_debug("dst6_max set to: %s\n", buf);
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 		i += len;
13621da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
13631da177e4SLinus Torvalds 		return count;
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
13661da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1367222f1806SLuiz Capitulino 		if (len < 0)
1368222f1806SLuiz Capitulino 			return len;
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13731da177e4SLinus Torvalds 			return -EFAULT;
13741da177e4SLinus Torvalds 		buf[len] = 0;
13751da177e4SLinus Torvalds 
1376c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
137747a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
13781da177e4SLinus Torvalds 
13794e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 		if (debug)
1382f342cda7SJoe Perches 			pr_debug("src6 set to: %s\n", buf);
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 		i += len;
13851da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
13861da177e4SLinus Torvalds 		return count;
13871da177e4SLinus Torvalds 	}
13881da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
13891da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
139063adc6fbSStephen Hemminger 		if (len < 0)
1391222f1806SLuiz Capitulino 			return len;
139263adc6fbSStephen Hemminger 
13931da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13941da177e4SLinus Torvalds 			return -EFAULT;
13951da177e4SLinus Torvalds 		buf[len] = 0;
13961da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
13971da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
13981da177e4SLinus Torvalds 			strncpy(pkt_dev->src_min, buf, len);
13991da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
14001da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
14011da177e4SLinus Torvalds 		}
14021da177e4SLinus Torvalds 		if (debug)
1403f342cda7SJoe Perches 			pr_debug("src_min set to: %s\n", pkt_dev->src_min);
14041da177e4SLinus Torvalds 		i += len;
14051da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
14061da177e4SLinus Torvalds 		return count;
14071da177e4SLinus Torvalds 	}
14081da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
14091da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
141063adc6fbSStephen Hemminger 		if (len < 0)
1411222f1806SLuiz Capitulino 			return len;
141263adc6fbSStephen Hemminger 
14131da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14141da177e4SLinus Torvalds 			return -EFAULT;
14151da177e4SLinus Torvalds 		buf[len] = 0;
14161da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
14171da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
14181da177e4SLinus Torvalds 			strncpy(pkt_dev->src_max, buf, len);
14191da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
14201da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
14211da177e4SLinus Torvalds 		}
14221da177e4SLinus Torvalds 		if (debug)
1423f342cda7SJoe Perches 			pr_debug("src_max set to: %s\n", pkt_dev->src_max);
14241da177e4SLinus Torvalds 		i += len;
14251da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
14261da177e4SLinus Torvalds 		return count;
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
14291da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
143063adc6fbSStephen Hemminger 		if (len < 0)
1431222f1806SLuiz Capitulino 			return len;
143263adc6fbSStephen Hemminger 
14331da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14341da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14351da177e4SLinus Torvalds 			return -EFAULT;
14361da177e4SLinus Torvalds 
14374940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->dst_mac))
14384940fc88SAlexey Dobriyan 			return -EINVAL;
14391da177e4SLinus Torvalds 		/* Set up Dest MAC */
14404940fc88SAlexey Dobriyan 		memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
14411da177e4SLinus Torvalds 
14424940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
14431da177e4SLinus Torvalds 		return count;
14441da177e4SLinus Torvalds 	}
14451da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
14461da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
144763adc6fbSStephen Hemminger 		if (len < 0)
1448222f1806SLuiz Capitulino 			return len;
144963adc6fbSStephen Hemminger 
14501da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14511da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14521da177e4SLinus Torvalds 			return -EFAULT;
14531da177e4SLinus Torvalds 
14544940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->src_mac))
14554940fc88SAlexey Dobriyan 			return -EINVAL;
1456ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
14574940fc88SAlexey Dobriyan 		memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
1458ce5d0b47SAdit Ranadive 
14594940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
14601da177e4SLinus Torvalds 		return count;
14611da177e4SLinus Torvalds 	}
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
14641da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
14651da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
14661da177e4SLinus Torvalds 		return count;
14671da177e4SLinus Torvalds 	}
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
14701da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
147163adc6fbSStephen Hemminger 		if (len < 0)
1472222f1806SLuiz Capitulino 			return len;
147363adc6fbSStephen Hemminger 
14741da177e4SLinus Torvalds 		i += len;
14751da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
14761da177e4SLinus Torvalds 			value = MAX_CFLOWS;
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 		pkt_dev->cflows = value;
14791da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
14801da177e4SLinus Torvalds 		return count;
14811da177e4SLinus Torvalds 	}
14821da177e4SLinus Torvalds 
1483de4aee7dSFan Du 	if (!strcmp(name, "spi")) {
1484de4aee7dSFan Du 		len = num_arg(&user_buffer[i], 10, &value);
1485de4aee7dSFan Du 		if (len < 0)
1486de4aee7dSFan Du 			return len;
1487de4aee7dSFan Du 
1488de4aee7dSFan Du 		i += len;
1489de4aee7dSFan Du 		pkt_dev->spi = value;
1490de4aee7dSFan Du 		sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1491de4aee7dSFan Du 		return count;
1492de4aee7dSFan Du 	}
1493de4aee7dSFan Du 
14941da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
14951da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
149663adc6fbSStephen Hemminger 		if (len < 0)
1497222f1806SLuiz Capitulino 			return len;
149863adc6fbSStephen Hemminger 
14991da177e4SLinus Torvalds 		i += len;
15001da177e4SLinus Torvalds 		pkt_dev->lflow = value;
15011da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
15021da177e4SLinus Torvalds 		return count;
15031da177e4SLinus Torvalds 	}
15041da177e4SLinus Torvalds 
150545b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
150645b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
150763adc6fbSStephen Hemminger 		if (len < 0)
150845b270f8SRobert Olsson 			return len;
150963adc6fbSStephen Hemminger 
151045b270f8SRobert Olsson 		i += len;
151145b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
151245b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
151345b270f8SRobert Olsson 		return count;
151445b270f8SRobert Olsson 	}
151545b270f8SRobert Olsson 
151645b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
151745b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
151863adc6fbSStephen Hemminger 		if (len < 0)
151945b270f8SRobert Olsson 			return len;
152063adc6fbSStephen Hemminger 
152145b270f8SRobert Olsson 		i += len;
152245b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
152345b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
152445b270f8SRobert Olsson 		return count;
152545b270f8SRobert Olsson 	}
152645b270f8SRobert Olsson 
1527ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
152895c96174SEric Dumazet 		unsigned int n, cnt;
1529cfcabdccSStephen Hemminger 
1530ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1531cfcabdccSStephen Hemminger 		if (len < 0)
1532cfcabdccSStephen Hemminger 			return len;
1533ca6549afSSteven Whitehouse 		i += len;
1534cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1535ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1536cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1537ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1538ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
153934954ddcSFrancesco Fondelli 
154034954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
154134954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
154234954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
154334954ddcSFrancesco Fondelli 
154434954ddcSFrancesco Fondelli 			if (debug)
1545f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN auto turned off\n");
154634954ddcSFrancesco Fondelli 		}
154734954ddcSFrancesco Fondelli 		return count;
154834954ddcSFrancesco Fondelli 	}
154934954ddcSFrancesco Fondelli 
155034954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
155134954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
155263adc6fbSStephen Hemminger 		if (len < 0)
155334954ddcSFrancesco Fondelli 			return len;
155463adc6fbSStephen Hemminger 
155534954ddcSFrancesco Fondelli 		i += len;
155634954ddcSFrancesco Fondelli 		if (value <= 4095) {
155734954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
155834954ddcSFrancesco Fondelli 
155934954ddcSFrancesco Fondelli 			if (debug)
1560f342cda7SJoe Perches 				pr_debug("VLAN turned on\n");
156134954ddcSFrancesco Fondelli 
156234954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1563f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
156434954ddcSFrancesco Fondelli 
156534954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
156634954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
156734954ddcSFrancesco Fondelli 		} else {
156834954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
156934954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
157034954ddcSFrancesco Fondelli 
157134954ddcSFrancesco Fondelli 			if (debug)
1572f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
157334954ddcSFrancesco Fondelli 		}
157434954ddcSFrancesco Fondelli 		return count;
157534954ddcSFrancesco Fondelli 	}
157634954ddcSFrancesco Fondelli 
157734954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
157834954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
157963adc6fbSStephen Hemminger 		if (len < 0)
158034954ddcSFrancesco Fondelli 			return len;
158163adc6fbSStephen Hemminger 
158234954ddcSFrancesco Fondelli 		i += len;
158334954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
158434954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
158534954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
158634954ddcSFrancesco Fondelli 		} else {
158734954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
158834954ddcSFrancesco Fondelli 		}
158934954ddcSFrancesco Fondelli 		return count;
159034954ddcSFrancesco Fondelli 	}
159134954ddcSFrancesco Fondelli 
159234954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
159334954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
159463adc6fbSStephen Hemminger 		if (len < 0)
159534954ddcSFrancesco Fondelli 			return len;
159663adc6fbSStephen Hemminger 
159734954ddcSFrancesco Fondelli 		i += len;
159834954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
159934954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
160034954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
160134954ddcSFrancesco Fondelli 		} else {
160234954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
160334954ddcSFrancesco Fondelli 		}
160434954ddcSFrancesco Fondelli 		return count;
160534954ddcSFrancesco Fondelli 	}
160634954ddcSFrancesco Fondelli 
160734954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
160834954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
160963adc6fbSStephen Hemminger 		if (len < 0)
161034954ddcSFrancesco Fondelli 			return len;
161163adc6fbSStephen Hemminger 
161234954ddcSFrancesco Fondelli 		i += len;
161334954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
161434954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
161534954ddcSFrancesco Fondelli 
161634954ddcSFrancesco Fondelli 			if (debug)
1617f342cda7SJoe Perches 				pr_debug("SVLAN turned on\n");
161834954ddcSFrancesco Fondelli 
161934954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1620f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
162134954ddcSFrancesco Fondelli 
162234954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
162334954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
162434954ddcSFrancesco Fondelli 		} else {
162534954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
162634954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
162734954ddcSFrancesco Fondelli 
162834954ddcSFrancesco Fondelli 			if (debug)
1629f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
163034954ddcSFrancesco Fondelli 		}
163134954ddcSFrancesco Fondelli 		return count;
163234954ddcSFrancesco Fondelli 	}
163334954ddcSFrancesco Fondelli 
163434954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
163534954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
163663adc6fbSStephen Hemminger 		if (len < 0)
163734954ddcSFrancesco Fondelli 			return len;
163863adc6fbSStephen Hemminger 
163934954ddcSFrancesco Fondelli 		i += len;
164034954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
164134954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
164234954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
164334954ddcSFrancesco Fondelli 		} else {
164434954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
164534954ddcSFrancesco Fondelli 		}
164634954ddcSFrancesco Fondelli 		return count;
164734954ddcSFrancesco Fondelli 	}
164834954ddcSFrancesco Fondelli 
164934954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
165034954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
165163adc6fbSStephen Hemminger 		if (len < 0)
165234954ddcSFrancesco Fondelli 			return len;
165363adc6fbSStephen Hemminger 
165434954ddcSFrancesco Fondelli 		i += len;
165534954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
165634954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
165734954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
165834954ddcSFrancesco Fondelli 		} else {
165934954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
166034954ddcSFrancesco Fondelli 		}
1661ca6549afSSteven Whitehouse 		return count;
1662ca6549afSSteven Whitehouse 	}
1663ca6549afSSteven Whitehouse 
16641ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
16651ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16661ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
166763adc6fbSStephen Hemminger 		if (len < 0)
16681ca7768cSFrancesco Fondelli 			return len;
166963adc6fbSStephen Hemminger 
16701ca7768cSFrancesco Fondelli 		i += len;
16711ca7768cSFrancesco Fondelli 		if (len == 2) {
16721ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
16731ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
16741ca7768cSFrancesco Fondelli 		} else {
16751ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
16761ca7768cSFrancesco Fondelli 		}
16771ca7768cSFrancesco Fondelli 		return count;
16781ca7768cSFrancesco Fondelli 	}
16791ca7768cSFrancesco Fondelli 
16801ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
16811ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16821ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
168363adc6fbSStephen Hemminger 		if (len < 0)
16841ca7768cSFrancesco Fondelli 			return len;
168563adc6fbSStephen Hemminger 
16861ca7768cSFrancesco Fondelli 		i += len;
16871ca7768cSFrancesco Fondelli 		if (len == 2) {
16881ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
16891ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
16901ca7768cSFrancesco Fondelli 		} else {
16911ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
16921ca7768cSFrancesco Fondelli 		}
16931ca7768cSFrancesco Fondelli 		return count;
16941ca7768cSFrancesco Fondelli 	}
16951ca7768cSFrancesco Fondelli 
16969e50e3acSJohn Fastabend 	if (!strcmp(name, "skb_priority")) {
16979e50e3acSJohn Fastabend 		len = num_arg(&user_buffer[i], 9, &value);
16989e50e3acSJohn Fastabend 		if (len < 0)
16999e50e3acSJohn Fastabend 			return len;
17009e50e3acSJohn Fastabend 
17019e50e3acSJohn Fastabend 		i += len;
17029e50e3acSJohn Fastabend 		pkt_dev->skb_priority = value;
17039e50e3acSJohn Fastabend 		sprintf(pg_result, "OK: skb_priority=%i",
17049e50e3acSJohn Fastabend 			pkt_dev->skb_priority);
17059e50e3acSJohn Fastabend 		return count;
17069e50e3acSJohn Fastabend 	}
17079e50e3acSJohn Fastabend 
17081da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
17091da177e4SLinus Torvalds 	return -EINVAL;
17101da177e4SLinus Torvalds }
17111da177e4SLinus Torvalds 
1712d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
17131da177e4SLinus Torvalds {
1714d9dda78bSAl Viro 	return single_open(file, pktgen_if_show, PDE_DATA(inode));
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
17179a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1718d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1719d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1720d50a6b56SStephen Hemminger 	.read    = seq_read,
1721d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1722d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1723d50a6b56SStephen Hemminger 	.release = single_release,
1724d50a6b56SStephen Hemminger };
1725d50a6b56SStephen Hemminger 
1726d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1727d50a6b56SStephen Hemminger {
1728d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1729648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1730d50a6b56SStephen Hemminger 
1731d50a6b56SStephen Hemminger 	BUG_ON(!t);
1732d50a6b56SStephen Hemminger 
1733d50a6b56SStephen Hemminger 	seq_printf(seq, "Running: ");
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	if_lock(t);
1736c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17371da177e4SLinus Torvalds 		if (pkt_dev->running)
1738593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17391da177e4SLinus Torvalds 
1740d50a6b56SStephen Hemminger 	seq_printf(seq, "\nStopped: ");
17411da177e4SLinus Torvalds 
1742c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17431da177e4SLinus Torvalds 		if (!pkt_dev->running)
1744593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 	if (t->result[0])
1747d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
17481da177e4SLinus Torvalds 	else
1749d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: NA\n");
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	if_unlock(t);
17521da177e4SLinus Torvalds 
1753d50a6b56SStephen Hemminger 	return 0;
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds 
1756d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1757d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1758d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
17591da177e4SLinus Torvalds {
17608a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
1761d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1762d6182223SPaul Gortmaker 	int i, max, len, ret;
17631da177e4SLinus Torvalds 	char name[40];
17641da177e4SLinus Torvalds 	char *pg_result;
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	if (count < 1) {
17671da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
17681da177e4SLinus Torvalds 		return -EINVAL;
17691da177e4SLinus Torvalds 	}
17701da177e4SLinus Torvalds 
1771d6182223SPaul Gortmaker 	max = count;
1772d6182223SPaul Gortmaker 	len = count_trail_chars(user_buffer, max);
17731da177e4SLinus Torvalds 	if (len < 0)
17741da177e4SLinus Torvalds 		return len;
17751da177e4SLinus Torvalds 
1776d6182223SPaul Gortmaker 	i = len;
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	/* Read variable name */
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
17811da177e4SLinus Torvalds 	if (len < 0)
17821da177e4SLinus Torvalds 		return len;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
17851da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
17861da177e4SLinus Torvalds 		return -EFAULT;
17871da177e4SLinus Torvalds 	i += len;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	max = count - i;
17901da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
17911da177e4SLinus Torvalds 	if (len < 0)
17921da177e4SLinus Torvalds 		return len;
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	i += len;
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds 	if (debug)
1797f342cda7SJoe Perches 		pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	if (!t) {
1800f9467eaeSJoe Perches 		pr_err("ERROR: No thread\n");
18011da177e4SLinus Torvalds 		ret = -EINVAL;
18021da177e4SLinus Torvalds 		goto out;
18031da177e4SLinus Torvalds 	}
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
18081da177e4SLinus Torvalds 		char f[32];
18091da177e4SLinus Torvalds 		memset(f, 0, 32);
18101da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
18111da177e4SLinus Torvalds 		if (len < 0) {
18121da177e4SLinus Torvalds 			ret = len;
18131da177e4SLinus Torvalds 			goto out;
18141da177e4SLinus Torvalds 		}
18151da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
18161da177e4SLinus Torvalds 			return -EFAULT;
18171da177e4SLinus Torvalds 		i += len;
18186146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
1819604dfd6eSCong Wang 		ret = pktgen_add_device(t, f);
18206146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1821604dfd6eSCong Wang 		if (!ret) {
18221da177e4SLinus Torvalds 			ret = count;
18231da177e4SLinus Torvalds 			sprintf(pg_result, "OK: add_device=%s", f);
1824604dfd6eSCong Wang 		} else
1825604dfd6eSCong Wang 			sprintf(pg_result, "ERROR: can not add device %s", f);
18261da177e4SLinus Torvalds 		goto out;
18271da177e4SLinus Torvalds 	}
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
18306146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
183195ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
18326146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1833121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
18341da177e4SLinus Torvalds 		ret = count;
18351da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
18361da177e4SLinus Torvalds 		goto out;
18371da177e4SLinus Torvalds 	}
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1840b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
18411da177e4SLinus Torvalds 		ret = count;
18421da177e4SLinus Torvalds 		goto out;
18431da177e4SLinus Torvalds 	}
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds 	ret = -EINVAL;
18461da177e4SLinus Torvalds out:
18471da177e4SLinus Torvalds 	return ret;
18481da177e4SLinus Torvalds }
18491da177e4SLinus Torvalds 
1850d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
18511da177e4SLinus Torvalds {
1852d9dda78bSAl Viro 	return single_open(file, pktgen_thread_show, PDE_DATA(inode));
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
18559a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1856d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1857d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1858d50a6b56SStephen Hemminger 	.read    = seq_read,
1859d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1860d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1861d50a6b56SStephen Hemminger 	.release = single_release,
1862d50a6b56SStephen Hemminger };
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds /* Think find or remove for NN */
18654e58a027SCong Wang static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
18664e58a027SCong Wang 					      const char *ifname, int remove)
18671da177e4SLinus Torvalds {
18681da177e4SLinus Torvalds 	struct pktgen_thread *t;
18691da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
18703e984840SEric Dumazet 	bool exact = (remove == FIND);
18711da177e4SLinus Torvalds 
18724e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
18733e984840SEric Dumazet 		pkt_dev = pktgen_find_dev(t, ifname, exact);
18741da177e4SLinus Torvalds 		if (pkt_dev) {
18751da177e4SLinus Torvalds 			if (remove) {
18761da177e4SLinus Torvalds 				if_lock(t);
187795ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
187895ed63f7SArthur Kepner 				t->control |= T_REMDEV;
18791da177e4SLinus Torvalds 				if_unlock(t);
18801da177e4SLinus Torvalds 			}
18811da177e4SLinus Torvalds 			break;
18821da177e4SLinus Torvalds 		}
18831da177e4SLinus Torvalds 	}
18841da177e4SLinus Torvalds 	return pkt_dev;
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds 
188795ed63f7SArthur Kepner /*
188895ed63f7SArthur Kepner  * mark a device for removal
188995ed63f7SArthur Kepner  */
18904e58a027SCong Wang static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
18911da177e4SLinus Torvalds {
18921da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
189395ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
189495ed63f7SArthur Kepner 	int i = 0;
189595ed63f7SArthur Kepner 
18966146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
1897f9467eaeSJoe Perches 	pr_debug("%s: marking %s for removal\n", __func__, ifname);
189895ed63f7SArthur Kepner 
189995ed63f7SArthur Kepner 	while (1) {
190095ed63f7SArthur Kepner 
19014e58a027SCong Wang 		pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1902222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1903222f1806SLuiz Capitulino 			break;	/* success */
190495ed63f7SArthur Kepner 
19056146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1906f9467eaeSJoe Perches 		pr_debug("%s: waiting for %s to disappear....\n",
1907f9467eaeSJoe Perches 			 __func__, ifname);
190895ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
19096146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
191095ed63f7SArthur Kepner 
191195ed63f7SArthur Kepner 		if (++i >= max_tries) {
1912f9467eaeSJoe Perches 			pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1913f9467eaeSJoe Perches 			       __func__, msec_per_try * i, ifname);
191495ed63f7SArthur Kepner 			break;
191595ed63f7SArthur Kepner 		}
191695ed63f7SArthur Kepner 
191795ed63f7SArthur Kepner 	}
191895ed63f7SArthur Kepner 
19196146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
192039df232fSStephen Hemminger }
192195ed63f7SArthur Kepner 
19224e58a027SCong Wang static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
192339df232fSStephen Hemminger {
192439df232fSStephen Hemminger 	struct pktgen_thread *t;
192539df232fSStephen Hemminger 
19264e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
192739df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
192839df232fSStephen Hemminger 
192939df232fSStephen Hemminger 		list_for_each_entry(pkt_dev, &t->if_list, list) {
193039df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
193139df232fSStephen Hemminger 				continue;
193239df232fSStephen Hemminger 
1933a8ca16eaSDavid Howells 			proc_remove(pkt_dev->entry);
193439df232fSStephen Hemminger 
19352975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
19364e58a027SCong Wang 							  pn->proc_dir,
19372975315bSAlexey Dobriyan 							  &pktgen_if_fops,
19382975315bSAlexey Dobriyan 							  pkt_dev);
193939df232fSStephen Hemminger 			if (!pkt_dev->entry)
1940f9467eaeSJoe Perches 				pr_err("can't move proc entry for '%s'\n",
1941f9467eaeSJoe Perches 				       dev->name);
194239df232fSStephen Hemminger 			break;
194339df232fSStephen Hemminger 		}
194439df232fSStephen Hemminger 	}
19451da177e4SLinus Torvalds }
19461da177e4SLinus Torvalds 
1947222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
1948222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
19491da177e4SLinus Torvalds {
1950351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
19514e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
19521da177e4SLinus Torvalds 
19534e58a027SCong Wang 	if (pn->pktgen_exiting)
1954e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1955e9dc8653SEric W. Biederman 
19561da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
19571da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
19581da177e4SLinus Torvalds 	 */
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	switch (event) {
196139df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
19624e58a027SCong Wang 		pktgen_change_name(pn, dev);
19631da177e4SLinus Torvalds 		break;
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
19664e58a027SCong Wang 		pktgen_mark_device(pn, dev->name);
19671da177e4SLinus Torvalds 		break;
19683ff50b79SStephen Hemminger 	}
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	return NOTIFY_DONE;
19711da177e4SLinus Torvalds }
19721da177e4SLinus Torvalds 
19734e58a027SCong Wang static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
19744e58a027SCong Wang 						 struct pktgen_dev *pkt_dev,
197563adc6fbSStephen Hemminger 						 const char *ifname)
1976e6fce5b9SRobert Olsson {
1977e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
1978d6182223SPaul Gortmaker 	int i;
1979e6fce5b9SRobert Olsson 
1980e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
1981e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
1982e6fce5b9SRobert Olsson 			break;
1983e6fce5b9SRobert Olsson 
1984e6fce5b9SRobert Olsson 		b[i] = ifname[i];
1985e6fce5b9SRobert Olsson 	}
1986e6fce5b9SRobert Olsson 	b[i] = 0;
1987e6fce5b9SRobert Olsson 
19884e58a027SCong Wang 	return dev_get_by_name(pn->net, b);
1989e6fce5b9SRobert Olsson }
1990e6fce5b9SRobert Olsson 
1991e6fce5b9SRobert Olsson 
19921da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
19931da177e4SLinus Torvalds 
19944e58a027SCong Wang static int pktgen_setup_dev(const struct pktgen_net *pn,
19954e58a027SCong Wang 			    struct pktgen_dev *pkt_dev, const char *ifname)
1996222f1806SLuiz Capitulino {
19971da177e4SLinus Torvalds 	struct net_device *odev;
199839df232fSStephen Hemminger 	int err;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	/* Clean old setups */
20011da177e4SLinus Torvalds 	if (pkt_dev->odev) {
20021da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
20031da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
20041da177e4SLinus Torvalds 	}
20051da177e4SLinus Torvalds 
20064e58a027SCong Wang 	odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
20071da177e4SLinus Torvalds 	if (!odev) {
2008f9467eaeSJoe Perches 		pr_err("no such netdevice: \"%s\"\n", ifname);
200939df232fSStephen Hemminger 		return -ENODEV;
20101da177e4SLinus Torvalds 	}
201139df232fSStephen Hemminger 
20121da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
2013f9467eaeSJoe Perches 		pr_err("not an ethernet device: \"%s\"\n", ifname);
201439df232fSStephen Hemminger 		err = -EINVAL;
201539df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
2016f9467eaeSJoe Perches 		pr_err("device is down: \"%s\"\n", ifname);
201739df232fSStephen Hemminger 		err = -ENETDOWN;
201839df232fSStephen Hemminger 	} else {
20191da177e4SLinus Torvalds 		pkt_dev->odev = odev;
202039df232fSStephen Hemminger 		return 0;
202139df232fSStephen Hemminger 	}
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 	dev_put(odev);
202439df232fSStephen Hemminger 	return err;
20251da177e4SLinus Torvalds }
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
20281da177e4SLinus Torvalds  * structure to have the right information to create/send packets
20291da177e4SLinus Torvalds  */
20301da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
20311da177e4SLinus Torvalds {
203264c00d81SAndrew Gallatin 	int ntxq;
203364c00d81SAndrew Gallatin 
20341da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
2035f9467eaeSJoe Perches 		pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2036222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
2037222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
20381da177e4SLinus Torvalds 		return;
20391da177e4SLinus Torvalds 	}
20401da177e4SLinus Torvalds 
204164c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
204264c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2043bfdbc0acSRobert Olsson 
204464c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
2045f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
204688271660SJesse Brandeburg 			   pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2047593f63b0SEric Dumazet 			   pkt_dev->odevname);
204826e29eedSDan Carpenter 		pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
204964c00d81SAndrew Gallatin 	}
205088271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
2051f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
205288271660SJesse Brandeburg 			   pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2053593f63b0SEric Dumazet 			   pkt_dev->odevname);
205426e29eedSDan Carpenter 		pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
205564c00d81SAndrew Gallatin 	}
205664c00d81SAndrew Gallatin 
20571da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
20581da177e4SLinus Torvalds 
2059f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
2060f404e9a6SKris Katterjohn 		memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	/* Set up Dest MAC */
2063f404e9a6SKris Katterjohn 	memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
20664c139b8cSAmerigo Wang 		int i, set = 0, err = 1;
20674c139b8cSAmerigo Wang 		struct inet6_dev *idev;
20684c139b8cSAmerigo Wang 
206968bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
207068bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
207168bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
207268bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
207368bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
207468bf9f0bSAmerigo Wang 		}
207568bf9f0bSAmerigo Wang 
20761da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
20771da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
20781da177e4SLinus Torvalds 				set = 1;
20791da177e4SLinus Torvalds 				break;
20801da177e4SLinus Torvalds 			}
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 		if (!set) {
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 			/*
20851da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
20861da177e4SLinus Torvalds 			 *
20871da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
20881da177e4SLinus Torvalds 			 */
20891da177e4SLinus Torvalds 
20908814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
209163adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
209263adc6fbSStephen Hemminger 			if (idev) {
20931da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
20964c139b8cSAmerigo Wang 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
20974c139b8cSAmerigo Wang 					if ((ifp->scope & IFA_LINK) &&
2098f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
20994e3fd7a0SAlexey Dobriyan 						pkt_dev->cur_in6_saddr = ifp->addr;
21001da177e4SLinus Torvalds 						err = 0;
21011da177e4SLinus Torvalds 						break;
21021da177e4SLinus Torvalds 					}
21031da177e4SLinus Torvalds 				}
21041da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
21051da177e4SLinus Torvalds 			}
21068814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2107222f1806SLuiz Capitulino 			if (err)
2108f9467eaeSJoe Perches 				pr_err("ERROR: IPv6 link address not available\n");
21091da177e4SLinus Torvalds 		}
2110222f1806SLuiz Capitulino 	} else {
211168bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
211268bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
211368bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
211468bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
211568bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
211668bf9f0bSAmerigo Wang 		}
211768bf9f0bSAmerigo Wang 
21181da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
21191da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
21201da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 			struct in_device *in_dev;
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds 			rcu_read_lock();
2125e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
21261da177e4SLinus Torvalds 			if (in_dev) {
21271da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2128222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2129222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
21301da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
21311da177e4SLinus Torvalds 				}
21321da177e4SLinus Torvalds 			}
21331da177e4SLinus Torvalds 			rcu_read_unlock();
2134222f1806SLuiz Capitulino 		} else {
21351da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
21361da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
21371da177e4SLinus Torvalds 		}
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
21401da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
21411da177e4SLinus Torvalds 	}
21421da177e4SLinus Torvalds 	/* Initialize current values. */
214368bf9f0bSAmerigo Wang 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
214468bf9f0bSAmerigo Wang 	if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
214568bf9f0bSAmerigo Wang 		pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
214668bf9f0bSAmerigo Wang 
21471da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
21481da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
21491da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
21501da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
21511da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
21521da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
21531da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
21541da177e4SLinus Torvalds }
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 
2157fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2158fd29cf72SStephen Hemminger {
2159ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2160417bc4b8SEric Dumazet 	s64 remaining;
21612bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2162fd29cf72SStephen Hemminger 
21632bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
21642bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2165fd29cf72SStephen Hemminger 
216643d28b65SDaniel Turull 	remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2167417bc4b8SEric Dumazet 	if (remaining <= 0) {
2168417bc4b8SEric Dumazet 		pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21691da177e4SLinus Torvalds 		return;
2170417bc4b8SEric Dumazet 	}
21712bc481cfSStephen Hemminger 
2172398f382cSDaniel Borkmann 	start_time = ktime_get();
217333136d12SEric Dumazet 	if (remaining < 100000) {
217433136d12SEric Dumazet 		/* for small delays (<100us), just loop until limit is reached */
217533136d12SEric Dumazet 		do {
2176398f382cSDaniel Borkmann 			end_time = ktime_get();
2177398f382cSDaniel Borkmann 		} while (ktime_compare(end_time, spin_until) < 0);
217833136d12SEric Dumazet 	} else {
21792bc481cfSStephen Hemminger 		/* see do_nanosleep */
21802bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
21812bc481cfSStephen Hemminger 		do {
21822bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
21832bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
21842bc481cfSStephen Hemminger 			if (!hrtimer_active(&t.timer))
21852bc481cfSStephen Hemminger 				t.task = NULL;
21862bc481cfSStephen Hemminger 
21872bc481cfSStephen Hemminger 			if (likely(t.task))
21881da177e4SLinus Torvalds 				schedule();
21891da177e4SLinus Torvalds 
21902bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
21912bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
21922bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
2193398f382cSDaniel Borkmann 		end_time = ktime_get();
219433136d12SEric Dumazet 	}
2195ef87979cSStephen Hemminger 
2196ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
219707a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
220016dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
220116dab72fSJamal Hadi Salim {
2202a553e4a6SJamal Hadi Salim 	pkt_dev->pkt_overhead = 0;
220316dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
220416dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
220516dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
220616dab72fSJamal Hadi Salim }
220716dab72fSJamal Hadi Salim 
2208648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2209007a531bSJamal Hadi Salim {
2210648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2211007a531bSJamal Hadi Salim }
2212007a531bSJamal Hadi Salim 
2213007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2214007a531bSJamal Hadi Salim {
2215007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2216007a531bSJamal Hadi Salim 
2217007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2218007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2219007a531bSJamal Hadi Salim 			/* reset time */
2220007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22211211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2222007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2223007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2224007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2225007a531bSJamal Hadi Salim 		}
2226007a531bSJamal Hadi Salim 	} else {
222733d7c5e5SAkinobu Mita 		flow = prandom_u32() % pkt_dev->cflows;
22281211a645SRobert Olsson 		pkt_dev->curfl = flow;
2229007a531bSJamal Hadi Salim 
22301211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2231007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22321211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
22331211a645SRobert Olsson 		}
2234007a531bSJamal Hadi Salim 	}
2235007a531bSJamal Hadi Salim 
2236007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2237007a531bSJamal Hadi Salim }
2238007a531bSJamal Hadi Salim 
2239a553e4a6SJamal Hadi Salim 
2240a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2241a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2242a553e4a6SJamal Hadi Salim  * we go look for it ...
2243a553e4a6SJamal Hadi Salim */
2244bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2245fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2246a553e4a6SJamal Hadi Salim {
2247a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
22484e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2249a553e4a6SJamal Hadi Salim 	if (!x) {
2250a553e4a6SJamal Hadi Salim 		/*slow path: we dont already have xfrm_state*/
22514e58a027SCong Wang 		x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
22525447c5e4SAlexey Dobriyan 					(xfrm_address_t *)&pkt_dev->cur_daddr,
2253a553e4a6SJamal Hadi Salim 					(xfrm_address_t *)&pkt_dev->cur_saddr,
2254a553e4a6SJamal Hadi Salim 					AF_INET,
2255a553e4a6SJamal Hadi Salim 					pkt_dev->ipsmode,
2256a553e4a6SJamal Hadi Salim 					pkt_dev->ipsproto, 0);
2257a553e4a6SJamal Hadi Salim 		if (x) {
2258a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2259a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2260a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2261a553e4a6SJamal Hadi Salim 		}
2262a553e4a6SJamal Hadi Salim 
2263a553e4a6SJamal Hadi Salim 	}
2264a553e4a6SJamal Hadi Salim }
2265a553e4a6SJamal Hadi Salim #endif
2266fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2267fd2ea0a7SDavid S. Miller {
2268e6fce5b9SRobert Olsson 
2269e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2270e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2271e6fce5b9SRobert Olsson 
2272896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2273fd2ea0a7SDavid S. Miller 		__u16 t;
2274fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
227533d7c5e5SAkinobu Mita 			t = prandom_u32() %
2276fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2277fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2278fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2279fd2ea0a7SDavid S. Miller 		} else {
2280fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2281fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2282fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2283fd2ea0a7SDavid S. Miller 		}
2284fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2285fd2ea0a7SDavid S. Miller 	}
2286bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2287fd2ea0a7SDavid S. Miller }
2288fd2ea0a7SDavid S. Miller 
22891da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
22901da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
22911da177e4SLinus Torvalds  */
2292222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2293222f1806SLuiz Capitulino {
22941da177e4SLinus Torvalds 	__u32 imn;
22951da177e4SLinus Torvalds 	__u32 imx;
22961da177e4SLinus Torvalds 	int flow = 0;
22971da177e4SLinus Torvalds 
2298007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2299007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	/*  Deal with source MAC */
23021da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
23031da177e4SLinus Torvalds 		__u32 mc;
23041da177e4SLinus Torvalds 		__u32 tmp;
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
230733d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->src_mac_count;
23081da177e4SLinus Torvalds 		else {
23091da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2310ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2311222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
23121da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
23131da177e4SLinus Torvalds 		}
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
23161da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
23171da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23181da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
23191da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23201da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
23211da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23221da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
23231da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
23241da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
23251da177e4SLinus Torvalds 	}
23261da177e4SLinus Torvalds 
23271da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
23281da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
23291da177e4SLinus Torvalds 		__u32 mc;
23301da177e4SLinus Torvalds 		__u32 tmp;
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
233333d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->dst_mac_count;
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 		else {
23361da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2337ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2338222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
23391da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
23401da177e4SLinus Torvalds 			}
23411da177e4SLinus Torvalds 		}
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
23441da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
23451da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23461da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
23471da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23481da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
23491da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23501da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
23511da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
23521da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
23531da177e4SLinus Torvalds 	}
23541da177e4SLinus Torvalds 
2355ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
235695c96174SEric Dumazet 		unsigned int i;
2357ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2358ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2359ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
236033d7c5e5SAkinobu Mita 					     ((__force __be32)prandom_u32() &
2361ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2362ca6549afSSteven Whitehouse 	}
2363ca6549afSSteven Whitehouse 
236434954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
236533d7c5e5SAkinobu Mita 		pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
236634954ddcSFrancesco Fondelli 	}
236734954ddcSFrancesco Fondelli 
236834954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
236933d7c5e5SAkinobu Mita 		pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
237034954ddcSFrancesco Fondelli 	}
237134954ddcSFrancesco Fondelli 
23721da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
23731da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
237433d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_src = prandom_u32() %
23755fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
23765fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 		else {
23791da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
23801da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
23811da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
23821da177e4SLinus Torvalds 		}
23831da177e4SLinus Torvalds 	}
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
23861da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
238733d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_dst = prandom_u32() %
23885fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
23895fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2390222f1806SLuiz Capitulino 		} else {
23911da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
23921da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
23931da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
23941da177e4SLinus Torvalds 		}
23951da177e4SLinus Torvalds 	}
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
23981da177e4SLinus Torvalds 
239963adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
240063adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
240163adc6fbSStephen Hemminger 		if (imn < imx) {
24021da177e4SLinus Torvalds 			__u32 t;
24031da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
240433d7c5e5SAkinobu Mita 				t = prandom_u32() % (imx - imn) + imn;
24051da177e4SLinus Torvalds 			else {
24061da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
24071da177e4SLinus Torvalds 				t++;
240863adc6fbSStephen Hemminger 				if (t > imx)
24091da177e4SLinus Torvalds 					t = imn;
241063adc6fbSStephen Hemminger 
24111da177e4SLinus Torvalds 			}
24121da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
24131da177e4SLinus Torvalds 		}
24141da177e4SLinus Torvalds 
2415007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
24161da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
24171da177e4SLinus Torvalds 		} else {
2418252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2419252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2420252e3346SAl Viro 			if (imn < imx) {
24211da177e4SLinus Torvalds 				__u32 t;
2422252e3346SAl Viro 				__be32 s;
24231da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
24241da177e4SLinus Torvalds 
242570e3ba72SAkinobu Mita 					do {
242633d7c5e5SAkinobu Mita 						t = prandom_u32() %
242733d7c5e5SAkinobu Mita 							(imx - imn) + imn;
2428252e3346SAl Viro 						s = htonl(t);
242970e3ba72SAkinobu Mita 					} while (ipv4_is_loopback(s) ||
243070e3ba72SAkinobu Mita 						ipv4_is_multicast(s) ||
243170e3ba72SAkinobu Mita 						ipv4_is_lbcast(s) ||
243270e3ba72SAkinobu Mita 						ipv4_is_zeronet(s) ||
243370e3ba72SAkinobu Mita 						ipv4_is_local_multicast(s));
2434252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2435252e3346SAl Viro 				} else {
24361da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
24371da177e4SLinus Torvalds 					t++;
24381da177e4SLinus Torvalds 					if (t > imx) {
24391da177e4SLinus Torvalds 						t = imn;
24401da177e4SLinus Torvalds 					}
24411da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
24421da177e4SLinus Torvalds 				}
24431da177e4SLinus Torvalds 			}
24441da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2445007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2446222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2447222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2448a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2449a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2450a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2451a553e4a6SJamal Hadi Salim #endif
24521da177e4SLinus Torvalds 				pkt_dev->nflows++;
24531da177e4SLinus Torvalds 			}
24541da177e4SLinus Torvalds 		}
2455222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2456222f1806SLuiz Capitulino 
245706e30411SJoe Perches 		if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
24581da177e4SLinus Torvalds 			int i;
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 			/* Only random destinations yet */
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
24631da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
246433d7c5e5SAkinobu Mita 				    (((__force __be32)prandom_u32() |
24651da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
24661da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
24671da177e4SLinus Torvalds 			}
24681da177e4SLinus Torvalds 		}
24691da177e4SLinus Torvalds 	}
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
24721da177e4SLinus Torvalds 		__u32 t;
24731da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
247433d7c5e5SAkinobu Mita 			t = prandom_u32() %
24755fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
24765fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2477222f1806SLuiz Capitulino 		} else {
24781da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
24791da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
24801da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
24811da177e4SLinus Torvalds 		}
24821da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
24831da177e4SLinus Torvalds 	}
24841da177e4SLinus Torvalds 
2485fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
248645b270f8SRobert Olsson 
24871da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
24881da177e4SLinus Torvalds }
24891da177e4SLinus Torvalds 
2490a553e4a6SJamal Hadi Salim 
2491a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2492*cf93d47eSFan Du u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2493*cf93d47eSFan Du 
2494*cf93d47eSFan Du 	[RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2495*cf93d47eSFan Du };
2496*cf93d47eSFan Du 
2497a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2498a553e4a6SJamal Hadi Salim {
2499a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2500a553e4a6SJamal Hadi Salim 	int err = 0;
25016de9ace4SFan Du 	struct net *net = dev_net(pkt_dev->odev);
2502a553e4a6SJamal Hadi Salim 
2503a553e4a6SJamal Hadi Salim 	if (!x)
2504a553e4a6SJamal Hadi Salim 		return 0;
2505a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2506a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2507*cf93d47eSFan Du 	if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2508a553e4a6SJamal Hadi Salim 		return 0;
2509a553e4a6SJamal Hadi Salim 
2510*cf93d47eSFan Du 	/* But when user specify an valid SPI, transformation
2511*cf93d47eSFan Du 	 * supports both transport/tunnel mode + ESP/AH type.
2512*cf93d47eSFan Du 	 */
2513*cf93d47eSFan Du 	if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2514*cf93d47eSFan Du 		skb->_skb_refdst = (unsigned long)&pkt_dev->dst | SKB_DST_NOREF;
2515*cf93d47eSFan Du 
2516*cf93d47eSFan Du 	rcu_read_lock_bh();
251713996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2518*cf93d47eSFan Du 	rcu_read_unlock_bh();
25196de9ace4SFan Du 	if (err) {
25206de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2521a553e4a6SJamal Hadi Salim 		goto error;
25226de9ace4SFan Du 	}
2523a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
25246de9ace4SFan Du 	if (err) {
25256de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2526a553e4a6SJamal Hadi Salim 		goto error;
25276de9ace4SFan Du 	}
25280af0a413SFan Du 	spin_lock_bh(&x->lock);
2529a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2530a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
25310af0a413SFan Du 	spin_unlock_bh(&x->lock);
2532a553e4a6SJamal Hadi Salim error:
2533a553e4a6SJamal Hadi Salim 	return err;
2534a553e4a6SJamal Hadi Salim }
2535a553e4a6SJamal Hadi Salim 
2536475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2537a553e4a6SJamal Hadi Salim {
2538a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2539a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2540d6182223SPaul Gortmaker 		int i;
2541d6182223SPaul Gortmaker 		for (i = 0; i < pkt_dev->cflows; i++) {
2542a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2543a553e4a6SJamal Hadi Salim 			if (x) {
2544a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2545a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2546a553e4a6SJamal Hadi Salim 			}
2547a553e4a6SJamal Hadi Salim 		}
2548a553e4a6SJamal Hadi Salim 	}
2549a553e4a6SJamal Hadi Salim }
2550a553e4a6SJamal Hadi Salim 
2551475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2552a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2553a553e4a6SJamal Hadi Salim {
2554a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2555a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2556a553e4a6SJamal Hadi Salim 		int nhead = 0;
2557a553e4a6SJamal Hadi Salim 		if (x) {
2558a553e4a6SJamal Hadi Salim 			int ret;
2559a553e4a6SJamal Hadi Salim 			__u8 *eth;
25603868204dSfan.du 			struct iphdr *iph;
25613868204dSfan.du 
2562a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2563a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2564a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2565a553e4a6SJamal Hadi Salim 				if (ret < 0) {
2566f9467eaeSJoe Perches 					pr_err("Error expanding ipsec packet %d\n",
2567f9467eaeSJoe Perches 					       ret);
2568b4bb4ac8SIlpo Järvinen 					goto err;
2569a553e4a6SJamal Hadi Salim 				}
2570a553e4a6SJamal Hadi Salim 			}
2571a553e4a6SJamal Hadi Salim 
2572a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2573a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2574a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2575a553e4a6SJamal Hadi Salim 			if (ret) {
2576f9467eaeSJoe Perches 				pr_err("Error creating ipsec packet %d\n", ret);
2577b4bb4ac8SIlpo Järvinen 				goto err;
2578a553e4a6SJamal Hadi Salim 			}
2579a553e4a6SJamal Hadi Salim 			/* restore ll */
2580a553e4a6SJamal Hadi Salim 			eth = (__u8 *) skb_push(skb, ETH_HLEN);
2581a553e4a6SJamal Hadi Salim 			memcpy(eth, pkt_dev->hh, 12);
2582a553e4a6SJamal Hadi Salim 			*(u16 *) &eth[12] = protocol;
25833868204dSfan.du 
25843868204dSfan.du 			/* Update IPv4 header len as well as checksum value */
25853868204dSfan.du 			iph = ip_hdr(skb);
25863868204dSfan.du 			iph->tot_len = htons(skb->len - ETH_HLEN);
25873868204dSfan.du 			ip_send_check(iph);
2588a553e4a6SJamal Hadi Salim 		}
2589a553e4a6SJamal Hadi Salim 	}
2590a553e4a6SJamal Hadi Salim 	return 1;
2591b4bb4ac8SIlpo Järvinen err:
2592b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2593b4bb4ac8SIlpo Järvinen 	return 0;
2594a553e4a6SJamal Hadi Salim }
2595a553e4a6SJamal Hadi Salim #endif
2596a553e4a6SJamal Hadi Salim 
2597ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2598ca6549afSSteven Whitehouse {
259995c96174SEric Dumazet 	unsigned int i;
260063adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2601ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
260263adc6fbSStephen Hemminger 
2603ca6549afSSteven Whitehouse 	mpls--;
2604ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2605ca6549afSSteven Whitehouse }
2606ca6549afSSteven Whitehouse 
26070f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
26080f37c605SAl Viro 			       unsigned int prio)
26090f37c605SAl Viro {
26100f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
26110f37c605SAl Viro }
26120f37c605SAl Viro 
261326ad7879SEric Dumazet static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
261426ad7879SEric Dumazet 				int datalen)
261526ad7879SEric Dumazet {
261626ad7879SEric Dumazet 	struct timeval timestamp;
261726ad7879SEric Dumazet 	struct pktgen_hdr *pgh;
261826ad7879SEric Dumazet 
261926ad7879SEric Dumazet 	pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
262026ad7879SEric Dumazet 	datalen -= sizeof(*pgh);
262126ad7879SEric Dumazet 
262226ad7879SEric Dumazet 	if (pkt_dev->nfrags <= 0) {
262305aebe2eSDaniel Turull 		memset(skb_put(skb, datalen), 0, datalen);
262426ad7879SEric Dumazet 	} else {
262526ad7879SEric Dumazet 		int frags = pkt_dev->nfrags;
262626ad7879SEric Dumazet 		int i, len;
26277d36a991Samit salecha 		int frag_len;
262826ad7879SEric Dumazet 
262926ad7879SEric Dumazet 
263026ad7879SEric Dumazet 		if (frags > MAX_SKB_FRAGS)
263126ad7879SEric Dumazet 			frags = MAX_SKB_FRAGS;
263226ad7879SEric Dumazet 		len = datalen - frags * PAGE_SIZE;
263326ad7879SEric Dumazet 		if (len > 0) {
263426ad7879SEric Dumazet 			memset(skb_put(skb, len), 0, len);
263526ad7879SEric Dumazet 			datalen = frags * PAGE_SIZE;
263626ad7879SEric Dumazet 		}
263726ad7879SEric Dumazet 
263826ad7879SEric Dumazet 		i = 0;
26397d36a991Samit salecha 		frag_len = (datalen/frags) < PAGE_SIZE ?
26407d36a991Samit salecha 			   (datalen/frags) : PAGE_SIZE;
264126ad7879SEric Dumazet 		while (datalen > 0) {
264226ad7879SEric Dumazet 			if (unlikely(!pkt_dev->page)) {
264326ad7879SEric Dumazet 				int node = numa_node_id();
264426ad7879SEric Dumazet 
264526ad7879SEric Dumazet 				if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
264626ad7879SEric Dumazet 					node = pkt_dev->node;
264726ad7879SEric Dumazet 				pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
264826ad7879SEric Dumazet 				if (!pkt_dev->page)
264926ad7879SEric Dumazet 					break;
265026ad7879SEric Dumazet 			}
2651a0bec1cdSIan Campbell 			get_page(pkt_dev->page);
2652ea2ab693SIan Campbell 			skb_frag_set_page(skb, i, pkt_dev->page);
265326ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page_offset = 0;
26547d36a991Samit salecha 			/*last fragment, fill rest of data*/
26557d36a991Samit salecha 			if (i == (frags - 1))
26569e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i],
26579e903e08SEric Dumazet 				    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
26587d36a991Samit salecha 			else
26599e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
26609e903e08SEric Dumazet 			datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
26619e903e08SEric Dumazet 			skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
26629e903e08SEric Dumazet 			skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
266326ad7879SEric Dumazet 			i++;
266426ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
266526ad7879SEric Dumazet 		}
266626ad7879SEric Dumazet 	}
266726ad7879SEric Dumazet 
266826ad7879SEric Dumazet 	/* Stamp the time, and sequence number,
266926ad7879SEric Dumazet 	 * convert them to network byte order
267026ad7879SEric Dumazet 	 */
267126ad7879SEric Dumazet 	pgh->pgh_magic = htonl(PKTGEN_MAGIC);
267226ad7879SEric Dumazet 	pgh->seq_num = htonl(pkt_dev->seq_num);
267326ad7879SEric Dumazet 
267426ad7879SEric Dumazet 	do_gettimeofday(&timestamp);
267526ad7879SEric Dumazet 	pgh->tv_sec = htonl(timestamp.tv_sec);
267626ad7879SEric Dumazet 	pgh->tv_usec = htonl(timestamp.tv_usec);
267726ad7879SEric Dumazet }
267826ad7879SEric Dumazet 
26797a6e288dSDaniel Borkmann static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
26807a6e288dSDaniel Borkmann 					struct pktgen_dev *pkt_dev,
26817a6e288dSDaniel Borkmann 					unsigned int extralen)
26827a6e288dSDaniel Borkmann {
26837a6e288dSDaniel Borkmann 	struct sk_buff *skb = NULL;
26847a6e288dSDaniel Borkmann 	unsigned int size = pkt_dev->cur_pkt_size + 64 + extralen +
26857a6e288dSDaniel Borkmann 			    pkt_dev->pkt_overhead;
26867a6e288dSDaniel Borkmann 
26877a6e288dSDaniel Borkmann 	if (pkt_dev->flags & F_NODE) {
26887a6e288dSDaniel Borkmann 		int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
26897a6e288dSDaniel Borkmann 
26907a6e288dSDaniel Borkmann 		skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
26917a6e288dSDaniel Borkmann 		if (likely(skb)) {
26927a6e288dSDaniel Borkmann 			skb_reserve(skb, NET_SKB_PAD);
26937a6e288dSDaniel Borkmann 			skb->dev = dev;
26947a6e288dSDaniel Borkmann 		}
26957a6e288dSDaniel Borkmann 	} else {
26967a6e288dSDaniel Borkmann 		 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
26977a6e288dSDaniel Borkmann 	}
26987a6e288dSDaniel Borkmann 
26997a6e288dSDaniel Borkmann 	return skb;
27007a6e288dSDaniel Borkmann }
27017a6e288dSDaniel Borkmann 
27021da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
27031da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
27041da177e4SLinus Torvalds {
27051da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
27061da177e4SLinus Torvalds 	__u8 *eth;
27071da177e4SLinus Torvalds 	struct udphdr *udph;
27081da177e4SLinus Torvalds 	int datalen, iplen;
27091da177e4SLinus Torvalds 	struct iphdr *iph;
2710d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2711ca6549afSSteven Whitehouse 	__be32 *mpls;
271234954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
271334954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
271434954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
271534954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2716fd2ea0a7SDavid S. Miller 	u16 queue_map;
2717ca6549afSSteven Whitehouse 
2718ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2719d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
27201da177e4SLinus Torvalds 
272134954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2722d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
272334954ddcSFrancesco Fondelli 
272464053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
272564053beeSRobert Olsson 	 * fields.
272664053beeSRobert Olsson 	 */
272764053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2728eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
272964053beeSRobert Olsson 
27307ac5459eSDavid S. Miller 	datalen = (odev->hard_header_len + 16) & ~0xf;
2731e99b99b4SRobert Olsson 
27327a6e288dSDaniel Borkmann 	skb = pktgen_alloc_skb(odev, pkt_dev, datalen);
27331da177e4SLinus Torvalds 	if (!skb) {
27341da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
27351da177e4SLinus Torvalds 		return NULL;
27361da177e4SLinus Torvalds 	}
27371da177e4SLinus Torvalds 
27387a6e288dSDaniel Borkmann 	prefetchw(skb->data);
27397ac5459eSDavid S. Miller 	skb_reserve(skb, datalen);
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
27421da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2743ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2744ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2745ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
274634954ddcSFrancesco Fondelli 
274734954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
274834954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
274934954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27500f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
27510f37c605SAl Viro 					       pkt_dev->svlan_cfi,
27520f37c605SAl Viro 					       pkt_dev->svlan_p);
275334954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2754d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
275534954ddcSFrancesco Fondelli 		}
275634954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27570f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
27580f37c605SAl Viro 				      pkt_dev->vlan_cfi,
27590f37c605SAl Viro 				      pkt_dev->vlan_p);
276034954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2761d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
276234954ddcSFrancesco Fondelli 	}
276334954ddcSFrancesco Fondelli 
2764525cebedSThomas Graf 	skb_set_mac_header(skb, 0);
2765525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2766525cebedSThomas Graf 	iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2767525cebedSThomas Graf 
2768525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
2769525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
2770fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
27719e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
27729e50e3acSJohn Fastabend 
27731da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2774252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
27751da177e4SLinus Torvalds 
2776ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2777ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
277816dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
27796af773e7SNishank Trivedi 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
27801da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
27831da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
27841da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
2785c26bf4a5SThomas Graf 	udph->check = 0;
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	iph->ihl = 5;
27881da177e4SLinus Torvalds 	iph->version = 4;
27891da177e4SLinus Torvalds 	iph->ttl = 32;
27901ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
27911da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
27921da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
27931da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
279466ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
279566ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
27961da177e4SLinus Torvalds 	iph->frag_off = 0;
27971da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
27981da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
279903c633e7SThomas Graf 	ip_send_check(iph);
2800ca6549afSSteven Whitehouse 	skb->protocol = protocol;
28011da177e4SLinus Torvalds 	skb->dev = odev;
28021da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
2803c26bf4a5SThomas Graf 
2804c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2805c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2806c26bf4a5SThomas Graf 	} else if (odev->features & NETIF_F_V4_CSUM) {
2807c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2808c26bf4a5SThomas Graf 		skb->csum = 0;
2809c26bf4a5SThomas Graf 		udp4_hwcsum(skb, udph->source, udph->dest);
2810c26bf4a5SThomas Graf 	} else {
2811c26bf4a5SThomas Graf 		__wsum csum = udp_csum(skb);
2812c26bf4a5SThomas Graf 
2813c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
2814c26bf4a5SThomas Graf 		udph->check = csum_tcpudp_magic(udph->source, udph->dest,
2815c26bf4a5SThomas Graf 						datalen + 8, IPPROTO_UDP, csum);
2816c26bf4a5SThomas Graf 
2817c26bf4a5SThomas Graf 		if (udph->check == 0)
2818c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2819c26bf4a5SThomas Graf 	}
2820c26bf4a5SThomas Graf 
282126ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
28221da177e4SLinus Torvalds 
2823a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2824a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2825a553e4a6SJamal Hadi Salim 		return NULL;
2826a553e4a6SJamal Hadi Salim #endif
2827a553e4a6SJamal Hadi Salim 
28281da177e4SLinus Torvalds 	return skb;
28291da177e4SLinus Torvalds }
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
28321da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
28331da177e4SLinus Torvalds {
28341da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
28351da177e4SLinus Torvalds 	__u8 *eth;
28361da177e4SLinus Torvalds 	struct udphdr *udph;
2837c26bf4a5SThomas Graf 	int datalen, udplen;
28381da177e4SLinus Torvalds 	struct ipv6hdr *iph;
2839d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2840ca6549afSSteven Whitehouse 	__be32 *mpls;
284134954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
284234954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
284334954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
284434954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2845fd2ea0a7SDavid S. Miller 	u16 queue_map;
2846ca6549afSSteven Whitehouse 
2847ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2848d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
28491da177e4SLinus Torvalds 
285034954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2851d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
285234954ddcSFrancesco Fondelli 
285364053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
285464053beeSRobert Olsson 	 * fields.
285564053beeSRobert Olsson 	 */
285664053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2857eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
285864053beeSRobert Olsson 
28597a6e288dSDaniel Borkmann 	skb = pktgen_alloc_skb(odev, pkt_dev, 16);
28601da177e4SLinus Torvalds 	if (!skb) {
28611da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
28621da177e4SLinus Torvalds 		return NULL;
28631da177e4SLinus Torvalds 	}
28641da177e4SLinus Torvalds 
28657a6e288dSDaniel Borkmann 	prefetchw(skb->data);
28661da177e4SLinus Torvalds 	skb_reserve(skb, 16);
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
28691da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2870ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2871ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2872ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
287334954ddcSFrancesco Fondelli 
287434954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
287534954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
287634954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28770f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
28780f37c605SAl Viro 					       pkt_dev->svlan_cfi,
28790f37c605SAl Viro 					       pkt_dev->svlan_p);
288034954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2881d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
288234954ddcSFrancesco Fondelli 		}
288334954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28840f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
28850f37c605SAl Viro 				      pkt_dev->vlan_cfi,
28860f37c605SAl Viro 				      pkt_dev->vlan_p);
288734954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2888d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
288934954ddcSFrancesco Fondelli 	}
289034954ddcSFrancesco Fondelli 
2891525cebedSThomas Graf 	skb_set_mac_header(skb, 0);
2892525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2893525cebedSThomas Graf 	iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2894525cebedSThomas Graf 
2895525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
2896525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
2897fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
28989e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2901252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
29021da177e4SLinus Torvalds 
2903ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2904ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
2905ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
290616dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
29071da177e4SLinus Torvalds 
29085aa8b572SAmerigo Wang 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
29091da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
2910e87cc472SJoe Perches 		net_info_ratelimited("increased datalen to %d\n", datalen);
29111da177e4SLinus Torvalds 	}
29121da177e4SLinus Torvalds 
2913c26bf4a5SThomas Graf 	udplen = datalen + sizeof(struct udphdr);
29141da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
29151da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
2916c26bf4a5SThomas Graf 	udph->len = htons(udplen);
2917c26bf4a5SThomas Graf 	udph->check = 0;
29181da177e4SLinus Torvalds 
2919d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
29201da177e4SLinus Torvalds 
29211ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
29221ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
2923252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
29241ca7768cSFrancesco Fondelli 	}
29251ca7768cSFrancesco Fondelli 
29261da177e4SLinus Torvalds 	iph->hop_limit = 32;
29271da177e4SLinus Torvalds 
2928c26bf4a5SThomas Graf 	iph->payload_len = htons(udplen);
29291da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
29301da177e4SLinus Torvalds 
29314e3fd7a0SAlexey Dobriyan 	iph->daddr = pkt_dev->cur_in6_daddr;
29324e3fd7a0SAlexey Dobriyan 	iph->saddr = pkt_dev->cur_in6_saddr;
29331da177e4SLinus Torvalds 
2934ca6549afSSteven Whitehouse 	skb->protocol = protocol;
29351da177e4SLinus Torvalds 	skb->dev = odev;
29361da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
29371da177e4SLinus Torvalds 
2938c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2939c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2940c26bf4a5SThomas Graf 	} else if (odev->features & NETIF_F_V6_CSUM) {
2941c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2942c26bf4a5SThomas Graf 		skb->csum_start = skb_transport_header(skb) - skb->head;
2943c26bf4a5SThomas Graf 		skb->csum_offset = offsetof(struct udphdr, check);
2944c26bf4a5SThomas Graf 		udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2945c26bf4a5SThomas Graf 	} else {
2946c26bf4a5SThomas Graf 		__wsum csum = udp_csum(skb);
2947c26bf4a5SThomas Graf 
2948c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
2949c26bf4a5SThomas Graf 		udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2950c26bf4a5SThomas Graf 
2951c26bf4a5SThomas Graf 		if (udph->check == 0)
2952c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2953c26bf4a5SThomas Graf 	}
2954c26bf4a5SThomas Graf 
295526ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
29561da177e4SLinus Torvalds 
29571da177e4SLinus Torvalds 	return skb;
29581da177e4SLinus Torvalds }
29591da177e4SLinus Torvalds 
2960475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
29611da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
29621da177e4SLinus Torvalds {
29631da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
29641da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
29651da177e4SLinus Torvalds 	else
29661da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
29671da177e4SLinus Torvalds }
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
29701da177e4SLinus Torvalds {
29711da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
29721da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
29731da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
29741da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
29751da177e4SLinus Torvalds 	pkt_dev->errors = 0;
29761da177e4SLinus Torvalds }
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
29811da177e4SLinus Torvalds {
2982c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
29831da177e4SLinus Torvalds 	int started = 0;
29841da177e4SLinus Torvalds 
2985f9467eaeSJoe Perches 	func_enter();
29861da177e4SLinus Torvalds 
29871da177e4SLinus Torvalds 	if_lock(t);
2988c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds 		/*
29911da177e4SLinus Torvalds 		 * setup odev and create initial packet.
29921da177e4SLinus Torvalds 		 */
29931da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds 		if (pkt_dev->odev) {
29961da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
29971da177e4SLinus Torvalds 			pkt_dev->running = 1;	/* Cranke yeself! */
29981da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
2999398f382cSDaniel Borkmann 			pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3000fd29cf72SStephen Hemminger 
300116dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
30041da177e4SLinus Torvalds 			started++;
3005222f1806SLuiz Capitulino 		} else
30061da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
30071da177e4SLinus Torvalds 	}
30081da177e4SLinus Torvalds 	if_unlock(t);
3009222f1806SLuiz Capitulino 	if (started)
3010222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
30111da177e4SLinus Torvalds }
30121da177e4SLinus Torvalds 
30134e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
30141da177e4SLinus Torvalds {
3015cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30161da177e4SLinus Torvalds 
3017f9467eaeSJoe Perches 	func_enter();
30181da177e4SLinus Torvalds 
30196146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3020cdcdbe0bSLuiz Capitulino 
30214e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
302295ed63f7SArthur Kepner 		t->control |= T_STOP;
3023cdcdbe0bSLuiz Capitulino 
30246146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
30251da177e4SLinus Torvalds }
30261da177e4SLinus Torvalds 
3027648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
30281da177e4SLinus Torvalds {
3029648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
30301da177e4SLinus Torvalds 
3031c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
3032648fda74SStephen Hemminger 		if (pkt_dev->running)
3033648fda74SStephen Hemminger 			return 1;
3034648fda74SStephen Hemminger 	return 0;
30351da177e4SLinus Torvalds }
30361da177e4SLinus Torvalds 
30371da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
30381da177e4SLinus Torvalds {
30391da177e4SLinus Torvalds 	if_lock(t);
30401da177e4SLinus Torvalds 
30411da177e4SLinus Torvalds 	while (thread_is_running(t)) {
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 		if_unlock(t);
30441da177e4SLinus Torvalds 
30451da177e4SLinus Torvalds 		msleep_interruptible(100);
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds 		if (signal_pending(current))
30481da177e4SLinus Torvalds 			goto signal;
30491da177e4SLinus Torvalds 		if_lock(t);
30501da177e4SLinus Torvalds 	}
30511da177e4SLinus Torvalds 	if_unlock(t);
30521da177e4SLinus Torvalds 	return 1;
30531da177e4SLinus Torvalds signal:
30541da177e4SLinus Torvalds 	return 0;
30551da177e4SLinus Torvalds }
30561da177e4SLinus Torvalds 
30574e58a027SCong Wang static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
30581da177e4SLinus Torvalds {
3059cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30601da177e4SLinus Torvalds 	int sig = 1;
30611da177e4SLinus Torvalds 
30626146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3063cdcdbe0bSLuiz Capitulino 
30644e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
30651da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
3066222f1806SLuiz Capitulino 		if (sig == 0)
3067222f1806SLuiz Capitulino 			break;
30681da177e4SLinus Torvalds 	}
3069cdcdbe0bSLuiz Capitulino 
3070cdcdbe0bSLuiz Capitulino 	if (sig == 0)
30714e58a027SCong Wang 		list_for_each_entry(t, &pn->pktgen_threads, th_list)
30721da177e4SLinus Torvalds 			t->control |= (T_STOP);
3073cdcdbe0bSLuiz Capitulino 
30746146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
30751da177e4SLinus Torvalds 	return sig;
30761da177e4SLinus Torvalds }
30771da177e4SLinus Torvalds 
30784e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn)
30791da177e4SLinus Torvalds {
3080cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30811da177e4SLinus Torvalds 
3082f9467eaeSJoe Perches 	func_enter();
30831da177e4SLinus Torvalds 
30846146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
30851da177e4SLinus Torvalds 
30864e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
30871da177e4SLinus Torvalds 		t->control |= (T_RUN);
3088cdcdbe0bSLuiz Capitulino 
30896146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
30901da177e4SLinus Torvalds 
309163adc6fbSStephen Hemminger 	/* Propagate thread->control  */
309263adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
30931da177e4SLinus Torvalds 
30944e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
30951da177e4SLinus Torvalds }
30961da177e4SLinus Torvalds 
30974e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn)
3098eb37b41cSJesse Brandeburg {
3099eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3100eb37b41cSJesse Brandeburg 
3101f9467eaeSJoe Perches 	func_enter();
3102eb37b41cSJesse Brandeburg 
3103eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3104eb37b41cSJesse Brandeburg 
31054e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
3106eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3107eb37b41cSJesse Brandeburg 
3108eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3109eb37b41cSJesse Brandeburg 
311063adc6fbSStephen Hemminger 	/* Propagate thread->control  */
311163adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3112eb37b41cSJesse Brandeburg 
31134e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
3114eb37b41cSJesse Brandeburg }
3115eb37b41cSJesse Brandeburg 
31161da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
31171da177e4SLinus Torvalds {
3118fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
31191da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3120fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3121fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3122fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
31231da177e4SLinus Torvalds 
312403a14ab1SDaniel Turull 	p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3125fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3126fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3127fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
31281da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
31291da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
31301da177e4SLinus Torvalds 
3131fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3132fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
31351da177e4SLinus Torvalds 
31361da177e4SLinus Torvalds 	mbps = bps;
31371da177e4SLinus Torvalds 	do_div(mbps, 1000000);
31381da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
31391da177e4SLinus Torvalds 		     (unsigned long long)pps,
31401da177e4SLinus Torvalds 		     (unsigned long long)mbps,
31411da177e4SLinus Torvalds 		     (unsigned long long)bps,
31421da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
31431da177e4SLinus Torvalds }
31441da177e4SLinus Torvalds 
31451da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
31461da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
31471da177e4SLinus Torvalds {
3148222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds 	if (!pkt_dev->running) {
3151f9467eaeSJoe Perches 		pr_warning("interface: %s is already stopped\n",
3152f9467eaeSJoe Perches 			   pkt_dev->odevname);
31531da177e4SLinus Torvalds 		return -EINVAL;
31541da177e4SLinus Torvalds 	}
31551da177e4SLinus Torvalds 
31563bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
31573bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3158398f382cSDaniel Borkmann 	pkt_dev->stopped_at = ktime_get();
31591da177e4SLinus Torvalds 	pkt_dev->running = 0;
31601da177e4SLinus Torvalds 
316195ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
31621da177e4SLinus Torvalds 
31631da177e4SLinus Torvalds 	return 0;
31641da177e4SLinus Torvalds }
31651da177e4SLinus Torvalds 
31661da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
31671da177e4SLinus Torvalds {
3168c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
31691da177e4SLinus Torvalds 
31701da177e4SLinus Torvalds 	if_lock(t);
31711da177e4SLinus Torvalds 
3172c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3173c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3174222f1806SLuiz Capitulino 			continue;
3175222f1806SLuiz Capitulino 		if (best == NULL)
3176c26a8016SLuiz Capitulino 			best = pkt_dev;
3177398f382cSDaniel Borkmann 		else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3178c26a8016SLuiz Capitulino 			best = pkt_dev;
31791da177e4SLinus Torvalds 	}
31801da177e4SLinus Torvalds 	if_unlock(t);
31811da177e4SLinus Torvalds 	return best;
31821da177e4SLinus Torvalds }
31831da177e4SLinus Torvalds 
3184222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3185222f1806SLuiz Capitulino {
3186c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
31871da177e4SLinus Torvalds 
3188f9467eaeSJoe Perches 	func_enter();
31891da177e4SLinus Torvalds 
31901da177e4SLinus Torvalds 	if_lock(t);
31911da177e4SLinus Torvalds 
3192c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3193c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
319495ed63f7SArthur Kepner 	}
319595ed63f7SArthur Kepner 
319695ed63f7SArthur Kepner 	if_unlock(t);
319795ed63f7SArthur Kepner }
319895ed63f7SArthur Kepner 
319995ed63f7SArthur Kepner /*
320095ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
320195ed63f7SArthur Kepner  * and remove it
320295ed63f7SArthur Kepner  */
320395ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
320495ed63f7SArthur Kepner {
3205c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3206c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
320795ed63f7SArthur Kepner 
3208f9467eaeSJoe Perches 	func_enter();
320995ed63f7SArthur Kepner 
321095ed63f7SArthur Kepner 	if_lock(t);
321195ed63f7SArthur Kepner 
3212c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3213c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
321495ed63f7SArthur Kepner 
3215222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3216222f1806SLuiz Capitulino 			continue;
321795ed63f7SArthur Kepner 
321895ed63f7SArthur Kepner 		kfree_skb(cur->skb);
321995ed63f7SArthur Kepner 		cur->skb = NULL;
322095ed63f7SArthur Kepner 
322195ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
322295ed63f7SArthur Kepner 
322395ed63f7SArthur Kepner 		break;
322495ed63f7SArthur Kepner 	}
32251da177e4SLinus Torvalds 
32261da177e4SLinus Torvalds 	if_unlock(t);
32271da177e4SLinus Torvalds }
32281da177e4SLinus Torvalds 
32291da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
32301da177e4SLinus Torvalds {
3231c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3232c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
32331da177e4SLinus Torvalds 
3234f9467eaeSJoe Perches 	func_enter();
3235f9467eaeSJoe Perches 
32361da177e4SLinus Torvalds 	/* Remove all devices, free mem */
32371da177e4SLinus Torvalds 
32381da177e4SLinus Torvalds 	if_lock(t);
32391da177e4SLinus Torvalds 
3240c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3241c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
324295ed63f7SArthur Kepner 
324395ed63f7SArthur Kepner 		kfree_skb(cur->skb);
324495ed63f7SArthur Kepner 		cur->skb = NULL;
324595ed63f7SArthur Kepner 
32461da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
32471da177e4SLinus Torvalds 	}
32481da177e4SLinus Torvalds 
32491da177e4SLinus Torvalds 	if_unlock(t);
32501da177e4SLinus Torvalds }
32511da177e4SLinus Torvalds 
32521da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
32531da177e4SLinus Torvalds {
32541da177e4SLinus Torvalds 	/* Remove from the thread list */
32554e58a027SCong Wang 	remove_proc_entry(t->tsk->comm, t->net->proc_dir);
32561da177e4SLinus Torvalds }
32571da177e4SLinus Torvalds 
3258ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
32593791decbSStephen Hemminger {
3260398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
32613791decbSStephen Hemminger 	schedule();
3262398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
32633791decbSStephen Hemminger }
32643791decbSStephen Hemminger 
3265ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3266ef87979cSStephen Hemminger {
3267398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
3268ef87979cSStephen Hemminger 
3269ef87979cSStephen Hemminger 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3270ef87979cSStephen Hemminger 		if (signal_pending(current))
3271ef87979cSStephen Hemminger 			break;
3272ef87979cSStephen Hemminger 
3273ef87979cSStephen Hemminger 		if (need_resched())
3274ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3275ef87979cSStephen Hemminger 		else
3276ef87979cSStephen Hemminger 			cpu_relax();
3277ef87979cSStephen Hemminger 	}
3278398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3279ef87979cSStephen Hemminger }
3280fd29cf72SStephen Hemminger 
3281475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
32821da177e4SLinus Torvalds {
328300829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
32846fef4c0cSStephen Hemminger 	netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
328500829823SStephen Hemminger 		= odev->netdev_ops->ndo_start_xmit;
3286fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
3287fd2ea0a7SDavid S. Miller 	u16 queue_map;
32881da177e4SLinus Torvalds 	int ret;
32891da177e4SLinus Torvalds 
3290ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3291ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
32921da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
32933791decbSStephen Hemminger 		return;
32941da177e4SLinus Torvalds 	}
32951da177e4SLinus Torvalds 
3296ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3297ef87979cSStephen Hemminger 	 * "never transmit"
3298ef87979cSStephen Hemminger 	 */
3299ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3300398f382cSDaniel Borkmann 		pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3301ef87979cSStephen Hemminger 		return;
3302ef87979cSStephen Hemminger 	}
3303ef87979cSStephen Hemminger 
3304ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
33057d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
33067d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
33071da177e4SLinus Torvalds 		/* build a new pkt */
33081da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
33091da177e4SLinus Torvalds 
33101da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
33111da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
3312f9467eaeSJoe Perches 			pr_err("ERROR: couldn't allocate skb in fill_packet\n");
33131da177e4SLinus Torvalds 			schedule();
33141da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
33153791decbSStephen Hemminger 			return;
33161da177e4SLinus Torvalds 		}
3317baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
33181da177e4SLinus Torvalds 		pkt_dev->allocated_skbs++;
33191da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
33201da177e4SLinus Torvalds 	}
33211da177e4SLinus Torvalds 
3322ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3323ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3324ef87979cSStephen Hemminger 
3325fd2ea0a7SDavid S. Miller 	queue_map = skb_get_queue_mapping(pkt_dev->skb);
3326fd2ea0a7SDavid S. Miller 	txq = netdev_get_tx_queue(odev, queue_map);
3327fd2ea0a7SDavid S. Miller 
3328fd2ea0a7SDavid S. Miller 	__netif_tx_lock_bh(txq);
33295b8db2f5SStephen Hemminger 
333073466498STom Herbert 	if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
3331ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
33320835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
33330835acfeSEric Dumazet 		goto unlock;
33340835acfeSEric Dumazet 	}
33350835acfeSEric Dumazet 	atomic_inc(&(pkt_dev->skb->users));
333600829823SStephen Hemminger 	ret = (*xmit)(pkt_dev->skb, odev);
3337ef87979cSStephen Hemminger 
33385b8db2f5SStephen Hemminger 	switch (ret) {
33395b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
334008baf561SEric Dumazet 		txq_trans_update(txq);
33411da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
33421da177e4SLinus Torvalds 		pkt_dev->sofar++;
33431da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3344baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
33455b8db2f5SStephen Hemminger 		break;
3346f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3347f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3348f466dba1SJohn Fastabend 	case NET_XMIT_POLICED:
3349f466dba1SJohn Fastabend 		/* skb has been consumed */
3350f466dba1SJohn Fastabend 		pkt_dev->errors++;
3351f466dba1SJohn Fastabend 		break;
33525b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
3353e87cc472SJoe Perches 		net_info_ratelimited("%s xmit error: %d\n",
3354e87cc472SJoe Perches 				     pkt_dev->odevname, ret);
33551da177e4SLinus Torvalds 		pkt_dev->errors++;
33565b8db2f5SStephen Hemminger 		/* fallthru */
3357ef87979cSStephen Hemminger 	case NETDEV_TX_LOCKED:
33585b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
33595b8db2f5SStephen Hemminger 		/* Retry it next time */
33605b8db2f5SStephen Hemminger 		atomic_dec(&(pkt_dev->skb->users));
33611da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
33621da177e4SLinus Torvalds 	}
33630835acfeSEric Dumazet unlock:
3364fd2ea0a7SDavid S. Miller 	__netif_tx_unlock_bh(txq);
33651da177e4SLinus Torvalds 
33661da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
33671da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3368ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
33691da177e4SLinus Torvalds 
33701da177e4SLinus Torvalds 		/* Done with this */
33711da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
33721da177e4SLinus Torvalds 	}
33731da177e4SLinus Torvalds }
33741da177e4SLinus Torvalds 
33751da177e4SLinus Torvalds /*
33761da177e4SLinus Torvalds  * Main loop of the thread goes here
33771da177e4SLinus Torvalds  */
33781da177e4SLinus Torvalds 
3379ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
33801da177e4SLinus Torvalds {
33811da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3382ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
33831da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
33841da177e4SLinus Torvalds 	int cpu = t->cpu;
33851da177e4SLinus Torvalds 
3386ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3389d3ede327SDenis V. Lunev 	complete(&t->start_done);
33901da177e4SLinus Torvalds 
3391f9467eaeSJoe Perches 	pr_debug("starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
33921da177e4SLinus Torvalds 
3393ee74baa7SDavid S. Miller 	set_current_state(TASK_INTERRUPTIBLE);
33941da177e4SLinus Torvalds 
339583144186SRafael J. Wysocki 	set_freezable();
339683144186SRafael J. Wysocki 
3397ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3398ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3399ee74baa7SDavid S. Miller 
3400ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
34014e58a027SCong Wang 			if (t->net->pktgen_exiting)
3402551eaff1SEric Dumazet 				break;
3403ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3404ef87979cSStephen Hemminger 							 t->control != 0,
3405ef87979cSStephen Hemminger 							 HZ/10);
34061b3f720bSRafael J. Wysocki 			try_to_freeze();
3407ef87979cSStephen Hemminger 			continue;
3408ee74baa7SDavid S. Miller 		}
34091da177e4SLinus Torvalds 
34101da177e4SLinus Torvalds 		__set_current_state(TASK_RUNNING);
34111da177e4SLinus Torvalds 
3412ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
34131da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
34141da177e4SLinus Torvalds 
3415ef87979cSStephen Hemminger 			if (need_resched())
3416ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3417ef87979cSStephen Hemminger 			else
3418ef87979cSStephen Hemminger 				cpu_relax();
3419ef87979cSStephen Hemminger 		}
3420ef87979cSStephen Hemminger 
34211da177e4SLinus Torvalds 		if (t->control & T_STOP) {
34221da177e4SLinus Torvalds 			pktgen_stop(t);
34231da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
34241da177e4SLinus Torvalds 		}
34251da177e4SLinus Torvalds 
34261da177e4SLinus Torvalds 		if (t->control & T_RUN) {
34271da177e4SLinus Torvalds 			pktgen_run(t);
34281da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
34291da177e4SLinus Torvalds 		}
34301da177e4SLinus Torvalds 
343195ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
34321da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
343395ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
343495ed63f7SArthur Kepner 		}
343595ed63f7SArthur Kepner 
343695ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
343795ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
34381da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
34391da177e4SLinus Torvalds 		}
34401da177e4SLinus Torvalds 
344109fe3ef4SAndrew Morton 		try_to_freeze();
344209fe3ef4SAndrew Morton 
3443ee74baa7SDavid S. Miller 		set_current_state(TASK_INTERRUPTIBLE);
34441da177e4SLinus Torvalds 	}
34451da177e4SLinus Torvalds 
3446f9467eaeSJoe Perches 	pr_debug("%s stopping all device\n", t->tsk->comm);
34471da177e4SLinus Torvalds 	pktgen_stop(t);
34481da177e4SLinus Torvalds 
3449f9467eaeSJoe Perches 	pr_debug("%s removing all device\n", t->tsk->comm);
34501da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
34511da177e4SLinus Torvalds 
3452f9467eaeSJoe Perches 	pr_debug("%s removing thread\n", t->tsk->comm);
34531da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3454cdcdbe0bSLuiz Capitulino 
3455551eaff1SEric Dumazet 	/* Wait for kthread_stop */
3456551eaff1SEric Dumazet 	while (!kthread_should_stop()) {
3457551eaff1SEric Dumazet 		set_current_state(TASK_INTERRUPTIBLE);
3458551eaff1SEric Dumazet 		schedule();
3459551eaff1SEric Dumazet 	}
3460551eaff1SEric Dumazet 	__set_current_state(TASK_RUNNING);
3461551eaff1SEric Dumazet 
3462ee74baa7SDavid S. Miller 	return 0;
34631da177e4SLinus Torvalds }
34641da177e4SLinus Torvalds 
3465222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
34663e984840SEric Dumazet 					  const char *ifname, bool exact)
34671da177e4SLinus Torvalds {
3468c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
34693e984840SEric Dumazet 	size_t len = strlen(ifname);
34701da177e4SLinus Torvalds 
34713e984840SEric Dumazet 	if_lock(t);
3472c26a8016SLuiz Capitulino 	list_for_each_entry(p, &t->if_list, list)
34733e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
34743e984840SEric Dumazet 			if (p->odevname[len]) {
34753e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
34763e984840SEric Dumazet 					continue;
34773e984840SEric Dumazet 			}
3478c26a8016SLuiz Capitulino 			pkt_dev = p;
34791da177e4SLinus Torvalds 			break;
34801da177e4SLinus Torvalds 		}
34811da177e4SLinus Torvalds 
34821da177e4SLinus Torvalds 	if_unlock(t);
3483f9467eaeSJoe Perches 	pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
34841da177e4SLinus Torvalds 	return pkt_dev;
34851da177e4SLinus Torvalds }
34861da177e4SLinus Torvalds 
34871da177e4SLinus Torvalds /*
34881da177e4SLinus Torvalds  * Adds a dev at front of if_list.
34891da177e4SLinus Torvalds  */
34901da177e4SLinus Torvalds 
3491222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3492222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
34931da177e4SLinus Torvalds {
34941da177e4SLinus Torvalds 	int rv = 0;
34951da177e4SLinus Torvalds 
34961da177e4SLinus Torvalds 	if_lock(t);
34971da177e4SLinus Torvalds 
34981da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
3499f9467eaeSJoe Perches 		pr_err("ERROR: already assigned to a thread\n");
35001da177e4SLinus Torvalds 		rv = -EBUSY;
35011da177e4SLinus Torvalds 		goto out;
35021da177e4SLinus Torvalds 	}
3503c26a8016SLuiz Capitulino 
3504c26a8016SLuiz Capitulino 	list_add(&pkt_dev->list, &t->if_list);
35051da177e4SLinus Torvalds 	pkt_dev->pg_thread = t;
35061da177e4SLinus Torvalds 	pkt_dev->running = 0;
35071da177e4SLinus Torvalds 
35081da177e4SLinus Torvalds out:
35091da177e4SLinus Torvalds 	if_unlock(t);
35101da177e4SLinus Torvalds 	return rv;
35111da177e4SLinus Torvalds }
35121da177e4SLinus Torvalds 
35131da177e4SLinus Torvalds /* Called under thread lock */
35141da177e4SLinus Torvalds 
35151da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
35161da177e4SLinus Torvalds {
35171da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
351839df232fSStephen Hemminger 	int err;
35193291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
35201da177e4SLinus Torvalds 
35211da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
35221da177e4SLinus Torvalds 
35234e58a027SCong Wang 	pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3524d50a6b56SStephen Hemminger 	if (pkt_dev) {
3525f9467eaeSJoe Perches 		pr_err("ERROR: interface already used\n");
3526d50a6b56SStephen Hemminger 		return -EBUSY;
3527d50a6b56SStephen Hemminger 	}
35281da177e4SLinus Torvalds 
35293291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
35301da177e4SLinus Torvalds 	if (!pkt_dev)
35311da177e4SLinus Torvalds 		return -ENOMEM;
35321da177e4SLinus Torvalds 
3533593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
353468d5ac2eSWANG Cong 	pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
35353291b9dbSEric Dumazet 				      node);
35361da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
35371da177e4SLinus Torvalds 		kfree(pkt_dev);
35381da177e4SLinus Torvalds 		return -ENOMEM;
35391da177e4SLinus Torvalds 	}
35401da177e4SLinus Torvalds 
354195ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
35421da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
3543fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
35441da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
35451da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
35461da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
35471da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
35481da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
35491da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
355034954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
355134954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
355234954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
355334954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
355434954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
355534954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
3556e99b99b4SRobert Olsson 	pkt_dev->node = -1;
355734954ddcSFrancesco Fondelli 
35584e58a027SCong Wang 	err = pktgen_setup_dev(t->net, pkt_dev, ifname);
355939df232fSStephen Hemminger 	if (err)
356039df232fSStephen Hemminger 		goto out1;
3561d8873315SNeil Horman 	if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3562d8873315SNeil Horman 		pkt_dev->clone_skb = pg_clone_skb_d;
35631da177e4SLinus Torvalds 
35644e58a027SCong Wang 	pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
35655efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
356639df232fSStephen Hemminger 	if (!pkt_dev->entry) {
3567f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3568d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
356939df232fSStephen Hemminger 		err = -EINVAL;
357039df232fSStephen Hemminger 		goto out2;
357139df232fSStephen Hemminger 	}
3572a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3573a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3574a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3575*cf93d47eSFan Du 
3576*cf93d47eSFan Du 	/* xfrm tunnel mode needs additional dst to extract outter
3577*cf93d47eSFan Du 	 * ip header protocol/ttl/id field, here creat a phony one.
3578*cf93d47eSFan Du 	 * instead of looking for a valid rt, which definitely hurting
3579*cf93d47eSFan Du 	 * performance under such circumstance.
3580*cf93d47eSFan Du 	 */
3581*cf93d47eSFan Du 	pkt_dev->dstops.family = AF_INET;
3582*cf93d47eSFan Du 	pkt_dev->dst.dev = pkt_dev->odev;
3583*cf93d47eSFan Du 	dst_init_metrics(&pkt_dev->dst, pktgen_dst_metrics, false);
3584*cf93d47eSFan Du 	pkt_dev->dst.child = &pkt_dev->dst;
3585*cf93d47eSFan Du 	pkt_dev->dst.ops = &pkt_dev->dstops;
3586a553e4a6SJamal Hadi Salim #endif
358739df232fSStephen Hemminger 
358839df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
358939df232fSStephen Hemminger out2:
359039df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
359139df232fSStephen Hemminger out1:
3592a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3593a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3594a553e4a6SJamal Hadi Salim #endif
35951da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
35961da177e4SLinus Torvalds 	kfree(pkt_dev);
359739df232fSStephen Hemminger 	return err;
35981da177e4SLinus Torvalds }
35991da177e4SLinus Torvalds 
36004e58a027SCong Wang static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
36011da177e4SLinus Torvalds {
3602cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3603d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3604ee74baa7SDavid S. Miller 	struct task_struct *p;
36051da177e4SLinus Torvalds 
36063291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
36073291b9dbSEric Dumazet 			 cpu_to_node(cpu));
36081da177e4SLinus Torvalds 	if (!t) {
3609f9467eaeSJoe Perches 		pr_err("ERROR: out of memory, can't create new thread\n");
36101da177e4SLinus Torvalds 		return -ENOMEM;
36111da177e4SLinus Torvalds 	}
36121da177e4SLinus Torvalds 
36131da177e4SLinus Torvalds 	spin_lock_init(&t->if_lock);
36141da177e4SLinus Torvalds 	t->cpu = cpu;
36151da177e4SLinus Torvalds 
3616ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3617ee74baa7SDavid S. Miller 
36184e58a027SCong Wang 	list_add_tail(&t->th_list, &pn->pktgen_threads);
3619d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3620ee74baa7SDavid S. Miller 
362194dcf29aSEric Dumazet 	p = kthread_create_on_node(pktgen_thread_worker,
362294dcf29aSEric Dumazet 				   t,
362394dcf29aSEric Dumazet 				   cpu_to_node(cpu),
362494dcf29aSEric Dumazet 				   "kpktgend_%d", cpu);
3625ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
3626f9467eaeSJoe Perches 		pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3627ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3628ee74baa7SDavid S. Miller 		kfree(t);
3629ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3630ee74baa7SDavid S. Miller 	}
3631ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3632ee74baa7SDavid S. Miller 	t->tsk = p;
3633ee74baa7SDavid S. Miller 
36344e58a027SCong Wang 	pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
36355efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3636d50a6b56SStephen Hemminger 	if (!pe) {
3637f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3638ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3639ee74baa7SDavid S. Miller 		kthread_stop(p);
3640ee74baa7SDavid S. Miller 		list_del(&t->th_list);
36411da177e4SLinus Torvalds 		kfree(t);
36421da177e4SLinus Torvalds 		return -EINVAL;
36431da177e4SLinus Torvalds 	}
3644d50a6b56SStephen Hemminger 
36454e58a027SCong Wang 	t->net = pn;
3646ee74baa7SDavid S. Miller 	wake_up_process(p);
3647d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
36481da177e4SLinus Torvalds 
36491da177e4SLinus Torvalds 	return 0;
36501da177e4SLinus Torvalds }
36511da177e4SLinus Torvalds 
36521da177e4SLinus Torvalds /*
36531da177e4SLinus Torvalds  * Removes a device from the thread if_list.
36541da177e4SLinus Torvalds  */
3655222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3656222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
36571da177e4SLinus Torvalds {
3658c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3659c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
36601da177e4SLinus Torvalds 
3661c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3662c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3663c26a8016SLuiz Capitulino 		if (p == pkt_dev)
3664c26a8016SLuiz Capitulino 			list_del(&p->list);
36651da177e4SLinus Torvalds 	}
36661da177e4SLinus Torvalds }
36671da177e4SLinus Torvalds 
3668222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3669222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
36701da177e4SLinus Torvalds {
3671f9467eaeSJoe Perches 	pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 	if (pkt_dev->running) {
3674f9467eaeSJoe Perches 		pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
36751da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
36761da177e4SLinus Torvalds 	}
36771da177e4SLinus Torvalds 
36781da177e4SLinus Torvalds 	/* Dis-associate from the interface */
36791da177e4SLinus Torvalds 
36801da177e4SLinus Torvalds 	if (pkt_dev->odev) {
36811da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
36821da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
36831da177e4SLinus Torvalds 	}
36841da177e4SLinus Torvalds 
36851da177e4SLinus Torvalds 	/* And update the thread if_list */
36861da177e4SLinus Torvalds 
36871da177e4SLinus Torvalds 	_rem_dev_from_if_list(t, pkt_dev);
36881da177e4SLinus Torvalds 
368939df232fSStephen Hemminger 	if (pkt_dev->entry)
3690a8ca16eaSDavid Howells 		proc_remove(pkt_dev->entry);
36911da177e4SLinus Torvalds 
3692a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3693a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3694a553e4a6SJamal Hadi Salim #endif
36951da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
369626ad7879SEric Dumazet 	if (pkt_dev->page)
369726ad7879SEric Dumazet 		put_page(pkt_dev->page);
36981da177e4SLinus Torvalds 	kfree(pkt_dev);
36991da177e4SLinus Torvalds 	return 0;
37001da177e4SLinus Torvalds }
37011da177e4SLinus Torvalds 
37024e58a027SCong Wang static int __net_init pg_net_init(struct net *net)
37031da177e4SLinus Torvalds {
37044e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3705d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
37064e58a027SCong Wang 	int cpu, ret = 0;
3707d50a6b56SStephen Hemminger 
37084e58a027SCong Wang 	pn->net = net;
37094e58a027SCong Wang 	INIT_LIST_HEAD(&pn->pktgen_threads);
37104e58a027SCong Wang 	pn->pktgen_exiting = false;
37114e58a027SCong Wang 	pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
37124e58a027SCong Wang 	if (!pn->proc_dir) {
37134e58a027SCong Wang 		pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3714d50a6b56SStephen Hemminger 		return -ENODEV;
37151da177e4SLinus Torvalds 	}
37164e58a027SCong Wang 	pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
37174e58a027SCong Wang 	if (pe == NULL) {
37184e58a027SCong Wang 		pr_err("cannot create %s procfs entry\n", PGCTRL);
37194e58a027SCong Wang 		ret = -EINVAL;
37204e58a027SCong Wang 		goto remove;
37214e58a027SCong Wang 	}
37221da177e4SLinus Torvalds 
3723670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
37248024bb24SLuiz Capitulino 		int err;
37251da177e4SLinus Torvalds 
37264e58a027SCong Wang 		err = pktgen_create_thread(cpu, pn);
37278024bb24SLuiz Capitulino 		if (err)
37284e58a027SCong Wang 			pr_warn("Cannot create thread for cpu %d (%d)\n",
3729f9467eaeSJoe Perches 				   cpu, err);
37301da177e4SLinus Torvalds 	}
37318024bb24SLuiz Capitulino 
37324e58a027SCong Wang 	if (list_empty(&pn->pktgen_threads)) {
37334e58a027SCong Wang 		pr_err("Initialization failed for all threads\n");
3734ce14f894SWANG Cong 		ret = -ENODEV;
37354e58a027SCong Wang 		goto remove_entry;
37368024bb24SLuiz Capitulino 	}
37378024bb24SLuiz Capitulino 
37381da177e4SLinus Torvalds 	return 0;
3739ce14f894SWANG Cong 
37404e58a027SCong Wang remove_entry:
37414e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
37424e58a027SCong Wang remove:
3743ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3744ce14f894SWANG Cong 	return ret;
37451da177e4SLinus Torvalds }
37461da177e4SLinus Torvalds 
37474e58a027SCong Wang static void __net_exit pg_net_exit(struct net *net)
37481da177e4SLinus Torvalds {
37494e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3750cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3751cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
3752d4b11335SEric Dumazet 	LIST_HEAD(list);
37531da177e4SLinus Torvalds 
37541da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
37554e58a027SCong Wang 	pn->pktgen_exiting = true;
37561da177e4SLinus Torvalds 
3757c57b5468SEric Dumazet 	mutex_lock(&pktgen_thread_lock);
37584e58a027SCong Wang 	list_splice_init(&pn->pktgen_threads, &list);
3759c57b5468SEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
3760c57b5468SEric Dumazet 
3761c57b5468SEric Dumazet 	list_for_each_safe(q, n, &list) {
3762cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3763c57b5468SEric Dumazet 		list_del(&t->th_list);
3764ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
3765ee74baa7SDavid S. Miller 		kfree(t);
37661da177e4SLinus Torvalds 	}
37671da177e4SLinus Torvalds 
37684e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
3769ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
37704e58a027SCong Wang }
37711da177e4SLinus Torvalds 
37724e58a027SCong Wang static struct pernet_operations pg_net_ops = {
37734e58a027SCong Wang 	.init = pg_net_init,
37744e58a027SCong Wang 	.exit = pg_net_exit,
37754e58a027SCong Wang 	.id   = &pg_net_id,
37764e58a027SCong Wang 	.size = sizeof(struct pktgen_net),
37774e58a027SCong Wang };
37784e58a027SCong Wang 
37794e58a027SCong Wang static int __init pg_init(void)
37804e58a027SCong Wang {
37814e58a027SCong Wang 	int ret = 0;
37824e58a027SCong Wang 
37834e58a027SCong Wang 	pr_info("%s", version);
37844e58a027SCong Wang 	ret = register_pernet_subsys(&pg_net_ops);
37854e58a027SCong Wang 	if (ret)
37864e58a027SCong Wang 		return ret;
37874e58a027SCong Wang 	ret = register_netdevice_notifier(&pktgen_notifier_block);
37884e58a027SCong Wang 	if (ret)
37894e58a027SCong Wang 		unregister_pernet_subsys(&pg_net_ops);
37904e58a027SCong Wang 
37914e58a027SCong Wang 	return ret;
37924e58a027SCong Wang }
37934e58a027SCong Wang 
37944e58a027SCong Wang static void __exit pg_cleanup(void)
37954e58a027SCong Wang {
37964e58a027SCong Wang 	unregister_netdevice_notifier(&pktgen_notifier_block);
37974e58a027SCong Wang 	unregister_pernet_subsys(&pg_net_ops);
37981da177e4SLinus Torvalds }
37991da177e4SLinus Torvalds 
38001da177e4SLinus Torvalds module_init(pg_init);
38011da177e4SLinus Torvalds module_exit(pg_cleanup);
38021da177e4SLinus Torvalds 
3803c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
38041da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
38051da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3806c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
38071da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3808c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
38091da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3810c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
38111da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3812c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
38131da177e4SLinus Torvalds module_param(debug, int, 0);
3814c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3815