xref: /openbmc/linux/net/core/pktgen.c (revision 2bc481cf433879f0e6cdd4d899fc21ee05dcea23)
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>
134*2bc481cfSStephen 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>
1661da177e4SLinus Torvalds #include <asm/io.h>
1671da177e4SLinus Torvalds #include <asm/dma.h>
1681da177e4SLinus Torvalds #include <asm/uaccess.h>
1691da177e4SLinus Torvalds #include <asm/div64.h>		/* do_div */
1701da177e4SLinus Torvalds #include <asm/timex.h>
1711da177e4SLinus Torvalds 
172e6fce5b9SRobert Olsson #define VERSION  "pktgen v2.70: Packet Generator for packet performance testing.\n"
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds #define IP_NAME_SZ 32
175ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
176d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100)
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* Device flag bits */
1791da177e4SLinus Torvalds #define F_IPSRC_RND   (1<<0)	/* IP-Src Random  */
1801da177e4SLinus Torvalds #define F_IPDST_RND   (1<<1)	/* IP-Dst Random  */
1811da177e4SLinus Torvalds #define F_UDPSRC_RND  (1<<2)	/* UDP-Src Random */
1821da177e4SLinus Torvalds #define F_UDPDST_RND  (1<<3)	/* UDP-Dst Random */
1831da177e4SLinus Torvalds #define F_MACSRC_RND  (1<<4)	/* MAC-Src Random */
1841da177e4SLinus Torvalds #define F_MACDST_RND  (1<<5)	/* MAC-Dst Random */
1851da177e4SLinus Torvalds #define F_TXSIZE_RND  (1<<6)	/* Transmit size is random */
1861da177e4SLinus Torvalds #define F_IPV6        (1<<7)	/* Interface in IPV6 Mode */
187ca6549afSSteven Whitehouse #define F_MPLS_RND    (1<<8)	/* Random MPLS labels */
18834954ddcSFrancesco Fondelli #define F_VID_RND     (1<<9)	/* Random VLAN ID */
18934954ddcSFrancesco Fondelli #define F_SVID_RND    (1<<10)	/* Random SVLAN ID */
190007a531bSJamal Hadi Salim #define F_FLOW_SEQ    (1<<11)	/* Sequential flows */
191a553e4a6SJamal Hadi Salim #define F_IPSEC_ON    (1<<12)	/* ipsec on for flows */
19245b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13)	/* queue map Random */
193e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14)	/* queue map mirrors smp_processor_id() */
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /* Thread control flag bits */
1961da177e4SLinus Torvalds #define T_TERMINATE   (1<<0)
1971da177e4SLinus Torvalds #define T_STOP        (1<<1)	/* Stop run */
1981da177e4SLinus Torvalds #define T_RUN         (1<<2)	/* Start run */
19995ed63f7SArthur Kepner #define T_REMDEVALL   (1<<3)	/* Remove all devs */
20095ed63f7SArthur Kepner #define T_REMDEV      (1<<4)	/* Remove one dev */
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds /* If lock -- can be removed after some work */
2031da177e4SLinus Torvalds #define   if_lock(t)           spin_lock(&(t->if_lock));
2041da177e4SLinus Torvalds #define   if_unlock(t)           spin_unlock(&(t->if_lock));
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */
2071da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955
208d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen"
209d50a6b56SStephen Hemminger #define PGCTRL	    "pgctrl"
210d50a6b56SStephen Hemminger static struct proc_dir_entry *pg_proc_dir = NULL;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds #define MAX_CFLOWS  65536
2131da177e4SLinus Torvalds 
21434954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
21534954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
21634954ddcSFrancesco Fondelli 
217222f1806SLuiz Capitulino struct flow_state {
218252e3346SAl Viro 	__be32 cur_daddr;
2191da177e4SLinus Torvalds 	int count;
220a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
221a553e4a6SJamal Hadi Salim 	struct xfrm_state *x;
222a553e4a6SJamal Hadi Salim #endif
223007a531bSJamal Hadi Salim 	__u32 flags;
2241da177e4SLinus Torvalds };
2251da177e4SLinus Torvalds 
226007a531bSJamal Hadi Salim /* flow flag bits */
227007a531bSJamal Hadi Salim #define F_INIT   (1<<0)		/* flow has been initialized */
228007a531bSJamal Hadi Salim 
2291da177e4SLinus Torvalds struct pktgen_dev {
2301da177e4SLinus Torvalds 	/*
2311da177e4SLinus Torvalds 	 * Try to keep frequent/infrequent used vars. separated.
2321da177e4SLinus Torvalds 	 */
23339df232fSStephen Hemminger 	struct proc_dir_entry *entry;	/* proc file */
2341da177e4SLinus Torvalds 	struct pktgen_thread *pg_thread;/* the owner */
235c26a8016SLuiz Capitulino 	struct list_head list;		/* Used for chaining in the thread's run-queue */
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	int running;		/* if this changes to false, the test will stop */
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/* If min != max, then we will either do a linear iteration, or
2401da177e4SLinus Torvalds 	 * we will do a random selection from within the range.
2411da177e4SLinus Torvalds 	 */
2421da177e4SLinus Torvalds 	__u32 flags;
24395ed63f7SArthur Kepner 	int removal_mark;	/* non-zero => the device is marked for
24495ed63f7SArthur Kepner 				 * removal by worker thread */
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	int min_pkt_size;	/* = ETH_ZLEN; */
2471da177e4SLinus Torvalds 	int max_pkt_size;	/* = ETH_ZLEN; */
24816dab72fSJamal Hadi Salim 	int pkt_overhead;	/* overhead for MPLS, VLANs, IPSEC etc */
2491da177e4SLinus Torvalds 	int nfrags;
250fd29cf72SStephen Hemminger 	u64 delay;		/* nano-seconds */
251fd29cf72SStephen Hemminger 
2521da177e4SLinus Torvalds 	__u64 count;		/* Default No packets to send */
2531da177e4SLinus Torvalds 	__u64 sofar;		/* How many pkts we've sent so far */
2541da177e4SLinus Torvalds 	__u64 tx_bytes;		/* How many bytes we've transmitted */
2551da177e4SLinus Torvalds 	__u64 errors;		/* Errors when trying to transmit, pkts will be re-sent */
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	/* runtime counters relating to clone_skb */
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	__u64 allocated_skbs;
2601da177e4SLinus Torvalds 	__u32 clone_count;
2611da177e4SLinus Torvalds 	int last_ok;		/* Was last skb sent?
2621da177e4SLinus Torvalds 				 * Or a failed transmit of some sort?  This will keep
2631da177e4SLinus Torvalds 				 * sequence numbers in order, for example.
2641da177e4SLinus Torvalds 				 */
265fd29cf72SStephen Hemminger 	ktime_t next_tx;
266fd29cf72SStephen Hemminger 	ktime_t started_at;
267fd29cf72SStephen Hemminger 	ktime_t stopped_at;
268fd29cf72SStephen Hemminger 	u64	idle_acc;	/* nano-seconds */
269fd29cf72SStephen Hemminger 
2701da177e4SLinus Torvalds 	__u32 seq_num;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	int clone_skb;		/* Use multiple SKBs during packet gen.  If this number
273b4099fabSStephen Hemminger 				 * is greater than 1, then that many copies of the same
2741da177e4SLinus Torvalds 				 * packet will be sent before a new packet is allocated.
2751da177e4SLinus Torvalds 				 * For instance, if you want to send 1024 identical packets
2761da177e4SLinus Torvalds 				 * before creating a new packet, set clone_skb to 1024.
2771da177e4SLinus Torvalds 				 */
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	char dst_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2801da177e4SLinus Torvalds 	char dst_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2811da177e4SLinus Torvalds 	char src_min[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2821da177e4SLinus Torvalds 	char src_max[IP_NAME_SZ];	/* IP, ie 1.2.3.4 */
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	struct in6_addr in6_saddr;
2851da177e4SLinus Torvalds 	struct in6_addr in6_daddr;
2861da177e4SLinus Torvalds 	struct in6_addr cur_in6_daddr;
2871da177e4SLinus Torvalds 	struct in6_addr cur_in6_saddr;
2881da177e4SLinus Torvalds 	/* For ranges */
2891da177e4SLinus Torvalds 	struct in6_addr min_in6_daddr;
2901da177e4SLinus Torvalds 	struct in6_addr max_in6_daddr;
2911da177e4SLinus Torvalds 	struct in6_addr min_in6_saddr;
2921da177e4SLinus Torvalds 	struct in6_addr max_in6_saddr;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	/* If we're doing ranges, random or incremental, then this
2951da177e4SLinus Torvalds 	 * defines the min/max for those ranges.
2961da177e4SLinus Torvalds 	 */
297252e3346SAl Viro 	__be32 saddr_min;	/* inclusive, source IP address */
298252e3346SAl Viro 	__be32 saddr_max;	/* exclusive, source IP address */
299252e3346SAl Viro 	__be32 daddr_min;	/* inclusive, dest IP address */
300252e3346SAl Viro 	__be32 daddr_max;	/* exclusive, dest IP address */
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	__u16 udp_src_min;	/* inclusive, source UDP port */
3031da177e4SLinus Torvalds 	__u16 udp_src_max;	/* exclusive, source UDP port */
3041da177e4SLinus Torvalds 	__u16 udp_dst_min;	/* inclusive, dest UDP port */
3051da177e4SLinus Torvalds 	__u16 udp_dst_max;	/* exclusive, dest UDP port */
3061da177e4SLinus Torvalds 
3071ca7768cSFrancesco Fondelli 	/* DSCP + ECN */
3081ca7768cSFrancesco Fondelli 	__u8 tos;            /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
3091ca7768cSFrancesco Fondelli 	__u8 traffic_class;  /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
3101ca7768cSFrancesco Fondelli 
311ca6549afSSteven Whitehouse 	/* MPLS */
312ca6549afSSteven Whitehouse 	unsigned nr_labels;	/* Depth of stack, 0 = no MPLS */
313ca6549afSSteven Whitehouse 	__be32 labels[MAX_MPLS_LABELS];
314ca6549afSSteven Whitehouse 
31534954ddcSFrancesco Fondelli 	/* VLAN/SVLAN (802.1Q/Q-in-Q) */
31634954ddcSFrancesco Fondelli 	__u8  vlan_p;
31734954ddcSFrancesco Fondelli 	__u8  vlan_cfi;
31834954ddcSFrancesco Fondelli 	__u16 vlan_id;  /* 0xffff means no vlan tag */
31934954ddcSFrancesco Fondelli 
32034954ddcSFrancesco Fondelli 	__u8  svlan_p;
32134954ddcSFrancesco Fondelli 	__u8  svlan_cfi;
32234954ddcSFrancesco Fondelli 	__u16 svlan_id; /* 0xffff means no svlan tag */
32334954ddcSFrancesco Fondelli 
3241da177e4SLinus Torvalds 	__u32 src_mac_count;	/* How many MACs to iterate through */
3251da177e4SLinus Torvalds 	__u32 dst_mac_count;	/* How many MACs to iterate through */
3261da177e4SLinus Torvalds 
327f404e9a6SKris Katterjohn 	unsigned char dst_mac[ETH_ALEN];
328f404e9a6SKris Katterjohn 	unsigned char src_mac[ETH_ALEN];
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	__u32 cur_dst_mac_offset;
3311da177e4SLinus Torvalds 	__u32 cur_src_mac_offset;
332252e3346SAl Viro 	__be32 cur_saddr;
333252e3346SAl Viro 	__be32 cur_daddr;
3341da177e4SLinus Torvalds 	__u16 cur_udp_dst;
3351da177e4SLinus Torvalds 	__u16 cur_udp_src;
33645b270f8SRobert Olsson 	__u16 cur_queue_map;
3371da177e4SLinus Torvalds 	__u32 cur_pkt_size;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	__u8 hh[14];
3401da177e4SLinus Torvalds 	/* = {
3411da177e4SLinus Torvalds 	   0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	   We fill in SRC address later
3441da177e4SLinus Torvalds 	   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3451da177e4SLinus Torvalds 	   0x08, 0x00
3461da177e4SLinus Torvalds 	   };
3471da177e4SLinus Torvalds 	 */
3481da177e4SLinus Torvalds 	__u16 pad;		/* pad out the hh struct to an even 16 bytes */
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	struct sk_buff *skb;	/* skb we are to transmit next, mainly used for when we
3511da177e4SLinus Torvalds 				 * are transmitting the same one multiple times
3521da177e4SLinus Torvalds 				 */
3531da177e4SLinus Torvalds 	struct net_device *odev;	/* The out-going device.  Note that the device should
3541da177e4SLinus Torvalds 					 * have it's pg_info pointer pointing back to this
3551da177e4SLinus Torvalds 					 * device.  This will be set when the user specifies
3561da177e4SLinus Torvalds 					 * the out-going device name (not when the inject is
3571da177e4SLinus Torvalds 					 * started as it used to do.)
3581da177e4SLinus Torvalds 					 */
3591da177e4SLinus Torvalds 	struct flow_state *flows;
3601da177e4SLinus Torvalds 	unsigned cflows;	/* Concurrent flows (config) */
3611da177e4SLinus Torvalds 	unsigned lflow;		/* Flow length  (config) */
3621da177e4SLinus Torvalds 	unsigned nflows;	/* accumulated flows (stats) */
363007a531bSJamal Hadi Salim 	unsigned curfl;		/* current sequenced flow (state)*/
36445b270f8SRobert Olsson 
36545b270f8SRobert Olsson 	u16 queue_map_min;
36645b270f8SRobert Olsson 	u16 queue_map_max;
36745b270f8SRobert Olsson 
368a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
369a553e4a6SJamal Hadi Salim 	__u8	ipsmode;		/* IPSEC mode (config) */
370a553e4a6SJamal Hadi Salim 	__u8	ipsproto;		/* IPSEC type (config) */
371a553e4a6SJamal Hadi Salim #endif
37239df232fSStephen Hemminger 	char result[512];
3731da177e4SLinus Torvalds };
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds struct pktgen_hdr {
376252e3346SAl Viro 	__be32 pgh_magic;
377252e3346SAl Viro 	__be32 seq_num;
378252e3346SAl Viro 	__be32 tv_sec;
379252e3346SAl Viro 	__be32 tv_usec;
3801da177e4SLinus Torvalds };
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds struct pktgen_thread {
3831da177e4SLinus Torvalds 	spinlock_t if_lock;
384c26a8016SLuiz Capitulino 	struct list_head if_list;	/* All device here */
385cdcdbe0bSLuiz Capitulino 	struct list_head th_list;
386ee74baa7SDavid S. Miller 	struct task_struct *tsk;
3871da177e4SLinus Torvalds 	char result[512];
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/* Field for thread to receive "posted" events terminate, stop ifs etc. */
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	u32 control;
3921da177e4SLinus Torvalds 	int cpu;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	wait_queue_head_t queue;
395d3ede327SDenis V. Lunev 	struct completion start_done;
3961da177e4SLinus Torvalds };
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds #define REMOVE 1
3991da177e4SLinus Torvalds #define FIND   0
4001da177e4SLinus Torvalds 
401fd29cf72SStephen Hemminger static inline ktime_t ktime_now(void)
4021da177e4SLinus Torvalds {
403fd29cf72SStephen Hemminger 	struct timespec ts;
404fd29cf72SStephen Hemminger 	ktime_get_ts(&ts);
405fd29cf72SStephen Hemminger 
406fd29cf72SStephen Hemminger 	return timespec_to_ktime(ts);
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
409fd29cf72SStephen Hemminger /* This works even if 32 bit because of careful byte order choice */
410fd29cf72SStephen Hemminger static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
4111da177e4SLinus Torvalds {
412fd29cf72SStephen Hemminger 	return cmp1.tv64 < cmp2.tv64;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
41565c5b786SStephen Hemminger static const char version[] __initconst = VERSION;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
4181da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
419222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
420222f1806SLuiz Capitulino 					  const char *ifname);
4211da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
4221da177e4SLinus Torvalds static void pktgen_run_all_threads(void);
423eb37b41cSJesse Brandeburg static void pktgen_reset_all_threads(void);
4241da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void);
4253bda06a3SStephen Hemminger 
4261da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t);
4271da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
42839df232fSStephen Hemminger 
4291da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]);
4301da177e4SLinus Torvalds static unsigned int fmt_ip6(char *s, const char ip[16]);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds /* Module parameters, defaults. */
43365c5b786SStephen Hemminger static int pg_count_d __read_mostly = 1000;
43465c5b786SStephen Hemminger static int pg_delay_d __read_mostly;
43565c5b786SStephen Hemminger static int pg_clone_skb_d  __read_mostly;
43665c5b786SStephen Hemminger static int debug  __read_mostly;
4371da177e4SLinus Torvalds 
438222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock);
439cdcdbe0bSLuiz Capitulino static LIST_HEAD(pktgen_threads);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = {
4421da177e4SLinus Torvalds 	.notifier_call = pktgen_device_event,
4431da177e4SLinus Torvalds };
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds /*
4461da177e4SLinus Torvalds  * /proc handling functions
4471da177e4SLinus Torvalds  *
4481da177e4SLinus Torvalds  */
4491da177e4SLinus Torvalds 
450d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v)
451d50a6b56SStephen Hemminger {
452d50a6b56SStephen Hemminger 	seq_puts(seq, VERSION);
453d50a6b56SStephen Hemminger 	return 0;
454d50a6b56SStephen Hemminger }
4551da177e4SLinus Torvalds 
456d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user * buf,
4571da177e4SLinus Torvalds 			    size_t count, loff_t * ppos)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	int err = 0;
460d50a6b56SStephen Hemminger 	char data[128];
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN)) {
4631da177e4SLinus Torvalds 		err = -EPERM;
4641da177e4SLinus Torvalds 		goto out;
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 
467d50a6b56SStephen Hemminger 	if (count > sizeof(data))
468d50a6b56SStephen Hemminger 		count = sizeof(data);
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count)) {
4711da177e4SLinus Torvalds 		err = -EFAULT;
472d50a6b56SStephen Hemminger 		goto out;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 	data[count - 1] = 0;	/* Make string */
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	if (!strcmp(data, "stop"))
4771da177e4SLinus Torvalds 		pktgen_stop_all_threads_ifs();
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	else if (!strcmp(data, "start"))
4801da177e4SLinus Torvalds 		pktgen_run_all_threads();
4811da177e4SLinus Torvalds 
482eb37b41cSJesse Brandeburg 	else if (!strcmp(data, "reset"))
483eb37b41cSJesse Brandeburg 		pktgen_reset_all_threads();
484eb37b41cSJesse Brandeburg 
4851da177e4SLinus Torvalds 	else
48625a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	err = count;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds out:
4911da177e4SLinus Torvalds 	return err;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
494d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file)
4951da177e4SLinus Torvalds {
496d50a6b56SStephen Hemminger 	return single_open(file, pgctrl_show, PDE(inode)->data);
497d50a6b56SStephen Hemminger }
498d50a6b56SStephen Hemminger 
4999a32144eSArjan van de Ven static const struct file_operations pktgen_fops = {
500d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
501d50a6b56SStephen Hemminger 	.open    = pgctrl_open,
502d50a6b56SStephen Hemminger 	.read    = seq_read,
503d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
504d50a6b56SStephen Hemminger 	.write   = pgctrl_write,
505d50a6b56SStephen Hemminger 	.release = single_release,
506d50a6b56SStephen Hemminger };
507d50a6b56SStephen Hemminger 
508d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v)
509d50a6b56SStephen Hemminger {
510648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev = seq->private;
511fd29cf72SStephen Hemminger 	ktime_t stopped;
512fd29cf72SStephen Hemminger 	u64 idle;
5131da177e4SLinus Torvalds 
514222f1806SLuiz Capitulino 	seq_printf(seq,
515222f1806SLuiz Capitulino 		   "Params: count %llu  min_pkt_size: %u  max_pkt_size: %u\n",
516222f1806SLuiz Capitulino 		   (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
517222f1806SLuiz Capitulino 		   pkt_dev->max_pkt_size);
5181da177e4SLinus Torvalds 
519222f1806SLuiz Capitulino 	seq_printf(seq,
520fd29cf72SStephen Hemminger 		   "     frags: %d  delay: %llu  clone_skb: %d  ifname: %s\n",
521fd29cf72SStephen Hemminger 		   pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
52239df232fSStephen Hemminger 		   pkt_dev->clone_skb, pkt_dev->odev->name);
5231da177e4SLinus Torvalds 
524222f1806SLuiz Capitulino 	seq_printf(seq, "     flows: %u flowlen: %u\n", pkt_dev->cflows,
525222f1806SLuiz Capitulino 		   pkt_dev->lflow);
5261da177e4SLinus Torvalds 
52745b270f8SRobert Olsson 	seq_printf(seq,
52845b270f8SRobert Olsson 		   "     queue_map_min: %u  queue_map_max: %u\n",
52945b270f8SRobert Olsson 		   pkt_dev->queue_map_min,
53045b270f8SRobert Olsson 		   pkt_dev->queue_map_max);
53145b270f8SRobert Olsson 
5321da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
5331da177e4SLinus Torvalds 		char b1[128], b2[128], b3[128];
5341da177e4SLinus Torvalds 		fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
5351da177e4SLinus Torvalds 		fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
5361da177e4SLinus Torvalds 		fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
537222f1806SLuiz Capitulino 		seq_printf(seq,
538222f1806SLuiz Capitulino 			   "     saddr: %s  min_saddr: %s  max_saddr: %s\n", b1,
539222f1806SLuiz Capitulino 			   b2, b3);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 		fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
5421da177e4SLinus Torvalds 		fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
5431da177e4SLinus Torvalds 		fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
544222f1806SLuiz Capitulino 		seq_printf(seq,
545222f1806SLuiz Capitulino 			   "     daddr: %s  min_daddr: %s  max_daddr: %s\n", b1,
546222f1806SLuiz Capitulino 			   b2, b3);
5471da177e4SLinus Torvalds 
548222f1806SLuiz Capitulino 	} else
549222f1806SLuiz Capitulino 		seq_printf(seq,
550222f1806SLuiz Capitulino 			   "     dst_min: %s  dst_max: %s\n     src_min: %s  src_max: %s\n",
551222f1806SLuiz Capitulino 			   pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
552222f1806SLuiz Capitulino 			   pkt_dev->src_max);
5531da177e4SLinus Torvalds 
554d50a6b56SStephen Hemminger 	seq_puts(seq, "     src_mac: ");
5551da177e4SLinus Torvalds 
556e174961cSJohannes Berg 	seq_printf(seq, "%pM ",
557e174961cSJohannes Berg 		   is_zero_ether_addr(pkt_dev->src_mac) ?
558e174961cSJohannes Berg 			     pkt_dev->odev->dev_addr : pkt_dev->src_mac);
5591da177e4SLinus Torvalds 
560d50a6b56SStephen Hemminger 	seq_printf(seq, "dst_mac: ");
561e174961cSJohannes Berg 	seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
5621da177e4SLinus Torvalds 
563222f1806SLuiz Capitulino 	seq_printf(seq,
564222f1806SLuiz Capitulino 		   "     udp_src_min: %d  udp_src_max: %d  udp_dst_min: %d  udp_dst_max: %d\n",
565222f1806SLuiz Capitulino 		   pkt_dev->udp_src_min, pkt_dev->udp_src_max,
566222f1806SLuiz Capitulino 		   pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
5671da177e4SLinus Torvalds 
568222f1806SLuiz Capitulino 	seq_printf(seq,
569ca6549afSSteven Whitehouse 		   "     src_mac_count: %d  dst_mac_count: %d\n",
5701da177e4SLinus Torvalds 		   pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
5711da177e4SLinus Torvalds 
572ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels) {
573ca6549afSSteven Whitehouse 		unsigned i;
574ca6549afSSteven Whitehouse 		seq_printf(seq, "     mpls: ");
575ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
576ca6549afSSteven Whitehouse 			seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
577ca6549afSSteven Whitehouse 				   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
578ca6549afSSteven Whitehouse 	}
579ca6549afSSteven Whitehouse 
58034954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
58134954ddcSFrancesco Fondelli 		seq_printf(seq, "     vlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
58234954ddcSFrancesco Fondelli 			   pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
58334954ddcSFrancesco Fondelli 	}
58434954ddcSFrancesco Fondelli 
58534954ddcSFrancesco Fondelli 	if (pkt_dev->svlan_id != 0xffff) {
58634954ddcSFrancesco Fondelli 		seq_printf(seq, "     svlan_id: %u  vlan_p: %u  vlan_cfi: %u\n",
58734954ddcSFrancesco Fondelli 			   pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
58834954ddcSFrancesco Fondelli 	}
58934954ddcSFrancesco Fondelli 
5901ca7768cSFrancesco Fondelli 	if (pkt_dev->tos) {
5911ca7768cSFrancesco Fondelli 		seq_printf(seq, "     tos: 0x%02x\n", pkt_dev->tos);
5921ca7768cSFrancesco Fondelli 	}
5931ca7768cSFrancesco Fondelli 
5941ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
5951ca7768cSFrancesco Fondelli 		seq_printf(seq, "     traffic_class: 0x%02x\n", pkt_dev->traffic_class);
5961ca7768cSFrancesco Fondelli 	}
5971ca7768cSFrancesco Fondelli 
598ca6549afSSteven Whitehouse 	seq_printf(seq, "     Flags: ");
599ca6549afSSteven Whitehouse 
6001da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
601d50a6b56SStephen Hemminger 		seq_printf(seq, "IPV6  ");
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPSRC_RND)
604d50a6b56SStephen Hemminger 		seq_printf(seq, "IPSRC_RND  ");
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPDST_RND)
607d50a6b56SStephen Hemminger 		seq_printf(seq, "IPDST_RND  ");
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	if (pkt_dev->flags & F_TXSIZE_RND)
610d50a6b56SStephen Hemminger 		seq_printf(seq, "TXSIZE_RND  ");
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPSRC_RND)
613d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPSRC_RND  ");
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	if (pkt_dev->flags & F_UDPDST_RND)
616d50a6b56SStephen Hemminger 		seq_printf(seq, "UDPDST_RND  ");
6171da177e4SLinus Torvalds 
618ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND)
619ca6549afSSteven Whitehouse 		seq_printf(seq,  "MPLS_RND  ");
620ca6549afSSteven Whitehouse 
62145b270f8SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_RND)
62245b270f8SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_RND  ");
62345b270f8SRobert Olsson 
624e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
625e6fce5b9SRobert Olsson 		seq_printf(seq,  "QUEUE_MAP_CPU  ");
626e6fce5b9SRobert Olsson 
627007a531bSJamal Hadi Salim 	if (pkt_dev->cflows) {
628007a531bSJamal Hadi Salim 		if (pkt_dev->flags & F_FLOW_SEQ)
629007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_SEQ  "); /*in sequence flows*/
630007a531bSJamal Hadi Salim 		else
631007a531bSJamal Hadi Salim 			seq_printf(seq,  "FLOW_RND  ");
632007a531bSJamal Hadi Salim 	}
633007a531bSJamal Hadi Salim 
634a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
635a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON)
636a553e4a6SJamal Hadi Salim 		seq_printf(seq,  "IPSEC  ");
637a553e4a6SJamal Hadi Salim #endif
638a553e4a6SJamal Hadi Salim 
6391da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACSRC_RND)
640d50a6b56SStephen Hemminger 		seq_printf(seq, "MACSRC_RND  ");
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	if (pkt_dev->flags & F_MACDST_RND)
643d50a6b56SStephen Hemminger 		seq_printf(seq, "MACDST_RND  ");
6441da177e4SLinus Torvalds 
64534954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_VID_RND)
64634954ddcSFrancesco Fondelli 		seq_printf(seq, "VID_RND  ");
64734954ddcSFrancesco Fondelli 
64834954ddcSFrancesco Fondelli 	if (pkt_dev->flags & F_SVID_RND)
64934954ddcSFrancesco Fondelli 		seq_printf(seq, "SVID_RND  ");
65034954ddcSFrancesco Fondelli 
651d50a6b56SStephen Hemminger 	seq_puts(seq, "\n");
6521da177e4SLinus Torvalds 
653fd29cf72SStephen Hemminger 	/* not really stopped, more like last-running-at */
654fd29cf72SStephen Hemminger 	stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
655fd29cf72SStephen Hemminger 	idle = pkt_dev->idle_acc;
656fd29cf72SStephen Hemminger 	do_div(idle, NSEC_PER_USEC);
6571da177e4SLinus Torvalds 
658222f1806SLuiz Capitulino 	seq_printf(seq,
659fd29cf72SStephen Hemminger 		   "Current:\n     pkts-sofar: %llu  errors: %llu\n",
6601da177e4SLinus Torvalds 		   (unsigned long long)pkt_dev->sofar,
661fd29cf72SStephen Hemminger 		   (unsigned long long)pkt_dev->errors);
662fd29cf72SStephen Hemminger 
663fd29cf72SStephen Hemminger 	seq_printf(seq,
664fd29cf72SStephen Hemminger 		   "     started: %lluus  stopped: %lluus idle: %lluus\n",
665fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(pkt_dev->started_at),
666fd29cf72SStephen Hemminger 		   (unsigned long long) ktime_to_us(stopped),
667fd29cf72SStephen Hemminger 		   (unsigned long long) idle);
6681da177e4SLinus Torvalds 
669222f1806SLuiz Capitulino 	seq_printf(seq,
670222f1806SLuiz Capitulino 		   "     seq_num: %d  cur_dst_mac_offset: %d  cur_src_mac_offset: %d\n",
671d50a6b56SStephen Hemminger 		   pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
672d50a6b56SStephen Hemminger 		   pkt_dev->cur_src_mac_offset);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
6751da177e4SLinus Torvalds 		char b1[128], b2[128];
6761da177e4SLinus Torvalds 		fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
6771da177e4SLinus Torvalds 		fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
678d50a6b56SStephen Hemminger 		seq_printf(seq, "     cur_saddr: %s  cur_daddr: %s\n", b2, b1);
679222f1806SLuiz Capitulino 	} else
680d50a6b56SStephen Hemminger 		seq_printf(seq, "     cur_saddr: 0x%x  cur_daddr: 0x%x\n",
6811da177e4SLinus Torvalds 			   pkt_dev->cur_saddr, pkt_dev->cur_daddr);
6821da177e4SLinus Torvalds 
683d50a6b56SStephen Hemminger 	seq_printf(seq, "     cur_udp_dst: %d  cur_udp_src: %d\n",
6841da177e4SLinus Torvalds 		   pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
6851da177e4SLinus Torvalds 
68645b270f8SRobert Olsson 	seq_printf(seq, "     cur_queue_map: %u\n", pkt_dev->cur_queue_map);
68745b270f8SRobert Olsson 
688d50a6b56SStephen Hemminger 	seq_printf(seq, "     flows: %u\n", pkt_dev->nflows);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	if (pkt_dev->result[0])
691d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: %s\n", pkt_dev->result);
6921da177e4SLinus Torvalds 	else
693d50a6b56SStephen Hemminger 		seq_printf(seq, "Result: Idle\n");
6941da177e4SLinus Torvalds 
695d50a6b56SStephen Hemminger 	return 0;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
698ca6549afSSteven Whitehouse 
6991ca7768cSFrancesco Fondelli static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
700ca6549afSSteven Whitehouse {
701ca6549afSSteven Whitehouse 	int i = 0;
702ca6549afSSteven Whitehouse 	*num = 0;
703ca6549afSSteven Whitehouse 
7041ca7768cSFrancesco Fondelli 	for (; i < maxlen; i++) {
705ca6549afSSteven Whitehouse 		char c;
706ca6549afSSteven Whitehouse 		*num <<= 4;
707ca6549afSSteven Whitehouse 		if (get_user(c, &user_buffer[i]))
708ca6549afSSteven Whitehouse 			return -EFAULT;
709ca6549afSSteven Whitehouse 		if ((c >= '0') && (c <= '9'))
710ca6549afSSteven Whitehouse 			*num |= c - '0';
711ca6549afSSteven Whitehouse 		else if ((c >= 'a') && (c <= 'f'))
712ca6549afSSteven Whitehouse 			*num |= c - 'a' + 10;
713ca6549afSSteven Whitehouse 		else if ((c >= 'A') && (c <= 'F'))
714ca6549afSSteven Whitehouse 			*num |= c - 'A' + 10;
715ca6549afSSteven Whitehouse 		else
716ca6549afSSteven Whitehouse 			break;
717ca6549afSSteven Whitehouse 	}
718ca6549afSSteven Whitehouse 	return i;
719ca6549afSSteven Whitehouse }
720ca6549afSSteven Whitehouse 
721222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer,
722222f1806SLuiz Capitulino 			     unsigned int maxlen)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	int i;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	for (i = 0; i < maxlen; i++) {
7271da177e4SLinus Torvalds 		char c;
7281da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7291da177e4SLinus Torvalds 			return -EFAULT;
7301da177e4SLinus Torvalds 		switch (c) {
7311da177e4SLinus Torvalds 		case '\"':
7321da177e4SLinus Torvalds 		case '\n':
7331da177e4SLinus Torvalds 		case '\r':
7341da177e4SLinus Torvalds 		case '\t':
7351da177e4SLinus Torvalds 		case ' ':
7361da177e4SLinus Torvalds 		case '=':
7371da177e4SLinus Torvalds 			break;
7381da177e4SLinus Torvalds 		default:
7391da177e4SLinus Torvalds 			goto done;
7403ff50b79SStephen Hemminger 		}
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds done:
7431da177e4SLinus Torvalds 	return i;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
746222f1806SLuiz Capitulino static unsigned long num_arg(const char __user * user_buffer,
747222f1806SLuiz Capitulino 			     unsigned long maxlen, unsigned long *num)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	int i = 0;
7501da177e4SLinus Torvalds 	*num = 0;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	for (; i < maxlen; i++) {
7531da177e4SLinus Torvalds 		char c;
7541da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7551da177e4SLinus Torvalds 			return -EFAULT;
7561da177e4SLinus Torvalds 		if ((c >= '0') && (c <= '9')) {
7571da177e4SLinus Torvalds 			*num *= 10;
7581da177e4SLinus Torvalds 			*num += c - '0';
7591da177e4SLinus Torvalds 		} else
7601da177e4SLinus Torvalds 			break;
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 	return i;
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	int i = 0;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	for (; i < maxlen; i++) {
7701da177e4SLinus Torvalds 		char c;
7711da177e4SLinus Torvalds 		if (get_user(c, &user_buffer[i]))
7721da177e4SLinus Torvalds 			return -EFAULT;
7731da177e4SLinus Torvalds 		switch (c) {
7741da177e4SLinus Torvalds 		case '\"':
7751da177e4SLinus Torvalds 		case '\n':
7761da177e4SLinus Torvalds 		case '\r':
7771da177e4SLinus Torvalds 		case '\t':
7781da177e4SLinus Torvalds 		case ' ':
7791da177e4SLinus Torvalds 			goto done_str;
7801da177e4SLinus Torvalds 			break;
7811da177e4SLinus Torvalds 		default:
7821da177e4SLinus Torvalds 			break;
7833ff50b79SStephen Hemminger 		}
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds done_str:
7861da177e4SLinus Torvalds 	return i;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
789ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
790ca6549afSSteven Whitehouse {
791ca6549afSSteven Whitehouse 	unsigned n = 0;
792ca6549afSSteven Whitehouse 	char c;
793ca6549afSSteven Whitehouse 	ssize_t i = 0;
794ca6549afSSteven Whitehouse 	int len;
795ca6549afSSteven Whitehouse 
796ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = 0;
797ca6549afSSteven Whitehouse 	do {
798ca6549afSSteven Whitehouse 		__u32 tmp;
7991ca7768cSFrancesco Fondelli 		len = hex32_arg(&buffer[i], 8, &tmp);
800ca6549afSSteven Whitehouse 		if (len <= 0)
801ca6549afSSteven Whitehouse 			return len;
802ca6549afSSteven Whitehouse 		pkt_dev->labels[n] = htonl(tmp);
803ca6549afSSteven Whitehouse 		if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
804ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
805ca6549afSSteven Whitehouse 		i += len;
806ca6549afSSteven Whitehouse 		if (get_user(c, &buffer[i]))
807ca6549afSSteven Whitehouse 			return -EFAULT;
808ca6549afSSteven Whitehouse 		i++;
809ca6549afSSteven Whitehouse 		n++;
810ca6549afSSteven Whitehouse 		if (n >= MAX_MPLS_LABELS)
811ca6549afSSteven Whitehouse 			return -E2BIG;
812ca6549afSSteven Whitehouse 	} while (c == ',');
813ca6549afSSteven Whitehouse 
814ca6549afSSteven Whitehouse 	pkt_dev->nr_labels = n;
815ca6549afSSteven Whitehouse 	return i;
816ca6549afSSteven Whitehouse }
817ca6549afSSteven Whitehouse 
818222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file,
819222f1806SLuiz Capitulino 			       const char __user * user_buffer, size_t count,
820222f1806SLuiz Capitulino 			       loff_t * offset)
8211da177e4SLinus Torvalds {
822d50a6b56SStephen Hemminger 	struct seq_file *seq = (struct seq_file *)file->private_data;
823d50a6b56SStephen Hemminger 	struct pktgen_dev *pkt_dev = seq->private;
8241da177e4SLinus Torvalds 	int i = 0, max, len;
8251da177e4SLinus Torvalds 	char name[16], valstr[32];
8261da177e4SLinus Torvalds 	unsigned long value = 0;
8271da177e4SLinus Torvalds 	char *pg_result = NULL;
8281da177e4SLinus Torvalds 	int tmp = 0;
8291da177e4SLinus Torvalds 	char buf[128];
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	pg_result = &(pkt_dev->result[0]);
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	if (count < 1) {
83425a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: wrong command format\n");
8351da177e4SLinus Torvalds 		return -EINVAL;
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	max = count - i;
8391da177e4SLinus Torvalds 	tmp = count_trail_chars(&user_buffer[i], max);
8401da177e4SLinus Torvalds 	if (tmp < 0) {
84125a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: illegal format\n");
8421da177e4SLinus Torvalds 		return tmp;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 	i += tmp;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	/* Read variable name */
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
849222f1806SLuiz Capitulino 	if (len < 0) {
850222f1806SLuiz Capitulino 		return len;
851222f1806SLuiz Capitulino 	}
8521da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
8531da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
8541da177e4SLinus Torvalds 		return -EFAULT;
8551da177e4SLinus Torvalds 	i += len;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	max = count - i;
8581da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
8591da177e4SLinus Torvalds 	if (len < 0)
8601da177e4SLinus Torvalds 		return len;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	i += len;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	if (debug) {
8651da177e4SLinus Torvalds 		char tb[count + 1];
8661da177e4SLinus Torvalds 		if (copy_from_user(tb, user_buffer, count))
8671da177e4SLinus Torvalds 			return -EFAULT;
8681da177e4SLinus Torvalds 		tb[count] = 0;
86925a8b254SDavid S. Miller 		printk(KERN_DEBUG "pktgen: %s,%lu  buffer -:%s:-\n", name,
870d50a6b56SStephen Hemminger 		       (unsigned long)count, tb);
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	if (!strcmp(name, "min_pkt_size")) {
8741da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
875222f1806SLuiz Capitulino 		if (len < 0) {
876222f1806SLuiz Capitulino 			return len;
877222f1806SLuiz Capitulino 		}
8781da177e4SLinus Torvalds 		i += len;
8791da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
8801da177e4SLinus Torvalds 			value = 14 + 20 + 8;
8811da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
8821da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
8831da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
8841da177e4SLinus Torvalds 		}
885222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: min_pkt_size=%u",
886222f1806SLuiz Capitulino 			pkt_dev->min_pkt_size);
8871da177e4SLinus Torvalds 		return count;
8881da177e4SLinus Torvalds 	}
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	if (!strcmp(name, "max_pkt_size")) {
8911da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
892222f1806SLuiz Capitulino 		if (len < 0) {
893222f1806SLuiz Capitulino 			return len;
894222f1806SLuiz Capitulino 		}
8951da177e4SLinus Torvalds 		i += len;
8961da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
8971da177e4SLinus Torvalds 			value = 14 + 20 + 8;
8981da177e4SLinus Torvalds 		if (value != pkt_dev->max_pkt_size) {
8991da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9001da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9011da177e4SLinus Torvalds 		}
902222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: max_pkt_size=%u",
903222f1806SLuiz Capitulino 			pkt_dev->max_pkt_size);
9041da177e4SLinus Torvalds 		return count;
9051da177e4SLinus Torvalds 	}
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/* Shortcut for min = max */
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	if (!strcmp(name, "pkt_size")) {
9101da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
911222f1806SLuiz Capitulino 		if (len < 0) {
912222f1806SLuiz Capitulino 			return len;
913222f1806SLuiz Capitulino 		}
9141da177e4SLinus Torvalds 		i += len;
9151da177e4SLinus Torvalds 		if (value < 14 + 20 + 8)
9161da177e4SLinus Torvalds 			value = 14 + 20 + 8;
9171da177e4SLinus Torvalds 		if (value != pkt_dev->min_pkt_size) {
9181da177e4SLinus Torvalds 			pkt_dev->min_pkt_size = value;
9191da177e4SLinus Torvalds 			pkt_dev->max_pkt_size = value;
9201da177e4SLinus Torvalds 			pkt_dev->cur_pkt_size = value;
9211da177e4SLinus Torvalds 		}
9221da177e4SLinus Torvalds 		sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
9231da177e4SLinus Torvalds 		return count;
9241da177e4SLinus Torvalds 	}
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	if (!strcmp(name, "debug")) {
9271da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
928222f1806SLuiz Capitulino 		if (len < 0) {
929222f1806SLuiz Capitulino 			return len;
930222f1806SLuiz Capitulino 		}
9311da177e4SLinus Torvalds 		i += len;
9321da177e4SLinus Torvalds 		debug = value;
9331da177e4SLinus Torvalds 		sprintf(pg_result, "OK: debug=%u", debug);
9341da177e4SLinus Torvalds 		return count;
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	if (!strcmp(name, "frags")) {
9381da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
939222f1806SLuiz Capitulino 		if (len < 0) {
940222f1806SLuiz Capitulino 			return len;
941222f1806SLuiz Capitulino 		}
9421da177e4SLinus Torvalds 		i += len;
9431da177e4SLinus Torvalds 		pkt_dev->nfrags = value;
9441da177e4SLinus Torvalds 		sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
9451da177e4SLinus Torvalds 		return count;
9461da177e4SLinus Torvalds 	}
9471da177e4SLinus Torvalds 	if (!strcmp(name, "delay")) {
9481da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
949222f1806SLuiz Capitulino 		if (len < 0) {
950222f1806SLuiz Capitulino 			return len;
951222f1806SLuiz Capitulino 		}
9521da177e4SLinus Torvalds 		i += len;
953fd29cf72SStephen Hemminger 		if (value == 0x7FFFFFFF)
954fd29cf72SStephen Hemminger 			pkt_dev->delay = ULLONG_MAX;
955fd29cf72SStephen Hemminger 		else
956fd29cf72SStephen Hemminger 			pkt_dev->delay = (u64)value * NSEC_PER_USEC;
957fd29cf72SStephen Hemminger 
958fd29cf72SStephen Hemminger 		sprintf(pg_result, "OK: delay=%llu",
959fd29cf72SStephen Hemminger 			(unsigned long long) pkt_dev->delay);
9601da177e4SLinus Torvalds 		return count;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_min")) {
9631da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
964222f1806SLuiz Capitulino 		if (len < 0) {
965222f1806SLuiz Capitulino 			return len;
966222f1806SLuiz Capitulino 		}
9671da177e4SLinus Torvalds 		i += len;
9681da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_min) {
9691da177e4SLinus Torvalds 			pkt_dev->udp_src_min = value;
9701da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
9711da177e4SLinus Torvalds 		}
9721da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
9731da177e4SLinus Torvalds 		return count;
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_min")) {
9761da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
977222f1806SLuiz Capitulino 		if (len < 0) {
978222f1806SLuiz Capitulino 			return len;
979222f1806SLuiz Capitulino 		}
9801da177e4SLinus Torvalds 		i += len;
9811da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_min) {
9821da177e4SLinus Torvalds 			pkt_dev->udp_dst_min = value;
9831da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
9841da177e4SLinus Torvalds 		}
9851da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
9861da177e4SLinus Torvalds 		return count;
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 	if (!strcmp(name, "udp_src_max")) {
9891da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
990222f1806SLuiz Capitulino 		if (len < 0) {
991222f1806SLuiz Capitulino 			return len;
992222f1806SLuiz Capitulino 		}
9931da177e4SLinus Torvalds 		i += len;
9941da177e4SLinus Torvalds 		if (value != pkt_dev->udp_src_max) {
9951da177e4SLinus Torvalds 			pkt_dev->udp_src_max = value;
9961da177e4SLinus Torvalds 			pkt_dev->cur_udp_src = value;
9971da177e4SLinus Torvalds 		}
9981da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
9991da177e4SLinus Torvalds 		return count;
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 	if (!strcmp(name, "udp_dst_max")) {
10021da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
1003222f1806SLuiz Capitulino 		if (len < 0) {
1004222f1806SLuiz Capitulino 			return len;
1005222f1806SLuiz Capitulino 		}
10061da177e4SLinus Torvalds 		i += len;
10071da177e4SLinus Torvalds 		if (value != pkt_dev->udp_dst_max) {
10081da177e4SLinus Torvalds 			pkt_dev->udp_dst_max = value;
10091da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst = value;
10101da177e4SLinus Torvalds 		}
10111da177e4SLinus Torvalds 		sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
10121da177e4SLinus Torvalds 		return count;
10131da177e4SLinus Torvalds 	}
10141da177e4SLinus Torvalds 	if (!strcmp(name, "clone_skb")) {
10151da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
1016222f1806SLuiz Capitulino 		if (len < 0) {
1017222f1806SLuiz Capitulino 			return len;
1018222f1806SLuiz Capitulino 		}
10191da177e4SLinus Torvalds 		i += len;
10201da177e4SLinus Torvalds 		pkt_dev->clone_skb = value;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 		sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
10231da177e4SLinus Torvalds 		return count;
10241da177e4SLinus Torvalds 	}
10251da177e4SLinus Torvalds 	if (!strcmp(name, "count")) {
10261da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
1027222f1806SLuiz Capitulino 		if (len < 0) {
1028222f1806SLuiz Capitulino 			return len;
1029222f1806SLuiz Capitulino 		}
10301da177e4SLinus Torvalds 		i += len;
10311da177e4SLinus Torvalds 		pkt_dev->count = value;
10321da177e4SLinus Torvalds 		sprintf(pg_result, "OK: count=%llu",
10331da177e4SLinus Torvalds 			(unsigned long long)pkt_dev->count);
10341da177e4SLinus Torvalds 		return count;
10351da177e4SLinus Torvalds 	}
10361da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac_count")) {
10371da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
1038222f1806SLuiz Capitulino 		if (len < 0) {
1039222f1806SLuiz Capitulino 			return len;
1040222f1806SLuiz Capitulino 		}
10411da177e4SLinus Torvalds 		i += len;
10421da177e4SLinus Torvalds 		if (pkt_dev->src_mac_count != value) {
10431da177e4SLinus Torvalds 			pkt_dev->src_mac_count = value;
10441da177e4SLinus Torvalds 			pkt_dev->cur_src_mac_offset = 0;
10451da177e4SLinus Torvalds 		}
1046222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: src_mac_count=%d",
1047222f1806SLuiz Capitulino 			pkt_dev->src_mac_count);
10481da177e4SLinus Torvalds 		return count;
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac_count")) {
10511da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
1052222f1806SLuiz Capitulino 		if (len < 0) {
1053222f1806SLuiz Capitulino 			return len;
1054222f1806SLuiz Capitulino 		}
10551da177e4SLinus Torvalds 		i += len;
10561da177e4SLinus Torvalds 		if (pkt_dev->dst_mac_count != value) {
10571da177e4SLinus Torvalds 			pkt_dev->dst_mac_count = value;
10581da177e4SLinus Torvalds 			pkt_dev->cur_dst_mac_offset = 0;
10591da177e4SLinus Torvalds 		}
1060222f1806SLuiz Capitulino 		sprintf(pg_result, "OK: dst_mac_count=%d",
1061222f1806SLuiz Capitulino 			pkt_dev->dst_mac_count);
10621da177e4SLinus Torvalds 		return count;
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 	if (!strcmp(name, "flag")) {
10651da177e4SLinus Torvalds 		char f[32];
10661da177e4SLinus Torvalds 		memset(f, 0, 32);
10671da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
1068222f1806SLuiz Capitulino 		if (len < 0) {
1069222f1806SLuiz Capitulino 			return len;
1070222f1806SLuiz Capitulino 		}
10711da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
10721da177e4SLinus Torvalds 			return -EFAULT;
10731da177e4SLinus Torvalds 		i += len;
10741da177e4SLinus Torvalds 		if (strcmp(f, "IPSRC_RND") == 0)
10751da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPSRC_RND;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 		else if (strcmp(f, "!IPSRC_RND") == 0)
10781da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPSRC_RND;
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 		else if (strcmp(f, "TXSIZE_RND") == 0)
10811da177e4SLinus Torvalds 			pkt_dev->flags |= F_TXSIZE_RND;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 		else if (strcmp(f, "!TXSIZE_RND") == 0)
10841da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_TXSIZE_RND;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 		else if (strcmp(f, "IPDST_RND") == 0)
10871da177e4SLinus Torvalds 			pkt_dev->flags |= F_IPDST_RND;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 		else if (strcmp(f, "!IPDST_RND") == 0)
10901da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_IPDST_RND;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 		else if (strcmp(f, "UDPSRC_RND") == 0)
10931da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPSRC_RND;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPSRC_RND") == 0)
10961da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPSRC_RND;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 		else if (strcmp(f, "UDPDST_RND") == 0)
10991da177e4SLinus Torvalds 			pkt_dev->flags |= F_UDPDST_RND;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 		else if (strcmp(f, "!UDPDST_RND") == 0)
11021da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_UDPDST_RND;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 		else if (strcmp(f, "MACSRC_RND") == 0)
11051da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACSRC_RND;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 		else if (strcmp(f, "!MACSRC_RND") == 0)
11081da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACSRC_RND;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 		else if (strcmp(f, "MACDST_RND") == 0)
11111da177e4SLinus Torvalds 			pkt_dev->flags |= F_MACDST_RND;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 		else if (strcmp(f, "!MACDST_RND") == 0)
11141da177e4SLinus Torvalds 			pkt_dev->flags &= ~F_MACDST_RND;
11151da177e4SLinus Torvalds 
1116ca6549afSSteven Whitehouse 		else if (strcmp(f, "MPLS_RND") == 0)
1117ca6549afSSteven Whitehouse 			pkt_dev->flags |= F_MPLS_RND;
1118ca6549afSSteven Whitehouse 
1119ca6549afSSteven Whitehouse 		else if (strcmp(f, "!MPLS_RND") == 0)
1120ca6549afSSteven Whitehouse 			pkt_dev->flags &= ~F_MPLS_RND;
1121ca6549afSSteven Whitehouse 
112234954ddcSFrancesco Fondelli 		else if (strcmp(f, "VID_RND") == 0)
112334954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_VID_RND;
112434954ddcSFrancesco Fondelli 
112534954ddcSFrancesco Fondelli 		else if (strcmp(f, "!VID_RND") == 0)
112634954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_VID_RND;
112734954ddcSFrancesco Fondelli 
112834954ddcSFrancesco Fondelli 		else if (strcmp(f, "SVID_RND") == 0)
112934954ddcSFrancesco Fondelli 			pkt_dev->flags |= F_SVID_RND;
113034954ddcSFrancesco Fondelli 
113134954ddcSFrancesco Fondelli 		else if (strcmp(f, "!SVID_RND") == 0)
113234954ddcSFrancesco Fondelli 			pkt_dev->flags &= ~F_SVID_RND;
113334954ddcSFrancesco Fondelli 
1134007a531bSJamal Hadi Salim 		else if (strcmp(f, "FLOW_SEQ") == 0)
1135007a531bSJamal Hadi Salim 			pkt_dev->flags |= F_FLOW_SEQ;
1136007a531bSJamal Hadi Salim 
113745b270f8SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_RND") == 0)
113845b270f8SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_RND;
113945b270f8SRobert Olsson 
114045b270f8SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
114145b270f8SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1142e6fce5b9SRobert Olsson 
1143e6fce5b9SRobert Olsson 		else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1144e6fce5b9SRobert Olsson 			pkt_dev->flags |= F_QUEUE_MAP_CPU;
1145e6fce5b9SRobert Olsson 
1146e6fce5b9SRobert Olsson 		else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1147e6fce5b9SRobert Olsson 			pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1148a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
1149a553e4a6SJamal Hadi Salim 		else if (strcmp(f, "IPSEC") == 0)
1150a553e4a6SJamal Hadi Salim 			pkt_dev->flags |= F_IPSEC_ON;
1151a553e4a6SJamal Hadi Salim #endif
1152a553e4a6SJamal Hadi Salim 
11531ca7768cSFrancesco Fondelli 		else if (strcmp(f, "!IPV6") == 0)
11541ca7768cSFrancesco Fondelli 			pkt_dev->flags &= ~F_IPV6;
11551ca7768cSFrancesco Fondelli 
11561da177e4SLinus Torvalds 		else {
1157222f1806SLuiz Capitulino 			sprintf(pg_result,
1158222f1806SLuiz Capitulino 				"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
11591da177e4SLinus Torvalds 				f,
11601ca7768cSFrancesco Fondelli 				"IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1161a553e4a6SJamal Hadi Salim 				"MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
11621da177e4SLinus Torvalds 			return count;
11631da177e4SLinus Torvalds 		}
11641da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
11651da177e4SLinus Torvalds 		return count;
11661da177e4SLinus Torvalds 	}
11671da177e4SLinus Torvalds 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
11681da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1169222f1806SLuiz Capitulino 		if (len < 0) {
1170222f1806SLuiz Capitulino 			return len;
1171222f1806SLuiz Capitulino 		}
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
11741da177e4SLinus Torvalds 			return -EFAULT;
11751da177e4SLinus Torvalds 		buf[len] = 0;
11761da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_min) != 0) {
11771da177e4SLinus Torvalds 			memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
11781da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_min, buf, len);
11791da177e4SLinus Torvalds 			pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
11801da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_min;
11811da177e4SLinus Torvalds 		}
11821da177e4SLinus Torvalds 		if (debug)
118325a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1184222f1806SLuiz Capitulino 			       pkt_dev->dst_min);
11851da177e4SLinus Torvalds 		i += len;
11861da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
11871da177e4SLinus Torvalds 		return count;
11881da177e4SLinus Torvalds 	}
11891da177e4SLinus Torvalds 	if (!strcmp(name, "dst_max")) {
11901da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1191222f1806SLuiz Capitulino 		if (len < 0) {
1192222f1806SLuiz Capitulino 			return len;
1193222f1806SLuiz Capitulino 		}
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
11961da177e4SLinus Torvalds 			return -EFAULT;
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 		buf[len] = 0;
11991da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->dst_max) != 0) {
12001da177e4SLinus Torvalds 			memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
12011da177e4SLinus Torvalds 			strncpy(pkt_dev->dst_max, buf, len);
12021da177e4SLinus Torvalds 			pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
12031da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->daddr_max;
12041da177e4SLinus Torvalds 		}
12051da177e4SLinus Torvalds 		if (debug)
120625a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1207222f1806SLuiz Capitulino 			       pkt_dev->dst_max);
12081da177e4SLinus Torvalds 		i += len;
12091da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
12101da177e4SLinus Torvalds 		return count;
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds 	if (!strcmp(name, "dst6")) {
12131da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1214222f1806SLuiz Capitulino 		if (len < 0)
1215222f1806SLuiz Capitulino 			return len;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12201da177e4SLinus Torvalds 			return -EFAULT;
12211da177e4SLinus Torvalds 		buf[len] = 0;
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
12241da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 		if (debug)
122925a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 		i += len;
12321da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6=%s", buf);
12331da177e4SLinus Torvalds 		return count;
12341da177e4SLinus Torvalds 	}
12351da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_min")) {
12361da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1237222f1806SLuiz Capitulino 		if (len < 0)
1238222f1806SLuiz Capitulino 			return len;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12431da177e4SLinus Torvalds 			return -EFAULT;
12441da177e4SLinus Torvalds 		buf[len] = 0;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
12471da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
12481da177e4SLinus Torvalds 
1249222f1806SLuiz Capitulino 		ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1250222f1806SLuiz Capitulino 			       &pkt_dev->min_in6_daddr);
12511da177e4SLinus Torvalds 		if (debug)
125225a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 		i += len;
12551da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_min=%s", buf);
12561da177e4SLinus Torvalds 		return count;
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds 	if (!strcmp(name, "dst6_max")) {
12591da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1260222f1806SLuiz Capitulino 		if (len < 0)
1261222f1806SLuiz Capitulino 			return len;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12661da177e4SLinus Torvalds 			return -EFAULT;
12671da177e4SLinus Torvalds 		buf[len] = 0;
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
12701da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 		if (debug)
127325a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 		i += len;
12761da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dst6_max=%s", buf);
12771da177e4SLinus Torvalds 		return count;
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds 	if (!strcmp(name, "src6")) {
12801da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1281222f1806SLuiz Capitulino 		if (len < 0)
1282222f1806SLuiz Capitulino 			return len;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 		pkt_dev->flags |= F_IPV6;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
12871da177e4SLinus Torvalds 			return -EFAULT;
12881da177e4SLinus Torvalds 		buf[len] = 0;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 		scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
12911da177e4SLinus Torvalds 		fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 		ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 		if (debug)
129625a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 		i += len;
12991da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src6=%s", buf);
13001da177e4SLinus Torvalds 		return count;
13011da177e4SLinus Torvalds 	}
13021da177e4SLinus Torvalds 	if (!strcmp(name, "src_min")) {
13031da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1304222f1806SLuiz Capitulino 		if (len < 0) {
1305222f1806SLuiz Capitulino 			return len;
1306222f1806SLuiz Capitulino 		}
13071da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13081da177e4SLinus Torvalds 			return -EFAULT;
13091da177e4SLinus Torvalds 		buf[len] = 0;
13101da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_min) != 0) {
13111da177e4SLinus Torvalds 			memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
13121da177e4SLinus Torvalds 			strncpy(pkt_dev->src_min, buf, len);
13131da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
13141da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_min;
13151da177e4SLinus Torvalds 		}
13161da177e4SLinus Torvalds 		if (debug)
131725a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1318222f1806SLuiz Capitulino 			       pkt_dev->src_min);
13191da177e4SLinus Torvalds 		i += len;
13201da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
13211da177e4SLinus Torvalds 		return count;
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds 	if (!strcmp(name, "src_max")) {
13241da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1325222f1806SLuiz Capitulino 		if (len < 0) {
1326222f1806SLuiz Capitulino 			return len;
1327222f1806SLuiz Capitulino 		}
13281da177e4SLinus Torvalds 		if (copy_from_user(buf, &user_buffer[i], len))
13291da177e4SLinus Torvalds 			return -EFAULT;
13301da177e4SLinus Torvalds 		buf[len] = 0;
13311da177e4SLinus Torvalds 		if (strcmp(buf, pkt_dev->src_max) != 0) {
13321da177e4SLinus Torvalds 			memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
13331da177e4SLinus Torvalds 			strncpy(pkt_dev->src_max, buf, len);
13341da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
13351da177e4SLinus Torvalds 			pkt_dev->cur_saddr = pkt_dev->saddr_max;
13361da177e4SLinus Torvalds 		}
13371da177e4SLinus Torvalds 		if (debug)
133825a8b254SDavid S. Miller 			printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1339222f1806SLuiz Capitulino 			       pkt_dev->src_max);
13401da177e4SLinus Torvalds 		i += len;
13411da177e4SLinus Torvalds 		sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
13421da177e4SLinus Torvalds 		return count;
13431da177e4SLinus Torvalds 	}
13441da177e4SLinus Torvalds 	if (!strcmp(name, "dst_mac")) {
13451da177e4SLinus Torvalds 		char *v = valstr;
1346f404e9a6SKris Katterjohn 		unsigned char old_dmac[ETH_ALEN];
13471da177e4SLinus Torvalds 		unsigned char *m = pkt_dev->dst_mac;
1348f404e9a6SKris Katterjohn 		memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1351222f1806SLuiz Capitulino 		if (len < 0) {
1352222f1806SLuiz Capitulino 			return len;
1353222f1806SLuiz Capitulino 		}
13541da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
13551da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
13561da177e4SLinus Torvalds 			return -EFAULT;
13571da177e4SLinus Torvalds 		i += len;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 		for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
13601da177e4SLinus Torvalds 			if (*v >= '0' && *v <= '9') {
13611da177e4SLinus Torvalds 				*m *= 16;
13621da177e4SLinus Torvalds 				*m += *v - '0';
13631da177e4SLinus Torvalds 			}
13641da177e4SLinus Torvalds 			if (*v >= 'A' && *v <= 'F') {
13651da177e4SLinus Torvalds 				*m *= 16;
13661da177e4SLinus Torvalds 				*m += *v - 'A' + 10;
13671da177e4SLinus Torvalds 			}
13681da177e4SLinus Torvalds 			if (*v >= 'a' && *v <= 'f') {
13691da177e4SLinus Torvalds 				*m *= 16;
13701da177e4SLinus Torvalds 				*m += *v - 'a' + 10;
13711da177e4SLinus Torvalds 			}
13721da177e4SLinus Torvalds 			if (*v == ':') {
13731da177e4SLinus Torvalds 				m++;
13741da177e4SLinus Torvalds 				*m = 0;
13751da177e4SLinus Torvalds 			}
13761da177e4SLinus Torvalds 		}
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 		/* Set up Dest MAC */
1379f404e9a6SKris Katterjohn 		if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1380f404e9a6SKris Katterjohn 			memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 		sprintf(pg_result, "OK: dstmac");
13831da177e4SLinus Torvalds 		return count;
13841da177e4SLinus Torvalds 	}
13851da177e4SLinus Torvalds 	if (!strcmp(name, "src_mac")) {
13861da177e4SLinus Torvalds 		char *v = valstr;
1387ce5d0b47SAdit Ranadive 		unsigned char old_smac[ETH_ALEN];
13881da177e4SLinus Torvalds 		unsigned char *m = pkt_dev->src_mac;
13891da177e4SLinus Torvalds 
1390ce5d0b47SAdit Ranadive 		memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1391ce5d0b47SAdit Ranadive 
13921da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1393222f1806SLuiz Capitulino 		if (len < 0) {
1394222f1806SLuiz Capitulino 			return len;
1395222f1806SLuiz Capitulino 		}
13961da177e4SLinus Torvalds 		memset(valstr, 0, sizeof(valstr));
13971da177e4SLinus Torvalds 		if (copy_from_user(valstr, &user_buffer[i], len))
13981da177e4SLinus Torvalds 			return -EFAULT;
13991da177e4SLinus Torvalds 		i += len;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 		for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
14021da177e4SLinus Torvalds 			if (*v >= '0' && *v <= '9') {
14031da177e4SLinus Torvalds 				*m *= 16;
14041da177e4SLinus Torvalds 				*m += *v - '0';
14051da177e4SLinus Torvalds 			}
14061da177e4SLinus Torvalds 			if (*v >= 'A' && *v <= 'F') {
14071da177e4SLinus Torvalds 				*m *= 16;
14081da177e4SLinus Torvalds 				*m += *v - 'A' + 10;
14091da177e4SLinus Torvalds 			}
14101da177e4SLinus Torvalds 			if (*v >= 'a' && *v <= 'f') {
14111da177e4SLinus Torvalds 				*m *= 16;
14121da177e4SLinus Torvalds 				*m += *v - 'a' + 10;
14131da177e4SLinus Torvalds 			}
14141da177e4SLinus Torvalds 			if (*v == ':') {
14151da177e4SLinus Torvalds 				m++;
14161da177e4SLinus Torvalds 				*m = 0;
14171da177e4SLinus Torvalds 			}
14181da177e4SLinus Torvalds 		}
14191da177e4SLinus Torvalds 
1420ce5d0b47SAdit Ranadive 		/* Set up Src MAC */
1421ce5d0b47SAdit Ranadive 		if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1422ce5d0b47SAdit Ranadive 			memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1423ce5d0b47SAdit Ranadive 
14241da177e4SLinus Torvalds 		sprintf(pg_result, "OK: srcmac");
14251da177e4SLinus Torvalds 		return count;
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	if (!strcmp(name, "clear_counters")) {
14291da177e4SLinus Torvalds 		pktgen_clear_counters(pkt_dev);
14301da177e4SLinus Torvalds 		sprintf(pg_result, "OK: Clearing counters.\n");
14311da177e4SLinus Torvalds 		return count;
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	if (!strcmp(name, "flows")) {
14351da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
1436222f1806SLuiz Capitulino 		if (len < 0) {
1437222f1806SLuiz Capitulino 			return len;
1438222f1806SLuiz Capitulino 		}
14391da177e4SLinus Torvalds 		i += len;
14401da177e4SLinus Torvalds 		if (value > MAX_CFLOWS)
14411da177e4SLinus Torvalds 			value = MAX_CFLOWS;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 		pkt_dev->cflows = value;
14441da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
14451da177e4SLinus Torvalds 		return count;
14461da177e4SLinus Torvalds 	}
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	if (!strcmp(name, "flowlen")) {
14491da177e4SLinus Torvalds 		len = num_arg(&user_buffer[i], 10, &value);
1450222f1806SLuiz Capitulino 		if (len < 0) {
1451222f1806SLuiz Capitulino 			return len;
1452222f1806SLuiz Capitulino 		}
14531da177e4SLinus Torvalds 		i += len;
14541da177e4SLinus Torvalds 		pkt_dev->lflow = value;
14551da177e4SLinus Torvalds 		sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
14561da177e4SLinus Torvalds 		return count;
14571da177e4SLinus Torvalds 	}
14581da177e4SLinus Torvalds 
145945b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_min")) {
146045b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
146145b270f8SRobert Olsson 		if (len < 0) {
146245b270f8SRobert Olsson 			return len;
146345b270f8SRobert Olsson 		}
146445b270f8SRobert Olsson 		i += len;
146545b270f8SRobert Olsson 		pkt_dev->queue_map_min = value;
146645b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
146745b270f8SRobert Olsson 		return count;
146845b270f8SRobert Olsson 	}
146945b270f8SRobert Olsson 
147045b270f8SRobert Olsson 	if (!strcmp(name, "queue_map_max")) {
147145b270f8SRobert Olsson 		len = num_arg(&user_buffer[i], 5, &value);
147245b270f8SRobert Olsson 		if (len < 0) {
147345b270f8SRobert Olsson 			return len;
147445b270f8SRobert Olsson 		}
147545b270f8SRobert Olsson 		i += len;
147645b270f8SRobert Olsson 		pkt_dev->queue_map_max = value;
147745b270f8SRobert Olsson 		sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
147845b270f8SRobert Olsson 		return count;
147945b270f8SRobert Olsson 	}
148045b270f8SRobert Olsson 
1481ca6549afSSteven Whitehouse 	if (!strcmp(name, "mpls")) {
1482cfcabdccSStephen Hemminger 		unsigned n, cnt;
1483cfcabdccSStephen Hemminger 
1484ca6549afSSteven Whitehouse 		len = get_labels(&user_buffer[i], pkt_dev);
1485cfcabdccSStephen Hemminger 		if (len < 0)
1486cfcabdccSStephen Hemminger 			return len;
1487ca6549afSSteven Whitehouse 		i += len;
1488cfcabdccSStephen Hemminger 		cnt = sprintf(pg_result, "OK: mpls=");
1489ca6549afSSteven Whitehouse 		for (n = 0; n < pkt_dev->nr_labels; n++)
1490cfcabdccSStephen Hemminger 			cnt += sprintf(pg_result + cnt,
1491ca6549afSSteven Whitehouse 				       "%08x%s", ntohl(pkt_dev->labels[n]),
1492ca6549afSSteven Whitehouse 				       n == pkt_dev->nr_labels-1 ? "" : ",");
149334954ddcSFrancesco Fondelli 
149434954ddcSFrancesco Fondelli 		if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
149534954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
149634954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
149734954ddcSFrancesco Fondelli 
149834954ddcSFrancesco Fondelli 			if (debug)
149925a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
150034954ddcSFrancesco Fondelli 		}
150134954ddcSFrancesco Fondelli 		return count;
150234954ddcSFrancesco Fondelli 	}
150334954ddcSFrancesco Fondelli 
150434954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_id")) {
150534954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
150634954ddcSFrancesco Fondelli 		if (len < 0) {
150734954ddcSFrancesco Fondelli 			return len;
150834954ddcSFrancesco Fondelli 		}
150934954ddcSFrancesco Fondelli 		i += len;
151034954ddcSFrancesco Fondelli 		if (value <= 4095) {
151134954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = value;  /* turn on VLAN */
151234954ddcSFrancesco Fondelli 
151334954ddcSFrancesco Fondelli 			if (debug)
151425a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN turned on\n");
151534954ddcSFrancesco Fondelli 
151634954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
151725a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
151834954ddcSFrancesco Fondelli 
151934954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
152034954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
152134954ddcSFrancesco Fondelli 		} else {
152234954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
152334954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
152434954ddcSFrancesco Fondelli 
152534954ddcSFrancesco Fondelli 			if (debug)
152625a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
152734954ddcSFrancesco Fondelli 		}
152834954ddcSFrancesco Fondelli 		return count;
152934954ddcSFrancesco Fondelli 	}
153034954ddcSFrancesco Fondelli 
153134954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_p")) {
153234954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
153334954ddcSFrancesco Fondelli 		if (len < 0) {
153434954ddcSFrancesco Fondelli 			return len;
153534954ddcSFrancesco Fondelli 		}
153634954ddcSFrancesco Fondelli 		i += len;
153734954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
153834954ddcSFrancesco Fondelli 			pkt_dev->vlan_p = value;
153934954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
154034954ddcSFrancesco Fondelli 		} else {
154134954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_p must be 0-7");
154234954ddcSFrancesco Fondelli 		}
154334954ddcSFrancesco Fondelli 		return count;
154434954ddcSFrancesco Fondelli 	}
154534954ddcSFrancesco Fondelli 
154634954ddcSFrancesco Fondelli 	if (!strcmp(name, "vlan_cfi")) {
154734954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
154834954ddcSFrancesco Fondelli 		if (len < 0) {
154934954ddcSFrancesco Fondelli 			return len;
155034954ddcSFrancesco Fondelli 		}
155134954ddcSFrancesco Fondelli 		i += len;
155234954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
155334954ddcSFrancesco Fondelli 			pkt_dev->vlan_cfi = value;
155434954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
155534954ddcSFrancesco Fondelli 		} else {
155634954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
155734954ddcSFrancesco Fondelli 		}
155834954ddcSFrancesco Fondelli 		return count;
155934954ddcSFrancesco Fondelli 	}
156034954ddcSFrancesco Fondelli 
156134954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_id")) {
156234954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 4, &value);
156334954ddcSFrancesco Fondelli 		if (len < 0) {
156434954ddcSFrancesco Fondelli 			return len;
156534954ddcSFrancesco Fondelli 		}
156634954ddcSFrancesco Fondelli 		i += len;
156734954ddcSFrancesco Fondelli 		if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
156834954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = value;  /* turn on SVLAN */
156934954ddcSFrancesco Fondelli 
157034954ddcSFrancesco Fondelli 			if (debug)
157125a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
157234954ddcSFrancesco Fondelli 
157334954ddcSFrancesco Fondelli 			if (debug && pkt_dev->nr_labels)
157425a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
157534954ddcSFrancesco Fondelli 
157634954ddcSFrancesco Fondelli 			pkt_dev->nr_labels = 0;    /* turn off MPLS */
157734954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
157834954ddcSFrancesco Fondelli 		} else {
157934954ddcSFrancesco Fondelli 			pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
158034954ddcSFrancesco Fondelli 			pkt_dev->svlan_id = 0xffff;
158134954ddcSFrancesco Fondelli 
158234954ddcSFrancesco Fondelli 			if (debug)
158325a8b254SDavid S. Miller 				printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
158434954ddcSFrancesco Fondelli 		}
158534954ddcSFrancesco Fondelli 		return count;
158634954ddcSFrancesco Fondelli 	}
158734954ddcSFrancesco Fondelli 
158834954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_p")) {
158934954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
159034954ddcSFrancesco Fondelli 		if (len < 0) {
159134954ddcSFrancesco Fondelli 			return len;
159234954ddcSFrancesco Fondelli 		}
159334954ddcSFrancesco Fondelli 		i += len;
159434954ddcSFrancesco Fondelli 		if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
159534954ddcSFrancesco Fondelli 			pkt_dev->svlan_p = value;
159634954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
159734954ddcSFrancesco Fondelli 		} else {
159834954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_p must be 0-7");
159934954ddcSFrancesco Fondelli 		}
160034954ddcSFrancesco Fondelli 		return count;
160134954ddcSFrancesco Fondelli 	}
160234954ddcSFrancesco Fondelli 
160334954ddcSFrancesco Fondelli 	if (!strcmp(name, "svlan_cfi")) {
160434954ddcSFrancesco Fondelli 		len = num_arg(&user_buffer[i], 1, &value);
160534954ddcSFrancesco Fondelli 		if (len < 0) {
160634954ddcSFrancesco Fondelli 			return len;
160734954ddcSFrancesco Fondelli 		}
160834954ddcSFrancesco Fondelli 		i += len;
160934954ddcSFrancesco Fondelli 		if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
161034954ddcSFrancesco Fondelli 			pkt_dev->svlan_cfi = value;
161134954ddcSFrancesco Fondelli 			sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
161234954ddcSFrancesco Fondelli 		} else {
161334954ddcSFrancesco Fondelli 			sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
161434954ddcSFrancesco Fondelli 		}
1615ca6549afSSteven Whitehouse 		return count;
1616ca6549afSSteven Whitehouse 	}
1617ca6549afSSteven Whitehouse 
16181ca7768cSFrancesco Fondelli 	if (!strcmp(name, "tos")) {
16191ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16201ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
16211ca7768cSFrancesco Fondelli 		if (len < 0) {
16221ca7768cSFrancesco Fondelli 			return len;
16231ca7768cSFrancesco Fondelli 		}
16241ca7768cSFrancesco Fondelli 		i += len;
16251ca7768cSFrancesco Fondelli 		if (len == 2) {
16261ca7768cSFrancesco Fondelli 			pkt_dev->tos = tmp_value;
16271ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
16281ca7768cSFrancesco Fondelli 		} else {
16291ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: tos must be 00-ff");
16301ca7768cSFrancesco Fondelli 		}
16311ca7768cSFrancesco Fondelli 		return count;
16321ca7768cSFrancesco Fondelli 	}
16331ca7768cSFrancesco Fondelli 
16341ca7768cSFrancesco Fondelli 	if (!strcmp(name, "traffic_class")) {
16351ca7768cSFrancesco Fondelli 		__u32 tmp_value = 0;
16361ca7768cSFrancesco Fondelli 		len = hex32_arg(&user_buffer[i], 2, &tmp_value);
16371ca7768cSFrancesco Fondelli 		if (len < 0) {
16381ca7768cSFrancesco Fondelli 			return len;
16391ca7768cSFrancesco Fondelli 		}
16401ca7768cSFrancesco Fondelli 		i += len;
16411ca7768cSFrancesco Fondelli 		if (len == 2) {
16421ca7768cSFrancesco Fondelli 			pkt_dev->traffic_class = tmp_value;
16431ca7768cSFrancesco Fondelli 			sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
16441ca7768cSFrancesco Fondelli 		} else {
16451ca7768cSFrancesco Fondelli 			sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
16461ca7768cSFrancesco Fondelli 		}
16471ca7768cSFrancesco Fondelli 		return count;
16481ca7768cSFrancesco Fondelli 	}
16491ca7768cSFrancesco Fondelli 
16501da177e4SLinus Torvalds 	sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
16511da177e4SLinus Torvalds 	return -EINVAL;
16521da177e4SLinus Torvalds }
16531da177e4SLinus Torvalds 
1654d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file)
16551da177e4SLinus Torvalds {
1656d50a6b56SStephen Hemminger 	return single_open(file, pktgen_if_show, PDE(inode)->data);
16571da177e4SLinus Torvalds }
16581da177e4SLinus Torvalds 
16599a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = {
1660d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1661d50a6b56SStephen Hemminger 	.open    = pktgen_if_open,
1662d50a6b56SStephen Hemminger 	.read    = seq_read,
1663d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1664d50a6b56SStephen Hemminger 	.write   = pktgen_if_write,
1665d50a6b56SStephen Hemminger 	.release = single_release,
1666d50a6b56SStephen Hemminger };
1667d50a6b56SStephen Hemminger 
1668d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v)
1669d50a6b56SStephen Hemminger {
1670d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
1671648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
1672d50a6b56SStephen Hemminger 
1673d50a6b56SStephen Hemminger 	BUG_ON(!t);
1674d50a6b56SStephen Hemminger 
1675d50a6b56SStephen Hemminger 	seq_printf(seq, "Running: ");
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	if_lock(t);
1678c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
16791da177e4SLinus Torvalds 		if (pkt_dev->running)
168039df232fSStephen Hemminger 			seq_printf(seq, "%s ", pkt_dev->odev->name);
16811da177e4SLinus Torvalds 
1682d50a6b56SStephen Hemminger 	seq_printf(seq, "\nStopped: ");
16831da177e4SLinus Torvalds 
1684c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
16851da177e4SLinus Torvalds 		if (!pkt_dev->running)
168639df232fSStephen Hemminger 			seq_printf(seq, "%s ", pkt_dev->odev->name);
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	if (t->result[0])
1689d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: %s\n", t->result);
16901da177e4SLinus Torvalds 	else
1691d50a6b56SStephen Hemminger 		seq_printf(seq, "\nResult: NA\n");
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 	if_unlock(t);
16941da177e4SLinus Torvalds 
1695d50a6b56SStephen Hemminger 	return 0;
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds 
1698d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file,
1699d50a6b56SStephen Hemminger 				   const char __user * user_buffer,
1700d50a6b56SStephen Hemminger 				   size_t count, loff_t * offset)
17011da177e4SLinus Torvalds {
1702d50a6b56SStephen Hemminger 	struct seq_file *seq = (struct seq_file *)file->private_data;
1703d50a6b56SStephen Hemminger 	struct pktgen_thread *t = seq->private;
17041da177e4SLinus Torvalds 	int i = 0, max, len, ret;
17051da177e4SLinus Torvalds 	char name[40];
17061da177e4SLinus Torvalds 	char *pg_result;
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	if (count < 1) {
17091da177e4SLinus Torvalds 		//      sprintf(pg_result, "Wrong command format");
17101da177e4SLinus Torvalds 		return -EINVAL;
17111da177e4SLinus Torvalds 	}
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 	max = count - i;
17141da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
17151da177e4SLinus Torvalds 	if (len < 0)
17161da177e4SLinus Torvalds 		return len;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	i += len;
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	/* Read variable name */
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	len = strn_len(&user_buffer[i], sizeof(name) - 1);
17231da177e4SLinus Torvalds 	if (len < 0)
17241da177e4SLinus Torvalds 		return len;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	memset(name, 0, sizeof(name));
17271da177e4SLinus Torvalds 	if (copy_from_user(name, &user_buffer[i], len))
17281da177e4SLinus Torvalds 		return -EFAULT;
17291da177e4SLinus Torvalds 	i += len;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	max = count - i;
17321da177e4SLinus Torvalds 	len = count_trail_chars(&user_buffer[i], max);
17331da177e4SLinus Torvalds 	if (len < 0)
17341da177e4SLinus Torvalds 		return len;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 	i += len;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	if (debug)
173925a8b254SDavid S. Miller 		printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
174025a8b254SDavid S. Miller 		       name, (unsigned long)count);
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds 	if (!t) {
174325a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: No thread\n");
17441da177e4SLinus Torvalds 		ret = -EINVAL;
17451da177e4SLinus Torvalds 		goto out;
17461da177e4SLinus Torvalds 	}
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	pg_result = &(t->result[0]);
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 	if (!strcmp(name, "add_device")) {
17511da177e4SLinus Torvalds 		char f[32];
17521da177e4SLinus Torvalds 		memset(f, 0, 32);
17531da177e4SLinus Torvalds 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
17541da177e4SLinus Torvalds 		if (len < 0) {
17551da177e4SLinus Torvalds 			ret = len;
17561da177e4SLinus Torvalds 			goto out;
17571da177e4SLinus Torvalds 		}
17581da177e4SLinus Torvalds 		if (copy_from_user(f, &user_buffer[i], len))
17591da177e4SLinus Torvalds 			return -EFAULT;
17601da177e4SLinus Torvalds 		i += len;
17616146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
17621da177e4SLinus Torvalds 		pktgen_add_device(t, f);
17636146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
17641da177e4SLinus Torvalds 		ret = count;
17651da177e4SLinus Torvalds 		sprintf(pg_result, "OK: add_device=%s", f);
17661da177e4SLinus Torvalds 		goto out;
17671da177e4SLinus Torvalds 	}
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	if (!strcmp(name, "rem_device_all")) {
17706146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
177195ed63f7SArthur Kepner 		t->control |= T_REMDEVALL;
17726146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
1773121caf57SNishanth Aravamudan 		schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
17741da177e4SLinus Torvalds 		ret = count;
17751da177e4SLinus Torvalds 		sprintf(pg_result, "OK: rem_device_all");
17761da177e4SLinus Torvalds 		goto out;
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	if (!strcmp(name, "max_before_softirq")) {
1780b163911fSRobert Olsson 		sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
17811da177e4SLinus Torvalds 		ret = count;
17821da177e4SLinus Torvalds 		goto out;
17831da177e4SLinus Torvalds 	}
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	ret = -EINVAL;
17861da177e4SLinus Torvalds out:
17871da177e4SLinus Torvalds 	return ret;
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
1790d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file)
17911da177e4SLinus Torvalds {
1792d50a6b56SStephen Hemminger 	return single_open(file, pktgen_thread_show, PDE(inode)->data);
17931da177e4SLinus Torvalds }
17941da177e4SLinus Torvalds 
17959a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = {
1796d50a6b56SStephen Hemminger 	.owner   = THIS_MODULE,
1797d50a6b56SStephen Hemminger 	.open    = pktgen_thread_open,
1798d50a6b56SStephen Hemminger 	.read    = seq_read,
1799d50a6b56SStephen Hemminger 	.llseek  = seq_lseek,
1800d50a6b56SStephen Hemminger 	.write   = pktgen_thread_write,
1801d50a6b56SStephen Hemminger 	.release = single_release,
1802d50a6b56SStephen Hemminger };
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds /* Think find or remove for NN */
18051da177e4SLinus Torvalds static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
18061da177e4SLinus Torvalds {
18071da177e4SLinus Torvalds 	struct pktgen_thread *t;
18081da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
18091da177e4SLinus Torvalds 
1810cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list) {
18111da177e4SLinus Torvalds 		pkt_dev = pktgen_find_dev(t, ifname);
18121da177e4SLinus Torvalds 		if (pkt_dev) {
18131da177e4SLinus Torvalds 			if (remove) {
18141da177e4SLinus Torvalds 				if_lock(t);
181595ed63f7SArthur Kepner 				pkt_dev->removal_mark = 1;
181695ed63f7SArthur Kepner 				t->control |= T_REMDEV;
18171da177e4SLinus Torvalds 				if_unlock(t);
18181da177e4SLinus Torvalds 			}
18191da177e4SLinus Torvalds 			break;
18201da177e4SLinus Torvalds 		}
18211da177e4SLinus Torvalds 	}
18221da177e4SLinus Torvalds 	return pkt_dev;
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds 
182595ed63f7SArthur Kepner /*
182695ed63f7SArthur Kepner  * mark a device for removal
182795ed63f7SArthur Kepner  */
182839df232fSStephen Hemminger static void pktgen_mark_device(const char *ifname)
18291da177e4SLinus Torvalds {
18301da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
183195ed63f7SArthur Kepner 	const int max_tries = 10, msec_per_try = 125;
183295ed63f7SArthur Kepner 	int i = 0;
183395ed63f7SArthur Kepner 
18346146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
183525c4e53aSStephen Hemminger 	pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
183695ed63f7SArthur Kepner 
183795ed63f7SArthur Kepner 	while (1) {
183895ed63f7SArthur Kepner 
183995ed63f7SArthur Kepner 		pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1840222f1806SLuiz Capitulino 		if (pkt_dev == NULL)
1841222f1806SLuiz Capitulino 			break;	/* success */
184295ed63f7SArthur Kepner 
18436146e6a4SLuiz Capitulino 		mutex_unlock(&pktgen_thread_lock);
184425c4e53aSStephen Hemminger 		pr_debug("pktgen: pktgen_mark_device waiting for %s "
184525c4e53aSStephen Hemminger 				"to disappear....\n", ifname);
184695ed63f7SArthur Kepner 		schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
18476146e6a4SLuiz Capitulino 		mutex_lock(&pktgen_thread_lock);
184895ed63f7SArthur Kepner 
184995ed63f7SArthur Kepner 		if (++i >= max_tries) {
185025a8b254SDavid S. Miller 			printk(KERN_ERR "pktgen_mark_device: timed out after "
185125a8b254SDavid S. Miller 			       "waiting %d msec for device %s to be removed\n",
185295ed63f7SArthur Kepner 			       msec_per_try * i, ifname);
185395ed63f7SArthur Kepner 			break;
185495ed63f7SArthur Kepner 		}
185595ed63f7SArthur Kepner 
185695ed63f7SArthur Kepner 	}
185795ed63f7SArthur Kepner 
18586146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
185939df232fSStephen Hemminger }
186095ed63f7SArthur Kepner 
186139df232fSStephen Hemminger static void pktgen_change_name(struct net_device *dev)
186239df232fSStephen Hemminger {
186339df232fSStephen Hemminger 	struct pktgen_thread *t;
186439df232fSStephen Hemminger 
186539df232fSStephen Hemminger 	list_for_each_entry(t, &pktgen_threads, th_list) {
186639df232fSStephen Hemminger 		struct pktgen_dev *pkt_dev;
186739df232fSStephen Hemminger 
186839df232fSStephen Hemminger 		list_for_each_entry(pkt_dev, &t->if_list, list) {
186939df232fSStephen Hemminger 			if (pkt_dev->odev != dev)
187039df232fSStephen Hemminger 				continue;
187139df232fSStephen Hemminger 
187239df232fSStephen Hemminger 			remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
187339df232fSStephen Hemminger 
187439df232fSStephen Hemminger 			pkt_dev->entry = create_proc_entry(dev->name, 0600,
187539df232fSStephen Hemminger 							   pg_proc_dir);
187639df232fSStephen Hemminger 			if (!pkt_dev->entry)
187739df232fSStephen Hemminger 				printk(KERN_ERR "pktgen: can't move proc "
187839df232fSStephen Hemminger 				       " entry for '%s'\n", dev->name);
187939df232fSStephen Hemminger 			break;
188039df232fSStephen Hemminger 		}
188139df232fSStephen Hemminger 	}
18821da177e4SLinus Torvalds }
18831da177e4SLinus Torvalds 
1884222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused,
1885222f1806SLuiz Capitulino 			       unsigned long event, void *ptr)
18861da177e4SLinus Torvalds {
188739df232fSStephen Hemminger 	struct net_device *dev = ptr;
18881da177e4SLinus Torvalds 
1889721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
1890e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1891e9dc8653SEric W. Biederman 
18921da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
18931da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
18941da177e4SLinus Torvalds 	 */
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 	switch (event) {
189739df232fSStephen Hemminger 	case NETDEV_CHANGENAME:
189839df232fSStephen Hemminger 		pktgen_change_name(dev);
18991da177e4SLinus Torvalds 		break;
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
190295ed63f7SArthur Kepner 		pktgen_mark_device(dev->name);
19031da177e4SLinus Torvalds 		break;
19043ff50b79SStephen Hemminger 	}
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	return NOTIFY_DONE;
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
1909e6fce5b9SRobert Olsson static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev, const char *ifname)
1910e6fce5b9SRobert Olsson {
1911e6fce5b9SRobert Olsson 	char b[IFNAMSIZ+5];
1912e6fce5b9SRobert Olsson 	int i = 0;
1913e6fce5b9SRobert Olsson 
1914e6fce5b9SRobert Olsson 	for(i=0; ifname[i] != '@'; i++) {
1915e6fce5b9SRobert Olsson 		if(i == IFNAMSIZ)
1916e6fce5b9SRobert Olsson 			break;
1917e6fce5b9SRobert Olsson 
1918e6fce5b9SRobert Olsson 		b[i] = ifname[i];
1919e6fce5b9SRobert Olsson 	}
1920e6fce5b9SRobert Olsson 	b[i] = 0;
1921e6fce5b9SRobert Olsson 
1922e6fce5b9SRobert Olsson 	return dev_get_by_name(&init_net, b);
1923e6fce5b9SRobert Olsson }
1924e6fce5b9SRobert Olsson 
1925e6fce5b9SRobert Olsson 
19261da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */
19271da177e4SLinus Torvalds 
192839df232fSStephen Hemminger static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1929222f1806SLuiz Capitulino {
19301da177e4SLinus Torvalds 	struct net_device *odev;
193139df232fSStephen Hemminger 	int err;
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	/* Clean old setups */
19341da177e4SLinus Torvalds 	if (pkt_dev->odev) {
19351da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
19361da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
19371da177e4SLinus Torvalds 	}
19381da177e4SLinus Torvalds 
1939e6fce5b9SRobert Olsson 	odev = pktgen_dev_get_by_name(pkt_dev, ifname);
19401da177e4SLinus Torvalds 	if (!odev) {
194125a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
194239df232fSStephen Hemminger 		return -ENODEV;
19431da177e4SLinus Torvalds 	}
194439df232fSStephen Hemminger 
19451da177e4SLinus Torvalds 	if (odev->type != ARPHRD_ETHER) {
194625a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
194739df232fSStephen Hemminger 		err = -EINVAL;
194839df232fSStephen Hemminger 	} else if (!netif_running(odev)) {
194925a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
195039df232fSStephen Hemminger 		err = -ENETDOWN;
195139df232fSStephen Hemminger 	} else {
19521da177e4SLinus Torvalds 		pkt_dev->odev = odev;
195339df232fSStephen Hemminger 		return 0;
195439df232fSStephen Hemminger 	}
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	dev_put(odev);
195739df232fSStephen Hemminger 	return err;
19581da177e4SLinus Torvalds }
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev
19611da177e4SLinus Torvalds  * structure to have the right information to create/send packets
19621da177e4SLinus Torvalds  */
19631da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
19641da177e4SLinus Torvalds {
196564c00d81SAndrew Gallatin 	int ntxq;
196664c00d81SAndrew Gallatin 
19671da177e4SLinus Torvalds 	if (!pkt_dev->odev) {
196825a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
196925a8b254SDavid S. Miller 		       "setup_inject.\n");
1970222f1806SLuiz Capitulino 		sprintf(pkt_dev->result,
1971222f1806SLuiz Capitulino 			"ERROR: pkt_dev->odev == NULL in setup_inject.\n");
19721da177e4SLinus Torvalds 		return;
19731da177e4SLinus Torvalds 	}
19741da177e4SLinus Torvalds 
197564c00d81SAndrew Gallatin 	/* make sure that we don't pick a non-existing transmit queue */
197664c00d81SAndrew Gallatin 	ntxq = pkt_dev->odev->real_num_tx_queues;
1977bfdbc0acSRobert Olsson 
197864c00d81SAndrew Gallatin 	if (ntxq <= pkt_dev->queue_map_min) {
197964c00d81SAndrew Gallatin 		printk(KERN_WARNING "pktgen: WARNING: Requested "
198088271660SJesse Brandeburg 		       "queue_map_min (zero-based) (%d) exceeds valid range "
198188271660SJesse Brandeburg 		       "[0 - %d] for (%d) queues on %s, resetting\n",
198288271660SJesse Brandeburg 		       pkt_dev->queue_map_min, (ntxq ?: 1)- 1, ntxq,
198388271660SJesse Brandeburg 		       pkt_dev->odev->name);
198464c00d81SAndrew Gallatin 		pkt_dev->queue_map_min = ntxq - 1;
198564c00d81SAndrew Gallatin 	}
198688271660SJesse Brandeburg 	if (pkt_dev->queue_map_max >= ntxq) {
198764c00d81SAndrew Gallatin 		printk(KERN_WARNING "pktgen: WARNING: Requested "
198888271660SJesse Brandeburg 		       "queue_map_max (zero-based) (%d) exceeds valid range "
198988271660SJesse Brandeburg 		       "[0 - %d] for (%d) queues on %s, resetting\n",
199088271660SJesse Brandeburg 		       pkt_dev->queue_map_max, (ntxq ?: 1)- 1, ntxq,
199188271660SJesse Brandeburg 		       pkt_dev->odev->name);
199264c00d81SAndrew Gallatin 		pkt_dev->queue_map_max = ntxq - 1;
199364c00d81SAndrew Gallatin 	}
199464c00d81SAndrew Gallatin 
19951da177e4SLinus Torvalds 	/* Default to the interface's mac if not explicitly set. */
19961da177e4SLinus Torvalds 
1997f404e9a6SKris Katterjohn 	if (is_zero_ether_addr(pkt_dev->src_mac))
1998f404e9a6SKris Katterjohn 		memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	/* Set up Dest MAC */
2001f404e9a6SKris Katterjohn 	memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	/* Set up pkt size */
20041da177e4SLinus Torvalds 	pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6) {
20071da177e4SLinus Torvalds 		/*
20081da177e4SLinus Torvalds 		 * Skip this automatic address setting until locks or functions
20091da177e4SLinus Torvalds 		 * gets exported
20101da177e4SLinus Torvalds 		 */
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds #ifdef NOTNOW
20131da177e4SLinus Torvalds 		int i, set = 0, err = 1;
20141da177e4SLinus Torvalds 		struct inet6_dev *idev;
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 		for (i = 0; i < IN6_ADDR_HSIZE; i++)
20171da177e4SLinus Torvalds 			if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
20181da177e4SLinus Torvalds 				set = 1;
20191da177e4SLinus Torvalds 				break;
20201da177e4SLinus Torvalds 			}
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 		if (!set) {
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 			/*
20251da177e4SLinus Torvalds 			 * Use linklevel address if unconfigured.
20261da177e4SLinus Torvalds 			 *
20271da177e4SLinus Torvalds 			 * use ipv6_get_lladdr if/when it's get exported
20281da177e4SLinus Torvalds 			 */
20291da177e4SLinus Torvalds 
20308814c4b5SYOSHIFUJI Hideaki 			rcu_read_lock();
20311da177e4SLinus Torvalds 			if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
20321da177e4SLinus Torvalds 				struct inet6_ifaddr *ifp;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 				read_lock_bh(&idev->lock);
2035222f1806SLuiz Capitulino 				for (ifp = idev->addr_list; ifp;
2036222f1806SLuiz Capitulino 				     ifp = ifp->if_next) {
2037222f1806SLuiz Capitulino 					if (ifp->scope == IFA_LINK
2038222f1806SLuiz Capitulino 					    && !(ifp->
2039222f1806SLuiz Capitulino 						 flags & IFA_F_TENTATIVE)) {
2040222f1806SLuiz Capitulino 						ipv6_addr_copy(&pkt_dev->
2041222f1806SLuiz Capitulino 							       cur_in6_saddr,
2042222f1806SLuiz Capitulino 							       &ifp->addr);
20431da177e4SLinus Torvalds 						err = 0;
20441da177e4SLinus Torvalds 						break;
20451da177e4SLinus Torvalds 					}
20461da177e4SLinus Torvalds 				}
20471da177e4SLinus Torvalds 				read_unlock_bh(&idev->lock);
20481da177e4SLinus Torvalds 			}
20498814c4b5SYOSHIFUJI Hideaki 			rcu_read_unlock();
2050222f1806SLuiz Capitulino 			if (err)
205125a8b254SDavid S. Miller 				printk(KERN_ERR "pktgen: ERROR: IPv6 link "
205225a8b254SDavid S. Miller 				       "address not availble.\n");
20531da177e4SLinus Torvalds 		}
20541da177e4SLinus Torvalds #endif
2055222f1806SLuiz Capitulino 	} else {
20561da177e4SLinus Torvalds 		pkt_dev->saddr_min = 0;
20571da177e4SLinus Torvalds 		pkt_dev->saddr_max = 0;
20581da177e4SLinus Torvalds 		if (strlen(pkt_dev->src_min) == 0) {
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 			struct in_device *in_dev;
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 			rcu_read_lock();
2063e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rcu(pkt_dev->odev);
20641da177e4SLinus Torvalds 			if (in_dev) {
20651da177e4SLinus Torvalds 				if (in_dev->ifa_list) {
2066222f1806SLuiz Capitulino 					pkt_dev->saddr_min =
2067222f1806SLuiz Capitulino 					    in_dev->ifa_list->ifa_address;
20681da177e4SLinus Torvalds 					pkt_dev->saddr_max = pkt_dev->saddr_min;
20691da177e4SLinus Torvalds 				}
20701da177e4SLinus Torvalds 			}
20711da177e4SLinus Torvalds 			rcu_read_unlock();
2072222f1806SLuiz Capitulino 		} else {
20731da177e4SLinus Torvalds 			pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
20741da177e4SLinus Torvalds 			pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
20751da177e4SLinus Torvalds 		}
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 		pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
20781da177e4SLinus Torvalds 		pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
20791da177e4SLinus Torvalds 	}
20801da177e4SLinus Torvalds 	/* Initialize current values. */
20811da177e4SLinus Torvalds 	pkt_dev->cur_dst_mac_offset = 0;
20821da177e4SLinus Torvalds 	pkt_dev->cur_src_mac_offset = 0;
20831da177e4SLinus Torvalds 	pkt_dev->cur_saddr = pkt_dev->saddr_min;
20841da177e4SLinus Torvalds 	pkt_dev->cur_daddr = pkt_dev->daddr_min;
20851da177e4SLinus Torvalds 	pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
20861da177e4SLinus Torvalds 	pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
20871da177e4SLinus Torvalds 	pkt_dev->nflows = 0;
20881da177e4SLinus Torvalds }
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 
2091fd29cf72SStephen Hemminger static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2092fd29cf72SStephen Hemminger {
2093*2bc481cfSStephen Hemminger 	ktime_t start;
2094*2bc481cfSStephen Hemminger 	s32 remaining;
2095*2bc481cfSStephen Hemminger 	struct hrtimer_sleeper t;
2096fd29cf72SStephen Hemminger 
2097*2bc481cfSStephen Hemminger 	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2098*2bc481cfSStephen Hemminger 	hrtimer_set_expires(&t.timer, spin_until);
2099fd29cf72SStephen Hemminger 
2100*2bc481cfSStephen Hemminger 	remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
2101*2bc481cfSStephen Hemminger 	if (remaining <= 0)
21021da177e4SLinus Torvalds 		return;
2103*2bc481cfSStephen Hemminger 
2104*2bc481cfSStephen Hemminger 	start = ktime_now();
2105*2bc481cfSStephen Hemminger 	if (remaining < 100)
2106*2bc481cfSStephen Hemminger 		udelay(remaining); 	/* really small just spin */
2107*2bc481cfSStephen Hemminger 	else {
2108*2bc481cfSStephen Hemminger 		/* see do_nanosleep */
2109*2bc481cfSStephen Hemminger 		hrtimer_init_sleeper(&t, current);
2110*2bc481cfSStephen Hemminger 		do {
2111*2bc481cfSStephen Hemminger 			set_current_state(TASK_INTERRUPTIBLE);
2112*2bc481cfSStephen Hemminger 			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2113*2bc481cfSStephen Hemminger 			if (!hrtimer_active(&t.timer))
2114*2bc481cfSStephen Hemminger 				t.task = NULL;
2115*2bc481cfSStephen Hemminger 
2116*2bc481cfSStephen Hemminger 			if (likely(t.task))
21171da177e4SLinus Torvalds 				schedule();
21181da177e4SLinus Torvalds 
2119*2bc481cfSStephen Hemminger 			hrtimer_cancel(&t.timer);
2120*2bc481cfSStephen Hemminger 		} while (t.task && pkt_dev->running && !signal_pending(current));
2121*2bc481cfSStephen Hemminger 		__set_current_state(TASK_RUNNING);
21221da177e4SLinus Torvalds 	}
2123*2bc481cfSStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), start));
21241da177e4SLinus Torvalds }
21251da177e4SLinus Torvalds 
212616dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
212716dab72fSJamal Hadi Salim {
2128a553e4a6SJamal Hadi Salim 	pkt_dev->pkt_overhead = 0;
212916dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
213016dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
213116dab72fSJamal Hadi Salim 	pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
213216dab72fSJamal Hadi Salim }
213316dab72fSJamal Hadi Salim 
2134648fda74SStephen Hemminger static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2135007a531bSJamal Hadi Salim {
2136648fda74SStephen Hemminger 	return !!(pkt_dev->flows[flow].flags & F_INIT);
2137007a531bSJamal Hadi Salim }
2138007a531bSJamal Hadi Salim 
2139007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev)
2140007a531bSJamal Hadi Salim {
2141007a531bSJamal Hadi Salim 	int flow = pkt_dev->curfl;
2142007a531bSJamal Hadi Salim 
2143007a531bSJamal Hadi Salim 	if (pkt_dev->flags & F_FLOW_SEQ) {
2144007a531bSJamal Hadi Salim 		if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2145007a531bSJamal Hadi Salim 			/* reset time */
2146007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
21471211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
2148007a531bSJamal Hadi Salim 			pkt_dev->curfl += 1;
2149007a531bSJamal Hadi Salim 			if (pkt_dev->curfl >= pkt_dev->cflows)
2150007a531bSJamal Hadi Salim 				pkt_dev->curfl = 0; /*reset */
2151007a531bSJamal Hadi Salim 		}
2152007a531bSJamal Hadi Salim 	} else {
2153007a531bSJamal Hadi Salim 		flow = random32() % pkt_dev->cflows;
21541211a645SRobert Olsson 		pkt_dev->curfl = flow;
2155007a531bSJamal Hadi Salim 
21561211a645SRobert Olsson 		if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2157007a531bSJamal Hadi Salim 			pkt_dev->flows[flow].count = 0;
21581211a645SRobert Olsson 			pkt_dev->flows[flow].flags = 0;
21591211a645SRobert Olsson 		}
2160007a531bSJamal Hadi Salim 	}
2161007a531bSJamal Hadi Salim 
2162007a531bSJamal Hadi Salim 	return pkt_dev->curfl;
2163007a531bSJamal Hadi Salim }
2164007a531bSJamal Hadi Salim 
2165a553e4a6SJamal Hadi Salim 
2166a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2167a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else
2168a553e4a6SJamal Hadi Salim  * we go look for it ...
2169a553e4a6SJamal Hadi Salim */
2170fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2171a553e4a6SJamal Hadi Salim {
2172a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[flow].x;
2173a553e4a6SJamal Hadi Salim 	if (!x) {
2174a553e4a6SJamal Hadi Salim 		/*slow path: we dont already have xfrm_state*/
21755447c5e4SAlexey Dobriyan 		x = xfrm_stateonly_find(&init_net,
21765447c5e4SAlexey Dobriyan 					(xfrm_address_t *)&pkt_dev->cur_daddr,
2177a553e4a6SJamal Hadi Salim 					(xfrm_address_t *)&pkt_dev->cur_saddr,
2178a553e4a6SJamal Hadi Salim 					AF_INET,
2179a553e4a6SJamal Hadi Salim 					pkt_dev->ipsmode,
2180a553e4a6SJamal Hadi Salim 					pkt_dev->ipsproto, 0);
2181a553e4a6SJamal Hadi Salim 		if (x) {
2182a553e4a6SJamal Hadi Salim 			pkt_dev->flows[flow].x = x;
2183a553e4a6SJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
2184a553e4a6SJamal Hadi Salim 			pkt_dev->pkt_overhead+=x->props.header_len;
2185a553e4a6SJamal Hadi Salim 		}
2186a553e4a6SJamal Hadi Salim 
2187a553e4a6SJamal Hadi Salim 	}
2188a553e4a6SJamal Hadi Salim }
2189a553e4a6SJamal Hadi Salim #endif
2190fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2191fd2ea0a7SDavid S. Miller {
2192e6fce5b9SRobert Olsson 
2193e6fce5b9SRobert Olsson 	if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2194e6fce5b9SRobert Olsson 		pkt_dev->cur_queue_map = smp_processor_id();
2195e6fce5b9SRobert Olsson 
2196e6fce5b9SRobert Olsson 	else if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2197fd2ea0a7SDavid S. Miller 		__u16 t;
2198fd2ea0a7SDavid S. Miller 		if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2199fd2ea0a7SDavid S. Miller 			t = random32() %
2200fd2ea0a7SDavid S. Miller 				(pkt_dev->queue_map_max -
2201fd2ea0a7SDavid S. Miller 				 pkt_dev->queue_map_min + 1)
2202fd2ea0a7SDavid S. Miller 				+ pkt_dev->queue_map_min;
2203fd2ea0a7SDavid S. Miller 		} else {
2204fd2ea0a7SDavid S. Miller 			t = pkt_dev->cur_queue_map + 1;
2205fd2ea0a7SDavid S. Miller 			if (t > pkt_dev->queue_map_max)
2206fd2ea0a7SDavid S. Miller 				t = pkt_dev->queue_map_min;
2207fd2ea0a7SDavid S. Miller 		}
2208fd2ea0a7SDavid S. Miller 		pkt_dev->cur_queue_map = t;
2209fd2ea0a7SDavid S. Miller 	}
2210bfdbc0acSRobert Olsson 	pkt_dev->cur_queue_map  = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2211fd2ea0a7SDavid S. Miller }
2212fd2ea0a7SDavid S. Miller 
22131da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values
22141da177e4SLinus Torvalds  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
22151da177e4SLinus Torvalds  */
2216222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2217222f1806SLuiz Capitulino {
22181da177e4SLinus Torvalds 	__u32 imn;
22191da177e4SLinus Torvalds 	__u32 imx;
22201da177e4SLinus Torvalds 	int flow = 0;
22211da177e4SLinus Torvalds 
2222007a531bSJamal Hadi Salim 	if (pkt_dev->cflows)
2223007a531bSJamal Hadi Salim 		flow = f_pick(pkt_dev);
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 	/*  Deal with source MAC */
22261da177e4SLinus Torvalds 	if (pkt_dev->src_mac_count > 1) {
22271da177e4SLinus Torvalds 		__u32 mc;
22281da177e4SLinus Torvalds 		__u32 tmp;
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACSRC_RND)
22315fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->src_mac_count;
22321da177e4SLinus Torvalds 		else {
22331da177e4SLinus Torvalds 			mc = pkt_dev->cur_src_mac_offset++;
2234ff2a79a5SRobert Olsson 			if (pkt_dev->cur_src_mac_offset >=
2235222f1806SLuiz Capitulino 			    pkt_dev->src_mac_count)
22361da177e4SLinus Torvalds 				pkt_dev->cur_src_mac_offset = 0;
22371da177e4SLinus Torvalds 		}
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 		tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
22401da177e4SLinus Torvalds 		pkt_dev->hh[11] = tmp;
22411da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
22421da177e4SLinus Torvalds 		pkt_dev->hh[10] = tmp;
22431da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
22441da177e4SLinus Torvalds 		pkt_dev->hh[9] = tmp;
22451da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
22461da177e4SLinus Torvalds 		pkt_dev->hh[8] = tmp;
22471da177e4SLinus Torvalds 		tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
22481da177e4SLinus Torvalds 		pkt_dev->hh[7] = tmp;
22491da177e4SLinus Torvalds 	}
22501da177e4SLinus Torvalds 
22511da177e4SLinus Torvalds 	/*  Deal with Destination MAC */
22521da177e4SLinus Torvalds 	if (pkt_dev->dst_mac_count > 1) {
22531da177e4SLinus Torvalds 		__u32 mc;
22541da177e4SLinus Torvalds 		__u32 tmp;
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds 		if (pkt_dev->flags & F_MACDST_RND)
22575fa6fc76SStephen Hemminger 			mc = random32() % pkt_dev->dst_mac_count;
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 		else {
22601da177e4SLinus Torvalds 			mc = pkt_dev->cur_dst_mac_offset++;
2261ff2a79a5SRobert Olsson 			if (pkt_dev->cur_dst_mac_offset >=
2262222f1806SLuiz Capitulino 			    pkt_dev->dst_mac_count) {
22631da177e4SLinus Torvalds 				pkt_dev->cur_dst_mac_offset = 0;
22641da177e4SLinus Torvalds 			}
22651da177e4SLinus Torvalds 		}
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 		tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
22681da177e4SLinus Torvalds 		pkt_dev->hh[5] = tmp;
22691da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
22701da177e4SLinus Torvalds 		pkt_dev->hh[4] = tmp;
22711da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
22721da177e4SLinus Torvalds 		pkt_dev->hh[3] = tmp;
22731da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
22741da177e4SLinus Torvalds 		pkt_dev->hh[2] = tmp;
22751da177e4SLinus Torvalds 		tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
22761da177e4SLinus Torvalds 		pkt_dev->hh[1] = tmp;
22771da177e4SLinus Torvalds 	}
22781da177e4SLinus Torvalds 
2279ca6549afSSteven Whitehouse 	if (pkt_dev->flags & F_MPLS_RND) {
2280ca6549afSSteven Whitehouse 		unsigned i;
2281ca6549afSSteven Whitehouse 		for (i = 0; i < pkt_dev->nr_labels; i++)
2282ca6549afSSteven Whitehouse 			if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2283ca6549afSSteven Whitehouse 				pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
22845fa6fc76SStephen Hemminger 					     ((__force __be32)random32() &
2285ca6549afSSteven Whitehouse 						      htonl(0x000fffff));
2286ca6549afSSteven Whitehouse 	}
2287ca6549afSSteven Whitehouse 
228834954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
22895fa6fc76SStephen Hemminger 		pkt_dev->vlan_id = random32() & (4096-1);
229034954ddcSFrancesco Fondelli 	}
229134954ddcSFrancesco Fondelli 
229234954ddcSFrancesco Fondelli 	if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
22935fa6fc76SStephen Hemminger 		pkt_dev->svlan_id = random32() & (4096 - 1);
229434954ddcSFrancesco Fondelli 	}
229534954ddcSFrancesco Fondelli 
22961da177e4SLinus Torvalds 	if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
22971da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPSRC_RND)
22985fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_src = random32() %
22995fa6fc76SStephen Hemminger 				(pkt_dev->udp_src_max - pkt_dev->udp_src_min)
23005fa6fc76SStephen Hemminger 				+ pkt_dev->udp_src_min;
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds 		else {
23031da177e4SLinus Torvalds 			pkt_dev->cur_udp_src++;
23041da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
23051da177e4SLinus Torvalds 				pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
23061da177e4SLinus Torvalds 		}
23071da177e4SLinus Torvalds 	}
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 	if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
23101da177e4SLinus Torvalds 		if (pkt_dev->flags & F_UDPDST_RND) {
23115fa6fc76SStephen Hemminger 			pkt_dev->cur_udp_dst = random32() %
23125fa6fc76SStephen Hemminger 				(pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
23135fa6fc76SStephen Hemminger 				+ pkt_dev->udp_dst_min;
2314222f1806SLuiz Capitulino 		} else {
23151da177e4SLinus Torvalds 			pkt_dev->cur_udp_dst++;
23161da177e4SLinus Torvalds 			if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
23171da177e4SLinus Torvalds 				pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
23181da177e4SLinus Torvalds 		}
23191da177e4SLinus Torvalds 	}
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds 	if (!(pkt_dev->flags & F_IPV6)) {
23221da177e4SLinus Torvalds 
2323222f1806SLuiz Capitulino 		if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2324222f1806SLuiz Capitulino 							 ntohl(pkt_dev->
2325222f1806SLuiz Capitulino 							       saddr_max))) {
23261da177e4SLinus Torvalds 			__u32 t;
23271da177e4SLinus Torvalds 			if (pkt_dev->flags & F_IPSRC_RND)
23285fa6fc76SStephen Hemminger 				t = random32() % (imx - imn) + imn;
23291da177e4SLinus Torvalds 			else {
23301da177e4SLinus Torvalds 				t = ntohl(pkt_dev->cur_saddr);
23311da177e4SLinus Torvalds 				t++;
23321da177e4SLinus Torvalds 				if (t > imx) {
23331da177e4SLinus Torvalds 					t = imn;
23341da177e4SLinus Torvalds 				}
23351da177e4SLinus Torvalds 			}
23361da177e4SLinus Torvalds 			pkt_dev->cur_saddr = htonl(t);
23371da177e4SLinus Torvalds 		}
23381da177e4SLinus Torvalds 
2339007a531bSJamal Hadi Salim 		if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
23401da177e4SLinus Torvalds 			pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
23411da177e4SLinus Torvalds 		} else {
2342252e3346SAl Viro 			imn = ntohl(pkt_dev->daddr_min);
2343252e3346SAl Viro 			imx = ntohl(pkt_dev->daddr_max);
2344252e3346SAl Viro 			if (imn < imx) {
23451da177e4SLinus Torvalds 				__u32 t;
2346252e3346SAl Viro 				__be32 s;
23471da177e4SLinus Torvalds 				if (pkt_dev->flags & F_IPDST_RND) {
23481da177e4SLinus Torvalds 
23495fa6fc76SStephen Hemminger 					t = random32() % (imx - imn) + imn;
2350252e3346SAl Viro 					s = htonl(t);
23511da177e4SLinus Torvalds 
235221cf2253SJoe Perches 					while (ipv4_is_loopback(s) ||
235321cf2253SJoe Perches 					       ipv4_is_multicast(s) ||
23541e637c74SJan Engelhardt 					       ipv4_is_lbcast(s) ||
235521cf2253SJoe Perches 					       ipv4_is_zeronet(s) ||
235621cf2253SJoe Perches 					       ipv4_is_local_multicast(s)) {
23575fa6fc76SStephen Hemminger 						t = random32() % (imx - imn) + imn;
2358252e3346SAl Viro 						s = htonl(t);
23591da177e4SLinus Torvalds 					}
2360252e3346SAl Viro 					pkt_dev->cur_daddr = s;
2361252e3346SAl Viro 				} else {
23621da177e4SLinus Torvalds 					t = ntohl(pkt_dev->cur_daddr);
23631da177e4SLinus Torvalds 					t++;
23641da177e4SLinus Torvalds 					if (t > imx) {
23651da177e4SLinus Torvalds 						t = imn;
23661da177e4SLinus Torvalds 					}
23671da177e4SLinus Torvalds 					pkt_dev->cur_daddr = htonl(t);
23681da177e4SLinus Torvalds 				}
23691da177e4SLinus Torvalds 			}
23701da177e4SLinus Torvalds 			if (pkt_dev->cflows) {
2371007a531bSJamal Hadi Salim 				pkt_dev->flows[flow].flags |= F_INIT;
2372222f1806SLuiz Capitulino 				pkt_dev->flows[flow].cur_daddr =
2373222f1806SLuiz Capitulino 				    pkt_dev->cur_daddr;
2374a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2375a553e4a6SJamal Hadi Salim 				if (pkt_dev->flags & F_IPSEC_ON)
2376a553e4a6SJamal Hadi Salim 					get_ipsec_sa(pkt_dev, flow);
2377a553e4a6SJamal Hadi Salim #endif
23781da177e4SLinus Torvalds 				pkt_dev->nflows++;
23791da177e4SLinus Torvalds 			}
23801da177e4SLinus Torvalds 		}
2381222f1806SLuiz Capitulino 	} else {		/* IPV6 * */
2382222f1806SLuiz Capitulino 
23831da177e4SLinus Torvalds 		if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
23841da177e4SLinus Torvalds 		    pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
23851da177e4SLinus Torvalds 		    pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
23861da177e4SLinus Torvalds 		    pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
23871da177e4SLinus Torvalds 		else {
23881da177e4SLinus Torvalds 			int i;
23891da177e4SLinus Torvalds 
23901da177e4SLinus Torvalds 			/* Only random destinations yet */
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 			for (i = 0; i < 4; i++) {
23931da177e4SLinus Torvalds 				pkt_dev->cur_in6_daddr.s6_addr32[i] =
23945fa6fc76SStephen Hemminger 				    (((__force __be32)random32() |
23951da177e4SLinus Torvalds 				      pkt_dev->min_in6_daddr.s6_addr32[i]) &
23961da177e4SLinus Torvalds 				     pkt_dev->max_in6_daddr.s6_addr32[i]);
23971da177e4SLinus Torvalds 			}
23981da177e4SLinus Torvalds 		}
23991da177e4SLinus Torvalds 	}
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 	if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
24021da177e4SLinus Torvalds 		__u32 t;
24031da177e4SLinus Torvalds 		if (pkt_dev->flags & F_TXSIZE_RND) {
24045fa6fc76SStephen Hemminger 			t = random32() %
24055fa6fc76SStephen Hemminger 				(pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
24065fa6fc76SStephen Hemminger 				+ pkt_dev->min_pkt_size;
2407222f1806SLuiz Capitulino 		} else {
24081da177e4SLinus Torvalds 			t = pkt_dev->cur_pkt_size + 1;
24091da177e4SLinus Torvalds 			if (t > pkt_dev->max_pkt_size)
24101da177e4SLinus Torvalds 				t = pkt_dev->min_pkt_size;
24111da177e4SLinus Torvalds 		}
24121da177e4SLinus Torvalds 		pkt_dev->cur_pkt_size = t;
24131da177e4SLinus Torvalds 	}
24141da177e4SLinus Torvalds 
2415fd2ea0a7SDavid S. Miller 	set_cur_queue_map(pkt_dev);
241645b270f8SRobert Olsson 
24171da177e4SLinus Torvalds 	pkt_dev->flows[flow].count++;
24181da177e4SLinus Torvalds }
24191da177e4SLinus Torvalds 
2420a553e4a6SJamal Hadi Salim 
2421a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2422a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2423a553e4a6SJamal Hadi Salim {
2424a553e4a6SJamal Hadi Salim 	struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2425a553e4a6SJamal Hadi Salim 	int err = 0;
2426a553e4a6SJamal Hadi Salim 	struct iphdr *iph;
2427a553e4a6SJamal Hadi Salim 
2428a553e4a6SJamal Hadi Salim 	if (!x)
2429a553e4a6SJamal Hadi Salim 		return 0;
2430a553e4a6SJamal Hadi Salim 	/* XXX: we dont support tunnel mode for now until
2431a553e4a6SJamal Hadi Salim 	 * we resolve the dst issue */
2432a553e4a6SJamal Hadi Salim 	if (x->props.mode != XFRM_MODE_TRANSPORT)
2433a553e4a6SJamal Hadi Salim 		return 0;
2434a553e4a6SJamal Hadi Salim 
2435a553e4a6SJamal Hadi Salim 	spin_lock(&x->lock);
2436a553e4a6SJamal Hadi Salim 	iph = ip_hdr(skb);
2437a553e4a6SJamal Hadi Salim 
243813996378SHerbert Xu 	err = x->outer_mode->output(x, skb);
2439a553e4a6SJamal Hadi Salim 	if (err)
2440a553e4a6SJamal Hadi Salim 		goto error;
2441a553e4a6SJamal Hadi Salim 	err = x->type->output(x, skb);
2442a553e4a6SJamal Hadi Salim 	if (err)
2443a553e4a6SJamal Hadi Salim 		goto error;
2444a553e4a6SJamal Hadi Salim 
2445a553e4a6SJamal Hadi Salim 	x->curlft.bytes +=skb->len;
2446a553e4a6SJamal Hadi Salim 	x->curlft.packets++;
2447a553e4a6SJamal Hadi Salim error:
2448a553e4a6SJamal Hadi Salim 	spin_unlock(&x->lock);
2449a553e4a6SJamal Hadi Salim 	return err;
2450a553e4a6SJamal Hadi Salim }
2451a553e4a6SJamal Hadi Salim 
2452475ac1e4SStephen Hemminger static void free_SAs(struct pktgen_dev *pkt_dev)
2453a553e4a6SJamal Hadi Salim {
2454a553e4a6SJamal Hadi Salim 	if (pkt_dev->cflows) {
2455a553e4a6SJamal Hadi Salim 		/* let go of the SAs if we have them */
2456a553e4a6SJamal Hadi Salim 		int i = 0;
24575b5f792aSFlorian Westphal 		for (;  i < pkt_dev->cflows; i++) {
2458a553e4a6SJamal Hadi Salim 			struct xfrm_state *x = pkt_dev->flows[i].x;
2459a553e4a6SJamal Hadi Salim 			if (x) {
2460a553e4a6SJamal Hadi Salim 				xfrm_state_put(x);
2461a553e4a6SJamal Hadi Salim 				pkt_dev->flows[i].x = NULL;
2462a553e4a6SJamal Hadi Salim 			}
2463a553e4a6SJamal Hadi Salim 		}
2464a553e4a6SJamal Hadi Salim 	}
2465a553e4a6SJamal Hadi Salim }
2466a553e4a6SJamal Hadi Salim 
2467475ac1e4SStephen Hemminger static int process_ipsec(struct pktgen_dev *pkt_dev,
2468a553e4a6SJamal Hadi Salim 			      struct sk_buff *skb, __be16 protocol)
2469a553e4a6SJamal Hadi Salim {
2470a553e4a6SJamal Hadi Salim 	if (pkt_dev->flags & F_IPSEC_ON) {
2471a553e4a6SJamal Hadi Salim 		struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2472a553e4a6SJamal Hadi Salim 		int nhead = 0;
2473a553e4a6SJamal Hadi Salim 		if (x) {
2474a553e4a6SJamal Hadi Salim 			int ret;
2475a553e4a6SJamal Hadi Salim 			__u8 *eth;
2476a553e4a6SJamal Hadi Salim 			nhead = x->props.header_len - skb_headroom(skb);
2477a553e4a6SJamal Hadi Salim 			if (nhead >0) {
2478a553e4a6SJamal Hadi Salim 				ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2479a553e4a6SJamal Hadi Salim 				if (ret < 0) {
248025a8b254SDavid S. Miller 					printk(KERN_ERR "Error expanding "
248125a8b254SDavid S. Miller 					       "ipsec packet %d\n",ret);
2482b4bb4ac8SIlpo Järvinen 					goto err;
2483a553e4a6SJamal Hadi Salim 				}
2484a553e4a6SJamal Hadi Salim 			}
2485a553e4a6SJamal Hadi Salim 
2486a553e4a6SJamal Hadi Salim 			/* ipsec is not expecting ll header */
2487a553e4a6SJamal Hadi Salim 			skb_pull(skb, ETH_HLEN);
2488a553e4a6SJamal Hadi Salim 			ret = pktgen_output_ipsec(skb, pkt_dev);
2489a553e4a6SJamal Hadi Salim 			if (ret) {
249025a8b254SDavid S. Miller 				printk(KERN_ERR "Error creating ipsec "
249125a8b254SDavid S. Miller 				       "packet %d\n",ret);
2492b4bb4ac8SIlpo Järvinen 				goto err;
2493a553e4a6SJamal Hadi Salim 			}
2494a553e4a6SJamal Hadi Salim 			/* restore ll */
2495a553e4a6SJamal Hadi Salim 			eth = (__u8 *) skb_push(skb, ETH_HLEN);
2496a553e4a6SJamal Hadi Salim 			memcpy(eth, pkt_dev->hh, 12);
2497a553e4a6SJamal Hadi Salim 			*(u16 *) & eth[12] = protocol;
2498a553e4a6SJamal Hadi Salim 		}
2499a553e4a6SJamal Hadi Salim 	}
2500a553e4a6SJamal Hadi Salim 	return 1;
2501b4bb4ac8SIlpo Järvinen err:
2502b4bb4ac8SIlpo Järvinen 	kfree_skb(skb);
2503b4bb4ac8SIlpo Järvinen 	return 0;
2504a553e4a6SJamal Hadi Salim }
2505a553e4a6SJamal Hadi Salim #endif
2506a553e4a6SJamal Hadi Salim 
2507ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2508ca6549afSSteven Whitehouse {
2509ca6549afSSteven Whitehouse 	unsigned i;
2510ca6549afSSteven Whitehouse 	for (i = 0; i < pkt_dev->nr_labels; i++) {
2511ca6549afSSteven Whitehouse 		*mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2512ca6549afSSteven Whitehouse 	}
2513ca6549afSSteven Whitehouse 	mpls--;
2514ca6549afSSteven Whitehouse 	*mpls |= MPLS_STACK_BOTTOM;
2515ca6549afSSteven Whitehouse }
2516ca6549afSSteven Whitehouse 
25170f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi,
25180f37c605SAl Viro 			       unsigned int prio)
25190f37c605SAl Viro {
25200f37c605SAl Viro 	return htons(id | (cfi << 12) | (prio << 13));
25210f37c605SAl Viro }
25220f37c605SAl Viro 
25231da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
25241da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
25251da177e4SLinus Torvalds {
25261da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
25271da177e4SLinus Torvalds 	__u8 *eth;
25281da177e4SLinus Torvalds 	struct udphdr *udph;
25291da177e4SLinus Torvalds 	int datalen, iplen;
25301da177e4SLinus Torvalds 	struct iphdr *iph;
25311da177e4SLinus Torvalds 	struct pktgen_hdr *pgh = NULL;
2532d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IP);
2533ca6549afSSteven Whitehouse 	__be32 *mpls;
253434954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
253534954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
253634954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
253734954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2538fd2ea0a7SDavid S. Miller 	u16 queue_map;
2539ca6549afSSteven Whitehouse 
2540ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2541d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
25421da177e4SLinus Torvalds 
254334954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2544d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
254534954ddcSFrancesco Fondelli 
254664053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
254764053beeSRobert Olsson 	 * fields.
254864053beeSRobert Olsson 	 */
2549fd2ea0a7SDavid S. Miller 	queue_map = pkt_dev->cur_queue_map;
255064053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
255164053beeSRobert Olsson 
25527ac5459eSDavid S. Miller 	datalen = (odev->hard_header_len + 16) & ~0xf;
2553e470757dSStephen Hemminger 	skb = __netdev_alloc_skb(odev,
2554e470757dSStephen Hemminger 				 pkt_dev->cur_pkt_size + 64
2555e470757dSStephen Hemminger 				 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
25561da177e4SLinus Torvalds 	if (!skb) {
25571da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
25581da177e4SLinus Torvalds 		return NULL;
25591da177e4SLinus Torvalds 	}
25601da177e4SLinus Torvalds 
25617ac5459eSDavid S. Miller 	skb_reserve(skb, datalen);
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
25641da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2565ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2566ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2567ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
256834954ddcSFrancesco Fondelli 
256934954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
257034954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
257134954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
25720f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
25730f37c605SAl Viro 					       pkt_dev->svlan_cfi,
25740f37c605SAl Viro 					       pkt_dev->svlan_p);
257534954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2576d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
257734954ddcSFrancesco Fondelli 		}
257834954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
25790f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
25800f37c605SAl Viro 				      pkt_dev->vlan_cfi,
25810f37c605SAl Viro 				      pkt_dev->vlan_p);
258234954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2583d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IP);
258434954ddcSFrancesco Fondelli 	}
258534954ddcSFrancesco Fondelli 
258627a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
2587b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct iphdr);
2588ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2589fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
2590ddc7b8e3SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
2591ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2594252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
25951da177e4SLinus Torvalds 
2596ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2597ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
259816dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
25991da177e4SLinus Torvalds 	if (datalen < sizeof(struct pktgen_hdr))
26001da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
26031da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
26041da177e4SLinus Torvalds 	udph->len = htons(datalen + 8);	/* DATA + udphdr */
26051da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds 	iph->ihl = 5;
26081da177e4SLinus Torvalds 	iph->version = 4;
26091da177e4SLinus Torvalds 	iph->ttl = 32;
26101ca7768cSFrancesco Fondelli 	iph->tos = pkt_dev->tos;
26111da177e4SLinus Torvalds 	iph->protocol = IPPROTO_UDP;	/* UDP */
26121da177e4SLinus Torvalds 	iph->saddr = pkt_dev->cur_saddr;
26131da177e4SLinus Torvalds 	iph->daddr = pkt_dev->cur_daddr;
26141da177e4SLinus Torvalds 	iph->frag_off = 0;
26151da177e4SLinus Torvalds 	iplen = 20 + 8 + datalen;
26161da177e4SLinus Torvalds 	iph->tot_len = htons(iplen);
26171da177e4SLinus Torvalds 	iph->check = 0;
26181da177e4SLinus Torvalds 	iph->check = ip_fast_csum((void *)iph, iph->ihl);
2619ca6549afSSteven Whitehouse 	skb->protocol = protocol;
2620b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
262116dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
26221da177e4SLinus Torvalds 	skb->dev = odev;
26231da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	if (pkt_dev->nfrags <= 0)
26261da177e4SLinus Torvalds 		pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
26271da177e4SLinus Torvalds 	else {
26281da177e4SLinus Torvalds 		int frags = pkt_dev->nfrags;
26291da177e4SLinus Torvalds 		int i;
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 		pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds 		if (frags > MAX_SKB_FRAGS)
26341da177e4SLinus Torvalds 			frags = MAX_SKB_FRAGS;
26351da177e4SLinus Torvalds 		if (datalen > frags * PAGE_SIZE) {
26361da177e4SLinus Torvalds 			skb_put(skb, datalen - frags * PAGE_SIZE);
26371da177e4SLinus Torvalds 			datalen = frags * PAGE_SIZE;
26381da177e4SLinus Torvalds 		}
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds 		i = 0;
26411da177e4SLinus Torvalds 		while (datalen > 0) {
26421da177e4SLinus Torvalds 			struct page *page = alloc_pages(GFP_KERNEL, 0);
26431da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].page = page;
26441da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].page_offset = 0;
26451da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].size =
26461da177e4SLinus Torvalds 			    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
26471da177e4SLinus Torvalds 			datalen -= skb_shinfo(skb)->frags[i].size;
26481da177e4SLinus Torvalds 			skb->len += skb_shinfo(skb)->frags[i].size;
26491da177e4SLinus Torvalds 			skb->data_len += skb_shinfo(skb)->frags[i].size;
26501da177e4SLinus Torvalds 			i++;
26511da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags = i;
26521da177e4SLinus Torvalds 		}
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 		while (i < frags) {
26551da177e4SLinus Torvalds 			int rem;
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 			if (i == 0)
26581da177e4SLinus Torvalds 				break;
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds 			rem = skb_shinfo(skb)->frags[i - 1].size / 2;
26611da177e4SLinus Torvalds 			if (rem == 0)
26621da177e4SLinus Torvalds 				break;
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i - 1].size -= rem;
26651da177e4SLinus Torvalds 
2666222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i] =
2667222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1];
26681da177e4SLinus Torvalds 			get_page(skb_shinfo(skb)->frags[i].page);
2669222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i].page =
2670222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1].page;
2671222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i].page_offset +=
2672222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1].size;
26731da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].size = rem;
26741da177e4SLinus Torvalds 			i++;
26751da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags = i;
26761da177e4SLinus Torvalds 		}
26771da177e4SLinus Torvalds 	}
26781da177e4SLinus Torvalds 
26791da177e4SLinus Torvalds 	/* Stamp the time, and sequence number, convert them to network byte order */
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds 	if (pgh) {
26821da177e4SLinus Torvalds 		struct timeval timestamp;
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 		pgh->pgh_magic = htonl(PKTGEN_MAGIC);
26851da177e4SLinus Torvalds 		pgh->seq_num = htonl(pkt_dev->seq_num);
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds 		do_gettimeofday(&timestamp);
26881da177e4SLinus Torvalds 		pgh->tv_sec = htonl(timestamp.tv_sec);
26891da177e4SLinus Torvalds 		pgh->tv_usec = htonl(timestamp.tv_usec);
26901da177e4SLinus Torvalds 	}
26911da177e4SLinus Torvalds 
2692a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
2693a553e4a6SJamal Hadi Salim 	if (!process_ipsec(pkt_dev, skb, protocol))
2694a553e4a6SJamal Hadi Salim 		return NULL;
2695a553e4a6SJamal Hadi Salim #endif
2696a553e4a6SJamal Hadi Salim 
26971da177e4SLinus Torvalds 	return skb;
26981da177e4SLinus Torvalds }
26991da177e4SLinus Torvalds 
27001da177e4SLinus Torvalds /*
27011da177e4SLinus Torvalds  * scan_ip6, fmt_ip taken from dietlibc-0.21
27021da177e4SLinus Torvalds  * Author Felix von Leitner <felix-dietlibc@fefe.de>
27031da177e4SLinus Torvalds  *
27041da177e4SLinus Torvalds  * Slightly modified for kernel.
27051da177e4SLinus Torvalds  * Should be candidate for net/ipv4/utils.c
27061da177e4SLinus Torvalds  * --ro
27071da177e4SLinus Torvalds  */
27081da177e4SLinus Torvalds 
27091da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16])
27101da177e4SLinus Torvalds {
27111da177e4SLinus Torvalds 	unsigned int i;
27121da177e4SLinus Torvalds 	unsigned int len = 0;
27131da177e4SLinus Torvalds 	unsigned long u;
27141da177e4SLinus Torvalds 	char suffix[16];
27151da177e4SLinus Torvalds 	unsigned int prefixlen = 0;
27161da177e4SLinus Torvalds 	unsigned int suffixlen = 0;
2717252e3346SAl Viro 	__be32 tmp;
2718cfcabdccSStephen Hemminger 	char *pos;
27191da177e4SLinus Torvalds 
2720222f1806SLuiz Capitulino 	for (i = 0; i < 16; i++)
2721222f1806SLuiz Capitulino 		ip[i] = 0;
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds 	for (;;) {
27241da177e4SLinus Torvalds 		if (*s == ':') {
27251da177e4SLinus Torvalds 			len++;
27261da177e4SLinus Torvalds 			if (s[1] == ':') {	/* Found "::", skip to part 2 */
27271da177e4SLinus Torvalds 				s += 2;
27281da177e4SLinus Torvalds 				len++;
27291da177e4SLinus Torvalds 				break;
27301da177e4SLinus Torvalds 			}
27311da177e4SLinus Torvalds 			s++;
27321da177e4SLinus Torvalds 		}
27331da177e4SLinus Torvalds 
2734cfcabdccSStephen Hemminger 		u = simple_strtoul(s, &pos, 16);
2735cfcabdccSStephen Hemminger 		i = pos - s;
2736222f1806SLuiz Capitulino 		if (!i)
2737222f1806SLuiz Capitulino 			return 0;
27381da177e4SLinus Torvalds 		if (prefixlen == 12 && s[i] == '.') {
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 			/* the last 4 bytes may be written as IPv4 address */
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds 			tmp = in_aton(s);
27431da177e4SLinus Torvalds 			memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
27441da177e4SLinus Torvalds 			return i + len;
27451da177e4SLinus Torvalds 		}
27461da177e4SLinus Torvalds 		ip[prefixlen++] = (u >> 8);
27471da177e4SLinus Torvalds 		ip[prefixlen++] = (u & 255);
2748222f1806SLuiz Capitulino 		s += i;
2749222f1806SLuiz Capitulino 		len += i;
27501da177e4SLinus Torvalds 		if (prefixlen == 16)
27511da177e4SLinus Torvalds 			return len;
27521da177e4SLinus Torvalds 	}
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds /* part 2, after "::" */
27551da177e4SLinus Torvalds 	for (;;) {
27561da177e4SLinus Torvalds 		if (*s == ':') {
27571da177e4SLinus Torvalds 			if (suffixlen == 0)
27581da177e4SLinus Torvalds 				break;
27591da177e4SLinus Torvalds 			s++;
27601da177e4SLinus Torvalds 			len++;
27611da177e4SLinus Torvalds 		} else if (suffixlen != 0)
27621da177e4SLinus Torvalds 			break;
2763cfcabdccSStephen Hemminger 
2764cfcabdccSStephen Hemminger 		u = simple_strtol(s, &pos, 16);
2765cfcabdccSStephen Hemminger 		i = pos - s;
27661da177e4SLinus Torvalds 		if (!i) {
2767222f1806SLuiz Capitulino 			if (*s)
2768222f1806SLuiz Capitulino 				len--;
27691da177e4SLinus Torvalds 			break;
27701da177e4SLinus Torvalds 		}
27711da177e4SLinus Torvalds 		if (suffixlen + prefixlen <= 12 && s[i] == '.') {
27721da177e4SLinus Torvalds 			tmp = in_aton(s);
2773222f1806SLuiz Capitulino 			memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2774222f1806SLuiz Capitulino 			       sizeof(tmp));
27751da177e4SLinus Torvalds 			suffixlen += 4;
27761da177e4SLinus Torvalds 			len += strlen(s);
27771da177e4SLinus Torvalds 			break;
27781da177e4SLinus Torvalds 		}
27791da177e4SLinus Torvalds 		suffix[suffixlen++] = (u >> 8);
27801da177e4SLinus Torvalds 		suffix[suffixlen++] = (u & 255);
2781222f1806SLuiz Capitulino 		s += i;
2782222f1806SLuiz Capitulino 		len += i;
27831da177e4SLinus Torvalds 		if (prefixlen + suffixlen == 16)
27841da177e4SLinus Torvalds 			break;
27851da177e4SLinus Torvalds 	}
27861da177e4SLinus Torvalds 	for (i = 0; i < suffixlen; i++)
27871da177e4SLinus Torvalds 		ip[16 - suffixlen + i] = suffix[i];
27881da177e4SLinus Torvalds 	return len;
27891da177e4SLinus Torvalds }
27901da177e4SLinus Torvalds 
2791222f1806SLuiz Capitulino static char tohex(char hexdigit)
2792222f1806SLuiz Capitulino {
27931da177e4SLinus Torvalds 	return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
27941da177e4SLinus Torvalds }
27951da177e4SLinus Torvalds 
2796222f1806SLuiz Capitulino static int fmt_xlong(char *s, unsigned int i)
2797222f1806SLuiz Capitulino {
27981da177e4SLinus Torvalds 	char *bak = s;
2799222f1806SLuiz Capitulino 	*s = tohex((i >> 12) & 0xf);
2800222f1806SLuiz Capitulino 	if (s != bak || *s != '0')
2801222f1806SLuiz Capitulino 		++s;
2802222f1806SLuiz Capitulino 	*s = tohex((i >> 8) & 0xf);
2803222f1806SLuiz Capitulino 	if (s != bak || *s != '0')
2804222f1806SLuiz Capitulino 		++s;
2805222f1806SLuiz Capitulino 	*s = tohex((i >> 4) & 0xf);
2806222f1806SLuiz Capitulino 	if (s != bak || *s != '0')
2807222f1806SLuiz Capitulino 		++s;
28081da177e4SLinus Torvalds 	*s = tohex(i & 0xf);
28091da177e4SLinus Torvalds 	return s - bak + 1;
28101da177e4SLinus Torvalds }
28111da177e4SLinus Torvalds 
2812222f1806SLuiz Capitulino static unsigned int fmt_ip6(char *s, const char ip[16])
2813222f1806SLuiz Capitulino {
28141da177e4SLinus Torvalds 	unsigned int len;
28151da177e4SLinus Torvalds 	unsigned int i;
28161da177e4SLinus Torvalds 	unsigned int temp;
28171da177e4SLinus Torvalds 	unsigned int compressing;
28181da177e4SLinus Torvalds 	int j;
28191da177e4SLinus Torvalds 
2820222f1806SLuiz Capitulino 	len = 0;
2821222f1806SLuiz Capitulino 	compressing = 0;
28221da177e4SLinus Torvalds 	for (j = 0; j < 16; j += 2) {
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds #ifdef V4MAPPEDPREFIX
28251da177e4SLinus Torvalds 		if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
28261da177e4SLinus Torvalds 			inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
28271da177e4SLinus Torvalds 			temp = strlen(s);
28281da177e4SLinus Torvalds 			return len + temp;
28291da177e4SLinus Torvalds 		}
28301da177e4SLinus Torvalds #endif
28311da177e4SLinus Torvalds 		temp = ((unsigned long)(unsigned char)ip[j] << 8) +
28321da177e4SLinus Torvalds 		    (unsigned long)(unsigned char)ip[j + 1];
28331da177e4SLinus Torvalds 		if (temp == 0) {
28341da177e4SLinus Torvalds 			if (!compressing) {
28351da177e4SLinus Torvalds 				compressing = 1;
28361da177e4SLinus Torvalds 				if (j == 0) {
2837222f1806SLuiz Capitulino 					*s++ = ':';
2838222f1806SLuiz Capitulino 					++len;
28391da177e4SLinus Torvalds 				}
28401da177e4SLinus Torvalds 			}
28411da177e4SLinus Torvalds 		} else {
28421da177e4SLinus Torvalds 			if (compressing) {
28431da177e4SLinus Torvalds 				compressing = 0;
2844222f1806SLuiz Capitulino 				*s++ = ':';
2845222f1806SLuiz Capitulino 				++len;
28461da177e4SLinus Torvalds 			}
2847222f1806SLuiz Capitulino 			i = fmt_xlong(s, temp);
2848222f1806SLuiz Capitulino 			len += i;
2849222f1806SLuiz Capitulino 			s += i;
28501da177e4SLinus Torvalds 			if (j < 14) {
28511da177e4SLinus Torvalds 				*s++ = ':';
28521da177e4SLinus Torvalds 				++len;
28531da177e4SLinus Torvalds 			}
28541da177e4SLinus Torvalds 		}
28551da177e4SLinus Torvalds 	}
28561da177e4SLinus Torvalds 	if (compressing) {
2857222f1806SLuiz Capitulino 		*s++ = ':';
2858222f1806SLuiz Capitulino 		++len;
28591da177e4SLinus Torvalds 	}
28601da177e4SLinus Torvalds 	*s = 0;
28611da177e4SLinus Torvalds 	return len;
28621da177e4SLinus Torvalds }
28631da177e4SLinus Torvalds 
28641da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
28651da177e4SLinus Torvalds 					struct pktgen_dev *pkt_dev)
28661da177e4SLinus Torvalds {
28671da177e4SLinus Torvalds 	struct sk_buff *skb = NULL;
28681da177e4SLinus Torvalds 	__u8 *eth;
28691da177e4SLinus Torvalds 	struct udphdr *udph;
28701da177e4SLinus Torvalds 	int datalen;
28711da177e4SLinus Torvalds 	struct ipv6hdr *iph;
28721da177e4SLinus Torvalds 	struct pktgen_hdr *pgh = NULL;
2873d5f1ce9aSStephen Hemminger 	__be16 protocol = htons(ETH_P_IPV6);
2874ca6549afSSteven Whitehouse 	__be32 *mpls;
287534954ddcSFrancesco Fondelli 	__be16 *vlan_tci = NULL;                 /* Encapsulates priority and VLAN ID */
287634954ddcSFrancesco Fondelli 	__be16 *vlan_encapsulated_proto = NULL;  /* packet type ID field (or len) for VLAN tag */
287734954ddcSFrancesco Fondelli 	__be16 *svlan_tci = NULL;                /* Encapsulates priority and SVLAN ID */
287834954ddcSFrancesco Fondelli 	__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2879fd2ea0a7SDavid S. Miller 	u16 queue_map;
2880ca6549afSSteven Whitehouse 
2881ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2882d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_MPLS_UC);
28831da177e4SLinus Torvalds 
288434954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff)
2885d5f1ce9aSStephen Hemminger 		protocol = htons(ETH_P_8021Q);
288634954ddcSFrancesco Fondelli 
288764053beeSRobert Olsson 	/* Update any of the values, used when we're incrementing various
288864053beeSRobert Olsson 	 * fields.
288964053beeSRobert Olsson 	 */
2890fd2ea0a7SDavid S. Miller 	queue_map = pkt_dev->cur_queue_map;
289164053beeSRobert Olsson 	mod_cur_headers(pkt_dev);
289264053beeSRobert Olsson 
2893e470757dSStephen Hemminger 	skb = __netdev_alloc_skb(odev,
2894e470757dSStephen Hemminger 				 pkt_dev->cur_pkt_size + 64
2895e470757dSStephen Hemminger 				 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
28961da177e4SLinus Torvalds 	if (!skb) {
28971da177e4SLinus Torvalds 		sprintf(pkt_dev->result, "No memory");
28981da177e4SLinus Torvalds 		return NULL;
28991da177e4SLinus Torvalds 	}
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	skb_reserve(skb, 16);
29021da177e4SLinus Torvalds 
29031da177e4SLinus Torvalds 	/*  Reserve for ethernet and IP header  */
29041da177e4SLinus Torvalds 	eth = (__u8 *) skb_push(skb, 14);
2905ca6549afSSteven Whitehouse 	mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2906ca6549afSSteven Whitehouse 	if (pkt_dev->nr_labels)
2907ca6549afSSteven Whitehouse 		mpls_push(mpls, pkt_dev);
290834954ddcSFrancesco Fondelli 
290934954ddcSFrancesco Fondelli 	if (pkt_dev->vlan_id != 0xffff) {
291034954ddcSFrancesco Fondelli 		if (pkt_dev->svlan_id != 0xffff) {
291134954ddcSFrancesco Fondelli 			svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29120f37c605SAl Viro 			*svlan_tci = build_tci(pkt_dev->svlan_id,
29130f37c605SAl Viro 					       pkt_dev->svlan_cfi,
29140f37c605SAl Viro 					       pkt_dev->svlan_p);
291534954ddcSFrancesco Fondelli 			svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2916d5f1ce9aSStephen Hemminger 			*svlan_encapsulated_proto = htons(ETH_P_8021Q);
291734954ddcSFrancesco Fondelli 		}
291834954ddcSFrancesco Fondelli 		vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
29190f37c605SAl Viro 		*vlan_tci = build_tci(pkt_dev->vlan_id,
29200f37c605SAl Viro 				      pkt_dev->vlan_cfi,
29210f37c605SAl Viro 				      pkt_dev->vlan_p);
292234954ddcSFrancesco Fondelli 		vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2923d5f1ce9aSStephen Hemminger 		*vlan_encapsulated_proto = htons(ETH_P_IPV6);
292434954ddcSFrancesco Fondelli 	}
292534954ddcSFrancesco Fondelli 
292627a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
2927b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2928ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2929fd2ea0a7SDavid S. Miller 	skb_set_queue_mapping(skb, queue_map);
2930ddc7b8e3SArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
2931ddc7b8e3SArnaldo Carvalho de Melo 	udph = udp_hdr(skb);
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds 	memcpy(eth, pkt_dev->hh, 12);
2934252e3346SAl Viro 	*(__be16 *) & eth[12] = protocol;
29351da177e4SLinus Torvalds 
2936ca6549afSSteven Whitehouse 	/* Eth + IPh + UDPh + mpls */
2937ca6549afSSteven Whitehouse 	datalen = pkt_dev->cur_pkt_size - 14 -
2938ca6549afSSteven Whitehouse 		  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
293916dab72fSJamal Hadi Salim 		  pkt_dev->pkt_overhead;
29401da177e4SLinus Torvalds 
29411da177e4SLinus Torvalds 	if (datalen < sizeof(struct pktgen_hdr)) {
29421da177e4SLinus Torvalds 		datalen = sizeof(struct pktgen_hdr);
29431da177e4SLinus Torvalds 		if (net_ratelimit())
2944222f1806SLuiz Capitulino 			printk(KERN_INFO "pktgen: increased datalen to %d\n",
2945222f1806SLuiz Capitulino 			       datalen);
29461da177e4SLinus Torvalds 	}
29471da177e4SLinus Torvalds 
29481da177e4SLinus Torvalds 	udph->source = htons(pkt_dev->cur_udp_src);
29491da177e4SLinus Torvalds 	udph->dest = htons(pkt_dev->cur_udp_dst);
29501da177e4SLinus Torvalds 	udph->len = htons(datalen + sizeof(struct udphdr));
29511da177e4SLinus Torvalds 	udph->check = 0;	/* No checksum */
29521da177e4SLinus Torvalds 
2953d5f1ce9aSStephen Hemminger 	*(__be32 *) iph = htonl(0x60000000);	/* Version + flow */
29541da177e4SLinus Torvalds 
29551ca7768cSFrancesco Fondelli 	if (pkt_dev->traffic_class) {
29561ca7768cSFrancesco Fondelli 		/* Version + traffic class + flow (0) */
2957252e3346SAl Viro 		*(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
29581ca7768cSFrancesco Fondelli 	}
29591ca7768cSFrancesco Fondelli 
29601da177e4SLinus Torvalds 	iph->hop_limit = 32;
29611da177e4SLinus Torvalds 
29621da177e4SLinus Torvalds 	iph->payload_len = htons(sizeof(struct udphdr) + datalen);
29631da177e4SLinus Torvalds 	iph->nexthdr = IPPROTO_UDP;
29641da177e4SLinus Torvalds 
29651da177e4SLinus Torvalds 	ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
29661da177e4SLinus Torvalds 	ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
29671da177e4SLinus Torvalds 
2968b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = (skb->network_header - ETH_HLEN -
296916dab72fSJamal Hadi Salim 			   pkt_dev->pkt_overhead);
2970ca6549afSSteven Whitehouse 	skb->protocol = protocol;
29711da177e4SLinus Torvalds 	skb->dev = odev;
29721da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
29731da177e4SLinus Torvalds 
29741da177e4SLinus Torvalds 	if (pkt_dev->nfrags <= 0)
29751da177e4SLinus Torvalds 		pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
29761da177e4SLinus Torvalds 	else {
29771da177e4SLinus Torvalds 		int frags = pkt_dev->nfrags;
29781da177e4SLinus Torvalds 		int i;
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds 		pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
29811da177e4SLinus Torvalds 
29821da177e4SLinus Torvalds 		if (frags > MAX_SKB_FRAGS)
29831da177e4SLinus Torvalds 			frags = MAX_SKB_FRAGS;
29841da177e4SLinus Torvalds 		if (datalen > frags * PAGE_SIZE) {
29851da177e4SLinus Torvalds 			skb_put(skb, datalen - frags * PAGE_SIZE);
29861da177e4SLinus Torvalds 			datalen = frags * PAGE_SIZE;
29871da177e4SLinus Torvalds 		}
29881da177e4SLinus Torvalds 
29891da177e4SLinus Torvalds 		i = 0;
29901da177e4SLinus Torvalds 		while (datalen > 0) {
29911da177e4SLinus Torvalds 			struct page *page = alloc_pages(GFP_KERNEL, 0);
29921da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].page = page;
29931da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].page_offset = 0;
29941da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].size =
29951da177e4SLinus Torvalds 			    (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
29961da177e4SLinus Torvalds 			datalen -= skb_shinfo(skb)->frags[i].size;
29971da177e4SLinus Torvalds 			skb->len += skb_shinfo(skb)->frags[i].size;
29981da177e4SLinus Torvalds 			skb->data_len += skb_shinfo(skb)->frags[i].size;
29991da177e4SLinus Torvalds 			i++;
30001da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags = i;
30011da177e4SLinus Torvalds 		}
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 		while (i < frags) {
30041da177e4SLinus Torvalds 			int rem;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 			if (i == 0)
30071da177e4SLinus Torvalds 				break;
30081da177e4SLinus Torvalds 
30091da177e4SLinus Torvalds 			rem = skb_shinfo(skb)->frags[i - 1].size / 2;
30101da177e4SLinus Torvalds 			if (rem == 0)
30111da177e4SLinus Torvalds 				break;
30121da177e4SLinus Torvalds 
30131da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i - 1].size -= rem;
30141da177e4SLinus Torvalds 
3015222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i] =
3016222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1];
30171da177e4SLinus Torvalds 			get_page(skb_shinfo(skb)->frags[i].page);
3018222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i].page =
3019222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1].page;
3020222f1806SLuiz Capitulino 			skb_shinfo(skb)->frags[i].page_offset +=
3021222f1806SLuiz Capitulino 			    skb_shinfo(skb)->frags[i - 1].size;
30221da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i].size = rem;
30231da177e4SLinus Torvalds 			i++;
30241da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags = i;
30251da177e4SLinus Torvalds 		}
30261da177e4SLinus Torvalds 	}
30271da177e4SLinus Torvalds 
30281da177e4SLinus Torvalds 	/* Stamp the time, and sequence number, convert them to network byte order */
30291da177e4SLinus Torvalds 	/* should we update cloned packets too ? */
30301da177e4SLinus Torvalds 	if (pgh) {
30311da177e4SLinus Torvalds 		struct timeval timestamp;
30321da177e4SLinus Torvalds 
30331da177e4SLinus Torvalds 		pgh->pgh_magic = htonl(PKTGEN_MAGIC);
30341da177e4SLinus Torvalds 		pgh->seq_num = htonl(pkt_dev->seq_num);
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 		do_gettimeofday(&timestamp);
30371da177e4SLinus Torvalds 		pgh->tv_sec = htonl(timestamp.tv_sec);
30381da177e4SLinus Torvalds 		pgh->tv_usec = htonl(timestamp.tv_usec);
30391da177e4SLinus Torvalds 	}
304034954ddcSFrancesco Fondelli 	/* pkt_dev->seq_num++; FF: you really mean this? */
30411da177e4SLinus Torvalds 
30421da177e4SLinus Torvalds 	return skb;
30431da177e4SLinus Torvalds }
30441da177e4SLinus Torvalds 
3045475ac1e4SStephen Hemminger static struct sk_buff *fill_packet(struct net_device *odev,
30461da177e4SLinus Torvalds 				   struct pktgen_dev *pkt_dev)
30471da177e4SLinus Torvalds {
30481da177e4SLinus Torvalds 	if (pkt_dev->flags & F_IPV6)
30491da177e4SLinus Torvalds 		return fill_packet_ipv6(odev, pkt_dev);
30501da177e4SLinus Torvalds 	else
30511da177e4SLinus Torvalds 		return fill_packet_ipv4(odev, pkt_dev);
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
30551da177e4SLinus Torvalds {
30561da177e4SLinus Torvalds 	pkt_dev->seq_num = 1;
30571da177e4SLinus Torvalds 	pkt_dev->idle_acc = 0;
30581da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
30591da177e4SLinus Torvalds 	pkt_dev->tx_bytes = 0;
30601da177e4SLinus Torvalds 	pkt_dev->errors = 0;
30611da177e4SLinus Torvalds }
30621da177e4SLinus Torvalds 
30631da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t)
30661da177e4SLinus Torvalds {
3067c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
30681da177e4SLinus Torvalds 	int started = 0;
30691da177e4SLinus Torvalds 
307025c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_run. %p\n", t);
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds 	if_lock(t);
3073c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
30741da177e4SLinus Torvalds 
30751da177e4SLinus Torvalds 		/*
30761da177e4SLinus Torvalds 		 * setup odev and create initial packet.
30771da177e4SLinus Torvalds 		 */
30781da177e4SLinus Torvalds 		pktgen_setup_inject(pkt_dev);
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds 		if (pkt_dev->odev) {
30811da177e4SLinus Torvalds 			pktgen_clear_counters(pkt_dev);
30821da177e4SLinus Torvalds 			pkt_dev->running = 1;	/* Cranke yeself! */
30831da177e4SLinus Torvalds 			pkt_dev->skb = NULL;
3084fd29cf72SStephen Hemminger 			pkt_dev->started_at =
3085fd29cf72SStephen Hemminger 				pkt_dev->next_tx = ktime_now();
3086fd29cf72SStephen Hemminger 
308716dab72fSJamal Hadi Salim 			set_pkt_overhead(pkt_dev);
30881da177e4SLinus Torvalds 
30891da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Starting");
30901da177e4SLinus Torvalds 			started++;
3091222f1806SLuiz Capitulino 		} else
30921da177e4SLinus Torvalds 			strcpy(pkt_dev->result, "Error starting");
30931da177e4SLinus Torvalds 	}
30941da177e4SLinus Torvalds 	if_unlock(t);
3095222f1806SLuiz Capitulino 	if (started)
3096222f1806SLuiz Capitulino 		t->control &= ~(T_STOP);
30971da177e4SLinus Torvalds }
30981da177e4SLinus Torvalds 
30991da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void)
31001da177e4SLinus Torvalds {
3101cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31021da177e4SLinus Torvalds 
310325c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
31041da177e4SLinus Torvalds 
31056146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3106cdcdbe0bSLuiz Capitulino 
3107cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list)
310895ed63f7SArthur Kepner 		t->control |= T_STOP;
3109cdcdbe0bSLuiz Capitulino 
31106146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31111da177e4SLinus Torvalds }
31121da177e4SLinus Torvalds 
3113648fda74SStephen Hemminger static int thread_is_running(const struct pktgen_thread *t)
31141da177e4SLinus Torvalds {
3115648fda74SStephen Hemminger 	const struct pktgen_dev *pkt_dev;
31161da177e4SLinus Torvalds 
3117c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list)
3118648fda74SStephen Hemminger 		if (pkt_dev->running)
3119648fda74SStephen Hemminger 			return 1;
3120648fda74SStephen Hemminger 	return 0;
31211da177e4SLinus Torvalds }
31221da177e4SLinus Torvalds 
31231da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t)
31241da177e4SLinus Torvalds {
31251da177e4SLinus Torvalds 	if_lock(t);
31261da177e4SLinus Torvalds 
31271da177e4SLinus Torvalds 	while (thread_is_running(t)) {
31281da177e4SLinus Torvalds 
31291da177e4SLinus Torvalds 		if_unlock(t);
31301da177e4SLinus Torvalds 
31311da177e4SLinus Torvalds 		msleep_interruptible(100);
31321da177e4SLinus Torvalds 
31331da177e4SLinus Torvalds 		if (signal_pending(current))
31341da177e4SLinus Torvalds 			goto signal;
31351da177e4SLinus Torvalds 		if_lock(t);
31361da177e4SLinus Torvalds 	}
31371da177e4SLinus Torvalds 	if_unlock(t);
31381da177e4SLinus Torvalds 	return 1;
31391da177e4SLinus Torvalds signal:
31401da177e4SLinus Torvalds 	return 0;
31411da177e4SLinus Torvalds }
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds static int pktgen_wait_all_threads_run(void)
31441da177e4SLinus Torvalds {
3145cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31461da177e4SLinus Torvalds 	int sig = 1;
31471da177e4SLinus Torvalds 
31486146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
3149cdcdbe0bSLuiz Capitulino 
3150cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list) {
31511da177e4SLinus Torvalds 		sig = pktgen_wait_thread_run(t);
3152222f1806SLuiz Capitulino 		if (sig == 0)
3153222f1806SLuiz Capitulino 			break;
31541da177e4SLinus Torvalds 	}
3155cdcdbe0bSLuiz Capitulino 
3156cdcdbe0bSLuiz Capitulino 	if (sig == 0)
3157cdcdbe0bSLuiz Capitulino 		list_for_each_entry(t, &pktgen_threads, th_list)
31581da177e4SLinus Torvalds 			t->control |= (T_STOP);
3159cdcdbe0bSLuiz Capitulino 
31606146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31611da177e4SLinus Torvalds 	return sig;
31621da177e4SLinus Torvalds }
31631da177e4SLinus Torvalds 
31641da177e4SLinus Torvalds static void pktgen_run_all_threads(void)
31651da177e4SLinus Torvalds {
3166cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
31671da177e4SLinus Torvalds 
316825c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_run_all_threads.\n");
31691da177e4SLinus Torvalds 
31706146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
31711da177e4SLinus Torvalds 
3172cdcdbe0bSLuiz Capitulino 	list_for_each_entry(t, &pktgen_threads, th_list)
31731da177e4SLinus Torvalds 		t->control |= (T_RUN);
3174cdcdbe0bSLuiz Capitulino 
31756146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
31761da177e4SLinus Torvalds 
3177121caf57SNishanth Aravamudan 	schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
31781da177e4SLinus Torvalds 
31791da177e4SLinus Torvalds 	pktgen_wait_all_threads_run();
31801da177e4SLinus Torvalds }
31811da177e4SLinus Torvalds 
3182eb37b41cSJesse Brandeburg static void pktgen_reset_all_threads(void)
3183eb37b41cSJesse Brandeburg {
3184eb37b41cSJesse Brandeburg 	struct pktgen_thread *t;
3185eb37b41cSJesse Brandeburg 
3186eb37b41cSJesse Brandeburg 	pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3187eb37b41cSJesse Brandeburg 
3188eb37b41cSJesse Brandeburg 	mutex_lock(&pktgen_thread_lock);
3189eb37b41cSJesse Brandeburg 
3190eb37b41cSJesse Brandeburg 	list_for_each_entry(t, &pktgen_threads, th_list)
3191eb37b41cSJesse Brandeburg 		t->control |= (T_REMDEVALL);
3192eb37b41cSJesse Brandeburg 
3193eb37b41cSJesse Brandeburg 	mutex_unlock(&pktgen_thread_lock);
3194eb37b41cSJesse Brandeburg 
3195eb37b41cSJesse Brandeburg 	schedule_timeout_interruptible(msecs_to_jiffies(125));	/* Propagate thread->control  */
3196eb37b41cSJesse Brandeburg 
3197eb37b41cSJesse Brandeburg 	pktgen_wait_all_threads_run();
3198eb37b41cSJesse Brandeburg }
3199eb37b41cSJesse Brandeburg 
32001da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
32011da177e4SLinus Torvalds {
3202fd29cf72SStephen Hemminger 	__u64 bps, mbps, pps;
32031da177e4SLinus Torvalds 	char *p = pkt_dev->result;
3204fd29cf72SStephen Hemminger 	ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3205fd29cf72SStephen Hemminger 				    pkt_dev->started_at);
3206fd29cf72SStephen Hemminger 	ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
32071da177e4SLinus Torvalds 
3208fd29cf72SStephen Hemminger 	p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3209fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(elapsed),
3210fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3211fd29cf72SStephen Hemminger 		     (unsigned long long)ktime_to_us(idle),
32121da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->sofar,
32131da177e4SLinus Torvalds 		     pkt_dev->cur_pkt_size, nr_frags);
32141da177e4SLinus Torvalds 
3215fd29cf72SStephen Hemminger 	pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3216fd29cf72SStephen Hemminger 			ktime_to_ns(elapsed));
32171da177e4SLinus Torvalds 
32181da177e4SLinus Torvalds 	bps = pps * 8 * pkt_dev->cur_pkt_size;
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds 	mbps = bps;
32211da177e4SLinus Torvalds 	do_div(mbps, 1000000);
32221da177e4SLinus Torvalds 	p += sprintf(p, "  %llupps %lluMb/sec (%llubps) errors: %llu",
32231da177e4SLinus Torvalds 		     (unsigned long long)pps,
32241da177e4SLinus Torvalds 		     (unsigned long long)mbps,
32251da177e4SLinus Torvalds 		     (unsigned long long)bps,
32261da177e4SLinus Torvalds 		     (unsigned long long)pkt_dev->errors);
32271da177e4SLinus Torvalds }
32281da177e4SLinus Torvalds 
32291da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */
32301da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
32311da177e4SLinus Torvalds {
3232222f1806SLuiz Capitulino 	int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
32331da177e4SLinus Torvalds 
32341da177e4SLinus Torvalds 	if (!pkt_dev->running) {
323525a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: interface: %s is already "
323625a8b254SDavid S. Miller 		       "stopped\n", pkt_dev->odev->name);
32371da177e4SLinus Torvalds 		return -EINVAL;
32381da177e4SLinus Torvalds 	}
32391da177e4SLinus Torvalds 
32403bda06a3SStephen Hemminger 	kfree_skb(pkt_dev->skb);
32413bda06a3SStephen Hemminger 	pkt_dev->skb = NULL;
3242fd29cf72SStephen Hemminger 	pkt_dev->stopped_at = ktime_now();
32431da177e4SLinus Torvalds 	pkt_dev->running = 0;
32441da177e4SLinus Torvalds 
324595ed63f7SArthur Kepner 	show_results(pkt_dev, nr_frags);
32461da177e4SLinus Torvalds 
32471da177e4SLinus Torvalds 	return 0;
32481da177e4SLinus Torvalds }
32491da177e4SLinus Torvalds 
32501da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
32511da177e4SLinus Torvalds {
3252c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev, *best = NULL;
32531da177e4SLinus Torvalds 
32541da177e4SLinus Torvalds 	if_lock(t);
32551da177e4SLinus Torvalds 
3256c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3257c26a8016SLuiz Capitulino 		if (!pkt_dev->running)
3258222f1806SLuiz Capitulino 			continue;
3259222f1806SLuiz Capitulino 		if (best == NULL)
3260c26a8016SLuiz Capitulino 			best = pkt_dev;
3261fd29cf72SStephen Hemminger 		else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
3262c26a8016SLuiz Capitulino 			best = pkt_dev;
32631da177e4SLinus Torvalds 	}
32641da177e4SLinus Torvalds 	if_unlock(t);
32651da177e4SLinus Torvalds 	return best;
32661da177e4SLinus Torvalds }
32671da177e4SLinus Torvalds 
3268222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t)
3269222f1806SLuiz Capitulino {
3270c26a8016SLuiz Capitulino 	struct pktgen_dev *pkt_dev;
32711da177e4SLinus Torvalds 
327225c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_stop\n");
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 	if_lock(t);
32751da177e4SLinus Torvalds 
3276c26a8016SLuiz Capitulino 	list_for_each_entry(pkt_dev, &t->if_list, list) {
3277c26a8016SLuiz Capitulino 		pktgen_stop_device(pkt_dev);
327895ed63f7SArthur Kepner 	}
327995ed63f7SArthur Kepner 
328095ed63f7SArthur Kepner 	if_unlock(t);
328195ed63f7SArthur Kepner }
328295ed63f7SArthur Kepner 
328395ed63f7SArthur Kepner /*
328495ed63f7SArthur Kepner  * one of our devices needs to be removed - find it
328595ed63f7SArthur Kepner  * and remove it
328695ed63f7SArthur Kepner  */
328795ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t)
328895ed63f7SArthur Kepner {
3289c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3290c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
329195ed63f7SArthur Kepner 
329225c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_rem_one_if\n");
329395ed63f7SArthur Kepner 
329495ed63f7SArthur Kepner 	if_lock(t);
329595ed63f7SArthur Kepner 
3296c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3297c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
329895ed63f7SArthur Kepner 
3299222f1806SLuiz Capitulino 		if (!cur->removal_mark)
3300222f1806SLuiz Capitulino 			continue;
330195ed63f7SArthur Kepner 
330295ed63f7SArthur Kepner 		kfree_skb(cur->skb);
330395ed63f7SArthur Kepner 		cur->skb = NULL;
330495ed63f7SArthur Kepner 
330595ed63f7SArthur Kepner 		pktgen_remove_device(t, cur);
330695ed63f7SArthur Kepner 
330795ed63f7SArthur Kepner 		break;
330895ed63f7SArthur Kepner 	}
33091da177e4SLinus Torvalds 
33101da177e4SLinus Torvalds 	if_unlock(t);
33111da177e4SLinus Torvalds }
33121da177e4SLinus Torvalds 
33131da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t)
33141da177e4SLinus Torvalds {
3315c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3316c26a8016SLuiz Capitulino 	struct pktgen_dev *cur;
33171da177e4SLinus Torvalds 
33181da177e4SLinus Torvalds 	/* Remove all devices, free mem */
33191da177e4SLinus Torvalds 
332025c4e53aSStephen Hemminger 	pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
33211da177e4SLinus Torvalds 	if_lock(t);
33221da177e4SLinus Torvalds 
3323c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3324c26a8016SLuiz Capitulino 		cur = list_entry(q, struct pktgen_dev, list);
332595ed63f7SArthur Kepner 
332695ed63f7SArthur Kepner 		kfree_skb(cur->skb);
332795ed63f7SArthur Kepner 		cur->skb = NULL;
332895ed63f7SArthur Kepner 
33291da177e4SLinus Torvalds 		pktgen_remove_device(t, cur);
33301da177e4SLinus Torvalds 	}
33311da177e4SLinus Torvalds 
33321da177e4SLinus Torvalds 	if_unlock(t);
33331da177e4SLinus Torvalds }
33341da177e4SLinus Torvalds 
33351da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t)
33361da177e4SLinus Torvalds {
33371da177e4SLinus Torvalds 	/* Remove from the thread list */
33381da177e4SLinus Torvalds 
3339ee74baa7SDavid S. Miller 	remove_proc_entry(t->tsk->comm, pg_proc_dir);
33401da177e4SLinus Torvalds 
33416146e6a4SLuiz Capitulino 	mutex_lock(&pktgen_thread_lock);
33421da177e4SLinus Torvalds 
3343cdcdbe0bSLuiz Capitulino 	list_del(&t->th_list);
3344cdcdbe0bSLuiz Capitulino 
33456146e6a4SLuiz Capitulino 	mutex_unlock(&pktgen_thread_lock);
33461da177e4SLinus Torvalds }
33471da177e4SLinus Torvalds 
33483791decbSStephen Hemminger static void idle(struct pktgen_dev *pkt_dev)
33493791decbSStephen Hemminger {
3350fd29cf72SStephen Hemminger 	ktime_t idle_start = ktime_now();
33513791decbSStephen Hemminger 
33523791decbSStephen Hemminger 	if (need_resched())
33533791decbSStephen Hemminger 		schedule();
33543791decbSStephen Hemminger 	else
33553791decbSStephen Hemminger 		cpu_relax();
33563791decbSStephen Hemminger 
3357fd29cf72SStephen Hemminger 	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
33583791decbSStephen Hemminger }
33593791decbSStephen Hemminger 
3360fd29cf72SStephen Hemminger 
3361475ac1e4SStephen Hemminger static void pktgen_xmit(struct pktgen_dev *pkt_dev)
33621da177e4SLinus Torvalds {
336300829823SStephen Hemminger 	struct net_device *odev = pkt_dev->odev;
336400829823SStephen Hemminger 	int (*xmit)(struct sk_buff *, struct net_device *)
336500829823SStephen Hemminger 		= odev->netdev_ops->ndo_start_xmit;
3366fd2ea0a7SDavid S. Miller 	struct netdev_queue *txq;
3367fd2ea0a7SDavid S. Miller 	u16 queue_map;
33681da177e4SLinus Torvalds 	int ret;
33691da177e4SLinus Torvalds 
3370fd29cf72SStephen Hemminger 	if (pkt_dev->delay) {
3371fd29cf72SStephen Hemminger 		spin(pkt_dev, pkt_dev->next_tx);
33721da177e4SLinus Torvalds 
33731da177e4SLinus Torvalds 		/* This is max DELAY, this has special meaning of
33741da177e4SLinus Torvalds 		 * "never transmit"
33751da177e4SLinus Torvalds 		 */
3376fd29cf72SStephen Hemminger 		if (pkt_dev->delay == ULLONG_MAX) {
3377fd29cf72SStephen Hemminger 			pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
33783791decbSStephen Hemminger 			return;
33791da177e4SLinus Torvalds 		}
33801da177e4SLinus Torvalds 	}
33811da177e4SLinus Torvalds 
3382fd2ea0a7SDavid S. Miller 	if (!pkt_dev->skb) {
3383fd2ea0a7SDavid S. Miller 		set_cur_queue_map(pkt_dev);
3384fd2ea0a7SDavid S. Miller 		queue_map = pkt_dev->cur_queue_map;
3385fd2ea0a7SDavid S. Miller 	} else {
3386fd2ea0a7SDavid S. Miller 		queue_map = skb_get_queue_mapping(pkt_dev->skb);
3387fd2ea0a7SDavid S. Miller 	}
3388fd2ea0a7SDavid S. Miller 
3389fd2ea0a7SDavid S. Miller 	txq = netdev_get_tx_queue(odev, queue_map);
33903791decbSStephen Hemminger 	/* Did we saturate the queue already? */
33913791decbSStephen Hemminger 	if (netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)) {
33923791decbSStephen Hemminger 		/* If device is down, then all queues are permnantly frozen */
33933791decbSStephen Hemminger 		if (netif_running(odev))
33943791decbSStephen Hemminger 			idle(pkt_dev);
33953791decbSStephen Hemminger 		else
33961da177e4SLinus Torvalds 			pktgen_stop_device(pkt_dev);
33973791decbSStephen Hemminger 		return;
33981da177e4SLinus Torvalds 	}
33991da177e4SLinus Torvalds 
34007d7bb1cfSStephen Hemminger 	if (!pkt_dev->skb || (pkt_dev->last_ok &&
34017d7bb1cfSStephen Hemminger 			      ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
34021da177e4SLinus Torvalds 		/* build a new pkt */
34031da177e4SLinus Torvalds 		kfree_skb(pkt_dev->skb);
34041da177e4SLinus Torvalds 
34051da177e4SLinus Torvalds 		pkt_dev->skb = fill_packet(odev, pkt_dev);
34061da177e4SLinus Torvalds 		if (pkt_dev->skb == NULL) {
340725a8b254SDavid S. Miller 			printk(KERN_ERR "pktgen: ERROR: couldn't "
340825a8b254SDavid S. Miller 			       "allocate skb in fill_packet.\n");
34091da177e4SLinus Torvalds 			schedule();
34101da177e4SLinus Torvalds 			pkt_dev->clone_count--;	/* back out increment, OOM */
34113791decbSStephen Hemminger 			return;
34121da177e4SLinus Torvalds 		}
34137d7bb1cfSStephen Hemminger 
34141da177e4SLinus Torvalds 		pkt_dev->allocated_skbs++;
34151da177e4SLinus Torvalds 		pkt_dev->clone_count = 0;	/* reset counter */
34161da177e4SLinus Torvalds 	}
34171da177e4SLinus Torvalds 
3418fd2ea0a7SDavid S. Miller 	/* fill_packet() might have changed the queue */
3419fd2ea0a7SDavid S. Miller 	queue_map = skb_get_queue_mapping(pkt_dev->skb);
3420fd2ea0a7SDavid S. Miller 	txq = netdev_get_tx_queue(odev, queue_map);
3421fd2ea0a7SDavid S. Miller 
3422fd2ea0a7SDavid S. Miller 	__netif_tx_lock_bh(txq);
34233791decbSStephen Hemminger 	if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)))
34243791decbSStephen Hemminger 		pkt_dev->last_ok = 0;
34253791decbSStephen Hemminger 	else {
34261da177e4SLinus Torvalds 		atomic_inc(&(pkt_dev->skb->users));
34275b8db2f5SStephen Hemminger 
34281da177e4SLinus Torvalds 	retry_now:
342900829823SStephen Hemminger 		ret = (*xmit)(pkt_dev->skb, odev);
34305b8db2f5SStephen Hemminger 		switch (ret) {
34315b8db2f5SStephen Hemminger 		case NETDEV_TX_OK:
343208baf561SEric Dumazet 			txq_trans_update(txq);
34331da177e4SLinus Torvalds 			pkt_dev->last_ok = 1;
34341da177e4SLinus Torvalds 			pkt_dev->sofar++;
34351da177e4SLinus Torvalds 			pkt_dev->seq_num++;
34361da177e4SLinus Torvalds 			pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
34375b8db2f5SStephen Hemminger 			break;
34385b8db2f5SStephen Hemminger 		case NETDEV_TX_LOCKED:
34391da177e4SLinus Torvalds 			cpu_relax();
34401da177e4SLinus Torvalds 			goto retry_now;
34415b8db2f5SStephen Hemminger 		default: /* Drivers are not supposed to return other values! */
34425b8db2f5SStephen Hemminger 			if (net_ratelimit())
34435b8db2f5SStephen Hemminger 				pr_info("pktgen: %s xmit error: %d\n",
34445b8db2f5SStephen Hemminger 					odev->name, ret);
34451da177e4SLinus Torvalds 			pkt_dev->errors++;
34465b8db2f5SStephen Hemminger 			/* fallthru */
34475b8db2f5SStephen Hemminger 		case NETDEV_TX_BUSY:
34485b8db2f5SStephen Hemminger 			/* Retry it next time */
34495b8db2f5SStephen Hemminger 			atomic_dec(&(pkt_dev->skb->users));
34501da177e4SLinus Torvalds 			pkt_dev->last_ok = 0;
34511da177e4SLinus Torvalds 		}
34521da177e4SLinus Torvalds 
3453fd29cf72SStephen Hemminger 		if (pkt_dev->delay)
3454fd29cf72SStephen Hemminger 			pkt_dev->next_tx = ktime_add_ns(ktime_now(),
3455fd29cf72SStephen Hemminger 							pkt_dev->delay);
34565c9d191cSStephen Hemminger 	}
3457fd2ea0a7SDavid S. Miller 	__netif_tx_unlock_bh(txq);
34581da177e4SLinus Torvalds 
34591da177e4SLinus Torvalds 	/* If pkt_dev->count is zero, then run forever */
34601da177e4SLinus Torvalds 	if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
34611da177e4SLinus Torvalds 		if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3462fd29cf72SStephen Hemminger 			ktime_t idle_start = ktime_now();
34631da177e4SLinus Torvalds 			while (atomic_read(&(pkt_dev->skb->users)) != 1) {
34641da177e4SLinus Torvalds 				if (signal_pending(current)) {
34651da177e4SLinus Torvalds 					break;
34661da177e4SLinus Torvalds 				}
34671da177e4SLinus Torvalds 				schedule();
34681da177e4SLinus Torvalds 			}
3469fd29cf72SStephen Hemminger 			pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(),
3470fd29cf72SStephen Hemminger 								   idle_start));
34711da177e4SLinus Torvalds 		}
34721da177e4SLinus Torvalds 
34731da177e4SLinus Torvalds 		/* Done with this */
34741da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
34751da177e4SLinus Torvalds 	}
34761da177e4SLinus Torvalds }
34771da177e4SLinus Torvalds 
34781da177e4SLinus Torvalds /*
34791da177e4SLinus Torvalds  * Main loop of the thread goes here
34801da177e4SLinus Torvalds  */
34811da177e4SLinus Torvalds 
3482ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg)
34831da177e4SLinus Torvalds {
34841da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3485ee74baa7SDavid S. Miller 	struct pktgen_thread *t = arg;
34861da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev = NULL;
34871da177e4SLinus Torvalds 	int cpu = t->cpu;
34881da177e4SLinus Torvalds 
3489ee74baa7SDavid S. Miller 	BUG_ON(smp_processor_id() != cpu);
34901da177e4SLinus Torvalds 
34911da177e4SLinus Torvalds 	init_waitqueue_head(&t->queue);
3492d3ede327SDenis V. Lunev 	complete(&t->start_done);
34931da177e4SLinus Torvalds 
3494ba25f9dcSPavel Emelyanov 	pr_debug("pktgen: starting pktgen/%d:  pid=%d\n", cpu, task_pid_nr(current));
34951da177e4SLinus Torvalds 
3496ee74baa7SDavid S. Miller 	set_current_state(TASK_INTERRUPTIBLE);
34971da177e4SLinus Torvalds 
349883144186SRafael J. Wysocki 	set_freezable();
349983144186SRafael J. Wysocki 
3500ee74baa7SDavid S. Miller 	while (!kthread_should_stop()) {
3501ee74baa7SDavid S. Miller 		pkt_dev = next_to_run(t);
3502ee74baa7SDavid S. Miller 
3503ee74baa7SDavid S. Miller 		if (!pkt_dev &&
3504ee74baa7SDavid S. Miller 		    (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3505ee74baa7SDavid S. Miller 		    == 0) {
3506ee74baa7SDavid S. Miller 			prepare_to_wait(&(t->queue), &wait,
3507ee74baa7SDavid S. Miller 					TASK_INTERRUPTIBLE);
3508ee74baa7SDavid S. Miller 			schedule_timeout(HZ / 10);
3509ee74baa7SDavid S. Miller 			finish_wait(&(t->queue), &wait);
3510ee74baa7SDavid S. Miller 		}
35111da177e4SLinus Torvalds 
35121da177e4SLinus Torvalds 		__set_current_state(TASK_RUNNING);
35131da177e4SLinus Torvalds 
3514b163911fSRobert Olsson 		if (pkt_dev)
35151da177e4SLinus Torvalds 			pktgen_xmit(pkt_dev);
35161da177e4SLinus Torvalds 
35171da177e4SLinus Torvalds 		if (t->control & T_STOP) {
35181da177e4SLinus Torvalds 			pktgen_stop(t);
35191da177e4SLinus Torvalds 			t->control &= ~(T_STOP);
35201da177e4SLinus Torvalds 		}
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds 		if (t->control & T_RUN) {
35231da177e4SLinus Torvalds 			pktgen_run(t);
35241da177e4SLinus Torvalds 			t->control &= ~(T_RUN);
35251da177e4SLinus Torvalds 		}
35261da177e4SLinus Torvalds 
352795ed63f7SArthur Kepner 		if (t->control & T_REMDEVALL) {
35281da177e4SLinus Torvalds 			pktgen_rem_all_ifs(t);
352995ed63f7SArthur Kepner 			t->control &= ~(T_REMDEVALL);
353095ed63f7SArthur Kepner 		}
353195ed63f7SArthur Kepner 
353295ed63f7SArthur Kepner 		if (t->control & T_REMDEV) {
353395ed63f7SArthur Kepner 			pktgen_rem_one_if(t);
35341da177e4SLinus Torvalds 			t->control &= ~(T_REMDEV);
35351da177e4SLinus Torvalds 		}
35361da177e4SLinus Torvalds 
353709fe3ef4SAndrew Morton 		try_to_freeze();
353809fe3ef4SAndrew Morton 
3539ee74baa7SDavid S. Miller 		set_current_state(TASK_INTERRUPTIBLE);
35401da177e4SLinus Torvalds 	}
35411da177e4SLinus Torvalds 
354225c4e53aSStephen Hemminger 	pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
35431da177e4SLinus Torvalds 	pktgen_stop(t);
35441da177e4SLinus Torvalds 
354525c4e53aSStephen Hemminger 	pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
35461da177e4SLinus Torvalds 	pktgen_rem_all_ifs(t);
35471da177e4SLinus Torvalds 
354825c4e53aSStephen Hemminger 	pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
35491da177e4SLinus Torvalds 	pktgen_rem_thread(t);
3550cdcdbe0bSLuiz Capitulino 
3551ee74baa7SDavid S. Miller 	return 0;
35521da177e4SLinus Torvalds }
35531da177e4SLinus Torvalds 
3554222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3555222f1806SLuiz Capitulino 					  const char *ifname)
35561da177e4SLinus Torvalds {
3557c26a8016SLuiz Capitulino 	struct pktgen_dev *p, *pkt_dev = NULL;
35581da177e4SLinus Torvalds 	if_lock(t);
35591da177e4SLinus Torvalds 
3560c26a8016SLuiz Capitulino 	list_for_each_entry(p, &t->if_list, list)
356139df232fSStephen Hemminger 		if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3562c26a8016SLuiz Capitulino 			pkt_dev = p;
35631da177e4SLinus Torvalds 			break;
35641da177e4SLinus Torvalds 		}
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds 	if_unlock(t);
356725c4e53aSStephen Hemminger 	pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
35681da177e4SLinus Torvalds 	return pkt_dev;
35691da177e4SLinus Torvalds }
35701da177e4SLinus Torvalds 
35711da177e4SLinus Torvalds /*
35721da177e4SLinus Torvalds  * Adds a dev at front of if_list.
35731da177e4SLinus Torvalds  */
35741da177e4SLinus Torvalds 
3575222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t,
3576222f1806SLuiz Capitulino 			     struct pktgen_dev *pkt_dev)
35771da177e4SLinus Torvalds {
35781da177e4SLinus Torvalds 	int rv = 0;
35791da177e4SLinus Torvalds 
35801da177e4SLinus Torvalds 	if_lock(t);
35811da177e4SLinus Torvalds 
35821da177e4SLinus Torvalds 	if (pkt_dev->pg_thread) {
358325a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: already assigned "
358425a8b254SDavid S. Miller 		       "to a thread.\n");
35851da177e4SLinus Torvalds 		rv = -EBUSY;
35861da177e4SLinus Torvalds 		goto out;
35871da177e4SLinus Torvalds 	}
3588c26a8016SLuiz Capitulino 
3589c26a8016SLuiz Capitulino 	list_add(&pkt_dev->list, &t->if_list);
35901da177e4SLinus Torvalds 	pkt_dev->pg_thread = t;
35911da177e4SLinus Torvalds 	pkt_dev->running = 0;
35921da177e4SLinus Torvalds 
35931da177e4SLinus Torvalds out:
35941da177e4SLinus Torvalds 	if_unlock(t);
35951da177e4SLinus Torvalds 	return rv;
35961da177e4SLinus Torvalds }
35971da177e4SLinus Torvalds 
35981da177e4SLinus Torvalds /* Called under thread lock */
35991da177e4SLinus Torvalds 
36001da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
36011da177e4SLinus Torvalds {
36021da177e4SLinus Torvalds 	struct pktgen_dev *pkt_dev;
360339df232fSStephen Hemminger 	int err;
36041da177e4SLinus Torvalds 
36051da177e4SLinus Torvalds 	/* We don't allow a device to be on several threads */
36061da177e4SLinus Torvalds 
3607d50a6b56SStephen Hemminger 	pkt_dev = __pktgen_NN_threads(ifname, FIND);
3608d50a6b56SStephen Hemminger 	if (pkt_dev) {
360925a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3610d50a6b56SStephen Hemminger 		return -EBUSY;
3611d50a6b56SStephen Hemminger 	}
36121da177e4SLinus Torvalds 
36132845b63bSStephen Hemminger 	pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
36141da177e4SLinus Torvalds 	if (!pkt_dev)
36151da177e4SLinus Torvalds 		return -ENOMEM;
36161da177e4SLinus Torvalds 
36171da177e4SLinus Torvalds 	pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
36181da177e4SLinus Torvalds 	if (pkt_dev->flows == NULL) {
36191da177e4SLinus Torvalds 		kfree(pkt_dev);
36201da177e4SLinus Torvalds 		return -ENOMEM;
36211da177e4SLinus Torvalds 	}
36221da177e4SLinus Torvalds 	memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
36231da177e4SLinus Torvalds 
362495ed63f7SArthur Kepner 	pkt_dev->removal_mark = 0;
36251da177e4SLinus Torvalds 	pkt_dev->min_pkt_size = ETH_ZLEN;
36261da177e4SLinus Torvalds 	pkt_dev->max_pkt_size = ETH_ZLEN;
36271da177e4SLinus Torvalds 	pkt_dev->nfrags = 0;
36281da177e4SLinus Torvalds 	pkt_dev->clone_skb = pg_clone_skb_d;
3629fd29cf72SStephen Hemminger 	pkt_dev->delay = pg_delay_d;
36301da177e4SLinus Torvalds 	pkt_dev->count = pg_count_d;
36311da177e4SLinus Torvalds 	pkt_dev->sofar = 0;
36321da177e4SLinus Torvalds 	pkt_dev->udp_src_min = 9;	/* sink port */
36331da177e4SLinus Torvalds 	pkt_dev->udp_src_max = 9;
36341da177e4SLinus Torvalds 	pkt_dev->udp_dst_min = 9;
36351da177e4SLinus Torvalds 	pkt_dev->udp_dst_max = 9;
36361da177e4SLinus Torvalds 
363734954ddcSFrancesco Fondelli 	pkt_dev->vlan_p = 0;
363834954ddcSFrancesco Fondelli 	pkt_dev->vlan_cfi = 0;
363934954ddcSFrancesco Fondelli 	pkt_dev->vlan_id = 0xffff;
364034954ddcSFrancesco Fondelli 	pkt_dev->svlan_p = 0;
364134954ddcSFrancesco Fondelli 	pkt_dev->svlan_cfi = 0;
364234954ddcSFrancesco Fondelli 	pkt_dev->svlan_id = 0xffff;
364334954ddcSFrancesco Fondelli 
364439df232fSStephen Hemminger 	err = pktgen_setup_dev(pkt_dev, ifname);
364539df232fSStephen Hemminger 	if (err)
364639df232fSStephen Hemminger 		goto out1;
36471da177e4SLinus Torvalds 
36485efdccbcSDenis V. Lunev 	pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
36495efdccbcSDenis V. Lunev 					  &pktgen_if_fops, pkt_dev);
365039df232fSStephen Hemminger 	if (!pkt_dev->entry) {
365125a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3652d50a6b56SStephen Hemminger 		       PG_PROC_DIR, ifname);
365339df232fSStephen Hemminger 		err = -EINVAL;
365439df232fSStephen Hemminger 		goto out2;
365539df232fSStephen Hemminger 	}
3656a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3657a553e4a6SJamal Hadi Salim 	pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3658a553e4a6SJamal Hadi Salim 	pkt_dev->ipsproto = IPPROTO_ESP;
3659a553e4a6SJamal Hadi Salim #endif
366039df232fSStephen Hemminger 
366139df232fSStephen Hemminger 	return add_dev_to_thread(t, pkt_dev);
366239df232fSStephen Hemminger out2:
366339df232fSStephen Hemminger 	dev_put(pkt_dev->odev);
366439df232fSStephen Hemminger out1:
3665a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3666a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3667a553e4a6SJamal Hadi Salim #endif
36681da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
36691da177e4SLinus Torvalds 	kfree(pkt_dev);
367039df232fSStephen Hemminger 	return err;
36711da177e4SLinus Torvalds }
36721da177e4SLinus Torvalds 
3673ee74baa7SDavid S. Miller static int __init pktgen_create_thread(int cpu)
36741da177e4SLinus Torvalds {
3675cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3676d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3677ee74baa7SDavid S. Miller 	struct task_struct *p;
36781da177e4SLinus Torvalds 
36792845b63bSStephen Hemminger 	t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
36801da177e4SLinus Torvalds 	if (!t) {
368125a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
368225a8b254SDavid S. Miller 		       "create new thread.\n");
36831da177e4SLinus Torvalds 		return -ENOMEM;
36841da177e4SLinus Torvalds 	}
36851da177e4SLinus Torvalds 
36861da177e4SLinus Torvalds 	spin_lock_init(&t->if_lock);
36871da177e4SLinus Torvalds 	t->cpu = cpu;
36881da177e4SLinus Torvalds 
3689ee74baa7SDavid S. Miller 	INIT_LIST_HEAD(&t->if_list);
3690ee74baa7SDavid S. Miller 
3691ee74baa7SDavid S. Miller 	list_add_tail(&t->th_list, &pktgen_threads);
3692d3ede327SDenis V. Lunev 	init_completion(&t->start_done);
3693ee74baa7SDavid S. Miller 
3694ee74baa7SDavid S. Miller 	p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3695ee74baa7SDavid S. Miller 	if (IS_ERR(p)) {
369625a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: kernel_thread() failed "
369725a8b254SDavid S. Miller 		       "for cpu %d\n", t->cpu);
3698ee74baa7SDavid S. Miller 		list_del(&t->th_list);
3699ee74baa7SDavid S. Miller 		kfree(t);
3700ee74baa7SDavid S. Miller 		return PTR_ERR(p);
3701ee74baa7SDavid S. Miller 	}
3702ee74baa7SDavid S. Miller 	kthread_bind(p, cpu);
3703ee74baa7SDavid S. Miller 	t->tsk = p;
3704ee74baa7SDavid S. Miller 
37055efdccbcSDenis V. Lunev 	pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
37065efdccbcSDenis V. Lunev 			      &pktgen_thread_fops, t);
3707d50a6b56SStephen Hemminger 	if (!pe) {
370825a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3709ee74baa7SDavid S. Miller 		       PG_PROC_DIR, t->tsk->comm);
3710ee74baa7SDavid S. Miller 		kthread_stop(p);
3711ee74baa7SDavid S. Miller 		list_del(&t->th_list);
37121da177e4SLinus Torvalds 		kfree(t);
37131da177e4SLinus Torvalds 		return -EINVAL;
37141da177e4SLinus Torvalds 	}
3715d50a6b56SStephen Hemminger 
3716ee74baa7SDavid S. Miller 	wake_up_process(p);
3717d3ede327SDenis V. Lunev 	wait_for_completion(&t->start_done);
37181da177e4SLinus Torvalds 
37191da177e4SLinus Torvalds 	return 0;
37201da177e4SLinus Torvalds }
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds /*
37231da177e4SLinus Torvalds  * Removes a device from the thread if_list.
37241da177e4SLinus Torvalds  */
3725222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t,
3726222f1806SLuiz Capitulino 				  struct pktgen_dev *pkt_dev)
37271da177e4SLinus Torvalds {
3728c26a8016SLuiz Capitulino 	struct list_head *q, *n;
3729c26a8016SLuiz Capitulino 	struct pktgen_dev *p;
37301da177e4SLinus Torvalds 
3731c26a8016SLuiz Capitulino 	list_for_each_safe(q, n, &t->if_list) {
3732c26a8016SLuiz Capitulino 		p = list_entry(q, struct pktgen_dev, list);
3733c26a8016SLuiz Capitulino 		if (p == pkt_dev)
3734c26a8016SLuiz Capitulino 			list_del(&p->list);
37351da177e4SLinus Torvalds 	}
37361da177e4SLinus Torvalds }
37371da177e4SLinus Torvalds 
3738222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t,
3739222f1806SLuiz Capitulino 				struct pktgen_dev *pkt_dev)
37401da177e4SLinus Torvalds {
37411da177e4SLinus Torvalds 
374225c4e53aSStephen Hemminger 	pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
37431da177e4SLinus Torvalds 
37441da177e4SLinus Torvalds 	if (pkt_dev->running) {
374525a8b254SDavid S. Miller 		printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
374625a8b254SDavid S. Miller 		       "running interface, stopping it now.\n");
37471da177e4SLinus Torvalds 		pktgen_stop_device(pkt_dev);
37481da177e4SLinus Torvalds 	}
37491da177e4SLinus Torvalds 
37501da177e4SLinus Torvalds 	/* Dis-associate from the interface */
37511da177e4SLinus Torvalds 
37521da177e4SLinus Torvalds 	if (pkt_dev->odev) {
37531da177e4SLinus Torvalds 		dev_put(pkt_dev->odev);
37541da177e4SLinus Torvalds 		pkt_dev->odev = NULL;
37551da177e4SLinus Torvalds 	}
37561da177e4SLinus Torvalds 
37571da177e4SLinus Torvalds 	/* And update the thread if_list */
37581da177e4SLinus Torvalds 
37591da177e4SLinus Torvalds 	_rem_dev_from_if_list(t, pkt_dev);
37601da177e4SLinus Torvalds 
376139df232fSStephen Hemminger 	if (pkt_dev->entry)
376239df232fSStephen Hemminger 		remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
37631da177e4SLinus Torvalds 
3764a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM
3765a553e4a6SJamal Hadi Salim 	free_SAs(pkt_dev);
3766a553e4a6SJamal Hadi Salim #endif
37671da177e4SLinus Torvalds 	vfree(pkt_dev->flows);
37681da177e4SLinus Torvalds 	kfree(pkt_dev);
37691da177e4SLinus Torvalds 	return 0;
37701da177e4SLinus Torvalds }
37711da177e4SLinus Torvalds 
37721da177e4SLinus Torvalds static int __init pg_init(void)
37731da177e4SLinus Torvalds {
37741da177e4SLinus Torvalds 	int cpu;
3775d50a6b56SStephen Hemminger 	struct proc_dir_entry *pe;
3776d50a6b56SStephen Hemminger 
377725a8b254SDavid S. Miller 	printk(KERN_INFO "%s", version);
37781da177e4SLinus Torvalds 
3779457c4cbcSEric W. Biederman 	pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3780d50a6b56SStephen Hemminger 	if (!pg_proc_dir)
3781d50a6b56SStephen Hemminger 		return -ENODEV;
37821da177e4SLinus Torvalds 
378325296d59SWang Chen 	pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3784d50a6b56SStephen Hemminger 	if (pe == NULL) {
378525a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: cannot create %s "
378625a8b254SDavid S. Miller 		       "procfs entry.\n", PGCTRL);
3787457c4cbcSEric W. Biederman 		proc_net_remove(&init_net, PG_PROC_DIR);
37881da177e4SLinus Torvalds 		return -EINVAL;
37891da177e4SLinus Torvalds 	}
37901da177e4SLinus Torvalds 
37911da177e4SLinus Torvalds 	/* Register us to receive netdevice events */
37921da177e4SLinus Torvalds 	register_netdevice_notifier(&pktgen_notifier_block);
37931da177e4SLinus Torvalds 
3794670c02c2SJohn Hawkes 	for_each_online_cpu(cpu) {
37958024bb24SLuiz Capitulino 		int err;
37961da177e4SLinus Torvalds 
3797ee74baa7SDavid S. Miller 		err = pktgen_create_thread(cpu);
37988024bb24SLuiz Capitulino 		if (err)
379925a8b254SDavid S. Miller 			printk(KERN_WARNING "pktgen: WARNING: Cannot create "
380025a8b254SDavid S. Miller 			       "thread for cpu %d (%d)\n", cpu, err);
38011da177e4SLinus Torvalds 	}
38028024bb24SLuiz Capitulino 
38038024bb24SLuiz Capitulino 	if (list_empty(&pktgen_threads)) {
380425a8b254SDavid S. Miller 		printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
380525a8b254SDavid S. Miller 		       "all threads\n");
38068024bb24SLuiz Capitulino 		unregister_netdevice_notifier(&pktgen_notifier_block);
38078024bb24SLuiz Capitulino 		remove_proc_entry(PGCTRL, pg_proc_dir);
3808457c4cbcSEric W. Biederman 		proc_net_remove(&init_net, PG_PROC_DIR);
38098024bb24SLuiz Capitulino 		return -ENODEV;
38108024bb24SLuiz Capitulino 	}
38118024bb24SLuiz Capitulino 
38121da177e4SLinus Torvalds 	return 0;
38131da177e4SLinus Torvalds }
38141da177e4SLinus Torvalds 
38151da177e4SLinus Torvalds static void __exit pg_cleanup(void)
38161da177e4SLinus Torvalds {
3817cdcdbe0bSLuiz Capitulino 	struct pktgen_thread *t;
3818cdcdbe0bSLuiz Capitulino 	struct list_head *q, *n;
38191da177e4SLinus Torvalds 	wait_queue_head_t queue;
38201da177e4SLinus Torvalds 	init_waitqueue_head(&queue);
38211da177e4SLinus Torvalds 
38221da177e4SLinus Torvalds 	/* Stop all interfaces & threads */
38231da177e4SLinus Torvalds 
3824cdcdbe0bSLuiz Capitulino 	list_for_each_safe(q, n, &pktgen_threads) {
3825cdcdbe0bSLuiz Capitulino 		t = list_entry(q, struct pktgen_thread, th_list);
3826ee74baa7SDavid S. Miller 		kthread_stop(t->tsk);
3827ee74baa7SDavid S. Miller 		kfree(t);
38281da177e4SLinus Torvalds 	}
38291da177e4SLinus Torvalds 
38301da177e4SLinus Torvalds 	/* Un-register us from receiving netdevice events */
38311da177e4SLinus Torvalds 	unregister_netdevice_notifier(&pktgen_notifier_block);
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds 	/* Clean up proc file system */
3834d50a6b56SStephen Hemminger 	remove_proc_entry(PGCTRL, pg_proc_dir);
3835457c4cbcSEric W. Biederman 	proc_net_remove(&init_net, PG_PROC_DIR);
38361da177e4SLinus Torvalds }
38371da177e4SLinus Torvalds 
38381da177e4SLinus Torvalds module_init(pg_init);
38391da177e4SLinus Torvalds module_exit(pg_cleanup);
38401da177e4SLinus Torvalds 
38411da177e4SLinus Torvalds MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
38421da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool");
38431da177e4SLinus Torvalds MODULE_LICENSE("GPL");
38441da177e4SLinus Torvalds module_param(pg_count_d, int, 0);
38451da177e4SLinus Torvalds module_param(pg_delay_d, int, 0);
38461da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0);
38471da177e4SLinus Torvalds module_param(debug, int, 0);
3848