xref: /openbmc/linux/net/core/pktgen.c (revision baac167b706600ebe7158acaeb7c489ae9d0bb8b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Authors:
31da177e4SLinus Torvalds  * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
41da177e4SLinus Torvalds  *                             Uppsala University and
51da177e4SLinus Torvalds  *                             Swedish University of Agricultural Sciences
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Alexey Kuznetsov  <kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  * Ben Greear <greearb@candelatech.com>
996de0e25SJan Engelhardt  * Jens Låås <jens.laas@data.slu.se>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * A tool for loading the network with preconfigurated packets.
181da177e4SLinus Torvalds  * The tool is implemented as a linux module.  Parameters are output
191da177e4SLinus Torvalds  * device, delay (to hard_xmit), number of packets, and whether
201da177e4SLinus Torvalds  * to use multiple SKBs or just the same one.
211da177e4SLinus Torvalds  * pktgen uses the installed interface's output routine.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * Additional hacking by:
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Jens.Laas@data.slu.se
261da177e4SLinus Torvalds  * Improved by ANK. 010120.
271da177e4SLinus Torvalds  * Improved by ANK even more. 010212.
281da177e4SLinus Torvalds  * MAC address typo fixed. 010417 --ro
291da177e4SLinus Torvalds  * Integrated.  020301 --DaveM
301da177e4SLinus Torvalds  * Added multiskb option 020301 --DaveM
311da177e4SLinus Torvalds  * Scaling of results. 020417--sigurdur@linpro.no
321da177e4SLinus Torvalds  * Significant re-work of the module:
331da177e4SLinus Torvalds  *   *  Convert to threaded model to more efficiently be able to transmit
341da177e4SLinus Torvalds  *       and receive on multiple interfaces at once.
351da177e4SLinus Torvalds  *   *  Converted many counters to __u64 to allow longer runs.
361da177e4SLinus Torvalds  *   *  Allow configuration of ranges, like min/max IP address, MACs,
371da177e4SLinus Torvalds  *       and UDP-ports, for both source and destination, and can
381da177e4SLinus Torvalds  *       set to use a random distribution or sequentially walk the range.
391da177e4SLinus Torvalds  *   *  Can now change most values after starting.
401da177e4SLinus Torvalds  *   *  Place 12-byte packet in UDP payload with magic number,
411da177e4SLinus Torvalds  *       sequence number, and timestamp.
421da177e4SLinus Torvalds  *   *  Add receiver code that detects dropped pkts, re-ordered pkts, and
431da177e4SLinus Torvalds  *       latencies (with micro-second) precision.
441da177e4SLinus Torvalds  *   *  Add IOCTL interface to easily get counters & configuration.
451da177e4SLinus Torvalds  *   --Ben Greear <greearb@candelatech.com>
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
481da177e4SLinus Torvalds  * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
491da177e4SLinus Torvalds  * as a "fastpath" with a configurable number of clones after alloc's.
501da177e4SLinus Torvalds  * clone_skb=0 means all packets are allocated this also means ranges time
511da177e4SLinus Torvalds  * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
521da177e4SLinus Torvalds  * clones.
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  * Also moved to /proc/net/pktgen/
551da177e4SLinus Torvalds  * --ro
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * Sept 10:  Fixed threading/locking.  Lots of bone-headed and more clever
581da177e4SLinus Torvalds  *    mistakes.  Also merged in DaveM's patch in the -pre6 patch.
591da177e4SLinus Torvalds  * --Ben Greear <greearb@candelatech.com>
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  *
641da177e4SLinus Torvalds  * 021124 Finished major redesign and rewrite for new functionality.
651da177e4SLinus Torvalds  * See Documentation/networking/pktgen.txt for how to use this.
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  * The new operation:
681da177e4SLinus Torvalds  * For each CPU one thread/process is created at start. This process checks
691da177e4SLinus Torvalds  * for running devices in the if_list and sends packets until count is 0 it
701da177e4SLinus Torvalds  * also the thread checks the thread->control which is used for inter-process
711da177e4SLinus Torvalds  * communication. controlling process "posts" operations to the threads this
721da177e4SLinus Torvalds  * way. The if_lock should be possible to remove when add/rem_device is merged
731da177e4SLinus Torvalds  * into this too.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * By design there should only be *one* "controlling" process. In practice
761da177e4SLinus Torvalds  * multiple write accesses gives unpredictable result. Understood by "write"
771da177e4SLinus Torvalds  * to /proc gives result code thats should be read be the "writer".
78b4099fabSStephen Hemminger  * For practical use this should be no problem.
791da177e4SLinus Torvalds  *
801da177e4SLinus Torvalds  * Note when adding devices to a specific CPU there good idea to also assign
811da177e4SLinus Torvalds  * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
821da177e4SLinus Torvalds  * --ro
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * Fix refcount off by one if first packet fails, potential null deref,
851da177e4SLinus Torvalds  * memleak 030710- KJP
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * First "ranges" functionality for ipv6 030726 --ro
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  * Included flow support. 030802 ANK.
901da177e4SLinus Torvalds  *
911da177e4SLinus Torvalds  * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
921da177e4SLinus Torvalds  *
931da177e4SLinus Torvalds  * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
941da177e4SLinus Torvalds  * ia64 compilation fix from  Aron Griffis <aron@hp.com> 040604
951da177e4SLinus Torvalds  *
961da177e4SLinus Torvalds  * New xmit() return, do_div and misc clean up by Stephen Hemminger
971da177e4SLinus Torvalds  * <shemminger@osdl.org> 040923
981da177e4SLinus Torvalds  *
99b4099fabSStephen Hemminger  * Randy Dunlap fixed u64 printk compiler waring
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * Remove FCS from BW calculation.  Lennert Buytenhek <buytenh@wantstofly.org>
1021da177e4SLinus Torvalds  * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  * Corrections from Nikolai Malykh (nmalykh@bilim.com)
1051da177e4SLinus Torvalds  * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
1081da177e4SLinus Torvalds  * 050103
109ca6549afSSteven Whitehouse  *
110ca6549afSSteven Whitehouse  * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111ca6549afSSteven Whitehouse  *
11234954ddcSFrancesco Fondelli  * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
11334954ddcSFrancesco Fondelli  *
114ce5d0b47SAdit Ranadive  * Fixed src_mac command to set source mac of packet to value specified in
115ce5d0b47SAdit Ranadive  * command by Adit Ranadive <adit.262@gmail.com>
116ce5d0b47SAdit Ranadive  *
1171da177e4SLinus Torvalds  */
118f9467eaeSJoe Perches 
119f9467eaeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
120f9467eaeSJoe Perches 
1211da177e4SLinus Torvalds #include <linux/sys.h>
1221da177e4SLinus Torvalds #include <linux/types.h>
1231da177e4SLinus Torvalds #include <linux/module.h>
1241da177e4SLinus Torvalds #include <linux/moduleparam.h>
1251da177e4SLinus Torvalds #include <linux/kernel.h>
126222fa076SLuiz Capitulino #include <linux/mutex.h>
1271da177e4SLinus Torvalds #include <linux/sched.h>
1281da177e4SLinus Torvalds #include <linux/slab.h>
1291da177e4SLinus Torvalds #include <linux/vmalloc.h>
1301da177e4SLinus Torvalds #include <linux/unistd.h>
1311da177e4SLinus Torvalds #include <linux/string.h>
1321da177e4SLinus Torvalds #include <linux/ptrace.h>
1331da177e4SLinus Torvalds #include <linux/errno.h>
1341da177e4SLinus Torvalds #include <linux/ioport.h>
1351da177e4SLinus Torvalds #include <linux/interrupt.h>
1364fc268d2SRandy Dunlap #include <linux/capability.h>
1372bc481cfSStephen Hemminger #include <linux/hrtimer.h>
13809fe3ef4SAndrew Morton #include <linux/freezer.h>
1391da177e4SLinus Torvalds #include <linux/delay.h>
1401da177e4SLinus Torvalds #include <linux/timer.h>
141cdcdbe0bSLuiz Capitulino #include <linux/list.h>
1421da177e4SLinus Torvalds #include <linux/init.h>
1431da177e4SLinus Torvalds #include <linux/skbuff.h>
1441da177e4SLinus Torvalds #include <linux/netdevice.h>
1451da177e4SLinus Torvalds #include <linux/inet.h>
1461da177e4SLinus Torvalds #include <linux/inetdevice.h>
1471da177e4SLinus Torvalds #include <linux/rtnetlink.h>
1481da177e4SLinus Torvalds #include <linux/if_arp.h>
14934954ddcSFrancesco Fondelli #include <linux/if_vlan.h>
1501da177e4SLinus Torvalds #include <linux/in.h>
1511da177e4SLinus Torvalds #include <linux/ip.h>
1521da177e4SLinus Torvalds #include <linux/ipv6.h>
1531da177e4SLinus Torvalds #include <linux/udp.h>
1541da177e4SLinus Torvalds #include <linux/proc_fs.h>
155d50a6b56SStephen Hemminger #include <linux/seq_file.h>
1561da177e4SLinus Torvalds #include <linux/wait.h>
157f404e9a6SKris Katterjohn #include <linux/etherdevice.h>
158ee74baa7SDavid S. Miller #include <linux/kthread.h>
159268bb0ceSLinus Torvalds #include <linux/prefetch.h>
160457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1611da177e4SLinus Torvalds #include <net/checksum.h>
1621da177e4SLinus Torvalds #include <net/ipv6.h>
163c26bf4a5SThomas Graf #include <net/udp.h>
16473d94e94SStephen Rothwell #include <net/ip6_checksum.h>
1651da177e4SLinus Torvalds #include <net/addrconf.h>
166a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
167a553e4a6SJamal Hadi Salim #include <net/xfrm.h>
168a553e4a6SJamal Hadi Salim #endif
1694e58a027SCong Wang #include <net/netns/generic.h>
1701da177e4SLinus Torvalds #include <asm/byteorder.h>
1711da177e4SLinus Torvalds #include <linux/rcupdate.h>
1721977f032SJiri Slaby #include <linux/bitops.h>
17363adc6fbSStephen Hemminger #include <linux/io.h>
17463adc6fbSStephen Hemminger #include <linux/timex.h>
17563adc6fbSStephen Hemminger #include <linux/uaccess.h>
1761da177e4SLinus Torvalds #include <asm/dma.h>
1771da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1781da177e4SLinus Torvalds 
17943d28b65SDaniel Turull #define VERSION	"2.74"
1801da177e4SLinus Torvalds #define IP_NAME_SZ 32
181ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
182d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1831da177e4SLinus Torvalds 
184f9467eaeSJoe Perches #define func_enter() pr_debug("entering %s\n", __func__);
185f9467eaeSJoe Perches 
1861da177e4SLinus Torvalds /* Device flag bits */
1871da177e4SLinus Torvalds #define F_IPSRC_RND   (1<<0)	/* IP-Src Random  */
1881da177e4SLinus Torvalds #define F_IPDST_RND   (1<<1)	/* IP-Dst Random  */
1891da177e4SLinus Torvalds #define F_UDPSRC_RND  (1<<2)	/* UDP-Src Random */
1901da177e4SLinus Torvalds #define F_UDPDST_RND  (1<<3)	/* UDP-Dst Random */
1911da177e4SLinus Torvalds #define F_MACSRC_RND  (1<<4)	/* MAC-Src Random */
1921da177e4SLinus Torvalds #define F_MACDST_RND  (1<<5)	/* MAC-Dst Random */
1931da177e4SLinus Torvalds #define F_TXSIZE_RND  (1<<6)	/* Transmit size is random */
1941da177e4SLinus Torvalds #define F_IPV6        (1<<7)	/* Interface in IPV6 Mode */
195ca6549afSSteven Whitehouse #define F_MPLS_RND    (1<<8)	/* Random MPLS labels */
19634954ddcSFrancesco Fondelli #define F_VID_RND     (1<<9)	/* Random VLAN ID */
19734954ddcSFrancesco Fondelli #define F_SVID_RND    (1<<10)	/* Random SVLAN ID */
198007a531bSJamal Hadi Salim #define F_FLOW_SEQ    (1<<11)	/* Sequential flows */
199a553e4a6SJamal Hadi Salim #define F_IPSEC_ON    (1<<12)	/* ipsec on for flows */
20045b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13)	/* queue map Random */
201e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14)	/* queue map mirrors smp_processor_id() */
202e99b99b4SRobert Olsson #define F_NODE          (1<<15)	/* Node memory alloc*/
203c26bf4a5SThomas Graf #define F_UDPCSUM       (1<<16)	/* Include UDP checksum */
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /* Thread control flag bits */
2066b80d6a6SStephen Hemminger #define T_STOP        (1<<0)	/* Stop run */
2076b80d6a6SStephen Hemminger #define T_RUN         (1<<1)	/* Start run */
2086b80d6a6SStephen Hemminger #define T_REMDEVALL   (1<<2)	/* Remove all devs */
2096b80d6a6SStephen Hemminger #define T_REMDEV      (1<<3)	/* Remove one dev */
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* If lock -- can be removed after some work */
2121da177e4SLinus Torvalds #define   if_lock(t)           spin_lock(&(t->if_lock));
2131da177e4SLinus Torvalds #define   if_unlock(t)           spin_unlock(&(t->if_lock));
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2161da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
217d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
218d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds #define MAX_CFLOWS  65536
2211da177e4SLinus Torvalds 
22234954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
22334954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
22434954ddcSFrancesco Fondelli 
225222f1806SLuiz Capitulino struct flow_state {
226252e3346SAl Viro 	__be32 cur_daddr;
2271da177e4SLinus Torvalds 	int count;
228a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
229a553e4a6SJamal Hadi Salim 	struct xfrm_state *x;
230a553e4a6SJamal Hadi Salim #endif
231007a531bSJamal Hadi Salim 	__u32 flags;
2321da177e4SLinus Torvalds };
2331da177e4SLinus Torvalds 
234007a531bSJamal Hadi Salim /* flow flag bits */
235007a531bSJamal Hadi Salim #define F_INIT   (1<<0)		/* flow has been initialized */
236007a531bSJamal Hadi Salim 
2371da177e4SLinus Torvalds struct pktgen_dev {
2381da177e4SLinus Torvalds 	/*
2391da177e4SLinus Torvalds 	 * Try to keep frequent/infrequent used vars. separated.
2401da177e4SLinus Torvalds 	 */
24139df232fSStephen Hemminger 	struct proc_dir_entry *entry;	/* proc file */
2421da177e4SLinus Torvalds 	struct pktgen_thread *pg_thread;/* the owner */
24363adc6fbSStephen Hemminger 	struct list_head list;		/* chaining in the thread's run-queue */
2441da177e4SLinus Torvalds 
24563adc6fbSStephen Hemminger 	int running;		/* if false, the test will stop */
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/* If min != max, then we will either do a linear iteration, or
2481da177e4SLinus Torvalds 	 * we will do a random selection from within the range.
2491da177e4SLinus Torvalds 	 */
2501da177e4SLinus Torvalds 	__u32 flags;
25195ed63f7SArthur Kepner 	int removal_mark;	/* non-zero => the device is marked for
25295ed63f7SArthur Kepner 				 * removal by worker thread */
2531da177e4SLinus Torvalds 
25468bf9f0bSAmerigo Wang 	int min_pkt_size;
25568bf9f0bSAmerigo Wang 	int max_pkt_size;
25616dab72fSJamal Hadi Salim 	int pkt_overhead;	/* overhead for MPLS, VLANs, IPSEC etc */
2571da177e4SLinus Torvalds 	int nfrags;
25826ad7879SEric Dumazet 	struct page *page;
259fd29cf72SStephen Hemminger 	u64 delay;		/* nano-seconds */
260fd29cf72SStephen Hemminger 
2611da177e4SLinus Torvalds 	__u64 count;		/* Default No packets to send */
2621da177e4SLinus Torvalds 	__u64 sofar;		/* How many pkts we've sent so far */
2631da177e4SLinus Torvalds 	__u64 tx_bytes;		/* How many bytes we've transmitted */
264f466dba1SJohn Fastabend 	__u64 errors;		/* Errors when trying to transmit, */
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	/* runtime counters relating to clone_skb */
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	__u64 allocated_skbs;
2691da177e4SLinus Torvalds 	__u32 clone_count;
2701da177e4SLinus Torvalds 	int last_ok;		/* Was last skb sent?
27163adc6fbSStephen Hemminger 				 * Or a failed transmit of some sort?
27263adc6fbSStephen Hemminger 				 * This will keep sequence numbers in order
2731da177e4SLinus Torvalds 				 */
274fd29cf72SStephen Hemminger 	ktime_t next_tx;
275fd29cf72SStephen Hemminger 	ktime_t started_at;
276fd29cf72SStephen Hemminger 	ktime_t stopped_at;
277fd29cf72SStephen Hemminger 	u64	idle_acc;	/* nano-seconds */
278fd29cf72SStephen Hemminger 
2791da177e4SLinus Torvalds 	__u32 seq_num;
2801da177e4SLinus Torvalds 
28163adc6fbSStephen Hemminger 	int clone_skb;		/*
28263adc6fbSStephen Hemminger 				 * Use multiple SKBs during packet gen.
28363adc6fbSStephen Hemminger 				 * If this number is greater than 1, then
28463adc6fbSStephen Hemminger 				 * that many copies of the same packet will be
28563adc6fbSStephen Hemminger 				 * sent before a new packet is allocated.
28663adc6fbSStephen Hemminger 				 * If you want to send 1024 identical packets
28763adc6fbSStephen Hemminger 				 * before creating a new packet,
28863adc6fbSStephen Hemminger 				 * set clone_skb to 1024.
2891da177e4SLinus Torvalds 				 */
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	char dst_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2921da177e4SLinus Torvalds 	char dst_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2931da177e4SLinus Torvalds 	char src_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2941da177e4SLinus Torvalds 	char src_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	struct in6_addr in6_saddr;
2971da177e4SLinus Torvalds 	struct in6_addr in6_daddr;
2981da177e4SLinus Torvalds 	struct in6_addr cur_in6_daddr;
2991da177e4SLinus Torvalds 	struct in6_addr cur_in6_saddr;
3001da177e4SLinus Torvalds 	/* For ranges */
3011da177e4SLinus Torvalds 	struct in6_addr min_in6_daddr;
3021da177e4SLinus Torvalds 	struct in6_addr max_in6_daddr;
3031da177e4SLinus Torvalds 	struct in6_addr min_in6_saddr;
3041da177e4SLinus Torvalds 	struct in6_addr max_in6_saddr;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	/* If we're doing ranges, random or incremental, then this
3071da177e4SLinus Torvalds 	 * defines the min/max for those ranges.
3081da177e4SLinus Torvalds 	 */
309252e3346SAl Viro 	__be32 saddr_min;	/* inclusive, source IP address */
310252e3346SAl Viro 	__be32 saddr_max;	/* exclusive, source IP address */
311252e3346SAl Viro 	__be32 daddr_min;	/* inclusive, dest IP address */
312252e3346SAl Viro 	__be32 daddr_max;	/* exclusive, dest IP address */
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	__u16 udp_src_min;	/* inclusive, source UDP port */
3151da177e4SLinus Torvalds 	__u16 udp_src_max;	/* exclusive, source UDP port */
3161da177e4SLinus Torvalds 	__u16 udp_dst_min;	/* inclusive, dest UDP port */
3171da177e4SLinus Torvalds 	__u16 udp_dst_max;	/* exclusive, dest UDP port */
3181da177e4SLinus Torvalds 
3191ca7768cSFrancesco Fondelli 	/* DSCP + ECN */
32063adc6fbSStephen Hemminger 	__u8 tos;            /* six MSB of (former) IPv4 TOS
32163adc6fbSStephen Hemminger 				are for dscp codepoint */
32263adc6fbSStephen Hemminger 	__u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6
32363adc6fbSStephen Hemminger 				(see RFC 3260, sec. 4) */
3241ca7768cSFrancesco Fondelli 
325ca6549afSSteven Whitehouse 	/* MPLS */
32695c96174SEric Dumazet 	unsigned int nr_labels;	/* Depth of stack, 0 = no MPLS */
327ca6549afSSteven Whitehouse 	__be32 labels[MAX_MPLS_LABELS];
328ca6549afSSteven Whitehouse 
32934954ddcSFrancesco Fondelli 	/* VLAN/SVLAN (802.1Q/Q-in-Q) */
33034954ddcSFrancesco Fondelli 	__u8  vlan_p;
33134954ddcSFrancesco Fondelli 	__u8  vlan_cfi;
33234954ddcSFrancesco Fondelli 	__u16 vlan_id;  /* 0xffff means no vlan tag */
33334954ddcSFrancesco Fondelli 
33434954ddcSFrancesco Fondelli 	__u8  svlan_p;
33534954ddcSFrancesco Fondelli 	__u8  svlan_cfi;
33634954ddcSFrancesco Fondelli 	__u16 svlan_id; /* 0xffff means no svlan tag */
33734954ddcSFrancesco Fondelli 
3381da177e4SLinus Torvalds 	__u32 src_mac_count;	/* How many MACs to iterate through */
3391da177e4SLinus Torvalds 	__u32 dst_mac_count;	/* How many MACs to iterate through */
3401da177e4SLinus Torvalds 
341f404e9a6SKris Katterjohn 	unsigned char dst_mac[ETH_ALEN];
342f404e9a6SKris Katterjohn 	unsigned char src_mac[ETH_ALEN];
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	__u32 cur_dst_mac_offset;
3451da177e4SLinus Torvalds 	__u32 cur_src_mac_offset;
346252e3346SAl Viro 	__be32 cur_saddr;
347252e3346SAl Viro 	__be32 cur_daddr;
34866ed1e5eSEric Dumazet 	__u16 ip_id;
3491da177e4SLinus Torvalds 	__u16 cur_udp_dst;
3501da177e4SLinus Torvalds 	__u16 cur_udp_src;
35145b270f8SRobert Olsson 	__u16 cur_queue_map;
3521da177e4SLinus Torvalds 	__u32 cur_pkt_size;
353baac8564SEric Dumazet 	__u32 last_pkt_size;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	__u8 hh[14];
3561da177e4SLinus Torvalds 	/* = {
3571da177e4SLinus Torvalds 	   0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	   We fill in SRC address later
3601da177e4SLinus Torvalds 	   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3611da177e4SLinus Torvalds 	   0x08, 0x00
3621da177e4SLinus Torvalds 	   };
3631da177e4SLinus Torvalds 	 */
3641da177e4SLinus Torvalds 	__u16 pad;		/* pad out the hh struct to an even 16 bytes */
3651da177e4SLinus Torvalds 
36663adc6fbSStephen Hemminger 	struct sk_buff *skb;	/* skb we are to transmit next, used for when we
3671da177e4SLinus Torvalds 				 * are transmitting the same one multiple times
3681da177e4SLinus Torvalds 				 */
36963adc6fbSStephen Hemminger 	struct net_device *odev; /* The out-going device.
37063adc6fbSStephen Hemminger 				  * Note that the device should have it's
37163adc6fbSStephen Hemminger 				  * pg_info pointer pointing back to this
37263adc6fbSStephen Hemminger 				  * device.
37363adc6fbSStephen Hemminger 				  * Set when the user specifies the out-going
37463adc6fbSStephen Hemminger 				  * device name (not when the inject is
3751da177e4SLinus Torvalds 				  * started as it used to do.)
3761da177e4SLinus Torvalds 				  */
377593f63b0SEric Dumazet 	char odevname[32];
3781da177e4SLinus Torvalds 	struct flow_state *flows;
37995c96174SEric Dumazet 	unsigned int cflows;	/* Concurrent flows (config) */
38095c96174SEric Dumazet 	unsigned int lflow;		/* Flow length  (config) */
38195c96174SEric Dumazet 	unsigned int nflows;	/* accumulated flows (stats) */
38295c96174SEric Dumazet 	unsigned int curfl;		/* current sequenced flow (state)*/
38345b270f8SRobert Olsson 
38445b270f8SRobert Olsson 	u16 queue_map_min;
38545b270f8SRobert Olsson 	u16 queue_map_max;
3869e50e3acSJohn Fastabend 	__u32 skb_priority;	/* skb priority field */
387e99b99b4SRobert Olsson 	int node;               /* Memory node */
38845b270f8SRobert Olsson 
389a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
390a553e4a6SJamal Hadi Salim 	__u8	ipsmode;		/* IPSEC mode (config) */
391a553e4a6SJamal Hadi Salim 	__u8	ipsproto;		/* IPSEC type (config) */
392de4aee7dSFan Du 	__u32	spi;
393cf93d47eSFan Du 	struct dst_entry dst;
394cf93d47eSFan Du 	struct dst_ops dstops;
395a553e4a6SJamal Hadi Salim #endif
39639df232fSStephen Hemminger 	char result[512];
3971da177e4SLinus Torvalds };
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds struct pktgen_hdr {
400252e3346SAl Viro 	__be32 pgh_magic;
401252e3346SAl Viro 	__be32 seq_num;
402252e3346SAl Viro 	__be32 tv_sec;
403252e3346SAl Viro 	__be32 tv_usec;
4041da177e4SLinus Torvalds };
4051da177e4SLinus Torvalds 
4064e58a027SCong Wang 
4074e58a027SCong Wang static int pg_net_id __read_mostly;
4084e58a027SCong Wang 
4094e58a027SCong Wang struct pktgen_net {
4104e58a027SCong Wang 	struct net		*net;
4114e58a027SCong Wang 	struct proc_dir_entry	*proc_dir;
4124e58a027SCong Wang 	struct list_head	pktgen_threads;
4134e58a027SCong Wang 	bool			pktgen_exiting;
4144e58a027SCong Wang };
415551eaff1SEric Dumazet 
4161da177e4SLinus Torvalds struct pktgen_thread {
41763adc6fbSStephen Hemminger 	spinlock_t if_lock;		/* for list of devices */
418c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
419cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
420ee74baa7SDavid S. Miller 	struct task_struct *tsk;
4211da177e4SLinus Torvalds 	char result[512];
4221da177e4SLinus Torvalds 
42363adc6fbSStephen Hemminger 	/* Field for thread to receive "posted" events terminate,
42463adc6fbSStephen Hemminger 	   stop ifs etc. */
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	u32 control;
4271da177e4SLinus Torvalds 	int cpu;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	wait_queue_head_t queue;
430d3ede327SDenis V. Lunev 	struct completion start_done;
4314e58a027SCong Wang 	struct pktgen_net *net;
4321da177e4SLinus Torvalds };
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds #define REMOVE 1
4351da177e4SLinus Torvalds #define FIND   0
4361da177e4SLinus Torvalds 
437c3d2f52dSStephen Hemminger static const char version[] =
438f9467eaeSJoe Perches 	"Packet Generator for packet performance testing. "
439f9467eaeSJoe Perches 	"Version: " VERSION "\n";
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4421da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
443222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
4443e984840SEric Dumazet 					  const char *ifname, bool exact);
4451da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
4464e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn);
4474e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn);
4484e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
4493bda06a3SStephen Hemminger 
4501da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4511da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
45239df232fSStephen Hemminger 
4531da177e4SLinus Torvalds /* Module parameters, defaults. */
45465c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
45565c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
45665c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
45765c5b786SStephen Hemminger static int debug  __read_mostly;
4581da177e4SLinus Torvalds 
459222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4621da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4631da177e4SLinus Torvalds };
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds /*
4661da177e4SLinus Torvalds  * /proc handling functions
4671da177e4SLinus Torvalds  *
4681da177e4SLinus Torvalds  */
4691da177e4SLinus Torvalds 
470d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
471d50a6b56SStephen Hemminger {
472c3d2f52dSStephen Hemminger 	seq_puts(seq, version);
473d50a6b56SStephen Hemminger 	return 0;
474d50a6b56SStephen Hemminger }
4751da177e4SLinus Torvalds 
476d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user *buf,
4771da177e4SLinus Torvalds 			    size_t count, loff_t *ppos)
4781da177e4SLinus Torvalds {
479d50a6b56SStephen Hemminger 	char data[128];
4804e58a027SCong Wang 	struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
4811da177e4SLinus Torvalds 
48209455747SMathias Krause 	if (!capable(CAP_NET_ADMIN))
48309455747SMathias Krause 		return -EPERM;
4841da177e4SLinus Torvalds 
48520b0c718SMathias Krause 	if (count == 0)
48620b0c718SMathias Krause 		return -EINVAL;
48720b0c718SMathias Krause 
488d50a6b56SStephen Hemminger 	if (count > sizeof(data))
489d50a6b56SStephen Hemminger 		count = sizeof(data);
4901da177e4SLinus Torvalds 
49109455747SMathias Krause 	if (copy_from_user(data, buf, count))
49209455747SMathias Krause 		return -EFAULT;
49309455747SMathias Krause 
49420b0c718SMathias Krause 	data[count - 1] = 0;	/* Strip trailing '\n' and terminate string */
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
4974e58a027SCong Wang 		pktgen_stop_all_threads_ifs(pn);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
5004e58a027SCong Wang 		pktgen_run_all_threads(pn);
5011da177e4SLinus Torvalds 
502eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
5034e58a027SCong Wang 		pktgen_reset_all_threads(pn);
504eb37b41cSJesse Brandeburg 
5051da177e4SLinus Torvalds 	else
506f9467eaeSJoe Perches 		pr_warning("Unknown command: %s\n", data);
5071da177e4SLinus Torvalds 
50809455747SMathias Krause 	return count;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
511d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
5121da177e4SLinus Torvalds {
513d9dda78bSAl Viro 	return single_open(file, pgctrl_show, PDE_DATA(inode));
514d50a6b56SStephen Hemminger }
515d50a6b56SStephen Hemminger 
5169a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
517d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
518d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
519d50a6b56SStephen Hemminger 	.read    = seq_read,
520d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
521d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
522d50a6b56SStephen Hemminger 	.release = single_release,
523d50a6b56SStephen Hemminger };
524d50a6b56SStephen Hemminger 
525d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
526d50a6b56SStephen Hemminger {
527648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
528fd29cf72SStephen Hemminger 	ktime_t stopped;
529fd29cf72SStephen Hemminger 	u64 idle;
5301da177e4SLinus Torvalds 
531222f1806SLuiz Capitulino 	seq_printf(seq,
532222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
533222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
534222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5351da177e4SLinus Torvalds 
536222f1806SLuiz Capitulino 	seq_printf(seq,
537fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
538fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
539593f63b0SEric Dumazet 		   pkt_dev->clone_skb, pkt_dev->odevname);
5401da177e4SLinus Torvalds 
541222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
542222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5431da177e4SLinus Torvalds 
54445b270f8SRobert Olsson 	seq_printf(seq,
54545b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
54645b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
54745b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
54845b270f8SRobert Olsson 
5499e50e3acSJohn Fastabend 	if (pkt_dev->skb_priority)
5509e50e3acSJohn Fastabend 		seq_printf(seq, "     skb_priority: %u\n",
5519e50e3acSJohn Fastabend 			   pkt_dev->skb_priority);
5529e50e3acSJohn Fastabend 
5531da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
554222f1806SLuiz Capitulino 		seq_printf(seq,
55547a0200dSAlexey Dobriyan 			   "     saddr: %pI6c  min_saddr: %pI6c  max_saddr: %pI6c\n"
55647a0200dSAlexey Dobriyan 			   "     daddr: %pI6c  min_daddr: %pI6c  max_daddr: %pI6c\n",
55747a0200dSAlexey Dobriyan 			   &pkt_dev->in6_saddr,
55847a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
55947a0200dSAlexey Dobriyan 			   &pkt_dev->in6_daddr,
56047a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
56163adc6fbSStephen Hemminger 	} else {
562222f1806SLuiz Capitulino 		seq_printf(seq,
56363adc6fbSStephen Hemminger 			   "     dst_min: %s  dst_max: %s\n",
56463adc6fbSStephen Hemminger 			   pkt_dev->dst_min, pkt_dev->dst_max);
56563adc6fbSStephen Hemminger 		seq_printf(seq,
56663adc6fbSStephen Hemminger 			   "        src_min: %s  src_max: %s\n",
56763adc6fbSStephen Hemminger 			   pkt_dev->src_min, pkt_dev->src_max);
56863adc6fbSStephen Hemminger 	}
5691da177e4SLinus Torvalds 
570d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5711da177e4SLinus Torvalds 
572e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
573e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
574e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5751da177e4SLinus Torvalds 
57697dc48e2SThomas Graf 	seq_puts(seq, "dst_mac: ");
577e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
5781da177e4SLinus Torvalds 
579222f1806SLuiz Capitulino 	seq_printf(seq,
58063adc6fbSStephen Hemminger 		   "     udp_src_min: %d  udp_src_max: %d"
58163adc6fbSStephen Hemminger 		   "  udp_dst_min: %d  udp_dst_max: %d\n",
582222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
583222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
5841da177e4SLinus Torvalds 
585222f1806SLuiz Capitulino 	seq_printf(seq,
586ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
5871da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
5881da177e4SLinus Torvalds 
589ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
59095c96174SEric Dumazet 		unsigned int i;
59197dc48e2SThomas Graf 		seq_puts(seq, "     mpls: ");
592ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
593ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
594ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
595ca6549afSSteven Whitehouse 	}
596ca6549afSSteven Whitehouse 
59763adc6fbSStephen Hemminger 	if (pkt_dev->vlan_id != 0xffff)
59834954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
59963adc6fbSStephen Hemminger 			   pkt_dev->vlan_id, pkt_dev->vlan_p,
60063adc6fbSStephen Hemminger 			   pkt_dev->vlan_cfi);
60134954ddcSFrancesco Fondelli 
60263adc6fbSStephen Hemminger 	if (pkt_dev->svlan_id != 0xffff)
60334954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
60463adc6fbSStephen Hemminger 			   pkt_dev->svlan_id, pkt_dev->svlan_p,
60563adc6fbSStephen Hemminger 			   pkt_dev->svlan_cfi);
60634954ddcSFrancesco Fondelli 
60763adc6fbSStephen Hemminger 	if (pkt_dev->tos)
6081ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
6091ca7768cSFrancesco Fondelli 
61063adc6fbSStephen Hemminger 	if (pkt_dev->traffic_class)
6111ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
6121ca7768cSFrancesco Fondelli 
613e99b99b4SRobert Olsson 	if (pkt_dev->node >= 0)
614e99b99b4SRobert Olsson 		seq_printf(seq, "     node: %d\n", pkt_dev->node);
615e99b99b4SRobert Olsson 
61697dc48e2SThomas Graf 	seq_puts(seq, "     Flags: ");
617ca6549afSSteven Whitehouse 
6181da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
61997dc48e2SThomas Graf 		seq_puts(seq, "IPV6  ");
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPSRC_RND)
62297dc48e2SThomas Graf 		seq_puts(seq, "IPSRC_RND  ");
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPDST_RND)
62597dc48e2SThomas Graf 		seq_puts(seq, "IPDST_RND  ");
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	if (pkt_dev->flags & F_TXSIZE_RND)
62897dc48e2SThomas Graf 		seq_puts(seq, "TXSIZE_RND  ");
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPSRC_RND)
63197dc48e2SThomas Graf 		seq_puts(seq, "UDPSRC_RND  ");
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPDST_RND)
63497dc48e2SThomas Graf 		seq_puts(seq, "UDPDST_RND  ");
6351da177e4SLinus Torvalds 
636c26bf4a5SThomas Graf 	if (pkt_dev->flags & F_UDPCSUM)
63797dc48e2SThomas Graf 		seq_puts(seq, "UDPCSUM  ");
638c26bf4a5SThomas Graf 
639ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND)
64097dc48e2SThomas Graf 		seq_puts(seq,  "MPLS_RND  ");
641ca6549afSSteven Whitehouse 
64245b270f8SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_RND)
64397dc48e2SThomas Graf 		seq_puts(seq,  "QUEUE_MAP_RND  ");
64445b270f8SRobert Olsson 
645e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
64697dc48e2SThomas Graf 		seq_puts(seq,  "QUEUE_MAP_CPU  ");
647e6fce5b9SRobert Olsson 
648007a531bSJamal Hadi Salim 	if (pkt_dev->cflows) {
649007a531bSJamal Hadi Salim 		if (pkt_dev->flags & F_FLOW_SEQ)
65097dc48e2SThomas Graf 			seq_puts(seq,  "FLOW_SEQ  "); /*in sequence flows*/
651007a531bSJamal Hadi Salim 		else
65297dc48e2SThomas Graf 			seq_puts(seq,  "FLOW_RND  ");
653007a531bSJamal Hadi Salim 	}
654007a531bSJamal Hadi Salim 
655a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
6568101328bSFan Du 	if (pkt_dev->flags & F_IPSEC_ON) {
65797dc48e2SThomas Graf 		seq_puts(seq,  "IPSEC  ");
6588101328bSFan Du 		if (pkt_dev->spi)
6598101328bSFan Du 			seq_printf(seq, "spi:%u", pkt_dev->spi);
6608101328bSFan Du 	}
661a553e4a6SJamal Hadi Salim #endif
662a553e4a6SJamal Hadi Salim 
6631da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACSRC_RND)
66497dc48e2SThomas Graf 		seq_puts(seq, "MACSRC_RND  ");
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACDST_RND)
66797dc48e2SThomas Graf 		seq_puts(seq, "MACDST_RND  ");
6681da177e4SLinus Torvalds 
66934954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_VID_RND)
67097dc48e2SThomas Graf 		seq_puts(seq, "VID_RND  ");
67134954ddcSFrancesco Fondelli 
67234954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_SVID_RND)
67397dc48e2SThomas Graf 		seq_puts(seq, "SVID_RND  ");
67434954ddcSFrancesco Fondelli 
675e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE)
67697dc48e2SThomas Graf 		seq_puts(seq, "NODE_ALLOC  ");
677e99b99b4SRobert Olsson 
678d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6791da177e4SLinus Torvalds 
680fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
681398f382cSDaniel Borkmann 	stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
682fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
683fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
6841da177e4SLinus Torvalds 
685222f1806SLuiz Capitulino 	seq_printf(seq,
686fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
6871da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
688fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
689fd29cf72SStephen Hemminger 
690fd29cf72SStephen Hemminger 	seq_printf(seq,
691fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
692fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
693fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
694fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
6951da177e4SLinus Torvalds 
696222f1806SLuiz Capitulino 	seq_printf(seq,
697222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
698d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
699d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
70247a0200dSAlexey Dobriyan 		seq_printf(seq, "     cur_saddr: %pI6c  cur_daddr: %pI6c\n",
70347a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_saddr,
70447a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_daddr);
705222f1806SLuiz Capitulino 	} else
7060373a946SAmerigo Wang 		seq_printf(seq, "     cur_saddr: %pI4  cur_daddr: %pI4\n",
7070373a946SAmerigo Wang 			   &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
7081da177e4SLinus Torvalds 
709d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
7101da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
7111da177e4SLinus Torvalds 
71245b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
71345b270f8SRobert Olsson 
714d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	if (pkt_dev->result[0])
717d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
7181da177e4SLinus Torvalds 	else
71997dc48e2SThomas Graf 		seq_puts(seq, "Result: Idle\n");
7201da177e4SLinus Torvalds 
721d50a6b56SStephen Hemminger 	return 0;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
724ca6549afSSteven Whitehouse 
72563adc6fbSStephen Hemminger static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
72663adc6fbSStephen Hemminger 		     __u32 *num)
727ca6549afSSteven Whitehouse {
728ca6549afSSteven Whitehouse 	int i = 0;
729ca6549afSSteven Whitehouse 	*num = 0;
730ca6549afSSteven Whitehouse 
7311ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
73282fd5b5dSAndy Shevchenko 		int value;
733ca6549afSSteven Whitehouse 		char c;
734ca6549afSSteven Whitehouse 		*num <<= 4;
735ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
736ca6549afSSteven Whitehouse 			return -EFAULT;
73782fd5b5dSAndy Shevchenko 		value = hex_to_bin(c);
73882fd5b5dSAndy Shevchenko 		if (value >= 0)
73982fd5b5dSAndy Shevchenko 			*num |= value;
740ca6549afSSteven Whitehouse 		else
741ca6549afSSteven Whitehouse 			break;
742ca6549afSSteven Whitehouse 	}
743ca6549afSSteven Whitehouse 	return i;
744ca6549afSSteven Whitehouse }
745ca6549afSSteven Whitehouse 
746222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
747222f1806SLuiz Capitulino 			     unsigned int maxlen)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	int i;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7521da177e4SLinus Torvalds 		char c;
7531da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7541da177e4SLinus Torvalds 			return -EFAULT;
7551da177e4SLinus Torvalds 		switch (c) {
7561da177e4SLinus Torvalds 		case '\"':
7571da177e4SLinus Torvalds 		case '\n':
7581da177e4SLinus Torvalds 		case '\r':
7591da177e4SLinus Torvalds 		case '\t':
7601da177e4SLinus Torvalds 		case ' ':
7611da177e4SLinus Torvalds 		case '=':
7621da177e4SLinus Torvalds 			break;
7631da177e4SLinus Torvalds 		default:
7641da177e4SLinus Torvalds 			goto done;
7653ff50b79SStephen Hemminger 		}
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds done:
7681da177e4SLinus Torvalds 	return i;
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
771bf0813bdSPaul Gortmaker static long num_arg(const char __user *user_buffer, unsigned long maxlen,
772bf0813bdSPaul Gortmaker 				unsigned long *num)
7731da177e4SLinus Torvalds {
774d6182223SPaul Gortmaker 	int i;
7751da177e4SLinus Torvalds 	*num = 0;
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 		if ((c >= '0') && (c <= '9')) {
7821da177e4SLinus Torvalds 			*num *= 10;
7831da177e4SLinus Torvalds 			*num += c - '0';
7841da177e4SLinus Torvalds 		} else
7851da177e4SLinus Torvalds 			break;
7861da177e4SLinus Torvalds 	}
7871da177e4SLinus Torvalds 	return i;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
7911da177e4SLinus Torvalds {
792d6182223SPaul Gortmaker 	int i;
7931da177e4SLinus Torvalds 
794d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7951da177e4SLinus Torvalds 		char c;
7961da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7971da177e4SLinus Torvalds 			return -EFAULT;
7981da177e4SLinus Torvalds 		switch (c) {
7991da177e4SLinus Torvalds 		case '\"':
8001da177e4SLinus Torvalds 		case '\n':
8011da177e4SLinus Torvalds 		case '\r':
8021da177e4SLinus Torvalds 		case '\t':
8031da177e4SLinus Torvalds 		case ' ':
8041da177e4SLinus Torvalds 			goto done_str;
8051da177e4SLinus Torvalds 			break;
8061da177e4SLinus Torvalds 		default:
8071da177e4SLinus Torvalds 			break;
8083ff50b79SStephen Hemminger 		}
8091da177e4SLinus Torvalds 	}
8101da177e4SLinus Torvalds done_str:
8111da177e4SLinus Torvalds 	return i;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
814ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
815ca6549afSSteven Whitehouse {
81695c96174SEric Dumazet 	unsigned int n = 0;
817ca6549afSSteven Whitehouse 	char c;
818ca6549afSSteven Whitehouse 	ssize_t i = 0;
819ca6549afSSteven Whitehouse 	int len;
820ca6549afSSteven Whitehouse 
821ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = 0;
822ca6549afSSteven Whitehouse 	do {
823ca6549afSSteven Whitehouse 		__u32 tmp;
8241ca7768cSFrancesco Fondelli 		len = hex32_arg(&buffer[i], 8, &tmp);
825ca6549afSSteven Whitehouse 		if (len <= 0)
826ca6549afSSteven Whitehouse 			return len;
827ca6549afSSteven Whitehouse 		pkt_dev->labels[n] = htonl(tmp);
828ca6549afSSteven Whitehouse 		if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
829ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
830ca6549afSSteven Whitehouse 		i += len;
831ca6549afSSteven Whitehouse 		if (get_user(c, &buffer[i]))
832ca6549afSSteven Whitehouse 			return -EFAULT;
833ca6549afSSteven Whitehouse 		i++;
834ca6549afSSteven Whitehouse 		n++;
835ca6549afSSteven Whitehouse 		if (n >= MAX_MPLS_LABELS)
836ca6549afSSteven Whitehouse 			return -E2BIG;
837ca6549afSSteven Whitehouse 	} while (c == ',');
838ca6549afSSteven Whitehouse 
839ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = n;
840ca6549afSSteven Whitehouse 	return i;
841ca6549afSSteven Whitehouse }
842ca6549afSSteven Whitehouse 
843222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file,
844222f1806SLuiz Capitulino 			       const char __user * user_buffer, size_t count,
845222f1806SLuiz Capitulino 			       loff_t * offset)
8461da177e4SLinus Torvalds {
8478a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
848d50a6b56SStephen Hemminger 	struct pktgen_dev *pkt_dev = seq->private;
849d6182223SPaul Gortmaker 	int i, max, len;
8501da177e4SLinus Torvalds 	char name[16], valstr[32];
8511da177e4SLinus Torvalds 	unsigned long value = 0;
8521da177e4SLinus Torvalds 	char *pg_result = NULL;
8531da177e4SLinus Torvalds 	int tmp = 0;
8541da177e4SLinus Torvalds 	char buf[128];
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	pg_result = &(pkt_dev->result[0]);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	if (count < 1) {
859f9467eaeSJoe Perches 		pr_warning("wrong command format\n");
8601da177e4SLinus Torvalds 		return -EINVAL;
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 
863d6182223SPaul Gortmaker 	max = count;
864d6182223SPaul Gortmaker 	tmp = count_trail_chars(user_buffer, max);
8651da177e4SLinus Torvalds 	if (tmp < 0) {
866f9467eaeSJoe Perches 		pr_warning("illegal format\n");
8671da177e4SLinus Torvalds 		return tmp;
8681da177e4SLinus Torvalds 	}
869d6182223SPaul Gortmaker 	i = tmp;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	/* Read variable name */
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
87463adc6fbSStephen Hemminger 	if (len < 0)
875222f1806SLuiz Capitulino 		return len;
87663adc6fbSStephen Hemminger 
8771da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
8781da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
8791da177e4SLinus Torvalds 		return -EFAULT;
8801da177e4SLinus Torvalds 	i += len;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	max = count - i;
8831da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
8841da177e4SLinus Torvalds 	if (len < 0)
8851da177e4SLinus Torvalds 		return len;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	i += len;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	if (debug) {
89086c2c0a8SDmitry Torokhov 		size_t copy = min_t(size_t, count, 1023);
891448d7b5dSNelson Elhage 		char tb[copy + 1];
892448d7b5dSNelson Elhage 		if (copy_from_user(tb, user_buffer, copy))
8931da177e4SLinus Torvalds 			return -EFAULT;
894448d7b5dSNelson Elhage 		tb[copy] = 0;
895f342cda7SJoe Perches 		pr_debug("%s,%lu  buffer -:%s:-\n",
896f342cda7SJoe Perches 			 name, (unsigned long)count, tb);
8971da177e4SLinus Torvalds 	}
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	if (!strcmp(name, "min_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->min_pkt_size) {
9081da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9091da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9101da177e4SLinus Torvalds 		}
911222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: min_pkt_size=%u",
912222f1806SLuiz Capitulino 			pkt_dev->min_pkt_size);
9131da177e4SLinus Torvalds 		return count;
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	if (!strcmp(name, "max_pkt_size")) {
9171da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
91863adc6fbSStephen Hemminger 		if (len < 0)
919222f1806SLuiz Capitulino 			return len;
92063adc6fbSStephen Hemminger 
9211da177e4SLinus Torvalds 		i += len;
9221da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9231da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9241da177e4SLinus Torvalds 		if (value != pkt_dev->max_pkt_size) {
9251da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9261da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9271da177e4SLinus Torvalds 		}
928222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: max_pkt_size=%u",
929222f1806SLuiz Capitulino 			pkt_dev->max_pkt_size);
9301da177e4SLinus Torvalds 		return count;
9311da177e4SLinus Torvalds 	}
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	/* Shortcut for min = max */
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	if (!strcmp(name, "pkt_size")) {
9361da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
93763adc6fbSStephen Hemminger 		if (len < 0)
938222f1806SLuiz Capitulino 			return len;
93963adc6fbSStephen Hemminger 
9401da177e4SLinus Torvalds 		i += len;
9411da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9421da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9431da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9441da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9451da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9461da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9471da177e4SLinus Torvalds 		}
9481da177e4SLinus Torvalds 		sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
9491da177e4SLinus Torvalds 		return count;
9501da177e4SLinus Torvalds 	}
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	if (!strcmp(name, "debug")) {
9531da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
95463adc6fbSStephen Hemminger 		if (len < 0)
955222f1806SLuiz Capitulino 			return len;
95663adc6fbSStephen Hemminger 
9571da177e4SLinus Torvalds 		i += len;
9581da177e4SLinus Torvalds 		debug = value;
9591da177e4SLinus Torvalds 		sprintf(pg_result, "OK: debug=%u", debug);
9601da177e4SLinus Torvalds 		return count;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	if (!strcmp(name, "frags")) {
9641da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
96563adc6fbSStephen Hemminger 		if (len < 0)
966222f1806SLuiz Capitulino 			return len;
96763adc6fbSStephen Hemminger 
9681da177e4SLinus Torvalds 		i += len;
9691da177e4SLinus Torvalds 		pkt_dev->nfrags = value;
9701da177e4SLinus Torvalds 		sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
9711da177e4SLinus Torvalds 		return count;
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 	if (!strcmp(name, "delay")) {
9741da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
97563adc6fbSStephen Hemminger 		if (len < 0)
976222f1806SLuiz Capitulino 			return len;
97763adc6fbSStephen Hemminger 
9781da177e4SLinus Torvalds 		i += len;
979fd29cf72SStephen Hemminger 		if (value == 0x7FFFFFFF)
980fd29cf72SStephen Hemminger 			pkt_dev->delay = ULLONG_MAX;
981fd29cf72SStephen Hemminger 		else
9829240d715SEric Dumazet 			pkt_dev->delay = (u64)value;
983fd29cf72SStephen Hemminger 
984fd29cf72SStephen Hemminger 		sprintf(pg_result, "OK: delay=%llu",
985fd29cf72SStephen Hemminger 			(unsigned long long) pkt_dev->delay);
9861da177e4SLinus Torvalds 		return count;
9871da177e4SLinus Torvalds 	}
98843d28b65SDaniel Turull 	if (!strcmp(name, "rate")) {
98943d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
99043d28b65SDaniel Turull 		if (len < 0)
99143d28b65SDaniel Turull 			return len;
99243d28b65SDaniel Turull 
99343d28b65SDaniel Turull 		i += len;
99443d28b65SDaniel Turull 		if (!value)
99543d28b65SDaniel Turull 			return len;
99643d28b65SDaniel Turull 		pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
99743d28b65SDaniel Turull 		if (debug)
998f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
99943d28b65SDaniel Turull 
100043d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
100143d28b65SDaniel Turull 		return count;
100243d28b65SDaniel Turull 	}
100343d28b65SDaniel Turull 	if (!strcmp(name, "ratep")) {
100443d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
100543d28b65SDaniel Turull 		if (len < 0)
100643d28b65SDaniel Turull 			return len;
100743d28b65SDaniel Turull 
100843d28b65SDaniel Turull 		i += len;
100943d28b65SDaniel Turull 		if (!value)
101043d28b65SDaniel Turull 			return len;
101143d28b65SDaniel Turull 		pkt_dev->delay = NSEC_PER_SEC/value;
101243d28b65SDaniel Turull 		if (debug)
1013f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
101443d28b65SDaniel Turull 
101543d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
101643d28b65SDaniel Turull 		return count;
101743d28b65SDaniel Turull 	}
10181da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_min")) {
10191da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
102063adc6fbSStephen Hemminger 		if (len < 0)
1021222f1806SLuiz Capitulino 			return len;
102263adc6fbSStephen Hemminger 
10231da177e4SLinus Torvalds 		i += len;
10241da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_min) {
10251da177e4SLinus Torvalds 			pkt_dev->udp_src_min = value;
10261da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10271da177e4SLinus Torvalds 		}
10281da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
10291da177e4SLinus Torvalds 		return count;
10301da177e4SLinus Torvalds 	}
10311da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_min")) {
10321da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
103363adc6fbSStephen Hemminger 		if (len < 0)
1034222f1806SLuiz Capitulino 			return len;
103563adc6fbSStephen Hemminger 
10361da177e4SLinus Torvalds 		i += len;
10371da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_min) {
10381da177e4SLinus Torvalds 			pkt_dev->udp_dst_min = value;
10391da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10401da177e4SLinus Torvalds 		}
10411da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
10421da177e4SLinus Torvalds 		return count;
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_max")) {
10451da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
104663adc6fbSStephen Hemminger 		if (len < 0)
1047222f1806SLuiz Capitulino 			return len;
104863adc6fbSStephen Hemminger 
10491da177e4SLinus Torvalds 		i += len;
10501da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_max) {
10511da177e4SLinus Torvalds 			pkt_dev->udp_src_max = value;
10521da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10531da177e4SLinus Torvalds 		}
10541da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
10551da177e4SLinus Torvalds 		return count;
10561da177e4SLinus Torvalds 	}
10571da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_max")) {
10581da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
105963adc6fbSStephen Hemminger 		if (len < 0)
1060222f1806SLuiz Capitulino 			return len;
106163adc6fbSStephen Hemminger 
10621da177e4SLinus Torvalds 		i += len;
10631da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_max) {
10641da177e4SLinus Torvalds 			pkt_dev->udp_dst_max = value;
10651da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10661da177e4SLinus Torvalds 		}
10671da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
10681da177e4SLinus Torvalds 		return count;
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 	if (!strcmp(name, "clone_skb")) {
10711da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
107263adc6fbSStephen Hemminger 		if (len < 0)
1073222f1806SLuiz Capitulino 			return len;
1074d8873315SNeil Horman 		if ((value > 0) &&
1075d8873315SNeil Horman 		    (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1076d8873315SNeil Horman 			return -ENOTSUPP;
10771da177e4SLinus Torvalds 		i += len;
10781da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
10811da177e4SLinus Torvalds 		return count;
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
10841da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
108563adc6fbSStephen Hemminger 		if (len < 0)
1086222f1806SLuiz Capitulino 			return len;
108763adc6fbSStephen Hemminger 
10881da177e4SLinus Torvalds 		i += len;
10891da177e4SLinus Torvalds 		pkt_dev->count = value;
10901da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
10911da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
10921da177e4SLinus Torvalds 		return count;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
10951da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
109663adc6fbSStephen Hemminger 		if (len < 0)
1097222f1806SLuiz Capitulino 			return len;
109863adc6fbSStephen Hemminger 
10991da177e4SLinus Torvalds 		i += len;
11001da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
11011da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
11021da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
11031da177e4SLinus Torvalds 		}
1104222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1105222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
11061da177e4SLinus Torvalds 		return count;
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
11091da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
111063adc6fbSStephen Hemminger 		if (len < 0)
1111222f1806SLuiz Capitulino 			return len;
111263adc6fbSStephen Hemminger 
11131da177e4SLinus Torvalds 		i += len;
11141da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
11151da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
11161da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
11171da177e4SLinus Torvalds 		}
1118222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1119222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
11201da177e4SLinus Torvalds 		return count;
11211da177e4SLinus Torvalds 	}
1122e99b99b4SRobert Olsson 	if (!strcmp(name, "node")) {
1123e99b99b4SRobert Olsson 		len = num_arg(&user_buffer[i], 10, &value);
1124e99b99b4SRobert Olsson 		if (len < 0)
1125e99b99b4SRobert Olsson 			return len;
1126e99b99b4SRobert Olsson 
1127e99b99b4SRobert Olsson 		i += len;
1128e99b99b4SRobert Olsson 
1129e99b99b4SRobert Olsson 		if (node_possible(value)) {
1130e99b99b4SRobert Olsson 			pkt_dev->node = value;
1131e99b99b4SRobert Olsson 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
113226ad7879SEric Dumazet 			if (pkt_dev->page) {
113326ad7879SEric Dumazet 				put_page(pkt_dev->page);
113426ad7879SEric Dumazet 				pkt_dev->page = NULL;
113526ad7879SEric Dumazet 			}
1136e99b99b4SRobert Olsson 		}
1137e99b99b4SRobert Olsson 		else
1138e99b99b4SRobert Olsson 			sprintf(pg_result, "ERROR: node not possible");
1139e99b99b4SRobert Olsson 		return count;
1140e99b99b4SRobert Olsson 	}
11411da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
11421da177e4SLinus Torvalds 		char f[32];
11431da177e4SLinus Torvalds 		memset(f, 0, 32);
11441da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
114563adc6fbSStephen Hemminger 		if (len < 0)
1146222f1806SLuiz Capitulino 			return len;
114763adc6fbSStephen Hemminger 
11481da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
11491da177e4SLinus Torvalds 			return -EFAULT;
11501da177e4SLinus Torvalds 		i += len;
11511da177e4SLinus Torvalds 		if (strcmp(f, "IPSRC_RND") == 0)
11521da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPSRC_RND;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 		else if (strcmp(f, "!IPSRC_RND") == 0)
11551da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPSRC_RND;
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 		else if (strcmp(f, "TXSIZE_RND") == 0)
11581da177e4SLinus Torvalds 			pkt_dev->flags |= F_TXSIZE_RND;
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 		else if (strcmp(f, "!TXSIZE_RND") == 0)
11611da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_TXSIZE_RND;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 		else if (strcmp(f, "IPDST_RND") == 0)
11641da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPDST_RND;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 		else if (strcmp(f, "!IPDST_RND") == 0)
11671da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPDST_RND;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 		else if (strcmp(f, "UDPSRC_RND") == 0)
11701da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPSRC_RND;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPSRC_RND") == 0)
11731da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPSRC_RND;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 		else if (strcmp(f, "UDPDST_RND") == 0)
11761da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPDST_RND;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPDST_RND") == 0)
11791da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPDST_RND;
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 		else if (strcmp(f, "MACSRC_RND") == 0)
11821da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACSRC_RND;
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 		else if (strcmp(f, "!MACSRC_RND") == 0)
11851da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACSRC_RND;
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 		else if (strcmp(f, "MACDST_RND") == 0)
11881da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACDST_RND;
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 		else if (strcmp(f, "!MACDST_RND") == 0)
11911da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACDST_RND;
11921da177e4SLinus Torvalds 
1193ca6549afSSteven Whitehouse 		else if (strcmp(f, "MPLS_RND") == 0)
1194ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
1195ca6549afSSteven Whitehouse 
1196ca6549afSSteven Whitehouse 		else if (strcmp(f, "!MPLS_RND") == 0)
1197ca6549afSSteven Whitehouse 			pkt_dev->flags &= ~F_MPLS_RND;
1198ca6549afSSteven Whitehouse 
119934954ddcSFrancesco Fondelli 		else if (strcmp(f, "VID_RND") == 0)
120034954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_VID_RND;
120134954ddcSFrancesco Fondelli 
120234954ddcSFrancesco Fondelli 		else if (strcmp(f, "!VID_RND") == 0)
120334954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_VID_RND;
120434954ddcSFrancesco Fondelli 
120534954ddcSFrancesco Fondelli 		else if (strcmp(f, "SVID_RND") == 0)
120634954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_SVID_RND;
120734954ddcSFrancesco Fondelli 
120834954ddcSFrancesco Fondelli 		else if (strcmp(f, "!SVID_RND") == 0)
120934954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_SVID_RND;
121034954ddcSFrancesco Fondelli 
1211007a531bSJamal Hadi Salim 		else if (strcmp(f, "FLOW_SEQ") == 0)
1212007a531bSJamal Hadi Salim 			pkt_dev->flags |= F_FLOW_SEQ;
1213007a531bSJamal Hadi Salim 
121445b270f8SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
121545b270f8SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_RND;
121645b270f8SRobert Olsson 
121745b270f8SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
121845b270f8SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1219e6fce5b9SRobert Olsson 
1220e6fce5b9SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1221e6fce5b9SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1222e6fce5b9SRobert Olsson 
1223e6fce5b9SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1224e6fce5b9SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1225a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
1226a553e4a6SJamal Hadi Salim 		else if (strcmp(f, "IPSEC") == 0)
1227a553e4a6SJamal Hadi Salim 			pkt_dev->flags |= F_IPSEC_ON;
1228a553e4a6SJamal Hadi Salim #endif
1229a553e4a6SJamal Hadi Salim 
12301ca7768cSFrancesco Fondelli 		else if (strcmp(f, "!IPV6") == 0)
12311ca7768cSFrancesco Fondelli 			pkt_dev->flags &= ~F_IPV6;
12321ca7768cSFrancesco Fondelli 
1233e99b99b4SRobert Olsson 		else if (strcmp(f, "NODE_ALLOC") == 0)
1234e99b99b4SRobert Olsson 			pkt_dev->flags |= F_NODE;
1235e99b99b4SRobert Olsson 
1236e99b99b4SRobert Olsson 		else if (strcmp(f, "!NODE_ALLOC") == 0)
1237e99b99b4SRobert Olsson 			pkt_dev->flags &= ~F_NODE;
1238e99b99b4SRobert Olsson 
1239c26bf4a5SThomas Graf 		else if (strcmp(f, "UDPCSUM") == 0)
1240c26bf4a5SThomas Graf 			pkt_dev->flags |= F_UDPCSUM;
1241c26bf4a5SThomas Graf 
1242c26bf4a5SThomas Graf 		else if (strcmp(f, "!UDPCSUM") == 0)
1243c26bf4a5SThomas Graf 			pkt_dev->flags &= ~F_UDPCSUM;
1244c26bf4a5SThomas Graf 
12451da177e4SLinus Torvalds 		else {
1246222f1806SLuiz Capitulino 			sprintf(pg_result,
1247222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
12481da177e4SLinus Torvalds 				f,
12491ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
125072f8e06fSMathias Krause 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
125172f8e06fSMathias Krause 				"MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
125272f8e06fSMathias Krause 				"QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
125372f8e06fSMathias Krause #ifdef CONFIG_XFRM
125472f8e06fSMathias Krause 				"IPSEC, "
125572f8e06fSMathias Krause #endif
125672f8e06fSMathias Krause 				"NODE_ALLOC\n");
12571da177e4SLinus Torvalds 			return count;
12581da177e4SLinus Torvalds 		}
12591da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
12601da177e4SLinus Torvalds 		return count;
12611da177e4SLinus Torvalds 	}
12621da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
12631da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
126463adc6fbSStephen Hemminger 		if (len < 0)
1265222f1806SLuiz Capitulino 			return len;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12681da177e4SLinus Torvalds 			return -EFAULT;
12691da177e4SLinus Torvalds 		buf[len] = 0;
12701da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
12711da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
12721da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_min, buf, len);
12731da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
12741da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
12751da177e4SLinus Torvalds 		}
12761da177e4SLinus Torvalds 		if (debug)
1277f342cda7SJoe Perches 			pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
12781da177e4SLinus Torvalds 		i += len;
12791da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
12801da177e4SLinus Torvalds 		return count;
12811da177e4SLinus Torvalds 	}
12821da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
12831da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
128463adc6fbSStephen Hemminger 		if (len < 0)
1285222f1806SLuiz Capitulino 			return len;
128663adc6fbSStephen Hemminger 
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12891da177e4SLinus Torvalds 			return -EFAULT;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 		buf[len] = 0;
12921da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
12931da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
12941da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_max, buf, len);
12951da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
12961da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
12971da177e4SLinus Torvalds 		}
12981da177e4SLinus Torvalds 		if (debug)
1299f342cda7SJoe Perches 			pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
13001da177e4SLinus Torvalds 		i += len;
13011da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
13021da177e4SLinus Torvalds 		return count;
13031da177e4SLinus Torvalds 	}
13041da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
13051da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1306222f1806SLuiz Capitulino 		if (len < 0)
1307222f1806SLuiz Capitulino 			return len;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13121da177e4SLinus Torvalds 			return -EFAULT;
13131da177e4SLinus Torvalds 		buf[len] = 0;
13141da177e4SLinus Torvalds 
1315c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
131647a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
13171da177e4SLinus Torvalds 
13184e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 		if (debug)
1321f342cda7SJoe Perches 			pr_debug("dst6 set to: %s\n", buf);
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 		i += len;
13241da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
13251da177e4SLinus Torvalds 		return count;
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
13281da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1329222f1806SLuiz Capitulino 		if (len < 0)
1330222f1806SLuiz Capitulino 			return len;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13351da177e4SLinus Torvalds 			return -EFAULT;
13361da177e4SLinus Torvalds 		buf[len] = 0;
13371da177e4SLinus Torvalds 
1338c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
133947a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
13401da177e4SLinus Torvalds 
13414e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
13421da177e4SLinus Torvalds 		if (debug)
1343f342cda7SJoe Perches 			pr_debug("dst6_min set to: %s\n", buf);
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 		i += len;
13461da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
13471da177e4SLinus Torvalds 		return count;
13481da177e4SLinus Torvalds 	}
13491da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
13501da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1351222f1806SLuiz Capitulino 		if (len < 0)
1352222f1806SLuiz Capitulino 			return len;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13571da177e4SLinus Torvalds 			return -EFAULT;
13581da177e4SLinus Torvalds 		buf[len] = 0;
13591da177e4SLinus Torvalds 
1360c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
136147a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 		if (debug)
1364f342cda7SJoe Perches 			pr_debug("dst6_max set to: %s\n", buf);
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 		i += len;
13671da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
13681da177e4SLinus Torvalds 		return count;
13691da177e4SLinus Torvalds 	}
13701da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
13711da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1372222f1806SLuiz Capitulino 		if (len < 0)
1373222f1806SLuiz Capitulino 			return len;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13781da177e4SLinus Torvalds 			return -EFAULT;
13791da177e4SLinus Torvalds 		buf[len] = 0;
13801da177e4SLinus Torvalds 
1381c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
138247a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
13831da177e4SLinus Torvalds 
13844e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 		if (debug)
1387f342cda7SJoe Perches 			pr_debug("src6 set to: %s\n", buf);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 		i += len;
13901da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
13911da177e4SLinus Torvalds 		return count;
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
13941da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
139563adc6fbSStephen Hemminger 		if (len < 0)
1396222f1806SLuiz Capitulino 			return len;
139763adc6fbSStephen Hemminger 
13981da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13991da177e4SLinus Torvalds 			return -EFAULT;
14001da177e4SLinus Torvalds 		buf[len] = 0;
14011da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
14021da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
14031da177e4SLinus Torvalds 			strncpy(pkt_dev->src_min, buf, len);
14041da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
14051da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
14061da177e4SLinus Torvalds 		}
14071da177e4SLinus Torvalds 		if (debug)
1408f342cda7SJoe Perches 			pr_debug("src_min set to: %s\n", pkt_dev->src_min);
14091da177e4SLinus Torvalds 		i += len;
14101da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
14111da177e4SLinus Torvalds 		return count;
14121da177e4SLinus Torvalds 	}
14131da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
14141da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
141563adc6fbSStephen Hemminger 		if (len < 0)
1416222f1806SLuiz Capitulino 			return len;
141763adc6fbSStephen Hemminger 
14181da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14191da177e4SLinus Torvalds 			return -EFAULT;
14201da177e4SLinus Torvalds 		buf[len] = 0;
14211da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
14221da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
14231da177e4SLinus Torvalds 			strncpy(pkt_dev->src_max, buf, len);
14241da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
14251da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
14261da177e4SLinus Torvalds 		}
14271da177e4SLinus Torvalds 		if (debug)
1428f342cda7SJoe Perches 			pr_debug("src_max set to: %s\n", pkt_dev->src_max);
14291da177e4SLinus Torvalds 		i += len;
14301da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
14311da177e4SLinus Torvalds 		return count;
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
14341da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
143563adc6fbSStephen Hemminger 		if (len < 0)
1436222f1806SLuiz Capitulino 			return len;
143763adc6fbSStephen Hemminger 
14381da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14391da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14401da177e4SLinus Torvalds 			return -EFAULT;
14411da177e4SLinus Torvalds 
14424940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->dst_mac))
14434940fc88SAlexey Dobriyan 			return -EINVAL;
14441da177e4SLinus Torvalds 		/* Set up Dest MAC */
14459ea08b12SJoe Perches 		ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
14461da177e4SLinus Torvalds 
14474940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
14481da177e4SLinus Torvalds 		return count;
14491da177e4SLinus Torvalds 	}
14501da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
14511da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
145263adc6fbSStephen Hemminger 		if (len < 0)
1453222f1806SLuiz Capitulino 			return len;
145463adc6fbSStephen Hemminger 
14551da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14561da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14571da177e4SLinus Torvalds 			return -EFAULT;
14581da177e4SLinus Torvalds 
14594940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->src_mac))
14604940fc88SAlexey Dobriyan 			return -EINVAL;
1461ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
14629ea08b12SJoe Perches 		ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
1463ce5d0b47SAdit Ranadive 
14644940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
14651da177e4SLinus Torvalds 		return count;
14661da177e4SLinus Torvalds 	}
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
14691da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
14701da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
14711da177e4SLinus Torvalds 		return count;
14721da177e4SLinus Torvalds 	}
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
14751da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
147663adc6fbSStephen Hemminger 		if (len < 0)
1477222f1806SLuiz Capitulino 			return len;
147863adc6fbSStephen Hemminger 
14791da177e4SLinus Torvalds 		i += len;
14801da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
14811da177e4SLinus Torvalds 			value = MAX_CFLOWS;
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 		pkt_dev->cflows = value;
14841da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
14851da177e4SLinus Torvalds 		return count;
14861da177e4SLinus Torvalds 	}
14876bae9190SFan Du #ifdef CONFIG_XFRM
1488de4aee7dSFan Du 	if (!strcmp(name, "spi")) {
1489de4aee7dSFan Du 		len = num_arg(&user_buffer[i], 10, &value);
1490de4aee7dSFan Du 		if (len < 0)
1491de4aee7dSFan Du 			return len;
1492de4aee7dSFan Du 
1493de4aee7dSFan Du 		i += len;
1494de4aee7dSFan Du 		pkt_dev->spi = value;
1495de4aee7dSFan Du 		sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1496de4aee7dSFan Du 		return count;
1497de4aee7dSFan Du 	}
14986bae9190SFan Du #endif
14991da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
15001da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
150163adc6fbSStephen Hemminger 		if (len < 0)
1502222f1806SLuiz Capitulino 			return len;
150363adc6fbSStephen Hemminger 
15041da177e4SLinus Torvalds 		i += len;
15051da177e4SLinus Torvalds 		pkt_dev->lflow = value;
15061da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
15071da177e4SLinus Torvalds 		return count;
15081da177e4SLinus Torvalds 	}
15091da177e4SLinus Torvalds 
151045b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
151145b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
151263adc6fbSStephen Hemminger 		if (len < 0)
151345b270f8SRobert Olsson 			return len;
151463adc6fbSStephen Hemminger 
151545b270f8SRobert Olsson 		i += len;
151645b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
151745b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
151845b270f8SRobert Olsson 		return count;
151945b270f8SRobert Olsson 	}
152045b270f8SRobert Olsson 
152145b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
152245b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
152363adc6fbSStephen Hemminger 		if (len < 0)
152445b270f8SRobert Olsson 			return len;
152563adc6fbSStephen Hemminger 
152645b270f8SRobert Olsson 		i += len;
152745b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
152845b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
152945b270f8SRobert Olsson 		return count;
153045b270f8SRobert Olsson 	}
153145b270f8SRobert Olsson 
1532ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
153395c96174SEric Dumazet 		unsigned int n, cnt;
1534cfcabdccSStephen Hemminger 
1535ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1536cfcabdccSStephen Hemminger 		if (len < 0)
1537cfcabdccSStephen Hemminger 			return len;
1538ca6549afSSteven Whitehouse 		i += len;
1539cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1540ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1541cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1542ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1543ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
154434954ddcSFrancesco Fondelli 
154534954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
154634954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
154734954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
154834954ddcSFrancesco Fondelli 
154934954ddcSFrancesco Fondelli 			if (debug)
1550f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN auto turned off\n");
155134954ddcSFrancesco Fondelli 		}
155234954ddcSFrancesco Fondelli 		return count;
155334954ddcSFrancesco Fondelli 	}
155434954ddcSFrancesco Fondelli 
155534954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
155634954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
155763adc6fbSStephen Hemminger 		if (len < 0)
155834954ddcSFrancesco Fondelli 			return len;
155963adc6fbSStephen Hemminger 
156034954ddcSFrancesco Fondelli 		i += len;
156134954ddcSFrancesco Fondelli 		if (value <= 4095) {
156234954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
156334954ddcSFrancesco Fondelli 
156434954ddcSFrancesco Fondelli 			if (debug)
1565f342cda7SJoe Perches 				pr_debug("VLAN turned on\n");
156634954ddcSFrancesco Fondelli 
156734954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1568f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
156934954ddcSFrancesco Fondelli 
157034954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
157134954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
157234954ddcSFrancesco Fondelli 		} else {
157334954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
157434954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
157534954ddcSFrancesco Fondelli 
157634954ddcSFrancesco Fondelli 			if (debug)
1577f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
157834954ddcSFrancesco Fondelli 		}
157934954ddcSFrancesco Fondelli 		return count;
158034954ddcSFrancesco Fondelli 	}
158134954ddcSFrancesco Fondelli 
158234954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
158334954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
158463adc6fbSStephen Hemminger 		if (len < 0)
158534954ddcSFrancesco Fondelli 			return len;
158663adc6fbSStephen Hemminger 
158734954ddcSFrancesco Fondelli 		i += len;
158834954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
158934954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
159034954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
159134954ddcSFrancesco Fondelli 		} else {
159234954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
159334954ddcSFrancesco Fondelli 		}
159434954ddcSFrancesco Fondelli 		return count;
159534954ddcSFrancesco Fondelli 	}
159634954ddcSFrancesco Fondelli 
159734954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
159834954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
159963adc6fbSStephen Hemminger 		if (len < 0)
160034954ddcSFrancesco Fondelli 			return len;
160163adc6fbSStephen Hemminger 
160234954ddcSFrancesco Fondelli 		i += len;
160334954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
160434954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
160534954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
160634954ddcSFrancesco Fondelli 		} else {
160734954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
160834954ddcSFrancesco Fondelli 		}
160934954ddcSFrancesco Fondelli 		return count;
161034954ddcSFrancesco Fondelli 	}
161134954ddcSFrancesco Fondelli 
161234954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
161334954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
161463adc6fbSStephen Hemminger 		if (len < 0)
161534954ddcSFrancesco Fondelli 			return len;
161663adc6fbSStephen Hemminger 
161734954ddcSFrancesco Fondelli 		i += len;
161834954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
161934954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
162034954ddcSFrancesco Fondelli 
162134954ddcSFrancesco Fondelli 			if (debug)
1622f342cda7SJoe Perches 				pr_debug("SVLAN turned on\n");
162334954ddcSFrancesco Fondelli 
162434954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1625f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
162634954ddcSFrancesco Fondelli 
162734954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
162834954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
162934954ddcSFrancesco Fondelli 		} else {
163034954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
163134954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
163234954ddcSFrancesco Fondelli 
163334954ddcSFrancesco Fondelli 			if (debug)
1634f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
163534954ddcSFrancesco Fondelli 		}
163634954ddcSFrancesco Fondelli 		return count;
163734954ddcSFrancesco Fondelli 	}
163834954ddcSFrancesco Fondelli 
163934954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
164034954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
164163adc6fbSStephen Hemminger 		if (len < 0)
164234954ddcSFrancesco Fondelli 			return len;
164363adc6fbSStephen Hemminger 
164434954ddcSFrancesco Fondelli 		i += len;
164534954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
164634954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
164734954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
164834954ddcSFrancesco Fondelli 		} else {
164934954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
165034954ddcSFrancesco Fondelli 		}
165134954ddcSFrancesco Fondelli 		return count;
165234954ddcSFrancesco Fondelli 	}
165334954ddcSFrancesco Fondelli 
165434954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
165534954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
165663adc6fbSStephen Hemminger 		if (len < 0)
165734954ddcSFrancesco Fondelli 			return len;
165863adc6fbSStephen Hemminger 
165934954ddcSFrancesco Fondelli 		i += len;
166034954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
166134954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
166234954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
166334954ddcSFrancesco Fondelli 		} else {
166434954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
166534954ddcSFrancesco Fondelli 		}
1666ca6549afSSteven Whitehouse 		return count;
1667ca6549afSSteven Whitehouse 	}
1668ca6549afSSteven Whitehouse 
16691ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
16701ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16711ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
167263adc6fbSStephen Hemminger 		if (len < 0)
16731ca7768cSFrancesco Fondelli 			return len;
167463adc6fbSStephen Hemminger 
16751ca7768cSFrancesco Fondelli 		i += len;
16761ca7768cSFrancesco Fondelli 		if (len == 2) {
16771ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
16781ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
16791ca7768cSFrancesco Fondelli 		} else {
16801ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
16811ca7768cSFrancesco Fondelli 		}
16821ca7768cSFrancesco Fondelli 		return count;
16831ca7768cSFrancesco Fondelli 	}
16841ca7768cSFrancesco Fondelli 
16851ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
16861ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16871ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
168863adc6fbSStephen Hemminger 		if (len < 0)
16891ca7768cSFrancesco Fondelli 			return len;
169063adc6fbSStephen Hemminger 
16911ca7768cSFrancesco Fondelli 		i += len;
16921ca7768cSFrancesco Fondelli 		if (len == 2) {
16931ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
16941ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
16951ca7768cSFrancesco Fondelli 		} else {
16961ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
16971ca7768cSFrancesco Fondelli 		}
16981ca7768cSFrancesco Fondelli 		return count;
16991ca7768cSFrancesco Fondelli 	}
17001ca7768cSFrancesco Fondelli 
17019e50e3acSJohn Fastabend 	if (!strcmp(name, "skb_priority")) {
17029e50e3acSJohn Fastabend 		len = num_arg(&user_buffer[i], 9, &value);
17039e50e3acSJohn Fastabend 		if (len < 0)
17049e50e3acSJohn Fastabend 			return len;
17059e50e3acSJohn Fastabend 
17069e50e3acSJohn Fastabend 		i += len;
17079e50e3acSJohn Fastabend 		pkt_dev->skb_priority = value;
17089e50e3acSJohn Fastabend 		sprintf(pg_result, "OK: skb_priority=%i",
17099e50e3acSJohn Fastabend 			pkt_dev->skb_priority);
17109e50e3acSJohn Fastabend 		return count;
17119e50e3acSJohn Fastabend 	}
17129e50e3acSJohn Fastabend 
17131da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
17141da177e4SLinus Torvalds 	return -EINVAL;
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
1717d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
17181da177e4SLinus Torvalds {
1719d9dda78bSAl Viro 	return single_open(file, pktgen_if_show, PDE_DATA(inode));
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17229a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1723d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1724d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1725d50a6b56SStephen Hemminger 	.read    = seq_read,
1726d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1727d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1728d50a6b56SStephen Hemminger 	.release = single_release,
1729d50a6b56SStephen Hemminger };
1730d50a6b56SStephen Hemminger 
1731d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1732d50a6b56SStephen Hemminger {
1733d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1734648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1735d50a6b56SStephen Hemminger 
1736d50a6b56SStephen Hemminger 	BUG_ON(!t);
1737d50a6b56SStephen Hemminger 
173897dc48e2SThomas Graf 	seq_puts(seq, "Running: ");
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds 	if_lock(t);
1741c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17421da177e4SLinus Torvalds 		if (pkt_dev->running)
1743593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17441da177e4SLinus Torvalds 
174597dc48e2SThomas Graf 	seq_puts(seq, "\nStopped: ");
17461da177e4SLinus Torvalds 
1747c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17481da177e4SLinus Torvalds 		if (!pkt_dev->running)
1749593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	if (t->result[0])
1752d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
17531da177e4SLinus Torvalds 	else
175497dc48e2SThomas Graf 		seq_puts(seq, "\nResult: NA\n");
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	if_unlock(t);
17571da177e4SLinus Torvalds 
1758d50a6b56SStephen Hemminger 	return 0;
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
1761d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1762d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1763d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
17641da177e4SLinus Torvalds {
17658a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
1766d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1767d6182223SPaul Gortmaker 	int i, max, len, ret;
17681da177e4SLinus Torvalds 	char name[40];
17691da177e4SLinus Torvalds 	char *pg_result;
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	if (count < 1) {
17721da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
17731da177e4SLinus Torvalds 		return -EINVAL;
17741da177e4SLinus Torvalds 	}
17751da177e4SLinus Torvalds 
1776d6182223SPaul Gortmaker 	max = count;
1777d6182223SPaul Gortmaker 	len = count_trail_chars(user_buffer, max);
17781da177e4SLinus Torvalds 	if (len < 0)
17791da177e4SLinus Torvalds 		return len;
17801da177e4SLinus Torvalds 
1781d6182223SPaul Gortmaker 	i = len;
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	/* Read variable name */
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
17861da177e4SLinus Torvalds 	if (len < 0)
17871da177e4SLinus Torvalds 		return len;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
17901da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
17911da177e4SLinus Torvalds 		return -EFAULT;
17921da177e4SLinus Torvalds 	i += len;
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	max = count - i;
17951da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
17961da177e4SLinus Torvalds 	if (len < 0)
17971da177e4SLinus Torvalds 		return len;
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	i += len;
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	if (debug)
1802f342cda7SJoe Perches 		pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds 	if (!t) {
1805f9467eaeSJoe Perches 		pr_err("ERROR: No thread\n");
18061da177e4SLinus Torvalds 		ret = -EINVAL;
18071da177e4SLinus Torvalds 		goto out;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
18131da177e4SLinus Torvalds 		char f[32];
18141da177e4SLinus Torvalds 		memset(f, 0, 32);
18151da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
18161da177e4SLinus Torvalds 		if (len < 0) {
18171da177e4SLinus Torvalds 			ret = len;
18181da177e4SLinus Torvalds 			goto out;
18191da177e4SLinus Torvalds 		}
18201da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
18211da177e4SLinus Torvalds 			return -EFAULT;
18221da177e4SLinus Torvalds 		i += len;
18236146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
1824604dfd6eSCong Wang 		ret = pktgen_add_device(t, f);
18256146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1826604dfd6eSCong Wang 		if (!ret) {
18271da177e4SLinus Torvalds 			ret = count;
18281da177e4SLinus Torvalds 			sprintf(pg_result, "OK: add_device=%s", f);
1829604dfd6eSCong Wang 		} else
1830604dfd6eSCong Wang 			sprintf(pg_result, "ERROR: can not add device %s", f);
18311da177e4SLinus Torvalds 		goto out;
18321da177e4SLinus Torvalds 	}
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
18356146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
183695ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
18376146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1838121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
18391da177e4SLinus Torvalds 		ret = count;
18401da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
18411da177e4SLinus Torvalds 		goto out;
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1845b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
18461da177e4SLinus Torvalds 		ret = count;
18471da177e4SLinus Torvalds 		goto out;
18481da177e4SLinus Torvalds 	}
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	ret = -EINVAL;
18511da177e4SLinus Torvalds out:
18521da177e4SLinus Torvalds 	return ret;
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
1855d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
18561da177e4SLinus Torvalds {
1857d9dda78bSAl Viro 	return single_open(file, pktgen_thread_show, PDE_DATA(inode));
18581da177e4SLinus Torvalds }
18591da177e4SLinus Torvalds 
18609a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1861d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1862d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1863d50a6b56SStephen Hemminger 	.read    = seq_read,
1864d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1865d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1866d50a6b56SStephen Hemminger 	.release = single_release,
1867d50a6b56SStephen Hemminger };
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds /* Think find or remove for NN */
18704e58a027SCong Wang static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
18714e58a027SCong Wang 					      const char *ifname, int remove)
18721da177e4SLinus Torvalds {
18731da177e4SLinus Torvalds 	struct pktgen_thread *t;
18741da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
18753e984840SEric Dumazet 	bool exact = (remove == FIND);
18761da177e4SLinus Torvalds 
18774e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
18783e984840SEric Dumazet 		pkt_dev = pktgen_find_dev(t, ifname, exact);
18791da177e4SLinus Torvalds 		if (pkt_dev) {
18801da177e4SLinus Torvalds 			if (remove) {
18811da177e4SLinus Torvalds 				if_lock(t);
188295ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
188395ed63f7SArthur Kepner 				t->control |= T_REMDEV;
18841da177e4SLinus Torvalds 				if_unlock(t);
18851da177e4SLinus Torvalds 			}
18861da177e4SLinus Torvalds 			break;
18871da177e4SLinus Torvalds 		}
18881da177e4SLinus Torvalds 	}
18891da177e4SLinus Torvalds 	return pkt_dev;
18901da177e4SLinus Torvalds }
18911da177e4SLinus Torvalds 
189295ed63f7SArthur Kepner /*
189395ed63f7SArthur Kepner  * mark a device for removal
189495ed63f7SArthur Kepner  */
18954e58a027SCong Wang static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
189895ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
189995ed63f7SArthur Kepner 	int i = 0;
190095ed63f7SArthur Kepner 
19016146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
1902f9467eaeSJoe Perches 	pr_debug("%s: marking %s for removal\n", __func__, ifname);
190395ed63f7SArthur Kepner 
190495ed63f7SArthur Kepner 	while (1) {
190595ed63f7SArthur Kepner 
19064e58a027SCong Wang 		pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1907222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1908222f1806SLuiz Capitulino 			break;	/* success */
190995ed63f7SArthur Kepner 
19106146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1911f9467eaeSJoe Perches 		pr_debug("%s: waiting for %s to disappear....\n",
1912f9467eaeSJoe Perches 			 __func__, ifname);
191395ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
19146146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
191595ed63f7SArthur Kepner 
191695ed63f7SArthur Kepner 		if (++i >= max_tries) {
1917f9467eaeSJoe Perches 			pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1918f9467eaeSJoe Perches 			       __func__, msec_per_try * i, ifname);
191995ed63f7SArthur Kepner 			break;
192095ed63f7SArthur Kepner 		}
192195ed63f7SArthur Kepner 
192295ed63f7SArthur Kepner 	}
192395ed63f7SArthur Kepner 
19246146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
192539df232fSStephen Hemminger }
192695ed63f7SArthur Kepner 
19274e58a027SCong Wang static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
192839df232fSStephen Hemminger {
192939df232fSStephen Hemminger 	struct pktgen_thread *t;
193039df232fSStephen Hemminger 
19314e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
193239df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
193339df232fSStephen Hemminger 
193439df232fSStephen Hemminger 		list_for_each_entry(pkt_dev, &t->if_list, list) {
193539df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
193639df232fSStephen Hemminger 				continue;
193739df232fSStephen Hemminger 
1938a8ca16eaSDavid Howells 			proc_remove(pkt_dev->entry);
193939df232fSStephen Hemminger 
19402975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
19414e58a027SCong Wang 							  pn->proc_dir,
19422975315bSAlexey Dobriyan 							  &pktgen_if_fops,
19432975315bSAlexey Dobriyan 							  pkt_dev);
194439df232fSStephen Hemminger 			if (!pkt_dev->entry)
1945f9467eaeSJoe Perches 				pr_err("can't move proc entry for '%s'\n",
1946f9467eaeSJoe Perches 				       dev->name);
194739df232fSStephen Hemminger 			break;
194839df232fSStephen Hemminger 		}
194939df232fSStephen Hemminger 	}
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
1952222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
1953222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
19541da177e4SLinus Torvalds {
1955351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
19564e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
19571da177e4SLinus Torvalds 
19584e58a027SCong Wang 	if (pn->pktgen_exiting)
1959e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1960e9dc8653SEric W. Biederman 
19611da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
19621da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
19631da177e4SLinus Torvalds 	 */
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	switch (event) {
196639df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
19674e58a027SCong Wang 		pktgen_change_name(pn, dev);
19681da177e4SLinus Torvalds 		break;
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
19714e58a027SCong Wang 		pktgen_mark_device(pn, dev->name);
19721da177e4SLinus Torvalds 		break;
19733ff50b79SStephen Hemminger 	}
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	return NOTIFY_DONE;
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds 
19784e58a027SCong Wang static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
19794e58a027SCong Wang 						 struct pktgen_dev *pkt_dev,
198063adc6fbSStephen Hemminger 						 const char *ifname)
1981e6fce5b9SRobert Olsson {
1982e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
1983d6182223SPaul Gortmaker 	int i;
1984e6fce5b9SRobert Olsson 
1985e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
1986e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
1987e6fce5b9SRobert Olsson 			break;
1988e6fce5b9SRobert Olsson 
1989e6fce5b9SRobert Olsson 		b[i] = ifname[i];
1990e6fce5b9SRobert Olsson 	}
1991e6fce5b9SRobert Olsson 	b[i] = 0;
1992e6fce5b9SRobert Olsson 
19934e58a027SCong Wang 	return dev_get_by_name(pn->net, b);
1994e6fce5b9SRobert Olsson }
1995e6fce5b9SRobert Olsson 
1996e6fce5b9SRobert Olsson 
19971da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
19981da177e4SLinus Torvalds 
19994e58a027SCong Wang static int pktgen_setup_dev(const struct pktgen_net *pn,
20004e58a027SCong Wang 			    struct pktgen_dev *pkt_dev, const char *ifname)
2001222f1806SLuiz Capitulino {
20021da177e4SLinus Torvalds 	struct net_device *odev;
200339df232fSStephen Hemminger 	int err;
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	/* Clean old setups */
20061da177e4SLinus Torvalds 	if (pkt_dev->odev) {
20071da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
20081da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
20091da177e4SLinus Torvalds 	}
20101da177e4SLinus Torvalds 
20114e58a027SCong Wang 	odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
20121da177e4SLinus Torvalds 	if (!odev) {
2013f9467eaeSJoe Perches 		pr_err("no such netdevice: \"%s\"\n", ifname);
201439df232fSStephen Hemminger 		return -ENODEV;
20151da177e4SLinus Torvalds 	}
201639df232fSStephen Hemminger 
20171da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
2018f9467eaeSJoe Perches 		pr_err("not an ethernet device: \"%s\"\n", ifname);
201939df232fSStephen Hemminger 		err = -EINVAL;
202039df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
2021f9467eaeSJoe Perches 		pr_err("device is down: \"%s\"\n", ifname);
202239df232fSStephen Hemminger 		err = -ENETDOWN;
202339df232fSStephen Hemminger 	} else {
20241da177e4SLinus Torvalds 		pkt_dev->odev = odev;
202539df232fSStephen Hemminger 		return 0;
202639df232fSStephen Hemminger 	}
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	dev_put(odev);
202939df232fSStephen Hemminger 	return err;
20301da177e4SLinus Torvalds }
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
20331da177e4SLinus Torvalds  * structure to have the right information to create/send packets
20341da177e4SLinus Torvalds  */
20351da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
20361da177e4SLinus Torvalds {
203764c00d81SAndrew Gallatin 	int ntxq;
203864c00d81SAndrew Gallatin 
20391da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
2040f9467eaeSJoe Perches 		pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2041222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
2042222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
20431da177e4SLinus Torvalds 		return;
20441da177e4SLinus Torvalds 	}
20451da177e4SLinus Torvalds 
204664c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
204764c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2048bfdbc0acSRobert Olsson 
204964c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
2050f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
205188271660SJesse Brandeburg 			   pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2052593f63b0SEric Dumazet 			   pkt_dev->odevname);
205326e29eedSDan Carpenter 		pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
205464c00d81SAndrew Gallatin 	}
205588271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
2056f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
205788271660SJesse Brandeburg 			   pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2058593f63b0SEric Dumazet 			   pkt_dev->odevname);
205926e29eedSDan Carpenter 		pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
206064c00d81SAndrew Gallatin 	}
206164c00d81SAndrew Gallatin 
20621da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
20631da177e4SLinus Torvalds 
2064f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
20659ea08b12SJoe Perches 		ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	/* Set up Dest MAC */
20689ea08b12SJoe Perches 	ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
20714c139b8cSAmerigo Wang 		int i, set = 0, err = 1;
20724c139b8cSAmerigo Wang 		struct inet6_dev *idev;
20734c139b8cSAmerigo Wang 
207468bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
207568bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
207668bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
207768bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
207868bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
207968bf9f0bSAmerigo Wang 		}
208068bf9f0bSAmerigo Wang 
20811da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
20821da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
20831da177e4SLinus Torvalds 				set = 1;
20841da177e4SLinus Torvalds 				break;
20851da177e4SLinus Torvalds 			}
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 		if (!set) {
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 			/*
20901da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
20911da177e4SLinus Torvalds 			 *
20921da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
20931da177e4SLinus Torvalds 			 */
20941da177e4SLinus Torvalds 
20958814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
209663adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
209763adc6fbSStephen Hemminger 			if (idev) {
20981da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
21014c139b8cSAmerigo Wang 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
21024c139b8cSAmerigo Wang 					if ((ifp->scope & IFA_LINK) &&
2103f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
21044e3fd7a0SAlexey Dobriyan 						pkt_dev->cur_in6_saddr = ifp->addr;
21051da177e4SLinus Torvalds 						err = 0;
21061da177e4SLinus Torvalds 						break;
21071da177e4SLinus Torvalds 					}
21081da177e4SLinus Torvalds 				}
21091da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
21101da177e4SLinus Torvalds 			}
21118814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2112222f1806SLuiz Capitulino 			if (err)
2113f9467eaeSJoe Perches 				pr_err("ERROR: IPv6 link address not available\n");
21141da177e4SLinus Torvalds 		}
2115222f1806SLuiz Capitulino 	} else {
211668bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
211768bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
211868bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
211968bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
212068bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
212168bf9f0bSAmerigo Wang 		}
212268bf9f0bSAmerigo Wang 
21231da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
21241da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
21251da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 			struct in_device *in_dev;
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds 			rcu_read_lock();
2130e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
21311da177e4SLinus Torvalds 			if (in_dev) {
21321da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2133222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2134222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
21351da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
21361da177e4SLinus Torvalds 				}
21371da177e4SLinus Torvalds 			}
21381da177e4SLinus Torvalds 			rcu_read_unlock();
2139222f1806SLuiz Capitulino 		} else {
21401da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
21411da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
21421da177e4SLinus Torvalds 		}
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
21451da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
21461da177e4SLinus Torvalds 	}
21471da177e4SLinus Torvalds 	/* Initialize current values. */
214868bf9f0bSAmerigo Wang 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
214968bf9f0bSAmerigo Wang 	if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
215068bf9f0bSAmerigo Wang 		pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
215168bf9f0bSAmerigo Wang 
21521da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
21531da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
21541da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
21551da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
21561da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
21571da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
21581da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
21591da177e4SLinus Torvalds }
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 
2162fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2163fd29cf72SStephen Hemminger {
2164ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2165417bc4b8SEric Dumazet 	s64 remaining;
21662bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2167fd29cf72SStephen Hemminger 
21682bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
21692bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2170fd29cf72SStephen Hemminger 
217143d28b65SDaniel Turull 	remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2172417bc4b8SEric Dumazet 	if (remaining <= 0) {
2173417bc4b8SEric Dumazet 		pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21741da177e4SLinus Torvalds 		return;
2175417bc4b8SEric Dumazet 	}
21762bc481cfSStephen Hemminger 
2177398f382cSDaniel Borkmann 	start_time = ktime_get();
217833136d12SEric Dumazet 	if (remaining < 100000) {
217933136d12SEric Dumazet 		/* for small delays (<100us), just loop until limit is reached */
218033136d12SEric Dumazet 		do {
2181398f382cSDaniel Borkmann 			end_time = ktime_get();
2182398f382cSDaniel Borkmann 		} while (ktime_compare(end_time, spin_until) < 0);
218333136d12SEric Dumazet 	} else {
21842bc481cfSStephen Hemminger 		/* see do_nanosleep */
21852bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
21862bc481cfSStephen Hemminger 		do {
21872bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
21882bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
21892bc481cfSStephen Hemminger 			if (!hrtimer_active(&t.timer))
21902bc481cfSStephen Hemminger 				t.task = NULL;
21912bc481cfSStephen Hemminger 
21922bc481cfSStephen Hemminger 			if (likely(t.task))
21931da177e4SLinus Torvalds 				schedule();
21941da177e4SLinus Torvalds 
21952bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
21962bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
21972bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
2198398f382cSDaniel Borkmann 		end_time = ktime_get();
219933136d12SEric Dumazet 	}
2200ef87979cSStephen Hemminger 
2201ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
220207a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
22031da177e4SLinus Torvalds }
22041da177e4SLinus Torvalds 
220516dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
220616dab72fSJamal Hadi Salim {
2207a553e4a6SJamal Hadi Salim 	pkt_dev->pkt_overhead = 0;
220816dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
220916dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
221016dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
221116dab72fSJamal Hadi Salim }
221216dab72fSJamal Hadi Salim 
2213648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2214007a531bSJamal Hadi Salim {
2215648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2216007a531bSJamal Hadi Salim }
2217007a531bSJamal Hadi Salim 
2218007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2219007a531bSJamal Hadi Salim {
2220007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2221007a531bSJamal Hadi Salim 
2222007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2223007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2224007a531bSJamal Hadi Salim 			/* reset time */
2225007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22261211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2227007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2228007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2229007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2230007a531bSJamal Hadi Salim 		}
2231007a531bSJamal Hadi Salim 	} else {
223233d7c5e5SAkinobu Mita 		flow = prandom_u32() % pkt_dev->cflows;
22331211a645SRobert Olsson 		pkt_dev->curfl = flow;
2234007a531bSJamal Hadi Salim 
22351211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2236007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22371211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
22381211a645SRobert Olsson 		}
2239007a531bSJamal Hadi Salim 	}
2240007a531bSJamal Hadi Salim 
2241007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2242007a531bSJamal Hadi Salim }
2243007a531bSJamal Hadi Salim 
2244a553e4a6SJamal Hadi Salim 
2245a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2246a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2247a553e4a6SJamal Hadi Salim  * we go look for it ...
2248a553e4a6SJamal Hadi Salim */
2249bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2250fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2251a553e4a6SJamal Hadi Salim {
2252a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
22534e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2254a553e4a6SJamal Hadi Salim 	if (!x) {
2255c454997eSFan Du 
2256c454997eSFan Du 		if (pkt_dev->spi) {
2257c454997eSFan Du 			/* We need as quick as possible to find the right SA
2258c454997eSFan Du 			 * Searching with minimum criteria to archieve this.
2259c454997eSFan Du 			 */
2260c454997eSFan Du 			x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2261c454997eSFan Du 		} else {
2262a553e4a6SJamal Hadi Salim 			/* slow path: we dont already have xfrm_state */
22634e58a027SCong Wang 			x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
22645447c5e4SAlexey Dobriyan 						(xfrm_address_t *)&pkt_dev->cur_daddr,
2265a553e4a6SJamal Hadi Salim 						(xfrm_address_t *)&pkt_dev->cur_saddr,
2266a553e4a6SJamal Hadi Salim 						AF_INET,
2267a553e4a6SJamal Hadi Salim 						pkt_dev->ipsmode,
2268a553e4a6SJamal Hadi Salim 						pkt_dev->ipsproto, 0);
2269c454997eSFan Du 		}
2270a553e4a6SJamal Hadi Salim 		if (x) {
2271a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2272a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2273a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2274a553e4a6SJamal Hadi Salim 		}
2275a553e4a6SJamal Hadi Salim 
2276a553e4a6SJamal Hadi Salim 	}
2277a553e4a6SJamal Hadi Salim }
2278a553e4a6SJamal Hadi Salim #endif
2279fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2280fd2ea0a7SDavid S. Miller {
2281e6fce5b9SRobert Olsson 
2282e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2283e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2284e6fce5b9SRobert Olsson 
2285896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2286fd2ea0a7SDavid S. Miller 		__u16 t;
2287fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
228833d7c5e5SAkinobu Mita 			t = prandom_u32() %
2289fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2290fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2291fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2292fd2ea0a7SDavid S. Miller 		} else {
2293fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2294fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2295fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2296fd2ea0a7SDavid S. Miller 		}
2297fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2298fd2ea0a7SDavid S. Miller 	}
2299bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2300fd2ea0a7SDavid S. Miller }
2301fd2ea0a7SDavid S. Miller 
23021da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
23031da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
23041da177e4SLinus Torvalds  */
2305222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2306222f1806SLuiz Capitulino {
23071da177e4SLinus Torvalds 	__u32 imn;
23081da177e4SLinus Torvalds 	__u32 imx;
23091da177e4SLinus Torvalds 	int flow = 0;
23101da177e4SLinus Torvalds 
2311007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2312007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds 	/*  Deal with source MAC */
23151da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
23161da177e4SLinus Torvalds 		__u32 mc;
23171da177e4SLinus Torvalds 		__u32 tmp;
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
232033d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->src_mac_count;
23211da177e4SLinus Torvalds 		else {
23221da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2323ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2324222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
23251da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
23261da177e4SLinus Torvalds 		}
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
23291da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
23301da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23311da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
23321da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23331da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
23341da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23351da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
23361da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
23371da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
23381da177e4SLinus Torvalds 	}
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
23411da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
23421da177e4SLinus Torvalds 		__u32 mc;
23431da177e4SLinus Torvalds 		__u32 tmp;
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
234633d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->dst_mac_count;
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds 		else {
23491da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2350ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2351222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
23521da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
23531da177e4SLinus Torvalds 			}
23541da177e4SLinus Torvalds 		}
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
23571da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
23581da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23591da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
23601da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23611da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
23621da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23631da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
23641da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
23651da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
23661da177e4SLinus Torvalds 	}
23671da177e4SLinus Torvalds 
2368ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
236995c96174SEric Dumazet 		unsigned int i;
2370ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2371ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2372ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
237333d7c5e5SAkinobu Mita 					     ((__force __be32)prandom_u32() &
2374ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2375ca6549afSSteven Whitehouse 	}
2376ca6549afSSteven Whitehouse 
237734954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
237833d7c5e5SAkinobu Mita 		pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
237934954ddcSFrancesco Fondelli 	}
238034954ddcSFrancesco Fondelli 
238134954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
238233d7c5e5SAkinobu Mita 		pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
238334954ddcSFrancesco Fondelli 	}
238434954ddcSFrancesco Fondelli 
23851da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
23861da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
238733d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_src = prandom_u32() %
23885fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
23895fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 		else {
23921da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
23931da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
23941da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
23951da177e4SLinus Torvalds 		}
23961da177e4SLinus Torvalds 	}
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
23991da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
240033d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_dst = prandom_u32() %
24015fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
24025fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2403222f1806SLuiz Capitulino 		} else {
24041da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
24051da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
24061da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
24071da177e4SLinus Torvalds 		}
24081da177e4SLinus Torvalds 	}
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
24111da177e4SLinus Torvalds 
241263adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
241363adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
241463adc6fbSStephen Hemminger 		if (imn < imx) {
24151da177e4SLinus Torvalds 			__u32 t;
24161da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
241733d7c5e5SAkinobu Mita 				t = prandom_u32() % (imx - imn) + imn;
24181da177e4SLinus Torvalds 			else {
24191da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
24201da177e4SLinus Torvalds 				t++;
242163adc6fbSStephen Hemminger 				if (t > imx)
24221da177e4SLinus Torvalds 					t = imn;
242363adc6fbSStephen Hemminger 
24241da177e4SLinus Torvalds 			}
24251da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
24261da177e4SLinus Torvalds 		}
24271da177e4SLinus Torvalds 
2428007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
24291da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
24301da177e4SLinus Torvalds 		} else {
2431252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2432252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2433252e3346SAl Viro 			if (imn < imx) {
24341da177e4SLinus Torvalds 				__u32 t;
2435252e3346SAl Viro 				__be32 s;
24361da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
24371da177e4SLinus Torvalds 
243870e3ba72SAkinobu Mita 					do {
243933d7c5e5SAkinobu Mita 						t = prandom_u32() %
244033d7c5e5SAkinobu Mita 							(imx - imn) + imn;
2441252e3346SAl Viro 						s = htonl(t);
244270e3ba72SAkinobu Mita 					} while (ipv4_is_loopback(s) ||
244370e3ba72SAkinobu Mita 						ipv4_is_multicast(s) ||
244470e3ba72SAkinobu Mita 						ipv4_is_lbcast(s) ||
244570e3ba72SAkinobu Mita 						ipv4_is_zeronet(s) ||
244670e3ba72SAkinobu Mita 						ipv4_is_local_multicast(s));
2447252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2448252e3346SAl Viro 				} else {
24491da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
24501da177e4SLinus Torvalds 					t++;
24511da177e4SLinus Torvalds 					if (t > imx) {
24521da177e4SLinus Torvalds 						t = imn;
24531da177e4SLinus Torvalds 					}
24541da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
24551da177e4SLinus Torvalds 				}
24561da177e4SLinus Torvalds 			}
24571da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2458007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2459222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2460222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2461a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2462a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2463a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2464a553e4a6SJamal Hadi Salim #endif
24651da177e4SLinus Torvalds 				pkt_dev->nflows++;
24661da177e4SLinus Torvalds 			}
24671da177e4SLinus Torvalds 		}
2468222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2469222f1806SLuiz Capitulino 
247006e30411SJoe Perches 		if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
24711da177e4SLinus Torvalds 			int i;
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 			/* Only random destinations yet */
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
24761da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
247733d7c5e5SAkinobu Mita 				    (((__force __be32)prandom_u32() |
24781da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
24791da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
24801da177e4SLinus Torvalds 			}
24811da177e4SLinus Torvalds 		}
24821da177e4SLinus Torvalds 	}
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
24851da177e4SLinus Torvalds 		__u32 t;
24861da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
248733d7c5e5SAkinobu Mita 			t = prandom_u32() %
24885fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
24895fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2490222f1806SLuiz Capitulino 		} else {
24911da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
24921da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
24931da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
24941da177e4SLinus Torvalds 		}
24951da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
24961da177e4SLinus Torvalds 	}
24971da177e4SLinus Torvalds 
2498fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
249945b270f8SRobert Olsson 
25001da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
25011da177e4SLinus Torvalds }
25021da177e4SLinus Torvalds 
2503a553e4a6SJamal Hadi Salim 
2504a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
25055537a055SFengguang Wu static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2506cf93d47eSFan Du 
2507cf93d47eSFan Du 	[RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2508cf93d47eSFan Du };
2509cf93d47eSFan Du 
2510a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2511a553e4a6SJamal Hadi Salim {
2512a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2513a553e4a6SJamal Hadi Salim 	int err = 0;
25146de9ace4SFan Du 	struct net *net = dev_net(pkt_dev->odev);
2515a553e4a6SJamal Hadi Salim 
2516a553e4a6SJamal Hadi Salim 	if (!x)
2517a553e4a6SJamal Hadi Salim 		return 0;
2518a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2519a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2520cf93d47eSFan Du 	if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2521a553e4a6SJamal Hadi Salim 		return 0;
2522a553e4a6SJamal Hadi Salim 
2523cf93d47eSFan Du 	/* But when user specify an valid SPI, transformation
2524cf93d47eSFan Du 	 * supports both transport/tunnel mode + ESP/AH type.
2525cf93d47eSFan Du 	 */
2526cf93d47eSFan Du 	if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2527cf93d47eSFan Du 		skb->_skb_refdst = (unsigned long)&pkt_dev->dst | SKB_DST_NOREF;
2528cf93d47eSFan Du 
2529cf93d47eSFan Du 	rcu_read_lock_bh();
253013996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2531cf93d47eSFan Du 	rcu_read_unlock_bh();
25326de9ace4SFan Du 	if (err) {
25336de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2534a553e4a6SJamal Hadi Salim 		goto error;
25356de9ace4SFan Du 	}
2536a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
25376de9ace4SFan Du 	if (err) {
25386de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2539a553e4a6SJamal Hadi Salim 		goto error;
25406de9ace4SFan Du 	}
25410af0a413SFan Du 	spin_lock_bh(&x->lock);
2542a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2543a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
25440af0a413SFan Du 	spin_unlock_bh(&x->lock);
2545a553e4a6SJamal Hadi Salim error:
2546a553e4a6SJamal Hadi Salim 	return err;
2547a553e4a6SJamal Hadi Salim }
2548a553e4a6SJamal Hadi Salim 
2549475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2550a553e4a6SJamal Hadi Salim {
2551a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2552a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2553d6182223SPaul Gortmaker 		int i;
2554d6182223SPaul Gortmaker 		for (i = 0; i < pkt_dev->cflows; i++) {
2555a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2556a553e4a6SJamal Hadi Salim 			if (x) {
2557a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2558a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2559a553e4a6SJamal Hadi Salim 			}
2560a553e4a6SJamal Hadi Salim 		}
2561a553e4a6SJamal Hadi Salim 	}
2562a553e4a6SJamal Hadi Salim }
2563a553e4a6SJamal Hadi Salim 
2564475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2565a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2566a553e4a6SJamal Hadi Salim {
2567a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2568a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2569a553e4a6SJamal Hadi Salim 		int nhead = 0;
2570a553e4a6SJamal Hadi Salim 		if (x) {
2571a553e4a6SJamal Hadi Salim 			int ret;
2572a553e4a6SJamal Hadi Salim 			__u8 *eth;
25733868204dSfan.du 			struct iphdr *iph;
25743868204dSfan.du 
2575a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2576a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2577a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2578a553e4a6SJamal Hadi Salim 				if (ret < 0) {
2579f9467eaeSJoe Perches 					pr_err("Error expanding ipsec packet %d\n",
2580f9467eaeSJoe Perches 					       ret);
2581b4bb4ac8SIlpo Järvinen 					goto err;
2582a553e4a6SJamal Hadi Salim 				}
2583a553e4a6SJamal Hadi Salim 			}
2584a553e4a6SJamal Hadi Salim 
2585a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2586a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2587a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2588a553e4a6SJamal Hadi Salim 			if (ret) {
2589f9467eaeSJoe Perches 				pr_err("Error creating ipsec packet %d\n", ret);
2590b4bb4ac8SIlpo Järvinen 				goto err;
2591a553e4a6SJamal Hadi Salim 			}
2592a553e4a6SJamal Hadi Salim 			/* restore ll */
2593a553e4a6SJamal Hadi Salim 			eth = (__u8 *) skb_push(skb, ETH_HLEN);
2594a553e4a6SJamal Hadi Salim 			memcpy(eth, pkt_dev->hh, 12);
2595a553e4a6SJamal Hadi Salim 			*(u16 *) &eth[12] = protocol;
25963868204dSfan.du 
25973868204dSfan.du 			/* Update IPv4 header len as well as checksum value */
25983868204dSfan.du 			iph = ip_hdr(skb);
25993868204dSfan.du 			iph->tot_len = htons(skb->len - ETH_HLEN);
26003868204dSfan.du 			ip_send_check(iph);
2601a553e4a6SJamal Hadi Salim 		}
2602a553e4a6SJamal Hadi Salim 	}
2603a553e4a6SJamal Hadi Salim 	return 1;
2604b4bb4ac8SIlpo Järvinen err:
2605b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2606b4bb4ac8SIlpo Järvinen 	return 0;
2607a553e4a6SJamal Hadi Salim }
2608a553e4a6SJamal Hadi Salim #endif
2609a553e4a6SJamal Hadi Salim 
2610ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2611ca6549afSSteven Whitehouse {
261295c96174SEric Dumazet 	unsigned int i;
261363adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2614ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
261563adc6fbSStephen Hemminger 
2616ca6549afSSteven Whitehouse 	mpls--;
2617ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2618ca6549afSSteven Whitehouse }
2619ca6549afSSteven Whitehouse 
26200f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
26210f37c605SAl Viro 			       unsigned int prio)
26220f37c605SAl Viro {
26230f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
26240f37c605SAl Viro }
26250f37c605SAl Viro 
262626ad7879SEric Dumazet static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
262726ad7879SEric Dumazet 				int datalen)
262826ad7879SEric Dumazet {
262926ad7879SEric Dumazet 	struct timeval timestamp;
263026ad7879SEric Dumazet 	struct pktgen_hdr *pgh;
263126ad7879SEric Dumazet 
263226ad7879SEric Dumazet 	pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
263326ad7879SEric Dumazet 	datalen -= sizeof(*pgh);
263426ad7879SEric Dumazet 
263526ad7879SEric Dumazet 	if (pkt_dev->nfrags <= 0) {
263605aebe2eSDaniel Turull 		memset(skb_put(skb, datalen), 0, datalen);
263726ad7879SEric Dumazet 	} else {
263826ad7879SEric Dumazet 		int frags = pkt_dev->nfrags;
263926ad7879SEric Dumazet 		int i, len;
26407d36a991Samit salecha 		int frag_len;
264126ad7879SEric Dumazet 
264226ad7879SEric Dumazet 
264326ad7879SEric Dumazet 		if (frags > MAX_SKB_FRAGS)
264426ad7879SEric Dumazet 			frags = MAX_SKB_FRAGS;
264526ad7879SEric Dumazet 		len = datalen - frags * PAGE_SIZE;
264626ad7879SEric Dumazet 		if (len > 0) {
264726ad7879SEric Dumazet 			memset(skb_put(skb, len), 0, len);
264826ad7879SEric Dumazet 			datalen = frags * PAGE_SIZE;
264926ad7879SEric Dumazet 		}
265026ad7879SEric Dumazet 
265126ad7879SEric Dumazet 		i = 0;
26527d36a991Samit salecha 		frag_len = (datalen/frags) < PAGE_SIZE ?
26537d36a991Samit salecha 			   (datalen/frags) : PAGE_SIZE;
265426ad7879SEric Dumazet 		while (datalen > 0) {
265526ad7879SEric Dumazet 			if (unlikely(!pkt_dev->page)) {
265626ad7879SEric Dumazet 				int node = numa_node_id();
265726ad7879SEric Dumazet 
265826ad7879SEric Dumazet 				if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
265926ad7879SEric Dumazet 					node = pkt_dev->node;
266026ad7879SEric Dumazet 				pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
266126ad7879SEric Dumazet 				if (!pkt_dev->page)
266226ad7879SEric Dumazet 					break;
266326ad7879SEric Dumazet 			}
2664a0bec1cdSIan Campbell 			get_page(pkt_dev->page);
2665ea2ab693SIan Campbell 			skb_frag_set_page(skb, i, pkt_dev->page);
266626ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page_offset = 0;
26677d36a991Samit salecha 			/*last fragment, fill rest of data*/
26687d36a991Samit salecha 			if (i == (frags - 1))
26699e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i],
26709e903e08SEric Dumazet 				    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
26717d36a991Samit salecha 			else
26729e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
26739e903e08SEric Dumazet 			datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
26749e903e08SEric Dumazet 			skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
26759e903e08SEric Dumazet 			skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
267626ad7879SEric Dumazet 			i++;
267726ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
267826ad7879SEric Dumazet 		}
267926ad7879SEric Dumazet 	}
268026ad7879SEric Dumazet 
268126ad7879SEric Dumazet 	/* Stamp the time, and sequence number,
268226ad7879SEric Dumazet 	 * convert them to network byte order
268326ad7879SEric Dumazet 	 */
268426ad7879SEric Dumazet 	pgh->pgh_magic = htonl(PKTGEN_MAGIC);
268526ad7879SEric Dumazet 	pgh->seq_num = htonl(pkt_dev->seq_num);
268626ad7879SEric Dumazet 
268726ad7879SEric Dumazet 	do_gettimeofday(&timestamp);
268826ad7879SEric Dumazet 	pgh->tv_sec = htonl(timestamp.tv_sec);
268926ad7879SEric Dumazet 	pgh->tv_usec = htonl(timestamp.tv_usec);
269026ad7879SEric Dumazet }
269126ad7879SEric Dumazet 
26927a6e288dSDaniel Borkmann static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
26937a6e288dSDaniel Borkmann 					struct pktgen_dev *pkt_dev,
26947a6e288dSDaniel Borkmann 					unsigned int extralen)
26957a6e288dSDaniel Borkmann {
26967a6e288dSDaniel Borkmann 	struct sk_buff *skb = NULL;
26977a6e288dSDaniel Borkmann 	unsigned int size = pkt_dev->cur_pkt_size + 64 + extralen +
26987a6e288dSDaniel Borkmann 			    pkt_dev->pkt_overhead;
26997a6e288dSDaniel Borkmann 
27007a6e288dSDaniel Borkmann 	if (pkt_dev->flags & F_NODE) {
27017a6e288dSDaniel Borkmann 		int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
27027a6e288dSDaniel Borkmann 
27037a6e288dSDaniel Borkmann 		skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
27047a6e288dSDaniel Borkmann 		if (likely(skb)) {
27057a6e288dSDaniel Borkmann 			skb_reserve(skb, NET_SKB_PAD);
27067a6e288dSDaniel Borkmann 			skb->dev = dev;
27077a6e288dSDaniel Borkmann 		}
27087a6e288dSDaniel Borkmann 	} else {
27097a6e288dSDaniel Borkmann 		 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
27107a6e288dSDaniel Borkmann 	}
27117a6e288dSDaniel Borkmann 
27127a6e288dSDaniel Borkmann 	return skb;
27137a6e288dSDaniel Borkmann }
27147a6e288dSDaniel Borkmann 
27151da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
27161da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
27171da177e4SLinus Torvalds {
27181da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
27191da177e4SLinus Torvalds 	__u8 *eth;
27201da177e4SLinus Torvalds 	struct udphdr *udph;
27211da177e4SLinus Torvalds 	int datalen, iplen;
27221da177e4SLinus Torvalds 	struct iphdr *iph;
2723d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2724ca6549afSSteven Whitehouse 	__be32 *mpls;
272534954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
272634954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
272734954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
272834954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2729fd2ea0a7SDavid S. Miller 	u16 queue_map;
2730ca6549afSSteven Whitehouse 
2731ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2732d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
27331da177e4SLinus Torvalds 
273434954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2735d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
273634954ddcSFrancesco Fondelli 
273764053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
273864053beeSRobert Olsson 	 * fields.
273964053beeSRobert Olsson 	 */
274064053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2741eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
274264053beeSRobert Olsson 
27437ac5459eSDavid S. Miller 	datalen = (odev->hard_header_len + 16) & ~0xf;
2744e99b99b4SRobert Olsson 
27457a6e288dSDaniel Borkmann 	skb = pktgen_alloc_skb(odev, pkt_dev, datalen);
27461da177e4SLinus Torvalds 	if (!skb) {
27471da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
27481da177e4SLinus Torvalds 		return NULL;
27491da177e4SLinus Torvalds 	}
27501da177e4SLinus Torvalds 
27517a6e288dSDaniel Borkmann 	prefetchw(skb->data);
27527ac5459eSDavid S. Miller 	skb_reserve(skb, datalen);
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
27551da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2756ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2757ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2758ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
275934954ddcSFrancesco Fondelli 
276034954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
276134954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
276234954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27630f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
27640f37c605SAl Viro 					       pkt_dev->svlan_cfi,
27650f37c605SAl Viro 					       pkt_dev->svlan_p);
276634954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2767d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
276834954ddcSFrancesco Fondelli 		}
276934954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27700f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
27710f37c605SAl Viro 				      pkt_dev->vlan_cfi,
27720f37c605SAl Viro 				      pkt_dev->vlan_p);
277334954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2774d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
277534954ddcSFrancesco Fondelli 	}
277634954ddcSFrancesco Fondelli 
2777525cebedSThomas Graf 	skb_set_mac_header(skb, 0);
2778525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2779525cebedSThomas Graf 	iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2780525cebedSThomas Graf 
2781525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
2782525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
2783fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
27849e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
27859e50e3acSJohn Fastabend 
27861da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2787252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
27881da177e4SLinus Torvalds 
2789ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2790ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
279116dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
27926af773e7SNishank Trivedi 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
27931da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
27941da177e4SLinus Torvalds 
27951da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
27961da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
27971da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
2798c26bf4a5SThomas Graf 	udph->check = 0;
27991da177e4SLinus Torvalds 
28001da177e4SLinus Torvalds 	iph->ihl = 5;
28011da177e4SLinus Torvalds 	iph->version = 4;
28021da177e4SLinus Torvalds 	iph->ttl = 32;
28031ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
28041da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
28051da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
28061da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
280766ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
280866ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
28091da177e4SLinus Torvalds 	iph->frag_off = 0;
28101da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
28111da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
281203c633e7SThomas Graf 	ip_send_check(iph);
2813ca6549afSSteven Whitehouse 	skb->protocol = protocol;
28141da177e4SLinus Torvalds 	skb->dev = odev;
28151da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
2816c26bf4a5SThomas Graf 
2817c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2818c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2819c26bf4a5SThomas Graf 	} else if (odev->features & NETIF_F_V4_CSUM) {
2820c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2821c26bf4a5SThomas Graf 		skb->csum = 0;
2822c26bf4a5SThomas Graf 		udp4_hwcsum(skb, udph->source, udph->dest);
2823c26bf4a5SThomas Graf 	} else {
2824c26bf4a5SThomas Graf 		__wsum csum = udp_csum(skb);
2825c26bf4a5SThomas Graf 
2826c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
2827c26bf4a5SThomas Graf 		udph->check = csum_tcpudp_magic(udph->source, udph->dest,
2828c26bf4a5SThomas Graf 						datalen + 8, IPPROTO_UDP, csum);
2829c26bf4a5SThomas Graf 
2830c26bf4a5SThomas Graf 		if (udph->check == 0)
2831c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2832c26bf4a5SThomas Graf 	}
2833c26bf4a5SThomas Graf 
283426ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
28351da177e4SLinus Torvalds 
2836a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2837a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2838a553e4a6SJamal Hadi Salim 		return NULL;
2839a553e4a6SJamal Hadi Salim #endif
2840a553e4a6SJamal Hadi Salim 
28411da177e4SLinus Torvalds 	return skb;
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
28451da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
28461da177e4SLinus Torvalds {
28471da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
28481da177e4SLinus Torvalds 	__u8 *eth;
28491da177e4SLinus Torvalds 	struct udphdr *udph;
2850c26bf4a5SThomas Graf 	int datalen, udplen;
28511da177e4SLinus Torvalds 	struct ipv6hdr *iph;
2852d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2853ca6549afSSteven Whitehouse 	__be32 *mpls;
285434954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
285534954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
285634954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
285734954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2858fd2ea0a7SDavid S. Miller 	u16 queue_map;
2859ca6549afSSteven Whitehouse 
2860ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2861d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
28621da177e4SLinus Torvalds 
286334954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2864d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
286534954ddcSFrancesco Fondelli 
286664053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
286764053beeSRobert Olsson 	 * fields.
286864053beeSRobert Olsson 	 */
286964053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2870eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
287164053beeSRobert Olsson 
28727a6e288dSDaniel Borkmann 	skb = pktgen_alloc_skb(odev, pkt_dev, 16);
28731da177e4SLinus Torvalds 	if (!skb) {
28741da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
28751da177e4SLinus Torvalds 		return NULL;
28761da177e4SLinus Torvalds 	}
28771da177e4SLinus Torvalds 
28787a6e288dSDaniel Borkmann 	prefetchw(skb->data);
28791da177e4SLinus Torvalds 	skb_reserve(skb, 16);
28801da177e4SLinus Torvalds 
28811da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
28821da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2883ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2884ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2885ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
288634954ddcSFrancesco Fondelli 
288734954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
288834954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
288934954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28900f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
28910f37c605SAl Viro 					       pkt_dev->svlan_cfi,
28920f37c605SAl Viro 					       pkt_dev->svlan_p);
289334954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2894d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
289534954ddcSFrancesco Fondelli 		}
289634954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
28970f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
28980f37c605SAl Viro 				      pkt_dev->vlan_cfi,
28990f37c605SAl Viro 				      pkt_dev->vlan_p);
290034954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2901d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
290234954ddcSFrancesco Fondelli 	}
290334954ddcSFrancesco Fondelli 
2904525cebedSThomas Graf 	skb_set_mac_header(skb, 0);
2905525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2906525cebedSThomas Graf 	iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2907525cebedSThomas Graf 
2908525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
2909525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
2910fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
29119e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
29121da177e4SLinus Torvalds 
29131da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2914252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
29151da177e4SLinus Torvalds 
2916ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2917ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
2918ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
291916dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
29201da177e4SLinus Torvalds 
29215aa8b572SAmerigo Wang 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
29221da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
2923e87cc472SJoe Perches 		net_info_ratelimited("increased datalen to %d\n", datalen);
29241da177e4SLinus Torvalds 	}
29251da177e4SLinus Torvalds 
2926c26bf4a5SThomas Graf 	udplen = datalen + sizeof(struct udphdr);
29271da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
29281da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
2929c26bf4a5SThomas Graf 	udph->len = htons(udplen);
2930c26bf4a5SThomas Graf 	udph->check = 0;
29311da177e4SLinus Torvalds 
2932d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
29331da177e4SLinus Torvalds 
29341ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
29351ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
2936252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
29371ca7768cSFrancesco Fondelli 	}
29381ca7768cSFrancesco Fondelli 
29391da177e4SLinus Torvalds 	iph->hop_limit = 32;
29401da177e4SLinus Torvalds 
2941c26bf4a5SThomas Graf 	iph->payload_len = htons(udplen);
29421da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
29431da177e4SLinus Torvalds 
29444e3fd7a0SAlexey Dobriyan 	iph->daddr = pkt_dev->cur_in6_daddr;
29454e3fd7a0SAlexey Dobriyan 	iph->saddr = pkt_dev->cur_in6_saddr;
29461da177e4SLinus Torvalds 
2947ca6549afSSteven Whitehouse 	skb->protocol = protocol;
29481da177e4SLinus Torvalds 	skb->dev = odev;
29491da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
29501da177e4SLinus Torvalds 
2951c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2952c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2953c26bf4a5SThomas Graf 	} else if (odev->features & NETIF_F_V6_CSUM) {
2954c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2955c26bf4a5SThomas Graf 		skb->csum_start = skb_transport_header(skb) - skb->head;
2956c26bf4a5SThomas Graf 		skb->csum_offset = offsetof(struct udphdr, check);
2957c26bf4a5SThomas Graf 		udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2958c26bf4a5SThomas Graf 	} else {
2959c26bf4a5SThomas Graf 		__wsum csum = udp_csum(skb);
2960c26bf4a5SThomas Graf 
2961c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
2962c26bf4a5SThomas Graf 		udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2963c26bf4a5SThomas Graf 
2964c26bf4a5SThomas Graf 		if (udph->check == 0)
2965c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2966c26bf4a5SThomas Graf 	}
2967c26bf4a5SThomas Graf 
296826ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
29691da177e4SLinus Torvalds 
29701da177e4SLinus Torvalds 	return skb;
29711da177e4SLinus Torvalds }
29721da177e4SLinus Torvalds 
2973475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
29741da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
29751da177e4SLinus Torvalds {
29761da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
29771da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
29781da177e4SLinus Torvalds 	else
29791da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
29801da177e4SLinus Torvalds }
29811da177e4SLinus Torvalds 
29821da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
29831da177e4SLinus Torvalds {
29841da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
29851da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
29861da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
29871da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
29881da177e4SLinus Torvalds 	pkt_dev->errors = 0;
29891da177e4SLinus Torvalds }
29901da177e4SLinus Torvalds 
29911da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
29921da177e4SLinus Torvalds 
29931da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
29941da177e4SLinus Torvalds {
2995c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
29961da177e4SLinus Torvalds 	int started = 0;
29971da177e4SLinus Torvalds 
2998f9467eaeSJoe Perches 	func_enter();
29991da177e4SLinus Torvalds 
30001da177e4SLinus Torvalds 	if_lock(t);
3001c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 		/*
30041da177e4SLinus Torvalds 		 * setup odev and create initial packet.
30051da177e4SLinus Torvalds 		 */
30061da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
30071da177e4SLinus Torvalds 
30081da177e4SLinus Torvalds 		if (pkt_dev->odev) {
30091da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
30101da177e4SLinus Torvalds 			pkt_dev->running = 1;	/* Cranke yeself! */
30111da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
3012398f382cSDaniel Borkmann 			pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3013fd29cf72SStephen Hemminger 
301416dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
30171da177e4SLinus Torvalds 			started++;
3018222f1806SLuiz Capitulino 		} else
30191da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
30201da177e4SLinus Torvalds 	}
30211da177e4SLinus Torvalds 	if_unlock(t);
3022222f1806SLuiz Capitulino 	if (started)
3023222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
30241da177e4SLinus Torvalds }
30251da177e4SLinus Torvalds 
30264e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
30271da177e4SLinus Torvalds {
3028cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30291da177e4SLinus Torvalds 
3030f9467eaeSJoe Perches 	func_enter();
30311da177e4SLinus Torvalds 
30326146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3033cdcdbe0bSLuiz Capitulino 
30344e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
303595ed63f7SArthur Kepner 		t->control |= T_STOP;
3036cdcdbe0bSLuiz Capitulino 
30376146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
30381da177e4SLinus Torvalds }
30391da177e4SLinus Torvalds 
3040648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
30411da177e4SLinus Torvalds {
3042648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
30431da177e4SLinus Torvalds 
3044c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
3045648fda74SStephen Hemminger 		if (pkt_dev->running)
3046648fda74SStephen Hemminger 			return 1;
3047648fda74SStephen Hemminger 	return 0;
30481da177e4SLinus Torvalds }
30491da177e4SLinus Torvalds 
30501da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
30511da177e4SLinus Torvalds {
30521da177e4SLinus Torvalds 	if_lock(t);
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds 	while (thread_is_running(t)) {
30551da177e4SLinus Torvalds 
30561da177e4SLinus Torvalds 		if_unlock(t);
30571da177e4SLinus Torvalds 
30581da177e4SLinus Torvalds 		msleep_interruptible(100);
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds 		if (signal_pending(current))
30611da177e4SLinus Torvalds 			goto signal;
30621da177e4SLinus Torvalds 		if_lock(t);
30631da177e4SLinus Torvalds 	}
30641da177e4SLinus Torvalds 	if_unlock(t);
30651da177e4SLinus Torvalds 	return 1;
30661da177e4SLinus Torvalds signal:
30671da177e4SLinus Torvalds 	return 0;
30681da177e4SLinus Torvalds }
30691da177e4SLinus Torvalds 
30704e58a027SCong Wang static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
30711da177e4SLinus Torvalds {
3072cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30731da177e4SLinus Torvalds 	int sig = 1;
30741da177e4SLinus Torvalds 
30756146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3076cdcdbe0bSLuiz Capitulino 
30774e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
30781da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
3079222f1806SLuiz Capitulino 		if (sig == 0)
3080222f1806SLuiz Capitulino 			break;
30811da177e4SLinus Torvalds 	}
3082cdcdbe0bSLuiz Capitulino 
3083cdcdbe0bSLuiz Capitulino 	if (sig == 0)
30844e58a027SCong Wang 		list_for_each_entry(t, &pn->pktgen_threads, th_list)
30851da177e4SLinus Torvalds 			t->control |= (T_STOP);
3086cdcdbe0bSLuiz Capitulino 
30876146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
30881da177e4SLinus Torvalds 	return sig;
30891da177e4SLinus Torvalds }
30901da177e4SLinus Torvalds 
30914e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn)
30921da177e4SLinus Torvalds {
3093cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30941da177e4SLinus Torvalds 
3095f9467eaeSJoe Perches 	func_enter();
30961da177e4SLinus Torvalds 
30976146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
30981da177e4SLinus Torvalds 
30994e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
31001da177e4SLinus Torvalds 		t->control |= (T_RUN);
3101cdcdbe0bSLuiz Capitulino 
31026146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31031da177e4SLinus Torvalds 
310463adc6fbSStephen Hemminger 	/* Propagate thread->control  */
310563adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
31061da177e4SLinus Torvalds 
31074e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
31081da177e4SLinus Torvalds }
31091da177e4SLinus Torvalds 
31104e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn)
3111eb37b41cSJesse Brandeburg {
3112eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3113eb37b41cSJesse Brandeburg 
3114f9467eaeSJoe Perches 	func_enter();
3115eb37b41cSJesse Brandeburg 
3116eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3117eb37b41cSJesse Brandeburg 
31184e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
3119eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3120eb37b41cSJesse Brandeburg 
3121eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3122eb37b41cSJesse Brandeburg 
312363adc6fbSStephen Hemminger 	/* Propagate thread->control  */
312463adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3125eb37b41cSJesse Brandeburg 
31264e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
3127eb37b41cSJesse Brandeburg }
3128eb37b41cSJesse Brandeburg 
31291da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
31301da177e4SLinus Torvalds {
3131fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
31321da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3133fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3134fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3135fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
31361da177e4SLinus Torvalds 
313703a14ab1SDaniel Turull 	p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3138fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3139fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3140fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
31411da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
31421da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
31431da177e4SLinus Torvalds 
3144fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3145fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
31481da177e4SLinus Torvalds 
31491da177e4SLinus Torvalds 	mbps = bps;
31501da177e4SLinus Torvalds 	do_div(mbps, 1000000);
31511da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
31521da177e4SLinus Torvalds 		     (unsigned long long)pps,
31531da177e4SLinus Torvalds 		     (unsigned long long)mbps,
31541da177e4SLinus Torvalds 		     (unsigned long long)bps,
31551da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
31561da177e4SLinus Torvalds }
31571da177e4SLinus Torvalds 
31581da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
31591da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
31601da177e4SLinus Torvalds {
3161222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
31621da177e4SLinus Torvalds 
31631da177e4SLinus Torvalds 	if (!pkt_dev->running) {
3164f9467eaeSJoe Perches 		pr_warning("interface: %s is already stopped\n",
3165f9467eaeSJoe Perches 			   pkt_dev->odevname);
31661da177e4SLinus Torvalds 		return -EINVAL;
31671da177e4SLinus Torvalds 	}
31681da177e4SLinus Torvalds 
31693bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
31703bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3171398f382cSDaniel Borkmann 	pkt_dev->stopped_at = ktime_get();
31721da177e4SLinus Torvalds 	pkt_dev->running = 0;
31731da177e4SLinus Torvalds 
317495ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
31751da177e4SLinus Torvalds 
31761da177e4SLinus Torvalds 	return 0;
31771da177e4SLinus Torvalds }
31781da177e4SLinus Torvalds 
31791da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
31801da177e4SLinus Torvalds {
3181c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
31821da177e4SLinus Torvalds 
31831da177e4SLinus Torvalds 	if_lock(t);
31841da177e4SLinus Torvalds 
3185c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3186c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3187222f1806SLuiz Capitulino 			continue;
3188222f1806SLuiz Capitulino 		if (best == NULL)
3189c26a8016SLuiz Capitulino 			best = pkt_dev;
3190398f382cSDaniel Borkmann 		else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3191c26a8016SLuiz Capitulino 			best = pkt_dev;
31921da177e4SLinus Torvalds 	}
31931da177e4SLinus Torvalds 	if_unlock(t);
31941da177e4SLinus Torvalds 	return best;
31951da177e4SLinus Torvalds }
31961da177e4SLinus Torvalds 
3197222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3198222f1806SLuiz Capitulino {
3199c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
32001da177e4SLinus Torvalds 
3201f9467eaeSJoe Perches 	func_enter();
32021da177e4SLinus Torvalds 
32031da177e4SLinus Torvalds 	if_lock(t);
32041da177e4SLinus Torvalds 
3205c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3206c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
320795ed63f7SArthur Kepner 	}
320895ed63f7SArthur Kepner 
320995ed63f7SArthur Kepner 	if_unlock(t);
321095ed63f7SArthur Kepner }
321195ed63f7SArthur Kepner 
321295ed63f7SArthur Kepner /*
321395ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
321495ed63f7SArthur Kepner  * and remove it
321595ed63f7SArthur Kepner  */
321695ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
321795ed63f7SArthur Kepner {
3218c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3219c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
322095ed63f7SArthur Kepner 
3221f9467eaeSJoe Perches 	func_enter();
322295ed63f7SArthur Kepner 
322395ed63f7SArthur Kepner 	if_lock(t);
322495ed63f7SArthur Kepner 
3225c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3226c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
322795ed63f7SArthur Kepner 
3228222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3229222f1806SLuiz Capitulino 			continue;
323095ed63f7SArthur Kepner 
323195ed63f7SArthur Kepner 		kfree_skb(cur->skb);
323295ed63f7SArthur Kepner 		cur->skb = NULL;
323395ed63f7SArthur Kepner 
323495ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
323595ed63f7SArthur Kepner 
323695ed63f7SArthur Kepner 		break;
323795ed63f7SArthur Kepner 	}
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds 	if_unlock(t);
32401da177e4SLinus Torvalds }
32411da177e4SLinus Torvalds 
32421da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
32431da177e4SLinus Torvalds {
3244c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3245c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
32461da177e4SLinus Torvalds 
3247f9467eaeSJoe Perches 	func_enter();
3248f9467eaeSJoe Perches 
32491da177e4SLinus Torvalds 	/* Remove all devices, free mem */
32501da177e4SLinus Torvalds 
32511da177e4SLinus Torvalds 	if_lock(t);
32521da177e4SLinus Torvalds 
3253c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3254c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
325595ed63f7SArthur Kepner 
325695ed63f7SArthur Kepner 		kfree_skb(cur->skb);
325795ed63f7SArthur Kepner 		cur->skb = NULL;
325895ed63f7SArthur Kepner 
32591da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
32601da177e4SLinus Torvalds 	}
32611da177e4SLinus Torvalds 
32621da177e4SLinus Torvalds 	if_unlock(t);
32631da177e4SLinus Torvalds }
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
32661da177e4SLinus Torvalds {
32671da177e4SLinus Torvalds 	/* Remove from the thread list */
32684e58a027SCong Wang 	remove_proc_entry(t->tsk->comm, t->net->proc_dir);
32691da177e4SLinus Torvalds }
32701da177e4SLinus Torvalds 
3271ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
32723791decbSStephen Hemminger {
3273398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
32743791decbSStephen Hemminger 	schedule();
3275398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
32763791decbSStephen Hemminger }
32773791decbSStephen Hemminger 
3278ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3279ef87979cSStephen Hemminger {
3280398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
3281ef87979cSStephen Hemminger 
3282ef87979cSStephen Hemminger 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3283ef87979cSStephen Hemminger 		if (signal_pending(current))
3284ef87979cSStephen Hemminger 			break;
3285ef87979cSStephen Hemminger 
3286ef87979cSStephen Hemminger 		if (need_resched())
3287ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3288ef87979cSStephen Hemminger 		else
3289ef87979cSStephen Hemminger 			cpu_relax();
3290ef87979cSStephen Hemminger 	}
3291398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3292ef87979cSStephen Hemminger }
3293fd29cf72SStephen Hemminger 
3294475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
32951da177e4SLinus Torvalds {
329600829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
32976fef4c0cSStephen Hemminger 	netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
329800829823SStephen Hemminger 		= odev->netdev_ops->ndo_start_xmit;
3299fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
3300fd2ea0a7SDavid S. Miller 	u16 queue_map;
33011da177e4SLinus Torvalds 	int ret;
33021da177e4SLinus Torvalds 
3303ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3304ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
33051da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
33063791decbSStephen Hemminger 		return;
33071da177e4SLinus Torvalds 	}
33081da177e4SLinus Torvalds 
3309ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3310ef87979cSStephen Hemminger 	 * "never transmit"
3311ef87979cSStephen Hemminger 	 */
3312ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3313398f382cSDaniel Borkmann 		pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3314ef87979cSStephen Hemminger 		return;
3315ef87979cSStephen Hemminger 	}
3316ef87979cSStephen Hemminger 
3317ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
33187d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
33197d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
33201da177e4SLinus Torvalds 		/* build a new pkt */
33211da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
33221da177e4SLinus Torvalds 
33231da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
33241da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
3325f9467eaeSJoe Perches 			pr_err("ERROR: couldn't allocate skb in fill_packet\n");
33261da177e4SLinus Torvalds 			schedule();
33271da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
33283791decbSStephen Hemminger 			return;
33291da177e4SLinus Torvalds 		}
3330baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
33311da177e4SLinus Torvalds 		pkt_dev->allocated_skbs++;
33321da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
33331da177e4SLinus Torvalds 	}
33341da177e4SLinus Torvalds 
3335ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3336ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3337ef87979cSStephen Hemminger 
3338fd2ea0a7SDavid S. Miller 	queue_map = skb_get_queue_mapping(pkt_dev->skb);
3339fd2ea0a7SDavid S. Miller 	txq = netdev_get_tx_queue(odev, queue_map);
3340fd2ea0a7SDavid S. Miller 
33410f2eea4bSDaniel Borkmann 	local_bh_disable();
33420f2eea4bSDaniel Borkmann 
33430f2eea4bSDaniel Borkmann 	HARD_TX_LOCK(odev, txq, smp_processor_id());
33445b8db2f5SStephen Hemminger 
33456f25cd47SDaniel Borkmann 	if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3346ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
33470835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
33480835acfeSEric Dumazet 		goto unlock;
33490835acfeSEric Dumazet 	}
33500835acfeSEric Dumazet 	atomic_inc(&(pkt_dev->skb->users));
335100829823SStephen Hemminger 	ret = (*xmit)(pkt_dev->skb, odev);
3352ef87979cSStephen Hemminger 
33535b8db2f5SStephen Hemminger 	switch (ret) {
33545b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
335508baf561SEric Dumazet 		txq_trans_update(txq);
33561da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
33571da177e4SLinus Torvalds 		pkt_dev->sofar++;
33581da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3359baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
33605b8db2f5SStephen Hemminger 		break;
3361f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3362f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3363f466dba1SJohn Fastabend 	case NET_XMIT_POLICED:
3364f466dba1SJohn Fastabend 		/* skb has been consumed */
3365f466dba1SJohn Fastabend 		pkt_dev->errors++;
3366f466dba1SJohn Fastabend 		break;
33675b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
3368e87cc472SJoe Perches 		net_info_ratelimited("%s xmit error: %d\n",
3369e87cc472SJoe Perches 				     pkt_dev->odevname, ret);
33701da177e4SLinus Torvalds 		pkt_dev->errors++;
33715b8db2f5SStephen Hemminger 		/* fallthru */
3372ef87979cSStephen Hemminger 	case NETDEV_TX_LOCKED:
33735b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
33745b8db2f5SStephen Hemminger 		/* Retry it next time */
33755b8db2f5SStephen Hemminger 		atomic_dec(&(pkt_dev->skb->users));
33761da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
33771da177e4SLinus Torvalds 	}
33780835acfeSEric Dumazet unlock:
33790f2eea4bSDaniel Borkmann 	HARD_TX_UNLOCK(odev, txq);
33800f2eea4bSDaniel Borkmann 
33810f2eea4bSDaniel Borkmann 	local_bh_enable();
33821da177e4SLinus Torvalds 
33831da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
33841da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3385ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds 		/* Done with this */
33881da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
33891da177e4SLinus Torvalds 	}
33901da177e4SLinus Torvalds }
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds /*
33931da177e4SLinus Torvalds  * Main loop of the thread goes here
33941da177e4SLinus Torvalds  */
33951da177e4SLinus Torvalds 
3396ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
33971da177e4SLinus Torvalds {
33981da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3399ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
34001da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
34011da177e4SLinus Torvalds 	int cpu = t->cpu;
34021da177e4SLinus Torvalds 
3403ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
34041da177e4SLinus Torvalds 
34051da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3406d3ede327SDenis V. Lunev 	complete(&t->start_done);
34071da177e4SLinus Torvalds 
3408f9467eaeSJoe Perches 	pr_debug("starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
34091da177e4SLinus Torvalds 
341083144186SRafael J. Wysocki 	set_freezable();
341183144186SRafael J. Wysocki 
3412*baac167bSJesper Dangaard Brouer 	__set_current_state(TASK_RUNNING);
3413*baac167bSJesper Dangaard Brouer 
3414ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3415ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3416ee74baa7SDavid S. Miller 
3417ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
34184e58a027SCong Wang 			if (t->net->pktgen_exiting)
3419551eaff1SEric Dumazet 				break;
3420ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3421ef87979cSStephen Hemminger 							 t->control != 0,
3422ef87979cSStephen Hemminger 							 HZ/10);
34231b3f720bSRafael J. Wysocki 			try_to_freeze();
3424ef87979cSStephen Hemminger 			continue;
3425ee74baa7SDavid S. Miller 		}
34261da177e4SLinus Torvalds 
3427ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
34281da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
34291da177e4SLinus Torvalds 
3430ef87979cSStephen Hemminger 			if (need_resched())
3431ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3432ef87979cSStephen Hemminger 			else
3433ef87979cSStephen Hemminger 				cpu_relax();
3434ef87979cSStephen Hemminger 		}
3435ef87979cSStephen Hemminger 
34361da177e4SLinus Torvalds 		if (t->control & T_STOP) {
34371da177e4SLinus Torvalds 			pktgen_stop(t);
34381da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
34391da177e4SLinus Torvalds 		}
34401da177e4SLinus Torvalds 
34411da177e4SLinus Torvalds 		if (t->control & T_RUN) {
34421da177e4SLinus Torvalds 			pktgen_run(t);
34431da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
34441da177e4SLinus Torvalds 		}
34451da177e4SLinus Torvalds 
344695ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
34471da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
344895ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
344995ed63f7SArthur Kepner 		}
345095ed63f7SArthur Kepner 
345195ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
345295ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
34531da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
34541da177e4SLinus Torvalds 		}
34551da177e4SLinus Torvalds 
345609fe3ef4SAndrew Morton 		try_to_freeze();
34571da177e4SLinus Torvalds 	}
3458*baac167bSJesper Dangaard Brouer 	set_current_state(TASK_INTERRUPTIBLE);
34591da177e4SLinus Torvalds 
3460f9467eaeSJoe Perches 	pr_debug("%s stopping all device\n", t->tsk->comm);
34611da177e4SLinus Torvalds 	pktgen_stop(t);
34621da177e4SLinus Torvalds 
3463f9467eaeSJoe Perches 	pr_debug("%s removing all device\n", t->tsk->comm);
34641da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
34651da177e4SLinus Torvalds 
3466f9467eaeSJoe Perches 	pr_debug("%s removing thread\n", t->tsk->comm);
34671da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3468cdcdbe0bSLuiz Capitulino 
3469551eaff1SEric Dumazet 	/* Wait for kthread_stop */
3470551eaff1SEric Dumazet 	while (!kthread_should_stop()) {
3471551eaff1SEric Dumazet 		set_current_state(TASK_INTERRUPTIBLE);
3472551eaff1SEric Dumazet 		schedule();
3473551eaff1SEric Dumazet 	}
3474551eaff1SEric Dumazet 	__set_current_state(TASK_RUNNING);
3475551eaff1SEric Dumazet 
3476ee74baa7SDavid S. Miller 	return 0;
34771da177e4SLinus Torvalds }
34781da177e4SLinus Torvalds 
3479222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
34803e984840SEric Dumazet 					  const char *ifname, bool exact)
34811da177e4SLinus Torvalds {
3482c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
34833e984840SEric Dumazet 	size_t len = strlen(ifname);
34841da177e4SLinus Torvalds 
34853e984840SEric Dumazet 	if_lock(t);
3486c26a8016SLuiz Capitulino 	list_for_each_entry(p, &t->if_list, list)
34873e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
34883e984840SEric Dumazet 			if (p->odevname[len]) {
34893e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
34903e984840SEric Dumazet 					continue;
34913e984840SEric Dumazet 			}
3492c26a8016SLuiz Capitulino 			pkt_dev = p;
34931da177e4SLinus Torvalds 			break;
34941da177e4SLinus Torvalds 		}
34951da177e4SLinus Torvalds 
34961da177e4SLinus Torvalds 	if_unlock(t);
3497f9467eaeSJoe Perches 	pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
34981da177e4SLinus Torvalds 	return pkt_dev;
34991da177e4SLinus Torvalds }
35001da177e4SLinus Torvalds 
35011da177e4SLinus Torvalds /*
35021da177e4SLinus Torvalds  * Adds a dev at front of if_list.
35031da177e4SLinus Torvalds  */
35041da177e4SLinus Torvalds 
3505222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3506222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
35071da177e4SLinus Torvalds {
35081da177e4SLinus Torvalds 	int rv = 0;
35091da177e4SLinus Torvalds 
35101da177e4SLinus Torvalds 	if_lock(t);
35111da177e4SLinus Torvalds 
35121da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
3513f9467eaeSJoe Perches 		pr_err("ERROR: already assigned to a thread\n");
35141da177e4SLinus Torvalds 		rv = -EBUSY;
35151da177e4SLinus Torvalds 		goto out;
35161da177e4SLinus Torvalds 	}
3517c26a8016SLuiz Capitulino 
3518c26a8016SLuiz Capitulino 	list_add(&pkt_dev->list, &t->if_list);
35191da177e4SLinus Torvalds 	pkt_dev->pg_thread = t;
35201da177e4SLinus Torvalds 	pkt_dev->running = 0;
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds out:
35231da177e4SLinus Torvalds 	if_unlock(t);
35241da177e4SLinus Torvalds 	return rv;
35251da177e4SLinus Torvalds }
35261da177e4SLinus Torvalds 
35271da177e4SLinus Torvalds /* Called under thread lock */
35281da177e4SLinus Torvalds 
35291da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
35301da177e4SLinus Torvalds {
35311da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
353239df232fSStephen Hemminger 	int err;
35333291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
35341da177e4SLinus Torvalds 
35351da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
35361da177e4SLinus Torvalds 
35374e58a027SCong Wang 	pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3538d50a6b56SStephen Hemminger 	if (pkt_dev) {
3539f9467eaeSJoe Perches 		pr_err("ERROR: interface already used\n");
3540d50a6b56SStephen Hemminger 		return -EBUSY;
3541d50a6b56SStephen Hemminger 	}
35421da177e4SLinus Torvalds 
35433291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
35441da177e4SLinus Torvalds 	if (!pkt_dev)
35451da177e4SLinus Torvalds 		return -ENOMEM;
35461da177e4SLinus Torvalds 
3547593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
354868d5ac2eSWANG Cong 	pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
35493291b9dbSEric Dumazet 				      node);
35501da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
35511da177e4SLinus Torvalds 		kfree(pkt_dev);
35521da177e4SLinus Torvalds 		return -ENOMEM;
35531da177e4SLinus Torvalds 	}
35541da177e4SLinus Torvalds 
355595ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
35561da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
3557fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
35581da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
35591da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
35601da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
35611da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
35621da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
35631da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
356434954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
356534954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
356634954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
356734954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
356834954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
356934954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
3570e99b99b4SRobert Olsson 	pkt_dev->node = -1;
357134954ddcSFrancesco Fondelli 
35724e58a027SCong Wang 	err = pktgen_setup_dev(t->net, pkt_dev, ifname);
357339df232fSStephen Hemminger 	if (err)
357439df232fSStephen Hemminger 		goto out1;
3575d8873315SNeil Horman 	if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3576d8873315SNeil Horman 		pkt_dev->clone_skb = pg_clone_skb_d;
35771da177e4SLinus Torvalds 
35784e58a027SCong Wang 	pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
35795efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
358039df232fSStephen Hemminger 	if (!pkt_dev->entry) {
3581f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3582d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
358339df232fSStephen Hemminger 		err = -EINVAL;
358439df232fSStephen Hemminger 		goto out2;
358539df232fSStephen Hemminger 	}
3586a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3587a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3588a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3589cf93d47eSFan Du 
3590cf93d47eSFan Du 	/* xfrm tunnel mode needs additional dst to extract outter
3591cf93d47eSFan Du 	 * ip header protocol/ttl/id field, here creat a phony one.
3592cf93d47eSFan Du 	 * instead of looking for a valid rt, which definitely hurting
3593cf93d47eSFan Du 	 * performance under such circumstance.
3594cf93d47eSFan Du 	 */
3595cf93d47eSFan Du 	pkt_dev->dstops.family = AF_INET;
3596cf93d47eSFan Du 	pkt_dev->dst.dev = pkt_dev->odev;
3597cf93d47eSFan Du 	dst_init_metrics(&pkt_dev->dst, pktgen_dst_metrics, false);
3598cf93d47eSFan Du 	pkt_dev->dst.child = &pkt_dev->dst;
3599cf93d47eSFan Du 	pkt_dev->dst.ops = &pkt_dev->dstops;
3600a553e4a6SJamal Hadi Salim #endif
360139df232fSStephen Hemminger 
360239df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
360339df232fSStephen Hemminger out2:
360439df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
360539df232fSStephen Hemminger out1:
3606a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3607a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3608a553e4a6SJamal Hadi Salim #endif
36091da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
36101da177e4SLinus Torvalds 	kfree(pkt_dev);
361139df232fSStephen Hemminger 	return err;
36121da177e4SLinus Torvalds }
36131da177e4SLinus Torvalds 
36144e58a027SCong Wang static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
36151da177e4SLinus Torvalds {
3616cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3617d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3618ee74baa7SDavid S. Miller 	struct task_struct *p;
36191da177e4SLinus Torvalds 
36203291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
36213291b9dbSEric Dumazet 			 cpu_to_node(cpu));
36221da177e4SLinus Torvalds 	if (!t) {
3623f9467eaeSJoe Perches 		pr_err("ERROR: out of memory, can't create new thread\n");
36241da177e4SLinus Torvalds 		return -ENOMEM;
36251da177e4SLinus Torvalds 	}
36261da177e4SLinus Torvalds 
36271da177e4SLinus Torvalds 	spin_lock_init(&t->if_lock);
36281da177e4SLinus Torvalds 	t->cpu = cpu;
36291da177e4SLinus Torvalds 
3630ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3631ee74baa7SDavid S. Miller 
36324e58a027SCong Wang 	list_add_tail(&t->th_list, &pn->pktgen_threads);
3633d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3634ee74baa7SDavid S. Miller 
363594dcf29aSEric Dumazet 	p = kthread_create_on_node(pktgen_thread_worker,
363694dcf29aSEric Dumazet 				   t,
363794dcf29aSEric Dumazet 				   cpu_to_node(cpu),
363894dcf29aSEric Dumazet 				   "kpktgend_%d", cpu);
3639ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
3640f9467eaeSJoe Perches 		pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3641ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3642ee74baa7SDavid S. Miller 		kfree(t);
3643ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3644ee74baa7SDavid S. Miller 	}
3645ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3646ee74baa7SDavid S. Miller 	t->tsk = p;
3647ee74baa7SDavid S. Miller 
36484e58a027SCong Wang 	pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
36495efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3650d50a6b56SStephen Hemminger 	if (!pe) {
3651f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3652ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3653ee74baa7SDavid S. Miller 		kthread_stop(p);
3654ee74baa7SDavid S. Miller 		list_del(&t->th_list);
36551da177e4SLinus Torvalds 		kfree(t);
36561da177e4SLinus Torvalds 		return -EINVAL;
36571da177e4SLinus Torvalds 	}
3658d50a6b56SStephen Hemminger 
36594e58a027SCong Wang 	t->net = pn;
3660ee74baa7SDavid S. Miller 	wake_up_process(p);
3661d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
36621da177e4SLinus Torvalds 
36631da177e4SLinus Torvalds 	return 0;
36641da177e4SLinus Torvalds }
36651da177e4SLinus Torvalds 
36661da177e4SLinus Torvalds /*
36671da177e4SLinus Torvalds  * Removes a device from the thread if_list.
36681da177e4SLinus Torvalds  */
3669222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3670222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
36711da177e4SLinus Torvalds {
3672c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3673c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
36741da177e4SLinus Torvalds 
3675c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3676c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3677c26a8016SLuiz Capitulino 		if (p == pkt_dev)
3678c26a8016SLuiz Capitulino 			list_del(&p->list);
36791da177e4SLinus Torvalds 	}
36801da177e4SLinus Torvalds }
36811da177e4SLinus Torvalds 
3682222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3683222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
36841da177e4SLinus Torvalds {
3685f9467eaeSJoe Perches 	pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
36861da177e4SLinus Torvalds 
36871da177e4SLinus Torvalds 	if (pkt_dev->running) {
3688f9467eaeSJoe Perches 		pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
36891da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
36901da177e4SLinus Torvalds 	}
36911da177e4SLinus Torvalds 
36921da177e4SLinus Torvalds 	/* Dis-associate from the interface */
36931da177e4SLinus Torvalds 
36941da177e4SLinus Torvalds 	if (pkt_dev->odev) {
36951da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
36961da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
36971da177e4SLinus Torvalds 	}
36981da177e4SLinus Torvalds 
36991da177e4SLinus Torvalds 	/* And update the thread if_list */
37001da177e4SLinus Torvalds 
37011da177e4SLinus Torvalds 	_rem_dev_from_if_list(t, pkt_dev);
37021da177e4SLinus Torvalds 
370339df232fSStephen Hemminger 	if (pkt_dev->entry)
3704a8ca16eaSDavid Howells 		proc_remove(pkt_dev->entry);
37051da177e4SLinus Torvalds 
3706a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3707a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3708a553e4a6SJamal Hadi Salim #endif
37091da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
371026ad7879SEric Dumazet 	if (pkt_dev->page)
371126ad7879SEric Dumazet 		put_page(pkt_dev->page);
37121da177e4SLinus Torvalds 	kfree(pkt_dev);
37131da177e4SLinus Torvalds 	return 0;
37141da177e4SLinus Torvalds }
37151da177e4SLinus Torvalds 
37164e58a027SCong Wang static int __net_init pg_net_init(struct net *net)
37171da177e4SLinus Torvalds {
37184e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3719d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
37204e58a027SCong Wang 	int cpu, ret = 0;
3721d50a6b56SStephen Hemminger 
37224e58a027SCong Wang 	pn->net = net;
37234e58a027SCong Wang 	INIT_LIST_HEAD(&pn->pktgen_threads);
37244e58a027SCong Wang 	pn->pktgen_exiting = false;
37254e58a027SCong Wang 	pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
37264e58a027SCong Wang 	if (!pn->proc_dir) {
37274e58a027SCong Wang 		pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3728d50a6b56SStephen Hemminger 		return -ENODEV;
37291da177e4SLinus Torvalds 	}
37304e58a027SCong Wang 	pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
37314e58a027SCong Wang 	if (pe == NULL) {
37324e58a027SCong Wang 		pr_err("cannot create %s procfs entry\n", PGCTRL);
37334e58a027SCong Wang 		ret = -EINVAL;
37344e58a027SCong Wang 		goto remove;
37354e58a027SCong Wang 	}
37361da177e4SLinus Torvalds 
3737670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
37388024bb24SLuiz Capitulino 		int err;
37391da177e4SLinus Torvalds 
37404e58a027SCong Wang 		err = pktgen_create_thread(cpu, pn);
37418024bb24SLuiz Capitulino 		if (err)
37424e58a027SCong Wang 			pr_warn("Cannot create thread for cpu %d (%d)\n",
3743f9467eaeSJoe Perches 				   cpu, err);
37441da177e4SLinus Torvalds 	}
37458024bb24SLuiz Capitulino 
37464e58a027SCong Wang 	if (list_empty(&pn->pktgen_threads)) {
37474e58a027SCong Wang 		pr_err("Initialization failed for all threads\n");
3748ce14f894SWANG Cong 		ret = -ENODEV;
37494e58a027SCong Wang 		goto remove_entry;
37508024bb24SLuiz Capitulino 	}
37518024bb24SLuiz Capitulino 
37521da177e4SLinus Torvalds 	return 0;
3753ce14f894SWANG Cong 
37544e58a027SCong Wang remove_entry:
37554e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
37564e58a027SCong Wang remove:
3757ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3758ce14f894SWANG Cong 	return ret;
37591da177e4SLinus Torvalds }
37601da177e4SLinus Torvalds 
37614e58a027SCong Wang static void __net_exit pg_net_exit(struct net *net)
37621da177e4SLinus Torvalds {
37634e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3764cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3765cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
3766d4b11335SEric Dumazet 	LIST_HEAD(list);
37671da177e4SLinus Torvalds 
37681da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
37694e58a027SCong Wang 	pn->pktgen_exiting = true;
37701da177e4SLinus Torvalds 
3771c57b5468SEric Dumazet 	mutex_lock(&pktgen_thread_lock);
37724e58a027SCong Wang 	list_splice_init(&pn->pktgen_threads, &list);
3773c57b5468SEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
3774c57b5468SEric Dumazet 
3775c57b5468SEric Dumazet 	list_for_each_safe(q, n, &list) {
3776cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3777c57b5468SEric Dumazet 		list_del(&t->th_list);
3778ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
3779ee74baa7SDavid S. Miller 		kfree(t);
37801da177e4SLinus Torvalds 	}
37811da177e4SLinus Torvalds 
37824e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
3783ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
37844e58a027SCong Wang }
37851da177e4SLinus Torvalds 
37864e58a027SCong Wang static struct pernet_operations pg_net_ops = {
37874e58a027SCong Wang 	.init = pg_net_init,
37884e58a027SCong Wang 	.exit = pg_net_exit,
37894e58a027SCong Wang 	.id   = &pg_net_id,
37904e58a027SCong Wang 	.size = sizeof(struct pktgen_net),
37914e58a027SCong Wang };
37924e58a027SCong Wang 
37934e58a027SCong Wang static int __init pg_init(void)
37944e58a027SCong Wang {
37954e58a027SCong Wang 	int ret = 0;
37964e58a027SCong Wang 
37974e58a027SCong Wang 	pr_info("%s", version);
37984e58a027SCong Wang 	ret = register_pernet_subsys(&pg_net_ops);
37994e58a027SCong Wang 	if (ret)
38004e58a027SCong Wang 		return ret;
38014e58a027SCong Wang 	ret = register_netdevice_notifier(&pktgen_notifier_block);
38024e58a027SCong Wang 	if (ret)
38034e58a027SCong Wang 		unregister_pernet_subsys(&pg_net_ops);
38044e58a027SCong Wang 
38054e58a027SCong Wang 	return ret;
38064e58a027SCong Wang }
38074e58a027SCong Wang 
38084e58a027SCong Wang static void __exit pg_cleanup(void)
38094e58a027SCong Wang {
38104e58a027SCong Wang 	unregister_netdevice_notifier(&pktgen_notifier_block);
38114e58a027SCong Wang 	unregister_pernet_subsys(&pg_net_ops);
38121da177e4SLinus Torvalds }
38131da177e4SLinus Torvalds 
38141da177e4SLinus Torvalds module_init(pg_init);
38151da177e4SLinus Torvalds module_exit(pg_cleanup);
38161da177e4SLinus Torvalds 
3817c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
38181da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
38191da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3820c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
38211da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3822c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
38231da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3824c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
38251da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3826c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
38271da177e4SLinus Torvalds module_param(debug, int, 0);
3828c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3829