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> 91da177e4SLinus Torvalds * 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> 13409fe3ef4SAndrew Morton #include <linux/freezer.h> 1351da177e4SLinus Torvalds #include <linux/delay.h> 1361da177e4SLinus Torvalds #include <linux/timer.h> 137cdcdbe0bSLuiz Capitulino #include <linux/list.h> 1381da177e4SLinus Torvalds #include <linux/init.h> 1391da177e4SLinus Torvalds #include <linux/skbuff.h> 1401da177e4SLinus Torvalds #include <linux/netdevice.h> 1411da177e4SLinus Torvalds #include <linux/inet.h> 1421da177e4SLinus Torvalds #include <linux/inetdevice.h> 1431da177e4SLinus Torvalds #include <linux/rtnetlink.h> 1441da177e4SLinus Torvalds #include <linux/if_arp.h> 14534954ddcSFrancesco Fondelli #include <linux/if_vlan.h> 1461da177e4SLinus Torvalds #include <linux/in.h> 1471da177e4SLinus Torvalds #include <linux/ip.h> 1481da177e4SLinus Torvalds #include <linux/ipv6.h> 1491da177e4SLinus Torvalds #include <linux/udp.h> 1501da177e4SLinus Torvalds #include <linux/proc_fs.h> 151d50a6b56SStephen Hemminger #include <linux/seq_file.h> 1521da177e4SLinus Torvalds #include <linux/wait.h> 153f404e9a6SKris Katterjohn #include <linux/etherdevice.h> 154ee74baa7SDavid S. Miller #include <linux/kthread.h> 155457c4cbcSEric W. Biederman #include <net/net_namespace.h> 1561da177e4SLinus Torvalds #include <net/checksum.h> 1571da177e4SLinus Torvalds #include <net/ipv6.h> 1581da177e4SLinus Torvalds #include <net/addrconf.h> 159a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 160a553e4a6SJamal Hadi Salim #include <net/xfrm.h> 161a553e4a6SJamal Hadi Salim #endif 1621da177e4SLinus Torvalds #include <asm/byteorder.h> 1631da177e4SLinus Torvalds #include <linux/rcupdate.h> 1641da177e4SLinus Torvalds #include <asm/bitops.h> 1651da177e4SLinus Torvalds #include <asm/io.h> 1661da177e4SLinus Torvalds #include <asm/dma.h> 1671da177e4SLinus Torvalds #include <asm/uaccess.h> 1681da177e4SLinus Torvalds #include <asm/div64.h> /* do_div */ 1691da177e4SLinus Torvalds #include <asm/timex.h> 1701da177e4SLinus Torvalds 171b163911fSRobert Olsson #define VERSION "pktgen v2.69: Packet Generator for packet performance testing.\n" 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* The buckets are exponential in 'width' */ 1741da177e4SLinus Torvalds #define LAT_BUCKETS_MAX 32 1751da177e4SLinus Torvalds #define IP_NAME_SZ 32 176ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */ 177d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100) 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds /* Device flag bits */ 1801da177e4SLinus Torvalds #define F_IPSRC_RND (1<<0) /* IP-Src Random */ 1811da177e4SLinus Torvalds #define F_IPDST_RND (1<<1) /* IP-Dst Random */ 1821da177e4SLinus Torvalds #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */ 1831da177e4SLinus Torvalds #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */ 1841da177e4SLinus Torvalds #define F_MACSRC_RND (1<<4) /* MAC-Src Random */ 1851da177e4SLinus Torvalds #define F_MACDST_RND (1<<5) /* MAC-Dst Random */ 1861da177e4SLinus Torvalds #define F_TXSIZE_RND (1<<6) /* Transmit size is random */ 1871da177e4SLinus Torvalds #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */ 188ca6549afSSteven Whitehouse #define F_MPLS_RND (1<<8) /* Random MPLS labels */ 18934954ddcSFrancesco Fondelli #define F_VID_RND (1<<9) /* Random VLAN ID */ 19034954ddcSFrancesco Fondelli #define F_SVID_RND (1<<10) /* Random SVLAN ID */ 191007a531bSJamal Hadi Salim #define F_FLOW_SEQ (1<<11) /* Sequential flows */ 192a553e4a6SJamal Hadi Salim #define F_IPSEC_ON (1<<12) /* ipsec on for flows */ 19345b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */ 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; 2501da177e4SLinus Torvalds __u32 delay_us; /* Default delay */ 2511da177e4SLinus Torvalds __u32 delay_ns; 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 __u64 next_tx_us; /* timestamp of when to tx next */ 2591da177e4SLinus Torvalds __u32 next_tx_ns; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds __u64 allocated_skbs; 2621da177e4SLinus Torvalds __u32 clone_count; 2631da177e4SLinus Torvalds int last_ok; /* Was last skb sent? 2641da177e4SLinus Torvalds * Or a failed transmit of some sort? This will keep 2651da177e4SLinus Torvalds * sequence numbers in order, for example. 2661da177e4SLinus Torvalds */ 2671da177e4SLinus Torvalds __u64 started_at; /* micro-seconds */ 2681da177e4SLinus Torvalds __u64 stopped_at; /* micro-seconds */ 2691da177e4SLinus Torvalds __u64 idle_acc; /* micro-seconds */ 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; 3951da177e4SLinus Torvalds }; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds #define REMOVE 1 3981da177e4SLinus Torvalds #define FIND 0 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /* This code works around the fact that do_div cannot handle two 64-bit 4011da177e4SLinus Torvalds numbers, and regular 64-bit division doesn't work on x86 kernels. 4021da177e4SLinus Torvalds --Ben 4031da177e4SLinus Torvalds */ 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds #define PG_DIV 0 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net> 4081da177e4SLinus Torvalds * Function copied/adapted/optimized from: 4091da177e4SLinus Torvalds * 4101da177e4SLinus Torvalds * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html 4111da177e4SLinus Torvalds * 4121da177e4SLinus Torvalds * Copyright 1994, University of Cambridge Computer Laboratory 4131da177e4SLinus Torvalds * All Rights Reserved. 4141da177e4SLinus Torvalds * 4151da177e4SLinus Torvalds */ 41677933d72SJesper Juhl static inline s64 divremdi3(s64 x, s64 y, int type) 4171da177e4SLinus Torvalds { 4181da177e4SLinus Torvalds u64 a = (x < 0) ? -x : x; 4191da177e4SLinus Torvalds u64 b = (y < 0) ? -y : y; 4201da177e4SLinus Torvalds u64 res = 0, d = 1; 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds if (b > 0) { 4231da177e4SLinus Torvalds while (b < a) { 4241da177e4SLinus Torvalds b <<= 1; 4251da177e4SLinus Torvalds d <<= 1; 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds do { 4301da177e4SLinus Torvalds if (a >= b) { 4311da177e4SLinus Torvalds a -= b; 4321da177e4SLinus Torvalds res += d; 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds b >>= 1; 4351da177e4SLinus Torvalds d >>= 1; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds while (d); 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (PG_DIV == type) { 4401da177e4SLinus Torvalds return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res; 441222f1806SLuiz Capitulino } else { 4421da177e4SLinus Torvalds return ((x & (1ll << 63)) == 0) ? a : -(s64) a; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds /* End of hacks to deal with 64-bit math on x86 */ 4471da177e4SLinus Torvalds 448b4099fabSStephen Hemminger /** Convert to milliseconds */ 4491da177e4SLinus Torvalds static inline __u64 tv_to_ms(const struct timeval *tv) 4501da177e4SLinus Torvalds { 4511da177e4SLinus Torvalds __u64 ms = tv->tv_usec / 1000; 4521da177e4SLinus Torvalds ms += (__u64) tv->tv_sec * (__u64) 1000; 4531da177e4SLinus Torvalds return ms; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /** Convert to micro-seconds */ 4571da177e4SLinus Torvalds static inline __u64 tv_to_us(const struct timeval *tv) 4581da177e4SLinus Torvalds { 4591da177e4SLinus Torvalds __u64 us = tv->tv_usec; 4601da177e4SLinus Torvalds us += (__u64) tv->tv_sec * (__u64) 1000000; 4611da177e4SLinus Torvalds return us; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 464222f1806SLuiz Capitulino static inline __u64 pg_div(__u64 n, __u32 base) 465222f1806SLuiz Capitulino { 4661da177e4SLinus Torvalds __u64 tmp = n; 4671da177e4SLinus Torvalds do_div(tmp, base); 4681da177e4SLinus Torvalds /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n", 4691da177e4SLinus Torvalds n, base, tmp); */ 4701da177e4SLinus Torvalds return tmp; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds static inline __u64 pg_div64(__u64 n, __u64 base) 4741da177e4SLinus Torvalds { 4751da177e4SLinus Torvalds __u64 tmp = n; 4761da177e4SLinus Torvalds /* 477b4099fabSStephen Hemminger * How do we know if the architecture we are running on 4781da177e4SLinus Torvalds * supports division with 64 bit base? 4791da177e4SLinus Torvalds * 4801da177e4SLinus Torvalds */ 4811da177e4SLinus Torvalds #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__) 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds do_div(tmp, base); 4841da177e4SLinus Torvalds #else 4851da177e4SLinus Torvalds tmp = divremdi3(n, base, PG_DIV); 4861da177e4SLinus Torvalds #endif 4871da177e4SLinus Torvalds return tmp; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds static inline __u64 getCurMs(void) 4911da177e4SLinus Torvalds { 4921da177e4SLinus Torvalds struct timeval tv; 4931da177e4SLinus Torvalds do_gettimeofday(&tv); 4941da177e4SLinus Torvalds return tv_to_ms(&tv); 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds static inline __u64 getCurUs(void) 4981da177e4SLinus Torvalds { 4991da177e4SLinus Torvalds struct timeval tv; 5001da177e4SLinus Torvalds do_gettimeofday(&tv); 5011da177e4SLinus Torvalds return tv_to_us(&tv); 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b) 5051da177e4SLinus Torvalds { 5061da177e4SLinus Torvalds return tv_to_us(a) - tv_to_us(b); 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds /* old include end */ 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds static char version[] __initdata = VERSION; 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i); 5141da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname); 515222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, 516222f1806SLuiz Capitulino const char *ifname); 5171da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *); 5181da177e4SLinus Torvalds static void pktgen_run_all_threads(void); 5191da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void); 5201da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev); 5211da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t); 5221da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev); 52339df232fSStephen Hemminger 5241da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]); 5251da177e4SLinus Torvalds static unsigned int fmt_ip6(char *s, const char ip[16]); 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds /* Module parameters, defaults. */ 5281da177e4SLinus Torvalds static int pg_count_d = 1000; /* 1000 pkts by default */ 529f34fbb97SJaco Kroon static int pg_delay_d; 530f34fbb97SJaco Kroon static int pg_clone_skb_d; 531f34fbb97SJaco Kroon static int debug; 5321da177e4SLinus Torvalds 533222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock); 534cdcdbe0bSLuiz Capitulino static LIST_HEAD(pktgen_threads); 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = { 5371da177e4SLinus Torvalds .notifier_call = pktgen_device_event, 5381da177e4SLinus Torvalds }; 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* 5411da177e4SLinus Torvalds * /proc handling functions 5421da177e4SLinus Torvalds * 5431da177e4SLinus Torvalds */ 5441da177e4SLinus Torvalds 545d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v) 546d50a6b56SStephen Hemminger { 547d50a6b56SStephen Hemminger seq_puts(seq, VERSION); 548d50a6b56SStephen Hemminger return 0; 549d50a6b56SStephen Hemminger } 5501da177e4SLinus Torvalds 551d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user * buf, 5521da177e4SLinus Torvalds size_t count, loff_t * ppos) 5531da177e4SLinus Torvalds { 5541da177e4SLinus Torvalds int err = 0; 555d50a6b56SStephen Hemminger char data[128]; 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) { 5581da177e4SLinus Torvalds err = -EPERM; 5591da177e4SLinus Torvalds goto out; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 562d50a6b56SStephen Hemminger if (count > sizeof(data)) 563d50a6b56SStephen Hemminger count = sizeof(data); 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds if (copy_from_user(data, buf, count)) { 5661da177e4SLinus Torvalds err = -EFAULT; 567d50a6b56SStephen Hemminger goto out; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds data[count - 1] = 0; /* Make string */ 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds if (!strcmp(data, "stop")) 5721da177e4SLinus Torvalds pktgen_stop_all_threads_ifs(); 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds else if (!strcmp(data, "start")) 5751da177e4SLinus Torvalds pktgen_run_all_threads(); 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds else 57825a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: Unknown command: %s\n", data); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds err = count; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds out: 5831da177e4SLinus Torvalds return err; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 586d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file) 5871da177e4SLinus Torvalds { 588d50a6b56SStephen Hemminger return single_open(file, pgctrl_show, PDE(inode)->data); 589d50a6b56SStephen Hemminger } 590d50a6b56SStephen Hemminger 5919a32144eSArjan van de Ven static const struct file_operations pktgen_fops = { 592d50a6b56SStephen Hemminger .owner = THIS_MODULE, 593d50a6b56SStephen Hemminger .open = pgctrl_open, 594d50a6b56SStephen Hemminger .read = seq_read, 595d50a6b56SStephen Hemminger .llseek = seq_lseek, 596d50a6b56SStephen Hemminger .write = pgctrl_write, 597d50a6b56SStephen Hemminger .release = single_release, 598d50a6b56SStephen Hemminger }; 599d50a6b56SStephen Hemminger 600d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v) 601d50a6b56SStephen Hemminger { 602d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 6031da177e4SLinus Torvalds __u64 sa; 6041da177e4SLinus Torvalds __u64 stopped; 6051da177e4SLinus Torvalds __u64 now = getCurUs(); 6060795af57SJoe Perches DECLARE_MAC_BUF(mac); 6071da177e4SLinus Torvalds 608222f1806SLuiz Capitulino seq_printf(seq, 609222f1806SLuiz Capitulino "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n", 610222f1806SLuiz Capitulino (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size, 611222f1806SLuiz Capitulino pkt_dev->max_pkt_size); 6121da177e4SLinus Torvalds 613222f1806SLuiz Capitulino seq_printf(seq, 614222f1806SLuiz Capitulino " frags: %d delay: %u clone_skb: %d ifname: %s\n", 615222f1806SLuiz Capitulino pkt_dev->nfrags, 616222f1806SLuiz Capitulino 1000 * pkt_dev->delay_us + pkt_dev->delay_ns, 61739df232fSStephen Hemminger pkt_dev->clone_skb, pkt_dev->odev->name); 6181da177e4SLinus Torvalds 619222f1806SLuiz Capitulino seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows, 620222f1806SLuiz Capitulino pkt_dev->lflow); 6211da177e4SLinus Torvalds 62245b270f8SRobert Olsson seq_printf(seq, 62345b270f8SRobert Olsson " queue_map_min: %u queue_map_max: %u\n", 62445b270f8SRobert Olsson pkt_dev->queue_map_min, 62545b270f8SRobert Olsson pkt_dev->queue_map_max); 62645b270f8SRobert Olsson 6271da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 6281da177e4SLinus Torvalds char b1[128], b2[128], b3[128]; 6291da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr); 6301da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr); 6311da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr); 632222f1806SLuiz Capitulino seq_printf(seq, 633222f1806SLuiz Capitulino " saddr: %s min_saddr: %s max_saddr: %s\n", b1, 634222f1806SLuiz Capitulino b2, b3); 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr); 6371da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr); 6381da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr); 639222f1806SLuiz Capitulino seq_printf(seq, 640222f1806SLuiz Capitulino " daddr: %s min_daddr: %s max_daddr: %s\n", b1, 641222f1806SLuiz Capitulino b2, b3); 6421da177e4SLinus Torvalds 643222f1806SLuiz Capitulino } else 644222f1806SLuiz Capitulino seq_printf(seq, 645222f1806SLuiz Capitulino " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n", 646222f1806SLuiz Capitulino pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min, 647222f1806SLuiz Capitulino pkt_dev->src_max); 6481da177e4SLinus Torvalds 649d50a6b56SStephen Hemminger seq_puts(seq, " src_mac: "); 6501da177e4SLinus Torvalds 6510795af57SJoe Perches seq_printf(seq, "%s ", 6520795af57SJoe Perches print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ? 6530795af57SJoe Perches pkt_dev->odev->dev_addr : pkt_dev->src_mac)); 6541da177e4SLinus Torvalds 655d50a6b56SStephen Hemminger seq_printf(seq, "dst_mac: "); 6560795af57SJoe Perches seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac)); 6571da177e4SLinus Torvalds 658222f1806SLuiz Capitulino seq_printf(seq, 659222f1806SLuiz Capitulino " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n", 660222f1806SLuiz Capitulino pkt_dev->udp_src_min, pkt_dev->udp_src_max, 661222f1806SLuiz Capitulino pkt_dev->udp_dst_min, pkt_dev->udp_dst_max); 6621da177e4SLinus Torvalds 663222f1806SLuiz Capitulino seq_printf(seq, 664ca6549afSSteven Whitehouse " src_mac_count: %d dst_mac_count: %d\n", 6651da177e4SLinus Torvalds pkt_dev->src_mac_count, pkt_dev->dst_mac_count); 6661da177e4SLinus Torvalds 667ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) { 668ca6549afSSteven Whitehouse unsigned i; 669ca6549afSSteven Whitehouse seq_printf(seq, " mpls: "); 670ca6549afSSteven Whitehouse for (i = 0; i < pkt_dev->nr_labels; i++) 671ca6549afSSteven Whitehouse seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]), 672ca6549afSSteven Whitehouse i == pkt_dev->nr_labels-1 ? "\n" : ", "); 673ca6549afSSteven Whitehouse } 674ca6549afSSteven Whitehouse 67534954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) { 67634954ddcSFrancesco Fondelli seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n", 67734954ddcSFrancesco Fondelli pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi); 67834954ddcSFrancesco Fondelli } 67934954ddcSFrancesco Fondelli 68034954ddcSFrancesco Fondelli if (pkt_dev->svlan_id != 0xffff) { 68134954ddcSFrancesco Fondelli seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n", 68234954ddcSFrancesco Fondelli pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi); 68334954ddcSFrancesco Fondelli } 68434954ddcSFrancesco Fondelli 6851ca7768cSFrancesco Fondelli if (pkt_dev->tos) { 6861ca7768cSFrancesco Fondelli seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos); 6871ca7768cSFrancesco Fondelli } 6881ca7768cSFrancesco Fondelli 6891ca7768cSFrancesco Fondelli if (pkt_dev->traffic_class) { 6901ca7768cSFrancesco Fondelli seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class); 6911ca7768cSFrancesco Fondelli } 6921ca7768cSFrancesco Fondelli 693ca6549afSSteven Whitehouse seq_printf(seq, " Flags: "); 694ca6549afSSteven Whitehouse 6951da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) 696d50a6b56SStephen Hemminger seq_printf(seq, "IPV6 "); 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 699d50a6b56SStephen Hemminger seq_printf(seq, "IPSRC_RND "); 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) 702d50a6b56SStephen Hemminger seq_printf(seq, "IPDST_RND "); 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) 705d50a6b56SStephen Hemminger seq_printf(seq, "TXSIZE_RND "); 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 708d50a6b56SStephen Hemminger seq_printf(seq, "UDPSRC_RND "); 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) 711d50a6b56SStephen Hemminger seq_printf(seq, "UDPDST_RND "); 7121da177e4SLinus Torvalds 713ca6549afSSteven Whitehouse if (pkt_dev->flags & F_MPLS_RND) 714ca6549afSSteven Whitehouse seq_printf(seq, "MPLS_RND "); 715ca6549afSSteven Whitehouse 71645b270f8SRobert Olsson if (pkt_dev->flags & F_QUEUE_MAP_RND) 71745b270f8SRobert Olsson seq_printf(seq, "QUEUE_MAP_RND "); 71845b270f8SRobert Olsson 719007a531bSJamal Hadi Salim if (pkt_dev->cflows) { 720007a531bSJamal Hadi Salim if (pkt_dev->flags & F_FLOW_SEQ) 721007a531bSJamal Hadi Salim seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/ 722007a531bSJamal Hadi Salim else 723007a531bSJamal Hadi Salim seq_printf(seq, "FLOW_RND "); 724007a531bSJamal Hadi Salim } 725007a531bSJamal Hadi Salim 726a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 727a553e4a6SJamal Hadi Salim if (pkt_dev->flags & F_IPSEC_ON) 728a553e4a6SJamal Hadi Salim seq_printf(seq, "IPSEC "); 729a553e4a6SJamal Hadi Salim #endif 730a553e4a6SJamal Hadi Salim 7311da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 732d50a6b56SStephen Hemminger seq_printf(seq, "MACSRC_RND "); 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 735d50a6b56SStephen Hemminger seq_printf(seq, "MACDST_RND "); 7361da177e4SLinus Torvalds 73734954ddcSFrancesco Fondelli if (pkt_dev->flags & F_VID_RND) 73834954ddcSFrancesco Fondelli seq_printf(seq, "VID_RND "); 73934954ddcSFrancesco Fondelli 74034954ddcSFrancesco Fondelli if (pkt_dev->flags & F_SVID_RND) 74134954ddcSFrancesco Fondelli seq_printf(seq, "SVID_RND "); 74234954ddcSFrancesco Fondelli 743d50a6b56SStephen Hemminger seq_puts(seq, "\n"); 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds sa = pkt_dev->started_at; 7461da177e4SLinus Torvalds stopped = pkt_dev->stopped_at; 7471da177e4SLinus Torvalds if (pkt_dev->running) 7481da177e4SLinus Torvalds stopped = now; /* not really stopped, more like last-running-at */ 7491da177e4SLinus Torvalds 750222f1806SLuiz Capitulino seq_printf(seq, 751222f1806SLuiz Capitulino "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n", 7521da177e4SLinus Torvalds (unsigned long long)pkt_dev->sofar, 753222f1806SLuiz Capitulino (unsigned long long)pkt_dev->errors, (unsigned long long)sa, 7541da177e4SLinus Torvalds (unsigned long long)stopped, 7551da177e4SLinus Torvalds (unsigned long long)pkt_dev->idle_acc); 7561da177e4SLinus Torvalds 757222f1806SLuiz Capitulino seq_printf(seq, 758222f1806SLuiz Capitulino " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n", 759d50a6b56SStephen Hemminger pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset, 760d50a6b56SStephen Hemminger pkt_dev->cur_src_mac_offset); 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 7631da177e4SLinus Torvalds char b1[128], b2[128]; 7641da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr); 7651da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr); 766d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1); 767222f1806SLuiz Capitulino } else 768d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n", 7691da177e4SLinus Torvalds pkt_dev->cur_saddr, pkt_dev->cur_daddr); 7701da177e4SLinus Torvalds 771d50a6b56SStephen Hemminger seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n", 7721da177e4SLinus Torvalds pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src); 7731da177e4SLinus Torvalds 77445b270f8SRobert Olsson seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map); 77545b270f8SRobert Olsson 776d50a6b56SStephen Hemminger seq_printf(seq, " flows: %u\n", pkt_dev->nflows); 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds if (pkt_dev->result[0]) 779d50a6b56SStephen Hemminger seq_printf(seq, "Result: %s\n", pkt_dev->result); 7801da177e4SLinus Torvalds else 781d50a6b56SStephen Hemminger seq_printf(seq, "Result: Idle\n"); 7821da177e4SLinus Torvalds 783d50a6b56SStephen Hemminger return 0; 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 786ca6549afSSteven Whitehouse 7871ca7768cSFrancesco Fondelli static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num) 788ca6549afSSteven Whitehouse { 789ca6549afSSteven Whitehouse int i = 0; 790ca6549afSSteven Whitehouse *num = 0; 791ca6549afSSteven Whitehouse 7921ca7768cSFrancesco Fondelli for (; i < maxlen; i++) { 793ca6549afSSteven Whitehouse char c; 794ca6549afSSteven Whitehouse *num <<= 4; 795ca6549afSSteven Whitehouse if (get_user(c, &user_buffer[i])) 796ca6549afSSteven Whitehouse return -EFAULT; 797ca6549afSSteven Whitehouse if ((c >= '0') && (c <= '9')) 798ca6549afSSteven Whitehouse *num |= c - '0'; 799ca6549afSSteven Whitehouse else if ((c >= 'a') && (c <= 'f')) 800ca6549afSSteven Whitehouse *num |= c - 'a' + 10; 801ca6549afSSteven Whitehouse else if ((c >= 'A') && (c <= 'F')) 802ca6549afSSteven Whitehouse *num |= c - 'A' + 10; 803ca6549afSSteven Whitehouse else 804ca6549afSSteven Whitehouse break; 805ca6549afSSteven Whitehouse } 806ca6549afSSteven Whitehouse return i; 807ca6549afSSteven Whitehouse } 808ca6549afSSteven Whitehouse 809222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer, 810222f1806SLuiz Capitulino unsigned int maxlen) 8111da177e4SLinus Torvalds { 8121da177e4SLinus Torvalds int i; 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds for (i = 0; i < maxlen; i++) { 8151da177e4SLinus Torvalds char c; 8161da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 8171da177e4SLinus Torvalds return -EFAULT; 8181da177e4SLinus Torvalds switch (c) { 8191da177e4SLinus Torvalds case '\"': 8201da177e4SLinus Torvalds case '\n': 8211da177e4SLinus Torvalds case '\r': 8221da177e4SLinus Torvalds case '\t': 8231da177e4SLinus Torvalds case ' ': 8241da177e4SLinus Torvalds case '=': 8251da177e4SLinus Torvalds break; 8261da177e4SLinus Torvalds default: 8271da177e4SLinus Torvalds goto done; 8283ff50b79SStephen Hemminger } 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds done: 8311da177e4SLinus Torvalds return i; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 834222f1806SLuiz Capitulino static unsigned long num_arg(const char __user * user_buffer, 835222f1806SLuiz Capitulino unsigned long maxlen, unsigned long *num) 8361da177e4SLinus Torvalds { 8371da177e4SLinus Torvalds int i = 0; 8381da177e4SLinus Torvalds *num = 0; 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds for (; i < maxlen; i++) { 8411da177e4SLinus Torvalds char c; 8421da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 8431da177e4SLinus Torvalds return -EFAULT; 8441da177e4SLinus Torvalds if ((c >= '0') && (c <= '9')) { 8451da177e4SLinus Torvalds *num *= 10; 8461da177e4SLinus Torvalds *num += c - '0'; 8471da177e4SLinus Torvalds } else 8481da177e4SLinus Torvalds break; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds return i; 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen) 8541da177e4SLinus Torvalds { 8551da177e4SLinus Torvalds int i = 0; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds for (; i < maxlen; i++) { 8581da177e4SLinus Torvalds char c; 8591da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 8601da177e4SLinus Torvalds return -EFAULT; 8611da177e4SLinus Torvalds switch (c) { 8621da177e4SLinus Torvalds case '\"': 8631da177e4SLinus Torvalds case '\n': 8641da177e4SLinus Torvalds case '\r': 8651da177e4SLinus Torvalds case '\t': 8661da177e4SLinus Torvalds case ' ': 8671da177e4SLinus Torvalds goto done_str; 8681da177e4SLinus Torvalds break; 8691da177e4SLinus Torvalds default: 8701da177e4SLinus Torvalds break; 8713ff50b79SStephen Hemminger } 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds done_str: 8741da177e4SLinus Torvalds return i; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 877ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev) 878ca6549afSSteven Whitehouse { 879ca6549afSSteven Whitehouse unsigned n = 0; 880ca6549afSSteven Whitehouse char c; 881ca6549afSSteven Whitehouse ssize_t i = 0; 882ca6549afSSteven Whitehouse int len; 883ca6549afSSteven Whitehouse 884ca6549afSSteven Whitehouse pkt_dev->nr_labels = 0; 885ca6549afSSteven Whitehouse do { 886ca6549afSSteven Whitehouse __u32 tmp; 8871ca7768cSFrancesco Fondelli len = hex32_arg(&buffer[i], 8, &tmp); 888ca6549afSSteven Whitehouse if (len <= 0) 889ca6549afSSteven Whitehouse return len; 890ca6549afSSteven Whitehouse pkt_dev->labels[n] = htonl(tmp); 891ca6549afSSteven Whitehouse if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM) 892ca6549afSSteven Whitehouse pkt_dev->flags |= F_MPLS_RND; 893ca6549afSSteven Whitehouse i += len; 894ca6549afSSteven Whitehouse if (get_user(c, &buffer[i])) 895ca6549afSSteven Whitehouse return -EFAULT; 896ca6549afSSteven Whitehouse i++; 897ca6549afSSteven Whitehouse n++; 898ca6549afSSteven Whitehouse if (n >= MAX_MPLS_LABELS) 899ca6549afSSteven Whitehouse return -E2BIG; 900ca6549afSSteven Whitehouse } while (c == ','); 901ca6549afSSteven Whitehouse 902ca6549afSSteven Whitehouse pkt_dev->nr_labels = n; 903ca6549afSSteven Whitehouse return i; 904ca6549afSSteven Whitehouse } 905ca6549afSSteven Whitehouse 906222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file, 907222f1806SLuiz Capitulino const char __user * user_buffer, size_t count, 908222f1806SLuiz Capitulino loff_t * offset) 9091da177e4SLinus Torvalds { 910d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *)file->private_data; 911d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 9121da177e4SLinus Torvalds int i = 0, max, len; 9131da177e4SLinus Torvalds char name[16], valstr[32]; 9141da177e4SLinus Torvalds unsigned long value = 0; 9151da177e4SLinus Torvalds char *pg_result = NULL; 9161da177e4SLinus Torvalds int tmp = 0; 9171da177e4SLinus Torvalds char buf[128]; 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds pg_result = &(pkt_dev->result[0]); 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds if (count < 1) { 92225a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: wrong command format\n"); 9231da177e4SLinus Torvalds return -EINVAL; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds max = count - i; 9271da177e4SLinus Torvalds tmp = count_trail_chars(&user_buffer[i], max); 9281da177e4SLinus Torvalds if (tmp < 0) { 92925a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: illegal format\n"); 9301da177e4SLinus Torvalds return tmp; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds i += tmp; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* Read variable name */ 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 937222f1806SLuiz Capitulino if (len < 0) { 938222f1806SLuiz Capitulino return len; 939222f1806SLuiz Capitulino } 9401da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 9411da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len)) 9421da177e4SLinus Torvalds return -EFAULT; 9431da177e4SLinus Torvalds i += len; 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds max = count - i; 9461da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 9471da177e4SLinus Torvalds if (len < 0) 9481da177e4SLinus Torvalds return len; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds i += len; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds if (debug) { 9531da177e4SLinus Torvalds char tb[count + 1]; 9541da177e4SLinus Torvalds if (copy_from_user(tb, user_buffer, count)) 9551da177e4SLinus Torvalds return -EFAULT; 9561da177e4SLinus Torvalds tb[count] = 0; 95725a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name, 958d50a6b56SStephen Hemminger (unsigned long)count, tb); 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds if (!strcmp(name, "min_pkt_size")) { 9621da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 963222f1806SLuiz Capitulino if (len < 0) { 964222f1806SLuiz Capitulino return len; 965222f1806SLuiz Capitulino } 9661da177e4SLinus Torvalds i += len; 9671da177e4SLinus Torvalds if (value < 14 + 20 + 8) 9681da177e4SLinus Torvalds value = 14 + 20 + 8; 9691da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 9701da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 9711da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 9721da177e4SLinus Torvalds } 973222f1806SLuiz Capitulino sprintf(pg_result, "OK: min_pkt_size=%u", 974222f1806SLuiz Capitulino pkt_dev->min_pkt_size); 9751da177e4SLinus Torvalds return count; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds if (!strcmp(name, "max_pkt_size")) { 9791da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 980222f1806SLuiz Capitulino if (len < 0) { 981222f1806SLuiz Capitulino return len; 982222f1806SLuiz Capitulino } 9831da177e4SLinus Torvalds i += len; 9841da177e4SLinus Torvalds if (value < 14 + 20 + 8) 9851da177e4SLinus Torvalds value = 14 + 20 + 8; 9861da177e4SLinus Torvalds if (value != pkt_dev->max_pkt_size) { 9871da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 9881da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 9891da177e4SLinus Torvalds } 990222f1806SLuiz Capitulino sprintf(pg_result, "OK: max_pkt_size=%u", 991222f1806SLuiz Capitulino pkt_dev->max_pkt_size); 9921da177e4SLinus Torvalds return count; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds /* Shortcut for min = max */ 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds if (!strcmp(name, "pkt_size")) { 9981da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 999222f1806SLuiz Capitulino if (len < 0) { 1000222f1806SLuiz Capitulino return len; 1001222f1806SLuiz Capitulino } 10021da177e4SLinus Torvalds i += len; 10031da177e4SLinus Torvalds if (value < 14 + 20 + 8) 10041da177e4SLinus Torvalds value = 14 + 20 + 8; 10051da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 10061da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 10071da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 10081da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size); 10111da177e4SLinus Torvalds return count; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds if (!strcmp(name, "debug")) { 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 debug = value; 10211da177e4SLinus Torvalds sprintf(pg_result, "OK: debug=%u", debug); 10221da177e4SLinus Torvalds return count; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds if (!strcmp(name, "frags")) { 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->nfrags = value; 10321da177e4SLinus Torvalds sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags); 10331da177e4SLinus Torvalds return count; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds if (!strcmp(name, "delay")) { 10361da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1037222f1806SLuiz Capitulino if (len < 0) { 1038222f1806SLuiz Capitulino return len; 1039222f1806SLuiz Capitulino } 10401da177e4SLinus Torvalds i += len; 10411da177e4SLinus Torvalds if (value == 0x7FFFFFFF) { 10421da177e4SLinus Torvalds pkt_dev->delay_us = 0x7FFFFFFF; 10431da177e4SLinus Torvalds pkt_dev->delay_ns = 0; 10441da177e4SLinus Torvalds } else { 10451da177e4SLinus Torvalds pkt_dev->delay_us = value / 1000; 10461da177e4SLinus Torvalds pkt_dev->delay_ns = value % 1000; 10471da177e4SLinus Torvalds } 1048222f1806SLuiz Capitulino sprintf(pg_result, "OK: delay=%u", 1049222f1806SLuiz Capitulino 1000 * pkt_dev->delay_us + pkt_dev->delay_ns); 10501da177e4SLinus Torvalds return count; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds if (!strcmp(name, "udp_src_min")) { 10531da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1054222f1806SLuiz Capitulino if (len < 0) { 1055222f1806SLuiz Capitulino return len; 1056222f1806SLuiz Capitulino } 10571da177e4SLinus Torvalds i += len; 10581da177e4SLinus Torvalds if (value != pkt_dev->udp_src_min) { 10591da177e4SLinus Torvalds pkt_dev->udp_src_min = value; 10601da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min); 10631da177e4SLinus Torvalds return count; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_min")) { 10661da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1067222f1806SLuiz Capitulino if (len < 0) { 1068222f1806SLuiz Capitulino return len; 1069222f1806SLuiz Capitulino } 10701da177e4SLinus Torvalds i += len; 10711da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_min) { 10721da177e4SLinus Torvalds pkt_dev->udp_dst_min = value; 10731da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min); 10761da177e4SLinus Torvalds return count; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds if (!strcmp(name, "udp_src_max")) { 10791da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1080222f1806SLuiz Capitulino if (len < 0) { 1081222f1806SLuiz Capitulino return len; 1082222f1806SLuiz Capitulino } 10831da177e4SLinus Torvalds i += len; 10841da177e4SLinus Torvalds if (value != pkt_dev->udp_src_max) { 10851da177e4SLinus Torvalds pkt_dev->udp_src_max = value; 10861da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max); 10891da177e4SLinus Torvalds return count; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_max")) { 10921da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1093222f1806SLuiz Capitulino if (len < 0) { 1094222f1806SLuiz Capitulino return len; 1095222f1806SLuiz Capitulino } 10961da177e4SLinus Torvalds i += len; 10971da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_max) { 10981da177e4SLinus Torvalds pkt_dev->udp_dst_max = value; 10991da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max); 11021da177e4SLinus Torvalds return count; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds if (!strcmp(name, "clone_skb")) { 11051da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1106222f1806SLuiz Capitulino if (len < 0) { 1107222f1806SLuiz Capitulino return len; 1108222f1806SLuiz Capitulino } 11091da177e4SLinus Torvalds i += len; 11101da177e4SLinus Torvalds pkt_dev->clone_skb = value; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb); 11131da177e4SLinus Torvalds return count; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds if (!strcmp(name, "count")) { 11161da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1117222f1806SLuiz Capitulino if (len < 0) { 1118222f1806SLuiz Capitulino return len; 1119222f1806SLuiz Capitulino } 11201da177e4SLinus Torvalds i += len; 11211da177e4SLinus Torvalds pkt_dev->count = value; 11221da177e4SLinus Torvalds sprintf(pg_result, "OK: count=%llu", 11231da177e4SLinus Torvalds (unsigned long long)pkt_dev->count); 11241da177e4SLinus Torvalds return count; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds if (!strcmp(name, "src_mac_count")) { 11271da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1128222f1806SLuiz Capitulino if (len < 0) { 1129222f1806SLuiz Capitulino return len; 1130222f1806SLuiz Capitulino } 11311da177e4SLinus Torvalds i += len; 11321da177e4SLinus Torvalds if (pkt_dev->src_mac_count != value) { 11331da177e4SLinus Torvalds pkt_dev->src_mac_count = value; 11341da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 11351da177e4SLinus Torvalds } 1136222f1806SLuiz Capitulino sprintf(pg_result, "OK: src_mac_count=%d", 1137222f1806SLuiz Capitulino pkt_dev->src_mac_count); 11381da177e4SLinus Torvalds return count; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds if (!strcmp(name, "dst_mac_count")) { 11411da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1142222f1806SLuiz Capitulino if (len < 0) { 1143222f1806SLuiz Capitulino return len; 1144222f1806SLuiz Capitulino } 11451da177e4SLinus Torvalds i += len; 11461da177e4SLinus Torvalds if (pkt_dev->dst_mac_count != value) { 11471da177e4SLinus Torvalds pkt_dev->dst_mac_count = value; 11481da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 11491da177e4SLinus Torvalds } 1150222f1806SLuiz Capitulino sprintf(pg_result, "OK: dst_mac_count=%d", 1151222f1806SLuiz Capitulino pkt_dev->dst_mac_count); 11521da177e4SLinus Torvalds return count; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds if (!strcmp(name, "flag")) { 11551da177e4SLinus Torvalds char f[32]; 11561da177e4SLinus Torvalds memset(f, 0, 32); 11571da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 1158222f1806SLuiz Capitulino if (len < 0) { 1159222f1806SLuiz Capitulino return len; 1160222f1806SLuiz Capitulino } 11611da177e4SLinus Torvalds if (copy_from_user(f, &user_buffer[i], len)) 11621da177e4SLinus Torvalds return -EFAULT; 11631da177e4SLinus Torvalds i += len; 11641da177e4SLinus Torvalds if (strcmp(f, "IPSRC_RND") == 0) 11651da177e4SLinus Torvalds pkt_dev->flags |= F_IPSRC_RND; 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds else if (strcmp(f, "!IPSRC_RND") == 0) 11681da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPSRC_RND; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds else if (strcmp(f, "TXSIZE_RND") == 0) 11711da177e4SLinus Torvalds pkt_dev->flags |= F_TXSIZE_RND; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds else if (strcmp(f, "!TXSIZE_RND") == 0) 11741da177e4SLinus Torvalds pkt_dev->flags &= ~F_TXSIZE_RND; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds else if (strcmp(f, "IPDST_RND") == 0) 11771da177e4SLinus Torvalds pkt_dev->flags |= F_IPDST_RND; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds else if (strcmp(f, "!IPDST_RND") == 0) 11801da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPDST_RND; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds else if (strcmp(f, "UDPSRC_RND") == 0) 11831da177e4SLinus Torvalds pkt_dev->flags |= F_UDPSRC_RND; 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds else if (strcmp(f, "!UDPSRC_RND") == 0) 11861da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPSRC_RND; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds else if (strcmp(f, "UDPDST_RND") == 0) 11891da177e4SLinus Torvalds pkt_dev->flags |= F_UDPDST_RND; 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds else if (strcmp(f, "!UDPDST_RND") == 0) 11921da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPDST_RND; 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds else if (strcmp(f, "MACSRC_RND") == 0) 11951da177e4SLinus Torvalds pkt_dev->flags |= F_MACSRC_RND; 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds else if (strcmp(f, "!MACSRC_RND") == 0) 11981da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACSRC_RND; 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds else if (strcmp(f, "MACDST_RND") == 0) 12011da177e4SLinus Torvalds pkt_dev->flags |= F_MACDST_RND; 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds else if (strcmp(f, "!MACDST_RND") == 0) 12041da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACDST_RND; 12051da177e4SLinus Torvalds 1206ca6549afSSteven Whitehouse else if (strcmp(f, "MPLS_RND") == 0) 1207ca6549afSSteven Whitehouse pkt_dev->flags |= F_MPLS_RND; 1208ca6549afSSteven Whitehouse 1209ca6549afSSteven Whitehouse else if (strcmp(f, "!MPLS_RND") == 0) 1210ca6549afSSteven Whitehouse pkt_dev->flags &= ~F_MPLS_RND; 1211ca6549afSSteven Whitehouse 121234954ddcSFrancesco Fondelli else if (strcmp(f, "VID_RND") == 0) 121334954ddcSFrancesco Fondelli pkt_dev->flags |= F_VID_RND; 121434954ddcSFrancesco Fondelli 121534954ddcSFrancesco Fondelli else if (strcmp(f, "!VID_RND") == 0) 121634954ddcSFrancesco Fondelli pkt_dev->flags &= ~F_VID_RND; 121734954ddcSFrancesco Fondelli 121834954ddcSFrancesco Fondelli else if (strcmp(f, "SVID_RND") == 0) 121934954ddcSFrancesco Fondelli pkt_dev->flags |= F_SVID_RND; 122034954ddcSFrancesco Fondelli 122134954ddcSFrancesco Fondelli else if (strcmp(f, "!SVID_RND") == 0) 122234954ddcSFrancesco Fondelli pkt_dev->flags &= ~F_SVID_RND; 122334954ddcSFrancesco Fondelli 1224007a531bSJamal Hadi Salim else if (strcmp(f, "FLOW_SEQ") == 0) 1225007a531bSJamal Hadi Salim pkt_dev->flags |= F_FLOW_SEQ; 1226007a531bSJamal Hadi Salim 122745b270f8SRobert Olsson else if (strcmp(f, "QUEUE_MAP_RND") == 0) 122845b270f8SRobert Olsson pkt_dev->flags |= F_QUEUE_MAP_RND; 122945b270f8SRobert Olsson 123045b270f8SRobert Olsson else if (strcmp(f, "!QUEUE_MAP_RND") == 0) 123145b270f8SRobert Olsson pkt_dev->flags &= ~F_QUEUE_MAP_RND; 1232a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 1233a553e4a6SJamal Hadi Salim else if (strcmp(f, "IPSEC") == 0) 1234a553e4a6SJamal Hadi Salim pkt_dev->flags |= F_IPSEC_ON; 1235a553e4a6SJamal Hadi Salim #endif 1236a553e4a6SJamal Hadi Salim 12371ca7768cSFrancesco Fondelli else if (strcmp(f, "!IPV6") == 0) 12381ca7768cSFrancesco Fondelli pkt_dev->flags &= ~F_IPV6; 12391ca7768cSFrancesco Fondelli 12401da177e4SLinus Torvalds else { 1241222f1806SLuiz Capitulino sprintf(pg_result, 1242222f1806SLuiz Capitulino "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s", 12431da177e4SLinus Torvalds f, 12441ca7768cSFrancesco Fondelli "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, " 1245a553e4a6SJamal Hadi Salim "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n"); 12461da177e4SLinus Torvalds return count; 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags); 12491da177e4SLinus Torvalds return count; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) { 12521da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1); 1253222f1806SLuiz Capitulino if (len < 0) { 1254222f1806SLuiz Capitulino return len; 1255222f1806SLuiz Capitulino } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12581da177e4SLinus Torvalds return -EFAULT; 12591da177e4SLinus Torvalds buf[len] = 0; 12601da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_min) != 0) { 12611da177e4SLinus Torvalds memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min)); 12621da177e4SLinus Torvalds strncpy(pkt_dev->dst_min, buf, len); 12631da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 12641da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds if (debug) 126725a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst_min set to: %s\n", 1268222f1806SLuiz Capitulino pkt_dev->dst_min); 12691da177e4SLinus Torvalds i += len; 12701da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min); 12711da177e4SLinus Torvalds return count; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds if (!strcmp(name, "dst_max")) { 12741da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1); 1275222f1806SLuiz Capitulino if (len < 0) { 1276222f1806SLuiz Capitulino return len; 1277222f1806SLuiz Capitulino } 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12801da177e4SLinus Torvalds return -EFAULT; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds buf[len] = 0; 12831da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_max) != 0) { 12841da177e4SLinus Torvalds memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max)); 12851da177e4SLinus Torvalds strncpy(pkt_dev->dst_max, buf, len); 12861da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 12871da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_max; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds if (debug) 129025a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst_max set to: %s\n", 1291222f1806SLuiz Capitulino pkt_dev->dst_max); 12921da177e4SLinus Torvalds i += len; 12931da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max); 12941da177e4SLinus Torvalds return count; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds if (!strcmp(name, "dst6")) { 12971da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1298222f1806SLuiz Capitulino if (len < 0) 1299222f1806SLuiz Capitulino return len; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13041da177e4SLinus Torvalds return -EFAULT; 13051da177e4SLinus Torvalds buf[len] = 0; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_daddr.s6_addr); 13081da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr); 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr); 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds if (debug) 131325a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf); 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds i += len; 13161da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6=%s", buf); 13171da177e4SLinus Torvalds return count; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds if (!strcmp(name, "dst6_min")) { 13201da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1321222f1806SLuiz Capitulino if (len < 0) 1322222f1806SLuiz Capitulino return len; 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13271da177e4SLinus Torvalds return -EFAULT; 13281da177e4SLinus Torvalds buf[len] = 0; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 13311da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 13321da177e4SLinus Torvalds 1333222f1806SLuiz Capitulino ipv6_addr_copy(&pkt_dev->cur_in6_daddr, 1334222f1806SLuiz Capitulino &pkt_dev->min_in6_daddr); 13351da177e4SLinus Torvalds if (debug) 133625a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf); 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds i += len; 13391da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_min=%s", buf); 13401da177e4SLinus Torvalds return count; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds if (!strcmp(name, "dst6_max")) { 13431da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1344222f1806SLuiz Capitulino if (len < 0) 1345222f1806SLuiz Capitulino return len; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13501da177e4SLinus Torvalds return -EFAULT; 13511da177e4SLinus Torvalds buf[len] = 0; 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 13541da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds if (debug) 135725a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf); 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds i += len; 13601da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_max=%s", buf); 13611da177e4SLinus Torvalds return count; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds if (!strcmp(name, "src6")) { 13641da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1365222f1806SLuiz Capitulino if (len < 0) 1366222f1806SLuiz Capitulino return len; 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13711da177e4SLinus Torvalds return -EFAULT; 13721da177e4SLinus Torvalds buf[len] = 0; 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_saddr.s6_addr); 13751da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr); 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr); 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds if (debug) 138025a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf); 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds i += len; 13831da177e4SLinus Torvalds sprintf(pg_result, "OK: src6=%s", buf); 13841da177e4SLinus Torvalds return count; 13851da177e4SLinus Torvalds } 13861da177e4SLinus Torvalds if (!strcmp(name, "src_min")) { 13871da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1); 1388222f1806SLuiz Capitulino if (len < 0) { 1389222f1806SLuiz Capitulino return len; 1390222f1806SLuiz Capitulino } 13911da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13921da177e4SLinus Torvalds return -EFAULT; 13931da177e4SLinus Torvalds buf[len] = 0; 13941da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_min) != 0) { 13951da177e4SLinus Torvalds memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min)); 13961da177e4SLinus Torvalds strncpy(pkt_dev->src_min, buf, len); 13971da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 13981da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds if (debug) 140125a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: src_min set to: %s\n", 1402222f1806SLuiz Capitulino pkt_dev->src_min); 14031da177e4SLinus Torvalds i += len; 14041da177e4SLinus Torvalds sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min); 14051da177e4SLinus Torvalds return count; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds if (!strcmp(name, "src_max")) { 14081da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1); 1409222f1806SLuiz Capitulino if (len < 0) { 1410222f1806SLuiz Capitulino return len; 1411222f1806SLuiz Capitulino } 14121da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 14131da177e4SLinus Torvalds return -EFAULT; 14141da177e4SLinus Torvalds buf[len] = 0; 14151da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_max) != 0) { 14161da177e4SLinus Torvalds memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max)); 14171da177e4SLinus Torvalds strncpy(pkt_dev->src_max, buf, len); 14181da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 14191da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_max; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds if (debug) 142225a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: src_max set to: %s\n", 1423222f1806SLuiz Capitulino pkt_dev->src_max); 14241da177e4SLinus Torvalds i += len; 14251da177e4SLinus Torvalds sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max); 14261da177e4SLinus Torvalds return count; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds if (!strcmp(name, "dst_mac")) { 14291da177e4SLinus Torvalds char *v = valstr; 1430f404e9a6SKris Katterjohn unsigned char old_dmac[ETH_ALEN]; 14311da177e4SLinus Torvalds unsigned char *m = pkt_dev->dst_mac; 1432f404e9a6SKris Katterjohn memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN); 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 1435222f1806SLuiz Capitulino if (len < 0) { 1436222f1806SLuiz Capitulino return len; 1437222f1806SLuiz Capitulino } 14381da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 14391da177e4SLinus Torvalds if (copy_from_user(valstr, &user_buffer[i], len)) 14401da177e4SLinus Torvalds return -EFAULT; 14411da177e4SLinus Torvalds i += len; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) { 14441da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 14451da177e4SLinus Torvalds *m *= 16; 14461da177e4SLinus Torvalds *m += *v - '0'; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 14491da177e4SLinus Torvalds *m *= 16; 14501da177e4SLinus Torvalds *m += *v - 'A' + 10; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 14531da177e4SLinus Torvalds *m *= 16; 14541da177e4SLinus Torvalds *m += *v - 'a' + 10; 14551da177e4SLinus Torvalds } 14561da177e4SLinus Torvalds if (*v == ':') { 14571da177e4SLinus Torvalds m++; 14581da177e4SLinus Torvalds *m = 0; 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds /* Set up Dest MAC */ 1463f404e9a6SKris Katterjohn if (compare_ether_addr(old_dmac, pkt_dev->dst_mac)) 1464f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds sprintf(pg_result, "OK: dstmac"); 14671da177e4SLinus Torvalds return count; 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds if (!strcmp(name, "src_mac")) { 14701da177e4SLinus Torvalds char *v = valstr; 1471ce5d0b47SAdit Ranadive unsigned char old_smac[ETH_ALEN]; 14721da177e4SLinus Torvalds unsigned char *m = pkt_dev->src_mac; 14731da177e4SLinus Torvalds 1474ce5d0b47SAdit Ranadive memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN); 1475ce5d0b47SAdit Ranadive 14761da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 1477222f1806SLuiz Capitulino if (len < 0) { 1478222f1806SLuiz Capitulino return len; 1479222f1806SLuiz Capitulino } 14801da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 14811da177e4SLinus Torvalds if (copy_from_user(valstr, &user_buffer[i], len)) 14821da177e4SLinus Torvalds return -EFAULT; 14831da177e4SLinus Torvalds i += len; 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) { 14861da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 14871da177e4SLinus Torvalds *m *= 16; 14881da177e4SLinus Torvalds *m += *v - '0'; 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 14911da177e4SLinus Torvalds *m *= 16; 14921da177e4SLinus Torvalds *m += *v - 'A' + 10; 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 14951da177e4SLinus Torvalds *m *= 16; 14961da177e4SLinus Torvalds *m += *v - 'a' + 10; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds if (*v == ':') { 14991da177e4SLinus Torvalds m++; 15001da177e4SLinus Torvalds *m = 0; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds 1504ce5d0b47SAdit Ranadive /* Set up Src MAC */ 1505ce5d0b47SAdit Ranadive if (compare_ether_addr(old_smac, pkt_dev->src_mac)) 1506ce5d0b47SAdit Ranadive memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN); 1507ce5d0b47SAdit Ranadive 15081da177e4SLinus Torvalds sprintf(pg_result, "OK: srcmac"); 15091da177e4SLinus Torvalds return count; 15101da177e4SLinus Torvalds } 15111da177e4SLinus Torvalds 15121da177e4SLinus Torvalds if (!strcmp(name, "clear_counters")) { 15131da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 15141da177e4SLinus Torvalds sprintf(pg_result, "OK: Clearing counters.\n"); 15151da177e4SLinus Torvalds return count; 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds if (!strcmp(name, "flows")) { 15191da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1520222f1806SLuiz Capitulino if (len < 0) { 1521222f1806SLuiz Capitulino return len; 1522222f1806SLuiz Capitulino } 15231da177e4SLinus Torvalds i += len; 15241da177e4SLinus Torvalds if (value > MAX_CFLOWS) 15251da177e4SLinus Torvalds value = MAX_CFLOWS; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds pkt_dev->cflows = value; 15281da177e4SLinus Torvalds sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows); 15291da177e4SLinus Torvalds return count; 15301da177e4SLinus Torvalds } 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds if (!strcmp(name, "flowlen")) { 15331da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1534222f1806SLuiz Capitulino if (len < 0) { 1535222f1806SLuiz Capitulino return len; 1536222f1806SLuiz Capitulino } 15371da177e4SLinus Torvalds i += len; 15381da177e4SLinus Torvalds pkt_dev->lflow = value; 15391da177e4SLinus Torvalds sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow); 15401da177e4SLinus Torvalds return count; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 154345b270f8SRobert Olsson if (!strcmp(name, "queue_map_min")) { 154445b270f8SRobert Olsson len = num_arg(&user_buffer[i], 5, &value); 154545b270f8SRobert Olsson if (len < 0) { 154645b270f8SRobert Olsson return len; 154745b270f8SRobert Olsson } 154845b270f8SRobert Olsson i += len; 154945b270f8SRobert Olsson pkt_dev->queue_map_min = value; 155045b270f8SRobert Olsson sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min); 155145b270f8SRobert Olsson return count; 155245b270f8SRobert Olsson } 155345b270f8SRobert Olsson 155445b270f8SRobert Olsson if (!strcmp(name, "queue_map_max")) { 155545b270f8SRobert Olsson len = num_arg(&user_buffer[i], 5, &value); 155645b270f8SRobert Olsson if (len < 0) { 155745b270f8SRobert Olsson return len; 155845b270f8SRobert Olsson } 155945b270f8SRobert Olsson i += len; 156045b270f8SRobert Olsson pkt_dev->queue_map_max = value; 156145b270f8SRobert Olsson sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max); 156245b270f8SRobert Olsson return count; 156345b270f8SRobert Olsson } 156445b270f8SRobert Olsson 1565ca6549afSSteven Whitehouse if (!strcmp(name, "mpls")) { 1566*cfcabdccSStephen Hemminger unsigned n, cnt; 1567*cfcabdccSStephen Hemminger 1568ca6549afSSteven Whitehouse len = get_labels(&user_buffer[i], pkt_dev); 1569*cfcabdccSStephen Hemminger if (len < 0) 1570*cfcabdccSStephen Hemminger return len; 1571ca6549afSSteven Whitehouse i += len; 1572*cfcabdccSStephen Hemminger cnt = sprintf(pg_result, "OK: mpls="); 1573ca6549afSSteven Whitehouse for (n = 0; n < pkt_dev->nr_labels; n++) 1574*cfcabdccSStephen Hemminger cnt += sprintf(pg_result + cnt, 1575ca6549afSSteven Whitehouse "%08x%s", ntohl(pkt_dev->labels[n]), 1576ca6549afSSteven Whitehouse n == pkt_dev->nr_labels-1 ? "" : ","); 157734954ddcSFrancesco Fondelli 157834954ddcSFrancesco Fondelli if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) { 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 auto turned off\n"); 158434954ddcSFrancesco Fondelli } 158534954ddcSFrancesco Fondelli return count; 158634954ddcSFrancesco Fondelli } 158734954ddcSFrancesco Fondelli 158834954ddcSFrancesco Fondelli if (!strcmp(name, "vlan_id")) { 158934954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 4, &value); 159034954ddcSFrancesco Fondelli if (len < 0) { 159134954ddcSFrancesco Fondelli return len; 159234954ddcSFrancesco Fondelli } 159334954ddcSFrancesco Fondelli i += len; 159434954ddcSFrancesco Fondelli if (value <= 4095) { 159534954ddcSFrancesco Fondelli pkt_dev->vlan_id = value; /* turn on VLAN */ 159634954ddcSFrancesco Fondelli 159734954ddcSFrancesco Fondelli if (debug) 159825a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: VLAN turned on\n"); 159934954ddcSFrancesco Fondelli 160034954ddcSFrancesco Fondelli if (debug && pkt_dev->nr_labels) 160125a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: MPLS auto turned off\n"); 160234954ddcSFrancesco Fondelli 160334954ddcSFrancesco Fondelli pkt_dev->nr_labels = 0; /* turn off MPLS */ 160434954ddcSFrancesco Fondelli sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id); 160534954ddcSFrancesco Fondelli } else { 160634954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ 160734954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 160834954ddcSFrancesco Fondelli 160934954ddcSFrancesco Fondelli if (debug) 161025a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n"); 161134954ddcSFrancesco Fondelli } 161234954ddcSFrancesco Fondelli return count; 161334954ddcSFrancesco Fondelli } 161434954ddcSFrancesco Fondelli 161534954ddcSFrancesco Fondelli if (!strcmp(name, "vlan_p")) { 161634954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 161734954ddcSFrancesco Fondelli if (len < 0) { 161834954ddcSFrancesco Fondelli return len; 161934954ddcSFrancesco Fondelli } 162034954ddcSFrancesco Fondelli i += len; 162134954ddcSFrancesco Fondelli if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) { 162234954ddcSFrancesco Fondelli pkt_dev->vlan_p = value; 162334954ddcSFrancesco Fondelli sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p); 162434954ddcSFrancesco Fondelli } else { 162534954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: vlan_p must be 0-7"); 162634954ddcSFrancesco Fondelli } 162734954ddcSFrancesco Fondelli return count; 162834954ddcSFrancesco Fondelli } 162934954ddcSFrancesco Fondelli 163034954ddcSFrancesco Fondelli if (!strcmp(name, "vlan_cfi")) { 163134954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 163234954ddcSFrancesco Fondelli if (len < 0) { 163334954ddcSFrancesco Fondelli return len; 163434954ddcSFrancesco Fondelli } 163534954ddcSFrancesco Fondelli i += len; 163634954ddcSFrancesco Fondelli if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) { 163734954ddcSFrancesco Fondelli pkt_dev->vlan_cfi = value; 163834954ddcSFrancesco Fondelli sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi); 163934954ddcSFrancesco Fondelli } else { 164034954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: vlan_cfi must be 0-1"); 164134954ddcSFrancesco Fondelli } 164234954ddcSFrancesco Fondelli return count; 164334954ddcSFrancesco Fondelli } 164434954ddcSFrancesco Fondelli 164534954ddcSFrancesco Fondelli if (!strcmp(name, "svlan_id")) { 164634954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 4, &value); 164734954ddcSFrancesco Fondelli if (len < 0) { 164834954ddcSFrancesco Fondelli return len; 164934954ddcSFrancesco Fondelli } 165034954ddcSFrancesco Fondelli i += len; 165134954ddcSFrancesco Fondelli if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) { 165234954ddcSFrancesco Fondelli pkt_dev->svlan_id = value; /* turn on SVLAN */ 165334954ddcSFrancesco Fondelli 165434954ddcSFrancesco Fondelli if (debug) 165525a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: SVLAN turned on\n"); 165634954ddcSFrancesco Fondelli 165734954ddcSFrancesco Fondelli if (debug && pkt_dev->nr_labels) 165825a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: MPLS auto turned off\n"); 165934954ddcSFrancesco Fondelli 166034954ddcSFrancesco Fondelli pkt_dev->nr_labels = 0; /* turn off MPLS */ 166134954ddcSFrancesco Fondelli sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id); 166234954ddcSFrancesco Fondelli } else { 166334954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ 166434954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 166534954ddcSFrancesco Fondelli 166634954ddcSFrancesco Fondelli if (debug) 166725a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n"); 166834954ddcSFrancesco Fondelli } 166934954ddcSFrancesco Fondelli return count; 167034954ddcSFrancesco Fondelli } 167134954ddcSFrancesco Fondelli 167234954ddcSFrancesco Fondelli if (!strcmp(name, "svlan_p")) { 167334954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 167434954ddcSFrancesco Fondelli if (len < 0) { 167534954ddcSFrancesco Fondelli return len; 167634954ddcSFrancesco Fondelli } 167734954ddcSFrancesco Fondelli i += len; 167834954ddcSFrancesco Fondelli if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) { 167934954ddcSFrancesco Fondelli pkt_dev->svlan_p = value; 168034954ddcSFrancesco Fondelli sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p); 168134954ddcSFrancesco Fondelli } else { 168234954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: svlan_p must be 0-7"); 168334954ddcSFrancesco Fondelli } 168434954ddcSFrancesco Fondelli return count; 168534954ddcSFrancesco Fondelli } 168634954ddcSFrancesco Fondelli 168734954ddcSFrancesco Fondelli if (!strcmp(name, "svlan_cfi")) { 168834954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 168934954ddcSFrancesco Fondelli if (len < 0) { 169034954ddcSFrancesco Fondelli return len; 169134954ddcSFrancesco Fondelli } 169234954ddcSFrancesco Fondelli i += len; 169334954ddcSFrancesco Fondelli if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) { 169434954ddcSFrancesco Fondelli pkt_dev->svlan_cfi = value; 169534954ddcSFrancesco Fondelli sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi); 169634954ddcSFrancesco Fondelli } else { 169734954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: svlan_cfi must be 0-1"); 169834954ddcSFrancesco Fondelli } 1699ca6549afSSteven Whitehouse return count; 1700ca6549afSSteven Whitehouse } 1701ca6549afSSteven Whitehouse 17021ca7768cSFrancesco Fondelli if (!strcmp(name, "tos")) { 17031ca7768cSFrancesco Fondelli __u32 tmp_value = 0; 17041ca7768cSFrancesco Fondelli len = hex32_arg(&user_buffer[i], 2, &tmp_value); 17051ca7768cSFrancesco Fondelli if (len < 0) { 17061ca7768cSFrancesco Fondelli return len; 17071ca7768cSFrancesco Fondelli } 17081ca7768cSFrancesco Fondelli i += len; 17091ca7768cSFrancesco Fondelli if (len == 2) { 17101ca7768cSFrancesco Fondelli pkt_dev->tos = tmp_value; 17111ca7768cSFrancesco Fondelli sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos); 17121ca7768cSFrancesco Fondelli } else { 17131ca7768cSFrancesco Fondelli sprintf(pg_result, "ERROR: tos must be 00-ff"); 17141ca7768cSFrancesco Fondelli } 17151ca7768cSFrancesco Fondelli return count; 17161ca7768cSFrancesco Fondelli } 17171ca7768cSFrancesco Fondelli 17181ca7768cSFrancesco Fondelli if (!strcmp(name, "traffic_class")) { 17191ca7768cSFrancesco Fondelli __u32 tmp_value = 0; 17201ca7768cSFrancesco Fondelli len = hex32_arg(&user_buffer[i], 2, &tmp_value); 17211ca7768cSFrancesco Fondelli if (len < 0) { 17221ca7768cSFrancesco Fondelli return len; 17231ca7768cSFrancesco Fondelli } 17241ca7768cSFrancesco Fondelli i += len; 17251ca7768cSFrancesco Fondelli if (len == 2) { 17261ca7768cSFrancesco Fondelli pkt_dev->traffic_class = tmp_value; 17271ca7768cSFrancesco Fondelli sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class); 17281ca7768cSFrancesco Fondelli } else { 17291ca7768cSFrancesco Fondelli sprintf(pg_result, "ERROR: traffic_class must be 00-ff"); 17301ca7768cSFrancesco Fondelli } 17311ca7768cSFrancesco Fondelli return count; 17321ca7768cSFrancesco Fondelli } 17331ca7768cSFrancesco Fondelli 17341da177e4SLinus Torvalds sprintf(pkt_dev->result, "No such parameter \"%s\"", name); 17351da177e4SLinus Torvalds return -EINVAL; 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds 1738d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file) 17391da177e4SLinus Torvalds { 1740d50a6b56SStephen Hemminger return single_open(file, pktgen_if_show, PDE(inode)->data); 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds 17439a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = { 1744d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1745d50a6b56SStephen Hemminger .open = pktgen_if_open, 1746d50a6b56SStephen Hemminger .read = seq_read, 1747d50a6b56SStephen Hemminger .llseek = seq_lseek, 1748d50a6b56SStephen Hemminger .write = pktgen_if_write, 1749d50a6b56SStephen Hemminger .release = single_release, 1750d50a6b56SStephen Hemminger }; 1751d50a6b56SStephen Hemminger 1752d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v) 1753d50a6b56SStephen Hemminger { 1754d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 1755c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 1756d50a6b56SStephen Hemminger 1757d50a6b56SStephen Hemminger BUG_ON(!t); 1758d50a6b56SStephen Hemminger 1759d50a6b56SStephen Hemminger seq_printf(seq, "Running: "); 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds if_lock(t); 1762c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 17631da177e4SLinus Torvalds if (pkt_dev->running) 176439df232fSStephen Hemminger seq_printf(seq, "%s ", pkt_dev->odev->name); 17651da177e4SLinus Torvalds 1766d50a6b56SStephen Hemminger seq_printf(seq, "\nStopped: "); 17671da177e4SLinus Torvalds 1768c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 17691da177e4SLinus Torvalds if (!pkt_dev->running) 177039df232fSStephen Hemminger seq_printf(seq, "%s ", pkt_dev->odev->name); 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds if (t->result[0]) 1773d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: %s\n", t->result); 17741da177e4SLinus Torvalds else 1775d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: NA\n"); 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds if_unlock(t); 17781da177e4SLinus Torvalds 1779d50a6b56SStephen Hemminger return 0; 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 1782d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file, 1783d50a6b56SStephen Hemminger const char __user * user_buffer, 1784d50a6b56SStephen Hemminger size_t count, loff_t * offset) 17851da177e4SLinus Torvalds { 1786d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *)file->private_data; 1787d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 17881da177e4SLinus Torvalds int i = 0, max, len, ret; 17891da177e4SLinus Torvalds char name[40]; 17901da177e4SLinus Torvalds char *pg_result; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds if (count < 1) { 17931da177e4SLinus Torvalds // sprintf(pg_result, "Wrong command format"); 17941da177e4SLinus Torvalds return -EINVAL; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds max = count - i; 17981da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 17991da177e4SLinus Torvalds if (len < 0) 18001da177e4SLinus Torvalds return len; 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds i += len; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds /* Read variable name */ 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 18071da177e4SLinus Torvalds if (len < 0) 18081da177e4SLinus Torvalds return len; 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 18111da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len)) 18121da177e4SLinus Torvalds return -EFAULT; 18131da177e4SLinus Torvalds i += len; 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds max = count - i; 18161da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 18171da177e4SLinus Torvalds if (len < 0) 18181da177e4SLinus Torvalds return len; 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds i += len; 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds if (debug) 182325a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n", 182425a8b254SDavid S. Miller name, (unsigned long)count); 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds if (!t) { 182725a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: No thread\n"); 18281da177e4SLinus Torvalds ret = -EINVAL; 18291da177e4SLinus Torvalds goto out; 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds pg_result = &(t->result[0]); 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds if (!strcmp(name, "add_device")) { 18351da177e4SLinus Torvalds char f[32]; 18361da177e4SLinus Torvalds memset(f, 0, 32); 18371da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 18381da177e4SLinus Torvalds if (len < 0) { 18391da177e4SLinus Torvalds ret = len; 18401da177e4SLinus Torvalds goto out; 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds if (copy_from_user(f, &user_buffer[i], len)) 18431da177e4SLinus Torvalds return -EFAULT; 18441da177e4SLinus Torvalds i += len; 18456146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 18461da177e4SLinus Torvalds pktgen_add_device(t, f); 18476146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 18481da177e4SLinus Torvalds ret = count; 18491da177e4SLinus Torvalds sprintf(pg_result, "OK: add_device=%s", f); 18501da177e4SLinus Torvalds goto out; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds if (!strcmp(name, "rem_device_all")) { 18546146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 185595ed63f7SArthur Kepner t->control |= T_REMDEVALL; 18566146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 1857121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 18581da177e4SLinus Torvalds ret = count; 18591da177e4SLinus Torvalds sprintf(pg_result, "OK: rem_device_all"); 18601da177e4SLinus Torvalds goto out; 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds if (!strcmp(name, "max_before_softirq")) { 1864b163911fSRobert Olsson sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use"); 18651da177e4SLinus Torvalds ret = count; 18661da177e4SLinus Torvalds goto out; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds ret = -EINVAL; 18701da177e4SLinus Torvalds out: 18711da177e4SLinus Torvalds return ret; 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 1874d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file) 18751da177e4SLinus Torvalds { 1876d50a6b56SStephen Hemminger return single_open(file, pktgen_thread_show, PDE(inode)->data); 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 18799a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = { 1880d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1881d50a6b56SStephen Hemminger .open = pktgen_thread_open, 1882d50a6b56SStephen Hemminger .read = seq_read, 1883d50a6b56SStephen Hemminger .llseek = seq_lseek, 1884d50a6b56SStephen Hemminger .write = pktgen_thread_write, 1885d50a6b56SStephen Hemminger .release = single_release, 1886d50a6b56SStephen Hemminger }; 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds /* Think find or remove for NN */ 18891da177e4SLinus Torvalds static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove) 18901da177e4SLinus Torvalds { 18911da177e4SLinus Torvalds struct pktgen_thread *t; 18921da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 18931da177e4SLinus Torvalds 1894cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) { 18951da177e4SLinus Torvalds pkt_dev = pktgen_find_dev(t, ifname); 18961da177e4SLinus Torvalds if (pkt_dev) { 18971da177e4SLinus Torvalds if (remove) { 18981da177e4SLinus Torvalds if_lock(t); 189995ed63f7SArthur Kepner pkt_dev->removal_mark = 1; 190095ed63f7SArthur Kepner t->control |= T_REMDEV; 19011da177e4SLinus Torvalds if_unlock(t); 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds break; 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds return pkt_dev; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 190995ed63f7SArthur Kepner /* 191095ed63f7SArthur Kepner * mark a device for removal 191195ed63f7SArthur Kepner */ 191239df232fSStephen Hemminger static void pktgen_mark_device(const char *ifname) 19131da177e4SLinus Torvalds { 19141da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 191595ed63f7SArthur Kepner const int max_tries = 10, msec_per_try = 125; 191695ed63f7SArthur Kepner int i = 0; 191795ed63f7SArthur Kepner 19186146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 191925c4e53aSStephen Hemminger pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname); 192095ed63f7SArthur Kepner 192195ed63f7SArthur Kepner while (1) { 192295ed63f7SArthur Kepner 192395ed63f7SArthur Kepner pkt_dev = __pktgen_NN_threads(ifname, REMOVE); 1924222f1806SLuiz Capitulino if (pkt_dev == NULL) 1925222f1806SLuiz Capitulino break; /* success */ 192695ed63f7SArthur Kepner 19276146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 192825c4e53aSStephen Hemminger pr_debug("pktgen: pktgen_mark_device waiting for %s " 192925c4e53aSStephen Hemminger "to disappear....\n", ifname); 193095ed63f7SArthur Kepner schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try)); 19316146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 193295ed63f7SArthur Kepner 193395ed63f7SArthur Kepner if (++i >= max_tries) { 193425a8b254SDavid S. Miller printk(KERN_ERR "pktgen_mark_device: timed out after " 193525a8b254SDavid S. Miller "waiting %d msec for device %s to be removed\n", 193695ed63f7SArthur Kepner msec_per_try * i, ifname); 193795ed63f7SArthur Kepner break; 193895ed63f7SArthur Kepner } 193995ed63f7SArthur Kepner 194095ed63f7SArthur Kepner } 194195ed63f7SArthur Kepner 19426146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 194339df232fSStephen Hemminger } 194495ed63f7SArthur Kepner 194539df232fSStephen Hemminger static void pktgen_change_name(struct net_device *dev) 194639df232fSStephen Hemminger { 194739df232fSStephen Hemminger struct pktgen_thread *t; 194839df232fSStephen Hemminger 194939df232fSStephen Hemminger list_for_each_entry(t, &pktgen_threads, th_list) { 195039df232fSStephen Hemminger struct pktgen_dev *pkt_dev; 195139df232fSStephen Hemminger 195239df232fSStephen Hemminger list_for_each_entry(pkt_dev, &t->if_list, list) { 195339df232fSStephen Hemminger if (pkt_dev->odev != dev) 195439df232fSStephen Hemminger continue; 195539df232fSStephen Hemminger 195639df232fSStephen Hemminger remove_proc_entry(pkt_dev->entry->name, pg_proc_dir); 195739df232fSStephen Hemminger 195839df232fSStephen Hemminger pkt_dev->entry = create_proc_entry(dev->name, 0600, 195939df232fSStephen Hemminger pg_proc_dir); 196039df232fSStephen Hemminger if (!pkt_dev->entry) 196139df232fSStephen Hemminger printk(KERN_ERR "pktgen: can't move proc " 196239df232fSStephen Hemminger " entry for '%s'\n", dev->name); 196339df232fSStephen Hemminger break; 196439df232fSStephen Hemminger } 196539df232fSStephen Hemminger } 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds 1968222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused, 1969222f1806SLuiz Capitulino unsigned long event, void *ptr) 19701da177e4SLinus Torvalds { 197139df232fSStephen Hemminger struct net_device *dev = ptr; 19721da177e4SLinus Torvalds 1973e9dc8653SEric W. Biederman if (dev->nd_net != &init_net) 1974e9dc8653SEric W. Biederman return NOTIFY_DONE; 1975e9dc8653SEric W. Biederman 19761da177e4SLinus Torvalds /* It is OK that we do not hold the group lock right now, 19771da177e4SLinus Torvalds * as we run under the RTNL lock. 19781da177e4SLinus Torvalds */ 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds switch (event) { 198139df232fSStephen Hemminger case NETDEV_CHANGENAME: 198239df232fSStephen Hemminger pktgen_change_name(dev); 19831da177e4SLinus Torvalds break; 19841da177e4SLinus Torvalds 19851da177e4SLinus Torvalds case NETDEV_UNREGISTER: 198695ed63f7SArthur Kepner pktgen_mark_device(dev->name); 19871da177e4SLinus Torvalds break; 19883ff50b79SStephen Hemminger } 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds return NOTIFY_DONE; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */ 19941da177e4SLinus Torvalds 199539df232fSStephen Hemminger static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname) 1996222f1806SLuiz Capitulino { 19971da177e4SLinus Torvalds struct net_device *odev; 199839df232fSStephen Hemminger int err; 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds /* Clean old setups */ 20011da177e4SLinus Torvalds if (pkt_dev->odev) { 20021da177e4SLinus Torvalds dev_put(pkt_dev->odev); 20031da177e4SLinus Torvalds pkt_dev->odev = NULL; 20041da177e4SLinus Torvalds } 20051da177e4SLinus Torvalds 2006881d966bSEric W. Biederman odev = dev_get_by_name(&init_net, ifname); 20071da177e4SLinus Torvalds if (!odev) { 200825a8b254SDavid S. Miller printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname); 200939df232fSStephen Hemminger return -ENODEV; 20101da177e4SLinus Torvalds } 201139df232fSStephen Hemminger 20121da177e4SLinus Torvalds if (odev->type != ARPHRD_ETHER) { 201325a8b254SDavid S. Miller printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname); 201439df232fSStephen Hemminger err = -EINVAL; 201539df232fSStephen Hemminger } else if (!netif_running(odev)) { 201625a8b254SDavid S. Miller printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname); 201739df232fSStephen Hemminger err = -ENETDOWN; 201839df232fSStephen Hemminger } else { 20191da177e4SLinus Torvalds pkt_dev->odev = odev; 202039df232fSStephen Hemminger return 0; 202139df232fSStephen Hemminger } 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds dev_put(odev); 202439df232fSStephen Hemminger return err; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev 20281da177e4SLinus Torvalds * structure to have the right information to create/send packets 20291da177e4SLinus Torvalds */ 20301da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev) 20311da177e4SLinus Torvalds { 20321da177e4SLinus Torvalds if (!pkt_dev->odev) { 203325a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in " 203425a8b254SDavid S. Miller "setup_inject.\n"); 2035222f1806SLuiz Capitulino sprintf(pkt_dev->result, 2036222f1806SLuiz Capitulino "ERROR: pkt_dev->odev == NULL in setup_inject.\n"); 20371da177e4SLinus Torvalds return; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds /* Default to the interface's mac if not explicitly set. */ 20411da177e4SLinus Torvalds 2042f404e9a6SKris Katterjohn if (is_zero_ether_addr(pkt_dev->src_mac)) 2043f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN); 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds /* Set up Dest MAC */ 2046f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds /* Set up pkt size */ 20491da177e4SLinus Torvalds pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 20521da177e4SLinus Torvalds /* 20531da177e4SLinus Torvalds * Skip this automatic address setting until locks or functions 20541da177e4SLinus Torvalds * gets exported 20551da177e4SLinus Torvalds */ 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds #ifdef NOTNOW 20581da177e4SLinus Torvalds int i, set = 0, err = 1; 20591da177e4SLinus Torvalds struct inet6_dev *idev; 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) 20621da177e4SLinus Torvalds if (pkt_dev->cur_in6_saddr.s6_addr[i]) { 20631da177e4SLinus Torvalds set = 1; 20641da177e4SLinus Torvalds break; 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds if (!set) { 20681da177e4SLinus Torvalds 20691da177e4SLinus Torvalds /* 20701da177e4SLinus Torvalds * Use linklevel address if unconfigured. 20711da177e4SLinus Torvalds * 20721da177e4SLinus Torvalds * use ipv6_get_lladdr if/when it's get exported 20731da177e4SLinus Torvalds */ 20741da177e4SLinus Torvalds 20758814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 20761da177e4SLinus Torvalds if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) { 20771da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2080222f1806SLuiz Capitulino for (ifp = idev->addr_list; ifp; 2081222f1806SLuiz Capitulino ifp = ifp->if_next) { 2082222f1806SLuiz Capitulino if (ifp->scope == IFA_LINK 2083222f1806SLuiz Capitulino && !(ifp-> 2084222f1806SLuiz Capitulino flags & IFA_F_TENTATIVE)) { 2085222f1806SLuiz Capitulino ipv6_addr_copy(&pkt_dev-> 2086222f1806SLuiz Capitulino cur_in6_saddr, 2087222f1806SLuiz Capitulino &ifp->addr); 20881da177e4SLinus Torvalds err = 0; 20891da177e4SLinus Torvalds break; 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 20931da177e4SLinus Torvalds } 20948814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 2095222f1806SLuiz Capitulino if (err) 209625a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: IPv6 link " 209725a8b254SDavid S. Miller "address not availble.\n"); 20981da177e4SLinus Torvalds } 20991da177e4SLinus Torvalds #endif 2100222f1806SLuiz Capitulino } else { 21011da177e4SLinus Torvalds pkt_dev->saddr_min = 0; 21021da177e4SLinus Torvalds pkt_dev->saddr_max = 0; 21031da177e4SLinus Torvalds if (strlen(pkt_dev->src_min) == 0) { 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds struct in_device *in_dev; 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds rcu_read_lock(); 2108e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(pkt_dev->odev); 21091da177e4SLinus Torvalds if (in_dev) { 21101da177e4SLinus Torvalds if (in_dev->ifa_list) { 2111222f1806SLuiz Capitulino pkt_dev->saddr_min = 2112222f1806SLuiz Capitulino in_dev->ifa_list->ifa_address; 21131da177e4SLinus Torvalds pkt_dev->saddr_max = pkt_dev->saddr_min; 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds } 21161da177e4SLinus Torvalds rcu_read_unlock(); 2117222f1806SLuiz Capitulino } else { 21181da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 21191da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 21231da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds /* Initialize current values. */ 21261da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 21271da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 21281da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 21291da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 21301da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 21311da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 21321da177e4SLinus Torvalds pkt_dev->nflows = 0; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us) 21361da177e4SLinus Torvalds { 21371da177e4SLinus Torvalds __u64 start; 21381da177e4SLinus Torvalds __u64 now; 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds start = now = getCurUs(); 21411da177e4SLinus Torvalds printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now)); 21421da177e4SLinus Torvalds while (now < spin_until_us) { 2143b4099fabSStephen Hemminger /* TODO: optimize sleeping behavior */ 2144121caf57SNishanth Aravamudan if (spin_until_us - now > jiffies_to_usecs(1) + 1) 2145121caf57SNishanth Aravamudan schedule_timeout_interruptible(1); 2146121caf57SNishanth Aravamudan else if (spin_until_us - now > 100) { 21471da177e4SLinus Torvalds if (!pkt_dev->running) 21481da177e4SLinus Torvalds return; 21491da177e4SLinus Torvalds if (need_resched()) 21501da177e4SLinus Torvalds schedule(); 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds now = getCurUs(); 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds pkt_dev->idle_acc += now - start; 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds 215916dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev) 216016dab72fSJamal Hadi Salim { 2161a553e4a6SJamal Hadi Salim pkt_dev->pkt_overhead = 0; 216216dab72fSJamal Hadi Salim pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32); 216316dab72fSJamal Hadi Salim pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev); 216416dab72fSJamal Hadi Salim pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev); 216516dab72fSJamal Hadi Salim } 216616dab72fSJamal Hadi Salim 2167007a531bSJamal Hadi Salim static inline int f_seen(struct pktgen_dev *pkt_dev, int flow) 2168007a531bSJamal Hadi Salim { 2169007a531bSJamal Hadi Salim 2170007a531bSJamal Hadi Salim if (pkt_dev->flows[flow].flags & F_INIT) 2171007a531bSJamal Hadi Salim return 1; 2172007a531bSJamal Hadi Salim else 2173007a531bSJamal Hadi Salim return 0; 2174007a531bSJamal Hadi Salim } 2175007a531bSJamal Hadi Salim 2176007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev) 2177007a531bSJamal Hadi Salim { 2178007a531bSJamal Hadi Salim int flow = pkt_dev->curfl; 2179007a531bSJamal Hadi Salim 2180007a531bSJamal Hadi Salim if (pkt_dev->flags & F_FLOW_SEQ) { 2181007a531bSJamal Hadi Salim if (pkt_dev->flows[flow].count >= pkt_dev->lflow) { 2182007a531bSJamal Hadi Salim /* reset time */ 2183007a531bSJamal Hadi Salim pkt_dev->flows[flow].count = 0; 2184007a531bSJamal Hadi Salim pkt_dev->curfl += 1; 2185007a531bSJamal Hadi Salim if (pkt_dev->curfl >= pkt_dev->cflows) 2186007a531bSJamal Hadi Salim pkt_dev->curfl = 0; /*reset */ 2187007a531bSJamal Hadi Salim } 2188007a531bSJamal Hadi Salim } else { 2189007a531bSJamal Hadi Salim flow = random32() % pkt_dev->cflows; 2190007a531bSJamal Hadi Salim 2191007a531bSJamal Hadi Salim if (pkt_dev->flows[flow].count > pkt_dev->lflow) 2192007a531bSJamal Hadi Salim pkt_dev->flows[flow].count = 0; 2193007a531bSJamal Hadi Salim } 2194007a531bSJamal Hadi Salim 2195007a531bSJamal Hadi Salim return pkt_dev->curfl; 2196007a531bSJamal Hadi Salim } 2197007a531bSJamal Hadi Salim 2198a553e4a6SJamal Hadi Salim 2199a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 2200a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else 2201a553e4a6SJamal Hadi Salim * we go look for it ... 2202a553e4a6SJamal Hadi Salim */ 2203fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow) 2204a553e4a6SJamal Hadi Salim { 2205a553e4a6SJamal Hadi Salim struct xfrm_state *x = pkt_dev->flows[flow].x; 2206a553e4a6SJamal Hadi Salim if (!x) { 2207a553e4a6SJamal Hadi Salim /*slow path: we dont already have xfrm_state*/ 2208a553e4a6SJamal Hadi Salim x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr, 2209a553e4a6SJamal Hadi Salim (xfrm_address_t *)&pkt_dev->cur_saddr, 2210a553e4a6SJamal Hadi Salim AF_INET, 2211a553e4a6SJamal Hadi Salim pkt_dev->ipsmode, 2212a553e4a6SJamal Hadi Salim pkt_dev->ipsproto, 0); 2213a553e4a6SJamal Hadi Salim if (x) { 2214a553e4a6SJamal Hadi Salim pkt_dev->flows[flow].x = x; 2215a553e4a6SJamal Hadi Salim set_pkt_overhead(pkt_dev); 2216a553e4a6SJamal Hadi Salim pkt_dev->pkt_overhead+=x->props.header_len; 2217a553e4a6SJamal Hadi Salim } 2218a553e4a6SJamal Hadi Salim 2219a553e4a6SJamal Hadi Salim } 2220a553e4a6SJamal Hadi Salim } 2221a553e4a6SJamal Hadi Salim #endif 22221da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values 22231da177e4SLinus Torvalds * for IP src/dest, UDP src/dst port, MAC-Addr src/dst 22241da177e4SLinus Torvalds */ 2225222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev) 2226222f1806SLuiz Capitulino { 22271da177e4SLinus Torvalds __u32 imn; 22281da177e4SLinus Torvalds __u32 imx; 22291da177e4SLinus Torvalds int flow = 0; 22301da177e4SLinus Torvalds 2231007a531bSJamal Hadi Salim if (pkt_dev->cflows) 2232007a531bSJamal Hadi Salim flow = f_pick(pkt_dev); 22331da177e4SLinus Torvalds 22341da177e4SLinus Torvalds /* Deal with source MAC */ 22351da177e4SLinus Torvalds if (pkt_dev->src_mac_count > 1) { 22361da177e4SLinus Torvalds __u32 mc; 22371da177e4SLinus Torvalds __u32 tmp; 22381da177e4SLinus Torvalds 22391da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 22405fa6fc76SStephen Hemminger mc = random32() % pkt_dev->src_mac_count; 22411da177e4SLinus Torvalds else { 22421da177e4SLinus Torvalds mc = pkt_dev->cur_src_mac_offset++; 2243222f1806SLuiz Capitulino if (pkt_dev->cur_src_mac_offset > 2244222f1806SLuiz Capitulino pkt_dev->src_mac_count) 22451da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds tmp = pkt_dev->src_mac[5] + (mc & 0xFF); 22491da177e4SLinus Torvalds pkt_dev->hh[11] = tmp; 22501da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 22511da177e4SLinus Torvalds pkt_dev->hh[10] = tmp; 22521da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 22531da177e4SLinus Torvalds pkt_dev->hh[9] = tmp; 22541da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 22551da177e4SLinus Torvalds pkt_dev->hh[8] = tmp; 22561da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[1] + (tmp >> 8)); 22571da177e4SLinus Torvalds pkt_dev->hh[7] = tmp; 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds /* Deal with Destination MAC */ 22611da177e4SLinus Torvalds if (pkt_dev->dst_mac_count > 1) { 22621da177e4SLinus Torvalds __u32 mc; 22631da177e4SLinus Torvalds __u32 tmp; 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 22665fa6fc76SStephen Hemminger mc = random32() % pkt_dev->dst_mac_count; 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds else { 22691da177e4SLinus Torvalds mc = pkt_dev->cur_dst_mac_offset++; 2270222f1806SLuiz Capitulino if (pkt_dev->cur_dst_mac_offset > 2271222f1806SLuiz Capitulino pkt_dev->dst_mac_count) { 22721da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds tmp = pkt_dev->dst_mac[5] + (mc & 0xFF); 22771da177e4SLinus Torvalds pkt_dev->hh[5] = tmp; 22781da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 22791da177e4SLinus Torvalds pkt_dev->hh[4] = tmp; 22801da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 22811da177e4SLinus Torvalds pkt_dev->hh[3] = tmp; 22821da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 22831da177e4SLinus Torvalds pkt_dev->hh[2] = tmp; 22841da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[1] + (tmp >> 8)); 22851da177e4SLinus Torvalds pkt_dev->hh[1] = tmp; 22861da177e4SLinus Torvalds } 22871da177e4SLinus Torvalds 2288ca6549afSSteven Whitehouse if (pkt_dev->flags & F_MPLS_RND) { 2289ca6549afSSteven Whitehouse unsigned i; 2290ca6549afSSteven Whitehouse for (i = 0; i < pkt_dev->nr_labels; i++) 2291ca6549afSSteven Whitehouse if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM) 2292ca6549afSSteven Whitehouse pkt_dev->labels[i] = MPLS_STACK_BOTTOM | 22935fa6fc76SStephen Hemminger ((__force __be32)random32() & 2294ca6549afSSteven Whitehouse htonl(0x000fffff)); 2295ca6549afSSteven Whitehouse } 2296ca6549afSSteven Whitehouse 229734954ddcSFrancesco Fondelli if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) { 22985fa6fc76SStephen Hemminger pkt_dev->vlan_id = random32() & (4096-1); 229934954ddcSFrancesco Fondelli } 230034954ddcSFrancesco Fondelli 230134954ddcSFrancesco Fondelli if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) { 23025fa6fc76SStephen Hemminger pkt_dev->svlan_id = random32() & (4096 - 1); 230334954ddcSFrancesco Fondelli } 230434954ddcSFrancesco Fondelli 23051da177e4SLinus Torvalds if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) { 23061da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 23075fa6fc76SStephen Hemminger pkt_dev->cur_udp_src = random32() % 23085fa6fc76SStephen Hemminger (pkt_dev->udp_src_max - pkt_dev->udp_src_min) 23095fa6fc76SStephen Hemminger + pkt_dev->udp_src_min; 23101da177e4SLinus Torvalds 23111da177e4SLinus Torvalds else { 23121da177e4SLinus Torvalds pkt_dev->cur_udp_src++; 23131da177e4SLinus Torvalds if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max) 23141da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds 23181da177e4SLinus Torvalds if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) { 23191da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) { 23205fa6fc76SStephen Hemminger pkt_dev->cur_udp_dst = random32() % 23215fa6fc76SStephen Hemminger (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min) 23225fa6fc76SStephen Hemminger + pkt_dev->udp_dst_min; 2323222f1806SLuiz Capitulino } else { 23241da177e4SLinus Torvalds pkt_dev->cur_udp_dst++; 23251da177e4SLinus Torvalds if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max) 23261da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 23271da177e4SLinus Torvalds } 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds if (!(pkt_dev->flags & F_IPV6)) { 23311da177e4SLinus Torvalds 2332222f1806SLuiz Capitulino if ((imn = ntohl(pkt_dev->saddr_min)) < (imx = 2333222f1806SLuiz Capitulino ntohl(pkt_dev-> 2334222f1806SLuiz Capitulino saddr_max))) { 23351da177e4SLinus Torvalds __u32 t; 23361da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 23375fa6fc76SStephen Hemminger t = random32() % (imx - imn) + imn; 23381da177e4SLinus Torvalds else { 23391da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_saddr); 23401da177e4SLinus Torvalds t++; 23411da177e4SLinus Torvalds if (t > imx) { 23421da177e4SLinus Torvalds t = imn; 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds } 23451da177e4SLinus Torvalds pkt_dev->cur_saddr = htonl(t); 23461da177e4SLinus Torvalds } 23471da177e4SLinus Torvalds 2348007a531bSJamal Hadi Salim if (pkt_dev->cflows && f_seen(pkt_dev, flow)) { 23491da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr; 23501da177e4SLinus Torvalds } else { 2351252e3346SAl Viro imn = ntohl(pkt_dev->daddr_min); 2352252e3346SAl Viro imx = ntohl(pkt_dev->daddr_max); 2353252e3346SAl Viro if (imn < imx) { 23541da177e4SLinus Torvalds __u32 t; 2355252e3346SAl Viro __be32 s; 23561da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) { 23571da177e4SLinus Torvalds 23585fa6fc76SStephen Hemminger t = random32() % (imx - imn) + imn; 2359252e3346SAl Viro s = htonl(t); 23601da177e4SLinus Torvalds 2361252e3346SAl Viro while (LOOPBACK(s) || MULTICAST(s) 2362252e3346SAl Viro || BADCLASS(s) || ZERONET(s) 2363252e3346SAl Viro || LOCAL_MCAST(s)) { 23645fa6fc76SStephen Hemminger t = random32() % (imx - imn) + imn; 2365252e3346SAl Viro s = htonl(t); 23661da177e4SLinus Torvalds } 2367252e3346SAl Viro pkt_dev->cur_daddr = s; 2368252e3346SAl Viro } else { 23691da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_daddr); 23701da177e4SLinus Torvalds t++; 23711da177e4SLinus Torvalds if (t > imx) { 23721da177e4SLinus Torvalds t = imn; 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds pkt_dev->cur_daddr = htonl(t); 23751da177e4SLinus Torvalds } 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds if (pkt_dev->cflows) { 2378007a531bSJamal Hadi Salim pkt_dev->flows[flow].flags |= F_INIT; 2379222f1806SLuiz Capitulino pkt_dev->flows[flow].cur_daddr = 2380222f1806SLuiz Capitulino pkt_dev->cur_daddr; 2381a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 2382a553e4a6SJamal Hadi Salim if (pkt_dev->flags & F_IPSEC_ON) 2383a553e4a6SJamal Hadi Salim get_ipsec_sa(pkt_dev, flow); 2384a553e4a6SJamal Hadi Salim #endif 23851da177e4SLinus Torvalds pkt_dev->nflows++; 23861da177e4SLinus Torvalds } 23871da177e4SLinus Torvalds } 2388222f1806SLuiz Capitulino } else { /* IPV6 * */ 2389222f1806SLuiz Capitulino 23901da177e4SLinus Torvalds if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 && 23911da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[1] == 0 && 23921da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[2] == 0 && 23931da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ; 23941da177e4SLinus Torvalds else { 23951da177e4SLinus Torvalds int i; 23961da177e4SLinus Torvalds 23971da177e4SLinus Torvalds /* Only random destinations yet */ 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds for (i = 0; i < 4; i++) { 24001da177e4SLinus Torvalds pkt_dev->cur_in6_daddr.s6_addr32[i] = 24015fa6fc76SStephen Hemminger (((__force __be32)random32() | 24021da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[i]) & 24031da177e4SLinus Torvalds pkt_dev->max_in6_daddr.s6_addr32[i]); 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds } 24061da177e4SLinus Torvalds } 24071da177e4SLinus Torvalds 24081da177e4SLinus Torvalds if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) { 24091da177e4SLinus Torvalds __u32 t; 24101da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) { 24115fa6fc76SStephen Hemminger t = random32() % 24125fa6fc76SStephen Hemminger (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size) 24135fa6fc76SStephen Hemminger + pkt_dev->min_pkt_size; 2414222f1806SLuiz Capitulino } else { 24151da177e4SLinus Torvalds t = pkt_dev->cur_pkt_size + 1; 24161da177e4SLinus Torvalds if (t > pkt_dev->max_pkt_size) 24171da177e4SLinus Torvalds t = pkt_dev->min_pkt_size; 24181da177e4SLinus Torvalds } 24191da177e4SLinus Torvalds pkt_dev->cur_pkt_size = t; 24201da177e4SLinus Torvalds } 24211da177e4SLinus Torvalds 242245b270f8SRobert Olsson if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) { 242345b270f8SRobert Olsson __u16 t; 242445b270f8SRobert Olsson if (pkt_dev->flags & F_QUEUE_MAP_RND) { 242545b270f8SRobert Olsson t = random32() % 242645b270f8SRobert Olsson (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1) 242745b270f8SRobert Olsson + pkt_dev->queue_map_min; 242845b270f8SRobert Olsson } else { 242945b270f8SRobert Olsson t = pkt_dev->cur_queue_map + 1; 243045b270f8SRobert Olsson if (t > pkt_dev->queue_map_max) 243145b270f8SRobert Olsson t = pkt_dev->queue_map_min; 243245b270f8SRobert Olsson } 243345b270f8SRobert Olsson pkt_dev->cur_queue_map = t; 243445b270f8SRobert Olsson } 243545b270f8SRobert Olsson 24361da177e4SLinus Torvalds pkt_dev->flows[flow].count++; 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds 2439a553e4a6SJamal Hadi Salim 2440a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 2441a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev) 2442a553e4a6SJamal Hadi Salim { 2443a553e4a6SJamal Hadi Salim struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x; 2444a553e4a6SJamal Hadi Salim int err = 0; 2445a553e4a6SJamal Hadi Salim struct iphdr *iph; 2446a553e4a6SJamal Hadi Salim 2447a553e4a6SJamal Hadi Salim if (!x) 2448a553e4a6SJamal Hadi Salim return 0; 2449a553e4a6SJamal Hadi Salim /* XXX: we dont support tunnel mode for now until 2450a553e4a6SJamal Hadi Salim * we resolve the dst issue */ 2451a553e4a6SJamal Hadi Salim if (x->props.mode != XFRM_MODE_TRANSPORT) 2452a553e4a6SJamal Hadi Salim return 0; 2453a553e4a6SJamal Hadi Salim 2454a553e4a6SJamal Hadi Salim spin_lock(&x->lock); 2455a553e4a6SJamal Hadi Salim iph = ip_hdr(skb); 2456a553e4a6SJamal Hadi Salim 2457a553e4a6SJamal Hadi Salim err = x->mode->output(x, skb); 2458a553e4a6SJamal Hadi Salim if (err) 2459a553e4a6SJamal Hadi Salim goto error; 2460a553e4a6SJamal Hadi Salim err = x->type->output(x, skb); 2461a553e4a6SJamal Hadi Salim if (err) 2462a553e4a6SJamal Hadi Salim goto error; 2463a553e4a6SJamal Hadi Salim 2464a553e4a6SJamal Hadi Salim x->curlft.bytes +=skb->len; 2465a553e4a6SJamal Hadi Salim x->curlft.packets++; 2466a553e4a6SJamal Hadi Salim spin_unlock(&x->lock); 2467a553e4a6SJamal Hadi Salim 2468a553e4a6SJamal Hadi Salim error: 2469a553e4a6SJamal Hadi Salim spin_unlock(&x->lock); 2470a553e4a6SJamal Hadi Salim return err; 2471a553e4a6SJamal Hadi Salim } 2472a553e4a6SJamal Hadi Salim 2473a553e4a6SJamal Hadi Salim static inline void free_SAs(struct pktgen_dev *pkt_dev) 2474a553e4a6SJamal Hadi Salim { 2475a553e4a6SJamal Hadi Salim if (pkt_dev->cflows) { 2476a553e4a6SJamal Hadi Salim /* let go of the SAs if we have them */ 2477a553e4a6SJamal Hadi Salim int i = 0; 2478a553e4a6SJamal Hadi Salim for (; i < pkt_dev->nflows; i++){ 2479a553e4a6SJamal Hadi Salim struct xfrm_state *x = pkt_dev->flows[i].x; 2480a553e4a6SJamal Hadi Salim if (x) { 2481a553e4a6SJamal Hadi Salim xfrm_state_put(x); 2482a553e4a6SJamal Hadi Salim pkt_dev->flows[i].x = NULL; 2483a553e4a6SJamal Hadi Salim } 2484a553e4a6SJamal Hadi Salim } 2485a553e4a6SJamal Hadi Salim } 2486a553e4a6SJamal Hadi Salim } 2487a553e4a6SJamal Hadi Salim 2488a553e4a6SJamal Hadi Salim static inline int process_ipsec(struct pktgen_dev *pkt_dev, 2489a553e4a6SJamal Hadi Salim struct sk_buff *skb, __be16 protocol) 2490a553e4a6SJamal Hadi Salim { 2491a553e4a6SJamal Hadi Salim if (pkt_dev->flags & F_IPSEC_ON) { 2492a553e4a6SJamal Hadi Salim struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x; 2493a553e4a6SJamal Hadi Salim int nhead = 0; 2494a553e4a6SJamal Hadi Salim if (x) { 2495a553e4a6SJamal Hadi Salim int ret; 2496a553e4a6SJamal Hadi Salim __u8 *eth; 2497a553e4a6SJamal Hadi Salim nhead = x->props.header_len - skb_headroom(skb); 2498a553e4a6SJamal Hadi Salim if (nhead >0) { 2499a553e4a6SJamal Hadi Salim ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC); 2500a553e4a6SJamal Hadi Salim if (ret < 0) { 250125a8b254SDavid S. Miller printk(KERN_ERR "Error expanding " 250225a8b254SDavid S. Miller "ipsec packet %d\n",ret); 2503a553e4a6SJamal Hadi Salim return 0; 2504a553e4a6SJamal Hadi Salim } 2505a553e4a6SJamal Hadi Salim } 2506a553e4a6SJamal Hadi Salim 2507a553e4a6SJamal Hadi Salim /* ipsec is not expecting ll header */ 2508a553e4a6SJamal Hadi Salim skb_pull(skb, ETH_HLEN); 2509a553e4a6SJamal Hadi Salim ret = pktgen_output_ipsec(skb, pkt_dev); 2510a553e4a6SJamal Hadi Salim if (ret) { 251125a8b254SDavid S. Miller printk(KERN_ERR "Error creating ipsec " 251225a8b254SDavid S. Miller "packet %d\n",ret); 2513a553e4a6SJamal Hadi Salim kfree_skb(skb); 2514a553e4a6SJamal Hadi Salim return 0; 2515a553e4a6SJamal Hadi Salim } 2516a553e4a6SJamal Hadi Salim /* restore ll */ 2517a553e4a6SJamal Hadi Salim eth = (__u8 *) skb_push(skb, ETH_HLEN); 2518a553e4a6SJamal Hadi Salim memcpy(eth, pkt_dev->hh, 12); 2519a553e4a6SJamal Hadi Salim *(u16 *) & eth[12] = protocol; 2520a553e4a6SJamal Hadi Salim } 2521a553e4a6SJamal Hadi Salim } 2522a553e4a6SJamal Hadi Salim return 1; 2523a553e4a6SJamal Hadi Salim } 2524a553e4a6SJamal Hadi Salim #endif 2525a553e4a6SJamal Hadi Salim 2526ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev) 2527ca6549afSSteven Whitehouse { 2528ca6549afSSteven Whitehouse unsigned i; 2529ca6549afSSteven Whitehouse for (i = 0; i < pkt_dev->nr_labels; i++) { 2530ca6549afSSteven Whitehouse *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM; 2531ca6549afSSteven Whitehouse } 2532ca6549afSSteven Whitehouse mpls--; 2533ca6549afSSteven Whitehouse *mpls |= MPLS_STACK_BOTTOM; 2534ca6549afSSteven Whitehouse } 2535ca6549afSSteven Whitehouse 25360f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi, 25370f37c605SAl Viro unsigned int prio) 25380f37c605SAl Viro { 25390f37c605SAl Viro return htons(id | (cfi << 12) | (prio << 13)); 25400f37c605SAl Viro } 25410f37c605SAl Viro 25421da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev, 25431da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 25441da177e4SLinus Torvalds { 25451da177e4SLinus Torvalds struct sk_buff *skb = NULL; 25461da177e4SLinus Torvalds __u8 *eth; 25471da177e4SLinus Torvalds struct udphdr *udph; 25481da177e4SLinus Torvalds int datalen, iplen; 25491da177e4SLinus Torvalds struct iphdr *iph; 25501da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 2551d5f1ce9aSStephen Hemminger __be16 protocol = htons(ETH_P_IP); 2552ca6549afSSteven Whitehouse __be32 *mpls; 255334954ddcSFrancesco Fondelli __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */ 255434954ddcSFrancesco Fondelli __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */ 255534954ddcSFrancesco Fondelli __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */ 255634954ddcSFrancesco Fondelli __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */ 255734954ddcSFrancesco Fondelli 2558ca6549afSSteven Whitehouse 2559ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2560d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_MPLS_UC); 25611da177e4SLinus Torvalds 256234954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) 2563d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_8021Q); 256434954ddcSFrancesco Fondelli 256564053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 256664053beeSRobert Olsson * fields. 256764053beeSRobert Olsson */ 256864053beeSRobert Olsson mod_cur_headers(pkt_dev); 256964053beeSRobert Olsson 25707ac5459eSDavid S. Miller datalen = (odev->hard_header_len + 16) & ~0xf; 2571ca6549afSSteven Whitehouse skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen + 257216dab72fSJamal Hadi Salim pkt_dev->pkt_overhead, GFP_ATOMIC); 25731da177e4SLinus Torvalds if (!skb) { 25741da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 25751da177e4SLinus Torvalds return NULL; 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds 25787ac5459eSDavid S. Miller skb_reserve(skb, datalen); 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 25811da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 2582ca6549afSSteven Whitehouse mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32)); 2583ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2584ca6549afSSteven Whitehouse mpls_push(mpls, pkt_dev); 258534954ddcSFrancesco Fondelli 258634954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) { 258734954ddcSFrancesco Fondelli if (pkt_dev->svlan_id != 0xffff) { 258834954ddcSFrancesco Fondelli svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 25890f37c605SAl Viro *svlan_tci = build_tci(pkt_dev->svlan_id, 25900f37c605SAl Viro pkt_dev->svlan_cfi, 25910f37c605SAl Viro pkt_dev->svlan_p); 259234954ddcSFrancesco Fondelli svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2593d5f1ce9aSStephen Hemminger *svlan_encapsulated_proto = htons(ETH_P_8021Q); 259434954ddcSFrancesco Fondelli } 259534954ddcSFrancesco Fondelli vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 25960f37c605SAl Viro *vlan_tci = build_tci(pkt_dev->vlan_id, 25970f37c605SAl Viro pkt_dev->vlan_cfi, 25980f37c605SAl Viro pkt_dev->vlan_p); 259934954ddcSFrancesco Fondelli vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2600d5f1ce9aSStephen Hemminger *vlan_encapsulated_proto = htons(ETH_P_IP); 260134954ddcSFrancesco Fondelli } 260234954ddcSFrancesco Fondelli 260327a884dcSArnaldo Carvalho de Melo skb->network_header = skb->tail; 2604b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr); 2605ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr)); 260645b270f8SRobert Olsson skb->queue_mapping = pkt_dev->cur_queue_map; 2607ddc7b8e3SArnaldo Carvalho de Melo 2608ddc7b8e3SArnaldo Carvalho de Melo iph = ip_hdr(skb); 2609ddc7b8e3SArnaldo Carvalho de Melo udph = udp_hdr(skb); 26101da177e4SLinus Torvalds 26111da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 2612252e3346SAl Viro *(__be16 *) & eth[12] = protocol; 26131da177e4SLinus Torvalds 2614ca6549afSSteven Whitehouse /* Eth + IPh + UDPh + mpls */ 2615ca6549afSSteven Whitehouse datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 - 261616dab72fSJamal Hadi Salim pkt_dev->pkt_overhead; 26171da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) 26181da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 26211da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 26221da177e4SLinus Torvalds udph->len = htons(datalen + 8); /* DATA + udphdr */ 26231da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds iph->ihl = 5; 26261da177e4SLinus Torvalds iph->version = 4; 26271da177e4SLinus Torvalds iph->ttl = 32; 26281ca7768cSFrancesco Fondelli iph->tos = pkt_dev->tos; 26291da177e4SLinus Torvalds iph->protocol = IPPROTO_UDP; /* UDP */ 26301da177e4SLinus Torvalds iph->saddr = pkt_dev->cur_saddr; 26311da177e4SLinus Torvalds iph->daddr = pkt_dev->cur_daddr; 26321da177e4SLinus Torvalds iph->frag_off = 0; 26331da177e4SLinus Torvalds iplen = 20 + 8 + datalen; 26341da177e4SLinus Torvalds iph->tot_len = htons(iplen); 26351da177e4SLinus Torvalds iph->check = 0; 26361da177e4SLinus Torvalds iph->check = ip_fast_csum((void *)iph, iph->ihl); 2637ca6549afSSteven Whitehouse skb->protocol = protocol; 2638b0e380b1SArnaldo Carvalho de Melo skb->mac_header = (skb->network_header - ETH_HLEN - 263916dab72fSJamal Hadi Salim pkt_dev->pkt_overhead); 26401da177e4SLinus Torvalds skb->dev = odev; 26411da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 26441da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 26451da177e4SLinus Torvalds else { 26461da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 26471da177e4SLinus Torvalds int i; 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 26521da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 26531da177e4SLinus Torvalds if (datalen > frags * PAGE_SIZE) { 26541da177e4SLinus Torvalds skb_put(skb, datalen - frags * PAGE_SIZE); 26551da177e4SLinus Torvalds datalen = frags * PAGE_SIZE; 26561da177e4SLinus Torvalds } 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds i = 0; 26591da177e4SLinus Torvalds while (datalen > 0) { 26601da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 26611da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 26621da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 26631da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 26641da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 26651da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 26661da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 26671da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 26681da177e4SLinus Torvalds i++; 26691da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds while (i < frags) { 26731da177e4SLinus Torvalds int rem; 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds if (i == 0) 26761da177e4SLinus Torvalds break; 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 26791da177e4SLinus Torvalds if (rem == 0) 26801da177e4SLinus Torvalds break; 26811da177e4SLinus Torvalds 26821da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 26831da177e4SLinus Torvalds 2684222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i] = 2685222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1]; 26861da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 2687222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page = 2688222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].page; 2689222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page_offset += 2690222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].size; 26911da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 26921da177e4SLinus Torvalds i++; 26931da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 26941da177e4SLinus Torvalds } 26951da177e4SLinus Torvalds } 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds if (pgh) { 27001da177e4SLinus Torvalds struct timeval timestamp; 27011da177e4SLinus Torvalds 27021da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 27031da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 27041da177e4SLinus Torvalds 27051da177e4SLinus Torvalds do_gettimeofday(×tamp); 27061da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 27071da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 27081da177e4SLinus Torvalds } 27091da177e4SLinus Torvalds 2710a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 2711a553e4a6SJamal Hadi Salim if (!process_ipsec(pkt_dev, skb, protocol)) 2712a553e4a6SJamal Hadi Salim return NULL; 2713a553e4a6SJamal Hadi Salim #endif 2714a553e4a6SJamal Hadi Salim 27151da177e4SLinus Torvalds return skb; 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds /* 27191da177e4SLinus Torvalds * scan_ip6, fmt_ip taken from dietlibc-0.21 27201da177e4SLinus Torvalds * Author Felix von Leitner <felix-dietlibc@fefe.de> 27211da177e4SLinus Torvalds * 27221da177e4SLinus Torvalds * Slightly modified for kernel. 27231da177e4SLinus Torvalds * Should be candidate for net/ipv4/utils.c 27241da177e4SLinus Torvalds * --ro 27251da177e4SLinus Torvalds */ 27261da177e4SLinus Torvalds 27271da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]) 27281da177e4SLinus Torvalds { 27291da177e4SLinus Torvalds unsigned int i; 27301da177e4SLinus Torvalds unsigned int len = 0; 27311da177e4SLinus Torvalds unsigned long u; 27321da177e4SLinus Torvalds char suffix[16]; 27331da177e4SLinus Torvalds unsigned int prefixlen = 0; 27341da177e4SLinus Torvalds unsigned int suffixlen = 0; 2735252e3346SAl Viro __be32 tmp; 2736*cfcabdccSStephen Hemminger char *pos; 27371da177e4SLinus Torvalds 2738222f1806SLuiz Capitulino for (i = 0; i < 16; i++) 2739222f1806SLuiz Capitulino ip[i] = 0; 27401da177e4SLinus Torvalds 27411da177e4SLinus Torvalds for (;;) { 27421da177e4SLinus Torvalds if (*s == ':') { 27431da177e4SLinus Torvalds len++; 27441da177e4SLinus Torvalds if (s[1] == ':') { /* Found "::", skip to part 2 */ 27451da177e4SLinus Torvalds s += 2; 27461da177e4SLinus Torvalds len++; 27471da177e4SLinus Torvalds break; 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds s++; 27501da177e4SLinus Torvalds } 27511da177e4SLinus Torvalds 2752*cfcabdccSStephen Hemminger u = simple_strtoul(s, &pos, 16); 2753*cfcabdccSStephen Hemminger i = pos - s; 2754222f1806SLuiz Capitulino if (!i) 2755222f1806SLuiz Capitulino return 0; 27561da177e4SLinus Torvalds if (prefixlen == 12 && s[i] == '.') { 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds /* the last 4 bytes may be written as IPv4 address */ 27591da177e4SLinus Torvalds 27601da177e4SLinus Torvalds tmp = in_aton(s); 27611da177e4SLinus Torvalds memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp)); 27621da177e4SLinus Torvalds return i + len; 27631da177e4SLinus Torvalds } 27641da177e4SLinus Torvalds ip[prefixlen++] = (u >> 8); 27651da177e4SLinus Torvalds ip[prefixlen++] = (u & 255); 2766222f1806SLuiz Capitulino s += i; 2767222f1806SLuiz Capitulino len += i; 27681da177e4SLinus Torvalds if (prefixlen == 16) 27691da177e4SLinus Torvalds return len; 27701da177e4SLinus Torvalds } 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds /* part 2, after "::" */ 27731da177e4SLinus Torvalds for (;;) { 27741da177e4SLinus Torvalds if (*s == ':') { 27751da177e4SLinus Torvalds if (suffixlen == 0) 27761da177e4SLinus Torvalds break; 27771da177e4SLinus Torvalds s++; 27781da177e4SLinus Torvalds len++; 27791da177e4SLinus Torvalds } else if (suffixlen != 0) 27801da177e4SLinus Torvalds break; 2781*cfcabdccSStephen Hemminger 2782*cfcabdccSStephen Hemminger u = simple_strtol(s, &pos, 16); 2783*cfcabdccSStephen Hemminger i = pos - s; 27841da177e4SLinus Torvalds if (!i) { 2785222f1806SLuiz Capitulino if (*s) 2786222f1806SLuiz Capitulino len--; 27871da177e4SLinus Torvalds break; 27881da177e4SLinus Torvalds } 27891da177e4SLinus Torvalds if (suffixlen + prefixlen <= 12 && s[i] == '.') { 27901da177e4SLinus Torvalds tmp = in_aton(s); 2791222f1806SLuiz Capitulino memcpy((struct in_addr *)(suffix + suffixlen), &tmp, 2792222f1806SLuiz Capitulino sizeof(tmp)); 27931da177e4SLinus Torvalds suffixlen += 4; 27941da177e4SLinus Torvalds len += strlen(s); 27951da177e4SLinus Torvalds break; 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds suffix[suffixlen++] = (u >> 8); 27981da177e4SLinus Torvalds suffix[suffixlen++] = (u & 255); 2799222f1806SLuiz Capitulino s += i; 2800222f1806SLuiz Capitulino len += i; 28011da177e4SLinus Torvalds if (prefixlen + suffixlen == 16) 28021da177e4SLinus Torvalds break; 28031da177e4SLinus Torvalds } 28041da177e4SLinus Torvalds for (i = 0; i < suffixlen; i++) 28051da177e4SLinus Torvalds ip[16 - suffixlen + i] = suffix[i]; 28061da177e4SLinus Torvalds return len; 28071da177e4SLinus Torvalds } 28081da177e4SLinus Torvalds 2809222f1806SLuiz Capitulino static char tohex(char hexdigit) 2810222f1806SLuiz Capitulino { 28111da177e4SLinus Torvalds return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0'; 28121da177e4SLinus Torvalds } 28131da177e4SLinus Torvalds 2814222f1806SLuiz Capitulino static int fmt_xlong(char *s, unsigned int i) 2815222f1806SLuiz Capitulino { 28161da177e4SLinus Torvalds char *bak = s; 2817222f1806SLuiz Capitulino *s = tohex((i >> 12) & 0xf); 2818222f1806SLuiz Capitulino if (s != bak || *s != '0') 2819222f1806SLuiz Capitulino ++s; 2820222f1806SLuiz Capitulino *s = tohex((i >> 8) & 0xf); 2821222f1806SLuiz Capitulino if (s != bak || *s != '0') 2822222f1806SLuiz Capitulino ++s; 2823222f1806SLuiz Capitulino *s = tohex((i >> 4) & 0xf); 2824222f1806SLuiz Capitulino if (s != bak || *s != '0') 2825222f1806SLuiz Capitulino ++s; 28261da177e4SLinus Torvalds *s = tohex(i & 0xf); 28271da177e4SLinus Torvalds return s - bak + 1; 28281da177e4SLinus Torvalds } 28291da177e4SLinus Torvalds 2830222f1806SLuiz Capitulino static unsigned int fmt_ip6(char *s, const char ip[16]) 2831222f1806SLuiz Capitulino { 28321da177e4SLinus Torvalds unsigned int len; 28331da177e4SLinus Torvalds unsigned int i; 28341da177e4SLinus Torvalds unsigned int temp; 28351da177e4SLinus Torvalds unsigned int compressing; 28361da177e4SLinus Torvalds int j; 28371da177e4SLinus Torvalds 2838222f1806SLuiz Capitulino len = 0; 2839222f1806SLuiz Capitulino compressing = 0; 28401da177e4SLinus Torvalds for (j = 0; j < 16; j += 2) { 28411da177e4SLinus Torvalds 28421da177e4SLinus Torvalds #ifdef V4MAPPEDPREFIX 28431da177e4SLinus Torvalds if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) { 28441da177e4SLinus Torvalds inet_ntoa_r(*(struct in_addr *)(ip + 12), s); 28451da177e4SLinus Torvalds temp = strlen(s); 28461da177e4SLinus Torvalds return len + temp; 28471da177e4SLinus Torvalds } 28481da177e4SLinus Torvalds #endif 28491da177e4SLinus Torvalds temp = ((unsigned long)(unsigned char)ip[j] << 8) + 28501da177e4SLinus Torvalds (unsigned long)(unsigned char)ip[j + 1]; 28511da177e4SLinus Torvalds if (temp == 0) { 28521da177e4SLinus Torvalds if (!compressing) { 28531da177e4SLinus Torvalds compressing = 1; 28541da177e4SLinus Torvalds if (j == 0) { 2855222f1806SLuiz Capitulino *s++ = ':'; 2856222f1806SLuiz Capitulino ++len; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds } 28591da177e4SLinus Torvalds } else { 28601da177e4SLinus Torvalds if (compressing) { 28611da177e4SLinus Torvalds compressing = 0; 2862222f1806SLuiz Capitulino *s++ = ':'; 2863222f1806SLuiz Capitulino ++len; 28641da177e4SLinus Torvalds } 2865222f1806SLuiz Capitulino i = fmt_xlong(s, temp); 2866222f1806SLuiz Capitulino len += i; 2867222f1806SLuiz Capitulino s += i; 28681da177e4SLinus Torvalds if (j < 14) { 28691da177e4SLinus Torvalds *s++ = ':'; 28701da177e4SLinus Torvalds ++len; 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds } 28741da177e4SLinus Torvalds if (compressing) { 2875222f1806SLuiz Capitulino *s++ = ':'; 2876222f1806SLuiz Capitulino ++len; 28771da177e4SLinus Torvalds } 28781da177e4SLinus Torvalds *s = 0; 28791da177e4SLinus Torvalds return len; 28801da177e4SLinus Torvalds } 28811da177e4SLinus Torvalds 28821da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev, 28831da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 28841da177e4SLinus Torvalds { 28851da177e4SLinus Torvalds struct sk_buff *skb = NULL; 28861da177e4SLinus Torvalds __u8 *eth; 28871da177e4SLinus Torvalds struct udphdr *udph; 28881da177e4SLinus Torvalds int datalen; 28891da177e4SLinus Torvalds struct ipv6hdr *iph; 28901da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 2891d5f1ce9aSStephen Hemminger __be16 protocol = htons(ETH_P_IPV6); 2892ca6549afSSteven Whitehouse __be32 *mpls; 289334954ddcSFrancesco Fondelli __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */ 289434954ddcSFrancesco Fondelli __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */ 289534954ddcSFrancesco Fondelli __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */ 289634954ddcSFrancesco Fondelli __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */ 2897ca6549afSSteven Whitehouse 2898ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2899d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_MPLS_UC); 29001da177e4SLinus Torvalds 290134954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) 2902d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_8021Q); 290334954ddcSFrancesco Fondelli 290464053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 290564053beeSRobert Olsson * fields. 290664053beeSRobert Olsson */ 290764053beeSRobert Olsson mod_cur_headers(pkt_dev); 290864053beeSRobert Olsson 2909ca6549afSSteven Whitehouse skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 + 291016dab72fSJamal Hadi Salim pkt_dev->pkt_overhead, GFP_ATOMIC); 29111da177e4SLinus Torvalds if (!skb) { 29121da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 29131da177e4SLinus Torvalds return NULL; 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds skb_reserve(skb, 16); 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 29191da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 2920ca6549afSSteven Whitehouse mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32)); 2921ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2922ca6549afSSteven Whitehouse mpls_push(mpls, pkt_dev); 292334954ddcSFrancesco Fondelli 292434954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) { 292534954ddcSFrancesco Fondelli if (pkt_dev->svlan_id != 0xffff) { 292634954ddcSFrancesco Fondelli svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 29270f37c605SAl Viro *svlan_tci = build_tci(pkt_dev->svlan_id, 29280f37c605SAl Viro pkt_dev->svlan_cfi, 29290f37c605SAl Viro pkt_dev->svlan_p); 293034954ddcSFrancesco Fondelli svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2931d5f1ce9aSStephen Hemminger *svlan_encapsulated_proto = htons(ETH_P_8021Q); 293234954ddcSFrancesco Fondelli } 293334954ddcSFrancesco Fondelli vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 29340f37c605SAl Viro *vlan_tci = build_tci(pkt_dev->vlan_id, 29350f37c605SAl Viro pkt_dev->vlan_cfi, 29360f37c605SAl Viro pkt_dev->vlan_p); 293734954ddcSFrancesco Fondelli vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2938d5f1ce9aSStephen Hemminger *vlan_encapsulated_proto = htons(ETH_P_IPV6); 293934954ddcSFrancesco Fondelli } 294034954ddcSFrancesco Fondelli 294127a884dcSArnaldo Carvalho de Melo skb->network_header = skb->tail; 2942b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct ipv6hdr); 2943ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr)); 294445b270f8SRobert Olsson skb->queue_mapping = pkt_dev->cur_queue_map; 2945ddc7b8e3SArnaldo Carvalho de Melo 2946ddc7b8e3SArnaldo Carvalho de Melo iph = ipv6_hdr(skb); 2947ddc7b8e3SArnaldo Carvalho de Melo udph = udp_hdr(skb); 29481da177e4SLinus Torvalds 29491da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 2950252e3346SAl Viro *(__be16 *) & eth[12] = protocol; 29511da177e4SLinus Torvalds 2952ca6549afSSteven Whitehouse /* Eth + IPh + UDPh + mpls */ 2953ca6549afSSteven Whitehouse datalen = pkt_dev->cur_pkt_size - 14 - 2954ca6549afSSteven Whitehouse sizeof(struct ipv6hdr) - sizeof(struct udphdr) - 295516dab72fSJamal Hadi Salim pkt_dev->pkt_overhead; 29561da177e4SLinus Torvalds 29571da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) { 29581da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 29591da177e4SLinus Torvalds if (net_ratelimit()) 2960222f1806SLuiz Capitulino printk(KERN_INFO "pktgen: increased datalen to %d\n", 2961222f1806SLuiz Capitulino datalen); 29621da177e4SLinus Torvalds } 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 29651da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 29661da177e4SLinus Torvalds udph->len = htons(datalen + sizeof(struct udphdr)); 29671da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 29681da177e4SLinus Torvalds 2969d5f1ce9aSStephen Hemminger *(__be32 *) iph = htonl(0x60000000); /* Version + flow */ 29701da177e4SLinus Torvalds 29711ca7768cSFrancesco Fondelli if (pkt_dev->traffic_class) { 29721ca7768cSFrancesco Fondelli /* Version + traffic class + flow (0) */ 2973252e3346SAl Viro *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20)); 29741ca7768cSFrancesco Fondelli } 29751ca7768cSFrancesco Fondelli 29761da177e4SLinus Torvalds iph->hop_limit = 32; 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds iph->payload_len = htons(sizeof(struct udphdr) + datalen); 29791da177e4SLinus Torvalds iph->nexthdr = IPPROTO_UDP; 29801da177e4SLinus Torvalds 29811da177e4SLinus Torvalds ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr); 29821da177e4SLinus Torvalds ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr); 29831da177e4SLinus Torvalds 2984b0e380b1SArnaldo Carvalho de Melo skb->mac_header = (skb->network_header - ETH_HLEN - 298516dab72fSJamal Hadi Salim pkt_dev->pkt_overhead); 2986ca6549afSSteven Whitehouse skb->protocol = protocol; 29871da177e4SLinus Torvalds skb->dev = odev; 29881da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 29891da177e4SLinus Torvalds 29901da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 29911da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 29921da177e4SLinus Torvalds else { 29931da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 29941da177e4SLinus Torvalds int i; 29951da177e4SLinus Torvalds 29961da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); 29971da177e4SLinus Torvalds 29981da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 29991da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 30001da177e4SLinus Torvalds if (datalen > frags * PAGE_SIZE) { 30011da177e4SLinus Torvalds skb_put(skb, datalen - frags * PAGE_SIZE); 30021da177e4SLinus Torvalds datalen = frags * PAGE_SIZE; 30031da177e4SLinus Torvalds } 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds i = 0; 30061da177e4SLinus Torvalds while (datalen > 0) { 30071da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 30081da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 30091da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 30101da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 30111da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 30121da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 30131da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 30141da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 30151da177e4SLinus Torvalds i++; 30161da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds 30191da177e4SLinus Torvalds while (i < frags) { 30201da177e4SLinus Torvalds int rem; 30211da177e4SLinus Torvalds 30221da177e4SLinus Torvalds if (i == 0) 30231da177e4SLinus Torvalds break; 30241da177e4SLinus Torvalds 30251da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 30261da177e4SLinus Torvalds if (rem == 0) 30271da177e4SLinus Torvalds break; 30281da177e4SLinus Torvalds 30291da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 30301da177e4SLinus Torvalds 3031222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i] = 3032222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1]; 30331da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 3034222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page = 3035222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].page; 3036222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page_offset += 3037222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].size; 30381da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 30391da177e4SLinus Torvalds i++; 30401da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 30411da177e4SLinus Torvalds } 30421da177e4SLinus Torvalds } 30431da177e4SLinus Torvalds 30441da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 30451da177e4SLinus Torvalds /* should we update cloned packets too ? */ 30461da177e4SLinus Torvalds if (pgh) { 30471da177e4SLinus Torvalds struct timeval timestamp; 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 30501da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 30511da177e4SLinus Torvalds 30521da177e4SLinus Torvalds do_gettimeofday(×tamp); 30531da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 30541da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 30551da177e4SLinus Torvalds } 305634954ddcSFrancesco Fondelli /* pkt_dev->seq_num++; FF: you really mean this? */ 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds return skb; 30591da177e4SLinus Torvalds } 30601da177e4SLinus Torvalds 30611da177e4SLinus Torvalds static inline struct sk_buff *fill_packet(struct net_device *odev, 30621da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 30631da177e4SLinus Torvalds { 30641da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) 30651da177e4SLinus Torvalds return fill_packet_ipv6(odev, pkt_dev); 30661da177e4SLinus Torvalds else 30671da177e4SLinus Torvalds return fill_packet_ipv4(odev, pkt_dev); 30681da177e4SLinus Torvalds } 30691da177e4SLinus Torvalds 30701da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev) 30711da177e4SLinus Torvalds { 30721da177e4SLinus Torvalds pkt_dev->seq_num = 1; 30731da177e4SLinus Torvalds pkt_dev->idle_acc = 0; 30741da177e4SLinus Torvalds pkt_dev->sofar = 0; 30751da177e4SLinus Torvalds pkt_dev->tx_bytes = 0; 30761da177e4SLinus Torvalds pkt_dev->errors = 0; 30771da177e4SLinus Torvalds } 30781da177e4SLinus Torvalds 30791da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */ 30801da177e4SLinus Torvalds 30811da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t) 30821da177e4SLinus Torvalds { 3083c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 30841da177e4SLinus Torvalds int started = 0; 30851da177e4SLinus Torvalds 308625c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_run. %p\n", t); 30871da177e4SLinus Torvalds 30881da177e4SLinus Torvalds if_lock(t); 3089c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 30901da177e4SLinus Torvalds 30911da177e4SLinus Torvalds /* 30921da177e4SLinus Torvalds * setup odev and create initial packet. 30931da177e4SLinus Torvalds */ 30941da177e4SLinus Torvalds pktgen_setup_inject(pkt_dev); 30951da177e4SLinus Torvalds 30961da177e4SLinus Torvalds if (pkt_dev->odev) { 30971da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 30981da177e4SLinus Torvalds pkt_dev->running = 1; /* Cranke yeself! */ 30991da177e4SLinus Torvalds pkt_dev->skb = NULL; 31001da177e4SLinus Torvalds pkt_dev->started_at = getCurUs(); 31011da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */ 31021da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 310316dab72fSJamal Hadi Salim set_pkt_overhead(pkt_dev); 31041da177e4SLinus Torvalds 31051da177e4SLinus Torvalds strcpy(pkt_dev->result, "Starting"); 31061da177e4SLinus Torvalds started++; 3107222f1806SLuiz Capitulino } else 31081da177e4SLinus Torvalds strcpy(pkt_dev->result, "Error starting"); 31091da177e4SLinus Torvalds } 31101da177e4SLinus Torvalds if_unlock(t); 3111222f1806SLuiz Capitulino if (started) 3112222f1806SLuiz Capitulino t->control &= ~(T_STOP); 31131da177e4SLinus Torvalds } 31141da177e4SLinus Torvalds 31151da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void) 31161da177e4SLinus Torvalds { 3117cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 31181da177e4SLinus Torvalds 311925c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n"); 31201da177e4SLinus Torvalds 31216146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 3122cdcdbe0bSLuiz Capitulino 3123cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 312495ed63f7SArthur Kepner t->control |= T_STOP; 3125cdcdbe0bSLuiz Capitulino 31266146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 31271da177e4SLinus Torvalds } 31281da177e4SLinus Torvalds 31291da177e4SLinus Torvalds static int thread_is_running(struct pktgen_thread *t) 31301da177e4SLinus Torvalds { 3131c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 31321da177e4SLinus Torvalds int res = 0; 31331da177e4SLinus Torvalds 3134c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 3135c26a8016SLuiz Capitulino if (pkt_dev->running) { 31361da177e4SLinus Torvalds res = 1; 31371da177e4SLinus Torvalds break; 31381da177e4SLinus Torvalds } 31391da177e4SLinus Torvalds return res; 31401da177e4SLinus Torvalds } 31411da177e4SLinus Torvalds 31421da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t) 31431da177e4SLinus Torvalds { 31441da177e4SLinus Torvalds if_lock(t); 31451da177e4SLinus Torvalds 31461da177e4SLinus Torvalds while (thread_is_running(t)) { 31471da177e4SLinus Torvalds 31481da177e4SLinus Torvalds if_unlock(t); 31491da177e4SLinus Torvalds 31501da177e4SLinus Torvalds msleep_interruptible(100); 31511da177e4SLinus Torvalds 31521da177e4SLinus Torvalds if (signal_pending(current)) 31531da177e4SLinus Torvalds goto signal; 31541da177e4SLinus Torvalds if_lock(t); 31551da177e4SLinus Torvalds } 31561da177e4SLinus Torvalds if_unlock(t); 31571da177e4SLinus Torvalds return 1; 31581da177e4SLinus Torvalds signal: 31591da177e4SLinus Torvalds return 0; 31601da177e4SLinus Torvalds } 31611da177e4SLinus Torvalds 31621da177e4SLinus Torvalds static int pktgen_wait_all_threads_run(void) 31631da177e4SLinus Torvalds { 3164cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 31651da177e4SLinus Torvalds int sig = 1; 31661da177e4SLinus Torvalds 31676146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 3168cdcdbe0bSLuiz Capitulino 3169cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) { 31701da177e4SLinus Torvalds sig = pktgen_wait_thread_run(t); 3171222f1806SLuiz Capitulino if (sig == 0) 3172222f1806SLuiz Capitulino break; 31731da177e4SLinus Torvalds } 3174cdcdbe0bSLuiz Capitulino 3175cdcdbe0bSLuiz Capitulino if (sig == 0) 3176cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 31771da177e4SLinus Torvalds t->control |= (T_STOP); 3178cdcdbe0bSLuiz Capitulino 31796146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 31801da177e4SLinus Torvalds return sig; 31811da177e4SLinus Torvalds } 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds static void pktgen_run_all_threads(void) 31841da177e4SLinus Torvalds { 3185cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 31861da177e4SLinus Torvalds 318725c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_run_all_threads.\n"); 31881da177e4SLinus Torvalds 31896146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 31901da177e4SLinus Torvalds 3191cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 31921da177e4SLinus Torvalds t->control |= (T_RUN); 3193cdcdbe0bSLuiz Capitulino 31946146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 31951da177e4SLinus Torvalds 3196121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 31971da177e4SLinus Torvalds 31981da177e4SLinus Torvalds pktgen_wait_all_threads_run(); 31991da177e4SLinus Torvalds } 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags) 32021da177e4SLinus Torvalds { 32031da177e4SLinus Torvalds __u64 total_us, bps, mbps, pps, idle; 32041da177e4SLinus Torvalds char *p = pkt_dev->result; 32051da177e4SLinus Torvalds 32061da177e4SLinus Torvalds total_us = pkt_dev->stopped_at - pkt_dev->started_at; 32071da177e4SLinus Torvalds 32081da177e4SLinus Torvalds idle = pkt_dev->idle_acc; 32091da177e4SLinus Torvalds 32101da177e4SLinus Torvalds p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n", 32111da177e4SLinus Torvalds (unsigned long long)total_us, 32121da177e4SLinus Torvalds (unsigned long long)(total_us - idle), 32131da177e4SLinus Torvalds (unsigned long long)idle, 32141da177e4SLinus Torvalds (unsigned long long)pkt_dev->sofar, 32151da177e4SLinus Torvalds pkt_dev->cur_pkt_size, nr_frags); 32161da177e4SLinus Torvalds 32171da177e4SLinus Torvalds pps = pkt_dev->sofar * USEC_PER_SEC; 32181da177e4SLinus Torvalds 32191da177e4SLinus Torvalds while ((total_us >> 32) != 0) { 32201da177e4SLinus Torvalds pps >>= 1; 32211da177e4SLinus Torvalds total_us >>= 1; 32221da177e4SLinus Torvalds } 32231da177e4SLinus Torvalds 32241da177e4SLinus Torvalds do_div(pps, total_us); 32251da177e4SLinus Torvalds 32261da177e4SLinus Torvalds bps = pps * 8 * pkt_dev->cur_pkt_size; 32271da177e4SLinus Torvalds 32281da177e4SLinus Torvalds mbps = bps; 32291da177e4SLinus Torvalds do_div(mbps, 1000000); 32301da177e4SLinus Torvalds p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu", 32311da177e4SLinus Torvalds (unsigned long long)pps, 32321da177e4SLinus Torvalds (unsigned long long)mbps, 32331da177e4SLinus Torvalds (unsigned long long)bps, 32341da177e4SLinus Torvalds (unsigned long long)pkt_dev->errors); 32351da177e4SLinus Torvalds } 32361da177e4SLinus Torvalds 32371da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */ 32381da177e4SLinus Torvalds 32391da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev) 32401da177e4SLinus Torvalds { 3241222f1806SLuiz Capitulino int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1; 32421da177e4SLinus Torvalds 32431da177e4SLinus Torvalds if (!pkt_dev->running) { 324425a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: interface: %s is already " 324525a8b254SDavid S. Miller "stopped\n", pkt_dev->odev->name); 32461da177e4SLinus Torvalds return -EINVAL; 32471da177e4SLinus Torvalds } 32481da177e4SLinus Torvalds 32491da177e4SLinus Torvalds pkt_dev->stopped_at = getCurUs(); 32501da177e4SLinus Torvalds pkt_dev->running = 0; 32511da177e4SLinus Torvalds 325295ed63f7SArthur Kepner show_results(pkt_dev, nr_frags); 32531da177e4SLinus Torvalds 32541da177e4SLinus Torvalds return 0; 32551da177e4SLinus Torvalds } 32561da177e4SLinus Torvalds 32571da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t) 32581da177e4SLinus Torvalds { 3259c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev, *best = NULL; 32601da177e4SLinus Torvalds 32611da177e4SLinus Torvalds if_lock(t); 32621da177e4SLinus Torvalds 3263c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 3264c26a8016SLuiz Capitulino if (!pkt_dev->running) 3265222f1806SLuiz Capitulino continue; 3266222f1806SLuiz Capitulino if (best == NULL) 3267c26a8016SLuiz Capitulino best = pkt_dev; 3268c26a8016SLuiz Capitulino else if (pkt_dev->next_tx_us < best->next_tx_us) 3269c26a8016SLuiz Capitulino best = pkt_dev; 32701da177e4SLinus Torvalds } 32711da177e4SLinus Torvalds if_unlock(t); 32721da177e4SLinus Torvalds return best; 32731da177e4SLinus Torvalds } 32741da177e4SLinus Torvalds 3275222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t) 3276222f1806SLuiz Capitulino { 3277c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 32781da177e4SLinus Torvalds 327925c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_stop\n"); 32801da177e4SLinus Torvalds 32811da177e4SLinus Torvalds if_lock(t); 32821da177e4SLinus Torvalds 3283c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 3284c26a8016SLuiz Capitulino pktgen_stop_device(pkt_dev); 3285c26a8016SLuiz Capitulino if (pkt_dev->skb) 3286c26a8016SLuiz Capitulino kfree_skb(pkt_dev->skb); 328795ed63f7SArthur Kepner 3288c26a8016SLuiz Capitulino pkt_dev->skb = NULL; 328995ed63f7SArthur Kepner } 329095ed63f7SArthur Kepner 329195ed63f7SArthur Kepner if_unlock(t); 329295ed63f7SArthur Kepner } 329395ed63f7SArthur Kepner 329495ed63f7SArthur Kepner /* 329595ed63f7SArthur Kepner * one of our devices needs to be removed - find it 329695ed63f7SArthur Kepner * and remove it 329795ed63f7SArthur Kepner */ 329895ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t) 329995ed63f7SArthur Kepner { 3300c26a8016SLuiz Capitulino struct list_head *q, *n; 3301c26a8016SLuiz Capitulino struct pktgen_dev *cur; 330295ed63f7SArthur Kepner 330325c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_rem_one_if\n"); 330495ed63f7SArthur Kepner 330595ed63f7SArthur Kepner if_lock(t); 330695ed63f7SArthur Kepner 3307c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3308c26a8016SLuiz Capitulino cur = list_entry(q, struct pktgen_dev, list); 330995ed63f7SArthur Kepner 3310222f1806SLuiz Capitulino if (!cur->removal_mark) 3311222f1806SLuiz Capitulino continue; 331295ed63f7SArthur Kepner 331395ed63f7SArthur Kepner if (cur->skb) 331495ed63f7SArthur Kepner kfree_skb(cur->skb); 331595ed63f7SArthur Kepner cur->skb = NULL; 331695ed63f7SArthur Kepner 331795ed63f7SArthur Kepner pktgen_remove_device(t, cur); 331895ed63f7SArthur Kepner 331995ed63f7SArthur Kepner break; 332095ed63f7SArthur Kepner } 33211da177e4SLinus Torvalds 33221da177e4SLinus Torvalds if_unlock(t); 33231da177e4SLinus Torvalds } 33241da177e4SLinus Torvalds 33251da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t) 33261da177e4SLinus Torvalds { 3327c26a8016SLuiz Capitulino struct list_head *q, *n; 3328c26a8016SLuiz Capitulino struct pktgen_dev *cur; 33291da177e4SLinus Torvalds 33301da177e4SLinus Torvalds /* Remove all devices, free mem */ 33311da177e4SLinus Torvalds 333225c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_rem_all_ifs\n"); 33331da177e4SLinus Torvalds if_lock(t); 33341da177e4SLinus Torvalds 3335c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3336c26a8016SLuiz Capitulino cur = list_entry(q, struct pktgen_dev, list); 333795ed63f7SArthur Kepner 333895ed63f7SArthur Kepner if (cur->skb) 333995ed63f7SArthur Kepner kfree_skb(cur->skb); 334095ed63f7SArthur Kepner cur->skb = NULL; 334195ed63f7SArthur Kepner 33421da177e4SLinus Torvalds pktgen_remove_device(t, cur); 33431da177e4SLinus Torvalds } 33441da177e4SLinus Torvalds 33451da177e4SLinus Torvalds if_unlock(t); 33461da177e4SLinus Torvalds } 33471da177e4SLinus Torvalds 33481da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t) 33491da177e4SLinus Torvalds { 33501da177e4SLinus Torvalds /* Remove from the thread list */ 33511da177e4SLinus Torvalds 3352ee74baa7SDavid S. Miller remove_proc_entry(t->tsk->comm, pg_proc_dir); 33531da177e4SLinus Torvalds 33546146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 33551da177e4SLinus Torvalds 3356cdcdbe0bSLuiz Capitulino list_del(&t->th_list); 3357cdcdbe0bSLuiz Capitulino 33586146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 33591da177e4SLinus Torvalds } 33601da177e4SLinus Torvalds 33611da177e4SLinus Torvalds static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev) 33621da177e4SLinus Torvalds { 33631da177e4SLinus Torvalds struct net_device *odev = NULL; 33641da177e4SLinus Torvalds __u64 idle_start = 0; 33651da177e4SLinus Torvalds int ret; 33661da177e4SLinus Torvalds 33671da177e4SLinus Torvalds odev = pkt_dev->odev; 33681da177e4SLinus Torvalds 33691da177e4SLinus Torvalds if (pkt_dev->delay_us || pkt_dev->delay_ns) { 33701da177e4SLinus Torvalds u64 now; 33711da177e4SLinus Torvalds 33721da177e4SLinus Torvalds now = getCurUs(); 33731da177e4SLinus Torvalds if (now < pkt_dev->next_tx_us) 33741da177e4SLinus Torvalds spin(pkt_dev, pkt_dev->next_tx_us); 33751da177e4SLinus Torvalds 33761da177e4SLinus Torvalds /* This is max DELAY, this has special meaning of 33771da177e4SLinus Torvalds * "never transmit" 33781da177e4SLinus Torvalds */ 33791da177e4SLinus Torvalds if (pkt_dev->delay_us == 0x7FFFFFFF) { 33801da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us; 33811da177e4SLinus Torvalds pkt_dev->next_tx_ns = pkt_dev->delay_ns; 33821da177e4SLinus Torvalds goto out; 33831da177e4SLinus Torvalds } 33841da177e4SLinus Torvalds } 33851da177e4SLinus Torvalds 3386f25f4e44SPeter P Waskiewicz Jr if ((netif_queue_stopped(odev) || 3387378be2c0SRobert Olsson (pkt_dev->skb && 3388378be2c0SRobert Olsson netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping))) || 3389f25f4e44SPeter P Waskiewicz Jr need_resched()) { 33901da177e4SLinus Torvalds idle_start = getCurUs(); 33911da177e4SLinus Torvalds 33921da177e4SLinus Torvalds if (!netif_running(odev)) { 33931da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 339495ed63f7SArthur Kepner if (pkt_dev->skb) 339595ed63f7SArthur Kepner kfree_skb(pkt_dev->skb); 339695ed63f7SArthur Kepner pkt_dev->skb = NULL; 33971da177e4SLinus Torvalds goto out; 33981da177e4SLinus Torvalds } 33991da177e4SLinus Torvalds if (need_resched()) 34001da177e4SLinus Torvalds schedule(); 34011da177e4SLinus Torvalds 34021da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 34031da177e4SLinus Torvalds 3404f25f4e44SPeter P Waskiewicz Jr if (netif_queue_stopped(odev) || 3405f25f4e44SPeter P Waskiewicz Jr netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) { 34061da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 34071da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 34081da177e4SLinus Torvalds goto out; /* Try the next interface */ 34091da177e4SLinus Torvalds } 34101da177e4SLinus Torvalds } 34111da177e4SLinus Torvalds 34121da177e4SLinus Torvalds if (pkt_dev->last_ok || !pkt_dev->skb) { 3413222f1806SLuiz Capitulino if ((++pkt_dev->clone_count >= pkt_dev->clone_skb) 3414222f1806SLuiz Capitulino || (!pkt_dev->skb)) { 34151da177e4SLinus Torvalds /* build a new pkt */ 34161da177e4SLinus Torvalds if (pkt_dev->skb) 34171da177e4SLinus Torvalds kfree_skb(pkt_dev->skb); 34181da177e4SLinus Torvalds 34191da177e4SLinus Torvalds pkt_dev->skb = fill_packet(odev, pkt_dev); 34201da177e4SLinus Torvalds if (pkt_dev->skb == NULL) { 342125a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: couldn't " 342225a8b254SDavid S. Miller "allocate skb in fill_packet.\n"); 34231da177e4SLinus Torvalds schedule(); 34241da177e4SLinus Torvalds pkt_dev->clone_count--; /* back out increment, OOM */ 34251da177e4SLinus Torvalds goto out; 34261da177e4SLinus Torvalds } 34271da177e4SLinus Torvalds pkt_dev->allocated_skbs++; 34281da177e4SLinus Torvalds pkt_dev->clone_count = 0; /* reset counter */ 34291da177e4SLinus Torvalds } 34301da177e4SLinus Torvalds } 34311da177e4SLinus Torvalds 3432932ff279SHerbert Xu netif_tx_lock_bh(odev); 3433f25f4e44SPeter P Waskiewicz Jr if (!netif_queue_stopped(odev) && 3434f25f4e44SPeter P Waskiewicz Jr !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) { 34351da177e4SLinus Torvalds 34361da177e4SLinus Torvalds atomic_inc(&(pkt_dev->skb->users)); 34371da177e4SLinus Torvalds retry_now: 34381da177e4SLinus Torvalds ret = odev->hard_start_xmit(pkt_dev->skb, odev); 34391da177e4SLinus Torvalds if (likely(ret == NETDEV_TX_OK)) { 34401da177e4SLinus Torvalds pkt_dev->last_ok = 1; 34411da177e4SLinus Torvalds pkt_dev->sofar++; 34421da177e4SLinus Torvalds pkt_dev->seq_num++; 34431da177e4SLinus Torvalds pkt_dev->tx_bytes += pkt_dev->cur_pkt_size; 34441da177e4SLinus Torvalds 34451da177e4SLinus Torvalds } else if (ret == NETDEV_TX_LOCKED 34461da177e4SLinus Torvalds && (odev->features & NETIF_F_LLTX)) { 34471da177e4SLinus Torvalds cpu_relax(); 34481da177e4SLinus Torvalds goto retry_now; 34491da177e4SLinus Torvalds } else { /* Retry it next time */ 34501da177e4SLinus Torvalds 34511da177e4SLinus Torvalds atomic_dec(&(pkt_dev->skb->users)); 34521da177e4SLinus Torvalds 34531da177e4SLinus Torvalds if (debug && net_ratelimit()) 34541da177e4SLinus Torvalds printk(KERN_INFO "pktgen: Hard xmit error\n"); 34551da177e4SLinus Torvalds 34561da177e4SLinus Torvalds pkt_dev->errors++; 34571da177e4SLinus Torvalds pkt_dev->last_ok = 0; 34581da177e4SLinus Torvalds } 34591da177e4SLinus Torvalds 34601da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); 34611da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 34621da177e4SLinus Torvalds 34631da177e4SLinus Torvalds pkt_dev->next_tx_us += pkt_dev->delay_us; 34641da177e4SLinus Torvalds pkt_dev->next_tx_ns += pkt_dev->delay_ns; 34651da177e4SLinus Torvalds 34661da177e4SLinus Torvalds if (pkt_dev->next_tx_ns > 1000) { 34671da177e4SLinus Torvalds pkt_dev->next_tx_us++; 34681da177e4SLinus Torvalds pkt_dev->next_tx_ns -= 1000; 34691da177e4SLinus Torvalds } 34701da177e4SLinus Torvalds } 34711da177e4SLinus Torvalds 34721da177e4SLinus Torvalds else { /* Retry it next time */ 34731da177e4SLinus Torvalds pkt_dev->last_ok = 0; 34741da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 34751da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 34761da177e4SLinus Torvalds } 34771da177e4SLinus Torvalds 3478932ff279SHerbert Xu netif_tx_unlock_bh(odev); 34791da177e4SLinus Torvalds 34801da177e4SLinus Torvalds /* If pkt_dev->count is zero, then run forever */ 34811da177e4SLinus Torvalds if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) { 34821da177e4SLinus Torvalds if (atomic_read(&(pkt_dev->skb->users)) != 1) { 34831da177e4SLinus Torvalds idle_start = getCurUs(); 34841da177e4SLinus Torvalds while (atomic_read(&(pkt_dev->skb->users)) != 1) { 34851da177e4SLinus Torvalds if (signal_pending(current)) { 34861da177e4SLinus Torvalds break; 34871da177e4SLinus Torvalds } 34881da177e4SLinus Torvalds schedule(); 34891da177e4SLinus Torvalds } 34901da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 34911da177e4SLinus Torvalds } 34921da177e4SLinus Torvalds 34931da177e4SLinus Torvalds /* Done with this */ 34941da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 349595ed63f7SArthur Kepner if (pkt_dev->skb) 349695ed63f7SArthur Kepner kfree_skb(pkt_dev->skb); 349795ed63f7SArthur Kepner pkt_dev->skb = NULL; 34981da177e4SLinus Torvalds } 34991da177e4SLinus Torvalds out:; 35001da177e4SLinus Torvalds } 35011da177e4SLinus Torvalds 35021da177e4SLinus Torvalds /* 35031da177e4SLinus Torvalds * Main loop of the thread goes here 35041da177e4SLinus Torvalds */ 35051da177e4SLinus Torvalds 3506ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg) 35071da177e4SLinus Torvalds { 35081da177e4SLinus Torvalds DEFINE_WAIT(wait); 3509ee74baa7SDavid S. Miller struct pktgen_thread *t = arg; 35101da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 35111da177e4SLinus Torvalds int cpu = t->cpu; 35121da177e4SLinus Torvalds 3513ee74baa7SDavid S. Miller BUG_ON(smp_processor_id() != cpu); 35141da177e4SLinus Torvalds 35151da177e4SLinus Torvalds init_waitqueue_head(&t->queue); 35161da177e4SLinus Torvalds 351725c4e53aSStephen Hemminger pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid); 35181da177e4SLinus Torvalds 3519ee74baa7SDavid S. Miller set_current_state(TASK_INTERRUPTIBLE); 35201da177e4SLinus Torvalds 352183144186SRafael J. Wysocki set_freezable(); 352283144186SRafael J. Wysocki 3523ee74baa7SDavid S. Miller while (!kthread_should_stop()) { 3524ee74baa7SDavid S. Miller pkt_dev = next_to_run(t); 3525ee74baa7SDavid S. Miller 3526ee74baa7SDavid S. Miller if (!pkt_dev && 3527ee74baa7SDavid S. Miller (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV)) 3528ee74baa7SDavid S. Miller == 0) { 3529ee74baa7SDavid S. Miller prepare_to_wait(&(t->queue), &wait, 3530ee74baa7SDavid S. Miller TASK_INTERRUPTIBLE); 3531ee74baa7SDavid S. Miller schedule_timeout(HZ / 10); 3532ee74baa7SDavid S. Miller finish_wait(&(t->queue), &wait); 3533ee74baa7SDavid S. Miller } 35341da177e4SLinus Torvalds 35351da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 35361da177e4SLinus Torvalds 3537b163911fSRobert Olsson if (pkt_dev) 35381da177e4SLinus Torvalds pktgen_xmit(pkt_dev); 35391da177e4SLinus Torvalds 35401da177e4SLinus Torvalds if (t->control & T_STOP) { 35411da177e4SLinus Torvalds pktgen_stop(t); 35421da177e4SLinus Torvalds t->control &= ~(T_STOP); 35431da177e4SLinus Torvalds } 35441da177e4SLinus Torvalds 35451da177e4SLinus Torvalds if (t->control & T_RUN) { 35461da177e4SLinus Torvalds pktgen_run(t); 35471da177e4SLinus Torvalds t->control &= ~(T_RUN); 35481da177e4SLinus Torvalds } 35491da177e4SLinus Torvalds 355095ed63f7SArthur Kepner if (t->control & T_REMDEVALL) { 35511da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 355295ed63f7SArthur Kepner t->control &= ~(T_REMDEVALL); 355395ed63f7SArthur Kepner } 355495ed63f7SArthur Kepner 355595ed63f7SArthur Kepner if (t->control & T_REMDEV) { 355695ed63f7SArthur Kepner pktgen_rem_one_if(t); 35571da177e4SLinus Torvalds t->control &= ~(T_REMDEV); 35581da177e4SLinus Torvalds } 35591da177e4SLinus Torvalds 356009fe3ef4SAndrew Morton try_to_freeze(); 356109fe3ef4SAndrew Morton 3562ee74baa7SDavid S. Miller set_current_state(TASK_INTERRUPTIBLE); 35631da177e4SLinus Torvalds } 35641da177e4SLinus Torvalds 356525c4e53aSStephen Hemminger pr_debug("pktgen: %s stopping all device\n", t->tsk->comm); 35661da177e4SLinus Torvalds pktgen_stop(t); 35671da177e4SLinus Torvalds 356825c4e53aSStephen Hemminger pr_debug("pktgen: %s removing all device\n", t->tsk->comm); 35691da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 35701da177e4SLinus Torvalds 357125c4e53aSStephen Hemminger pr_debug("pktgen: %s removing thread.\n", t->tsk->comm); 35721da177e4SLinus Torvalds pktgen_rem_thread(t); 3573cdcdbe0bSLuiz Capitulino 3574ee74baa7SDavid S. Miller return 0; 35751da177e4SLinus Torvalds } 35761da177e4SLinus Torvalds 3577222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, 3578222f1806SLuiz Capitulino const char *ifname) 35791da177e4SLinus Torvalds { 3580c26a8016SLuiz Capitulino struct pktgen_dev *p, *pkt_dev = NULL; 35811da177e4SLinus Torvalds if_lock(t); 35821da177e4SLinus Torvalds 3583c26a8016SLuiz Capitulino list_for_each_entry(p, &t->if_list, list) 358439df232fSStephen Hemminger if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) { 3585c26a8016SLuiz Capitulino pkt_dev = p; 35861da177e4SLinus Torvalds break; 35871da177e4SLinus Torvalds } 35881da177e4SLinus Torvalds 35891da177e4SLinus Torvalds if_unlock(t); 359025c4e53aSStephen Hemminger pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev); 35911da177e4SLinus Torvalds return pkt_dev; 35921da177e4SLinus Torvalds } 35931da177e4SLinus Torvalds 35941da177e4SLinus Torvalds /* 35951da177e4SLinus Torvalds * Adds a dev at front of if_list. 35961da177e4SLinus Torvalds */ 35971da177e4SLinus Torvalds 3598222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t, 3599222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 36001da177e4SLinus Torvalds { 36011da177e4SLinus Torvalds int rv = 0; 36021da177e4SLinus Torvalds 36031da177e4SLinus Torvalds if_lock(t); 36041da177e4SLinus Torvalds 36051da177e4SLinus Torvalds if (pkt_dev->pg_thread) { 360625a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: already assigned " 360725a8b254SDavid S. Miller "to a thread.\n"); 36081da177e4SLinus Torvalds rv = -EBUSY; 36091da177e4SLinus Torvalds goto out; 36101da177e4SLinus Torvalds } 3611c26a8016SLuiz Capitulino 3612c26a8016SLuiz Capitulino list_add(&pkt_dev->list, &t->if_list); 36131da177e4SLinus Torvalds pkt_dev->pg_thread = t; 36141da177e4SLinus Torvalds pkt_dev->running = 0; 36151da177e4SLinus Torvalds 36161da177e4SLinus Torvalds out: 36171da177e4SLinus Torvalds if_unlock(t); 36181da177e4SLinus Torvalds return rv; 36191da177e4SLinus Torvalds } 36201da177e4SLinus Torvalds 36211da177e4SLinus Torvalds /* Called under thread lock */ 36221da177e4SLinus Torvalds 36231da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname) 36241da177e4SLinus Torvalds { 36251da177e4SLinus Torvalds struct pktgen_dev *pkt_dev; 362639df232fSStephen Hemminger int err; 36271da177e4SLinus Torvalds 36281da177e4SLinus Torvalds /* We don't allow a device to be on several threads */ 36291da177e4SLinus Torvalds 3630d50a6b56SStephen Hemminger pkt_dev = __pktgen_NN_threads(ifname, FIND); 3631d50a6b56SStephen Hemminger if (pkt_dev) { 363225a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: interface already used.\n"); 3633d50a6b56SStephen Hemminger return -EBUSY; 3634d50a6b56SStephen Hemminger } 36351da177e4SLinus Torvalds 36362845b63bSStephen Hemminger pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL); 36371da177e4SLinus Torvalds if (!pkt_dev) 36381da177e4SLinus Torvalds return -ENOMEM; 36391da177e4SLinus Torvalds 36401da177e4SLinus Torvalds pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state)); 36411da177e4SLinus Torvalds if (pkt_dev->flows == NULL) { 36421da177e4SLinus Torvalds kfree(pkt_dev); 36431da177e4SLinus Torvalds return -ENOMEM; 36441da177e4SLinus Torvalds } 36451da177e4SLinus Torvalds memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state)); 36461da177e4SLinus Torvalds 364795ed63f7SArthur Kepner pkt_dev->removal_mark = 0; 36481da177e4SLinus Torvalds pkt_dev->min_pkt_size = ETH_ZLEN; 36491da177e4SLinus Torvalds pkt_dev->max_pkt_size = ETH_ZLEN; 36501da177e4SLinus Torvalds pkt_dev->nfrags = 0; 36511da177e4SLinus Torvalds pkt_dev->clone_skb = pg_clone_skb_d; 36521da177e4SLinus Torvalds pkt_dev->delay_us = pg_delay_d / 1000; 36531da177e4SLinus Torvalds pkt_dev->delay_ns = pg_delay_d % 1000; 36541da177e4SLinus Torvalds pkt_dev->count = pg_count_d; 36551da177e4SLinus Torvalds pkt_dev->sofar = 0; 36561da177e4SLinus Torvalds pkt_dev->udp_src_min = 9; /* sink port */ 36571da177e4SLinus Torvalds pkt_dev->udp_src_max = 9; 36581da177e4SLinus Torvalds pkt_dev->udp_dst_min = 9; 36591da177e4SLinus Torvalds pkt_dev->udp_dst_max = 9; 36601da177e4SLinus Torvalds 366134954ddcSFrancesco Fondelli pkt_dev->vlan_p = 0; 366234954ddcSFrancesco Fondelli pkt_dev->vlan_cfi = 0; 366334954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; 366434954ddcSFrancesco Fondelli pkt_dev->svlan_p = 0; 366534954ddcSFrancesco Fondelli pkt_dev->svlan_cfi = 0; 366634954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 366734954ddcSFrancesco Fondelli 366839df232fSStephen Hemminger err = pktgen_setup_dev(pkt_dev, ifname); 366939df232fSStephen Hemminger if (err) 367039df232fSStephen Hemminger goto out1; 36711da177e4SLinus Torvalds 367239df232fSStephen Hemminger pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir); 367339df232fSStephen Hemminger if (!pkt_dev->entry) { 367425a8b254SDavid S. Miller printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n", 3675d50a6b56SStephen Hemminger PG_PROC_DIR, ifname); 367639df232fSStephen Hemminger err = -EINVAL; 367739df232fSStephen Hemminger goto out2; 367839df232fSStephen Hemminger } 367939df232fSStephen Hemminger pkt_dev->entry->proc_fops = &pktgen_if_fops; 368039df232fSStephen Hemminger pkt_dev->entry->data = pkt_dev; 3681a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 3682a553e4a6SJamal Hadi Salim pkt_dev->ipsmode = XFRM_MODE_TRANSPORT; 3683a553e4a6SJamal Hadi Salim pkt_dev->ipsproto = IPPROTO_ESP; 3684a553e4a6SJamal Hadi Salim #endif 368539df232fSStephen Hemminger 368639df232fSStephen Hemminger return add_dev_to_thread(t, pkt_dev); 368739df232fSStephen Hemminger out2: 368839df232fSStephen Hemminger dev_put(pkt_dev->odev); 368939df232fSStephen Hemminger out1: 3690a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 3691a553e4a6SJamal Hadi Salim free_SAs(pkt_dev); 3692a553e4a6SJamal Hadi Salim #endif 36931da177e4SLinus Torvalds if (pkt_dev->flows) 36941da177e4SLinus Torvalds vfree(pkt_dev->flows); 36951da177e4SLinus Torvalds kfree(pkt_dev); 369639df232fSStephen Hemminger return err; 36971da177e4SLinus Torvalds } 36981da177e4SLinus Torvalds 3699ee74baa7SDavid S. Miller static int __init pktgen_create_thread(int cpu) 37001da177e4SLinus Torvalds { 3701cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 3702d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 3703ee74baa7SDavid S. Miller struct task_struct *p; 37041da177e4SLinus Torvalds 37052845b63bSStephen Hemminger t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL); 37061da177e4SLinus Torvalds if (!t) { 370725a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: out of memory, can't " 370825a8b254SDavid S. Miller "create new thread.\n"); 37091da177e4SLinus Torvalds return -ENOMEM; 37101da177e4SLinus Torvalds } 37111da177e4SLinus Torvalds 37121da177e4SLinus Torvalds spin_lock_init(&t->if_lock); 37131da177e4SLinus Torvalds t->cpu = cpu; 37141da177e4SLinus Torvalds 3715ee74baa7SDavid S. Miller INIT_LIST_HEAD(&t->if_list); 3716ee74baa7SDavid S. Miller 3717ee74baa7SDavid S. Miller list_add_tail(&t->th_list, &pktgen_threads); 3718ee74baa7SDavid S. Miller 3719ee74baa7SDavid S. Miller p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu); 3720ee74baa7SDavid S. Miller if (IS_ERR(p)) { 372125a8b254SDavid S. Miller printk(KERN_ERR "pktgen: kernel_thread() failed " 372225a8b254SDavid S. Miller "for cpu %d\n", t->cpu); 3723ee74baa7SDavid S. Miller list_del(&t->th_list); 3724ee74baa7SDavid S. Miller kfree(t); 3725ee74baa7SDavid S. Miller return PTR_ERR(p); 3726ee74baa7SDavid S. Miller } 3727ee74baa7SDavid S. Miller kthread_bind(p, cpu); 3728ee74baa7SDavid S. Miller t->tsk = p; 3729ee74baa7SDavid S. Miller 3730ee74baa7SDavid S. Miller pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir); 3731d50a6b56SStephen Hemminger if (!pe) { 373225a8b254SDavid S. Miller printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n", 3733ee74baa7SDavid S. Miller PG_PROC_DIR, t->tsk->comm); 3734ee74baa7SDavid S. Miller kthread_stop(p); 3735ee74baa7SDavid S. Miller list_del(&t->th_list); 37361da177e4SLinus Torvalds kfree(t); 37371da177e4SLinus Torvalds return -EINVAL; 37381da177e4SLinus Torvalds } 3739d50a6b56SStephen Hemminger 3740d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_thread_fops; 3741d50a6b56SStephen Hemminger pe->data = t; 37421da177e4SLinus Torvalds 3743ee74baa7SDavid S. Miller wake_up_process(p); 37441da177e4SLinus Torvalds 37451da177e4SLinus Torvalds return 0; 37461da177e4SLinus Torvalds } 37471da177e4SLinus Torvalds 37481da177e4SLinus Torvalds /* 37491da177e4SLinus Torvalds * Removes a device from the thread if_list. 37501da177e4SLinus Torvalds */ 3751222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t, 3752222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 37531da177e4SLinus Torvalds { 3754c26a8016SLuiz Capitulino struct list_head *q, *n; 3755c26a8016SLuiz Capitulino struct pktgen_dev *p; 37561da177e4SLinus Torvalds 3757c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3758c26a8016SLuiz Capitulino p = list_entry(q, struct pktgen_dev, list); 3759c26a8016SLuiz Capitulino if (p == pkt_dev) 3760c26a8016SLuiz Capitulino list_del(&p->list); 37611da177e4SLinus Torvalds } 37621da177e4SLinus Torvalds } 37631da177e4SLinus Torvalds 3764222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t, 3765222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 37661da177e4SLinus Torvalds { 37671da177e4SLinus Torvalds 376825c4e53aSStephen Hemminger pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev); 37691da177e4SLinus Torvalds 37701da177e4SLinus Torvalds if (pkt_dev->running) { 377125a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: WARNING: trying to remove a " 377225a8b254SDavid S. Miller "running interface, stopping it now.\n"); 37731da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 37741da177e4SLinus Torvalds } 37751da177e4SLinus Torvalds 37761da177e4SLinus Torvalds /* Dis-associate from the interface */ 37771da177e4SLinus Torvalds 37781da177e4SLinus Torvalds if (pkt_dev->odev) { 37791da177e4SLinus Torvalds dev_put(pkt_dev->odev); 37801da177e4SLinus Torvalds pkt_dev->odev = NULL; 37811da177e4SLinus Torvalds } 37821da177e4SLinus Torvalds 37831da177e4SLinus Torvalds /* And update the thread if_list */ 37841da177e4SLinus Torvalds 37851da177e4SLinus Torvalds _rem_dev_from_if_list(t, pkt_dev); 37861da177e4SLinus Torvalds 378739df232fSStephen Hemminger if (pkt_dev->entry) 378839df232fSStephen Hemminger remove_proc_entry(pkt_dev->entry->name, pg_proc_dir); 37891da177e4SLinus Torvalds 3790a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 3791a553e4a6SJamal Hadi Salim free_SAs(pkt_dev); 3792a553e4a6SJamal Hadi Salim #endif 37931da177e4SLinus Torvalds if (pkt_dev->flows) 37941da177e4SLinus Torvalds vfree(pkt_dev->flows); 37951da177e4SLinus Torvalds kfree(pkt_dev); 37961da177e4SLinus Torvalds return 0; 37971da177e4SLinus Torvalds } 37981da177e4SLinus Torvalds 37991da177e4SLinus Torvalds static int __init pg_init(void) 38001da177e4SLinus Torvalds { 38011da177e4SLinus Torvalds int cpu; 3802d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 3803d50a6b56SStephen Hemminger 380425a8b254SDavid S. Miller printk(KERN_INFO "%s", version); 38051da177e4SLinus Torvalds 3806457c4cbcSEric W. Biederman pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net); 3807d50a6b56SStephen Hemminger if (!pg_proc_dir) 3808d50a6b56SStephen Hemminger return -ENODEV; 3809d50a6b56SStephen Hemminger pg_proc_dir->owner = THIS_MODULE; 38101da177e4SLinus Torvalds 3811d50a6b56SStephen Hemminger pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir); 3812d50a6b56SStephen Hemminger if (pe == NULL) { 381325a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: cannot create %s " 381425a8b254SDavid S. Miller "procfs entry.\n", PGCTRL); 3815457c4cbcSEric W. Biederman proc_net_remove(&init_net, PG_PROC_DIR); 38161da177e4SLinus Torvalds return -EINVAL; 38171da177e4SLinus Torvalds } 38181da177e4SLinus Torvalds 3819d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_fops; 3820d50a6b56SStephen Hemminger pe->data = NULL; 38211da177e4SLinus Torvalds 38221da177e4SLinus Torvalds /* Register us to receive netdevice events */ 38231da177e4SLinus Torvalds register_netdevice_notifier(&pktgen_notifier_block); 38241da177e4SLinus Torvalds 3825670c02c2SJohn Hawkes for_each_online_cpu(cpu) { 38268024bb24SLuiz Capitulino int err; 38271da177e4SLinus Torvalds 3828ee74baa7SDavid S. Miller err = pktgen_create_thread(cpu); 38298024bb24SLuiz Capitulino if (err) 383025a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: WARNING: Cannot create " 383125a8b254SDavid S. Miller "thread for cpu %d (%d)\n", cpu, err); 38321da177e4SLinus Torvalds } 38338024bb24SLuiz Capitulino 38348024bb24SLuiz Capitulino if (list_empty(&pktgen_threads)) { 383525a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: Initialization failed for " 383625a8b254SDavid S. Miller "all threads\n"); 38378024bb24SLuiz Capitulino unregister_netdevice_notifier(&pktgen_notifier_block); 38388024bb24SLuiz Capitulino remove_proc_entry(PGCTRL, pg_proc_dir); 3839457c4cbcSEric W. Biederman proc_net_remove(&init_net, PG_PROC_DIR); 38408024bb24SLuiz Capitulino return -ENODEV; 38418024bb24SLuiz Capitulino } 38428024bb24SLuiz Capitulino 38431da177e4SLinus Torvalds return 0; 38441da177e4SLinus Torvalds } 38451da177e4SLinus Torvalds 38461da177e4SLinus Torvalds static void __exit pg_cleanup(void) 38471da177e4SLinus Torvalds { 3848cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 3849cdcdbe0bSLuiz Capitulino struct list_head *q, *n; 38501da177e4SLinus Torvalds wait_queue_head_t queue; 38511da177e4SLinus Torvalds init_waitqueue_head(&queue); 38521da177e4SLinus Torvalds 38531da177e4SLinus Torvalds /* Stop all interfaces & threads */ 38541da177e4SLinus Torvalds 3855cdcdbe0bSLuiz Capitulino list_for_each_safe(q, n, &pktgen_threads) { 3856cdcdbe0bSLuiz Capitulino t = list_entry(q, struct pktgen_thread, th_list); 3857ee74baa7SDavid S. Miller kthread_stop(t->tsk); 3858ee74baa7SDavid S. Miller kfree(t); 38591da177e4SLinus Torvalds } 38601da177e4SLinus Torvalds 38611da177e4SLinus Torvalds /* Un-register us from receiving netdevice events */ 38621da177e4SLinus Torvalds unregister_netdevice_notifier(&pktgen_notifier_block); 38631da177e4SLinus Torvalds 38641da177e4SLinus Torvalds /* Clean up proc file system */ 3865d50a6b56SStephen Hemminger remove_proc_entry(PGCTRL, pg_proc_dir); 3866457c4cbcSEric W. Biederman proc_net_remove(&init_net, PG_PROC_DIR); 38671da177e4SLinus Torvalds } 38681da177e4SLinus Torvalds 38691da177e4SLinus Torvalds module_init(pg_init); 38701da177e4SLinus Torvalds module_exit(pg_cleanup); 38711da177e4SLinus Torvalds 38721da177e4SLinus Torvalds MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se"); 38731da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool"); 38741da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 38751da177e4SLinus Torvalds module_param(pg_count_d, int, 0); 38761da177e4SLinus Torvalds module_param(pg_delay_d, int, 0); 38771da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0); 38781da177e4SLinus Torvalds module_param(debug, int, 0); 3879