xref: /openbmc/linux/net/core/pktgen.c (revision 398f382c0a5bd62f0f31871e844700e1b9fd1ad3)
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
1671da177e4SLinus Torvalds #include <asm/byteorder.h>
1681da177e4SLinus Torvalds #include <linux/rcupdate.h>
1691977f032SJiri Slaby #include <linux/bitops.h>
17063adc6fbSStephen Hemminger #include <linux/io.h>
17163adc6fbSStephen Hemminger #include <linux/timex.h>
17263adc6fbSStephen Hemminger #include <linux/uaccess.h>
1731da177e4SLinus Torvalds #include <asm/dma.h>
1741da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1751da177e4SLinus Torvalds 
17643d28b65SDaniel Turull #define VERSION	"2.74"
1771da177e4SLinus Torvalds #define IP_NAME_SZ 32
178ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
179d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1801da177e4SLinus Torvalds 
181f9467eaeSJoe Perches #define func_enter() pr_debug("entering %s\n", __func__);
182f9467eaeSJoe Perches 
1831da177e4SLinus Torvalds /* Device flag bits */
1841da177e4SLinus Torvalds #define F_IPSRC_RND   (1<<0)	/* IP-Src Random  */
1851da177e4SLinus Torvalds #define F_IPDST_RND   (1<<1)	/* IP-Dst Random  */
1861da177e4SLinus Torvalds #define F_UDPSRC_RND  (1<<2)	/* UDP-Src Random */
1871da177e4SLinus Torvalds #define F_UDPDST_RND  (1<<3)	/* UDP-Dst Random */
1881da177e4SLinus Torvalds #define F_MACSRC_RND  (1<<4)	/* MAC-Src Random */
1891da177e4SLinus Torvalds #define F_MACDST_RND  (1<<5)	/* MAC-Dst Random */
1901da177e4SLinus Torvalds #define F_TXSIZE_RND  (1<<6)	/* Transmit size is random */
1911da177e4SLinus Torvalds #define F_IPV6        (1<<7)	/* Interface in IPV6 Mode */
192ca6549afSSteven Whitehouse #define F_MPLS_RND    (1<<8)	/* Random MPLS labels */
19334954ddcSFrancesco Fondelli #define F_VID_RND     (1<<9)	/* Random VLAN ID */
19434954ddcSFrancesco Fondelli #define F_SVID_RND    (1<<10)	/* Random SVLAN ID */
195007a531bSJamal Hadi Salim #define F_FLOW_SEQ    (1<<11)	/* Sequential flows */
196a553e4a6SJamal Hadi Salim #define F_IPSEC_ON    (1<<12)	/* ipsec on for flows */
19745b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13)	/* queue map Random */
198e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14)	/* queue map mirrors smp_processor_id() */
199e99b99b4SRobert Olsson #define F_NODE          (1<<15)	/* Node memory alloc*/
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /* Thread control flag bits */
2026b80d6a6SStephen Hemminger #define T_STOP        (1<<0)	/* Stop run */
2036b80d6a6SStephen Hemminger #define T_RUN         (1<<1)	/* Start run */
2046b80d6a6SStephen Hemminger #define T_REMDEVALL   (1<<2)	/* Remove all devs */
2056b80d6a6SStephen Hemminger #define T_REMDEV      (1<<3)	/* Remove one dev */
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /* If lock -- can be removed after some work */
2081da177e4SLinus Torvalds #define   if_lock(t)           spin_lock(&(t->if_lock));
2091da177e4SLinus Torvalds #define   if_unlock(t)           spin_unlock(&(t->if_lock));
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2121da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
213d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
214d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
21563adc6fbSStephen Hemminger static struct proc_dir_entry *pg_proc_dir;
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 
400551eaff1SEric Dumazet static bool pktgen_exiting __read_mostly;
401551eaff1SEric Dumazet 
4021da177e4SLinus Torvalds struct pktgen_thread {
40363adc6fbSStephen Hemminger 	spinlock_t if_lock;		/* for list of devices */
404c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
405cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
406ee74baa7SDavid S. Miller 	struct task_struct *tsk;
4071da177e4SLinus Torvalds 	char result[512];
4081da177e4SLinus Torvalds 
40963adc6fbSStephen Hemminger 	/* Field for thread to receive "posted" events terminate,
41063adc6fbSStephen Hemminger 	   stop ifs etc. */
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	u32 control;
4131da177e4SLinus Torvalds 	int cpu;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	wait_queue_head_t queue;
416d3ede327SDenis V. Lunev 	struct completion start_done;
4171da177e4SLinus Torvalds };
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds #define REMOVE 1
4201da177e4SLinus Torvalds #define FIND   0
4211da177e4SLinus Torvalds 
422c3d2f52dSStephen Hemminger static const char version[] =
423f9467eaeSJoe Perches 	"Packet Generator for packet performance testing. "
424f9467eaeSJoe Perches 	"Version: " VERSION "\n";
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4271da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
428222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
4293e984840SEric Dumazet 					  const char *ifname, bool exact);
4301da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
4311da177e4SLinus Torvalds static void pktgen_run_all_threads(void);
432eb37b41cSJesse Brandeburg static void pktgen_reset_all_threads(void);
4331da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void);
4343bda06a3SStephen Hemminger 
4351da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4361da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
43739df232fSStephen Hemminger 
4381da177e4SLinus Torvalds /* Module parameters, defaults. */
43965c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
44065c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
44165c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
44265c5b786SStephen Hemminger static int debug  __read_mostly;
4431da177e4SLinus Torvalds 
444222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
445cdcdbe0bSLuiz Capitulino static LIST_HEAD(pktgen_threads);
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4481da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4491da177e4SLinus Torvalds };
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds /*
4521da177e4SLinus Torvalds  * /proc handling functions
4531da177e4SLinus Torvalds  *
4541da177e4SLinus Torvalds  */
4551da177e4SLinus Torvalds 
456d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
457d50a6b56SStephen Hemminger {
458c3d2f52dSStephen Hemminger 	seq_puts(seq, version);
459d50a6b56SStephen Hemminger 	return 0;
460d50a6b56SStephen Hemminger }
4611da177e4SLinus Torvalds 
462d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user *buf,
4631da177e4SLinus Torvalds 			    size_t count, loff_t *ppos)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	int err = 0;
466d50a6b56SStephen Hemminger 	char data[128];
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN)) {
4691da177e4SLinus Torvalds 		err = -EPERM;
4701da177e4SLinus Torvalds 		goto out;
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 
473d50a6b56SStephen Hemminger 	if (count > sizeof(data))
474d50a6b56SStephen Hemminger 		count = sizeof(data);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count)) {
4771da177e4SLinus Torvalds 		err = -EFAULT;
478d50a6b56SStephen Hemminger 		goto out;
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 	data[count - 1] = 0;	/* Make string */
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
4831da177e4SLinus Torvalds 		pktgen_stop_all_threads_ifs();
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
4861da177e4SLinus Torvalds 		pktgen_run_all_threads();
4871da177e4SLinus Torvalds 
488eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
489eb37b41cSJesse Brandeburg 		pktgen_reset_all_threads();
490eb37b41cSJesse Brandeburg 
4911da177e4SLinus Torvalds 	else
492f9467eaeSJoe Perches 		pr_warning("Unknown command: %s\n", data);
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	err = count;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds out:
4971da177e4SLinus Torvalds 	return err;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
500d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
5011da177e4SLinus Torvalds {
502d50a6b56SStephen Hemminger 	return single_open(file, pgctrl_show, PDE(inode)->data);
503d50a6b56SStephen Hemminger }
504d50a6b56SStephen Hemminger 
5059a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
506d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
507d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
508d50a6b56SStephen Hemminger 	.read    = seq_read,
509d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
510d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
511d50a6b56SStephen Hemminger 	.release = single_release,
512d50a6b56SStephen Hemminger };
513d50a6b56SStephen Hemminger 
514d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
515d50a6b56SStephen Hemminger {
516648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
517fd29cf72SStephen Hemminger 	ktime_t stopped;
518fd29cf72SStephen Hemminger 	u64 idle;
5191da177e4SLinus Torvalds 
520222f1806SLuiz Capitulino 	seq_printf(seq,
521222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
522222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
523222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5241da177e4SLinus Torvalds 
525222f1806SLuiz Capitulino 	seq_printf(seq,
526fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
527fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
528593f63b0SEric Dumazet 		   pkt_dev->clone_skb, pkt_dev->odevname);
5291da177e4SLinus Torvalds 
530222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
531222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5321da177e4SLinus Torvalds 
53345b270f8SRobert Olsson 	seq_printf(seq,
53445b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
53545b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
53645b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
53745b270f8SRobert Olsson 
5389e50e3acSJohn Fastabend 	if (pkt_dev->skb_priority)
5399e50e3acSJohn Fastabend 		seq_printf(seq, "     skb_priority: %u\n",
5409e50e3acSJohn Fastabend 			   pkt_dev->skb_priority);
5419e50e3acSJohn Fastabend 
5421da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
543222f1806SLuiz Capitulino 		seq_printf(seq,
54447a0200dSAlexey Dobriyan 			   "     saddr: %pI6c  min_saddr: %pI6c  max_saddr: %pI6c\n"
54547a0200dSAlexey Dobriyan 			   "     daddr: %pI6c  min_daddr: %pI6c  max_daddr: %pI6c\n",
54647a0200dSAlexey Dobriyan 			   &pkt_dev->in6_saddr,
54747a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
54847a0200dSAlexey Dobriyan 			   &pkt_dev->in6_daddr,
54947a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
55063adc6fbSStephen Hemminger 	} else {
551222f1806SLuiz Capitulino 		seq_printf(seq,
55263adc6fbSStephen Hemminger 			   "     dst_min: %s  dst_max: %s\n",
55363adc6fbSStephen Hemminger 			   pkt_dev->dst_min, pkt_dev->dst_max);
55463adc6fbSStephen Hemminger 		seq_printf(seq,
55563adc6fbSStephen Hemminger 			   "        src_min: %s  src_max: %s\n",
55663adc6fbSStephen Hemminger 			   pkt_dev->src_min, pkt_dev->src_max);
55763adc6fbSStephen Hemminger 	}
5581da177e4SLinus Torvalds 
559d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5601da177e4SLinus Torvalds 
561e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
562e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
563e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5641da177e4SLinus Torvalds 
565d50a6b56SStephen Hemminger 	seq_printf(seq, "dst_mac: ");
566e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
5671da177e4SLinus Torvalds 
568222f1806SLuiz Capitulino 	seq_printf(seq,
56963adc6fbSStephen Hemminger 		   "     udp_src_min: %d  udp_src_max: %d"
57063adc6fbSStephen Hemminger 		   "  udp_dst_min: %d  udp_dst_max: %d\n",
571222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
572222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
5731da177e4SLinus Torvalds 
574222f1806SLuiz Capitulino 	seq_printf(seq,
575ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
5761da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
5771da177e4SLinus Torvalds 
578ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
57995c96174SEric Dumazet 		unsigned int i;
580ca6549afSSteven Whitehouse 		seq_printf(seq, "     mpls: ");
581ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
582ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
583ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
584ca6549afSSteven Whitehouse 	}
585ca6549afSSteven Whitehouse 
58663adc6fbSStephen Hemminger 	if (pkt_dev->vlan_id != 0xffff)
58734954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
58863adc6fbSStephen Hemminger 			   pkt_dev->vlan_id, pkt_dev->vlan_p,
58963adc6fbSStephen Hemminger 			   pkt_dev->vlan_cfi);
59034954ddcSFrancesco Fondelli 
59163adc6fbSStephen Hemminger 	if (pkt_dev->svlan_id != 0xffff)
59234954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
59363adc6fbSStephen Hemminger 			   pkt_dev->svlan_id, pkt_dev->svlan_p,
59463adc6fbSStephen Hemminger 			   pkt_dev->svlan_cfi);
59534954ddcSFrancesco Fondelli 
59663adc6fbSStephen Hemminger 	if (pkt_dev->tos)
5971ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
5981ca7768cSFrancesco Fondelli 
59963adc6fbSStephen Hemminger 	if (pkt_dev->traffic_class)
6001ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
6011ca7768cSFrancesco Fondelli 
602e99b99b4SRobert Olsson 	if (pkt_dev->node >= 0)
603e99b99b4SRobert Olsson 		seq_printf(seq, "     node: %d\n", pkt_dev->node);
604e99b99b4SRobert Olsson 
605ca6549afSSteven Whitehouse 	seq_printf(seq, "     Flags: ");
606ca6549afSSteven Whitehouse 
6071da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
608d50a6b56SStephen Hemminger 		seq_printf(seq, "IPV6  ");
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPSRC_RND)
611d50a6b56SStephen Hemminger 		seq_printf(seq, "IPSRC_RND  ");
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPDST_RND)
614d50a6b56SStephen Hemminger 		seq_printf(seq, "IPDST_RND  ");
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	if (pkt_dev->flags & F_TXSIZE_RND)
617d50a6b56SStephen Hemminger 		seq_printf(seq, "TXSIZE_RND  ");
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPSRC_RND)
620d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPSRC_RND  ");
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPDST_RND)
623d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPDST_RND  ");
6241da177e4SLinus Torvalds 
625ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND)
626ca6549afSSteven Whitehouse 		seq_printf(seq,  "MPLS_RND  ");
627ca6549afSSteven Whitehouse 
62845b270f8SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_RND)
62945b270f8SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_RND  ");
63045b270f8SRobert Olsson 
631e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
632e6fce5b9SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_CPU  ");
633e6fce5b9SRobert Olsson 
634007a531bSJamal Hadi Salim 	if (pkt_dev->cflows) {
635007a531bSJamal Hadi Salim 		if (pkt_dev->flags & F_FLOW_SEQ)
636007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_SEQ  "); /*in sequence flows*/
637007a531bSJamal Hadi Salim 		else
638007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_RND  ");
639007a531bSJamal Hadi Salim 	}
640007a531bSJamal Hadi Salim 
641a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
642a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON)
643a553e4a6SJamal Hadi Salim 		seq_printf(seq,  "IPSEC  ");
644a553e4a6SJamal Hadi Salim #endif
645a553e4a6SJamal Hadi Salim 
6461da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACSRC_RND)
647d50a6b56SStephen Hemminger 		seq_printf(seq, "MACSRC_RND  ");
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACDST_RND)
650d50a6b56SStephen Hemminger 		seq_printf(seq, "MACDST_RND  ");
6511da177e4SLinus Torvalds 
65234954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_VID_RND)
65334954ddcSFrancesco Fondelli 		seq_printf(seq, "VID_RND  ");
65434954ddcSFrancesco Fondelli 
65534954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_SVID_RND)
65634954ddcSFrancesco Fondelli 		seq_printf(seq, "SVID_RND  ");
65734954ddcSFrancesco Fondelli 
658e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE)
659e99b99b4SRobert Olsson 		seq_printf(seq, "NODE_ALLOC  ");
660e99b99b4SRobert Olsson 
661d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6621da177e4SLinus Torvalds 
663fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
664*398f382cSDaniel Borkmann 	stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
665fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
666fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
6671da177e4SLinus Torvalds 
668222f1806SLuiz Capitulino 	seq_printf(seq,
669fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
6701da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
671fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
672fd29cf72SStephen Hemminger 
673fd29cf72SStephen Hemminger 	seq_printf(seq,
674fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
675fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
676fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
677fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
6781da177e4SLinus Torvalds 
679222f1806SLuiz Capitulino 	seq_printf(seq,
680222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
681d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
682d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
68547a0200dSAlexey Dobriyan 		seq_printf(seq, "     cur_saddr: %pI6c  cur_daddr: %pI6c\n",
68647a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_saddr,
68747a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_daddr);
688222f1806SLuiz Capitulino 	} else
6890373a946SAmerigo Wang 		seq_printf(seq, "     cur_saddr: %pI4  cur_daddr: %pI4\n",
6900373a946SAmerigo Wang 			   &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
6911da177e4SLinus Torvalds 
692d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
6931da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
6941da177e4SLinus Torvalds 
69545b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
69645b270f8SRobert Olsson 
697d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	if (pkt_dev->result[0])
700d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
7011da177e4SLinus Torvalds 	else
702d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: Idle\n");
7031da177e4SLinus Torvalds 
704d50a6b56SStephen Hemminger 	return 0;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
707ca6549afSSteven Whitehouse 
70863adc6fbSStephen Hemminger static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
70963adc6fbSStephen Hemminger 		     __u32 *num)
710ca6549afSSteven Whitehouse {
711ca6549afSSteven Whitehouse 	int i = 0;
712ca6549afSSteven Whitehouse 	*num = 0;
713ca6549afSSteven Whitehouse 
7141ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
71582fd5b5dSAndy Shevchenko 		int value;
716ca6549afSSteven Whitehouse 		char c;
717ca6549afSSteven Whitehouse 		*num <<= 4;
718ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
719ca6549afSSteven Whitehouse 			return -EFAULT;
72082fd5b5dSAndy Shevchenko 		value = hex_to_bin(c);
72182fd5b5dSAndy Shevchenko 		if (value >= 0)
72282fd5b5dSAndy Shevchenko 			*num |= value;
723ca6549afSSteven Whitehouse 		else
724ca6549afSSteven Whitehouse 			break;
725ca6549afSSteven Whitehouse 	}
726ca6549afSSteven Whitehouse 	return i;
727ca6549afSSteven Whitehouse }
728ca6549afSSteven Whitehouse 
729222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
730222f1806SLuiz Capitulino 			     unsigned int maxlen)
7311da177e4SLinus Torvalds {
7321da177e4SLinus Torvalds 	int i;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7351da177e4SLinus Torvalds 		char c;
7361da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7371da177e4SLinus Torvalds 			return -EFAULT;
7381da177e4SLinus Torvalds 		switch (c) {
7391da177e4SLinus Torvalds 		case '\"':
7401da177e4SLinus Torvalds 		case '\n':
7411da177e4SLinus Torvalds 		case '\r':
7421da177e4SLinus Torvalds 		case '\t':
7431da177e4SLinus Torvalds 		case ' ':
7441da177e4SLinus Torvalds 		case '=':
7451da177e4SLinus Torvalds 			break;
7461da177e4SLinus Torvalds 		default:
7471da177e4SLinus Torvalds 			goto done;
7483ff50b79SStephen Hemminger 		}
7491da177e4SLinus Torvalds 	}
7501da177e4SLinus Torvalds done:
7511da177e4SLinus Torvalds 	return i;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
754bf0813bdSPaul Gortmaker static long num_arg(const char __user *user_buffer, unsigned long maxlen,
755bf0813bdSPaul Gortmaker 				unsigned long *num)
7561da177e4SLinus Torvalds {
757d6182223SPaul Gortmaker 	int i;
7581da177e4SLinus Torvalds 	*num = 0;
7591da177e4SLinus Torvalds 
760d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7611da177e4SLinus Torvalds 		char c;
7621da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7631da177e4SLinus Torvalds 			return -EFAULT;
7641da177e4SLinus Torvalds 		if ((c >= '0') && (c <= '9')) {
7651da177e4SLinus Torvalds 			*num *= 10;
7661da177e4SLinus Torvalds 			*num += c - '0';
7671da177e4SLinus Torvalds 		} else
7681da177e4SLinus Torvalds 			break;
7691da177e4SLinus Torvalds 	}
7701da177e4SLinus Torvalds 	return i;
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
7741da177e4SLinus Torvalds {
775d6182223SPaul Gortmaker 	int i;
7761da177e4SLinus Torvalds 
777d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7781da177e4SLinus Torvalds 		char c;
7791da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7801da177e4SLinus Torvalds 			return -EFAULT;
7811da177e4SLinus Torvalds 		switch (c) {
7821da177e4SLinus Torvalds 		case '\"':
7831da177e4SLinus Torvalds 		case '\n':
7841da177e4SLinus Torvalds 		case '\r':
7851da177e4SLinus Torvalds 		case '\t':
7861da177e4SLinus Torvalds 		case ' ':
7871da177e4SLinus Torvalds 			goto done_str;
7881da177e4SLinus Torvalds 			break;
7891da177e4SLinus Torvalds 		default:
7901da177e4SLinus Torvalds 			break;
7913ff50b79SStephen Hemminger 		}
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds done_str:
7941da177e4SLinus Torvalds 	return i;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
797ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
798ca6549afSSteven Whitehouse {
79995c96174SEric Dumazet 	unsigned int n = 0;
800ca6549afSSteven Whitehouse 	char c;
801ca6549afSSteven Whitehouse 	ssize_t i = 0;
802ca6549afSSteven Whitehouse 	int len;
803ca6549afSSteven Whitehouse 
804ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = 0;
805ca6549afSSteven Whitehouse 	do {
806ca6549afSSteven Whitehouse 		__u32 tmp;
8071ca7768cSFrancesco Fondelli 		len = hex32_arg(&buffer[i], 8, &tmp);
808ca6549afSSteven Whitehouse 		if (len <= 0)
809ca6549afSSteven Whitehouse 			return len;
810ca6549afSSteven Whitehouse 		pkt_dev->labels[n] = htonl(tmp);
811ca6549afSSteven Whitehouse 		if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
812ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
813ca6549afSSteven Whitehouse 		i += len;
814ca6549afSSteven Whitehouse 		if (get_user(c, &buffer[i]))
815ca6549afSSteven Whitehouse 			return -EFAULT;
816ca6549afSSteven Whitehouse 		i++;
817ca6549afSSteven Whitehouse 		n++;
818ca6549afSSteven Whitehouse 		if (n >= MAX_MPLS_LABELS)
819ca6549afSSteven Whitehouse 			return -E2BIG;
820ca6549afSSteven Whitehouse 	} while (c == ',');
821ca6549afSSteven Whitehouse 
822ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = n;
823ca6549afSSteven Whitehouse 	return i;
824ca6549afSSteven Whitehouse }
825ca6549afSSteven Whitehouse 
826222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file,
827222f1806SLuiz Capitulino 			       const char __user * user_buffer, size_t count,
828222f1806SLuiz Capitulino 			       loff_t * offset)
8291da177e4SLinus Torvalds {
8308a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
831d50a6b56SStephen Hemminger 	struct pktgen_dev *pkt_dev = seq->private;
832d6182223SPaul Gortmaker 	int i, max, len;
8331da177e4SLinus Torvalds 	char name[16], valstr[32];
8341da177e4SLinus Torvalds 	unsigned long value = 0;
8351da177e4SLinus Torvalds 	char *pg_result = NULL;
8361da177e4SLinus Torvalds 	int tmp = 0;
8371da177e4SLinus Torvalds 	char buf[128];
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	pg_result = &(pkt_dev->result[0]);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	if (count < 1) {
842f9467eaeSJoe Perches 		pr_warning("wrong command format\n");
8431da177e4SLinus Torvalds 		return -EINVAL;
8441da177e4SLinus Torvalds 	}
8451da177e4SLinus Torvalds 
846d6182223SPaul Gortmaker 	max = count;
847d6182223SPaul Gortmaker 	tmp = count_trail_chars(user_buffer, max);
8481da177e4SLinus Torvalds 	if (tmp < 0) {
849f9467eaeSJoe Perches 		pr_warning("illegal format\n");
8501da177e4SLinus Torvalds 		return tmp;
8511da177e4SLinus Torvalds 	}
852d6182223SPaul Gortmaker 	i = tmp;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	/* Read variable name */
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
85763adc6fbSStephen Hemminger 	if (len < 0)
858222f1806SLuiz Capitulino 		return len;
85963adc6fbSStephen Hemminger 
8601da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
8611da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
8621da177e4SLinus Torvalds 		return -EFAULT;
8631da177e4SLinus Torvalds 	i += len;
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	max = count - i;
8661da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
8671da177e4SLinus Torvalds 	if (len < 0)
8681da177e4SLinus Torvalds 		return len;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	i += len;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	if (debug) {
87386c2c0a8SDmitry Torokhov 		size_t copy = min_t(size_t, count, 1023);
874448d7b5dSNelson Elhage 		char tb[copy + 1];
875448d7b5dSNelson Elhage 		if (copy_from_user(tb, user_buffer, copy))
8761da177e4SLinus Torvalds 			return -EFAULT;
877448d7b5dSNelson Elhage 		tb[copy] = 0;
878f342cda7SJoe Perches 		pr_debug("%s,%lu  buffer -:%s:-\n",
879f342cda7SJoe Perches 			 name, (unsigned long)count, tb);
8801da177e4SLinus Torvalds 	}
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	if (!strcmp(name, "min_pkt_size")) {
8831da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
88463adc6fbSStephen Hemminger 		if (len < 0)
885222f1806SLuiz Capitulino 			return len;
88663adc6fbSStephen Hemminger 
8871da177e4SLinus Torvalds 		i += len;
8881da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
8891da177e4SLinus Torvalds 			value = 14 + 20 + 8;
8901da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
8911da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
8921da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
8931da177e4SLinus Torvalds 		}
894222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: min_pkt_size=%u",
895222f1806SLuiz Capitulino 			pkt_dev->min_pkt_size);
8961da177e4SLinus Torvalds 		return count;
8971da177e4SLinus Torvalds 	}
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	if (!strcmp(name, "max_pkt_size")) {
9001da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
90163adc6fbSStephen Hemminger 		if (len < 0)
902222f1806SLuiz Capitulino 			return len;
90363adc6fbSStephen Hemminger 
9041da177e4SLinus Torvalds 		i += len;
9051da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9061da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9071da177e4SLinus Torvalds 		if (value != pkt_dev->max_pkt_size) {
9081da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9091da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9101da177e4SLinus Torvalds 		}
911222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: max_pkt_size=%u",
912222f1806SLuiz Capitulino 			pkt_dev->max_pkt_size);
9131da177e4SLinus Torvalds 		return count;
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	/* Shortcut for min = max */
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	if (!strcmp(name, "pkt_size")) {
9191da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
92063adc6fbSStephen Hemminger 		if (len < 0)
921222f1806SLuiz Capitulino 			return len;
92263adc6fbSStephen Hemminger 
9231da177e4SLinus Torvalds 		i += len;
9241da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9251da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9261da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9271da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9281da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9291da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9301da177e4SLinus Torvalds 		}
9311da177e4SLinus Torvalds 		sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
9321da177e4SLinus Torvalds 		return count;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	if (!strcmp(name, "debug")) {
9361da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
93763adc6fbSStephen Hemminger 		if (len < 0)
938222f1806SLuiz Capitulino 			return len;
93963adc6fbSStephen Hemminger 
9401da177e4SLinus Torvalds 		i += len;
9411da177e4SLinus Torvalds 		debug = value;
9421da177e4SLinus Torvalds 		sprintf(pg_result, "OK: debug=%u", debug);
9431da177e4SLinus Torvalds 		return count;
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	if (!strcmp(name, "frags")) {
9471da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
94863adc6fbSStephen Hemminger 		if (len < 0)
949222f1806SLuiz Capitulino 			return len;
95063adc6fbSStephen Hemminger 
9511da177e4SLinus Torvalds 		i += len;
9521da177e4SLinus Torvalds 		pkt_dev->nfrags = value;
9531da177e4SLinus Torvalds 		sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
9541da177e4SLinus Torvalds 		return count;
9551da177e4SLinus Torvalds 	}
9561da177e4SLinus Torvalds 	if (!strcmp(name, "delay")) {
9571da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
95863adc6fbSStephen Hemminger 		if (len < 0)
959222f1806SLuiz Capitulino 			return len;
96063adc6fbSStephen Hemminger 
9611da177e4SLinus Torvalds 		i += len;
962fd29cf72SStephen Hemminger 		if (value == 0x7FFFFFFF)
963fd29cf72SStephen Hemminger 			pkt_dev->delay = ULLONG_MAX;
964fd29cf72SStephen Hemminger 		else
9659240d715SEric Dumazet 			pkt_dev->delay = (u64)value;
966fd29cf72SStephen Hemminger 
967fd29cf72SStephen Hemminger 		sprintf(pg_result, "OK: delay=%llu",
968fd29cf72SStephen Hemminger 			(unsigned long long) pkt_dev->delay);
9691da177e4SLinus Torvalds 		return count;
9701da177e4SLinus Torvalds 	}
97143d28b65SDaniel Turull 	if (!strcmp(name, "rate")) {
97243d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
97343d28b65SDaniel Turull 		if (len < 0)
97443d28b65SDaniel Turull 			return len;
97543d28b65SDaniel Turull 
97643d28b65SDaniel Turull 		i += len;
97743d28b65SDaniel Turull 		if (!value)
97843d28b65SDaniel Turull 			return len;
97943d28b65SDaniel Turull 		pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
98043d28b65SDaniel Turull 		if (debug)
981f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
98243d28b65SDaniel Turull 
98343d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
98443d28b65SDaniel Turull 		return count;
98543d28b65SDaniel Turull 	}
98643d28b65SDaniel Turull 	if (!strcmp(name, "ratep")) {
98743d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
98843d28b65SDaniel Turull 		if (len < 0)
98943d28b65SDaniel Turull 			return len;
99043d28b65SDaniel Turull 
99143d28b65SDaniel Turull 		i += len;
99243d28b65SDaniel Turull 		if (!value)
99343d28b65SDaniel Turull 			return len;
99443d28b65SDaniel Turull 		pkt_dev->delay = NSEC_PER_SEC/value;
99543d28b65SDaniel Turull 		if (debug)
996f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
99743d28b65SDaniel Turull 
99843d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
99943d28b65SDaniel Turull 		return count;
100043d28b65SDaniel Turull 	}
10011da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_min")) {
10021da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
100363adc6fbSStephen Hemminger 		if (len < 0)
1004222f1806SLuiz Capitulino 			return len;
100563adc6fbSStephen Hemminger 
10061da177e4SLinus Torvalds 		i += len;
10071da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_min) {
10081da177e4SLinus Torvalds 			pkt_dev->udp_src_min = value;
10091da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10101da177e4SLinus Torvalds 		}
10111da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
10121da177e4SLinus Torvalds 		return count;
10131da177e4SLinus Torvalds 	}
10141da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_min")) {
10151da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
101663adc6fbSStephen Hemminger 		if (len < 0)
1017222f1806SLuiz Capitulino 			return len;
101863adc6fbSStephen Hemminger 
10191da177e4SLinus Torvalds 		i += len;
10201da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_min) {
10211da177e4SLinus Torvalds 			pkt_dev->udp_dst_min = value;
10221da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10231da177e4SLinus Torvalds 		}
10241da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
10251da177e4SLinus Torvalds 		return count;
10261da177e4SLinus Torvalds 	}
10271da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_max")) {
10281da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
102963adc6fbSStephen Hemminger 		if (len < 0)
1030222f1806SLuiz Capitulino 			return len;
103163adc6fbSStephen Hemminger 
10321da177e4SLinus Torvalds 		i += len;
10331da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_max) {
10341da177e4SLinus Torvalds 			pkt_dev->udp_src_max = value;
10351da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10361da177e4SLinus Torvalds 		}
10371da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
10381da177e4SLinus Torvalds 		return count;
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_max")) {
10411da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
104263adc6fbSStephen Hemminger 		if (len < 0)
1043222f1806SLuiz Capitulino 			return len;
104463adc6fbSStephen Hemminger 
10451da177e4SLinus Torvalds 		i += len;
10461da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_max) {
10471da177e4SLinus Torvalds 			pkt_dev->udp_dst_max = value;
10481da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10491da177e4SLinus Torvalds 		}
10501da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
10511da177e4SLinus Torvalds 		return count;
10521da177e4SLinus Torvalds 	}
10531da177e4SLinus Torvalds 	if (!strcmp(name, "clone_skb")) {
10541da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
105563adc6fbSStephen Hemminger 		if (len < 0)
1056222f1806SLuiz Capitulino 			return len;
1057d8873315SNeil Horman 		if ((value > 0) &&
1058d8873315SNeil Horman 		    (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1059d8873315SNeil Horman 			return -ENOTSUPP;
10601da177e4SLinus Torvalds 		i += len;
10611da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
10641da177e4SLinus Torvalds 		return count;
10651da177e4SLinus Torvalds 	}
10661da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
10671da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
106863adc6fbSStephen Hemminger 		if (len < 0)
1069222f1806SLuiz Capitulino 			return len;
107063adc6fbSStephen Hemminger 
10711da177e4SLinus Torvalds 		i += len;
10721da177e4SLinus Torvalds 		pkt_dev->count = value;
10731da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
10741da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
10751da177e4SLinus Torvalds 		return count;
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
10781da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
107963adc6fbSStephen Hemminger 		if (len < 0)
1080222f1806SLuiz Capitulino 			return len;
108163adc6fbSStephen Hemminger 
10821da177e4SLinus Torvalds 		i += len;
10831da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
10841da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
10851da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
10861da177e4SLinus Torvalds 		}
1087222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1088222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
10891da177e4SLinus Torvalds 		return count;
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
10921da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
109363adc6fbSStephen Hemminger 		if (len < 0)
1094222f1806SLuiz Capitulino 			return len;
109563adc6fbSStephen Hemminger 
10961da177e4SLinus Torvalds 		i += len;
10971da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
10981da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
10991da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
11001da177e4SLinus Torvalds 		}
1101222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1102222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
11031da177e4SLinus Torvalds 		return count;
11041da177e4SLinus Torvalds 	}
1105e99b99b4SRobert Olsson 	if (!strcmp(name, "node")) {
1106e99b99b4SRobert Olsson 		len = num_arg(&user_buffer[i], 10, &value);
1107e99b99b4SRobert Olsson 		if (len < 0)
1108e99b99b4SRobert Olsson 			return len;
1109e99b99b4SRobert Olsson 
1110e99b99b4SRobert Olsson 		i += len;
1111e99b99b4SRobert Olsson 
1112e99b99b4SRobert Olsson 		if (node_possible(value)) {
1113e99b99b4SRobert Olsson 			pkt_dev->node = value;
1114e99b99b4SRobert Olsson 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
111526ad7879SEric Dumazet 			if (pkt_dev->page) {
111626ad7879SEric Dumazet 				put_page(pkt_dev->page);
111726ad7879SEric Dumazet 				pkt_dev->page = NULL;
111826ad7879SEric Dumazet 			}
1119e99b99b4SRobert Olsson 		}
1120e99b99b4SRobert Olsson 		else
1121e99b99b4SRobert Olsson 			sprintf(pg_result, "ERROR: node not possible");
1122e99b99b4SRobert Olsson 		return count;
1123e99b99b4SRobert Olsson 	}
11241da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
11251da177e4SLinus Torvalds 		char f[32];
11261da177e4SLinus Torvalds 		memset(f, 0, 32);
11271da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
112863adc6fbSStephen Hemminger 		if (len < 0)
1129222f1806SLuiz Capitulino 			return len;
113063adc6fbSStephen Hemminger 
11311da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
11321da177e4SLinus Torvalds 			return -EFAULT;
11331da177e4SLinus Torvalds 		i += len;
11341da177e4SLinus Torvalds 		if (strcmp(f, "IPSRC_RND") == 0)
11351da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPSRC_RND;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 		else if (strcmp(f, "!IPSRC_RND") == 0)
11381da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPSRC_RND;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 		else if (strcmp(f, "TXSIZE_RND") == 0)
11411da177e4SLinus Torvalds 			pkt_dev->flags |= F_TXSIZE_RND;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 		else if (strcmp(f, "!TXSIZE_RND") == 0)
11441da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_TXSIZE_RND;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		else if (strcmp(f, "IPDST_RND") == 0)
11471da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPDST_RND;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 		else if (strcmp(f, "!IPDST_RND") == 0)
11501da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPDST_RND;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 		else if (strcmp(f, "UDPSRC_RND") == 0)
11531da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPSRC_RND;
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPSRC_RND") == 0)
11561da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPSRC_RND;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 		else if (strcmp(f, "UDPDST_RND") == 0)
11591da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPDST_RND;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPDST_RND") == 0)
11621da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPDST_RND;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 		else if (strcmp(f, "MACSRC_RND") == 0)
11651da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACSRC_RND;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 		else if (strcmp(f, "!MACSRC_RND") == 0)
11681da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACSRC_RND;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		else if (strcmp(f, "MACDST_RND") == 0)
11711da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACDST_RND;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 		else if (strcmp(f, "!MACDST_RND") == 0)
11741da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACDST_RND;
11751da177e4SLinus Torvalds 
1176ca6549afSSteven Whitehouse 		else if (strcmp(f, "MPLS_RND") == 0)
1177ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
1178ca6549afSSteven Whitehouse 
1179ca6549afSSteven Whitehouse 		else if (strcmp(f, "!MPLS_RND") == 0)
1180ca6549afSSteven Whitehouse 			pkt_dev->flags &= ~F_MPLS_RND;
1181ca6549afSSteven Whitehouse 
118234954ddcSFrancesco Fondelli 		else if (strcmp(f, "VID_RND") == 0)
118334954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_VID_RND;
118434954ddcSFrancesco Fondelli 
118534954ddcSFrancesco Fondelli 		else if (strcmp(f, "!VID_RND") == 0)
118634954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_VID_RND;
118734954ddcSFrancesco Fondelli 
118834954ddcSFrancesco Fondelli 		else if (strcmp(f, "SVID_RND") == 0)
118934954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_SVID_RND;
119034954ddcSFrancesco Fondelli 
119134954ddcSFrancesco Fondelli 		else if (strcmp(f, "!SVID_RND") == 0)
119234954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_SVID_RND;
119334954ddcSFrancesco Fondelli 
1194007a531bSJamal Hadi Salim 		else if (strcmp(f, "FLOW_SEQ") == 0)
1195007a531bSJamal Hadi Salim 			pkt_dev->flags |= F_FLOW_SEQ;
1196007a531bSJamal Hadi Salim 
119745b270f8SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
119845b270f8SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_RND;
119945b270f8SRobert Olsson 
120045b270f8SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
120145b270f8SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1202e6fce5b9SRobert Olsson 
1203e6fce5b9SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1204e6fce5b9SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1205e6fce5b9SRobert Olsson 
1206e6fce5b9SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1207e6fce5b9SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1208a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
1209a553e4a6SJamal Hadi Salim 		else if (strcmp(f, "IPSEC") == 0)
1210a553e4a6SJamal Hadi Salim 			pkt_dev->flags |= F_IPSEC_ON;
1211a553e4a6SJamal Hadi Salim #endif
1212a553e4a6SJamal Hadi Salim 
12131ca7768cSFrancesco Fondelli 		else if (strcmp(f, "!IPV6") == 0)
12141ca7768cSFrancesco Fondelli 			pkt_dev->flags &= ~F_IPV6;
12151ca7768cSFrancesco Fondelli 
1216e99b99b4SRobert Olsson 		else if (strcmp(f, "NODE_ALLOC") == 0)
1217e99b99b4SRobert Olsson 			pkt_dev->flags |= F_NODE;
1218e99b99b4SRobert Olsson 
1219e99b99b4SRobert Olsson 		else if (strcmp(f, "!NODE_ALLOC") == 0)
1220e99b99b4SRobert Olsson 			pkt_dev->flags &= ~F_NODE;
1221e99b99b4SRobert Olsson 
12221da177e4SLinus Torvalds 		else {
1223222f1806SLuiz Capitulino 			sprintf(pg_result,
1224222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
12251da177e4SLinus Torvalds 				f,
12261ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1227e99b99b4SRobert Olsson 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
12281da177e4SLinus Torvalds 			return count;
12291da177e4SLinus Torvalds 		}
12301da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
12311da177e4SLinus Torvalds 		return count;
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
12341da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
123563adc6fbSStephen Hemminger 		if (len < 0)
1236222f1806SLuiz Capitulino 			return len;
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12391da177e4SLinus Torvalds 			return -EFAULT;
12401da177e4SLinus Torvalds 		buf[len] = 0;
12411da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
12421da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
12431da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_min, buf, len);
12441da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
12451da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 		if (debug)
1248f342cda7SJoe Perches 			pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
12491da177e4SLinus Torvalds 		i += len;
12501da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
12511da177e4SLinus Torvalds 		return count;
12521da177e4SLinus Torvalds 	}
12531da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
12541da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
125563adc6fbSStephen Hemminger 		if (len < 0)
1256222f1806SLuiz Capitulino 			return len;
125763adc6fbSStephen Hemminger 
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12601da177e4SLinus Torvalds 			return -EFAULT;
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 		buf[len] = 0;
12631da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
12641da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
12651da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_max, buf, len);
12661da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
12671da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
12681da177e4SLinus Torvalds 		}
12691da177e4SLinus Torvalds 		if (debug)
1270f342cda7SJoe Perches 			pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
12711da177e4SLinus Torvalds 		i += len;
12721da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
12731da177e4SLinus Torvalds 		return count;
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
12761da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1277222f1806SLuiz Capitulino 		if (len < 0)
1278222f1806SLuiz Capitulino 			return len;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12831da177e4SLinus Torvalds 			return -EFAULT;
12841da177e4SLinus Torvalds 		buf[len] = 0;
12851da177e4SLinus Torvalds 
1286c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
128747a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
12881da177e4SLinus Torvalds 
12894e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 		if (debug)
1292f342cda7SJoe Perches 			pr_debug("dst6 set to: %s\n", buf);
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 		i += len;
12951da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
12961da177e4SLinus Torvalds 		return count;
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
12991da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1300222f1806SLuiz Capitulino 		if (len < 0)
1301222f1806SLuiz Capitulino 			return len;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13061da177e4SLinus Torvalds 			return -EFAULT;
13071da177e4SLinus Torvalds 		buf[len] = 0;
13081da177e4SLinus Torvalds 
1309c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
131047a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
13111da177e4SLinus Torvalds 
13124e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
13131da177e4SLinus Torvalds 		if (debug)
1314f342cda7SJoe Perches 			pr_debug("dst6_min set to: %s\n", buf);
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 		i += len;
13171da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
13181da177e4SLinus Torvalds 		return count;
13191da177e4SLinus Torvalds 	}
13201da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
13211da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1322222f1806SLuiz Capitulino 		if (len < 0)
1323222f1806SLuiz Capitulino 			return len;
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13281da177e4SLinus Torvalds 			return -EFAULT;
13291da177e4SLinus Torvalds 		buf[len] = 0;
13301da177e4SLinus Torvalds 
1331c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
133247a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 		if (debug)
1335f342cda7SJoe Perches 			pr_debug("dst6_max set to: %s\n", buf);
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 		i += len;
13381da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
13391da177e4SLinus Torvalds 		return count;
13401da177e4SLinus Torvalds 	}
13411da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
13421da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1343222f1806SLuiz Capitulino 		if (len < 0)
1344222f1806SLuiz Capitulino 			return len;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13491da177e4SLinus Torvalds 			return -EFAULT;
13501da177e4SLinus Torvalds 		buf[len] = 0;
13511da177e4SLinus Torvalds 
1352c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
135347a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
13541da177e4SLinus Torvalds 
13554e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 		if (debug)
1358f342cda7SJoe Perches 			pr_debug("src6 set to: %s\n", buf);
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 		i += len;
13611da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
13621da177e4SLinus Torvalds 		return count;
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
13651da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
136663adc6fbSStephen Hemminger 		if (len < 0)
1367222f1806SLuiz Capitulino 			return len;
136863adc6fbSStephen Hemminger 
13691da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13701da177e4SLinus Torvalds 			return -EFAULT;
13711da177e4SLinus Torvalds 		buf[len] = 0;
13721da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
13731da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
13741da177e4SLinus Torvalds 			strncpy(pkt_dev->src_min, buf, len);
13751da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
13761da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
13771da177e4SLinus Torvalds 		}
13781da177e4SLinus Torvalds 		if (debug)
1379f342cda7SJoe Perches 			pr_debug("src_min set to: %s\n", pkt_dev->src_min);
13801da177e4SLinus Torvalds 		i += len;
13811da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
13821da177e4SLinus Torvalds 		return count;
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
13851da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
138663adc6fbSStephen Hemminger 		if (len < 0)
1387222f1806SLuiz Capitulino 			return len;
138863adc6fbSStephen Hemminger 
13891da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13901da177e4SLinus Torvalds 			return -EFAULT;
13911da177e4SLinus Torvalds 		buf[len] = 0;
13921da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
13931da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
13941da177e4SLinus Torvalds 			strncpy(pkt_dev->src_max, buf, len);
13951da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
13961da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
13971da177e4SLinus Torvalds 		}
13981da177e4SLinus Torvalds 		if (debug)
1399f342cda7SJoe Perches 			pr_debug("src_max set to: %s\n", pkt_dev->src_max);
14001da177e4SLinus Torvalds 		i += len;
14011da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
14021da177e4SLinus Torvalds 		return count;
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
14051da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
140663adc6fbSStephen Hemminger 		if (len < 0)
1407222f1806SLuiz Capitulino 			return len;
140863adc6fbSStephen Hemminger 
14091da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14101da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14111da177e4SLinus Torvalds 			return -EFAULT;
14121da177e4SLinus Torvalds 
14134940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->dst_mac))
14144940fc88SAlexey Dobriyan 			return -EINVAL;
14151da177e4SLinus Torvalds 		/* Set up Dest MAC */
14164940fc88SAlexey Dobriyan 		memcpy(&pkt_dev->hh[0], pkt_dev->dst_mac, ETH_ALEN);
14171da177e4SLinus Torvalds 
14184940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
14191da177e4SLinus Torvalds 		return count;
14201da177e4SLinus Torvalds 	}
14211da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
14221da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
142363adc6fbSStephen Hemminger 		if (len < 0)
1424222f1806SLuiz Capitulino 			return len;
142563adc6fbSStephen Hemminger 
14261da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14271da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14281da177e4SLinus Torvalds 			return -EFAULT;
14291da177e4SLinus Torvalds 
14304940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->src_mac))
14314940fc88SAlexey Dobriyan 			return -EINVAL;
1432ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
14334940fc88SAlexey Dobriyan 		memcpy(&pkt_dev->hh[6], pkt_dev->src_mac, ETH_ALEN);
1434ce5d0b47SAdit Ranadive 
14354940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
14361da177e4SLinus Torvalds 		return count;
14371da177e4SLinus Torvalds 	}
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
14401da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
14411da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
14421da177e4SLinus Torvalds 		return count;
14431da177e4SLinus Torvalds 	}
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
14461da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
144763adc6fbSStephen Hemminger 		if (len < 0)
1448222f1806SLuiz Capitulino 			return len;
144963adc6fbSStephen Hemminger 
14501da177e4SLinus Torvalds 		i += len;
14511da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
14521da177e4SLinus Torvalds 			value = MAX_CFLOWS;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 		pkt_dev->cflows = value;
14551da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
14561da177e4SLinus Torvalds 		return count;
14571da177e4SLinus Torvalds 	}
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
14601da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
146163adc6fbSStephen Hemminger 		if (len < 0)
1462222f1806SLuiz Capitulino 			return len;
146363adc6fbSStephen Hemminger 
14641da177e4SLinus Torvalds 		i += len;
14651da177e4SLinus Torvalds 		pkt_dev->lflow = value;
14661da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
14671da177e4SLinus Torvalds 		return count;
14681da177e4SLinus Torvalds 	}
14691da177e4SLinus Torvalds 
147045b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
147145b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
147263adc6fbSStephen Hemminger 		if (len < 0)
147345b270f8SRobert Olsson 			return len;
147463adc6fbSStephen Hemminger 
147545b270f8SRobert Olsson 		i += len;
147645b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
147745b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
147845b270f8SRobert Olsson 		return count;
147945b270f8SRobert Olsson 	}
148045b270f8SRobert Olsson 
148145b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
148245b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
148363adc6fbSStephen Hemminger 		if (len < 0)
148445b270f8SRobert Olsson 			return len;
148563adc6fbSStephen Hemminger 
148645b270f8SRobert Olsson 		i += len;
148745b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
148845b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
148945b270f8SRobert Olsson 		return count;
149045b270f8SRobert Olsson 	}
149145b270f8SRobert Olsson 
1492ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
149395c96174SEric Dumazet 		unsigned int n, cnt;
1494cfcabdccSStephen Hemminger 
1495ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1496cfcabdccSStephen Hemminger 		if (len < 0)
1497cfcabdccSStephen Hemminger 			return len;
1498ca6549afSSteven Whitehouse 		i += len;
1499cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1500ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1501cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1502ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1503ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
150434954ddcSFrancesco Fondelli 
150534954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
150634954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
150734954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
150834954ddcSFrancesco Fondelli 
150934954ddcSFrancesco Fondelli 			if (debug)
1510f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN auto turned off\n");
151134954ddcSFrancesco Fondelli 		}
151234954ddcSFrancesco Fondelli 		return count;
151334954ddcSFrancesco Fondelli 	}
151434954ddcSFrancesco Fondelli 
151534954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
151634954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
151763adc6fbSStephen Hemminger 		if (len < 0)
151834954ddcSFrancesco Fondelli 			return len;
151963adc6fbSStephen Hemminger 
152034954ddcSFrancesco Fondelli 		i += len;
152134954ddcSFrancesco Fondelli 		if (value <= 4095) {
152234954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
152334954ddcSFrancesco Fondelli 
152434954ddcSFrancesco Fondelli 			if (debug)
1525f342cda7SJoe Perches 				pr_debug("VLAN turned on\n");
152634954ddcSFrancesco Fondelli 
152734954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1528f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
152934954ddcSFrancesco Fondelli 
153034954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
153134954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
153234954ddcSFrancesco Fondelli 		} else {
153334954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
153434954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
153534954ddcSFrancesco Fondelli 
153634954ddcSFrancesco Fondelli 			if (debug)
1537f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
153834954ddcSFrancesco Fondelli 		}
153934954ddcSFrancesco Fondelli 		return count;
154034954ddcSFrancesco Fondelli 	}
154134954ddcSFrancesco Fondelli 
154234954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
154334954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
154463adc6fbSStephen Hemminger 		if (len < 0)
154534954ddcSFrancesco Fondelli 			return len;
154663adc6fbSStephen Hemminger 
154734954ddcSFrancesco Fondelli 		i += len;
154834954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
154934954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
155034954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
155134954ddcSFrancesco Fondelli 		} else {
155234954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
155334954ddcSFrancesco Fondelli 		}
155434954ddcSFrancesco Fondelli 		return count;
155534954ddcSFrancesco Fondelli 	}
155634954ddcSFrancesco Fondelli 
155734954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
155834954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
155963adc6fbSStephen Hemminger 		if (len < 0)
156034954ddcSFrancesco Fondelli 			return len;
156163adc6fbSStephen Hemminger 
156234954ddcSFrancesco Fondelli 		i += len;
156334954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
156434954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
156534954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
156634954ddcSFrancesco Fondelli 		} else {
156734954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
156834954ddcSFrancesco Fondelli 		}
156934954ddcSFrancesco Fondelli 		return count;
157034954ddcSFrancesco Fondelli 	}
157134954ddcSFrancesco Fondelli 
157234954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
157334954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
157463adc6fbSStephen Hemminger 		if (len < 0)
157534954ddcSFrancesco Fondelli 			return len;
157663adc6fbSStephen Hemminger 
157734954ddcSFrancesco Fondelli 		i += len;
157834954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
157934954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
158034954ddcSFrancesco Fondelli 
158134954ddcSFrancesco Fondelli 			if (debug)
1582f342cda7SJoe Perches 				pr_debug("SVLAN turned on\n");
158334954ddcSFrancesco Fondelli 
158434954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1585f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
158634954ddcSFrancesco Fondelli 
158734954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
158834954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
158934954ddcSFrancesco Fondelli 		} else {
159034954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
159134954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
159234954ddcSFrancesco Fondelli 
159334954ddcSFrancesco Fondelli 			if (debug)
1594f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
159534954ddcSFrancesco Fondelli 		}
159634954ddcSFrancesco Fondelli 		return count;
159734954ddcSFrancesco Fondelli 	}
159834954ddcSFrancesco Fondelli 
159934954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
160034954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
160163adc6fbSStephen Hemminger 		if (len < 0)
160234954ddcSFrancesco Fondelli 			return len;
160363adc6fbSStephen Hemminger 
160434954ddcSFrancesco Fondelli 		i += len;
160534954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
160634954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
160734954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
160834954ddcSFrancesco Fondelli 		} else {
160934954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
161034954ddcSFrancesco Fondelli 		}
161134954ddcSFrancesco Fondelli 		return count;
161234954ddcSFrancesco Fondelli 	}
161334954ddcSFrancesco Fondelli 
161434954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
161534954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
161663adc6fbSStephen Hemminger 		if (len < 0)
161734954ddcSFrancesco Fondelli 			return len;
161863adc6fbSStephen Hemminger 
161934954ddcSFrancesco Fondelli 		i += len;
162034954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
162134954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
162234954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
162334954ddcSFrancesco Fondelli 		} else {
162434954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
162534954ddcSFrancesco Fondelli 		}
1626ca6549afSSteven Whitehouse 		return count;
1627ca6549afSSteven Whitehouse 	}
1628ca6549afSSteven Whitehouse 
16291ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
16301ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16311ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
163263adc6fbSStephen Hemminger 		if (len < 0)
16331ca7768cSFrancesco Fondelli 			return len;
163463adc6fbSStephen Hemminger 
16351ca7768cSFrancesco Fondelli 		i += len;
16361ca7768cSFrancesco Fondelli 		if (len == 2) {
16371ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
16381ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
16391ca7768cSFrancesco Fondelli 		} else {
16401ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
16411ca7768cSFrancesco Fondelli 		}
16421ca7768cSFrancesco Fondelli 		return count;
16431ca7768cSFrancesco Fondelli 	}
16441ca7768cSFrancesco Fondelli 
16451ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
16461ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16471ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
164863adc6fbSStephen Hemminger 		if (len < 0)
16491ca7768cSFrancesco Fondelli 			return len;
165063adc6fbSStephen Hemminger 
16511ca7768cSFrancesco Fondelli 		i += len;
16521ca7768cSFrancesco Fondelli 		if (len == 2) {
16531ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
16541ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
16551ca7768cSFrancesco Fondelli 		} else {
16561ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
16571ca7768cSFrancesco Fondelli 		}
16581ca7768cSFrancesco Fondelli 		return count;
16591ca7768cSFrancesco Fondelli 	}
16601ca7768cSFrancesco Fondelli 
16619e50e3acSJohn Fastabend 	if (!strcmp(name, "skb_priority")) {
16629e50e3acSJohn Fastabend 		len = num_arg(&user_buffer[i], 9, &value);
16639e50e3acSJohn Fastabend 		if (len < 0)
16649e50e3acSJohn Fastabend 			return len;
16659e50e3acSJohn Fastabend 
16669e50e3acSJohn Fastabend 		i += len;
16679e50e3acSJohn Fastabend 		pkt_dev->skb_priority = value;
16689e50e3acSJohn Fastabend 		sprintf(pg_result, "OK: skb_priority=%i",
16699e50e3acSJohn Fastabend 			pkt_dev->skb_priority);
16709e50e3acSJohn Fastabend 		return count;
16719e50e3acSJohn Fastabend 	}
16729e50e3acSJohn Fastabend 
16731da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
16741da177e4SLinus Torvalds 	return -EINVAL;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
1677d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
16781da177e4SLinus Torvalds {
1679d50a6b56SStephen Hemminger 	return single_open(file, pktgen_if_show, PDE(inode)->data);
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
16829a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1683d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1684d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1685d50a6b56SStephen Hemminger 	.read    = seq_read,
1686d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1687d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1688d50a6b56SStephen Hemminger 	.release = single_release,
1689d50a6b56SStephen Hemminger };
1690d50a6b56SStephen Hemminger 
1691d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1692d50a6b56SStephen Hemminger {
1693d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1694648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1695d50a6b56SStephen Hemminger 
1696d50a6b56SStephen Hemminger 	BUG_ON(!t);
1697d50a6b56SStephen Hemminger 
1698d50a6b56SStephen Hemminger 	seq_printf(seq, "Running: ");
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	if_lock(t);
1701c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17021da177e4SLinus Torvalds 		if (pkt_dev->running)
1703593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17041da177e4SLinus Torvalds 
1705d50a6b56SStephen Hemminger 	seq_printf(seq, "\nStopped: ");
17061da177e4SLinus Torvalds 
1707c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17081da177e4SLinus Torvalds 		if (!pkt_dev->running)
1709593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	if (t->result[0])
1712d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
17131da177e4SLinus Torvalds 	else
1714d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: NA\n");
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	if_unlock(t);
17171da177e4SLinus Torvalds 
1718d50a6b56SStephen Hemminger 	return 0;
17191da177e4SLinus Torvalds }
17201da177e4SLinus Torvalds 
1721d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1722d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1723d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
17241da177e4SLinus Torvalds {
17258a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
1726d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1727d6182223SPaul Gortmaker 	int i, max, len, ret;
17281da177e4SLinus Torvalds 	char name[40];
17291da177e4SLinus Torvalds 	char *pg_result;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	if (count < 1) {
17321da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
17331da177e4SLinus Torvalds 		return -EINVAL;
17341da177e4SLinus Torvalds 	}
17351da177e4SLinus Torvalds 
1736d6182223SPaul Gortmaker 	max = count;
1737d6182223SPaul Gortmaker 	len = count_trail_chars(user_buffer, max);
17381da177e4SLinus Torvalds 	if (len < 0)
17391da177e4SLinus Torvalds 		return len;
17401da177e4SLinus Torvalds 
1741d6182223SPaul Gortmaker 	i = len;
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	/* Read variable name */
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
17461da177e4SLinus Torvalds 	if (len < 0)
17471da177e4SLinus Torvalds 		return len;
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
17501da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
17511da177e4SLinus Torvalds 		return -EFAULT;
17521da177e4SLinus Torvalds 	i += len;
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	max = count - i;
17551da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
17561da177e4SLinus Torvalds 	if (len < 0)
17571da177e4SLinus Torvalds 		return len;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 	i += len;
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	if (debug)
1762f342cda7SJoe Perches 		pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	if (!t) {
1765f9467eaeSJoe Perches 		pr_err("ERROR: No thread\n");
17661da177e4SLinus Torvalds 		ret = -EINVAL;
17671da177e4SLinus Torvalds 		goto out;
17681da177e4SLinus Torvalds 	}
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
17731da177e4SLinus Torvalds 		char f[32];
17741da177e4SLinus Torvalds 		memset(f, 0, 32);
17751da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
17761da177e4SLinus Torvalds 		if (len < 0) {
17771da177e4SLinus Torvalds 			ret = len;
17781da177e4SLinus Torvalds 			goto out;
17791da177e4SLinus Torvalds 		}
17801da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
17811da177e4SLinus Torvalds 			return -EFAULT;
17821da177e4SLinus Torvalds 		i += len;
17836146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
17841da177e4SLinus Torvalds 		pktgen_add_device(t, f);
17856146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
17861da177e4SLinus Torvalds 		ret = count;
17871da177e4SLinus Torvalds 		sprintf(pg_result, "OK: add_device=%s", f);
17881da177e4SLinus Torvalds 		goto out;
17891da177e4SLinus Torvalds 	}
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
17926146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
179395ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
17946146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1795121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
17961da177e4SLinus Torvalds 		ret = count;
17971da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
17981da177e4SLinus Torvalds 		goto out;
17991da177e4SLinus Torvalds 	}
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1802b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
18031da177e4SLinus Torvalds 		ret = count;
18041da177e4SLinus Torvalds 		goto out;
18051da177e4SLinus Torvalds 	}
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	ret = -EINVAL;
18081da177e4SLinus Torvalds out:
18091da177e4SLinus Torvalds 	return ret;
18101da177e4SLinus Torvalds }
18111da177e4SLinus Torvalds 
1812d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
18131da177e4SLinus Torvalds {
1814d50a6b56SStephen Hemminger 	return single_open(file, pktgen_thread_show, PDE(inode)->data);
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
18179a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1818d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1819d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1820d50a6b56SStephen Hemminger 	.read    = seq_read,
1821d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1822d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1823d50a6b56SStephen Hemminger 	.release = single_release,
1824d50a6b56SStephen Hemminger };
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds /* Think find or remove for NN */
18271da177e4SLinus Torvalds static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
18281da177e4SLinus Torvalds {
18291da177e4SLinus Torvalds 	struct pktgen_thread *t;
18301da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
18313e984840SEric Dumazet 	bool exact = (remove == FIND);
18321da177e4SLinus Torvalds 
1833cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list) {
18343e984840SEric Dumazet 		pkt_dev = pktgen_find_dev(t, ifname, exact);
18351da177e4SLinus Torvalds 		if (pkt_dev) {
18361da177e4SLinus Torvalds 			if (remove) {
18371da177e4SLinus Torvalds 				if_lock(t);
183895ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
183995ed63f7SArthur Kepner 				t->control |= T_REMDEV;
18401da177e4SLinus Torvalds 				if_unlock(t);
18411da177e4SLinus Torvalds 			}
18421da177e4SLinus Torvalds 			break;
18431da177e4SLinus Torvalds 		}
18441da177e4SLinus Torvalds 	}
18451da177e4SLinus Torvalds 	return pkt_dev;
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
184895ed63f7SArthur Kepner /*
184995ed63f7SArthur Kepner  * mark a device for removal
185095ed63f7SArthur Kepner  */
185139df232fSStephen Hemminger static void pktgen_mark_device(const char *ifname)
18521da177e4SLinus Torvalds {
18531da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
185495ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
185595ed63f7SArthur Kepner 	int i = 0;
185695ed63f7SArthur Kepner 
18576146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
1858f9467eaeSJoe Perches 	pr_debug("%s: marking %s for removal\n", __func__, ifname);
185995ed63f7SArthur Kepner 
186095ed63f7SArthur Kepner 	while (1) {
186195ed63f7SArthur Kepner 
186295ed63f7SArthur Kepner 		pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1863222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1864222f1806SLuiz Capitulino 			break;	/* success */
186595ed63f7SArthur Kepner 
18666146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1867f9467eaeSJoe Perches 		pr_debug("%s: waiting for %s to disappear....\n",
1868f9467eaeSJoe Perches 			 __func__, ifname);
186995ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
18706146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
187195ed63f7SArthur Kepner 
187295ed63f7SArthur Kepner 		if (++i >= max_tries) {
1873f9467eaeSJoe Perches 			pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1874f9467eaeSJoe Perches 			       __func__, msec_per_try * i, ifname);
187595ed63f7SArthur Kepner 			break;
187695ed63f7SArthur Kepner 		}
187795ed63f7SArthur Kepner 
187895ed63f7SArthur Kepner 	}
187995ed63f7SArthur Kepner 
18806146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
188139df232fSStephen Hemminger }
188295ed63f7SArthur Kepner 
188339df232fSStephen Hemminger static void pktgen_change_name(struct net_device *dev)
188439df232fSStephen Hemminger {
188539df232fSStephen Hemminger 	struct pktgen_thread *t;
188639df232fSStephen Hemminger 
188739df232fSStephen Hemminger 	list_for_each_entry(t, &pktgen_threads, th_list) {
188839df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
188939df232fSStephen Hemminger 
189039df232fSStephen Hemminger 		list_for_each_entry(pkt_dev, &t->if_list, list) {
189139df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
189239df232fSStephen Hemminger 				continue;
189339df232fSStephen Hemminger 
189439df232fSStephen Hemminger 			remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
189539df232fSStephen Hemminger 
18962975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
18972975315bSAlexey Dobriyan 							  pg_proc_dir,
18982975315bSAlexey Dobriyan 							  &pktgen_if_fops,
18992975315bSAlexey Dobriyan 							  pkt_dev);
190039df232fSStephen Hemminger 			if (!pkt_dev->entry)
1901f9467eaeSJoe Perches 				pr_err("can't move proc entry for '%s'\n",
1902f9467eaeSJoe Perches 				       dev->name);
190339df232fSStephen Hemminger 			break;
190439df232fSStephen Hemminger 		}
190539df232fSStephen Hemminger 	}
19061da177e4SLinus Torvalds }
19071da177e4SLinus Torvalds 
1908222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
1909222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
19101da177e4SLinus Torvalds {
191139df232fSStephen Hemminger 	struct net_device *dev = ptr;
19121da177e4SLinus Torvalds 
1913c57b5468SEric Dumazet 	if (!net_eq(dev_net(dev), &init_net) || pktgen_exiting)
1914e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1915e9dc8653SEric W. Biederman 
19161da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
19171da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
19181da177e4SLinus Torvalds 	 */
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 	switch (event) {
192139df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
192239df232fSStephen Hemminger 		pktgen_change_name(dev);
19231da177e4SLinus Torvalds 		break;
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
192695ed63f7SArthur Kepner 		pktgen_mark_device(dev->name);
19271da177e4SLinus Torvalds 		break;
19283ff50b79SStephen Hemminger 	}
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	return NOTIFY_DONE;
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
193363adc6fbSStephen Hemminger static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
193463adc6fbSStephen Hemminger 						 const char *ifname)
1935e6fce5b9SRobert Olsson {
1936e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
1937d6182223SPaul Gortmaker 	int i;
1938e6fce5b9SRobert Olsson 
1939e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
1940e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
1941e6fce5b9SRobert Olsson 			break;
1942e6fce5b9SRobert Olsson 
1943e6fce5b9SRobert Olsson 		b[i] = ifname[i];
1944e6fce5b9SRobert Olsson 	}
1945e6fce5b9SRobert Olsson 	b[i] = 0;
1946e6fce5b9SRobert Olsson 
1947e6fce5b9SRobert Olsson 	return dev_get_by_name(&init_net, b);
1948e6fce5b9SRobert Olsson }
1949e6fce5b9SRobert Olsson 
1950e6fce5b9SRobert Olsson 
19511da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
19521da177e4SLinus Torvalds 
195339df232fSStephen Hemminger static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1954222f1806SLuiz Capitulino {
19551da177e4SLinus Torvalds 	struct net_device *odev;
195639df232fSStephen Hemminger 	int err;
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 	/* Clean old setups */
19591da177e4SLinus Torvalds 	if (pkt_dev->odev) {
19601da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
19611da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
19621da177e4SLinus Torvalds 	}
19631da177e4SLinus Torvalds 
1964e6fce5b9SRobert Olsson 	odev = pktgen_dev_get_by_name(pkt_dev, ifname);
19651da177e4SLinus Torvalds 	if (!odev) {
1966f9467eaeSJoe Perches 		pr_err("no such netdevice: \"%s\"\n", ifname);
196739df232fSStephen Hemminger 		return -ENODEV;
19681da177e4SLinus Torvalds 	}
196939df232fSStephen Hemminger 
19701da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
1971f9467eaeSJoe Perches 		pr_err("not an ethernet device: \"%s\"\n", ifname);
197239df232fSStephen Hemminger 		err = -EINVAL;
197339df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
1974f9467eaeSJoe Perches 		pr_err("device is down: \"%s\"\n", ifname);
197539df232fSStephen Hemminger 		err = -ENETDOWN;
197639df232fSStephen Hemminger 	} else {
19771da177e4SLinus Torvalds 		pkt_dev->odev = odev;
197839df232fSStephen Hemminger 		return 0;
197939df232fSStephen Hemminger 	}
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	dev_put(odev);
198239df232fSStephen Hemminger 	return err;
19831da177e4SLinus Torvalds }
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
19861da177e4SLinus Torvalds  * structure to have the right information to create/send packets
19871da177e4SLinus Torvalds  */
19881da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
19891da177e4SLinus Torvalds {
199064c00d81SAndrew Gallatin 	int ntxq;
199164c00d81SAndrew Gallatin 
19921da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
1993f9467eaeSJoe Perches 		pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
1994222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
1995222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
19961da177e4SLinus Torvalds 		return;
19971da177e4SLinus Torvalds 	}
19981da177e4SLinus Torvalds 
199964c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
200064c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2001bfdbc0acSRobert Olsson 
200264c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
2003f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
200488271660SJesse Brandeburg 			   pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2005593f63b0SEric Dumazet 			   pkt_dev->odevname);
200626e29eedSDan Carpenter 		pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
200764c00d81SAndrew Gallatin 	}
200888271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
2009f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
201088271660SJesse Brandeburg 			   pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2011593f63b0SEric Dumazet 			   pkt_dev->odevname);
201226e29eedSDan Carpenter 		pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
201364c00d81SAndrew Gallatin 	}
201464c00d81SAndrew Gallatin 
20151da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
20161da177e4SLinus Torvalds 
2017f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
2018f404e9a6SKris Katterjohn 		memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	/* Set up Dest MAC */
2021f404e9a6SKris Katterjohn 	memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
20244c139b8cSAmerigo Wang 		int i, set = 0, err = 1;
20254c139b8cSAmerigo Wang 		struct inet6_dev *idev;
20264c139b8cSAmerigo Wang 
202768bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
202868bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
202968bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
203068bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
203168bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
203268bf9f0bSAmerigo Wang 		}
203368bf9f0bSAmerigo Wang 
20341da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
20351da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
20361da177e4SLinus Torvalds 				set = 1;
20371da177e4SLinus Torvalds 				break;
20381da177e4SLinus Torvalds 			}
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 		if (!set) {
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 			/*
20431da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
20441da177e4SLinus Torvalds 			 *
20451da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
20461da177e4SLinus Torvalds 			 */
20471da177e4SLinus Torvalds 
20488814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
204963adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
205063adc6fbSStephen Hemminger 			if (idev) {
20511da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
20544c139b8cSAmerigo Wang 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
20554c139b8cSAmerigo Wang 					if ((ifp->scope & IFA_LINK) &&
2056f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
20574e3fd7a0SAlexey Dobriyan 						pkt_dev->cur_in6_saddr = ifp->addr;
20581da177e4SLinus Torvalds 						err = 0;
20591da177e4SLinus Torvalds 						break;
20601da177e4SLinus Torvalds 					}
20611da177e4SLinus Torvalds 				}
20621da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
20631da177e4SLinus Torvalds 			}
20648814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2065222f1806SLuiz Capitulino 			if (err)
2066f9467eaeSJoe Perches 				pr_err("ERROR: IPv6 link address not available\n");
20671da177e4SLinus Torvalds 		}
2068222f1806SLuiz Capitulino 	} else {
206968bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
207068bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
207168bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
207268bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
207368bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
207468bf9f0bSAmerigo Wang 		}
207568bf9f0bSAmerigo Wang 
20761da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
20771da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
20781da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 			struct in_device *in_dev;
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 			rcu_read_lock();
2083e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
20841da177e4SLinus Torvalds 			if (in_dev) {
20851da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2086222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2087222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
20881da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
20891da177e4SLinus Torvalds 				}
20901da177e4SLinus Torvalds 			}
20911da177e4SLinus Torvalds 			rcu_read_unlock();
2092222f1806SLuiz Capitulino 		} else {
20931da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
20941da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
20951da177e4SLinus Torvalds 		}
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
20981da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
20991da177e4SLinus Torvalds 	}
21001da177e4SLinus Torvalds 	/* Initialize current values. */
210168bf9f0bSAmerigo Wang 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
210268bf9f0bSAmerigo Wang 	if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
210368bf9f0bSAmerigo Wang 		pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
210468bf9f0bSAmerigo Wang 
21051da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
21061da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
21071da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
21081da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
21091da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
21101da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
21111da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 
2115fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2116fd29cf72SStephen Hemminger {
2117ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2118417bc4b8SEric Dumazet 	s64 remaining;
21192bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2120fd29cf72SStephen Hemminger 
21212bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
21222bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2123fd29cf72SStephen Hemminger 
212443d28b65SDaniel Turull 	remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2125417bc4b8SEric Dumazet 	if (remaining <= 0) {
2126417bc4b8SEric Dumazet 		pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21271da177e4SLinus Torvalds 		return;
2128417bc4b8SEric Dumazet 	}
21292bc481cfSStephen Hemminger 
2130*398f382cSDaniel Borkmann 	start_time = ktime_get();
213133136d12SEric Dumazet 	if (remaining < 100000) {
213233136d12SEric Dumazet 		/* for small delays (<100us), just loop until limit is reached */
213333136d12SEric Dumazet 		do {
2134*398f382cSDaniel Borkmann 			end_time = ktime_get();
2135*398f382cSDaniel Borkmann 		} while (ktime_compare(end_time, spin_until) < 0);
213633136d12SEric Dumazet 	} else {
21372bc481cfSStephen Hemminger 		/* see do_nanosleep */
21382bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
21392bc481cfSStephen Hemminger 		do {
21402bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
21412bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
21422bc481cfSStephen Hemminger 			if (!hrtimer_active(&t.timer))
21432bc481cfSStephen Hemminger 				t.task = NULL;
21442bc481cfSStephen Hemminger 
21452bc481cfSStephen Hemminger 			if (likely(t.task))
21461da177e4SLinus Torvalds 				schedule();
21471da177e4SLinus Torvalds 
21482bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
21492bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
21502bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
2151*398f382cSDaniel Borkmann 		end_time = ktime_get();
215233136d12SEric Dumazet 	}
2153ef87979cSStephen Hemminger 
2154ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
215507a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21561da177e4SLinus Torvalds }
21571da177e4SLinus Torvalds 
215816dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
215916dab72fSJamal Hadi Salim {
2160a553e4a6SJamal Hadi Salim 	pkt_dev->pkt_overhead = 0;
216116dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
216216dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
216316dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
216416dab72fSJamal Hadi Salim }
216516dab72fSJamal Hadi Salim 
2166648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2167007a531bSJamal Hadi Salim {
2168648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2169007a531bSJamal Hadi Salim }
2170007a531bSJamal Hadi Salim 
2171007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2172007a531bSJamal Hadi Salim {
2173007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2174007a531bSJamal Hadi Salim 
2175007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2176007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2177007a531bSJamal Hadi Salim 			/* reset time */
2178007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
21791211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2180007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2181007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2182007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2183007a531bSJamal Hadi Salim 		}
2184007a531bSJamal Hadi Salim 	} else {
2185007a531bSJamal Hadi Salim 		flow = random32() % pkt_dev->cflows;
21861211a645SRobert Olsson 		pkt_dev->curfl = flow;
2187007a531bSJamal Hadi Salim 
21881211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2189007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
21901211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
21911211a645SRobert Olsson 		}
2192007a531bSJamal Hadi Salim 	}
2193007a531bSJamal Hadi Salim 
2194007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2195007a531bSJamal Hadi Salim }
2196007a531bSJamal Hadi Salim 
2197a553e4a6SJamal Hadi Salim 
2198a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2199a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2200a553e4a6SJamal Hadi Salim  * we go look for it ...
2201a553e4a6SJamal Hadi Salim */
2202bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2203fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2204a553e4a6SJamal Hadi Salim {
2205a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
2206a553e4a6SJamal Hadi Salim 	if (!x) {
2207a553e4a6SJamal Hadi Salim 		/*slow path: we dont already have xfrm_state*/
2208bd55775cSJamal Hadi Salim 		x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
22095447c5e4SAlexey Dobriyan 					(xfrm_address_t *)&pkt_dev->cur_daddr,
2210a553e4a6SJamal Hadi Salim 					(xfrm_address_t *)&pkt_dev->cur_saddr,
2211a553e4a6SJamal Hadi Salim 					AF_INET,
2212a553e4a6SJamal Hadi Salim 					pkt_dev->ipsmode,
2213a553e4a6SJamal Hadi Salim 					pkt_dev->ipsproto, 0);
2214a553e4a6SJamal Hadi Salim 		if (x) {
2215a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2216a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2217a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2218a553e4a6SJamal Hadi Salim 		}
2219a553e4a6SJamal Hadi Salim 
2220a553e4a6SJamal Hadi Salim 	}
2221a553e4a6SJamal Hadi Salim }
2222a553e4a6SJamal Hadi Salim #endif
2223fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2224fd2ea0a7SDavid S. Miller {
2225e6fce5b9SRobert Olsson 
2226e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2227e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2228e6fce5b9SRobert Olsson 
2229896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2230fd2ea0a7SDavid S. Miller 		__u16 t;
2231fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2232fd2ea0a7SDavid S. Miller 			t = random32() %
2233fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2234fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2235fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2236fd2ea0a7SDavid S. Miller 		} else {
2237fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2238fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2239fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2240fd2ea0a7SDavid S. Miller 		}
2241fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2242fd2ea0a7SDavid S. Miller 	}
2243bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2244fd2ea0a7SDavid S. Miller }
2245fd2ea0a7SDavid S. Miller 
22461da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
22471da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
22481da177e4SLinus Torvalds  */
2249222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2250222f1806SLuiz Capitulino {
22511da177e4SLinus Torvalds 	__u32 imn;
22521da177e4SLinus Torvalds 	__u32 imx;
22531da177e4SLinus Torvalds 	int flow = 0;
22541da177e4SLinus Torvalds 
2255007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2256007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds 	/*  Deal with source MAC */
22591da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
22601da177e4SLinus Torvalds 		__u32 mc;
22611da177e4SLinus Torvalds 		__u32 tmp;
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
22645fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->src_mac_count;
22651da177e4SLinus Torvalds 		else {
22661da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2267ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2268222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
22691da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
22701da177e4SLinus Torvalds 		}
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
22731da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
22741da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
22751da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
22761da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
22771da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
22781da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
22791da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
22801da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
22811da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
22821da177e4SLinus Torvalds 	}
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
22851da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
22861da177e4SLinus Torvalds 		__u32 mc;
22871da177e4SLinus Torvalds 		__u32 tmp;
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
22905fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->dst_mac_count;
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds 		else {
22931da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2294ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2295222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
22961da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
22971da177e4SLinus Torvalds 			}
22981da177e4SLinus Torvalds 		}
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
23011da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
23021da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23031da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
23041da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23051da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
23061da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23071da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
23081da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
23091da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
23101da177e4SLinus Torvalds 	}
23111da177e4SLinus Torvalds 
2312ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
231395c96174SEric Dumazet 		unsigned int i;
2314ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2315ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2316ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
23175fa6fc76SStephen Hemminger 					     ((__force __be32)random32() &
2318ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2319ca6549afSSteven Whitehouse 	}
2320ca6549afSSteven Whitehouse 
232134954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
23225fa6fc76SStephen Hemminger 		pkt_dev->vlan_id = random32() & (4096-1);
232334954ddcSFrancesco Fondelli 	}
232434954ddcSFrancesco Fondelli 
232534954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
23265fa6fc76SStephen Hemminger 		pkt_dev->svlan_id = random32() & (4096 - 1);
232734954ddcSFrancesco Fondelli 	}
232834954ddcSFrancesco Fondelli 
23291da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
23301da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
23315fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_src = random32() %
23325fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
23335fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 		else {
23361da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
23371da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
23381da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
23391da177e4SLinus Torvalds 		}
23401da177e4SLinus Torvalds 	}
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
23431da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
23445fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_dst = random32() %
23455fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
23465fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2347222f1806SLuiz Capitulino 		} else {
23481da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
23491da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
23501da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
23511da177e4SLinus Torvalds 		}
23521da177e4SLinus Torvalds 	}
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
23551da177e4SLinus Torvalds 
235663adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
235763adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
235863adc6fbSStephen Hemminger 		if (imn < imx) {
23591da177e4SLinus Torvalds 			__u32 t;
23601da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
23615fa6fc76SStephen Hemminger 				t = random32() % (imx - imn) + imn;
23621da177e4SLinus Torvalds 			else {
23631da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
23641da177e4SLinus Torvalds 				t++;
236563adc6fbSStephen Hemminger 				if (t > imx)
23661da177e4SLinus Torvalds 					t = imn;
236763adc6fbSStephen Hemminger 
23681da177e4SLinus Torvalds 			}
23691da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
23701da177e4SLinus Torvalds 		}
23711da177e4SLinus Torvalds 
2372007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
23731da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
23741da177e4SLinus Torvalds 		} else {
2375252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2376252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2377252e3346SAl Viro 			if (imn < imx) {
23781da177e4SLinus Torvalds 				__u32 t;
2379252e3346SAl Viro 				__be32 s;
23801da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
23811da177e4SLinus Torvalds 
23825fa6fc76SStephen Hemminger 					t = random32() % (imx - imn) + imn;
2383252e3346SAl Viro 					s = htonl(t);
23841da177e4SLinus Torvalds 
238521cf2253SJoe Perches 					while (ipv4_is_loopback(s) ||
238621cf2253SJoe Perches 					       ipv4_is_multicast(s) ||
23871e637c74SJan Engelhardt 					       ipv4_is_lbcast(s) ||
238821cf2253SJoe Perches 					       ipv4_is_zeronet(s) ||
238921cf2253SJoe Perches 					       ipv4_is_local_multicast(s)) {
23905fa6fc76SStephen Hemminger 						t = random32() % (imx - imn) + imn;
2391252e3346SAl Viro 						s = htonl(t);
23921da177e4SLinus Torvalds 					}
2393252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2394252e3346SAl Viro 				} else {
23951da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
23961da177e4SLinus Torvalds 					t++;
23971da177e4SLinus Torvalds 					if (t > imx) {
23981da177e4SLinus Torvalds 						t = imn;
23991da177e4SLinus Torvalds 					}
24001da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
24011da177e4SLinus Torvalds 				}
24021da177e4SLinus Torvalds 			}
24031da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2404007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2405222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2406222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2407a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2408a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2409a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2410a553e4a6SJamal Hadi Salim #endif
24111da177e4SLinus Torvalds 				pkt_dev->nflows++;
24121da177e4SLinus Torvalds 			}
24131da177e4SLinus Torvalds 		}
2414222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2415222f1806SLuiz Capitulino 
241606e30411SJoe Perches 		if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
24171da177e4SLinus Torvalds 			int i;
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds 			/* Only random destinations yet */
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
24221da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
24235fa6fc76SStephen Hemminger 				    (((__force __be32)random32() |
24241da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
24251da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
24261da177e4SLinus Torvalds 			}
24271da177e4SLinus Torvalds 		}
24281da177e4SLinus Torvalds 	}
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
24311da177e4SLinus Torvalds 		__u32 t;
24321da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
24335fa6fc76SStephen Hemminger 			t = random32() %
24345fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
24355fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2436222f1806SLuiz Capitulino 		} else {
24371da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
24381da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
24391da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
24401da177e4SLinus Torvalds 		}
24411da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
24421da177e4SLinus Torvalds 	}
24431da177e4SLinus Torvalds 
2444fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
244545b270f8SRobert Olsson 
24461da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds 
2449a553e4a6SJamal Hadi Salim 
2450a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2451a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2452a553e4a6SJamal Hadi Salim {
2453a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2454a553e4a6SJamal Hadi Salim 	int err = 0;
2455a553e4a6SJamal Hadi Salim 
2456a553e4a6SJamal Hadi Salim 	if (!x)
2457a553e4a6SJamal Hadi Salim 		return 0;
2458a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2459a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2460a553e4a6SJamal Hadi Salim 	if (x->props.mode != XFRM_MODE_TRANSPORT)
2461a553e4a6SJamal Hadi Salim 		return 0;
2462a553e4a6SJamal Hadi Salim 
2463a553e4a6SJamal Hadi Salim 	spin_lock(&x->lock);
2464a553e4a6SJamal Hadi Salim 
246513996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2466a553e4a6SJamal Hadi Salim 	if (err)
2467a553e4a6SJamal Hadi Salim 		goto error;
2468a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
2469a553e4a6SJamal Hadi Salim 	if (err)
2470a553e4a6SJamal Hadi Salim 		goto error;
2471a553e4a6SJamal Hadi Salim 
2472a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2473a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
2474a553e4a6SJamal Hadi Salim error:
2475a553e4a6SJamal Hadi Salim 	spin_unlock(&x->lock);
2476a553e4a6SJamal Hadi Salim 	return err;
2477a553e4a6SJamal Hadi Salim }
2478a553e4a6SJamal Hadi Salim 
2479475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2480a553e4a6SJamal Hadi Salim {
2481a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2482a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2483d6182223SPaul Gortmaker 		int i;
2484d6182223SPaul Gortmaker 		for (i = 0; i < pkt_dev->cflows; i++) {
2485a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2486a553e4a6SJamal Hadi Salim 			if (x) {
2487a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2488a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2489a553e4a6SJamal Hadi Salim 			}
2490a553e4a6SJamal Hadi Salim 		}
2491a553e4a6SJamal Hadi Salim 	}
2492a553e4a6SJamal Hadi Salim }
2493a553e4a6SJamal Hadi Salim 
2494475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2495a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2496a553e4a6SJamal Hadi Salim {
2497a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2498a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2499a553e4a6SJamal Hadi Salim 		int nhead = 0;
2500a553e4a6SJamal Hadi Salim 		if (x) {
2501a553e4a6SJamal Hadi Salim 			int ret;
2502a553e4a6SJamal Hadi Salim 			__u8 *eth;
2503a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2504a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2505a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2506a553e4a6SJamal Hadi Salim 				if (ret < 0) {
2507f9467eaeSJoe Perches 					pr_err("Error expanding ipsec packet %d\n",
2508f9467eaeSJoe Perches 					       ret);
2509b4bb4ac8SIlpo Järvinen 					goto err;
2510a553e4a6SJamal Hadi Salim 				}
2511a553e4a6SJamal Hadi Salim 			}
2512a553e4a6SJamal Hadi Salim 
2513a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2514a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2515a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2516a553e4a6SJamal Hadi Salim 			if (ret) {
2517f9467eaeSJoe Perches 				pr_err("Error creating ipsec packet %d\n", ret);
2518b4bb4ac8SIlpo Järvinen 				goto err;
2519a553e4a6SJamal Hadi Salim 			}
2520a553e4a6SJamal Hadi Salim 			/* restore ll */
2521a553e4a6SJamal Hadi Salim 			eth = (__u8 *) skb_push(skb, ETH_HLEN);
2522a553e4a6SJamal Hadi Salim 			memcpy(eth, pkt_dev->hh, 12);
2523a553e4a6SJamal Hadi Salim 			*(u16 *) &eth[12] = protocol;
2524a553e4a6SJamal Hadi Salim 		}
2525a553e4a6SJamal Hadi Salim 	}
2526a553e4a6SJamal Hadi Salim 	return 1;
2527b4bb4ac8SIlpo Järvinen err:
2528b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2529b4bb4ac8SIlpo Järvinen 	return 0;
2530a553e4a6SJamal Hadi Salim }
2531a553e4a6SJamal Hadi Salim #endif
2532a553e4a6SJamal Hadi Salim 
2533ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2534ca6549afSSteven Whitehouse {
253595c96174SEric Dumazet 	unsigned int i;
253663adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2537ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
253863adc6fbSStephen Hemminger 
2539ca6549afSSteven Whitehouse 	mpls--;
2540ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2541ca6549afSSteven Whitehouse }
2542ca6549afSSteven Whitehouse 
25430f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
25440f37c605SAl Viro 			       unsigned int prio)
25450f37c605SAl Viro {
25460f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
25470f37c605SAl Viro }
25480f37c605SAl Viro 
254926ad7879SEric Dumazet static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
255026ad7879SEric Dumazet 				int datalen)
255126ad7879SEric Dumazet {
255226ad7879SEric Dumazet 	struct timeval timestamp;
255326ad7879SEric Dumazet 	struct pktgen_hdr *pgh;
255426ad7879SEric Dumazet 
255526ad7879SEric Dumazet 	pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
255626ad7879SEric Dumazet 	datalen -= sizeof(*pgh);
255726ad7879SEric Dumazet 
255826ad7879SEric Dumazet 	if (pkt_dev->nfrags <= 0) {
255905aebe2eSDaniel Turull 		memset(skb_put(skb, datalen), 0, datalen);
256026ad7879SEric Dumazet 	} else {
256126ad7879SEric Dumazet 		int frags = pkt_dev->nfrags;
256226ad7879SEric Dumazet 		int i, len;
25637d36a991Samit salecha 		int frag_len;
256426ad7879SEric Dumazet 
256526ad7879SEric Dumazet 
256626ad7879SEric Dumazet 		if (frags > MAX_SKB_FRAGS)
256726ad7879SEric Dumazet 			frags = MAX_SKB_FRAGS;
256826ad7879SEric Dumazet 		len = datalen - frags * PAGE_SIZE;
256926ad7879SEric Dumazet 		if (len > 0) {
257026ad7879SEric Dumazet 			memset(skb_put(skb, len), 0, len);
257126ad7879SEric Dumazet 			datalen = frags * PAGE_SIZE;
257226ad7879SEric Dumazet 		}
257326ad7879SEric Dumazet 
257426ad7879SEric Dumazet 		i = 0;
25757d36a991Samit salecha 		frag_len = (datalen/frags) < PAGE_SIZE ?
25767d36a991Samit salecha 			   (datalen/frags) : PAGE_SIZE;
257726ad7879SEric Dumazet 		while (datalen > 0) {
257826ad7879SEric Dumazet 			if (unlikely(!pkt_dev->page)) {
257926ad7879SEric Dumazet 				int node = numa_node_id();
258026ad7879SEric Dumazet 
258126ad7879SEric Dumazet 				if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
258226ad7879SEric Dumazet 					node = pkt_dev->node;
258326ad7879SEric Dumazet 				pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
258426ad7879SEric Dumazet 				if (!pkt_dev->page)
258526ad7879SEric Dumazet 					break;
258626ad7879SEric Dumazet 			}
2587a0bec1cdSIan Campbell 			get_page(pkt_dev->page);
2588ea2ab693SIan Campbell 			skb_frag_set_page(skb, i, pkt_dev->page);
258926ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page_offset = 0;
25907d36a991Samit salecha 			/*last fragment, fill rest of data*/
25917d36a991Samit salecha 			if (i == (frags - 1))
25929e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i],
25939e903e08SEric Dumazet 				    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
25947d36a991Samit salecha 			else
25959e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
25969e903e08SEric Dumazet 			datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
25979e903e08SEric Dumazet 			skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
25989e903e08SEric Dumazet 			skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
259926ad7879SEric Dumazet 			i++;
260026ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
260126ad7879SEric Dumazet 		}
260226ad7879SEric Dumazet 	}
260326ad7879SEric Dumazet 
260426ad7879SEric Dumazet 	/* Stamp the time, and sequence number,
260526ad7879SEric Dumazet 	 * convert them to network byte order
260626ad7879SEric Dumazet 	 */
260726ad7879SEric Dumazet 	pgh->pgh_magic = htonl(PKTGEN_MAGIC);
260826ad7879SEric Dumazet 	pgh->seq_num = htonl(pkt_dev->seq_num);
260926ad7879SEric Dumazet 
261026ad7879SEric Dumazet 	do_gettimeofday(&timestamp);
261126ad7879SEric Dumazet 	pgh->tv_sec = htonl(timestamp.tv_sec);
261226ad7879SEric Dumazet 	pgh->tv_usec = htonl(timestamp.tv_usec);
261326ad7879SEric Dumazet }
261426ad7879SEric Dumazet 
26151da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
26161da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
26171da177e4SLinus Torvalds {
26181da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
26191da177e4SLinus Torvalds 	__u8 *eth;
26201da177e4SLinus Torvalds 	struct udphdr *udph;
26211da177e4SLinus Torvalds 	int datalen, iplen;
26221da177e4SLinus Torvalds 	struct iphdr *iph;
2623d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2624ca6549afSSteven Whitehouse 	__be32 *mpls;
262534954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
262634954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
262734954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
262834954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2629fd2ea0a7SDavid S. Miller 	u16 queue_map;
2630ca6549afSSteven Whitehouse 
2631ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2632d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
26331da177e4SLinus Torvalds 
263434954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2635d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
263634954ddcSFrancesco Fondelli 
263764053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
263864053beeSRobert Olsson 	 * fields.
263964053beeSRobert Olsson 	 */
264064053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2641eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
264264053beeSRobert Olsson 
26437ac5459eSDavid S. Miller 	datalen = (odev->hard_header_len + 16) & ~0xf;
2644e99b99b4SRobert Olsson 
2645e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE) {
2646e99b99b4SRobert Olsson 		int node;
2647e99b99b4SRobert Olsson 
2648e99b99b4SRobert Olsson 		if (pkt_dev->node >= 0)
2649e99b99b4SRobert Olsson 			node = pkt_dev->node;
2650e99b99b4SRobert Olsson 		else
2651e99b99b4SRobert Olsson 			node =  numa_node_id();
2652e99b99b4SRobert Olsson 
2653e99b99b4SRobert Olsson 		skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2654e99b99b4SRobert Olsson 				  + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2655e99b99b4SRobert Olsson 		if (likely(skb)) {
2656e99b99b4SRobert Olsson 			skb_reserve(skb, NET_SKB_PAD);
2657e99b99b4SRobert Olsson 			skb->dev = odev;
2658e99b99b4SRobert Olsson 		}
2659e99b99b4SRobert Olsson 	}
2660e99b99b4SRobert Olsson 	else
2661e470757dSStephen Hemminger 	  skb = __netdev_alloc_skb(odev,
2662e470757dSStephen Hemminger 				   pkt_dev->cur_pkt_size + 64
2663e470757dSStephen Hemminger 				   + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2664e99b99b4SRobert Olsson 
26651da177e4SLinus Torvalds 	if (!skb) {
26661da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
26671da177e4SLinus Torvalds 		return NULL;
26681da177e4SLinus Torvalds 	}
2669a8d764b9SJunchang Wang 	prefetchw(skb->data);
26701da177e4SLinus Torvalds 
26717ac5459eSDavid S. Miller 	skb_reserve(skb, datalen);
26721da177e4SLinus Torvalds 
26731da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
26741da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2675ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2676ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2677ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
267834954ddcSFrancesco Fondelli 
267934954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
268034954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
268134954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
26820f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
26830f37c605SAl Viro 					       pkt_dev->svlan_cfi,
26840f37c605SAl Viro 					       pkt_dev->svlan_p);
268534954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2686d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
268734954ddcSFrancesco Fondelli 		}
268834954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
26890f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
26900f37c605SAl Viro 				      pkt_dev->vlan_cfi,
26910f37c605SAl Viro 				      pkt_dev->vlan_p);
269234954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2693d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
269434954ddcSFrancesco Fondelli 	}
269534954ddcSFrancesco Fondelli 
269627a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
2697b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct iphdr);
2698ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2699fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
27009e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
27019e50e3acSJohn Fastabend 
2702ddc7b8e3SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
2703ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
27041da177e4SLinus Torvalds 
27051da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2706252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
27071da177e4SLinus Torvalds 
2708ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2709ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
271016dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
27116af773e7SNishank Trivedi 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
27121da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
27151da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
27161da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
27171da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	iph->ihl = 5;
27201da177e4SLinus Torvalds 	iph->version = 4;
27211da177e4SLinus Torvalds 	iph->ttl = 32;
27221ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
27231da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
27241da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
27251da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
272666ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
272766ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
27281da177e4SLinus Torvalds 	iph->frag_off = 0;
27291da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
27301da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
27311da177e4SLinus Torvalds 	iph->check = 0;
27321da177e4SLinus Torvalds 	iph->check = ip_fast_csum((void *)iph, iph->ihl);
2733ca6549afSSteven Whitehouse 	skb->protocol = protocol;
2734b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
273516dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
27361da177e4SLinus Torvalds 	skb->dev = odev;
27371da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
273826ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
27391da177e4SLinus Torvalds 
2740a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2741a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2742a553e4a6SJamal Hadi Salim 		return NULL;
2743a553e4a6SJamal Hadi Salim #endif
2744a553e4a6SJamal Hadi Salim 
27451da177e4SLinus Torvalds 	return skb;
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
27491da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
27501da177e4SLinus Torvalds {
27511da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
27521da177e4SLinus Torvalds 	__u8 *eth;
27531da177e4SLinus Torvalds 	struct udphdr *udph;
27541da177e4SLinus Torvalds 	int datalen;
27551da177e4SLinus Torvalds 	struct ipv6hdr *iph;
2756d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2757ca6549afSSteven Whitehouse 	__be32 *mpls;
275834954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
275934954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
276034954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
276134954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2762fd2ea0a7SDavid S. Miller 	u16 queue_map;
2763ca6549afSSteven Whitehouse 
2764ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2765d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
27661da177e4SLinus Torvalds 
276734954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2768d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
276934954ddcSFrancesco Fondelli 
277064053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
277164053beeSRobert Olsson 	 * fields.
277264053beeSRobert Olsson 	 */
277364053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2774eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
277564053beeSRobert Olsson 
2776e470757dSStephen Hemminger 	skb = __netdev_alloc_skb(odev,
2777e470757dSStephen Hemminger 				 pkt_dev->cur_pkt_size + 64
2778e470757dSStephen Hemminger 				 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
27791da177e4SLinus Torvalds 	if (!skb) {
27801da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
27811da177e4SLinus Torvalds 		return NULL;
27821da177e4SLinus Torvalds 	}
2783a8d764b9SJunchang Wang 	prefetchw(skb->data);
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds 	skb_reserve(skb, 16);
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
27881da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2789ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2790ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2791ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
279234954ddcSFrancesco Fondelli 
279334954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
279434954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
279534954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27960f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
27970f37c605SAl Viro 					       pkt_dev->svlan_cfi,
27980f37c605SAl Viro 					       pkt_dev->svlan_p);
279934954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2800d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
280134954ddcSFrancesco Fondelli 		}
280234954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28030f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
28040f37c605SAl Viro 				      pkt_dev->vlan_cfi,
28050f37c605SAl Viro 				      pkt_dev->vlan_p);
280634954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2807d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
280834954ddcSFrancesco Fondelli 	}
280934954ddcSFrancesco Fondelli 
281027a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
2811b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2812ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2813fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
28149e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
2815ddc7b8e3SArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
2816ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2819252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
28201da177e4SLinus Torvalds 
2821ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2822ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
2823ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
282416dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
28251da177e4SLinus Torvalds 
28265aa8b572SAmerigo Wang 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
28271da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
2828e87cc472SJoe Perches 		net_info_ratelimited("increased datalen to %d\n", datalen);
28291da177e4SLinus Torvalds 	}
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
28321da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
28331da177e4SLinus Torvalds 	udph->len = htons(datalen + sizeof(struct udphdr));
28341da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
28351da177e4SLinus Torvalds 
2836d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
28371da177e4SLinus Torvalds 
28381ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
28391ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
2840252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
28411ca7768cSFrancesco Fondelli 	}
28421ca7768cSFrancesco Fondelli 
28431da177e4SLinus Torvalds 	iph->hop_limit = 32;
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds 	iph->payload_len = htons(sizeof(struct udphdr) + datalen);
28461da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
28471da177e4SLinus Torvalds 
28484e3fd7a0SAlexey Dobriyan 	iph->daddr = pkt_dev->cur_in6_daddr;
28494e3fd7a0SAlexey Dobriyan 	iph->saddr = pkt_dev->cur_in6_saddr;
28501da177e4SLinus Torvalds 
2851b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
285216dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
2853ca6549afSSteven Whitehouse 	skb->protocol = protocol;
28541da177e4SLinus Torvalds 	skb->dev = odev;
28551da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
28561da177e4SLinus Torvalds 
285726ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
28581da177e4SLinus Torvalds 
28591da177e4SLinus Torvalds 	return skb;
28601da177e4SLinus Torvalds }
28611da177e4SLinus Torvalds 
2862475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
28631da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
28641da177e4SLinus Torvalds {
28651da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
28661da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
28671da177e4SLinus Torvalds 	else
28681da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
28691da177e4SLinus Torvalds }
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
28721da177e4SLinus Torvalds {
28731da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
28741da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
28751da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
28761da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
28771da177e4SLinus Torvalds 	pkt_dev->errors = 0;
28781da177e4SLinus Torvalds }
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
28831da177e4SLinus Torvalds {
2884c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
28851da177e4SLinus Torvalds 	int started = 0;
28861da177e4SLinus Torvalds 
2887f9467eaeSJoe Perches 	func_enter();
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	if_lock(t);
2890c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds 		/*
28931da177e4SLinus Torvalds 		 * setup odev and create initial packet.
28941da177e4SLinus Torvalds 		 */
28951da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
28961da177e4SLinus Torvalds 
28971da177e4SLinus Torvalds 		if (pkt_dev->odev) {
28981da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
28991da177e4SLinus Torvalds 			pkt_dev->running = 1;	/* Cranke yeself! */
29001da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
2901*398f382cSDaniel Borkmann 			pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
2902fd29cf72SStephen Hemminger 
290316dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
29061da177e4SLinus Torvalds 			started++;
2907222f1806SLuiz Capitulino 		} else
29081da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
29091da177e4SLinus Torvalds 	}
29101da177e4SLinus Torvalds 	if_unlock(t);
2911222f1806SLuiz Capitulino 	if (started)
2912222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
29131da177e4SLinus Torvalds }
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void)
29161da177e4SLinus Torvalds {
2917cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
29181da177e4SLinus Torvalds 
2919f9467eaeSJoe Perches 	func_enter();
29201da177e4SLinus Torvalds 
29216146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
2922cdcdbe0bSLuiz Capitulino 
2923cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list)
292495ed63f7SArthur Kepner 		t->control |= T_STOP;
2925cdcdbe0bSLuiz Capitulino 
29266146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
29271da177e4SLinus Torvalds }
29281da177e4SLinus Torvalds 
2929648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
29301da177e4SLinus Torvalds {
2931648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
29321da177e4SLinus Torvalds 
2933c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
2934648fda74SStephen Hemminger 		if (pkt_dev->running)
2935648fda74SStephen Hemminger 			return 1;
2936648fda74SStephen Hemminger 	return 0;
29371da177e4SLinus Torvalds }
29381da177e4SLinus Torvalds 
29391da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
29401da177e4SLinus Torvalds {
29411da177e4SLinus Torvalds 	if_lock(t);
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds 	while (thread_is_running(t)) {
29441da177e4SLinus Torvalds 
29451da177e4SLinus Torvalds 		if_unlock(t);
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds 		msleep_interruptible(100);
29481da177e4SLinus Torvalds 
29491da177e4SLinus Torvalds 		if (signal_pending(current))
29501da177e4SLinus Torvalds 			goto signal;
29511da177e4SLinus Torvalds 		if_lock(t);
29521da177e4SLinus Torvalds 	}
29531da177e4SLinus Torvalds 	if_unlock(t);
29541da177e4SLinus Torvalds 	return 1;
29551da177e4SLinus Torvalds signal:
29561da177e4SLinus Torvalds 	return 0;
29571da177e4SLinus Torvalds }
29581da177e4SLinus Torvalds 
29591da177e4SLinus Torvalds static int pktgen_wait_all_threads_run(void)
29601da177e4SLinus Torvalds {
2961cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
29621da177e4SLinus Torvalds 	int sig = 1;
29631da177e4SLinus Torvalds 
29646146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
2965cdcdbe0bSLuiz Capitulino 
2966cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list) {
29671da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
2968222f1806SLuiz Capitulino 		if (sig == 0)
2969222f1806SLuiz Capitulino 			break;
29701da177e4SLinus Torvalds 	}
2971cdcdbe0bSLuiz Capitulino 
2972cdcdbe0bSLuiz Capitulino 	if (sig == 0)
2973cdcdbe0bSLuiz Capitulino 		list_for_each_entry(t, &pktgen_threads, th_list)
29741da177e4SLinus Torvalds 			t->control |= (T_STOP);
2975cdcdbe0bSLuiz Capitulino 
29766146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
29771da177e4SLinus Torvalds 	return sig;
29781da177e4SLinus Torvalds }
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds static void pktgen_run_all_threads(void)
29811da177e4SLinus Torvalds {
2982cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
29831da177e4SLinus Torvalds 
2984f9467eaeSJoe Perches 	func_enter();
29851da177e4SLinus Torvalds 
29866146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
29871da177e4SLinus Torvalds 
2988cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list)
29891da177e4SLinus Torvalds 		t->control |= (T_RUN);
2990cdcdbe0bSLuiz Capitulino 
29916146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
29921da177e4SLinus Torvalds 
299363adc6fbSStephen Hemminger 	/* Propagate thread->control  */
299463adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds 	pktgen_wait_all_threads_run();
29971da177e4SLinus Torvalds }
29981da177e4SLinus Torvalds 
2999eb37b41cSJesse Brandeburg static void pktgen_reset_all_threads(void)
3000eb37b41cSJesse Brandeburg {
3001eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3002eb37b41cSJesse Brandeburg 
3003f9467eaeSJoe Perches 	func_enter();
3004eb37b41cSJesse Brandeburg 
3005eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3006eb37b41cSJesse Brandeburg 
3007eb37b41cSJesse Brandeburg 	list_for_each_entry(t, &pktgen_threads, th_list)
3008eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3009eb37b41cSJesse Brandeburg 
3010eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3011eb37b41cSJesse Brandeburg 
301263adc6fbSStephen Hemminger 	/* Propagate thread->control  */
301363adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3014eb37b41cSJesse Brandeburg 
3015eb37b41cSJesse Brandeburg 	pktgen_wait_all_threads_run();
3016eb37b41cSJesse Brandeburg }
3017eb37b41cSJesse Brandeburg 
30181da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
30191da177e4SLinus Torvalds {
3020fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
30211da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3022fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3023fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3024fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
30251da177e4SLinus Torvalds 
302603a14ab1SDaniel Turull 	p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3027fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3028fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3029fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
30301da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
30311da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
30321da177e4SLinus Torvalds 
3033fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3034fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds 	mbps = bps;
30391da177e4SLinus Torvalds 	do_div(mbps, 1000000);
30401da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
30411da177e4SLinus Torvalds 		     (unsigned long long)pps,
30421da177e4SLinus Torvalds 		     (unsigned long long)mbps,
30431da177e4SLinus Torvalds 		     (unsigned long long)bps,
30441da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
30451da177e4SLinus Torvalds }
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
30481da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
30491da177e4SLinus Torvalds {
3050222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds 	if (!pkt_dev->running) {
3053f9467eaeSJoe Perches 		pr_warning("interface: %s is already stopped\n",
3054f9467eaeSJoe Perches 			   pkt_dev->odevname);
30551da177e4SLinus Torvalds 		return -EINVAL;
30561da177e4SLinus Torvalds 	}
30571da177e4SLinus Torvalds 
30583bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
30593bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3060*398f382cSDaniel Borkmann 	pkt_dev->stopped_at = ktime_get();
30611da177e4SLinus Torvalds 	pkt_dev->running = 0;
30621da177e4SLinus Torvalds 
306395ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 	return 0;
30661da177e4SLinus Torvalds }
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
30691da177e4SLinus Torvalds {
3070c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds 	if_lock(t);
30731da177e4SLinus Torvalds 
3074c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3075c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3076222f1806SLuiz Capitulino 			continue;
3077222f1806SLuiz Capitulino 		if (best == NULL)
3078c26a8016SLuiz Capitulino 			best = pkt_dev;
3079*398f382cSDaniel Borkmann 		else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3080c26a8016SLuiz Capitulino 			best = pkt_dev;
30811da177e4SLinus Torvalds 	}
30821da177e4SLinus Torvalds 	if_unlock(t);
30831da177e4SLinus Torvalds 	return best;
30841da177e4SLinus Torvalds }
30851da177e4SLinus Torvalds 
3086222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3087222f1806SLuiz Capitulino {
3088c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
30891da177e4SLinus Torvalds 
3090f9467eaeSJoe Perches 	func_enter();
30911da177e4SLinus Torvalds 
30921da177e4SLinus Torvalds 	if_lock(t);
30931da177e4SLinus Torvalds 
3094c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3095c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
309695ed63f7SArthur Kepner 	}
309795ed63f7SArthur Kepner 
309895ed63f7SArthur Kepner 	if_unlock(t);
309995ed63f7SArthur Kepner }
310095ed63f7SArthur Kepner 
310195ed63f7SArthur Kepner /*
310295ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
310395ed63f7SArthur Kepner  * and remove it
310495ed63f7SArthur Kepner  */
310595ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
310695ed63f7SArthur Kepner {
3107c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3108c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
310995ed63f7SArthur Kepner 
3110f9467eaeSJoe Perches 	func_enter();
311195ed63f7SArthur Kepner 
311295ed63f7SArthur Kepner 	if_lock(t);
311395ed63f7SArthur Kepner 
3114c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3115c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
311695ed63f7SArthur Kepner 
3117222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3118222f1806SLuiz Capitulino 			continue;
311995ed63f7SArthur Kepner 
312095ed63f7SArthur Kepner 		kfree_skb(cur->skb);
312195ed63f7SArthur Kepner 		cur->skb = NULL;
312295ed63f7SArthur Kepner 
312395ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
312495ed63f7SArthur Kepner 
312595ed63f7SArthur Kepner 		break;
312695ed63f7SArthur Kepner 	}
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 	if_unlock(t);
31291da177e4SLinus Torvalds }
31301da177e4SLinus Torvalds 
31311da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
31321da177e4SLinus Torvalds {
3133c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3134c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
31351da177e4SLinus Torvalds 
3136f9467eaeSJoe Perches 	func_enter();
3137f9467eaeSJoe Perches 
31381da177e4SLinus Torvalds 	/* Remove all devices, free mem */
31391da177e4SLinus Torvalds 
31401da177e4SLinus Torvalds 	if_lock(t);
31411da177e4SLinus Torvalds 
3142c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3143c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
314495ed63f7SArthur Kepner 
314595ed63f7SArthur Kepner 		kfree_skb(cur->skb);
314695ed63f7SArthur Kepner 		cur->skb = NULL;
314795ed63f7SArthur Kepner 
31481da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
31491da177e4SLinus Torvalds 	}
31501da177e4SLinus Torvalds 
31511da177e4SLinus Torvalds 	if_unlock(t);
31521da177e4SLinus Torvalds }
31531da177e4SLinus Torvalds 
31541da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
31551da177e4SLinus Torvalds {
31561da177e4SLinus Torvalds 	/* Remove from the thread list */
31571da177e4SLinus Torvalds 
3158ee74baa7SDavid S. Miller 	remove_proc_entry(t->tsk->comm, pg_proc_dir);
31591da177e4SLinus Torvalds 
31601da177e4SLinus Torvalds }
31611da177e4SLinus Torvalds 
3162ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
31633791decbSStephen Hemminger {
3164*398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
31653791decbSStephen Hemminger 	schedule();
3166*398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
31673791decbSStephen Hemminger }
31683791decbSStephen Hemminger 
3169ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3170ef87979cSStephen Hemminger {
3171*398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
3172ef87979cSStephen Hemminger 
3173ef87979cSStephen Hemminger 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3174ef87979cSStephen Hemminger 		if (signal_pending(current))
3175ef87979cSStephen Hemminger 			break;
3176ef87979cSStephen Hemminger 
3177ef87979cSStephen Hemminger 		if (need_resched())
3178ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3179ef87979cSStephen Hemminger 		else
3180ef87979cSStephen Hemminger 			cpu_relax();
3181ef87979cSStephen Hemminger 	}
3182*398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3183ef87979cSStephen Hemminger }
3184fd29cf72SStephen Hemminger 
3185475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
31861da177e4SLinus Torvalds {
318700829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
31886fef4c0cSStephen Hemminger 	netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
318900829823SStephen Hemminger 		= odev->netdev_ops->ndo_start_xmit;
3190fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
3191fd2ea0a7SDavid S. Miller 	u16 queue_map;
31921da177e4SLinus Torvalds 	int ret;
31931da177e4SLinus Torvalds 
3194ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3195ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
31961da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
31973791decbSStephen Hemminger 		return;
31981da177e4SLinus Torvalds 	}
31991da177e4SLinus Torvalds 
3200ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3201ef87979cSStephen Hemminger 	 * "never transmit"
3202ef87979cSStephen Hemminger 	 */
3203ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3204*398f382cSDaniel Borkmann 		pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3205ef87979cSStephen Hemminger 		return;
3206ef87979cSStephen Hemminger 	}
3207ef87979cSStephen Hemminger 
3208ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
32097d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
32107d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
32111da177e4SLinus Torvalds 		/* build a new pkt */
32121da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
32131da177e4SLinus Torvalds 
32141da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
32151da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
3216f9467eaeSJoe Perches 			pr_err("ERROR: couldn't allocate skb in fill_packet\n");
32171da177e4SLinus Torvalds 			schedule();
32181da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
32193791decbSStephen Hemminger 			return;
32201da177e4SLinus Torvalds 		}
3221baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
32221da177e4SLinus Torvalds 		pkt_dev->allocated_skbs++;
32231da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
32241da177e4SLinus Torvalds 	}
32251da177e4SLinus Torvalds 
3226ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3227ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3228ef87979cSStephen Hemminger 
3229fd2ea0a7SDavid S. Miller 	queue_map = skb_get_queue_mapping(pkt_dev->skb);
3230fd2ea0a7SDavid S. Miller 	txq = netdev_get_tx_queue(odev, queue_map);
3231fd2ea0a7SDavid S. Miller 
3232fd2ea0a7SDavid S. Miller 	__netif_tx_lock_bh(txq);
32335b8db2f5SStephen Hemminger 
323473466498STom Herbert 	if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
3235ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
32360835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
32370835acfeSEric Dumazet 		goto unlock;
32380835acfeSEric Dumazet 	}
32390835acfeSEric Dumazet 	atomic_inc(&(pkt_dev->skb->users));
324000829823SStephen Hemminger 	ret = (*xmit)(pkt_dev->skb, odev);
3241ef87979cSStephen Hemminger 
32425b8db2f5SStephen Hemminger 	switch (ret) {
32435b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
324408baf561SEric Dumazet 		txq_trans_update(txq);
32451da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
32461da177e4SLinus Torvalds 		pkt_dev->sofar++;
32471da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3248baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
32495b8db2f5SStephen Hemminger 		break;
3250f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3251f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3252f466dba1SJohn Fastabend 	case NET_XMIT_POLICED:
3253f466dba1SJohn Fastabend 		/* skb has been consumed */
3254f466dba1SJohn Fastabend 		pkt_dev->errors++;
3255f466dba1SJohn Fastabend 		break;
32565b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
3257e87cc472SJoe Perches 		net_info_ratelimited("%s xmit error: %d\n",
3258e87cc472SJoe Perches 				     pkt_dev->odevname, ret);
32591da177e4SLinus Torvalds 		pkt_dev->errors++;
32605b8db2f5SStephen Hemminger 		/* fallthru */
3261ef87979cSStephen Hemminger 	case NETDEV_TX_LOCKED:
32625b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
32635b8db2f5SStephen Hemminger 		/* Retry it next time */
32645b8db2f5SStephen Hemminger 		atomic_dec(&(pkt_dev->skb->users));
32651da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
32661da177e4SLinus Torvalds 	}
32670835acfeSEric Dumazet unlock:
3268fd2ea0a7SDavid S. Miller 	__netif_tx_unlock_bh(txq);
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
32711da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3272ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 		/* Done with this */
32751da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
32761da177e4SLinus Torvalds 	}
32771da177e4SLinus Torvalds }
32781da177e4SLinus Torvalds 
32791da177e4SLinus Torvalds /*
32801da177e4SLinus Torvalds  * Main loop of the thread goes here
32811da177e4SLinus Torvalds  */
32821da177e4SLinus Torvalds 
3283ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
32841da177e4SLinus Torvalds {
32851da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3286ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
32871da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
32881da177e4SLinus Torvalds 	int cpu = t->cpu;
32891da177e4SLinus Torvalds 
3290ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
32911da177e4SLinus Torvalds 
32921da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3293d3ede327SDenis V. Lunev 	complete(&t->start_done);
32941da177e4SLinus Torvalds 
3295f9467eaeSJoe Perches 	pr_debug("starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
32961da177e4SLinus Torvalds 
3297ee74baa7SDavid S. Miller 	set_current_state(TASK_INTERRUPTIBLE);
32981da177e4SLinus Torvalds 
329983144186SRafael J. Wysocki 	set_freezable();
330083144186SRafael J. Wysocki 
3301ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3302ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3303ee74baa7SDavid S. Miller 
3304ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
3305551eaff1SEric Dumazet 			if (pktgen_exiting)
3306551eaff1SEric Dumazet 				break;
3307ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3308ef87979cSStephen Hemminger 							 t->control != 0,
3309ef87979cSStephen Hemminger 							 HZ/10);
33101b3f720bSRafael J. Wysocki 			try_to_freeze();
3311ef87979cSStephen Hemminger 			continue;
3312ee74baa7SDavid S. Miller 		}
33131da177e4SLinus Torvalds 
33141da177e4SLinus Torvalds 		__set_current_state(TASK_RUNNING);
33151da177e4SLinus Torvalds 
3316ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
33171da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
33181da177e4SLinus Torvalds 
3319ef87979cSStephen Hemminger 			if (need_resched())
3320ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3321ef87979cSStephen Hemminger 			else
3322ef87979cSStephen Hemminger 				cpu_relax();
3323ef87979cSStephen Hemminger 		}
3324ef87979cSStephen Hemminger 
33251da177e4SLinus Torvalds 		if (t->control & T_STOP) {
33261da177e4SLinus Torvalds 			pktgen_stop(t);
33271da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
33281da177e4SLinus Torvalds 		}
33291da177e4SLinus Torvalds 
33301da177e4SLinus Torvalds 		if (t->control & T_RUN) {
33311da177e4SLinus Torvalds 			pktgen_run(t);
33321da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
33331da177e4SLinus Torvalds 		}
33341da177e4SLinus Torvalds 
333595ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
33361da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
333795ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
333895ed63f7SArthur Kepner 		}
333995ed63f7SArthur Kepner 
334095ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
334195ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
33421da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
33431da177e4SLinus Torvalds 		}
33441da177e4SLinus Torvalds 
334509fe3ef4SAndrew Morton 		try_to_freeze();
334609fe3ef4SAndrew Morton 
3347ee74baa7SDavid S. Miller 		set_current_state(TASK_INTERRUPTIBLE);
33481da177e4SLinus Torvalds 	}
33491da177e4SLinus Torvalds 
3350f9467eaeSJoe Perches 	pr_debug("%s stopping all device\n", t->tsk->comm);
33511da177e4SLinus Torvalds 	pktgen_stop(t);
33521da177e4SLinus Torvalds 
3353f9467eaeSJoe Perches 	pr_debug("%s removing all device\n", t->tsk->comm);
33541da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
33551da177e4SLinus Torvalds 
3356f9467eaeSJoe Perches 	pr_debug("%s removing thread\n", t->tsk->comm);
33571da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3358cdcdbe0bSLuiz Capitulino 
3359551eaff1SEric Dumazet 	/* Wait for kthread_stop */
3360551eaff1SEric Dumazet 	while (!kthread_should_stop()) {
3361551eaff1SEric Dumazet 		set_current_state(TASK_INTERRUPTIBLE);
3362551eaff1SEric Dumazet 		schedule();
3363551eaff1SEric Dumazet 	}
3364551eaff1SEric Dumazet 	__set_current_state(TASK_RUNNING);
3365551eaff1SEric Dumazet 
3366ee74baa7SDavid S. Miller 	return 0;
33671da177e4SLinus Torvalds }
33681da177e4SLinus Torvalds 
3369222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
33703e984840SEric Dumazet 					  const char *ifname, bool exact)
33711da177e4SLinus Torvalds {
3372c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
33733e984840SEric Dumazet 	size_t len = strlen(ifname);
33741da177e4SLinus Torvalds 
33753e984840SEric Dumazet 	if_lock(t);
3376c26a8016SLuiz Capitulino 	list_for_each_entry(p, &t->if_list, list)
33773e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
33783e984840SEric Dumazet 			if (p->odevname[len]) {
33793e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
33803e984840SEric Dumazet 					continue;
33813e984840SEric Dumazet 			}
3382c26a8016SLuiz Capitulino 			pkt_dev = p;
33831da177e4SLinus Torvalds 			break;
33841da177e4SLinus Torvalds 		}
33851da177e4SLinus Torvalds 
33861da177e4SLinus Torvalds 	if_unlock(t);
3387f9467eaeSJoe Perches 	pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
33881da177e4SLinus Torvalds 	return pkt_dev;
33891da177e4SLinus Torvalds }
33901da177e4SLinus Torvalds 
33911da177e4SLinus Torvalds /*
33921da177e4SLinus Torvalds  * Adds a dev at front of if_list.
33931da177e4SLinus Torvalds  */
33941da177e4SLinus Torvalds 
3395222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3396222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
33971da177e4SLinus Torvalds {
33981da177e4SLinus Torvalds 	int rv = 0;
33991da177e4SLinus Torvalds 
34001da177e4SLinus Torvalds 	if_lock(t);
34011da177e4SLinus Torvalds 
34021da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
3403f9467eaeSJoe Perches 		pr_err("ERROR: already assigned to a thread\n");
34041da177e4SLinus Torvalds 		rv = -EBUSY;
34051da177e4SLinus Torvalds 		goto out;
34061da177e4SLinus Torvalds 	}
3407c26a8016SLuiz Capitulino 
3408c26a8016SLuiz Capitulino 	list_add(&pkt_dev->list, &t->if_list);
34091da177e4SLinus Torvalds 	pkt_dev->pg_thread = t;
34101da177e4SLinus Torvalds 	pkt_dev->running = 0;
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds out:
34131da177e4SLinus Torvalds 	if_unlock(t);
34141da177e4SLinus Torvalds 	return rv;
34151da177e4SLinus Torvalds }
34161da177e4SLinus Torvalds 
34171da177e4SLinus Torvalds /* Called under thread lock */
34181da177e4SLinus Torvalds 
34191da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
34201da177e4SLinus Torvalds {
34211da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
342239df232fSStephen Hemminger 	int err;
34233291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
34241da177e4SLinus Torvalds 
34251da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
34261da177e4SLinus Torvalds 
3427d50a6b56SStephen Hemminger 	pkt_dev = __pktgen_NN_threads(ifname, FIND);
3428d50a6b56SStephen Hemminger 	if (pkt_dev) {
3429f9467eaeSJoe Perches 		pr_err("ERROR: interface already used\n");
3430d50a6b56SStephen Hemminger 		return -EBUSY;
3431d50a6b56SStephen Hemminger 	}
34321da177e4SLinus Torvalds 
34333291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
34341da177e4SLinus Torvalds 	if (!pkt_dev)
34351da177e4SLinus Torvalds 		return -ENOMEM;
34361da177e4SLinus Torvalds 
3437593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
343868d5ac2eSWANG Cong 	pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
34393291b9dbSEric Dumazet 				      node);
34401da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
34411da177e4SLinus Torvalds 		kfree(pkt_dev);
34421da177e4SLinus Torvalds 		return -ENOMEM;
34431da177e4SLinus Torvalds 	}
34441da177e4SLinus Torvalds 
344595ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
34461da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
3447fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
34481da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
34491da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
34501da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
34511da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
34521da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
34531da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
345434954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
345534954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
345634954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
345734954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
345834954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
345934954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
3460e99b99b4SRobert Olsson 	pkt_dev->node = -1;
346134954ddcSFrancesco Fondelli 
346239df232fSStephen Hemminger 	err = pktgen_setup_dev(pkt_dev, ifname);
346339df232fSStephen Hemminger 	if (err)
346439df232fSStephen Hemminger 		goto out1;
3465d8873315SNeil Horman 	if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3466d8873315SNeil Horman 		pkt_dev->clone_skb = pg_clone_skb_d;
34671da177e4SLinus Torvalds 
34685efdccbcSDenis V. Lunev 	pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
34695efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
347039df232fSStephen Hemminger 	if (!pkt_dev->entry) {
3471f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3472d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
347339df232fSStephen Hemminger 		err = -EINVAL;
347439df232fSStephen Hemminger 		goto out2;
347539df232fSStephen Hemminger 	}
3476a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3477a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3478a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3479a553e4a6SJamal Hadi Salim #endif
348039df232fSStephen Hemminger 
348139df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
348239df232fSStephen Hemminger out2:
348339df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
348439df232fSStephen Hemminger out1:
3485a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3486a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3487a553e4a6SJamal Hadi Salim #endif
34881da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
34891da177e4SLinus Torvalds 	kfree(pkt_dev);
349039df232fSStephen Hemminger 	return err;
34911da177e4SLinus Torvalds }
34921da177e4SLinus Torvalds 
3493ee74baa7SDavid S. Miller static int __init pktgen_create_thread(int cpu)
34941da177e4SLinus Torvalds {
3495cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3496d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3497ee74baa7SDavid S. Miller 	struct task_struct *p;
34981da177e4SLinus Torvalds 
34993291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
35003291b9dbSEric Dumazet 			 cpu_to_node(cpu));
35011da177e4SLinus Torvalds 	if (!t) {
3502f9467eaeSJoe Perches 		pr_err("ERROR: out of memory, can't create new thread\n");
35031da177e4SLinus Torvalds 		return -ENOMEM;
35041da177e4SLinus Torvalds 	}
35051da177e4SLinus Torvalds 
35061da177e4SLinus Torvalds 	spin_lock_init(&t->if_lock);
35071da177e4SLinus Torvalds 	t->cpu = cpu;
35081da177e4SLinus Torvalds 
3509ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3510ee74baa7SDavid S. Miller 
3511ee74baa7SDavid S. Miller 	list_add_tail(&t->th_list, &pktgen_threads);
3512d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3513ee74baa7SDavid S. Miller 
351494dcf29aSEric Dumazet 	p = kthread_create_on_node(pktgen_thread_worker,
351594dcf29aSEric Dumazet 				   t,
351694dcf29aSEric Dumazet 				   cpu_to_node(cpu),
351794dcf29aSEric Dumazet 				   "kpktgend_%d", cpu);
3518ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
3519f9467eaeSJoe Perches 		pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3520ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3521ee74baa7SDavid S. Miller 		kfree(t);
3522ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3523ee74baa7SDavid S. Miller 	}
3524ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3525ee74baa7SDavid S. Miller 	t->tsk = p;
3526ee74baa7SDavid S. Miller 
35275efdccbcSDenis V. Lunev 	pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
35285efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3529d50a6b56SStephen Hemminger 	if (!pe) {
3530f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3531ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3532ee74baa7SDavid S. Miller 		kthread_stop(p);
3533ee74baa7SDavid S. Miller 		list_del(&t->th_list);
35341da177e4SLinus Torvalds 		kfree(t);
35351da177e4SLinus Torvalds 		return -EINVAL;
35361da177e4SLinus Torvalds 	}
3537d50a6b56SStephen Hemminger 
3538ee74baa7SDavid S. Miller 	wake_up_process(p);
3539d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
35401da177e4SLinus Torvalds 
35411da177e4SLinus Torvalds 	return 0;
35421da177e4SLinus Torvalds }
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds /*
35451da177e4SLinus Torvalds  * Removes a device from the thread if_list.
35461da177e4SLinus Torvalds  */
3547222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3548222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
35491da177e4SLinus Torvalds {
3550c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3551c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
35521da177e4SLinus Torvalds 
3553c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3554c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3555c26a8016SLuiz Capitulino 		if (p == pkt_dev)
3556c26a8016SLuiz Capitulino 			list_del(&p->list);
35571da177e4SLinus Torvalds 	}
35581da177e4SLinus Torvalds }
35591da177e4SLinus Torvalds 
3560222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3561222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
35621da177e4SLinus Torvalds {
35631da177e4SLinus Torvalds 
3564f9467eaeSJoe Perches 	pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds 	if (pkt_dev->running) {
3567f9467eaeSJoe Perches 		pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
35681da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
35691da177e4SLinus Torvalds 	}
35701da177e4SLinus Torvalds 
35711da177e4SLinus Torvalds 	/* Dis-associate from the interface */
35721da177e4SLinus Torvalds 
35731da177e4SLinus Torvalds 	if (pkt_dev->odev) {
35741da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
35751da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
35761da177e4SLinus Torvalds 	}
35771da177e4SLinus Torvalds 
35781da177e4SLinus Torvalds 	/* And update the thread if_list */
35791da177e4SLinus Torvalds 
35801da177e4SLinus Torvalds 	_rem_dev_from_if_list(t, pkt_dev);
35811da177e4SLinus Torvalds 
358239df232fSStephen Hemminger 	if (pkt_dev->entry)
358339df232fSStephen Hemminger 		remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
35841da177e4SLinus Torvalds 
3585a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3586a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3587a553e4a6SJamal Hadi Salim #endif
35881da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
358926ad7879SEric Dumazet 	if (pkt_dev->page)
359026ad7879SEric Dumazet 		put_page(pkt_dev->page);
35911da177e4SLinus Torvalds 	kfree(pkt_dev);
35921da177e4SLinus Torvalds 	return 0;
35931da177e4SLinus Torvalds }
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds static int __init pg_init(void)
35961da177e4SLinus Torvalds {
35971da177e4SLinus Torvalds 	int cpu;
3598d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3599ce14f894SWANG Cong 	int ret = 0;
3600d50a6b56SStephen Hemminger 
3601f9467eaeSJoe Perches 	pr_info("%s", version);
36021da177e4SLinus Torvalds 
3603457c4cbcSEric W. Biederman 	pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3604d50a6b56SStephen Hemminger 	if (!pg_proc_dir)
3605d50a6b56SStephen Hemminger 		return -ENODEV;
36061da177e4SLinus Torvalds 
360725296d59SWang Chen 	pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3608d50a6b56SStephen Hemminger 	if (pe == NULL) {
3609f9467eaeSJoe Perches 		pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
3610ce14f894SWANG Cong 		ret = -EINVAL;
3611ce14f894SWANG Cong 		goto remove_dir;
36121da177e4SLinus Torvalds 	}
36131da177e4SLinus Torvalds 
36141da177e4SLinus Torvalds 	register_netdevice_notifier(&pktgen_notifier_block);
36151da177e4SLinus Torvalds 
3616670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
36178024bb24SLuiz Capitulino 		int err;
36181da177e4SLinus Torvalds 
3619ee74baa7SDavid S. Miller 		err = pktgen_create_thread(cpu);
36208024bb24SLuiz Capitulino 		if (err)
3621f9467eaeSJoe Perches 			pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3622f9467eaeSJoe Perches 				   cpu, err);
36231da177e4SLinus Torvalds 	}
36248024bb24SLuiz Capitulino 
36258024bb24SLuiz Capitulino 	if (list_empty(&pktgen_threads)) {
3626f9467eaeSJoe Perches 		pr_err("ERROR: Initialization failed for all threads\n");
3627ce14f894SWANG Cong 		ret = -ENODEV;
3628ce14f894SWANG Cong 		goto unregister;
36298024bb24SLuiz Capitulino 	}
36308024bb24SLuiz Capitulino 
36311da177e4SLinus Torvalds 	return 0;
3632ce14f894SWANG Cong 
3633ce14f894SWANG Cong  unregister:
3634ce14f894SWANG Cong 	unregister_netdevice_notifier(&pktgen_notifier_block);
3635ce14f894SWANG Cong 	remove_proc_entry(PGCTRL, pg_proc_dir);
3636ce14f894SWANG Cong  remove_dir:
3637ce14f894SWANG Cong 	proc_net_remove(&init_net, PG_PROC_DIR);
3638ce14f894SWANG Cong 	return ret;
36391da177e4SLinus Torvalds }
36401da177e4SLinus Torvalds 
36411da177e4SLinus Torvalds static void __exit pg_cleanup(void)
36421da177e4SLinus Torvalds {
3643cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3644cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
3645d4b11335SEric Dumazet 	LIST_HEAD(list);
36461da177e4SLinus Torvalds 
36471da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
3648551eaff1SEric Dumazet 	pktgen_exiting = true;
36491da177e4SLinus Torvalds 
3650c57b5468SEric Dumazet 	mutex_lock(&pktgen_thread_lock);
3651d4b11335SEric Dumazet 	list_splice_init(&pktgen_threads, &list);
3652c57b5468SEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
3653c57b5468SEric Dumazet 
3654c57b5468SEric Dumazet 	list_for_each_safe(q, n, &list) {
3655cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3656c57b5468SEric Dumazet 		list_del(&t->th_list);
3657ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
3658ee74baa7SDavid S. Miller 		kfree(t);
36591da177e4SLinus Torvalds 	}
36601da177e4SLinus Torvalds 
36611da177e4SLinus Torvalds 	/* Un-register us from receiving netdevice events */
36621da177e4SLinus Torvalds 	unregister_netdevice_notifier(&pktgen_notifier_block);
36631da177e4SLinus Torvalds 
36641da177e4SLinus Torvalds 	/* Clean up proc file system */
3665d50a6b56SStephen Hemminger 	remove_proc_entry(PGCTRL, pg_proc_dir);
3666457c4cbcSEric W. Biederman 	proc_net_remove(&init_net, PG_PROC_DIR);
36671da177e4SLinus Torvalds }
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds module_init(pg_init);
36701da177e4SLinus Torvalds module_exit(pg_cleanup);
36711da177e4SLinus Torvalds 
3672c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
36731da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
36741da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3675c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
36761da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3677c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
36781da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3679c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
36801da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3681c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
36821da177e4SLinus Torvalds module_param(debug, int, 0);
3683c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3684