xref: /openbmc/linux/net/core/pktgen.c (revision 268bb0ce3e87872cb9290c322b0d35bce230d88f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Authors:
31da177e4SLinus Torvalds  * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
41da177e4SLinus Torvalds  *                             Uppsala University and
51da177e4SLinus Torvalds  *                             Swedish University of Agricultural Sciences
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Alexey Kuznetsov  <kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  * Ben Greear <greearb@candelatech.com>
996de0e25SJan Engelhardt  * Jens Låås <jens.laas@data.slu.se>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * A tool for loading the network with preconfigurated packets.
181da177e4SLinus Torvalds  * The tool is implemented as a linux module.  Parameters are output
191da177e4SLinus Torvalds  * device, delay (to hard_xmit), number of packets, and whether
201da177e4SLinus Torvalds  * to use multiple SKBs or just the same one.
211da177e4SLinus Torvalds  * pktgen uses the installed interface's output routine.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * Additional hacking by:
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Jens.Laas@data.slu.se
261da177e4SLinus Torvalds  * Improved by ANK. 010120.
271da177e4SLinus Torvalds  * Improved by ANK even more. 010212.
281da177e4SLinus Torvalds  * MAC address typo fixed. 010417 --ro
291da177e4SLinus Torvalds  * Integrated.  020301 --DaveM
301da177e4SLinus Torvalds  * Added multiskb option 020301 --DaveM
311da177e4SLinus Torvalds  * Scaling of results. 020417--sigurdur@linpro.no
321da177e4SLinus Torvalds  * Significant re-work of the module:
331da177e4SLinus Torvalds  *   *  Convert to threaded model to more efficiently be able to transmit
341da177e4SLinus Torvalds  *       and receive on multiple interfaces at once.
351da177e4SLinus Torvalds  *   *  Converted many counters to __u64 to allow longer runs.
361da177e4SLinus Torvalds  *   *  Allow configuration of ranges, like min/max IP address, MACs,
371da177e4SLinus Torvalds  *       and UDP-ports, for both source and destination, and can
381da177e4SLinus Torvalds  *       set to use a random distribution or sequentially walk the range.
391da177e4SLinus Torvalds  *   *  Can now change most values after starting.
401da177e4SLinus Torvalds  *   *  Place 12-byte packet in UDP payload with magic number,
411da177e4SLinus Torvalds  *       sequence number, and timestamp.
421da177e4SLinus Torvalds  *   *  Add receiver code that detects dropped pkts, re-ordered pkts, and
431da177e4SLinus Torvalds  *       latencies (with micro-second) precision.
441da177e4SLinus Torvalds  *   *  Add IOCTL interface to easily get counters & configuration.
451da177e4SLinus Torvalds  *   --Ben Greear <greearb@candelatech.com>
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
481da177e4SLinus Torvalds  * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
491da177e4SLinus Torvalds  * as a "fastpath" with a configurable number of clones after alloc's.
501da177e4SLinus Torvalds  * clone_skb=0 means all packets are allocated this also means ranges time
511da177e4SLinus Torvalds  * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
521da177e4SLinus Torvalds  * clones.
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  * Also moved to /proc/net/pktgen/
551da177e4SLinus Torvalds  * --ro
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * Sept 10:  Fixed threading/locking.  Lots of bone-headed and more clever
581da177e4SLinus Torvalds  *    mistakes.  Also merged in DaveM's patch in the -pre6 patch.
591da177e4SLinus Torvalds  * --Ben Greear <greearb@candelatech.com>
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  *
641da177e4SLinus Torvalds  * 021124 Finished major redesign and rewrite for new functionality.
651da177e4SLinus Torvalds  * See Documentation/networking/pktgen.txt for how to use this.
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  * The new operation:
681da177e4SLinus Torvalds  * For each CPU one thread/process is created at start. This process checks
691da177e4SLinus Torvalds  * for running devices in the if_list and sends packets until count is 0 it
701da177e4SLinus Torvalds  * also the thread checks the thread->control which is used for inter-process
711da177e4SLinus Torvalds  * communication. controlling process "posts" operations to the threads this
721da177e4SLinus Torvalds  * way. The if_lock should be possible to remove when add/rem_device is merged
731da177e4SLinus Torvalds  * into this too.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * By design there should only be *one* "controlling" process. In practice
761da177e4SLinus Torvalds  * multiple write accesses gives unpredictable result. Understood by "write"
771da177e4SLinus Torvalds  * to /proc gives result code thats should be read be the "writer".
78b4099fabSStephen Hemminger  * For practical use this should be no problem.
791da177e4SLinus Torvalds  *
801da177e4SLinus Torvalds  * Note when adding devices to a specific CPU there good idea to also assign
811da177e4SLinus Torvalds  * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
821da177e4SLinus Torvalds  * --ro
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * Fix refcount off by one if first packet fails, potential null deref,
851da177e4SLinus Torvalds  * memleak 030710- KJP
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * First "ranges" functionality for ipv6 030726 --ro
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  * Included flow support. 030802 ANK.
901da177e4SLinus Torvalds  *
911da177e4SLinus Torvalds  * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
921da177e4SLinus Torvalds  *
931da177e4SLinus Torvalds  * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
941da177e4SLinus Torvalds  * ia64 compilation fix from  Aron Griffis <aron@hp.com> 040604
951da177e4SLinus Torvalds  *
961da177e4SLinus Torvalds  * New xmit() return, do_div and misc clean up by Stephen Hemminger
971da177e4SLinus Torvalds  * <shemminger@osdl.org> 040923
981da177e4SLinus Torvalds  *
99b4099fabSStephen Hemminger  * Randy Dunlap fixed u64 printk compiler waring
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * Remove FCS from BW calculation.  Lennert Buytenhek <buytenh@wantstofly.org>
1021da177e4SLinus Torvalds  * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  * Corrections from Nikolai Malykh (nmalykh@bilim.com)
1051da177e4SLinus Torvalds  * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
1081da177e4SLinus Torvalds  * 050103
109ca6549afSSteven Whitehouse  *
110ca6549afSSteven Whitehouse  * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111ca6549afSSteven Whitehouse  *
11234954ddcSFrancesco Fondelli  * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
11334954ddcSFrancesco Fondelli  *
114ce5d0b47SAdit Ranadive  * Fixed src_mac command to set source mac of packet to value specified in
115ce5d0b47SAdit Ranadive  * command by Adit Ranadive <adit.262@gmail.com>
116ce5d0b47SAdit Ranadive  *
1171da177e4SLinus Torvalds  */
118f9467eaeSJoe Perches 
119f9467eaeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
120f9467eaeSJoe Perches 
1211da177e4SLinus Torvalds #include <linux/sys.h>
1221da177e4SLinus Torvalds #include <linux/types.h>
1231da177e4SLinus Torvalds #include <linux/module.h>
1241da177e4SLinus Torvalds #include <linux/moduleparam.h>
1251da177e4SLinus Torvalds #include <linux/kernel.h>
126222fa076SLuiz Capitulino #include <linux/mutex.h>
1271da177e4SLinus Torvalds #include <linux/sched.h>
1281da177e4SLinus Torvalds #include <linux/slab.h>
1291da177e4SLinus Torvalds #include <linux/vmalloc.h>
1301da177e4SLinus Torvalds #include <linux/unistd.h>
1311da177e4SLinus Torvalds #include <linux/string.h>
1321da177e4SLinus Torvalds #include <linux/ptrace.h>
1331da177e4SLinus Torvalds #include <linux/errno.h>
1341da177e4SLinus Torvalds #include <linux/ioport.h>
1351da177e4SLinus Torvalds #include <linux/interrupt.h>
1364fc268d2SRandy Dunlap #include <linux/capability.h>
1372bc481cfSStephen Hemminger #include <linux/hrtimer.h>
13809fe3ef4SAndrew Morton #include <linux/freezer.h>
1391da177e4SLinus Torvalds #include <linux/delay.h>
1401da177e4SLinus Torvalds #include <linux/timer.h>
141cdcdbe0bSLuiz Capitulino #include <linux/list.h>
1421da177e4SLinus Torvalds #include <linux/init.h>
1431da177e4SLinus Torvalds #include <linux/skbuff.h>
1441da177e4SLinus Torvalds #include <linux/netdevice.h>
1451da177e4SLinus Torvalds #include <linux/inet.h>
1461da177e4SLinus Torvalds #include <linux/inetdevice.h>
1471da177e4SLinus Torvalds #include <linux/rtnetlink.h>
1481da177e4SLinus Torvalds #include <linux/if_arp.h>
14934954ddcSFrancesco Fondelli #include <linux/if_vlan.h>
1501da177e4SLinus Torvalds #include <linux/in.h>
1511da177e4SLinus Torvalds #include <linux/ip.h>
1521da177e4SLinus Torvalds #include <linux/ipv6.h>
1531da177e4SLinus Torvalds #include <linux/udp.h>
1541da177e4SLinus Torvalds #include <linux/proc_fs.h>
155d50a6b56SStephen Hemminger #include <linux/seq_file.h>
1561da177e4SLinus Torvalds #include <linux/wait.h>
157f404e9a6SKris Katterjohn #include <linux/etherdevice.h>
158ee74baa7SDavid S. Miller #include <linux/kthread.h>
159*268bb0ceSLinus Torvalds #include <linux/prefetch.h>
160457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1611da177e4SLinus Torvalds #include <net/checksum.h>
1621da177e4SLinus Torvalds #include <net/ipv6.h>
1631da177e4SLinus Torvalds #include <net/addrconf.h>
164a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
165a553e4a6SJamal Hadi Salim #include <net/xfrm.h>
166a553e4a6SJamal Hadi Salim #endif
1671da177e4SLinus Torvalds #include <asm/byteorder.h>
1681da177e4SLinus Torvalds #include <linux/rcupdate.h>
1691977f032SJiri Slaby #include <linux/bitops.h>
17063adc6fbSStephen Hemminger #include <linux/io.h>
17163adc6fbSStephen Hemminger #include <linux/timex.h>
17263adc6fbSStephen Hemminger #include <linux/uaccess.h>
1731da177e4SLinus Torvalds #include <asm/dma.h>
1741da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1751da177e4SLinus Torvalds 
17643d28b65SDaniel Turull #define VERSION	"2.74"
1771da177e4SLinus Torvalds #define IP_NAME_SZ 32
178ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
179d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1801da177e4SLinus Torvalds 
181f9467eaeSJoe Perches #define func_enter() pr_debug("entering %s\n", __func__);
182f9467eaeSJoe Perches 
1831da177e4SLinus Torvalds /* Device flag bits */
1841da177e4SLinus Torvalds #define F_IPSRC_RND   (1<<0)	/* IP-Src Random  */
1851da177e4SLinus Torvalds #define F_IPDST_RND   (1<<1)	/* IP-Dst Random  */
1861da177e4SLinus Torvalds #define F_UDPSRC_RND  (1<<2)	/* UDP-Src Random */
1871da177e4SLinus Torvalds #define F_UDPDST_RND  (1<<3)	/* UDP-Dst Random */
1881da177e4SLinus Torvalds #define F_MACSRC_RND  (1<<4)	/* MAC-Src Random */
1891da177e4SLinus Torvalds #define F_MACDST_RND  (1<<5)	/* MAC-Dst Random */
1901da177e4SLinus Torvalds #define F_TXSIZE_RND  (1<<6)	/* Transmit size is random */
1911da177e4SLinus Torvalds #define F_IPV6        (1<<7)	/* Interface in IPV6 Mode */
192ca6549afSSteven Whitehouse #define F_MPLS_RND    (1<<8)	/* Random MPLS labels */
19334954ddcSFrancesco Fondelli #define F_VID_RND     (1<<9)	/* Random VLAN ID */
19434954ddcSFrancesco Fondelli #define F_SVID_RND    (1<<10)	/* Random SVLAN ID */
195007a531bSJamal Hadi Salim #define F_FLOW_SEQ    (1<<11)	/* Sequential flows */
196a553e4a6SJamal Hadi Salim #define F_IPSEC_ON    (1<<12)	/* ipsec on for flows */
19745b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13)	/* queue map Random */
198e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14)	/* queue map mirrors smp_processor_id() */
199e99b99b4SRobert Olsson #define F_NODE          (1<<15)	/* Node memory alloc*/
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /* Thread control flag bits */
2026b80d6a6SStephen Hemminger #define T_STOP        (1<<0)	/* Stop run */
2036b80d6a6SStephen Hemminger #define T_RUN         (1<<1)	/* Start run */
2046b80d6a6SStephen Hemminger #define T_REMDEVALL   (1<<2)	/* Remove all devs */
2056b80d6a6SStephen Hemminger #define T_REMDEV      (1<<3)	/* Remove one dev */
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /* If lock -- can be removed after some work */
2081da177e4SLinus Torvalds #define   if_lock(t)           spin_lock(&(t->if_lock));
2091da177e4SLinus Torvalds #define   if_unlock(t)           spin_unlock(&(t->if_lock));
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2121da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
213d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
214d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
21563adc6fbSStephen Hemminger static struct proc_dir_entry *pg_proc_dir;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds #define MAX_CFLOWS  65536
2181da177e4SLinus Torvalds 
21934954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
22034954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
22134954ddcSFrancesco Fondelli 
222222f1806SLuiz Capitulino struct flow_state {
223252e3346SAl Viro 	__be32 cur_daddr;
2241da177e4SLinus Torvalds 	int count;
225a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
226a553e4a6SJamal Hadi Salim 	struct xfrm_state *x;
227a553e4a6SJamal Hadi Salim #endif
228007a531bSJamal Hadi Salim 	__u32 flags;
2291da177e4SLinus Torvalds };
2301da177e4SLinus Torvalds 
231007a531bSJamal Hadi Salim /* flow flag bits */
232007a531bSJamal Hadi Salim #define F_INIT   (1<<0)		/* flow has been initialized */
233007a531bSJamal Hadi Salim 
2341da177e4SLinus Torvalds struct pktgen_dev {
2351da177e4SLinus Torvalds 	/*
2361da177e4SLinus Torvalds 	 * Try to keep frequent/infrequent used vars. separated.
2371da177e4SLinus Torvalds 	 */
23839df232fSStephen Hemminger 	struct proc_dir_entry *entry;	/* proc file */
2391da177e4SLinus Torvalds 	struct pktgen_thread *pg_thread;/* the owner */
24063adc6fbSStephen Hemminger 	struct list_head list;		/* chaining in the thread's run-queue */
2411da177e4SLinus Torvalds 
24263adc6fbSStephen Hemminger 	int running;		/* if false, the test will stop */
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/* If min != max, then we will either do a linear iteration, or
2451da177e4SLinus Torvalds 	 * we will do a random selection from within the range.
2461da177e4SLinus Torvalds 	 */
2471da177e4SLinus Torvalds 	__u32 flags;
24895ed63f7SArthur Kepner 	int removal_mark;	/* non-zero => the device is marked for
24995ed63f7SArthur Kepner 				 * removal by worker thread */
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	int min_pkt_size;	/* = ETH_ZLEN; */
2521da177e4SLinus Torvalds 	int max_pkt_size;	/* = ETH_ZLEN; */
25316dab72fSJamal Hadi Salim 	int pkt_overhead;	/* overhead for MPLS, VLANs, IPSEC etc */
2541da177e4SLinus Torvalds 	int nfrags;
25526ad7879SEric Dumazet 	struct page *page;
256fd29cf72SStephen Hemminger 	u64 delay;		/* nano-seconds */
257fd29cf72SStephen Hemminger 
2581da177e4SLinus Torvalds 	__u64 count;		/* Default No packets to send */
2591da177e4SLinus Torvalds 	__u64 sofar;		/* How many pkts we've sent so far */
2601da177e4SLinus Torvalds 	__u64 tx_bytes;		/* How many bytes we've transmitted */
261f466dba1SJohn Fastabend 	__u64 errors;		/* Errors when trying to transmit, */
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	/* runtime counters relating to clone_skb */
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	__u64 allocated_skbs;
2661da177e4SLinus Torvalds 	__u32 clone_count;
2671da177e4SLinus Torvalds 	int last_ok;		/* Was last skb sent?
26863adc6fbSStephen Hemminger 				 * Or a failed transmit of some sort?
26963adc6fbSStephen Hemminger 				 * This will keep sequence numbers in order
2701da177e4SLinus Torvalds 				 */
271fd29cf72SStephen Hemminger 	ktime_t next_tx;
272fd29cf72SStephen Hemminger 	ktime_t started_at;
273fd29cf72SStephen Hemminger 	ktime_t stopped_at;
274fd29cf72SStephen Hemminger 	u64	idle_acc;	/* nano-seconds */
275fd29cf72SStephen Hemminger 
2761da177e4SLinus Torvalds 	__u32 seq_num;
2771da177e4SLinus Torvalds 
27863adc6fbSStephen Hemminger 	int clone_skb;		/*
27963adc6fbSStephen Hemminger 				 * Use multiple SKBs during packet gen.
28063adc6fbSStephen Hemminger 				 * If this number is greater than 1, then
28163adc6fbSStephen Hemminger 				 * that many copies of the same packet will be
28263adc6fbSStephen Hemminger 				 * sent before a new packet is allocated.
28363adc6fbSStephen Hemminger 				 * If you want to send 1024 identical packets
28463adc6fbSStephen Hemminger 				 * before creating a new packet,
28563adc6fbSStephen Hemminger 				 * set clone_skb to 1024.
2861da177e4SLinus Torvalds 				 */
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	char dst_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2891da177e4SLinus Torvalds 	char dst_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2901da177e4SLinus Torvalds 	char src_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2911da177e4SLinus Torvalds 	char src_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	struct in6_addr in6_saddr;
2941da177e4SLinus Torvalds 	struct in6_addr in6_daddr;
2951da177e4SLinus Torvalds 	struct in6_addr cur_in6_daddr;
2961da177e4SLinus Torvalds 	struct in6_addr cur_in6_saddr;
2971da177e4SLinus Torvalds 	/* For ranges */
2981da177e4SLinus Torvalds 	struct in6_addr min_in6_daddr;
2991da177e4SLinus Torvalds 	struct in6_addr max_in6_daddr;
3001da177e4SLinus Torvalds 	struct in6_addr min_in6_saddr;
3011da177e4SLinus Torvalds 	struct in6_addr max_in6_saddr;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	/* If we're doing ranges, random or incremental, then this
3041da177e4SLinus Torvalds 	 * defines the min/max for those ranges.
3051da177e4SLinus Torvalds 	 */
306252e3346SAl Viro 	__be32 saddr_min;	/* inclusive, source IP address */
307252e3346SAl Viro 	__be32 saddr_max;	/* exclusive, source IP address */
308252e3346SAl Viro 	__be32 daddr_min;	/* inclusive, dest IP address */
309252e3346SAl Viro 	__be32 daddr_max;	/* exclusive, dest IP address */
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	__u16 udp_src_min;	/* inclusive, source UDP port */
3121da177e4SLinus Torvalds 	__u16 udp_src_max;	/* exclusive, source UDP port */
3131da177e4SLinus Torvalds 	__u16 udp_dst_min;	/* inclusive, dest UDP port */
3141da177e4SLinus Torvalds 	__u16 udp_dst_max;	/* exclusive, dest UDP port */
3151da177e4SLinus Torvalds 
3161ca7768cSFrancesco Fondelli 	/* DSCP + ECN */
31763adc6fbSStephen Hemminger 	__u8 tos;            /* six MSB of (former) IPv4 TOS
31863adc6fbSStephen Hemminger 				are for dscp codepoint */
31963adc6fbSStephen Hemminger 	__u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6
32063adc6fbSStephen Hemminger 				(see RFC 3260, sec. 4) */
3211ca7768cSFrancesco Fondelli 
322ca6549afSSteven Whitehouse 	/* MPLS */
323ca6549afSSteven Whitehouse 	unsigned nr_labels;	/* Depth of stack, 0 = no MPLS */
324ca6549afSSteven Whitehouse 	__be32 labels[MAX_MPLS_LABELS];
325ca6549afSSteven Whitehouse 
32634954ddcSFrancesco Fondelli 	/* VLAN/SVLAN (802.1Q/Q-in-Q) */
32734954ddcSFrancesco Fondelli 	__u8  vlan_p;
32834954ddcSFrancesco Fondelli 	__u8  vlan_cfi;
32934954ddcSFrancesco Fondelli 	__u16 vlan_id;  /* 0xffff means no vlan tag */
33034954ddcSFrancesco Fondelli 
33134954ddcSFrancesco Fondelli 	__u8  svlan_p;
33234954ddcSFrancesco Fondelli 	__u8  svlan_cfi;
33334954ddcSFrancesco Fondelli 	__u16 svlan_id; /* 0xffff means no svlan tag */
33434954ddcSFrancesco Fondelli 
3351da177e4SLinus Torvalds 	__u32 src_mac_count;	/* How many MACs to iterate through */
3361da177e4SLinus Torvalds 	__u32 dst_mac_count;	/* How many MACs to iterate through */
3371da177e4SLinus Torvalds 
338f404e9a6SKris Katterjohn 	unsigned char dst_mac[ETH_ALEN];
339f404e9a6SKris Katterjohn 	unsigned char src_mac[ETH_ALEN];
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	__u32 cur_dst_mac_offset;
3421da177e4SLinus Torvalds 	__u32 cur_src_mac_offset;
343252e3346SAl Viro 	__be32 cur_saddr;
344252e3346SAl Viro 	__be32 cur_daddr;
34566ed1e5eSEric Dumazet 	__u16 ip_id;
3461da177e4SLinus Torvalds 	__u16 cur_udp_dst;
3471da177e4SLinus Torvalds 	__u16 cur_udp_src;
34845b270f8SRobert Olsson 	__u16 cur_queue_map;
3491da177e4SLinus Torvalds 	__u32 cur_pkt_size;
350baac8564SEric Dumazet 	__u32 last_pkt_size;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	__u8 hh[14];
3531da177e4SLinus Torvalds 	/* = {
3541da177e4SLinus Torvalds 	   0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	   We fill in SRC address later
3571da177e4SLinus Torvalds 	   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3581da177e4SLinus Torvalds 	   0x08, 0x00
3591da177e4SLinus Torvalds 	   };
3601da177e4SLinus Torvalds 	 */
3611da177e4SLinus Torvalds 	__u16 pad;		/* pad out the hh struct to an even 16 bytes */
3621da177e4SLinus Torvalds 
36363adc6fbSStephen Hemminger 	struct sk_buff *skb;	/* skb we are to transmit next, used for when we
3641da177e4SLinus Torvalds 				 * are transmitting the same one multiple times
3651da177e4SLinus Torvalds 				 */
36663adc6fbSStephen Hemminger 	struct net_device *odev; /* The out-going device.
36763adc6fbSStephen Hemminger 				  * Note that the device should have it's
36863adc6fbSStephen Hemminger 				  * pg_info pointer pointing back to this
36963adc6fbSStephen Hemminger 				  * device.
37063adc6fbSStephen Hemminger 				  * Set when the user specifies the out-going
37163adc6fbSStephen Hemminger 				  * device name (not when the inject is
3721da177e4SLinus Torvalds 				  * started as it used to do.)
3731da177e4SLinus Torvalds 				  */
374593f63b0SEric Dumazet 	char odevname[32];
3751da177e4SLinus Torvalds 	struct flow_state *flows;
3761da177e4SLinus Torvalds 	unsigned cflows;	/* Concurrent flows (config) */
3771da177e4SLinus Torvalds 	unsigned lflow;		/* Flow length  (config) */
3781da177e4SLinus Torvalds 	unsigned nflows;	/* accumulated flows (stats) */
379007a531bSJamal Hadi Salim 	unsigned curfl;		/* current sequenced flow (state)*/
38045b270f8SRobert Olsson 
38145b270f8SRobert Olsson 	u16 queue_map_min;
38245b270f8SRobert Olsson 	u16 queue_map_max;
3839e50e3acSJohn Fastabend 	__u32 skb_priority;	/* skb priority field */
384e99b99b4SRobert Olsson 	int node;               /* Memory node */
38545b270f8SRobert Olsson 
386a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
387a553e4a6SJamal Hadi Salim 	__u8	ipsmode;		/* IPSEC mode (config) */
388a553e4a6SJamal Hadi Salim 	__u8	ipsproto;		/* IPSEC type (config) */
389a553e4a6SJamal Hadi Salim #endif
39039df232fSStephen Hemminger 	char result[512];
3911da177e4SLinus Torvalds };
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds struct pktgen_hdr {
394252e3346SAl Viro 	__be32 pgh_magic;
395252e3346SAl Viro 	__be32 seq_num;
396252e3346SAl Viro 	__be32 tv_sec;
397252e3346SAl Viro 	__be32 tv_usec;
3981da177e4SLinus Torvalds };
3991da177e4SLinus Torvalds 
400551eaff1SEric Dumazet static bool pktgen_exiting __read_mostly;
401551eaff1SEric Dumazet 
4021da177e4SLinus Torvalds struct pktgen_thread {
40363adc6fbSStephen Hemminger 	spinlock_t if_lock;		/* for list of devices */
404c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
405cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
406ee74baa7SDavid S. Miller 	struct task_struct *tsk;
4071da177e4SLinus Torvalds 	char result[512];
4081da177e4SLinus Torvalds 
40963adc6fbSStephen Hemminger 	/* Field for thread to receive "posted" events terminate,
41063adc6fbSStephen Hemminger 	   stop ifs etc. */
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	u32 control;
4131da177e4SLinus Torvalds 	int cpu;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	wait_queue_head_t queue;
416d3ede327SDenis V. Lunev 	struct completion start_done;
4171da177e4SLinus Torvalds };
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds #define REMOVE 1
4201da177e4SLinus Torvalds #define FIND   0
4211da177e4SLinus Torvalds 
422fd29cf72SStephen Hemminger static inline ktime_t ktime_now(void)
4231da177e4SLinus Torvalds {
424fd29cf72SStephen Hemminger 	struct timespec ts;
425fd29cf72SStephen Hemminger 	ktime_get_ts(&ts);
426fd29cf72SStephen Hemminger 
427fd29cf72SStephen Hemminger 	return timespec_to_ktime(ts);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430fd29cf72SStephen Hemminger /* This works even if 32 bit because of careful byte order choice */
431fd29cf72SStephen Hemminger static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
4321da177e4SLinus Torvalds {
433fd29cf72SStephen Hemminger 	return cmp1.tv64 < cmp2.tv64;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
436c3d2f52dSStephen Hemminger static const char version[] =
437f9467eaeSJoe Perches 	"Packet Generator for packet performance testing. "
438f9467eaeSJoe Perches 	"Version: " VERSION "\n";
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4411da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
442222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
4433e984840SEric Dumazet 					  const char *ifname, bool exact);
4441da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
4451da177e4SLinus Torvalds static void pktgen_run_all_threads(void);
446eb37b41cSJesse Brandeburg static void pktgen_reset_all_threads(void);
4471da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void);
4483bda06a3SStephen Hemminger 
4491da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4501da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
45139df232fSStephen Hemminger 
4521da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]);
4531da177e4SLinus Torvalds static unsigned int fmt_ip6(char *s, const char ip[16]);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /* Module parameters, defaults. */
45665c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
45765c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
45865c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
45965c5b786SStephen Hemminger static int debug  __read_mostly;
4601da177e4SLinus Torvalds 
461222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
462cdcdbe0bSLuiz Capitulino static LIST_HEAD(pktgen_threads);
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4651da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4661da177e4SLinus Torvalds };
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /*
4691da177e4SLinus Torvalds  * /proc handling functions
4701da177e4SLinus Torvalds  *
4711da177e4SLinus Torvalds  */
4721da177e4SLinus Torvalds 
473d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
474d50a6b56SStephen Hemminger {
475c3d2f52dSStephen Hemminger 	seq_puts(seq, version);
476d50a6b56SStephen Hemminger 	return 0;
477d50a6b56SStephen Hemminger }
4781da177e4SLinus Torvalds 
479d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user *buf,
4801da177e4SLinus Torvalds 			    size_t count, loff_t *ppos)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	int err = 0;
483d50a6b56SStephen Hemminger 	char data[128];
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN)) {
4861da177e4SLinus Torvalds 		err = -EPERM;
4871da177e4SLinus Torvalds 		goto out;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 
490d50a6b56SStephen Hemminger 	if (count > sizeof(data))
491d50a6b56SStephen Hemminger 		count = sizeof(data);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count)) {
4941da177e4SLinus Torvalds 		err = -EFAULT;
495d50a6b56SStephen Hemminger 		goto out;
4961da177e4SLinus Torvalds 	}
4971da177e4SLinus Torvalds 	data[count - 1] = 0;	/* Make string */
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
5001da177e4SLinus Torvalds 		pktgen_stop_all_threads_ifs();
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
5031da177e4SLinus Torvalds 		pktgen_run_all_threads();
5041da177e4SLinus Torvalds 
505eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
506eb37b41cSJesse Brandeburg 		pktgen_reset_all_threads();
507eb37b41cSJesse Brandeburg 
5081da177e4SLinus Torvalds 	else
509f9467eaeSJoe Perches 		pr_warning("Unknown command: %s\n", data);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	err = count;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds out:
5141da177e4SLinus Torvalds 	return err;
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
517d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
5181da177e4SLinus Torvalds {
519d50a6b56SStephen Hemminger 	return single_open(file, pgctrl_show, PDE(inode)->data);
520d50a6b56SStephen Hemminger }
521d50a6b56SStephen Hemminger 
5229a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
523d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
524d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
525d50a6b56SStephen Hemminger 	.read    = seq_read,
526d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
527d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
528d50a6b56SStephen Hemminger 	.release = single_release,
529d50a6b56SStephen Hemminger };
530d50a6b56SStephen Hemminger 
531d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
532d50a6b56SStephen Hemminger {
533648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
534fd29cf72SStephen Hemminger 	ktime_t stopped;
535fd29cf72SStephen Hemminger 	u64 idle;
5361da177e4SLinus Torvalds 
537222f1806SLuiz Capitulino 	seq_printf(seq,
538222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
539222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
540222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5411da177e4SLinus Torvalds 
542222f1806SLuiz Capitulino 	seq_printf(seq,
543fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
544fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
545593f63b0SEric Dumazet 		   pkt_dev->clone_skb, pkt_dev->odevname);
5461da177e4SLinus Torvalds 
547222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
548222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5491da177e4SLinus Torvalds 
55045b270f8SRobert Olsson 	seq_printf(seq,
55145b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
55245b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
55345b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
55445b270f8SRobert Olsson 
5559e50e3acSJohn Fastabend 	if (pkt_dev->skb_priority)
5569e50e3acSJohn Fastabend 		seq_printf(seq, "     skb_priority: %u\n",
5579e50e3acSJohn Fastabend 			   pkt_dev->skb_priority);
5589e50e3acSJohn Fastabend 
5591da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
5601da177e4SLinus Torvalds 		char b1[128], b2[128], b3[128];
5611da177e4SLinus Torvalds 		fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
5621da177e4SLinus Torvalds 		fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
5631da177e4SLinus Torvalds 		fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
564222f1806SLuiz Capitulino 		seq_printf(seq,
565222f1806SLuiz Capitulino 			   "     saddr: %s  min_saddr: %s  max_saddr: %s\n", b1,
566222f1806SLuiz Capitulino 			   b2, b3);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 		fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
5691da177e4SLinus Torvalds 		fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
5701da177e4SLinus Torvalds 		fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
571222f1806SLuiz Capitulino 		seq_printf(seq,
572222f1806SLuiz Capitulino 			   "     daddr: %s  min_daddr: %s  max_daddr: %s\n", b1,
573222f1806SLuiz Capitulino 			   b2, b3);
5741da177e4SLinus Torvalds 
57563adc6fbSStephen Hemminger 	} else {
576222f1806SLuiz Capitulino 		seq_printf(seq,
57763adc6fbSStephen Hemminger 			   "     dst_min: %s  dst_max: %s\n",
57863adc6fbSStephen Hemminger 			   pkt_dev->dst_min, pkt_dev->dst_max);
57963adc6fbSStephen Hemminger 		seq_printf(seq,
58063adc6fbSStephen Hemminger 			   "        src_min: %s  src_max: %s\n",
58163adc6fbSStephen Hemminger 			   pkt_dev->src_min, pkt_dev->src_max);
58263adc6fbSStephen Hemminger 	}
5831da177e4SLinus Torvalds 
584d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5851da177e4SLinus Torvalds 
586e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
587e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
588e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5891da177e4SLinus Torvalds 
590d50a6b56SStephen Hemminger 	seq_printf(seq, "dst_mac: ");
591e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
5921da177e4SLinus Torvalds 
593222f1806SLuiz Capitulino 	seq_printf(seq,
59463adc6fbSStephen Hemminger 		   "     udp_src_min: %d  udp_src_max: %d"
59563adc6fbSStephen Hemminger 		   "  udp_dst_min: %d  udp_dst_max: %d\n",
596222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
597222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
5981da177e4SLinus Torvalds 
599222f1806SLuiz Capitulino 	seq_printf(seq,
600ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
6011da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
6021da177e4SLinus Torvalds 
603ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
604ca6549afSSteven Whitehouse 		unsigned i;
605ca6549afSSteven Whitehouse 		seq_printf(seq, "     mpls: ");
606ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
607ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
608ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
609ca6549afSSteven Whitehouse 	}
610ca6549afSSteven Whitehouse 
61163adc6fbSStephen Hemminger 	if (pkt_dev->vlan_id != 0xffff)
61234954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
61363adc6fbSStephen Hemminger 			   pkt_dev->vlan_id, pkt_dev->vlan_p,
61463adc6fbSStephen Hemminger 			   pkt_dev->vlan_cfi);
61534954ddcSFrancesco Fondelli 
61663adc6fbSStephen Hemminger 	if (pkt_dev->svlan_id != 0xffff)
61734954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
61863adc6fbSStephen Hemminger 			   pkt_dev->svlan_id, pkt_dev->svlan_p,
61963adc6fbSStephen Hemminger 			   pkt_dev->svlan_cfi);
62034954ddcSFrancesco Fondelli 
62163adc6fbSStephen Hemminger 	if (pkt_dev->tos)
6221ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
6231ca7768cSFrancesco Fondelli 
62463adc6fbSStephen Hemminger 	if (pkt_dev->traffic_class)
6251ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
6261ca7768cSFrancesco Fondelli 
627e99b99b4SRobert Olsson 	if (pkt_dev->node >= 0)
628e99b99b4SRobert Olsson 		seq_printf(seq, "     node: %d\n", pkt_dev->node);
629e99b99b4SRobert Olsson 
630ca6549afSSteven Whitehouse 	seq_printf(seq, "     Flags: ");
631ca6549afSSteven Whitehouse 
6321da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
633d50a6b56SStephen Hemminger 		seq_printf(seq, "IPV6  ");
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPSRC_RND)
636d50a6b56SStephen Hemminger 		seq_printf(seq, "IPSRC_RND  ");
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPDST_RND)
639d50a6b56SStephen Hemminger 		seq_printf(seq, "IPDST_RND  ");
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	if (pkt_dev->flags & F_TXSIZE_RND)
642d50a6b56SStephen Hemminger 		seq_printf(seq, "TXSIZE_RND  ");
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPSRC_RND)
645d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPSRC_RND  ");
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPDST_RND)
648d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPDST_RND  ");
6491da177e4SLinus Torvalds 
650ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND)
651ca6549afSSteven Whitehouse 		seq_printf(seq,  "MPLS_RND  ");
652ca6549afSSteven Whitehouse 
65345b270f8SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_RND)
65445b270f8SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_RND  ");
65545b270f8SRobert Olsson 
656e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
657e6fce5b9SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_CPU  ");
658e6fce5b9SRobert Olsson 
659007a531bSJamal Hadi Salim 	if (pkt_dev->cflows) {
660007a531bSJamal Hadi Salim 		if (pkt_dev->flags & F_FLOW_SEQ)
661007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_SEQ  "); /*in sequence flows*/
662007a531bSJamal Hadi Salim 		else
663007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_RND  ");
664007a531bSJamal Hadi Salim 	}
665007a531bSJamal Hadi Salim 
666a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
667a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON)
668a553e4a6SJamal Hadi Salim 		seq_printf(seq,  "IPSEC  ");
669a553e4a6SJamal Hadi Salim #endif
670a553e4a6SJamal Hadi Salim 
6711da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACSRC_RND)
672d50a6b56SStephen Hemminger 		seq_printf(seq, "MACSRC_RND  ");
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACDST_RND)
675d50a6b56SStephen Hemminger 		seq_printf(seq, "MACDST_RND  ");
6761da177e4SLinus Torvalds 
67734954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_VID_RND)
67834954ddcSFrancesco Fondelli 		seq_printf(seq, "VID_RND  ");
67934954ddcSFrancesco Fondelli 
68034954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_SVID_RND)
68134954ddcSFrancesco Fondelli 		seq_printf(seq, "SVID_RND  ");
68234954ddcSFrancesco Fondelli 
683e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE)
684e99b99b4SRobert Olsson 		seq_printf(seq, "NODE_ALLOC  ");
685e99b99b4SRobert Olsson 
686d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6871da177e4SLinus Torvalds 
688fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
689fd29cf72SStephen Hemminger 	stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
690fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
691fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
6921da177e4SLinus Torvalds 
693222f1806SLuiz Capitulino 	seq_printf(seq,
694fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
6951da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
696fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
697fd29cf72SStephen Hemminger 
698fd29cf72SStephen Hemminger 	seq_printf(seq,
699fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
700fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
701fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
702fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
7031da177e4SLinus Torvalds 
704222f1806SLuiz Capitulino 	seq_printf(seq,
705222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
706d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
707d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
7101da177e4SLinus Torvalds 		char b1[128], b2[128];
7111da177e4SLinus Torvalds 		fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
7121da177e4SLinus Torvalds 		fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
713d50a6b56SStephen Hemminger 		seq_printf(seq, "     cur_saddr: %s  cur_daddr: %s\n", b2, b1);
714222f1806SLuiz Capitulino 	} else
715d50a6b56SStephen Hemminger 		seq_printf(seq, "     cur_saddr: 0x%x  cur_daddr: 0x%x\n",
7161da177e4SLinus Torvalds 			   pkt_dev->cur_saddr, pkt_dev->cur_daddr);
7171da177e4SLinus Torvalds 
718d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
7191da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
7201da177e4SLinus Torvalds 
72145b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
72245b270f8SRobert Olsson 
723d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	if (pkt_dev->result[0])
726d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
7271da177e4SLinus Torvalds 	else
728d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: Idle\n");
7291da177e4SLinus Torvalds 
730d50a6b56SStephen Hemminger 	return 0;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
733ca6549afSSteven Whitehouse 
73463adc6fbSStephen Hemminger static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
73563adc6fbSStephen Hemminger 		     __u32 *num)
736ca6549afSSteven Whitehouse {
737ca6549afSSteven Whitehouse 	int i = 0;
738ca6549afSSteven Whitehouse 	*num = 0;
739ca6549afSSteven Whitehouse 
7401ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
74182fd5b5dSAndy Shevchenko 		int value;
742ca6549afSSteven Whitehouse 		char c;
743ca6549afSSteven Whitehouse 		*num <<= 4;
744ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
745ca6549afSSteven Whitehouse 			return -EFAULT;
74682fd5b5dSAndy Shevchenko 		value = hex_to_bin(c);
74782fd5b5dSAndy Shevchenko 		if (value >= 0)
74882fd5b5dSAndy Shevchenko 			*num |= value;
749ca6549afSSteven Whitehouse 		else
750ca6549afSSteven Whitehouse 			break;
751ca6549afSSteven Whitehouse 	}
752ca6549afSSteven Whitehouse 	return i;
753ca6549afSSteven Whitehouse }
754ca6549afSSteven Whitehouse 
755222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
756222f1806SLuiz Capitulino 			     unsigned int maxlen)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	int i;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7611da177e4SLinus Torvalds 		char c;
7621da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7631da177e4SLinus Torvalds 			return -EFAULT;
7641da177e4SLinus Torvalds 		switch (c) {
7651da177e4SLinus Torvalds 		case '\"':
7661da177e4SLinus Torvalds 		case '\n':
7671da177e4SLinus Torvalds 		case '\r':
7681da177e4SLinus Torvalds 		case '\t':
7691da177e4SLinus Torvalds 		case ' ':
7701da177e4SLinus Torvalds 		case '=':
7711da177e4SLinus Torvalds 			break;
7721da177e4SLinus Torvalds 		default:
7731da177e4SLinus Torvalds 			goto done;
7743ff50b79SStephen Hemminger 		}
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds done:
7771da177e4SLinus Torvalds 	return i;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
780222f1806SLuiz Capitulino static unsigned long num_arg(const char __user * user_buffer,
781222f1806SLuiz Capitulino 			     unsigned long maxlen, unsigned long *num)
7821da177e4SLinus Torvalds {
783d6182223SPaul Gortmaker 	int i;
7841da177e4SLinus Torvalds 	*num = 0;
7851da177e4SLinus Torvalds 
786d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
7871da177e4SLinus Torvalds 		char c;
7881da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7891da177e4SLinus Torvalds 			return -EFAULT;
7901da177e4SLinus Torvalds 		if ((c >= '0') && (c <= '9')) {
7911da177e4SLinus Torvalds 			*num *= 10;
7921da177e4SLinus Torvalds 			*num += c - '0';
7931da177e4SLinus Torvalds 		} else
7941da177e4SLinus Torvalds 			break;
7951da177e4SLinus Torvalds 	}
7961da177e4SLinus Torvalds 	return i;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
8001da177e4SLinus Torvalds {
801d6182223SPaul Gortmaker 	int i;
8021da177e4SLinus Torvalds 
803d6182223SPaul Gortmaker 	for (i = 0; i < maxlen; i++) {
8041da177e4SLinus Torvalds 		char c;
8051da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
8061da177e4SLinus Torvalds 			return -EFAULT;
8071da177e4SLinus Torvalds 		switch (c) {
8081da177e4SLinus Torvalds 		case '\"':
8091da177e4SLinus Torvalds 		case '\n':
8101da177e4SLinus Torvalds 		case '\r':
8111da177e4SLinus Torvalds 		case '\t':
8121da177e4SLinus Torvalds 		case ' ':
8131da177e4SLinus Torvalds 			goto done_str;
8141da177e4SLinus Torvalds 			break;
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 {
825ca6549afSSteven Whitehouse 	unsigned 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) {
868f9467eaeSJoe Perches 		pr_warning("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) {
875f9467eaeSJoe Perches 		pr_warning("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;
90425a8b254SDavid S. Miller 		printk(KERN_DEBUG "pktgen: %s,%lu  buffer -:%s:-\n", name,
905d50a6b56SStephen Hemminger 		       (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;
108363adc6fbSStephen Hemminger 
10841da177e4SLinus Torvalds 		i += len;
10851da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
10881da177e4SLinus Torvalds 		return count;
10891da177e4SLinus Torvalds 	}
10901da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
10911da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
109263adc6fbSStephen Hemminger 		if (len < 0)
1093222f1806SLuiz Capitulino 			return len;
109463adc6fbSStephen Hemminger 
10951da177e4SLinus Torvalds 		i += len;
10961da177e4SLinus Torvalds 		pkt_dev->count = value;
10971da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
10981da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
10991da177e4SLinus Torvalds 		return count;
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
11021da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
110363adc6fbSStephen Hemminger 		if (len < 0)
1104222f1806SLuiz Capitulino 			return len;
110563adc6fbSStephen Hemminger 
11061da177e4SLinus Torvalds 		i += len;
11071da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
11081da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
11091da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
11101da177e4SLinus Torvalds 		}
1111222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1112222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
11131da177e4SLinus Torvalds 		return count;
11141da177e4SLinus Torvalds 	}
11151da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
11161da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
111763adc6fbSStephen Hemminger 		if (len < 0)
1118222f1806SLuiz Capitulino 			return len;
111963adc6fbSStephen Hemminger 
11201da177e4SLinus Torvalds 		i += len;
11211da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
11221da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
11231da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
11241da177e4SLinus Torvalds 		}
1125222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1126222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
11271da177e4SLinus Torvalds 		return count;
11281da177e4SLinus Torvalds 	}
1129e99b99b4SRobert Olsson 	if (!strcmp(name, "node")) {
1130e99b99b4SRobert Olsson 		len = num_arg(&user_buffer[i], 10, &value);
1131e99b99b4SRobert Olsson 		if (len < 0)
1132e99b99b4SRobert Olsson 			return len;
1133e99b99b4SRobert Olsson 
1134e99b99b4SRobert Olsson 		i += len;
1135e99b99b4SRobert Olsson 
1136e99b99b4SRobert Olsson 		if (node_possible(value)) {
1137e99b99b4SRobert Olsson 			pkt_dev->node = value;
1138e99b99b4SRobert Olsson 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
113926ad7879SEric Dumazet 			if (pkt_dev->page) {
114026ad7879SEric Dumazet 				put_page(pkt_dev->page);
114126ad7879SEric Dumazet 				pkt_dev->page = NULL;
114226ad7879SEric Dumazet 			}
1143e99b99b4SRobert Olsson 		}
1144e99b99b4SRobert Olsson 		else
1145e99b99b4SRobert Olsson 			sprintf(pg_result, "ERROR: node not possible");
1146e99b99b4SRobert Olsson 		return count;
1147e99b99b4SRobert Olsson 	}
11481da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
11491da177e4SLinus Torvalds 		char f[32];
11501da177e4SLinus Torvalds 		memset(f, 0, 32);
11511da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
115263adc6fbSStephen Hemminger 		if (len < 0)
1153222f1806SLuiz Capitulino 			return len;
115463adc6fbSStephen Hemminger 
11551da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
11561da177e4SLinus Torvalds 			return -EFAULT;
11571da177e4SLinus Torvalds 		i += len;
11581da177e4SLinus Torvalds 		if (strcmp(f, "IPSRC_RND") == 0)
11591da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPSRC_RND;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 		else if (strcmp(f, "!IPSRC_RND") == 0)
11621da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPSRC_RND;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 		else if (strcmp(f, "TXSIZE_RND") == 0)
11651da177e4SLinus Torvalds 			pkt_dev->flags |= F_TXSIZE_RND;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 		else if (strcmp(f, "!TXSIZE_RND") == 0)
11681da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_TXSIZE_RND;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		else if (strcmp(f, "IPDST_RND") == 0)
11711da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPDST_RND;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 		else if (strcmp(f, "!IPDST_RND") == 0)
11741da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPDST_RND;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 		else if (strcmp(f, "UDPSRC_RND") == 0)
11771da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPSRC_RND;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPSRC_RND") == 0)
11801da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPSRC_RND;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 		else if (strcmp(f, "UDPDST_RND") == 0)
11831da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPDST_RND;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPDST_RND") == 0)
11861da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPDST_RND;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 		else if (strcmp(f, "MACSRC_RND") == 0)
11891da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACSRC_RND;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 		else if (strcmp(f, "!MACSRC_RND") == 0)
11921da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACSRC_RND;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 		else if (strcmp(f, "MACDST_RND") == 0)
11951da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACDST_RND;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 		else if (strcmp(f, "!MACDST_RND") == 0)
11981da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACDST_RND;
11991da177e4SLinus Torvalds 
1200ca6549afSSteven Whitehouse 		else if (strcmp(f, "MPLS_RND") == 0)
1201ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
1202ca6549afSSteven Whitehouse 
1203ca6549afSSteven Whitehouse 		else if (strcmp(f, "!MPLS_RND") == 0)
1204ca6549afSSteven Whitehouse 			pkt_dev->flags &= ~F_MPLS_RND;
1205ca6549afSSteven Whitehouse 
120634954ddcSFrancesco Fondelli 		else if (strcmp(f, "VID_RND") == 0)
120734954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_VID_RND;
120834954ddcSFrancesco Fondelli 
120934954ddcSFrancesco Fondelli 		else if (strcmp(f, "!VID_RND") == 0)
121034954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_VID_RND;
121134954ddcSFrancesco Fondelli 
121234954ddcSFrancesco Fondelli 		else if (strcmp(f, "SVID_RND") == 0)
121334954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_SVID_RND;
121434954ddcSFrancesco Fondelli 
121534954ddcSFrancesco Fondelli 		else if (strcmp(f, "!SVID_RND") == 0)
121634954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_SVID_RND;
121734954ddcSFrancesco Fondelli 
1218007a531bSJamal Hadi Salim 		else if (strcmp(f, "FLOW_SEQ") == 0)
1219007a531bSJamal Hadi Salim 			pkt_dev->flags |= F_FLOW_SEQ;
1220007a531bSJamal Hadi Salim 
122145b270f8SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
122245b270f8SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_RND;
122345b270f8SRobert Olsson 
122445b270f8SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
122545b270f8SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1226e6fce5b9SRobert Olsson 
1227e6fce5b9SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1228e6fce5b9SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1229e6fce5b9SRobert Olsson 
1230e6fce5b9SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1231e6fce5b9SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1232a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
1233a553e4a6SJamal Hadi Salim 		else if (strcmp(f, "IPSEC") == 0)
1234a553e4a6SJamal Hadi Salim 			pkt_dev->flags |= F_IPSEC_ON;
1235a553e4a6SJamal Hadi Salim #endif
1236a553e4a6SJamal Hadi Salim 
12371ca7768cSFrancesco Fondelli 		else if (strcmp(f, "!IPV6") == 0)
12381ca7768cSFrancesco Fondelli 			pkt_dev->flags &= ~F_IPV6;
12391ca7768cSFrancesco Fondelli 
1240e99b99b4SRobert Olsson 		else if (strcmp(f, "NODE_ALLOC") == 0)
1241e99b99b4SRobert Olsson 			pkt_dev->flags |= F_NODE;
1242e99b99b4SRobert Olsson 
1243e99b99b4SRobert Olsson 		else if (strcmp(f, "!NODE_ALLOC") == 0)
1244e99b99b4SRobert Olsson 			pkt_dev->flags &= ~F_NODE;
1245e99b99b4SRobert Olsson 
12461da177e4SLinus Torvalds 		else {
1247222f1806SLuiz Capitulino 			sprintf(pg_result,
1248222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
12491da177e4SLinus Torvalds 				f,
12501ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1251e99b99b4SRobert Olsson 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
12521da177e4SLinus Torvalds 			return count;
12531da177e4SLinus Torvalds 		}
12541da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
12551da177e4SLinus Torvalds 		return count;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
12581da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
125963adc6fbSStephen Hemminger 		if (len < 0)
1260222f1806SLuiz Capitulino 			return len;
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12631da177e4SLinus Torvalds 			return -EFAULT;
12641da177e4SLinus Torvalds 		buf[len] = 0;
12651da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
12661da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
12671da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_min, buf, len);
12681da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
12691da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
12701da177e4SLinus Torvalds 		}
12711da177e4SLinus Torvalds 		if (debug)
127225a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1273222f1806SLuiz Capitulino 			       pkt_dev->dst_min);
12741da177e4SLinus Torvalds 		i += len;
12751da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
12761da177e4SLinus Torvalds 		return count;
12771da177e4SLinus Torvalds 	}
12781da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
12791da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
128063adc6fbSStephen Hemminger 		if (len < 0)
1281222f1806SLuiz Capitulino 			return len;
128263adc6fbSStephen Hemminger 
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12851da177e4SLinus Torvalds 			return -EFAULT;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 		buf[len] = 0;
12881da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
12891da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
12901da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_max, buf, len);
12911da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
12921da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
12931da177e4SLinus Torvalds 		}
12941da177e4SLinus Torvalds 		if (debug)
129525a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1296222f1806SLuiz Capitulino 			       pkt_dev->dst_max);
12971da177e4SLinus Torvalds 		i += len;
12981da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
12991da177e4SLinus Torvalds 		return count;
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
13021da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1303222f1806SLuiz Capitulino 		if (len < 0)
1304222f1806SLuiz Capitulino 			return len;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13091da177e4SLinus Torvalds 			return -EFAULT;
13101da177e4SLinus Torvalds 		buf[len] = 0;
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
13131da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 		if (debug)
131825a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 		i += len;
13211da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
13221da177e4SLinus Torvalds 		return count;
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
13251da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1326222f1806SLuiz Capitulino 		if (len < 0)
1327222f1806SLuiz Capitulino 			return len;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13321da177e4SLinus Torvalds 			return -EFAULT;
13331da177e4SLinus Torvalds 		buf[len] = 0;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
13361da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
13371da177e4SLinus Torvalds 
1338222f1806SLuiz Capitulino 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1339222f1806SLuiz Capitulino 			       &pkt_dev->min_in6_daddr);
13401da177e4SLinus Torvalds 		if (debug)
134125a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 		i += len;
13441da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
13451da177e4SLinus Torvalds 		return count;
13461da177e4SLinus Torvalds 	}
13471da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
13481da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1349222f1806SLuiz Capitulino 		if (len < 0)
1350222f1806SLuiz Capitulino 			return len;
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13551da177e4SLinus Torvalds 			return -EFAULT;
13561da177e4SLinus Torvalds 		buf[len] = 0;
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
13591da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 		if (debug)
136225a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 		i += len;
13651da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
13661da177e4SLinus Torvalds 		return count;
13671da177e4SLinus Torvalds 	}
13681da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
13691da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1370222f1806SLuiz Capitulino 		if (len < 0)
1371222f1806SLuiz Capitulino 			return len;
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13761da177e4SLinus Torvalds 			return -EFAULT;
13771da177e4SLinus Torvalds 		buf[len] = 0;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
13801da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 		ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 		if (debug)
138525a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 		i += len;
13881da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
13891da177e4SLinus Torvalds 		return count;
13901da177e4SLinus Torvalds 	}
13911da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
13921da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
139363adc6fbSStephen Hemminger 		if (len < 0)
1394222f1806SLuiz Capitulino 			return len;
139563adc6fbSStephen Hemminger 
13961da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13971da177e4SLinus Torvalds 			return -EFAULT;
13981da177e4SLinus Torvalds 		buf[len] = 0;
13991da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
14001da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
14011da177e4SLinus Torvalds 			strncpy(pkt_dev->src_min, buf, len);
14021da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
14031da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
14041da177e4SLinus Torvalds 		}
14051da177e4SLinus Torvalds 		if (debug)
140625a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1407222f1806SLuiz Capitulino 			       pkt_dev->src_min);
14081da177e4SLinus Torvalds 		i += len;
14091da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
14101da177e4SLinus Torvalds 		return count;
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
14131da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
141463adc6fbSStephen Hemminger 		if (len < 0)
1415222f1806SLuiz Capitulino 			return len;
141663adc6fbSStephen Hemminger 
14171da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
14181da177e4SLinus Torvalds 			return -EFAULT;
14191da177e4SLinus Torvalds 		buf[len] = 0;
14201da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
14211da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
14221da177e4SLinus Torvalds 			strncpy(pkt_dev->src_max, buf, len);
14231da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
14241da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
14251da177e4SLinus Torvalds 		}
14261da177e4SLinus Torvalds 		if (debug)
142725a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1428222f1806SLuiz Capitulino 			       pkt_dev->src_max);
14291da177e4SLinus Torvalds 		i += len;
14301da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
14311da177e4SLinus Torvalds 		return count;
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
14341da177e4SLinus Torvalds 		char *v = valstr;
1435f404e9a6SKris Katterjohn 		unsigned char old_dmac[ETH_ALEN];
14361da177e4SLinus Torvalds 		unsigned char *m = pkt_dev->dst_mac;
1437f404e9a6SKris Katterjohn 		memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
144063adc6fbSStephen Hemminger 		if (len < 0)
1441222f1806SLuiz Capitulino 			return len;
144263adc6fbSStephen Hemminger 
14431da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14441da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14451da177e4SLinus Torvalds 			return -EFAULT;
14461da177e4SLinus Torvalds 		i += len;
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 		for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1449451e07a2SAndy Shevchenko 			int value;
1450451e07a2SAndy Shevchenko 
1451451e07a2SAndy Shevchenko 			value = hex_to_bin(*v);
1452451e07a2SAndy Shevchenko 			if (value >= 0)
1453451e07a2SAndy Shevchenko 				*m = *m * 16 + value;
1454451e07a2SAndy Shevchenko 
14551da177e4SLinus Torvalds 			if (*v == ':') {
14561da177e4SLinus Torvalds 				m++;
14571da177e4SLinus Torvalds 				*m = 0;
14581da177e4SLinus Torvalds 			}
14591da177e4SLinus Torvalds 		}
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 		/* Set up Dest MAC */
1462f404e9a6SKris Katterjohn 		if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1463f404e9a6SKris Katterjohn 			memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dstmac");
14661da177e4SLinus Torvalds 		return count;
14671da177e4SLinus Torvalds 	}
14681da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
14691da177e4SLinus Torvalds 		char *v = valstr;
1470ce5d0b47SAdit Ranadive 		unsigned char old_smac[ETH_ALEN];
14711da177e4SLinus Torvalds 		unsigned char *m = pkt_dev->src_mac;
14721da177e4SLinus Torvalds 
1473ce5d0b47SAdit Ranadive 		memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1474ce5d0b47SAdit Ranadive 
14751da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
147663adc6fbSStephen Hemminger 		if (len < 0)
1477222f1806SLuiz Capitulino 			return len;
147863adc6fbSStephen Hemminger 
14791da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14801da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14811da177e4SLinus Torvalds 			return -EFAULT;
14821da177e4SLinus Torvalds 		i += len;
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 		for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1485451e07a2SAndy Shevchenko 			int value;
1486451e07a2SAndy Shevchenko 
1487451e07a2SAndy Shevchenko 			value = hex_to_bin(*v);
1488451e07a2SAndy Shevchenko 			if (value >= 0)
1489451e07a2SAndy Shevchenko 				*m = *m * 16 + value;
1490451e07a2SAndy Shevchenko 
14911da177e4SLinus Torvalds 			if (*v == ':') {
14921da177e4SLinus Torvalds 				m++;
14931da177e4SLinus Torvalds 				*m = 0;
14941da177e4SLinus Torvalds 			}
14951da177e4SLinus Torvalds 		}
14961da177e4SLinus Torvalds 
1497ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
1498ce5d0b47SAdit Ranadive 		if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1499ce5d0b47SAdit Ranadive 			memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1500ce5d0b47SAdit Ranadive 
15011da177e4SLinus Torvalds 		sprintf(pg_result, "OK: srcmac");
15021da177e4SLinus Torvalds 		return count;
15031da177e4SLinus Torvalds 	}
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
15061da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
15071da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
15081da177e4SLinus Torvalds 		return count;
15091da177e4SLinus Torvalds 	}
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
15121da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
151363adc6fbSStephen Hemminger 		if (len < 0)
1514222f1806SLuiz Capitulino 			return len;
151563adc6fbSStephen Hemminger 
15161da177e4SLinus Torvalds 		i += len;
15171da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
15181da177e4SLinus Torvalds 			value = MAX_CFLOWS;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 		pkt_dev->cflows = value;
15211da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
15221da177e4SLinus Torvalds 		return count;
15231da177e4SLinus Torvalds 	}
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
15261da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
152763adc6fbSStephen Hemminger 		if (len < 0)
1528222f1806SLuiz Capitulino 			return len;
152963adc6fbSStephen Hemminger 
15301da177e4SLinus Torvalds 		i += len;
15311da177e4SLinus Torvalds 		pkt_dev->lflow = value;
15321da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
15331da177e4SLinus Torvalds 		return count;
15341da177e4SLinus Torvalds 	}
15351da177e4SLinus Torvalds 
153645b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
153745b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
153863adc6fbSStephen Hemminger 		if (len < 0)
153945b270f8SRobert Olsson 			return len;
154063adc6fbSStephen Hemminger 
154145b270f8SRobert Olsson 		i += len;
154245b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
154345b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
154445b270f8SRobert Olsson 		return count;
154545b270f8SRobert Olsson 	}
154645b270f8SRobert Olsson 
154745b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
154845b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
154963adc6fbSStephen Hemminger 		if (len < 0)
155045b270f8SRobert Olsson 			return len;
155163adc6fbSStephen Hemminger 
155245b270f8SRobert Olsson 		i += len;
155345b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
155445b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
155545b270f8SRobert Olsson 		return count;
155645b270f8SRobert Olsson 	}
155745b270f8SRobert Olsson 
1558ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
1559cfcabdccSStephen Hemminger 		unsigned n, cnt;
1560cfcabdccSStephen Hemminger 
1561ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1562cfcabdccSStephen Hemminger 		if (len < 0)
1563cfcabdccSStephen Hemminger 			return len;
1564ca6549afSSteven Whitehouse 		i += len;
1565cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1566ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1567cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1568ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1569ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
157034954ddcSFrancesco Fondelli 
157134954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
157234954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
157334954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
157434954ddcSFrancesco Fondelli 
157534954ddcSFrancesco Fondelli 			if (debug)
157625a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
157734954ddcSFrancesco Fondelli 		}
157834954ddcSFrancesco Fondelli 		return count;
157934954ddcSFrancesco Fondelli 	}
158034954ddcSFrancesco Fondelli 
158134954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
158234954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
158363adc6fbSStephen Hemminger 		if (len < 0)
158434954ddcSFrancesco Fondelli 			return len;
158563adc6fbSStephen Hemminger 
158634954ddcSFrancesco Fondelli 		i += len;
158734954ddcSFrancesco Fondelli 		if (value <= 4095) {
158834954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
158934954ddcSFrancesco Fondelli 
159034954ddcSFrancesco Fondelli 			if (debug)
159125a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN turned on\n");
159234954ddcSFrancesco Fondelli 
159334954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
159425a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
159534954ddcSFrancesco Fondelli 
159634954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
159734954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
159834954ddcSFrancesco Fondelli 		} else {
159934954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
160034954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
160134954ddcSFrancesco Fondelli 
160234954ddcSFrancesco Fondelli 			if (debug)
160325a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
160434954ddcSFrancesco Fondelli 		}
160534954ddcSFrancesco Fondelli 		return count;
160634954ddcSFrancesco Fondelli 	}
160734954ddcSFrancesco Fondelli 
160834954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
160934954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
161063adc6fbSStephen Hemminger 		if (len < 0)
161134954ddcSFrancesco Fondelli 			return len;
161263adc6fbSStephen Hemminger 
161334954ddcSFrancesco Fondelli 		i += len;
161434954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
161534954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
161634954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
161734954ddcSFrancesco Fondelli 		} else {
161834954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
161934954ddcSFrancesco Fondelli 		}
162034954ddcSFrancesco Fondelli 		return count;
162134954ddcSFrancesco Fondelli 	}
162234954ddcSFrancesco Fondelli 
162334954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
162434954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
162563adc6fbSStephen Hemminger 		if (len < 0)
162634954ddcSFrancesco Fondelli 			return len;
162763adc6fbSStephen Hemminger 
162834954ddcSFrancesco Fondelli 		i += len;
162934954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
163034954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
163134954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
163234954ddcSFrancesco Fondelli 		} else {
163334954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
163434954ddcSFrancesco Fondelli 		}
163534954ddcSFrancesco Fondelli 		return count;
163634954ddcSFrancesco Fondelli 	}
163734954ddcSFrancesco Fondelli 
163834954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
163934954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
164063adc6fbSStephen Hemminger 		if (len < 0)
164134954ddcSFrancesco Fondelli 			return len;
164263adc6fbSStephen Hemminger 
164334954ddcSFrancesco Fondelli 		i += len;
164434954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
164534954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
164634954ddcSFrancesco Fondelli 
164734954ddcSFrancesco Fondelli 			if (debug)
164825a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
164934954ddcSFrancesco Fondelli 
165034954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
165125a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
165234954ddcSFrancesco Fondelli 
165334954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
165434954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
165534954ddcSFrancesco Fondelli 		} else {
165634954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
165734954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
165834954ddcSFrancesco Fondelli 
165934954ddcSFrancesco Fondelli 			if (debug)
166025a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
166134954ddcSFrancesco Fondelli 		}
166234954ddcSFrancesco Fondelli 		return count;
166334954ddcSFrancesco Fondelli 	}
166434954ddcSFrancesco Fondelli 
166534954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
166634954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
166763adc6fbSStephen Hemminger 		if (len < 0)
166834954ddcSFrancesco Fondelli 			return len;
166963adc6fbSStephen Hemminger 
167034954ddcSFrancesco Fondelli 		i += len;
167134954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
167234954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
167334954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
167434954ddcSFrancesco Fondelli 		} else {
167534954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
167634954ddcSFrancesco Fondelli 		}
167734954ddcSFrancesco Fondelli 		return count;
167834954ddcSFrancesco Fondelli 	}
167934954ddcSFrancesco Fondelli 
168034954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
168134954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
168263adc6fbSStephen Hemminger 		if (len < 0)
168334954ddcSFrancesco Fondelli 			return len;
168463adc6fbSStephen Hemminger 
168534954ddcSFrancesco Fondelli 		i += len;
168634954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
168734954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
168834954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
168934954ddcSFrancesco Fondelli 		} else {
169034954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
169134954ddcSFrancesco Fondelli 		}
1692ca6549afSSteven Whitehouse 		return count;
1693ca6549afSSteven Whitehouse 	}
1694ca6549afSSteven Whitehouse 
16951ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
16961ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16971ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
169863adc6fbSStephen Hemminger 		if (len < 0)
16991ca7768cSFrancesco Fondelli 			return len;
170063adc6fbSStephen Hemminger 
17011ca7768cSFrancesco Fondelli 		i += len;
17021ca7768cSFrancesco Fondelli 		if (len == 2) {
17031ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
17041ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
17051ca7768cSFrancesco Fondelli 		} else {
17061ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
17071ca7768cSFrancesco Fondelli 		}
17081ca7768cSFrancesco Fondelli 		return count;
17091ca7768cSFrancesco Fondelli 	}
17101ca7768cSFrancesco Fondelli 
17111ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
17121ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
17131ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
171463adc6fbSStephen Hemminger 		if (len < 0)
17151ca7768cSFrancesco Fondelli 			return len;
171663adc6fbSStephen Hemminger 
17171ca7768cSFrancesco Fondelli 		i += len;
17181ca7768cSFrancesco Fondelli 		if (len == 2) {
17191ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
17201ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
17211ca7768cSFrancesco Fondelli 		} else {
17221ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
17231ca7768cSFrancesco Fondelli 		}
17241ca7768cSFrancesco Fondelli 		return count;
17251ca7768cSFrancesco Fondelli 	}
17261ca7768cSFrancesco Fondelli 
17279e50e3acSJohn Fastabend 	if (!strcmp(name, "skb_priority")) {
17289e50e3acSJohn Fastabend 		len = num_arg(&user_buffer[i], 9, &value);
17299e50e3acSJohn Fastabend 		if (len < 0)
17309e50e3acSJohn Fastabend 			return len;
17319e50e3acSJohn Fastabend 
17329e50e3acSJohn Fastabend 		i += len;
17339e50e3acSJohn Fastabend 		pkt_dev->skb_priority = value;
17349e50e3acSJohn Fastabend 		sprintf(pg_result, "OK: skb_priority=%i",
17359e50e3acSJohn Fastabend 			pkt_dev->skb_priority);
17369e50e3acSJohn Fastabend 		return count;
17379e50e3acSJohn Fastabend 	}
17389e50e3acSJohn Fastabend 
17391da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
17401da177e4SLinus Torvalds 	return -EINVAL;
17411da177e4SLinus Torvalds }
17421da177e4SLinus Torvalds 
1743d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
17441da177e4SLinus Torvalds {
1745d50a6b56SStephen Hemminger 	return single_open(file, pktgen_if_show, PDE(inode)->data);
17461da177e4SLinus Torvalds }
17471da177e4SLinus Torvalds 
17489a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1749d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1750d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1751d50a6b56SStephen Hemminger 	.read    = seq_read,
1752d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1753d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1754d50a6b56SStephen Hemminger 	.release = single_release,
1755d50a6b56SStephen Hemminger };
1756d50a6b56SStephen Hemminger 
1757d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1758d50a6b56SStephen Hemminger {
1759d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1760648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1761d50a6b56SStephen Hemminger 
1762d50a6b56SStephen Hemminger 	BUG_ON(!t);
1763d50a6b56SStephen Hemminger 
1764d50a6b56SStephen Hemminger 	seq_printf(seq, "Running: ");
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	if_lock(t);
1767c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17681da177e4SLinus Torvalds 		if (pkt_dev->running)
1769593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17701da177e4SLinus Torvalds 
1771d50a6b56SStephen Hemminger 	seq_printf(seq, "\nStopped: ");
17721da177e4SLinus Torvalds 
1773c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17741da177e4SLinus Torvalds 		if (!pkt_dev->running)
1775593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	if (t->result[0])
1778d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
17791da177e4SLinus Torvalds 	else
1780d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: NA\n");
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 	if_unlock(t);
17831da177e4SLinus Torvalds 
1784d50a6b56SStephen Hemminger 	return 0;
17851da177e4SLinus Torvalds }
17861da177e4SLinus Torvalds 
1787d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1788d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1789d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
17901da177e4SLinus Torvalds {
17918a994a71SJoe Perches 	struct seq_file *seq = file->private_data;
1792d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1793d6182223SPaul Gortmaker 	int i, max, len, ret;
17941da177e4SLinus Torvalds 	char name[40];
17951da177e4SLinus Torvalds 	char *pg_result;
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 	if (count < 1) {
17981da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
17991da177e4SLinus Torvalds 		return -EINVAL;
18001da177e4SLinus Torvalds 	}
18011da177e4SLinus Torvalds 
1802d6182223SPaul Gortmaker 	max = count;
1803d6182223SPaul Gortmaker 	len = count_trail_chars(user_buffer, max);
18041da177e4SLinus Torvalds 	if (len < 0)
18051da177e4SLinus Torvalds 		return len;
18061da177e4SLinus Torvalds 
1807d6182223SPaul Gortmaker 	i = len;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	/* Read variable name */
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
18121da177e4SLinus Torvalds 	if (len < 0)
18131da177e4SLinus Torvalds 		return len;
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
18161da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
18171da177e4SLinus Torvalds 		return -EFAULT;
18181da177e4SLinus Torvalds 	i += len;
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	max = count - i;
18211da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
18221da177e4SLinus Torvalds 	if (len < 0)
18231da177e4SLinus Torvalds 		return len;
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	i += len;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	if (debug)
182825a8b254SDavid S. Miller 		printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
182925a8b254SDavid S. Miller 		       name, (unsigned long)count);
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	if (!t) {
1832f9467eaeSJoe Perches 		pr_err("ERROR: No thread\n");
18331da177e4SLinus Torvalds 		ret = -EINVAL;
18341da177e4SLinus Torvalds 		goto out;
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
18401da177e4SLinus Torvalds 		char f[32];
18411da177e4SLinus Torvalds 		memset(f, 0, 32);
18421da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
18431da177e4SLinus Torvalds 		if (len < 0) {
18441da177e4SLinus Torvalds 			ret = len;
18451da177e4SLinus Torvalds 			goto out;
18461da177e4SLinus Torvalds 		}
18471da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
18481da177e4SLinus Torvalds 			return -EFAULT;
18491da177e4SLinus Torvalds 		i += len;
18506146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
18511da177e4SLinus Torvalds 		pktgen_add_device(t, f);
18526146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
18531da177e4SLinus Torvalds 		ret = count;
18541da177e4SLinus Torvalds 		sprintf(pg_result, "OK: add_device=%s", f);
18551da177e4SLinus Torvalds 		goto out;
18561da177e4SLinus Torvalds 	}
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
18596146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
186095ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
18616146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1862121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
18631da177e4SLinus Torvalds 		ret = count;
18641da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
18651da177e4SLinus Torvalds 		goto out;
18661da177e4SLinus Torvalds 	}
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1869b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
18701da177e4SLinus Torvalds 		ret = count;
18711da177e4SLinus Torvalds 		goto out;
18721da177e4SLinus Torvalds 	}
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	ret = -EINVAL;
18751da177e4SLinus Torvalds out:
18761da177e4SLinus Torvalds 	return ret;
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
1879d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
18801da177e4SLinus Torvalds {
1881d50a6b56SStephen Hemminger 	return single_open(file, pktgen_thread_show, PDE(inode)->data);
18821da177e4SLinus Torvalds }
18831da177e4SLinus Torvalds 
18849a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1885d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1886d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1887d50a6b56SStephen Hemminger 	.read    = seq_read,
1888d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1889d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1890d50a6b56SStephen Hemminger 	.release = single_release,
1891d50a6b56SStephen Hemminger };
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds /* Think find or remove for NN */
18941da177e4SLinus Torvalds static struct pktgen_dev *__pktgen_NN_threads(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 
1900cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list) {
19013e984840SEric Dumazet 		pkt_dev = pktgen_find_dev(t, ifname, exact);
19021da177e4SLinus Torvalds 		if (pkt_dev) {
19031da177e4SLinus Torvalds 			if (remove) {
19041da177e4SLinus Torvalds 				if_lock(t);
190595ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
190695ed63f7SArthur Kepner 				t->control |= T_REMDEV;
19071da177e4SLinus Torvalds 				if_unlock(t);
19081da177e4SLinus Torvalds 			}
19091da177e4SLinus Torvalds 			break;
19101da177e4SLinus Torvalds 		}
19111da177e4SLinus Torvalds 	}
19121da177e4SLinus Torvalds 	return pkt_dev;
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
191595ed63f7SArthur Kepner /*
191695ed63f7SArthur Kepner  * mark a device for removal
191795ed63f7SArthur Kepner  */
191839df232fSStephen Hemminger static void pktgen_mark_device(const char *ifname)
19191da177e4SLinus Torvalds {
19201da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
192195ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
192295ed63f7SArthur Kepner 	int i = 0;
192395ed63f7SArthur Kepner 
19246146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
1925f9467eaeSJoe Perches 	pr_debug("%s: marking %s for removal\n", __func__, ifname);
192695ed63f7SArthur Kepner 
192795ed63f7SArthur Kepner 	while (1) {
192895ed63f7SArthur Kepner 
192995ed63f7SArthur Kepner 		pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1930222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1931222f1806SLuiz Capitulino 			break;	/* success */
193295ed63f7SArthur Kepner 
19336146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1934f9467eaeSJoe Perches 		pr_debug("%s: waiting for %s to disappear....\n",
1935f9467eaeSJoe Perches 			 __func__, ifname);
193695ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
19376146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
193895ed63f7SArthur Kepner 
193995ed63f7SArthur Kepner 		if (++i >= max_tries) {
1940f9467eaeSJoe Perches 			pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1941f9467eaeSJoe Perches 			       __func__, msec_per_try * i, ifname);
194295ed63f7SArthur Kepner 			break;
194395ed63f7SArthur Kepner 		}
194495ed63f7SArthur Kepner 
194595ed63f7SArthur Kepner 	}
194695ed63f7SArthur Kepner 
19476146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
194839df232fSStephen Hemminger }
194995ed63f7SArthur Kepner 
195039df232fSStephen Hemminger static void pktgen_change_name(struct net_device *dev)
195139df232fSStephen Hemminger {
195239df232fSStephen Hemminger 	struct pktgen_thread *t;
195339df232fSStephen Hemminger 
195439df232fSStephen Hemminger 	list_for_each_entry(t, &pktgen_threads, th_list) {
195539df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
195639df232fSStephen Hemminger 
195739df232fSStephen Hemminger 		list_for_each_entry(pkt_dev, &t->if_list, list) {
195839df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
195939df232fSStephen Hemminger 				continue;
196039df232fSStephen Hemminger 
196139df232fSStephen Hemminger 			remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
196239df232fSStephen Hemminger 
19632975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
19642975315bSAlexey Dobriyan 							  pg_proc_dir,
19652975315bSAlexey Dobriyan 							  &pktgen_if_fops,
19662975315bSAlexey Dobriyan 							  pkt_dev);
196739df232fSStephen Hemminger 			if (!pkt_dev->entry)
1968f9467eaeSJoe Perches 				pr_err("can't move proc entry for '%s'\n",
1969f9467eaeSJoe Perches 				       dev->name);
197039df232fSStephen Hemminger 			break;
197139df232fSStephen Hemminger 		}
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 {
197839df232fSStephen Hemminger 	struct net_device *dev = ptr;
19791da177e4SLinus Torvalds 
1980721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
1981e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1982e9dc8653SEric W. Biederman 
19831da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
19841da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
19851da177e4SLinus Torvalds 	 */
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 	switch (event) {
198839df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
198939df232fSStephen Hemminger 		pktgen_change_name(dev);
19901da177e4SLinus Torvalds 		break;
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
199395ed63f7SArthur Kepner 		pktgen_mark_device(dev->name);
19941da177e4SLinus Torvalds 		break;
19953ff50b79SStephen Hemminger 	}
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 	return NOTIFY_DONE;
19981da177e4SLinus Torvalds }
19991da177e4SLinus Torvalds 
200063adc6fbSStephen Hemminger static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
200163adc6fbSStephen Hemminger 						 const char *ifname)
2002e6fce5b9SRobert Olsson {
2003e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
2004d6182223SPaul Gortmaker 	int i;
2005e6fce5b9SRobert Olsson 
2006e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
2007e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
2008e6fce5b9SRobert Olsson 			break;
2009e6fce5b9SRobert Olsson 
2010e6fce5b9SRobert Olsson 		b[i] = ifname[i];
2011e6fce5b9SRobert Olsson 	}
2012e6fce5b9SRobert Olsson 	b[i] = 0;
2013e6fce5b9SRobert Olsson 
2014e6fce5b9SRobert Olsson 	return dev_get_by_name(&init_net, b);
2015e6fce5b9SRobert Olsson }
2016e6fce5b9SRobert Olsson 
2017e6fce5b9SRobert Olsson 
20181da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
20191da177e4SLinus Torvalds 
202039df232fSStephen Hemminger static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
2021222f1806SLuiz Capitulino {
20221da177e4SLinus Torvalds 	struct net_device *odev;
202339df232fSStephen Hemminger 	int err;
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds 	/* Clean old setups */
20261da177e4SLinus Torvalds 	if (pkt_dev->odev) {
20271da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
20281da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
20291da177e4SLinus Torvalds 	}
20301da177e4SLinus Torvalds 
2031e6fce5b9SRobert Olsson 	odev = pktgen_dev_get_by_name(pkt_dev, ifname);
20321da177e4SLinus Torvalds 	if (!odev) {
2033f9467eaeSJoe Perches 		pr_err("no such netdevice: \"%s\"\n", ifname);
203439df232fSStephen Hemminger 		return -ENODEV;
20351da177e4SLinus Torvalds 	}
203639df232fSStephen Hemminger 
20371da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
2038f9467eaeSJoe Perches 		pr_err("not an ethernet device: \"%s\"\n", ifname);
203939df232fSStephen Hemminger 		err = -EINVAL;
204039df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
2041f9467eaeSJoe Perches 		pr_err("device is down: \"%s\"\n", ifname);
204239df232fSStephen Hemminger 		err = -ENETDOWN;
204339df232fSStephen Hemminger 	} else {
20441da177e4SLinus Torvalds 		pkt_dev->odev = odev;
204539df232fSStephen Hemminger 		return 0;
204639df232fSStephen Hemminger 	}
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	dev_put(odev);
204939df232fSStephen Hemminger 	return err;
20501da177e4SLinus Torvalds }
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
20531da177e4SLinus Torvalds  * structure to have the right information to create/send packets
20541da177e4SLinus Torvalds  */
20551da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
20561da177e4SLinus Torvalds {
205764c00d81SAndrew Gallatin 	int ntxq;
205864c00d81SAndrew Gallatin 
20591da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
2060f9467eaeSJoe Perches 		pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2061222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
2062222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
20631da177e4SLinus Torvalds 		return;
20641da177e4SLinus Torvalds 	}
20651da177e4SLinus Torvalds 
206664c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
206764c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2068bfdbc0acSRobert Olsson 
206964c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
2070f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
207188271660SJesse Brandeburg 			   pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2072593f63b0SEric Dumazet 			   pkt_dev->odevname);
207364c00d81SAndrew Gallatin 		pkt_dev->queue_map_min = ntxq - 1;
207464c00d81SAndrew Gallatin 	}
207588271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
2076f9467eaeSJoe Perches 		pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
207788271660SJesse Brandeburg 			   pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2078593f63b0SEric Dumazet 			   pkt_dev->odevname);
207964c00d81SAndrew Gallatin 		pkt_dev->queue_map_max = ntxq - 1;
208064c00d81SAndrew Gallatin 	}
208164c00d81SAndrew Gallatin 
20821da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
20831da177e4SLinus Torvalds 
2084f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
2085f404e9a6SKris Katterjohn 		memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 	/* Set up Dest MAC */
2088f404e9a6SKris Katterjohn 	memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 	/* Set up pkt size */
20911da177e4SLinus Torvalds 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
20941da177e4SLinus Torvalds 		/*
20951da177e4SLinus Torvalds 		 * Skip this automatic address setting until locks or functions
20961da177e4SLinus Torvalds 		 * gets exported
20971da177e4SLinus Torvalds 		 */
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds #ifdef NOTNOW
21001da177e4SLinus Torvalds 		int i, set = 0, err = 1;
21011da177e4SLinus Torvalds 		struct inet6_dev *idev;
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
21041da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
21051da177e4SLinus Torvalds 				set = 1;
21061da177e4SLinus Torvalds 				break;
21071da177e4SLinus Torvalds 			}
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 		if (!set) {
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds 			/*
21121da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
21131da177e4SLinus Torvalds 			 *
21141da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
21151da177e4SLinus Torvalds 			 */
21161da177e4SLinus Torvalds 
21178814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
211863adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
211963adc6fbSStephen Hemminger 			if (idev) {
21201da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
2123222f1806SLuiz Capitulino 				for (ifp = idev->addr_list; ifp;
2124222f1806SLuiz Capitulino 				     ifp = ifp->if_next) {
2125f64f9e71SJoe Perches 					if (ifp->scope == IFA_LINK &&
2126f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
2127222f1806SLuiz Capitulino 						ipv6_addr_copy(&pkt_dev->
2128222f1806SLuiz Capitulino 							       cur_in6_saddr,
2129222f1806SLuiz Capitulino 							       &ifp->addr);
21301da177e4SLinus Torvalds 						err = 0;
21311da177e4SLinus Torvalds 						break;
21321da177e4SLinus Torvalds 					}
21331da177e4SLinus Torvalds 				}
21341da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
21351da177e4SLinus Torvalds 			}
21368814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2137222f1806SLuiz Capitulino 			if (err)
2138f9467eaeSJoe Perches 				pr_err("ERROR: IPv6 link address not available\n");
21391da177e4SLinus Torvalds 		}
21401da177e4SLinus Torvalds #endif
2141222f1806SLuiz Capitulino 	} else {
21421da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
21431da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
21441da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 			struct in_device *in_dev;
21471da177e4SLinus Torvalds 
21481da177e4SLinus Torvalds 			rcu_read_lock();
2149e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
21501da177e4SLinus Torvalds 			if (in_dev) {
21511da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2152222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2153222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
21541da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
21551da177e4SLinus Torvalds 				}
21561da177e4SLinus Torvalds 			}
21571da177e4SLinus Torvalds 			rcu_read_unlock();
2158222f1806SLuiz Capitulino 		} else {
21591da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
21601da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
21611da177e4SLinus Torvalds 		}
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
21641da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
21651da177e4SLinus Torvalds 	}
21661da177e4SLinus Torvalds 	/* Initialize current values. */
21671da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
21681da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
21691da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
21701da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
21711da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
21721da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
21731da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
21741da177e4SLinus Torvalds }
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 
2177fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2178fd29cf72SStephen Hemminger {
2179ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2180417bc4b8SEric Dumazet 	s64 remaining;
21812bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2182fd29cf72SStephen Hemminger 
21832bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
21842bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2185fd29cf72SStephen Hemminger 
218643d28b65SDaniel Turull 	remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2187417bc4b8SEric Dumazet 	if (remaining <= 0) {
2188417bc4b8SEric Dumazet 		pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21891da177e4SLinus Torvalds 		return;
2190417bc4b8SEric Dumazet 	}
21912bc481cfSStephen Hemminger 
2192ef87979cSStephen Hemminger 	start_time = ktime_now();
219343d28b65SDaniel Turull 	if (remaining < 100000)
219443d28b65SDaniel Turull 		ndelay(remaining);	/* really small just spin */
21952bc481cfSStephen Hemminger 	else {
21962bc481cfSStephen Hemminger 		/* see do_nanosleep */
21972bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
21982bc481cfSStephen Hemminger 		do {
21992bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
22002bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
22012bc481cfSStephen Hemminger 			if (!hrtimer_active(&t.timer))
22022bc481cfSStephen Hemminger 				t.task = NULL;
22032bc481cfSStephen Hemminger 
22042bc481cfSStephen Hemminger 			if (likely(t.task))
22051da177e4SLinus Torvalds 				schedule();
22061da177e4SLinus Torvalds 
22072bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
22082bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
22092bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
22101da177e4SLinus Torvalds 	}
2211ef87979cSStephen Hemminger 	end_time = ktime_now();
2212ef87979cSStephen Hemminger 
2213ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
221407a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds 
221716dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
221816dab72fSJamal Hadi Salim {
2219a553e4a6SJamal Hadi Salim 	pkt_dev->pkt_overhead = 0;
222016dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
222116dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
222216dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
222316dab72fSJamal Hadi Salim }
222416dab72fSJamal Hadi Salim 
2225648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2226007a531bSJamal Hadi Salim {
2227648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2228007a531bSJamal Hadi Salim }
2229007a531bSJamal Hadi Salim 
2230007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2231007a531bSJamal Hadi Salim {
2232007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2233007a531bSJamal Hadi Salim 
2234007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2235007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2236007a531bSJamal Hadi Salim 			/* reset time */
2237007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22381211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2239007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2240007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2241007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2242007a531bSJamal Hadi Salim 		}
2243007a531bSJamal Hadi Salim 	} else {
2244007a531bSJamal Hadi Salim 		flow = random32() % pkt_dev->cflows;
22451211a645SRobert Olsson 		pkt_dev->curfl = flow;
2246007a531bSJamal Hadi Salim 
22471211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2248007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22491211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
22501211a645SRobert Olsson 		}
2251007a531bSJamal Hadi Salim 	}
2252007a531bSJamal Hadi Salim 
2253007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2254007a531bSJamal Hadi Salim }
2255007a531bSJamal Hadi Salim 
2256a553e4a6SJamal Hadi Salim 
2257a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2258a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2259a553e4a6SJamal Hadi Salim  * we go look for it ...
2260a553e4a6SJamal Hadi Salim */
2261bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2262fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2263a553e4a6SJamal Hadi Salim {
2264a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
2265a553e4a6SJamal Hadi Salim 	if (!x) {
2266a553e4a6SJamal Hadi Salim 		/*slow path: we dont already have xfrm_state*/
2267bd55775cSJamal Hadi Salim 		x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
22685447c5e4SAlexey Dobriyan 					(xfrm_address_t *)&pkt_dev->cur_daddr,
2269a553e4a6SJamal Hadi Salim 					(xfrm_address_t *)&pkt_dev->cur_saddr,
2270a553e4a6SJamal Hadi Salim 					AF_INET,
2271a553e4a6SJamal Hadi Salim 					pkt_dev->ipsmode,
2272a553e4a6SJamal Hadi Salim 					pkt_dev->ipsproto, 0);
2273a553e4a6SJamal Hadi Salim 		if (x) {
2274a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2275a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2276a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2277a553e4a6SJamal Hadi Salim 		}
2278a553e4a6SJamal Hadi Salim 
2279a553e4a6SJamal Hadi Salim 	}
2280a553e4a6SJamal Hadi Salim }
2281a553e4a6SJamal Hadi Salim #endif
2282fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2283fd2ea0a7SDavid S. Miller {
2284e6fce5b9SRobert Olsson 
2285e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2286e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2287e6fce5b9SRobert Olsson 
2288896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2289fd2ea0a7SDavid S. Miller 		__u16 t;
2290fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2291fd2ea0a7SDavid S. Miller 			t = random32() %
2292fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2293fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2294fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2295fd2ea0a7SDavid S. Miller 		} else {
2296fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2297fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2298fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2299fd2ea0a7SDavid S. Miller 		}
2300fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2301fd2ea0a7SDavid S. Miller 	}
2302bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2303fd2ea0a7SDavid S. Miller }
2304fd2ea0a7SDavid S. Miller 
23051da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
23061da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
23071da177e4SLinus Torvalds  */
2308222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2309222f1806SLuiz Capitulino {
23101da177e4SLinus Torvalds 	__u32 imn;
23111da177e4SLinus Torvalds 	__u32 imx;
23121da177e4SLinus Torvalds 	int flow = 0;
23131da177e4SLinus Torvalds 
2314007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2315007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds 	/*  Deal with source MAC */
23181da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
23191da177e4SLinus Torvalds 		__u32 mc;
23201da177e4SLinus Torvalds 		__u32 tmp;
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
23235fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->src_mac_count;
23241da177e4SLinus Torvalds 		else {
23251da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2326ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2327222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
23281da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
23291da177e4SLinus Torvalds 		}
23301da177e4SLinus Torvalds 
23311da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
23321da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
23331da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23341da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
23351da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23361da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
23371da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23381da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
23391da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
23401da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
23411da177e4SLinus Torvalds 	}
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
23441da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
23451da177e4SLinus Torvalds 		__u32 mc;
23461da177e4SLinus Torvalds 		__u32 tmp;
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
23495fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->dst_mac_count;
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 		else {
23521da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2353ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2354222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
23551da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
23561da177e4SLinus Torvalds 			}
23571da177e4SLinus Torvalds 		}
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
23601da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
23611da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23621da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
23631da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23641da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
23651da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23661da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
23671da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
23681da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
23691da177e4SLinus Torvalds 	}
23701da177e4SLinus Torvalds 
2371ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
2372ca6549afSSteven Whitehouse 		unsigned i;
2373ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2374ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2375ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
23765fa6fc76SStephen Hemminger 					     ((__force __be32)random32() &
2377ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2378ca6549afSSteven Whitehouse 	}
2379ca6549afSSteven Whitehouse 
238034954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
23815fa6fc76SStephen Hemminger 		pkt_dev->vlan_id = random32() & (4096-1);
238234954ddcSFrancesco Fondelli 	}
238334954ddcSFrancesco Fondelli 
238434954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
23855fa6fc76SStephen Hemminger 		pkt_dev->svlan_id = random32() & (4096 - 1);
238634954ddcSFrancesco Fondelli 	}
238734954ddcSFrancesco Fondelli 
23881da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
23891da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
23905fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_src = random32() %
23915fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
23925fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 		else {
23951da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
23961da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
23971da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
23981da177e4SLinus Torvalds 		}
23991da177e4SLinus Torvalds 	}
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
24021da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
24035fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_dst = random32() %
24045fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
24055fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2406222f1806SLuiz Capitulino 		} else {
24071da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
24081da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
24091da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
24101da177e4SLinus Torvalds 		}
24111da177e4SLinus Torvalds 	}
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
24141da177e4SLinus Torvalds 
241563adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
241663adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
241763adc6fbSStephen Hemminger 		if (imn < imx) {
24181da177e4SLinus Torvalds 			__u32 t;
24191da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
24205fa6fc76SStephen Hemminger 				t = random32() % (imx - imn) + imn;
24211da177e4SLinus Torvalds 			else {
24221da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
24231da177e4SLinus Torvalds 				t++;
242463adc6fbSStephen Hemminger 				if (t > imx)
24251da177e4SLinus Torvalds 					t = imn;
242663adc6fbSStephen Hemminger 
24271da177e4SLinus Torvalds 			}
24281da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
24291da177e4SLinus Torvalds 		}
24301da177e4SLinus Torvalds 
2431007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
24321da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
24331da177e4SLinus Torvalds 		} else {
2434252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2435252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2436252e3346SAl Viro 			if (imn < imx) {
24371da177e4SLinus Torvalds 				__u32 t;
2438252e3346SAl Viro 				__be32 s;
24391da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
24401da177e4SLinus Torvalds 
24415fa6fc76SStephen Hemminger 					t = random32() % (imx - imn) + imn;
2442252e3346SAl Viro 					s = htonl(t);
24431da177e4SLinus Torvalds 
244421cf2253SJoe Perches 					while (ipv4_is_loopback(s) ||
244521cf2253SJoe Perches 					       ipv4_is_multicast(s) ||
24461e637c74SJan Engelhardt 					       ipv4_is_lbcast(s) ||
244721cf2253SJoe Perches 					       ipv4_is_zeronet(s) ||
244821cf2253SJoe Perches 					       ipv4_is_local_multicast(s)) {
24495fa6fc76SStephen Hemminger 						t = random32() % (imx - imn) + imn;
2450252e3346SAl Viro 						s = htonl(t);
24511da177e4SLinus Torvalds 					}
2452252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2453252e3346SAl Viro 				} else {
24541da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
24551da177e4SLinus Torvalds 					t++;
24561da177e4SLinus Torvalds 					if (t > imx) {
24571da177e4SLinus Torvalds 						t = imn;
24581da177e4SLinus Torvalds 					}
24591da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
24601da177e4SLinus Torvalds 				}
24611da177e4SLinus Torvalds 			}
24621da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2463007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2464222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2465222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2466a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2467a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2468a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2469a553e4a6SJamal Hadi Salim #endif
24701da177e4SLinus Torvalds 				pkt_dev->nflows++;
24711da177e4SLinus Torvalds 			}
24721da177e4SLinus Torvalds 		}
2473222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2474222f1806SLuiz Capitulino 
24751da177e4SLinus Torvalds 		if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
24761da177e4SLinus Torvalds 		    pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
24771da177e4SLinus Torvalds 		    pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
24781da177e4SLinus Torvalds 		    pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
24791da177e4SLinus Torvalds 		else {
24801da177e4SLinus Torvalds 			int i;
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 			/* Only random destinations yet */
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
24851da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
24865fa6fc76SStephen Hemminger 				    (((__force __be32)random32() |
24871da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
24881da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
24891da177e4SLinus Torvalds 			}
24901da177e4SLinus Torvalds 		}
24911da177e4SLinus Torvalds 	}
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
24941da177e4SLinus Torvalds 		__u32 t;
24951da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
24965fa6fc76SStephen Hemminger 			t = random32() %
24975fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
24985fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2499222f1806SLuiz Capitulino 		} else {
25001da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
25011da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
25021da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
25031da177e4SLinus Torvalds 		}
25041da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
25051da177e4SLinus Torvalds 	}
25061da177e4SLinus Torvalds 
2507fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
250845b270f8SRobert Olsson 
25091da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
25101da177e4SLinus Torvalds }
25111da177e4SLinus Torvalds 
2512a553e4a6SJamal Hadi Salim 
2513a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2514a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2515a553e4a6SJamal Hadi Salim {
2516a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2517a553e4a6SJamal Hadi Salim 	int err = 0;
2518a553e4a6SJamal Hadi Salim 	struct iphdr *iph;
2519a553e4a6SJamal Hadi Salim 
2520a553e4a6SJamal Hadi Salim 	if (!x)
2521a553e4a6SJamal Hadi Salim 		return 0;
2522a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2523a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2524a553e4a6SJamal Hadi Salim 	if (x->props.mode != XFRM_MODE_TRANSPORT)
2525a553e4a6SJamal Hadi Salim 		return 0;
2526a553e4a6SJamal Hadi Salim 
2527a553e4a6SJamal Hadi Salim 	spin_lock(&x->lock);
2528a553e4a6SJamal Hadi Salim 	iph = ip_hdr(skb);
2529a553e4a6SJamal Hadi Salim 
253013996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2531a553e4a6SJamal Hadi Salim 	if (err)
2532a553e4a6SJamal Hadi Salim 		goto error;
2533a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
2534a553e4a6SJamal Hadi Salim 	if (err)
2535a553e4a6SJamal Hadi Salim 		goto error;
2536a553e4a6SJamal Hadi Salim 
2537a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2538a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
2539a553e4a6SJamal Hadi Salim error:
2540a553e4a6SJamal Hadi Salim 	spin_unlock(&x->lock);
2541a553e4a6SJamal Hadi Salim 	return err;
2542a553e4a6SJamal Hadi Salim }
2543a553e4a6SJamal Hadi Salim 
2544475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2545a553e4a6SJamal Hadi Salim {
2546a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2547a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2548d6182223SPaul Gortmaker 		int i;
2549d6182223SPaul Gortmaker 		for (i = 0; i < pkt_dev->cflows; i++) {
2550a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2551a553e4a6SJamal Hadi Salim 			if (x) {
2552a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2553a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2554a553e4a6SJamal Hadi Salim 			}
2555a553e4a6SJamal Hadi Salim 		}
2556a553e4a6SJamal Hadi Salim 	}
2557a553e4a6SJamal Hadi Salim }
2558a553e4a6SJamal Hadi Salim 
2559475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2560a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2561a553e4a6SJamal Hadi Salim {
2562a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2563a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2564a553e4a6SJamal Hadi Salim 		int nhead = 0;
2565a553e4a6SJamal Hadi Salim 		if (x) {
2566a553e4a6SJamal Hadi Salim 			int ret;
2567a553e4a6SJamal Hadi Salim 			__u8 *eth;
2568a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2569a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2570a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2571a553e4a6SJamal Hadi Salim 				if (ret < 0) {
2572f9467eaeSJoe Perches 					pr_err("Error expanding ipsec packet %d\n",
2573f9467eaeSJoe Perches 					       ret);
2574b4bb4ac8SIlpo Järvinen 					goto err;
2575a553e4a6SJamal Hadi Salim 				}
2576a553e4a6SJamal Hadi Salim 			}
2577a553e4a6SJamal Hadi Salim 
2578a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2579a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2580a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2581a553e4a6SJamal Hadi Salim 			if (ret) {
2582f9467eaeSJoe Perches 				pr_err("Error creating ipsec packet %d\n", ret);
2583b4bb4ac8SIlpo Järvinen 				goto err;
2584a553e4a6SJamal Hadi Salim 			}
2585a553e4a6SJamal Hadi Salim 			/* restore ll */
2586a553e4a6SJamal Hadi Salim 			eth = (__u8 *) skb_push(skb, ETH_HLEN);
2587a553e4a6SJamal Hadi Salim 			memcpy(eth, pkt_dev->hh, 12);
2588a553e4a6SJamal Hadi Salim 			*(u16 *) &eth[12] = protocol;
2589a553e4a6SJamal Hadi Salim 		}
2590a553e4a6SJamal Hadi Salim 	}
2591a553e4a6SJamal Hadi Salim 	return 1;
2592b4bb4ac8SIlpo Järvinen err:
2593b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2594b4bb4ac8SIlpo Järvinen 	return 0;
2595a553e4a6SJamal Hadi Salim }
2596a553e4a6SJamal Hadi Salim #endif
2597a553e4a6SJamal Hadi Salim 
2598ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2599ca6549afSSteven Whitehouse {
2600ca6549afSSteven Whitehouse 	unsigned i;
260163adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2602ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
260363adc6fbSStephen Hemminger 
2604ca6549afSSteven Whitehouse 	mpls--;
2605ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2606ca6549afSSteven Whitehouse }
2607ca6549afSSteven Whitehouse 
26080f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
26090f37c605SAl Viro 			       unsigned int prio)
26100f37c605SAl Viro {
26110f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
26120f37c605SAl Viro }
26130f37c605SAl Viro 
261426ad7879SEric Dumazet static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
261526ad7879SEric Dumazet 				int datalen)
261626ad7879SEric Dumazet {
261726ad7879SEric Dumazet 	struct timeval timestamp;
261826ad7879SEric Dumazet 	struct pktgen_hdr *pgh;
261926ad7879SEric Dumazet 
262026ad7879SEric Dumazet 	pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
262126ad7879SEric Dumazet 	datalen -= sizeof(*pgh);
262226ad7879SEric Dumazet 
262326ad7879SEric Dumazet 	if (pkt_dev->nfrags <= 0) {
262405aebe2eSDaniel Turull 		memset(skb_put(skb, datalen), 0, datalen);
262526ad7879SEric Dumazet 	} else {
262626ad7879SEric Dumazet 		int frags = pkt_dev->nfrags;
262726ad7879SEric Dumazet 		int i, len;
262826ad7879SEric Dumazet 
262926ad7879SEric Dumazet 
263026ad7879SEric Dumazet 		if (frags > MAX_SKB_FRAGS)
263126ad7879SEric Dumazet 			frags = MAX_SKB_FRAGS;
263226ad7879SEric Dumazet 		len = datalen - frags * PAGE_SIZE;
263326ad7879SEric Dumazet 		if (len > 0) {
263426ad7879SEric Dumazet 			memset(skb_put(skb, len), 0, len);
263526ad7879SEric Dumazet 			datalen = frags * PAGE_SIZE;
263626ad7879SEric Dumazet 		}
263726ad7879SEric Dumazet 
263826ad7879SEric Dumazet 		i = 0;
263926ad7879SEric Dumazet 		while (datalen > 0) {
264026ad7879SEric Dumazet 			if (unlikely(!pkt_dev->page)) {
264126ad7879SEric Dumazet 				int node = numa_node_id();
264226ad7879SEric Dumazet 
264326ad7879SEric Dumazet 				if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
264426ad7879SEric Dumazet 					node = pkt_dev->node;
264526ad7879SEric Dumazet 				pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
264626ad7879SEric Dumazet 				if (!pkt_dev->page)
264726ad7879SEric Dumazet 					break;
264826ad7879SEric Dumazet 			}
264926ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page = pkt_dev->page;
265026ad7879SEric Dumazet 			get_page(pkt_dev->page);
265126ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page_offset = 0;
265226ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].size =
265326ad7879SEric Dumazet 			    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
265426ad7879SEric Dumazet 			datalen -= skb_shinfo(skb)->frags[i].size;
265526ad7879SEric Dumazet 			skb->len += skb_shinfo(skb)->frags[i].size;
265626ad7879SEric Dumazet 			skb->data_len += skb_shinfo(skb)->frags[i].size;
265726ad7879SEric Dumazet 			i++;
265826ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
265926ad7879SEric Dumazet 		}
266026ad7879SEric Dumazet 
266126ad7879SEric Dumazet 		while (i < frags) {
266226ad7879SEric Dumazet 			int rem;
266326ad7879SEric Dumazet 
266426ad7879SEric Dumazet 			if (i == 0)
266526ad7879SEric Dumazet 				break;
266626ad7879SEric Dumazet 
266726ad7879SEric Dumazet 			rem = skb_shinfo(skb)->frags[i - 1].size / 2;
266826ad7879SEric Dumazet 			if (rem == 0)
266926ad7879SEric Dumazet 				break;
267026ad7879SEric Dumazet 
267126ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i - 1].size -= rem;
267226ad7879SEric Dumazet 
267326ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i] =
267426ad7879SEric Dumazet 			    skb_shinfo(skb)->frags[i - 1];
267526ad7879SEric Dumazet 			get_page(skb_shinfo(skb)->frags[i].page);
267626ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page =
267726ad7879SEric Dumazet 			    skb_shinfo(skb)->frags[i - 1].page;
267826ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].page_offset +=
267926ad7879SEric Dumazet 			    skb_shinfo(skb)->frags[i - 1].size;
268026ad7879SEric Dumazet 			skb_shinfo(skb)->frags[i].size = rem;
268126ad7879SEric Dumazet 			i++;
268226ad7879SEric Dumazet 			skb_shinfo(skb)->nr_frags = i;
268326ad7879SEric Dumazet 		}
268426ad7879SEric Dumazet 	}
268526ad7879SEric Dumazet 
268626ad7879SEric Dumazet 	/* Stamp the time, and sequence number,
268726ad7879SEric Dumazet 	 * convert them to network byte order
268826ad7879SEric Dumazet 	 */
268926ad7879SEric Dumazet 	pgh->pgh_magic = htonl(PKTGEN_MAGIC);
269026ad7879SEric Dumazet 	pgh->seq_num = htonl(pkt_dev->seq_num);
269126ad7879SEric Dumazet 
269226ad7879SEric Dumazet 	do_gettimeofday(&timestamp);
269326ad7879SEric Dumazet 	pgh->tv_sec = htonl(timestamp.tv_sec);
269426ad7879SEric Dumazet 	pgh->tv_usec = htonl(timestamp.tv_usec);
269526ad7879SEric Dumazet }
269626ad7879SEric Dumazet 
26971da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
26981da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
26991da177e4SLinus Torvalds {
27001da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
27011da177e4SLinus Torvalds 	__u8 *eth;
27021da177e4SLinus Torvalds 	struct udphdr *udph;
27031da177e4SLinus Torvalds 	int datalen, iplen;
27041da177e4SLinus Torvalds 	struct iphdr *iph;
2705d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2706ca6549afSSteven Whitehouse 	__be32 *mpls;
270734954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
270834954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
270934954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
271034954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2711fd2ea0a7SDavid S. Miller 	u16 queue_map;
2712ca6549afSSteven Whitehouse 
2713ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2714d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
27151da177e4SLinus Torvalds 
271634954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2717d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
271834954ddcSFrancesco Fondelli 
271964053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
272064053beeSRobert Olsson 	 * fields.
272164053beeSRobert Olsson 	 */
272264053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
2723eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
272464053beeSRobert Olsson 
27257ac5459eSDavid S. Miller 	datalen = (odev->hard_header_len + 16) & ~0xf;
2726e99b99b4SRobert Olsson 
2727e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE) {
2728e99b99b4SRobert Olsson 		int node;
2729e99b99b4SRobert Olsson 
2730e99b99b4SRobert Olsson 		if (pkt_dev->node >= 0)
2731e99b99b4SRobert Olsson 			node = pkt_dev->node;
2732e99b99b4SRobert Olsson 		else
2733e99b99b4SRobert Olsson 			node =  numa_node_id();
2734e99b99b4SRobert Olsson 
2735e99b99b4SRobert Olsson 		skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2736e99b99b4SRobert Olsson 				  + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2737e99b99b4SRobert Olsson 		if (likely(skb)) {
2738e99b99b4SRobert Olsson 			skb_reserve(skb, NET_SKB_PAD);
2739e99b99b4SRobert Olsson 			skb->dev = odev;
2740e99b99b4SRobert Olsson 		}
2741e99b99b4SRobert Olsson 	}
2742e99b99b4SRobert Olsson 	else
2743e470757dSStephen Hemminger 	  skb = __netdev_alloc_skb(odev,
2744e470757dSStephen Hemminger 				   pkt_dev->cur_pkt_size + 64
2745e470757dSStephen Hemminger 				   + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2746e99b99b4SRobert Olsson 
27471da177e4SLinus Torvalds 	if (!skb) {
27481da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
27491da177e4SLinus Torvalds 		return NULL;
27501da177e4SLinus Torvalds 	}
2751a8d764b9SJunchang Wang 	prefetchw(skb->data);
27521da177e4SLinus Torvalds 
27537ac5459eSDavid S. Miller 	skb_reserve(skb, datalen);
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
27561da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2757ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2758ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2759ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
276034954ddcSFrancesco Fondelli 
276134954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
276234954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
276334954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27640f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
27650f37c605SAl Viro 					       pkt_dev->svlan_cfi,
27660f37c605SAl Viro 					       pkt_dev->svlan_p);
276734954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2768d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
276934954ddcSFrancesco Fondelli 		}
277034954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
27710f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
27720f37c605SAl Viro 				      pkt_dev->vlan_cfi,
27730f37c605SAl Viro 				      pkt_dev->vlan_p);
277434954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2775d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
277634954ddcSFrancesco Fondelli 	}
277734954ddcSFrancesco Fondelli 
277827a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
2779b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct iphdr);
2780ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2781fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
27829e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
27839e50e3acSJohn Fastabend 
2784ddc7b8e3SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
2785ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2788252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
27891da177e4SLinus Torvalds 
2790ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2791ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
279216dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
27931da177e4SLinus Torvalds 	if (datalen < sizeof(struct pktgen_hdr))
27941da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
27971da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
27981da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
27991da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds 	iph->ihl = 5;
28021da177e4SLinus Torvalds 	iph->version = 4;
28031da177e4SLinus Torvalds 	iph->ttl = 32;
28041ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
28051da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
28061da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
28071da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
280866ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
280966ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
28101da177e4SLinus Torvalds 	iph->frag_off = 0;
28111da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
28121da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
28131da177e4SLinus Torvalds 	iph->check = 0;
28141da177e4SLinus Torvalds 	iph->check = ip_fast_csum((void *)iph, iph->ihl);
2815ca6549afSSteven Whitehouse 	skb->protocol = protocol;
2816b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
281716dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
28181da177e4SLinus Torvalds 	skb->dev = odev;
28191da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
282026ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
28211da177e4SLinus Torvalds 
2822a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2823a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2824a553e4a6SJamal Hadi Salim 		return NULL;
2825a553e4a6SJamal Hadi Salim #endif
2826a553e4a6SJamal Hadi Salim 
28271da177e4SLinus Torvalds 	return skb;
28281da177e4SLinus Torvalds }
28291da177e4SLinus Torvalds 
28301da177e4SLinus Torvalds /*
28311da177e4SLinus Torvalds  * scan_ip6, fmt_ip taken from dietlibc-0.21
28321da177e4SLinus Torvalds  * Author Felix von Leitner <felix-dietlibc@fefe.de>
28331da177e4SLinus Torvalds  *
28341da177e4SLinus Torvalds  * Slightly modified for kernel.
28351da177e4SLinus Torvalds  * Should be candidate for net/ipv4/utils.c
28361da177e4SLinus Torvalds  * --ro
28371da177e4SLinus Torvalds  */
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16])
28401da177e4SLinus Torvalds {
28411da177e4SLinus Torvalds 	unsigned int i;
28421da177e4SLinus Torvalds 	unsigned int len = 0;
28431da177e4SLinus Torvalds 	unsigned long u;
28441da177e4SLinus Torvalds 	char suffix[16];
28451da177e4SLinus Torvalds 	unsigned int prefixlen = 0;
28461da177e4SLinus Torvalds 	unsigned int suffixlen = 0;
2847252e3346SAl Viro 	__be32 tmp;
2848cfcabdccSStephen Hemminger 	char *pos;
28491da177e4SLinus Torvalds 
2850222f1806SLuiz Capitulino 	for (i = 0; i < 16; i++)
2851222f1806SLuiz Capitulino 		ip[i] = 0;
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds 	for (;;) {
28541da177e4SLinus Torvalds 		if (*s == ':') {
28551da177e4SLinus Torvalds 			len++;
28561da177e4SLinus Torvalds 			if (s[1] == ':') {	/* Found "::", skip to part 2 */
28571da177e4SLinus Torvalds 				s += 2;
28581da177e4SLinus Torvalds 				len++;
28591da177e4SLinus Torvalds 				break;
28601da177e4SLinus Torvalds 			}
28611da177e4SLinus Torvalds 			s++;
28621da177e4SLinus Torvalds 		}
28631da177e4SLinus Torvalds 
2864cfcabdccSStephen Hemminger 		u = simple_strtoul(s, &pos, 16);
2865cfcabdccSStephen Hemminger 		i = pos - s;
2866222f1806SLuiz Capitulino 		if (!i)
2867222f1806SLuiz Capitulino 			return 0;
28681da177e4SLinus Torvalds 		if (prefixlen == 12 && s[i] == '.') {
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 			/* the last 4 bytes may be written as IPv4 address */
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds 			tmp = in_aton(s);
28731da177e4SLinus Torvalds 			memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
28741da177e4SLinus Torvalds 			return i + len;
28751da177e4SLinus Torvalds 		}
28761da177e4SLinus Torvalds 		ip[prefixlen++] = (u >> 8);
28771da177e4SLinus Torvalds 		ip[prefixlen++] = (u & 255);
2878222f1806SLuiz Capitulino 		s += i;
2879222f1806SLuiz Capitulino 		len += i;
28801da177e4SLinus Torvalds 		if (prefixlen == 16)
28811da177e4SLinus Torvalds 			return len;
28821da177e4SLinus Torvalds 	}
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds /* part 2, after "::" */
28851da177e4SLinus Torvalds 	for (;;) {
28861da177e4SLinus Torvalds 		if (*s == ':') {
28871da177e4SLinus Torvalds 			if (suffixlen == 0)
28881da177e4SLinus Torvalds 				break;
28891da177e4SLinus Torvalds 			s++;
28901da177e4SLinus Torvalds 			len++;
28911da177e4SLinus Torvalds 		} else if (suffixlen != 0)
28921da177e4SLinus Torvalds 			break;
2893cfcabdccSStephen Hemminger 
2894cfcabdccSStephen Hemminger 		u = simple_strtol(s, &pos, 16);
2895cfcabdccSStephen Hemminger 		i = pos - s;
28961da177e4SLinus Torvalds 		if (!i) {
2897222f1806SLuiz Capitulino 			if (*s)
2898222f1806SLuiz Capitulino 				len--;
28991da177e4SLinus Torvalds 			break;
29001da177e4SLinus Torvalds 		}
29011da177e4SLinus Torvalds 		if (suffixlen + prefixlen <= 12 && s[i] == '.') {
29021da177e4SLinus Torvalds 			tmp = in_aton(s);
2903222f1806SLuiz Capitulino 			memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2904222f1806SLuiz Capitulino 			       sizeof(tmp));
29051da177e4SLinus Torvalds 			suffixlen += 4;
29061da177e4SLinus Torvalds 			len += strlen(s);
29071da177e4SLinus Torvalds 			break;
29081da177e4SLinus Torvalds 		}
29091da177e4SLinus Torvalds 		suffix[suffixlen++] = (u >> 8);
29101da177e4SLinus Torvalds 		suffix[suffixlen++] = (u & 255);
2911222f1806SLuiz Capitulino 		s += i;
2912222f1806SLuiz Capitulino 		len += i;
29131da177e4SLinus Torvalds 		if (prefixlen + suffixlen == 16)
29141da177e4SLinus Torvalds 			break;
29151da177e4SLinus Torvalds 	}
29161da177e4SLinus Torvalds 	for (i = 0; i < suffixlen; i++)
29171da177e4SLinus Torvalds 		ip[16 - suffixlen + i] = suffix[i];
29181da177e4SLinus Torvalds 	return len;
29191da177e4SLinus Torvalds }
29201da177e4SLinus Torvalds 
2921222f1806SLuiz Capitulino static char tohex(char hexdigit)
2922222f1806SLuiz Capitulino {
29231da177e4SLinus Torvalds 	return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
29241da177e4SLinus Torvalds }
29251da177e4SLinus Torvalds 
2926222f1806SLuiz Capitulino static int fmt_xlong(char *s, unsigned int i)
2927222f1806SLuiz Capitulino {
29281da177e4SLinus Torvalds 	char *bak = s;
2929222f1806SLuiz Capitulino 	*s = tohex((i >> 12) & 0xf);
2930222f1806SLuiz Capitulino 	if (s != bak || *s != '0')
2931222f1806SLuiz Capitulino 		++s;
2932222f1806SLuiz Capitulino 	*s = tohex((i >> 8) & 0xf);
2933222f1806SLuiz Capitulino 	if (s != bak || *s != '0')
2934222f1806SLuiz Capitulino 		++s;
2935222f1806SLuiz Capitulino 	*s = tohex((i >> 4) & 0xf);
2936222f1806SLuiz Capitulino 	if (s != bak || *s != '0')
2937222f1806SLuiz Capitulino 		++s;
29381da177e4SLinus Torvalds 	*s = tohex(i & 0xf);
29391da177e4SLinus Torvalds 	return s - bak + 1;
29401da177e4SLinus Torvalds }
29411da177e4SLinus Torvalds 
2942222f1806SLuiz Capitulino static unsigned int fmt_ip6(char *s, const char ip[16])
2943222f1806SLuiz Capitulino {
29441da177e4SLinus Torvalds 	unsigned int len;
29451da177e4SLinus Torvalds 	unsigned int i;
29461da177e4SLinus Torvalds 	unsigned int temp;
29471da177e4SLinus Torvalds 	unsigned int compressing;
29481da177e4SLinus Torvalds 	int j;
29491da177e4SLinus Torvalds 
2950222f1806SLuiz Capitulino 	len = 0;
2951222f1806SLuiz Capitulino 	compressing = 0;
29521da177e4SLinus Torvalds 	for (j = 0; j < 16; j += 2) {
29531da177e4SLinus Torvalds 
29541da177e4SLinus Torvalds #ifdef V4MAPPEDPREFIX
29551da177e4SLinus Torvalds 		if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
29561da177e4SLinus Torvalds 			inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
29571da177e4SLinus Torvalds 			temp = strlen(s);
29581da177e4SLinus Torvalds 			return len + temp;
29591da177e4SLinus Torvalds 		}
29601da177e4SLinus Torvalds #endif
29611da177e4SLinus Torvalds 		temp = ((unsigned long)(unsigned char)ip[j] << 8) +
29621da177e4SLinus Torvalds 		    (unsigned long)(unsigned char)ip[j + 1];
29631da177e4SLinus Torvalds 		if (temp == 0) {
29641da177e4SLinus Torvalds 			if (!compressing) {
29651da177e4SLinus Torvalds 				compressing = 1;
29661da177e4SLinus Torvalds 				if (j == 0) {
2967222f1806SLuiz Capitulino 					*s++ = ':';
2968222f1806SLuiz Capitulino 					++len;
29691da177e4SLinus Torvalds 				}
29701da177e4SLinus Torvalds 			}
29711da177e4SLinus Torvalds 		} else {
29721da177e4SLinus Torvalds 			if (compressing) {
29731da177e4SLinus Torvalds 				compressing = 0;
2974222f1806SLuiz Capitulino 				*s++ = ':';
2975222f1806SLuiz Capitulino 				++len;
29761da177e4SLinus Torvalds 			}
2977222f1806SLuiz Capitulino 			i = fmt_xlong(s, temp);
2978222f1806SLuiz Capitulino 			len += i;
2979222f1806SLuiz Capitulino 			s += i;
29801da177e4SLinus Torvalds 			if (j < 14) {
29811da177e4SLinus Torvalds 				*s++ = ':';
29821da177e4SLinus Torvalds 				++len;
29831da177e4SLinus Torvalds 			}
29841da177e4SLinus Torvalds 		}
29851da177e4SLinus Torvalds 	}
29861da177e4SLinus Torvalds 	if (compressing) {
2987222f1806SLuiz Capitulino 		*s++ = ':';
2988222f1806SLuiz Capitulino 		++len;
29891da177e4SLinus Torvalds 	}
29901da177e4SLinus Torvalds 	*s = 0;
29911da177e4SLinus Torvalds 	return len;
29921da177e4SLinus Torvalds }
29931da177e4SLinus Torvalds 
29941da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
29951da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
29961da177e4SLinus Torvalds {
29971da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
29981da177e4SLinus Torvalds 	__u8 *eth;
29991da177e4SLinus Torvalds 	struct udphdr *udph;
30001da177e4SLinus Torvalds 	int datalen;
30011da177e4SLinus Torvalds 	struct ipv6hdr *iph;
3002d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
3003ca6549afSSteven Whitehouse 	__be32 *mpls;
300434954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
300534954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
300634954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
300734954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
3008fd2ea0a7SDavid S. Miller 	u16 queue_map;
3009ca6549afSSteven Whitehouse 
3010ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
3011d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
30121da177e4SLinus Torvalds 
301334954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
3014d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
301534954ddcSFrancesco Fondelli 
301664053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
301764053beeSRobert Olsson 	 * fields.
301864053beeSRobert Olsson 	 */
301964053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
3020eb589063SJunchang Wang 	queue_map = pkt_dev->cur_queue_map;
302164053beeSRobert Olsson 
3022e470757dSStephen Hemminger 	skb = __netdev_alloc_skb(odev,
3023e470757dSStephen Hemminger 				 pkt_dev->cur_pkt_size + 64
3024e470757dSStephen Hemminger 				 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
30251da177e4SLinus Torvalds 	if (!skb) {
30261da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
30271da177e4SLinus Torvalds 		return NULL;
30281da177e4SLinus Torvalds 	}
3029a8d764b9SJunchang Wang 	prefetchw(skb->data);
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds 	skb_reserve(skb, 16);
30321da177e4SLinus Torvalds 
30331da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
30341da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
3035ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
3036ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
3037ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
303834954ddcSFrancesco Fondelli 
303934954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
304034954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
304134954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
30420f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
30430f37c605SAl Viro 					       pkt_dev->svlan_cfi,
30440f37c605SAl Viro 					       pkt_dev->svlan_p);
304534954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
3046d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
304734954ddcSFrancesco Fondelli 		}
304834954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
30490f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
30500f37c605SAl Viro 				      pkt_dev->vlan_cfi,
30510f37c605SAl Viro 				      pkt_dev->vlan_p);
305234954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
3053d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
305434954ddcSFrancesco Fondelli 	}
305534954ddcSFrancesco Fondelli 
305627a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
3057b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
3058ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
3059fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
30609e50e3acSJohn Fastabend 	skb->priority = pkt_dev->skb_priority;
3061ddc7b8e3SArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
3062ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
30631da177e4SLinus Torvalds 
30641da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
3065252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
30661da177e4SLinus Torvalds 
3067ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
3068ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
3069ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
307016dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds 	if (datalen < sizeof(struct pktgen_hdr)) {
30731da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
30741da177e4SLinus Torvalds 		if (net_ratelimit())
3075f9467eaeSJoe Perches 			pr_info("increased datalen to %d\n", datalen);
30761da177e4SLinus Torvalds 	}
30771da177e4SLinus Torvalds 
30781da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
30791da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
30801da177e4SLinus Torvalds 	udph->len = htons(datalen + sizeof(struct udphdr));
30811da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
30821da177e4SLinus Torvalds 
3083d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
30841da177e4SLinus Torvalds 
30851ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
30861ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
3087252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
30881ca7768cSFrancesco Fondelli 	}
30891ca7768cSFrancesco Fondelli 
30901da177e4SLinus Torvalds 	iph->hop_limit = 32;
30911da177e4SLinus Torvalds 
30921da177e4SLinus Torvalds 	iph->payload_len = htons(sizeof(struct udphdr) + datalen);
30931da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds 	ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
30961da177e4SLinus Torvalds 	ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
30971da177e4SLinus Torvalds 
3098b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
309916dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
3100ca6549afSSteven Whitehouse 	skb->protocol = protocol;
31011da177e4SLinus Torvalds 	skb->dev = odev;
31021da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
31031da177e4SLinus Torvalds 
310426ad7879SEric Dumazet 	pktgen_finalize_skb(pkt_dev, skb, datalen);
31051da177e4SLinus Torvalds 
31061da177e4SLinus Torvalds 	return skb;
31071da177e4SLinus Torvalds }
31081da177e4SLinus Torvalds 
3109475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
31101da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
31111da177e4SLinus Torvalds {
31121da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
31131da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
31141da177e4SLinus Torvalds 	else
31151da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
31161da177e4SLinus Torvalds }
31171da177e4SLinus Torvalds 
31181da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
31191da177e4SLinus Torvalds {
31201da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
31211da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
31221da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
31231da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
31241da177e4SLinus Torvalds 	pkt_dev->errors = 0;
31251da177e4SLinus Torvalds }
31261da177e4SLinus Torvalds 
31271da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
31281da177e4SLinus Torvalds 
31291da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
31301da177e4SLinus Torvalds {
3131c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
31321da177e4SLinus Torvalds 	int started = 0;
31331da177e4SLinus Torvalds 
3134f9467eaeSJoe Perches 	func_enter();
31351da177e4SLinus Torvalds 
31361da177e4SLinus Torvalds 	if_lock(t);
3137c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
31381da177e4SLinus Torvalds 
31391da177e4SLinus Torvalds 		/*
31401da177e4SLinus Torvalds 		 * setup odev and create initial packet.
31411da177e4SLinus Torvalds 		 */
31421da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds 		if (pkt_dev->odev) {
31451da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
31461da177e4SLinus Torvalds 			pkt_dev->running = 1;	/* Cranke yeself! */
31471da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
3148fd29cf72SStephen Hemminger 			pkt_dev->started_at =
3149fd29cf72SStephen Hemminger 				pkt_dev->next_tx = ktime_now();
3150fd29cf72SStephen Hemminger 
315116dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
31521da177e4SLinus Torvalds 
31531da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
31541da177e4SLinus Torvalds 			started++;
3155222f1806SLuiz Capitulino 		} else
31561da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
31571da177e4SLinus Torvalds 	}
31581da177e4SLinus Torvalds 	if_unlock(t);
3159222f1806SLuiz Capitulino 	if (started)
3160222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
31611da177e4SLinus Torvalds }
31621da177e4SLinus Torvalds 
31631da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void)
31641da177e4SLinus Torvalds {
3165cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31661da177e4SLinus Torvalds 
3167f9467eaeSJoe Perches 	func_enter();
31681da177e4SLinus Torvalds 
31696146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3170cdcdbe0bSLuiz Capitulino 
3171cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list)
317295ed63f7SArthur Kepner 		t->control |= T_STOP;
3173cdcdbe0bSLuiz Capitulino 
31746146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31751da177e4SLinus Torvalds }
31761da177e4SLinus Torvalds 
3177648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
31781da177e4SLinus Torvalds {
3179648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
31801da177e4SLinus Torvalds 
3181c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
3182648fda74SStephen Hemminger 		if (pkt_dev->running)
3183648fda74SStephen Hemminger 			return 1;
3184648fda74SStephen Hemminger 	return 0;
31851da177e4SLinus Torvalds }
31861da177e4SLinus Torvalds 
31871da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
31881da177e4SLinus Torvalds {
31891da177e4SLinus Torvalds 	if_lock(t);
31901da177e4SLinus Torvalds 
31911da177e4SLinus Torvalds 	while (thread_is_running(t)) {
31921da177e4SLinus Torvalds 
31931da177e4SLinus Torvalds 		if_unlock(t);
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds 		msleep_interruptible(100);
31961da177e4SLinus Torvalds 
31971da177e4SLinus Torvalds 		if (signal_pending(current))
31981da177e4SLinus Torvalds 			goto signal;
31991da177e4SLinus Torvalds 		if_lock(t);
32001da177e4SLinus Torvalds 	}
32011da177e4SLinus Torvalds 	if_unlock(t);
32021da177e4SLinus Torvalds 	return 1;
32031da177e4SLinus Torvalds signal:
32041da177e4SLinus Torvalds 	return 0;
32051da177e4SLinus Torvalds }
32061da177e4SLinus Torvalds 
32071da177e4SLinus Torvalds static int pktgen_wait_all_threads_run(void)
32081da177e4SLinus Torvalds {
3209cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
32101da177e4SLinus Torvalds 	int sig = 1;
32111da177e4SLinus Torvalds 
32126146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3213cdcdbe0bSLuiz Capitulino 
3214cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list) {
32151da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
3216222f1806SLuiz Capitulino 		if (sig == 0)
3217222f1806SLuiz Capitulino 			break;
32181da177e4SLinus Torvalds 	}
3219cdcdbe0bSLuiz Capitulino 
3220cdcdbe0bSLuiz Capitulino 	if (sig == 0)
3221cdcdbe0bSLuiz Capitulino 		list_for_each_entry(t, &pktgen_threads, th_list)
32221da177e4SLinus Torvalds 			t->control |= (T_STOP);
3223cdcdbe0bSLuiz Capitulino 
32246146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
32251da177e4SLinus Torvalds 	return sig;
32261da177e4SLinus Torvalds }
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds static void pktgen_run_all_threads(void)
32291da177e4SLinus Torvalds {
3230cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
32311da177e4SLinus Torvalds 
3232f9467eaeSJoe Perches 	func_enter();
32331da177e4SLinus Torvalds 
32346146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
32351da177e4SLinus Torvalds 
3236cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list)
32371da177e4SLinus Torvalds 		t->control |= (T_RUN);
3238cdcdbe0bSLuiz Capitulino 
32396146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
32401da177e4SLinus Torvalds 
324163adc6fbSStephen Hemminger 	/* Propagate thread->control  */
324263adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	pktgen_wait_all_threads_run();
32451da177e4SLinus Torvalds }
32461da177e4SLinus Torvalds 
3247eb37b41cSJesse Brandeburg static void pktgen_reset_all_threads(void)
3248eb37b41cSJesse Brandeburg {
3249eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3250eb37b41cSJesse Brandeburg 
3251f9467eaeSJoe Perches 	func_enter();
3252eb37b41cSJesse Brandeburg 
3253eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3254eb37b41cSJesse Brandeburg 
3255eb37b41cSJesse Brandeburg 	list_for_each_entry(t, &pktgen_threads, th_list)
3256eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3257eb37b41cSJesse Brandeburg 
3258eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3259eb37b41cSJesse Brandeburg 
326063adc6fbSStephen Hemminger 	/* Propagate thread->control  */
326163adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3262eb37b41cSJesse Brandeburg 
3263eb37b41cSJesse Brandeburg 	pktgen_wait_all_threads_run();
3264eb37b41cSJesse Brandeburg }
3265eb37b41cSJesse Brandeburg 
32661da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
32671da177e4SLinus Torvalds {
3268fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
32691da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3270fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3271fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3272fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
32731da177e4SLinus Torvalds 
327403a14ab1SDaniel Turull 	p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3275fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3276fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3277fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
32781da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
32791da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
32801da177e4SLinus Torvalds 
3281fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3282fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
32851da177e4SLinus Torvalds 
32861da177e4SLinus Torvalds 	mbps = bps;
32871da177e4SLinus Torvalds 	do_div(mbps, 1000000);
32881da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
32891da177e4SLinus Torvalds 		     (unsigned long long)pps,
32901da177e4SLinus Torvalds 		     (unsigned long long)mbps,
32911da177e4SLinus Torvalds 		     (unsigned long long)bps,
32921da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
32931da177e4SLinus Torvalds }
32941da177e4SLinus Torvalds 
32951da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
32961da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
32971da177e4SLinus Torvalds {
3298222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
32991da177e4SLinus Torvalds 
33001da177e4SLinus Torvalds 	if (!pkt_dev->running) {
3301f9467eaeSJoe Perches 		pr_warning("interface: %s is already stopped\n",
3302f9467eaeSJoe Perches 			   pkt_dev->odevname);
33031da177e4SLinus Torvalds 		return -EINVAL;
33041da177e4SLinus Torvalds 	}
33051da177e4SLinus Torvalds 
33063bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
33073bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3308fd29cf72SStephen Hemminger 	pkt_dev->stopped_at = ktime_now();
33091da177e4SLinus Torvalds 	pkt_dev->running = 0;
33101da177e4SLinus Torvalds 
331195ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
33121da177e4SLinus Torvalds 
33131da177e4SLinus Torvalds 	return 0;
33141da177e4SLinus Torvalds }
33151da177e4SLinus Torvalds 
33161da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
33171da177e4SLinus Torvalds {
3318c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
33191da177e4SLinus Torvalds 
33201da177e4SLinus Torvalds 	if_lock(t);
33211da177e4SLinus Torvalds 
3322c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3323c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3324222f1806SLuiz Capitulino 			continue;
3325222f1806SLuiz Capitulino 		if (best == NULL)
3326c26a8016SLuiz Capitulino 			best = pkt_dev;
3327fd29cf72SStephen Hemminger 		else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
3328c26a8016SLuiz Capitulino 			best = pkt_dev;
33291da177e4SLinus Torvalds 	}
33301da177e4SLinus Torvalds 	if_unlock(t);
33311da177e4SLinus Torvalds 	return best;
33321da177e4SLinus Torvalds }
33331da177e4SLinus Torvalds 
3334222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3335222f1806SLuiz Capitulino {
3336c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
33371da177e4SLinus Torvalds 
3338f9467eaeSJoe Perches 	func_enter();
33391da177e4SLinus Torvalds 
33401da177e4SLinus Torvalds 	if_lock(t);
33411da177e4SLinus Torvalds 
3342c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3343c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
334495ed63f7SArthur Kepner 	}
334595ed63f7SArthur Kepner 
334695ed63f7SArthur Kepner 	if_unlock(t);
334795ed63f7SArthur Kepner }
334895ed63f7SArthur Kepner 
334995ed63f7SArthur Kepner /*
335095ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
335195ed63f7SArthur Kepner  * and remove it
335295ed63f7SArthur Kepner  */
335395ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
335495ed63f7SArthur Kepner {
3355c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3356c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
335795ed63f7SArthur Kepner 
3358f9467eaeSJoe Perches 	func_enter();
335995ed63f7SArthur Kepner 
336095ed63f7SArthur Kepner 	if_lock(t);
336195ed63f7SArthur Kepner 
3362c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3363c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
336495ed63f7SArthur Kepner 
3365222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3366222f1806SLuiz Capitulino 			continue;
336795ed63f7SArthur Kepner 
336895ed63f7SArthur Kepner 		kfree_skb(cur->skb);
336995ed63f7SArthur Kepner 		cur->skb = NULL;
337095ed63f7SArthur Kepner 
337195ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
337295ed63f7SArthur Kepner 
337395ed63f7SArthur Kepner 		break;
337495ed63f7SArthur Kepner 	}
33751da177e4SLinus Torvalds 
33761da177e4SLinus Torvalds 	if_unlock(t);
33771da177e4SLinus Torvalds }
33781da177e4SLinus Torvalds 
33791da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
33801da177e4SLinus Torvalds {
3381c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3382c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
33831da177e4SLinus Torvalds 
3384f9467eaeSJoe Perches 	func_enter();
3385f9467eaeSJoe Perches 
33861da177e4SLinus Torvalds 	/* Remove all devices, free mem */
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds 	if_lock(t);
33891da177e4SLinus Torvalds 
3390c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3391c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
339295ed63f7SArthur Kepner 
339395ed63f7SArthur Kepner 		kfree_skb(cur->skb);
339495ed63f7SArthur Kepner 		cur->skb = NULL;
339595ed63f7SArthur Kepner 
33961da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
33971da177e4SLinus Torvalds 	}
33981da177e4SLinus Torvalds 
33991da177e4SLinus Torvalds 	if_unlock(t);
34001da177e4SLinus Torvalds }
34011da177e4SLinus Torvalds 
34021da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
34031da177e4SLinus Torvalds {
34041da177e4SLinus Torvalds 	/* Remove from the thread list */
34051da177e4SLinus Torvalds 
3406ee74baa7SDavid S. Miller 	remove_proc_entry(t->tsk->comm, pg_proc_dir);
34071da177e4SLinus Torvalds 
34081da177e4SLinus Torvalds }
34091da177e4SLinus Torvalds 
3410ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
34113791decbSStephen Hemminger {
3412fd29cf72SStephen Hemminger 	ktime_t idle_start = ktime_now();
34133791decbSStephen Hemminger 	schedule();
3414fd29cf72SStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
34153791decbSStephen Hemminger }
34163791decbSStephen Hemminger 
3417ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3418ef87979cSStephen Hemminger {
3419ef87979cSStephen Hemminger 	ktime_t idle_start = ktime_now();
3420ef87979cSStephen Hemminger 
3421ef87979cSStephen Hemminger 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3422ef87979cSStephen Hemminger 		if (signal_pending(current))
3423ef87979cSStephen Hemminger 			break;
3424ef87979cSStephen Hemminger 
3425ef87979cSStephen Hemminger 		if (need_resched())
3426ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3427ef87979cSStephen Hemminger 		else
3428ef87979cSStephen Hemminger 			cpu_relax();
3429ef87979cSStephen Hemminger 	}
3430ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3431ef87979cSStephen Hemminger }
3432fd29cf72SStephen Hemminger 
3433475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
34341da177e4SLinus Torvalds {
343500829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
34366fef4c0cSStephen Hemminger 	netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
343700829823SStephen Hemminger 		= odev->netdev_ops->ndo_start_xmit;
3438fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
3439fd2ea0a7SDavid S. Miller 	u16 queue_map;
34401da177e4SLinus Torvalds 	int ret;
34411da177e4SLinus Torvalds 
3442ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3443ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
34441da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
34453791decbSStephen Hemminger 		return;
34461da177e4SLinus Torvalds 	}
34471da177e4SLinus Torvalds 
3448ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3449ef87979cSStephen Hemminger 	 * "never transmit"
3450ef87979cSStephen Hemminger 	 */
3451ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3452ef87979cSStephen Hemminger 		pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3453ef87979cSStephen Hemminger 		return;
3454ef87979cSStephen Hemminger 	}
3455ef87979cSStephen Hemminger 
3456ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
34577d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
34587d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
34591da177e4SLinus Torvalds 		/* build a new pkt */
34601da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
34611da177e4SLinus Torvalds 
34621da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
34631da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
3464f9467eaeSJoe Perches 			pr_err("ERROR: couldn't allocate skb in fill_packet\n");
34651da177e4SLinus Torvalds 			schedule();
34661da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
34673791decbSStephen Hemminger 			return;
34681da177e4SLinus Torvalds 		}
3469baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
34701da177e4SLinus Torvalds 		pkt_dev->allocated_skbs++;
34711da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
34721da177e4SLinus Torvalds 	}
34731da177e4SLinus Torvalds 
3474ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3475ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3476ef87979cSStephen Hemminger 
3477fd2ea0a7SDavid S. Miller 	queue_map = skb_get_queue_mapping(pkt_dev->skb);
3478fd2ea0a7SDavid S. Miller 	txq = netdev_get_tx_queue(odev, queue_map);
3479fd2ea0a7SDavid S. Miller 
3480fd2ea0a7SDavid S. Miller 	__netif_tx_lock_bh(txq);
34815b8db2f5SStephen Hemminger 
34825a0d2268SEric Dumazet 	if (unlikely(netif_tx_queue_frozen_or_stopped(txq))) {
3483ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
34840835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
34850835acfeSEric Dumazet 		goto unlock;
34860835acfeSEric Dumazet 	}
34870835acfeSEric Dumazet 	atomic_inc(&(pkt_dev->skb->users));
348800829823SStephen Hemminger 	ret = (*xmit)(pkt_dev->skb, odev);
3489ef87979cSStephen Hemminger 
34905b8db2f5SStephen Hemminger 	switch (ret) {
34915b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
349208baf561SEric Dumazet 		txq_trans_update(txq);
34931da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
34941da177e4SLinus Torvalds 		pkt_dev->sofar++;
34951da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3496baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
34975b8db2f5SStephen Hemminger 		break;
3498f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3499f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3500f466dba1SJohn Fastabend 	case NET_XMIT_POLICED:
3501f466dba1SJohn Fastabend 		/* skb has been consumed */
3502f466dba1SJohn Fastabend 		pkt_dev->errors++;
3503f466dba1SJohn Fastabend 		break;
35045b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
35055b8db2f5SStephen Hemminger 		if (net_ratelimit())
350658231186SJoe Perches 			pr_info("%s xmit error: %d\n", pkt_dev->odevname, ret);
35071da177e4SLinus Torvalds 		pkt_dev->errors++;
35085b8db2f5SStephen Hemminger 		/* fallthru */
3509ef87979cSStephen Hemminger 	case NETDEV_TX_LOCKED:
35105b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
35115b8db2f5SStephen Hemminger 		/* Retry it next time */
35125b8db2f5SStephen Hemminger 		atomic_dec(&(pkt_dev->skb->users));
35131da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
35141da177e4SLinus Torvalds 	}
35150835acfeSEric Dumazet unlock:
3516fd2ea0a7SDavid S. Miller 	__netif_tx_unlock_bh(txq);
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
35191da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3520ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds 		/* Done with this */
35231da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
35241da177e4SLinus Torvalds 	}
35251da177e4SLinus Torvalds }
35261da177e4SLinus Torvalds 
35271da177e4SLinus Torvalds /*
35281da177e4SLinus Torvalds  * Main loop of the thread goes here
35291da177e4SLinus Torvalds  */
35301da177e4SLinus Torvalds 
3531ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
35321da177e4SLinus Torvalds {
35331da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3534ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
35351da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
35361da177e4SLinus Torvalds 	int cpu = t->cpu;
35371da177e4SLinus Torvalds 
3538ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
35391da177e4SLinus Torvalds 
35401da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3541d3ede327SDenis V. Lunev 	complete(&t->start_done);
35421da177e4SLinus Torvalds 
3543f9467eaeSJoe Perches 	pr_debug("starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
35441da177e4SLinus Torvalds 
3545ee74baa7SDavid S. Miller 	set_current_state(TASK_INTERRUPTIBLE);
35461da177e4SLinus Torvalds 
354783144186SRafael J. Wysocki 	set_freezable();
354883144186SRafael J. Wysocki 
3549ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3550ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3551ee74baa7SDavid S. Miller 
3552ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
3553551eaff1SEric Dumazet 			if (pktgen_exiting)
3554551eaff1SEric Dumazet 				break;
3555ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3556ef87979cSStephen Hemminger 							 t->control != 0,
3557ef87979cSStephen Hemminger 							 HZ/10);
35581b3f720bSRafael J. Wysocki 			try_to_freeze();
3559ef87979cSStephen Hemminger 			continue;
3560ee74baa7SDavid S. Miller 		}
35611da177e4SLinus Torvalds 
35621da177e4SLinus Torvalds 		__set_current_state(TASK_RUNNING);
35631da177e4SLinus Torvalds 
3564ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
35651da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
35661da177e4SLinus Torvalds 
3567ef87979cSStephen Hemminger 			if (need_resched())
3568ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3569ef87979cSStephen Hemminger 			else
3570ef87979cSStephen Hemminger 				cpu_relax();
3571ef87979cSStephen Hemminger 		}
3572ef87979cSStephen Hemminger 
35731da177e4SLinus Torvalds 		if (t->control & T_STOP) {
35741da177e4SLinus Torvalds 			pktgen_stop(t);
35751da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
35761da177e4SLinus Torvalds 		}
35771da177e4SLinus Torvalds 
35781da177e4SLinus Torvalds 		if (t->control & T_RUN) {
35791da177e4SLinus Torvalds 			pktgen_run(t);
35801da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
35811da177e4SLinus Torvalds 		}
35821da177e4SLinus Torvalds 
358395ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
35841da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
358595ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
358695ed63f7SArthur Kepner 		}
358795ed63f7SArthur Kepner 
358895ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
358995ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
35901da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
35911da177e4SLinus Torvalds 		}
35921da177e4SLinus Torvalds 
359309fe3ef4SAndrew Morton 		try_to_freeze();
359409fe3ef4SAndrew Morton 
3595ee74baa7SDavid S. Miller 		set_current_state(TASK_INTERRUPTIBLE);
35961da177e4SLinus Torvalds 	}
35971da177e4SLinus Torvalds 
3598f9467eaeSJoe Perches 	pr_debug("%s stopping all device\n", t->tsk->comm);
35991da177e4SLinus Torvalds 	pktgen_stop(t);
36001da177e4SLinus Torvalds 
3601f9467eaeSJoe Perches 	pr_debug("%s removing all device\n", t->tsk->comm);
36021da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
36031da177e4SLinus Torvalds 
3604f9467eaeSJoe Perches 	pr_debug("%s removing thread\n", t->tsk->comm);
36051da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3606cdcdbe0bSLuiz Capitulino 
3607551eaff1SEric Dumazet 	/* Wait for kthread_stop */
3608551eaff1SEric Dumazet 	while (!kthread_should_stop()) {
3609551eaff1SEric Dumazet 		set_current_state(TASK_INTERRUPTIBLE);
3610551eaff1SEric Dumazet 		schedule();
3611551eaff1SEric Dumazet 	}
3612551eaff1SEric Dumazet 	__set_current_state(TASK_RUNNING);
3613551eaff1SEric Dumazet 
3614ee74baa7SDavid S. Miller 	return 0;
36151da177e4SLinus Torvalds }
36161da177e4SLinus Torvalds 
3617222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
36183e984840SEric Dumazet 					  const char *ifname, bool exact)
36191da177e4SLinus Torvalds {
3620c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
36213e984840SEric Dumazet 	size_t len = strlen(ifname);
36221da177e4SLinus Torvalds 
36233e984840SEric Dumazet 	if_lock(t);
3624c26a8016SLuiz Capitulino 	list_for_each_entry(p, &t->if_list, list)
36253e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
36263e984840SEric Dumazet 			if (p->odevname[len]) {
36273e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
36283e984840SEric Dumazet 					continue;
36293e984840SEric Dumazet 			}
3630c26a8016SLuiz Capitulino 			pkt_dev = p;
36311da177e4SLinus Torvalds 			break;
36321da177e4SLinus Torvalds 		}
36331da177e4SLinus Torvalds 
36341da177e4SLinus Torvalds 	if_unlock(t);
3635f9467eaeSJoe Perches 	pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
36361da177e4SLinus Torvalds 	return pkt_dev;
36371da177e4SLinus Torvalds }
36381da177e4SLinus Torvalds 
36391da177e4SLinus Torvalds /*
36401da177e4SLinus Torvalds  * Adds a dev at front of if_list.
36411da177e4SLinus Torvalds  */
36421da177e4SLinus Torvalds 
3643222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3644222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
36451da177e4SLinus Torvalds {
36461da177e4SLinus Torvalds 	int rv = 0;
36471da177e4SLinus Torvalds 
36481da177e4SLinus Torvalds 	if_lock(t);
36491da177e4SLinus Torvalds 
36501da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
3651f9467eaeSJoe Perches 		pr_err("ERROR: already assigned to a thread\n");
36521da177e4SLinus Torvalds 		rv = -EBUSY;
36531da177e4SLinus Torvalds 		goto out;
36541da177e4SLinus Torvalds 	}
3655c26a8016SLuiz Capitulino 
3656c26a8016SLuiz Capitulino 	list_add(&pkt_dev->list, &t->if_list);
36571da177e4SLinus Torvalds 	pkt_dev->pg_thread = t;
36581da177e4SLinus Torvalds 	pkt_dev->running = 0;
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds out:
36611da177e4SLinus Torvalds 	if_unlock(t);
36621da177e4SLinus Torvalds 	return rv;
36631da177e4SLinus Torvalds }
36641da177e4SLinus Torvalds 
36651da177e4SLinus Torvalds /* Called under thread lock */
36661da177e4SLinus Torvalds 
36671da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
36681da177e4SLinus Torvalds {
36691da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
367039df232fSStephen Hemminger 	int err;
36713291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
36741da177e4SLinus Torvalds 
3675d50a6b56SStephen Hemminger 	pkt_dev = __pktgen_NN_threads(ifname, FIND);
3676d50a6b56SStephen Hemminger 	if (pkt_dev) {
3677f9467eaeSJoe Perches 		pr_err("ERROR: interface already used\n");
3678d50a6b56SStephen Hemminger 		return -EBUSY;
3679d50a6b56SStephen Hemminger 	}
36801da177e4SLinus Torvalds 
36813291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
36821da177e4SLinus Torvalds 	if (!pkt_dev)
36831da177e4SLinus Torvalds 		return -ENOMEM;
36841da177e4SLinus Torvalds 
3685593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
36863291b9dbSEric Dumazet 	pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
36873291b9dbSEric Dumazet 				      node);
36881da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
36891da177e4SLinus Torvalds 		kfree(pkt_dev);
36901da177e4SLinus Torvalds 		return -ENOMEM;
36911da177e4SLinus Torvalds 	}
36921da177e4SLinus Torvalds 	memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
36931da177e4SLinus Torvalds 
369495ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
36951da177e4SLinus Torvalds 	pkt_dev->min_pkt_size = ETH_ZLEN;
36961da177e4SLinus Torvalds 	pkt_dev->max_pkt_size = ETH_ZLEN;
36971da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
36981da177e4SLinus Torvalds 	pkt_dev->clone_skb = pg_clone_skb_d;
3699fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
37001da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
37011da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
37021da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
37031da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
37041da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
37051da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
37061da177e4SLinus Torvalds 
370734954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
370834954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
370934954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
371034954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
371134954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
371234954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
3713e99b99b4SRobert Olsson 	pkt_dev->node = -1;
371434954ddcSFrancesco Fondelli 
371539df232fSStephen Hemminger 	err = pktgen_setup_dev(pkt_dev, ifname);
371639df232fSStephen Hemminger 	if (err)
371739df232fSStephen Hemminger 		goto out1;
37181da177e4SLinus Torvalds 
37195efdccbcSDenis V. Lunev 	pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
37205efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
372139df232fSStephen Hemminger 	if (!pkt_dev->entry) {
3722f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3723d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
372439df232fSStephen Hemminger 		err = -EINVAL;
372539df232fSStephen Hemminger 		goto out2;
372639df232fSStephen Hemminger 	}
3727a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3728a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3729a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3730a553e4a6SJamal Hadi Salim #endif
373139df232fSStephen Hemminger 
373239df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
373339df232fSStephen Hemminger out2:
373439df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
373539df232fSStephen Hemminger out1:
3736a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3737a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3738a553e4a6SJamal Hadi Salim #endif
37391da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
37401da177e4SLinus Torvalds 	kfree(pkt_dev);
374139df232fSStephen Hemminger 	return err;
37421da177e4SLinus Torvalds }
37431da177e4SLinus Torvalds 
3744ee74baa7SDavid S. Miller static int __init pktgen_create_thread(int cpu)
37451da177e4SLinus Torvalds {
3746cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3747d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3748ee74baa7SDavid S. Miller 	struct task_struct *p;
37491da177e4SLinus Torvalds 
37503291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
37513291b9dbSEric Dumazet 			 cpu_to_node(cpu));
37521da177e4SLinus Torvalds 	if (!t) {
3753f9467eaeSJoe Perches 		pr_err("ERROR: out of memory, can't create new thread\n");
37541da177e4SLinus Torvalds 		return -ENOMEM;
37551da177e4SLinus Torvalds 	}
37561da177e4SLinus Torvalds 
37571da177e4SLinus Torvalds 	spin_lock_init(&t->if_lock);
37581da177e4SLinus Torvalds 	t->cpu = cpu;
37591da177e4SLinus Torvalds 
3760ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3761ee74baa7SDavid S. Miller 
3762ee74baa7SDavid S. Miller 	list_add_tail(&t->th_list, &pktgen_threads);
3763d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3764ee74baa7SDavid S. Miller 
376594dcf29aSEric Dumazet 	p = kthread_create_on_node(pktgen_thread_worker,
376694dcf29aSEric Dumazet 				   t,
376794dcf29aSEric Dumazet 				   cpu_to_node(cpu),
376894dcf29aSEric Dumazet 				   "kpktgend_%d", cpu);
3769ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
3770f9467eaeSJoe Perches 		pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3771ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3772ee74baa7SDavid S. Miller 		kfree(t);
3773ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3774ee74baa7SDavid S. Miller 	}
3775ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3776ee74baa7SDavid S. Miller 	t->tsk = p;
3777ee74baa7SDavid S. Miller 
37785efdccbcSDenis V. Lunev 	pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
37795efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3780d50a6b56SStephen Hemminger 	if (!pe) {
3781f9467eaeSJoe Perches 		pr_err("cannot create %s/%s procfs entry\n",
3782ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3783ee74baa7SDavid S. Miller 		kthread_stop(p);
3784ee74baa7SDavid S. Miller 		list_del(&t->th_list);
37851da177e4SLinus Torvalds 		kfree(t);
37861da177e4SLinus Torvalds 		return -EINVAL;
37871da177e4SLinus Torvalds 	}
3788d50a6b56SStephen Hemminger 
3789ee74baa7SDavid S. Miller 	wake_up_process(p);
3790d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
37911da177e4SLinus Torvalds 
37921da177e4SLinus Torvalds 	return 0;
37931da177e4SLinus Torvalds }
37941da177e4SLinus Torvalds 
37951da177e4SLinus Torvalds /*
37961da177e4SLinus Torvalds  * Removes a device from the thread if_list.
37971da177e4SLinus Torvalds  */
3798222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3799222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
38001da177e4SLinus Torvalds {
3801c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3802c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
38031da177e4SLinus Torvalds 
3804c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3805c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3806c26a8016SLuiz Capitulino 		if (p == pkt_dev)
3807c26a8016SLuiz Capitulino 			list_del(&p->list);
38081da177e4SLinus Torvalds 	}
38091da177e4SLinus Torvalds }
38101da177e4SLinus Torvalds 
3811222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3812222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
38131da177e4SLinus Torvalds {
38141da177e4SLinus Torvalds 
3815f9467eaeSJoe Perches 	pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
38161da177e4SLinus Torvalds 
38171da177e4SLinus Torvalds 	if (pkt_dev->running) {
3818f9467eaeSJoe Perches 		pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
38191da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
38201da177e4SLinus Torvalds 	}
38211da177e4SLinus Torvalds 
38221da177e4SLinus Torvalds 	/* Dis-associate from the interface */
38231da177e4SLinus Torvalds 
38241da177e4SLinus Torvalds 	if (pkt_dev->odev) {
38251da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
38261da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
38271da177e4SLinus Torvalds 	}
38281da177e4SLinus Torvalds 
38291da177e4SLinus Torvalds 	/* And update the thread if_list */
38301da177e4SLinus Torvalds 
38311da177e4SLinus Torvalds 	_rem_dev_from_if_list(t, pkt_dev);
38321da177e4SLinus Torvalds 
383339df232fSStephen Hemminger 	if (pkt_dev->entry)
383439df232fSStephen Hemminger 		remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
38351da177e4SLinus Torvalds 
3836a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3837a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3838a553e4a6SJamal Hadi Salim #endif
38391da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
384026ad7879SEric Dumazet 	if (pkt_dev->page)
384126ad7879SEric Dumazet 		put_page(pkt_dev->page);
38421da177e4SLinus Torvalds 	kfree(pkt_dev);
38431da177e4SLinus Torvalds 	return 0;
38441da177e4SLinus Torvalds }
38451da177e4SLinus Torvalds 
38461da177e4SLinus Torvalds static int __init pg_init(void)
38471da177e4SLinus Torvalds {
38481da177e4SLinus Torvalds 	int cpu;
3849d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3850d50a6b56SStephen Hemminger 
3851f9467eaeSJoe Perches 	pr_info("%s", version);
38521da177e4SLinus Torvalds 
3853457c4cbcSEric W. Biederman 	pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3854d50a6b56SStephen Hemminger 	if (!pg_proc_dir)
3855d50a6b56SStephen Hemminger 		return -ENODEV;
38561da177e4SLinus Torvalds 
385725296d59SWang Chen 	pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3858d50a6b56SStephen Hemminger 	if (pe == NULL) {
3859f9467eaeSJoe Perches 		pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
3860457c4cbcSEric W. Biederman 		proc_net_remove(&init_net, PG_PROC_DIR);
38611da177e4SLinus Torvalds 		return -EINVAL;
38621da177e4SLinus Torvalds 	}
38631da177e4SLinus Torvalds 
38641da177e4SLinus Torvalds 	/* Register us to receive netdevice events */
38651da177e4SLinus Torvalds 	register_netdevice_notifier(&pktgen_notifier_block);
38661da177e4SLinus Torvalds 
3867670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
38688024bb24SLuiz Capitulino 		int err;
38691da177e4SLinus Torvalds 
3870ee74baa7SDavid S. Miller 		err = pktgen_create_thread(cpu);
38718024bb24SLuiz Capitulino 		if (err)
3872f9467eaeSJoe Perches 			pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3873f9467eaeSJoe Perches 				   cpu, err);
38741da177e4SLinus Torvalds 	}
38758024bb24SLuiz Capitulino 
38768024bb24SLuiz Capitulino 	if (list_empty(&pktgen_threads)) {
3877f9467eaeSJoe Perches 		pr_err("ERROR: Initialization failed for all threads\n");
38788024bb24SLuiz Capitulino 		unregister_netdevice_notifier(&pktgen_notifier_block);
38798024bb24SLuiz Capitulino 		remove_proc_entry(PGCTRL, pg_proc_dir);
3880457c4cbcSEric W. Biederman 		proc_net_remove(&init_net, PG_PROC_DIR);
38818024bb24SLuiz Capitulino 		return -ENODEV;
38828024bb24SLuiz Capitulino 	}
38838024bb24SLuiz Capitulino 
38841da177e4SLinus Torvalds 	return 0;
38851da177e4SLinus Torvalds }
38861da177e4SLinus Torvalds 
38871da177e4SLinus Torvalds static void __exit pg_cleanup(void)
38881da177e4SLinus Torvalds {
3889cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3890cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
38911da177e4SLinus Torvalds 
38921da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
3893551eaff1SEric Dumazet 	pktgen_exiting = true;
38941da177e4SLinus Torvalds 
3895cdcdbe0bSLuiz Capitulino 	list_for_each_safe(q, n, &pktgen_threads) {
3896cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3897ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
3898ee74baa7SDavid S. Miller 		kfree(t);
38991da177e4SLinus Torvalds 	}
39001da177e4SLinus Torvalds 
39011da177e4SLinus Torvalds 	/* Un-register us from receiving netdevice events */
39021da177e4SLinus Torvalds 	unregister_netdevice_notifier(&pktgen_notifier_block);
39031da177e4SLinus Torvalds 
39041da177e4SLinus Torvalds 	/* Clean up proc file system */
3905d50a6b56SStephen Hemminger 	remove_proc_entry(PGCTRL, pg_proc_dir);
3906457c4cbcSEric W. Biederman 	proc_net_remove(&init_net, PG_PROC_DIR);
39071da177e4SLinus Torvalds }
39081da177e4SLinus Torvalds 
39091da177e4SLinus Torvalds module_init(pg_init);
39101da177e4SLinus Torvalds module_exit(pg_cleanup);
39111da177e4SLinus Torvalds 
3912c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
39131da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
39141da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3915c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
39161da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3917c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
39181da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3919c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
39201da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3921c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
39221da177e4SLinus Torvalds module_param(debug, int, 0);
3923c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3924