xref: /openbmc/linux/net/core/pktgen.c (revision 07a0f0f07a68014c92c752a5598102372bddf46e)
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  */
1181da177e4SLinus Torvalds #include <linux/sys.h>
1191da177e4SLinus Torvalds #include <linux/types.h>
1201da177e4SLinus Torvalds #include <linux/module.h>
1211da177e4SLinus Torvalds #include <linux/moduleparam.h>
1221da177e4SLinus Torvalds #include <linux/kernel.h>
123222fa076SLuiz Capitulino #include <linux/mutex.h>
1241da177e4SLinus Torvalds #include <linux/sched.h>
1251da177e4SLinus Torvalds #include <linux/slab.h>
1261da177e4SLinus Torvalds #include <linux/vmalloc.h>
1271da177e4SLinus Torvalds #include <linux/unistd.h>
1281da177e4SLinus Torvalds #include <linux/string.h>
1291da177e4SLinus Torvalds #include <linux/ptrace.h>
1301da177e4SLinus Torvalds #include <linux/errno.h>
1311da177e4SLinus Torvalds #include <linux/ioport.h>
1321da177e4SLinus Torvalds #include <linux/interrupt.h>
1334fc268d2SRandy Dunlap #include <linux/capability.h>
1342bc481cfSStephen Hemminger #include <linux/hrtimer.h>
13509fe3ef4SAndrew Morton #include <linux/freezer.h>
1361da177e4SLinus Torvalds #include <linux/delay.h>
1371da177e4SLinus Torvalds #include <linux/timer.h>
138cdcdbe0bSLuiz Capitulino #include <linux/list.h>
1391da177e4SLinus Torvalds #include <linux/init.h>
1401da177e4SLinus Torvalds #include <linux/skbuff.h>
1411da177e4SLinus Torvalds #include <linux/netdevice.h>
1421da177e4SLinus Torvalds #include <linux/inet.h>
1431da177e4SLinus Torvalds #include <linux/inetdevice.h>
1441da177e4SLinus Torvalds #include <linux/rtnetlink.h>
1451da177e4SLinus Torvalds #include <linux/if_arp.h>
14634954ddcSFrancesco Fondelli #include <linux/if_vlan.h>
1471da177e4SLinus Torvalds #include <linux/in.h>
1481da177e4SLinus Torvalds #include <linux/ip.h>
1491da177e4SLinus Torvalds #include <linux/ipv6.h>
1501da177e4SLinus Torvalds #include <linux/udp.h>
1511da177e4SLinus Torvalds #include <linux/proc_fs.h>
152d50a6b56SStephen Hemminger #include <linux/seq_file.h>
1531da177e4SLinus Torvalds #include <linux/wait.h>
154f404e9a6SKris Katterjohn #include <linux/etherdevice.h>
155ee74baa7SDavid S. Miller #include <linux/kthread.h>
156457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1571da177e4SLinus Torvalds #include <net/checksum.h>
1581da177e4SLinus Torvalds #include <net/ipv6.h>
1591da177e4SLinus Torvalds #include <net/addrconf.h>
160a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
161a553e4a6SJamal Hadi Salim #include <net/xfrm.h>
162a553e4a6SJamal Hadi Salim #endif
1631da177e4SLinus Torvalds #include <asm/byteorder.h>
1641da177e4SLinus Torvalds #include <linux/rcupdate.h>
1651977f032SJiri Slaby #include <linux/bitops.h>
16663adc6fbSStephen Hemminger #include <linux/io.h>
16763adc6fbSStephen Hemminger #include <linux/timex.h>
16863adc6fbSStephen Hemminger #include <linux/uaccess.h>
1691da177e4SLinus Torvalds #include <asm/dma.h>
1701da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1711da177e4SLinus Torvalds 
172e99b99b4SRobert Olsson #define VERSION 	"2.73"
1731da177e4SLinus Torvalds #define IP_NAME_SZ 32
174ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
175d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds /* Device flag bits */
1781da177e4SLinus Torvalds #define F_IPSRC_RND   (1<<0)	/* IP-Src Random  */
1791da177e4SLinus Torvalds #define F_IPDST_RND   (1<<1)	/* IP-Dst Random  */
1801da177e4SLinus Torvalds #define F_UDPSRC_RND  (1<<2)	/* UDP-Src Random */
1811da177e4SLinus Torvalds #define F_UDPDST_RND  (1<<3)	/* UDP-Dst Random */
1821da177e4SLinus Torvalds #define F_MACSRC_RND  (1<<4)	/* MAC-Src Random */
1831da177e4SLinus Torvalds #define F_MACDST_RND  (1<<5)	/* MAC-Dst Random */
1841da177e4SLinus Torvalds #define F_TXSIZE_RND  (1<<6)	/* Transmit size is random */
1851da177e4SLinus Torvalds #define F_IPV6        (1<<7)	/* Interface in IPV6 Mode */
186ca6549afSSteven Whitehouse #define F_MPLS_RND    (1<<8)	/* Random MPLS labels */
18734954ddcSFrancesco Fondelli #define F_VID_RND     (1<<9)	/* Random VLAN ID */
18834954ddcSFrancesco Fondelli #define F_SVID_RND    (1<<10)	/* Random SVLAN ID */
189007a531bSJamal Hadi Salim #define F_FLOW_SEQ    (1<<11)	/* Sequential flows */
190a553e4a6SJamal Hadi Salim #define F_IPSEC_ON    (1<<12)	/* ipsec on for flows */
19145b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13)	/* queue map Random */
192e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14)	/* queue map mirrors smp_processor_id() */
193e99b99b4SRobert Olsson #define F_NODE          (1<<15)	/* Node memory alloc*/
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /* Thread control flag bits */
1966b80d6a6SStephen Hemminger #define T_STOP        (1<<0)	/* Stop run */
1976b80d6a6SStephen Hemminger #define T_RUN         (1<<1)	/* Start run */
1986b80d6a6SStephen Hemminger #define T_REMDEVALL   (1<<2)	/* Remove all devs */
1996b80d6a6SStephen Hemminger #define T_REMDEV      (1<<3)	/* Remove one dev */
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /* If lock -- can be removed after some work */
2021da177e4SLinus Torvalds #define   if_lock(t)           spin_lock(&(t->if_lock));
2031da177e4SLinus Torvalds #define   if_unlock(t)           spin_unlock(&(t->if_lock));
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2061da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
207d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
208d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
20963adc6fbSStephen Hemminger static struct proc_dir_entry *pg_proc_dir;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds #define MAX_CFLOWS  65536
2121da177e4SLinus Torvalds 
21334954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
21434954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
21534954ddcSFrancesco Fondelli 
216222f1806SLuiz Capitulino struct flow_state {
217252e3346SAl Viro 	__be32 cur_daddr;
2181da177e4SLinus Torvalds 	int count;
219a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
220a553e4a6SJamal Hadi Salim 	struct xfrm_state *x;
221a553e4a6SJamal Hadi Salim #endif
222007a531bSJamal Hadi Salim 	__u32 flags;
2231da177e4SLinus Torvalds };
2241da177e4SLinus Torvalds 
225007a531bSJamal Hadi Salim /* flow flag bits */
226007a531bSJamal Hadi Salim #define F_INIT   (1<<0)		/* flow has been initialized */
227007a531bSJamal Hadi Salim 
2281da177e4SLinus Torvalds struct pktgen_dev {
2291da177e4SLinus Torvalds 	/*
2301da177e4SLinus Torvalds 	 * Try to keep frequent/infrequent used vars. separated.
2311da177e4SLinus Torvalds 	 */
23239df232fSStephen Hemminger 	struct proc_dir_entry *entry;	/* proc file */
2331da177e4SLinus Torvalds 	struct pktgen_thread *pg_thread;/* the owner */
23463adc6fbSStephen Hemminger 	struct list_head list;		/* chaining in the thread's run-queue */
2351da177e4SLinus Torvalds 
23663adc6fbSStephen Hemminger 	int running;		/* if false, the test will stop */
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	/* If min != max, then we will either do a linear iteration, or
2391da177e4SLinus Torvalds 	 * we will do a random selection from within the range.
2401da177e4SLinus Torvalds 	 */
2411da177e4SLinus Torvalds 	__u32 flags;
24295ed63f7SArthur Kepner 	int removal_mark;	/* non-zero => the device is marked for
24395ed63f7SArthur Kepner 				 * removal by worker thread */
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	int min_pkt_size;	/* = ETH_ZLEN; */
2461da177e4SLinus Torvalds 	int max_pkt_size;	/* = ETH_ZLEN; */
24716dab72fSJamal Hadi Salim 	int pkt_overhead;	/* overhead for MPLS, VLANs, IPSEC etc */
2481da177e4SLinus Torvalds 	int nfrags;
249fd29cf72SStephen Hemminger 	u64 delay;		/* nano-seconds */
250fd29cf72SStephen Hemminger 
2511da177e4SLinus Torvalds 	__u64 count;		/* Default No packets to send */
2521da177e4SLinus Torvalds 	__u64 sofar;		/* How many pkts we've sent so far */
2531da177e4SLinus Torvalds 	__u64 tx_bytes;		/* How many bytes we've transmitted */
254f466dba1SJohn Fastabend 	__u64 errors;		/* Errors when trying to transmit, */
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/* runtime counters relating to clone_skb */
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	__u64 allocated_skbs;
2591da177e4SLinus Torvalds 	__u32 clone_count;
2601da177e4SLinus Torvalds 	int last_ok;		/* Was last skb sent?
26163adc6fbSStephen Hemminger 				 * Or a failed transmit of some sort?
26263adc6fbSStephen Hemminger 				 * This will keep sequence numbers in order
2631da177e4SLinus Torvalds 				 */
264fd29cf72SStephen Hemminger 	ktime_t next_tx;
265fd29cf72SStephen Hemminger 	ktime_t started_at;
266fd29cf72SStephen Hemminger 	ktime_t stopped_at;
267fd29cf72SStephen Hemminger 	u64	idle_acc;	/* nano-seconds */
268fd29cf72SStephen Hemminger 
2691da177e4SLinus Torvalds 	__u32 seq_num;
2701da177e4SLinus Torvalds 
27163adc6fbSStephen Hemminger 	int clone_skb;		/*
27263adc6fbSStephen Hemminger 				 * Use multiple SKBs during packet gen.
27363adc6fbSStephen Hemminger 				 * If this number is greater than 1, then
27463adc6fbSStephen Hemminger 				 * that many copies of the same packet will be
27563adc6fbSStephen Hemminger 				 * sent before a new packet is allocated.
27663adc6fbSStephen Hemminger 				 * If you want to send 1024 identical packets
27763adc6fbSStephen Hemminger 				 * before creating a new packet,
27863adc6fbSStephen Hemminger 				 * set clone_skb to 1024.
2791da177e4SLinus Torvalds 				 */
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	char dst_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2821da177e4SLinus Torvalds 	char dst_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2831da177e4SLinus Torvalds 	char src_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2841da177e4SLinus Torvalds 	char src_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	struct in6_addr in6_saddr;
2871da177e4SLinus Torvalds 	struct in6_addr in6_daddr;
2881da177e4SLinus Torvalds 	struct in6_addr cur_in6_daddr;
2891da177e4SLinus Torvalds 	struct in6_addr cur_in6_saddr;
2901da177e4SLinus Torvalds 	/* For ranges */
2911da177e4SLinus Torvalds 	struct in6_addr min_in6_daddr;
2921da177e4SLinus Torvalds 	struct in6_addr max_in6_daddr;
2931da177e4SLinus Torvalds 	struct in6_addr min_in6_saddr;
2941da177e4SLinus Torvalds 	struct in6_addr max_in6_saddr;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* If we're doing ranges, random or incremental, then this
2971da177e4SLinus Torvalds 	 * defines the min/max for those ranges.
2981da177e4SLinus Torvalds 	 */
299252e3346SAl Viro 	__be32 saddr_min;	/* inclusive, source IP address */
300252e3346SAl Viro 	__be32 saddr_max;	/* exclusive, source IP address */
301252e3346SAl Viro 	__be32 daddr_min;	/* inclusive, dest IP address */
302252e3346SAl Viro 	__be32 daddr_max;	/* exclusive, dest IP address */
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	__u16 udp_src_min;	/* inclusive, source UDP port */
3051da177e4SLinus Torvalds 	__u16 udp_src_max;	/* exclusive, source UDP port */
3061da177e4SLinus Torvalds 	__u16 udp_dst_min;	/* inclusive, dest UDP port */
3071da177e4SLinus Torvalds 	__u16 udp_dst_max;	/* exclusive, dest UDP port */
3081da177e4SLinus Torvalds 
3091ca7768cSFrancesco Fondelli 	/* DSCP + ECN */
31063adc6fbSStephen Hemminger 	__u8 tos;            /* six MSB of (former) IPv4 TOS
31163adc6fbSStephen Hemminger 				are for dscp codepoint */
31263adc6fbSStephen Hemminger 	__u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6
31363adc6fbSStephen Hemminger 				(see RFC 3260, sec. 4) */
3141ca7768cSFrancesco Fondelli 
315ca6549afSSteven Whitehouse 	/* MPLS */
316ca6549afSSteven Whitehouse 	unsigned nr_labels;	/* Depth of stack, 0 = no MPLS */
317ca6549afSSteven Whitehouse 	__be32 labels[MAX_MPLS_LABELS];
318ca6549afSSteven Whitehouse 
31934954ddcSFrancesco Fondelli 	/* VLAN/SVLAN (802.1Q/Q-in-Q) */
32034954ddcSFrancesco Fondelli 	__u8  vlan_p;
32134954ddcSFrancesco Fondelli 	__u8  vlan_cfi;
32234954ddcSFrancesco Fondelli 	__u16 vlan_id;  /* 0xffff means no vlan tag */
32334954ddcSFrancesco Fondelli 
32434954ddcSFrancesco Fondelli 	__u8  svlan_p;
32534954ddcSFrancesco Fondelli 	__u8  svlan_cfi;
32634954ddcSFrancesco Fondelli 	__u16 svlan_id; /* 0xffff means no svlan tag */
32734954ddcSFrancesco Fondelli 
3281da177e4SLinus Torvalds 	__u32 src_mac_count;	/* How many MACs to iterate through */
3291da177e4SLinus Torvalds 	__u32 dst_mac_count;	/* How many MACs to iterate through */
3301da177e4SLinus Torvalds 
331f404e9a6SKris Katterjohn 	unsigned char dst_mac[ETH_ALEN];
332f404e9a6SKris Katterjohn 	unsigned char src_mac[ETH_ALEN];
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	__u32 cur_dst_mac_offset;
3351da177e4SLinus Torvalds 	__u32 cur_src_mac_offset;
336252e3346SAl Viro 	__be32 cur_saddr;
337252e3346SAl Viro 	__be32 cur_daddr;
33866ed1e5eSEric Dumazet 	__u16 ip_id;
3391da177e4SLinus Torvalds 	__u16 cur_udp_dst;
3401da177e4SLinus Torvalds 	__u16 cur_udp_src;
34145b270f8SRobert Olsson 	__u16 cur_queue_map;
3421da177e4SLinus Torvalds 	__u32 cur_pkt_size;
343baac8564SEric Dumazet 	__u32 last_pkt_size;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	__u8 hh[14];
3461da177e4SLinus Torvalds 	/* = {
3471da177e4SLinus Torvalds 	   0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	   We fill in SRC address later
3501da177e4SLinus Torvalds 	   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3511da177e4SLinus Torvalds 	   0x08, 0x00
3521da177e4SLinus Torvalds 	   };
3531da177e4SLinus Torvalds 	 */
3541da177e4SLinus Torvalds 	__u16 pad;		/* pad out the hh struct to an even 16 bytes */
3551da177e4SLinus Torvalds 
35663adc6fbSStephen Hemminger 	struct sk_buff *skb;	/* skb we are to transmit next, used for when we
3571da177e4SLinus Torvalds 				 * are transmitting the same one multiple times
3581da177e4SLinus Torvalds 				 */
35963adc6fbSStephen Hemminger 	struct net_device *odev; /* The out-going device.
36063adc6fbSStephen Hemminger 				  * Note that the device should have it's
36163adc6fbSStephen Hemminger 				  * pg_info pointer pointing back to this
36263adc6fbSStephen Hemminger 				  * device.
36363adc6fbSStephen Hemminger 				  * Set when the user specifies the out-going
36463adc6fbSStephen Hemminger 				  * device name (not when the inject is
3651da177e4SLinus Torvalds 				  * started as it used to do.)
3661da177e4SLinus Torvalds 				  */
367593f63b0SEric Dumazet 	char odevname[32];
3681da177e4SLinus Torvalds 	struct flow_state *flows;
3691da177e4SLinus Torvalds 	unsigned cflows;	/* Concurrent flows (config) */
3701da177e4SLinus Torvalds 	unsigned lflow;		/* Flow length  (config) */
3711da177e4SLinus Torvalds 	unsigned nflows;	/* accumulated flows (stats) */
372007a531bSJamal Hadi Salim 	unsigned curfl;		/* current sequenced flow (state)*/
37345b270f8SRobert Olsson 
37445b270f8SRobert Olsson 	u16 queue_map_min;
37545b270f8SRobert Olsson 	u16 queue_map_max;
376e99b99b4SRobert Olsson 	int node;               /* Memory node */
37745b270f8SRobert Olsson 
378a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
379a553e4a6SJamal Hadi Salim 	__u8	ipsmode;		/* IPSEC mode (config) */
380a553e4a6SJamal Hadi Salim 	__u8	ipsproto;		/* IPSEC type (config) */
381a553e4a6SJamal Hadi Salim #endif
38239df232fSStephen Hemminger 	char result[512];
3831da177e4SLinus Torvalds };
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds struct pktgen_hdr {
386252e3346SAl Viro 	__be32 pgh_magic;
387252e3346SAl Viro 	__be32 seq_num;
388252e3346SAl Viro 	__be32 tv_sec;
389252e3346SAl Viro 	__be32 tv_usec;
3901da177e4SLinus Torvalds };
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds struct pktgen_thread {
39363adc6fbSStephen Hemminger 	spinlock_t if_lock;		/* for list of devices */
394c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
395cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
396ee74baa7SDavid S. Miller 	struct task_struct *tsk;
3971da177e4SLinus Torvalds 	char result[512];
3981da177e4SLinus Torvalds 
39963adc6fbSStephen Hemminger 	/* Field for thread to receive "posted" events terminate,
40063adc6fbSStephen Hemminger 	   stop ifs etc. */
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	u32 control;
4031da177e4SLinus Torvalds 	int cpu;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	wait_queue_head_t queue;
406d3ede327SDenis V. Lunev 	struct completion start_done;
4071da177e4SLinus Torvalds };
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds #define REMOVE 1
4101da177e4SLinus Torvalds #define FIND   0
4111da177e4SLinus Torvalds 
412fd29cf72SStephen Hemminger static inline ktime_t ktime_now(void)
4131da177e4SLinus Torvalds {
414fd29cf72SStephen Hemminger 	struct timespec ts;
415fd29cf72SStephen Hemminger 	ktime_get_ts(&ts);
416fd29cf72SStephen Hemminger 
417fd29cf72SStephen Hemminger 	return timespec_to_ktime(ts);
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
420fd29cf72SStephen Hemminger /* This works even if 32 bit because of careful byte order choice */
421fd29cf72SStephen Hemminger static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
4221da177e4SLinus Torvalds {
423fd29cf72SStephen Hemminger 	return cmp1.tv64 < cmp2.tv64;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
426c3d2f52dSStephen Hemminger static const char version[] =
427c3d2f52dSStephen Hemminger 	"pktgen " VERSION ": Packet Generator for packet performance testing.\n";
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4301da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
431222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
4323e984840SEric Dumazet 					  const char *ifname, bool exact);
4331da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
4341da177e4SLinus Torvalds static void pktgen_run_all_threads(void);
435eb37b41cSJesse Brandeburg static void pktgen_reset_all_threads(void);
4361da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void);
4373bda06a3SStephen Hemminger 
4381da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4391da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
44039df232fSStephen Hemminger 
4411da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]);
4421da177e4SLinus Torvalds static unsigned int fmt_ip6(char *s, const char ip[16]);
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds /* Module parameters, defaults. */
44565c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
44665c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
44765c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
44865c5b786SStephen Hemminger static int debug  __read_mostly;
4491da177e4SLinus Torvalds 
450222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
451cdcdbe0bSLuiz Capitulino static LIST_HEAD(pktgen_threads);
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4541da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4551da177e4SLinus Torvalds };
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /*
4581da177e4SLinus Torvalds  * /proc handling functions
4591da177e4SLinus Torvalds  *
4601da177e4SLinus Torvalds  */
4611da177e4SLinus Torvalds 
462d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
463d50a6b56SStephen Hemminger {
464c3d2f52dSStephen Hemminger 	seq_puts(seq, version);
465d50a6b56SStephen Hemminger 	return 0;
466d50a6b56SStephen Hemminger }
4671da177e4SLinus Torvalds 
468d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user *buf,
4691da177e4SLinus Torvalds 			    size_t count, loff_t *ppos)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	int err = 0;
472d50a6b56SStephen Hemminger 	char data[128];
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN)) {
4751da177e4SLinus Torvalds 		err = -EPERM;
4761da177e4SLinus Torvalds 		goto out;
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 
479d50a6b56SStephen Hemminger 	if (count > sizeof(data))
480d50a6b56SStephen Hemminger 		count = sizeof(data);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count)) {
4831da177e4SLinus Torvalds 		err = -EFAULT;
484d50a6b56SStephen Hemminger 		goto out;
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 	data[count - 1] = 0;	/* Make string */
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
4891da177e4SLinus Torvalds 		pktgen_stop_all_threads_ifs();
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
4921da177e4SLinus Torvalds 		pktgen_run_all_threads();
4931da177e4SLinus Torvalds 
494eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
495eb37b41cSJesse Brandeburg 		pktgen_reset_all_threads();
496eb37b41cSJesse Brandeburg 
4971da177e4SLinus Torvalds 	else
49825a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	err = count;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds out:
5031da177e4SLinus Torvalds 	return err;
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
506d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
5071da177e4SLinus Torvalds {
508d50a6b56SStephen Hemminger 	return single_open(file, pgctrl_show, PDE(inode)->data);
509d50a6b56SStephen Hemminger }
510d50a6b56SStephen Hemminger 
5119a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
512d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
513d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
514d50a6b56SStephen Hemminger 	.read    = seq_read,
515d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
516d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
517d50a6b56SStephen Hemminger 	.release = single_release,
518d50a6b56SStephen Hemminger };
519d50a6b56SStephen Hemminger 
520d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
521d50a6b56SStephen Hemminger {
522648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
523fd29cf72SStephen Hemminger 	ktime_t stopped;
524fd29cf72SStephen Hemminger 	u64 idle;
5251da177e4SLinus Torvalds 
526222f1806SLuiz Capitulino 	seq_printf(seq,
527222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
528222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
529222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5301da177e4SLinus Torvalds 
531222f1806SLuiz Capitulino 	seq_printf(seq,
532fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
533fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
534593f63b0SEric Dumazet 		   pkt_dev->clone_skb, pkt_dev->odevname);
5351da177e4SLinus Torvalds 
536222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
537222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5381da177e4SLinus Torvalds 
53945b270f8SRobert Olsson 	seq_printf(seq,
54045b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
54145b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
54245b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
54345b270f8SRobert Olsson 
5441da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
5451da177e4SLinus Torvalds 		char b1[128], b2[128], b3[128];
5461da177e4SLinus Torvalds 		fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
5471da177e4SLinus Torvalds 		fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
5481da177e4SLinus Torvalds 		fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
549222f1806SLuiz Capitulino 		seq_printf(seq,
550222f1806SLuiz Capitulino 			   "     saddr: %s  min_saddr: %s  max_saddr: %s\n", b1,
551222f1806SLuiz Capitulino 			   b2, b3);
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 		fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
5541da177e4SLinus Torvalds 		fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
5551da177e4SLinus Torvalds 		fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
556222f1806SLuiz Capitulino 		seq_printf(seq,
557222f1806SLuiz Capitulino 			   "     daddr: %s  min_daddr: %s  max_daddr: %s\n", b1,
558222f1806SLuiz Capitulino 			   b2, b3);
5591da177e4SLinus Torvalds 
56063adc6fbSStephen Hemminger 	} else {
561222f1806SLuiz Capitulino 		seq_printf(seq,
56263adc6fbSStephen Hemminger 			   "     dst_min: %s  dst_max: %s\n",
56363adc6fbSStephen Hemminger 			   pkt_dev->dst_min, pkt_dev->dst_max);
56463adc6fbSStephen Hemminger 		seq_printf(seq,
56563adc6fbSStephen Hemminger 			   "        src_min: %s  src_max: %s\n",
56663adc6fbSStephen Hemminger 			   pkt_dev->src_min, pkt_dev->src_max);
56763adc6fbSStephen Hemminger 	}
5681da177e4SLinus Torvalds 
569d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5701da177e4SLinus Torvalds 
571e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
572e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
573e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5741da177e4SLinus Torvalds 
575d50a6b56SStephen Hemminger 	seq_printf(seq, "dst_mac: ");
576e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
5771da177e4SLinus Torvalds 
578222f1806SLuiz Capitulino 	seq_printf(seq,
57963adc6fbSStephen Hemminger 		   "     udp_src_min: %d  udp_src_max: %d"
58063adc6fbSStephen Hemminger 		   "  udp_dst_min: %d  udp_dst_max: %d\n",
581222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
582222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
5831da177e4SLinus Torvalds 
584222f1806SLuiz Capitulino 	seq_printf(seq,
585ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
5861da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
5871da177e4SLinus Torvalds 
588ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
589ca6549afSSteven Whitehouse 		unsigned i;
590ca6549afSSteven Whitehouse 		seq_printf(seq, "     mpls: ");
591ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
592ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
593ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
594ca6549afSSteven Whitehouse 	}
595ca6549afSSteven Whitehouse 
59663adc6fbSStephen Hemminger 	if (pkt_dev->vlan_id != 0xffff)
59734954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
59863adc6fbSStephen Hemminger 			   pkt_dev->vlan_id, pkt_dev->vlan_p,
59963adc6fbSStephen Hemminger 			   pkt_dev->vlan_cfi);
60034954ddcSFrancesco Fondelli 
60163adc6fbSStephen Hemminger 	if (pkt_dev->svlan_id != 0xffff)
60234954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
60363adc6fbSStephen Hemminger 			   pkt_dev->svlan_id, pkt_dev->svlan_p,
60463adc6fbSStephen Hemminger 			   pkt_dev->svlan_cfi);
60534954ddcSFrancesco Fondelli 
60663adc6fbSStephen Hemminger 	if (pkt_dev->tos)
6071ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
6081ca7768cSFrancesco Fondelli 
60963adc6fbSStephen Hemminger 	if (pkt_dev->traffic_class)
6101ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
6111ca7768cSFrancesco Fondelli 
612e99b99b4SRobert Olsson 	if (pkt_dev->node >= 0)
613e99b99b4SRobert Olsson 		seq_printf(seq, "     node: %d\n", pkt_dev->node);
614e99b99b4SRobert Olsson 
615ca6549afSSteven Whitehouse 	seq_printf(seq, "     Flags: ");
616ca6549afSSteven Whitehouse 
6171da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
618d50a6b56SStephen Hemminger 		seq_printf(seq, "IPV6  ");
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPSRC_RND)
621d50a6b56SStephen Hemminger 		seq_printf(seq, "IPSRC_RND  ");
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPDST_RND)
624d50a6b56SStephen Hemminger 		seq_printf(seq, "IPDST_RND  ");
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	if (pkt_dev->flags & F_TXSIZE_RND)
627d50a6b56SStephen Hemminger 		seq_printf(seq, "TXSIZE_RND  ");
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPSRC_RND)
630d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPSRC_RND  ");
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPDST_RND)
633d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPDST_RND  ");
6341da177e4SLinus Torvalds 
635ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND)
636ca6549afSSteven Whitehouse 		seq_printf(seq,  "MPLS_RND  ");
637ca6549afSSteven Whitehouse 
63845b270f8SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_RND)
63945b270f8SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_RND  ");
64045b270f8SRobert Olsson 
641e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
642e6fce5b9SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_CPU  ");
643e6fce5b9SRobert Olsson 
644007a531bSJamal Hadi Salim 	if (pkt_dev->cflows) {
645007a531bSJamal Hadi Salim 		if (pkt_dev->flags & F_FLOW_SEQ)
646007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_SEQ  "); /*in sequence flows*/
647007a531bSJamal Hadi Salim 		else
648007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_RND  ");
649007a531bSJamal Hadi Salim 	}
650007a531bSJamal Hadi Salim 
651a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
652a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON)
653a553e4a6SJamal Hadi Salim 		seq_printf(seq,  "IPSEC  ");
654a553e4a6SJamal Hadi Salim #endif
655a553e4a6SJamal Hadi Salim 
6561da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACSRC_RND)
657d50a6b56SStephen Hemminger 		seq_printf(seq, "MACSRC_RND  ");
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACDST_RND)
660d50a6b56SStephen Hemminger 		seq_printf(seq, "MACDST_RND  ");
6611da177e4SLinus Torvalds 
66234954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_VID_RND)
66334954ddcSFrancesco Fondelli 		seq_printf(seq, "VID_RND  ");
66434954ddcSFrancesco Fondelli 
66534954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_SVID_RND)
66634954ddcSFrancesco Fondelli 		seq_printf(seq, "SVID_RND  ");
66734954ddcSFrancesco Fondelli 
668e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE)
669e99b99b4SRobert Olsson 		seq_printf(seq, "NODE_ALLOC  ");
670e99b99b4SRobert Olsson 
671d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6721da177e4SLinus Torvalds 
673fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
674fd29cf72SStephen Hemminger 	stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
675fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
676fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
6771da177e4SLinus Torvalds 
678222f1806SLuiz Capitulino 	seq_printf(seq,
679fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
6801da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
681fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
682fd29cf72SStephen Hemminger 
683fd29cf72SStephen Hemminger 	seq_printf(seq,
684fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
685fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
686fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
687fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
6881da177e4SLinus Torvalds 
689222f1806SLuiz Capitulino 	seq_printf(seq,
690222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
691d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
692d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
6951da177e4SLinus Torvalds 		char b1[128], b2[128];
6961da177e4SLinus Torvalds 		fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
6971da177e4SLinus Torvalds 		fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
698d50a6b56SStephen Hemminger 		seq_printf(seq, "     cur_saddr: %s  cur_daddr: %s\n", b2, b1);
699222f1806SLuiz Capitulino 	} else
700d50a6b56SStephen Hemminger 		seq_printf(seq, "     cur_saddr: 0x%x  cur_daddr: 0x%x\n",
7011da177e4SLinus Torvalds 			   pkt_dev->cur_saddr, pkt_dev->cur_daddr);
7021da177e4SLinus Torvalds 
703d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
7041da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
7051da177e4SLinus Torvalds 
70645b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
70745b270f8SRobert Olsson 
708d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	if (pkt_dev->result[0])
711d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
7121da177e4SLinus Torvalds 	else
713d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: Idle\n");
7141da177e4SLinus Torvalds 
715d50a6b56SStephen Hemminger 	return 0;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
718ca6549afSSteven Whitehouse 
71963adc6fbSStephen Hemminger static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
72063adc6fbSStephen Hemminger 		     __u32 *num)
721ca6549afSSteven Whitehouse {
722ca6549afSSteven Whitehouse 	int i = 0;
723ca6549afSSteven Whitehouse 	*num = 0;
724ca6549afSSteven Whitehouse 
7251ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
726ca6549afSSteven Whitehouse 		char c;
727ca6549afSSteven Whitehouse 		*num <<= 4;
728ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
729ca6549afSSteven Whitehouse 			return -EFAULT;
730ca6549afSSteven Whitehouse 		if ((c >= '0') && (c <= '9'))
731ca6549afSSteven Whitehouse 			*num |= c - '0';
732ca6549afSSteven Whitehouse 		else if ((c >= 'a') && (c <= 'f'))
733ca6549afSSteven Whitehouse 			*num |= c - 'a' + 10;
734ca6549afSSteven Whitehouse 		else if ((c >= 'A') && (c <= 'F'))
735ca6549afSSteven Whitehouse 			*num |= c - 'A' + 10;
736ca6549afSSteven Whitehouse 		else
737ca6549afSSteven Whitehouse 			break;
738ca6549afSSteven Whitehouse 	}
739ca6549afSSteven Whitehouse 	return i;
740ca6549afSSteven Whitehouse }
741ca6549afSSteven Whitehouse 
742222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
743222f1806SLuiz Capitulino 			     unsigned int maxlen)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	int i;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7481da177e4SLinus Torvalds 		char c;
7491da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7501da177e4SLinus Torvalds 			return -EFAULT;
7511da177e4SLinus Torvalds 		switch (c) {
7521da177e4SLinus Torvalds 		case '\"':
7531da177e4SLinus Torvalds 		case '\n':
7541da177e4SLinus Torvalds 		case '\r':
7551da177e4SLinus Torvalds 		case '\t':
7561da177e4SLinus Torvalds 		case ' ':
7571da177e4SLinus Torvalds 		case '=':
7581da177e4SLinus Torvalds 			break;
7591da177e4SLinus Torvalds 		default:
7601da177e4SLinus Torvalds 			goto done;
7613ff50b79SStephen Hemminger 		}
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds done:
7641da177e4SLinus Torvalds 	return i;
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
767222f1806SLuiz Capitulino static unsigned long num_arg(const char __user * user_buffer,
768222f1806SLuiz Capitulino 			     unsigned long maxlen, unsigned long *num)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	int i = 0;
7711da177e4SLinus Torvalds 	*num = 0;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	for (; i < maxlen; i++) {
7741da177e4SLinus Torvalds 		char c;
7751da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7761da177e4SLinus Torvalds 			return -EFAULT;
7771da177e4SLinus Torvalds 		if ((c >= '0') && (c <= '9')) {
7781da177e4SLinus Torvalds 			*num *= 10;
7791da177e4SLinus Torvalds 			*num += c - '0';
7801da177e4SLinus Torvalds 		} else
7811da177e4SLinus Torvalds 			break;
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 	return i;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
7871da177e4SLinus Torvalds {
7881da177e4SLinus Torvalds 	int i = 0;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	for (; i < maxlen; i++) {
7911da177e4SLinus Torvalds 		char c;
7921da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7931da177e4SLinus Torvalds 			return -EFAULT;
7941da177e4SLinus Torvalds 		switch (c) {
7951da177e4SLinus Torvalds 		case '\"':
7961da177e4SLinus Torvalds 		case '\n':
7971da177e4SLinus Torvalds 		case '\r':
7981da177e4SLinus Torvalds 		case '\t':
7991da177e4SLinus Torvalds 		case ' ':
8001da177e4SLinus Torvalds 			goto done_str;
8011da177e4SLinus Torvalds 			break;
8021da177e4SLinus Torvalds 		default:
8031da177e4SLinus Torvalds 			break;
8043ff50b79SStephen Hemminger 		}
8051da177e4SLinus Torvalds 	}
8061da177e4SLinus Torvalds done_str:
8071da177e4SLinus Torvalds 	return i;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
810ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
811ca6549afSSteven Whitehouse {
812ca6549afSSteven Whitehouse 	unsigned n = 0;
813ca6549afSSteven Whitehouse 	char c;
814ca6549afSSteven Whitehouse 	ssize_t i = 0;
815ca6549afSSteven Whitehouse 	int len;
816ca6549afSSteven Whitehouse 
817ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = 0;
818ca6549afSSteven Whitehouse 	do {
819ca6549afSSteven Whitehouse 		__u32 tmp;
8201ca7768cSFrancesco Fondelli 		len = hex32_arg(&buffer[i], 8, &tmp);
821ca6549afSSteven Whitehouse 		if (len <= 0)
822ca6549afSSteven Whitehouse 			return len;
823ca6549afSSteven Whitehouse 		pkt_dev->labels[n] = htonl(tmp);
824ca6549afSSteven Whitehouse 		if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
825ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
826ca6549afSSteven Whitehouse 		i += len;
827ca6549afSSteven Whitehouse 		if (get_user(c, &buffer[i]))
828ca6549afSSteven Whitehouse 			return -EFAULT;
829ca6549afSSteven Whitehouse 		i++;
830ca6549afSSteven Whitehouse 		n++;
831ca6549afSSteven Whitehouse 		if (n >= MAX_MPLS_LABELS)
832ca6549afSSteven Whitehouse 			return -E2BIG;
833ca6549afSSteven Whitehouse 	} while (c == ',');
834ca6549afSSteven Whitehouse 
835ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = n;
836ca6549afSSteven Whitehouse 	return i;
837ca6549afSSteven Whitehouse }
838ca6549afSSteven Whitehouse 
839222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file,
840222f1806SLuiz Capitulino 			       const char __user * user_buffer, size_t count,
841222f1806SLuiz Capitulino 			       loff_t * offset)
8421da177e4SLinus Torvalds {
843d50a6b56SStephen Hemminger 	struct seq_file *seq = (struct seq_file *)file->private_data;
844d50a6b56SStephen Hemminger 	struct pktgen_dev *pkt_dev = seq->private;
8451da177e4SLinus Torvalds 	int i = 0, max, len;
8461da177e4SLinus Torvalds 	char name[16], valstr[32];
8471da177e4SLinus Torvalds 	unsigned long value = 0;
8481da177e4SLinus Torvalds 	char *pg_result = NULL;
8491da177e4SLinus Torvalds 	int tmp = 0;
8501da177e4SLinus Torvalds 	char buf[128];
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	pg_result = &(pkt_dev->result[0]);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	if (count < 1) {
85525a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: wrong command format\n");
8561da177e4SLinus Torvalds 		return -EINVAL;
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	max = count - i;
8601da177e4SLinus Torvalds 	tmp = count_trail_chars(&user_buffer[i], max);
8611da177e4SLinus Torvalds 	if (tmp < 0) {
86225a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: illegal format\n");
8631da177e4SLinus Torvalds 		return tmp;
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 	i += tmp;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	/* Read variable name */
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
87063adc6fbSStephen Hemminger 	if (len < 0)
871222f1806SLuiz Capitulino 		return len;
87263adc6fbSStephen Hemminger 
8731da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
8741da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
8751da177e4SLinus Torvalds 		return -EFAULT;
8761da177e4SLinus Torvalds 	i += len;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	max = count - i;
8791da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
8801da177e4SLinus Torvalds 	if (len < 0)
8811da177e4SLinus Torvalds 		return len;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	i += len;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	if (debug) {
8861da177e4SLinus Torvalds 		char tb[count + 1];
8871da177e4SLinus Torvalds 		if (copy_from_user(tb, user_buffer, count))
8881da177e4SLinus Torvalds 			return -EFAULT;
8891da177e4SLinus Torvalds 		tb[count] = 0;
89025a8b254SDavid S. Miller 		printk(KERN_DEBUG "pktgen: %s,%lu  buffer -:%s:-\n", name,
891d50a6b56SStephen Hemminger 		       (unsigned long)count, tb);
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	if (!strcmp(name, "min_pkt_size")) {
8951da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
89663adc6fbSStephen Hemminger 		if (len < 0)
897222f1806SLuiz Capitulino 			return len;
89863adc6fbSStephen Hemminger 
8991da177e4SLinus Torvalds 		i += len;
9001da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9011da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9021da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9031da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9041da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9051da177e4SLinus Torvalds 		}
906222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: min_pkt_size=%u",
907222f1806SLuiz Capitulino 			pkt_dev->min_pkt_size);
9081da177e4SLinus Torvalds 		return count;
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	if (!strcmp(name, "max_pkt_size")) {
9121da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
91363adc6fbSStephen Hemminger 		if (len < 0)
914222f1806SLuiz Capitulino 			return len;
91563adc6fbSStephen Hemminger 
9161da177e4SLinus Torvalds 		i += len;
9171da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9181da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9191da177e4SLinus Torvalds 		if (value != pkt_dev->max_pkt_size) {
9201da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9211da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9221da177e4SLinus Torvalds 		}
923222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: max_pkt_size=%u",
924222f1806SLuiz Capitulino 			pkt_dev->max_pkt_size);
9251da177e4SLinus Torvalds 		return count;
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	/* Shortcut for min = max */
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	if (!strcmp(name, "pkt_size")) {
9311da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
93263adc6fbSStephen Hemminger 		if (len < 0)
933222f1806SLuiz Capitulino 			return len;
93463adc6fbSStephen Hemminger 
9351da177e4SLinus Torvalds 		i += len;
9361da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9371da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9381da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9391da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9401da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9411da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9421da177e4SLinus Torvalds 		}
9431da177e4SLinus Torvalds 		sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
9441da177e4SLinus Torvalds 		return count;
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	if (!strcmp(name, "debug")) {
9481da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
94963adc6fbSStephen Hemminger 		if (len < 0)
950222f1806SLuiz Capitulino 			return len;
95163adc6fbSStephen Hemminger 
9521da177e4SLinus Torvalds 		i += len;
9531da177e4SLinus Torvalds 		debug = value;
9541da177e4SLinus Torvalds 		sprintf(pg_result, "OK: debug=%u", debug);
9551da177e4SLinus Torvalds 		return count;
9561da177e4SLinus Torvalds 	}
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	if (!strcmp(name, "frags")) {
9591da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
96063adc6fbSStephen Hemminger 		if (len < 0)
961222f1806SLuiz Capitulino 			return len;
96263adc6fbSStephen Hemminger 
9631da177e4SLinus Torvalds 		i += len;
9641da177e4SLinus Torvalds 		pkt_dev->nfrags = value;
9651da177e4SLinus Torvalds 		sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
9661da177e4SLinus Torvalds 		return count;
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds 	if (!strcmp(name, "delay")) {
9691da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
97063adc6fbSStephen Hemminger 		if (len < 0)
971222f1806SLuiz Capitulino 			return len;
97263adc6fbSStephen Hemminger 
9731da177e4SLinus Torvalds 		i += len;
974fd29cf72SStephen Hemminger 		if (value == 0x7FFFFFFF)
975fd29cf72SStephen Hemminger 			pkt_dev->delay = ULLONG_MAX;
976fd29cf72SStephen Hemminger 		else
9779240d715SEric Dumazet 			pkt_dev->delay = (u64)value;
978fd29cf72SStephen Hemminger 
979fd29cf72SStephen Hemminger 		sprintf(pg_result, "OK: delay=%llu",
980fd29cf72SStephen Hemminger 			(unsigned long long) pkt_dev->delay);
9811da177e4SLinus Torvalds 		return count;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_min")) {
9841da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
98563adc6fbSStephen Hemminger 		if (len < 0)
986222f1806SLuiz Capitulino 			return len;
98763adc6fbSStephen Hemminger 
9881da177e4SLinus Torvalds 		i += len;
9891da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_min) {
9901da177e4SLinus Torvalds 			pkt_dev->udp_src_min = value;
9911da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
9921da177e4SLinus Torvalds 		}
9931da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
9941da177e4SLinus Torvalds 		return count;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_min")) {
9971da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
99863adc6fbSStephen Hemminger 		if (len < 0)
999222f1806SLuiz Capitulino 			return len;
100063adc6fbSStephen Hemminger 
10011da177e4SLinus Torvalds 		i += len;
10021da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_min) {
10031da177e4SLinus Torvalds 			pkt_dev->udp_dst_min = value;
10041da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10051da177e4SLinus Torvalds 		}
10061da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
10071da177e4SLinus Torvalds 		return count;
10081da177e4SLinus Torvalds 	}
10091da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_max")) {
10101da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
101163adc6fbSStephen Hemminger 		if (len < 0)
1012222f1806SLuiz Capitulino 			return len;
101363adc6fbSStephen Hemminger 
10141da177e4SLinus Torvalds 		i += len;
10151da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_max) {
10161da177e4SLinus Torvalds 			pkt_dev->udp_src_max = value;
10171da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
10181da177e4SLinus Torvalds 		}
10191da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
10201da177e4SLinus Torvalds 		return count;
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_max")) {
10231da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
102463adc6fbSStephen Hemminger 		if (len < 0)
1025222f1806SLuiz Capitulino 			return len;
102663adc6fbSStephen Hemminger 
10271da177e4SLinus Torvalds 		i += len;
10281da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_max) {
10291da177e4SLinus Torvalds 			pkt_dev->udp_dst_max = value;
10301da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10311da177e4SLinus Torvalds 		}
10321da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
10331da177e4SLinus Torvalds 		return count;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 	if (!strcmp(name, "clone_skb")) {
10361da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
103763adc6fbSStephen Hemminger 		if (len < 0)
1038222f1806SLuiz Capitulino 			return len;
103963adc6fbSStephen Hemminger 
10401da177e4SLinus Torvalds 		i += len;
10411da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
10441da177e4SLinus Torvalds 		return count;
10451da177e4SLinus Torvalds 	}
10461da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
10471da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
104863adc6fbSStephen Hemminger 		if (len < 0)
1049222f1806SLuiz Capitulino 			return len;
105063adc6fbSStephen Hemminger 
10511da177e4SLinus Torvalds 		i += len;
10521da177e4SLinus Torvalds 		pkt_dev->count = value;
10531da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
10541da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
10551da177e4SLinus Torvalds 		return count;
10561da177e4SLinus Torvalds 	}
10571da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
10581da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
105963adc6fbSStephen Hemminger 		if (len < 0)
1060222f1806SLuiz Capitulino 			return len;
106163adc6fbSStephen Hemminger 
10621da177e4SLinus Torvalds 		i += len;
10631da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
10641da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
10651da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
10661da177e4SLinus Torvalds 		}
1067222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1068222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
10691da177e4SLinus Torvalds 		return count;
10701da177e4SLinus Torvalds 	}
10711da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
10721da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
107363adc6fbSStephen Hemminger 		if (len < 0)
1074222f1806SLuiz Capitulino 			return len;
107563adc6fbSStephen Hemminger 
10761da177e4SLinus Torvalds 		i += len;
10771da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
10781da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
10791da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
10801da177e4SLinus Torvalds 		}
1081222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1082222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
10831da177e4SLinus Torvalds 		return count;
10841da177e4SLinus Torvalds 	}
1085e99b99b4SRobert Olsson 	if (!strcmp(name, "node")) {
1086e99b99b4SRobert Olsson 		len = num_arg(&user_buffer[i], 10, &value);
1087e99b99b4SRobert Olsson 		if (len < 0)
1088e99b99b4SRobert Olsson 			return len;
1089e99b99b4SRobert Olsson 
1090e99b99b4SRobert Olsson 		i += len;
1091e99b99b4SRobert Olsson 
1092e99b99b4SRobert Olsson 		if (node_possible(value)) {
1093e99b99b4SRobert Olsson 			pkt_dev->node = value;
1094e99b99b4SRobert Olsson 			sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1095e99b99b4SRobert Olsson 		}
1096e99b99b4SRobert Olsson 		else
1097e99b99b4SRobert Olsson 			sprintf(pg_result, "ERROR: node not possible");
1098e99b99b4SRobert Olsson 		return count;
1099e99b99b4SRobert Olsson 	}
11001da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
11011da177e4SLinus Torvalds 		char f[32];
11021da177e4SLinus Torvalds 		memset(f, 0, 32);
11031da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
110463adc6fbSStephen Hemminger 		if (len < 0)
1105222f1806SLuiz Capitulino 			return len;
110663adc6fbSStephen Hemminger 
11071da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
11081da177e4SLinus Torvalds 			return -EFAULT;
11091da177e4SLinus Torvalds 		i += len;
11101da177e4SLinus Torvalds 		if (strcmp(f, "IPSRC_RND") == 0)
11111da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPSRC_RND;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 		else if (strcmp(f, "!IPSRC_RND") == 0)
11141da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPSRC_RND;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 		else if (strcmp(f, "TXSIZE_RND") == 0)
11171da177e4SLinus Torvalds 			pkt_dev->flags |= F_TXSIZE_RND;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		else if (strcmp(f, "!TXSIZE_RND") == 0)
11201da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_TXSIZE_RND;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 		else if (strcmp(f, "IPDST_RND") == 0)
11231da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPDST_RND;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 		else if (strcmp(f, "!IPDST_RND") == 0)
11261da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPDST_RND;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 		else if (strcmp(f, "UDPSRC_RND") == 0)
11291da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPSRC_RND;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPSRC_RND") == 0)
11321da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPSRC_RND;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 		else if (strcmp(f, "UDPDST_RND") == 0)
11351da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPDST_RND;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPDST_RND") == 0)
11381da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPDST_RND;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 		else if (strcmp(f, "MACSRC_RND") == 0)
11411da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACSRC_RND;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 		else if (strcmp(f, "!MACSRC_RND") == 0)
11441da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACSRC_RND;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		else if (strcmp(f, "MACDST_RND") == 0)
11471da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACDST_RND;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 		else if (strcmp(f, "!MACDST_RND") == 0)
11501da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACDST_RND;
11511da177e4SLinus Torvalds 
1152ca6549afSSteven Whitehouse 		else if (strcmp(f, "MPLS_RND") == 0)
1153ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
1154ca6549afSSteven Whitehouse 
1155ca6549afSSteven Whitehouse 		else if (strcmp(f, "!MPLS_RND") == 0)
1156ca6549afSSteven Whitehouse 			pkt_dev->flags &= ~F_MPLS_RND;
1157ca6549afSSteven Whitehouse 
115834954ddcSFrancesco Fondelli 		else if (strcmp(f, "VID_RND") == 0)
115934954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_VID_RND;
116034954ddcSFrancesco Fondelli 
116134954ddcSFrancesco Fondelli 		else if (strcmp(f, "!VID_RND") == 0)
116234954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_VID_RND;
116334954ddcSFrancesco Fondelli 
116434954ddcSFrancesco Fondelli 		else if (strcmp(f, "SVID_RND") == 0)
116534954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_SVID_RND;
116634954ddcSFrancesco Fondelli 
116734954ddcSFrancesco Fondelli 		else if (strcmp(f, "!SVID_RND") == 0)
116834954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_SVID_RND;
116934954ddcSFrancesco Fondelli 
1170007a531bSJamal Hadi Salim 		else if (strcmp(f, "FLOW_SEQ") == 0)
1171007a531bSJamal Hadi Salim 			pkt_dev->flags |= F_FLOW_SEQ;
1172007a531bSJamal Hadi Salim 
117345b270f8SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
117445b270f8SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_RND;
117545b270f8SRobert Olsson 
117645b270f8SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
117745b270f8SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1178e6fce5b9SRobert Olsson 
1179e6fce5b9SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1180e6fce5b9SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1181e6fce5b9SRobert Olsson 
1182e6fce5b9SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1183e6fce5b9SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1184a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
1185a553e4a6SJamal Hadi Salim 		else if (strcmp(f, "IPSEC") == 0)
1186a553e4a6SJamal Hadi Salim 			pkt_dev->flags |= F_IPSEC_ON;
1187a553e4a6SJamal Hadi Salim #endif
1188a553e4a6SJamal Hadi Salim 
11891ca7768cSFrancesco Fondelli 		else if (strcmp(f, "!IPV6") == 0)
11901ca7768cSFrancesco Fondelli 			pkt_dev->flags &= ~F_IPV6;
11911ca7768cSFrancesco Fondelli 
1192e99b99b4SRobert Olsson 		else if (strcmp(f, "NODE_ALLOC") == 0)
1193e99b99b4SRobert Olsson 			pkt_dev->flags |= F_NODE;
1194e99b99b4SRobert Olsson 
1195e99b99b4SRobert Olsson 		else if (strcmp(f, "!NODE_ALLOC") == 0)
1196e99b99b4SRobert Olsson 			pkt_dev->flags &= ~F_NODE;
1197e99b99b4SRobert Olsson 
11981da177e4SLinus Torvalds 		else {
1199222f1806SLuiz Capitulino 			sprintf(pg_result,
1200222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
12011da177e4SLinus Torvalds 				f,
12021ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1203e99b99b4SRobert Olsson 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
12041da177e4SLinus Torvalds 			return count;
12051da177e4SLinus Torvalds 		}
12061da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
12071da177e4SLinus Torvalds 		return count;
12081da177e4SLinus Torvalds 	}
12091da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
12101da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
121163adc6fbSStephen Hemminger 		if (len < 0)
1212222f1806SLuiz Capitulino 			return len;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12151da177e4SLinus Torvalds 			return -EFAULT;
12161da177e4SLinus Torvalds 		buf[len] = 0;
12171da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
12181da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
12191da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_min, buf, len);
12201da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
12211da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 		if (debug)
122425a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1225222f1806SLuiz Capitulino 			       pkt_dev->dst_min);
12261da177e4SLinus Torvalds 		i += len;
12271da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
12281da177e4SLinus Torvalds 		return count;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
12311da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
123263adc6fbSStephen Hemminger 		if (len < 0)
1233222f1806SLuiz Capitulino 			return len;
123463adc6fbSStephen Hemminger 
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12371da177e4SLinus Torvalds 			return -EFAULT;
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 		buf[len] = 0;
12401da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
12411da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
12421da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_max, buf, len);
12431da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
12441da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
12451da177e4SLinus Torvalds 		}
12461da177e4SLinus Torvalds 		if (debug)
124725a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1248222f1806SLuiz Capitulino 			       pkt_dev->dst_max);
12491da177e4SLinus Torvalds 		i += len;
12501da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
12511da177e4SLinus Torvalds 		return count;
12521da177e4SLinus Torvalds 	}
12531da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
12541da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1255222f1806SLuiz Capitulino 		if (len < 0)
1256222f1806SLuiz Capitulino 			return len;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12611da177e4SLinus Torvalds 			return -EFAULT;
12621da177e4SLinus Torvalds 		buf[len] = 0;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
12651da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 		if (debug)
127025a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 		i += len;
12731da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
12741da177e4SLinus Torvalds 		return count;
12751da177e4SLinus Torvalds 	}
12761da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
12771da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1278222f1806SLuiz Capitulino 		if (len < 0)
1279222f1806SLuiz Capitulino 			return len;
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12841da177e4SLinus Torvalds 			return -EFAULT;
12851da177e4SLinus Torvalds 		buf[len] = 0;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
12881da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
12891da177e4SLinus Torvalds 
1290222f1806SLuiz Capitulino 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1291222f1806SLuiz Capitulino 			       &pkt_dev->min_in6_daddr);
12921da177e4SLinus Torvalds 		if (debug)
129325a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 		i += len;
12961da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
12971da177e4SLinus Torvalds 		return count;
12981da177e4SLinus Torvalds 	}
12991da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
13001da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1301222f1806SLuiz Capitulino 		if (len < 0)
1302222f1806SLuiz Capitulino 			return len;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13071da177e4SLinus Torvalds 			return -EFAULT;
13081da177e4SLinus Torvalds 		buf[len] = 0;
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
13111da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 		if (debug)
131425a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 		i += len;
13171da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
13181da177e4SLinus Torvalds 		return count;
13191da177e4SLinus Torvalds 	}
13201da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
13211da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1322222f1806SLuiz Capitulino 		if (len < 0)
1323222f1806SLuiz Capitulino 			return len;
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13281da177e4SLinus Torvalds 			return -EFAULT;
13291da177e4SLinus Torvalds 		buf[len] = 0;
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
13321da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 		ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 		if (debug)
133725a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 		i += len;
13401da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
13411da177e4SLinus Torvalds 		return count;
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
13441da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
134563adc6fbSStephen Hemminger 		if (len < 0)
1346222f1806SLuiz Capitulino 			return len;
134763adc6fbSStephen Hemminger 
13481da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13491da177e4SLinus Torvalds 			return -EFAULT;
13501da177e4SLinus Torvalds 		buf[len] = 0;
13511da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
13521da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
13531da177e4SLinus Torvalds 			strncpy(pkt_dev->src_min, buf, len);
13541da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
13551da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
13561da177e4SLinus Torvalds 		}
13571da177e4SLinus Torvalds 		if (debug)
135825a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1359222f1806SLuiz Capitulino 			       pkt_dev->src_min);
13601da177e4SLinus Torvalds 		i += len;
13611da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
13621da177e4SLinus Torvalds 		return count;
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
13651da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
136663adc6fbSStephen Hemminger 		if (len < 0)
1367222f1806SLuiz Capitulino 			return len;
136863adc6fbSStephen Hemminger 
13691da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13701da177e4SLinus Torvalds 			return -EFAULT;
13711da177e4SLinus Torvalds 		buf[len] = 0;
13721da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
13731da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
13741da177e4SLinus Torvalds 			strncpy(pkt_dev->src_max, buf, len);
13751da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
13761da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
13771da177e4SLinus Torvalds 		}
13781da177e4SLinus Torvalds 		if (debug)
137925a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1380222f1806SLuiz Capitulino 			       pkt_dev->src_max);
13811da177e4SLinus Torvalds 		i += len;
13821da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
13831da177e4SLinus Torvalds 		return count;
13841da177e4SLinus Torvalds 	}
13851da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
13861da177e4SLinus Torvalds 		char *v = valstr;
1387f404e9a6SKris Katterjohn 		unsigned char old_dmac[ETH_ALEN];
13881da177e4SLinus Torvalds 		unsigned char *m = pkt_dev->dst_mac;
1389f404e9a6SKris Katterjohn 		memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
139263adc6fbSStephen Hemminger 		if (len < 0)
1393222f1806SLuiz Capitulino 			return len;
139463adc6fbSStephen Hemminger 
13951da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
13961da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
13971da177e4SLinus Torvalds 			return -EFAULT;
13981da177e4SLinus Torvalds 		i += len;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 		for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
14011da177e4SLinus Torvalds 			if (*v >= '0' && *v <= '9') {
14021da177e4SLinus Torvalds 				*m *= 16;
14031da177e4SLinus Torvalds 				*m += *v - '0';
14041da177e4SLinus Torvalds 			}
14051da177e4SLinus Torvalds 			if (*v >= 'A' && *v <= 'F') {
14061da177e4SLinus Torvalds 				*m *= 16;
14071da177e4SLinus Torvalds 				*m += *v - 'A' + 10;
14081da177e4SLinus Torvalds 			}
14091da177e4SLinus Torvalds 			if (*v >= 'a' && *v <= 'f') {
14101da177e4SLinus Torvalds 				*m *= 16;
14111da177e4SLinus Torvalds 				*m += *v - 'a' + 10;
14121da177e4SLinus Torvalds 			}
14131da177e4SLinus Torvalds 			if (*v == ':') {
14141da177e4SLinus Torvalds 				m++;
14151da177e4SLinus Torvalds 				*m = 0;
14161da177e4SLinus Torvalds 			}
14171da177e4SLinus Torvalds 		}
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 		/* Set up Dest MAC */
1420f404e9a6SKris Katterjohn 		if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1421f404e9a6SKris Katterjohn 			memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dstmac");
14241da177e4SLinus Torvalds 		return count;
14251da177e4SLinus Torvalds 	}
14261da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
14271da177e4SLinus Torvalds 		char *v = valstr;
1428ce5d0b47SAdit Ranadive 		unsigned char old_smac[ETH_ALEN];
14291da177e4SLinus Torvalds 		unsigned char *m = pkt_dev->src_mac;
14301da177e4SLinus Torvalds 
1431ce5d0b47SAdit Ranadive 		memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1432ce5d0b47SAdit Ranadive 
14331da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
143463adc6fbSStephen Hemminger 		if (len < 0)
1435222f1806SLuiz Capitulino 			return len;
143663adc6fbSStephen Hemminger 
14371da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
14381da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
14391da177e4SLinus Torvalds 			return -EFAULT;
14401da177e4SLinus Torvalds 		i += len;
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 		for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
14431da177e4SLinus Torvalds 			if (*v >= '0' && *v <= '9') {
14441da177e4SLinus Torvalds 				*m *= 16;
14451da177e4SLinus Torvalds 				*m += *v - '0';
14461da177e4SLinus Torvalds 			}
14471da177e4SLinus Torvalds 			if (*v >= 'A' && *v <= 'F') {
14481da177e4SLinus Torvalds 				*m *= 16;
14491da177e4SLinus Torvalds 				*m += *v - 'A' + 10;
14501da177e4SLinus Torvalds 			}
14511da177e4SLinus Torvalds 			if (*v >= 'a' && *v <= 'f') {
14521da177e4SLinus Torvalds 				*m *= 16;
14531da177e4SLinus Torvalds 				*m += *v - 'a' + 10;
14541da177e4SLinus Torvalds 			}
14551da177e4SLinus Torvalds 			if (*v == ':') {
14561da177e4SLinus Torvalds 				m++;
14571da177e4SLinus Torvalds 				*m = 0;
14581da177e4SLinus Torvalds 			}
14591da177e4SLinus Torvalds 		}
14601da177e4SLinus Torvalds 
1461ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
1462ce5d0b47SAdit Ranadive 		if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1463ce5d0b47SAdit Ranadive 			memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1464ce5d0b47SAdit Ranadive 
14651da177e4SLinus Torvalds 		sprintf(pg_result, "OK: srcmac");
14661da177e4SLinus Torvalds 		return count;
14671da177e4SLinus Torvalds 	}
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
14701da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
14711da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
14721da177e4SLinus Torvalds 		return count;
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
14761da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
147763adc6fbSStephen Hemminger 		if (len < 0)
1478222f1806SLuiz Capitulino 			return len;
147963adc6fbSStephen Hemminger 
14801da177e4SLinus Torvalds 		i += len;
14811da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
14821da177e4SLinus Torvalds 			value = MAX_CFLOWS;
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 		pkt_dev->cflows = value;
14851da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
14861da177e4SLinus Torvalds 		return count;
14871da177e4SLinus Torvalds 	}
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
14901da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
149163adc6fbSStephen Hemminger 		if (len < 0)
1492222f1806SLuiz Capitulino 			return len;
149363adc6fbSStephen Hemminger 
14941da177e4SLinus Torvalds 		i += len;
14951da177e4SLinus Torvalds 		pkt_dev->lflow = value;
14961da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
14971da177e4SLinus Torvalds 		return count;
14981da177e4SLinus Torvalds 	}
14991da177e4SLinus Torvalds 
150045b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
150145b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
150263adc6fbSStephen Hemminger 		if (len < 0)
150345b270f8SRobert Olsson 			return len;
150463adc6fbSStephen Hemminger 
150545b270f8SRobert Olsson 		i += len;
150645b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
150745b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
150845b270f8SRobert Olsson 		return count;
150945b270f8SRobert Olsson 	}
151045b270f8SRobert Olsson 
151145b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
151245b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
151363adc6fbSStephen Hemminger 		if (len < 0)
151445b270f8SRobert Olsson 			return len;
151563adc6fbSStephen Hemminger 
151645b270f8SRobert Olsson 		i += len;
151745b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
151845b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
151945b270f8SRobert Olsson 		return count;
152045b270f8SRobert Olsson 	}
152145b270f8SRobert Olsson 
1522ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
1523cfcabdccSStephen Hemminger 		unsigned n, cnt;
1524cfcabdccSStephen Hemminger 
1525ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1526cfcabdccSStephen Hemminger 		if (len < 0)
1527cfcabdccSStephen Hemminger 			return len;
1528ca6549afSSteven Whitehouse 		i += len;
1529cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1530ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1531cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1532ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1533ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
153434954ddcSFrancesco Fondelli 
153534954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
153634954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
153734954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
153834954ddcSFrancesco Fondelli 
153934954ddcSFrancesco Fondelli 			if (debug)
154025a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
154134954ddcSFrancesco Fondelli 		}
154234954ddcSFrancesco Fondelli 		return count;
154334954ddcSFrancesco Fondelli 	}
154434954ddcSFrancesco Fondelli 
154534954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
154634954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
154763adc6fbSStephen Hemminger 		if (len < 0)
154834954ddcSFrancesco Fondelli 			return len;
154963adc6fbSStephen Hemminger 
155034954ddcSFrancesco Fondelli 		i += len;
155134954ddcSFrancesco Fondelli 		if (value <= 4095) {
155234954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
155334954ddcSFrancesco Fondelli 
155434954ddcSFrancesco Fondelli 			if (debug)
155525a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN turned on\n");
155634954ddcSFrancesco Fondelli 
155734954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
155825a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
155934954ddcSFrancesco Fondelli 
156034954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
156134954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
156234954ddcSFrancesco Fondelli 		} else {
156334954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
156434954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
156534954ddcSFrancesco Fondelli 
156634954ddcSFrancesco Fondelli 			if (debug)
156725a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
156834954ddcSFrancesco Fondelli 		}
156934954ddcSFrancesco Fondelli 		return count;
157034954ddcSFrancesco Fondelli 	}
157134954ddcSFrancesco Fondelli 
157234954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
157334954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
157463adc6fbSStephen Hemminger 		if (len < 0)
157534954ddcSFrancesco Fondelli 			return len;
157663adc6fbSStephen Hemminger 
157734954ddcSFrancesco Fondelli 		i += len;
157834954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
157934954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
158034954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
158134954ddcSFrancesco Fondelli 		} else {
158234954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
158334954ddcSFrancesco Fondelli 		}
158434954ddcSFrancesco Fondelli 		return count;
158534954ddcSFrancesco Fondelli 	}
158634954ddcSFrancesco Fondelli 
158734954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
158834954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
158963adc6fbSStephen Hemminger 		if (len < 0)
159034954ddcSFrancesco Fondelli 			return len;
159163adc6fbSStephen Hemminger 
159234954ddcSFrancesco Fondelli 		i += len;
159334954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
159434954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
159534954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
159634954ddcSFrancesco Fondelli 		} else {
159734954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
159834954ddcSFrancesco Fondelli 		}
159934954ddcSFrancesco Fondelli 		return count;
160034954ddcSFrancesco Fondelli 	}
160134954ddcSFrancesco Fondelli 
160234954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
160334954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
160463adc6fbSStephen Hemminger 		if (len < 0)
160534954ddcSFrancesco Fondelli 			return len;
160663adc6fbSStephen Hemminger 
160734954ddcSFrancesco Fondelli 		i += len;
160834954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
160934954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
161034954ddcSFrancesco Fondelli 
161134954ddcSFrancesco Fondelli 			if (debug)
161225a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
161334954ddcSFrancesco Fondelli 
161434954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
161525a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
161634954ddcSFrancesco Fondelli 
161734954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
161834954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
161934954ddcSFrancesco Fondelli 		} else {
162034954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
162134954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
162234954ddcSFrancesco Fondelli 
162334954ddcSFrancesco Fondelli 			if (debug)
162425a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
162534954ddcSFrancesco Fondelli 		}
162634954ddcSFrancesco Fondelli 		return count;
162734954ddcSFrancesco Fondelli 	}
162834954ddcSFrancesco Fondelli 
162934954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
163034954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
163163adc6fbSStephen Hemminger 		if (len < 0)
163234954ddcSFrancesco Fondelli 			return len;
163363adc6fbSStephen Hemminger 
163434954ddcSFrancesco Fondelli 		i += len;
163534954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
163634954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
163734954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
163834954ddcSFrancesco Fondelli 		} else {
163934954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
164034954ddcSFrancesco Fondelli 		}
164134954ddcSFrancesco Fondelli 		return count;
164234954ddcSFrancesco Fondelli 	}
164334954ddcSFrancesco Fondelli 
164434954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
164534954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
164663adc6fbSStephen Hemminger 		if (len < 0)
164734954ddcSFrancesco Fondelli 			return len;
164863adc6fbSStephen Hemminger 
164934954ddcSFrancesco Fondelli 		i += len;
165034954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
165134954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
165234954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
165334954ddcSFrancesco Fondelli 		} else {
165434954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
165534954ddcSFrancesco Fondelli 		}
1656ca6549afSSteven Whitehouse 		return count;
1657ca6549afSSteven Whitehouse 	}
1658ca6549afSSteven Whitehouse 
16591ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
16601ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16611ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
166263adc6fbSStephen Hemminger 		if (len < 0)
16631ca7768cSFrancesco Fondelli 			return len;
166463adc6fbSStephen Hemminger 
16651ca7768cSFrancesco Fondelli 		i += len;
16661ca7768cSFrancesco Fondelli 		if (len == 2) {
16671ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
16681ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
16691ca7768cSFrancesco Fondelli 		} else {
16701ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
16711ca7768cSFrancesco Fondelli 		}
16721ca7768cSFrancesco Fondelli 		return count;
16731ca7768cSFrancesco Fondelli 	}
16741ca7768cSFrancesco Fondelli 
16751ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
16761ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16771ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
167863adc6fbSStephen Hemminger 		if (len < 0)
16791ca7768cSFrancesco Fondelli 			return len;
168063adc6fbSStephen Hemminger 
16811ca7768cSFrancesco Fondelli 		i += len;
16821ca7768cSFrancesco Fondelli 		if (len == 2) {
16831ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
16841ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
16851ca7768cSFrancesco Fondelli 		} else {
16861ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
16871ca7768cSFrancesco Fondelli 		}
16881ca7768cSFrancesco Fondelli 		return count;
16891ca7768cSFrancesco Fondelli 	}
16901ca7768cSFrancesco Fondelli 
16911da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
16921da177e4SLinus Torvalds 	return -EINVAL;
16931da177e4SLinus Torvalds }
16941da177e4SLinus Torvalds 
1695d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
16961da177e4SLinus Torvalds {
1697d50a6b56SStephen Hemminger 	return single_open(file, pktgen_if_show, PDE(inode)->data);
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
17009a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1701d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1702d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1703d50a6b56SStephen Hemminger 	.read    = seq_read,
1704d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1705d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1706d50a6b56SStephen Hemminger 	.release = single_release,
1707d50a6b56SStephen Hemminger };
1708d50a6b56SStephen Hemminger 
1709d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1710d50a6b56SStephen Hemminger {
1711d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1712648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1713d50a6b56SStephen Hemminger 
1714d50a6b56SStephen Hemminger 	BUG_ON(!t);
1715d50a6b56SStephen Hemminger 
1716d50a6b56SStephen Hemminger 	seq_printf(seq, "Running: ");
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	if_lock(t);
1719c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17201da177e4SLinus Torvalds 		if (pkt_dev->running)
1721593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17221da177e4SLinus Torvalds 
1723d50a6b56SStephen Hemminger 	seq_printf(seq, "\nStopped: ");
17241da177e4SLinus Torvalds 
1725c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
17261da177e4SLinus Torvalds 		if (!pkt_dev->running)
1727593f63b0SEric Dumazet 			seq_printf(seq, "%s ", pkt_dev->odevname);
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds 	if (t->result[0])
1730d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
17311da177e4SLinus Torvalds 	else
1732d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: NA\n");
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 	if_unlock(t);
17351da177e4SLinus Torvalds 
1736d50a6b56SStephen Hemminger 	return 0;
17371da177e4SLinus Torvalds }
17381da177e4SLinus Torvalds 
1739d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1740d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1741d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
17421da177e4SLinus Torvalds {
1743d50a6b56SStephen Hemminger 	struct seq_file *seq = (struct seq_file *)file->private_data;
1744d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
17451da177e4SLinus Torvalds 	int i = 0, max, len, ret;
17461da177e4SLinus Torvalds 	char name[40];
17471da177e4SLinus Torvalds 	char *pg_result;
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	if (count < 1) {
17501da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
17511da177e4SLinus Torvalds 		return -EINVAL;
17521da177e4SLinus Torvalds 	}
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	max = count - i;
17551da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
17561da177e4SLinus Torvalds 	if (len < 0)
17571da177e4SLinus Torvalds 		return len;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 	i += len;
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	/* Read variable name */
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
17641da177e4SLinus Torvalds 	if (len < 0)
17651da177e4SLinus Torvalds 		return len;
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
17681da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
17691da177e4SLinus Torvalds 		return -EFAULT;
17701da177e4SLinus Torvalds 	i += len;
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	max = count - i;
17731da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
17741da177e4SLinus Torvalds 	if (len < 0)
17751da177e4SLinus Torvalds 		return len;
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	i += len;
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	if (debug)
178025a8b254SDavid S. Miller 		printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
178125a8b254SDavid S. Miller 		       name, (unsigned long)count);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	if (!t) {
178425a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: No thread\n");
17851da177e4SLinus Torvalds 		ret = -EINVAL;
17861da177e4SLinus Torvalds 		goto out;
17871da177e4SLinus Torvalds 	}
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
17921da177e4SLinus Torvalds 		char f[32];
17931da177e4SLinus Torvalds 		memset(f, 0, 32);
17941da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
17951da177e4SLinus Torvalds 		if (len < 0) {
17961da177e4SLinus Torvalds 			ret = len;
17971da177e4SLinus Torvalds 			goto out;
17981da177e4SLinus Torvalds 		}
17991da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
18001da177e4SLinus Torvalds 			return -EFAULT;
18011da177e4SLinus Torvalds 		i += len;
18026146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
18031da177e4SLinus Torvalds 		pktgen_add_device(t, f);
18046146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
18051da177e4SLinus Torvalds 		ret = count;
18061da177e4SLinus Torvalds 		sprintf(pg_result, "OK: add_device=%s", f);
18071da177e4SLinus Torvalds 		goto out;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
18116146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
181295ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
18136146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1814121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
18151da177e4SLinus Torvalds 		ret = count;
18161da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
18171da177e4SLinus Torvalds 		goto out;
18181da177e4SLinus Torvalds 	}
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1821b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
18221da177e4SLinus Torvalds 		ret = count;
18231da177e4SLinus Torvalds 		goto out;
18241da177e4SLinus Torvalds 	}
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 	ret = -EINVAL;
18271da177e4SLinus Torvalds out:
18281da177e4SLinus Torvalds 	return ret;
18291da177e4SLinus Torvalds }
18301da177e4SLinus Torvalds 
1831d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
18321da177e4SLinus Torvalds {
1833d50a6b56SStephen Hemminger 	return single_open(file, pktgen_thread_show, PDE(inode)->data);
18341da177e4SLinus Torvalds }
18351da177e4SLinus Torvalds 
18369a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1837d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1838d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1839d50a6b56SStephen Hemminger 	.read    = seq_read,
1840d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1841d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1842d50a6b56SStephen Hemminger 	.release = single_release,
1843d50a6b56SStephen Hemminger };
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds /* Think find or remove for NN */
18461da177e4SLinus Torvalds static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
18471da177e4SLinus Torvalds {
18481da177e4SLinus Torvalds 	struct pktgen_thread *t;
18491da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
18503e984840SEric Dumazet 	bool exact = (remove == FIND);
18511da177e4SLinus Torvalds 
1852cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list) {
18533e984840SEric Dumazet 		pkt_dev = pktgen_find_dev(t, ifname, exact);
18541da177e4SLinus Torvalds 		if (pkt_dev) {
18551da177e4SLinus Torvalds 			if (remove) {
18561da177e4SLinus Torvalds 				if_lock(t);
185795ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
185895ed63f7SArthur Kepner 				t->control |= T_REMDEV;
18591da177e4SLinus Torvalds 				if_unlock(t);
18601da177e4SLinus Torvalds 			}
18611da177e4SLinus Torvalds 			break;
18621da177e4SLinus Torvalds 		}
18631da177e4SLinus Torvalds 	}
18641da177e4SLinus Torvalds 	return pkt_dev;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
186795ed63f7SArthur Kepner /*
186895ed63f7SArthur Kepner  * mark a device for removal
186995ed63f7SArthur Kepner  */
187039df232fSStephen Hemminger static void pktgen_mark_device(const char *ifname)
18711da177e4SLinus Torvalds {
18721da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
187395ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
187495ed63f7SArthur Kepner 	int i = 0;
187595ed63f7SArthur Kepner 
18766146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
187725c4e53aSStephen Hemminger 	pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
187895ed63f7SArthur Kepner 
187995ed63f7SArthur Kepner 	while (1) {
188095ed63f7SArthur Kepner 
188195ed63f7SArthur Kepner 		pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1882222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1883222f1806SLuiz Capitulino 			break;	/* success */
188495ed63f7SArthur Kepner 
18856146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
188625c4e53aSStephen Hemminger 		pr_debug("pktgen: pktgen_mark_device waiting for %s "
188725c4e53aSStephen Hemminger 				"to disappear....\n", ifname);
188895ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
18896146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
189095ed63f7SArthur Kepner 
189195ed63f7SArthur Kepner 		if (++i >= max_tries) {
189225a8b254SDavid S. Miller 			printk(KERN_ERR "pktgen_mark_device: timed out after "
189325a8b254SDavid S. Miller 			       "waiting %d msec for device %s to be removed\n",
189495ed63f7SArthur Kepner 			       msec_per_try * i, ifname);
189595ed63f7SArthur Kepner 			break;
189695ed63f7SArthur Kepner 		}
189795ed63f7SArthur Kepner 
189895ed63f7SArthur Kepner 	}
189995ed63f7SArthur Kepner 
19006146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
190139df232fSStephen Hemminger }
190295ed63f7SArthur Kepner 
190339df232fSStephen Hemminger static void pktgen_change_name(struct net_device *dev)
190439df232fSStephen Hemminger {
190539df232fSStephen Hemminger 	struct pktgen_thread *t;
190639df232fSStephen Hemminger 
190739df232fSStephen Hemminger 	list_for_each_entry(t, &pktgen_threads, th_list) {
190839df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
190939df232fSStephen Hemminger 
191039df232fSStephen Hemminger 		list_for_each_entry(pkt_dev, &t->if_list, list) {
191139df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
191239df232fSStephen Hemminger 				continue;
191339df232fSStephen Hemminger 
191439df232fSStephen Hemminger 			remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
191539df232fSStephen Hemminger 
19162975315bSAlexey Dobriyan 			pkt_dev->entry = proc_create_data(dev->name, 0600,
19172975315bSAlexey Dobriyan 							  pg_proc_dir,
19182975315bSAlexey Dobriyan 							  &pktgen_if_fops,
19192975315bSAlexey Dobriyan 							  pkt_dev);
192039df232fSStephen Hemminger 			if (!pkt_dev->entry)
192139df232fSStephen Hemminger 				printk(KERN_ERR "pktgen: can't move proc "
192239df232fSStephen Hemminger 				       " entry for '%s'\n", dev->name);
192339df232fSStephen Hemminger 			break;
192439df232fSStephen Hemminger 		}
192539df232fSStephen Hemminger 	}
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds 
1928222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
1929222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
19301da177e4SLinus Torvalds {
193139df232fSStephen Hemminger 	struct net_device *dev = ptr;
19321da177e4SLinus Torvalds 
1933721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
1934e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1935e9dc8653SEric W. Biederman 
19361da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
19371da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
19381da177e4SLinus Torvalds 	 */
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 	switch (event) {
194139df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
194239df232fSStephen Hemminger 		pktgen_change_name(dev);
19431da177e4SLinus Torvalds 		break;
19441da177e4SLinus Torvalds 
19451da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
194695ed63f7SArthur Kepner 		pktgen_mark_device(dev->name);
19471da177e4SLinus Torvalds 		break;
19483ff50b79SStephen Hemminger 	}
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	return NOTIFY_DONE;
19511da177e4SLinus Torvalds }
19521da177e4SLinus Torvalds 
195363adc6fbSStephen Hemminger static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
195463adc6fbSStephen Hemminger 						 const char *ifname)
1955e6fce5b9SRobert Olsson {
1956e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
1957e6fce5b9SRobert Olsson 	int i = 0;
1958e6fce5b9SRobert Olsson 
1959e6fce5b9SRobert Olsson 	for (i = 0; ifname[i] != '@'; i++) {
1960e6fce5b9SRobert Olsson 		if (i == IFNAMSIZ)
1961e6fce5b9SRobert Olsson 			break;
1962e6fce5b9SRobert Olsson 
1963e6fce5b9SRobert Olsson 		b[i] = ifname[i];
1964e6fce5b9SRobert Olsson 	}
1965e6fce5b9SRobert Olsson 	b[i] = 0;
1966e6fce5b9SRobert Olsson 
1967e6fce5b9SRobert Olsson 	return dev_get_by_name(&init_net, b);
1968e6fce5b9SRobert Olsson }
1969e6fce5b9SRobert Olsson 
1970e6fce5b9SRobert Olsson 
19711da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
19721da177e4SLinus Torvalds 
197339df232fSStephen Hemminger static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1974222f1806SLuiz Capitulino {
19751da177e4SLinus Torvalds 	struct net_device *odev;
197639df232fSStephen Hemminger 	int err;
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 	/* Clean old setups */
19791da177e4SLinus Torvalds 	if (pkt_dev->odev) {
19801da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
19811da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
19821da177e4SLinus Torvalds 	}
19831da177e4SLinus Torvalds 
1984e6fce5b9SRobert Olsson 	odev = pktgen_dev_get_by_name(pkt_dev, ifname);
19851da177e4SLinus Torvalds 	if (!odev) {
198625a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
198739df232fSStephen Hemminger 		return -ENODEV;
19881da177e4SLinus Torvalds 	}
198939df232fSStephen Hemminger 
19901da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
199125a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
199239df232fSStephen Hemminger 		err = -EINVAL;
199339df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
199425a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
199539df232fSStephen Hemminger 		err = -ENETDOWN;
199639df232fSStephen Hemminger 	} else {
19971da177e4SLinus Torvalds 		pkt_dev->odev = odev;
199839df232fSStephen Hemminger 		return 0;
199939df232fSStephen Hemminger 	}
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	dev_put(odev);
200239df232fSStephen Hemminger 	return err;
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
20061da177e4SLinus Torvalds  * structure to have the right information to create/send packets
20071da177e4SLinus Torvalds  */
20081da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
20091da177e4SLinus Torvalds {
201064c00d81SAndrew Gallatin 	int ntxq;
201164c00d81SAndrew Gallatin 
20121da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
201325a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
201425a8b254SDavid S. Miller 		       "setup_inject.\n");
2015222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
2016222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
20171da177e4SLinus Torvalds 		return;
20181da177e4SLinus Torvalds 	}
20191da177e4SLinus Torvalds 
202064c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
202164c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
2022bfdbc0acSRobert Olsson 
202364c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
202464c00d81SAndrew Gallatin 		printk(KERN_WARNING "pktgen: WARNING: Requested "
202588271660SJesse Brandeburg 		       "queue_map_min (zero-based) (%d) exceeds valid range "
202688271660SJesse Brandeburg 		       "[0 - %d] for (%d) queues on %s, resetting\n",
202788271660SJesse Brandeburg 		       pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2028593f63b0SEric Dumazet 		       pkt_dev->odevname);
202964c00d81SAndrew Gallatin 		pkt_dev->queue_map_min = ntxq - 1;
203064c00d81SAndrew Gallatin 	}
203188271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
203264c00d81SAndrew Gallatin 		printk(KERN_WARNING "pktgen: WARNING: Requested "
203388271660SJesse Brandeburg 		       "queue_map_max (zero-based) (%d) exceeds valid range "
203488271660SJesse Brandeburg 		       "[0 - %d] for (%d) queues on %s, resetting\n",
203588271660SJesse Brandeburg 		       pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2036593f63b0SEric Dumazet 		       pkt_dev->odevname);
203764c00d81SAndrew Gallatin 		pkt_dev->queue_map_max = ntxq - 1;
203864c00d81SAndrew Gallatin 	}
203964c00d81SAndrew Gallatin 
20401da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
20411da177e4SLinus Torvalds 
2042f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
2043f404e9a6SKris Katterjohn 		memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	/* Set up Dest MAC */
2046f404e9a6SKris Katterjohn 	memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	/* Set up pkt size */
20491da177e4SLinus Torvalds 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
20521da177e4SLinus Torvalds 		/*
20531da177e4SLinus Torvalds 		 * Skip this automatic address setting until locks or functions
20541da177e4SLinus Torvalds 		 * gets exported
20551da177e4SLinus Torvalds 		 */
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds #ifdef NOTNOW
20581da177e4SLinus Torvalds 		int i, set = 0, err = 1;
20591da177e4SLinus Torvalds 		struct inet6_dev *idev;
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
20621da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
20631da177e4SLinus Torvalds 				set = 1;
20641da177e4SLinus Torvalds 				break;
20651da177e4SLinus Torvalds 			}
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 		if (!set) {
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 			/*
20701da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
20711da177e4SLinus Torvalds 			 *
20721da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
20731da177e4SLinus Torvalds 			 */
20741da177e4SLinus Torvalds 
20758814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
207663adc6fbSStephen Hemminger 			idev = __in6_dev_get(pkt_dev->odev);
207763adc6fbSStephen Hemminger 			if (idev) {
20781da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
2081222f1806SLuiz Capitulino 				for (ifp = idev->addr_list; ifp;
2082222f1806SLuiz Capitulino 				     ifp = ifp->if_next) {
2083f64f9e71SJoe Perches 					if (ifp->scope == IFA_LINK &&
2084f64f9e71SJoe Perches 					    !(ifp->flags & IFA_F_TENTATIVE)) {
2085222f1806SLuiz Capitulino 						ipv6_addr_copy(&pkt_dev->
2086222f1806SLuiz Capitulino 							       cur_in6_saddr,
2087222f1806SLuiz Capitulino 							       &ifp->addr);
20881da177e4SLinus Torvalds 						err = 0;
20891da177e4SLinus Torvalds 						break;
20901da177e4SLinus Torvalds 					}
20911da177e4SLinus Torvalds 				}
20921da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
20931da177e4SLinus Torvalds 			}
20948814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2095222f1806SLuiz Capitulino 			if (err)
209625a8b254SDavid S. Miller 				printk(KERN_ERR "pktgen: ERROR: IPv6 link "
209725a8b254SDavid S. Miller 				       "address not availble.\n");
20981da177e4SLinus Torvalds 		}
20991da177e4SLinus Torvalds #endif
2100222f1806SLuiz Capitulino 	} else {
21011da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
21021da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
21031da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 			struct in_device *in_dev;
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 			rcu_read_lock();
2108e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
21091da177e4SLinus Torvalds 			if (in_dev) {
21101da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2111222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2112222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
21131da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
21141da177e4SLinus Torvalds 				}
21151da177e4SLinus Torvalds 			}
21161da177e4SLinus Torvalds 			rcu_read_unlock();
2117222f1806SLuiz Capitulino 		} else {
21181da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
21191da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
21201da177e4SLinus Torvalds 		}
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
21231da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
21241da177e4SLinus Torvalds 	}
21251da177e4SLinus Torvalds 	/* Initialize current values. */
21261da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
21271da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
21281da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
21291da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
21301da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
21311da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
21321da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
21331da177e4SLinus Torvalds }
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds 
2136fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2137fd29cf72SStephen Hemminger {
2138ef87979cSStephen Hemminger 	ktime_t start_time, end_time;
2139417bc4b8SEric Dumazet 	s64 remaining;
21402bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2141fd29cf72SStephen Hemminger 
21422bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
21432bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2144fd29cf72SStephen Hemminger 
21452bc481cfSStephen Hemminger 	remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
2146417bc4b8SEric Dumazet 	if (remaining <= 0) {
2147417bc4b8SEric Dumazet 		pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21481da177e4SLinus Torvalds 		return;
2149417bc4b8SEric Dumazet 	}
21502bc481cfSStephen Hemminger 
2151ef87979cSStephen Hemminger 	start_time = ktime_now();
21522bc481cfSStephen Hemminger 	if (remaining < 100)
21532bc481cfSStephen Hemminger 		udelay(remaining); 	/* really small just spin */
21542bc481cfSStephen Hemminger 	else {
21552bc481cfSStephen Hemminger 		/* see do_nanosleep */
21562bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
21572bc481cfSStephen Hemminger 		do {
21582bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
21592bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
21602bc481cfSStephen Hemminger 			if (!hrtimer_active(&t.timer))
21612bc481cfSStephen Hemminger 				t.task = NULL;
21622bc481cfSStephen Hemminger 
21632bc481cfSStephen Hemminger 			if (likely(t.task))
21641da177e4SLinus Torvalds 				schedule();
21651da177e4SLinus Torvalds 
21662bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
21672bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
21682bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
21691da177e4SLinus Torvalds 	}
2170ef87979cSStephen Hemminger 	end_time = ktime_now();
2171ef87979cSStephen Hemminger 
2172ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2173*07a0f0f0SDaniel Turull 	pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
21741da177e4SLinus Torvalds }
21751da177e4SLinus Torvalds 
217616dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
217716dab72fSJamal Hadi Salim {
2178a553e4a6SJamal Hadi Salim 	pkt_dev->pkt_overhead = 0;
217916dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
218016dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
218116dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
218216dab72fSJamal Hadi Salim }
218316dab72fSJamal Hadi Salim 
2184648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2185007a531bSJamal Hadi Salim {
2186648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2187007a531bSJamal Hadi Salim }
2188007a531bSJamal Hadi Salim 
2189007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2190007a531bSJamal Hadi Salim {
2191007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2192007a531bSJamal Hadi Salim 
2193007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2194007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2195007a531bSJamal Hadi Salim 			/* reset time */
2196007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
21971211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2198007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2199007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2200007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2201007a531bSJamal Hadi Salim 		}
2202007a531bSJamal Hadi Salim 	} else {
2203007a531bSJamal Hadi Salim 		flow = random32() % pkt_dev->cflows;
22041211a645SRobert Olsson 		pkt_dev->curfl = flow;
2205007a531bSJamal Hadi Salim 
22061211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2207007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
22081211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
22091211a645SRobert Olsson 		}
2210007a531bSJamal Hadi Salim 	}
2211007a531bSJamal Hadi Salim 
2212007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2213007a531bSJamal Hadi Salim }
2214007a531bSJamal Hadi Salim 
2215a553e4a6SJamal Hadi Salim 
2216a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2217a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2218a553e4a6SJamal Hadi Salim  * we go look for it ...
2219a553e4a6SJamal Hadi Salim */
2220bd55775cSJamal Hadi Salim #define DUMMY_MARK 0
2221fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2222a553e4a6SJamal Hadi Salim {
2223a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
2224a553e4a6SJamal Hadi Salim 	if (!x) {
2225a553e4a6SJamal Hadi Salim 		/*slow path: we dont already have xfrm_state*/
2226bd55775cSJamal Hadi Salim 		x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
22275447c5e4SAlexey Dobriyan 					(xfrm_address_t *)&pkt_dev->cur_daddr,
2228a553e4a6SJamal Hadi Salim 					(xfrm_address_t *)&pkt_dev->cur_saddr,
2229a553e4a6SJamal Hadi Salim 					AF_INET,
2230a553e4a6SJamal Hadi Salim 					pkt_dev->ipsmode,
2231a553e4a6SJamal Hadi Salim 					pkt_dev->ipsproto, 0);
2232a553e4a6SJamal Hadi Salim 		if (x) {
2233a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2234a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2235a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead += x->props.header_len;
2236a553e4a6SJamal Hadi Salim 		}
2237a553e4a6SJamal Hadi Salim 
2238a553e4a6SJamal Hadi Salim 	}
2239a553e4a6SJamal Hadi Salim }
2240a553e4a6SJamal Hadi Salim #endif
2241fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2242fd2ea0a7SDavid S. Miller {
2243e6fce5b9SRobert Olsson 
2244e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2245e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2246e6fce5b9SRobert Olsson 
2247896a7cf8SEric Dumazet 	else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2248fd2ea0a7SDavid S. Miller 		__u16 t;
2249fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2250fd2ea0a7SDavid S. Miller 			t = random32() %
2251fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2252fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2253fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2254fd2ea0a7SDavid S. Miller 		} else {
2255fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2256fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2257fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2258fd2ea0a7SDavid S. Miller 		}
2259fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2260fd2ea0a7SDavid S. Miller 	}
2261bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2262fd2ea0a7SDavid S. Miller }
2263fd2ea0a7SDavid S. Miller 
22641da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
22651da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
22661da177e4SLinus Torvalds  */
2267222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2268222f1806SLuiz Capitulino {
22691da177e4SLinus Torvalds 	__u32 imn;
22701da177e4SLinus Torvalds 	__u32 imx;
22711da177e4SLinus Torvalds 	int flow = 0;
22721da177e4SLinus Torvalds 
2273007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2274007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	/*  Deal with source MAC */
22771da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
22781da177e4SLinus Torvalds 		__u32 mc;
22791da177e4SLinus Torvalds 		__u32 tmp;
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
22825fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->src_mac_count;
22831da177e4SLinus Torvalds 		else {
22841da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2285ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2286222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
22871da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
22881da177e4SLinus Torvalds 		}
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
22911da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
22921da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
22931da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
22941da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
22951da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
22961da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
22971da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
22981da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
22991da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
23031da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
23041da177e4SLinus Torvalds 		__u32 mc;
23051da177e4SLinus Torvalds 		__u32 tmp;
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
23085fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->dst_mac_count;
23091da177e4SLinus Torvalds 
23101da177e4SLinus Torvalds 		else {
23111da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2312ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2313222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
23141da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
23151da177e4SLinus Torvalds 			}
23161da177e4SLinus Torvalds 		}
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
23191da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
23201da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
23211da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
23221da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
23231da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
23241da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
23251da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
23261da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
23271da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
23281da177e4SLinus Torvalds 	}
23291da177e4SLinus Torvalds 
2330ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
2331ca6549afSSteven Whitehouse 		unsigned i;
2332ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2333ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2334ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
23355fa6fc76SStephen Hemminger 					     ((__force __be32)random32() &
2336ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2337ca6549afSSteven Whitehouse 	}
2338ca6549afSSteven Whitehouse 
233934954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
23405fa6fc76SStephen Hemminger 		pkt_dev->vlan_id = random32() & (4096-1);
234134954ddcSFrancesco Fondelli 	}
234234954ddcSFrancesco Fondelli 
234334954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
23445fa6fc76SStephen Hemminger 		pkt_dev->svlan_id = random32() & (4096 - 1);
234534954ddcSFrancesco Fondelli 	}
234634954ddcSFrancesco Fondelli 
23471da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
23481da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
23495fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_src = random32() %
23505fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
23515fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds 		else {
23541da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
23551da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
23561da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
23571da177e4SLinus Torvalds 		}
23581da177e4SLinus Torvalds 	}
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
23611da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
23625fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_dst = random32() %
23635fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
23645fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2365222f1806SLuiz Capitulino 		} else {
23661da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
23671da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
23681da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
23691da177e4SLinus Torvalds 		}
23701da177e4SLinus Torvalds 	}
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
23731da177e4SLinus Torvalds 
237463adc6fbSStephen Hemminger 		imn = ntohl(pkt_dev->saddr_min);
237563adc6fbSStephen Hemminger 		imx = ntohl(pkt_dev->saddr_max);
237663adc6fbSStephen Hemminger 		if (imn < imx) {
23771da177e4SLinus Torvalds 			__u32 t;
23781da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
23795fa6fc76SStephen Hemminger 				t = random32() % (imx - imn) + imn;
23801da177e4SLinus Torvalds 			else {
23811da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
23821da177e4SLinus Torvalds 				t++;
238363adc6fbSStephen Hemminger 				if (t > imx)
23841da177e4SLinus Torvalds 					t = imn;
238563adc6fbSStephen Hemminger 
23861da177e4SLinus Torvalds 			}
23871da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
23881da177e4SLinus Torvalds 		}
23891da177e4SLinus Torvalds 
2390007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
23911da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
23921da177e4SLinus Torvalds 		} else {
2393252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2394252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2395252e3346SAl Viro 			if (imn < imx) {
23961da177e4SLinus Torvalds 				__u32 t;
2397252e3346SAl Viro 				__be32 s;
23981da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
23991da177e4SLinus Torvalds 
24005fa6fc76SStephen Hemminger 					t = random32() % (imx - imn) + imn;
2401252e3346SAl Viro 					s = htonl(t);
24021da177e4SLinus Torvalds 
240321cf2253SJoe Perches 					while (ipv4_is_loopback(s) ||
240421cf2253SJoe Perches 					       ipv4_is_multicast(s) ||
24051e637c74SJan Engelhardt 					       ipv4_is_lbcast(s) ||
240621cf2253SJoe Perches 					       ipv4_is_zeronet(s) ||
240721cf2253SJoe Perches 					       ipv4_is_local_multicast(s)) {
24085fa6fc76SStephen Hemminger 						t = random32() % (imx - imn) + imn;
2409252e3346SAl Viro 						s = htonl(t);
24101da177e4SLinus Torvalds 					}
2411252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2412252e3346SAl Viro 				} else {
24131da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
24141da177e4SLinus Torvalds 					t++;
24151da177e4SLinus Torvalds 					if (t > imx) {
24161da177e4SLinus Torvalds 						t = imn;
24171da177e4SLinus Torvalds 					}
24181da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
24191da177e4SLinus Torvalds 				}
24201da177e4SLinus Torvalds 			}
24211da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2422007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2423222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2424222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2425a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2426a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2427a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2428a553e4a6SJamal Hadi Salim #endif
24291da177e4SLinus Torvalds 				pkt_dev->nflows++;
24301da177e4SLinus Torvalds 			}
24311da177e4SLinus Torvalds 		}
2432222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2433222f1806SLuiz Capitulino 
24341da177e4SLinus Torvalds 		if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
24351da177e4SLinus Torvalds 		    pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
24361da177e4SLinus Torvalds 		    pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
24371da177e4SLinus Torvalds 		    pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
24381da177e4SLinus Torvalds 		else {
24391da177e4SLinus Torvalds 			int i;
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 			/* Only random destinations yet */
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
24441da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
24455fa6fc76SStephen Hemminger 				    (((__force __be32)random32() |
24461da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
24471da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
24481da177e4SLinus Torvalds 			}
24491da177e4SLinus Torvalds 		}
24501da177e4SLinus Torvalds 	}
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
24531da177e4SLinus Torvalds 		__u32 t;
24541da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
24555fa6fc76SStephen Hemminger 			t = random32() %
24565fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
24575fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2458222f1806SLuiz Capitulino 		} else {
24591da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
24601da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
24611da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
24621da177e4SLinus Torvalds 		}
24631da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
24641da177e4SLinus Torvalds 	}
24651da177e4SLinus Torvalds 
2466fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
246745b270f8SRobert Olsson 
24681da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
24691da177e4SLinus Torvalds }
24701da177e4SLinus Torvalds 
2471a553e4a6SJamal Hadi Salim 
2472a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2473a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2474a553e4a6SJamal Hadi Salim {
2475a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2476a553e4a6SJamal Hadi Salim 	int err = 0;
2477a553e4a6SJamal Hadi Salim 	struct iphdr *iph;
2478a553e4a6SJamal Hadi Salim 
2479a553e4a6SJamal Hadi Salim 	if (!x)
2480a553e4a6SJamal Hadi Salim 		return 0;
2481a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2482a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2483a553e4a6SJamal Hadi Salim 	if (x->props.mode != XFRM_MODE_TRANSPORT)
2484a553e4a6SJamal Hadi Salim 		return 0;
2485a553e4a6SJamal Hadi Salim 
2486a553e4a6SJamal Hadi Salim 	spin_lock(&x->lock);
2487a553e4a6SJamal Hadi Salim 	iph = ip_hdr(skb);
2488a553e4a6SJamal Hadi Salim 
248913996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2490a553e4a6SJamal Hadi Salim 	if (err)
2491a553e4a6SJamal Hadi Salim 		goto error;
2492a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
2493a553e4a6SJamal Hadi Salim 	if (err)
2494a553e4a6SJamal Hadi Salim 		goto error;
2495a553e4a6SJamal Hadi Salim 
2496a553e4a6SJamal Hadi Salim 	x->curlft.bytes += skb->len;
2497a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
2498a553e4a6SJamal Hadi Salim error:
2499a553e4a6SJamal Hadi Salim 	spin_unlock(&x->lock);
2500a553e4a6SJamal Hadi Salim 	return err;
2501a553e4a6SJamal Hadi Salim }
2502a553e4a6SJamal Hadi Salim 
2503475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2504a553e4a6SJamal Hadi Salim {
2505a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2506a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2507a553e4a6SJamal Hadi Salim 		int i = 0;
25085b5f792aSFlorian Westphal 		for (;  i < pkt_dev->cflows; i++) {
2509a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2510a553e4a6SJamal Hadi Salim 			if (x) {
2511a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2512a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2513a553e4a6SJamal Hadi Salim 			}
2514a553e4a6SJamal Hadi Salim 		}
2515a553e4a6SJamal Hadi Salim 	}
2516a553e4a6SJamal Hadi Salim }
2517a553e4a6SJamal Hadi Salim 
2518475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2519a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2520a553e4a6SJamal Hadi Salim {
2521a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2522a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2523a553e4a6SJamal Hadi Salim 		int nhead = 0;
2524a553e4a6SJamal Hadi Salim 		if (x) {
2525a553e4a6SJamal Hadi Salim 			int ret;
2526a553e4a6SJamal Hadi Salim 			__u8 *eth;
2527a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2528a553e4a6SJamal Hadi Salim 			if (nhead > 0) {
2529a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2530a553e4a6SJamal Hadi Salim 				if (ret < 0) {
253125a8b254SDavid S. Miller 					printk(KERN_ERR "Error expanding "
253225a8b254SDavid S. Miller 					       "ipsec packet %d\n", ret);
2533b4bb4ac8SIlpo Järvinen 					goto err;
2534a553e4a6SJamal Hadi Salim 				}
2535a553e4a6SJamal Hadi Salim 			}
2536a553e4a6SJamal Hadi Salim 
2537a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2538a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2539a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2540a553e4a6SJamal Hadi Salim 			if (ret) {
254125a8b254SDavid S. Miller 				printk(KERN_ERR "Error creating ipsec "
254225a8b254SDavid S. Miller 				       "packet %d\n", ret);
2543b4bb4ac8SIlpo Järvinen 				goto err;
2544a553e4a6SJamal Hadi Salim 			}
2545a553e4a6SJamal Hadi Salim 			/* restore ll */
2546a553e4a6SJamal Hadi Salim 			eth = (__u8 *) skb_push(skb, ETH_HLEN);
2547a553e4a6SJamal Hadi Salim 			memcpy(eth, pkt_dev->hh, 12);
2548a553e4a6SJamal Hadi Salim 			*(u16 *) &eth[12] = protocol;
2549a553e4a6SJamal Hadi Salim 		}
2550a553e4a6SJamal Hadi Salim 	}
2551a553e4a6SJamal Hadi Salim 	return 1;
2552b4bb4ac8SIlpo Järvinen err:
2553b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2554b4bb4ac8SIlpo Järvinen 	return 0;
2555a553e4a6SJamal Hadi Salim }
2556a553e4a6SJamal Hadi Salim #endif
2557a553e4a6SJamal Hadi Salim 
2558ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2559ca6549afSSteven Whitehouse {
2560ca6549afSSteven Whitehouse 	unsigned i;
256163adc6fbSStephen Hemminger 	for (i = 0; i < pkt_dev->nr_labels; i++)
2562ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
256363adc6fbSStephen Hemminger 
2564ca6549afSSteven Whitehouse 	mpls--;
2565ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2566ca6549afSSteven Whitehouse }
2567ca6549afSSteven Whitehouse 
25680f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
25690f37c605SAl Viro 			       unsigned int prio)
25700f37c605SAl Viro {
25710f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
25720f37c605SAl Viro }
25730f37c605SAl Viro 
25741da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
25751da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
25761da177e4SLinus Torvalds {
25771da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
25781da177e4SLinus Torvalds 	__u8 *eth;
25791da177e4SLinus Torvalds 	struct udphdr *udph;
25801da177e4SLinus Torvalds 	int datalen, iplen;
25811da177e4SLinus Torvalds 	struct iphdr *iph;
25821da177e4SLinus Torvalds 	struct pktgen_hdr *pgh = NULL;
2583d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2584ca6549afSSteven Whitehouse 	__be32 *mpls;
258534954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
258634954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
258734954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
258834954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2589fd2ea0a7SDavid S. Miller 	u16 queue_map;
2590ca6549afSSteven Whitehouse 
2591ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2592d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
25931da177e4SLinus Torvalds 
259434954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2595d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
259634954ddcSFrancesco Fondelli 
259764053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
259864053beeSRobert Olsson 	 * fields.
259964053beeSRobert Olsson 	 */
2600fd2ea0a7SDavid S. Miller 	queue_map = pkt_dev->cur_queue_map;
260164053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
260264053beeSRobert Olsson 
26037ac5459eSDavid S. Miller 	datalen = (odev->hard_header_len + 16) & ~0xf;
2604e99b99b4SRobert Olsson 
2605e99b99b4SRobert Olsson 	if (pkt_dev->flags & F_NODE) {
2606e99b99b4SRobert Olsson 		int node;
2607e99b99b4SRobert Olsson 
2608e99b99b4SRobert Olsson 		if (pkt_dev->node >= 0)
2609e99b99b4SRobert Olsson 			node = pkt_dev->node;
2610e99b99b4SRobert Olsson 		else
2611e99b99b4SRobert Olsson 			node =  numa_node_id();
2612e99b99b4SRobert Olsson 
2613e99b99b4SRobert Olsson 		skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2614e99b99b4SRobert Olsson 				  + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2615e99b99b4SRobert Olsson 		if (likely(skb)) {
2616e99b99b4SRobert Olsson 			skb_reserve(skb, NET_SKB_PAD);
2617e99b99b4SRobert Olsson 			skb->dev = odev;
2618e99b99b4SRobert Olsson 		}
2619e99b99b4SRobert Olsson 	}
2620e99b99b4SRobert Olsson 	else
2621e470757dSStephen Hemminger 	  skb = __netdev_alloc_skb(odev,
2622e470757dSStephen Hemminger 				   pkt_dev->cur_pkt_size + 64
2623e470757dSStephen Hemminger 				   + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2624e99b99b4SRobert Olsson 
26251da177e4SLinus Torvalds 	if (!skb) {
26261da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
26271da177e4SLinus Torvalds 		return NULL;
26281da177e4SLinus Torvalds 	}
26291da177e4SLinus Torvalds 
26307ac5459eSDavid S. Miller 	skb_reserve(skb, datalen);
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
26331da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2634ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2635ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2636ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
263734954ddcSFrancesco Fondelli 
263834954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
263934954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
264034954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
26410f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
26420f37c605SAl Viro 					       pkt_dev->svlan_cfi,
26430f37c605SAl Viro 					       pkt_dev->svlan_p);
264434954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2645d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
264634954ddcSFrancesco Fondelli 		}
264734954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
26480f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
26490f37c605SAl Viro 				      pkt_dev->vlan_cfi,
26500f37c605SAl Viro 				      pkt_dev->vlan_p);
265134954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2652d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
265334954ddcSFrancesco Fondelli 	}
265434954ddcSFrancesco Fondelli 
265527a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
2656b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct iphdr);
2657ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2658fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
2659ddc7b8e3SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
2660ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2663252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
26641da177e4SLinus Torvalds 
2665ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2666ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
266716dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
26681da177e4SLinus Torvalds 	if (datalen < sizeof(struct pktgen_hdr))
26691da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
26721da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
26731da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
26741da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds 	iph->ihl = 5;
26771da177e4SLinus Torvalds 	iph->version = 4;
26781da177e4SLinus Torvalds 	iph->ttl = 32;
26791ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
26801da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
26811da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
26821da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
268366ed1e5eSEric Dumazet 	iph->id = htons(pkt_dev->ip_id);
268466ed1e5eSEric Dumazet 	pkt_dev->ip_id++;
26851da177e4SLinus Torvalds 	iph->frag_off = 0;
26861da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
26871da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
26881da177e4SLinus Torvalds 	iph->check = 0;
26891da177e4SLinus Torvalds 	iph->check = ip_fast_csum((void *)iph, iph->ihl);
2690ca6549afSSteven Whitehouse 	skb->protocol = protocol;
2691b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
269216dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
26931da177e4SLinus Torvalds 	skb->dev = odev;
26941da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
26951da177e4SLinus Torvalds 
269666ed1e5eSEric Dumazet 	if (pkt_dev->nfrags <= 0) {
26971da177e4SLinus Torvalds 		pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
269866ed1e5eSEric Dumazet 		memset(pgh + 1, 0, datalen - sizeof(struct pktgen_hdr));
269966ed1e5eSEric Dumazet 	} else {
27001da177e4SLinus Torvalds 		int frags = pkt_dev->nfrags;
270166ed1e5eSEric Dumazet 		int i, len;
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 		pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
27041da177e4SLinus Torvalds 
27051da177e4SLinus Torvalds 		if (frags > MAX_SKB_FRAGS)
27061da177e4SLinus Torvalds 			frags = MAX_SKB_FRAGS;
27071da177e4SLinus Torvalds 		if (datalen > frags * PAGE_SIZE) {
270866ed1e5eSEric Dumazet 			len = datalen - frags * PAGE_SIZE;
270966ed1e5eSEric Dumazet 			memset(skb_put(skb, len), 0, len);
27101da177e4SLinus Torvalds 			datalen = frags * PAGE_SIZE;
27111da177e4SLinus Torvalds 		}
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds 		i = 0;
27141da177e4SLinus Torvalds 		while (datalen > 0) {
271566ed1e5eSEric Dumazet 			struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
27161da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].page = page;
27171da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].page_offset = 0;
27181da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].size =
27191da177e4SLinus Torvalds 			    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
27201da177e4SLinus Torvalds 			datalen -= skb_shinfo(skb)->frags[i].size;
27211da177e4SLinus Torvalds 			skb->len += skb_shinfo(skb)->frags[i].size;
27221da177e4SLinus Torvalds 			skb->data_len += skb_shinfo(skb)->frags[i].size;
27231da177e4SLinus Torvalds 			i++;
27241da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags = i;
27251da177e4SLinus Torvalds 		}
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 		while (i < frags) {
27281da177e4SLinus Torvalds 			int rem;
27291da177e4SLinus Torvalds 
27301da177e4SLinus Torvalds 			if (i == 0)
27311da177e4SLinus Torvalds 				break;
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds 			rem = skb_shinfo(skb)->frags[i - 1].size / 2;
27341da177e4SLinus Torvalds 			if (rem == 0)
27351da177e4SLinus Torvalds 				break;
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i - 1].size -= rem;
27381da177e4SLinus Torvalds 
2739222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i] =
2740222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1];
27411da177e4SLinus Torvalds 			get_page(skb_shinfo(skb)->frags[i].page);
2742222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i].page =
2743222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1].page;
2744222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i].page_offset +=
2745222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1].size;
27461da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].size = rem;
27471da177e4SLinus Torvalds 			i++;
27481da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags = i;
27491da177e4SLinus Torvalds 		}
27501da177e4SLinus Torvalds 	}
27511da177e4SLinus Torvalds 
275263adc6fbSStephen Hemminger 	/* Stamp the time, and sequence number,
275363adc6fbSStephen Hemminger 	 * convert them to network byte order
275463adc6fbSStephen Hemminger 	 */
27551da177e4SLinus Torvalds 	if (pgh) {
27561da177e4SLinus Torvalds 		struct timeval timestamp;
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds 		pgh->pgh_magic = htonl(PKTGEN_MAGIC);
27591da177e4SLinus Torvalds 		pgh->seq_num = htonl(pkt_dev->seq_num);
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 		do_gettimeofday(&timestamp);
27621da177e4SLinus Torvalds 		pgh->tv_sec = htonl(timestamp.tv_sec);
27631da177e4SLinus Torvalds 		pgh->tv_usec = htonl(timestamp.tv_usec);
27641da177e4SLinus Torvalds 	}
27651da177e4SLinus Torvalds 
2766a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2767a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2768a553e4a6SJamal Hadi Salim 		return NULL;
2769a553e4a6SJamal Hadi Salim #endif
2770a553e4a6SJamal Hadi Salim 
27711da177e4SLinus Torvalds 	return skb;
27721da177e4SLinus Torvalds }
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds /*
27751da177e4SLinus Torvalds  * scan_ip6, fmt_ip taken from dietlibc-0.21
27761da177e4SLinus Torvalds  * Author Felix von Leitner <felix-dietlibc@fefe.de>
27771da177e4SLinus Torvalds  *
27781da177e4SLinus Torvalds  * Slightly modified for kernel.
27791da177e4SLinus Torvalds  * Should be candidate for net/ipv4/utils.c
27801da177e4SLinus Torvalds  * --ro
27811da177e4SLinus Torvalds  */
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16])
27841da177e4SLinus Torvalds {
27851da177e4SLinus Torvalds 	unsigned int i;
27861da177e4SLinus Torvalds 	unsigned int len = 0;
27871da177e4SLinus Torvalds 	unsigned long u;
27881da177e4SLinus Torvalds 	char suffix[16];
27891da177e4SLinus Torvalds 	unsigned int prefixlen = 0;
27901da177e4SLinus Torvalds 	unsigned int suffixlen = 0;
2791252e3346SAl Viro 	__be32 tmp;
2792cfcabdccSStephen Hemminger 	char *pos;
27931da177e4SLinus Torvalds 
2794222f1806SLuiz Capitulino 	for (i = 0; i < 16; i++)
2795222f1806SLuiz Capitulino 		ip[i] = 0;
27961da177e4SLinus Torvalds 
27971da177e4SLinus Torvalds 	for (;;) {
27981da177e4SLinus Torvalds 		if (*s == ':') {
27991da177e4SLinus Torvalds 			len++;
28001da177e4SLinus Torvalds 			if (s[1] == ':') {	/* Found "::", skip to part 2 */
28011da177e4SLinus Torvalds 				s += 2;
28021da177e4SLinus Torvalds 				len++;
28031da177e4SLinus Torvalds 				break;
28041da177e4SLinus Torvalds 			}
28051da177e4SLinus Torvalds 			s++;
28061da177e4SLinus Torvalds 		}
28071da177e4SLinus Torvalds 
2808cfcabdccSStephen Hemminger 		u = simple_strtoul(s, &pos, 16);
2809cfcabdccSStephen Hemminger 		i = pos - s;
2810222f1806SLuiz Capitulino 		if (!i)
2811222f1806SLuiz Capitulino 			return 0;
28121da177e4SLinus Torvalds 		if (prefixlen == 12 && s[i] == '.') {
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds 			/* the last 4 bytes may be written as IPv4 address */
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 			tmp = in_aton(s);
28171da177e4SLinus Torvalds 			memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
28181da177e4SLinus Torvalds 			return i + len;
28191da177e4SLinus Torvalds 		}
28201da177e4SLinus Torvalds 		ip[prefixlen++] = (u >> 8);
28211da177e4SLinus Torvalds 		ip[prefixlen++] = (u & 255);
2822222f1806SLuiz Capitulino 		s += i;
2823222f1806SLuiz Capitulino 		len += i;
28241da177e4SLinus Torvalds 		if (prefixlen == 16)
28251da177e4SLinus Torvalds 			return len;
28261da177e4SLinus Torvalds 	}
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds /* part 2, after "::" */
28291da177e4SLinus Torvalds 	for (;;) {
28301da177e4SLinus Torvalds 		if (*s == ':') {
28311da177e4SLinus Torvalds 			if (suffixlen == 0)
28321da177e4SLinus Torvalds 				break;
28331da177e4SLinus Torvalds 			s++;
28341da177e4SLinus Torvalds 			len++;
28351da177e4SLinus Torvalds 		} else if (suffixlen != 0)
28361da177e4SLinus Torvalds 			break;
2837cfcabdccSStephen Hemminger 
2838cfcabdccSStephen Hemminger 		u = simple_strtol(s, &pos, 16);
2839cfcabdccSStephen Hemminger 		i = pos - s;
28401da177e4SLinus Torvalds 		if (!i) {
2841222f1806SLuiz Capitulino 			if (*s)
2842222f1806SLuiz Capitulino 				len--;
28431da177e4SLinus Torvalds 			break;
28441da177e4SLinus Torvalds 		}
28451da177e4SLinus Torvalds 		if (suffixlen + prefixlen <= 12 && s[i] == '.') {
28461da177e4SLinus Torvalds 			tmp = in_aton(s);
2847222f1806SLuiz Capitulino 			memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2848222f1806SLuiz Capitulino 			       sizeof(tmp));
28491da177e4SLinus Torvalds 			suffixlen += 4;
28501da177e4SLinus Torvalds 			len += strlen(s);
28511da177e4SLinus Torvalds 			break;
28521da177e4SLinus Torvalds 		}
28531da177e4SLinus Torvalds 		suffix[suffixlen++] = (u >> 8);
28541da177e4SLinus Torvalds 		suffix[suffixlen++] = (u & 255);
2855222f1806SLuiz Capitulino 		s += i;
2856222f1806SLuiz Capitulino 		len += i;
28571da177e4SLinus Torvalds 		if (prefixlen + suffixlen == 16)
28581da177e4SLinus Torvalds 			break;
28591da177e4SLinus Torvalds 	}
28601da177e4SLinus Torvalds 	for (i = 0; i < suffixlen; i++)
28611da177e4SLinus Torvalds 		ip[16 - suffixlen + i] = suffix[i];
28621da177e4SLinus Torvalds 	return len;
28631da177e4SLinus Torvalds }
28641da177e4SLinus Torvalds 
2865222f1806SLuiz Capitulino static char tohex(char hexdigit)
2866222f1806SLuiz Capitulino {
28671da177e4SLinus Torvalds 	return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
28681da177e4SLinus Torvalds }
28691da177e4SLinus Torvalds 
2870222f1806SLuiz Capitulino static int fmt_xlong(char *s, unsigned int i)
2871222f1806SLuiz Capitulino {
28721da177e4SLinus Torvalds 	char *bak = s;
2873222f1806SLuiz Capitulino 	*s = tohex((i >> 12) & 0xf);
2874222f1806SLuiz Capitulino 	if (s != bak || *s != '0')
2875222f1806SLuiz Capitulino 		++s;
2876222f1806SLuiz Capitulino 	*s = tohex((i >> 8) & 0xf);
2877222f1806SLuiz Capitulino 	if (s != bak || *s != '0')
2878222f1806SLuiz Capitulino 		++s;
2879222f1806SLuiz Capitulino 	*s = tohex((i >> 4) & 0xf);
2880222f1806SLuiz Capitulino 	if (s != bak || *s != '0')
2881222f1806SLuiz Capitulino 		++s;
28821da177e4SLinus Torvalds 	*s = tohex(i & 0xf);
28831da177e4SLinus Torvalds 	return s - bak + 1;
28841da177e4SLinus Torvalds }
28851da177e4SLinus Torvalds 
2886222f1806SLuiz Capitulino static unsigned int fmt_ip6(char *s, const char ip[16])
2887222f1806SLuiz Capitulino {
28881da177e4SLinus Torvalds 	unsigned int len;
28891da177e4SLinus Torvalds 	unsigned int i;
28901da177e4SLinus Torvalds 	unsigned int temp;
28911da177e4SLinus Torvalds 	unsigned int compressing;
28921da177e4SLinus Torvalds 	int j;
28931da177e4SLinus Torvalds 
2894222f1806SLuiz Capitulino 	len = 0;
2895222f1806SLuiz Capitulino 	compressing = 0;
28961da177e4SLinus Torvalds 	for (j = 0; j < 16; j += 2) {
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds #ifdef V4MAPPEDPREFIX
28991da177e4SLinus Torvalds 		if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
29001da177e4SLinus Torvalds 			inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
29011da177e4SLinus Torvalds 			temp = strlen(s);
29021da177e4SLinus Torvalds 			return len + temp;
29031da177e4SLinus Torvalds 		}
29041da177e4SLinus Torvalds #endif
29051da177e4SLinus Torvalds 		temp = ((unsigned long)(unsigned char)ip[j] << 8) +
29061da177e4SLinus Torvalds 		    (unsigned long)(unsigned char)ip[j + 1];
29071da177e4SLinus Torvalds 		if (temp == 0) {
29081da177e4SLinus Torvalds 			if (!compressing) {
29091da177e4SLinus Torvalds 				compressing = 1;
29101da177e4SLinus Torvalds 				if (j == 0) {
2911222f1806SLuiz Capitulino 					*s++ = ':';
2912222f1806SLuiz Capitulino 					++len;
29131da177e4SLinus Torvalds 				}
29141da177e4SLinus Torvalds 			}
29151da177e4SLinus Torvalds 		} else {
29161da177e4SLinus Torvalds 			if (compressing) {
29171da177e4SLinus Torvalds 				compressing = 0;
2918222f1806SLuiz Capitulino 				*s++ = ':';
2919222f1806SLuiz Capitulino 				++len;
29201da177e4SLinus Torvalds 			}
2921222f1806SLuiz Capitulino 			i = fmt_xlong(s, temp);
2922222f1806SLuiz Capitulino 			len += i;
2923222f1806SLuiz Capitulino 			s += i;
29241da177e4SLinus Torvalds 			if (j < 14) {
29251da177e4SLinus Torvalds 				*s++ = ':';
29261da177e4SLinus Torvalds 				++len;
29271da177e4SLinus Torvalds 			}
29281da177e4SLinus Torvalds 		}
29291da177e4SLinus Torvalds 	}
29301da177e4SLinus Torvalds 	if (compressing) {
2931222f1806SLuiz Capitulino 		*s++ = ':';
2932222f1806SLuiz Capitulino 		++len;
29331da177e4SLinus Torvalds 	}
29341da177e4SLinus Torvalds 	*s = 0;
29351da177e4SLinus Torvalds 	return len;
29361da177e4SLinus Torvalds }
29371da177e4SLinus Torvalds 
29381da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
29391da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
29401da177e4SLinus Torvalds {
29411da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
29421da177e4SLinus Torvalds 	__u8 *eth;
29431da177e4SLinus Torvalds 	struct udphdr *udph;
29441da177e4SLinus Torvalds 	int datalen;
29451da177e4SLinus Torvalds 	struct ipv6hdr *iph;
29461da177e4SLinus Torvalds 	struct pktgen_hdr *pgh = NULL;
2947d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2948ca6549afSSteven Whitehouse 	__be32 *mpls;
294934954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
295034954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
295134954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
295234954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2953fd2ea0a7SDavid S. Miller 	u16 queue_map;
2954ca6549afSSteven Whitehouse 
2955ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2956d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
29571da177e4SLinus Torvalds 
295834954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2959d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
296034954ddcSFrancesco Fondelli 
296164053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
296264053beeSRobert Olsson 	 * fields.
296364053beeSRobert Olsson 	 */
2964fd2ea0a7SDavid S. Miller 	queue_map = pkt_dev->cur_queue_map;
296564053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
296664053beeSRobert Olsson 
2967e470757dSStephen Hemminger 	skb = __netdev_alloc_skb(odev,
2968e470757dSStephen Hemminger 				 pkt_dev->cur_pkt_size + 64
2969e470757dSStephen Hemminger 				 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
29701da177e4SLinus Torvalds 	if (!skb) {
29711da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
29721da177e4SLinus Torvalds 		return NULL;
29731da177e4SLinus Torvalds 	}
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds 	skb_reserve(skb, 16);
29761da177e4SLinus Torvalds 
29771da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
29781da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2979ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2980ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2981ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
298234954ddcSFrancesco Fondelli 
298334954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
298434954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
298534954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29860f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
29870f37c605SAl Viro 					       pkt_dev->svlan_cfi,
29880f37c605SAl Viro 					       pkt_dev->svlan_p);
298934954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2990d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
299134954ddcSFrancesco Fondelli 		}
299234954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29930f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
29940f37c605SAl Viro 				      pkt_dev->vlan_cfi,
29950f37c605SAl Viro 				      pkt_dev->vlan_p);
299634954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2997d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
299834954ddcSFrancesco Fondelli 	}
299934954ddcSFrancesco Fondelli 
300027a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
3001b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
3002ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
3003fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
3004ddc7b8e3SArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
3005ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
30061da177e4SLinus Torvalds 
30071da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
3008252e3346SAl Viro 	*(__be16 *) &eth[12] = protocol;
30091da177e4SLinus Torvalds 
3010ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
3011ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
3012ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
301316dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds 	if (datalen < sizeof(struct pktgen_hdr)) {
30161da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
30171da177e4SLinus Torvalds 		if (net_ratelimit())
3018222f1806SLuiz Capitulino 			printk(KERN_INFO "pktgen: increased datalen to %d\n",
3019222f1806SLuiz Capitulino 			       datalen);
30201da177e4SLinus Torvalds 	}
30211da177e4SLinus Torvalds 
30221da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
30231da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
30241da177e4SLinus Torvalds 	udph->len = htons(datalen + sizeof(struct udphdr));
30251da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
30261da177e4SLinus Torvalds 
3027d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
30281da177e4SLinus Torvalds 
30291ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
30301ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
3031252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
30321ca7768cSFrancesco Fondelli 	}
30331ca7768cSFrancesco Fondelli 
30341da177e4SLinus Torvalds 	iph->hop_limit = 32;
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 	iph->payload_len = htons(sizeof(struct udphdr) + datalen);
30371da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds 	ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
30401da177e4SLinus Torvalds 	ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
30411da177e4SLinus Torvalds 
3042b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
304316dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
3044ca6549afSSteven Whitehouse 	skb->protocol = protocol;
30451da177e4SLinus Torvalds 	skb->dev = odev;
30461da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
30471da177e4SLinus Torvalds 
30481da177e4SLinus Torvalds 	if (pkt_dev->nfrags <= 0)
30491da177e4SLinus Torvalds 		pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
30501da177e4SLinus Torvalds 	else {
30511da177e4SLinus Torvalds 		int frags = pkt_dev->nfrags;
30521da177e4SLinus Torvalds 		int i;
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds 		pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
30551da177e4SLinus Torvalds 
30561da177e4SLinus Torvalds 		if (frags > MAX_SKB_FRAGS)
30571da177e4SLinus Torvalds 			frags = MAX_SKB_FRAGS;
30581da177e4SLinus Torvalds 		if (datalen > frags * PAGE_SIZE) {
30591da177e4SLinus Torvalds 			skb_put(skb, datalen - frags * PAGE_SIZE);
30601da177e4SLinus Torvalds 			datalen = frags * PAGE_SIZE;
30611da177e4SLinus Torvalds 		}
30621da177e4SLinus Torvalds 
30631da177e4SLinus Torvalds 		i = 0;
30641da177e4SLinus Torvalds 		while (datalen > 0) {
30651da177e4SLinus Torvalds 			struct page *page = alloc_pages(GFP_KERNEL, 0);
30661da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].page = page;
30671da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].page_offset = 0;
30681da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].size =
30691da177e4SLinus Torvalds 			    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
30701da177e4SLinus Torvalds 			datalen -= skb_shinfo(skb)->frags[i].size;
30711da177e4SLinus Torvalds 			skb->len += skb_shinfo(skb)->frags[i].size;
30721da177e4SLinus Torvalds 			skb->data_len += skb_shinfo(skb)->frags[i].size;
30731da177e4SLinus Torvalds 			i++;
30741da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags = i;
30751da177e4SLinus Torvalds 		}
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds 		while (i < frags) {
30781da177e4SLinus Torvalds 			int rem;
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds 			if (i == 0)
30811da177e4SLinus Torvalds 				break;
30821da177e4SLinus Torvalds 
30831da177e4SLinus Torvalds 			rem = skb_shinfo(skb)->frags[i - 1].size / 2;
30841da177e4SLinus Torvalds 			if (rem == 0)
30851da177e4SLinus Torvalds 				break;
30861da177e4SLinus Torvalds 
30871da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i - 1].size -= rem;
30881da177e4SLinus Torvalds 
3089222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i] =
3090222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1];
30911da177e4SLinus Torvalds 			get_page(skb_shinfo(skb)->frags[i].page);
3092222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i].page =
3093222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1].page;
3094222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i].page_offset +=
3095222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1].size;
30961da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].size = rem;
30971da177e4SLinus Torvalds 			i++;
30981da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags = i;
30991da177e4SLinus Torvalds 		}
31001da177e4SLinus Torvalds 	}
31011da177e4SLinus Torvalds 
310263adc6fbSStephen Hemminger 	/* Stamp the time, and sequence number,
310363adc6fbSStephen Hemminger 	 * convert them to network byte order
310463adc6fbSStephen Hemminger 	 * should we update cloned packets too ?
310563adc6fbSStephen Hemminger 	 */
31061da177e4SLinus Torvalds 	if (pgh) {
31071da177e4SLinus Torvalds 		struct timeval timestamp;
31081da177e4SLinus Torvalds 
31091da177e4SLinus Torvalds 		pgh->pgh_magic = htonl(PKTGEN_MAGIC);
31101da177e4SLinus Torvalds 		pgh->seq_num = htonl(pkt_dev->seq_num);
31111da177e4SLinus Torvalds 
31121da177e4SLinus Torvalds 		do_gettimeofday(&timestamp);
31131da177e4SLinus Torvalds 		pgh->tv_sec = htonl(timestamp.tv_sec);
31141da177e4SLinus Torvalds 		pgh->tv_usec = htonl(timestamp.tv_usec);
31151da177e4SLinus Torvalds 	}
311634954ddcSFrancesco Fondelli 	/* pkt_dev->seq_num++; FF: you really mean this? */
31171da177e4SLinus Torvalds 
31181da177e4SLinus Torvalds 	return skb;
31191da177e4SLinus Torvalds }
31201da177e4SLinus Torvalds 
3121475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
31221da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
31231da177e4SLinus Torvalds {
31241da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
31251da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
31261da177e4SLinus Torvalds 	else
31271da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
31281da177e4SLinus Torvalds }
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
31311da177e4SLinus Torvalds {
31321da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
31331da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
31341da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
31351da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
31361da177e4SLinus Torvalds 	pkt_dev->errors = 0;
31371da177e4SLinus Torvalds }
31381da177e4SLinus Torvalds 
31391da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
31401da177e4SLinus Torvalds 
31411da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
31421da177e4SLinus Torvalds {
3143c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
31441da177e4SLinus Torvalds 	int started = 0;
31451da177e4SLinus Torvalds 
314625c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_run. %p\n", t);
31471da177e4SLinus Torvalds 
31481da177e4SLinus Torvalds 	if_lock(t);
3149c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
31501da177e4SLinus Torvalds 
31511da177e4SLinus Torvalds 		/*
31521da177e4SLinus Torvalds 		 * setup odev and create initial packet.
31531da177e4SLinus Torvalds 		 */
31541da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
31551da177e4SLinus Torvalds 
31561da177e4SLinus Torvalds 		if (pkt_dev->odev) {
31571da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
31581da177e4SLinus Torvalds 			pkt_dev->running = 1;	/* Cranke yeself! */
31591da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
3160fd29cf72SStephen Hemminger 			pkt_dev->started_at =
3161fd29cf72SStephen Hemminger 				pkt_dev->next_tx = ktime_now();
3162fd29cf72SStephen Hemminger 
316316dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
31661da177e4SLinus Torvalds 			started++;
3167222f1806SLuiz Capitulino 		} else
31681da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
31691da177e4SLinus Torvalds 	}
31701da177e4SLinus Torvalds 	if_unlock(t);
3171222f1806SLuiz Capitulino 	if (started)
3172222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
31731da177e4SLinus Torvalds }
31741da177e4SLinus Torvalds 
31751da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void)
31761da177e4SLinus Torvalds {
3177cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31781da177e4SLinus Torvalds 
317925c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
31801da177e4SLinus Torvalds 
31816146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3182cdcdbe0bSLuiz Capitulino 
3183cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list)
318495ed63f7SArthur Kepner 		t->control |= T_STOP;
3185cdcdbe0bSLuiz Capitulino 
31866146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31871da177e4SLinus Torvalds }
31881da177e4SLinus Torvalds 
3189648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
31901da177e4SLinus Torvalds {
3191648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
31921da177e4SLinus Torvalds 
3193c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
3194648fda74SStephen Hemminger 		if (pkt_dev->running)
3195648fda74SStephen Hemminger 			return 1;
3196648fda74SStephen Hemminger 	return 0;
31971da177e4SLinus Torvalds }
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
32001da177e4SLinus Torvalds {
32011da177e4SLinus Torvalds 	if_lock(t);
32021da177e4SLinus Torvalds 
32031da177e4SLinus Torvalds 	while (thread_is_running(t)) {
32041da177e4SLinus Torvalds 
32051da177e4SLinus Torvalds 		if_unlock(t);
32061da177e4SLinus Torvalds 
32071da177e4SLinus Torvalds 		msleep_interruptible(100);
32081da177e4SLinus Torvalds 
32091da177e4SLinus Torvalds 		if (signal_pending(current))
32101da177e4SLinus Torvalds 			goto signal;
32111da177e4SLinus Torvalds 		if_lock(t);
32121da177e4SLinus Torvalds 	}
32131da177e4SLinus Torvalds 	if_unlock(t);
32141da177e4SLinus Torvalds 	return 1;
32151da177e4SLinus Torvalds signal:
32161da177e4SLinus Torvalds 	return 0;
32171da177e4SLinus Torvalds }
32181da177e4SLinus Torvalds 
32191da177e4SLinus Torvalds static int pktgen_wait_all_threads_run(void)
32201da177e4SLinus Torvalds {
3221cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
32221da177e4SLinus Torvalds 	int sig = 1;
32231da177e4SLinus Torvalds 
32246146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3225cdcdbe0bSLuiz Capitulino 
3226cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list) {
32271da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
3228222f1806SLuiz Capitulino 		if (sig == 0)
3229222f1806SLuiz Capitulino 			break;
32301da177e4SLinus Torvalds 	}
3231cdcdbe0bSLuiz Capitulino 
3232cdcdbe0bSLuiz Capitulino 	if (sig == 0)
3233cdcdbe0bSLuiz Capitulino 		list_for_each_entry(t, &pktgen_threads, th_list)
32341da177e4SLinus Torvalds 			t->control |= (T_STOP);
3235cdcdbe0bSLuiz Capitulino 
32366146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
32371da177e4SLinus Torvalds 	return sig;
32381da177e4SLinus Torvalds }
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds static void pktgen_run_all_threads(void)
32411da177e4SLinus Torvalds {
3242cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
32431da177e4SLinus Torvalds 
324425c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_run_all_threads.\n");
32451da177e4SLinus Torvalds 
32466146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
32471da177e4SLinus Torvalds 
3248cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list)
32491da177e4SLinus Torvalds 		t->control |= (T_RUN);
3250cdcdbe0bSLuiz Capitulino 
32516146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
32521da177e4SLinus Torvalds 
325363adc6fbSStephen Hemminger 	/* Propagate thread->control  */
325463adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
32551da177e4SLinus Torvalds 
32561da177e4SLinus Torvalds 	pktgen_wait_all_threads_run();
32571da177e4SLinus Torvalds }
32581da177e4SLinus Torvalds 
3259eb37b41cSJesse Brandeburg static void pktgen_reset_all_threads(void)
3260eb37b41cSJesse Brandeburg {
3261eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3262eb37b41cSJesse Brandeburg 
3263eb37b41cSJesse Brandeburg 	pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3264eb37b41cSJesse Brandeburg 
3265eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3266eb37b41cSJesse Brandeburg 
3267eb37b41cSJesse Brandeburg 	list_for_each_entry(t, &pktgen_threads, th_list)
3268eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3269eb37b41cSJesse Brandeburg 
3270eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3271eb37b41cSJesse Brandeburg 
327263adc6fbSStephen Hemminger 	/* Propagate thread->control  */
327363adc6fbSStephen Hemminger 	schedule_timeout_interruptible(msecs_to_jiffies(125));
3274eb37b41cSJesse Brandeburg 
3275eb37b41cSJesse Brandeburg 	pktgen_wait_all_threads_run();
3276eb37b41cSJesse Brandeburg }
3277eb37b41cSJesse Brandeburg 
32781da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
32791da177e4SLinus Torvalds {
3280fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
32811da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3282fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3283fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3284fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
32851da177e4SLinus Torvalds 
3286fd29cf72SStephen Hemminger 	p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3287fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3288fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3289fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
32901da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
32911da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
32921da177e4SLinus Torvalds 
3293fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3294fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
32951da177e4SLinus Torvalds 
32961da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
32971da177e4SLinus Torvalds 
32981da177e4SLinus Torvalds 	mbps = bps;
32991da177e4SLinus Torvalds 	do_div(mbps, 1000000);
33001da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
33011da177e4SLinus Torvalds 		     (unsigned long long)pps,
33021da177e4SLinus Torvalds 		     (unsigned long long)mbps,
33031da177e4SLinus Torvalds 		     (unsigned long long)bps,
33041da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
33051da177e4SLinus Torvalds }
33061da177e4SLinus Torvalds 
33071da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
33081da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
33091da177e4SLinus Torvalds {
3310222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
33111da177e4SLinus Torvalds 
33121da177e4SLinus Torvalds 	if (!pkt_dev->running) {
331325a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: interface: %s is already "
3314593f63b0SEric Dumazet 		       "stopped\n", pkt_dev->odevname);
33151da177e4SLinus Torvalds 		return -EINVAL;
33161da177e4SLinus Torvalds 	}
33171da177e4SLinus Torvalds 
33183bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
33193bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3320fd29cf72SStephen Hemminger 	pkt_dev->stopped_at = ktime_now();
33211da177e4SLinus Torvalds 	pkt_dev->running = 0;
33221da177e4SLinus Torvalds 
332395ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
33241da177e4SLinus Torvalds 
33251da177e4SLinus Torvalds 	return 0;
33261da177e4SLinus Torvalds }
33271da177e4SLinus Torvalds 
33281da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
33291da177e4SLinus Torvalds {
3330c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
33311da177e4SLinus Torvalds 
33321da177e4SLinus Torvalds 	if_lock(t);
33331da177e4SLinus Torvalds 
3334c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3335c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3336222f1806SLuiz Capitulino 			continue;
3337222f1806SLuiz Capitulino 		if (best == NULL)
3338c26a8016SLuiz Capitulino 			best = pkt_dev;
3339fd29cf72SStephen Hemminger 		else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
3340c26a8016SLuiz Capitulino 			best = pkt_dev;
33411da177e4SLinus Torvalds 	}
33421da177e4SLinus Torvalds 	if_unlock(t);
33431da177e4SLinus Torvalds 	return best;
33441da177e4SLinus Torvalds }
33451da177e4SLinus Torvalds 
3346222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3347222f1806SLuiz Capitulino {
3348c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
33491da177e4SLinus Torvalds 
335025c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_stop\n");
33511da177e4SLinus Torvalds 
33521da177e4SLinus Torvalds 	if_lock(t);
33531da177e4SLinus Torvalds 
3354c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3355c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
335695ed63f7SArthur Kepner 	}
335795ed63f7SArthur Kepner 
335895ed63f7SArthur Kepner 	if_unlock(t);
335995ed63f7SArthur Kepner }
336095ed63f7SArthur Kepner 
336195ed63f7SArthur Kepner /*
336295ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
336395ed63f7SArthur Kepner  * and remove it
336495ed63f7SArthur Kepner  */
336595ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
336695ed63f7SArthur Kepner {
3367c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3368c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
336995ed63f7SArthur Kepner 
337025c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_rem_one_if\n");
337195ed63f7SArthur Kepner 
337295ed63f7SArthur Kepner 	if_lock(t);
337395ed63f7SArthur Kepner 
3374c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3375c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
337695ed63f7SArthur Kepner 
3377222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3378222f1806SLuiz Capitulino 			continue;
337995ed63f7SArthur Kepner 
338095ed63f7SArthur Kepner 		kfree_skb(cur->skb);
338195ed63f7SArthur Kepner 		cur->skb = NULL;
338295ed63f7SArthur Kepner 
338395ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
338495ed63f7SArthur Kepner 
338595ed63f7SArthur Kepner 		break;
338695ed63f7SArthur Kepner 	}
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds 	if_unlock(t);
33891da177e4SLinus Torvalds }
33901da177e4SLinus Torvalds 
33911da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
33921da177e4SLinus Torvalds {
3393c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3394c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds 	/* Remove all devices, free mem */
33971da177e4SLinus Torvalds 
339825c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
33991da177e4SLinus Torvalds 	if_lock(t);
34001da177e4SLinus Torvalds 
3401c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3402c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
340395ed63f7SArthur Kepner 
340495ed63f7SArthur Kepner 		kfree_skb(cur->skb);
340595ed63f7SArthur Kepner 		cur->skb = NULL;
340695ed63f7SArthur Kepner 
34071da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
34081da177e4SLinus Torvalds 	}
34091da177e4SLinus Torvalds 
34101da177e4SLinus Torvalds 	if_unlock(t);
34111da177e4SLinus Torvalds }
34121da177e4SLinus Torvalds 
34131da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
34141da177e4SLinus Torvalds {
34151da177e4SLinus Torvalds 	/* Remove from the thread list */
34161da177e4SLinus Torvalds 
3417ee74baa7SDavid S. Miller 	remove_proc_entry(t->tsk->comm, pg_proc_dir);
34181da177e4SLinus Torvalds 
34196146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
34201da177e4SLinus Torvalds 
3421cdcdbe0bSLuiz Capitulino 	list_del(&t->th_list);
3422cdcdbe0bSLuiz Capitulino 
34236146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
34241da177e4SLinus Torvalds }
34251da177e4SLinus Torvalds 
3426ef87979cSStephen Hemminger static void pktgen_resched(struct pktgen_dev *pkt_dev)
34273791decbSStephen Hemminger {
3428fd29cf72SStephen Hemminger 	ktime_t idle_start = ktime_now();
34293791decbSStephen Hemminger 	schedule();
3430fd29cf72SStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
34313791decbSStephen Hemminger }
34323791decbSStephen Hemminger 
3433ef87979cSStephen Hemminger static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3434ef87979cSStephen Hemminger {
3435ef87979cSStephen Hemminger 	ktime_t idle_start = ktime_now();
3436ef87979cSStephen Hemminger 
3437ef87979cSStephen Hemminger 	while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3438ef87979cSStephen Hemminger 		if (signal_pending(current))
3439ef87979cSStephen Hemminger 			break;
3440ef87979cSStephen Hemminger 
3441ef87979cSStephen Hemminger 		if (need_resched())
3442ef87979cSStephen Hemminger 			pktgen_resched(pkt_dev);
3443ef87979cSStephen Hemminger 		else
3444ef87979cSStephen Hemminger 			cpu_relax();
3445ef87979cSStephen Hemminger 	}
3446ef87979cSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3447ef87979cSStephen Hemminger }
3448fd29cf72SStephen Hemminger 
3449475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
34501da177e4SLinus Torvalds {
345100829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
34526fef4c0cSStephen Hemminger 	netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
345300829823SStephen Hemminger 		= odev->netdev_ops->ndo_start_xmit;
3454fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
3455fd2ea0a7SDavid S. Miller 	u16 queue_map;
34561da177e4SLinus Torvalds 	int ret;
34571da177e4SLinus Torvalds 
3458ef87979cSStephen Hemminger 	/* If device is offline, then don't send */
3459ef87979cSStephen Hemminger 	if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
34601da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
34613791decbSStephen Hemminger 		return;
34621da177e4SLinus Torvalds 	}
34631da177e4SLinus Torvalds 
3464ef87979cSStephen Hemminger 	/* This is max DELAY, this has special meaning of
3465ef87979cSStephen Hemminger 	 * "never transmit"
3466ef87979cSStephen Hemminger 	 */
3467ef87979cSStephen Hemminger 	if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3468ef87979cSStephen Hemminger 		pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3469ef87979cSStephen Hemminger 		return;
3470ef87979cSStephen Hemminger 	}
3471ef87979cSStephen Hemminger 
3472ef87979cSStephen Hemminger 	/* If no skb or clone count exhausted then get new one */
34737d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
34747d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
34751da177e4SLinus Torvalds 		/* build a new pkt */
34761da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
34771da177e4SLinus Torvalds 
34781da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
34791da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
348025a8b254SDavid S. Miller 			printk(KERN_ERR "pktgen: ERROR: couldn't "
348125a8b254SDavid S. Miller 			       "allocate skb in fill_packet.\n");
34821da177e4SLinus Torvalds 			schedule();
34831da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
34843791decbSStephen Hemminger 			return;
34851da177e4SLinus Torvalds 		}
3486baac8564SEric Dumazet 		pkt_dev->last_pkt_size = pkt_dev->skb->len;
34871da177e4SLinus Torvalds 		pkt_dev->allocated_skbs++;
34881da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
34891da177e4SLinus Torvalds 	}
34901da177e4SLinus Torvalds 
3491ef87979cSStephen Hemminger 	if (pkt_dev->delay && pkt_dev->last_ok)
3492ef87979cSStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
3493ef87979cSStephen Hemminger 
3494fd2ea0a7SDavid S. Miller 	queue_map = skb_get_queue_mapping(pkt_dev->skb);
3495fd2ea0a7SDavid S. Miller 	txq = netdev_get_tx_queue(odev, queue_map);
3496fd2ea0a7SDavid S. Miller 
3497fd2ea0a7SDavid S. Miller 	__netif_tx_lock_bh(txq);
34985b8db2f5SStephen Hemminger 
34990835acfeSEric Dumazet 	if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq))) {
3500ef87979cSStephen Hemminger 		ret = NETDEV_TX_BUSY;
35010835acfeSEric Dumazet 		pkt_dev->last_ok = 0;
35020835acfeSEric Dumazet 		goto unlock;
35030835acfeSEric Dumazet 	}
35040835acfeSEric Dumazet 	atomic_inc(&(pkt_dev->skb->users));
350500829823SStephen Hemminger 	ret = (*xmit)(pkt_dev->skb, odev);
3506ef87979cSStephen Hemminger 
35075b8db2f5SStephen Hemminger 	switch (ret) {
35085b8db2f5SStephen Hemminger 	case NETDEV_TX_OK:
350908baf561SEric Dumazet 		txq_trans_update(txq);
35101da177e4SLinus Torvalds 		pkt_dev->last_ok = 1;
35111da177e4SLinus Torvalds 		pkt_dev->sofar++;
35121da177e4SLinus Torvalds 		pkt_dev->seq_num++;
3513baac8564SEric Dumazet 		pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
35145b8db2f5SStephen Hemminger 		break;
3515f466dba1SJohn Fastabend 	case NET_XMIT_DROP:
3516f466dba1SJohn Fastabend 	case NET_XMIT_CN:
3517f466dba1SJohn Fastabend 	case NET_XMIT_POLICED:
3518f466dba1SJohn Fastabend 		/* skb has been consumed */
3519f466dba1SJohn Fastabend 		pkt_dev->errors++;
3520f466dba1SJohn Fastabend 		break;
35215b8db2f5SStephen Hemminger 	default: /* Drivers are not supposed to return other values! */
35225b8db2f5SStephen Hemminger 		if (net_ratelimit())
35235b8db2f5SStephen Hemminger 			pr_info("pktgen: %s xmit error: %d\n",
3524593f63b0SEric Dumazet 				pkt_dev->odevname, ret);
35251da177e4SLinus Torvalds 		pkt_dev->errors++;
35265b8db2f5SStephen Hemminger 		/* fallthru */
3527ef87979cSStephen Hemminger 	case NETDEV_TX_LOCKED:
35285b8db2f5SStephen Hemminger 	case NETDEV_TX_BUSY:
35295b8db2f5SStephen Hemminger 		/* Retry it next time */
35305b8db2f5SStephen Hemminger 		atomic_dec(&(pkt_dev->skb->users));
35311da177e4SLinus Torvalds 		pkt_dev->last_ok = 0;
35321da177e4SLinus Torvalds 	}
35330835acfeSEric Dumazet unlock:
3534fd2ea0a7SDavid S. Miller 	__netif_tx_unlock_bh(txq);
35351da177e4SLinus Torvalds 
35361da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
35371da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3538ef87979cSStephen Hemminger 		pktgen_wait_for_skb(pkt_dev);
35391da177e4SLinus Torvalds 
35401da177e4SLinus Torvalds 		/* Done with this */
35411da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
35421da177e4SLinus Torvalds 	}
35431da177e4SLinus Torvalds }
35441da177e4SLinus Torvalds 
35451da177e4SLinus Torvalds /*
35461da177e4SLinus Torvalds  * Main loop of the thread goes here
35471da177e4SLinus Torvalds  */
35481da177e4SLinus Torvalds 
3549ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
35501da177e4SLinus Torvalds {
35511da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3552ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
35531da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
35541da177e4SLinus Torvalds 	int cpu = t->cpu;
35551da177e4SLinus Torvalds 
3556ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3559d3ede327SDenis V. Lunev 	complete(&t->start_done);
35601da177e4SLinus Torvalds 
356163adc6fbSStephen Hemminger 	pr_debug("pktgen: starting pktgen/%d:  pid=%d\n",
356263adc6fbSStephen Hemminger 		 cpu, task_pid_nr(current));
35631da177e4SLinus Torvalds 
3564ee74baa7SDavid S. Miller 	set_current_state(TASK_INTERRUPTIBLE);
35651da177e4SLinus Torvalds 
356683144186SRafael J. Wysocki 	set_freezable();
356783144186SRafael J. Wysocki 
3568ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3569ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3570ee74baa7SDavid S. Miller 
3571ef87979cSStephen Hemminger 		if (unlikely(!pkt_dev && t->control == 0)) {
3572ef87979cSStephen Hemminger 			wait_event_interruptible_timeout(t->queue,
3573ef87979cSStephen Hemminger 							 t->control != 0,
3574ef87979cSStephen Hemminger 							 HZ/10);
35751b3f720bSRafael J. Wysocki 			try_to_freeze();
3576ef87979cSStephen Hemminger 			continue;
3577ee74baa7SDavid S. Miller 		}
35781da177e4SLinus Torvalds 
35791da177e4SLinus Torvalds 		__set_current_state(TASK_RUNNING);
35801da177e4SLinus Torvalds 
3581ef87979cSStephen Hemminger 		if (likely(pkt_dev)) {
35821da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
35831da177e4SLinus Torvalds 
3584ef87979cSStephen Hemminger 			if (need_resched())
3585ef87979cSStephen Hemminger 				pktgen_resched(pkt_dev);
3586ef87979cSStephen Hemminger 			else
3587ef87979cSStephen Hemminger 				cpu_relax();
3588ef87979cSStephen Hemminger 		}
3589ef87979cSStephen Hemminger 
35901da177e4SLinus Torvalds 		if (t->control & T_STOP) {
35911da177e4SLinus Torvalds 			pktgen_stop(t);
35921da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
35931da177e4SLinus Torvalds 		}
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds 		if (t->control & T_RUN) {
35961da177e4SLinus Torvalds 			pktgen_run(t);
35971da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
35981da177e4SLinus Torvalds 		}
35991da177e4SLinus Torvalds 
360095ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
36011da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
360295ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
360395ed63f7SArthur Kepner 		}
360495ed63f7SArthur Kepner 
360595ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
360695ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
36071da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
36081da177e4SLinus Torvalds 		}
36091da177e4SLinus Torvalds 
361009fe3ef4SAndrew Morton 		try_to_freeze();
361109fe3ef4SAndrew Morton 
3612ee74baa7SDavid S. Miller 		set_current_state(TASK_INTERRUPTIBLE);
36131da177e4SLinus Torvalds 	}
36141da177e4SLinus Torvalds 
361525c4e53aSStephen Hemminger 	pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
36161da177e4SLinus Torvalds 	pktgen_stop(t);
36171da177e4SLinus Torvalds 
361825c4e53aSStephen Hemminger 	pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
36191da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
36201da177e4SLinus Torvalds 
362125c4e53aSStephen Hemminger 	pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
36221da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3623cdcdbe0bSLuiz Capitulino 
3624ee74baa7SDavid S. Miller 	return 0;
36251da177e4SLinus Torvalds }
36261da177e4SLinus Torvalds 
3627222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
36283e984840SEric Dumazet 					  const char *ifname, bool exact)
36291da177e4SLinus Torvalds {
3630c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
36313e984840SEric Dumazet 	size_t len = strlen(ifname);
36321da177e4SLinus Torvalds 
36333e984840SEric Dumazet 	if_lock(t);
3634c26a8016SLuiz Capitulino 	list_for_each_entry(p, &t->if_list, list)
36353e984840SEric Dumazet 		if (strncmp(p->odevname, ifname, len) == 0) {
36363e984840SEric Dumazet 			if (p->odevname[len]) {
36373e984840SEric Dumazet 				if (exact || p->odevname[len] != '@')
36383e984840SEric Dumazet 					continue;
36393e984840SEric Dumazet 			}
3640c26a8016SLuiz Capitulino 			pkt_dev = p;
36411da177e4SLinus Torvalds 			break;
36421da177e4SLinus Torvalds 		}
36431da177e4SLinus Torvalds 
36441da177e4SLinus Torvalds 	if_unlock(t);
364525c4e53aSStephen Hemminger 	pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
36461da177e4SLinus Torvalds 	return pkt_dev;
36471da177e4SLinus Torvalds }
36481da177e4SLinus Torvalds 
36491da177e4SLinus Torvalds /*
36501da177e4SLinus Torvalds  * Adds a dev at front of if_list.
36511da177e4SLinus Torvalds  */
36521da177e4SLinus Torvalds 
3653222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3654222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
36551da177e4SLinus Torvalds {
36561da177e4SLinus Torvalds 	int rv = 0;
36571da177e4SLinus Torvalds 
36581da177e4SLinus Torvalds 	if_lock(t);
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
366125a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: already assigned "
366225a8b254SDavid S. Miller 		       "to a thread.\n");
36631da177e4SLinus Torvalds 		rv = -EBUSY;
36641da177e4SLinus Torvalds 		goto out;
36651da177e4SLinus Torvalds 	}
3666c26a8016SLuiz Capitulino 
3667c26a8016SLuiz Capitulino 	list_add(&pkt_dev->list, &t->if_list);
36681da177e4SLinus Torvalds 	pkt_dev->pg_thread = t;
36691da177e4SLinus Torvalds 	pkt_dev->running = 0;
36701da177e4SLinus Torvalds 
36711da177e4SLinus Torvalds out:
36721da177e4SLinus Torvalds 	if_unlock(t);
36731da177e4SLinus Torvalds 	return rv;
36741da177e4SLinus Torvalds }
36751da177e4SLinus Torvalds 
36761da177e4SLinus Torvalds /* Called under thread lock */
36771da177e4SLinus Torvalds 
36781da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
36791da177e4SLinus Torvalds {
36801da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
368139df232fSStephen Hemminger 	int err;
36823291b9dbSEric Dumazet 	int node = cpu_to_node(t->cpu);
36831da177e4SLinus Torvalds 
36841da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
36851da177e4SLinus Torvalds 
3686d50a6b56SStephen Hemminger 	pkt_dev = __pktgen_NN_threads(ifname, FIND);
3687d50a6b56SStephen Hemminger 	if (pkt_dev) {
368825a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3689d50a6b56SStephen Hemminger 		return -EBUSY;
3690d50a6b56SStephen Hemminger 	}
36911da177e4SLinus Torvalds 
36923291b9dbSEric Dumazet 	pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
36931da177e4SLinus Torvalds 	if (!pkt_dev)
36941da177e4SLinus Torvalds 		return -ENOMEM;
36951da177e4SLinus Torvalds 
3696593f63b0SEric Dumazet 	strcpy(pkt_dev->odevname, ifname);
36973291b9dbSEric Dumazet 	pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
36983291b9dbSEric Dumazet 				      node);
36991da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
37001da177e4SLinus Torvalds 		kfree(pkt_dev);
37011da177e4SLinus Torvalds 		return -ENOMEM;
37021da177e4SLinus Torvalds 	}
37031da177e4SLinus Torvalds 	memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
37041da177e4SLinus Torvalds 
370595ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
37061da177e4SLinus Torvalds 	pkt_dev->min_pkt_size = ETH_ZLEN;
37071da177e4SLinus Torvalds 	pkt_dev->max_pkt_size = ETH_ZLEN;
37081da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
37091da177e4SLinus Torvalds 	pkt_dev->clone_skb = pg_clone_skb_d;
3710fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
37111da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
37121da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
37131da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
37141da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
37151da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
37161da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
37171da177e4SLinus Torvalds 
371834954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
371934954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
372034954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
372134954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
372234954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
372334954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
3724e99b99b4SRobert Olsson 	pkt_dev->node = -1;
372534954ddcSFrancesco Fondelli 
372639df232fSStephen Hemminger 	err = pktgen_setup_dev(pkt_dev, ifname);
372739df232fSStephen Hemminger 	if (err)
372839df232fSStephen Hemminger 		goto out1;
37291da177e4SLinus Torvalds 
37305efdccbcSDenis V. Lunev 	pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
37315efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
373239df232fSStephen Hemminger 	if (!pkt_dev->entry) {
373325a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3734d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
373539df232fSStephen Hemminger 		err = -EINVAL;
373639df232fSStephen Hemminger 		goto out2;
373739df232fSStephen Hemminger 	}
3738a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3739a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3740a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3741a553e4a6SJamal Hadi Salim #endif
374239df232fSStephen Hemminger 
374339df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
374439df232fSStephen Hemminger out2:
374539df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
374639df232fSStephen Hemminger out1:
3747a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3748a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3749a553e4a6SJamal Hadi Salim #endif
37501da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
37511da177e4SLinus Torvalds 	kfree(pkt_dev);
375239df232fSStephen Hemminger 	return err;
37531da177e4SLinus Torvalds }
37541da177e4SLinus Torvalds 
3755ee74baa7SDavid S. Miller static int __init pktgen_create_thread(int cpu)
37561da177e4SLinus Torvalds {
3757cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3758d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3759ee74baa7SDavid S. Miller 	struct task_struct *p;
37601da177e4SLinus Torvalds 
37613291b9dbSEric Dumazet 	t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
37623291b9dbSEric Dumazet 			 cpu_to_node(cpu));
37631da177e4SLinus Torvalds 	if (!t) {
376425a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
376525a8b254SDavid S. Miller 		       "create new thread.\n");
37661da177e4SLinus Torvalds 		return -ENOMEM;
37671da177e4SLinus Torvalds 	}
37681da177e4SLinus Torvalds 
37691da177e4SLinus Torvalds 	spin_lock_init(&t->if_lock);
37701da177e4SLinus Torvalds 	t->cpu = cpu;
37711da177e4SLinus Torvalds 
3772ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3773ee74baa7SDavid S. Miller 
3774ee74baa7SDavid S. Miller 	list_add_tail(&t->th_list, &pktgen_threads);
3775d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3776ee74baa7SDavid S. Miller 
3777ee74baa7SDavid S. Miller 	p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3778ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
377925a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: kernel_thread() failed "
378025a8b254SDavid S. Miller 		       "for cpu %d\n", t->cpu);
3781ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3782ee74baa7SDavid S. Miller 		kfree(t);
3783ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3784ee74baa7SDavid S. Miller 	}
3785ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3786ee74baa7SDavid S. Miller 	t->tsk = p;
3787ee74baa7SDavid S. Miller 
37885efdccbcSDenis V. Lunev 	pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
37895efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3790d50a6b56SStephen Hemminger 	if (!pe) {
379125a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3792ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3793ee74baa7SDavid S. Miller 		kthread_stop(p);
3794ee74baa7SDavid S. Miller 		list_del(&t->th_list);
37951da177e4SLinus Torvalds 		kfree(t);
37961da177e4SLinus Torvalds 		return -EINVAL;
37971da177e4SLinus Torvalds 	}
3798d50a6b56SStephen Hemminger 
3799ee74baa7SDavid S. Miller 	wake_up_process(p);
3800d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
38011da177e4SLinus Torvalds 
38021da177e4SLinus Torvalds 	return 0;
38031da177e4SLinus Torvalds }
38041da177e4SLinus Torvalds 
38051da177e4SLinus Torvalds /*
38061da177e4SLinus Torvalds  * Removes a device from the thread if_list.
38071da177e4SLinus Torvalds  */
3808222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3809222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
38101da177e4SLinus Torvalds {
3811c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3812c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
38131da177e4SLinus Torvalds 
3814c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3815c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3816c26a8016SLuiz Capitulino 		if (p == pkt_dev)
3817c26a8016SLuiz Capitulino 			list_del(&p->list);
38181da177e4SLinus Torvalds 	}
38191da177e4SLinus Torvalds }
38201da177e4SLinus Torvalds 
3821222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3822222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
38231da177e4SLinus Torvalds {
38241da177e4SLinus Torvalds 
382525c4e53aSStephen Hemminger 	pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
38261da177e4SLinus Torvalds 
38271da177e4SLinus Torvalds 	if (pkt_dev->running) {
382825a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
382925a8b254SDavid S. Miller 		       "running interface, stopping it now.\n");
38301da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
38311da177e4SLinus Torvalds 	}
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds 	/* Dis-associate from the interface */
38341da177e4SLinus Torvalds 
38351da177e4SLinus Torvalds 	if (pkt_dev->odev) {
38361da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
38371da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
38381da177e4SLinus Torvalds 	}
38391da177e4SLinus Torvalds 
38401da177e4SLinus Torvalds 	/* And update the thread if_list */
38411da177e4SLinus Torvalds 
38421da177e4SLinus Torvalds 	_rem_dev_from_if_list(t, pkt_dev);
38431da177e4SLinus Torvalds 
384439df232fSStephen Hemminger 	if (pkt_dev->entry)
384539df232fSStephen Hemminger 		remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
38461da177e4SLinus Torvalds 
3847a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3848a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3849a553e4a6SJamal Hadi Salim #endif
38501da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
38511da177e4SLinus Torvalds 	kfree(pkt_dev);
38521da177e4SLinus Torvalds 	return 0;
38531da177e4SLinus Torvalds }
38541da177e4SLinus Torvalds 
38551da177e4SLinus Torvalds static int __init pg_init(void)
38561da177e4SLinus Torvalds {
38571da177e4SLinus Torvalds 	int cpu;
3858d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3859d50a6b56SStephen Hemminger 
386025a8b254SDavid S. Miller 	printk(KERN_INFO "%s", version);
38611da177e4SLinus Torvalds 
3862457c4cbcSEric W. Biederman 	pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3863d50a6b56SStephen Hemminger 	if (!pg_proc_dir)
3864d50a6b56SStephen Hemminger 		return -ENODEV;
38651da177e4SLinus Torvalds 
386625296d59SWang Chen 	pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3867d50a6b56SStephen Hemminger 	if (pe == NULL) {
386825a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: cannot create %s "
386925a8b254SDavid S. Miller 		       "procfs entry.\n", PGCTRL);
3870457c4cbcSEric W. Biederman 		proc_net_remove(&init_net, PG_PROC_DIR);
38711da177e4SLinus Torvalds 		return -EINVAL;
38721da177e4SLinus Torvalds 	}
38731da177e4SLinus Torvalds 
38741da177e4SLinus Torvalds 	/* Register us to receive netdevice events */
38751da177e4SLinus Torvalds 	register_netdevice_notifier(&pktgen_notifier_block);
38761da177e4SLinus Torvalds 
3877670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
38788024bb24SLuiz Capitulino 		int err;
38791da177e4SLinus Torvalds 
3880ee74baa7SDavid S. Miller 		err = pktgen_create_thread(cpu);
38818024bb24SLuiz Capitulino 		if (err)
388225a8b254SDavid S. Miller 			printk(KERN_WARNING "pktgen: WARNING: Cannot create "
388325a8b254SDavid S. Miller 			       "thread for cpu %d (%d)\n", cpu, err);
38841da177e4SLinus Torvalds 	}
38858024bb24SLuiz Capitulino 
38868024bb24SLuiz Capitulino 	if (list_empty(&pktgen_threads)) {
388725a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
388825a8b254SDavid S. Miller 		       "all threads\n");
38898024bb24SLuiz Capitulino 		unregister_netdevice_notifier(&pktgen_notifier_block);
38908024bb24SLuiz Capitulino 		remove_proc_entry(PGCTRL, pg_proc_dir);
3891457c4cbcSEric W. Biederman 		proc_net_remove(&init_net, PG_PROC_DIR);
38928024bb24SLuiz Capitulino 		return -ENODEV;
38938024bb24SLuiz Capitulino 	}
38948024bb24SLuiz Capitulino 
38951da177e4SLinus Torvalds 	return 0;
38961da177e4SLinus Torvalds }
38971da177e4SLinus Torvalds 
38981da177e4SLinus Torvalds static void __exit pg_cleanup(void)
38991da177e4SLinus Torvalds {
3900cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3901cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
39021da177e4SLinus Torvalds 	wait_queue_head_t queue;
39031da177e4SLinus Torvalds 	init_waitqueue_head(&queue);
39041da177e4SLinus Torvalds 
39051da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
39061da177e4SLinus Torvalds 
3907cdcdbe0bSLuiz Capitulino 	list_for_each_safe(q, n, &pktgen_threads) {
3908cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3909ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
3910ee74baa7SDavid S. Miller 		kfree(t);
39111da177e4SLinus Torvalds 	}
39121da177e4SLinus Torvalds 
39131da177e4SLinus Torvalds 	/* Un-register us from receiving netdevice events */
39141da177e4SLinus Torvalds 	unregister_netdevice_notifier(&pktgen_notifier_block);
39151da177e4SLinus Torvalds 
39161da177e4SLinus Torvalds 	/* Clean up proc file system */
3917d50a6b56SStephen Hemminger 	remove_proc_entry(PGCTRL, pg_proc_dir);
3918457c4cbcSEric W. Biederman 	proc_net_remove(&init_net, PG_PROC_DIR);
39191da177e4SLinus Torvalds }
39201da177e4SLinus Torvalds 
39211da177e4SLinus Torvalds module_init(pg_init);
39221da177e4SLinus Torvalds module_exit(pg_cleanup);
39231da177e4SLinus Torvalds 
3924c3d2f52dSStephen Hemminger MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
39251da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
39261da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3927c3d2f52dSStephen Hemminger MODULE_VERSION(VERSION);
39281da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
3929c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
39301da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
3931c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
39321da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
3933c3d2f52dSStephen Hemminger MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
39341da177e4SLinus Torvalds module_param(debug, int, 0);
3935c3d2f52dSStephen Hemminger MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
3936