xref: /openbmc/linux/net/core/pktgen.c (revision 38b2cf2982dc73d3f07fe84fec8cc4ed9f64c1c5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Authors:
31da177e4SLinus Torvalds  * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
41da177e4SLinus Torvalds  *                             Uppsala University and
51da177e4SLinus Torvalds  *                             Swedish University of Agricultural Sciences
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Alexey Kuznetsov  <kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  * Ben Greear <greearb@candelatech.com>
996de0e25SJan Engelhardt  * Jens Låås <jens.laas@data.slu.se>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * A tool for loading the network with preconfigurated packets.
181da177e4SLinus Torvalds  * The tool is implemented as a linux module.  Parameters are output
191da177e4SLinus Torvalds  * device, delay (to hard_xmit), number of packets, and whether
201da177e4SLinus Torvalds  * to use multiple SKBs or just the same one.
211da177e4SLinus Torvalds  * pktgen uses the installed interface's output routine.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * Additional hacking by:
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Jens.Laas@data.slu.se
261da177e4SLinus Torvalds  * Improved by ANK. 010120.
271da177e4SLinus Torvalds  * Improved by ANK even more. 010212.
281da177e4SLinus Torvalds  * MAC address typo fixed. 010417 --ro
291da177e4SLinus Torvalds  * Integrated.  020301 --DaveM
301da177e4SLinus Torvalds  * Added multiskb option 020301 --DaveM
311da177e4SLinus Torvalds  * Scaling of results. 020417--sigurdur@linpro.no
321da177e4SLinus Torvalds  * Significant re-work of the module:
331da177e4SLinus Torvalds  *   *  Convert to threaded model to more efficiently be able to transmit
341da177e4SLinus Torvalds  *       and receive on multiple interfaces at once.
351da177e4SLinus Torvalds  *   *  Converted many counters to __u64 to allow longer runs.
361da177e4SLinus Torvalds  *   *  Allow configuration of ranges, like min/max IP address, MACs,
371da177e4SLinus Torvalds  *       and UDP-ports, for both source and destination, and can
381da177e4SLinus Torvalds  *       set to use a random distribution or sequentially walk the range.
391da177e4SLinus Torvalds  *   *  Can now change most values after starting.
401da177e4SLinus Torvalds  *   *  Place 12-byte packet in UDP payload with magic number,
411da177e4SLinus Torvalds  *       sequence number, and timestamp.
421da177e4SLinus Torvalds  *   *  Add receiver code that detects dropped pkts, re-ordered pkts, and
431da177e4SLinus Torvalds  *       latencies (with micro-second) precision.
441da177e4SLinus Torvalds  *   *  Add IOCTL interface to easily get counters & configuration.
451da177e4SLinus Torvalds  *   --Ben Greear <greearb@candelatech.com>
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
481da177e4SLinus Torvalds  * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
491da177e4SLinus Torvalds  * as a "fastpath" with a configurable number of clones after alloc's.
501da177e4SLinus Torvalds  * clone_skb=0 means all packets are allocated this also means ranges time
511da177e4SLinus Torvalds  * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
521da177e4SLinus Torvalds  * clones.
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  * Also moved to /proc/net/pktgen/
551da177e4SLinus Torvalds  * --ro
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * Sept 10:  Fixed threading/locking.  Lots of bone-headed and more clever
581da177e4SLinus Torvalds  *    mistakes.  Also merged in DaveM's patch in the -pre6 patch.
591da177e4SLinus Torvalds  * --Ben Greear <greearb@candelatech.com>
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  *
641da177e4SLinus Torvalds  * 021124 Finished major redesign and rewrite for new functionality.
651da177e4SLinus Torvalds  * See Documentation/networking/pktgen.txt for how to use this.
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  * The new operation:
681da177e4SLinus Torvalds  * For each CPU one thread/process is created at start. This process checks
691da177e4SLinus Torvalds  * for running devices in the if_list and sends packets until count is 0 it
701da177e4SLinus Torvalds  * also the thread checks the thread->control which is used for inter-process
711da177e4SLinus Torvalds  * communication. controlling process "posts" operations to the threads this
728788370aSJesper Dangaard Brouer  * way.
738788370aSJesper Dangaard Brouer  * The if_list is RCU protected, and the if_lock remains to protect updating
748788370aSJesper Dangaard Brouer  * of if_list, from "add_device" as it invoked from userspace (via proc write).
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  * By design there should only be *one* "controlling" process. In practice
771da177e4SLinus Torvalds  * multiple write accesses gives unpredictable result. Understood by "write"
781da177e4SLinus Torvalds  * to /proc gives result code thats should be read be the "writer".
79b4099fabSStephen Hemminger  * For practical use this should be no problem.
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  * Note when adding devices to a specific CPU there good idea to also assign
821da177e4SLinus Torvalds  * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
831da177e4SLinus Torvalds  * --ro
841da177e4SLinus Torvalds  *
851da177e4SLinus Torvalds  * Fix refcount off by one if first packet fails, potential null deref,
861da177e4SLinus Torvalds  * memleak 030710- KJP
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * First "ranges" functionality for ipv6 030726 --ro
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * Included flow support. 030802 ANK.
911da177e4SLinus Torvalds  *
921da177e4SLinus Torvalds  * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
931da177e4SLinus Torvalds  *
941da177e4SLinus Torvalds  * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
951da177e4SLinus Torvalds  * ia64 compilation fix from  Aron Griffis <aron@hp.com> 040604
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  * New xmit() return, do_div and misc clean up by Stephen Hemminger
981da177e4SLinus Torvalds  * <shemminger@osdl.org> 040923
991da177e4SLinus Torvalds  *
100b4099fabSStephen Hemminger  * Randy Dunlap fixed u64 printk compiler waring
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  * Remove FCS from BW calculation.  Lennert Buytenhek <buytenh@wantstofly.org>
1031da177e4SLinus Torvalds  * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
1041da177e4SLinus Torvalds  *
1051da177e4SLinus Torvalds  * Corrections from Nikolai Malykh (nmalykh@bilim.com)
1061da177e4SLinus Torvalds  * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
1071da177e4SLinus Torvalds  *
1081da177e4SLinus Torvalds  * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
1091da177e4SLinus Torvalds  * 050103
110ca6549afSSteven Whitehouse  *
111ca6549afSSteven Whitehouse  * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112ca6549afSSteven Whitehouse  *
11334954ddcSFrancesco Fondelli  * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
11434954ddcSFrancesco Fondelli  *
115ce5d0b47SAdit Ranadive  * Fixed src_mac command to set source mac of packet to value specified in
116ce5d0b47SAdit Ranadive  * command by Adit Ranadive <adit.262@gmail.com>
117ce5d0b47SAdit Ranadive  *
1181da177e4SLinus Torvalds  */
119f9467eaeSJoe Perches 
120f9467eaeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
121f9467eaeSJoe Perches 
1221da177e4SLinus Torvalds #include <linux/sys.h>
1231da177e4SLinus Torvalds #include <linux/types.h>
1241da177e4SLinus Torvalds #include <linux/module.h>
1251da177e4SLinus Torvalds #include <linux/moduleparam.h>
1261da177e4SLinus Torvalds #include <linux/kernel.h>
127222fa076SLuiz Capitulino #include <linux/mutex.h>
1281da177e4SLinus Torvalds #include <linux/sched.h>
1291da177e4SLinus Torvalds #include <linux/slab.h>
1301da177e4SLinus Torvalds #include <linux/vmalloc.h>
1311da177e4SLinus Torvalds #include <linux/unistd.h>
1321da177e4SLinus Torvalds #include <linux/string.h>
1331da177e4SLinus Torvalds #include <linux/ptrace.h>
1341da177e4SLinus Torvalds #include <linux/errno.h>
1351da177e4SLinus Torvalds #include <linux/ioport.h>
1361da177e4SLinus Torvalds #include <linux/interrupt.h>
1374fc268d2SRandy Dunlap #include <linux/capability.h>
1382bc481cfSStephen Hemminger #include <linux/hrtimer.h>
13909fe3ef4SAndrew Morton #include <linux/freezer.h>
1401da177e4SLinus Torvalds #include <linux/delay.h>
1411da177e4SLinus Torvalds #include <linux/timer.h>
142cdcdbe0bSLuiz Capitulino #include <linux/list.h>
1431da177e4SLinus Torvalds #include <linux/init.h>
1441da177e4SLinus Torvalds #include <linux/skbuff.h>
1451da177e4SLinus Torvalds #include <linux/netdevice.h>
1461da177e4SLinus Torvalds #include <linux/inet.h>
1471da177e4SLinus Torvalds #include <linux/inetdevice.h>
1481da177e4SLinus Torvalds #include <linux/rtnetlink.h>
1491da177e4SLinus Torvalds #include <linux/if_arp.h>
15034954ddcSFrancesco Fondelli #include <linux/if_vlan.h>
1511da177e4SLinus Torvalds #include <linux/in.h>
1521da177e4SLinus Torvalds #include <linux/ip.h>
1531da177e4SLinus Torvalds #include <linux/ipv6.h>
1541da177e4SLinus Torvalds #include <linux/udp.h>
1551da177e4SLinus Torvalds #include <linux/proc_fs.h>
156d50a6b56SStephen Hemminger #include <linux/seq_file.h>
1571da177e4SLinus Torvalds #include <linux/wait.h>
158f404e9a6SKris Katterjohn #include <linux/etherdevice.h>
159ee74baa7SDavid S. Miller #include <linux/kthread.h>
160268bb0ceSLinus Torvalds #include <linux/prefetch.h>
161457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1621da177e4SLinus Torvalds #include <net/checksum.h>
1631da177e4SLinus Torvalds #include <net/ipv6.h>
164c26bf4a5SThomas Graf #include <net/udp.h>
16573d94e94SStephen Rothwell #include <net/ip6_checksum.h>
1661da177e4SLinus Torvalds #include <net/addrconf.h>
167a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
168a553e4a6SJamal Hadi Salim #include <net/xfrm.h>
169a553e4a6SJamal Hadi Salim #endif
1704e58a027SCong Wang #include <net/netns/generic.h>
1711da177e4SLinus Torvalds #include <asm/byteorder.h>
1721da177e4SLinus Torvalds #include <linux/rcupdate.h>
1731977f032SJiri Slaby #include <linux/bitops.h>
17463adc6fbSStephen Hemminger #include <linux/io.h>
17563adc6fbSStephen Hemminger #include <linux/timex.h>
17663adc6fbSStephen Hemminger #include <linux/uaccess.h>
1771da177e4SLinus Torvalds #include <asm/dma.h>
1781da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1791da177e4SLinus Torvalds 
18043d28b65SDaniel Turull #define VERSION	"2.74"
1811da177e4SLinus Torvalds #define IP_NAME_SZ 32
182ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
183d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1841da177e4SLinus Torvalds 
185f9467eaeSJoe Perches #define func_enter() pr_debug("entering %s\n", __func__);
186f9467eaeSJoe Perches 
1871da177e4SLinus Torvalds /* Device flag bits */
1881da177e4SLinus Torvalds #define F_IPSRC_RND   (1<<0)	/* IP-Src Random  */
1891da177e4SLinus Torvalds #define F_IPDST_RND   (1<<1)	/* IP-Dst Random  */
1901da177e4SLinus Torvalds #define F_UDPSRC_RND  (1<<2)	/* UDP-Src Random */
1911da177e4SLinus Torvalds #define F_UDPDST_RND  (1<<3)	/* UDP-Dst Random */
1921da177e4SLinus Torvalds #define F_MACSRC_RND  (1<<4)	/* MAC-Src Random */
1931da177e4SLinus Torvalds #define F_MACDST_RND  (1<<5)	/* MAC-Dst Random */
1941da177e4SLinus Torvalds #define F_TXSIZE_RND  (1<<6)	/* Transmit size is random */
1951da177e4SLinus Torvalds #define F_IPV6        (1<<7)	/* Interface in IPV6 Mode */
196ca6549afSSteven Whitehouse #define F_MPLS_RND    (1<<8)	/* Random MPLS labels */
19734954ddcSFrancesco Fondelli #define F_VID_RND     (1<<9)	/* Random VLAN ID */
19834954ddcSFrancesco Fondelli #define F_SVID_RND    (1<<10)	/* Random SVLAN ID */
199007a531bSJamal Hadi Salim #define F_FLOW_SEQ    (1<<11)	/* Sequential flows */
200a553e4a6SJamal Hadi Salim #define F_IPSEC_ON    (1<<12)	/* ipsec on for flows */
20145b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13)	/* queue map Random */
202e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14)	/* queue map mirrors smp_processor_id() */
203e99b99b4SRobert Olsson #define F_NODE          (1<<15)	/* Node memory alloc*/
204c26bf4a5SThomas Graf #define F_UDPCSUM       (1<<16)	/* Include UDP checksum */
205afb84b62SJesper Dangaard Brouer #define F_NO_TIMESTAMP  (1<<17)	/* Don't timestamp packets (default TS) */
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /* Thread control flag bits */
2086b80d6a6SStephen Hemminger #define T_STOP        (1<<0)	/* Stop run */
2096b80d6a6SStephen Hemminger #define T_RUN         (1<<1)	/* Start run */
2106b80d6a6SStephen Hemminger #define T_REMDEVALL   (1<<2)	/* Remove all devs */
2116b80d6a6SStephen Hemminger #define T_REMDEV      (1<<3)	/* Remove one dev */
2121da177e4SLinus Torvalds 
2138788370aSJesper Dangaard Brouer /* If lock -- protects updating of if_list */
2141da177e4SLinus Torvalds #define   if_lock(t)           spin_lock(&(t->if_lock));
2151da177e4SLinus Torvalds #define   if_unlock(t)           spin_unlock(&(t->if_lock));
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2181da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
219d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
220d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds #define MAX_CFLOWS  65536
2231da177e4SLinus Torvalds 
22434954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
22534954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
22634954ddcSFrancesco Fondelli 
227222f1806SLuiz Capitulino struct flow_state {
228252e3346SAl Viro 	__be32 cur_daddr;
2291da177e4SLinus Torvalds 	int count;
230a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
231a553e4a6SJamal Hadi Salim 	struct xfrm_state *x;
232a553e4a6SJamal Hadi Salim #endif
233007a531bSJamal Hadi Salim 	__u32 flags;
2341da177e4SLinus Torvalds };
2351da177e4SLinus Torvalds 
236007a531bSJamal Hadi Salim /* flow flag bits */
237007a531bSJamal Hadi Salim #define F_INIT   (1<<0)		/* flow has been initialized */
238007a531bSJamal Hadi Salim 
2391da177e4SLinus Torvalds struct pktgen_dev {
2401da177e4SLinus Torvalds 	/*
2411da177e4SLinus Torvalds 	 * Try to keep frequent/infrequent used vars. separated.
2421da177e4SLinus Torvalds 	 */
24339df232fSStephen Hemminger 	struct proc_dir_entry *entry;	/* proc file */
2441da177e4SLinus Torvalds 	struct pktgen_thread *pg_thread;/* the owner */
24563adc6fbSStephen Hemminger 	struct list_head list;		/* chaining in the thread's run-queue */
2468788370aSJesper Dangaard Brouer 	struct rcu_head	 rcu;		/* freed by RCU */
2471da177e4SLinus Torvalds 
24863adc6fbSStephen Hemminger 	int running;		/* if false, the test will stop */
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	/* If min != max, then we will either do a linear iteration, or
2511da177e4SLinus Torvalds 	 * we will do a random selection from within the range.
2521da177e4SLinus Torvalds 	 */
2531da177e4SLinus Torvalds 	__u32 flags;
25495ed63f7SArthur Kepner 	int removal_mark;	/* non-zero => the device is marked for
25595ed63f7SArthur Kepner 				 * removal by worker thread */
2561da177e4SLinus Torvalds 
25768bf9f0bSAmerigo Wang 	int min_pkt_size;
25868bf9f0bSAmerigo Wang 	int max_pkt_size;
25916dab72fSJamal Hadi Salim 	int pkt_overhead;	/* overhead for MPLS, VLANs, IPSEC etc */
2601da177e4SLinus Torvalds 	int nfrags;
26126ad7879SEric Dumazet 	struct page *page;
262fd29cf72SStephen Hemminger 	u64 delay;		/* nano-seconds */
263fd29cf72SStephen Hemminger 
2641da177e4SLinus Torvalds 	__u64 count;		/* Default No packets to send */
2651da177e4SLinus Torvalds 	__u64 sofar;		/* How many pkts we've sent so far */
2661da177e4SLinus Torvalds 	__u64 tx_bytes;		/* How many bytes we've transmitted */
267f466dba1SJohn Fastabend 	__u64 errors;		/* Errors when trying to transmit, */
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	/* runtime counters relating to clone_skb */
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	__u64 allocated_skbs;
2721da177e4SLinus Torvalds 	__u32 clone_count;
2731da177e4SLinus Torvalds 	int last_ok;		/* Was last skb sent?
27463adc6fbSStephen Hemminger 				 * Or a failed transmit of some sort?
27563adc6fbSStephen Hemminger 				 * This will keep sequence numbers in order
2761da177e4SLinus Torvalds 				 */
277fd29cf72SStephen Hemminger 	ktime_t next_tx;
278fd29cf72SStephen Hemminger 	ktime_t started_at;
279fd29cf72SStephen Hemminger 	ktime_t stopped_at;
280fd29cf72SStephen Hemminger 	u64	idle_acc;	/* nano-seconds */
281fd29cf72SStephen Hemminger 
2821da177e4SLinus Torvalds 	__u32 seq_num;
2831da177e4SLinus Torvalds 
28463adc6fbSStephen Hemminger 	int clone_skb;		/*
28563adc6fbSStephen Hemminger 				 * Use multiple SKBs during packet gen.
28663adc6fbSStephen Hemminger 				 * If this number is greater than 1, then
28763adc6fbSStephen Hemminger 				 * that many copies of the same packet will be
28863adc6fbSStephen Hemminger 				 * sent before a new packet is allocated.
28963adc6fbSStephen Hemminger 				 * If you want to send 1024 identical packets
29063adc6fbSStephen Hemminger 				 * before creating a new packet,
29163adc6fbSStephen Hemminger 				 * set clone_skb to 1024.
2921da177e4SLinus Torvalds 				 */
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	char dst_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2951da177e4SLinus Torvalds 	char dst_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2961da177e4SLinus Torvalds 	char src_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2971da177e4SLinus Torvalds 	char src_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	struct in6_addr in6_saddr;
3001da177e4SLinus Torvalds 	struct in6_addr in6_daddr;
3011da177e4SLinus Torvalds 	struct in6_addr cur_in6_daddr;
3021da177e4SLinus Torvalds 	struct in6_addr cur_in6_saddr;
3031da177e4SLinus Torvalds 	/* For ranges */
3041da177e4SLinus Torvalds 	struct in6_addr min_in6_daddr;
3051da177e4SLinus Torvalds 	struct in6_addr max_in6_daddr;
3061da177e4SLinus Torvalds 	struct in6_addr min_in6_saddr;
3071da177e4SLinus Torvalds 	struct in6_addr max_in6_saddr;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* If we're doing ranges, random or incremental, then this
3101da177e4SLinus Torvalds 	 * defines the min/max for those ranges.
3111da177e4SLinus Torvalds 	 */
312252e3346SAl Viro 	__be32 saddr_min;	/* inclusive, source IP address */
313252e3346SAl Viro 	__be32 saddr_max;	/* exclusive, source IP address */
314252e3346SAl Viro 	__be32 daddr_min;	/* inclusive, dest IP address */
315252e3346SAl Viro 	__be32 daddr_max;	/* exclusive, dest IP address */
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	__u16 udp_src_min;	/* inclusive, source UDP port */
3181da177e4SLinus Torvalds 	__u16 udp_src_max;	/* exclusive, source UDP port */
3191da177e4SLinus Torvalds 	__u16 udp_dst_min;	/* inclusive, dest UDP port */
3201da177e4SLinus Torvalds 	__u16 udp_dst_max;	/* exclusive, dest UDP port */
3211da177e4SLinus Torvalds 
3221ca7768cSFrancesco Fondelli 	/* DSCP + ECN */
32363adc6fbSStephen Hemminger 	__u8 tos;            /* six MSB of (former) IPv4 TOS
32463adc6fbSStephen Hemminger 				are for dscp codepoint */
32563adc6fbSStephen Hemminger 	__u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6
32663adc6fbSStephen Hemminger 				(see RFC 3260, sec. 4) */
3271ca7768cSFrancesco Fondelli 
328ca6549afSSteven Whitehouse 	/* MPLS */
32995c96174SEric Dumazet 	unsigned int nr_labels;	/* Depth of stack, 0 = no MPLS */
330ca6549afSSteven Whitehouse 	__be32 labels[MAX_MPLS_LABELS];
331ca6549afSSteven Whitehouse 
33234954ddcSFrancesco Fondelli 	/* VLAN/SVLAN (802.1Q/Q-in-Q) */
33334954ddcSFrancesco Fondelli 	__u8  vlan_p;
33434954ddcSFrancesco Fondelli 	__u8  vlan_cfi;
33534954ddcSFrancesco Fondelli 	__u16 vlan_id;  /* 0xffff means no vlan tag */
33634954ddcSFrancesco Fondelli 
33734954ddcSFrancesco Fondelli 	__u8  svlan_p;
33834954ddcSFrancesco Fondelli 	__u8  svlan_cfi;
33934954ddcSFrancesco Fondelli 	__u16 svlan_id; /* 0xffff means no svlan tag */
34034954ddcSFrancesco Fondelli 
3411da177e4SLinus Torvalds 	__u32 src_mac_count;	/* How many MACs to iterate through */
3421da177e4SLinus Torvalds 	__u32 dst_mac_count;	/* How many MACs to iterate through */
3431da177e4SLinus Torvalds 
344f404e9a6SKris Katterjohn 	unsigned char dst_mac[ETH_ALEN];
345f404e9a6SKris Katterjohn 	unsigned char src_mac[ETH_ALEN];
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	__u32 cur_dst_mac_offset;
3481da177e4SLinus Torvalds 	__u32 cur_src_mac_offset;
349252e3346SAl Viro 	__be32 cur_saddr;
350252e3346SAl Viro 	__be32 cur_daddr;
35166ed1e5eSEric Dumazet 	__u16 ip_id;
3521da177e4SLinus Torvalds 	__u16 cur_udp_dst;
3531da177e4SLinus Torvalds 	__u16 cur_udp_src;
35445b270f8SRobert Olsson 	__u16 cur_queue_map;
3551da177e4SLinus Torvalds 	__u32 cur_pkt_size;
356baac8564SEric Dumazet 	__u32 last_pkt_size;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	__u8 hh[14];
3591da177e4SLinus Torvalds 	/* = {
3601da177e4SLinus Torvalds 	   0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	   We fill in SRC address later
3631da177e4SLinus Torvalds 	   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3641da177e4SLinus Torvalds 	   0x08, 0x00
3651da177e4SLinus Torvalds 	   };
3661da177e4SLinus Torvalds 	 */
3671da177e4SLinus Torvalds 	__u16 pad;		/* pad out the hh struct to an even 16 bytes */
3681da177e4SLinus Torvalds 
36963adc6fbSStephen Hemminger 	struct sk_buff *skb;	/* skb we are to transmit next, used for when we
3701da177e4SLinus Torvalds 				 * are transmitting the same one multiple times
3711da177e4SLinus Torvalds 				 */
37263adc6fbSStephen Hemminger 	struct net_device *odev; /* The out-going device.
37363adc6fbSStephen Hemminger 				  * Note that the device should have it's
37463adc6fbSStephen Hemminger 				  * pg_info pointer pointing back to this
37563adc6fbSStephen Hemminger 				  * device.
37663adc6fbSStephen Hemminger 				  * Set when the user specifies the out-going
37763adc6fbSStephen Hemminger 				  * device name (not when the inject is
3781da177e4SLinus Torvalds 				  * started as it used to do.)
3791da177e4SLinus Torvalds 				  */
380593f63b0SEric Dumazet 	char odevname[32];
3811da177e4SLinus Torvalds 	struct flow_state *flows;
38295c96174SEric Dumazet 	unsigned int cflows;	/* Concurrent flows (config) */
38395c96174SEric Dumazet 	unsigned int lflow;		/* Flow length  (config) */
38495c96174SEric Dumazet 	unsigned int nflows;	/* accumulated flows (stats) */
38595c96174SEric Dumazet 	unsigned int curfl;		/* current sequenced flow (state)*/
38645b270f8SRobert Olsson 
38745b270f8SRobert Olsson 	u16 queue_map_min;
38845b270f8SRobert Olsson 	u16 queue_map_max;
3899e50e3acSJohn Fastabend 	__u32 skb_priority;	/* skb priority field */
390*38b2cf29SAlexei Starovoitov 	unsigned int burst;	/* number of duplicated packets to burst */
391e99b99b4SRobert Olsson 	int node;               /* Memory node */
39245b270f8SRobert Olsson 
393a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
394a553e4a6SJamal Hadi Salim 	__u8	ipsmode;		/* IPSEC mode (config) */
395a553e4a6SJamal Hadi Salim 	__u8	ipsproto;		/* IPSEC type (config) */
396de4aee7dSFan Du 	__u32	spi;
397cf93d47eSFan Du 	struct dst_entry dst;
398cf93d47eSFan Du 	struct dst_ops dstops;
399a553e4a6SJamal Hadi Salim #endif
40039df232fSStephen Hemminger 	char result[512];
4011da177e4SLinus Torvalds };
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds struct pktgen_hdr {
404252e3346SAl Viro 	__be32 pgh_magic;
405252e3346SAl Viro 	__be32 seq_num;
406252e3346SAl Viro 	__be32 tv_sec;
407252e3346SAl Viro 	__be32 tv_usec;
4081da177e4SLinus Torvalds };
4091da177e4SLinus Torvalds 
4104e58a027SCong Wang 
4114e58a027SCong Wang static int pg_net_id __read_mostly;
4124e58a027SCong Wang 
4134e58a027SCong Wang struct pktgen_net {
4144e58a027SCong Wang 	struct net		*net;
4154e58a027SCong Wang 	struct proc_dir_entry	*proc_dir;
4164e58a027SCong Wang 	struct list_head	pktgen_threads;
4174e58a027SCong Wang 	bool			pktgen_exiting;
4184e58a027SCong Wang };
419551eaff1SEric Dumazet 
4201da177e4SLinus Torvalds struct pktgen_thread {
42163adc6fbSStephen Hemminger 	spinlock_t if_lock;		/* for list of devices */
422c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
423cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
424ee74baa7SDavid S. Miller 	struct task_struct *tsk;
4251da177e4SLinus Torvalds 	char result[512];
4261da177e4SLinus Torvalds 
42763adc6fbSStephen Hemminger 	/* Field for thread to receive "posted" events terminate,
42863adc6fbSStephen Hemminger 	   stop ifs etc. */
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	u32 control;
4311da177e4SLinus Torvalds 	int cpu;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	wait_queue_head_t queue;
434d3ede327SDenis V. Lunev 	struct completion start_done;
4354e58a027SCong Wang 	struct pktgen_net *net;
4361da177e4SLinus Torvalds };
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds #define REMOVE 1
4391da177e4SLinus Torvalds #define FIND   0
4401da177e4SLinus Torvalds 
441c3d2f52dSStephen Hemminger static const char version[] =
442f9467eaeSJoe Perches 	"Packet Generator for packet performance testing. "
443f9467eaeSJoe Perches 	"Version: " VERSION "\n";
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4461da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
447222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
4483e984840SEric Dumazet 					  const char *ifname, bool exact);
4491da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
4504e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn);
4514e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn);
4524e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
4533bda06a3SStephen Hemminger 
4541da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4551da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
45639df232fSStephen Hemminger 
4571da177e4SLinus Torvalds /* Module parameters, defaults. */
45865c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
45965c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
46065c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
46165c5b786SStephen Hemminger static int debug  __read_mostly;
4621da177e4SLinus Torvalds 
463222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4661da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4671da177e4SLinus Torvalds };
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  * /proc handling functions
4711da177e4SLinus Torvalds  *
4721da177e4SLinus Torvalds  */
4731da177e4SLinus Torvalds 
474d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
475d50a6b56SStephen Hemminger {
476c3d2f52dSStephen Hemminger 	seq_puts(seq, version);
477d50a6b56SStephen Hemminger 	return 0;
478d50a6b56SStephen Hemminger }
4791da177e4SLinus Torvalds 
480d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user *buf,
4811da177e4SLinus Torvalds 			    size_t count, loff_t *ppos)
4821da177e4SLinus Torvalds {
483d50a6b56SStephen Hemminger 	char data[128];
4844e58a027SCong Wang 	struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
4851da177e4SLinus Torvalds 
48609455747SMathias Krause 	if (!capable(CAP_NET_ADMIN))
48709455747SMathias Krause 		return -EPERM;
4881da177e4SLinus Torvalds 
48920b0c718SMathias Krause 	if (count == 0)
49020b0c718SMathias Krause 		return -EINVAL;
49120b0c718SMathias Krause 
492d50a6b56SStephen Hemminger 	if (count > sizeof(data))
493d50a6b56SStephen Hemminger 		count = sizeof(data);
4941da177e4SLinus Torvalds 
49509455747SMathias Krause 	if (copy_from_user(data, buf, count))
49609455747SMathias Krause 		return -EFAULT;
49709455747SMathias Krause 
49820b0c718SMathias Krause 	data[count - 1] = 0;	/* Strip trailing '\n' and terminate string */
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
5014e58a027SCong Wang 		pktgen_stop_all_threads_ifs(pn);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
5044e58a027SCong Wang 		pktgen_run_all_threads(pn);
5051da177e4SLinus Torvalds 
506eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
5074e58a027SCong Wang 		pktgen_reset_all_threads(pn);
508eb37b41cSJesse Brandeburg 
5091da177e4SLinus Torvalds 	else
510294a0b7fSJoe Perches 		pr_warn("Unknown command: %s\n", data);
5111da177e4SLinus Torvalds 
51209455747SMathias Krause 	return count;
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
515d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
5161da177e4SLinus Torvalds {
517d9dda78bSAl Viro 	return single_open(file, pgctrl_show, PDE_DATA(inode));
518d50a6b56SStephen Hemminger }
519d50a6b56SStephen Hemminger 
5209a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
521d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
522d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
523d50a6b56SStephen Hemminger 	.read    = seq_read,
524d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
525d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
526d50a6b56SStephen Hemminger 	.release = single_release,
527d50a6b56SStephen Hemminger };
528d50a6b56SStephen Hemminger 
529d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
530d50a6b56SStephen Hemminger {
531648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
532fd29cf72SStephen Hemminger 	ktime_t stopped;
533fd29cf72SStephen Hemminger 	u64 idle;
5341da177e4SLinus Torvalds 
535222f1806SLuiz Capitulino 	seq_printf(seq,
536222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
537222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
538222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5391da177e4SLinus Torvalds 
540222f1806SLuiz Capitulino 	seq_printf(seq,
541fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
542fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
543593f63b0SEric Dumazet 		   pkt_dev->clone_skb, pkt_dev->odevname);
5441da177e4SLinus Torvalds 
545222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
546222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5471da177e4SLinus Torvalds 
54845b270f8SRobert Olsson 	seq_printf(seq,
54945b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
55045b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
55145b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
55245b270f8SRobert Olsson 
5539e50e3acSJohn Fastabend 	if (pkt_dev->skb_priority)
5549e50e3acSJohn Fastabend 		seq_printf(seq, "     skb_priority: %u\n",
5559e50e3acSJohn Fastabend 			   pkt_dev->skb_priority);
5569e50e3acSJohn Fastabend 
5571da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
558222f1806SLuiz Capitulino 		seq_printf(seq,
55947a0200dSAlexey Dobriyan 			   "     saddr: %pI6c  min_saddr: %pI6c  max_saddr: %pI6c\n"
56047a0200dSAlexey Dobriyan 			   "     daddr: %pI6c  min_daddr: %pI6c  max_daddr: %pI6c\n",
56147a0200dSAlexey Dobriyan 			   &pkt_dev->in6_saddr,
56247a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
56347a0200dSAlexey Dobriyan 			   &pkt_dev->in6_daddr,
56447a0200dSAlexey Dobriyan 			   &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
56563adc6fbSStephen Hemminger 	} else {
566222f1806SLuiz Capitulino 		seq_printf(seq,
56763adc6fbSStephen Hemminger 			   "     dst_min: %s  dst_max: %s\n",
56863adc6fbSStephen Hemminger 			   pkt_dev->dst_min, pkt_dev->dst_max);
56963adc6fbSStephen Hemminger 		seq_printf(seq,
57063adc6fbSStephen Hemminger 			   "        src_min: %s  src_max: %s\n",
57163adc6fbSStephen Hemminger 			   pkt_dev->src_min, pkt_dev->src_max);
57263adc6fbSStephen Hemminger 	}
5731da177e4SLinus Torvalds 
574d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5751da177e4SLinus Torvalds 
576e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
577e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
578e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5791da177e4SLinus Torvalds 
58097dc48e2SThomas Graf 	seq_puts(seq, "dst_mac: ");
581e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
5821da177e4SLinus Torvalds 
583222f1806SLuiz Capitulino 	seq_printf(seq,
58463adc6fbSStephen Hemminger 		   "     udp_src_min: %d  udp_src_max: %d"
58563adc6fbSStephen Hemminger 		   "  udp_dst_min: %d  udp_dst_max: %d\n",
586222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
587222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
5881da177e4SLinus Torvalds 
589222f1806SLuiz Capitulino 	seq_printf(seq,
590ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
5911da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
5921da177e4SLinus Torvalds 
593ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
59495c96174SEric Dumazet 		unsigned int i;
59597dc48e2SThomas Graf 		seq_puts(seq, "     mpls: ");
596ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
597ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
598ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
599ca6549afSSteven Whitehouse 	}
600ca6549afSSteven Whitehouse 
60163adc6fbSStephen Hemminger 	if (pkt_dev->vlan_id != 0xffff)
60234954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
60363adc6fbSStephen Hemminger 			   pkt_dev->vlan_id, pkt_dev->vlan_p,
60463adc6fbSStephen Hemminger 			   pkt_dev->vlan_cfi);
60534954ddcSFrancesco Fondelli 
60663adc6fbSStephen Hemminger 	if (pkt_dev->svlan_id != 0xffff)
60734954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
60863adc6fbSStephen Hemminger 			   pkt_dev->svlan_id, pkt_dev->svlan_p,
60963adc6fbSStephen Hemminger 			   pkt_dev->svlan_cfi);
61034954ddcSFrancesco Fondelli 
61163adc6fbSStephen Hemminger 	if (pkt_dev->tos)
6121ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
6131ca7768cSFrancesco Fondelli 
61463adc6fbSStephen Hemminger 	if (pkt_dev->traffic_class)
6151ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
6161ca7768cSFrancesco Fondelli 
617*38b2cf29SAlexei Starovoitov 	if (pkt_dev->burst > 1)
618*38b2cf29SAlexei Starovoitov 		seq_printf(seq, "     burst: %d\n", pkt_dev->burst);
619*38b2cf29SAlexei Starovoitov 
620e99b99b4SRobert Olsson 	if (pkt_dev->node >= 0)
621e99b99b4SRobert Olsson 		seq_printf(seq, "     node: %d\n", pkt_dev->node);
622e99b99b4SRobert Olsson 
62397dc48e2SThomas Graf 	seq_puts(seq, "     Flags: ");
624ca6549afSSteven Whitehouse 
6251da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
62697dc48e2SThomas Graf 		seq_puts(seq, "IPV6  ");
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPSRC_RND)
62997dc48e2SThomas Graf 		seq_puts(seq, "IPSRC_RND  ");
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPDST_RND)
63297dc48e2SThomas Graf 		seq_puts(seq, "IPDST_RND  ");
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	if (pkt_dev->flags & F_TXSIZE_RND)
63597dc48e2SThomas Graf 		seq_puts(seq, "TXSIZE_RND  ");
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPSRC_RND)
63897dc48e2SThomas Graf 		seq_puts(seq, "UDPSRC_RND  ");
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPDST_RND)
64197dc48e2SThomas Graf 		seq_puts(seq, "UDPDST_RND  ");
6421da177e4SLinus Torvalds 
643c26bf4a5SThomas Graf 	if (pkt_dev->flags & F_UDPCSUM)
64497dc48e2SThomas Graf 		seq_puts(seq, "UDPCSUM  ");
645c26bf4a5SThomas Graf 
646afb84b62SJesper Dangaard Brouer 	if (pkt_dev->flags & F_NO_TIMESTAMP)
647afb84b62SJesper Dangaard Brouer 		seq_puts(seq, "NO_TIMESTAMP  ");
648afb84b62SJesper Dangaard Brouer 
649ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND)
65097dc48e2SThomas Graf 		seq_puts(seq,  "MPLS_RND  ");
651ca6549afSSteven Whitehouse 
65245b270f8SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_RND)
65397dc48e2SThomas Graf 		seq_puts(seq,  "QUEUE_MAP_RND  ");
65445b270f8SRobert Olsson 
655e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
65697dc48e2SThomas Graf 		seq_puts(seq,  "QUEUE_MAP_CPU  ");
657e6fce5b9SRobert Olsson 
658007a531bSJamal Hadi Salim 	if (pkt_dev->cflows) {
659007a531bSJamal Hadi Salim 		if (pkt_dev->flags & F_FLOW_SEQ)
66097dc48e2SThomas Graf 			seq_puts(seq,  "FLOW_SEQ  "); /*in sequence flows*/
661007a531bSJamal Hadi Salim 		else
66297dc48e2SThomas Graf 			seq_puts(seq,  "FLOW_RND  ");
663007a531bSJamal Hadi Salim 	}
664007a531bSJamal Hadi Salim 
665a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
6668101328bSFan Du 	if (pkt_dev->flags & F_IPSEC_ON) {
66797dc48e2SThomas Graf 		seq_puts(seq,  "IPSEC  ");
6688101328bSFan Du 		if (pkt_dev->spi)
6698101328bSFan Du 			seq_printf(seq, "spi:%u", pkt_dev->spi);
6708101328bSFan Du 	}
671a553e4a6SJamal Hadi Salim #endif
672a553e4a6SJamal Hadi Salim 
6731da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACSRC_RND)
67497dc48e2SThomas Graf 		seq_puts(seq, "MACSRC_RND  ");
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACDST_RND)
67797dc48e2SThomas Graf 		seq_puts(seq, "MACDST_RND  ");
6781da177e4SLinus Torvalds 
67934954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_VID_RND)
68097dc48e2SThomas Graf 		seq_puts(seq, "VID_RND  ");
68134954ddcSFrancesco Fondelli 
68234954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_SVID_RND)
68397dc48e2SThomas Graf 		seq_puts(seq, "SVID_RND  ");
68434954ddcSFrancesco Fondelli 
685e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE)
68697dc48e2SThomas Graf 		seq_puts(seq, "NODE_ALLOC  ");
687e99b99b4SRobert Olsson 
688d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6891da177e4SLinus Torvalds 
690fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
691398f382cSDaniel Borkmann 	stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
692fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
693fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
6941da177e4SLinus Torvalds 
695222f1806SLuiz Capitulino 	seq_printf(seq,
696fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
6971da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
698fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
699fd29cf72SStephen Hemminger 
700fd29cf72SStephen Hemminger 	seq_printf(seq,
701fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
702fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
703fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
704fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
7051da177e4SLinus Torvalds 
706222f1806SLuiz Capitulino 	seq_printf(seq,
707222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
708d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
709d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
71247a0200dSAlexey Dobriyan 		seq_printf(seq, "     cur_saddr: %pI6c  cur_daddr: %pI6c\n",
71347a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_saddr,
71447a0200dSAlexey Dobriyan 				&pkt_dev->cur_in6_daddr);
715222f1806SLuiz Capitulino 	} else
7160373a946SAmerigo Wang 		seq_printf(seq, "     cur_saddr: %pI4  cur_daddr: %pI4\n",
7170373a946SAmerigo Wang 			   &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
7181da177e4SLinus Torvalds 
719d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
7201da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
7211da177e4SLinus Torvalds 
72245b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
72345b270f8SRobert Olsson 
724d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	if (pkt_dev->result[0])
727d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
7281da177e4SLinus Torvalds 	else
72997dc48e2SThomas Graf 		seq_puts(seq, "Result: Idle\n");
7301da177e4SLinus Torvalds 
731d50a6b56SStephen Hemminger 	return 0;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734ca6549afSSteven Whitehouse 
73563adc6fbSStephen Hemminger static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
73663adc6fbSStephen Hemminger 		     __u32 *num)
737ca6549afSSteven Whitehouse {
738ca6549afSSteven Whitehouse 	int i = 0;
739ca6549afSSteven Whitehouse 	*num = 0;
740ca6549afSSteven Whitehouse 
7411ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
74282fd5b5dSAndy Shevchenko 		int value;
743ca6549afSSteven Whitehouse 		char c;
744ca6549afSSteven Whitehouse 		*num <<= 4;
745ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
746ca6549afSSteven Whitehouse 			return -EFAULT;
74782fd5b5dSAndy Shevchenko 		value = hex_to_bin(c);
74882fd5b5dSAndy Shevchenko 		if (value >= 0)
74982fd5b5dSAndy Shevchenko 			*num |= value;
750ca6549afSSteven Whitehouse 		else
751ca6549afSSteven Whitehouse 			break;
752ca6549afSSteven Whitehouse 	}
753ca6549afSSteven Whitehouse 	return i;
754ca6549afSSteven Whitehouse }
755ca6549afSSteven Whitehouse 
756222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
757222f1806SLuiz Capitulino 			     unsigned int maxlen)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	int i;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7621da177e4SLinus Torvalds 		char c;
7631da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7641da177e4SLinus Torvalds 			return -EFAULT;
7651da177e4SLinus Torvalds 		switch (c) {
7661da177e4SLinus Torvalds 		case '\"':
7671da177e4SLinus Torvalds 		case '\n':
7681da177e4SLinus Torvalds 		case '\r':
7691da177e4SLinus Torvalds 		case '\t':
7701da177e4SLinus Torvalds 		case ' ':
7711da177e4SLinus Torvalds 		case '=':
7721da177e4SLinus Torvalds 			break;
7731da177e4SLinus Torvalds 		default:
7741da177e4SLinus Torvalds 			goto done;
7753ff50b79SStephen Hemminger 		}
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds done:
7781da177e4SLinus Torvalds 	return i;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
781bf0813bdSPaul Gortmaker static long num_arg(const char __user *user_buffer, unsigned long maxlen,
782bf0813bdSPaul Gortmaker 				unsigned long *num)
7831da177e4SLinus Torvalds {
784d6182223SPaul Gortmaker 	int i;
7851da177e4SLinus Torvalds 	*num = 0;
7861da177e4SLinus Torvalds 
787d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7881da177e4SLinus Torvalds 		char c;
7891da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7901da177e4SLinus Torvalds 			return -EFAULT;
7911da177e4SLinus Torvalds 		if ((c >= '0') && (c <= '9')) {
7921da177e4SLinus Torvalds 			*num *= 10;
7931da177e4SLinus Torvalds 			*num += c - '0';
7941da177e4SLinus Torvalds 		} else
7951da177e4SLinus Torvalds 			break;
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 	return i;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
8011da177e4SLinus Torvalds {
802d6182223SPaul Gortmaker 	int i;
8031da177e4SLinus Torvalds 
804d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
8051da177e4SLinus Torvalds 		char c;
8061da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
8071da177e4SLinus Torvalds 			return -EFAULT;
8081da177e4SLinus Torvalds 		switch (c) {
8091da177e4SLinus Torvalds 		case '\"':
8101da177e4SLinus Torvalds 		case '\n':
8111da177e4SLinus Torvalds 		case '\r':
8121da177e4SLinus Torvalds 		case '\t':
8131da177e4SLinus Torvalds 		case ' ':
8141da177e4SLinus Torvalds 			goto done_str;
8151da177e4SLinus Torvalds 		default:
8161da177e4SLinus Torvalds 			break;
8173ff50b79SStephen Hemminger 		}
8181da177e4SLinus Torvalds 	}
8191da177e4SLinus Torvalds done_str:
8201da177e4SLinus Torvalds 	return i;
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
823ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
824ca6549afSSteven Whitehouse {
82595c96174SEric Dumazet 	unsigned int n = 0;
826ca6549afSSteven Whitehouse 	char c;
827ca6549afSSteven Whitehouse 	ssize_t i = 0;
828ca6549afSSteven Whitehouse 	int len;
829ca6549afSSteven Whitehouse 
830ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = 0;
831ca6549afSSteven Whitehouse 	do {
832ca6549afSSteven Whitehouse 		__u32 tmp;
8331ca7768cSFrancesco Fondelli 		len = hex32_arg(&buffer[i], 8, &tmp);
834ca6549afSSteven Whitehouse 		if (len <= 0)
835ca6549afSSteven Whitehouse 			return len;
836ca6549afSSteven Whitehouse 		pkt_dev->labels[n] = htonl(tmp);
837ca6549afSSteven Whitehouse 		if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
838ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
839ca6549afSSteven Whitehouse 		i += len;
840ca6549afSSteven Whitehouse 		if (get_user(c, &buffer[i]))
841ca6549afSSteven Whitehouse 			return -EFAULT;
842ca6549afSSteven Whitehouse 		i++;
843ca6549afSSteven Whitehouse 		n++;
844ca6549afSSteven Whitehouse 		if (n >= MAX_MPLS_LABELS)
845ca6549afSSteven Whitehouse 			return -E2BIG;
846ca6549afSSteven Whitehouse 	} while (c == ',');
847ca6549afSSteven Whitehouse 
848ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = n;
849ca6549afSSteven Whitehouse 	return i;
850ca6549afSSteven Whitehouse }
851ca6549afSSteven Whitehouse 
852222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file,
853222f1806SLuiz Capitulino 			       const char __user * user_buffer, size_t count,
854222f1806SLuiz Capitulino 			       loff_t * offset)
8551da177e4SLinus Torvalds {
8568a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
857d50a6b56SStephen Hemminger 	struct pktgen_dev *pkt_dev = seq->private;
858d6182223SPaul Gortmaker 	int i, max, len;
8591da177e4SLinus Torvalds 	char name[16], valstr[32];
8601da177e4SLinus Torvalds 	unsigned long value = 0;
8611da177e4SLinus Torvalds 	char *pg_result = NULL;
8621da177e4SLinus Torvalds 	int tmp = 0;
8631da177e4SLinus Torvalds 	char buf[128];
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	pg_result = &(pkt_dev->result[0]);
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	if (count < 1) {
868294a0b7fSJoe Perches 		pr_warn("wrong command format\n");
8691da177e4SLinus Torvalds 		return -EINVAL;
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds 
872d6182223SPaul Gortmaker 	max = count;
873d6182223SPaul Gortmaker 	tmp = count_trail_chars(user_buffer, max);
8741da177e4SLinus Torvalds 	if (tmp < 0) {
875294a0b7fSJoe Perches 		pr_warn("illegal format\n");
8761da177e4SLinus Torvalds 		return tmp;
8771da177e4SLinus Torvalds 	}
878d6182223SPaul Gortmaker 	i = tmp;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	/* Read variable name */
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
88363adc6fbSStephen Hemminger 	if (len < 0)
884222f1806SLuiz Capitulino 		return len;
88563adc6fbSStephen Hemminger 
8861da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
8871da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
8881da177e4SLinus Torvalds 		return -EFAULT;
8891da177e4SLinus Torvalds 	i += len;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	max = count - i;
8921da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
8931da177e4SLinus Torvalds 	if (len < 0)
8941da177e4SLinus Torvalds 		return len;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	i += len;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	if (debug) {
89986c2c0a8SDmitry Torokhov 		size_t copy = min_t(size_t, count, 1023);
900448d7b5dSNelson Elhage 		char tb[copy + 1];
901448d7b5dSNelson Elhage 		if (copy_from_user(tb, user_buffer, copy))
9021da177e4SLinus Torvalds 			return -EFAULT;
903448d7b5dSNelson Elhage 		tb[copy] = 0;
904f342cda7SJoe Perches 		pr_debug("%s,%lu  buffer -:%s:-\n",
905f342cda7SJoe Perches 			 name, (unsigned long)count, tb);
9061da177e4SLinus Torvalds 	}
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	if (!strcmp(name, "min_pkt_size")) {
9091da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
91063adc6fbSStephen Hemminger 		if (len < 0)
911222f1806SLuiz Capitulino 			return len;
91263adc6fbSStephen Hemminger 
9131da177e4SLinus Torvalds 		i += len;
9141da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9151da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9161da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9171da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9181da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9191da177e4SLinus Torvalds 		}
920222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: min_pkt_size=%u",
921222f1806SLuiz Capitulino 			pkt_dev->min_pkt_size);
9221da177e4SLinus Torvalds 		return count;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	if (!strcmp(name, "max_pkt_size")) {
9261da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
92763adc6fbSStephen Hemminger 		if (len < 0)
928222f1806SLuiz Capitulino 			return len;
92963adc6fbSStephen Hemminger 
9301da177e4SLinus Torvalds 		i += len;
9311da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9321da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9331da177e4SLinus Torvalds 		if (value != pkt_dev->max_pkt_size) {
9341da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9351da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9361da177e4SLinus Torvalds 		}
937222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: max_pkt_size=%u",
938222f1806SLuiz Capitulino 			pkt_dev->max_pkt_size);
9391da177e4SLinus Torvalds 		return count;
9401da177e4SLinus Torvalds 	}
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	/* Shortcut for min = max */
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	if (!strcmp(name, "pkt_size")) {
9451da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
94663adc6fbSStephen Hemminger 		if (len < 0)
947222f1806SLuiz Capitulino 			return len;
94863adc6fbSStephen Hemminger 
9491da177e4SLinus Torvalds 		i += len;
9501da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9511da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9521da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9531da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9541da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9551da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9561da177e4SLinus Torvalds 		}
9571da177e4SLinus Torvalds 		sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
9581da177e4SLinus Torvalds 		return count;
9591da177e4SLinus Torvalds 	}
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	if (!strcmp(name, "debug")) {
9621da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
96363adc6fbSStephen Hemminger 		if (len < 0)
964222f1806SLuiz Capitulino 			return len;
96563adc6fbSStephen Hemminger 
9661da177e4SLinus Torvalds 		i += len;
9671da177e4SLinus Torvalds 		debug = value;
9681da177e4SLinus Torvalds 		sprintf(pg_result, "OK: debug=%u", debug);
9691da177e4SLinus Torvalds 		return count;
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	if (!strcmp(name, "frags")) {
9731da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
97463adc6fbSStephen Hemminger 		if (len < 0)
975222f1806SLuiz Capitulino 			return len;
97663adc6fbSStephen Hemminger 
9771da177e4SLinus Torvalds 		i += len;
9781da177e4SLinus Torvalds 		pkt_dev->nfrags = value;
9791da177e4SLinus Torvalds 		sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
9801da177e4SLinus Torvalds 		return count;
9811da177e4SLinus Torvalds 	}
9821da177e4SLinus Torvalds 	if (!strcmp(name, "delay")) {
9831da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
98463adc6fbSStephen Hemminger 		if (len < 0)
985222f1806SLuiz Capitulino 			return len;
98663adc6fbSStephen Hemminger 
9871da177e4SLinus Torvalds 		i += len;
988fd29cf72SStephen Hemminger 		if (value == 0x7FFFFFFF)
989fd29cf72SStephen Hemminger 			pkt_dev->delay = ULLONG_MAX;
990fd29cf72SStephen Hemminger 		else
9919240d715SEric Dumazet 			pkt_dev->delay = (u64)value;
992fd29cf72SStephen Hemminger 
993fd29cf72SStephen Hemminger 		sprintf(pg_result, "OK: delay=%llu",
994fd29cf72SStephen Hemminger 			(unsigned long long) pkt_dev->delay);
9951da177e4SLinus Torvalds 		return count;
9961da177e4SLinus Torvalds 	}
99743d28b65SDaniel Turull 	if (!strcmp(name, "rate")) {
99843d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
99943d28b65SDaniel Turull 		if (len < 0)
100043d28b65SDaniel Turull 			return len;
100143d28b65SDaniel Turull 
100243d28b65SDaniel Turull 		i += len;
100343d28b65SDaniel Turull 		if (!value)
100443d28b65SDaniel Turull 			return len;
100543d28b65SDaniel Turull 		pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
100643d28b65SDaniel Turull 		if (debug)
1007f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
100843d28b65SDaniel Turull 
100943d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
101043d28b65SDaniel Turull 		return count;
101143d28b65SDaniel Turull 	}
101243d28b65SDaniel Turull 	if (!strcmp(name, "ratep")) {
101343d28b65SDaniel Turull 		len = num_arg(&user_buffer[i], 10, &value);
101443d28b65SDaniel Turull 		if (len < 0)
101543d28b65SDaniel Turull 			return len;
101643d28b65SDaniel Turull 
101743d28b65SDaniel Turull 		i += len;
101843d28b65SDaniel Turull 		if (!value)
101943d28b65SDaniel Turull 			return len;
102043d28b65SDaniel Turull 		pkt_dev->delay = NSEC_PER_SEC/value;
102143d28b65SDaniel Turull 		if (debug)
1022f9467eaeSJoe Perches 			pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
102343d28b65SDaniel Turull 
102443d28b65SDaniel Turull 		sprintf(pg_result, "OK: rate=%lu", value);
102543d28b65SDaniel Turull 		return count;
102643d28b65SDaniel Turull 	}
10271da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_min")) {
10281da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
102963adc6fbSStephen Hemminger 		if (len < 0)
1030222f1806SLuiz Capitulino 			return len;
103163adc6fbSStephen Hemminger 
10321da177e4SLinus Torvalds 		i += len;
10331da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_min) {
10341da177e4SLinus Torvalds 			pkt_dev->udp_src_min = value;
10351da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10361da177e4SLinus Torvalds 		}
10371da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
10381da177e4SLinus Torvalds 		return count;
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_min")) {
10411da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
104263adc6fbSStephen Hemminger 		if (len < 0)
1043222f1806SLuiz Capitulino 			return len;
104463adc6fbSStephen Hemminger 
10451da177e4SLinus Torvalds 		i += len;
10461da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_min) {
10471da177e4SLinus Torvalds 			pkt_dev->udp_dst_min = value;
10481da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10491da177e4SLinus Torvalds 		}
10501da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
10511da177e4SLinus Torvalds 		return count;
10521da177e4SLinus Torvalds 	}
10531da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_max")) {
10541da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
105563adc6fbSStephen Hemminger 		if (len < 0)
1056222f1806SLuiz Capitulino 			return len;
105763adc6fbSStephen Hemminger 
10581da177e4SLinus Torvalds 		i += len;
10591da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_max) {
10601da177e4SLinus Torvalds 			pkt_dev->udp_src_max = value;
10611da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10621da177e4SLinus Torvalds 		}
10631da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
10641da177e4SLinus Torvalds 		return count;
10651da177e4SLinus Torvalds 	}
10661da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_max")) {
10671da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
106863adc6fbSStephen Hemminger 		if (len < 0)
1069222f1806SLuiz Capitulino 			return len;
107063adc6fbSStephen Hemminger 
10711da177e4SLinus Torvalds 		i += len;
10721da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_max) {
10731da177e4SLinus Torvalds 			pkt_dev->udp_dst_max = value;
10741da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10751da177e4SLinus Torvalds 		}
10761da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
10771da177e4SLinus Torvalds 		return count;
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 	if (!strcmp(name, "clone_skb")) {
10801da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
108163adc6fbSStephen Hemminger 		if (len < 0)
1082222f1806SLuiz Capitulino 			return len;
1083d8873315SNeil Horman 		if ((value > 0) &&
1084d8873315SNeil Horman 		    (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1085d8873315SNeil Horman 			return -ENOTSUPP;
10861da177e4SLinus Torvalds 		i += len;
10871da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
10901da177e4SLinus Torvalds 		return count;
10911da177e4SLinus Torvalds 	}
10921da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
10931da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
109463adc6fbSStephen Hemminger 		if (len < 0)
1095222f1806SLuiz Capitulino 			return len;
109663adc6fbSStephen Hemminger 
10971da177e4SLinus Torvalds 		i += len;
10981da177e4SLinus Torvalds 		pkt_dev->count = value;
10991da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
11001da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
11011da177e4SLinus Torvalds 		return count;
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
11041da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
110563adc6fbSStephen Hemminger 		if (len < 0)
1106222f1806SLuiz Capitulino 			return len;
110763adc6fbSStephen Hemminger 
11081da177e4SLinus Torvalds 		i += len;
11091da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
11101da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
11111da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
11121da177e4SLinus Torvalds 		}
1113222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1114222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
11151da177e4SLinus Torvalds 		return count;
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
11181da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
111963adc6fbSStephen Hemminger 		if (len < 0)
1120222f1806SLuiz Capitulino 			return len;
112163adc6fbSStephen Hemminger 
11221da177e4SLinus Torvalds 		i += len;
11231da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
11241da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
11251da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
11261da177e4SLinus Torvalds 		}
1127222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1128222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
11291da177e4SLinus Torvalds 		return count;
11301da177e4SLinus Torvalds 	}
1131*38b2cf29SAlexei Starovoitov 	if (!strcmp(name, "burst")) {
1132*38b2cf29SAlexei Starovoitov 		len = num_arg(&user_buffer[i], 10, &value);
1133*38b2cf29SAlexei Starovoitov 		if (len < 0)
1134*38b2cf29SAlexei Starovoitov 			return len;
1135*38b2cf29SAlexei Starovoitov 
1136*38b2cf29SAlexei Starovoitov 		i += len;
1137*38b2cf29SAlexei Starovoitov 		pkt_dev->burst = value < 1 ? 1 : value;
1138*38b2cf29SAlexei Starovoitov 		sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
1139*38b2cf29SAlexei Starovoitov 		return count;
1140*38b2cf29SAlexei Starovoitov 	}
1141e99b99b4SRobert Olsson 	if (!strcmp(name, "node")) {
1142e99b99b4SRobert Olsson 		len = num_arg(&user_buffer[i], 10, &value);
1143e99b99b4SRobert Olsson 		if (len < 0)
1144e99b99b4SRobert Olsson 			return len;
1145e99b99b4SRobert Olsson 
1146e99b99b4SRobert Olsson 		i += len;
1147e99b99b4SRobert Olsson 
1148e99b99b4SRobert Olsson 		if (node_possible(value)) {
1149e99b99b4SRobert Olsson 			pkt_dev->node = value;
1150e99b99b4SRobert Olsson 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
115126ad7879SEric Dumazet 			if (pkt_dev->page) {
115226ad7879SEric Dumazet 				put_page(pkt_dev->page);
115326ad7879SEric Dumazet 				pkt_dev->page = NULL;
115426ad7879SEric Dumazet 			}
1155e99b99b4SRobert Olsson 		}
1156e99b99b4SRobert Olsson 		else
1157e99b99b4SRobert Olsson 			sprintf(pg_result, "ERROR: node not possible");
1158e99b99b4SRobert Olsson 		return count;
1159e99b99b4SRobert Olsson 	}
11601da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
11611da177e4SLinus Torvalds 		char f[32];
11621da177e4SLinus Torvalds 		memset(f, 0, 32);
11631da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
116463adc6fbSStephen Hemminger 		if (len < 0)
1165222f1806SLuiz Capitulino 			return len;
116663adc6fbSStephen Hemminger 
11671da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
11681da177e4SLinus Torvalds 			return -EFAULT;
11691da177e4SLinus Torvalds 		i += len;
11701da177e4SLinus Torvalds 		if (strcmp(f, "IPSRC_RND") == 0)
11711da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPSRC_RND;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 		else if (strcmp(f, "!IPSRC_RND") == 0)
11741da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPSRC_RND;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 		else if (strcmp(f, "TXSIZE_RND") == 0)
11771da177e4SLinus Torvalds 			pkt_dev->flags |= F_TXSIZE_RND;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 		else if (strcmp(f, "!TXSIZE_RND") == 0)
11801da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_TXSIZE_RND;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 		else if (strcmp(f, "IPDST_RND") == 0)
11831da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPDST_RND;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 		else if (strcmp(f, "!IPDST_RND") == 0)
11861da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPDST_RND;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 		else if (strcmp(f, "UDPSRC_RND") == 0)
11891da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPSRC_RND;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPSRC_RND") == 0)
11921da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPSRC_RND;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 		else if (strcmp(f, "UDPDST_RND") == 0)
11951da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPDST_RND;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPDST_RND") == 0)
11981da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPDST_RND;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 		else if (strcmp(f, "MACSRC_RND") == 0)
12011da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACSRC_RND;
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 		else if (strcmp(f, "!MACSRC_RND") == 0)
12041da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACSRC_RND;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 		else if (strcmp(f, "MACDST_RND") == 0)
12071da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACDST_RND;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 		else if (strcmp(f, "!MACDST_RND") == 0)
12101da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACDST_RND;
12111da177e4SLinus Torvalds 
1212ca6549afSSteven Whitehouse 		else if (strcmp(f, "MPLS_RND") == 0)
1213ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
1214ca6549afSSteven Whitehouse 
1215ca6549afSSteven Whitehouse 		else if (strcmp(f, "!MPLS_RND") == 0)
1216ca6549afSSteven Whitehouse 			pkt_dev->flags &= ~F_MPLS_RND;
1217ca6549afSSteven Whitehouse 
121834954ddcSFrancesco Fondelli 		else if (strcmp(f, "VID_RND") == 0)
121934954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_VID_RND;
122034954ddcSFrancesco Fondelli 
122134954ddcSFrancesco Fondelli 		else if (strcmp(f, "!VID_RND") == 0)
122234954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_VID_RND;
122334954ddcSFrancesco Fondelli 
122434954ddcSFrancesco Fondelli 		else if (strcmp(f, "SVID_RND") == 0)
122534954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_SVID_RND;
122634954ddcSFrancesco Fondelli 
122734954ddcSFrancesco Fondelli 		else if (strcmp(f, "!SVID_RND") == 0)
122834954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_SVID_RND;
122934954ddcSFrancesco Fondelli 
1230007a531bSJamal Hadi Salim 		else if (strcmp(f, "FLOW_SEQ") == 0)
1231007a531bSJamal Hadi Salim 			pkt_dev->flags |= F_FLOW_SEQ;
1232007a531bSJamal Hadi Salim 
123345b270f8SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
123445b270f8SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_RND;
123545b270f8SRobert Olsson 
123645b270f8SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
123745b270f8SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1238e6fce5b9SRobert Olsson 
1239e6fce5b9SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1240e6fce5b9SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1241e6fce5b9SRobert Olsson 
1242e6fce5b9SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1243e6fce5b9SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1244a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
1245a553e4a6SJamal Hadi Salim 		else if (strcmp(f, "IPSEC") == 0)
1246a553e4a6SJamal Hadi Salim 			pkt_dev->flags |= F_IPSEC_ON;
1247a553e4a6SJamal Hadi Salim #endif
1248a553e4a6SJamal Hadi Salim 
12491ca7768cSFrancesco Fondelli 		else if (strcmp(f, "!IPV6") == 0)
12501ca7768cSFrancesco Fondelli 			pkt_dev->flags &= ~F_IPV6;
12511ca7768cSFrancesco Fondelli 
1252e99b99b4SRobert Olsson 		else if (strcmp(f, "NODE_ALLOC") == 0)
1253e99b99b4SRobert Olsson 			pkt_dev->flags |= F_NODE;
1254e99b99b4SRobert Olsson 
1255e99b99b4SRobert Olsson 		else if (strcmp(f, "!NODE_ALLOC") == 0)
1256e99b99b4SRobert Olsson 			pkt_dev->flags &= ~F_NODE;
1257e99b99b4SRobert Olsson 
1258c26bf4a5SThomas Graf 		else if (strcmp(f, "UDPCSUM") == 0)
1259c26bf4a5SThomas Graf 			pkt_dev->flags |= F_UDPCSUM;
1260c26bf4a5SThomas Graf 
1261c26bf4a5SThomas Graf 		else if (strcmp(f, "!UDPCSUM") == 0)
1262c26bf4a5SThomas Graf 			pkt_dev->flags &= ~F_UDPCSUM;
1263c26bf4a5SThomas Graf 
1264afb84b62SJesper Dangaard Brouer 		else if (strcmp(f, "NO_TIMESTAMP") == 0)
1265afb84b62SJesper Dangaard Brouer 			pkt_dev->flags |= F_NO_TIMESTAMP;
1266afb84b62SJesper Dangaard Brouer 
12671da177e4SLinus Torvalds 		else {
1268222f1806SLuiz Capitulino 			sprintf(pg_result,
1269222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
12701da177e4SLinus Torvalds 				f,
12711ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
127272f8e06fSMathias Krause 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
127372f8e06fSMathias Krause 				"MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
127472f8e06fSMathias Krause 				"QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
1275afb84b62SJesper Dangaard Brouer 				"NO_TIMESTAMP, "
127672f8e06fSMathias Krause #ifdef CONFIG_XFRM
127772f8e06fSMathias Krause 				"IPSEC, "
127872f8e06fSMathias Krause #endif
127972f8e06fSMathias Krause 				"NODE_ALLOC\n");
12801da177e4SLinus Torvalds 			return count;
12811da177e4SLinus Torvalds 		}
12821da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
12831da177e4SLinus Torvalds 		return count;
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
12861da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
128763adc6fbSStephen Hemminger 		if (len < 0)
1288222f1806SLuiz Capitulino 			return len;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12911da177e4SLinus Torvalds 			return -EFAULT;
12921da177e4SLinus Torvalds 		buf[len] = 0;
12931da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
12941da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
12951da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_min, buf, len);
12961da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
12971da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
12981da177e4SLinus Torvalds 		}
12991da177e4SLinus Torvalds 		if (debug)
1300f342cda7SJoe Perches 			pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
13011da177e4SLinus Torvalds 		i += len;
13021da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
13031da177e4SLinus Torvalds 		return count;
13041da177e4SLinus Torvalds 	}
13051da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
13061da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
130763adc6fbSStephen Hemminger 		if (len < 0)
1308222f1806SLuiz Capitulino 			return len;
130963adc6fbSStephen Hemminger 
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13121da177e4SLinus Torvalds 			return -EFAULT;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 		buf[len] = 0;
13151da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
13161da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
13171da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_max, buf, len);
13181da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
13191da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
13201da177e4SLinus Torvalds 		}
13211da177e4SLinus Torvalds 		if (debug)
1322f342cda7SJoe Perches 			pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
13231da177e4SLinus Torvalds 		i += len;
13241da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
13251da177e4SLinus Torvalds 		return count;
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
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->in6_daddr.s6_addr, -1, NULL);
133947a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
13401da177e4SLinus Torvalds 
13414e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 		if (debug)
1344f342cda7SJoe Perches 			pr_debug("dst6 set to: %s\n", buf);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 		i += len;
13471da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
13481da177e4SLinus Torvalds 		return count;
13491da177e4SLinus Torvalds 	}
13501da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
13511da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1352222f1806SLuiz Capitulino 		if (len < 0)
1353222f1806SLuiz Capitulino 			return len;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13581da177e4SLinus Torvalds 			return -EFAULT;
13591da177e4SLinus Torvalds 		buf[len] = 0;
13601da177e4SLinus Torvalds 
1361c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
136247a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
13631da177e4SLinus Torvalds 
13644e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
13651da177e4SLinus Torvalds 		if (debug)
1366f342cda7SJoe Perches 			pr_debug("dst6_min set to: %s\n", buf);
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 		i += len;
13691da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
13701da177e4SLinus Torvalds 		return count;
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
13731da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1374222f1806SLuiz Capitulino 		if (len < 0)
1375222f1806SLuiz Capitulino 			return len;
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13801da177e4SLinus Torvalds 			return -EFAULT;
13811da177e4SLinus Torvalds 		buf[len] = 0;
13821da177e4SLinus Torvalds 
1383c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
138447a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 		if (debug)
1387f342cda7SJoe Perches 			pr_debug("dst6_max set to: %s\n", buf);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 		i += len;
13901da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
13911da177e4SLinus Torvalds 		return count;
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
13941da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1395222f1806SLuiz Capitulino 		if (len < 0)
1396222f1806SLuiz Capitulino 			return len;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14011da177e4SLinus Torvalds 			return -EFAULT;
14021da177e4SLinus Torvalds 		buf[len] = 0;
14031da177e4SLinus Torvalds 
1404c468fb13SAmerigo Wang 		in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
140547a0200dSAlexey Dobriyan 		snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
14061da177e4SLinus Torvalds 
14074e3fd7a0SAlexey Dobriyan 		pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 		if (debug)
1410f342cda7SJoe Perches 			pr_debug("src6 set to: %s\n", buf);
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 		i += len;
14131da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
14141da177e4SLinus Torvalds 		return count;
14151da177e4SLinus Torvalds 	}
14161da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
14171da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
141863adc6fbSStephen Hemminger 		if (len < 0)
1419222f1806SLuiz Capitulino 			return len;
142063adc6fbSStephen Hemminger 
14211da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14221da177e4SLinus Torvalds 			return -EFAULT;
14231da177e4SLinus Torvalds 		buf[len] = 0;
14241da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
14251da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
14261da177e4SLinus Torvalds 			strncpy(pkt_dev->src_min, buf, len);
14271da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
14281da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
14291da177e4SLinus Torvalds 		}
14301da177e4SLinus Torvalds 		if (debug)
1431f342cda7SJoe Perches 			pr_debug("src_min set to: %s\n", pkt_dev->src_min);
14321da177e4SLinus Torvalds 		i += len;
14331da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
14341da177e4SLinus Torvalds 		return count;
14351da177e4SLinus Torvalds 	}
14361da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
14371da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
143863adc6fbSStephen Hemminger 		if (len < 0)
1439222f1806SLuiz Capitulino 			return len;
144063adc6fbSStephen Hemminger 
14411da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14421da177e4SLinus Torvalds 			return -EFAULT;
14431da177e4SLinus Torvalds 		buf[len] = 0;
14441da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
14451da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
14461da177e4SLinus Torvalds 			strncpy(pkt_dev->src_max, buf, len);
14471da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
14481da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
14491da177e4SLinus Torvalds 		}
14501da177e4SLinus Torvalds 		if (debug)
1451f342cda7SJoe Perches 			pr_debug("src_max set to: %s\n", pkt_dev->src_max);
14521da177e4SLinus Torvalds 		i += len;
14531da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
14541da177e4SLinus Torvalds 		return count;
14551da177e4SLinus Torvalds 	}
14561da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
14571da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
145863adc6fbSStephen Hemminger 		if (len < 0)
1459222f1806SLuiz Capitulino 			return len;
146063adc6fbSStephen Hemminger 
14611da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14621da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14631da177e4SLinus Torvalds 			return -EFAULT;
14641da177e4SLinus Torvalds 
14654940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->dst_mac))
14664940fc88SAlexey Dobriyan 			return -EINVAL;
14671da177e4SLinus Torvalds 		/* Set up Dest MAC */
14689ea08b12SJoe Perches 		ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
14691da177e4SLinus Torvalds 
14704940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
14711da177e4SLinus Torvalds 		return count;
14721da177e4SLinus Torvalds 	}
14731da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
14741da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
147563adc6fbSStephen Hemminger 		if (len < 0)
1476222f1806SLuiz Capitulino 			return len;
147763adc6fbSStephen Hemminger 
14781da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14791da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14801da177e4SLinus Torvalds 			return -EFAULT;
14811da177e4SLinus Torvalds 
14824940fc88SAlexey Dobriyan 		if (!mac_pton(valstr, pkt_dev->src_mac))
14834940fc88SAlexey Dobriyan 			return -EINVAL;
1484ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
14859ea08b12SJoe Perches 		ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
1486ce5d0b47SAdit Ranadive 
14874940fc88SAlexey Dobriyan 		sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
14881da177e4SLinus Torvalds 		return count;
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
14921da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
14931da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
14941da177e4SLinus Torvalds 		return count;
14951da177e4SLinus Torvalds 	}
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
14981da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
149963adc6fbSStephen Hemminger 		if (len < 0)
1500222f1806SLuiz Capitulino 			return len;
150163adc6fbSStephen Hemminger 
15021da177e4SLinus Torvalds 		i += len;
15031da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
15041da177e4SLinus Torvalds 			value = MAX_CFLOWS;
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 		pkt_dev->cflows = value;
15071da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
15081da177e4SLinus Torvalds 		return count;
15091da177e4SLinus Torvalds 	}
15106bae9190SFan Du #ifdef CONFIG_XFRM
1511de4aee7dSFan Du 	if (!strcmp(name, "spi")) {
1512de4aee7dSFan Du 		len = num_arg(&user_buffer[i], 10, &value);
1513de4aee7dSFan Du 		if (len < 0)
1514de4aee7dSFan Du 			return len;
1515de4aee7dSFan Du 
1516de4aee7dSFan Du 		i += len;
1517de4aee7dSFan Du 		pkt_dev->spi = value;
1518de4aee7dSFan Du 		sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1519de4aee7dSFan Du 		return count;
1520de4aee7dSFan Du 	}
15216bae9190SFan Du #endif
15221da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
15231da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
152463adc6fbSStephen Hemminger 		if (len < 0)
1525222f1806SLuiz Capitulino 			return len;
152663adc6fbSStephen Hemminger 
15271da177e4SLinus Torvalds 		i += len;
15281da177e4SLinus Torvalds 		pkt_dev->lflow = value;
15291da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
15301da177e4SLinus Torvalds 		return count;
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 
153345b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
153445b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
153563adc6fbSStephen Hemminger 		if (len < 0)
153645b270f8SRobert Olsson 			return len;
153763adc6fbSStephen Hemminger 
153845b270f8SRobert Olsson 		i += len;
153945b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
154045b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
154145b270f8SRobert Olsson 		return count;
154245b270f8SRobert Olsson 	}
154345b270f8SRobert Olsson 
154445b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
154545b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
154663adc6fbSStephen Hemminger 		if (len < 0)
154745b270f8SRobert Olsson 			return len;
154863adc6fbSStephen Hemminger 
154945b270f8SRobert Olsson 		i += len;
155045b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
155145b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
155245b270f8SRobert Olsson 		return count;
155345b270f8SRobert Olsson 	}
155445b270f8SRobert Olsson 
1555ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
155695c96174SEric Dumazet 		unsigned int n, cnt;
1557cfcabdccSStephen Hemminger 
1558ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1559cfcabdccSStephen Hemminger 		if (len < 0)
1560cfcabdccSStephen Hemminger 			return len;
1561ca6549afSSteven Whitehouse 		i += len;
1562cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1563ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1564cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1565ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1566ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
156734954ddcSFrancesco Fondelli 
156834954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
156934954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
157034954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
157134954ddcSFrancesco Fondelli 
157234954ddcSFrancesco Fondelli 			if (debug)
1573f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN auto turned off\n");
157434954ddcSFrancesco Fondelli 		}
157534954ddcSFrancesco Fondelli 		return count;
157634954ddcSFrancesco Fondelli 	}
157734954ddcSFrancesco Fondelli 
157834954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
157934954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
158063adc6fbSStephen Hemminger 		if (len < 0)
158134954ddcSFrancesco Fondelli 			return len;
158263adc6fbSStephen Hemminger 
158334954ddcSFrancesco Fondelli 		i += len;
158434954ddcSFrancesco Fondelli 		if (value <= 4095) {
158534954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
158634954ddcSFrancesco Fondelli 
158734954ddcSFrancesco Fondelli 			if (debug)
1588f342cda7SJoe Perches 				pr_debug("VLAN turned on\n");
158934954ddcSFrancesco Fondelli 
159034954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1591f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
159234954ddcSFrancesco Fondelli 
159334954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
159434954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
159534954ddcSFrancesco Fondelli 		} else {
159634954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
159734954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
159834954ddcSFrancesco Fondelli 
159934954ddcSFrancesco Fondelli 			if (debug)
1600f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
160134954ddcSFrancesco Fondelli 		}
160234954ddcSFrancesco Fondelli 		return count;
160334954ddcSFrancesco Fondelli 	}
160434954ddcSFrancesco Fondelli 
160534954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
160634954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
160763adc6fbSStephen Hemminger 		if (len < 0)
160834954ddcSFrancesco Fondelli 			return len;
160963adc6fbSStephen Hemminger 
161034954ddcSFrancesco Fondelli 		i += len;
161134954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
161234954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
161334954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
161434954ddcSFrancesco Fondelli 		} else {
161534954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
161634954ddcSFrancesco Fondelli 		}
161734954ddcSFrancesco Fondelli 		return count;
161834954ddcSFrancesco Fondelli 	}
161934954ddcSFrancesco Fondelli 
162034954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
162134954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
162263adc6fbSStephen Hemminger 		if (len < 0)
162334954ddcSFrancesco Fondelli 			return len;
162463adc6fbSStephen Hemminger 
162534954ddcSFrancesco Fondelli 		i += len;
162634954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
162734954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
162834954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
162934954ddcSFrancesco Fondelli 		} else {
163034954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
163134954ddcSFrancesco Fondelli 		}
163234954ddcSFrancesco Fondelli 		return count;
163334954ddcSFrancesco Fondelli 	}
163434954ddcSFrancesco Fondelli 
163534954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
163634954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
163763adc6fbSStephen Hemminger 		if (len < 0)
163834954ddcSFrancesco Fondelli 			return len;
163963adc6fbSStephen Hemminger 
164034954ddcSFrancesco Fondelli 		i += len;
164134954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
164234954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
164334954ddcSFrancesco Fondelli 
164434954ddcSFrancesco Fondelli 			if (debug)
1645f342cda7SJoe Perches 				pr_debug("SVLAN turned on\n");
164634954ddcSFrancesco Fondelli 
164734954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
1648f342cda7SJoe Perches 				pr_debug("MPLS auto turned off\n");
164934954ddcSFrancesco Fondelli 
165034954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
165134954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
165234954ddcSFrancesco Fondelli 		} else {
165334954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
165434954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
165534954ddcSFrancesco Fondelli 
165634954ddcSFrancesco Fondelli 			if (debug)
1657f342cda7SJoe Perches 				pr_debug("VLAN/SVLAN turned off\n");
165834954ddcSFrancesco Fondelli 		}
165934954ddcSFrancesco Fondelli 		return count;
166034954ddcSFrancesco Fondelli 	}
166134954ddcSFrancesco Fondelli 
166234954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
166334954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
166463adc6fbSStephen Hemminger 		if (len < 0)
166534954ddcSFrancesco Fondelli 			return len;
166663adc6fbSStephen Hemminger 
166734954ddcSFrancesco Fondelli 		i += len;
166834954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
166934954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
167034954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
167134954ddcSFrancesco Fondelli 		} else {
167234954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
167334954ddcSFrancesco Fondelli 		}
167434954ddcSFrancesco Fondelli 		return count;
167534954ddcSFrancesco Fondelli 	}
167634954ddcSFrancesco Fondelli 
167734954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
167834954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
167963adc6fbSStephen Hemminger 		if (len < 0)
168034954ddcSFrancesco Fondelli 			return len;
168163adc6fbSStephen Hemminger 
168234954ddcSFrancesco Fondelli 		i += len;
168334954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
168434954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
168534954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
168634954ddcSFrancesco Fondelli 		} else {
168734954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
168834954ddcSFrancesco Fondelli 		}
1689ca6549afSSteven Whitehouse 		return count;
1690ca6549afSSteven Whitehouse 	}
1691ca6549afSSteven Whitehouse 
16921ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
16931ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16941ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
169563adc6fbSStephen Hemminger 		if (len < 0)
16961ca7768cSFrancesco Fondelli 			return len;
169763adc6fbSStephen Hemminger 
16981ca7768cSFrancesco Fondelli 		i += len;
16991ca7768cSFrancesco Fondelli 		if (len == 2) {
17001ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
17011ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
17021ca7768cSFrancesco Fondelli 		} else {
17031ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
17041ca7768cSFrancesco Fondelli 		}
17051ca7768cSFrancesco Fondelli 		return count;
17061ca7768cSFrancesco Fondelli 	}
17071ca7768cSFrancesco Fondelli 
17081ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
17091ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
17101ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
171163adc6fbSStephen Hemminger 		if (len < 0)
17121ca7768cSFrancesco Fondelli 			return len;
171363adc6fbSStephen Hemminger 
17141ca7768cSFrancesco Fondelli 		i += len;
17151ca7768cSFrancesco Fondelli 		if (len == 2) {
17161ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
17171ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
17181ca7768cSFrancesco Fondelli 		} else {
17191ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
17201ca7768cSFrancesco Fondelli 		}
17211ca7768cSFrancesco Fondelli 		return count;
17221ca7768cSFrancesco Fondelli 	}
17231ca7768cSFrancesco Fondelli 
17249e50e3acSJohn Fastabend 	if (!strcmp(name, "skb_priority")) {
17259e50e3acSJohn Fastabend 		len = num_arg(&user_buffer[i], 9, &value);
17269e50e3acSJohn Fastabend 		if (len < 0)
17279e50e3acSJohn Fastabend 			return len;
17289e50e3acSJohn Fastabend 
17299e50e3acSJohn Fastabend 		i += len;
17309e50e3acSJohn Fastabend 		pkt_dev->skb_priority = value;
17319e50e3acSJohn Fastabend 		sprintf(pg_result, "OK: skb_priority=%i",
17329e50e3acSJohn Fastabend 			pkt_dev->skb_priority);
17339e50e3acSJohn Fastabend 		return count;
17349e50e3acSJohn Fastabend 	}
17359e50e3acSJohn Fastabend 
17361da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
17371da177e4SLinus Torvalds 	return -EINVAL;
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
1740d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
17411da177e4SLinus Torvalds {
1742d9dda78bSAl Viro 	return single_open(file, pktgen_if_show, PDE_DATA(inode));
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
17459a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1746d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1747d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1748d50a6b56SStephen Hemminger 	.read    = seq_read,
1749d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1750d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1751d50a6b56SStephen Hemminger 	.release = single_release,
1752d50a6b56SStephen Hemminger };
1753d50a6b56SStephen Hemminger 
1754d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1755d50a6b56SStephen Hemminger {
1756d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1757648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1758d50a6b56SStephen Hemminger 
1759d50a6b56SStephen Hemminger 	BUG_ON(!t);
1760d50a6b56SStephen Hemminger 
176197dc48e2SThomas Graf 	seq_puts(seq, "Running: ");
17621da177e4SLinus Torvalds 
17638788370aSJesper Dangaard Brouer 	rcu_read_lock();
17648788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
17651da177e4SLinus Torvalds 		if (pkt_dev->running)
1766593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17671da177e4SLinus Torvalds 
176897dc48e2SThomas Graf 	seq_puts(seq, "\nStopped: ");
17691da177e4SLinus Torvalds 
17708788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
17711da177e4SLinus Torvalds 		if (!pkt_dev->running)
1772593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	if (t->result[0])
1775d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
17761da177e4SLinus Torvalds 	else
177797dc48e2SThomas Graf 		seq_puts(seq, "\nResult: NA\n");
17781da177e4SLinus Torvalds 
17798788370aSJesper Dangaard Brouer 	rcu_read_unlock();
17801da177e4SLinus Torvalds 
1781d50a6b56SStephen Hemminger 	return 0;
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
1784d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1785d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1786d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
17871da177e4SLinus Torvalds {
17888a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
1789d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1790d6182223SPaul Gortmaker 	int i, max, len, ret;
17911da177e4SLinus Torvalds 	char name[40];
17921da177e4SLinus Torvalds 	char *pg_result;
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	if (count < 1) {
17951da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
17961da177e4SLinus Torvalds 		return -EINVAL;
17971da177e4SLinus Torvalds 	}
17981da177e4SLinus Torvalds 
1799d6182223SPaul Gortmaker 	max = count;
1800d6182223SPaul Gortmaker 	len = count_trail_chars(user_buffer, max);
18011da177e4SLinus Torvalds 	if (len < 0)
18021da177e4SLinus Torvalds 		return len;
18031da177e4SLinus Torvalds 
1804d6182223SPaul Gortmaker 	i = len;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	/* Read variable name */
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
18091da177e4SLinus Torvalds 	if (len < 0)
18101da177e4SLinus Torvalds 		return len;
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
18131da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
18141da177e4SLinus Torvalds 		return -EFAULT;
18151da177e4SLinus Torvalds 	i += len;
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	max = count - i;
18181da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
18191da177e4SLinus Torvalds 	if (len < 0)
18201da177e4SLinus Torvalds 		return len;
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	i += len;
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 	if (debug)
1825f342cda7SJoe Perches 		pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	if (!t) {
1828f9467eaeSJoe Perches 		pr_err("ERROR: No thread\n");
18291da177e4SLinus Torvalds 		ret = -EINVAL;
18301da177e4SLinus Torvalds 		goto out;
18311da177e4SLinus Torvalds 	}
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
18361da177e4SLinus Torvalds 		char f[32];
18371da177e4SLinus Torvalds 		memset(f, 0, 32);
18381da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
18391da177e4SLinus Torvalds 		if (len < 0) {
18401da177e4SLinus Torvalds 			ret = len;
18411da177e4SLinus Torvalds 			goto out;
18421da177e4SLinus Torvalds 		}
18431da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
18441da177e4SLinus Torvalds 			return -EFAULT;
18451da177e4SLinus Torvalds 		i += len;
18466146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
1847604dfd6eSCong Wang 		ret = pktgen_add_device(t, f);
18486146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1849604dfd6eSCong Wang 		if (!ret) {
18501da177e4SLinus Torvalds 			ret = count;
18511da177e4SLinus Torvalds 			sprintf(pg_result, "OK: add_device=%s", f);
1852604dfd6eSCong Wang 		} else
1853604dfd6eSCong Wang 			sprintf(pg_result, "ERROR: can not add device %s", f);
18541da177e4SLinus Torvalds 		goto out;
18551da177e4SLinus Torvalds 	}
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
18586146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
185995ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
18606146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1861121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
18621da177e4SLinus Torvalds 		ret = count;
18631da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
18641da177e4SLinus Torvalds 		goto out;
18651da177e4SLinus Torvalds 	}
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1868b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
18691da177e4SLinus Torvalds 		ret = count;
18701da177e4SLinus Torvalds 		goto out;
18711da177e4SLinus Torvalds 	}
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds 	ret = -EINVAL;
18741da177e4SLinus Torvalds out:
18751da177e4SLinus Torvalds 	return ret;
18761da177e4SLinus Torvalds }
18771da177e4SLinus Torvalds 
1878d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
18791da177e4SLinus Torvalds {
1880d9dda78bSAl Viro 	return single_open(file, pktgen_thread_show, PDE_DATA(inode));
18811da177e4SLinus Torvalds }
18821da177e4SLinus Torvalds 
18839a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1884d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1885d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1886d50a6b56SStephen Hemminger 	.read    = seq_read,
1887d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1888d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1889d50a6b56SStephen Hemminger 	.release = single_release,
1890d50a6b56SStephen Hemminger };
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds /* Think find or remove for NN */
18934e58a027SCong Wang static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
18944e58a027SCong Wang 					      const char *ifname, int remove)
18951da177e4SLinus Torvalds {
18961da177e4SLinus Torvalds 	struct pktgen_thread *t;
18971da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
18983e984840SEric Dumazet 	bool exact = (remove == FIND);
18991da177e4SLinus Torvalds 
19004e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
19013e984840SEric Dumazet 		pkt_dev = pktgen_find_dev(t, ifname, exact);
19021da177e4SLinus Torvalds 		if (pkt_dev) {
19031da177e4SLinus Torvalds 			if (remove) {
190495ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
190595ed63f7SArthur Kepner 				t->control |= T_REMDEV;
19061da177e4SLinus Torvalds 			}
19071da177e4SLinus Torvalds 			break;
19081da177e4SLinus Torvalds 		}
19091da177e4SLinus Torvalds 	}
19101da177e4SLinus Torvalds 	return pkt_dev;
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds 
191395ed63f7SArthur Kepner /*
191495ed63f7SArthur Kepner  * mark a device for removal
191595ed63f7SArthur Kepner  */
19164e58a027SCong Wang static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
19171da177e4SLinus Torvalds {
19181da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
191995ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
192095ed63f7SArthur Kepner 	int i = 0;
192195ed63f7SArthur Kepner 
19226146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
1923f9467eaeSJoe Perches 	pr_debug("%s: marking %s for removal\n", __func__, ifname);
192495ed63f7SArthur Kepner 
192595ed63f7SArthur Kepner 	while (1) {
192695ed63f7SArthur Kepner 
19274e58a027SCong Wang 		pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1928222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1929222f1806SLuiz Capitulino 			break;	/* success */
193095ed63f7SArthur Kepner 
19316146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1932f9467eaeSJoe Perches 		pr_debug("%s: waiting for %s to disappear....\n",
1933f9467eaeSJoe Perches 			 __func__, ifname);
193495ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
19356146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
193695ed63f7SArthur Kepner 
193795ed63f7SArthur Kepner 		if (++i >= max_tries) {
1938f9467eaeSJoe Perches 			pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1939f9467eaeSJoe Perches 			       __func__, msec_per_try * i, ifname);
194095ed63f7SArthur Kepner 			break;
194195ed63f7SArthur Kepner 		}
194295ed63f7SArthur Kepner 
194395ed63f7SArthur Kepner 	}
194495ed63f7SArthur Kepner 
19456146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
194639df232fSStephen Hemminger }
194795ed63f7SArthur Kepner 
19484e58a027SCong Wang static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
194939df232fSStephen Hemminger {
195039df232fSStephen Hemminger 	struct pktgen_thread *t;
195139df232fSStephen Hemminger 
19524e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
195339df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
195439df232fSStephen Hemminger 
19558788370aSJesper Dangaard Brouer 		rcu_read_lock();
19568788370aSJesper Dangaard Brouer 		list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
195739df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
195839df232fSStephen Hemminger 				continue;
195939df232fSStephen Hemminger 
1960a8ca16eaSDavid Howells 			proc_remove(pkt_dev->entry);
196139df232fSStephen Hemminger 
19622975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
19634e58a027SCong Wang 							  pn->proc_dir,
19642975315bSAlexey Dobriyan 							  &pktgen_if_fops,
19652975315bSAlexey Dobriyan 							  pkt_dev);
196639df232fSStephen Hemminger 			if (!pkt_dev->entry)
1967f9467eaeSJoe Perches 				pr_err("can't move proc entry for '%s'\n",
1968f9467eaeSJoe Perches 				       dev->name);
196939df232fSStephen Hemminger 			break;
197039df232fSStephen Hemminger 		}
19718788370aSJesper Dangaard Brouer 		rcu_read_unlock();
197239df232fSStephen Hemminger 	}
19731da177e4SLinus Torvalds }
19741da177e4SLinus Torvalds 
1975222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
1976222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
19771da177e4SLinus Torvalds {
1978351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
19794e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
19801da177e4SLinus Torvalds 
19814e58a027SCong Wang 	if (pn->pktgen_exiting)
1982e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1983e9dc8653SEric W. Biederman 
19841da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
19851da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
19861da177e4SLinus Torvalds 	 */
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	switch (event) {
198939df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
19904e58a027SCong Wang 		pktgen_change_name(pn, dev);
19911da177e4SLinus Torvalds 		break;
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
19944e58a027SCong Wang 		pktgen_mark_device(pn, dev->name);
19951da177e4SLinus Torvalds 		break;
19963ff50b79SStephen Hemminger 	}
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	return NOTIFY_DONE;
19991da177e4SLinus Torvalds }
20001da177e4SLinus Torvalds 
20014e58a027SCong Wang static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
20024e58a027SCong Wang 						 struct pktgen_dev *pkt_dev,
200363adc6fbSStephen Hemminger 						 const char *ifname)
2004e6fce5b9SRobert Olsson {
2005e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
2006d6182223SPaul Gortmaker 	int i;
2007e6fce5b9SRobert Olsson 
2008e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
2009e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
2010e6fce5b9SRobert Olsson 			break;
2011e6fce5b9SRobert Olsson 
2012e6fce5b9SRobert Olsson 		b[i] = ifname[i];
2013e6fce5b9SRobert Olsson 	}
2014e6fce5b9SRobert Olsson 	b[i] = 0;
2015e6fce5b9SRobert Olsson 
20164e58a027SCong Wang 	return dev_get_by_name(pn->net, b);
2017e6fce5b9SRobert Olsson }
2018e6fce5b9SRobert Olsson 
2019e6fce5b9SRobert Olsson 
20201da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
20211da177e4SLinus Torvalds 
20224e58a027SCong Wang static int pktgen_setup_dev(const struct pktgen_net *pn,
20234e58a027SCong Wang 			    struct pktgen_dev *pkt_dev, const char *ifname)
2024222f1806SLuiz Capitulino {
20251da177e4SLinus Torvalds 	struct net_device *odev;
202639df232fSStephen Hemminger 	int err;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	/* Clean old setups */
20291da177e4SLinus Torvalds 	if (pkt_dev->odev) {
20301da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
20311da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
20321da177e4SLinus Torvalds 	}
20331da177e4SLinus Torvalds 
20344e58a027SCong Wang 	odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
20351da177e4SLinus Torvalds 	if (!odev) {
2036f9467eaeSJoe Perches 		pr_err("no such netdevice: \"%s\"\n", ifname);
203739df232fSStephen Hemminger 		return -ENODEV;
20381da177e4SLinus Torvalds 	}
203939df232fSStephen Hemminger 
20401da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
2041f9467eaeSJoe Perches 		pr_err("not an ethernet device: \"%s\"\n", ifname);
204239df232fSStephen Hemminger 		err = -EINVAL;
204339df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
2044f9467eaeSJoe Perches 		pr_err("device is down: \"%s\"\n", ifname);
204539df232fSStephen Hemminger 		err = -ENETDOWN;
204639df232fSStephen Hemminger 	} else {
20471da177e4SLinus Torvalds 		pkt_dev->odev = odev;
204839df232fSStephen Hemminger 		return 0;
204939df232fSStephen Hemminger 	}
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 	dev_put(odev);
205239df232fSStephen Hemminger 	return err;
20531da177e4SLinus Torvalds }
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
20561da177e4SLinus Torvalds  * structure to have the right information to create/send packets
20571da177e4SLinus Torvalds  */
20581da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
20591da177e4SLinus Torvalds {
206064c00d81SAndrew Gallatin 	int ntxq;
206164c00d81SAndrew Gallatin 
20621da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
2063f9467eaeSJoe Perches 		pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2064222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
2065222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
20661da177e4SLinus Torvalds 		return;
20671da177e4SLinus Torvalds 	}
20681da177e4SLinus Torvalds 
206964c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
207064c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2071bfdbc0acSRobert Olsson 
207264c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
2073294a0b7fSJoe Perches 		pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
207488271660SJesse Brandeburg 			pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2075593f63b0SEric Dumazet 			pkt_dev->odevname);
207626e29eedSDan Carpenter 		pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
207764c00d81SAndrew Gallatin 	}
207888271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
2079294a0b7fSJoe Perches 		pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
208088271660SJesse Brandeburg 			pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2081593f63b0SEric Dumazet 			pkt_dev->odevname);
208226e29eedSDan Carpenter 		pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
208364c00d81SAndrew Gallatin 	}
208464c00d81SAndrew Gallatin 
20851da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
20861da177e4SLinus Torvalds 
2087f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
20889ea08b12SJoe Perches 		ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 	/* Set up Dest MAC */
20919ea08b12SJoe Perches 	ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
20944c139b8cSAmerigo Wang 		int i, set = 0, err = 1;
20954c139b8cSAmerigo Wang 		struct inet6_dev *idev;
20964c139b8cSAmerigo Wang 
209768bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
209868bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
209968bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
210068bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
210168bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
210268bf9f0bSAmerigo Wang 		}
210368bf9f0bSAmerigo Wang 
21041da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
21051da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
21061da177e4SLinus Torvalds 				set = 1;
21071da177e4SLinus Torvalds 				break;
21081da177e4SLinus Torvalds 			}
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 		if (!set) {
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 			/*
21131da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
21141da177e4SLinus Torvalds 			 *
21151da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
21161da177e4SLinus Torvalds 			 */
21171da177e4SLinus Torvalds 
21188814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
211963adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
212063adc6fbSStephen Hemminger 			if (idev) {
21211da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
21244c139b8cSAmerigo Wang 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
21254c139b8cSAmerigo Wang 					if ((ifp->scope & IFA_LINK) &&
2126f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
21274e3fd7a0SAlexey Dobriyan 						pkt_dev->cur_in6_saddr = ifp->addr;
21281da177e4SLinus Torvalds 						err = 0;
21291da177e4SLinus Torvalds 						break;
21301da177e4SLinus Torvalds 					}
21311da177e4SLinus Torvalds 				}
21321da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
21331da177e4SLinus Torvalds 			}
21348814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2135222f1806SLuiz Capitulino 			if (err)
2136f9467eaeSJoe Perches 				pr_err("ERROR: IPv6 link address not available\n");
21371da177e4SLinus Torvalds 		}
2138222f1806SLuiz Capitulino 	} else {
213968bf9f0bSAmerigo Wang 		if (pkt_dev->min_pkt_size == 0) {
214068bf9f0bSAmerigo Wang 			pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
214168bf9f0bSAmerigo Wang 						+ sizeof(struct udphdr)
214268bf9f0bSAmerigo Wang 						+ sizeof(struct pktgen_hdr)
214368bf9f0bSAmerigo Wang 						+ pkt_dev->pkt_overhead;
214468bf9f0bSAmerigo Wang 		}
214568bf9f0bSAmerigo Wang 
21461da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
21471da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
21481da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 			struct in_device *in_dev;
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 			rcu_read_lock();
2153e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
21541da177e4SLinus Torvalds 			if (in_dev) {
21551da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2156222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2157222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
21581da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
21591da177e4SLinus Torvalds 				}
21601da177e4SLinus Torvalds 			}
21611da177e4SLinus Torvalds 			rcu_read_unlock();
2162222f1806SLuiz Capitulino 		} else {
21631da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
21641da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
21651da177e4SLinus Torvalds 		}
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
21681da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
21691da177e4SLinus Torvalds 	}
21701da177e4SLinus Torvalds 	/* Initialize current values. */
217168bf9f0bSAmerigo Wang 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
217268bf9f0bSAmerigo Wang 	if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
217368bf9f0bSAmerigo Wang 		pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
217468bf9f0bSAmerigo Wang 
21751da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
21761da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
21771da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
21781da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
21791da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
21801da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
21811da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
21821da177e4SLinus Torvalds }
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 
2185fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2186fd29cf72SStephen Hemminger {
2187ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2188417bc4b8SEric Dumazet 	s64 remaining;
21892bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2190fd29cf72SStephen Hemminger 
21912bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
21922bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2193fd29cf72SStephen Hemminger 
219443d28b65SDaniel Turull 	remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2195417bc4b8SEric Dumazet 	if (remaining <= 0) {
2196417bc4b8SEric Dumazet 		pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21971da177e4SLinus Torvalds 		return;
2198417bc4b8SEric Dumazet 	}
21992bc481cfSStephen Hemminger 
2200398f382cSDaniel Borkmann 	start_time = ktime_get();
220133136d12SEric Dumazet 	if (remaining < 100000) {
220233136d12SEric Dumazet 		/* for small delays (<100us), just loop until limit is reached */
220333136d12SEric Dumazet 		do {
2204398f382cSDaniel Borkmann 			end_time = ktime_get();
2205398f382cSDaniel Borkmann 		} while (ktime_compare(end_time, spin_until) < 0);
220633136d12SEric Dumazet 	} else {
22072bc481cfSStephen Hemminger 		/* see do_nanosleep */
22082bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
22092bc481cfSStephen Hemminger 		do {
22102bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
22112bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
22122bc481cfSStephen Hemminger 			if (!hrtimer_active(&t.timer))
22132bc481cfSStephen Hemminger 				t.task = NULL;
22142bc481cfSStephen Hemminger 
22152bc481cfSStephen Hemminger 			if (likely(t.task))
22161da177e4SLinus Torvalds 				schedule();
22171da177e4SLinus Torvalds 
22182bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
22192bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
22202bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
2221398f382cSDaniel Borkmann 		end_time = ktime_get();
222233136d12SEric Dumazet 	}
2223ef87979cSStephen Hemminger 
2224ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
222507a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
22261da177e4SLinus Torvalds }
22271da177e4SLinus Torvalds 
222816dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
222916dab72fSJamal Hadi Salim {
2230a553e4a6SJamal Hadi Salim 	pkt_dev->pkt_overhead = 0;
223116dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
223216dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
223316dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
223416dab72fSJamal Hadi Salim }
223516dab72fSJamal Hadi Salim 
2236648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2237007a531bSJamal Hadi Salim {
2238648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2239007a531bSJamal Hadi Salim }
2240007a531bSJamal Hadi Salim 
2241007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2242007a531bSJamal Hadi Salim {
2243007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2244007a531bSJamal Hadi Salim 
2245007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2246007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2247007a531bSJamal Hadi Salim 			/* reset time */
2248007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22491211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2250007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2251007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2252007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2253007a531bSJamal Hadi Salim 		}
2254007a531bSJamal Hadi Salim 	} else {
225533d7c5e5SAkinobu Mita 		flow = prandom_u32() % pkt_dev->cflows;
22561211a645SRobert Olsson 		pkt_dev->curfl = flow;
2257007a531bSJamal Hadi Salim 
22581211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2259007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22601211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
22611211a645SRobert Olsson 		}
2262007a531bSJamal Hadi Salim 	}
2263007a531bSJamal Hadi Salim 
2264007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2265007a531bSJamal Hadi Salim }
2266007a531bSJamal Hadi Salim 
2267a553e4a6SJamal Hadi Salim 
2268a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2269a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2270a553e4a6SJamal Hadi Salim  * we go look for it ...
2271a553e4a6SJamal Hadi Salim */
2272bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2273fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2274a553e4a6SJamal Hadi Salim {
2275a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
22764e58a027SCong Wang 	struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2277a553e4a6SJamal Hadi Salim 	if (!x) {
2278c454997eSFan Du 
2279c454997eSFan Du 		if (pkt_dev->spi) {
2280c454997eSFan Du 			/* We need as quick as possible to find the right SA
2281c454997eSFan Du 			 * Searching with minimum criteria to archieve this.
2282c454997eSFan Du 			 */
2283c454997eSFan Du 			x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2284c454997eSFan Du 		} else {
2285a553e4a6SJamal Hadi Salim 			/* slow path: we dont already have xfrm_state */
22864e58a027SCong Wang 			x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
22875447c5e4SAlexey Dobriyan 						(xfrm_address_t *)&pkt_dev->cur_daddr,
2288a553e4a6SJamal Hadi Salim 						(xfrm_address_t *)&pkt_dev->cur_saddr,
2289a553e4a6SJamal Hadi Salim 						AF_INET,
2290a553e4a6SJamal Hadi Salim 						pkt_dev->ipsmode,
2291a553e4a6SJamal Hadi Salim 						pkt_dev->ipsproto, 0);
2292c454997eSFan Du 		}
2293a553e4a6SJamal Hadi Salim 		if (x) {
2294a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2295a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2296a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2297a553e4a6SJamal Hadi Salim 		}
2298a553e4a6SJamal Hadi Salim 
2299a553e4a6SJamal Hadi Salim 	}
2300a553e4a6SJamal Hadi Salim }
2301a553e4a6SJamal Hadi Salim #endif
2302fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2303fd2ea0a7SDavid S. Miller {
2304e6fce5b9SRobert Olsson 
2305e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2306e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2307e6fce5b9SRobert Olsson 
2308896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2309fd2ea0a7SDavid S. Miller 		__u16 t;
2310fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
231133d7c5e5SAkinobu Mita 			t = prandom_u32() %
2312fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2313fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2314fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2315fd2ea0a7SDavid S. Miller 		} else {
2316fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2317fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2318fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2319fd2ea0a7SDavid S. Miller 		}
2320fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2321fd2ea0a7SDavid S. Miller 	}
2322bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2323fd2ea0a7SDavid S. Miller }
2324fd2ea0a7SDavid S. Miller 
23251da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
23261da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
23271da177e4SLinus Torvalds  */
2328222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2329222f1806SLuiz Capitulino {
23301da177e4SLinus Torvalds 	__u32 imn;
23311da177e4SLinus Torvalds 	__u32 imx;
23321da177e4SLinus Torvalds 	int flow = 0;
23331da177e4SLinus Torvalds 
2334007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2335007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 	/*  Deal with source MAC */
23381da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
23391da177e4SLinus Torvalds 		__u32 mc;
23401da177e4SLinus Torvalds 		__u32 tmp;
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
234333d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->src_mac_count;
23441da177e4SLinus Torvalds 		else {
23451da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2346ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2347222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
23481da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
23491da177e4SLinus Torvalds 		}
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
23521da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
23531da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23541da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
23551da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23561da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
23571da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23581da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
23591da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
23601da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
23611da177e4SLinus Torvalds 	}
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
23641da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
23651da177e4SLinus Torvalds 		__u32 mc;
23661da177e4SLinus Torvalds 		__u32 tmp;
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
236933d7c5e5SAkinobu Mita 			mc = prandom_u32() % pkt_dev->dst_mac_count;
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 		else {
23721da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2373ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2374222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
23751da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
23761da177e4SLinus Torvalds 			}
23771da177e4SLinus Torvalds 		}
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
23801da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
23811da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23821da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
23831da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23841da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
23851da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23861da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
23871da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
23881da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
23891da177e4SLinus Torvalds 	}
23901da177e4SLinus Torvalds 
2391ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
239295c96174SEric Dumazet 		unsigned int i;
2393ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2394ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2395ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
239633d7c5e5SAkinobu Mita 					     ((__force __be32)prandom_u32() &
2397ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2398ca6549afSSteven Whitehouse 	}
2399ca6549afSSteven Whitehouse 
240034954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
240133d7c5e5SAkinobu Mita 		pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
240234954ddcSFrancesco Fondelli 	}
240334954ddcSFrancesco Fondelli 
240434954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
240533d7c5e5SAkinobu Mita 		pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
240634954ddcSFrancesco Fondelli 	}
240734954ddcSFrancesco Fondelli 
24081da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
24091da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
241033d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_src = prandom_u32() %
24115fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
24125fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 		else {
24151da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
24161da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
24171da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
24181da177e4SLinus Torvalds 		}
24191da177e4SLinus Torvalds 	}
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
24221da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
242333d7c5e5SAkinobu Mita 			pkt_dev->cur_udp_dst = prandom_u32() %
24245fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
24255fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2426222f1806SLuiz Capitulino 		} else {
24271da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
24281da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
24291da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
24301da177e4SLinus Torvalds 		}
24311da177e4SLinus Torvalds 	}
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
24341da177e4SLinus Torvalds 
243563adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
243663adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
243763adc6fbSStephen Hemminger 		if (imn < imx) {
24381da177e4SLinus Torvalds 			__u32 t;
24391da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
244033d7c5e5SAkinobu Mita 				t = prandom_u32() % (imx - imn) + imn;
24411da177e4SLinus Torvalds 			else {
24421da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
24431da177e4SLinus Torvalds 				t++;
244463adc6fbSStephen Hemminger 				if (t > imx)
24451da177e4SLinus Torvalds 					t = imn;
244663adc6fbSStephen Hemminger 
24471da177e4SLinus Torvalds 			}
24481da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
24491da177e4SLinus Torvalds 		}
24501da177e4SLinus Torvalds 
2451007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
24521da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
24531da177e4SLinus Torvalds 		} else {
2454252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2455252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2456252e3346SAl Viro 			if (imn < imx) {
24571da177e4SLinus Torvalds 				__u32 t;
2458252e3346SAl Viro 				__be32 s;
24591da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
24601da177e4SLinus Torvalds 
246170e3ba72SAkinobu Mita 					do {
246233d7c5e5SAkinobu Mita 						t = prandom_u32() %
246333d7c5e5SAkinobu Mita 							(imx - imn) + imn;
2464252e3346SAl Viro 						s = htonl(t);
246570e3ba72SAkinobu Mita 					} while (ipv4_is_loopback(s) ||
246670e3ba72SAkinobu Mita 						ipv4_is_multicast(s) ||
246770e3ba72SAkinobu Mita 						ipv4_is_lbcast(s) ||
246870e3ba72SAkinobu Mita 						ipv4_is_zeronet(s) ||
246970e3ba72SAkinobu Mita 						ipv4_is_local_multicast(s));
2470252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2471252e3346SAl Viro 				} else {
24721da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
24731da177e4SLinus Torvalds 					t++;
24741da177e4SLinus Torvalds 					if (t > imx) {
24751da177e4SLinus Torvalds 						t = imn;
24761da177e4SLinus Torvalds 					}
24771da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
24781da177e4SLinus Torvalds 				}
24791da177e4SLinus Torvalds 			}
24801da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2481007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2482222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2483222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2484a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2485a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2486a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2487a553e4a6SJamal Hadi Salim #endif
24881da177e4SLinus Torvalds 				pkt_dev->nflows++;
24891da177e4SLinus Torvalds 			}
24901da177e4SLinus Torvalds 		}
2491222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2492222f1806SLuiz Capitulino 
249306e30411SJoe Perches 		if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
24941da177e4SLinus Torvalds 			int i;
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds 			/* Only random destinations yet */
24971da177e4SLinus Torvalds 
24981da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
24991da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
250033d7c5e5SAkinobu Mita 				    (((__force __be32)prandom_u32() |
25011da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
25021da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
25031da177e4SLinus Torvalds 			}
25041da177e4SLinus Torvalds 		}
25051da177e4SLinus Torvalds 	}
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
25081da177e4SLinus Torvalds 		__u32 t;
25091da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
251033d7c5e5SAkinobu Mita 			t = prandom_u32() %
25115fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
25125fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2513222f1806SLuiz Capitulino 		} else {
25141da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
25151da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
25161da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
25171da177e4SLinus Torvalds 		}
25181da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
25191da177e4SLinus Torvalds 	}
25201da177e4SLinus Torvalds 
2521fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
252245b270f8SRobert Olsson 
25231da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
2526a553e4a6SJamal Hadi Salim 
2527a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
25285537a055SFengguang Wu static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2529cf93d47eSFan Du 
2530cf93d47eSFan Du 	[RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2531cf93d47eSFan Du };
2532cf93d47eSFan Du 
2533a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2534a553e4a6SJamal Hadi Salim {
2535a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2536a553e4a6SJamal Hadi Salim 	int err = 0;
25376de9ace4SFan Du 	struct net *net = dev_net(pkt_dev->odev);
2538a553e4a6SJamal Hadi Salim 
2539a553e4a6SJamal Hadi Salim 	if (!x)
2540a553e4a6SJamal Hadi Salim 		return 0;
2541a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2542a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2543cf93d47eSFan Du 	if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2544a553e4a6SJamal Hadi Salim 		return 0;
2545a553e4a6SJamal Hadi Salim 
2546cf93d47eSFan Du 	/* But when user specify an valid SPI, transformation
2547cf93d47eSFan Du 	 * supports both transport/tunnel mode + ESP/AH type.
2548cf93d47eSFan Du 	 */
2549cf93d47eSFan Du 	if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2550cf93d47eSFan Du 		skb->_skb_refdst = (unsigned long)&pkt_dev->dst | SKB_DST_NOREF;
2551cf93d47eSFan Du 
2552cf93d47eSFan Du 	rcu_read_lock_bh();
255313996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2554cf93d47eSFan Du 	rcu_read_unlock_bh();
25556de9ace4SFan Du 	if (err) {
25566de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2557a553e4a6SJamal Hadi Salim 		goto error;
25586de9ace4SFan Du 	}
2559a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
25606de9ace4SFan Du 	if (err) {
25616de9ace4SFan Du 		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2562a553e4a6SJamal Hadi Salim 		goto error;
25636de9ace4SFan Du 	}
25640af0a413SFan Du 	spin_lock_bh(&x->lock);
2565a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2566a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
25670af0a413SFan Du 	spin_unlock_bh(&x->lock);
2568a553e4a6SJamal Hadi Salim error:
2569a553e4a6SJamal Hadi Salim 	return err;
2570a553e4a6SJamal Hadi Salim }
2571a553e4a6SJamal Hadi Salim 
2572475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2573a553e4a6SJamal Hadi Salim {
2574a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2575a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2576d6182223SPaul Gortmaker 		int i;
2577d6182223SPaul Gortmaker 		for (i = 0; i < pkt_dev->cflows; i++) {
2578a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2579a553e4a6SJamal Hadi Salim 			if (x) {
2580a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2581a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2582a553e4a6SJamal Hadi Salim 			}
2583a553e4a6SJamal Hadi Salim 		}
2584a553e4a6SJamal Hadi Salim 	}
2585a553e4a6SJamal Hadi Salim }
2586a553e4a6SJamal Hadi Salim 
2587475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2588a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2589a553e4a6SJamal Hadi Salim {
2590a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2591a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2592a553e4a6SJamal Hadi Salim 		int nhead = 0;
2593a553e4a6SJamal Hadi Salim 		if (x) {
2594a553e4a6SJamal Hadi Salim 			int ret;
2595a553e4a6SJamal Hadi Salim 			__u8 *eth;
25963868204dSfan.du 			struct iphdr *iph;
25973868204dSfan.du 
2598a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2599a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2600a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2601a553e4a6SJamal Hadi Salim 				if (ret < 0) {
2602f9467eaeSJoe Perches 					pr_err("Error expanding ipsec packet %d\n",
2603f9467eaeSJoe Perches 					       ret);
2604b4bb4ac8SIlpo Järvinen 					goto err;
2605a553e4a6SJamal Hadi Salim 				}
2606a553e4a6SJamal Hadi Salim 			}
2607a553e4a6SJamal Hadi Salim 
2608a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2609a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2610a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2611a553e4a6SJamal Hadi Salim 			if (ret) {
2612f9467eaeSJoe Perches 				pr_err("Error creating ipsec packet %d\n", ret);
2613b4bb4ac8SIlpo Järvinen 				goto err;
2614a553e4a6SJamal Hadi Salim 			}
2615a553e4a6SJamal Hadi Salim 			/* restore ll */
2616a553e4a6SJamal Hadi Salim 			eth = (__u8 *) skb_push(skb, ETH_HLEN);
2617a553e4a6SJamal Hadi Salim 			memcpy(eth, pkt_dev->hh, 12);
2618a553e4a6SJamal Hadi Salim 			*(u16 *) &eth[12] = protocol;
26193868204dSfan.du 
26203868204dSfan.du 			/* Update IPv4 header len as well as checksum value */
26213868204dSfan.du 			iph = ip_hdr(skb);
26223868204dSfan.du 			iph->tot_len = htons(skb->len - ETH_HLEN);
26233868204dSfan.du 			ip_send_check(iph);
2624a553e4a6SJamal Hadi Salim 		}
2625a553e4a6SJamal Hadi Salim 	}
2626a553e4a6SJamal Hadi Salim 	return 1;
2627b4bb4ac8SIlpo Järvinen err:
2628b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2629b4bb4ac8SIlpo Järvinen 	return 0;
2630a553e4a6SJamal Hadi Salim }
2631a553e4a6SJamal Hadi Salim #endif
2632a553e4a6SJamal Hadi Salim 
2633ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2634ca6549afSSteven Whitehouse {
263595c96174SEric Dumazet 	unsigned int i;
263663adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2637ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
263863adc6fbSStephen Hemminger 
2639ca6549afSSteven Whitehouse 	mpls--;
2640ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2641ca6549afSSteven Whitehouse }
2642ca6549afSSteven Whitehouse 
26430f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
26440f37c605SAl Viro 			       unsigned int prio)
26450f37c605SAl Viro {
26460f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
26470f37c605SAl Viro }
26480f37c605SAl Viro 
264926ad7879SEric Dumazet static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
265026ad7879SEric Dumazet 				int datalen)
265126ad7879SEric Dumazet {
265226ad7879SEric Dumazet 	struct timeval timestamp;
265326ad7879SEric Dumazet 	struct pktgen_hdr *pgh;
265426ad7879SEric Dumazet 
265526ad7879SEric Dumazet 	pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
265626ad7879SEric Dumazet 	datalen -= sizeof(*pgh);
265726ad7879SEric Dumazet 
265826ad7879SEric Dumazet 	if (pkt_dev->nfrags <= 0) {
265905aebe2eSDaniel Turull 		memset(skb_put(skb, datalen), 0, datalen);
266026ad7879SEric Dumazet 	} else {
266126ad7879SEric Dumazet 		int frags = pkt_dev->nfrags;
266226ad7879SEric Dumazet 		int i, len;
26637d36a991Samit salecha 		int frag_len;
266426ad7879SEric Dumazet 
266526ad7879SEric Dumazet 
266626ad7879SEric Dumazet 		if (frags > MAX_SKB_FRAGS)
266726ad7879SEric Dumazet 			frags = MAX_SKB_FRAGS;
266826ad7879SEric Dumazet 		len = datalen - frags * PAGE_SIZE;
266926ad7879SEric Dumazet 		if (len > 0) {
267026ad7879SEric Dumazet 			memset(skb_put(skb, len), 0, len);
267126ad7879SEric Dumazet 			datalen = frags * PAGE_SIZE;
267226ad7879SEric Dumazet 		}
267326ad7879SEric Dumazet 
267426ad7879SEric Dumazet 		i = 0;
26757d36a991Samit salecha 		frag_len = (datalen/frags) < PAGE_SIZE ?
26767d36a991Samit salecha 			   (datalen/frags) : PAGE_SIZE;
267726ad7879SEric Dumazet 		while (datalen > 0) {
267826ad7879SEric Dumazet 			if (unlikely(!pkt_dev->page)) {
267926ad7879SEric Dumazet 				int node = numa_node_id();
268026ad7879SEric Dumazet 
268126ad7879SEric Dumazet 				if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
268226ad7879SEric Dumazet 					node = pkt_dev->node;
268326ad7879SEric Dumazet 				pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
268426ad7879SEric Dumazet 				if (!pkt_dev->page)
268526ad7879SEric Dumazet 					break;
268626ad7879SEric Dumazet 			}
2687a0bec1cdSIan Campbell 			get_page(pkt_dev->page);
2688ea2ab693SIan Campbell 			skb_frag_set_page(skb, i, pkt_dev->page);
268926ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page_offset = 0;
26907d36a991Samit salecha 			/*last fragment, fill rest of data*/
26917d36a991Samit salecha 			if (i == (frags - 1))
26929e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i],
26939e903e08SEric Dumazet 				    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
26947d36a991Samit salecha 			else
26959e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
26969e903e08SEric Dumazet 			datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
26979e903e08SEric Dumazet 			skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
26989e903e08SEric Dumazet 			skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
269926ad7879SEric Dumazet 			i++;
270026ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
270126ad7879SEric Dumazet 		}
270226ad7879SEric Dumazet 	}
270326ad7879SEric Dumazet 
270426ad7879SEric Dumazet 	/* Stamp the time, and sequence number,
270526ad7879SEric Dumazet 	 * convert them to network byte order
270626ad7879SEric Dumazet 	 */
270726ad7879SEric Dumazet 	pgh->pgh_magic = htonl(PKTGEN_MAGIC);
270826ad7879SEric Dumazet 	pgh->seq_num = htonl(pkt_dev->seq_num);
270926ad7879SEric Dumazet 
2710afb84b62SJesper Dangaard Brouer 	if (pkt_dev->flags & F_NO_TIMESTAMP) {
2711afb84b62SJesper Dangaard Brouer 		pgh->tv_sec = 0;
2712afb84b62SJesper Dangaard Brouer 		pgh->tv_usec = 0;
2713afb84b62SJesper Dangaard Brouer 	} else {
271426ad7879SEric Dumazet 		do_gettimeofday(&timestamp);
271526ad7879SEric Dumazet 		pgh->tv_sec = htonl(timestamp.tv_sec);
271626ad7879SEric Dumazet 		pgh->tv_usec = htonl(timestamp.tv_usec);
271726ad7879SEric Dumazet 	}
2718afb84b62SJesper Dangaard Brouer }
271926ad7879SEric Dumazet 
27207a6e288dSDaniel Borkmann static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
27217a6e288dSDaniel Borkmann 					struct pktgen_dev *pkt_dev,
27227a6e288dSDaniel Borkmann 					unsigned int extralen)
27237a6e288dSDaniel Borkmann {
27247a6e288dSDaniel Borkmann 	struct sk_buff *skb = NULL;
27257a6e288dSDaniel Borkmann 	unsigned int size = pkt_dev->cur_pkt_size + 64 + extralen +
27267a6e288dSDaniel Borkmann 			    pkt_dev->pkt_overhead;
27277a6e288dSDaniel Borkmann 
27287a6e288dSDaniel Borkmann 	if (pkt_dev->flags & F_NODE) {
27297a6e288dSDaniel Borkmann 		int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
27307a6e288dSDaniel Borkmann 
27317a6e288dSDaniel Borkmann 		skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
27327a6e288dSDaniel Borkmann 		if (likely(skb)) {
27337a6e288dSDaniel Borkmann 			skb_reserve(skb, NET_SKB_PAD);
27347a6e288dSDaniel Borkmann 			skb->dev = dev;
27357a6e288dSDaniel Borkmann 		}
27367a6e288dSDaniel Borkmann 	} else {
27377a6e288dSDaniel Borkmann 		 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
27387a6e288dSDaniel Borkmann 	}
27397a6e288dSDaniel Borkmann 
27407a6e288dSDaniel Borkmann 	return skb;
27417a6e288dSDaniel Borkmann }
27427a6e288dSDaniel Borkmann 
27431da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
27441da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
27451da177e4SLinus Torvalds {
27461da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
27471da177e4SLinus Torvalds 	__u8 *eth;
27481da177e4SLinus Torvalds 	struct udphdr *udph;
27491da177e4SLinus Torvalds 	int datalen, iplen;
27501da177e4SLinus Torvalds 	struct iphdr *iph;
2751d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2752ca6549afSSteven Whitehouse 	__be32 *mpls;
275334954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
275434954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
275534954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
275634954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2757fd2ea0a7SDavid S. Miller 	u16 queue_map;
2758ca6549afSSteven Whitehouse 
2759ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2760d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
27611da177e4SLinus Torvalds 
276234954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2763d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
276434954ddcSFrancesco Fondelli 
276564053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
276664053beeSRobert Olsson 	 * fields.
276764053beeSRobert Olsson 	 */
276864053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2769eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
277064053beeSRobert Olsson 
27717ac5459eSDavid S. Miller 	datalen = (odev->hard_header_len + 16) & ~0xf;
2772e99b99b4SRobert Olsson 
27737a6e288dSDaniel Borkmann 	skb = pktgen_alloc_skb(odev, pkt_dev, datalen);
27741da177e4SLinus Torvalds 	if (!skb) {
27751da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
27761da177e4SLinus Torvalds 		return NULL;
27771da177e4SLinus Torvalds 	}
27781da177e4SLinus Torvalds 
27797a6e288dSDaniel Borkmann 	prefetchw(skb->data);
27807ac5459eSDavid S. Miller 	skb_reserve(skb, datalen);
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
27831da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2784ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2785ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2786ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
278734954ddcSFrancesco Fondelli 
278834954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
278934954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
279034954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27910f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
27920f37c605SAl Viro 					       pkt_dev->svlan_cfi,
27930f37c605SAl Viro 					       pkt_dev->svlan_p);
279434954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2795d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
279634954ddcSFrancesco Fondelli 		}
279734954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27980f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
27990f37c605SAl Viro 				      pkt_dev->vlan_cfi,
28000f37c605SAl Viro 				      pkt_dev->vlan_p);
280134954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2802d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
280334954ddcSFrancesco Fondelli 	}
280434954ddcSFrancesco Fondelli 
2805525cebedSThomas Graf 	skb_set_mac_header(skb, 0);
2806525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2807525cebedSThomas Graf 	iph = (struct iphdr *) skb_put(skb, sizeof(struct iphdr));
2808525cebedSThomas Graf 
2809525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
2810525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
2811fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
28129e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
28139e50e3acSJohn Fastabend 
28141da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2815252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
28161da177e4SLinus Torvalds 
2817ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2818ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
281916dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
28206af773e7SNishank Trivedi 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
28211da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
28241da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
28251da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
2826c26bf4a5SThomas Graf 	udph->check = 0;
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds 	iph->ihl = 5;
28291da177e4SLinus Torvalds 	iph->version = 4;
28301da177e4SLinus Torvalds 	iph->ttl = 32;
28311ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
28321da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
28331da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
28341da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
283566ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
283666ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
28371da177e4SLinus Torvalds 	iph->frag_off = 0;
28381da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
28391da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
284003c633e7SThomas Graf 	ip_send_check(iph);
2841ca6549afSSteven Whitehouse 	skb->protocol = protocol;
28421da177e4SLinus Torvalds 	skb->dev = odev;
28431da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
2844c26bf4a5SThomas Graf 
2845c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2846c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2847c26bf4a5SThomas Graf 	} else if (odev->features & NETIF_F_V4_CSUM) {
2848c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2849c26bf4a5SThomas Graf 		skb->csum = 0;
2850c26bf4a5SThomas Graf 		udp4_hwcsum(skb, udph->source, udph->dest);
2851c26bf4a5SThomas Graf 	} else {
2852c26bf4a5SThomas Graf 		__wsum csum = udp_csum(skb);
2853c26bf4a5SThomas Graf 
2854c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
2855c26bf4a5SThomas Graf 		udph->check = csum_tcpudp_magic(udph->source, udph->dest,
2856c26bf4a5SThomas Graf 						datalen + 8, IPPROTO_UDP, csum);
2857c26bf4a5SThomas Graf 
2858c26bf4a5SThomas Graf 		if (udph->check == 0)
2859c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2860c26bf4a5SThomas Graf 	}
2861c26bf4a5SThomas Graf 
286226ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
28631da177e4SLinus Torvalds 
2864a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2865a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2866a553e4a6SJamal Hadi Salim 		return NULL;
2867a553e4a6SJamal Hadi Salim #endif
2868a553e4a6SJamal Hadi Salim 
28691da177e4SLinus Torvalds 	return skb;
28701da177e4SLinus Torvalds }
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
28731da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
28741da177e4SLinus Torvalds {
28751da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
28761da177e4SLinus Torvalds 	__u8 *eth;
28771da177e4SLinus Torvalds 	struct udphdr *udph;
2878c26bf4a5SThomas Graf 	int datalen, udplen;
28791da177e4SLinus Torvalds 	struct ipv6hdr *iph;
2880d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2881ca6549afSSteven Whitehouse 	__be32 *mpls;
288234954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
288334954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
288434954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
288534954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2886fd2ea0a7SDavid S. Miller 	u16 queue_map;
2887ca6549afSSteven Whitehouse 
2888ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2889d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
28901da177e4SLinus Torvalds 
289134954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2892d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
289334954ddcSFrancesco Fondelli 
289464053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
289564053beeSRobert Olsson 	 * fields.
289664053beeSRobert Olsson 	 */
289764053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2898eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
289964053beeSRobert Olsson 
29007a6e288dSDaniel Borkmann 	skb = pktgen_alloc_skb(odev, pkt_dev, 16);
29011da177e4SLinus Torvalds 	if (!skb) {
29021da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
29031da177e4SLinus Torvalds 		return NULL;
29041da177e4SLinus Torvalds 	}
29051da177e4SLinus Torvalds 
29067a6e288dSDaniel Borkmann 	prefetchw(skb->data);
29071da177e4SLinus Torvalds 	skb_reserve(skb, 16);
29081da177e4SLinus Torvalds 
29091da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
29101da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2911ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2912ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2913ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
291434954ddcSFrancesco Fondelli 
291534954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
291634954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
291734954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29180f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
29190f37c605SAl Viro 					       pkt_dev->svlan_cfi,
29200f37c605SAl Viro 					       pkt_dev->svlan_p);
292134954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2922d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
292334954ddcSFrancesco Fondelli 		}
292434954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29250f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
29260f37c605SAl Viro 				      pkt_dev->vlan_cfi,
29270f37c605SAl Viro 				      pkt_dev->vlan_p);
292834954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2929d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
293034954ddcSFrancesco Fondelli 	}
293134954ddcSFrancesco Fondelli 
2932525cebedSThomas Graf 	skb_set_mac_header(skb, 0);
2933525cebedSThomas Graf 	skb_set_network_header(skb, skb->len);
2934525cebedSThomas Graf 	iph = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
2935525cebedSThomas Graf 
2936525cebedSThomas Graf 	skb_set_transport_header(skb, skb->len);
2937525cebedSThomas Graf 	udph = (struct udphdr *) skb_put(skb, sizeof(struct udphdr));
2938fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
29399e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
29401da177e4SLinus Torvalds 
29411da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2942252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
29431da177e4SLinus Torvalds 
2944ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2945ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
2946ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
294716dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
29481da177e4SLinus Torvalds 
29495aa8b572SAmerigo Wang 	if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
29501da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
2951e87cc472SJoe Perches 		net_info_ratelimited("increased datalen to %d\n", datalen);
29521da177e4SLinus Torvalds 	}
29531da177e4SLinus Torvalds 
2954c26bf4a5SThomas Graf 	udplen = datalen + sizeof(struct udphdr);
29551da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
29561da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
2957c26bf4a5SThomas Graf 	udph->len = htons(udplen);
2958c26bf4a5SThomas Graf 	udph->check = 0;
29591da177e4SLinus Torvalds 
2960d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
29611da177e4SLinus Torvalds 
29621ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
29631ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
2964252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
29651ca7768cSFrancesco Fondelli 	}
29661ca7768cSFrancesco Fondelli 
29671da177e4SLinus Torvalds 	iph->hop_limit = 32;
29681da177e4SLinus Torvalds 
2969c26bf4a5SThomas Graf 	iph->payload_len = htons(udplen);
29701da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
29711da177e4SLinus Torvalds 
29724e3fd7a0SAlexey Dobriyan 	iph->daddr = pkt_dev->cur_in6_daddr;
29734e3fd7a0SAlexey Dobriyan 	iph->saddr = pkt_dev->cur_in6_saddr;
29741da177e4SLinus Torvalds 
2975ca6549afSSteven Whitehouse 	skb->protocol = protocol;
29761da177e4SLinus Torvalds 	skb->dev = odev;
29771da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
29781da177e4SLinus Torvalds 
2979c26bf4a5SThomas Graf 	if (!(pkt_dev->flags & F_UDPCSUM)) {
2980c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_NONE;
2981c26bf4a5SThomas Graf 	} else if (odev->features & NETIF_F_V6_CSUM) {
2982c26bf4a5SThomas Graf 		skb->ip_summed = CHECKSUM_PARTIAL;
2983c26bf4a5SThomas Graf 		skb->csum_start = skb_transport_header(skb) - skb->head;
2984c26bf4a5SThomas Graf 		skb->csum_offset = offsetof(struct udphdr, check);
2985c26bf4a5SThomas Graf 		udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2986c26bf4a5SThomas Graf 	} else {
2987c26bf4a5SThomas Graf 		__wsum csum = udp_csum(skb);
2988c26bf4a5SThomas Graf 
2989c26bf4a5SThomas Graf 		/* add protocol-dependent pseudo-header */
2990c26bf4a5SThomas Graf 		udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2991c26bf4a5SThomas Graf 
2992c26bf4a5SThomas Graf 		if (udph->check == 0)
2993c26bf4a5SThomas Graf 			udph->check = CSUM_MANGLED_0;
2994c26bf4a5SThomas Graf 	}
2995c26bf4a5SThomas Graf 
299626ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
29971da177e4SLinus Torvalds 
29981da177e4SLinus Torvalds 	return skb;
29991da177e4SLinus Torvalds }
30001da177e4SLinus Torvalds 
3001475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
30021da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
30031da177e4SLinus Torvalds {
30041da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
30051da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
30061da177e4SLinus Torvalds 	else
30071da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
30081da177e4SLinus Torvalds }
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
30111da177e4SLinus Torvalds {
30121da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
30131da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
30141da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
30151da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
30161da177e4SLinus Torvalds 	pkt_dev->errors = 0;
30171da177e4SLinus Torvalds }
30181da177e4SLinus Torvalds 
30191da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
30221da177e4SLinus Torvalds {
3023c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
30241da177e4SLinus Torvalds 	int started = 0;
30251da177e4SLinus Torvalds 
3026f9467eaeSJoe Perches 	func_enter();
30271da177e4SLinus Torvalds 
30288788370aSJesper Dangaard Brouer 	rcu_read_lock();
30298788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds 		/*
30321da177e4SLinus Torvalds 		 * setup odev and create initial packet.
30331da177e4SLinus Torvalds 		 */
30341da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 		if (pkt_dev->odev) {
30371da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
30381da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
3039398f382cSDaniel Borkmann 			pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3040fd29cf72SStephen Hemminger 
304116dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
30448788370aSJesper Dangaard Brouer 			pkt_dev->running = 1;	/* Cranke yeself! */
30451da177e4SLinus Torvalds 			started++;
3046222f1806SLuiz Capitulino 		} else
30471da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
30481da177e4SLinus Torvalds 	}
30498788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3050222f1806SLuiz Capitulino 	if (started)
3051222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
30544e58a027SCong Wang static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
30551da177e4SLinus Torvalds {
3056cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30571da177e4SLinus Torvalds 
3058f9467eaeSJoe Perches 	func_enter();
30591da177e4SLinus Torvalds 
30606146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3061cdcdbe0bSLuiz Capitulino 
30624e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
306395ed63f7SArthur Kepner 		t->control |= T_STOP;
3064cdcdbe0bSLuiz Capitulino 
30656146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
30661da177e4SLinus Torvalds }
30671da177e4SLinus Torvalds 
3068648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
30691da177e4SLinus Torvalds {
3070648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
30711da177e4SLinus Torvalds 
30728788370aSJesper Dangaard Brouer 	rcu_read_lock();
30738788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
30748788370aSJesper Dangaard Brouer 		if (pkt_dev->running) {
30758788370aSJesper Dangaard Brouer 			rcu_read_unlock();
3076648fda74SStephen Hemminger 			return 1;
30778788370aSJesper Dangaard Brouer 		}
30788788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3079648fda74SStephen Hemminger 	return 0;
30801da177e4SLinus Torvalds }
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
30831da177e4SLinus Torvalds {
30841da177e4SLinus Torvalds 	while (thread_is_running(t)) {
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds 		msleep_interruptible(100);
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds 		if (signal_pending(current))
30891da177e4SLinus Torvalds 			goto signal;
30901da177e4SLinus Torvalds 	}
30911da177e4SLinus Torvalds 	return 1;
30921da177e4SLinus Torvalds signal:
30931da177e4SLinus Torvalds 	return 0;
30941da177e4SLinus Torvalds }
30951da177e4SLinus Torvalds 
30964e58a027SCong Wang static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
30971da177e4SLinus Torvalds {
3098cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
30991da177e4SLinus Torvalds 	int sig = 1;
31001da177e4SLinus Torvalds 
31016146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3102cdcdbe0bSLuiz Capitulino 
31034e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list) {
31041da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
3105222f1806SLuiz Capitulino 		if (sig == 0)
3106222f1806SLuiz Capitulino 			break;
31071da177e4SLinus Torvalds 	}
3108cdcdbe0bSLuiz Capitulino 
3109cdcdbe0bSLuiz Capitulino 	if (sig == 0)
31104e58a027SCong Wang 		list_for_each_entry(t, &pn->pktgen_threads, th_list)
31111da177e4SLinus Torvalds 			t->control |= (T_STOP);
3112cdcdbe0bSLuiz Capitulino 
31136146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31141da177e4SLinus Torvalds 	return sig;
31151da177e4SLinus Torvalds }
31161da177e4SLinus Torvalds 
31174e58a027SCong Wang static void pktgen_run_all_threads(struct pktgen_net *pn)
31181da177e4SLinus Torvalds {
3119cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31201da177e4SLinus Torvalds 
3121f9467eaeSJoe Perches 	func_enter();
31221da177e4SLinus Torvalds 
31236146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
31241da177e4SLinus Torvalds 
31254e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
31261da177e4SLinus Torvalds 		t->control |= (T_RUN);
3127cdcdbe0bSLuiz Capitulino 
31286146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31291da177e4SLinus Torvalds 
313063adc6fbSStephen Hemminger 	/* Propagate thread->control  */
313163adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
31321da177e4SLinus Torvalds 
31334e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
31341da177e4SLinus Torvalds }
31351da177e4SLinus Torvalds 
31364e58a027SCong Wang static void pktgen_reset_all_threads(struct pktgen_net *pn)
3137eb37b41cSJesse Brandeburg {
3138eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3139eb37b41cSJesse Brandeburg 
3140f9467eaeSJoe Perches 	func_enter();
3141eb37b41cSJesse Brandeburg 
3142eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3143eb37b41cSJesse Brandeburg 
31444e58a027SCong Wang 	list_for_each_entry(t, &pn->pktgen_threads, th_list)
3145eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3146eb37b41cSJesse Brandeburg 
3147eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3148eb37b41cSJesse Brandeburg 
314963adc6fbSStephen Hemminger 	/* Propagate thread->control  */
315063adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3151eb37b41cSJesse Brandeburg 
31524e58a027SCong Wang 	pktgen_wait_all_threads_run(pn);
3153eb37b41cSJesse Brandeburg }
3154eb37b41cSJesse Brandeburg 
31551da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
31561da177e4SLinus Torvalds {
3157fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
31581da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3159fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3160fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3161fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
31621da177e4SLinus Torvalds 
316303a14ab1SDaniel Turull 	p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3164fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3165fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3166fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
31671da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
31681da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
31691da177e4SLinus Torvalds 
3170fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3171fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
31741da177e4SLinus Torvalds 
31751da177e4SLinus Torvalds 	mbps = bps;
31761da177e4SLinus Torvalds 	do_div(mbps, 1000000);
31771da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
31781da177e4SLinus Torvalds 		     (unsigned long long)pps,
31791da177e4SLinus Torvalds 		     (unsigned long long)mbps,
31801da177e4SLinus Torvalds 		     (unsigned long long)bps,
31811da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
31821da177e4SLinus Torvalds }
31831da177e4SLinus Torvalds 
31841da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
31851da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
31861da177e4SLinus Torvalds {
3187222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
31881da177e4SLinus Torvalds 
31891da177e4SLinus Torvalds 	if (!pkt_dev->running) {
3190294a0b7fSJoe Perches 		pr_warn("interface: %s is already stopped\n",
3191f9467eaeSJoe Perches 			pkt_dev->odevname);
31921da177e4SLinus Torvalds 		return -EINVAL;
31931da177e4SLinus Torvalds 	}
31941da177e4SLinus Torvalds 
31958788370aSJesper Dangaard Brouer 	pkt_dev->running = 0;
31963bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
31973bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3198398f382cSDaniel Borkmann 	pkt_dev->stopped_at = ktime_get();
31991da177e4SLinus Torvalds 
320095ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
32011da177e4SLinus Torvalds 
32021da177e4SLinus Torvalds 	return 0;
32031da177e4SLinus Torvalds }
32041da177e4SLinus Torvalds 
32051da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
32061da177e4SLinus Torvalds {
3207c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
32081da177e4SLinus Torvalds 
32098788370aSJesper Dangaard Brouer 	rcu_read_lock();
32108788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3211c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3212222f1806SLuiz Capitulino 			continue;
3213222f1806SLuiz Capitulino 		if (best == NULL)
3214c26a8016SLuiz Capitulino 			best = pkt_dev;
3215398f382cSDaniel Borkmann 		else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3216c26a8016SLuiz Capitulino 			best = pkt_dev;
32171da177e4SLinus Torvalds 	}
32188788370aSJesper Dangaard Brouer 	rcu_read_unlock();
32198788370aSJesper Dangaard Brouer 
32201da177e4SLinus Torvalds 	return best;
32211da177e4SLinus Torvalds }
32221da177e4SLinus Torvalds 
3223222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3224222f1806SLuiz Capitulino {
3225c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
32261da177e4SLinus Torvalds 
3227f9467eaeSJoe Perches 	func_enter();
32281da177e4SLinus Torvalds 
32298788370aSJesper Dangaard Brouer 	rcu_read_lock();
32301da177e4SLinus Torvalds 
32318788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3232c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
323395ed63f7SArthur Kepner 	}
323495ed63f7SArthur Kepner 
32358788370aSJesper Dangaard Brouer 	rcu_read_unlock();
323695ed63f7SArthur Kepner }
323795ed63f7SArthur Kepner 
323895ed63f7SArthur Kepner /*
323995ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
324095ed63f7SArthur Kepner  * and remove it
324195ed63f7SArthur Kepner  */
324295ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
324395ed63f7SArthur Kepner {
3244c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3245c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
324695ed63f7SArthur Kepner 
3247f9467eaeSJoe Perches 	func_enter();
324895ed63f7SArthur Kepner 
3249c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3250c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
325195ed63f7SArthur Kepner 
3252222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3253222f1806SLuiz Capitulino 			continue;
325495ed63f7SArthur Kepner 
325595ed63f7SArthur Kepner 		kfree_skb(cur->skb);
325695ed63f7SArthur Kepner 		cur->skb = NULL;
325795ed63f7SArthur Kepner 
325895ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
325995ed63f7SArthur Kepner 
326095ed63f7SArthur Kepner 		break;
326195ed63f7SArthur Kepner 	}
32621da177e4SLinus Torvalds }
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
32651da177e4SLinus Torvalds {
3266c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3267c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
32681da177e4SLinus Torvalds 
3269f9467eaeSJoe Perches 	func_enter();
3270f9467eaeSJoe Perches 
32711da177e4SLinus Torvalds 	/* Remove all devices, free mem */
32721da177e4SLinus Torvalds 
3273c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3274c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
327595ed63f7SArthur Kepner 
327695ed63f7SArthur Kepner 		kfree_skb(cur->skb);
327795ed63f7SArthur Kepner 		cur->skb = NULL;
327895ed63f7SArthur Kepner 
32791da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
32801da177e4SLinus Torvalds 	}
32811da177e4SLinus Torvalds }
32821da177e4SLinus Torvalds 
32831da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
32841da177e4SLinus Torvalds {
32851da177e4SLinus Torvalds 	/* Remove from the thread list */
32864e58a027SCong Wang 	remove_proc_entry(t->tsk->comm, t->net->proc_dir);
32871da177e4SLinus Torvalds }
32881da177e4SLinus Torvalds 
3289ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
32903791decbSStephen Hemminger {
3291398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
32923791decbSStephen Hemminger 	schedule();
3293398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
32943791decbSStephen Hemminger }
32953791decbSStephen Hemminger 
3296ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3297ef87979cSStephen Hemminger {
3298398f382cSDaniel Borkmann 	ktime_t idle_start = ktime_get();
3299ef87979cSStephen Hemminger 
3300ef87979cSStephen Hemminger 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3301ef87979cSStephen Hemminger 		if (signal_pending(current))
3302ef87979cSStephen Hemminger 			break;
3303ef87979cSStephen Hemminger 
3304ef87979cSStephen Hemminger 		if (need_resched())
3305ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3306ef87979cSStephen Hemminger 		else
3307ef87979cSStephen Hemminger 			cpu_relax();
3308ef87979cSStephen Hemminger 	}
3309398f382cSDaniel Borkmann 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3310ef87979cSStephen Hemminger }
3311fd29cf72SStephen Hemminger 
3312475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
33131da177e4SLinus Torvalds {
3314*38b2cf29SAlexei Starovoitov 	unsigned int burst = ACCESS_ONCE(pkt_dev->burst);
331500829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
3316fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
33171da177e4SLinus Torvalds 	int ret;
33181da177e4SLinus Torvalds 
3319ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3320ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
33211da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
33223791decbSStephen Hemminger 		return;
33231da177e4SLinus Torvalds 	}
33241da177e4SLinus Torvalds 
3325ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3326ef87979cSStephen Hemminger 	 * "never transmit"
3327ef87979cSStephen Hemminger 	 */
3328ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3329398f382cSDaniel Borkmann 		pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3330ef87979cSStephen Hemminger 		return;
3331ef87979cSStephen Hemminger 	}
3332ef87979cSStephen Hemminger 
3333ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
33347d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
33357d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
33361da177e4SLinus Torvalds 		/* build a new pkt */
33371da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
33381da177e4SLinus Torvalds 
33391da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
33401da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
3341f9467eaeSJoe Perches 			pr_err("ERROR: couldn't allocate skb in fill_packet\n");
33421da177e4SLinus Torvalds 			schedule();
33431da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
33443791decbSStephen Hemminger 			return;
33451da177e4SLinus Torvalds 		}
3346baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
33471da177e4SLinus Torvalds 		pkt_dev->allocated_skbs++;
33481da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
33491da177e4SLinus Torvalds 	}
33501da177e4SLinus Torvalds 
3351ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3352ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3353ef87979cSStephen Hemminger 
335410c51b56SDaniel Borkmann 	txq = skb_get_tx_queue(odev, pkt_dev->skb);
3355fd2ea0a7SDavid S. Miller 
33560f2eea4bSDaniel Borkmann 	local_bh_disable();
33570f2eea4bSDaniel Borkmann 
33580f2eea4bSDaniel Borkmann 	HARD_TX_LOCK(odev, txq, smp_processor_id());
33595b8db2f5SStephen Hemminger 
33606f25cd47SDaniel Borkmann 	if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3361ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
33620835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
33630835acfeSEric Dumazet 		goto unlock;
33640835acfeSEric Dumazet 	}
3365*38b2cf29SAlexei Starovoitov 	atomic_add(burst, &pkt_dev->skb->users);
3366*38b2cf29SAlexei Starovoitov 
3367*38b2cf29SAlexei Starovoitov xmit_more:
3368*38b2cf29SAlexei Starovoitov 	ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
3369ef87979cSStephen Hemminger 
33705b8db2f5SStephen Hemminger 	switch (ret) {
33715b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
33721da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
33731da177e4SLinus Torvalds 		pkt_dev->sofar++;
33741da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3375baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3376*38b2cf29SAlexei Starovoitov 		if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3377*38b2cf29SAlexei Starovoitov 			goto xmit_more;
33785b8db2f5SStephen Hemminger 		break;
3379f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3380f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3381f466dba1SJohn Fastabend 	case NET_XMIT_POLICED:
3382f466dba1SJohn Fastabend 		/* skb has been consumed */
3383f466dba1SJohn Fastabend 		pkt_dev->errors++;
3384f466dba1SJohn Fastabend 		break;
33855b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
3386e87cc472SJoe Perches 		net_info_ratelimited("%s xmit error: %d\n",
3387e87cc472SJoe Perches 				     pkt_dev->odevname, ret);
33881da177e4SLinus Torvalds 		pkt_dev->errors++;
33895b8db2f5SStephen Hemminger 		/* fallthru */
3390ef87979cSStephen Hemminger 	case NETDEV_TX_LOCKED:
33915b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
33925b8db2f5SStephen Hemminger 		/* Retry it next time */
33935b8db2f5SStephen Hemminger 		atomic_dec(&(pkt_dev->skb->users));
33941da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
33951da177e4SLinus Torvalds 	}
3396*38b2cf29SAlexei Starovoitov 	if (unlikely(burst))
3397*38b2cf29SAlexei Starovoitov 		atomic_sub(burst, &pkt_dev->skb->users);
33980835acfeSEric Dumazet unlock:
33990f2eea4bSDaniel Borkmann 	HARD_TX_UNLOCK(odev, txq);
34000f2eea4bSDaniel Borkmann 
34010f2eea4bSDaniel Borkmann 	local_bh_enable();
34021da177e4SLinus Torvalds 
34031da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
34041da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3405ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
34061da177e4SLinus Torvalds 
34071da177e4SLinus Torvalds 		/* Done with this */
34081da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
34091da177e4SLinus Torvalds 	}
34101da177e4SLinus Torvalds }
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds /*
34131da177e4SLinus Torvalds  * Main loop of the thread goes here
34141da177e4SLinus Torvalds  */
34151da177e4SLinus Torvalds 
3416ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
34171da177e4SLinus Torvalds {
34181da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3419ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
34201da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
34211da177e4SLinus Torvalds 	int cpu = t->cpu;
34221da177e4SLinus Torvalds 
3423ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
34241da177e4SLinus Torvalds 
34251da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3426d3ede327SDenis V. Lunev 	complete(&t->start_done);
34271da177e4SLinus Torvalds 
3428f9467eaeSJoe Perches 	pr_debug("starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
34291da177e4SLinus Torvalds 
343083144186SRafael J. Wysocki 	set_freezable();
343183144186SRafael J. Wysocki 
3432baac167bSJesper Dangaard Brouer 	__set_current_state(TASK_RUNNING);
3433baac167bSJesper Dangaard Brouer 
3434ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3435ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3436ee74baa7SDavid S. Miller 
3437ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
34384e58a027SCong Wang 			if (t->net->pktgen_exiting)
3439551eaff1SEric Dumazet 				break;
3440ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3441ef87979cSStephen Hemminger 							 t->control != 0,
3442ef87979cSStephen Hemminger 							 HZ/10);
34431b3f720bSRafael J. Wysocki 			try_to_freeze();
3444ef87979cSStephen Hemminger 			continue;
3445ee74baa7SDavid S. Miller 		}
34461da177e4SLinus Torvalds 
3447ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
34481da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
34491da177e4SLinus Torvalds 
3450ef87979cSStephen Hemminger 			if (need_resched())
3451ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3452ef87979cSStephen Hemminger 			else
3453ef87979cSStephen Hemminger 				cpu_relax();
3454ef87979cSStephen Hemminger 		}
3455ef87979cSStephen Hemminger 
34561da177e4SLinus Torvalds 		if (t->control & T_STOP) {
34571da177e4SLinus Torvalds 			pktgen_stop(t);
34581da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
34591da177e4SLinus Torvalds 		}
34601da177e4SLinus Torvalds 
34611da177e4SLinus Torvalds 		if (t->control & T_RUN) {
34621da177e4SLinus Torvalds 			pktgen_run(t);
34631da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
34641da177e4SLinus Torvalds 		}
34651da177e4SLinus Torvalds 
346695ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
34671da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
346895ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
346995ed63f7SArthur Kepner 		}
347095ed63f7SArthur Kepner 
347195ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
347295ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
34731da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
34741da177e4SLinus Torvalds 		}
34751da177e4SLinus Torvalds 
347609fe3ef4SAndrew Morton 		try_to_freeze();
34771da177e4SLinus Torvalds 	}
3478baac167bSJesper Dangaard Brouer 	set_current_state(TASK_INTERRUPTIBLE);
34791da177e4SLinus Torvalds 
3480f9467eaeSJoe Perches 	pr_debug("%s stopping all device\n", t->tsk->comm);
34811da177e4SLinus Torvalds 	pktgen_stop(t);
34821da177e4SLinus Torvalds 
3483f9467eaeSJoe Perches 	pr_debug("%s removing all device\n", t->tsk->comm);
34841da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
34851da177e4SLinus Torvalds 
3486f9467eaeSJoe Perches 	pr_debug("%s removing thread\n", t->tsk->comm);
34871da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3488cdcdbe0bSLuiz Capitulino 
3489551eaff1SEric Dumazet 	/* Wait for kthread_stop */
3490551eaff1SEric Dumazet 	while (!kthread_should_stop()) {
3491551eaff1SEric Dumazet 		set_current_state(TASK_INTERRUPTIBLE);
3492551eaff1SEric Dumazet 		schedule();
3493551eaff1SEric Dumazet 	}
3494551eaff1SEric Dumazet 	__set_current_state(TASK_RUNNING);
3495551eaff1SEric Dumazet 
3496ee74baa7SDavid S. Miller 	return 0;
34971da177e4SLinus Torvalds }
34981da177e4SLinus Torvalds 
3499222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
35003e984840SEric Dumazet 					  const char *ifname, bool exact)
35011da177e4SLinus Torvalds {
3502c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
35033e984840SEric Dumazet 	size_t len = strlen(ifname);
35041da177e4SLinus Torvalds 
35058788370aSJesper Dangaard Brouer 	rcu_read_lock();
35068788370aSJesper Dangaard Brouer 	list_for_each_entry_rcu(p, &t->if_list, list)
35073e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
35083e984840SEric Dumazet 			if (p->odevname[len]) {
35093e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
35103e984840SEric Dumazet 					continue;
35113e984840SEric Dumazet 			}
3512c26a8016SLuiz Capitulino 			pkt_dev = p;
35131da177e4SLinus Torvalds 			break;
35141da177e4SLinus Torvalds 		}
35151da177e4SLinus Torvalds 
35168788370aSJesper Dangaard Brouer 	rcu_read_unlock();
3517f9467eaeSJoe Perches 	pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
35181da177e4SLinus Torvalds 	return pkt_dev;
35191da177e4SLinus Torvalds }
35201da177e4SLinus Torvalds 
35211da177e4SLinus Torvalds /*
35221da177e4SLinus Torvalds  * Adds a dev at front of if_list.
35231da177e4SLinus Torvalds  */
35241da177e4SLinus Torvalds 
3525222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3526222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
35271da177e4SLinus Torvalds {
35281da177e4SLinus Torvalds 	int rv = 0;
35291da177e4SLinus Torvalds 
35308788370aSJesper Dangaard Brouer 	/* This function cannot be called concurrently, as its called
35318788370aSJesper Dangaard Brouer 	 * under pktgen_thread_lock mutex, but it can run from
35328788370aSJesper Dangaard Brouer 	 * userspace on another CPU than the kthread.  The if_lock()
35338788370aSJesper Dangaard Brouer 	 * is used here to sync with concurrent instances of
35348788370aSJesper Dangaard Brouer 	 * _rem_dev_from_if_list() invoked via kthread, which is also
35358788370aSJesper Dangaard Brouer 	 * updating the if_list */
35361da177e4SLinus Torvalds 	if_lock(t);
35371da177e4SLinus Torvalds 
35381da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
3539f9467eaeSJoe Perches 		pr_err("ERROR: already assigned to a thread\n");
35401da177e4SLinus Torvalds 		rv = -EBUSY;
35411da177e4SLinus Torvalds 		goto out;
35421da177e4SLinus Torvalds 	}
3543c26a8016SLuiz Capitulino 
35441da177e4SLinus Torvalds 	pkt_dev->running = 0;
35458788370aSJesper Dangaard Brouer 	pkt_dev->pg_thread = t;
35468788370aSJesper Dangaard Brouer 	list_add_rcu(&pkt_dev->list, &t->if_list);
35471da177e4SLinus Torvalds 
35481da177e4SLinus Torvalds out:
35491da177e4SLinus Torvalds 	if_unlock(t);
35501da177e4SLinus Torvalds 	return rv;
35511da177e4SLinus Torvalds }
35521da177e4SLinus Torvalds 
35531da177e4SLinus Torvalds /* Called under thread lock */
35541da177e4SLinus Torvalds 
35551da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
35561da177e4SLinus Torvalds {
35571da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
355839df232fSStephen Hemminger 	int err;
35593291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
35601da177e4SLinus Torvalds 
35611da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
35621da177e4SLinus Torvalds 
35634e58a027SCong Wang 	pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3564d50a6b56SStephen Hemminger 	if (pkt_dev) {
3565f9467eaeSJoe Perches 		pr_err("ERROR: interface already used\n");
3566d50a6b56SStephen Hemminger 		return -EBUSY;
3567d50a6b56SStephen Hemminger 	}
35681da177e4SLinus Torvalds 
35693291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
35701da177e4SLinus Torvalds 	if (!pkt_dev)
35711da177e4SLinus Torvalds 		return -ENOMEM;
35721da177e4SLinus Torvalds 
3573593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
357468d5ac2eSWANG Cong 	pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
35753291b9dbSEric Dumazet 				      node);
35761da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
35771da177e4SLinus Torvalds 		kfree(pkt_dev);
35781da177e4SLinus Torvalds 		return -ENOMEM;
35791da177e4SLinus Torvalds 	}
35801da177e4SLinus Torvalds 
358195ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
35821da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
3583fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
35841da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
35851da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
35861da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
35871da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
35881da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
35891da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
359034954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
359134954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
359234954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
359334954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
359434954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
359534954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
3596*38b2cf29SAlexei Starovoitov 	pkt_dev->burst = 1;
3597e99b99b4SRobert Olsson 	pkt_dev->node = -1;
359834954ddcSFrancesco Fondelli 
35994e58a027SCong Wang 	err = pktgen_setup_dev(t->net, pkt_dev, ifname);
360039df232fSStephen Hemminger 	if (err)
360139df232fSStephen Hemminger 		goto out1;
3602d8873315SNeil Horman 	if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3603d8873315SNeil Horman 		pkt_dev->clone_skb = pg_clone_skb_d;
36041da177e4SLinus Torvalds 
36054e58a027SCong Wang 	pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
36065efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
360739df232fSStephen Hemminger 	if (!pkt_dev->entry) {
3608f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3609d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
361039df232fSStephen Hemminger 		err = -EINVAL;
361139df232fSStephen Hemminger 		goto out2;
361239df232fSStephen Hemminger 	}
3613a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3614a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3615a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3616cf93d47eSFan Du 
3617cf93d47eSFan Du 	/* xfrm tunnel mode needs additional dst to extract outter
3618cf93d47eSFan Du 	 * ip header protocol/ttl/id field, here creat a phony one.
3619cf93d47eSFan Du 	 * instead of looking for a valid rt, which definitely hurting
3620cf93d47eSFan Du 	 * performance under such circumstance.
3621cf93d47eSFan Du 	 */
3622cf93d47eSFan Du 	pkt_dev->dstops.family = AF_INET;
3623cf93d47eSFan Du 	pkt_dev->dst.dev = pkt_dev->odev;
3624cf93d47eSFan Du 	dst_init_metrics(&pkt_dev->dst, pktgen_dst_metrics, false);
3625cf93d47eSFan Du 	pkt_dev->dst.child = &pkt_dev->dst;
3626cf93d47eSFan Du 	pkt_dev->dst.ops = &pkt_dev->dstops;
3627a553e4a6SJamal Hadi Salim #endif
362839df232fSStephen Hemminger 
362939df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
363039df232fSStephen Hemminger out2:
363139df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
363239df232fSStephen Hemminger out1:
3633a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3634a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3635a553e4a6SJamal Hadi Salim #endif
36361da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
36371da177e4SLinus Torvalds 	kfree(pkt_dev);
363839df232fSStephen Hemminger 	return err;
36391da177e4SLinus Torvalds }
36401da177e4SLinus Torvalds 
36414e58a027SCong Wang static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
36421da177e4SLinus Torvalds {
3643cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3644d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3645ee74baa7SDavid S. Miller 	struct task_struct *p;
36461da177e4SLinus Torvalds 
36473291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
36483291b9dbSEric Dumazet 			 cpu_to_node(cpu));
36491da177e4SLinus Torvalds 	if (!t) {
3650f9467eaeSJoe Perches 		pr_err("ERROR: out of memory, can't create new thread\n");
36511da177e4SLinus Torvalds 		return -ENOMEM;
36521da177e4SLinus Torvalds 	}
36531da177e4SLinus Torvalds 
36541da177e4SLinus Torvalds 	spin_lock_init(&t->if_lock);
36551da177e4SLinus Torvalds 	t->cpu = cpu;
36561da177e4SLinus Torvalds 
3657ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3658ee74baa7SDavid S. Miller 
36594e58a027SCong Wang 	list_add_tail(&t->th_list, &pn->pktgen_threads);
3660d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3661ee74baa7SDavid S. Miller 
366294dcf29aSEric Dumazet 	p = kthread_create_on_node(pktgen_thread_worker,
366394dcf29aSEric Dumazet 				   t,
366494dcf29aSEric Dumazet 				   cpu_to_node(cpu),
366594dcf29aSEric Dumazet 				   "kpktgend_%d", cpu);
3666ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
3667f9467eaeSJoe Perches 		pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3668ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3669ee74baa7SDavid S. Miller 		kfree(t);
3670ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3671ee74baa7SDavid S. Miller 	}
3672ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3673ee74baa7SDavid S. Miller 	t->tsk = p;
3674ee74baa7SDavid S. Miller 
36754e58a027SCong Wang 	pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
36765efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3677d50a6b56SStephen Hemminger 	if (!pe) {
3678f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3679ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3680ee74baa7SDavid S. Miller 		kthread_stop(p);
3681ee74baa7SDavid S. Miller 		list_del(&t->th_list);
36821da177e4SLinus Torvalds 		kfree(t);
36831da177e4SLinus Torvalds 		return -EINVAL;
36841da177e4SLinus Torvalds 	}
3685d50a6b56SStephen Hemminger 
36864e58a027SCong Wang 	t->net = pn;
3687ee74baa7SDavid S. Miller 	wake_up_process(p);
3688d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
36891da177e4SLinus Torvalds 
36901da177e4SLinus Torvalds 	return 0;
36911da177e4SLinus Torvalds }
36921da177e4SLinus Torvalds 
36931da177e4SLinus Torvalds /*
36941da177e4SLinus Torvalds  * Removes a device from the thread if_list.
36951da177e4SLinus Torvalds  */
3696222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3697222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
36981da177e4SLinus Torvalds {
3699c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3700c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
37011da177e4SLinus Torvalds 
37028788370aSJesper Dangaard Brouer 	if_lock(t);
3703c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3704c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3705c26a8016SLuiz Capitulino 		if (p == pkt_dev)
37068788370aSJesper Dangaard Brouer 			list_del_rcu(&p->list);
37071da177e4SLinus Torvalds 	}
37088788370aSJesper Dangaard Brouer 	if_unlock(t);
37091da177e4SLinus Torvalds }
37101da177e4SLinus Torvalds 
3711222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3712222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
37131da177e4SLinus Torvalds {
3714f9467eaeSJoe Perches 	pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
37151da177e4SLinus Torvalds 
37161da177e4SLinus Torvalds 	if (pkt_dev->running) {
3717294a0b7fSJoe Perches 		pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
37181da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
37191da177e4SLinus Torvalds 	}
37201da177e4SLinus Torvalds 
37211da177e4SLinus Torvalds 	/* Dis-associate from the interface */
37221da177e4SLinus Torvalds 
37231da177e4SLinus Torvalds 	if (pkt_dev->odev) {
37241da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
37251da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
37261da177e4SLinus Torvalds 	}
37271da177e4SLinus Torvalds 
37288788370aSJesper Dangaard Brouer 	/* Remove proc before if_list entry, because add_device uses
37298788370aSJesper Dangaard Brouer 	 * list to determine if interface already exist, avoid race
37308788370aSJesper Dangaard Brouer 	 * with proc_create_data() */
373139df232fSStephen Hemminger 	if (pkt_dev->entry)
3732a8ca16eaSDavid Howells 		proc_remove(pkt_dev->entry);
37331da177e4SLinus Torvalds 
37348788370aSJesper Dangaard Brouer 	/* And update the thread if_list */
37358788370aSJesper Dangaard Brouer 	_rem_dev_from_if_list(t, pkt_dev);
37368788370aSJesper Dangaard Brouer 
3737a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3738a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3739a553e4a6SJamal Hadi Salim #endif
37401da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
374126ad7879SEric Dumazet 	if (pkt_dev->page)
374226ad7879SEric Dumazet 		put_page(pkt_dev->page);
37438788370aSJesper Dangaard Brouer 	kfree_rcu(pkt_dev, rcu);
37441da177e4SLinus Torvalds 	return 0;
37451da177e4SLinus Torvalds }
37461da177e4SLinus Torvalds 
37474e58a027SCong Wang static int __net_init pg_net_init(struct net *net)
37481da177e4SLinus Torvalds {
37494e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3750d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
37514e58a027SCong Wang 	int cpu, ret = 0;
3752d50a6b56SStephen Hemminger 
37534e58a027SCong Wang 	pn->net = net;
37544e58a027SCong Wang 	INIT_LIST_HEAD(&pn->pktgen_threads);
37554e58a027SCong Wang 	pn->pktgen_exiting = false;
37564e58a027SCong Wang 	pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
37574e58a027SCong Wang 	if (!pn->proc_dir) {
37584e58a027SCong Wang 		pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3759d50a6b56SStephen Hemminger 		return -ENODEV;
37601da177e4SLinus Torvalds 	}
37614e58a027SCong Wang 	pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
37624e58a027SCong Wang 	if (pe == NULL) {
37634e58a027SCong Wang 		pr_err("cannot create %s procfs entry\n", PGCTRL);
37644e58a027SCong Wang 		ret = -EINVAL;
37654e58a027SCong Wang 		goto remove;
37664e58a027SCong Wang 	}
37671da177e4SLinus Torvalds 
3768670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
37698024bb24SLuiz Capitulino 		int err;
37701da177e4SLinus Torvalds 
37714e58a027SCong Wang 		err = pktgen_create_thread(cpu, pn);
37728024bb24SLuiz Capitulino 		if (err)
37734e58a027SCong Wang 			pr_warn("Cannot create thread for cpu %d (%d)\n",
3774f9467eaeSJoe Perches 				   cpu, err);
37751da177e4SLinus Torvalds 	}
37768024bb24SLuiz Capitulino 
37774e58a027SCong Wang 	if (list_empty(&pn->pktgen_threads)) {
37784e58a027SCong Wang 		pr_err("Initialization failed for all threads\n");
3779ce14f894SWANG Cong 		ret = -ENODEV;
37804e58a027SCong Wang 		goto remove_entry;
37818024bb24SLuiz Capitulino 	}
37828024bb24SLuiz Capitulino 
37831da177e4SLinus Torvalds 	return 0;
3784ce14f894SWANG Cong 
37854e58a027SCong Wang remove_entry:
37864e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
37874e58a027SCong Wang remove:
3788ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3789ce14f894SWANG Cong 	return ret;
37901da177e4SLinus Torvalds }
37911da177e4SLinus Torvalds 
37924e58a027SCong Wang static void __net_exit pg_net_exit(struct net *net)
37931da177e4SLinus Torvalds {
37944e58a027SCong Wang 	struct pktgen_net *pn = net_generic(net, pg_net_id);
3795cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3796cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
3797d4b11335SEric Dumazet 	LIST_HEAD(list);
37981da177e4SLinus Torvalds 
37991da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
38004e58a027SCong Wang 	pn->pktgen_exiting = true;
38011da177e4SLinus Torvalds 
3802c57b5468SEric Dumazet 	mutex_lock(&pktgen_thread_lock);
38034e58a027SCong Wang 	list_splice_init(&pn->pktgen_threads, &list);
3804c57b5468SEric Dumazet 	mutex_unlock(&pktgen_thread_lock);
3805c57b5468SEric Dumazet 
3806c57b5468SEric Dumazet 	list_for_each_safe(q, n, &list) {
3807cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3808c57b5468SEric Dumazet 		list_del(&t->th_list);
3809ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
3810ee74baa7SDavid S. Miller 		kfree(t);
38111da177e4SLinus Torvalds 	}
38121da177e4SLinus Torvalds 
38134e58a027SCong Wang 	remove_proc_entry(PGCTRL, pn->proc_dir);
3814ece31ffdSGao feng 	remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
38154e58a027SCong Wang }
38161da177e4SLinus Torvalds 
38174e58a027SCong Wang static struct pernet_operations pg_net_ops = {
38184e58a027SCong Wang 	.init = pg_net_init,
38194e58a027SCong Wang 	.exit = pg_net_exit,
38204e58a027SCong Wang 	.id   = &pg_net_id,
38214e58a027SCong Wang 	.size = sizeof(struct pktgen_net),
38224e58a027SCong Wang };
38234e58a027SCong Wang 
38244e58a027SCong Wang static int __init pg_init(void)
38254e58a027SCong Wang {
38264e58a027SCong Wang 	int ret = 0;
38274e58a027SCong Wang 
38284e58a027SCong Wang 	pr_info("%s", version);
38294e58a027SCong Wang 	ret = register_pernet_subsys(&pg_net_ops);
38304e58a027SCong Wang 	if (ret)
38314e58a027SCong Wang 		return ret;
38324e58a027SCong Wang 	ret = register_netdevice_notifier(&pktgen_notifier_block);
38334e58a027SCong Wang 	if (ret)
38344e58a027SCong Wang 		unregister_pernet_subsys(&pg_net_ops);
38354e58a027SCong Wang 
38364e58a027SCong Wang 	return ret;
38374e58a027SCong Wang }
38384e58a027SCong Wang 
38394e58a027SCong Wang static void __exit pg_cleanup(void)
38404e58a027SCong Wang {
38414e58a027SCong Wang 	unregister_netdevice_notifier(&pktgen_notifier_block);
38424e58a027SCong Wang 	unregister_pernet_subsys(&pg_net_ops);
38438788370aSJesper Dangaard Brouer 	/* Don't need rcu_barrier() due to use of kfree_rcu() */
38441da177e4SLinus Torvalds }
38451da177e4SLinus Torvalds 
38461da177e4SLinus Torvalds module_init(pg_init);
38471da177e4SLinus Torvalds module_exit(pg_cleanup);
38481da177e4SLinus Torvalds 
3849c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
38501da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
38511da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3852c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
38531da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3854c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
38551da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3856c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
38571da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3858c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
38591da177e4SLinus Torvalds module_param(debug, int, 0);
3860c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3861