xref: /openbmc/linux/net/core/pktgen.c (revision 4e58a0275015e5c8988bda9cd0635e2ef405c985)
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>
1631da177e4SLinus Torvalds #include <net/addrconf.h>
164a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
165a553e4a6SJamal Hadi Salim #include <net/xfrm.h>
166a553e4a6SJamal Hadi Salim #endif
167*4e58a027SCong Wang #include <net/netns/generic.h>
1681da177e4SLinus Torvalds #include <asm/byteorder.h>
1691da177e4SLinus Torvalds #include <linux/rcupdate.h>
1701977f032SJiri Slaby #include <linux/bitops.h>
17163adc6fbSStephen Hemminger #include <linux/io.h>
17263adc6fbSStephen Hemminger #include <linux/timex.h>
17363adc6fbSStephen Hemminger #include <linux/uaccess.h>
1741da177e4SLinus Torvalds #include <asm/dma.h>
1751da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1761da177e4SLinus Torvalds 
17743d28b65SDaniel Turull #define VERSION	"2.74"
1781da177e4SLinus Torvalds #define IP_NAME_SZ 32
179ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
180d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1811da177e4SLinus Torvalds 
182f9467eaeSJoe Perches #define func_enter() pr_debug("entering %s\n", __func__);
183f9467eaeSJoe Perches 
1841da177e4SLinus Torvalds /* Device flag bits */
1851da177e4SLinus Torvalds #define F_IPSRC_RND   (1<<0)	/* IP-Src Random  */
1861da177e4SLinus Torvalds #define F_IPDST_RND   (1<<1)	/* IP-Dst Random  */
1871da177e4SLinus Torvalds #define F_UDPSRC_RND  (1<<2)	/* UDP-Src Random */
1881da177e4SLinus Torvalds #define F_UDPDST_RND  (1<<3)	/* UDP-Dst Random */
1891da177e4SLinus Torvalds #define F_MACSRC_RND  (1<<4)	/* MAC-Src Random */
1901da177e4SLinus Torvalds #define F_MACDST_RND  (1<<5)	/* MAC-Dst Random */
1911da177e4SLinus Torvalds #define F_TXSIZE_RND  (1<<6)	/* Transmit size is random */
1921da177e4SLinus Torvalds #define F_IPV6        (1<<7)	/* Interface in IPV6 Mode */
193ca6549afSSteven Whitehouse #define F_MPLS_RND    (1<<8)	/* Random MPLS labels */
19434954ddcSFrancesco Fondelli #define F_VID_RND     (1<<9)	/* Random VLAN ID */
19534954ddcSFrancesco Fondelli #define F_SVID_RND    (1<<10)	/* Random SVLAN ID */
196007a531bSJamal Hadi Salim #define F_FLOW_SEQ    (1<<11)	/* Sequential flows */
197a553e4a6SJamal Hadi Salim #define F_IPSEC_ON    (1<<12)	/* ipsec on for flows */
19845b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13)	/* queue map Random */
199e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14)	/* queue map mirrors smp_processor_id() */
200e99b99b4SRobert Olsson #define F_NODE          (1<<15)	/* Node memory alloc*/
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds /* Thread control flag bits */
2036b80d6a6SStephen Hemminger #define T_STOP        (1<<0)	/* Stop run */
2046b80d6a6SStephen Hemminger #define T_RUN         (1<<1)	/* Start run */
2056b80d6a6SStephen Hemminger #define T_REMDEVALL   (1<<2)	/* Remove all devs */
2066b80d6a6SStephen Hemminger #define T_REMDEV      (1<<3)	/* Remove one dev */
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds /* If lock -- can be removed after some work */
2091da177e4SLinus Torvalds #define   if_lock(t)           spin_lock(&(t->if_lock));
2101da177e4SLinus Torvalds #define   if_unlock(t)           spin_unlock(&(t->if_lock));
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2131da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
214d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
215d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds #define MAX_CFLOWS  65536
2181da177e4SLinus Torvalds 
21934954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
22034954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
22134954ddcSFrancesco Fondelli 
222222f1806SLuiz Capitulino struct flow_state {
223252e3346SAl Viro 	__be32 cur_daddr;
2241da177e4SLinus Torvalds 	int count;
225a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
226a553e4a6SJamal Hadi Salim 	struct xfrm_state *x;
227a553e4a6SJamal Hadi Salim #endif
228007a531bSJamal Hadi Salim 	__u32 flags;
2291da177e4SLinus Torvalds };
2301da177e4SLinus Torvalds 
231007a531bSJamal Hadi Salim /* flow flag bits */
232007a531bSJamal Hadi Salim #define F_INIT   (1<<0)		/* flow has been initialized */
233007a531bSJamal Hadi Salim 
2341da177e4SLinus Torvalds struct pktgen_dev {
2351da177e4SLinus Torvalds 	/*
2361da177e4SLinus Torvalds 	 * Try to keep frequent/infrequent used vars. separated.
2371da177e4SLinus Torvalds 	 */
23839df232fSStephen Hemminger 	struct proc_dir_entry *entry;	/* proc file */
2391da177e4SLinus Torvalds 	struct pktgen_thread *pg_thread;/* the owner */
24063adc6fbSStephen Hemminger 	struct list_head list;		/* chaining in the thread's run-queue */
2411da177e4SLinus Torvalds 
24263adc6fbSStephen Hemminger 	int running;		/* if false, the test will stop */
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/* If min != max, then we will either do a linear iteration, or
2451da177e4SLinus Torvalds 	 * we will do a random selection from within the range.
2461da177e4SLinus Torvalds 	 */
2471da177e4SLinus Torvalds 	__u32 flags;
24895ed63f7SArthur Kepner 	int removal_mark;	/* non-zero => the device is marked for
24995ed63f7SArthur Kepner 				 * removal by worker thread */
2501da177e4SLinus Torvalds 
25168bf9f0bSAmerigo Wang 	int min_pkt_size;
25268bf9f0bSAmerigo Wang 	int max_pkt_size;
25316dab72fSJamal Hadi Salim 	int pkt_overhead;	/* overhead for MPLS, VLANs, IPSEC etc */
2541da177e4SLinus Torvalds 	int nfrags;
25526ad7879SEric Dumazet 	struct page *page;
256fd29cf72SStephen Hemminger 	u64 delay;		/* nano-seconds */
257fd29cf72SStephen Hemminger 
2581da177e4SLinus Torvalds 	__u64 count;		/* Default No packets to send */
2591da177e4SLinus Torvalds 	__u64 sofar;		/* How many pkts we've sent so far */
2601da177e4SLinus Torvalds 	__u64 tx_bytes;		/* How many bytes we've transmitted */
261f466dba1SJohn Fastabend 	__u64 errors;		/* Errors when trying to transmit, */
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	/* runtime counters relating to clone_skb */
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	__u64 allocated_skbs;
2661da177e4SLinus Torvalds 	__u32 clone_count;
2671da177e4SLinus Torvalds 	int last_ok;		/* Was last skb sent?
26863adc6fbSStephen Hemminger 				 * Or a failed transmit of some sort?
26963adc6fbSStephen Hemminger 				 * This will keep sequence numbers in order
2701da177e4SLinus Torvalds 				 */
271fd29cf72SStephen Hemminger 	ktime_t next_tx;
272fd29cf72SStephen Hemminger 	ktime_t started_at;
273fd29cf72SStephen Hemminger 	ktime_t stopped_at;
274fd29cf72SStephen Hemminger 	u64	idle_acc;	/* nano-seconds */
275fd29cf72SStephen Hemminger 
2761da177e4SLinus Torvalds 	__u32 seq_num;
2771da177e4SLinus Torvalds 
27863adc6fbSStephen Hemminger 	int clone_skb;		/*
27963adc6fbSStephen Hemminger 				 * Use multiple SKBs during packet gen.
28063adc6fbSStephen Hemminger 				 * If this number is greater than 1, then
28163adc6fbSStephen Hemminger 				 * that many copies of the same packet will be
28263adc6fbSStephen Hemminger 				 * sent before a new packet is allocated.
28363adc6fbSStephen Hemminger 				 * If you want to send 1024 identical packets
28463adc6fbSStephen Hemminger 				 * before creating a new packet,
28563adc6fbSStephen Hemminger 				 * set clone_skb to 1024.
2861da177e4SLinus Torvalds 				 */
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	char dst_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2891da177e4SLinus Torvalds 	char dst_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2901da177e4SLinus Torvalds 	char src_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2911da177e4SLinus Torvalds 	char src_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	struct in6_addr in6_saddr;
2941da177e4SLinus Torvalds 	struct in6_addr in6_daddr;
2951da177e4SLinus Torvalds 	struct in6_addr cur_in6_daddr;
2961da177e4SLinus Torvalds 	struct in6_addr cur_in6_saddr;
2971da177e4SLinus Torvalds 	/* For ranges */
2981da177e4SLinus Torvalds 	struct in6_addr min_in6_daddr;
2991da177e4SLinus Torvalds 	struct in6_addr max_in6_daddr;
3001da177e4SLinus Torvalds 	struct in6_addr min_in6_saddr;
3011da177e4SLinus Torvalds 	struct in6_addr max_in6_saddr;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	/* If we're doing ranges, random or incremental, then this
3041da177e4SLinus Torvalds 	 * defines the min/max for those ranges.
3051da177e4SLinus Torvalds 	 */
306252e3346SAl Viro 	__be32 saddr_min;	/* inclusive, source IP address */
307252e3346SAl Viro 	__be32 saddr_max;	/* exclusive, source IP address */
308252e3346SAl Viro 	__be32 daddr_min;	/* inclusive, dest IP address */
309252e3346SAl Viro 	__be32 daddr_max;	/* exclusive, dest IP address */
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	__u16 udp_src_min;	/* inclusive, source UDP port */
3121da177e4SLinus Torvalds 	__u16 udp_src_max;	/* exclusive, source UDP port */
3131da177e4SLinus Torvalds 	__u16 udp_dst_min;	/* inclusive, dest UDP port */
3141da177e4SLinus Torvalds 	__u16 udp_dst_max;	/* exclusive, dest UDP port */
3151da177e4SLinus Torvalds 
3161ca7768cSFrancesco Fondelli 	/* DSCP + ECN */
31763adc6fbSStephen Hemminger 	__u8 tos;            /* six MSB of (former) IPv4 TOS
31863adc6fbSStephen Hemminger 				are for dscp codepoint */
31963adc6fbSStephen Hemminger 	__u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6
32063adc6fbSStephen Hemminger 				(see RFC 3260, sec. 4) */
3211ca7768cSFrancesco Fondelli 
322ca6549afSSteven Whitehouse 	/* MPLS */
32395c96174SEric Dumazet 	unsigned int nr_labels;	/* Depth of stack, 0 = no MPLS */
324ca6549afSSteven Whitehouse 	__be32 labels[MAX_MPLS_LABELS];
325ca6549afSSteven Whitehouse 
32634954ddcSFrancesco Fondelli 	/* VLAN/SVLAN (802.1Q/Q-in-Q) */
32734954ddcSFrancesco Fondelli 	__u8  vlan_p;
32834954ddcSFrancesco Fondelli 	__u8  vlan_cfi;
32934954ddcSFrancesco Fondelli 	__u16 vlan_id;  /* 0xffff means no vlan tag */
33034954ddcSFrancesco Fondelli 
33134954ddcSFrancesco Fondelli 	__u8  svlan_p;
33234954ddcSFrancesco Fondelli 	__u8  svlan_cfi;
33334954ddcSFrancesco Fondelli 	__u16 svlan_id; /* 0xffff means no svlan tag */
33434954ddcSFrancesco Fondelli 
3351da177e4SLinus Torvalds 	__u32 src_mac_count;	/* How many MACs to iterate through */
3361da177e4SLinus Torvalds 	__u32 dst_mac_count;	/* How many MACs to iterate through */
3371da177e4SLinus Torvalds 
338f404e9a6SKris Katterjohn 	unsigned char dst_mac[ETH_ALEN];
339f404e9a6SKris Katterjohn 	unsigned char src_mac[ETH_ALEN];
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	__u32 cur_dst_mac_offset;
3421da177e4SLinus Torvalds 	__u32 cur_src_mac_offset;
343252e3346SAl Viro 	__be32 cur_saddr;
344252e3346SAl Viro 	__be32 cur_daddr;
34566ed1e5eSEric Dumazet 	__u16 ip_id;
3461da177e4SLinus Torvalds 	__u16 cur_udp_dst;
3471da177e4SLinus Torvalds 	__u16 cur_udp_src;
34845b270f8SRobert Olsson 	__u16 cur_queue_map;
3491da177e4SLinus Torvalds 	__u32 cur_pkt_size;
350baac8564SEric Dumazet 	__u32 last_pkt_size;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	__u8 hh[14];
3531da177e4SLinus Torvalds 	/* = {
3541da177e4SLinus Torvalds 	   0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	   We fill in SRC address later
3571da177e4SLinus Torvalds 	   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3581da177e4SLinus Torvalds 	   0x08, 0x00
3591da177e4SLinus Torvalds 	   };
3601da177e4SLinus Torvalds 	 */
3611da177e4SLinus Torvalds 	__u16 pad;		/* pad out the hh struct to an even 16 bytes */
3621da177e4SLinus Torvalds 
36363adc6fbSStephen Hemminger 	struct sk_buff *skb;	/* skb we are to transmit next, used for when we
3641da177e4SLinus Torvalds 				 * are transmitting the same one multiple times
3651da177e4SLinus Torvalds 				 */
36663adc6fbSStephen Hemminger 	struct net_device *odev; /* The out-going device.
36763adc6fbSStephen Hemminger 				  * Note that the device should have it's
36863adc6fbSStephen Hemminger 				  * pg_info pointer pointing back to this
36963adc6fbSStephen Hemminger 				  * device.
37063adc6fbSStephen Hemminger 				  * Set when the user specifies the out-going
37163adc6fbSStephen Hemminger 				  * device name (not when the inject is
3721da177e4SLinus Torvalds 				  * started as it used to do.)
3731da177e4SLinus Torvalds 				  */
374593f63b0SEric Dumazet 	char odevname[32];
3751da177e4SLinus Torvalds 	struct flow_state *flows;
37695c96174SEric Dumazet 	unsigned int cflows;	/* Concurrent flows (config) */
37795c96174SEric Dumazet 	unsigned int lflow;		/* Flow length  (config) */
37895c96174SEric Dumazet 	unsigned int nflows;	/* accumulated flows (stats) */
37995c96174SEric Dumazet 	unsigned int curfl;		/* current sequenced flow (state)*/
38045b270f8SRobert Olsson 
38145b270f8SRobert Olsson 	u16 queue_map_min;
38245b270f8SRobert Olsson 	u16 queue_map_max;
3839e50e3acSJohn Fastabend 	__u32 skb_priority;	/* skb priority field */
384e99b99b4SRobert Olsson 	int node;               /* Memory node */
38545b270f8SRobert Olsson 
386a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
387a553e4a6SJamal Hadi Salim 	__u8	ipsmode;		/* IPSEC mode (config) */
388a553e4a6SJamal Hadi Salim 	__u8	ipsproto;		/* IPSEC type (config) */
389a553e4a6SJamal Hadi Salim #endif
39039df232fSStephen Hemminger 	char result[512];
3911da177e4SLinus Torvalds };
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds struct pktgen_hdr {
394252e3346SAl Viro 	__be32 pgh_magic;
395252e3346SAl Viro 	__be32 seq_num;
396252e3346SAl Viro 	__be32 tv_sec;
397252e3346SAl Viro 	__be32 tv_usec;
3981da177e4SLinus Torvalds };
3991da177e4SLinus Torvalds 
400*4e58a027SCong Wang 
401*4e58a027SCong Wang static int pg_net_id __read_mostly;
402*4e58a027SCong Wang 
403*4e58a027SCong Wang struct pktgen_net {
404*4e58a027SCong Wang 	struct net		*net;
405*4e58a027SCong Wang 	struct proc_dir_entry	*proc_dir;
406*4e58a027SCong Wang 	struct list_head	pktgen_threads;
407*4e58a027SCong Wang 	bool			pktgen_exiting;
408*4e58a027SCong Wang };
409551eaff1SEric Dumazet 
4101da177e4SLinus Torvalds struct pktgen_thread {
41163adc6fbSStephen Hemminger 	spinlock_t if_lock;		/* for list of devices */
412c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
413cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
414ee74baa7SDavid S. Miller 	struct task_struct *tsk;
4151da177e4SLinus Torvalds 	char result[512];
4161da177e4SLinus Torvalds 
41763adc6fbSStephen Hemminger 	/* Field for thread to receive "posted" events terminate,
41863adc6fbSStephen Hemminger 	   stop ifs etc. */
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	u32 control;
4211da177e4SLinus Torvalds 	int cpu;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	wait_queue_head_t queue;
424d3ede327SDenis V. Lunev 	struct completion start_done;
425*4e58a027SCong Wang 	struct pktgen_net *net;
4261da177e4SLinus Torvalds };
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds #define REMOVE 1
4291da177e4SLinus Torvalds #define FIND   0
4301da177e4SLinus Torvalds 
431c3d2f52dSStephen Hemminger static const char version[] =
432f9467eaeSJoe Perches 	"Packet Generator for packet performance testing. "
433f9467eaeSJoe Perches 	"Version: " VERSION "\n";
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4361da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
437222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
4383e984840SEric Dumazet 					  const char *ifname, bool exact);
4391da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
440*4e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn);
441*4e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn);
442*4e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
4433bda06a3SStephen Hemminger 
4441da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4451da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
44639df232fSStephen Hemminger 
4471da177e4SLinus Torvalds /* Module parameters, defaults. */
44865c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
44965c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
45065c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
45165c5b786SStephen Hemminger static int debug  __read_mostly;
4521da177e4SLinus Torvalds 
453222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4561da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4571da177e4SLinus Torvalds };
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /*
4601da177e4SLinus Torvalds  * /proc handling functions
4611da177e4SLinus Torvalds  *
4621da177e4SLinus Torvalds  */
4631da177e4SLinus Torvalds 
464d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
465d50a6b56SStephen Hemminger {
466c3d2f52dSStephen Hemminger 	seq_puts(seq, version);
467d50a6b56SStephen Hemminger 	return 0;
468d50a6b56SStephen Hemminger }
4691da177e4SLinus Torvalds 
470d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user *buf,
4711da177e4SLinus Torvalds 			    size_t count, loff_t *ppos)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	int err = 0;
474d50a6b56SStephen Hemminger 	char data[128];
475*4e58a027SCong Wang 	struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN)) {
4781da177e4SLinus Torvalds 		err = -EPERM;
4791da177e4SLinus Torvalds 		goto out;
4801da177e4SLinus Torvalds 	}
4811da177e4SLinus Torvalds 
482d50a6b56SStephen Hemminger 	if (count > sizeof(data))
483d50a6b56SStephen Hemminger 		count = sizeof(data);
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count)) {
4861da177e4SLinus Torvalds 		err = -EFAULT;
487d50a6b56SStephen Hemminger 		goto out;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 	data[count - 1] = 0;	/* Make string */
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
492*4e58a027SCong Wang 		pktgen_stop_all_threads_ifs(pn);
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
495*4e58a027SCong Wang 		pktgen_run_all_threads(pn);
4961da177e4SLinus Torvalds 
497eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
498*4e58a027SCong Wang 		pktgen_reset_all_threads(pn);
499eb37b41cSJesse Brandeburg 
5001da177e4SLinus Torvalds 	else
501f9467eaeSJoe Perches 		pr_warning("Unknown command: %s\n", data);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	err = count;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds out:
5061da177e4SLinus Torvalds 	return err;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
509d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
5101da177e4SLinus Torvalds {
511d50a6b56SStephen Hemminger 	return single_open(file, pgctrl_show, PDE(inode)->data);
512d50a6b56SStephen Hemminger }
513d50a6b56SStephen Hemminger 
5149a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
515d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
516d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
517d50a6b56SStephen Hemminger 	.read    = seq_read,
518d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
519d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
520d50a6b56SStephen Hemminger 	.release = single_release,
521d50a6b56SStephen Hemminger };
522d50a6b56SStephen Hemminger 
523d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
524d50a6b56SStephen Hemminger {
525648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
526fd29cf72SStephen Hemminger 	ktime_t stopped;
527fd29cf72SStephen Hemminger 	u64 idle;
5281da177e4SLinus Torvalds 
529222f1806SLuiz Capitulino 	seq_printf(seq,
530222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
531222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
532222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5331da177e4SLinus Torvalds 
534222f1806SLuiz Capitulino 	seq_printf(seq,
535fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
536fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
537593f63b0SEric Dumazet 		   pkt_dev->clone_skb, pkt_dev->odevname);
5381da177e4SLinus Torvalds 
539222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
540222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5411da177e4SLinus Torvalds 
54245b270f8SRobert Olsson 	seq_printf(seq,
54345b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
54445b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
54545b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
54645b270f8SRobert Olsson 
5479e50e3acSJohn Fastabend 	if (pkt_dev->skb_priority)
5489e50e3acSJohn Fastabend 		seq_printf(seq, "     skb_priority: %u\n",
5499e50e3acSJohn Fastabend 			   pkt_dev->skb_priority);
5509e50e3acSJohn Fastabend 
5511da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
552222f1806SLuiz Capitulino 		seq_printf(seq,
55347a0200dSAlexey Dobriyan 			   "     saddr: %pI6c  min_saddr: %pI6c  max_saddr: %pI6c\n"
55447a0200dSAlexey Dobriyan 			   "     daddr: %pI6c  min_daddr: %pI6c  max_daddr: %pI6c\n",
55547a0200dSAlexey Dobriyan 			   &pkt_dev->in6_saddr,
55647a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
55747a0200dSAlexey Dobriyan 			   &pkt_dev->in6_daddr,
55847a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
55963adc6fbSStephen Hemminger 	} else {
560222f1806SLuiz Capitulino 		seq_printf(seq,
56163adc6fbSStephen Hemminger 			   "     dst_min: %s  dst_max: %s\n",
56263adc6fbSStephen Hemminger 			   pkt_dev->dst_min, pkt_dev->dst_max);
56363adc6fbSStephen Hemminger 		seq_printf(seq,
56463adc6fbSStephen Hemminger 			   "        src_min: %s  src_max: %s\n",
56563adc6fbSStephen Hemminger 			   pkt_dev->src_min, pkt_dev->src_max);
56663adc6fbSStephen Hemminger 	}
5671da177e4SLinus Torvalds 
568d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5691da177e4SLinus Torvalds 
570e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
571e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
572e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5731da177e4SLinus Torvalds 
574d50a6b56SStephen Hemminger 	seq_printf(seq, "dst_mac: ");
575e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
5761da177e4SLinus Torvalds 
577222f1806SLuiz Capitulino 	seq_printf(seq,
57863adc6fbSStephen Hemminger 		   "     udp_src_min: %d  udp_src_max: %d"
57963adc6fbSStephen Hemminger 		   "  udp_dst_min: %d  udp_dst_max: %d\n",
580222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
581222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
5821da177e4SLinus Torvalds 
583222f1806SLuiz Capitulino 	seq_printf(seq,
584ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
5851da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
5861da177e4SLinus Torvalds 
587ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
58895c96174SEric Dumazet 		unsigned int i;
589ca6549afSSteven Whitehouse 		seq_printf(seq, "     mpls: ");
590ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
591ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
592ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
593ca6549afSSteven Whitehouse 	}
594ca6549afSSteven Whitehouse 
59563adc6fbSStephen Hemminger 	if (pkt_dev->vlan_id != 0xffff)
59634954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
59763adc6fbSStephen Hemminger 			   pkt_dev->vlan_id, pkt_dev->vlan_p,
59863adc6fbSStephen Hemminger 			   pkt_dev->vlan_cfi);
59934954ddcSFrancesco Fondelli 
60063adc6fbSStephen Hemminger 	if (pkt_dev->svlan_id != 0xffff)
60134954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
60263adc6fbSStephen Hemminger 			   pkt_dev->svlan_id, pkt_dev->svlan_p,
60363adc6fbSStephen Hemminger 			   pkt_dev->svlan_cfi);
60434954ddcSFrancesco Fondelli 
60563adc6fbSStephen Hemminger 	if (pkt_dev->tos)
6061ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
6071ca7768cSFrancesco Fondelli 
60863adc6fbSStephen Hemminger 	if (pkt_dev->traffic_class)
6091ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
6101ca7768cSFrancesco Fondelli 
611e99b99b4SRobert Olsson 	if (pkt_dev->node >= 0)
612e99b99b4SRobert Olsson 		seq_printf(seq, "     node: %d\n", pkt_dev->node);
613e99b99b4SRobert Olsson 
614ca6549afSSteven Whitehouse 	seq_printf(seq, "     Flags: ");
615ca6549afSSteven Whitehouse 
6161da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
617d50a6b56SStephen Hemminger 		seq_printf(seq, "IPV6  ");
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPSRC_RND)
620d50a6b56SStephen Hemminger 		seq_printf(seq, "IPSRC_RND  ");
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPDST_RND)
623d50a6b56SStephen Hemminger 		seq_printf(seq, "IPDST_RND  ");
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (pkt_dev->flags & F_TXSIZE_RND)
626d50a6b56SStephen Hemminger 		seq_printf(seq, "TXSIZE_RND  ");
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPSRC_RND)
629d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPSRC_RND  ");
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPDST_RND)
632d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPDST_RND  ");
6331da177e4SLinus Torvalds 
634ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND)
635ca6549afSSteven Whitehouse 		seq_printf(seq,  "MPLS_RND  ");
636ca6549afSSteven Whitehouse 
63745b270f8SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_RND)
63845b270f8SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_RND  ");
63945b270f8SRobert Olsson 
640e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
641e6fce5b9SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_CPU  ");
642e6fce5b9SRobert Olsson 
643007a531bSJamal Hadi Salim 	if (pkt_dev->cflows) {
644007a531bSJamal Hadi Salim 		if (pkt_dev->flags & F_FLOW_SEQ)
645007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_SEQ  "); /*in sequence flows*/
646007a531bSJamal Hadi Salim 		else
647007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_RND  ");
648007a531bSJamal Hadi Salim 	}
649007a531bSJamal Hadi Salim 
650a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
651a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON)
652a553e4a6SJamal Hadi Salim 		seq_printf(seq,  "IPSEC  ");
653a553e4a6SJamal Hadi Salim #endif
654a553e4a6SJamal Hadi Salim 
6551da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACSRC_RND)
656d50a6b56SStephen Hemminger 		seq_printf(seq, "MACSRC_RND  ");
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACDST_RND)
659d50a6b56SStephen Hemminger 		seq_printf(seq, "MACDST_RND  ");
6601da177e4SLinus Torvalds 
66134954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_VID_RND)
66234954ddcSFrancesco Fondelli 		seq_printf(seq, "VID_RND  ");
66334954ddcSFrancesco Fondelli 
66434954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_SVID_RND)
66534954ddcSFrancesco Fondelli 		seq_printf(seq, "SVID_RND  ");
66634954ddcSFrancesco Fondelli 
667e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE)
668e99b99b4SRobert Olsson 		seq_printf(seq, "NODE_ALLOC  ");
669e99b99b4SRobert Olsson 
670d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6711da177e4SLinus Torvalds 
672fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
673398f382cSDaniel Borkmann 	stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
674fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
675fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
6761da177e4SLinus Torvalds 
677222f1806SLuiz Capitulino 	seq_printf(seq,
678fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
6791da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
680fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
681fd29cf72SStephen Hemminger 
682fd29cf72SStephen Hemminger 	seq_printf(seq,
683fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
684fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
685fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
686fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
6871da177e4SLinus Torvalds 
688222f1806SLuiz Capitulino 	seq_printf(seq,
689222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
690d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
691d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
69447a0200dSAlexey Dobriyan 		seq_printf(seq, "     cur_saddr: %pI6c  cur_daddr: %pI6c\n",
69547a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_saddr,
69647a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_daddr);
697222f1806SLuiz Capitulino 	} else
6980373a946SAmerigo Wang 		seq_printf(seq, "     cur_saddr: %pI4  cur_daddr: %pI4\n",
6990373a946SAmerigo Wang 			   &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
7001da177e4SLinus Torvalds 
701d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
7021da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
7031da177e4SLinus Torvalds 
70445b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
70545b270f8SRobert Olsson 
706d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	if (pkt_dev->result[0])
709d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
7101da177e4SLinus Torvalds 	else
711d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: Idle\n");
7121da177e4SLinus Torvalds 
713d50a6b56SStephen Hemminger 	return 0;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
716ca6549afSSteven Whitehouse 
71763adc6fbSStephen Hemminger static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
71863adc6fbSStephen Hemminger 		     __u32 *num)
719ca6549afSSteven Whitehouse {
720ca6549afSSteven Whitehouse 	int i = 0;
721ca6549afSSteven Whitehouse 	*num = 0;
722ca6549afSSteven Whitehouse 
7231ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
72482fd5b5dSAndy Shevchenko 		int value;
725ca6549afSSteven Whitehouse 		char c;
726ca6549afSSteven Whitehouse 		*num <<= 4;
727ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
728ca6549afSSteven Whitehouse 			return -EFAULT;
72982fd5b5dSAndy Shevchenko 		value = hex_to_bin(c);
73082fd5b5dSAndy Shevchenko 		if (value >= 0)
73182fd5b5dSAndy Shevchenko 			*num |= value;
732ca6549afSSteven Whitehouse 		else
733ca6549afSSteven Whitehouse 			break;
734ca6549afSSteven Whitehouse 	}
735ca6549afSSteven Whitehouse 	return i;
736ca6549afSSteven Whitehouse }
737ca6549afSSteven Whitehouse 
738222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
739222f1806SLuiz Capitulino 			     unsigned int maxlen)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds 	int i;
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7441da177e4SLinus Torvalds 		char c;
7451da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7461da177e4SLinus Torvalds 			return -EFAULT;
7471da177e4SLinus Torvalds 		switch (c) {
7481da177e4SLinus Torvalds 		case '\"':
7491da177e4SLinus Torvalds 		case '\n':
7501da177e4SLinus Torvalds 		case '\r':
7511da177e4SLinus Torvalds 		case '\t':
7521da177e4SLinus Torvalds 		case ' ':
7531da177e4SLinus Torvalds 		case '=':
7541da177e4SLinus Torvalds 			break;
7551da177e4SLinus Torvalds 		default:
7561da177e4SLinus Torvalds 			goto done;
7573ff50b79SStephen Hemminger 		}
7581da177e4SLinus Torvalds 	}
7591da177e4SLinus Torvalds done:
7601da177e4SLinus Torvalds 	return i;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
763bf0813bdSPaul Gortmaker static long num_arg(const char __user *user_buffer, unsigned long maxlen,
764bf0813bdSPaul Gortmaker 				unsigned long *num)
7651da177e4SLinus Torvalds {
766d6182223SPaul Gortmaker 	int i;
7671da177e4SLinus Torvalds 	*num = 0;
7681da177e4SLinus Torvalds 
769d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7701da177e4SLinus Torvalds 		char c;
7711da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7721da177e4SLinus Torvalds 			return -EFAULT;
7731da177e4SLinus Torvalds 		if ((c >= '0') && (c <= '9')) {
7741da177e4SLinus Torvalds 			*num *= 10;
7751da177e4SLinus Torvalds 			*num += c - '0';
7761da177e4SLinus Torvalds 		} else
7771da177e4SLinus Torvalds 			break;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 	return i;
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
7831da177e4SLinus Torvalds {
784d6182223SPaul Gortmaker 	int i;
7851da177e4SLinus Torvalds 
786d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7871da177e4SLinus Torvalds 		char c;
7881da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7891da177e4SLinus Torvalds 			return -EFAULT;
7901da177e4SLinus Torvalds 		switch (c) {
7911da177e4SLinus Torvalds 		case '\"':
7921da177e4SLinus Torvalds 		case '\n':
7931da177e4SLinus Torvalds 		case '\r':
7941da177e4SLinus Torvalds 		case '\t':
7951da177e4SLinus Torvalds 		case ' ':
7961da177e4SLinus Torvalds 			goto done_str;
7971da177e4SLinus Torvalds 			break;
7981da177e4SLinus Torvalds 		default:
7991da177e4SLinus Torvalds 			break;
8003ff50b79SStephen Hemminger 		}
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds done_str:
8031da177e4SLinus Torvalds 	return i;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
806ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
807ca6549afSSteven Whitehouse {
80895c96174SEric Dumazet 	unsigned int n = 0;
809ca6549afSSteven Whitehouse 	char c;
810ca6549afSSteven Whitehouse 	ssize_t i = 0;
811ca6549afSSteven Whitehouse 	int len;
812ca6549afSSteven Whitehouse 
813ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = 0;
814ca6549afSSteven Whitehouse 	do {
815ca6549afSSteven Whitehouse 		__u32 tmp;
8161ca7768cSFrancesco Fondelli 		len = hex32_arg(&buffer[i], 8, &tmp);
817ca6549afSSteven Whitehouse 		if (len <= 0)
818ca6549afSSteven Whitehouse 			return len;
819ca6549afSSteven Whitehouse 		pkt_dev->labels[n] = htonl(tmp);
820ca6549afSSteven Whitehouse 		if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
821ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
822ca6549afSSteven Whitehouse 		i += len;
823ca6549afSSteven Whitehouse 		if (get_user(c, &buffer[i]))
824ca6549afSSteven Whitehouse 			return -EFAULT;
825ca6549afSSteven Whitehouse 		i++;
826ca6549afSSteven Whitehouse 		n++;
827ca6549afSSteven Whitehouse 		if (n >= MAX_MPLS_LABELS)
828ca6549afSSteven Whitehouse 			return -E2BIG;
829ca6549afSSteven Whitehouse 	} while (c == ',');
830ca6549afSSteven Whitehouse 
831ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = n;
832ca6549afSSteven Whitehouse 	return i;
833ca6549afSSteven Whitehouse }
834ca6549afSSteven Whitehouse 
835222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file,
836222f1806SLuiz Capitulino 			       const char __user * user_buffer, size_t count,
837222f1806SLuiz Capitulino 			       loff_t * offset)
8381da177e4SLinus Torvalds {
8398a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
840d50a6b56SStephen Hemminger 	struct pktgen_dev *pkt_dev = seq->private;
841d6182223SPaul Gortmaker 	int i, max, len;
8421da177e4SLinus Torvalds 	char name[16], valstr[32];
8431da177e4SLinus Torvalds 	unsigned long value = 0;
8441da177e4SLinus Torvalds 	char *pg_result = NULL;
8451da177e4SLinus Torvalds 	int tmp = 0;
8461da177e4SLinus Torvalds 	char buf[128];
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	pg_result = &(pkt_dev->result[0]);
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	if (count < 1) {
851f9467eaeSJoe Perches 		pr_warning("wrong command format\n");
8521da177e4SLinus Torvalds 		return -EINVAL;
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
855d6182223SPaul Gortmaker 	max = count;
856d6182223SPaul Gortmaker 	tmp = count_trail_chars(user_buffer, max);
8571da177e4SLinus Torvalds 	if (tmp < 0) {
858f9467eaeSJoe Perches 		pr_warning("illegal format\n");
8591da177e4SLinus Torvalds 		return tmp;
8601da177e4SLinus Torvalds 	}
861d6182223SPaul Gortmaker 	i = tmp;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	/* Read variable name */
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
86663adc6fbSStephen Hemminger 	if (len < 0)
867222f1806SLuiz Capitulino 		return len;
86863adc6fbSStephen Hemminger 
8691da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
8701da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
8711da177e4SLinus Torvalds 		return -EFAULT;
8721da177e4SLinus Torvalds 	i += len;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	max = count - i;
8751da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
8761da177e4SLinus Torvalds 	if (len < 0)
8771da177e4SLinus Torvalds 		return len;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	i += len;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	if (debug) {
88286c2c0a8SDmitry Torokhov 		size_t copy = min_t(size_t, count, 1023);
883448d7b5dSNelson Elhage 		char tb[copy + 1];
884448d7b5dSNelson Elhage 		if (copy_from_user(tb, user_buffer, copy))
8851da177e4SLinus Torvalds 			return -EFAULT;
886448d7b5dSNelson Elhage 		tb[copy] = 0;
887f342cda7SJoe Perches 		pr_debug("%s,%lu  buffer -:%s:-\n",
888f342cda7SJoe Perches 			 name, (unsigned long)count, tb);
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	if (!strcmp(name, "min_pkt_size")) {
8921da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
89363adc6fbSStephen Hemminger 		if (len < 0)
894222f1806SLuiz Capitulino 			return len;
89563adc6fbSStephen Hemminger 
8961da177e4SLinus Torvalds 		i += len;
8971da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
8981da177e4SLinus Torvalds 			value = 14 + 20 + 8;
8991da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9001da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9011da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9021da177e4SLinus Torvalds 		}
903222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: min_pkt_size=%u",
904222f1806SLuiz Capitulino 			pkt_dev->min_pkt_size);
9051da177e4SLinus Torvalds 		return count;
9061da177e4SLinus Torvalds 	}
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	if (!strcmp(name, "max_pkt_size")) {
9091da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
91063adc6fbSStephen Hemminger 		if (len < 0)
911222f1806SLuiz Capitulino 			return len;
91263adc6fbSStephen Hemminger 
9131da177e4SLinus Torvalds 		i += len;
9141da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9151da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9161da177e4SLinus Torvalds 		if (value != pkt_dev->max_pkt_size) {
9171da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9181da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9191da177e4SLinus Torvalds 		}
920222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: max_pkt_size=%u",
921222f1806SLuiz Capitulino 			pkt_dev->max_pkt_size);
9221da177e4SLinus Torvalds 		return count;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	/* Shortcut for min = max */
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	if (!strcmp(name, "pkt_size")) {
9281da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
92963adc6fbSStephen Hemminger 		if (len < 0)
930222f1806SLuiz Capitulino 			return len;
93163adc6fbSStephen Hemminger 
9321da177e4SLinus Torvalds 		i += len;
9331da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9341da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9351da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9361da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9371da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9381da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9391da177e4SLinus Torvalds 		}
9401da177e4SLinus Torvalds 		sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
9411da177e4SLinus Torvalds 		return count;
9421da177e4SLinus Torvalds 	}
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	if (!strcmp(name, "debug")) {
9451da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
94663adc6fbSStephen Hemminger 		if (len < 0)
947222f1806SLuiz Capitulino 			return len;
94863adc6fbSStephen Hemminger 
9491da177e4SLinus Torvalds 		i += len;
9501da177e4SLinus Torvalds 		debug = value;
9511da177e4SLinus Torvalds 		sprintf(pg_result, "OK: debug=%u", debug);
9521da177e4SLinus Torvalds 		return count;
9531da177e4SLinus Torvalds 	}
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	if (!strcmp(name, "frags")) {
9561da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
95763adc6fbSStephen Hemminger 		if (len < 0)
958222f1806SLuiz Capitulino 			return len;
95963adc6fbSStephen Hemminger 
9601da177e4SLinus Torvalds 		i += len;
9611da177e4SLinus Torvalds 		pkt_dev->nfrags = value;
9621da177e4SLinus Torvalds 		sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
9631da177e4SLinus Torvalds 		return count;
9641da177e4SLinus Torvalds 	}
9651da177e4SLinus Torvalds 	if (!strcmp(name, "delay")) {
9661da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
96763adc6fbSStephen Hemminger 		if (len < 0)
968222f1806SLuiz Capitulino 			return len;
96963adc6fbSStephen Hemminger 
9701da177e4SLinus Torvalds 		i += len;
971fd29cf72SStephen Hemminger 		if (value == 0x7FFFFFFF)
972fd29cf72SStephen Hemminger 			pkt_dev->delay = ULLONG_MAX;
973fd29cf72SStephen Hemminger 		else
9749240d715SEric Dumazet 			pkt_dev->delay = (u64)value;
975fd29cf72SStephen Hemminger 
976fd29cf72SStephen Hemminger 		sprintf(pg_result, "OK: delay=%llu",
977fd29cf72SStephen Hemminger 			(unsigned long long) pkt_dev->delay);
9781da177e4SLinus Torvalds 		return count;
9791da177e4SLinus Torvalds 	}
98043d28b65SDaniel Turull 	if (!strcmp(name, "rate")) {
98143d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
98243d28b65SDaniel Turull 		if (len < 0)
98343d28b65SDaniel Turull 			return len;
98443d28b65SDaniel Turull 
98543d28b65SDaniel Turull 		i += len;
98643d28b65SDaniel Turull 		if (!value)
98743d28b65SDaniel Turull 			return len;
98843d28b65SDaniel Turull 		pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
98943d28b65SDaniel Turull 		if (debug)
990f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
99143d28b65SDaniel Turull 
99243d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
99343d28b65SDaniel Turull 		return count;
99443d28b65SDaniel Turull 	}
99543d28b65SDaniel Turull 	if (!strcmp(name, "ratep")) {
99643d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
99743d28b65SDaniel Turull 		if (len < 0)
99843d28b65SDaniel Turull 			return len;
99943d28b65SDaniel Turull 
100043d28b65SDaniel Turull 		i += len;
100143d28b65SDaniel Turull 		if (!value)
100243d28b65SDaniel Turull 			return len;
100343d28b65SDaniel Turull 		pkt_dev->delay = NSEC_PER_SEC/value;
100443d28b65SDaniel Turull 		if (debug)
1005f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
100643d28b65SDaniel Turull 
100743d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
100843d28b65SDaniel Turull 		return count;
100943d28b65SDaniel Turull 	}
10101da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_min")) {
10111da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
101263adc6fbSStephen Hemminger 		if (len < 0)
1013222f1806SLuiz Capitulino 			return len;
101463adc6fbSStephen Hemminger 
10151da177e4SLinus Torvalds 		i += len;
10161da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_min) {
10171da177e4SLinus Torvalds 			pkt_dev->udp_src_min = value;
10181da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10191da177e4SLinus Torvalds 		}
10201da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
10211da177e4SLinus Torvalds 		return count;
10221da177e4SLinus Torvalds 	}
10231da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_min")) {
10241da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
102563adc6fbSStephen Hemminger 		if (len < 0)
1026222f1806SLuiz Capitulino 			return len;
102763adc6fbSStephen Hemminger 
10281da177e4SLinus Torvalds 		i += len;
10291da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_min) {
10301da177e4SLinus Torvalds 			pkt_dev->udp_dst_min = value;
10311da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10321da177e4SLinus Torvalds 		}
10331da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
10341da177e4SLinus Torvalds 		return count;
10351da177e4SLinus Torvalds 	}
10361da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_max")) {
10371da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
103863adc6fbSStephen Hemminger 		if (len < 0)
1039222f1806SLuiz Capitulino 			return len;
104063adc6fbSStephen Hemminger 
10411da177e4SLinus Torvalds 		i += len;
10421da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_max) {
10431da177e4SLinus Torvalds 			pkt_dev->udp_src_max = value;
10441da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10451da177e4SLinus Torvalds 		}
10461da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
10471da177e4SLinus Torvalds 		return count;
10481da177e4SLinus Torvalds 	}
10491da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_max")) {
10501da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
105163adc6fbSStephen Hemminger 		if (len < 0)
1052222f1806SLuiz Capitulino 			return len;
105363adc6fbSStephen Hemminger 
10541da177e4SLinus Torvalds 		i += len;
10551da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_max) {
10561da177e4SLinus Torvalds 			pkt_dev->udp_dst_max = value;
10571da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10581da177e4SLinus Torvalds 		}
10591da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
10601da177e4SLinus Torvalds 		return count;
10611da177e4SLinus Torvalds 	}
10621da177e4SLinus Torvalds 	if (!strcmp(name, "clone_skb")) {
10631da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
106463adc6fbSStephen Hemminger 		if (len < 0)
1065222f1806SLuiz Capitulino 			return len;
1066d8873315SNeil Horman 		if ((value > 0) &&
1067d8873315SNeil Horman 		    (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1068d8873315SNeil Horman 			return -ENOTSUPP;
10691da177e4SLinus Torvalds 		i += len;
10701da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
10731da177e4SLinus Torvalds 		return count;
10741da177e4SLinus Torvalds 	}
10751da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
10761da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
107763adc6fbSStephen Hemminger 		if (len < 0)
1078222f1806SLuiz Capitulino 			return len;
107963adc6fbSStephen Hemminger 
10801da177e4SLinus Torvalds 		i += len;
10811da177e4SLinus Torvalds 		pkt_dev->count = value;
10821da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
10831da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
10841da177e4SLinus Torvalds 		return count;
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
10871da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
108863adc6fbSStephen Hemminger 		if (len < 0)
1089222f1806SLuiz Capitulino 			return len;
109063adc6fbSStephen Hemminger 
10911da177e4SLinus Torvalds 		i += len;
10921da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
10931da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
10941da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
10951da177e4SLinus Torvalds 		}
1096222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1097222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
10981da177e4SLinus Torvalds 		return count;
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
11011da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
110263adc6fbSStephen Hemminger 		if (len < 0)
1103222f1806SLuiz Capitulino 			return len;
110463adc6fbSStephen Hemminger 
11051da177e4SLinus Torvalds 		i += len;
11061da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
11071da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
11081da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
11091da177e4SLinus Torvalds 		}
1110222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1111222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
11121da177e4SLinus Torvalds 		return count;
11131da177e4SLinus Torvalds 	}
1114e99b99b4SRobert Olsson 	if (!strcmp(name, "node")) {
1115e99b99b4SRobert Olsson 		len = num_arg(&user_buffer[i], 10, &value);
1116e99b99b4SRobert Olsson 		if (len < 0)
1117e99b99b4SRobert Olsson 			return len;
1118e99b99b4SRobert Olsson 
1119e99b99b4SRobert Olsson 		i += len;
1120e99b99b4SRobert Olsson 
1121e99b99b4SRobert Olsson 		if (node_possible(value)) {
1122e99b99b4SRobert Olsson 			pkt_dev->node = value;
1123e99b99b4SRobert Olsson 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
112426ad7879SEric Dumazet 			if (pkt_dev->page) {
112526ad7879SEric Dumazet 				put_page(pkt_dev->page);
112626ad7879SEric Dumazet 				pkt_dev->page = NULL;
112726ad7879SEric Dumazet 			}
1128e99b99b4SRobert Olsson 		}
1129e99b99b4SRobert Olsson 		else
1130e99b99b4SRobert Olsson 			sprintf(pg_result, "ERROR: node not possible");
1131e99b99b4SRobert Olsson 		return count;
1132e99b99b4SRobert Olsson 	}
11331da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
11341da177e4SLinus Torvalds 		char f[32];
11351da177e4SLinus Torvalds 		memset(f, 0, 32);
11361da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
113763adc6fbSStephen Hemminger 		if (len < 0)
1138222f1806SLuiz Capitulino 			return len;
113963adc6fbSStephen Hemminger 
11401da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
11411da177e4SLinus Torvalds 			return -EFAULT;
11421da177e4SLinus Torvalds 		i += len;
11431da177e4SLinus Torvalds 		if (strcmp(f, "IPSRC_RND") == 0)
11441da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPSRC_RND;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		else if (strcmp(f, "!IPSRC_RND") == 0)
11471da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPSRC_RND;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 		else if (strcmp(f, "TXSIZE_RND") == 0)
11501da177e4SLinus Torvalds 			pkt_dev->flags |= F_TXSIZE_RND;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 		else if (strcmp(f, "!TXSIZE_RND") == 0)
11531da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_TXSIZE_RND;
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 		else if (strcmp(f, "IPDST_RND") == 0)
11561da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPDST_RND;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 		else if (strcmp(f, "!IPDST_RND") == 0)
11591da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPDST_RND;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 		else if (strcmp(f, "UDPSRC_RND") == 0)
11621da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPSRC_RND;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPSRC_RND") == 0)
11651da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPSRC_RND;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 		else if (strcmp(f, "UDPDST_RND") == 0)
11681da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPDST_RND;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPDST_RND") == 0)
11711da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPDST_RND;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 		else if (strcmp(f, "MACSRC_RND") == 0)
11741da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACSRC_RND;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 		else if (strcmp(f, "!MACSRC_RND") == 0)
11771da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACSRC_RND;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 		else if (strcmp(f, "MACDST_RND") == 0)
11801da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACDST_RND;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 		else if (strcmp(f, "!MACDST_RND") == 0)
11831da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACDST_RND;
11841da177e4SLinus Torvalds 
1185ca6549afSSteven Whitehouse 		else if (strcmp(f, "MPLS_RND") == 0)
1186ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
1187ca6549afSSteven Whitehouse 
1188ca6549afSSteven Whitehouse 		else if (strcmp(f, "!MPLS_RND") == 0)
1189ca6549afSSteven Whitehouse 			pkt_dev->flags &= ~F_MPLS_RND;
1190ca6549afSSteven Whitehouse 
119134954ddcSFrancesco Fondelli 		else if (strcmp(f, "VID_RND") == 0)
119234954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_VID_RND;
119334954ddcSFrancesco Fondelli 
119434954ddcSFrancesco Fondelli 		else if (strcmp(f, "!VID_RND") == 0)
119534954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_VID_RND;
119634954ddcSFrancesco Fondelli 
119734954ddcSFrancesco Fondelli 		else if (strcmp(f, "SVID_RND") == 0)
119834954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_SVID_RND;
119934954ddcSFrancesco Fondelli 
120034954ddcSFrancesco Fondelli 		else if (strcmp(f, "!SVID_RND") == 0)
120134954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_SVID_RND;
120234954ddcSFrancesco Fondelli 
1203007a531bSJamal Hadi Salim 		else if (strcmp(f, "FLOW_SEQ") == 0)
1204007a531bSJamal Hadi Salim 			pkt_dev->flags |= F_FLOW_SEQ;
1205007a531bSJamal Hadi Salim 
120645b270f8SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
120745b270f8SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_RND;
120845b270f8SRobert Olsson 
120945b270f8SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
121045b270f8SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1211e6fce5b9SRobert Olsson 
1212e6fce5b9SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1213e6fce5b9SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1214e6fce5b9SRobert Olsson 
1215e6fce5b9SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1216e6fce5b9SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1217a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
1218a553e4a6SJamal Hadi Salim 		else if (strcmp(f, "IPSEC") == 0)
1219a553e4a6SJamal Hadi Salim 			pkt_dev->flags |= F_IPSEC_ON;
1220a553e4a6SJamal Hadi Salim #endif
1221a553e4a6SJamal Hadi Salim 
12221ca7768cSFrancesco Fondelli 		else if (strcmp(f, "!IPV6") == 0)
12231ca7768cSFrancesco Fondelli 			pkt_dev->flags &= ~F_IPV6;
12241ca7768cSFrancesco Fondelli 
1225e99b99b4SRobert Olsson 		else if (strcmp(f, "NODE_ALLOC") == 0)
1226e99b99b4SRobert Olsson 			pkt_dev->flags |= F_NODE;
1227e99b99b4SRobert Olsson 
1228e99b99b4SRobert Olsson 		else if (strcmp(f, "!NODE_ALLOC") == 0)
1229e99b99b4SRobert Olsson 			pkt_dev->flags &= ~F_NODE;
1230e99b99b4SRobert Olsson 
12311da177e4SLinus Torvalds 		else {
1232222f1806SLuiz Capitulino 			sprintf(pg_result,
1233222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
12341da177e4SLinus Torvalds 				f,
12351ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1236e99b99b4SRobert Olsson 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
12371da177e4SLinus Torvalds 			return count;
12381da177e4SLinus Torvalds 		}
12391da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
12401da177e4SLinus Torvalds 		return count;
12411da177e4SLinus Torvalds 	}
12421da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
12431da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
124463adc6fbSStephen Hemminger 		if (len < 0)
1245222f1806SLuiz Capitulino 			return len;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12481da177e4SLinus Torvalds 			return -EFAULT;
12491da177e4SLinus Torvalds 		buf[len] = 0;
12501da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
12511da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
12521da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_min, buf, len);
12531da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
12541da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
12551da177e4SLinus Torvalds 		}
12561da177e4SLinus Torvalds 		if (debug)
1257f342cda7SJoe Perches 			pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
12581da177e4SLinus Torvalds 		i += len;
12591da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
12601da177e4SLinus Torvalds 		return count;
12611da177e4SLinus Torvalds 	}
12621da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
12631da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
126463adc6fbSStephen Hemminger 		if (len < 0)
1265222f1806SLuiz Capitulino 			return len;
126663adc6fbSStephen Hemminger 
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12691da177e4SLinus Torvalds 			return -EFAULT;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 		buf[len] = 0;
12721da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
12731da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
12741da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_max, buf, len);
12751da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
12761da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
12771da177e4SLinus Torvalds 		}
12781da177e4SLinus Torvalds 		if (debug)
1279f342cda7SJoe Perches 			pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
12801da177e4SLinus Torvalds 		i += len;
12811da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
12821da177e4SLinus Torvalds 		return count;
12831da177e4SLinus Torvalds 	}
12841da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
12851da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1286222f1806SLuiz Capitulino 		if (len < 0)
1287222f1806SLuiz Capitulino 			return len;
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12921da177e4SLinus Torvalds 			return -EFAULT;
12931da177e4SLinus Torvalds 		buf[len] = 0;
12941da177e4SLinus Torvalds 
1295c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
129647a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
12971da177e4SLinus Torvalds 
12984e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 		if (debug)
1301f342cda7SJoe Perches 			pr_debug("dst6 set to: %s\n", buf);
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 		i += len;
13041da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
13051da177e4SLinus Torvalds 		return count;
13061da177e4SLinus Torvalds 	}
13071da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
13081da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1309222f1806SLuiz Capitulino 		if (len < 0)
1310222f1806SLuiz Capitulino 			return len;
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13151da177e4SLinus Torvalds 			return -EFAULT;
13161da177e4SLinus Torvalds 		buf[len] = 0;
13171da177e4SLinus Torvalds 
1318c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
131947a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
13201da177e4SLinus Torvalds 
13214e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
13221da177e4SLinus Torvalds 		if (debug)
1323f342cda7SJoe Perches 			pr_debug("dst6_min set to: %s\n", buf);
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 		i += len;
13261da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
13271da177e4SLinus Torvalds 		return count;
13281da177e4SLinus Torvalds 	}
13291da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
13301da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1331222f1806SLuiz Capitulino 		if (len < 0)
1332222f1806SLuiz Capitulino 			return len;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13371da177e4SLinus Torvalds 			return -EFAULT;
13381da177e4SLinus Torvalds 		buf[len] = 0;
13391da177e4SLinus Torvalds 
1340c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
134147a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 		if (debug)
1344f342cda7SJoe Perches 			pr_debug("dst6_max set to: %s\n", buf);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 		i += len;
13471da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
13481da177e4SLinus Torvalds 		return count;
13491da177e4SLinus Torvalds 	}
13501da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
13511da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1352222f1806SLuiz Capitulino 		if (len < 0)
1353222f1806SLuiz Capitulino 			return len;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13581da177e4SLinus Torvalds 			return -EFAULT;
13591da177e4SLinus Torvalds 		buf[len] = 0;
13601da177e4SLinus Torvalds 
1361c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
136247a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
13631da177e4SLinus Torvalds 
13644e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 		if (debug)
1367f342cda7SJoe Perches 			pr_debug("src6 set to: %s\n", buf);
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 		i += len;
13701da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
13711da177e4SLinus Torvalds 		return count;
13721da177e4SLinus Torvalds 	}
13731da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
13741da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
137563adc6fbSStephen Hemminger 		if (len < 0)
1376222f1806SLuiz Capitulino 			return len;
137763adc6fbSStephen Hemminger 
13781da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13791da177e4SLinus Torvalds 			return -EFAULT;
13801da177e4SLinus Torvalds 		buf[len] = 0;
13811da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
13821da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
13831da177e4SLinus Torvalds 			strncpy(pkt_dev->src_min, buf, len);
13841da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
13851da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
13861da177e4SLinus Torvalds 		}
13871da177e4SLinus Torvalds 		if (debug)
1388f342cda7SJoe Perches 			pr_debug("src_min set to: %s\n", pkt_dev->src_min);
13891da177e4SLinus Torvalds 		i += len;
13901da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
13911da177e4SLinus Torvalds 		return count;
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
13941da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
139563adc6fbSStephen Hemminger 		if (len < 0)
1396222f1806SLuiz Capitulino 			return len;
139763adc6fbSStephen Hemminger 
13981da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13991da177e4SLinus Torvalds 			return -EFAULT;
14001da177e4SLinus Torvalds 		buf[len] = 0;
14011da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
14021da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
14031da177e4SLinus Torvalds 			strncpy(pkt_dev->src_max, buf, len);
14041da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
14051da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
14061da177e4SLinus Torvalds 		}
14071da177e4SLinus Torvalds 		if (debug)
1408f342cda7SJoe Perches 			pr_debug("src_max set to: %s\n", pkt_dev->src_max);
14091da177e4SLinus Torvalds 		i += len;
14101da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
14111da177e4SLinus Torvalds 		return count;
14121da177e4SLinus Torvalds 	}
14131da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
14141da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
141563adc6fbSStephen Hemminger 		if (len < 0)
1416222f1806SLuiz Capitulino 			return len;
141763adc6fbSStephen Hemminger 
14181da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14191da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14201da177e4SLinus Torvalds 			return -EFAULT;
14211da177e4SLinus Torvalds 
14224940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->dst_mac))
14234940fc88SAlexey Dobriyan 			return -EINVAL;
14241da177e4SLinus Torvalds 		/* Set up Dest MAC */
14254940fc88SAlexey Dobriyan 		memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
14261da177e4SLinus Torvalds 
14274940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
14281da177e4SLinus Torvalds 		return count;
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
14311da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
143263adc6fbSStephen Hemminger 		if (len < 0)
1433222f1806SLuiz Capitulino 			return len;
143463adc6fbSStephen Hemminger 
14351da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14361da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14371da177e4SLinus Torvalds 			return -EFAULT;
14381da177e4SLinus Torvalds 
14394940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->src_mac))
14404940fc88SAlexey Dobriyan 			return -EINVAL;
1441ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
14424940fc88SAlexey Dobriyan 		memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
1443ce5d0b47SAdit Ranadive 
14444940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
14451da177e4SLinus Torvalds 		return count;
14461da177e4SLinus Torvalds 	}
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
14491da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
14501da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
14511da177e4SLinus Torvalds 		return count;
14521da177e4SLinus Torvalds 	}
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
14551da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
145663adc6fbSStephen Hemminger 		if (len < 0)
1457222f1806SLuiz Capitulino 			return len;
145863adc6fbSStephen Hemminger 
14591da177e4SLinus Torvalds 		i += len;
14601da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
14611da177e4SLinus Torvalds 			value = MAX_CFLOWS;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 		pkt_dev->cflows = value;
14641da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
14651da177e4SLinus Torvalds 		return count;
14661da177e4SLinus Torvalds 	}
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
14691da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
147063adc6fbSStephen Hemminger 		if (len < 0)
1471222f1806SLuiz Capitulino 			return len;
147263adc6fbSStephen Hemminger 
14731da177e4SLinus Torvalds 		i += len;
14741da177e4SLinus Torvalds 		pkt_dev->lflow = value;
14751da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
14761da177e4SLinus Torvalds 		return count;
14771da177e4SLinus Torvalds 	}
14781da177e4SLinus Torvalds 
147945b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
148045b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
148163adc6fbSStephen Hemminger 		if (len < 0)
148245b270f8SRobert Olsson 			return len;
148363adc6fbSStephen Hemminger 
148445b270f8SRobert Olsson 		i += len;
148545b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
148645b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
148745b270f8SRobert Olsson 		return count;
148845b270f8SRobert Olsson 	}
148945b270f8SRobert Olsson 
149045b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
149145b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
149263adc6fbSStephen Hemminger 		if (len < 0)
149345b270f8SRobert Olsson 			return len;
149463adc6fbSStephen Hemminger 
149545b270f8SRobert Olsson 		i += len;
149645b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
149745b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
149845b270f8SRobert Olsson 		return count;
149945b270f8SRobert Olsson 	}
150045b270f8SRobert Olsson 
1501ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
150295c96174SEric Dumazet 		unsigned int n, cnt;
1503cfcabdccSStephen Hemminger 
1504ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1505cfcabdccSStephen Hemminger 		if (len < 0)
1506cfcabdccSStephen Hemminger 			return len;
1507ca6549afSSteven Whitehouse 		i += len;
1508cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1509ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1510cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1511ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1512ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
151334954ddcSFrancesco Fondelli 
151434954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
151534954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
151634954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
151734954ddcSFrancesco Fondelli 
151834954ddcSFrancesco Fondelli 			if (debug)
1519f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN auto turned off\n");
152034954ddcSFrancesco Fondelli 		}
152134954ddcSFrancesco Fondelli 		return count;
152234954ddcSFrancesco Fondelli 	}
152334954ddcSFrancesco Fondelli 
152434954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
152534954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
152663adc6fbSStephen Hemminger 		if (len < 0)
152734954ddcSFrancesco Fondelli 			return len;
152863adc6fbSStephen Hemminger 
152934954ddcSFrancesco Fondelli 		i += len;
153034954ddcSFrancesco Fondelli 		if (value <= 4095) {
153134954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
153234954ddcSFrancesco Fondelli 
153334954ddcSFrancesco Fondelli 			if (debug)
1534f342cda7SJoe Perches 				pr_debug("VLAN turned on\n");
153534954ddcSFrancesco Fondelli 
153634954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1537f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
153834954ddcSFrancesco Fondelli 
153934954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
154034954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
154134954ddcSFrancesco Fondelli 		} else {
154234954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
154334954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
154434954ddcSFrancesco Fondelli 
154534954ddcSFrancesco Fondelli 			if (debug)
1546f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
154734954ddcSFrancesco Fondelli 		}
154834954ddcSFrancesco Fondelli 		return count;
154934954ddcSFrancesco Fondelli 	}
155034954ddcSFrancesco Fondelli 
155134954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
155234954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
155363adc6fbSStephen Hemminger 		if (len < 0)
155434954ddcSFrancesco Fondelli 			return len;
155563adc6fbSStephen Hemminger 
155634954ddcSFrancesco Fondelli 		i += len;
155734954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
155834954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
155934954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
156034954ddcSFrancesco Fondelli 		} else {
156134954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
156234954ddcSFrancesco Fondelli 		}
156334954ddcSFrancesco Fondelli 		return count;
156434954ddcSFrancesco Fondelli 	}
156534954ddcSFrancesco Fondelli 
156634954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
156734954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
156863adc6fbSStephen Hemminger 		if (len < 0)
156934954ddcSFrancesco Fondelli 			return len;
157063adc6fbSStephen Hemminger 
157134954ddcSFrancesco Fondelli 		i += len;
157234954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
157334954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
157434954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
157534954ddcSFrancesco Fondelli 		} else {
157634954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
157734954ddcSFrancesco Fondelli 		}
157834954ddcSFrancesco Fondelli 		return count;
157934954ddcSFrancesco Fondelli 	}
158034954ddcSFrancesco Fondelli 
158134954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
158234954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
158363adc6fbSStephen Hemminger 		if (len < 0)
158434954ddcSFrancesco Fondelli 			return len;
158563adc6fbSStephen Hemminger 
158634954ddcSFrancesco Fondelli 		i += len;
158734954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
158834954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
158934954ddcSFrancesco Fondelli 
159034954ddcSFrancesco Fondelli 			if (debug)
1591f342cda7SJoe Perches 				pr_debug("SVLAN turned on\n");
159234954ddcSFrancesco Fondelli 
159334954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1594f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
159534954ddcSFrancesco Fondelli 
159634954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
159734954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
159834954ddcSFrancesco Fondelli 		} else {
159934954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
160034954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
160134954ddcSFrancesco Fondelli 
160234954ddcSFrancesco Fondelli 			if (debug)
1603f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
160434954ddcSFrancesco Fondelli 		}
160534954ddcSFrancesco Fondelli 		return count;
160634954ddcSFrancesco Fondelli 	}
160734954ddcSFrancesco Fondelli 
160834954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
160934954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
161063adc6fbSStephen Hemminger 		if (len < 0)
161134954ddcSFrancesco Fondelli 			return len;
161263adc6fbSStephen Hemminger 
161334954ddcSFrancesco Fondelli 		i += len;
161434954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
161534954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
161634954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
161734954ddcSFrancesco Fondelli 		} else {
161834954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
161934954ddcSFrancesco Fondelli 		}
162034954ddcSFrancesco Fondelli 		return count;
162134954ddcSFrancesco Fondelli 	}
162234954ddcSFrancesco Fondelli 
162334954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
162434954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
162563adc6fbSStephen Hemminger 		if (len < 0)
162634954ddcSFrancesco Fondelli 			return len;
162763adc6fbSStephen Hemminger 
162834954ddcSFrancesco Fondelli 		i += len;
162934954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
163034954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
163134954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
163234954ddcSFrancesco Fondelli 		} else {
163334954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
163434954ddcSFrancesco Fondelli 		}
1635ca6549afSSteven Whitehouse 		return count;
1636ca6549afSSteven Whitehouse 	}
1637ca6549afSSteven Whitehouse 
16381ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
16391ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16401ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
164163adc6fbSStephen Hemminger 		if (len < 0)
16421ca7768cSFrancesco Fondelli 			return len;
164363adc6fbSStephen Hemminger 
16441ca7768cSFrancesco Fondelli 		i += len;
16451ca7768cSFrancesco Fondelli 		if (len == 2) {
16461ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
16471ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
16481ca7768cSFrancesco Fondelli 		} else {
16491ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
16501ca7768cSFrancesco Fondelli 		}
16511ca7768cSFrancesco Fondelli 		return count;
16521ca7768cSFrancesco Fondelli 	}
16531ca7768cSFrancesco Fondelli 
16541ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
16551ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16561ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
165763adc6fbSStephen Hemminger 		if (len < 0)
16581ca7768cSFrancesco Fondelli 			return len;
165963adc6fbSStephen Hemminger 
16601ca7768cSFrancesco Fondelli 		i += len;
16611ca7768cSFrancesco Fondelli 		if (len == 2) {
16621ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
16631ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
16641ca7768cSFrancesco Fondelli 		} else {
16651ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
16661ca7768cSFrancesco Fondelli 		}
16671ca7768cSFrancesco Fondelli 		return count;
16681ca7768cSFrancesco Fondelli 	}
16691ca7768cSFrancesco Fondelli 
16709e50e3acSJohn Fastabend 	if (!strcmp(name, "skb_priority")) {
16719e50e3acSJohn Fastabend 		len = num_arg(&user_buffer[i], 9, &value);
16729e50e3acSJohn Fastabend 		if (len < 0)
16739e50e3acSJohn Fastabend 			return len;
16749e50e3acSJohn Fastabend 
16759e50e3acSJohn Fastabend 		i += len;
16769e50e3acSJohn Fastabend 		pkt_dev->skb_priority = value;
16779e50e3acSJohn Fastabend 		sprintf(pg_result, "OK: skb_priority=%i",
16789e50e3acSJohn Fastabend 			pkt_dev->skb_priority);
16799e50e3acSJohn Fastabend 		return count;
16809e50e3acSJohn Fastabend 	}
16819e50e3acSJohn Fastabend 
16821da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
16831da177e4SLinus Torvalds 	return -EINVAL;
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
1686d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
16871da177e4SLinus Torvalds {
1688d50a6b56SStephen Hemminger 	return single_open(file, pktgen_if_show, PDE(inode)->data);
16891da177e4SLinus Torvalds }
16901da177e4SLinus Torvalds 
16919a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1692d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1693d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1694d50a6b56SStephen Hemminger 	.read    = seq_read,
1695d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1696d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1697d50a6b56SStephen Hemminger 	.release = single_release,
1698d50a6b56SStephen Hemminger };
1699d50a6b56SStephen Hemminger 
1700d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1701d50a6b56SStephen Hemminger {
1702d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1703648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1704d50a6b56SStephen Hemminger 
1705d50a6b56SStephen Hemminger 	BUG_ON(!t);
1706d50a6b56SStephen Hemminger 
1707d50a6b56SStephen Hemminger 	seq_printf(seq, "Running: ");
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	if_lock(t);
1710c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17111da177e4SLinus Torvalds 		if (pkt_dev->running)
1712593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17131da177e4SLinus Torvalds 
1714d50a6b56SStephen Hemminger 	seq_printf(seq, "\nStopped: ");
17151da177e4SLinus Torvalds 
1716c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17171da177e4SLinus Torvalds 		if (!pkt_dev->running)
1718593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	if (t->result[0])
1721d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
17221da177e4SLinus Torvalds 	else
1723d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: NA\n");
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 	if_unlock(t);
17261da177e4SLinus Torvalds 
1727d50a6b56SStephen Hemminger 	return 0;
17281da177e4SLinus Torvalds }
17291da177e4SLinus Torvalds 
1730d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1731d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1732d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
17331da177e4SLinus Torvalds {
17348a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
1735d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1736d6182223SPaul Gortmaker 	int i, max, len, ret;
17371da177e4SLinus Torvalds 	char name[40];
17381da177e4SLinus Torvalds 	char *pg_result;
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds 	if (count < 1) {
17411da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
17421da177e4SLinus Torvalds 		return -EINVAL;
17431da177e4SLinus Torvalds 	}
17441da177e4SLinus Torvalds 
1745d6182223SPaul Gortmaker 	max = count;
1746d6182223SPaul Gortmaker 	len = count_trail_chars(user_buffer, max);
17471da177e4SLinus Torvalds 	if (len < 0)
17481da177e4SLinus Torvalds 		return len;
17491da177e4SLinus Torvalds 
1750d6182223SPaul Gortmaker 	i = len;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	/* Read variable name */
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
17551da177e4SLinus Torvalds 	if (len < 0)
17561da177e4SLinus Torvalds 		return len;
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
17591da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
17601da177e4SLinus Torvalds 		return -EFAULT;
17611da177e4SLinus Torvalds 	i += len;
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	max = count - i;
17641da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
17651da177e4SLinus Torvalds 	if (len < 0)
17661da177e4SLinus Torvalds 		return len;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	i += len;
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	if (debug)
1771f342cda7SJoe Perches 		pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	if (!t) {
1774f9467eaeSJoe Perches 		pr_err("ERROR: No thread\n");
17751da177e4SLinus Torvalds 		ret = -EINVAL;
17761da177e4SLinus Torvalds 		goto out;
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
17821da177e4SLinus Torvalds 		char f[32];
17831da177e4SLinus Torvalds 		memset(f, 0, 32);
17841da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
17851da177e4SLinus Torvalds 		if (len < 0) {
17861da177e4SLinus Torvalds 			ret = len;
17871da177e4SLinus Torvalds 			goto out;
17881da177e4SLinus Torvalds 		}
17891da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
17901da177e4SLinus Torvalds 			return -EFAULT;
17911da177e4SLinus Torvalds 		i += len;
17926146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
17931da177e4SLinus Torvalds 		pktgen_add_device(t, f);
17946146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
17951da177e4SLinus Torvalds 		ret = count;
17961da177e4SLinus Torvalds 		sprintf(pg_result, "OK: add_device=%s", f);
17971da177e4SLinus Torvalds 		goto out;
17981da177e4SLinus Torvalds 	}
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
18016146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
180295ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
18036146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1804121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
18051da177e4SLinus Torvalds 		ret = count;
18061da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
18071da177e4SLinus Torvalds 		goto out;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1811b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
18121da177e4SLinus Torvalds 		ret = count;
18131da177e4SLinus Torvalds 		goto out;
18141da177e4SLinus Torvalds 	}
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 	ret = -EINVAL;
18171da177e4SLinus Torvalds out:
18181da177e4SLinus Torvalds 	return ret;
18191da177e4SLinus Torvalds }
18201da177e4SLinus Torvalds 
1821d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
18221da177e4SLinus Torvalds {
1823d50a6b56SStephen Hemminger 	return single_open(file, pktgen_thread_show, PDE(inode)->data);
18241da177e4SLinus Torvalds }
18251da177e4SLinus Torvalds 
18269a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1827d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1828d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1829d50a6b56SStephen Hemminger 	.read    = seq_read,
1830d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1831d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1832d50a6b56SStephen Hemminger 	.release = single_release,
1833d50a6b56SStephen Hemminger };
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds /* Think find or remove for NN */
1836*4e58a027SCong Wang static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1837*4e58a027SCong Wang 					      const char *ifname, int remove)
18381da177e4SLinus Torvalds {
18391da177e4SLinus Torvalds 	struct pktgen_thread *t;
18401da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
18413e984840SEric Dumazet 	bool exact = (remove == FIND);
18421da177e4SLinus Torvalds 
1843*4e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
18443e984840SEric Dumazet 		pkt_dev = pktgen_find_dev(t, ifname, exact);
18451da177e4SLinus Torvalds 		if (pkt_dev) {
18461da177e4SLinus Torvalds 			if (remove) {
18471da177e4SLinus Torvalds 				if_lock(t);
184895ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
184995ed63f7SArthur Kepner 				t->control |= T_REMDEV;
18501da177e4SLinus Torvalds 				if_unlock(t);
18511da177e4SLinus Torvalds 			}
18521da177e4SLinus Torvalds 			break;
18531da177e4SLinus Torvalds 		}
18541da177e4SLinus Torvalds 	}
18551da177e4SLinus Torvalds 	return pkt_dev;
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds 
185895ed63f7SArthur Kepner /*
185995ed63f7SArthur Kepner  * mark a device for removal
186095ed63f7SArthur Kepner  */
1861*4e58a027SCong Wang static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
18621da177e4SLinus Torvalds {
18631da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
186495ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
186595ed63f7SArthur Kepner 	int i = 0;
186695ed63f7SArthur Kepner 
18676146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
1868f9467eaeSJoe Perches 	pr_debug("%s: marking %s for removal\n", __func__, ifname);
186995ed63f7SArthur Kepner 
187095ed63f7SArthur Kepner 	while (1) {
187195ed63f7SArthur Kepner 
1872*4e58a027SCong Wang 		pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1873222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1874222f1806SLuiz Capitulino 			break;	/* success */
187595ed63f7SArthur Kepner 
18766146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1877f9467eaeSJoe Perches 		pr_debug("%s: waiting for %s to disappear....\n",
1878f9467eaeSJoe Perches 			 __func__, ifname);
187995ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
18806146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
188195ed63f7SArthur Kepner 
188295ed63f7SArthur Kepner 		if (++i >= max_tries) {
1883f9467eaeSJoe Perches 			pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1884f9467eaeSJoe Perches 			       __func__, msec_per_try * i, ifname);
188595ed63f7SArthur Kepner 			break;
188695ed63f7SArthur Kepner 		}
188795ed63f7SArthur Kepner 
188895ed63f7SArthur Kepner 	}
188995ed63f7SArthur Kepner 
18906146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
189139df232fSStephen Hemminger }
189295ed63f7SArthur Kepner 
1893*4e58a027SCong Wang static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
189439df232fSStephen Hemminger {
189539df232fSStephen Hemminger 	struct pktgen_thread *t;
189639df232fSStephen Hemminger 
1897*4e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
189839df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
189939df232fSStephen Hemminger 
190039df232fSStephen Hemminger 		list_for_each_entry(pkt_dev, &t->if_list, list) {
190139df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
190239df232fSStephen Hemminger 				continue;
190339df232fSStephen Hemminger 
1904*4e58a027SCong Wang 			remove_proc_entry(pkt_dev->entry->name, pn->proc_dir);
190539df232fSStephen Hemminger 
19062975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
1907*4e58a027SCong Wang 							  pn->proc_dir,
19082975315bSAlexey Dobriyan 							  &pktgen_if_fops,
19092975315bSAlexey Dobriyan 							  pkt_dev);
191039df232fSStephen Hemminger 			if (!pkt_dev->entry)
1911f9467eaeSJoe Perches 				pr_err("can't move proc entry for '%s'\n",
1912f9467eaeSJoe Perches 				       dev->name);
191339df232fSStephen Hemminger 			break;
191439df232fSStephen Hemminger 		}
191539df232fSStephen Hemminger 	}
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
1918222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
1919222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
19201da177e4SLinus Torvalds {
192139df232fSStephen Hemminger 	struct net_device *dev = ptr;
1922*4e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
19231da177e4SLinus Torvalds 
1924*4e58a027SCong Wang 	if (pn->pktgen_exiting)
1925e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1926e9dc8653SEric W. Biederman 
19271da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
19281da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
19291da177e4SLinus Torvalds 	 */
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	switch (event) {
193239df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
1933*4e58a027SCong Wang 		pktgen_change_name(pn, dev);
19341da177e4SLinus Torvalds 		break;
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
1937*4e58a027SCong Wang 		pktgen_mark_device(pn, dev->name);
19381da177e4SLinus Torvalds 		break;
19393ff50b79SStephen Hemminger 	}
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	return NOTIFY_DONE;
19421da177e4SLinus Torvalds }
19431da177e4SLinus Torvalds 
1944*4e58a027SCong Wang static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1945*4e58a027SCong Wang 						 struct pktgen_dev *pkt_dev,
194663adc6fbSStephen Hemminger 						 const char *ifname)
1947e6fce5b9SRobert Olsson {
1948e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
1949d6182223SPaul Gortmaker 	int i;
1950e6fce5b9SRobert Olsson 
1951e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
1952e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
1953e6fce5b9SRobert Olsson 			break;
1954e6fce5b9SRobert Olsson 
1955e6fce5b9SRobert Olsson 		b[i] = ifname[i];
1956e6fce5b9SRobert Olsson 	}
1957e6fce5b9SRobert Olsson 	b[i] = 0;
1958e6fce5b9SRobert Olsson 
1959*4e58a027SCong Wang 	return dev_get_by_name(pn->net, b);
1960e6fce5b9SRobert Olsson }
1961e6fce5b9SRobert Olsson 
1962e6fce5b9SRobert Olsson 
19631da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
19641da177e4SLinus Torvalds 
1965*4e58a027SCong Wang static int pktgen_setup_dev(const struct pktgen_net *pn,
1966*4e58a027SCong Wang 			    struct pktgen_dev *pkt_dev, const char *ifname)
1967222f1806SLuiz Capitulino {
19681da177e4SLinus Torvalds 	struct net_device *odev;
196939df232fSStephen Hemminger 	int err;
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	/* Clean old setups */
19721da177e4SLinus Torvalds 	if (pkt_dev->odev) {
19731da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
19741da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
19751da177e4SLinus Torvalds 	}
19761da177e4SLinus Torvalds 
1977*4e58a027SCong Wang 	odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
19781da177e4SLinus Torvalds 	if (!odev) {
1979f9467eaeSJoe Perches 		pr_err("no such netdevice: \"%s\"\n", ifname);
198039df232fSStephen Hemminger 		return -ENODEV;
19811da177e4SLinus Torvalds 	}
198239df232fSStephen Hemminger 
19831da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
1984f9467eaeSJoe Perches 		pr_err("not an ethernet device: \"%s\"\n", ifname);
198539df232fSStephen Hemminger 		err = -EINVAL;
198639df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
1987f9467eaeSJoe Perches 		pr_err("device is down: \"%s\"\n", ifname);
198839df232fSStephen Hemminger 		err = -ENETDOWN;
198939df232fSStephen Hemminger 	} else {
19901da177e4SLinus Torvalds 		pkt_dev->odev = odev;
199139df232fSStephen Hemminger 		return 0;
199239df232fSStephen Hemminger 	}
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	dev_put(odev);
199539df232fSStephen Hemminger 	return err;
19961da177e4SLinus Torvalds }
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
19991da177e4SLinus Torvalds  * structure to have the right information to create/send packets
20001da177e4SLinus Torvalds  */
20011da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
20021da177e4SLinus Torvalds {
200364c00d81SAndrew Gallatin 	int ntxq;
200464c00d81SAndrew Gallatin 
20051da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
2006f9467eaeSJoe Perches 		pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2007222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
2008222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
20091da177e4SLinus Torvalds 		return;
20101da177e4SLinus Torvalds 	}
20111da177e4SLinus Torvalds 
201264c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
201364c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2014bfdbc0acSRobert Olsson 
201564c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
2016f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
201788271660SJesse Brandeburg 			   pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2018593f63b0SEric Dumazet 			   pkt_dev->odevname);
201926e29eedSDan Carpenter 		pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
202064c00d81SAndrew Gallatin 	}
202188271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
2022f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
202388271660SJesse Brandeburg 			   pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2024593f63b0SEric Dumazet 			   pkt_dev->odevname);
202526e29eedSDan Carpenter 		pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
202664c00d81SAndrew Gallatin 	}
202764c00d81SAndrew Gallatin 
20281da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
20291da177e4SLinus Torvalds 
2030f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
2031f404e9a6SKris Katterjohn 		memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	/* Set up Dest MAC */
2034f404e9a6SKris Katterjohn 	memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
20374c139b8cSAmerigo Wang 		int i, set = 0, err = 1;
20384c139b8cSAmerigo Wang 		struct inet6_dev *idev;
20394c139b8cSAmerigo Wang 
204068bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
204168bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
204268bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
204368bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
204468bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
204568bf9f0bSAmerigo Wang 		}
204668bf9f0bSAmerigo Wang 
20471da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
20481da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
20491da177e4SLinus Torvalds 				set = 1;
20501da177e4SLinus Torvalds 				break;
20511da177e4SLinus Torvalds 			}
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 		if (!set) {
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 			/*
20561da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
20571da177e4SLinus Torvalds 			 *
20581da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
20591da177e4SLinus Torvalds 			 */
20601da177e4SLinus Torvalds 
20618814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
206263adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
206363adc6fbSStephen Hemminger 			if (idev) {
20641da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
20674c139b8cSAmerigo Wang 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
20684c139b8cSAmerigo Wang 					if ((ifp->scope & IFA_LINK) &&
2069f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
20704e3fd7a0SAlexey Dobriyan 						pkt_dev->cur_in6_saddr = ifp->addr;
20711da177e4SLinus Torvalds 						err = 0;
20721da177e4SLinus Torvalds 						break;
20731da177e4SLinus Torvalds 					}
20741da177e4SLinus Torvalds 				}
20751da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
20761da177e4SLinus Torvalds 			}
20778814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2078222f1806SLuiz Capitulino 			if (err)
2079f9467eaeSJoe Perches 				pr_err("ERROR: IPv6 link address not available\n");
20801da177e4SLinus Torvalds 		}
2081222f1806SLuiz Capitulino 	} else {
208268bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
208368bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
208468bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
208568bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
208668bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
208768bf9f0bSAmerigo Wang 		}
208868bf9f0bSAmerigo Wang 
20891da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
20901da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
20911da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 			struct in_device *in_dev;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 			rcu_read_lock();
2096e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
20971da177e4SLinus Torvalds 			if (in_dev) {
20981da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2099222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2100222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
21011da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
21021da177e4SLinus Torvalds 				}
21031da177e4SLinus Torvalds 			}
21041da177e4SLinus Torvalds 			rcu_read_unlock();
2105222f1806SLuiz Capitulino 		} else {
21061da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
21071da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
21081da177e4SLinus Torvalds 		}
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
21111da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
21121da177e4SLinus Torvalds 	}
21131da177e4SLinus Torvalds 	/* Initialize current values. */
211468bf9f0bSAmerigo Wang 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
211568bf9f0bSAmerigo Wang 	if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
211668bf9f0bSAmerigo Wang 		pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
211768bf9f0bSAmerigo Wang 
21181da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
21191da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
21201da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
21211da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
21221da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
21231da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
21241da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 
2128fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2129fd29cf72SStephen Hemminger {
2130ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2131417bc4b8SEric Dumazet 	s64 remaining;
21322bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2133fd29cf72SStephen Hemminger 
21342bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
21352bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2136fd29cf72SStephen Hemminger 
213743d28b65SDaniel Turull 	remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2138417bc4b8SEric Dumazet 	if (remaining <= 0) {
2139417bc4b8SEric Dumazet 		pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21401da177e4SLinus Torvalds 		return;
2141417bc4b8SEric Dumazet 	}
21422bc481cfSStephen Hemminger 
2143398f382cSDaniel Borkmann 	start_time = ktime_get();
214433136d12SEric Dumazet 	if (remaining < 100000) {
214533136d12SEric Dumazet 		/* for small delays (<100us), just loop until limit is reached */
214633136d12SEric Dumazet 		do {
2147398f382cSDaniel Borkmann 			end_time = ktime_get();
2148398f382cSDaniel Borkmann 		} while (ktime_compare(end_time, spin_until) < 0);
214933136d12SEric Dumazet 	} else {
21502bc481cfSStephen Hemminger 		/* see do_nanosleep */
21512bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
21522bc481cfSStephen Hemminger 		do {
21532bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
21542bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
21552bc481cfSStephen Hemminger 			if (!hrtimer_active(&t.timer))
21562bc481cfSStephen Hemminger 				t.task = NULL;
21572bc481cfSStephen Hemminger 
21582bc481cfSStephen Hemminger 			if (likely(t.task))
21591da177e4SLinus Torvalds 				schedule();
21601da177e4SLinus Torvalds 
21612bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
21622bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
21632bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
2164398f382cSDaniel Borkmann 		end_time = ktime_get();
216533136d12SEric Dumazet 	}
2166ef87979cSStephen Hemminger 
2167ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
216807a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds 
217116dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
217216dab72fSJamal Hadi Salim {
2173a553e4a6SJamal Hadi Salim 	pkt_dev->pkt_overhead = 0;
217416dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
217516dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
217616dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
217716dab72fSJamal Hadi Salim }
217816dab72fSJamal Hadi Salim 
2179648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2180007a531bSJamal Hadi Salim {
2181648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2182007a531bSJamal Hadi Salim }
2183007a531bSJamal Hadi Salim 
2184007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2185007a531bSJamal Hadi Salim {
2186007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2187007a531bSJamal Hadi Salim 
2188007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2189007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2190007a531bSJamal Hadi Salim 			/* reset time */
2191007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
21921211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2193007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2194007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2195007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2196007a531bSJamal Hadi Salim 		}
2197007a531bSJamal Hadi Salim 	} else {
2198007a531bSJamal Hadi Salim 		flow = random32() % pkt_dev->cflows;
21991211a645SRobert Olsson 		pkt_dev->curfl = flow;
2200007a531bSJamal Hadi Salim 
22011211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2202007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22031211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
22041211a645SRobert Olsson 		}
2205007a531bSJamal Hadi Salim 	}
2206007a531bSJamal Hadi Salim 
2207007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2208007a531bSJamal Hadi Salim }
2209007a531bSJamal Hadi Salim 
2210a553e4a6SJamal Hadi Salim 
2211a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2212a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2213a553e4a6SJamal Hadi Salim  * we go look for it ...
2214a553e4a6SJamal Hadi Salim */
2215bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2216fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2217a553e4a6SJamal Hadi Salim {
2218a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
2219*4e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2220a553e4a6SJamal Hadi Salim 	if (!x) {
2221a553e4a6SJamal Hadi Salim 		/*slow path: we dont already have xfrm_state*/
2222*4e58a027SCong Wang 		x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
22235447c5e4SAlexey Dobriyan 					(xfrm_address_t *)&pkt_dev->cur_daddr,
2224a553e4a6SJamal Hadi Salim 					(xfrm_address_t *)&pkt_dev->cur_saddr,
2225a553e4a6SJamal Hadi Salim 					AF_INET,
2226a553e4a6SJamal Hadi Salim 					pkt_dev->ipsmode,
2227a553e4a6SJamal Hadi Salim 					pkt_dev->ipsproto, 0);
2228a553e4a6SJamal Hadi Salim 		if (x) {
2229a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2230a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2231a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2232a553e4a6SJamal Hadi Salim 		}
2233a553e4a6SJamal Hadi Salim 
2234a553e4a6SJamal Hadi Salim 	}
2235a553e4a6SJamal Hadi Salim }
2236a553e4a6SJamal Hadi Salim #endif
2237fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2238fd2ea0a7SDavid S. Miller {
2239e6fce5b9SRobert Olsson 
2240e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2241e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2242e6fce5b9SRobert Olsson 
2243896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2244fd2ea0a7SDavid S. Miller 		__u16 t;
2245fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2246fd2ea0a7SDavid S. Miller 			t = random32() %
2247fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2248fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2249fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2250fd2ea0a7SDavid S. Miller 		} else {
2251fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2252fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2253fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2254fd2ea0a7SDavid S. Miller 		}
2255fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2256fd2ea0a7SDavid S. Miller 	}
2257bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2258fd2ea0a7SDavid S. Miller }
2259fd2ea0a7SDavid S. Miller 
22601da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
22611da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
22621da177e4SLinus Torvalds  */
2263222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2264222f1806SLuiz Capitulino {
22651da177e4SLinus Torvalds 	__u32 imn;
22661da177e4SLinus Torvalds 	__u32 imx;
22671da177e4SLinus Torvalds 	int flow = 0;
22681da177e4SLinus Torvalds 
2269007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2270007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 	/*  Deal with source MAC */
22731da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
22741da177e4SLinus Torvalds 		__u32 mc;
22751da177e4SLinus Torvalds 		__u32 tmp;
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
22785fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->src_mac_count;
22791da177e4SLinus Torvalds 		else {
22801da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2281ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2282222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
22831da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
22841da177e4SLinus Torvalds 		}
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
22871da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
22881da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
22891da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
22901da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
22911da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
22921da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
22931da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
22941da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
22951da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
22961da177e4SLinus Torvalds 	}
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
22991da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
23001da177e4SLinus Torvalds 		__u32 mc;
23011da177e4SLinus Torvalds 		__u32 tmp;
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
23045fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->dst_mac_count;
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds 		else {
23071da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2308ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2309222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
23101da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
23111da177e4SLinus Torvalds 			}
23121da177e4SLinus Torvalds 		}
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
23151da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
23161da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23171da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
23181da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23191da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
23201da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23211da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
23221da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
23231da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
23241da177e4SLinus Torvalds 	}
23251da177e4SLinus Torvalds 
2326ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
232795c96174SEric Dumazet 		unsigned int i;
2328ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2329ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2330ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
23315fa6fc76SStephen Hemminger 					     ((__force __be32)random32() &
2332ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2333ca6549afSSteven Whitehouse 	}
2334ca6549afSSteven Whitehouse 
233534954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
23365fa6fc76SStephen Hemminger 		pkt_dev->vlan_id = random32() & (4096-1);
233734954ddcSFrancesco Fondelli 	}
233834954ddcSFrancesco Fondelli 
233934954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
23405fa6fc76SStephen Hemminger 		pkt_dev->svlan_id = random32() & (4096 - 1);
234134954ddcSFrancesco Fondelli 	}
234234954ddcSFrancesco Fondelli 
23431da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
23441da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
23455fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_src = random32() %
23465fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
23475fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 		else {
23501da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
23511da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
23521da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
23531da177e4SLinus Torvalds 		}
23541da177e4SLinus Torvalds 	}
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
23571da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
23585fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_dst = random32() %
23595fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
23605fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2361222f1806SLuiz Capitulino 		} else {
23621da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
23631da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
23641da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
23651da177e4SLinus Torvalds 		}
23661da177e4SLinus Torvalds 	}
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
23691da177e4SLinus Torvalds 
237063adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
237163adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
237263adc6fbSStephen Hemminger 		if (imn < imx) {
23731da177e4SLinus Torvalds 			__u32 t;
23741da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
23755fa6fc76SStephen Hemminger 				t = random32() % (imx - imn) + imn;
23761da177e4SLinus Torvalds 			else {
23771da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
23781da177e4SLinus Torvalds 				t++;
237963adc6fbSStephen Hemminger 				if (t > imx)
23801da177e4SLinus Torvalds 					t = imn;
238163adc6fbSStephen Hemminger 
23821da177e4SLinus Torvalds 			}
23831da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
23841da177e4SLinus Torvalds 		}
23851da177e4SLinus Torvalds 
2386007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
23871da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
23881da177e4SLinus Torvalds 		} else {
2389252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2390252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2391252e3346SAl Viro 			if (imn < imx) {
23921da177e4SLinus Torvalds 				__u32 t;
2393252e3346SAl Viro 				__be32 s;
23941da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
23951da177e4SLinus Torvalds 
23965fa6fc76SStephen Hemminger 					t = random32() % (imx - imn) + imn;
2397252e3346SAl Viro 					s = htonl(t);
23981da177e4SLinus Torvalds 
239921cf2253SJoe Perches 					while (ipv4_is_loopback(s) ||
240021cf2253SJoe Perches 					       ipv4_is_multicast(s) ||
24011e637c74SJan Engelhardt 					       ipv4_is_lbcast(s) ||
240221cf2253SJoe Perches 					       ipv4_is_zeronet(s) ||
240321cf2253SJoe Perches 					       ipv4_is_local_multicast(s)) {
24045fa6fc76SStephen Hemminger 						t = random32() % (imx - imn) + imn;
2405252e3346SAl Viro 						s = htonl(t);
24061da177e4SLinus Torvalds 					}
2407252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2408252e3346SAl Viro 				} else {
24091da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
24101da177e4SLinus Torvalds 					t++;
24111da177e4SLinus Torvalds 					if (t > imx) {
24121da177e4SLinus Torvalds 						t = imn;
24131da177e4SLinus Torvalds 					}
24141da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
24151da177e4SLinus Torvalds 				}
24161da177e4SLinus Torvalds 			}
24171da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2418007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2419222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2420222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2421a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2422a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2423a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2424a553e4a6SJamal Hadi Salim #endif
24251da177e4SLinus Torvalds 				pkt_dev->nflows++;
24261da177e4SLinus Torvalds 			}
24271da177e4SLinus Torvalds 		}
2428222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2429222f1806SLuiz Capitulino 
243006e30411SJoe Perches 		if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
24311da177e4SLinus Torvalds 			int i;
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 			/* Only random destinations yet */
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
24361da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
24375fa6fc76SStephen Hemminger 				    (((__force __be32)random32() |
24381da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
24391da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
24401da177e4SLinus Torvalds 			}
24411da177e4SLinus Torvalds 		}
24421da177e4SLinus Torvalds 	}
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
24451da177e4SLinus Torvalds 		__u32 t;
24461da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
24475fa6fc76SStephen Hemminger 			t = random32() %
24485fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
24495fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2450222f1806SLuiz Capitulino 		} else {
24511da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
24521da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
24531da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
24541da177e4SLinus Torvalds 		}
24551da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
24561da177e4SLinus Torvalds 	}
24571da177e4SLinus Torvalds 
2458fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
245945b270f8SRobert Olsson 
24601da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
24611da177e4SLinus Torvalds }
24621da177e4SLinus Torvalds 
2463a553e4a6SJamal Hadi Salim 
2464a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2465a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2466a553e4a6SJamal Hadi Salim {
2467a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2468a553e4a6SJamal Hadi Salim 	int err = 0;
2469a553e4a6SJamal Hadi Salim 
2470a553e4a6SJamal Hadi Salim 	if (!x)
2471a553e4a6SJamal Hadi Salim 		return 0;
2472a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2473a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2474a553e4a6SJamal Hadi Salim 	if (x->props.mode != XFRM_MODE_TRANSPORT)
2475a553e4a6SJamal Hadi Salim 		return 0;
2476a553e4a6SJamal Hadi Salim 
2477a553e4a6SJamal Hadi Salim 	spin_lock(&x->lock);
2478a553e4a6SJamal Hadi Salim 
247913996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2480a553e4a6SJamal Hadi Salim 	if (err)
2481a553e4a6SJamal Hadi Salim 		goto error;
2482a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
2483a553e4a6SJamal Hadi Salim 	if (err)
2484a553e4a6SJamal Hadi Salim 		goto error;
2485a553e4a6SJamal Hadi Salim 
2486a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2487a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
2488a553e4a6SJamal Hadi Salim error:
2489a553e4a6SJamal Hadi Salim 	spin_unlock(&x->lock);
2490a553e4a6SJamal Hadi Salim 	return err;
2491a553e4a6SJamal Hadi Salim }
2492a553e4a6SJamal Hadi Salim 
2493475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2494a553e4a6SJamal Hadi Salim {
2495a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2496a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2497d6182223SPaul Gortmaker 		int i;
2498d6182223SPaul Gortmaker 		for (i = 0; i < pkt_dev->cflows; i++) {
2499a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2500a553e4a6SJamal Hadi Salim 			if (x) {
2501a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2502a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2503a553e4a6SJamal Hadi Salim 			}
2504a553e4a6SJamal Hadi Salim 		}
2505a553e4a6SJamal Hadi Salim 	}
2506a553e4a6SJamal Hadi Salim }
2507a553e4a6SJamal Hadi Salim 
2508475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2509a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2510a553e4a6SJamal Hadi Salim {
2511a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2512a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2513a553e4a6SJamal Hadi Salim 		int nhead = 0;
2514a553e4a6SJamal Hadi Salim 		if (x) {
2515a553e4a6SJamal Hadi Salim 			int ret;
2516a553e4a6SJamal Hadi Salim 			__u8 *eth;
2517a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2518a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2519a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2520a553e4a6SJamal Hadi Salim 				if (ret < 0) {
2521f9467eaeSJoe Perches 					pr_err("Error expanding ipsec packet %d\n",
2522f9467eaeSJoe Perches 					       ret);
2523b4bb4ac8SIlpo Järvinen 					goto err;
2524a553e4a6SJamal Hadi Salim 				}
2525a553e4a6SJamal Hadi Salim 			}
2526a553e4a6SJamal Hadi Salim 
2527a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2528a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2529a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2530a553e4a6SJamal Hadi Salim 			if (ret) {
2531f9467eaeSJoe Perches 				pr_err("Error creating ipsec packet %d\n", ret);
2532b4bb4ac8SIlpo Järvinen 				goto err;
2533a553e4a6SJamal Hadi Salim 			}
2534a553e4a6SJamal Hadi Salim 			/* restore ll */
2535a553e4a6SJamal Hadi Salim 			eth = (__u8 *) skb_push(skb, ETH_HLEN);
2536a553e4a6SJamal Hadi Salim 			memcpy(eth, pkt_dev->hh, 12);
2537a553e4a6SJamal Hadi Salim 			*(u16 *) &eth[12] = protocol;
2538a553e4a6SJamal Hadi Salim 		}
2539a553e4a6SJamal Hadi Salim 	}
2540a553e4a6SJamal Hadi Salim 	return 1;
2541b4bb4ac8SIlpo Järvinen err:
2542b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2543b4bb4ac8SIlpo Järvinen 	return 0;
2544a553e4a6SJamal Hadi Salim }
2545a553e4a6SJamal Hadi Salim #endif
2546a553e4a6SJamal Hadi Salim 
2547ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2548ca6549afSSteven Whitehouse {
254995c96174SEric Dumazet 	unsigned int i;
255063adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2551ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
255263adc6fbSStephen Hemminger 
2553ca6549afSSteven Whitehouse 	mpls--;
2554ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2555ca6549afSSteven Whitehouse }
2556ca6549afSSteven Whitehouse 
25570f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
25580f37c605SAl Viro 			       unsigned int prio)
25590f37c605SAl Viro {
25600f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
25610f37c605SAl Viro }
25620f37c605SAl Viro 
256326ad7879SEric Dumazet static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
256426ad7879SEric Dumazet 				int datalen)
256526ad7879SEric Dumazet {
256626ad7879SEric Dumazet 	struct timeval timestamp;
256726ad7879SEric Dumazet 	struct pktgen_hdr *pgh;
256826ad7879SEric Dumazet 
256926ad7879SEric Dumazet 	pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
257026ad7879SEric Dumazet 	datalen -= sizeof(*pgh);
257126ad7879SEric Dumazet 
257226ad7879SEric Dumazet 	if (pkt_dev->nfrags <= 0) {
257305aebe2eSDaniel Turull 		memset(skb_put(skb, datalen), 0, datalen);
257426ad7879SEric Dumazet 	} else {
257526ad7879SEric Dumazet 		int frags = pkt_dev->nfrags;
257626ad7879SEric Dumazet 		int i, len;
25777d36a991Samit salecha 		int frag_len;
257826ad7879SEric Dumazet 
257926ad7879SEric Dumazet 
258026ad7879SEric Dumazet 		if (frags > MAX_SKB_FRAGS)
258126ad7879SEric Dumazet 			frags = MAX_SKB_FRAGS;
258226ad7879SEric Dumazet 		len = datalen - frags * PAGE_SIZE;
258326ad7879SEric Dumazet 		if (len > 0) {
258426ad7879SEric Dumazet 			memset(skb_put(skb, len), 0, len);
258526ad7879SEric Dumazet 			datalen = frags * PAGE_SIZE;
258626ad7879SEric Dumazet 		}
258726ad7879SEric Dumazet 
258826ad7879SEric Dumazet 		i = 0;
25897d36a991Samit salecha 		frag_len = (datalen/frags) < PAGE_SIZE ?
25907d36a991Samit salecha 			   (datalen/frags) : PAGE_SIZE;
259126ad7879SEric Dumazet 		while (datalen > 0) {
259226ad7879SEric Dumazet 			if (unlikely(!pkt_dev->page)) {
259326ad7879SEric Dumazet 				int node = numa_node_id();
259426ad7879SEric Dumazet 
259526ad7879SEric Dumazet 				if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
259626ad7879SEric Dumazet 					node = pkt_dev->node;
259726ad7879SEric Dumazet 				pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
259826ad7879SEric Dumazet 				if (!pkt_dev->page)
259926ad7879SEric Dumazet 					break;
260026ad7879SEric Dumazet 			}
2601a0bec1cdSIan Campbell 			get_page(pkt_dev->page);
2602ea2ab693SIan Campbell 			skb_frag_set_page(skb, i, pkt_dev->page);
260326ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page_offset = 0;
26047d36a991Samit salecha 			/*last fragment, fill rest of data*/
26057d36a991Samit salecha 			if (i == (frags - 1))
26069e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i],
26079e903e08SEric Dumazet 				    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
26087d36a991Samit salecha 			else
26099e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
26109e903e08SEric Dumazet 			datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
26119e903e08SEric Dumazet 			skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
26129e903e08SEric Dumazet 			skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
261326ad7879SEric Dumazet 			i++;
261426ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
261526ad7879SEric Dumazet 		}
261626ad7879SEric Dumazet 	}
261726ad7879SEric Dumazet 
261826ad7879SEric Dumazet 	/* Stamp the time, and sequence number,
261926ad7879SEric Dumazet 	 * convert them to network byte order
262026ad7879SEric Dumazet 	 */
262126ad7879SEric Dumazet 	pgh->pgh_magic = htonl(PKTGEN_MAGIC);
262226ad7879SEric Dumazet 	pgh->seq_num = htonl(pkt_dev->seq_num);
262326ad7879SEric Dumazet 
262426ad7879SEric Dumazet 	do_gettimeofday(&timestamp);
262526ad7879SEric Dumazet 	pgh->tv_sec = htonl(timestamp.tv_sec);
262626ad7879SEric Dumazet 	pgh->tv_usec = htonl(timestamp.tv_usec);
262726ad7879SEric Dumazet }
262826ad7879SEric Dumazet 
26291da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
26301da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
26311da177e4SLinus Torvalds {
26321da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
26331da177e4SLinus Torvalds 	__u8 *eth;
26341da177e4SLinus Torvalds 	struct udphdr *udph;
26351da177e4SLinus Torvalds 	int datalen, iplen;
26361da177e4SLinus Torvalds 	struct iphdr *iph;
2637d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2638ca6549afSSteven Whitehouse 	__be32 *mpls;
263934954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
264034954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
264134954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
264234954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2643fd2ea0a7SDavid S. Miller 	u16 queue_map;
2644ca6549afSSteven Whitehouse 
2645ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2646d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
26471da177e4SLinus Torvalds 
264834954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2649d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
265034954ddcSFrancesco Fondelli 
265164053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
265264053beeSRobert Olsson 	 * fields.
265364053beeSRobert Olsson 	 */
265464053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2655eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
265664053beeSRobert Olsson 
26577ac5459eSDavid S. Miller 	datalen = (odev->hard_header_len + 16) & ~0xf;
2658e99b99b4SRobert Olsson 
2659e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE) {
2660e99b99b4SRobert Olsson 		int node;
2661e99b99b4SRobert Olsson 
2662e99b99b4SRobert Olsson 		if (pkt_dev->node >= 0)
2663e99b99b4SRobert Olsson 			node = pkt_dev->node;
2664e99b99b4SRobert Olsson 		else
2665e99b99b4SRobert Olsson 			node =  numa_node_id();
2666e99b99b4SRobert Olsson 
2667e99b99b4SRobert Olsson 		skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2668e99b99b4SRobert Olsson 				  + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2669e99b99b4SRobert Olsson 		if (likely(skb)) {
2670e99b99b4SRobert Olsson 			skb_reserve(skb, NET_SKB_PAD);
2671e99b99b4SRobert Olsson 			skb->dev = odev;
2672e99b99b4SRobert Olsson 		}
2673e99b99b4SRobert Olsson 	}
2674e99b99b4SRobert Olsson 	else
2675e470757dSStephen Hemminger 	  skb = __netdev_alloc_skb(odev,
2676e470757dSStephen Hemminger 				   pkt_dev->cur_pkt_size + 64
2677e470757dSStephen Hemminger 				   + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2678e99b99b4SRobert Olsson 
26791da177e4SLinus Torvalds 	if (!skb) {
26801da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
26811da177e4SLinus Torvalds 		return NULL;
26821da177e4SLinus Torvalds 	}
2683a8d764b9SJunchang Wang 	prefetchw(skb->data);
26841da177e4SLinus Torvalds 
26857ac5459eSDavid S. Miller 	skb_reserve(skb, datalen);
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
26881da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2689ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2690ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2691ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
269234954ddcSFrancesco Fondelli 
269334954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
269434954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
269534954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
26960f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
26970f37c605SAl Viro 					       pkt_dev->svlan_cfi,
26980f37c605SAl Viro 					       pkt_dev->svlan_p);
269934954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2700d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
270134954ddcSFrancesco Fondelli 		}
270234954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27030f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
27040f37c605SAl Viro 				      pkt_dev->vlan_cfi,
27050f37c605SAl Viro 				      pkt_dev->vlan_p);
270634954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2707d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
270834954ddcSFrancesco Fondelli 	}
270934954ddcSFrancesco Fondelli 
271027a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
2711b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct iphdr);
2712ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2713fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
27149e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
27159e50e3acSJohn Fastabend 
2716ddc7b8e3SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
2717ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2720252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
27211da177e4SLinus Torvalds 
2722ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2723ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
272416dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
27256af773e7SNishank Trivedi 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
27261da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
27291da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
27301da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
27311da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds 	iph->ihl = 5;
27341da177e4SLinus Torvalds 	iph->version = 4;
27351da177e4SLinus Torvalds 	iph->ttl = 32;
27361ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
27371da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
27381da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
27391da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
274066ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
274166ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
27421da177e4SLinus Torvalds 	iph->frag_off = 0;
27431da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
27441da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
27451da177e4SLinus Torvalds 	iph->check = 0;
27461da177e4SLinus Torvalds 	iph->check = ip_fast_csum((void *)iph, iph->ihl);
2747ca6549afSSteven Whitehouse 	skb->protocol = protocol;
2748b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
274916dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
27501da177e4SLinus Torvalds 	skb->dev = odev;
27511da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
275226ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
27531da177e4SLinus Torvalds 
2754a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2755a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2756a553e4a6SJamal Hadi Salim 		return NULL;
2757a553e4a6SJamal Hadi Salim #endif
2758a553e4a6SJamal Hadi Salim 
27591da177e4SLinus Torvalds 	return skb;
27601da177e4SLinus Torvalds }
27611da177e4SLinus Torvalds 
27621da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
27631da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
27641da177e4SLinus Torvalds {
27651da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
27661da177e4SLinus Torvalds 	__u8 *eth;
27671da177e4SLinus Torvalds 	struct udphdr *udph;
27681da177e4SLinus Torvalds 	int datalen;
27691da177e4SLinus Torvalds 	struct ipv6hdr *iph;
2770d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2771ca6549afSSteven Whitehouse 	__be32 *mpls;
277234954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
277334954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
277434954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
277534954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2776fd2ea0a7SDavid S. Miller 	u16 queue_map;
2777ca6549afSSteven Whitehouse 
2778ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2779d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
27801da177e4SLinus Torvalds 
278134954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2782d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
278334954ddcSFrancesco Fondelli 
278464053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
278564053beeSRobert Olsson 	 * fields.
278664053beeSRobert Olsson 	 */
278764053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2788eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
278964053beeSRobert Olsson 
2790e470757dSStephen Hemminger 	skb = __netdev_alloc_skb(odev,
2791e470757dSStephen Hemminger 				 pkt_dev->cur_pkt_size + 64
2792e470757dSStephen Hemminger 				 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
27931da177e4SLinus Torvalds 	if (!skb) {
27941da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
27951da177e4SLinus Torvalds 		return NULL;
27961da177e4SLinus Torvalds 	}
2797a8d764b9SJunchang Wang 	prefetchw(skb->data);
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds 	skb_reserve(skb, 16);
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
28021da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2803ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2804ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2805ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
280634954ddcSFrancesco Fondelli 
280734954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
280834954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
280934954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28100f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
28110f37c605SAl Viro 					       pkt_dev->svlan_cfi,
28120f37c605SAl Viro 					       pkt_dev->svlan_p);
281334954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2814d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
281534954ddcSFrancesco Fondelli 		}
281634954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28170f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
28180f37c605SAl Viro 				      pkt_dev->vlan_cfi,
28190f37c605SAl Viro 				      pkt_dev->vlan_p);
282034954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2821d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
282234954ddcSFrancesco Fondelli 	}
282334954ddcSFrancesco Fondelli 
282427a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
2825b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2826ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2827fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
28289e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
2829ddc7b8e3SArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
2830ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2833252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
28341da177e4SLinus Torvalds 
2835ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2836ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
2837ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
283816dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
28391da177e4SLinus Torvalds 
28405aa8b572SAmerigo Wang 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
28411da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
2842e87cc472SJoe Perches 		net_info_ratelimited("increased datalen to %d\n", datalen);
28431da177e4SLinus Torvalds 	}
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
28461da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
28471da177e4SLinus Torvalds 	udph->len = htons(datalen + sizeof(struct udphdr));
28481da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
28491da177e4SLinus Torvalds 
2850d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
28511da177e4SLinus Torvalds 
28521ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
28531ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
2854252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
28551ca7768cSFrancesco Fondelli 	}
28561ca7768cSFrancesco Fondelli 
28571da177e4SLinus Torvalds 	iph->hop_limit = 32;
28581da177e4SLinus Torvalds 
28591da177e4SLinus Torvalds 	iph->payload_len = htons(sizeof(struct udphdr) + datalen);
28601da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
28611da177e4SLinus Torvalds 
28624e3fd7a0SAlexey Dobriyan 	iph->daddr = pkt_dev->cur_in6_daddr;
28634e3fd7a0SAlexey Dobriyan 	iph->saddr = pkt_dev->cur_in6_saddr;
28641da177e4SLinus Torvalds 
2865b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
286616dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
2867ca6549afSSteven Whitehouse 	skb->protocol = protocol;
28681da177e4SLinus Torvalds 	skb->dev = odev;
28691da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
28701da177e4SLinus Torvalds 
287126ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds 	return skb;
28741da177e4SLinus Torvalds }
28751da177e4SLinus Torvalds 
2876475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
28771da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
28781da177e4SLinus Torvalds {
28791da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
28801da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
28811da177e4SLinus Torvalds 	else
28821da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
28831da177e4SLinus Torvalds }
28841da177e4SLinus Torvalds 
28851da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
28861da177e4SLinus Torvalds {
28871da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
28881da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
28891da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
28901da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
28911da177e4SLinus Torvalds 	pkt_dev->errors = 0;
28921da177e4SLinus Torvalds }
28931da177e4SLinus Torvalds 
28941da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
28951da177e4SLinus Torvalds 
28961da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
28971da177e4SLinus Torvalds {
2898c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
28991da177e4SLinus Torvalds 	int started = 0;
29001da177e4SLinus Torvalds 
2901f9467eaeSJoe Perches 	func_enter();
29021da177e4SLinus Torvalds 
29031da177e4SLinus Torvalds 	if_lock(t);
2904c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
29051da177e4SLinus Torvalds 
29061da177e4SLinus Torvalds 		/*
29071da177e4SLinus Torvalds 		 * setup odev and create initial packet.
29081da177e4SLinus Torvalds 		 */
29091da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
29101da177e4SLinus Torvalds 
29111da177e4SLinus Torvalds 		if (pkt_dev->odev) {
29121da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
29131da177e4SLinus Torvalds 			pkt_dev->running = 1;	/* Cranke yeself! */
29141da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
2915398f382cSDaniel Borkmann 			pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
2916fd29cf72SStephen Hemminger 
291716dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
29201da177e4SLinus Torvalds 			started++;
2921222f1806SLuiz Capitulino 		} else
29221da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
29231da177e4SLinus Torvalds 	}
29241da177e4SLinus Torvalds 	if_unlock(t);
2925222f1806SLuiz Capitulino 	if (started)
2926222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
29271da177e4SLinus Torvalds }
29281da177e4SLinus Torvalds 
2929*4e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
29301da177e4SLinus Torvalds {
2931cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
29321da177e4SLinus Torvalds 
2933f9467eaeSJoe Perches 	func_enter();
29341da177e4SLinus Torvalds 
29356146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
2936cdcdbe0bSLuiz Capitulino 
2937*4e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
293895ed63f7SArthur Kepner 		t->control |= T_STOP;
2939cdcdbe0bSLuiz Capitulino 
29406146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds 
2943648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
29441da177e4SLinus Torvalds {
2945648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
29461da177e4SLinus Torvalds 
2947c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
2948648fda74SStephen Hemminger 		if (pkt_dev->running)
2949648fda74SStephen Hemminger 			return 1;
2950648fda74SStephen Hemminger 	return 0;
29511da177e4SLinus Torvalds }
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
29541da177e4SLinus Torvalds {
29551da177e4SLinus Torvalds 	if_lock(t);
29561da177e4SLinus Torvalds 
29571da177e4SLinus Torvalds 	while (thread_is_running(t)) {
29581da177e4SLinus Torvalds 
29591da177e4SLinus Torvalds 		if_unlock(t);
29601da177e4SLinus Torvalds 
29611da177e4SLinus Torvalds 		msleep_interruptible(100);
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds 		if (signal_pending(current))
29641da177e4SLinus Torvalds 			goto signal;
29651da177e4SLinus Torvalds 		if_lock(t);
29661da177e4SLinus Torvalds 	}
29671da177e4SLinus Torvalds 	if_unlock(t);
29681da177e4SLinus Torvalds 	return 1;
29691da177e4SLinus Torvalds signal:
29701da177e4SLinus Torvalds 	return 0;
29711da177e4SLinus Torvalds }
29721da177e4SLinus Torvalds 
2973*4e58a027SCong Wang static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
29741da177e4SLinus Torvalds {
2975cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
29761da177e4SLinus Torvalds 	int sig = 1;
29771da177e4SLinus Torvalds 
29786146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
2979cdcdbe0bSLuiz Capitulino 
2980*4e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
29811da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
2982222f1806SLuiz Capitulino 		if (sig == 0)
2983222f1806SLuiz Capitulino 			break;
29841da177e4SLinus Torvalds 	}
2985cdcdbe0bSLuiz Capitulino 
2986cdcdbe0bSLuiz Capitulino 	if (sig == 0)
2987*4e58a027SCong Wang 		list_for_each_entry(t, &pn->pktgen_threads, th_list)
29881da177e4SLinus Torvalds 			t->control |= (T_STOP);
2989cdcdbe0bSLuiz Capitulino 
29906146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
29911da177e4SLinus Torvalds 	return sig;
29921da177e4SLinus Torvalds }
29931da177e4SLinus Torvalds 
2994*4e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn)
29951da177e4SLinus Torvalds {
2996cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
29971da177e4SLinus Torvalds 
2998f9467eaeSJoe Perches 	func_enter();
29991da177e4SLinus Torvalds 
30006146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
30011da177e4SLinus Torvalds 
3002*4e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
30031da177e4SLinus Torvalds 		t->control |= (T_RUN);
3004cdcdbe0bSLuiz Capitulino 
30056146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
30061da177e4SLinus Torvalds 
300763adc6fbSStephen Hemminger 	/* Propagate thread->control  */
300863adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
30091da177e4SLinus Torvalds 
3010*4e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
30111da177e4SLinus Torvalds }
30121da177e4SLinus Torvalds 
3013*4e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn)
3014eb37b41cSJesse Brandeburg {
3015eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3016eb37b41cSJesse Brandeburg 
3017f9467eaeSJoe Perches 	func_enter();
3018eb37b41cSJesse Brandeburg 
3019eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3020eb37b41cSJesse Brandeburg 
3021*4e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
3022eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3023eb37b41cSJesse Brandeburg 
3024eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3025eb37b41cSJesse Brandeburg 
302663adc6fbSStephen Hemminger 	/* Propagate thread->control  */
302763adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3028eb37b41cSJesse Brandeburg 
3029*4e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
3030eb37b41cSJesse Brandeburg }
3031eb37b41cSJesse Brandeburg 
30321da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
30331da177e4SLinus Torvalds {
3034fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
30351da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3036fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3037fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3038fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
30391da177e4SLinus Torvalds 
304003a14ab1SDaniel Turull 	p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3041fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3042fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3043fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
30441da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
30451da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
30461da177e4SLinus Torvalds 
3047fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3048fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
30491da177e4SLinus Torvalds 
30501da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds 	mbps = bps;
30531da177e4SLinus Torvalds 	do_div(mbps, 1000000);
30541da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
30551da177e4SLinus Torvalds 		     (unsigned long long)pps,
30561da177e4SLinus Torvalds 		     (unsigned long long)mbps,
30571da177e4SLinus Torvalds 		     (unsigned long long)bps,
30581da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
30591da177e4SLinus Torvalds }
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
30621da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
30631da177e4SLinus Torvalds {
3064222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
30651da177e4SLinus Torvalds 
30661da177e4SLinus Torvalds 	if (!pkt_dev->running) {
3067f9467eaeSJoe Perches 		pr_warning("interface: %s is already stopped\n",
3068f9467eaeSJoe Perches 			   pkt_dev->odevname);
30691da177e4SLinus Torvalds 		return -EINVAL;
30701da177e4SLinus Torvalds 	}
30711da177e4SLinus Torvalds 
30723bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
30733bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3074398f382cSDaniel Borkmann 	pkt_dev->stopped_at = ktime_get();
30751da177e4SLinus Torvalds 	pkt_dev->running = 0;
30761da177e4SLinus Torvalds 
307795ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
30781da177e4SLinus Torvalds 
30791da177e4SLinus Torvalds 	return 0;
30801da177e4SLinus Torvalds }
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
30831da177e4SLinus Torvalds {
3084c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds 	if_lock(t);
30871da177e4SLinus Torvalds 
3088c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3089c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3090222f1806SLuiz Capitulino 			continue;
3091222f1806SLuiz Capitulino 		if (best == NULL)
3092c26a8016SLuiz Capitulino 			best = pkt_dev;
3093398f382cSDaniel Borkmann 		else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3094c26a8016SLuiz Capitulino 			best = pkt_dev;
30951da177e4SLinus Torvalds 	}
30961da177e4SLinus Torvalds 	if_unlock(t);
30971da177e4SLinus Torvalds 	return best;
30981da177e4SLinus Torvalds }
30991da177e4SLinus Torvalds 
3100222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3101222f1806SLuiz Capitulino {
3102c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
31031da177e4SLinus Torvalds 
3104f9467eaeSJoe Perches 	func_enter();
31051da177e4SLinus Torvalds 
31061da177e4SLinus Torvalds 	if_lock(t);
31071da177e4SLinus Torvalds 
3108c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3109c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
311095ed63f7SArthur Kepner 	}
311195ed63f7SArthur Kepner 
311295ed63f7SArthur Kepner 	if_unlock(t);
311395ed63f7SArthur Kepner }
311495ed63f7SArthur Kepner 
311595ed63f7SArthur Kepner /*
311695ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
311795ed63f7SArthur Kepner  * and remove it
311895ed63f7SArthur Kepner  */
311995ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
312095ed63f7SArthur Kepner {
3121c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3122c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
312395ed63f7SArthur Kepner 
3124f9467eaeSJoe Perches 	func_enter();
312595ed63f7SArthur Kepner 
312695ed63f7SArthur Kepner 	if_lock(t);
312795ed63f7SArthur Kepner 
3128c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3129c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
313095ed63f7SArthur Kepner 
3131222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3132222f1806SLuiz Capitulino 			continue;
313395ed63f7SArthur Kepner 
313495ed63f7SArthur Kepner 		kfree_skb(cur->skb);
313595ed63f7SArthur Kepner 		cur->skb = NULL;
313695ed63f7SArthur Kepner 
313795ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
313895ed63f7SArthur Kepner 
313995ed63f7SArthur Kepner 		break;
314095ed63f7SArthur Kepner 	}
31411da177e4SLinus Torvalds 
31421da177e4SLinus Torvalds 	if_unlock(t);
31431da177e4SLinus Torvalds }
31441da177e4SLinus Torvalds 
31451da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
31461da177e4SLinus Torvalds {
3147c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3148c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
31491da177e4SLinus Torvalds 
3150f9467eaeSJoe Perches 	func_enter();
3151f9467eaeSJoe Perches 
31521da177e4SLinus Torvalds 	/* Remove all devices, free mem */
31531da177e4SLinus Torvalds 
31541da177e4SLinus Torvalds 	if_lock(t);
31551da177e4SLinus Torvalds 
3156c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3157c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
315895ed63f7SArthur Kepner 
315995ed63f7SArthur Kepner 		kfree_skb(cur->skb);
316095ed63f7SArthur Kepner 		cur->skb = NULL;
316195ed63f7SArthur Kepner 
31621da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
31631da177e4SLinus Torvalds 	}
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 	if_unlock(t);
31661da177e4SLinus Torvalds }
31671da177e4SLinus Torvalds 
31681da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
31691da177e4SLinus Torvalds {
31701da177e4SLinus Torvalds 	/* Remove from the thread list */
3171*4e58a027SCong Wang 	remove_proc_entry(t->tsk->comm, t->net->proc_dir);
31721da177e4SLinus Torvalds }
31731da177e4SLinus Torvalds 
3174ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
31753791decbSStephen Hemminger {
3176398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
31773791decbSStephen Hemminger 	schedule();
3178398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
31793791decbSStephen Hemminger }
31803791decbSStephen Hemminger 
3181ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3182ef87979cSStephen Hemminger {
3183398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
3184ef87979cSStephen Hemminger 
3185ef87979cSStephen Hemminger 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3186ef87979cSStephen Hemminger 		if (signal_pending(current))
3187ef87979cSStephen Hemminger 			break;
3188ef87979cSStephen Hemminger 
3189ef87979cSStephen Hemminger 		if (need_resched())
3190ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3191ef87979cSStephen Hemminger 		else
3192ef87979cSStephen Hemminger 			cpu_relax();
3193ef87979cSStephen Hemminger 	}
3194398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3195ef87979cSStephen Hemminger }
3196fd29cf72SStephen Hemminger 
3197475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
31981da177e4SLinus Torvalds {
319900829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
32006fef4c0cSStephen Hemminger 	netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
320100829823SStephen Hemminger 		= odev->netdev_ops->ndo_start_xmit;
3202fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
3203fd2ea0a7SDavid S. Miller 	u16 queue_map;
32041da177e4SLinus Torvalds 	int ret;
32051da177e4SLinus Torvalds 
3206ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3207ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
32081da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
32093791decbSStephen Hemminger 		return;
32101da177e4SLinus Torvalds 	}
32111da177e4SLinus Torvalds 
3212ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3213ef87979cSStephen Hemminger 	 * "never transmit"
3214ef87979cSStephen Hemminger 	 */
3215ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3216398f382cSDaniel Borkmann 		pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3217ef87979cSStephen Hemminger 		return;
3218ef87979cSStephen Hemminger 	}
3219ef87979cSStephen Hemminger 
3220ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
32217d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
32227d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
32231da177e4SLinus Torvalds 		/* build a new pkt */
32241da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
32251da177e4SLinus Torvalds 
32261da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
32271da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
3228f9467eaeSJoe Perches 			pr_err("ERROR: couldn't allocate skb in fill_packet\n");
32291da177e4SLinus Torvalds 			schedule();
32301da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
32313791decbSStephen Hemminger 			return;
32321da177e4SLinus Torvalds 		}
3233baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
32341da177e4SLinus Torvalds 		pkt_dev->allocated_skbs++;
32351da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
32361da177e4SLinus Torvalds 	}
32371da177e4SLinus Torvalds 
3238ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3239ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3240ef87979cSStephen Hemminger 
3241fd2ea0a7SDavid S. Miller 	queue_map = skb_get_queue_mapping(pkt_dev->skb);
3242fd2ea0a7SDavid S. Miller 	txq = netdev_get_tx_queue(odev, queue_map);
3243fd2ea0a7SDavid S. Miller 
3244fd2ea0a7SDavid S. Miller 	__netif_tx_lock_bh(txq);
32455b8db2f5SStephen Hemminger 
324673466498STom Herbert 	if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
3247ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
32480835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
32490835acfeSEric Dumazet 		goto unlock;
32500835acfeSEric Dumazet 	}
32510835acfeSEric Dumazet 	atomic_inc(&(pkt_dev->skb->users));
325200829823SStephen Hemminger 	ret = (*xmit)(pkt_dev->skb, odev);
3253ef87979cSStephen Hemminger 
32545b8db2f5SStephen Hemminger 	switch (ret) {
32555b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
325608baf561SEric Dumazet 		txq_trans_update(txq);
32571da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
32581da177e4SLinus Torvalds 		pkt_dev->sofar++;
32591da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3260baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
32615b8db2f5SStephen Hemminger 		break;
3262f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3263f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3264f466dba1SJohn Fastabend 	case NET_XMIT_POLICED:
3265f466dba1SJohn Fastabend 		/* skb has been consumed */
3266f466dba1SJohn Fastabend 		pkt_dev->errors++;
3267f466dba1SJohn Fastabend 		break;
32685b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
3269e87cc472SJoe Perches 		net_info_ratelimited("%s xmit error: %d\n",
3270e87cc472SJoe Perches 				     pkt_dev->odevname, ret);
32711da177e4SLinus Torvalds 		pkt_dev->errors++;
32725b8db2f5SStephen Hemminger 		/* fallthru */
3273ef87979cSStephen Hemminger 	case NETDEV_TX_LOCKED:
32745b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
32755b8db2f5SStephen Hemminger 		/* Retry it next time */
32765b8db2f5SStephen Hemminger 		atomic_dec(&(pkt_dev->skb->users));
32771da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
32781da177e4SLinus Torvalds 	}
32790835acfeSEric Dumazet unlock:
3280fd2ea0a7SDavid S. Miller 	__netif_tx_unlock_bh(txq);
32811da177e4SLinus Torvalds 
32821da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
32831da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3284ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
32851da177e4SLinus Torvalds 
32861da177e4SLinus Torvalds 		/* Done with this */
32871da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
32881da177e4SLinus Torvalds 	}
32891da177e4SLinus Torvalds }
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds /*
32921da177e4SLinus Torvalds  * Main loop of the thread goes here
32931da177e4SLinus Torvalds  */
32941da177e4SLinus Torvalds 
3295ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
32961da177e4SLinus Torvalds {
32971da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3298ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
32991da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
33001da177e4SLinus Torvalds 	int cpu = t->cpu;
33011da177e4SLinus Torvalds 
3302ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
33031da177e4SLinus Torvalds 
33041da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3305d3ede327SDenis V. Lunev 	complete(&t->start_done);
33061da177e4SLinus Torvalds 
3307f9467eaeSJoe Perches 	pr_debug("starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
33081da177e4SLinus Torvalds 
3309ee74baa7SDavid S. Miller 	set_current_state(TASK_INTERRUPTIBLE);
33101da177e4SLinus Torvalds 
331183144186SRafael J. Wysocki 	set_freezable();
331283144186SRafael J. Wysocki 
3313ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3314ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3315ee74baa7SDavid S. Miller 
3316ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
3317*4e58a027SCong Wang 			if (t->net->pktgen_exiting)
3318551eaff1SEric Dumazet 				break;
3319ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3320ef87979cSStephen Hemminger 							 t->control != 0,
3321ef87979cSStephen Hemminger 							 HZ/10);
33221b3f720bSRafael J. Wysocki 			try_to_freeze();
3323ef87979cSStephen Hemminger 			continue;
3324ee74baa7SDavid S. Miller 		}
33251da177e4SLinus Torvalds 
33261da177e4SLinus Torvalds 		__set_current_state(TASK_RUNNING);
33271da177e4SLinus Torvalds 
3328ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
33291da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
33301da177e4SLinus Torvalds 
3331ef87979cSStephen Hemminger 			if (need_resched())
3332ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3333ef87979cSStephen Hemminger 			else
3334ef87979cSStephen Hemminger 				cpu_relax();
3335ef87979cSStephen Hemminger 		}
3336ef87979cSStephen Hemminger 
33371da177e4SLinus Torvalds 		if (t->control & T_STOP) {
33381da177e4SLinus Torvalds 			pktgen_stop(t);
33391da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
33401da177e4SLinus Torvalds 		}
33411da177e4SLinus Torvalds 
33421da177e4SLinus Torvalds 		if (t->control & T_RUN) {
33431da177e4SLinus Torvalds 			pktgen_run(t);
33441da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
33451da177e4SLinus Torvalds 		}
33461da177e4SLinus Torvalds 
334795ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
33481da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
334995ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
335095ed63f7SArthur Kepner 		}
335195ed63f7SArthur Kepner 
335295ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
335395ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
33541da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
33551da177e4SLinus Torvalds 		}
33561da177e4SLinus Torvalds 
335709fe3ef4SAndrew Morton 		try_to_freeze();
335809fe3ef4SAndrew Morton 
3359ee74baa7SDavid S. Miller 		set_current_state(TASK_INTERRUPTIBLE);
33601da177e4SLinus Torvalds 	}
33611da177e4SLinus Torvalds 
3362f9467eaeSJoe Perches 	pr_debug("%s stopping all device\n", t->tsk->comm);
33631da177e4SLinus Torvalds 	pktgen_stop(t);
33641da177e4SLinus Torvalds 
3365f9467eaeSJoe Perches 	pr_debug("%s removing all device\n", t->tsk->comm);
33661da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
33671da177e4SLinus Torvalds 
3368f9467eaeSJoe Perches 	pr_debug("%s removing thread\n", t->tsk->comm);
33691da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3370cdcdbe0bSLuiz Capitulino 
3371551eaff1SEric Dumazet 	/* Wait for kthread_stop */
3372551eaff1SEric Dumazet 	while (!kthread_should_stop()) {
3373551eaff1SEric Dumazet 		set_current_state(TASK_INTERRUPTIBLE);
3374551eaff1SEric Dumazet 		schedule();
3375551eaff1SEric Dumazet 	}
3376551eaff1SEric Dumazet 	__set_current_state(TASK_RUNNING);
3377551eaff1SEric Dumazet 
3378ee74baa7SDavid S. Miller 	return 0;
33791da177e4SLinus Torvalds }
33801da177e4SLinus Torvalds 
3381222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
33823e984840SEric Dumazet 					  const char *ifname, bool exact)
33831da177e4SLinus Torvalds {
3384c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
33853e984840SEric Dumazet 	size_t len = strlen(ifname);
33861da177e4SLinus Torvalds 
33873e984840SEric Dumazet 	if_lock(t);
3388c26a8016SLuiz Capitulino 	list_for_each_entry(p, &t->if_list, list)
33893e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
33903e984840SEric Dumazet 			if (p->odevname[len]) {
33913e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
33923e984840SEric Dumazet 					continue;
33933e984840SEric Dumazet 			}
3394c26a8016SLuiz Capitulino 			pkt_dev = p;
33951da177e4SLinus Torvalds 			break;
33961da177e4SLinus Torvalds 		}
33971da177e4SLinus Torvalds 
33981da177e4SLinus Torvalds 	if_unlock(t);
3399f9467eaeSJoe Perches 	pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
34001da177e4SLinus Torvalds 	return pkt_dev;
34011da177e4SLinus Torvalds }
34021da177e4SLinus Torvalds 
34031da177e4SLinus Torvalds /*
34041da177e4SLinus Torvalds  * Adds a dev at front of if_list.
34051da177e4SLinus Torvalds  */
34061da177e4SLinus Torvalds 
3407222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3408222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
34091da177e4SLinus Torvalds {
34101da177e4SLinus Torvalds 	int rv = 0;
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds 	if_lock(t);
34131da177e4SLinus Torvalds 
34141da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
3415f9467eaeSJoe Perches 		pr_err("ERROR: already assigned to a thread\n");
34161da177e4SLinus Torvalds 		rv = -EBUSY;
34171da177e4SLinus Torvalds 		goto out;
34181da177e4SLinus Torvalds 	}
3419c26a8016SLuiz Capitulino 
3420c26a8016SLuiz Capitulino 	list_add(&pkt_dev->list, &t->if_list);
34211da177e4SLinus Torvalds 	pkt_dev->pg_thread = t;
34221da177e4SLinus Torvalds 	pkt_dev->running = 0;
34231da177e4SLinus Torvalds 
34241da177e4SLinus Torvalds out:
34251da177e4SLinus Torvalds 	if_unlock(t);
34261da177e4SLinus Torvalds 	return rv;
34271da177e4SLinus Torvalds }
34281da177e4SLinus Torvalds 
34291da177e4SLinus Torvalds /* Called under thread lock */
34301da177e4SLinus Torvalds 
34311da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
34321da177e4SLinus Torvalds {
34331da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
343439df232fSStephen Hemminger 	int err;
34353291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
34361da177e4SLinus Torvalds 
34371da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
34381da177e4SLinus Torvalds 
3439*4e58a027SCong Wang 	pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3440d50a6b56SStephen Hemminger 	if (pkt_dev) {
3441f9467eaeSJoe Perches 		pr_err("ERROR: interface already used\n");
3442d50a6b56SStephen Hemminger 		return -EBUSY;
3443d50a6b56SStephen Hemminger 	}
34441da177e4SLinus Torvalds 
34453291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
34461da177e4SLinus Torvalds 	if (!pkt_dev)
34471da177e4SLinus Torvalds 		return -ENOMEM;
34481da177e4SLinus Torvalds 
3449593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
345068d5ac2eSWANG Cong 	pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
34513291b9dbSEric Dumazet 				      node);
34521da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
34531da177e4SLinus Torvalds 		kfree(pkt_dev);
34541da177e4SLinus Torvalds 		return -ENOMEM;
34551da177e4SLinus Torvalds 	}
34561da177e4SLinus Torvalds 
345795ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
34581da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
3459fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
34601da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
34611da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
34621da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
34631da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
34641da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
34651da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
346634954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
346734954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
346834954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
346934954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
347034954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
347134954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
3472e99b99b4SRobert Olsson 	pkt_dev->node = -1;
347334954ddcSFrancesco Fondelli 
3474*4e58a027SCong Wang 	err = pktgen_setup_dev(t->net, pkt_dev, ifname);
347539df232fSStephen Hemminger 	if (err)
347639df232fSStephen Hemminger 		goto out1;
3477d8873315SNeil Horman 	if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3478d8873315SNeil Horman 		pkt_dev->clone_skb = pg_clone_skb_d;
34791da177e4SLinus Torvalds 
3480*4e58a027SCong Wang 	pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
34815efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
348239df232fSStephen Hemminger 	if (!pkt_dev->entry) {
3483f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3484d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
348539df232fSStephen Hemminger 		err = -EINVAL;
348639df232fSStephen Hemminger 		goto out2;
348739df232fSStephen Hemminger 	}
3488a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3489a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3490a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3491a553e4a6SJamal Hadi Salim #endif
349239df232fSStephen Hemminger 
349339df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
349439df232fSStephen Hemminger out2:
349539df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
349639df232fSStephen Hemminger out1:
3497a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3498a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3499a553e4a6SJamal Hadi Salim #endif
35001da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
35011da177e4SLinus Torvalds 	kfree(pkt_dev);
350239df232fSStephen Hemminger 	return err;
35031da177e4SLinus Torvalds }
35041da177e4SLinus Torvalds 
3505*4e58a027SCong Wang static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
35061da177e4SLinus Torvalds {
3507cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3508d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3509ee74baa7SDavid S. Miller 	struct task_struct *p;
35101da177e4SLinus Torvalds 
35113291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
35123291b9dbSEric Dumazet 			 cpu_to_node(cpu));
35131da177e4SLinus Torvalds 	if (!t) {
3514f9467eaeSJoe Perches 		pr_err("ERROR: out of memory, can't create new thread\n");
35151da177e4SLinus Torvalds 		return -ENOMEM;
35161da177e4SLinus Torvalds 	}
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds 	spin_lock_init(&t->if_lock);
35191da177e4SLinus Torvalds 	t->cpu = cpu;
35201da177e4SLinus Torvalds 
3521ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3522ee74baa7SDavid S. Miller 
3523*4e58a027SCong Wang 	list_add_tail(&t->th_list, &pn->pktgen_threads);
3524d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3525ee74baa7SDavid S. Miller 
352694dcf29aSEric Dumazet 	p = kthread_create_on_node(pktgen_thread_worker,
352794dcf29aSEric Dumazet 				   t,
352894dcf29aSEric Dumazet 				   cpu_to_node(cpu),
352994dcf29aSEric Dumazet 				   "kpktgend_%d", cpu);
3530ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
3531f9467eaeSJoe Perches 		pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3532ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3533ee74baa7SDavid S. Miller 		kfree(t);
3534ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3535ee74baa7SDavid S. Miller 	}
3536ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3537ee74baa7SDavid S. Miller 	t->tsk = p;
3538ee74baa7SDavid S. Miller 
3539*4e58a027SCong Wang 	pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
35405efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3541d50a6b56SStephen Hemminger 	if (!pe) {
3542f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3543ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3544ee74baa7SDavid S. Miller 		kthread_stop(p);
3545ee74baa7SDavid S. Miller 		list_del(&t->th_list);
35461da177e4SLinus Torvalds 		kfree(t);
35471da177e4SLinus Torvalds 		return -EINVAL;
35481da177e4SLinus Torvalds 	}
3549d50a6b56SStephen Hemminger 
3550*4e58a027SCong Wang 	t->net = pn;
3551ee74baa7SDavid S. Miller 	wake_up_process(p);
3552d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
35531da177e4SLinus Torvalds 
35541da177e4SLinus Torvalds 	return 0;
35551da177e4SLinus Torvalds }
35561da177e4SLinus Torvalds 
35571da177e4SLinus Torvalds /*
35581da177e4SLinus Torvalds  * Removes a device from the thread if_list.
35591da177e4SLinus Torvalds  */
3560222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3561222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
35621da177e4SLinus Torvalds {
3563c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3564c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
35651da177e4SLinus Torvalds 
3566c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3567c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3568c26a8016SLuiz Capitulino 		if (p == pkt_dev)
3569c26a8016SLuiz Capitulino 			list_del(&p->list);
35701da177e4SLinus Torvalds 	}
35711da177e4SLinus Torvalds }
35721da177e4SLinus Torvalds 
3573222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3574222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
35751da177e4SLinus Torvalds {
3576*4e58a027SCong Wang 	struct pktgen_net *pn = t->net;
35771da177e4SLinus Torvalds 
3578f9467eaeSJoe Perches 	pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
35791da177e4SLinus Torvalds 
35801da177e4SLinus Torvalds 	if (pkt_dev->running) {
3581f9467eaeSJoe Perches 		pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
35821da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
35831da177e4SLinus Torvalds 	}
35841da177e4SLinus Torvalds 
35851da177e4SLinus Torvalds 	/* Dis-associate from the interface */
35861da177e4SLinus Torvalds 
35871da177e4SLinus Torvalds 	if (pkt_dev->odev) {
35881da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
35891da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
35901da177e4SLinus Torvalds 	}
35911da177e4SLinus Torvalds 
35921da177e4SLinus Torvalds 	/* And update the thread if_list */
35931da177e4SLinus Torvalds 
35941da177e4SLinus Torvalds 	_rem_dev_from_if_list(t, pkt_dev);
35951da177e4SLinus Torvalds 
359639df232fSStephen Hemminger 	if (pkt_dev->entry)
3597*4e58a027SCong Wang 		remove_proc_entry(pkt_dev->entry->name, pn->proc_dir);
35981da177e4SLinus Torvalds 
3599a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3600a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3601a553e4a6SJamal Hadi Salim #endif
36021da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
360326ad7879SEric Dumazet 	if (pkt_dev->page)
360426ad7879SEric Dumazet 		put_page(pkt_dev->page);
36051da177e4SLinus Torvalds 	kfree(pkt_dev);
36061da177e4SLinus Torvalds 	return 0;
36071da177e4SLinus Torvalds }
36081da177e4SLinus Torvalds 
3609*4e58a027SCong Wang static int __net_init pg_net_init(struct net *net)
36101da177e4SLinus Torvalds {
3611*4e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3612d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3613*4e58a027SCong Wang 	int cpu, ret = 0;
3614d50a6b56SStephen Hemminger 
3615*4e58a027SCong Wang 	pn->net = net;
3616*4e58a027SCong Wang 	INIT_LIST_HEAD(&pn->pktgen_threads);
3617*4e58a027SCong Wang 	pn->pktgen_exiting = false;
3618*4e58a027SCong Wang 	pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3619*4e58a027SCong Wang 	if (!pn->proc_dir) {
3620*4e58a027SCong Wang 		pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3621d50a6b56SStephen Hemminger 		return -ENODEV;
36221da177e4SLinus Torvalds 	}
3623*4e58a027SCong Wang 	pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3624*4e58a027SCong Wang 	if (pe == NULL) {
3625*4e58a027SCong Wang 		pr_err("cannot create %s procfs entry\n", PGCTRL);
3626*4e58a027SCong Wang 		ret = -EINVAL;
3627*4e58a027SCong Wang 		goto remove;
3628*4e58a027SCong Wang 	}
36291da177e4SLinus Torvalds 
3630670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
36318024bb24SLuiz Capitulino 		int err;
36321da177e4SLinus Torvalds 
3633*4e58a027SCong Wang 		err = pktgen_create_thread(cpu, pn);
36348024bb24SLuiz Capitulino 		if (err)
3635*4e58a027SCong Wang 			pr_warn("Cannot create thread for cpu %d (%d)\n",
3636f9467eaeSJoe Perches 				   cpu, err);
36371da177e4SLinus Torvalds 	}
36388024bb24SLuiz Capitulino 
3639*4e58a027SCong Wang 	if (list_empty(&pn->pktgen_threads)) {
3640*4e58a027SCong Wang 		pr_err("Initialization failed for all threads\n");
3641ce14f894SWANG Cong 		ret = -ENODEV;
3642*4e58a027SCong Wang 		goto remove_entry;
36438024bb24SLuiz Capitulino 	}
36448024bb24SLuiz Capitulino 
36451da177e4SLinus Torvalds 	return 0;
3646ce14f894SWANG Cong 
3647*4e58a027SCong Wang remove_entry:
3648*4e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
3649*4e58a027SCong Wang remove:
3650*4e58a027SCong Wang 	proc_net_remove(pn->net, PG_PROC_DIR);
3651ce14f894SWANG Cong 	return ret;
36521da177e4SLinus Torvalds }
36531da177e4SLinus Torvalds 
3654*4e58a027SCong Wang static void __net_exit pg_net_exit(struct net *net)
36551da177e4SLinus Torvalds {
3656*4e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3657cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3658cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
3659d4b11335SEric Dumazet 	LIST_HEAD(list);
36601da177e4SLinus Torvalds 
36611da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
3662*4e58a027SCong Wang 	pn->pktgen_exiting = true;
36631da177e4SLinus Torvalds 
3664c57b5468SEric Dumazet 	mutex_lock(&pktgen_thread_lock);
3665*4e58a027SCong Wang 	list_splice_init(&pn->pktgen_threads, &list);
3666c57b5468SEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
3667c57b5468SEric Dumazet 
3668c57b5468SEric Dumazet 	list_for_each_safe(q, n, &list) {
3669cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3670c57b5468SEric Dumazet 		list_del(&t->th_list);
3671ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
3672ee74baa7SDavid S. Miller 		kfree(t);
36731da177e4SLinus Torvalds 	}
36741da177e4SLinus Torvalds 
3675*4e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
3676*4e58a027SCong Wang 	proc_net_remove(pn->net, PG_PROC_DIR);
3677*4e58a027SCong Wang }
36781da177e4SLinus Torvalds 
3679*4e58a027SCong Wang static struct pernet_operations pg_net_ops = {
3680*4e58a027SCong Wang 	.init = pg_net_init,
3681*4e58a027SCong Wang 	.exit = pg_net_exit,
3682*4e58a027SCong Wang 	.id   = &pg_net_id,
3683*4e58a027SCong Wang 	.size = sizeof(struct pktgen_net),
3684*4e58a027SCong Wang };
3685*4e58a027SCong Wang 
3686*4e58a027SCong Wang static int __init pg_init(void)
3687*4e58a027SCong Wang {
3688*4e58a027SCong Wang 	int ret = 0;
3689*4e58a027SCong Wang 
3690*4e58a027SCong Wang 	pr_info("%s", version);
3691*4e58a027SCong Wang 	ret = register_pernet_subsys(&pg_net_ops);
3692*4e58a027SCong Wang 	if (ret)
3693*4e58a027SCong Wang 		return ret;
3694*4e58a027SCong Wang 	ret = register_netdevice_notifier(&pktgen_notifier_block);
3695*4e58a027SCong Wang 	if (ret)
3696*4e58a027SCong Wang 		unregister_pernet_subsys(&pg_net_ops);
3697*4e58a027SCong Wang 
3698*4e58a027SCong Wang 	return ret;
3699*4e58a027SCong Wang }
3700*4e58a027SCong Wang 
3701*4e58a027SCong Wang static void __exit pg_cleanup(void)
3702*4e58a027SCong Wang {
3703*4e58a027SCong Wang 	unregister_netdevice_notifier(&pktgen_notifier_block);
3704*4e58a027SCong Wang 	unregister_pernet_subsys(&pg_net_ops);
37051da177e4SLinus Torvalds }
37061da177e4SLinus Torvalds 
37071da177e4SLinus Torvalds module_init(pg_init);
37081da177e4SLinus Torvalds module_exit(pg_cleanup);
37091da177e4SLinus Torvalds 
3710c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
37111da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
37121da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3713c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
37141da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3715c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
37161da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3717c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
37181da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3719c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
37201da177e4SLinus Torvalds module_param(debug, int, 0);
3721c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3722