11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Authors: 31da177e4SLinus Torvalds * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se> 41da177e4SLinus Torvalds * Uppsala University and 51da177e4SLinus Torvalds * Swedish University of Agricultural Sciences 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 81da177e4SLinus Torvalds * Ben Greear <greearb@candelatech.com> 996de0e25SJan Engelhardt * Jens Låås <jens.laas@data.slu.se> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 121da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 131da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 141da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * A tool for loading the network with preconfigurated packets. 181da177e4SLinus Torvalds * The tool is implemented as a linux module. Parameters are output 191da177e4SLinus Torvalds * device, delay (to hard_xmit), number of packets, and whether 201da177e4SLinus Torvalds * to use multiple SKBs or just the same one. 211da177e4SLinus Torvalds * pktgen uses the installed interface's output routine. 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Additional hacking by: 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * Jens.Laas@data.slu.se 261da177e4SLinus Torvalds * Improved by ANK. 010120. 271da177e4SLinus Torvalds * Improved by ANK even more. 010212. 281da177e4SLinus Torvalds * MAC address typo fixed. 010417 --ro 291da177e4SLinus Torvalds * Integrated. 020301 --DaveM 301da177e4SLinus Torvalds * Added multiskb option 020301 --DaveM 311da177e4SLinus Torvalds * Scaling of results. 020417--sigurdur@linpro.no 321da177e4SLinus Torvalds * Significant re-work of the module: 331da177e4SLinus Torvalds * * Convert to threaded model to more efficiently be able to transmit 341da177e4SLinus Torvalds * and receive on multiple interfaces at once. 351da177e4SLinus Torvalds * * Converted many counters to __u64 to allow longer runs. 361da177e4SLinus Torvalds * * Allow configuration of ranges, like min/max IP address, MACs, 371da177e4SLinus Torvalds * and UDP-ports, for both source and destination, and can 381da177e4SLinus Torvalds * set to use a random distribution or sequentially walk the range. 391da177e4SLinus Torvalds * * Can now change most values after starting. 401da177e4SLinus Torvalds * * Place 12-byte packet in UDP payload with magic number, 411da177e4SLinus Torvalds * sequence number, and timestamp. 421da177e4SLinus Torvalds * * Add receiver code that detects dropped pkts, re-ordered pkts, and 431da177e4SLinus Torvalds * latencies (with micro-second) precision. 441da177e4SLinus Torvalds * * Add IOCTL interface to easily get counters & configuration. 451da177e4SLinus Torvalds * --Ben Greear <greearb@candelatech.com> 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * Renamed multiskb to clone_skb and cleaned up sending core for two distinct 481da177e4SLinus Torvalds * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0 491da177e4SLinus Torvalds * as a "fastpath" with a configurable number of clones after alloc's. 501da177e4SLinus Torvalds * clone_skb=0 means all packets are allocated this also means ranges time 511da177e4SLinus Torvalds * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100 521da177e4SLinus Torvalds * clones. 531da177e4SLinus Torvalds * 541da177e4SLinus Torvalds * Also moved to /proc/net/pktgen/ 551da177e4SLinus Torvalds * --ro 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever 581da177e4SLinus Torvalds * mistakes. Also merged in DaveM's patch in the -pre6 patch. 591da177e4SLinus Torvalds * --Ben Greear <greearb@candelatech.com> 601da177e4SLinus Torvalds * 611da177e4SLinus Torvalds * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br) 621da177e4SLinus Torvalds * 631da177e4SLinus Torvalds * 641da177e4SLinus Torvalds * 021124 Finished major redesign and rewrite for new functionality. 651da177e4SLinus Torvalds * See Documentation/networking/pktgen.txt for how to use this. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * The new operation: 681da177e4SLinus Torvalds * For each CPU one thread/process is created at start. This process checks 691da177e4SLinus Torvalds * for running devices in the if_list and sends packets until count is 0 it 701da177e4SLinus Torvalds * also the thread checks the thread->control which is used for inter-process 711da177e4SLinus Torvalds * communication. controlling process "posts" operations to the threads this 721da177e4SLinus Torvalds * way. The if_lock should be possible to remove when add/rem_device is merged 731da177e4SLinus Torvalds * into this too. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * By design there should only be *one* "controlling" process. In practice 761da177e4SLinus Torvalds * multiple write accesses gives unpredictable result. Understood by "write" 771da177e4SLinus Torvalds * to /proc gives result code thats should be read be the "writer". 78b4099fabSStephen Hemminger * For practical use this should be no problem. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * Note when adding devices to a specific CPU there good idea to also assign 811da177e4SLinus Torvalds * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU. 821da177e4SLinus Torvalds * --ro 831da177e4SLinus Torvalds * 841da177e4SLinus Torvalds * Fix refcount off by one if first packet fails, potential null deref, 851da177e4SLinus Torvalds * memleak 030710- KJP 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * First "ranges" functionality for ipv6 030726 --ro 881da177e4SLinus Torvalds * 891da177e4SLinus Torvalds * Included flow support. 030802 ANK. 901da177e4SLinus Torvalds * 911da177e4SLinus Torvalds * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org> 921da177e4SLinus Torvalds * 931da177e4SLinus Torvalds * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419 941da177e4SLinus Torvalds * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604 951da177e4SLinus Torvalds * 961da177e4SLinus Torvalds * New xmit() return, do_div and misc clean up by Stephen Hemminger 971da177e4SLinus Torvalds * <shemminger@osdl.org> 040923 981da177e4SLinus Torvalds * 99b4099fabSStephen Hemminger * Randy Dunlap fixed u64 printk compiler waring 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org> 1021da177e4SLinus Torvalds * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * Corrections from Nikolai Malykh (nmalykh@bilim.com) 1051da177e4SLinus Torvalds * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230 1061da177e4SLinus Torvalds * 1071da177e4SLinus Torvalds * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com> 1081da177e4SLinus Torvalds * 050103 109ca6549afSSteven Whitehouse * 110ca6549afSSteven Whitehouse * MPLS support by Steven Whitehouse <steve@chygwyn.com> 111ca6549afSSteven Whitehouse * 11234954ddcSFrancesco Fondelli * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com> 11334954ddcSFrancesco Fondelli * 114ce5d0b47SAdit Ranadive * Fixed src_mac command to set source mac of packet to value specified in 115ce5d0b47SAdit Ranadive * command by Adit Ranadive <adit.262@gmail.com> 116ce5d0b47SAdit Ranadive * 1171da177e4SLinus Torvalds */ 1181da177e4SLinus Torvalds #include <linux/sys.h> 1191da177e4SLinus Torvalds #include <linux/types.h> 1201da177e4SLinus Torvalds #include <linux/module.h> 1211da177e4SLinus Torvalds #include <linux/moduleparam.h> 1221da177e4SLinus Torvalds #include <linux/kernel.h> 123222fa076SLuiz Capitulino #include <linux/mutex.h> 1241da177e4SLinus Torvalds #include <linux/sched.h> 1251da177e4SLinus Torvalds #include <linux/slab.h> 1261da177e4SLinus Torvalds #include <linux/vmalloc.h> 1271da177e4SLinus Torvalds #include <linux/unistd.h> 1281da177e4SLinus Torvalds #include <linux/string.h> 1291da177e4SLinus Torvalds #include <linux/ptrace.h> 1301da177e4SLinus Torvalds #include <linux/errno.h> 1311da177e4SLinus Torvalds #include <linux/ioport.h> 1321da177e4SLinus Torvalds #include <linux/interrupt.h> 1334fc268d2SRandy Dunlap #include <linux/capability.h> 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> 1641977f032SJiri Slaby #include <linux/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 171e6fce5b9SRobert Olsson #define VERSION "pktgen v2.70: Packet Generator for packet performance testing.\n" 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds #define IP_NAME_SZ 32 174ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */ 175d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100) 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds /* Device flag bits */ 1781da177e4SLinus Torvalds #define F_IPSRC_RND (1<<0) /* IP-Src Random */ 1791da177e4SLinus Torvalds #define F_IPDST_RND (1<<1) /* IP-Dst Random */ 1801da177e4SLinus Torvalds #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */ 1811da177e4SLinus Torvalds #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */ 1821da177e4SLinus Torvalds #define F_MACSRC_RND (1<<4) /* MAC-Src Random */ 1831da177e4SLinus Torvalds #define F_MACDST_RND (1<<5) /* MAC-Dst Random */ 1841da177e4SLinus Torvalds #define F_TXSIZE_RND (1<<6) /* Transmit size is random */ 1851da177e4SLinus Torvalds #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */ 186ca6549afSSteven Whitehouse #define F_MPLS_RND (1<<8) /* Random MPLS labels */ 18734954ddcSFrancesco Fondelli #define F_VID_RND (1<<9) /* Random VLAN ID */ 18834954ddcSFrancesco Fondelli #define F_SVID_RND (1<<10) /* Random SVLAN ID */ 189007a531bSJamal Hadi Salim #define F_FLOW_SEQ (1<<11) /* Sequential flows */ 190a553e4a6SJamal Hadi Salim #define F_IPSEC_ON (1<<12) /* ipsec on for flows */ 19145b270f8SRobert Olsson #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */ 192e6fce5b9SRobert Olsson #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */ 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* Thread control flag bits */ 1951da177e4SLinus Torvalds #define T_TERMINATE (1<<0) 1961da177e4SLinus Torvalds #define T_STOP (1<<1) /* Stop run */ 1971da177e4SLinus Torvalds #define T_RUN (1<<2) /* Start run */ 19895ed63f7SArthur Kepner #define T_REMDEVALL (1<<3) /* Remove all devs */ 19995ed63f7SArthur Kepner #define T_REMDEV (1<<4) /* Remove one dev */ 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds /* If lock -- can be removed after some work */ 2021da177e4SLinus Torvalds #define if_lock(t) spin_lock(&(t->if_lock)); 2031da177e4SLinus Torvalds #define if_unlock(t) spin_unlock(&(t->if_lock)); 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */ 2061da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955 207d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen" 208d50a6b56SStephen Hemminger #define PGCTRL "pgctrl" 209d50a6b56SStephen Hemminger static struct proc_dir_entry *pg_proc_dir = NULL; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds #define MAX_CFLOWS 65536 2121da177e4SLinus Torvalds 21334954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4) 21434954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4) 21534954ddcSFrancesco Fondelli 216222f1806SLuiz Capitulino struct flow_state { 217252e3346SAl Viro __be32 cur_daddr; 2181da177e4SLinus Torvalds int count; 219a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 220a553e4a6SJamal Hadi Salim struct xfrm_state *x; 221a553e4a6SJamal Hadi Salim #endif 222007a531bSJamal Hadi Salim __u32 flags; 2231da177e4SLinus Torvalds }; 2241da177e4SLinus Torvalds 225007a531bSJamal Hadi Salim /* flow flag bits */ 226007a531bSJamal Hadi Salim #define F_INIT (1<<0) /* flow has been initialized */ 227007a531bSJamal Hadi Salim 2281da177e4SLinus Torvalds struct pktgen_dev { 2291da177e4SLinus Torvalds /* 2301da177e4SLinus Torvalds * Try to keep frequent/infrequent used vars. separated. 2311da177e4SLinus Torvalds */ 23239df232fSStephen Hemminger struct proc_dir_entry *entry; /* proc file */ 2331da177e4SLinus Torvalds struct pktgen_thread *pg_thread;/* the owner */ 234c26a8016SLuiz Capitulino struct list_head list; /* Used for chaining in the thread's run-queue */ 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds int running; /* if this changes to false, the test will stop */ 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds /* If min != max, then we will either do a linear iteration, or 2391da177e4SLinus Torvalds * we will do a random selection from within the range. 2401da177e4SLinus Torvalds */ 2411da177e4SLinus Torvalds __u32 flags; 24295ed63f7SArthur Kepner int removal_mark; /* non-zero => the device is marked for 24395ed63f7SArthur Kepner * removal by worker thread */ 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds int min_pkt_size; /* = ETH_ZLEN; */ 2461da177e4SLinus Torvalds int max_pkt_size; /* = ETH_ZLEN; */ 24716dab72fSJamal Hadi Salim int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */ 2481da177e4SLinus Torvalds int nfrags; 2491da177e4SLinus Torvalds __u32 delay_us; /* Default delay */ 2501da177e4SLinus Torvalds __u32 delay_ns; 2511da177e4SLinus Torvalds __u64 count; /* Default No packets to send */ 2521da177e4SLinus Torvalds __u64 sofar; /* How many pkts we've sent so far */ 2531da177e4SLinus Torvalds __u64 tx_bytes; /* How many bytes we've transmitted */ 2541da177e4SLinus Torvalds __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */ 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* runtime counters relating to clone_skb */ 2571da177e4SLinus Torvalds __u64 next_tx_us; /* timestamp of when to tx next */ 2581da177e4SLinus Torvalds __u32 next_tx_ns; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds __u64 allocated_skbs; 2611da177e4SLinus Torvalds __u32 clone_count; 2621da177e4SLinus Torvalds int last_ok; /* Was last skb sent? 2631da177e4SLinus Torvalds * Or a failed transmit of some sort? This will keep 2641da177e4SLinus Torvalds * sequence numbers in order, for example. 2651da177e4SLinus Torvalds */ 2661da177e4SLinus Torvalds __u64 started_at; /* micro-seconds */ 2671da177e4SLinus Torvalds __u64 stopped_at; /* micro-seconds */ 2681da177e4SLinus Torvalds __u64 idle_acc; /* micro-seconds */ 2691da177e4SLinus Torvalds __u32 seq_num; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds int clone_skb; /* Use multiple SKBs during packet gen. If this number 272b4099fabSStephen Hemminger * is greater than 1, then that many copies of the same 2731da177e4SLinus Torvalds * packet will be sent before a new packet is allocated. 2741da177e4SLinus Torvalds * For instance, if you want to send 1024 identical packets 2751da177e4SLinus Torvalds * before creating a new packet, set clone_skb to 1024. 2761da177e4SLinus Torvalds */ 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2791da177e4SLinus Torvalds char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2801da177e4SLinus Torvalds char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2811da177e4SLinus Torvalds char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds struct in6_addr in6_saddr; 2841da177e4SLinus Torvalds struct in6_addr in6_daddr; 2851da177e4SLinus Torvalds struct in6_addr cur_in6_daddr; 2861da177e4SLinus Torvalds struct in6_addr cur_in6_saddr; 2871da177e4SLinus Torvalds /* For ranges */ 2881da177e4SLinus Torvalds struct in6_addr min_in6_daddr; 2891da177e4SLinus Torvalds struct in6_addr max_in6_daddr; 2901da177e4SLinus Torvalds struct in6_addr min_in6_saddr; 2911da177e4SLinus Torvalds struct in6_addr max_in6_saddr; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds /* If we're doing ranges, random or incremental, then this 2941da177e4SLinus Torvalds * defines the min/max for those ranges. 2951da177e4SLinus Torvalds */ 296252e3346SAl Viro __be32 saddr_min; /* inclusive, source IP address */ 297252e3346SAl Viro __be32 saddr_max; /* exclusive, source IP address */ 298252e3346SAl Viro __be32 daddr_min; /* inclusive, dest IP address */ 299252e3346SAl Viro __be32 daddr_max; /* exclusive, dest IP address */ 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds __u16 udp_src_min; /* inclusive, source UDP port */ 3021da177e4SLinus Torvalds __u16 udp_src_max; /* exclusive, source UDP port */ 3031da177e4SLinus Torvalds __u16 udp_dst_min; /* inclusive, dest UDP port */ 3041da177e4SLinus Torvalds __u16 udp_dst_max; /* exclusive, dest UDP port */ 3051da177e4SLinus Torvalds 3061ca7768cSFrancesco Fondelli /* DSCP + ECN */ 3071ca7768cSFrancesco Fondelli __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */ 3081ca7768cSFrancesco Fondelli __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */ 3091ca7768cSFrancesco Fondelli 310ca6549afSSteven Whitehouse /* MPLS */ 311ca6549afSSteven Whitehouse unsigned nr_labels; /* Depth of stack, 0 = no MPLS */ 312ca6549afSSteven Whitehouse __be32 labels[MAX_MPLS_LABELS]; 313ca6549afSSteven Whitehouse 31434954ddcSFrancesco Fondelli /* VLAN/SVLAN (802.1Q/Q-in-Q) */ 31534954ddcSFrancesco Fondelli __u8 vlan_p; 31634954ddcSFrancesco Fondelli __u8 vlan_cfi; 31734954ddcSFrancesco Fondelli __u16 vlan_id; /* 0xffff means no vlan tag */ 31834954ddcSFrancesco Fondelli 31934954ddcSFrancesco Fondelli __u8 svlan_p; 32034954ddcSFrancesco Fondelli __u8 svlan_cfi; 32134954ddcSFrancesco Fondelli __u16 svlan_id; /* 0xffff means no svlan tag */ 32234954ddcSFrancesco Fondelli 3231da177e4SLinus Torvalds __u32 src_mac_count; /* How many MACs to iterate through */ 3241da177e4SLinus Torvalds __u32 dst_mac_count; /* How many MACs to iterate through */ 3251da177e4SLinus Torvalds 326f404e9a6SKris Katterjohn unsigned char dst_mac[ETH_ALEN]; 327f404e9a6SKris Katterjohn unsigned char src_mac[ETH_ALEN]; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds __u32 cur_dst_mac_offset; 3301da177e4SLinus Torvalds __u32 cur_src_mac_offset; 331252e3346SAl Viro __be32 cur_saddr; 332252e3346SAl Viro __be32 cur_daddr; 3331da177e4SLinus Torvalds __u16 cur_udp_dst; 3341da177e4SLinus Torvalds __u16 cur_udp_src; 33545b270f8SRobert Olsson __u16 cur_queue_map; 3361da177e4SLinus Torvalds __u32 cur_pkt_size; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds __u8 hh[14]; 3391da177e4SLinus Torvalds /* = { 3401da177e4SLinus Torvalds 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB, 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds We fill in SRC address later 3431da177e4SLinus Torvalds 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 3441da177e4SLinus Torvalds 0x08, 0x00 3451da177e4SLinus Torvalds }; 3461da177e4SLinus Torvalds */ 3471da177e4SLinus Torvalds __u16 pad; /* pad out the hh struct to an even 16 bytes */ 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we 3501da177e4SLinus Torvalds * are transmitting the same one multiple times 3511da177e4SLinus Torvalds */ 3521da177e4SLinus Torvalds struct net_device *odev; /* The out-going device. Note that the device should 3531da177e4SLinus Torvalds * have it's pg_info pointer pointing back to this 3541da177e4SLinus Torvalds * device. This will be set when the user specifies 3551da177e4SLinus Torvalds * the out-going device name (not when the inject is 3561da177e4SLinus Torvalds * started as it used to do.) 3571da177e4SLinus Torvalds */ 3581da177e4SLinus Torvalds struct flow_state *flows; 3591da177e4SLinus Torvalds unsigned cflows; /* Concurrent flows (config) */ 3601da177e4SLinus Torvalds unsigned lflow; /* Flow length (config) */ 3611da177e4SLinus Torvalds unsigned nflows; /* accumulated flows (stats) */ 362007a531bSJamal Hadi Salim unsigned curfl; /* current sequenced flow (state)*/ 36345b270f8SRobert Olsson 36445b270f8SRobert Olsson u16 queue_map_min; 36545b270f8SRobert Olsson u16 queue_map_max; 36645b270f8SRobert Olsson 367a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 368a553e4a6SJamal Hadi Salim __u8 ipsmode; /* IPSEC mode (config) */ 369a553e4a6SJamal Hadi Salim __u8 ipsproto; /* IPSEC type (config) */ 370a553e4a6SJamal Hadi Salim #endif 37139df232fSStephen Hemminger char result[512]; 3721da177e4SLinus Torvalds }; 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds struct pktgen_hdr { 375252e3346SAl Viro __be32 pgh_magic; 376252e3346SAl Viro __be32 seq_num; 377252e3346SAl Viro __be32 tv_sec; 378252e3346SAl Viro __be32 tv_usec; 3791da177e4SLinus Torvalds }; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds struct pktgen_thread { 3821da177e4SLinus Torvalds spinlock_t if_lock; 383c26a8016SLuiz Capitulino struct list_head if_list; /* All device here */ 384cdcdbe0bSLuiz Capitulino struct list_head th_list; 385ee74baa7SDavid S. Miller struct task_struct *tsk; 3861da177e4SLinus Torvalds char result[512]; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* Field for thread to receive "posted" events terminate, stop ifs etc. */ 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds u32 control; 3911da177e4SLinus Torvalds int cpu; 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds wait_queue_head_t queue; 394d3ede327SDenis V. Lunev struct completion start_done; 3951da177e4SLinus Torvalds }; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds #define REMOVE 1 3981da177e4SLinus Torvalds #define FIND 0 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /** Convert to micro-seconds */ 4011da177e4SLinus Torvalds static inline __u64 tv_to_us(const struct timeval *tv) 4021da177e4SLinus Torvalds { 4031da177e4SLinus Torvalds __u64 us = tv->tv_usec; 4041da177e4SLinus Torvalds us += (__u64) tv->tv_sec * (__u64) 1000000; 4051da177e4SLinus Torvalds return us; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4086db105dbSIlpo Järvinen static __u64 getCurUs(void) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds struct timeval tv; 4111da177e4SLinus Torvalds do_gettimeofday(&tv); 4121da177e4SLinus Torvalds return tv_to_us(&tv); 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /* old include end */ 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds static char version[] __initdata = VERSION; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i); 4201da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname); 421222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, 422222f1806SLuiz Capitulino const char *ifname); 4231da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *); 4241da177e4SLinus Torvalds static void pktgen_run_all_threads(void); 4251da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void); 4261da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev); 4271da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t); 4281da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev); 42939df232fSStephen Hemminger 4301da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]); 4311da177e4SLinus Torvalds static unsigned int fmt_ip6(char *s, const char ip[16]); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds /* Module parameters, defaults. */ 4341da177e4SLinus Torvalds static int pg_count_d = 1000; /* 1000 pkts by default */ 435f34fbb97SJaco Kroon static int pg_delay_d; 436f34fbb97SJaco Kroon static int pg_clone_skb_d; 437f34fbb97SJaco Kroon static int debug; 4381da177e4SLinus Torvalds 439222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock); 440cdcdbe0bSLuiz Capitulino static LIST_HEAD(pktgen_threads); 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = { 4431da177e4SLinus Torvalds .notifier_call = pktgen_device_event, 4441da177e4SLinus Torvalds }; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds /* 4471da177e4SLinus Torvalds * /proc handling functions 4481da177e4SLinus Torvalds * 4491da177e4SLinus Torvalds */ 4501da177e4SLinus Torvalds 451d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v) 452d50a6b56SStephen Hemminger { 453d50a6b56SStephen Hemminger seq_puts(seq, VERSION); 454d50a6b56SStephen Hemminger return 0; 455d50a6b56SStephen Hemminger } 4561da177e4SLinus Torvalds 457d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user * buf, 4581da177e4SLinus Torvalds size_t count, loff_t * ppos) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds int err = 0; 461d50a6b56SStephen Hemminger char data[128]; 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) { 4641da177e4SLinus Torvalds err = -EPERM; 4651da177e4SLinus Torvalds goto out; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 468d50a6b56SStephen Hemminger if (count > sizeof(data)) 469d50a6b56SStephen Hemminger count = sizeof(data); 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds if (copy_from_user(data, buf, count)) { 4721da177e4SLinus Torvalds err = -EFAULT; 473d50a6b56SStephen Hemminger goto out; 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds data[count - 1] = 0; /* Make string */ 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds if (!strcmp(data, "stop")) 4781da177e4SLinus Torvalds pktgen_stop_all_threads_ifs(); 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds else if (!strcmp(data, "start")) 4811da177e4SLinus Torvalds pktgen_run_all_threads(); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds else 48425a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: Unknown command: %s\n", data); 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds err = count; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds out: 4891da177e4SLinus Torvalds return err; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 492d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file) 4931da177e4SLinus Torvalds { 494d50a6b56SStephen Hemminger return single_open(file, pgctrl_show, PDE(inode)->data); 495d50a6b56SStephen Hemminger } 496d50a6b56SStephen Hemminger 4979a32144eSArjan van de Ven static const struct file_operations pktgen_fops = { 498d50a6b56SStephen Hemminger .owner = THIS_MODULE, 499d50a6b56SStephen Hemminger .open = pgctrl_open, 500d50a6b56SStephen Hemminger .read = seq_read, 501d50a6b56SStephen Hemminger .llseek = seq_lseek, 502d50a6b56SStephen Hemminger .write = pgctrl_write, 503d50a6b56SStephen Hemminger .release = single_release, 504d50a6b56SStephen Hemminger }; 505d50a6b56SStephen Hemminger 506d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v) 507d50a6b56SStephen Hemminger { 508d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 5091da177e4SLinus Torvalds __u64 sa; 5101da177e4SLinus Torvalds __u64 stopped; 5111da177e4SLinus Torvalds __u64 now = getCurUs(); 5121da177e4SLinus Torvalds 513222f1806SLuiz Capitulino seq_printf(seq, 514222f1806SLuiz Capitulino "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n", 515222f1806SLuiz Capitulino (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size, 516222f1806SLuiz Capitulino pkt_dev->max_pkt_size); 5171da177e4SLinus Torvalds 518222f1806SLuiz Capitulino seq_printf(seq, 519222f1806SLuiz Capitulino " frags: %d delay: %u clone_skb: %d ifname: %s\n", 520222f1806SLuiz Capitulino pkt_dev->nfrags, 521222f1806SLuiz Capitulino 1000 * pkt_dev->delay_us + pkt_dev->delay_ns, 52239df232fSStephen Hemminger pkt_dev->clone_skb, pkt_dev->odev->name); 5231da177e4SLinus Torvalds 524222f1806SLuiz Capitulino seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows, 525222f1806SLuiz Capitulino pkt_dev->lflow); 5261da177e4SLinus Torvalds 52745b270f8SRobert Olsson seq_printf(seq, 52845b270f8SRobert Olsson " queue_map_min: %u queue_map_max: %u\n", 52945b270f8SRobert Olsson pkt_dev->queue_map_min, 53045b270f8SRobert Olsson pkt_dev->queue_map_max); 53145b270f8SRobert Olsson 5321da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 5331da177e4SLinus Torvalds char b1[128], b2[128], b3[128]; 5341da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr); 5351da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr); 5361da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr); 537222f1806SLuiz Capitulino seq_printf(seq, 538222f1806SLuiz Capitulino " saddr: %s min_saddr: %s max_saddr: %s\n", b1, 539222f1806SLuiz Capitulino b2, b3); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr); 5421da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr); 5431da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr); 544222f1806SLuiz Capitulino seq_printf(seq, 545222f1806SLuiz Capitulino " daddr: %s min_daddr: %s max_daddr: %s\n", b1, 546222f1806SLuiz Capitulino b2, b3); 5471da177e4SLinus Torvalds 548222f1806SLuiz Capitulino } else 549222f1806SLuiz Capitulino seq_printf(seq, 550222f1806SLuiz Capitulino " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n", 551222f1806SLuiz Capitulino pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min, 552222f1806SLuiz Capitulino pkt_dev->src_max); 5531da177e4SLinus Torvalds 554d50a6b56SStephen Hemminger seq_puts(seq, " src_mac: "); 5551da177e4SLinus Torvalds 556*e174961cSJohannes Berg seq_printf(seq, "%pM ", 557*e174961cSJohannes Berg is_zero_ether_addr(pkt_dev->src_mac) ? 558*e174961cSJohannes Berg pkt_dev->odev->dev_addr : pkt_dev->src_mac); 5591da177e4SLinus Torvalds 560d50a6b56SStephen Hemminger seq_printf(seq, "dst_mac: "); 561*e174961cSJohannes Berg seq_printf(seq, "%pM\n", pkt_dev->dst_mac); 5621da177e4SLinus Torvalds 563222f1806SLuiz Capitulino seq_printf(seq, 564222f1806SLuiz Capitulino " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n", 565222f1806SLuiz Capitulino pkt_dev->udp_src_min, pkt_dev->udp_src_max, 566222f1806SLuiz Capitulino pkt_dev->udp_dst_min, pkt_dev->udp_dst_max); 5671da177e4SLinus Torvalds 568222f1806SLuiz Capitulino seq_printf(seq, 569ca6549afSSteven Whitehouse " src_mac_count: %d dst_mac_count: %d\n", 5701da177e4SLinus Torvalds pkt_dev->src_mac_count, pkt_dev->dst_mac_count); 5711da177e4SLinus Torvalds 572ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) { 573ca6549afSSteven Whitehouse unsigned i; 574ca6549afSSteven Whitehouse seq_printf(seq, " mpls: "); 575ca6549afSSteven Whitehouse for (i = 0; i < pkt_dev->nr_labels; i++) 576ca6549afSSteven Whitehouse seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]), 577ca6549afSSteven Whitehouse i == pkt_dev->nr_labels-1 ? "\n" : ", "); 578ca6549afSSteven Whitehouse } 579ca6549afSSteven Whitehouse 58034954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) { 58134954ddcSFrancesco Fondelli seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n", 58234954ddcSFrancesco Fondelli pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi); 58334954ddcSFrancesco Fondelli } 58434954ddcSFrancesco Fondelli 58534954ddcSFrancesco Fondelli if (pkt_dev->svlan_id != 0xffff) { 58634954ddcSFrancesco Fondelli seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n", 58734954ddcSFrancesco Fondelli pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi); 58834954ddcSFrancesco Fondelli } 58934954ddcSFrancesco Fondelli 5901ca7768cSFrancesco Fondelli if (pkt_dev->tos) { 5911ca7768cSFrancesco Fondelli seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos); 5921ca7768cSFrancesco Fondelli } 5931ca7768cSFrancesco Fondelli 5941ca7768cSFrancesco Fondelli if (pkt_dev->traffic_class) { 5951ca7768cSFrancesco Fondelli seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class); 5961ca7768cSFrancesco Fondelli } 5971ca7768cSFrancesco Fondelli 598ca6549afSSteven Whitehouse seq_printf(seq, " Flags: "); 599ca6549afSSteven Whitehouse 6001da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) 601d50a6b56SStephen Hemminger seq_printf(seq, "IPV6 "); 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 604d50a6b56SStephen Hemminger seq_printf(seq, "IPSRC_RND "); 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) 607d50a6b56SStephen Hemminger seq_printf(seq, "IPDST_RND "); 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) 610d50a6b56SStephen Hemminger seq_printf(seq, "TXSIZE_RND "); 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 613d50a6b56SStephen Hemminger seq_printf(seq, "UDPSRC_RND "); 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) 616d50a6b56SStephen Hemminger seq_printf(seq, "UDPDST_RND "); 6171da177e4SLinus Torvalds 618ca6549afSSteven Whitehouse if (pkt_dev->flags & F_MPLS_RND) 619ca6549afSSteven Whitehouse seq_printf(seq, "MPLS_RND "); 620ca6549afSSteven Whitehouse 62145b270f8SRobert Olsson if (pkt_dev->flags & F_QUEUE_MAP_RND) 62245b270f8SRobert Olsson seq_printf(seq, "QUEUE_MAP_RND "); 62345b270f8SRobert Olsson 624e6fce5b9SRobert Olsson if (pkt_dev->flags & F_QUEUE_MAP_CPU) 625e6fce5b9SRobert Olsson seq_printf(seq, "QUEUE_MAP_CPU "); 626e6fce5b9SRobert Olsson 627007a531bSJamal Hadi Salim if (pkt_dev->cflows) { 628007a531bSJamal Hadi Salim if (pkt_dev->flags & F_FLOW_SEQ) 629007a531bSJamal Hadi Salim seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/ 630007a531bSJamal Hadi Salim else 631007a531bSJamal Hadi Salim seq_printf(seq, "FLOW_RND "); 632007a531bSJamal Hadi Salim } 633007a531bSJamal Hadi Salim 634a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 635a553e4a6SJamal Hadi Salim if (pkt_dev->flags & F_IPSEC_ON) 636a553e4a6SJamal Hadi Salim seq_printf(seq, "IPSEC "); 637a553e4a6SJamal Hadi Salim #endif 638a553e4a6SJamal Hadi Salim 6391da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 640d50a6b56SStephen Hemminger seq_printf(seq, "MACSRC_RND "); 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 643d50a6b56SStephen Hemminger seq_printf(seq, "MACDST_RND "); 6441da177e4SLinus Torvalds 64534954ddcSFrancesco Fondelli if (pkt_dev->flags & F_VID_RND) 64634954ddcSFrancesco Fondelli seq_printf(seq, "VID_RND "); 64734954ddcSFrancesco Fondelli 64834954ddcSFrancesco Fondelli if (pkt_dev->flags & F_SVID_RND) 64934954ddcSFrancesco Fondelli seq_printf(seq, "SVID_RND "); 65034954ddcSFrancesco Fondelli 651d50a6b56SStephen Hemminger seq_puts(seq, "\n"); 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds sa = pkt_dev->started_at; 6541da177e4SLinus Torvalds stopped = pkt_dev->stopped_at; 6551da177e4SLinus Torvalds if (pkt_dev->running) 6561da177e4SLinus Torvalds stopped = now; /* not really stopped, more like last-running-at */ 6571da177e4SLinus Torvalds 658222f1806SLuiz Capitulino seq_printf(seq, 659222f1806SLuiz Capitulino "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n", 6601da177e4SLinus Torvalds (unsigned long long)pkt_dev->sofar, 661222f1806SLuiz Capitulino (unsigned long long)pkt_dev->errors, (unsigned long long)sa, 6621da177e4SLinus Torvalds (unsigned long long)stopped, 6631da177e4SLinus Torvalds (unsigned long long)pkt_dev->idle_acc); 6641da177e4SLinus Torvalds 665222f1806SLuiz Capitulino seq_printf(seq, 666222f1806SLuiz Capitulino " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n", 667d50a6b56SStephen Hemminger pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset, 668d50a6b56SStephen Hemminger pkt_dev->cur_src_mac_offset); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 6711da177e4SLinus Torvalds char b1[128], b2[128]; 6721da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr); 6731da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr); 674d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1); 675222f1806SLuiz Capitulino } else 676d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n", 6771da177e4SLinus Torvalds pkt_dev->cur_saddr, pkt_dev->cur_daddr); 6781da177e4SLinus Torvalds 679d50a6b56SStephen Hemminger seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n", 6801da177e4SLinus Torvalds pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src); 6811da177e4SLinus Torvalds 68245b270f8SRobert Olsson seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map); 68345b270f8SRobert Olsson 684d50a6b56SStephen Hemminger seq_printf(seq, " flows: %u\n", pkt_dev->nflows); 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds if (pkt_dev->result[0]) 687d50a6b56SStephen Hemminger seq_printf(seq, "Result: %s\n", pkt_dev->result); 6881da177e4SLinus Torvalds else 689d50a6b56SStephen Hemminger seq_printf(seq, "Result: Idle\n"); 6901da177e4SLinus Torvalds 691d50a6b56SStephen Hemminger return 0; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 694ca6549afSSteven Whitehouse 6951ca7768cSFrancesco Fondelli static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num) 696ca6549afSSteven Whitehouse { 697ca6549afSSteven Whitehouse int i = 0; 698ca6549afSSteven Whitehouse *num = 0; 699ca6549afSSteven Whitehouse 7001ca7768cSFrancesco Fondelli for (; i < maxlen; i++) { 701ca6549afSSteven Whitehouse char c; 702ca6549afSSteven Whitehouse *num <<= 4; 703ca6549afSSteven Whitehouse if (get_user(c, &user_buffer[i])) 704ca6549afSSteven Whitehouse return -EFAULT; 705ca6549afSSteven Whitehouse if ((c >= '0') && (c <= '9')) 706ca6549afSSteven Whitehouse *num |= c - '0'; 707ca6549afSSteven Whitehouse else if ((c >= 'a') && (c <= 'f')) 708ca6549afSSteven Whitehouse *num |= c - 'a' + 10; 709ca6549afSSteven Whitehouse else if ((c >= 'A') && (c <= 'F')) 710ca6549afSSteven Whitehouse *num |= c - 'A' + 10; 711ca6549afSSteven Whitehouse else 712ca6549afSSteven Whitehouse break; 713ca6549afSSteven Whitehouse } 714ca6549afSSteven Whitehouse return i; 715ca6549afSSteven Whitehouse } 716ca6549afSSteven Whitehouse 717222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer, 718222f1806SLuiz Capitulino unsigned int maxlen) 7191da177e4SLinus Torvalds { 7201da177e4SLinus Torvalds int i; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds for (i = 0; i < maxlen; i++) { 7231da177e4SLinus Torvalds char c; 7241da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 7251da177e4SLinus Torvalds return -EFAULT; 7261da177e4SLinus Torvalds switch (c) { 7271da177e4SLinus Torvalds case '\"': 7281da177e4SLinus Torvalds case '\n': 7291da177e4SLinus Torvalds case '\r': 7301da177e4SLinus Torvalds case '\t': 7311da177e4SLinus Torvalds case ' ': 7321da177e4SLinus Torvalds case '=': 7331da177e4SLinus Torvalds break; 7341da177e4SLinus Torvalds default: 7351da177e4SLinus Torvalds goto done; 7363ff50b79SStephen Hemminger } 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds done: 7391da177e4SLinus Torvalds return i; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 742222f1806SLuiz Capitulino static unsigned long num_arg(const char __user * user_buffer, 743222f1806SLuiz Capitulino unsigned long maxlen, unsigned long *num) 7441da177e4SLinus Torvalds { 7451da177e4SLinus Torvalds int i = 0; 7461da177e4SLinus Torvalds *num = 0; 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds for (; i < maxlen; i++) { 7491da177e4SLinus Torvalds char c; 7501da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 7511da177e4SLinus Torvalds return -EFAULT; 7521da177e4SLinus Torvalds if ((c >= '0') && (c <= '9')) { 7531da177e4SLinus Torvalds *num *= 10; 7541da177e4SLinus Torvalds *num += c - '0'; 7551da177e4SLinus Torvalds } else 7561da177e4SLinus Torvalds break; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds return i; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen) 7621da177e4SLinus Torvalds { 7631da177e4SLinus Torvalds int i = 0; 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds for (; i < maxlen; i++) { 7661da177e4SLinus Torvalds char c; 7671da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 7681da177e4SLinus Torvalds return -EFAULT; 7691da177e4SLinus Torvalds switch (c) { 7701da177e4SLinus Torvalds case '\"': 7711da177e4SLinus Torvalds case '\n': 7721da177e4SLinus Torvalds case '\r': 7731da177e4SLinus Torvalds case '\t': 7741da177e4SLinus Torvalds case ' ': 7751da177e4SLinus Torvalds goto done_str; 7761da177e4SLinus Torvalds break; 7771da177e4SLinus Torvalds default: 7781da177e4SLinus Torvalds break; 7793ff50b79SStephen Hemminger } 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds done_str: 7821da177e4SLinus Torvalds return i; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 785ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev) 786ca6549afSSteven Whitehouse { 787ca6549afSSteven Whitehouse unsigned n = 0; 788ca6549afSSteven Whitehouse char c; 789ca6549afSSteven Whitehouse ssize_t i = 0; 790ca6549afSSteven Whitehouse int len; 791ca6549afSSteven Whitehouse 792ca6549afSSteven Whitehouse pkt_dev->nr_labels = 0; 793ca6549afSSteven Whitehouse do { 794ca6549afSSteven Whitehouse __u32 tmp; 7951ca7768cSFrancesco Fondelli len = hex32_arg(&buffer[i], 8, &tmp); 796ca6549afSSteven Whitehouse if (len <= 0) 797ca6549afSSteven Whitehouse return len; 798ca6549afSSteven Whitehouse pkt_dev->labels[n] = htonl(tmp); 799ca6549afSSteven Whitehouse if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM) 800ca6549afSSteven Whitehouse pkt_dev->flags |= F_MPLS_RND; 801ca6549afSSteven Whitehouse i += len; 802ca6549afSSteven Whitehouse if (get_user(c, &buffer[i])) 803ca6549afSSteven Whitehouse return -EFAULT; 804ca6549afSSteven Whitehouse i++; 805ca6549afSSteven Whitehouse n++; 806ca6549afSSteven Whitehouse if (n >= MAX_MPLS_LABELS) 807ca6549afSSteven Whitehouse return -E2BIG; 808ca6549afSSteven Whitehouse } while (c == ','); 809ca6549afSSteven Whitehouse 810ca6549afSSteven Whitehouse pkt_dev->nr_labels = n; 811ca6549afSSteven Whitehouse return i; 812ca6549afSSteven Whitehouse } 813ca6549afSSteven Whitehouse 814222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file, 815222f1806SLuiz Capitulino const char __user * user_buffer, size_t count, 816222f1806SLuiz Capitulino loff_t * offset) 8171da177e4SLinus Torvalds { 818d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *)file->private_data; 819d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 8201da177e4SLinus Torvalds int i = 0, max, len; 8211da177e4SLinus Torvalds char name[16], valstr[32]; 8221da177e4SLinus Torvalds unsigned long value = 0; 8231da177e4SLinus Torvalds char *pg_result = NULL; 8241da177e4SLinus Torvalds int tmp = 0; 8251da177e4SLinus Torvalds char buf[128]; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds pg_result = &(pkt_dev->result[0]); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds if (count < 1) { 83025a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: wrong command format\n"); 8311da177e4SLinus Torvalds return -EINVAL; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds max = count - i; 8351da177e4SLinus Torvalds tmp = count_trail_chars(&user_buffer[i], max); 8361da177e4SLinus Torvalds if (tmp < 0) { 83725a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: illegal format\n"); 8381da177e4SLinus Torvalds return tmp; 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds i += tmp; 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds /* Read variable name */ 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 845222f1806SLuiz Capitulino if (len < 0) { 846222f1806SLuiz Capitulino return len; 847222f1806SLuiz Capitulino } 8481da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 8491da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len)) 8501da177e4SLinus Torvalds return -EFAULT; 8511da177e4SLinus Torvalds i += len; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds max = count - i; 8541da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 8551da177e4SLinus Torvalds if (len < 0) 8561da177e4SLinus Torvalds return len; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds i += len; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds if (debug) { 8611da177e4SLinus Torvalds char tb[count + 1]; 8621da177e4SLinus Torvalds if (copy_from_user(tb, user_buffer, count)) 8631da177e4SLinus Torvalds return -EFAULT; 8641da177e4SLinus Torvalds tb[count] = 0; 86525a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name, 866d50a6b56SStephen Hemminger (unsigned long)count, tb); 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds if (!strcmp(name, "min_pkt_size")) { 8701da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 871222f1806SLuiz Capitulino if (len < 0) { 872222f1806SLuiz Capitulino return len; 873222f1806SLuiz Capitulino } 8741da177e4SLinus Torvalds i += len; 8751da177e4SLinus Torvalds if (value < 14 + 20 + 8) 8761da177e4SLinus Torvalds value = 14 + 20 + 8; 8771da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 8781da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 8791da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 8801da177e4SLinus Torvalds } 881222f1806SLuiz Capitulino sprintf(pg_result, "OK: min_pkt_size=%u", 882222f1806SLuiz Capitulino pkt_dev->min_pkt_size); 8831da177e4SLinus Torvalds return count; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds if (!strcmp(name, "max_pkt_size")) { 8871da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 888222f1806SLuiz Capitulino if (len < 0) { 889222f1806SLuiz Capitulino return len; 890222f1806SLuiz Capitulino } 8911da177e4SLinus Torvalds i += len; 8921da177e4SLinus Torvalds if (value < 14 + 20 + 8) 8931da177e4SLinus Torvalds value = 14 + 20 + 8; 8941da177e4SLinus Torvalds if (value != pkt_dev->max_pkt_size) { 8951da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 8961da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 8971da177e4SLinus Torvalds } 898222f1806SLuiz Capitulino sprintf(pg_result, "OK: max_pkt_size=%u", 899222f1806SLuiz Capitulino pkt_dev->max_pkt_size); 9001da177e4SLinus Torvalds return count; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds /* Shortcut for min = max */ 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds if (!strcmp(name, "pkt_size")) { 9061da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 907222f1806SLuiz Capitulino if (len < 0) { 908222f1806SLuiz Capitulino return len; 909222f1806SLuiz Capitulino } 9101da177e4SLinus Torvalds i += len; 9111da177e4SLinus Torvalds if (value < 14 + 20 + 8) 9121da177e4SLinus Torvalds value = 14 + 20 + 8; 9131da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 9141da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 9151da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 9161da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size); 9191da177e4SLinus Torvalds return count; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds if (!strcmp(name, "debug")) { 9231da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 924222f1806SLuiz Capitulino if (len < 0) { 925222f1806SLuiz Capitulino return len; 926222f1806SLuiz Capitulino } 9271da177e4SLinus Torvalds i += len; 9281da177e4SLinus Torvalds debug = value; 9291da177e4SLinus Torvalds sprintf(pg_result, "OK: debug=%u", debug); 9301da177e4SLinus Torvalds return count; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds if (!strcmp(name, "frags")) { 9341da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 935222f1806SLuiz Capitulino if (len < 0) { 936222f1806SLuiz Capitulino return len; 937222f1806SLuiz Capitulino } 9381da177e4SLinus Torvalds i += len; 9391da177e4SLinus Torvalds pkt_dev->nfrags = value; 9401da177e4SLinus Torvalds sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags); 9411da177e4SLinus Torvalds return count; 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds if (!strcmp(name, "delay")) { 9441da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 945222f1806SLuiz Capitulino if (len < 0) { 946222f1806SLuiz Capitulino return len; 947222f1806SLuiz Capitulino } 9481da177e4SLinus Torvalds i += len; 9491da177e4SLinus Torvalds if (value == 0x7FFFFFFF) { 9501da177e4SLinus Torvalds pkt_dev->delay_us = 0x7FFFFFFF; 9511da177e4SLinus Torvalds pkt_dev->delay_ns = 0; 9521da177e4SLinus Torvalds } else { 9531da177e4SLinus Torvalds pkt_dev->delay_us = value / 1000; 9541da177e4SLinus Torvalds pkt_dev->delay_ns = value % 1000; 9551da177e4SLinus Torvalds } 956222f1806SLuiz Capitulino sprintf(pg_result, "OK: delay=%u", 957222f1806SLuiz Capitulino 1000 * pkt_dev->delay_us + pkt_dev->delay_ns); 9581da177e4SLinus Torvalds return count; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds if (!strcmp(name, "udp_src_min")) { 9611da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 962222f1806SLuiz Capitulino if (len < 0) { 963222f1806SLuiz Capitulino return len; 964222f1806SLuiz Capitulino } 9651da177e4SLinus Torvalds i += len; 9661da177e4SLinus Torvalds if (value != pkt_dev->udp_src_min) { 9671da177e4SLinus Torvalds pkt_dev->udp_src_min = value; 9681da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min); 9711da177e4SLinus Torvalds return count; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_min")) { 9741da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 975222f1806SLuiz Capitulino if (len < 0) { 976222f1806SLuiz Capitulino return len; 977222f1806SLuiz Capitulino } 9781da177e4SLinus Torvalds i += len; 9791da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_min) { 9801da177e4SLinus Torvalds pkt_dev->udp_dst_min = value; 9811da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min); 9841da177e4SLinus Torvalds return count; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds if (!strcmp(name, "udp_src_max")) { 9871da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 988222f1806SLuiz Capitulino if (len < 0) { 989222f1806SLuiz Capitulino return len; 990222f1806SLuiz Capitulino } 9911da177e4SLinus Torvalds i += len; 9921da177e4SLinus Torvalds if (value != pkt_dev->udp_src_max) { 9931da177e4SLinus Torvalds pkt_dev->udp_src_max = value; 9941da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max); 9971da177e4SLinus Torvalds return count; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_max")) { 10001da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1001222f1806SLuiz Capitulino if (len < 0) { 1002222f1806SLuiz Capitulino return len; 1003222f1806SLuiz Capitulino } 10041da177e4SLinus Torvalds i += len; 10051da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_max) { 10061da177e4SLinus Torvalds pkt_dev->udp_dst_max = value; 10071da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max); 10101da177e4SLinus Torvalds return count; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds if (!strcmp(name, "clone_skb")) { 10131da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1014222f1806SLuiz Capitulino if (len < 0) { 1015222f1806SLuiz Capitulino return len; 1016222f1806SLuiz Capitulino } 10171da177e4SLinus Torvalds i += len; 10181da177e4SLinus Torvalds pkt_dev->clone_skb = value; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb); 10211da177e4SLinus Torvalds return count; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds if (!strcmp(name, "count")) { 10241da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1025222f1806SLuiz Capitulino if (len < 0) { 1026222f1806SLuiz Capitulino return len; 1027222f1806SLuiz Capitulino } 10281da177e4SLinus Torvalds i += len; 10291da177e4SLinus Torvalds pkt_dev->count = value; 10301da177e4SLinus Torvalds sprintf(pg_result, "OK: count=%llu", 10311da177e4SLinus Torvalds (unsigned long long)pkt_dev->count); 10321da177e4SLinus Torvalds return count; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds if (!strcmp(name, "src_mac_count")) { 10351da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1036222f1806SLuiz Capitulino if (len < 0) { 1037222f1806SLuiz Capitulino return len; 1038222f1806SLuiz Capitulino } 10391da177e4SLinus Torvalds i += len; 10401da177e4SLinus Torvalds if (pkt_dev->src_mac_count != value) { 10411da177e4SLinus Torvalds pkt_dev->src_mac_count = value; 10421da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 10431da177e4SLinus Torvalds } 1044222f1806SLuiz Capitulino sprintf(pg_result, "OK: src_mac_count=%d", 1045222f1806SLuiz Capitulino pkt_dev->src_mac_count); 10461da177e4SLinus Torvalds return count; 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds if (!strcmp(name, "dst_mac_count")) { 10491da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1050222f1806SLuiz Capitulino if (len < 0) { 1051222f1806SLuiz Capitulino return len; 1052222f1806SLuiz Capitulino } 10531da177e4SLinus Torvalds i += len; 10541da177e4SLinus Torvalds if (pkt_dev->dst_mac_count != value) { 10551da177e4SLinus Torvalds pkt_dev->dst_mac_count = value; 10561da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 10571da177e4SLinus Torvalds } 1058222f1806SLuiz Capitulino sprintf(pg_result, "OK: dst_mac_count=%d", 1059222f1806SLuiz Capitulino pkt_dev->dst_mac_count); 10601da177e4SLinus Torvalds return count; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds if (!strcmp(name, "flag")) { 10631da177e4SLinus Torvalds char f[32]; 10641da177e4SLinus Torvalds memset(f, 0, 32); 10651da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 1066222f1806SLuiz Capitulino if (len < 0) { 1067222f1806SLuiz Capitulino return len; 1068222f1806SLuiz Capitulino } 10691da177e4SLinus Torvalds if (copy_from_user(f, &user_buffer[i], len)) 10701da177e4SLinus Torvalds return -EFAULT; 10711da177e4SLinus Torvalds i += len; 10721da177e4SLinus Torvalds if (strcmp(f, "IPSRC_RND") == 0) 10731da177e4SLinus Torvalds pkt_dev->flags |= F_IPSRC_RND; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds else if (strcmp(f, "!IPSRC_RND") == 0) 10761da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPSRC_RND; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds else if (strcmp(f, "TXSIZE_RND") == 0) 10791da177e4SLinus Torvalds pkt_dev->flags |= F_TXSIZE_RND; 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds else if (strcmp(f, "!TXSIZE_RND") == 0) 10821da177e4SLinus Torvalds pkt_dev->flags &= ~F_TXSIZE_RND; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds else if (strcmp(f, "IPDST_RND") == 0) 10851da177e4SLinus Torvalds pkt_dev->flags |= F_IPDST_RND; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds else if (strcmp(f, "!IPDST_RND") == 0) 10881da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPDST_RND; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds else if (strcmp(f, "UDPSRC_RND") == 0) 10911da177e4SLinus Torvalds pkt_dev->flags |= F_UDPSRC_RND; 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds else if (strcmp(f, "!UDPSRC_RND") == 0) 10941da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPSRC_RND; 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds else if (strcmp(f, "UDPDST_RND") == 0) 10971da177e4SLinus Torvalds pkt_dev->flags |= F_UDPDST_RND; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds else if (strcmp(f, "!UDPDST_RND") == 0) 11001da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPDST_RND; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds else if (strcmp(f, "MACSRC_RND") == 0) 11031da177e4SLinus Torvalds pkt_dev->flags |= F_MACSRC_RND; 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds else if (strcmp(f, "!MACSRC_RND") == 0) 11061da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACSRC_RND; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds else if (strcmp(f, "MACDST_RND") == 0) 11091da177e4SLinus Torvalds pkt_dev->flags |= F_MACDST_RND; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds else if (strcmp(f, "!MACDST_RND") == 0) 11121da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACDST_RND; 11131da177e4SLinus Torvalds 1114ca6549afSSteven Whitehouse else if (strcmp(f, "MPLS_RND") == 0) 1115ca6549afSSteven Whitehouse pkt_dev->flags |= F_MPLS_RND; 1116ca6549afSSteven Whitehouse 1117ca6549afSSteven Whitehouse else if (strcmp(f, "!MPLS_RND") == 0) 1118ca6549afSSteven Whitehouse pkt_dev->flags &= ~F_MPLS_RND; 1119ca6549afSSteven Whitehouse 112034954ddcSFrancesco Fondelli else if (strcmp(f, "VID_RND") == 0) 112134954ddcSFrancesco Fondelli pkt_dev->flags |= F_VID_RND; 112234954ddcSFrancesco Fondelli 112334954ddcSFrancesco Fondelli else if (strcmp(f, "!VID_RND") == 0) 112434954ddcSFrancesco Fondelli pkt_dev->flags &= ~F_VID_RND; 112534954ddcSFrancesco Fondelli 112634954ddcSFrancesco Fondelli else if (strcmp(f, "SVID_RND") == 0) 112734954ddcSFrancesco Fondelli pkt_dev->flags |= F_SVID_RND; 112834954ddcSFrancesco Fondelli 112934954ddcSFrancesco Fondelli else if (strcmp(f, "!SVID_RND") == 0) 113034954ddcSFrancesco Fondelli pkt_dev->flags &= ~F_SVID_RND; 113134954ddcSFrancesco Fondelli 1132007a531bSJamal Hadi Salim else if (strcmp(f, "FLOW_SEQ") == 0) 1133007a531bSJamal Hadi Salim pkt_dev->flags |= F_FLOW_SEQ; 1134007a531bSJamal Hadi Salim 113545b270f8SRobert Olsson else if (strcmp(f, "QUEUE_MAP_RND") == 0) 113645b270f8SRobert Olsson pkt_dev->flags |= F_QUEUE_MAP_RND; 113745b270f8SRobert Olsson 113845b270f8SRobert Olsson else if (strcmp(f, "!QUEUE_MAP_RND") == 0) 113945b270f8SRobert Olsson pkt_dev->flags &= ~F_QUEUE_MAP_RND; 1140e6fce5b9SRobert Olsson 1141e6fce5b9SRobert Olsson else if (strcmp(f, "QUEUE_MAP_CPU") == 0) 1142e6fce5b9SRobert Olsson pkt_dev->flags |= F_QUEUE_MAP_CPU; 1143e6fce5b9SRobert Olsson 1144e6fce5b9SRobert Olsson else if (strcmp(f, "!QUEUE_MAP_CPU") == 0) 1145e6fce5b9SRobert Olsson pkt_dev->flags &= ~F_QUEUE_MAP_CPU; 1146a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 1147a553e4a6SJamal Hadi Salim else if (strcmp(f, "IPSEC") == 0) 1148a553e4a6SJamal Hadi Salim pkt_dev->flags |= F_IPSEC_ON; 1149a553e4a6SJamal Hadi Salim #endif 1150a553e4a6SJamal Hadi Salim 11511ca7768cSFrancesco Fondelli else if (strcmp(f, "!IPV6") == 0) 11521ca7768cSFrancesco Fondelli pkt_dev->flags &= ~F_IPV6; 11531ca7768cSFrancesco Fondelli 11541da177e4SLinus Torvalds else { 1155222f1806SLuiz Capitulino sprintf(pg_result, 1156222f1806SLuiz Capitulino "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s", 11571da177e4SLinus Torvalds f, 11581ca7768cSFrancesco Fondelli "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, " 1159a553e4a6SJamal Hadi Salim "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n"); 11601da177e4SLinus Torvalds return count; 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags); 11631da177e4SLinus Torvalds return count; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) { 11661da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1); 1167222f1806SLuiz Capitulino if (len < 0) { 1168222f1806SLuiz Capitulino return len; 1169222f1806SLuiz Capitulino } 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11721da177e4SLinus Torvalds return -EFAULT; 11731da177e4SLinus Torvalds buf[len] = 0; 11741da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_min) != 0) { 11751da177e4SLinus Torvalds memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min)); 11761da177e4SLinus Torvalds strncpy(pkt_dev->dst_min, buf, len); 11771da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 11781da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds if (debug) 118125a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst_min set to: %s\n", 1182222f1806SLuiz Capitulino pkt_dev->dst_min); 11831da177e4SLinus Torvalds i += len; 11841da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min); 11851da177e4SLinus Torvalds return count; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds if (!strcmp(name, "dst_max")) { 11881da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1); 1189222f1806SLuiz Capitulino if (len < 0) { 1190222f1806SLuiz Capitulino return len; 1191222f1806SLuiz Capitulino } 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11941da177e4SLinus Torvalds return -EFAULT; 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds buf[len] = 0; 11971da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_max) != 0) { 11981da177e4SLinus Torvalds memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max)); 11991da177e4SLinus Torvalds strncpy(pkt_dev->dst_max, buf, len); 12001da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 12011da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_max; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds if (debug) 120425a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst_max set to: %s\n", 1205222f1806SLuiz Capitulino pkt_dev->dst_max); 12061da177e4SLinus Torvalds i += len; 12071da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max); 12081da177e4SLinus Torvalds return count; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds if (!strcmp(name, "dst6")) { 12111da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1212222f1806SLuiz Capitulino if (len < 0) 1213222f1806SLuiz Capitulino return len; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12181da177e4SLinus Torvalds return -EFAULT; 12191da177e4SLinus Torvalds buf[len] = 0; 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_daddr.s6_addr); 12221da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr); 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr); 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds if (debug) 122725a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf); 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds i += len; 12301da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6=%s", buf); 12311da177e4SLinus Torvalds return count; 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds if (!strcmp(name, "dst6_min")) { 12341da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1235222f1806SLuiz Capitulino if (len < 0) 1236222f1806SLuiz Capitulino return len; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12411da177e4SLinus Torvalds return -EFAULT; 12421da177e4SLinus Torvalds buf[len] = 0; 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 12451da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 12461da177e4SLinus Torvalds 1247222f1806SLuiz Capitulino ipv6_addr_copy(&pkt_dev->cur_in6_daddr, 1248222f1806SLuiz Capitulino &pkt_dev->min_in6_daddr); 12491da177e4SLinus Torvalds if (debug) 125025a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf); 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds i += len; 12531da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_min=%s", buf); 12541da177e4SLinus Torvalds return count; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds if (!strcmp(name, "dst6_max")) { 12571da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1258222f1806SLuiz Capitulino if (len < 0) 1259222f1806SLuiz Capitulino return len; 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12641da177e4SLinus Torvalds return -EFAULT; 12651da177e4SLinus Torvalds buf[len] = 0; 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 12681da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds if (debug) 127125a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf); 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds i += len; 12741da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_max=%s", buf); 12751da177e4SLinus Torvalds return count; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds if (!strcmp(name, "src6")) { 12781da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1279222f1806SLuiz Capitulino if (len < 0) 1280222f1806SLuiz Capitulino return len; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12851da177e4SLinus Torvalds return -EFAULT; 12861da177e4SLinus Torvalds buf[len] = 0; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_saddr.s6_addr); 12891da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr); 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr); 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds if (debug) 129425a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf); 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds i += len; 12971da177e4SLinus Torvalds sprintf(pg_result, "OK: src6=%s", buf); 12981da177e4SLinus Torvalds return count; 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds if (!strcmp(name, "src_min")) { 13011da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1); 1302222f1806SLuiz Capitulino if (len < 0) { 1303222f1806SLuiz Capitulino return len; 1304222f1806SLuiz Capitulino } 13051da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13061da177e4SLinus Torvalds return -EFAULT; 13071da177e4SLinus Torvalds buf[len] = 0; 13081da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_min) != 0) { 13091da177e4SLinus Torvalds memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min)); 13101da177e4SLinus Torvalds strncpy(pkt_dev->src_min, buf, len); 13111da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 13121da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds if (debug) 131525a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: src_min set to: %s\n", 1316222f1806SLuiz Capitulino pkt_dev->src_min); 13171da177e4SLinus Torvalds i += len; 13181da177e4SLinus Torvalds sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min); 13191da177e4SLinus Torvalds return count; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds if (!strcmp(name, "src_max")) { 13221da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1); 1323222f1806SLuiz Capitulino if (len < 0) { 1324222f1806SLuiz Capitulino return len; 1325222f1806SLuiz Capitulino } 13261da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13271da177e4SLinus Torvalds return -EFAULT; 13281da177e4SLinus Torvalds buf[len] = 0; 13291da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_max) != 0) { 13301da177e4SLinus Torvalds memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max)); 13311da177e4SLinus Torvalds strncpy(pkt_dev->src_max, buf, len); 13321da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 13331da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_max; 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds if (debug) 133625a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: src_max set to: %s\n", 1337222f1806SLuiz Capitulino pkt_dev->src_max); 13381da177e4SLinus Torvalds i += len; 13391da177e4SLinus Torvalds sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max); 13401da177e4SLinus Torvalds return count; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds if (!strcmp(name, "dst_mac")) { 13431da177e4SLinus Torvalds char *v = valstr; 1344f404e9a6SKris Katterjohn unsigned char old_dmac[ETH_ALEN]; 13451da177e4SLinus Torvalds unsigned char *m = pkt_dev->dst_mac; 1346f404e9a6SKris Katterjohn memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN); 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 1349222f1806SLuiz Capitulino if (len < 0) { 1350222f1806SLuiz Capitulino return len; 1351222f1806SLuiz Capitulino } 13521da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 13531da177e4SLinus Torvalds if (copy_from_user(valstr, &user_buffer[i], len)) 13541da177e4SLinus Torvalds return -EFAULT; 13551da177e4SLinus Torvalds i += len; 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) { 13581da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 13591da177e4SLinus Torvalds *m *= 16; 13601da177e4SLinus Torvalds *m += *v - '0'; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 13631da177e4SLinus Torvalds *m *= 16; 13641da177e4SLinus Torvalds *m += *v - 'A' + 10; 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 13671da177e4SLinus Torvalds *m *= 16; 13681da177e4SLinus Torvalds *m += *v - 'a' + 10; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds if (*v == ':') { 13711da177e4SLinus Torvalds m++; 13721da177e4SLinus Torvalds *m = 0; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /* Set up Dest MAC */ 1377f404e9a6SKris Katterjohn if (compare_ether_addr(old_dmac, pkt_dev->dst_mac)) 1378f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds sprintf(pg_result, "OK: dstmac"); 13811da177e4SLinus Torvalds return count; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds if (!strcmp(name, "src_mac")) { 13841da177e4SLinus Torvalds char *v = valstr; 1385ce5d0b47SAdit Ranadive unsigned char old_smac[ETH_ALEN]; 13861da177e4SLinus Torvalds unsigned char *m = pkt_dev->src_mac; 13871da177e4SLinus Torvalds 1388ce5d0b47SAdit Ranadive memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN); 1389ce5d0b47SAdit Ranadive 13901da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 1391222f1806SLuiz Capitulino if (len < 0) { 1392222f1806SLuiz Capitulino return len; 1393222f1806SLuiz Capitulino } 13941da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 13951da177e4SLinus Torvalds if (copy_from_user(valstr, &user_buffer[i], len)) 13961da177e4SLinus Torvalds return -EFAULT; 13971da177e4SLinus Torvalds i += len; 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) { 14001da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 14011da177e4SLinus Torvalds *m *= 16; 14021da177e4SLinus Torvalds *m += *v - '0'; 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 14051da177e4SLinus Torvalds *m *= 16; 14061da177e4SLinus Torvalds *m += *v - 'A' + 10; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 14091da177e4SLinus Torvalds *m *= 16; 14101da177e4SLinus Torvalds *m += *v - 'a' + 10; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds if (*v == ':') { 14131da177e4SLinus Torvalds m++; 14141da177e4SLinus Torvalds *m = 0; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 1418ce5d0b47SAdit Ranadive /* Set up Src MAC */ 1419ce5d0b47SAdit Ranadive if (compare_ether_addr(old_smac, pkt_dev->src_mac)) 1420ce5d0b47SAdit Ranadive memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN); 1421ce5d0b47SAdit Ranadive 14221da177e4SLinus Torvalds sprintf(pg_result, "OK: srcmac"); 14231da177e4SLinus Torvalds return count; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds if (!strcmp(name, "clear_counters")) { 14271da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 14281da177e4SLinus Torvalds sprintf(pg_result, "OK: Clearing counters.\n"); 14291da177e4SLinus Torvalds return count; 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds if (!strcmp(name, "flows")) { 14331da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1434222f1806SLuiz Capitulino if (len < 0) { 1435222f1806SLuiz Capitulino return len; 1436222f1806SLuiz Capitulino } 14371da177e4SLinus Torvalds i += len; 14381da177e4SLinus Torvalds if (value > MAX_CFLOWS) 14391da177e4SLinus Torvalds value = MAX_CFLOWS; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds pkt_dev->cflows = value; 14421da177e4SLinus Torvalds sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows); 14431da177e4SLinus Torvalds return count; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds if (!strcmp(name, "flowlen")) { 14471da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1448222f1806SLuiz Capitulino if (len < 0) { 1449222f1806SLuiz Capitulino return len; 1450222f1806SLuiz Capitulino } 14511da177e4SLinus Torvalds i += len; 14521da177e4SLinus Torvalds pkt_dev->lflow = value; 14531da177e4SLinus Torvalds sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow); 14541da177e4SLinus Torvalds return count; 14551da177e4SLinus Torvalds } 14561da177e4SLinus Torvalds 145745b270f8SRobert Olsson if (!strcmp(name, "queue_map_min")) { 145845b270f8SRobert Olsson len = num_arg(&user_buffer[i], 5, &value); 145945b270f8SRobert Olsson if (len < 0) { 146045b270f8SRobert Olsson return len; 146145b270f8SRobert Olsson } 146245b270f8SRobert Olsson i += len; 146345b270f8SRobert Olsson pkt_dev->queue_map_min = value; 146445b270f8SRobert Olsson sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min); 146545b270f8SRobert Olsson return count; 146645b270f8SRobert Olsson } 146745b270f8SRobert Olsson 146845b270f8SRobert Olsson if (!strcmp(name, "queue_map_max")) { 146945b270f8SRobert Olsson len = num_arg(&user_buffer[i], 5, &value); 147045b270f8SRobert Olsson if (len < 0) { 147145b270f8SRobert Olsson return len; 147245b270f8SRobert Olsson } 147345b270f8SRobert Olsson i += len; 147445b270f8SRobert Olsson pkt_dev->queue_map_max = value; 147545b270f8SRobert Olsson sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max); 147645b270f8SRobert Olsson return count; 147745b270f8SRobert Olsson } 147845b270f8SRobert Olsson 1479ca6549afSSteven Whitehouse if (!strcmp(name, "mpls")) { 1480cfcabdccSStephen Hemminger unsigned n, cnt; 1481cfcabdccSStephen Hemminger 1482ca6549afSSteven Whitehouse len = get_labels(&user_buffer[i], pkt_dev); 1483cfcabdccSStephen Hemminger if (len < 0) 1484cfcabdccSStephen Hemminger return len; 1485ca6549afSSteven Whitehouse i += len; 1486cfcabdccSStephen Hemminger cnt = sprintf(pg_result, "OK: mpls="); 1487ca6549afSSteven Whitehouse for (n = 0; n < pkt_dev->nr_labels; n++) 1488cfcabdccSStephen Hemminger cnt += sprintf(pg_result + cnt, 1489ca6549afSSteven Whitehouse "%08x%s", ntohl(pkt_dev->labels[n]), 1490ca6549afSSteven Whitehouse n == pkt_dev->nr_labels-1 ? "" : ","); 149134954ddcSFrancesco Fondelli 149234954ddcSFrancesco Fondelli if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) { 149334954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ 149434954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 149534954ddcSFrancesco Fondelli 149634954ddcSFrancesco Fondelli if (debug) 149725a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n"); 149834954ddcSFrancesco Fondelli } 149934954ddcSFrancesco Fondelli return count; 150034954ddcSFrancesco Fondelli } 150134954ddcSFrancesco Fondelli 150234954ddcSFrancesco Fondelli if (!strcmp(name, "vlan_id")) { 150334954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 4, &value); 150434954ddcSFrancesco Fondelli if (len < 0) { 150534954ddcSFrancesco Fondelli return len; 150634954ddcSFrancesco Fondelli } 150734954ddcSFrancesco Fondelli i += len; 150834954ddcSFrancesco Fondelli if (value <= 4095) { 150934954ddcSFrancesco Fondelli pkt_dev->vlan_id = value; /* turn on VLAN */ 151034954ddcSFrancesco Fondelli 151134954ddcSFrancesco Fondelli if (debug) 151225a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: VLAN turned on\n"); 151334954ddcSFrancesco Fondelli 151434954ddcSFrancesco Fondelli if (debug && pkt_dev->nr_labels) 151525a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: MPLS auto turned off\n"); 151634954ddcSFrancesco Fondelli 151734954ddcSFrancesco Fondelli pkt_dev->nr_labels = 0; /* turn off MPLS */ 151834954ddcSFrancesco Fondelli sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id); 151934954ddcSFrancesco Fondelli } else { 152034954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ 152134954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 152234954ddcSFrancesco Fondelli 152334954ddcSFrancesco Fondelli if (debug) 152425a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n"); 152534954ddcSFrancesco Fondelli } 152634954ddcSFrancesco Fondelli return count; 152734954ddcSFrancesco Fondelli } 152834954ddcSFrancesco Fondelli 152934954ddcSFrancesco Fondelli if (!strcmp(name, "vlan_p")) { 153034954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 153134954ddcSFrancesco Fondelli if (len < 0) { 153234954ddcSFrancesco Fondelli return len; 153334954ddcSFrancesco Fondelli } 153434954ddcSFrancesco Fondelli i += len; 153534954ddcSFrancesco Fondelli if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) { 153634954ddcSFrancesco Fondelli pkt_dev->vlan_p = value; 153734954ddcSFrancesco Fondelli sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p); 153834954ddcSFrancesco Fondelli } else { 153934954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: vlan_p must be 0-7"); 154034954ddcSFrancesco Fondelli } 154134954ddcSFrancesco Fondelli return count; 154234954ddcSFrancesco Fondelli } 154334954ddcSFrancesco Fondelli 154434954ddcSFrancesco Fondelli if (!strcmp(name, "vlan_cfi")) { 154534954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 154634954ddcSFrancesco Fondelli if (len < 0) { 154734954ddcSFrancesco Fondelli return len; 154834954ddcSFrancesco Fondelli } 154934954ddcSFrancesco Fondelli i += len; 155034954ddcSFrancesco Fondelli if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) { 155134954ddcSFrancesco Fondelli pkt_dev->vlan_cfi = value; 155234954ddcSFrancesco Fondelli sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi); 155334954ddcSFrancesco Fondelli } else { 155434954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: vlan_cfi must be 0-1"); 155534954ddcSFrancesco Fondelli } 155634954ddcSFrancesco Fondelli return count; 155734954ddcSFrancesco Fondelli } 155834954ddcSFrancesco Fondelli 155934954ddcSFrancesco Fondelli if (!strcmp(name, "svlan_id")) { 156034954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 4, &value); 156134954ddcSFrancesco Fondelli if (len < 0) { 156234954ddcSFrancesco Fondelli return len; 156334954ddcSFrancesco Fondelli } 156434954ddcSFrancesco Fondelli i += len; 156534954ddcSFrancesco Fondelli if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) { 156634954ddcSFrancesco Fondelli pkt_dev->svlan_id = value; /* turn on SVLAN */ 156734954ddcSFrancesco Fondelli 156834954ddcSFrancesco Fondelli if (debug) 156925a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: SVLAN turned on\n"); 157034954ddcSFrancesco Fondelli 157134954ddcSFrancesco Fondelli if (debug && pkt_dev->nr_labels) 157225a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: MPLS auto turned off\n"); 157334954ddcSFrancesco Fondelli 157434954ddcSFrancesco Fondelli pkt_dev->nr_labels = 0; /* turn off MPLS */ 157534954ddcSFrancesco Fondelli sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id); 157634954ddcSFrancesco Fondelli } else { 157734954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ 157834954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 157934954ddcSFrancesco Fondelli 158034954ddcSFrancesco Fondelli if (debug) 158125a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n"); 158234954ddcSFrancesco Fondelli } 158334954ddcSFrancesco Fondelli return count; 158434954ddcSFrancesco Fondelli } 158534954ddcSFrancesco Fondelli 158634954ddcSFrancesco Fondelli if (!strcmp(name, "svlan_p")) { 158734954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 158834954ddcSFrancesco Fondelli if (len < 0) { 158934954ddcSFrancesco Fondelli return len; 159034954ddcSFrancesco Fondelli } 159134954ddcSFrancesco Fondelli i += len; 159234954ddcSFrancesco Fondelli if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) { 159334954ddcSFrancesco Fondelli pkt_dev->svlan_p = value; 159434954ddcSFrancesco Fondelli sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p); 159534954ddcSFrancesco Fondelli } else { 159634954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: svlan_p must be 0-7"); 159734954ddcSFrancesco Fondelli } 159834954ddcSFrancesco Fondelli return count; 159934954ddcSFrancesco Fondelli } 160034954ddcSFrancesco Fondelli 160134954ddcSFrancesco Fondelli if (!strcmp(name, "svlan_cfi")) { 160234954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 160334954ddcSFrancesco Fondelli if (len < 0) { 160434954ddcSFrancesco Fondelli return len; 160534954ddcSFrancesco Fondelli } 160634954ddcSFrancesco Fondelli i += len; 160734954ddcSFrancesco Fondelli if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) { 160834954ddcSFrancesco Fondelli pkt_dev->svlan_cfi = value; 160934954ddcSFrancesco Fondelli sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi); 161034954ddcSFrancesco Fondelli } else { 161134954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: svlan_cfi must be 0-1"); 161234954ddcSFrancesco Fondelli } 1613ca6549afSSteven Whitehouse return count; 1614ca6549afSSteven Whitehouse } 1615ca6549afSSteven Whitehouse 16161ca7768cSFrancesco Fondelli if (!strcmp(name, "tos")) { 16171ca7768cSFrancesco Fondelli __u32 tmp_value = 0; 16181ca7768cSFrancesco Fondelli len = hex32_arg(&user_buffer[i], 2, &tmp_value); 16191ca7768cSFrancesco Fondelli if (len < 0) { 16201ca7768cSFrancesco Fondelli return len; 16211ca7768cSFrancesco Fondelli } 16221ca7768cSFrancesco Fondelli i += len; 16231ca7768cSFrancesco Fondelli if (len == 2) { 16241ca7768cSFrancesco Fondelli pkt_dev->tos = tmp_value; 16251ca7768cSFrancesco Fondelli sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos); 16261ca7768cSFrancesco Fondelli } else { 16271ca7768cSFrancesco Fondelli sprintf(pg_result, "ERROR: tos must be 00-ff"); 16281ca7768cSFrancesco Fondelli } 16291ca7768cSFrancesco Fondelli return count; 16301ca7768cSFrancesco Fondelli } 16311ca7768cSFrancesco Fondelli 16321ca7768cSFrancesco Fondelli if (!strcmp(name, "traffic_class")) { 16331ca7768cSFrancesco Fondelli __u32 tmp_value = 0; 16341ca7768cSFrancesco Fondelli len = hex32_arg(&user_buffer[i], 2, &tmp_value); 16351ca7768cSFrancesco Fondelli if (len < 0) { 16361ca7768cSFrancesco Fondelli return len; 16371ca7768cSFrancesco Fondelli } 16381ca7768cSFrancesco Fondelli i += len; 16391ca7768cSFrancesco Fondelli if (len == 2) { 16401ca7768cSFrancesco Fondelli pkt_dev->traffic_class = tmp_value; 16411ca7768cSFrancesco Fondelli sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class); 16421ca7768cSFrancesco Fondelli } else { 16431ca7768cSFrancesco Fondelli sprintf(pg_result, "ERROR: traffic_class must be 00-ff"); 16441ca7768cSFrancesco Fondelli } 16451ca7768cSFrancesco Fondelli return count; 16461ca7768cSFrancesco Fondelli } 16471ca7768cSFrancesco Fondelli 16481da177e4SLinus Torvalds sprintf(pkt_dev->result, "No such parameter \"%s\"", name); 16491da177e4SLinus Torvalds return -EINVAL; 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds 1652d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file) 16531da177e4SLinus Torvalds { 1654d50a6b56SStephen Hemminger return single_open(file, pktgen_if_show, PDE(inode)->data); 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 16579a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = { 1658d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1659d50a6b56SStephen Hemminger .open = pktgen_if_open, 1660d50a6b56SStephen Hemminger .read = seq_read, 1661d50a6b56SStephen Hemminger .llseek = seq_lseek, 1662d50a6b56SStephen Hemminger .write = pktgen_if_write, 1663d50a6b56SStephen Hemminger .release = single_release, 1664d50a6b56SStephen Hemminger }; 1665d50a6b56SStephen Hemminger 1666d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v) 1667d50a6b56SStephen Hemminger { 1668d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 1669c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 1670d50a6b56SStephen Hemminger 1671d50a6b56SStephen Hemminger BUG_ON(!t); 1672d50a6b56SStephen Hemminger 1673d50a6b56SStephen Hemminger seq_printf(seq, "Running: "); 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds if_lock(t); 1676c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 16771da177e4SLinus Torvalds if (pkt_dev->running) 167839df232fSStephen Hemminger seq_printf(seq, "%s ", pkt_dev->odev->name); 16791da177e4SLinus Torvalds 1680d50a6b56SStephen Hemminger seq_printf(seq, "\nStopped: "); 16811da177e4SLinus Torvalds 1682c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 16831da177e4SLinus Torvalds if (!pkt_dev->running) 168439df232fSStephen Hemminger seq_printf(seq, "%s ", pkt_dev->odev->name); 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds if (t->result[0]) 1687d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: %s\n", t->result); 16881da177e4SLinus Torvalds else 1689d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: NA\n"); 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds if_unlock(t); 16921da177e4SLinus Torvalds 1693d50a6b56SStephen Hemminger return 0; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 1696d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file, 1697d50a6b56SStephen Hemminger const char __user * user_buffer, 1698d50a6b56SStephen Hemminger size_t count, loff_t * offset) 16991da177e4SLinus Torvalds { 1700d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *)file->private_data; 1701d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 17021da177e4SLinus Torvalds int i = 0, max, len, ret; 17031da177e4SLinus Torvalds char name[40]; 17041da177e4SLinus Torvalds char *pg_result; 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds if (count < 1) { 17071da177e4SLinus Torvalds // sprintf(pg_result, "Wrong command format"); 17081da177e4SLinus Torvalds return -EINVAL; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds max = count - i; 17121da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 17131da177e4SLinus Torvalds if (len < 0) 17141da177e4SLinus Torvalds return len; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds i += len; 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds /* Read variable name */ 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 17211da177e4SLinus Torvalds if (len < 0) 17221da177e4SLinus Torvalds return len; 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 17251da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len)) 17261da177e4SLinus Torvalds return -EFAULT; 17271da177e4SLinus Torvalds i += len; 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds max = count - i; 17301da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 17311da177e4SLinus Torvalds if (len < 0) 17321da177e4SLinus Torvalds return len; 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds i += len; 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds if (debug) 173725a8b254SDavid S. Miller printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n", 173825a8b254SDavid S. Miller name, (unsigned long)count); 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds if (!t) { 174125a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: No thread\n"); 17421da177e4SLinus Torvalds ret = -EINVAL; 17431da177e4SLinus Torvalds goto out; 17441da177e4SLinus Torvalds } 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds pg_result = &(t->result[0]); 17471da177e4SLinus Torvalds 17481da177e4SLinus Torvalds if (!strcmp(name, "add_device")) { 17491da177e4SLinus Torvalds char f[32]; 17501da177e4SLinus Torvalds memset(f, 0, 32); 17511da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 17521da177e4SLinus Torvalds if (len < 0) { 17531da177e4SLinus Torvalds ret = len; 17541da177e4SLinus Torvalds goto out; 17551da177e4SLinus Torvalds } 17561da177e4SLinus Torvalds if (copy_from_user(f, &user_buffer[i], len)) 17571da177e4SLinus Torvalds return -EFAULT; 17581da177e4SLinus Torvalds i += len; 17596146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 17601da177e4SLinus Torvalds pktgen_add_device(t, f); 17616146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 17621da177e4SLinus Torvalds ret = count; 17631da177e4SLinus Torvalds sprintf(pg_result, "OK: add_device=%s", f); 17641da177e4SLinus Torvalds goto out; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds if (!strcmp(name, "rem_device_all")) { 17686146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 176995ed63f7SArthur Kepner t->control |= T_REMDEVALL; 17706146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 1771121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 17721da177e4SLinus Torvalds ret = count; 17731da177e4SLinus Torvalds sprintf(pg_result, "OK: rem_device_all"); 17741da177e4SLinus Torvalds goto out; 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds if (!strcmp(name, "max_before_softirq")) { 1778b163911fSRobert Olsson sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use"); 17791da177e4SLinus Torvalds ret = count; 17801da177e4SLinus Torvalds goto out; 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds ret = -EINVAL; 17841da177e4SLinus Torvalds out: 17851da177e4SLinus Torvalds return ret; 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds 1788d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file) 17891da177e4SLinus Torvalds { 1790d50a6b56SStephen Hemminger return single_open(file, pktgen_thread_show, PDE(inode)->data); 17911da177e4SLinus Torvalds } 17921da177e4SLinus Torvalds 17939a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = { 1794d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1795d50a6b56SStephen Hemminger .open = pktgen_thread_open, 1796d50a6b56SStephen Hemminger .read = seq_read, 1797d50a6b56SStephen Hemminger .llseek = seq_lseek, 1798d50a6b56SStephen Hemminger .write = pktgen_thread_write, 1799d50a6b56SStephen Hemminger .release = single_release, 1800d50a6b56SStephen Hemminger }; 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds /* Think find or remove for NN */ 18031da177e4SLinus Torvalds static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove) 18041da177e4SLinus Torvalds { 18051da177e4SLinus Torvalds struct pktgen_thread *t; 18061da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 18071da177e4SLinus Torvalds 1808cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) { 18091da177e4SLinus Torvalds pkt_dev = pktgen_find_dev(t, ifname); 18101da177e4SLinus Torvalds if (pkt_dev) { 18111da177e4SLinus Torvalds if (remove) { 18121da177e4SLinus Torvalds if_lock(t); 181395ed63f7SArthur Kepner pkt_dev->removal_mark = 1; 181495ed63f7SArthur Kepner t->control |= T_REMDEV; 18151da177e4SLinus Torvalds if_unlock(t); 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds break; 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds return pkt_dev; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 182395ed63f7SArthur Kepner /* 182495ed63f7SArthur Kepner * mark a device for removal 182595ed63f7SArthur Kepner */ 182639df232fSStephen Hemminger static void pktgen_mark_device(const char *ifname) 18271da177e4SLinus Torvalds { 18281da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 182995ed63f7SArthur Kepner const int max_tries = 10, msec_per_try = 125; 183095ed63f7SArthur Kepner int i = 0; 183195ed63f7SArthur Kepner 18326146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 183325c4e53aSStephen Hemminger pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname); 183495ed63f7SArthur Kepner 183595ed63f7SArthur Kepner while (1) { 183695ed63f7SArthur Kepner 183795ed63f7SArthur Kepner pkt_dev = __pktgen_NN_threads(ifname, REMOVE); 1838222f1806SLuiz Capitulino if (pkt_dev == NULL) 1839222f1806SLuiz Capitulino break; /* success */ 184095ed63f7SArthur Kepner 18416146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 184225c4e53aSStephen Hemminger pr_debug("pktgen: pktgen_mark_device waiting for %s " 184325c4e53aSStephen Hemminger "to disappear....\n", ifname); 184495ed63f7SArthur Kepner schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try)); 18456146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 184695ed63f7SArthur Kepner 184795ed63f7SArthur Kepner if (++i >= max_tries) { 184825a8b254SDavid S. Miller printk(KERN_ERR "pktgen_mark_device: timed out after " 184925a8b254SDavid S. Miller "waiting %d msec for device %s to be removed\n", 185095ed63f7SArthur Kepner msec_per_try * i, ifname); 185195ed63f7SArthur Kepner break; 185295ed63f7SArthur Kepner } 185395ed63f7SArthur Kepner 185495ed63f7SArthur Kepner } 185595ed63f7SArthur Kepner 18566146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 185739df232fSStephen Hemminger } 185895ed63f7SArthur Kepner 185939df232fSStephen Hemminger static void pktgen_change_name(struct net_device *dev) 186039df232fSStephen Hemminger { 186139df232fSStephen Hemminger struct pktgen_thread *t; 186239df232fSStephen Hemminger 186339df232fSStephen Hemminger list_for_each_entry(t, &pktgen_threads, th_list) { 186439df232fSStephen Hemminger struct pktgen_dev *pkt_dev; 186539df232fSStephen Hemminger 186639df232fSStephen Hemminger list_for_each_entry(pkt_dev, &t->if_list, list) { 186739df232fSStephen Hemminger if (pkt_dev->odev != dev) 186839df232fSStephen Hemminger continue; 186939df232fSStephen Hemminger 187039df232fSStephen Hemminger remove_proc_entry(pkt_dev->entry->name, pg_proc_dir); 187139df232fSStephen Hemminger 187239df232fSStephen Hemminger pkt_dev->entry = create_proc_entry(dev->name, 0600, 187339df232fSStephen Hemminger pg_proc_dir); 187439df232fSStephen Hemminger if (!pkt_dev->entry) 187539df232fSStephen Hemminger printk(KERN_ERR "pktgen: can't move proc " 187639df232fSStephen Hemminger " entry for '%s'\n", dev->name); 187739df232fSStephen Hemminger break; 187839df232fSStephen Hemminger } 187939df232fSStephen Hemminger } 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds 1882222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused, 1883222f1806SLuiz Capitulino unsigned long event, void *ptr) 18841da177e4SLinus Torvalds { 188539df232fSStephen Hemminger struct net_device *dev = ptr; 18861da177e4SLinus Torvalds 1887721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net)) 1888e9dc8653SEric W. Biederman return NOTIFY_DONE; 1889e9dc8653SEric W. Biederman 18901da177e4SLinus Torvalds /* It is OK that we do not hold the group lock right now, 18911da177e4SLinus Torvalds * as we run under the RTNL lock. 18921da177e4SLinus Torvalds */ 18931da177e4SLinus Torvalds 18941da177e4SLinus Torvalds switch (event) { 189539df232fSStephen Hemminger case NETDEV_CHANGENAME: 189639df232fSStephen Hemminger pktgen_change_name(dev); 18971da177e4SLinus Torvalds break; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds case NETDEV_UNREGISTER: 190095ed63f7SArthur Kepner pktgen_mark_device(dev->name); 19011da177e4SLinus Torvalds break; 19023ff50b79SStephen Hemminger } 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds return NOTIFY_DONE; 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds 1907e6fce5b9SRobert Olsson static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev, const char *ifname) 1908e6fce5b9SRobert Olsson { 1909e6fce5b9SRobert Olsson char b[IFNAMSIZ+5]; 1910e6fce5b9SRobert Olsson int i = 0; 1911e6fce5b9SRobert Olsson 1912e6fce5b9SRobert Olsson for(i=0; ifname[i] != '@'; i++) { 1913e6fce5b9SRobert Olsson if(i == IFNAMSIZ) 1914e6fce5b9SRobert Olsson break; 1915e6fce5b9SRobert Olsson 1916e6fce5b9SRobert Olsson b[i] = ifname[i]; 1917e6fce5b9SRobert Olsson } 1918e6fce5b9SRobert Olsson b[i] = 0; 1919e6fce5b9SRobert Olsson 1920e6fce5b9SRobert Olsson return dev_get_by_name(&init_net, b); 1921e6fce5b9SRobert Olsson } 1922e6fce5b9SRobert Olsson 1923e6fce5b9SRobert Olsson 19241da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */ 19251da177e4SLinus Torvalds 192639df232fSStephen Hemminger static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname) 1927222f1806SLuiz Capitulino { 19281da177e4SLinus Torvalds struct net_device *odev; 192939df232fSStephen Hemminger int err; 19301da177e4SLinus Torvalds 19311da177e4SLinus Torvalds /* Clean old setups */ 19321da177e4SLinus Torvalds if (pkt_dev->odev) { 19331da177e4SLinus Torvalds dev_put(pkt_dev->odev); 19341da177e4SLinus Torvalds pkt_dev->odev = NULL; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds 1937e6fce5b9SRobert Olsson odev = pktgen_dev_get_by_name(pkt_dev, ifname); 19381da177e4SLinus Torvalds if (!odev) { 193925a8b254SDavid S. Miller printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname); 194039df232fSStephen Hemminger return -ENODEV; 19411da177e4SLinus Torvalds } 194239df232fSStephen Hemminger 19431da177e4SLinus Torvalds if (odev->type != ARPHRD_ETHER) { 194425a8b254SDavid S. Miller printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname); 194539df232fSStephen Hemminger err = -EINVAL; 194639df232fSStephen Hemminger } else if (!netif_running(odev)) { 194725a8b254SDavid S. Miller printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname); 194839df232fSStephen Hemminger err = -ENETDOWN; 194939df232fSStephen Hemminger } else { 19501da177e4SLinus Torvalds pkt_dev->odev = odev; 195139df232fSStephen Hemminger return 0; 195239df232fSStephen Hemminger } 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds dev_put(odev); 195539df232fSStephen Hemminger return err; 19561da177e4SLinus Torvalds } 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev 19591da177e4SLinus Torvalds * structure to have the right information to create/send packets 19601da177e4SLinus Torvalds */ 19611da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev) 19621da177e4SLinus Torvalds { 196364c00d81SAndrew Gallatin int ntxq; 196464c00d81SAndrew Gallatin 19651da177e4SLinus Torvalds if (!pkt_dev->odev) { 196625a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in " 196725a8b254SDavid S. Miller "setup_inject.\n"); 1968222f1806SLuiz Capitulino sprintf(pkt_dev->result, 1969222f1806SLuiz Capitulino "ERROR: pkt_dev->odev == NULL in setup_inject.\n"); 19701da177e4SLinus Torvalds return; 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds 197364c00d81SAndrew Gallatin /* make sure that we don't pick a non-existing transmit queue */ 197464c00d81SAndrew Gallatin ntxq = pkt_dev->odev->real_num_tx_queues; 197564c00d81SAndrew Gallatin if (ntxq <= num_online_cpus() && (pkt_dev->flags & F_QUEUE_MAP_CPU)) { 197664c00d81SAndrew Gallatin printk(KERN_WARNING "pktgen: WARNING: QUEUE_MAP_CPU " 197764c00d81SAndrew Gallatin "disabled because CPU count (%d) exceeds number ", 197864c00d81SAndrew Gallatin num_online_cpus()); 197964c00d81SAndrew Gallatin printk(KERN_WARNING "pktgen: WARNING: of tx queues " 198064c00d81SAndrew Gallatin "(%d) on %s \n", ntxq, pkt_dev->odev->name); 198164c00d81SAndrew Gallatin pkt_dev->flags &= ~F_QUEUE_MAP_CPU; 198264c00d81SAndrew Gallatin } 198364c00d81SAndrew Gallatin if (ntxq <= pkt_dev->queue_map_min) { 198464c00d81SAndrew Gallatin printk(KERN_WARNING "pktgen: WARNING: Requested " 198564c00d81SAndrew Gallatin "queue_map_min (%d) exceeds number of tx\n", 198664c00d81SAndrew Gallatin pkt_dev->queue_map_min); 198764c00d81SAndrew Gallatin printk(KERN_WARNING "pktgen: WARNING: queues (%d) on " 198864c00d81SAndrew Gallatin "%s, resetting\n", ntxq, pkt_dev->odev->name); 198964c00d81SAndrew Gallatin pkt_dev->queue_map_min = ntxq - 1; 199064c00d81SAndrew Gallatin } 199164c00d81SAndrew Gallatin if (ntxq <= pkt_dev->queue_map_max) { 199264c00d81SAndrew Gallatin printk(KERN_WARNING "pktgen: WARNING: Requested " 199364c00d81SAndrew Gallatin "queue_map_max (%d) exceeds number of tx\n", 199464c00d81SAndrew Gallatin pkt_dev->queue_map_max); 199564c00d81SAndrew Gallatin printk(KERN_WARNING "pktgen: WARNING: queues (%d) on " 199664c00d81SAndrew Gallatin "%s, resetting\n", ntxq, pkt_dev->odev->name); 199764c00d81SAndrew Gallatin pkt_dev->queue_map_max = ntxq - 1; 199864c00d81SAndrew Gallatin } 199964c00d81SAndrew Gallatin 20001da177e4SLinus Torvalds /* Default to the interface's mac if not explicitly set. */ 20011da177e4SLinus Torvalds 2002f404e9a6SKris Katterjohn if (is_zero_ether_addr(pkt_dev->src_mac)) 2003f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN); 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds /* Set up Dest MAC */ 2006f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds /* Set up pkt size */ 20091da177e4SLinus Torvalds pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 20121da177e4SLinus Torvalds /* 20131da177e4SLinus Torvalds * Skip this automatic address setting until locks or functions 20141da177e4SLinus Torvalds * gets exported 20151da177e4SLinus Torvalds */ 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds #ifdef NOTNOW 20181da177e4SLinus Torvalds int i, set = 0, err = 1; 20191da177e4SLinus Torvalds struct inet6_dev *idev; 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) 20221da177e4SLinus Torvalds if (pkt_dev->cur_in6_saddr.s6_addr[i]) { 20231da177e4SLinus Torvalds set = 1; 20241da177e4SLinus Torvalds break; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds if (!set) { 20281da177e4SLinus Torvalds 20291da177e4SLinus Torvalds /* 20301da177e4SLinus Torvalds * Use linklevel address if unconfigured. 20311da177e4SLinus Torvalds * 20321da177e4SLinus Torvalds * use ipv6_get_lladdr if/when it's get exported 20331da177e4SLinus Torvalds */ 20341da177e4SLinus Torvalds 20358814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 20361da177e4SLinus Torvalds if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) { 20371da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2040222f1806SLuiz Capitulino for (ifp = idev->addr_list; ifp; 2041222f1806SLuiz Capitulino ifp = ifp->if_next) { 2042222f1806SLuiz Capitulino if (ifp->scope == IFA_LINK 2043222f1806SLuiz Capitulino && !(ifp-> 2044222f1806SLuiz Capitulino flags & IFA_F_TENTATIVE)) { 2045222f1806SLuiz Capitulino ipv6_addr_copy(&pkt_dev-> 2046222f1806SLuiz Capitulino cur_in6_saddr, 2047222f1806SLuiz Capitulino &ifp->addr); 20481da177e4SLinus Torvalds err = 0; 20491da177e4SLinus Torvalds break; 20501da177e4SLinus Torvalds } 20511da177e4SLinus Torvalds } 20521da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 20531da177e4SLinus Torvalds } 20548814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 2055222f1806SLuiz Capitulino if (err) 205625a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: IPv6 link " 205725a8b254SDavid S. Miller "address not availble.\n"); 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds #endif 2060222f1806SLuiz Capitulino } else { 20611da177e4SLinus Torvalds pkt_dev->saddr_min = 0; 20621da177e4SLinus Torvalds pkt_dev->saddr_max = 0; 20631da177e4SLinus Torvalds if (strlen(pkt_dev->src_min) == 0) { 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds struct in_device *in_dev; 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds rcu_read_lock(); 2068e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(pkt_dev->odev); 20691da177e4SLinus Torvalds if (in_dev) { 20701da177e4SLinus Torvalds if (in_dev->ifa_list) { 2071222f1806SLuiz Capitulino pkt_dev->saddr_min = 2072222f1806SLuiz Capitulino in_dev->ifa_list->ifa_address; 20731da177e4SLinus Torvalds pkt_dev->saddr_max = pkt_dev->saddr_min; 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds rcu_read_unlock(); 2077222f1806SLuiz Capitulino } else { 20781da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 20791da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 20831da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 20841da177e4SLinus Torvalds } 20851da177e4SLinus Torvalds /* Initialize current values. */ 20861da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 20871da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 20881da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 20891da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 20901da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 20911da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 20921da177e4SLinus Torvalds pkt_dev->nflows = 0; 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us) 20961da177e4SLinus Torvalds { 20971da177e4SLinus Torvalds __u64 start; 20981da177e4SLinus Torvalds __u64 now; 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds start = now = getCurUs(); 21011da177e4SLinus Torvalds while (now < spin_until_us) { 2102b4099fabSStephen Hemminger /* TODO: optimize sleeping behavior */ 2103121caf57SNishanth Aravamudan if (spin_until_us - now > jiffies_to_usecs(1) + 1) 2104121caf57SNishanth Aravamudan schedule_timeout_interruptible(1); 2105121caf57SNishanth Aravamudan else if (spin_until_us - now > 100) { 21061da177e4SLinus Torvalds if (!pkt_dev->running) 21071da177e4SLinus Torvalds return; 21081da177e4SLinus Torvalds if (need_resched()) 21091da177e4SLinus Torvalds schedule(); 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds now = getCurUs(); 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds pkt_dev->idle_acc += now - start; 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds 211816dab72fSJamal Hadi Salim static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev) 211916dab72fSJamal Hadi Salim { 2120a553e4a6SJamal Hadi Salim pkt_dev->pkt_overhead = 0; 212116dab72fSJamal Hadi Salim pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32); 212216dab72fSJamal Hadi Salim pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev); 212316dab72fSJamal Hadi Salim pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev); 212416dab72fSJamal Hadi Salim } 212516dab72fSJamal Hadi Salim 2126007a531bSJamal Hadi Salim static inline int f_seen(struct pktgen_dev *pkt_dev, int flow) 2127007a531bSJamal Hadi Salim { 2128007a531bSJamal Hadi Salim 2129007a531bSJamal Hadi Salim if (pkt_dev->flows[flow].flags & F_INIT) 2130007a531bSJamal Hadi Salim return 1; 2131007a531bSJamal Hadi Salim else 2132007a531bSJamal Hadi Salim return 0; 2133007a531bSJamal Hadi Salim } 2134007a531bSJamal Hadi Salim 2135007a531bSJamal Hadi Salim static inline int f_pick(struct pktgen_dev *pkt_dev) 2136007a531bSJamal Hadi Salim { 2137007a531bSJamal Hadi Salim int flow = pkt_dev->curfl; 2138007a531bSJamal Hadi Salim 2139007a531bSJamal Hadi Salim if (pkt_dev->flags & F_FLOW_SEQ) { 2140007a531bSJamal Hadi Salim if (pkt_dev->flows[flow].count >= pkt_dev->lflow) { 2141007a531bSJamal Hadi Salim /* reset time */ 2142007a531bSJamal Hadi Salim pkt_dev->flows[flow].count = 0; 21431211a645SRobert Olsson pkt_dev->flows[flow].flags = 0; 2144007a531bSJamal Hadi Salim pkt_dev->curfl += 1; 2145007a531bSJamal Hadi Salim if (pkt_dev->curfl >= pkt_dev->cflows) 2146007a531bSJamal Hadi Salim pkt_dev->curfl = 0; /*reset */ 2147007a531bSJamal Hadi Salim } 2148007a531bSJamal Hadi Salim } else { 2149007a531bSJamal Hadi Salim flow = random32() % pkt_dev->cflows; 21501211a645SRobert Olsson pkt_dev->curfl = flow; 2151007a531bSJamal Hadi Salim 21521211a645SRobert Olsson if (pkt_dev->flows[flow].count > pkt_dev->lflow) { 2153007a531bSJamal Hadi Salim pkt_dev->flows[flow].count = 0; 21541211a645SRobert Olsson pkt_dev->flows[flow].flags = 0; 21551211a645SRobert Olsson } 2156007a531bSJamal Hadi Salim } 2157007a531bSJamal Hadi Salim 2158007a531bSJamal Hadi Salim return pkt_dev->curfl; 2159007a531bSJamal Hadi Salim } 2160007a531bSJamal Hadi Salim 2161a553e4a6SJamal Hadi Salim 2162a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 2163a553e4a6SJamal Hadi Salim /* If there was already an IPSEC SA, we keep it as is, else 2164a553e4a6SJamal Hadi Salim * we go look for it ... 2165a553e4a6SJamal Hadi Salim */ 2166fea1ab0fSAdrian Bunk static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow) 2167a553e4a6SJamal Hadi Salim { 2168a553e4a6SJamal Hadi Salim struct xfrm_state *x = pkt_dev->flows[flow].x; 2169a553e4a6SJamal Hadi Salim if (!x) { 2170a553e4a6SJamal Hadi Salim /*slow path: we dont already have xfrm_state*/ 2171a553e4a6SJamal Hadi Salim x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr, 2172a553e4a6SJamal Hadi Salim (xfrm_address_t *)&pkt_dev->cur_saddr, 2173a553e4a6SJamal Hadi Salim AF_INET, 2174a553e4a6SJamal Hadi Salim pkt_dev->ipsmode, 2175a553e4a6SJamal Hadi Salim pkt_dev->ipsproto, 0); 2176a553e4a6SJamal Hadi Salim if (x) { 2177a553e4a6SJamal Hadi Salim pkt_dev->flows[flow].x = x; 2178a553e4a6SJamal Hadi Salim set_pkt_overhead(pkt_dev); 2179a553e4a6SJamal Hadi Salim pkt_dev->pkt_overhead+=x->props.header_len; 2180a553e4a6SJamal Hadi Salim } 2181a553e4a6SJamal Hadi Salim 2182a553e4a6SJamal Hadi Salim } 2183a553e4a6SJamal Hadi Salim } 2184a553e4a6SJamal Hadi Salim #endif 2185fd2ea0a7SDavid S. Miller static void set_cur_queue_map(struct pktgen_dev *pkt_dev) 2186fd2ea0a7SDavid S. Miller { 2187e6fce5b9SRobert Olsson 2188e6fce5b9SRobert Olsson if (pkt_dev->flags & F_QUEUE_MAP_CPU) 2189e6fce5b9SRobert Olsson pkt_dev->cur_queue_map = smp_processor_id(); 2190e6fce5b9SRobert Olsson 2191e6fce5b9SRobert Olsson else if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) { 2192fd2ea0a7SDavid S. Miller __u16 t; 2193fd2ea0a7SDavid S. Miller if (pkt_dev->flags & F_QUEUE_MAP_RND) { 2194fd2ea0a7SDavid S. Miller t = random32() % 2195fd2ea0a7SDavid S. Miller (pkt_dev->queue_map_max - 2196fd2ea0a7SDavid S. Miller pkt_dev->queue_map_min + 1) 2197fd2ea0a7SDavid S. Miller + pkt_dev->queue_map_min; 2198fd2ea0a7SDavid S. Miller } else { 2199fd2ea0a7SDavid S. Miller t = pkt_dev->cur_queue_map + 1; 2200fd2ea0a7SDavid S. Miller if (t > pkt_dev->queue_map_max) 2201fd2ea0a7SDavid S. Miller t = pkt_dev->queue_map_min; 2202fd2ea0a7SDavid S. Miller } 2203fd2ea0a7SDavid S. Miller pkt_dev->cur_queue_map = t; 2204fd2ea0a7SDavid S. Miller } 2205fd2ea0a7SDavid S. Miller } 2206fd2ea0a7SDavid S. Miller 22071da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values 22081da177e4SLinus Torvalds * for IP src/dest, UDP src/dst port, MAC-Addr src/dst 22091da177e4SLinus Torvalds */ 2210222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev) 2211222f1806SLuiz Capitulino { 22121da177e4SLinus Torvalds __u32 imn; 22131da177e4SLinus Torvalds __u32 imx; 22141da177e4SLinus Torvalds int flow = 0; 22151da177e4SLinus Torvalds 2216007a531bSJamal Hadi Salim if (pkt_dev->cflows) 2217007a531bSJamal Hadi Salim flow = f_pick(pkt_dev); 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds /* Deal with source MAC */ 22201da177e4SLinus Torvalds if (pkt_dev->src_mac_count > 1) { 22211da177e4SLinus Torvalds __u32 mc; 22221da177e4SLinus Torvalds __u32 tmp; 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 22255fa6fc76SStephen Hemminger mc = random32() % pkt_dev->src_mac_count; 22261da177e4SLinus Torvalds else { 22271da177e4SLinus Torvalds mc = pkt_dev->cur_src_mac_offset++; 2228ff2a79a5SRobert Olsson if (pkt_dev->cur_src_mac_offset >= 2229222f1806SLuiz Capitulino pkt_dev->src_mac_count) 22301da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 22311da177e4SLinus Torvalds } 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds tmp = pkt_dev->src_mac[5] + (mc & 0xFF); 22341da177e4SLinus Torvalds pkt_dev->hh[11] = tmp; 22351da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 22361da177e4SLinus Torvalds pkt_dev->hh[10] = tmp; 22371da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 22381da177e4SLinus Torvalds pkt_dev->hh[9] = tmp; 22391da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 22401da177e4SLinus Torvalds pkt_dev->hh[8] = tmp; 22411da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[1] + (tmp >> 8)); 22421da177e4SLinus Torvalds pkt_dev->hh[7] = tmp; 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds 22451da177e4SLinus Torvalds /* Deal with Destination MAC */ 22461da177e4SLinus Torvalds if (pkt_dev->dst_mac_count > 1) { 22471da177e4SLinus Torvalds __u32 mc; 22481da177e4SLinus Torvalds __u32 tmp; 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 22515fa6fc76SStephen Hemminger mc = random32() % pkt_dev->dst_mac_count; 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds else { 22541da177e4SLinus Torvalds mc = pkt_dev->cur_dst_mac_offset++; 2255ff2a79a5SRobert Olsson if (pkt_dev->cur_dst_mac_offset >= 2256222f1806SLuiz Capitulino pkt_dev->dst_mac_count) { 22571da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds tmp = pkt_dev->dst_mac[5] + (mc & 0xFF); 22621da177e4SLinus Torvalds pkt_dev->hh[5] = tmp; 22631da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 22641da177e4SLinus Torvalds pkt_dev->hh[4] = tmp; 22651da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 22661da177e4SLinus Torvalds pkt_dev->hh[3] = tmp; 22671da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 22681da177e4SLinus Torvalds pkt_dev->hh[2] = tmp; 22691da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[1] + (tmp >> 8)); 22701da177e4SLinus Torvalds pkt_dev->hh[1] = tmp; 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds 2273ca6549afSSteven Whitehouse if (pkt_dev->flags & F_MPLS_RND) { 2274ca6549afSSteven Whitehouse unsigned i; 2275ca6549afSSteven Whitehouse for (i = 0; i < pkt_dev->nr_labels; i++) 2276ca6549afSSteven Whitehouse if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM) 2277ca6549afSSteven Whitehouse pkt_dev->labels[i] = MPLS_STACK_BOTTOM | 22785fa6fc76SStephen Hemminger ((__force __be32)random32() & 2279ca6549afSSteven Whitehouse htonl(0x000fffff)); 2280ca6549afSSteven Whitehouse } 2281ca6549afSSteven Whitehouse 228234954ddcSFrancesco Fondelli if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) { 22835fa6fc76SStephen Hemminger pkt_dev->vlan_id = random32() & (4096-1); 228434954ddcSFrancesco Fondelli } 228534954ddcSFrancesco Fondelli 228634954ddcSFrancesco Fondelli if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) { 22875fa6fc76SStephen Hemminger pkt_dev->svlan_id = random32() & (4096 - 1); 228834954ddcSFrancesco Fondelli } 228934954ddcSFrancesco Fondelli 22901da177e4SLinus Torvalds if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) { 22911da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 22925fa6fc76SStephen Hemminger pkt_dev->cur_udp_src = random32() % 22935fa6fc76SStephen Hemminger (pkt_dev->udp_src_max - pkt_dev->udp_src_min) 22945fa6fc76SStephen Hemminger + pkt_dev->udp_src_min; 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds else { 22971da177e4SLinus Torvalds pkt_dev->cur_udp_src++; 22981da177e4SLinus Torvalds if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max) 22991da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 23001da177e4SLinus Torvalds } 23011da177e4SLinus Torvalds } 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) { 23041da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) { 23055fa6fc76SStephen Hemminger pkt_dev->cur_udp_dst = random32() % 23065fa6fc76SStephen Hemminger (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min) 23075fa6fc76SStephen Hemminger + pkt_dev->udp_dst_min; 2308222f1806SLuiz Capitulino } else { 23091da177e4SLinus Torvalds pkt_dev->cur_udp_dst++; 23101da177e4SLinus Torvalds if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max) 23111da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds } 23141da177e4SLinus Torvalds 23151da177e4SLinus Torvalds if (!(pkt_dev->flags & F_IPV6)) { 23161da177e4SLinus Torvalds 2317222f1806SLuiz Capitulino if ((imn = ntohl(pkt_dev->saddr_min)) < (imx = 2318222f1806SLuiz Capitulino ntohl(pkt_dev-> 2319222f1806SLuiz Capitulino saddr_max))) { 23201da177e4SLinus Torvalds __u32 t; 23211da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 23225fa6fc76SStephen Hemminger t = random32() % (imx - imn) + imn; 23231da177e4SLinus Torvalds else { 23241da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_saddr); 23251da177e4SLinus Torvalds t++; 23261da177e4SLinus Torvalds if (t > imx) { 23271da177e4SLinus Torvalds t = imn; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds } 23301da177e4SLinus Torvalds pkt_dev->cur_saddr = htonl(t); 23311da177e4SLinus Torvalds } 23321da177e4SLinus Torvalds 2333007a531bSJamal Hadi Salim if (pkt_dev->cflows && f_seen(pkt_dev, flow)) { 23341da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr; 23351da177e4SLinus Torvalds } else { 2336252e3346SAl Viro imn = ntohl(pkt_dev->daddr_min); 2337252e3346SAl Viro imx = ntohl(pkt_dev->daddr_max); 2338252e3346SAl Viro if (imn < imx) { 23391da177e4SLinus Torvalds __u32 t; 2340252e3346SAl Viro __be32 s; 23411da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) { 23421da177e4SLinus Torvalds 23435fa6fc76SStephen Hemminger t = random32() % (imx - imn) + imn; 2344252e3346SAl Viro s = htonl(t); 23451da177e4SLinus Torvalds 234621cf2253SJoe Perches while (ipv4_is_loopback(s) || 234721cf2253SJoe Perches ipv4_is_multicast(s) || 23481e637c74SJan Engelhardt ipv4_is_lbcast(s) || 234921cf2253SJoe Perches ipv4_is_zeronet(s) || 235021cf2253SJoe Perches ipv4_is_local_multicast(s)) { 23515fa6fc76SStephen Hemminger t = random32() % (imx - imn) + imn; 2352252e3346SAl Viro s = htonl(t); 23531da177e4SLinus Torvalds } 2354252e3346SAl Viro pkt_dev->cur_daddr = s; 2355252e3346SAl Viro } else { 23561da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_daddr); 23571da177e4SLinus Torvalds t++; 23581da177e4SLinus Torvalds if (t > imx) { 23591da177e4SLinus Torvalds t = imn; 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds pkt_dev->cur_daddr = htonl(t); 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds if (pkt_dev->cflows) { 2365007a531bSJamal Hadi Salim pkt_dev->flows[flow].flags |= F_INIT; 2366222f1806SLuiz Capitulino pkt_dev->flows[flow].cur_daddr = 2367222f1806SLuiz Capitulino pkt_dev->cur_daddr; 2368a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 2369a553e4a6SJamal Hadi Salim if (pkt_dev->flags & F_IPSEC_ON) 2370a553e4a6SJamal Hadi Salim get_ipsec_sa(pkt_dev, flow); 2371a553e4a6SJamal Hadi Salim #endif 23721da177e4SLinus Torvalds pkt_dev->nflows++; 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds } 2375222f1806SLuiz Capitulino } else { /* IPV6 * */ 2376222f1806SLuiz Capitulino 23771da177e4SLinus Torvalds if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 && 23781da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[1] == 0 && 23791da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[2] == 0 && 23801da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ; 23811da177e4SLinus Torvalds else { 23821da177e4SLinus Torvalds int i; 23831da177e4SLinus Torvalds 23841da177e4SLinus Torvalds /* Only random destinations yet */ 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds for (i = 0; i < 4; i++) { 23871da177e4SLinus Torvalds pkt_dev->cur_in6_daddr.s6_addr32[i] = 23885fa6fc76SStephen Hemminger (((__force __be32)random32() | 23891da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[i]) & 23901da177e4SLinus Torvalds pkt_dev->max_in6_daddr.s6_addr32[i]); 23911da177e4SLinus Torvalds } 23921da177e4SLinus Torvalds } 23931da177e4SLinus Torvalds } 23941da177e4SLinus Torvalds 23951da177e4SLinus Torvalds if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) { 23961da177e4SLinus Torvalds __u32 t; 23971da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) { 23985fa6fc76SStephen Hemminger t = random32() % 23995fa6fc76SStephen Hemminger (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size) 24005fa6fc76SStephen Hemminger + pkt_dev->min_pkt_size; 2401222f1806SLuiz Capitulino } else { 24021da177e4SLinus Torvalds t = pkt_dev->cur_pkt_size + 1; 24031da177e4SLinus Torvalds if (t > pkt_dev->max_pkt_size) 24041da177e4SLinus Torvalds t = pkt_dev->min_pkt_size; 24051da177e4SLinus Torvalds } 24061da177e4SLinus Torvalds pkt_dev->cur_pkt_size = t; 24071da177e4SLinus Torvalds } 24081da177e4SLinus Torvalds 2409fd2ea0a7SDavid S. Miller set_cur_queue_map(pkt_dev); 241045b270f8SRobert Olsson 24111da177e4SLinus Torvalds pkt_dev->flows[flow].count++; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 2414a553e4a6SJamal Hadi Salim 2415a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 2416a553e4a6SJamal Hadi Salim static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev) 2417a553e4a6SJamal Hadi Salim { 2418a553e4a6SJamal Hadi Salim struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x; 2419a553e4a6SJamal Hadi Salim int err = 0; 2420a553e4a6SJamal Hadi Salim struct iphdr *iph; 2421a553e4a6SJamal Hadi Salim 2422a553e4a6SJamal Hadi Salim if (!x) 2423a553e4a6SJamal Hadi Salim return 0; 2424a553e4a6SJamal Hadi Salim /* XXX: we dont support tunnel mode for now until 2425a553e4a6SJamal Hadi Salim * we resolve the dst issue */ 2426a553e4a6SJamal Hadi Salim if (x->props.mode != XFRM_MODE_TRANSPORT) 2427a553e4a6SJamal Hadi Salim return 0; 2428a553e4a6SJamal Hadi Salim 2429a553e4a6SJamal Hadi Salim spin_lock(&x->lock); 2430a553e4a6SJamal Hadi Salim iph = ip_hdr(skb); 2431a553e4a6SJamal Hadi Salim 243213996378SHerbert Xu err = x->outer_mode->output(x, skb); 2433a553e4a6SJamal Hadi Salim if (err) 2434a553e4a6SJamal Hadi Salim goto error; 2435a553e4a6SJamal Hadi Salim err = x->type->output(x, skb); 2436a553e4a6SJamal Hadi Salim if (err) 2437a553e4a6SJamal Hadi Salim goto error; 2438a553e4a6SJamal Hadi Salim 2439a553e4a6SJamal Hadi Salim x->curlft.bytes +=skb->len; 2440a553e4a6SJamal Hadi Salim x->curlft.packets++; 2441a553e4a6SJamal Hadi Salim error: 2442a553e4a6SJamal Hadi Salim spin_unlock(&x->lock); 2443a553e4a6SJamal Hadi Salim return err; 2444a553e4a6SJamal Hadi Salim } 2445a553e4a6SJamal Hadi Salim 2446a553e4a6SJamal Hadi Salim static inline void free_SAs(struct pktgen_dev *pkt_dev) 2447a553e4a6SJamal Hadi Salim { 2448a553e4a6SJamal Hadi Salim if (pkt_dev->cflows) { 2449a553e4a6SJamal Hadi Salim /* let go of the SAs if we have them */ 2450a553e4a6SJamal Hadi Salim int i = 0; 2451a553e4a6SJamal Hadi Salim for (; i < pkt_dev->nflows; i++){ 2452a553e4a6SJamal Hadi Salim struct xfrm_state *x = pkt_dev->flows[i].x; 2453a553e4a6SJamal Hadi Salim if (x) { 2454a553e4a6SJamal Hadi Salim xfrm_state_put(x); 2455a553e4a6SJamal Hadi Salim pkt_dev->flows[i].x = NULL; 2456a553e4a6SJamal Hadi Salim } 2457a553e4a6SJamal Hadi Salim } 2458a553e4a6SJamal Hadi Salim } 2459a553e4a6SJamal Hadi Salim } 2460a553e4a6SJamal Hadi Salim 2461a553e4a6SJamal Hadi Salim static inline int process_ipsec(struct pktgen_dev *pkt_dev, 2462a553e4a6SJamal Hadi Salim struct sk_buff *skb, __be16 protocol) 2463a553e4a6SJamal Hadi Salim { 2464a553e4a6SJamal Hadi Salim if (pkt_dev->flags & F_IPSEC_ON) { 2465a553e4a6SJamal Hadi Salim struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x; 2466a553e4a6SJamal Hadi Salim int nhead = 0; 2467a553e4a6SJamal Hadi Salim if (x) { 2468a553e4a6SJamal Hadi Salim int ret; 2469a553e4a6SJamal Hadi Salim __u8 *eth; 2470a553e4a6SJamal Hadi Salim nhead = x->props.header_len - skb_headroom(skb); 2471a553e4a6SJamal Hadi Salim if (nhead >0) { 2472a553e4a6SJamal Hadi Salim ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC); 2473a553e4a6SJamal Hadi Salim if (ret < 0) { 247425a8b254SDavid S. Miller printk(KERN_ERR "Error expanding " 247525a8b254SDavid S. Miller "ipsec packet %d\n",ret); 2476b4bb4ac8SIlpo Järvinen goto err; 2477a553e4a6SJamal Hadi Salim } 2478a553e4a6SJamal Hadi Salim } 2479a553e4a6SJamal Hadi Salim 2480a553e4a6SJamal Hadi Salim /* ipsec is not expecting ll header */ 2481a553e4a6SJamal Hadi Salim skb_pull(skb, ETH_HLEN); 2482a553e4a6SJamal Hadi Salim ret = pktgen_output_ipsec(skb, pkt_dev); 2483a553e4a6SJamal Hadi Salim if (ret) { 248425a8b254SDavid S. Miller printk(KERN_ERR "Error creating ipsec " 248525a8b254SDavid S. Miller "packet %d\n",ret); 2486b4bb4ac8SIlpo Järvinen goto err; 2487a553e4a6SJamal Hadi Salim } 2488a553e4a6SJamal Hadi Salim /* restore ll */ 2489a553e4a6SJamal Hadi Salim eth = (__u8 *) skb_push(skb, ETH_HLEN); 2490a553e4a6SJamal Hadi Salim memcpy(eth, pkt_dev->hh, 12); 2491a553e4a6SJamal Hadi Salim *(u16 *) & eth[12] = protocol; 2492a553e4a6SJamal Hadi Salim } 2493a553e4a6SJamal Hadi Salim } 2494a553e4a6SJamal Hadi Salim return 1; 2495b4bb4ac8SIlpo Järvinen err: 2496b4bb4ac8SIlpo Järvinen kfree_skb(skb); 2497b4bb4ac8SIlpo Järvinen return 0; 2498a553e4a6SJamal Hadi Salim } 2499a553e4a6SJamal Hadi Salim #endif 2500a553e4a6SJamal Hadi Salim 2501ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev) 2502ca6549afSSteven Whitehouse { 2503ca6549afSSteven Whitehouse unsigned i; 2504ca6549afSSteven Whitehouse for (i = 0; i < pkt_dev->nr_labels; i++) { 2505ca6549afSSteven Whitehouse *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM; 2506ca6549afSSteven Whitehouse } 2507ca6549afSSteven Whitehouse mpls--; 2508ca6549afSSteven Whitehouse *mpls |= MPLS_STACK_BOTTOM; 2509ca6549afSSteven Whitehouse } 2510ca6549afSSteven Whitehouse 25110f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi, 25120f37c605SAl Viro unsigned int prio) 25130f37c605SAl Viro { 25140f37c605SAl Viro return htons(id | (cfi << 12) | (prio << 13)); 25150f37c605SAl Viro } 25160f37c605SAl Viro 25171da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev, 25181da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 25191da177e4SLinus Torvalds { 25201da177e4SLinus Torvalds struct sk_buff *skb = NULL; 25211da177e4SLinus Torvalds __u8 *eth; 25221da177e4SLinus Torvalds struct udphdr *udph; 25231da177e4SLinus Torvalds int datalen, iplen; 25241da177e4SLinus Torvalds struct iphdr *iph; 25251da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 2526d5f1ce9aSStephen Hemminger __be16 protocol = htons(ETH_P_IP); 2527ca6549afSSteven Whitehouse __be32 *mpls; 252834954ddcSFrancesco Fondelli __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */ 252934954ddcSFrancesco Fondelli __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */ 253034954ddcSFrancesco Fondelli __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */ 253134954ddcSFrancesco Fondelli __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */ 2532fd2ea0a7SDavid S. Miller u16 queue_map; 2533ca6549afSSteven Whitehouse 2534ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2535d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_MPLS_UC); 25361da177e4SLinus Torvalds 253734954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) 2538d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_8021Q); 253934954ddcSFrancesco Fondelli 254064053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 254164053beeSRobert Olsson * fields. 254264053beeSRobert Olsson */ 2543fd2ea0a7SDavid S. Miller queue_map = pkt_dev->cur_queue_map; 254464053beeSRobert Olsson mod_cur_headers(pkt_dev); 254564053beeSRobert Olsson 25467ac5459eSDavid S. Miller datalen = (odev->hard_header_len + 16) & ~0xf; 2547ca6549afSSteven Whitehouse skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen + 254816dab72fSJamal Hadi Salim pkt_dev->pkt_overhead, GFP_ATOMIC); 25491da177e4SLinus Torvalds if (!skb) { 25501da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 25511da177e4SLinus Torvalds return NULL; 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds 25547ac5459eSDavid S. Miller skb_reserve(skb, datalen); 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 25571da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 2558ca6549afSSteven Whitehouse mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32)); 2559ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2560ca6549afSSteven Whitehouse mpls_push(mpls, pkt_dev); 256134954ddcSFrancesco Fondelli 256234954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) { 256334954ddcSFrancesco Fondelli if (pkt_dev->svlan_id != 0xffff) { 256434954ddcSFrancesco Fondelli svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 25650f37c605SAl Viro *svlan_tci = build_tci(pkt_dev->svlan_id, 25660f37c605SAl Viro pkt_dev->svlan_cfi, 25670f37c605SAl Viro pkt_dev->svlan_p); 256834954ddcSFrancesco Fondelli svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2569d5f1ce9aSStephen Hemminger *svlan_encapsulated_proto = htons(ETH_P_8021Q); 257034954ddcSFrancesco Fondelli } 257134954ddcSFrancesco Fondelli vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 25720f37c605SAl Viro *vlan_tci = build_tci(pkt_dev->vlan_id, 25730f37c605SAl Viro pkt_dev->vlan_cfi, 25740f37c605SAl Viro pkt_dev->vlan_p); 257534954ddcSFrancesco Fondelli vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2576d5f1ce9aSStephen Hemminger *vlan_encapsulated_proto = htons(ETH_P_IP); 257734954ddcSFrancesco Fondelli } 257834954ddcSFrancesco Fondelli 257927a884dcSArnaldo Carvalho de Melo skb->network_header = skb->tail; 2580b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr); 2581ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr)); 2582fd2ea0a7SDavid S. Miller skb_set_queue_mapping(skb, queue_map); 2583ddc7b8e3SArnaldo Carvalho de Melo iph = ip_hdr(skb); 2584ddc7b8e3SArnaldo Carvalho de Melo udph = udp_hdr(skb); 25851da177e4SLinus Torvalds 25861da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 2587252e3346SAl Viro *(__be16 *) & eth[12] = protocol; 25881da177e4SLinus Torvalds 2589ca6549afSSteven Whitehouse /* Eth + IPh + UDPh + mpls */ 2590ca6549afSSteven Whitehouse datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 - 259116dab72fSJamal Hadi Salim pkt_dev->pkt_overhead; 25921da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) 25931da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 25961da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 25971da177e4SLinus Torvalds udph->len = htons(datalen + 8); /* DATA + udphdr */ 25981da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds iph->ihl = 5; 26011da177e4SLinus Torvalds iph->version = 4; 26021da177e4SLinus Torvalds iph->ttl = 32; 26031ca7768cSFrancesco Fondelli iph->tos = pkt_dev->tos; 26041da177e4SLinus Torvalds iph->protocol = IPPROTO_UDP; /* UDP */ 26051da177e4SLinus Torvalds iph->saddr = pkt_dev->cur_saddr; 26061da177e4SLinus Torvalds iph->daddr = pkt_dev->cur_daddr; 26071da177e4SLinus Torvalds iph->frag_off = 0; 26081da177e4SLinus Torvalds iplen = 20 + 8 + datalen; 26091da177e4SLinus Torvalds iph->tot_len = htons(iplen); 26101da177e4SLinus Torvalds iph->check = 0; 26111da177e4SLinus Torvalds iph->check = ip_fast_csum((void *)iph, iph->ihl); 2612ca6549afSSteven Whitehouse skb->protocol = protocol; 2613b0e380b1SArnaldo Carvalho de Melo skb->mac_header = (skb->network_header - ETH_HLEN - 261416dab72fSJamal Hadi Salim pkt_dev->pkt_overhead); 26151da177e4SLinus Torvalds skb->dev = odev; 26161da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 26191da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 26201da177e4SLinus Torvalds else { 26211da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 26221da177e4SLinus Torvalds int i; 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 26271da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 26281da177e4SLinus Torvalds if (datalen > frags * PAGE_SIZE) { 26291da177e4SLinus Torvalds skb_put(skb, datalen - frags * PAGE_SIZE); 26301da177e4SLinus Torvalds datalen = frags * PAGE_SIZE; 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds i = 0; 26341da177e4SLinus Torvalds while (datalen > 0) { 26351da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 26361da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 26371da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 26381da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 26391da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 26401da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 26411da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 26421da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 26431da177e4SLinus Torvalds i++; 26441da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds 26471da177e4SLinus Torvalds while (i < frags) { 26481da177e4SLinus Torvalds int rem; 26491da177e4SLinus Torvalds 26501da177e4SLinus Torvalds if (i == 0) 26511da177e4SLinus Torvalds break; 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 26541da177e4SLinus Torvalds if (rem == 0) 26551da177e4SLinus Torvalds break; 26561da177e4SLinus Torvalds 26571da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 26581da177e4SLinus Torvalds 2659222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i] = 2660222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1]; 26611da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 2662222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page = 2663222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].page; 2664222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page_offset += 2665222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].size; 26661da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 26671da177e4SLinus Torvalds i++; 26681da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 26731da177e4SLinus Torvalds 26741da177e4SLinus Torvalds if (pgh) { 26751da177e4SLinus Torvalds struct timeval timestamp; 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 26781da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 26791da177e4SLinus Torvalds 26801da177e4SLinus Torvalds do_gettimeofday(×tamp); 26811da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 26821da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds 2685a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 2686a553e4a6SJamal Hadi Salim if (!process_ipsec(pkt_dev, skb, protocol)) 2687a553e4a6SJamal Hadi Salim return NULL; 2688a553e4a6SJamal Hadi Salim #endif 2689a553e4a6SJamal Hadi Salim 26901da177e4SLinus Torvalds return skb; 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds /* 26941da177e4SLinus Torvalds * scan_ip6, fmt_ip taken from dietlibc-0.21 26951da177e4SLinus Torvalds * Author Felix von Leitner <felix-dietlibc@fefe.de> 26961da177e4SLinus Torvalds * 26971da177e4SLinus Torvalds * Slightly modified for kernel. 26981da177e4SLinus Torvalds * Should be candidate for net/ipv4/utils.c 26991da177e4SLinus Torvalds * --ro 27001da177e4SLinus Torvalds */ 27011da177e4SLinus Torvalds 27021da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]) 27031da177e4SLinus Torvalds { 27041da177e4SLinus Torvalds unsigned int i; 27051da177e4SLinus Torvalds unsigned int len = 0; 27061da177e4SLinus Torvalds unsigned long u; 27071da177e4SLinus Torvalds char suffix[16]; 27081da177e4SLinus Torvalds unsigned int prefixlen = 0; 27091da177e4SLinus Torvalds unsigned int suffixlen = 0; 2710252e3346SAl Viro __be32 tmp; 2711cfcabdccSStephen Hemminger char *pos; 27121da177e4SLinus Torvalds 2713222f1806SLuiz Capitulino for (i = 0; i < 16; i++) 2714222f1806SLuiz Capitulino ip[i] = 0; 27151da177e4SLinus Torvalds 27161da177e4SLinus Torvalds for (;;) { 27171da177e4SLinus Torvalds if (*s == ':') { 27181da177e4SLinus Torvalds len++; 27191da177e4SLinus Torvalds if (s[1] == ':') { /* Found "::", skip to part 2 */ 27201da177e4SLinus Torvalds s += 2; 27211da177e4SLinus Torvalds len++; 27221da177e4SLinus Torvalds break; 27231da177e4SLinus Torvalds } 27241da177e4SLinus Torvalds s++; 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds 2727cfcabdccSStephen Hemminger u = simple_strtoul(s, &pos, 16); 2728cfcabdccSStephen Hemminger i = pos - s; 2729222f1806SLuiz Capitulino if (!i) 2730222f1806SLuiz Capitulino return 0; 27311da177e4SLinus Torvalds if (prefixlen == 12 && s[i] == '.') { 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds /* the last 4 bytes may be written as IPv4 address */ 27341da177e4SLinus Torvalds 27351da177e4SLinus Torvalds tmp = in_aton(s); 27361da177e4SLinus Torvalds memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp)); 27371da177e4SLinus Torvalds return i + len; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds ip[prefixlen++] = (u >> 8); 27401da177e4SLinus Torvalds ip[prefixlen++] = (u & 255); 2741222f1806SLuiz Capitulino s += i; 2742222f1806SLuiz Capitulino len += i; 27431da177e4SLinus Torvalds if (prefixlen == 16) 27441da177e4SLinus Torvalds return len; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds 27471da177e4SLinus Torvalds /* part 2, after "::" */ 27481da177e4SLinus Torvalds for (;;) { 27491da177e4SLinus Torvalds if (*s == ':') { 27501da177e4SLinus Torvalds if (suffixlen == 0) 27511da177e4SLinus Torvalds break; 27521da177e4SLinus Torvalds s++; 27531da177e4SLinus Torvalds len++; 27541da177e4SLinus Torvalds } else if (suffixlen != 0) 27551da177e4SLinus Torvalds break; 2756cfcabdccSStephen Hemminger 2757cfcabdccSStephen Hemminger u = simple_strtol(s, &pos, 16); 2758cfcabdccSStephen Hemminger i = pos - s; 27591da177e4SLinus Torvalds if (!i) { 2760222f1806SLuiz Capitulino if (*s) 2761222f1806SLuiz Capitulino len--; 27621da177e4SLinus Torvalds break; 27631da177e4SLinus Torvalds } 27641da177e4SLinus Torvalds if (suffixlen + prefixlen <= 12 && s[i] == '.') { 27651da177e4SLinus Torvalds tmp = in_aton(s); 2766222f1806SLuiz Capitulino memcpy((struct in_addr *)(suffix + suffixlen), &tmp, 2767222f1806SLuiz Capitulino sizeof(tmp)); 27681da177e4SLinus Torvalds suffixlen += 4; 27691da177e4SLinus Torvalds len += strlen(s); 27701da177e4SLinus Torvalds break; 27711da177e4SLinus Torvalds } 27721da177e4SLinus Torvalds suffix[suffixlen++] = (u >> 8); 27731da177e4SLinus Torvalds suffix[suffixlen++] = (u & 255); 2774222f1806SLuiz Capitulino s += i; 2775222f1806SLuiz Capitulino len += i; 27761da177e4SLinus Torvalds if (prefixlen + suffixlen == 16) 27771da177e4SLinus Torvalds break; 27781da177e4SLinus Torvalds } 27791da177e4SLinus Torvalds for (i = 0; i < suffixlen; i++) 27801da177e4SLinus Torvalds ip[16 - suffixlen + i] = suffix[i]; 27811da177e4SLinus Torvalds return len; 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds 2784222f1806SLuiz Capitulino static char tohex(char hexdigit) 2785222f1806SLuiz Capitulino { 27861da177e4SLinus Torvalds return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0'; 27871da177e4SLinus Torvalds } 27881da177e4SLinus Torvalds 2789222f1806SLuiz Capitulino static int fmt_xlong(char *s, unsigned int i) 2790222f1806SLuiz Capitulino { 27911da177e4SLinus Torvalds char *bak = s; 2792222f1806SLuiz Capitulino *s = tohex((i >> 12) & 0xf); 2793222f1806SLuiz Capitulino if (s != bak || *s != '0') 2794222f1806SLuiz Capitulino ++s; 2795222f1806SLuiz Capitulino *s = tohex((i >> 8) & 0xf); 2796222f1806SLuiz Capitulino if (s != bak || *s != '0') 2797222f1806SLuiz Capitulino ++s; 2798222f1806SLuiz Capitulino *s = tohex((i >> 4) & 0xf); 2799222f1806SLuiz Capitulino if (s != bak || *s != '0') 2800222f1806SLuiz Capitulino ++s; 28011da177e4SLinus Torvalds *s = tohex(i & 0xf); 28021da177e4SLinus Torvalds return s - bak + 1; 28031da177e4SLinus Torvalds } 28041da177e4SLinus Torvalds 2805222f1806SLuiz Capitulino static unsigned int fmt_ip6(char *s, const char ip[16]) 2806222f1806SLuiz Capitulino { 28071da177e4SLinus Torvalds unsigned int len; 28081da177e4SLinus Torvalds unsigned int i; 28091da177e4SLinus Torvalds unsigned int temp; 28101da177e4SLinus Torvalds unsigned int compressing; 28111da177e4SLinus Torvalds int j; 28121da177e4SLinus Torvalds 2813222f1806SLuiz Capitulino len = 0; 2814222f1806SLuiz Capitulino compressing = 0; 28151da177e4SLinus Torvalds for (j = 0; j < 16; j += 2) { 28161da177e4SLinus Torvalds 28171da177e4SLinus Torvalds #ifdef V4MAPPEDPREFIX 28181da177e4SLinus Torvalds if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) { 28191da177e4SLinus Torvalds inet_ntoa_r(*(struct in_addr *)(ip + 12), s); 28201da177e4SLinus Torvalds temp = strlen(s); 28211da177e4SLinus Torvalds return len + temp; 28221da177e4SLinus Torvalds } 28231da177e4SLinus Torvalds #endif 28241da177e4SLinus Torvalds temp = ((unsigned long)(unsigned char)ip[j] << 8) + 28251da177e4SLinus Torvalds (unsigned long)(unsigned char)ip[j + 1]; 28261da177e4SLinus Torvalds if (temp == 0) { 28271da177e4SLinus Torvalds if (!compressing) { 28281da177e4SLinus Torvalds compressing = 1; 28291da177e4SLinus Torvalds if (j == 0) { 2830222f1806SLuiz Capitulino *s++ = ':'; 2831222f1806SLuiz Capitulino ++len; 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds } else { 28351da177e4SLinus Torvalds if (compressing) { 28361da177e4SLinus Torvalds compressing = 0; 2837222f1806SLuiz Capitulino *s++ = ':'; 2838222f1806SLuiz Capitulino ++len; 28391da177e4SLinus Torvalds } 2840222f1806SLuiz Capitulino i = fmt_xlong(s, temp); 2841222f1806SLuiz Capitulino len += i; 2842222f1806SLuiz Capitulino s += i; 28431da177e4SLinus Torvalds if (j < 14) { 28441da177e4SLinus Torvalds *s++ = ':'; 28451da177e4SLinus Torvalds ++len; 28461da177e4SLinus Torvalds } 28471da177e4SLinus Torvalds } 28481da177e4SLinus Torvalds } 28491da177e4SLinus Torvalds if (compressing) { 2850222f1806SLuiz Capitulino *s++ = ':'; 2851222f1806SLuiz Capitulino ++len; 28521da177e4SLinus Torvalds } 28531da177e4SLinus Torvalds *s = 0; 28541da177e4SLinus Torvalds return len; 28551da177e4SLinus Torvalds } 28561da177e4SLinus Torvalds 28571da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev, 28581da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 28591da177e4SLinus Torvalds { 28601da177e4SLinus Torvalds struct sk_buff *skb = NULL; 28611da177e4SLinus Torvalds __u8 *eth; 28621da177e4SLinus Torvalds struct udphdr *udph; 28631da177e4SLinus Torvalds int datalen; 28641da177e4SLinus Torvalds struct ipv6hdr *iph; 28651da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 2866d5f1ce9aSStephen Hemminger __be16 protocol = htons(ETH_P_IPV6); 2867ca6549afSSteven Whitehouse __be32 *mpls; 286834954ddcSFrancesco Fondelli __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */ 286934954ddcSFrancesco Fondelli __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */ 287034954ddcSFrancesco Fondelli __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */ 287134954ddcSFrancesco Fondelli __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */ 2872fd2ea0a7SDavid S. Miller u16 queue_map; 2873ca6549afSSteven Whitehouse 2874ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2875d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_MPLS_UC); 28761da177e4SLinus Torvalds 287734954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) 2878d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_8021Q); 287934954ddcSFrancesco Fondelli 288064053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 288164053beeSRobert Olsson * fields. 288264053beeSRobert Olsson */ 2883fd2ea0a7SDavid S. Miller queue_map = pkt_dev->cur_queue_map; 288464053beeSRobert Olsson mod_cur_headers(pkt_dev); 288564053beeSRobert Olsson 2886ca6549afSSteven Whitehouse skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 + 288716dab72fSJamal Hadi Salim pkt_dev->pkt_overhead, GFP_ATOMIC); 28881da177e4SLinus Torvalds if (!skb) { 28891da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 28901da177e4SLinus Torvalds return NULL; 28911da177e4SLinus Torvalds } 28921da177e4SLinus Torvalds 28931da177e4SLinus Torvalds skb_reserve(skb, 16); 28941da177e4SLinus Torvalds 28951da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 28961da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 2897ca6549afSSteven Whitehouse mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32)); 2898ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2899ca6549afSSteven Whitehouse mpls_push(mpls, pkt_dev); 290034954ddcSFrancesco Fondelli 290134954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) { 290234954ddcSFrancesco Fondelli if (pkt_dev->svlan_id != 0xffff) { 290334954ddcSFrancesco Fondelli svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 29040f37c605SAl Viro *svlan_tci = build_tci(pkt_dev->svlan_id, 29050f37c605SAl Viro pkt_dev->svlan_cfi, 29060f37c605SAl Viro pkt_dev->svlan_p); 290734954ddcSFrancesco Fondelli svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2908d5f1ce9aSStephen Hemminger *svlan_encapsulated_proto = htons(ETH_P_8021Q); 290934954ddcSFrancesco Fondelli } 291034954ddcSFrancesco Fondelli vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 29110f37c605SAl Viro *vlan_tci = build_tci(pkt_dev->vlan_id, 29120f37c605SAl Viro pkt_dev->vlan_cfi, 29130f37c605SAl Viro pkt_dev->vlan_p); 291434954ddcSFrancesco Fondelli vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2915d5f1ce9aSStephen Hemminger *vlan_encapsulated_proto = htons(ETH_P_IPV6); 291634954ddcSFrancesco Fondelli } 291734954ddcSFrancesco Fondelli 291827a884dcSArnaldo Carvalho de Melo skb->network_header = skb->tail; 2919b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct ipv6hdr); 2920ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr)); 2921fd2ea0a7SDavid S. Miller skb_set_queue_mapping(skb, queue_map); 2922ddc7b8e3SArnaldo Carvalho de Melo iph = ipv6_hdr(skb); 2923ddc7b8e3SArnaldo Carvalho de Melo udph = udp_hdr(skb); 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 2926252e3346SAl Viro *(__be16 *) & eth[12] = protocol; 29271da177e4SLinus Torvalds 2928ca6549afSSteven Whitehouse /* Eth + IPh + UDPh + mpls */ 2929ca6549afSSteven Whitehouse datalen = pkt_dev->cur_pkt_size - 14 - 2930ca6549afSSteven Whitehouse sizeof(struct ipv6hdr) - sizeof(struct udphdr) - 293116dab72fSJamal Hadi Salim pkt_dev->pkt_overhead; 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) { 29341da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 29351da177e4SLinus Torvalds if (net_ratelimit()) 2936222f1806SLuiz Capitulino printk(KERN_INFO "pktgen: increased datalen to %d\n", 2937222f1806SLuiz Capitulino datalen); 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds 29401da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 29411da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 29421da177e4SLinus Torvalds udph->len = htons(datalen + sizeof(struct udphdr)); 29431da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 29441da177e4SLinus Torvalds 2945d5f1ce9aSStephen Hemminger *(__be32 *) iph = htonl(0x60000000); /* Version + flow */ 29461da177e4SLinus Torvalds 29471ca7768cSFrancesco Fondelli if (pkt_dev->traffic_class) { 29481ca7768cSFrancesco Fondelli /* Version + traffic class + flow (0) */ 2949252e3346SAl Viro *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20)); 29501ca7768cSFrancesco Fondelli } 29511ca7768cSFrancesco Fondelli 29521da177e4SLinus Torvalds iph->hop_limit = 32; 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds iph->payload_len = htons(sizeof(struct udphdr) + datalen); 29551da177e4SLinus Torvalds iph->nexthdr = IPPROTO_UDP; 29561da177e4SLinus Torvalds 29571da177e4SLinus Torvalds ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr); 29581da177e4SLinus Torvalds ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr); 29591da177e4SLinus Torvalds 2960b0e380b1SArnaldo Carvalho de Melo skb->mac_header = (skb->network_header - ETH_HLEN - 296116dab72fSJamal Hadi Salim pkt_dev->pkt_overhead); 2962ca6549afSSteven Whitehouse skb->protocol = protocol; 29631da177e4SLinus Torvalds skb->dev = odev; 29641da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 29671da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 29681da177e4SLinus Torvalds else { 29691da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 29701da177e4SLinus Torvalds int i; 29711da177e4SLinus Torvalds 29721da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 29751da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 29761da177e4SLinus Torvalds if (datalen > frags * PAGE_SIZE) { 29771da177e4SLinus Torvalds skb_put(skb, datalen - frags * PAGE_SIZE); 29781da177e4SLinus Torvalds datalen = frags * PAGE_SIZE; 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds 29811da177e4SLinus Torvalds i = 0; 29821da177e4SLinus Torvalds while (datalen > 0) { 29831da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 29841da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 29851da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 29861da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 29871da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 29881da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 29891da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 29901da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 29911da177e4SLinus Torvalds i++; 29921da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 29931da177e4SLinus Torvalds } 29941da177e4SLinus Torvalds 29951da177e4SLinus Torvalds while (i < frags) { 29961da177e4SLinus Torvalds int rem; 29971da177e4SLinus Torvalds 29981da177e4SLinus Torvalds if (i == 0) 29991da177e4SLinus Torvalds break; 30001da177e4SLinus Torvalds 30011da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 30021da177e4SLinus Torvalds if (rem == 0) 30031da177e4SLinus Torvalds break; 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 30061da177e4SLinus Torvalds 3007222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i] = 3008222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1]; 30091da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 3010222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page = 3011222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].page; 3012222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page_offset += 3013222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].size; 30141da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 30151da177e4SLinus Torvalds i++; 30161da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds } 30191da177e4SLinus Torvalds 30201da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 30211da177e4SLinus Torvalds /* should we update cloned packets too ? */ 30221da177e4SLinus Torvalds if (pgh) { 30231da177e4SLinus Torvalds struct timeval timestamp; 30241da177e4SLinus Torvalds 30251da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 30261da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 30271da177e4SLinus Torvalds 30281da177e4SLinus Torvalds do_gettimeofday(×tamp); 30291da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 30301da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 30311da177e4SLinus Torvalds } 303234954ddcSFrancesco Fondelli /* pkt_dev->seq_num++; FF: you really mean this? */ 30331da177e4SLinus Torvalds 30341da177e4SLinus Torvalds return skb; 30351da177e4SLinus Torvalds } 30361da177e4SLinus Torvalds 30371da177e4SLinus Torvalds static inline struct sk_buff *fill_packet(struct net_device *odev, 30381da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 30391da177e4SLinus Torvalds { 30401da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) 30411da177e4SLinus Torvalds return fill_packet_ipv6(odev, pkt_dev); 30421da177e4SLinus Torvalds else 30431da177e4SLinus Torvalds return fill_packet_ipv4(odev, pkt_dev); 30441da177e4SLinus Torvalds } 30451da177e4SLinus Torvalds 30461da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev) 30471da177e4SLinus Torvalds { 30481da177e4SLinus Torvalds pkt_dev->seq_num = 1; 30491da177e4SLinus Torvalds pkt_dev->idle_acc = 0; 30501da177e4SLinus Torvalds pkt_dev->sofar = 0; 30511da177e4SLinus Torvalds pkt_dev->tx_bytes = 0; 30521da177e4SLinus Torvalds pkt_dev->errors = 0; 30531da177e4SLinus Torvalds } 30541da177e4SLinus Torvalds 30551da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */ 30561da177e4SLinus Torvalds 30571da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t) 30581da177e4SLinus Torvalds { 3059c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 30601da177e4SLinus Torvalds int started = 0; 30611da177e4SLinus Torvalds 306225c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_run. %p\n", t); 30631da177e4SLinus Torvalds 30641da177e4SLinus Torvalds if_lock(t); 3065c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 30661da177e4SLinus Torvalds 30671da177e4SLinus Torvalds /* 30681da177e4SLinus Torvalds * setup odev and create initial packet. 30691da177e4SLinus Torvalds */ 30701da177e4SLinus Torvalds pktgen_setup_inject(pkt_dev); 30711da177e4SLinus Torvalds 30721da177e4SLinus Torvalds if (pkt_dev->odev) { 30731da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 30741da177e4SLinus Torvalds pkt_dev->running = 1; /* Cranke yeself! */ 30751da177e4SLinus Torvalds pkt_dev->skb = NULL; 30761da177e4SLinus Torvalds pkt_dev->started_at = getCurUs(); 30771da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */ 30781da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 307916dab72fSJamal Hadi Salim set_pkt_overhead(pkt_dev); 30801da177e4SLinus Torvalds 30811da177e4SLinus Torvalds strcpy(pkt_dev->result, "Starting"); 30821da177e4SLinus Torvalds started++; 3083222f1806SLuiz Capitulino } else 30841da177e4SLinus Torvalds strcpy(pkt_dev->result, "Error starting"); 30851da177e4SLinus Torvalds } 30861da177e4SLinus Torvalds if_unlock(t); 3087222f1806SLuiz Capitulino if (started) 3088222f1806SLuiz Capitulino t->control &= ~(T_STOP); 30891da177e4SLinus Torvalds } 30901da177e4SLinus Torvalds 30911da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void) 30921da177e4SLinus Torvalds { 3093cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 30941da177e4SLinus Torvalds 309525c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n"); 30961da177e4SLinus Torvalds 30976146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 3098cdcdbe0bSLuiz Capitulino 3099cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 310095ed63f7SArthur Kepner t->control |= T_STOP; 3101cdcdbe0bSLuiz Capitulino 31026146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 31031da177e4SLinus Torvalds } 31041da177e4SLinus Torvalds 31051da177e4SLinus Torvalds static int thread_is_running(struct pktgen_thread *t) 31061da177e4SLinus Torvalds { 3107c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 31081da177e4SLinus Torvalds int res = 0; 31091da177e4SLinus Torvalds 3110c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 3111c26a8016SLuiz Capitulino if (pkt_dev->running) { 31121da177e4SLinus Torvalds res = 1; 31131da177e4SLinus Torvalds break; 31141da177e4SLinus Torvalds } 31151da177e4SLinus Torvalds return res; 31161da177e4SLinus Torvalds } 31171da177e4SLinus Torvalds 31181da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t) 31191da177e4SLinus Torvalds { 31201da177e4SLinus Torvalds if_lock(t); 31211da177e4SLinus Torvalds 31221da177e4SLinus Torvalds while (thread_is_running(t)) { 31231da177e4SLinus Torvalds 31241da177e4SLinus Torvalds if_unlock(t); 31251da177e4SLinus Torvalds 31261da177e4SLinus Torvalds msleep_interruptible(100); 31271da177e4SLinus Torvalds 31281da177e4SLinus Torvalds if (signal_pending(current)) 31291da177e4SLinus Torvalds goto signal; 31301da177e4SLinus Torvalds if_lock(t); 31311da177e4SLinus Torvalds } 31321da177e4SLinus Torvalds if_unlock(t); 31331da177e4SLinus Torvalds return 1; 31341da177e4SLinus Torvalds signal: 31351da177e4SLinus Torvalds return 0; 31361da177e4SLinus Torvalds } 31371da177e4SLinus Torvalds 31381da177e4SLinus Torvalds static int pktgen_wait_all_threads_run(void) 31391da177e4SLinus Torvalds { 3140cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 31411da177e4SLinus Torvalds int sig = 1; 31421da177e4SLinus Torvalds 31436146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 3144cdcdbe0bSLuiz Capitulino 3145cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) { 31461da177e4SLinus Torvalds sig = pktgen_wait_thread_run(t); 3147222f1806SLuiz Capitulino if (sig == 0) 3148222f1806SLuiz Capitulino break; 31491da177e4SLinus Torvalds } 3150cdcdbe0bSLuiz Capitulino 3151cdcdbe0bSLuiz Capitulino if (sig == 0) 3152cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 31531da177e4SLinus Torvalds t->control |= (T_STOP); 3154cdcdbe0bSLuiz Capitulino 31556146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 31561da177e4SLinus Torvalds return sig; 31571da177e4SLinus Torvalds } 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds static void pktgen_run_all_threads(void) 31601da177e4SLinus Torvalds { 3161cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 31621da177e4SLinus Torvalds 316325c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_run_all_threads.\n"); 31641da177e4SLinus Torvalds 31656146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 31661da177e4SLinus Torvalds 3167cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 31681da177e4SLinus Torvalds t->control |= (T_RUN); 3169cdcdbe0bSLuiz Capitulino 31706146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 31711da177e4SLinus Torvalds 3172121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 31731da177e4SLinus Torvalds 31741da177e4SLinus Torvalds pktgen_wait_all_threads_run(); 31751da177e4SLinus Torvalds } 31761da177e4SLinus Torvalds 31771da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags) 31781da177e4SLinus Torvalds { 31791da177e4SLinus Torvalds __u64 total_us, bps, mbps, pps, idle; 31801da177e4SLinus Torvalds char *p = pkt_dev->result; 31811da177e4SLinus Torvalds 31821da177e4SLinus Torvalds total_us = pkt_dev->stopped_at - pkt_dev->started_at; 31831da177e4SLinus Torvalds 31841da177e4SLinus Torvalds idle = pkt_dev->idle_acc; 31851da177e4SLinus Torvalds 31861da177e4SLinus Torvalds p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n", 31871da177e4SLinus Torvalds (unsigned long long)total_us, 31881da177e4SLinus Torvalds (unsigned long long)(total_us - idle), 31891da177e4SLinus Torvalds (unsigned long long)idle, 31901da177e4SLinus Torvalds (unsigned long long)pkt_dev->sofar, 31911da177e4SLinus Torvalds pkt_dev->cur_pkt_size, nr_frags); 31921da177e4SLinus Torvalds 31931da177e4SLinus Torvalds pps = pkt_dev->sofar * USEC_PER_SEC; 31941da177e4SLinus Torvalds 31951da177e4SLinus Torvalds while ((total_us >> 32) != 0) { 31961da177e4SLinus Torvalds pps >>= 1; 31971da177e4SLinus Torvalds total_us >>= 1; 31981da177e4SLinus Torvalds } 31991da177e4SLinus Torvalds 32001da177e4SLinus Torvalds do_div(pps, total_us); 32011da177e4SLinus Torvalds 32021da177e4SLinus Torvalds bps = pps * 8 * pkt_dev->cur_pkt_size; 32031da177e4SLinus Torvalds 32041da177e4SLinus Torvalds mbps = bps; 32051da177e4SLinus Torvalds do_div(mbps, 1000000); 32061da177e4SLinus Torvalds p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu", 32071da177e4SLinus Torvalds (unsigned long long)pps, 32081da177e4SLinus Torvalds (unsigned long long)mbps, 32091da177e4SLinus Torvalds (unsigned long long)bps, 32101da177e4SLinus Torvalds (unsigned long long)pkt_dev->errors); 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds 32131da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */ 32141da177e4SLinus Torvalds 32151da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev) 32161da177e4SLinus Torvalds { 3217222f1806SLuiz Capitulino int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1; 32181da177e4SLinus Torvalds 32191da177e4SLinus Torvalds if (!pkt_dev->running) { 322025a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: interface: %s is already " 322125a8b254SDavid S. Miller "stopped\n", pkt_dev->odev->name); 32221da177e4SLinus Torvalds return -EINVAL; 32231da177e4SLinus Torvalds } 32241da177e4SLinus Torvalds 32251da177e4SLinus Torvalds pkt_dev->stopped_at = getCurUs(); 32261da177e4SLinus Torvalds pkt_dev->running = 0; 32271da177e4SLinus Torvalds 322895ed63f7SArthur Kepner show_results(pkt_dev, nr_frags); 32291da177e4SLinus Torvalds 32301da177e4SLinus Torvalds return 0; 32311da177e4SLinus Torvalds } 32321da177e4SLinus Torvalds 32331da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t) 32341da177e4SLinus Torvalds { 3235c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev, *best = NULL; 32361da177e4SLinus Torvalds 32371da177e4SLinus Torvalds if_lock(t); 32381da177e4SLinus Torvalds 3239c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 3240c26a8016SLuiz Capitulino if (!pkt_dev->running) 3241222f1806SLuiz Capitulino continue; 3242222f1806SLuiz Capitulino if (best == NULL) 3243c26a8016SLuiz Capitulino best = pkt_dev; 3244c26a8016SLuiz Capitulino else if (pkt_dev->next_tx_us < best->next_tx_us) 3245c26a8016SLuiz Capitulino best = pkt_dev; 32461da177e4SLinus Torvalds } 32471da177e4SLinus Torvalds if_unlock(t); 32481da177e4SLinus Torvalds return best; 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds 3251222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t) 3252222f1806SLuiz Capitulino { 3253c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 32541da177e4SLinus Torvalds 325525c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_stop\n"); 32561da177e4SLinus Torvalds 32571da177e4SLinus Torvalds if_lock(t); 32581da177e4SLinus Torvalds 3259c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 3260c26a8016SLuiz Capitulino pktgen_stop_device(pkt_dev); 3261c26a8016SLuiz Capitulino if (pkt_dev->skb) 3262c26a8016SLuiz Capitulino kfree_skb(pkt_dev->skb); 326395ed63f7SArthur Kepner 3264c26a8016SLuiz Capitulino pkt_dev->skb = NULL; 326595ed63f7SArthur Kepner } 326695ed63f7SArthur Kepner 326795ed63f7SArthur Kepner if_unlock(t); 326895ed63f7SArthur Kepner } 326995ed63f7SArthur Kepner 327095ed63f7SArthur Kepner /* 327195ed63f7SArthur Kepner * one of our devices needs to be removed - find it 327295ed63f7SArthur Kepner * and remove it 327395ed63f7SArthur Kepner */ 327495ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t) 327595ed63f7SArthur Kepner { 3276c26a8016SLuiz Capitulino struct list_head *q, *n; 3277c26a8016SLuiz Capitulino struct pktgen_dev *cur; 327895ed63f7SArthur Kepner 327925c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_rem_one_if\n"); 328095ed63f7SArthur Kepner 328195ed63f7SArthur Kepner if_lock(t); 328295ed63f7SArthur Kepner 3283c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3284c26a8016SLuiz Capitulino cur = list_entry(q, struct pktgen_dev, list); 328595ed63f7SArthur Kepner 3286222f1806SLuiz Capitulino if (!cur->removal_mark) 3287222f1806SLuiz Capitulino continue; 328895ed63f7SArthur Kepner 328995ed63f7SArthur Kepner if (cur->skb) 329095ed63f7SArthur Kepner kfree_skb(cur->skb); 329195ed63f7SArthur Kepner cur->skb = NULL; 329295ed63f7SArthur Kepner 329395ed63f7SArthur Kepner pktgen_remove_device(t, cur); 329495ed63f7SArthur Kepner 329595ed63f7SArthur Kepner break; 329695ed63f7SArthur Kepner } 32971da177e4SLinus Torvalds 32981da177e4SLinus Torvalds if_unlock(t); 32991da177e4SLinus Torvalds } 33001da177e4SLinus Torvalds 33011da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t) 33021da177e4SLinus Torvalds { 3303c26a8016SLuiz Capitulino struct list_head *q, *n; 3304c26a8016SLuiz Capitulino struct pktgen_dev *cur; 33051da177e4SLinus Torvalds 33061da177e4SLinus Torvalds /* Remove all devices, free mem */ 33071da177e4SLinus Torvalds 330825c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_rem_all_ifs\n"); 33091da177e4SLinus Torvalds if_lock(t); 33101da177e4SLinus Torvalds 3311c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3312c26a8016SLuiz Capitulino cur = list_entry(q, struct pktgen_dev, list); 331395ed63f7SArthur Kepner 331495ed63f7SArthur Kepner if (cur->skb) 331595ed63f7SArthur Kepner kfree_skb(cur->skb); 331695ed63f7SArthur Kepner cur->skb = NULL; 331795ed63f7SArthur Kepner 33181da177e4SLinus Torvalds pktgen_remove_device(t, cur); 33191da177e4SLinus Torvalds } 33201da177e4SLinus Torvalds 33211da177e4SLinus Torvalds if_unlock(t); 33221da177e4SLinus Torvalds } 33231da177e4SLinus Torvalds 33241da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t) 33251da177e4SLinus Torvalds { 33261da177e4SLinus Torvalds /* Remove from the thread list */ 33271da177e4SLinus Torvalds 3328ee74baa7SDavid S. Miller remove_proc_entry(t->tsk->comm, pg_proc_dir); 33291da177e4SLinus Torvalds 33306146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 33311da177e4SLinus Torvalds 3332cdcdbe0bSLuiz Capitulino list_del(&t->th_list); 3333cdcdbe0bSLuiz Capitulino 33346146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 33351da177e4SLinus Torvalds } 33361da177e4SLinus Torvalds 33371da177e4SLinus Torvalds static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev) 33381da177e4SLinus Torvalds { 33391da177e4SLinus Torvalds struct net_device *odev = NULL; 3340fd2ea0a7SDavid S. Miller struct netdev_queue *txq; 33411da177e4SLinus Torvalds __u64 idle_start = 0; 3342fd2ea0a7SDavid S. Miller u16 queue_map; 33431da177e4SLinus Torvalds int ret; 33441da177e4SLinus Torvalds 33451da177e4SLinus Torvalds odev = pkt_dev->odev; 33461da177e4SLinus Torvalds 33471da177e4SLinus Torvalds if (pkt_dev->delay_us || pkt_dev->delay_ns) { 33481da177e4SLinus Torvalds u64 now; 33491da177e4SLinus Torvalds 33501da177e4SLinus Torvalds now = getCurUs(); 33511da177e4SLinus Torvalds if (now < pkt_dev->next_tx_us) 33521da177e4SLinus Torvalds spin(pkt_dev, pkt_dev->next_tx_us); 33531da177e4SLinus Torvalds 33541da177e4SLinus Torvalds /* This is max DELAY, this has special meaning of 33551da177e4SLinus Torvalds * "never transmit" 33561da177e4SLinus Torvalds */ 33571da177e4SLinus Torvalds if (pkt_dev->delay_us == 0x7FFFFFFF) { 33581da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us; 33591da177e4SLinus Torvalds pkt_dev->next_tx_ns = pkt_dev->delay_ns; 33601da177e4SLinus Torvalds goto out; 33611da177e4SLinus Torvalds } 33621da177e4SLinus Torvalds } 33631da177e4SLinus Torvalds 3364fd2ea0a7SDavid S. Miller if (!pkt_dev->skb) { 3365fd2ea0a7SDavid S. Miller set_cur_queue_map(pkt_dev); 3366fd2ea0a7SDavid S. Miller queue_map = pkt_dev->cur_queue_map; 3367fd2ea0a7SDavid S. Miller } else { 3368fd2ea0a7SDavid S. Miller queue_map = skb_get_queue_mapping(pkt_dev->skb); 3369fd2ea0a7SDavid S. Miller } 3370fd2ea0a7SDavid S. Miller 3371fd2ea0a7SDavid S. Miller txq = netdev_get_tx_queue(odev, queue_map); 3372fd2ea0a7SDavid S. Miller if (netif_tx_queue_stopped(txq) || 3373c3f26a26SDavid S. Miller netif_tx_queue_frozen(txq) || 3374f25f4e44SPeter P Waskiewicz Jr need_resched()) { 33751da177e4SLinus Torvalds idle_start = getCurUs(); 33761da177e4SLinus Torvalds 33771da177e4SLinus Torvalds if (!netif_running(odev)) { 33781da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 337995ed63f7SArthur Kepner if (pkt_dev->skb) 338095ed63f7SArthur Kepner kfree_skb(pkt_dev->skb); 338195ed63f7SArthur Kepner pkt_dev->skb = NULL; 33821da177e4SLinus Torvalds goto out; 33831da177e4SLinus Torvalds } 33841da177e4SLinus Torvalds if (need_resched()) 33851da177e4SLinus Torvalds schedule(); 33861da177e4SLinus Torvalds 33871da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 33881da177e4SLinus Torvalds 3389c3f26a26SDavid S. Miller if (netif_tx_queue_stopped(txq) || 3390c3f26a26SDavid S. Miller netif_tx_queue_frozen(txq)) { 33911da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 33921da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 33931da177e4SLinus Torvalds goto out; /* Try the next interface */ 33941da177e4SLinus Torvalds } 33951da177e4SLinus Torvalds } 33961da177e4SLinus Torvalds 33971da177e4SLinus Torvalds if (pkt_dev->last_ok || !pkt_dev->skb) { 3398222f1806SLuiz Capitulino if ((++pkt_dev->clone_count >= pkt_dev->clone_skb) 3399222f1806SLuiz Capitulino || (!pkt_dev->skb)) { 34001da177e4SLinus Torvalds /* build a new pkt */ 34011da177e4SLinus Torvalds if (pkt_dev->skb) 34021da177e4SLinus Torvalds kfree_skb(pkt_dev->skb); 34031da177e4SLinus Torvalds 34041da177e4SLinus Torvalds pkt_dev->skb = fill_packet(odev, pkt_dev); 34051da177e4SLinus Torvalds if (pkt_dev->skb == NULL) { 340625a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: couldn't " 340725a8b254SDavid S. Miller "allocate skb in fill_packet.\n"); 34081da177e4SLinus Torvalds schedule(); 34091da177e4SLinus Torvalds pkt_dev->clone_count--; /* back out increment, OOM */ 34101da177e4SLinus Torvalds goto out; 34111da177e4SLinus Torvalds } 34121da177e4SLinus Torvalds pkt_dev->allocated_skbs++; 34131da177e4SLinus Torvalds pkt_dev->clone_count = 0; /* reset counter */ 34141da177e4SLinus Torvalds } 34151da177e4SLinus Torvalds } 34161da177e4SLinus Torvalds 3417fd2ea0a7SDavid S. Miller /* fill_packet() might have changed the queue */ 3418fd2ea0a7SDavid S. Miller queue_map = skb_get_queue_mapping(pkt_dev->skb); 3419fd2ea0a7SDavid S. Miller txq = netdev_get_tx_queue(odev, queue_map); 3420fd2ea0a7SDavid S. Miller 3421fd2ea0a7SDavid S. Miller __netif_tx_lock_bh(txq); 3422c3f26a26SDavid S. Miller if (!netif_tx_queue_stopped(txq) && 3423c3f26a26SDavid S. Miller !netif_tx_queue_frozen(txq)) { 34241da177e4SLinus Torvalds 34251da177e4SLinus Torvalds atomic_inc(&(pkt_dev->skb->users)); 34261da177e4SLinus Torvalds retry_now: 34271da177e4SLinus Torvalds ret = odev->hard_start_xmit(pkt_dev->skb, odev); 34281da177e4SLinus Torvalds if (likely(ret == NETDEV_TX_OK)) { 34291da177e4SLinus Torvalds pkt_dev->last_ok = 1; 34301da177e4SLinus Torvalds pkt_dev->sofar++; 34311da177e4SLinus Torvalds pkt_dev->seq_num++; 34321da177e4SLinus Torvalds pkt_dev->tx_bytes += pkt_dev->cur_pkt_size; 34331da177e4SLinus Torvalds 34341da177e4SLinus Torvalds } else if (ret == NETDEV_TX_LOCKED 34351da177e4SLinus Torvalds && (odev->features & NETIF_F_LLTX)) { 34361da177e4SLinus Torvalds cpu_relax(); 34371da177e4SLinus Torvalds goto retry_now; 34381da177e4SLinus Torvalds } else { /* Retry it next time */ 34391da177e4SLinus Torvalds 34401da177e4SLinus Torvalds atomic_dec(&(pkt_dev->skb->users)); 34411da177e4SLinus Torvalds 34421da177e4SLinus Torvalds if (debug && net_ratelimit()) 34431da177e4SLinus Torvalds printk(KERN_INFO "pktgen: Hard xmit error\n"); 34441da177e4SLinus Torvalds 34451da177e4SLinus Torvalds pkt_dev->errors++; 34461da177e4SLinus Torvalds pkt_dev->last_ok = 0; 34471da177e4SLinus Torvalds } 34481da177e4SLinus Torvalds 34491da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); 34501da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 34511da177e4SLinus Torvalds 34521da177e4SLinus Torvalds pkt_dev->next_tx_us += pkt_dev->delay_us; 34531da177e4SLinus Torvalds pkt_dev->next_tx_ns += pkt_dev->delay_ns; 34541da177e4SLinus Torvalds 34551da177e4SLinus Torvalds if (pkt_dev->next_tx_ns > 1000) { 34561da177e4SLinus Torvalds pkt_dev->next_tx_us++; 34571da177e4SLinus Torvalds pkt_dev->next_tx_ns -= 1000; 34581da177e4SLinus Torvalds } 34591da177e4SLinus Torvalds } 34601da177e4SLinus Torvalds 34611da177e4SLinus Torvalds else { /* Retry it next time */ 34621da177e4SLinus Torvalds pkt_dev->last_ok = 0; 34631da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 34641da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 34651da177e4SLinus Torvalds } 34661da177e4SLinus Torvalds 3467fd2ea0a7SDavid S. Miller __netif_tx_unlock_bh(txq); 34681da177e4SLinus Torvalds 34691da177e4SLinus Torvalds /* If pkt_dev->count is zero, then run forever */ 34701da177e4SLinus Torvalds if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) { 34711da177e4SLinus Torvalds if (atomic_read(&(pkt_dev->skb->users)) != 1) { 34721da177e4SLinus Torvalds idle_start = getCurUs(); 34731da177e4SLinus Torvalds while (atomic_read(&(pkt_dev->skb->users)) != 1) { 34741da177e4SLinus Torvalds if (signal_pending(current)) { 34751da177e4SLinus Torvalds break; 34761da177e4SLinus Torvalds } 34771da177e4SLinus Torvalds schedule(); 34781da177e4SLinus Torvalds } 34791da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 34801da177e4SLinus Torvalds } 34811da177e4SLinus Torvalds 34821da177e4SLinus Torvalds /* Done with this */ 34831da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 348495ed63f7SArthur Kepner if (pkt_dev->skb) 348595ed63f7SArthur Kepner kfree_skb(pkt_dev->skb); 348695ed63f7SArthur Kepner pkt_dev->skb = NULL; 34871da177e4SLinus Torvalds } 34881da177e4SLinus Torvalds out:; 34891da177e4SLinus Torvalds } 34901da177e4SLinus Torvalds 34911da177e4SLinus Torvalds /* 34921da177e4SLinus Torvalds * Main loop of the thread goes here 34931da177e4SLinus Torvalds */ 34941da177e4SLinus Torvalds 3495ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg) 34961da177e4SLinus Torvalds { 34971da177e4SLinus Torvalds DEFINE_WAIT(wait); 3498ee74baa7SDavid S. Miller struct pktgen_thread *t = arg; 34991da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 35001da177e4SLinus Torvalds int cpu = t->cpu; 35011da177e4SLinus Torvalds 3502ee74baa7SDavid S. Miller BUG_ON(smp_processor_id() != cpu); 35031da177e4SLinus Torvalds 35041da177e4SLinus Torvalds init_waitqueue_head(&t->queue); 3505d3ede327SDenis V. Lunev complete(&t->start_done); 35061da177e4SLinus Torvalds 3507ba25f9dcSPavel Emelyanov pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current)); 35081da177e4SLinus Torvalds 3509ee74baa7SDavid S. Miller set_current_state(TASK_INTERRUPTIBLE); 35101da177e4SLinus Torvalds 351183144186SRafael J. Wysocki set_freezable(); 351283144186SRafael J. Wysocki 3513ee74baa7SDavid S. Miller while (!kthread_should_stop()) { 3514ee74baa7SDavid S. Miller pkt_dev = next_to_run(t); 3515ee74baa7SDavid S. Miller 3516ee74baa7SDavid S. Miller if (!pkt_dev && 3517ee74baa7SDavid S. Miller (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV)) 3518ee74baa7SDavid S. Miller == 0) { 3519ee74baa7SDavid S. Miller prepare_to_wait(&(t->queue), &wait, 3520ee74baa7SDavid S. Miller TASK_INTERRUPTIBLE); 3521ee74baa7SDavid S. Miller schedule_timeout(HZ / 10); 3522ee74baa7SDavid S. Miller finish_wait(&(t->queue), &wait); 3523ee74baa7SDavid S. Miller } 35241da177e4SLinus Torvalds 35251da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 35261da177e4SLinus Torvalds 3527b163911fSRobert Olsson if (pkt_dev) 35281da177e4SLinus Torvalds pktgen_xmit(pkt_dev); 35291da177e4SLinus Torvalds 35301da177e4SLinus Torvalds if (t->control & T_STOP) { 35311da177e4SLinus Torvalds pktgen_stop(t); 35321da177e4SLinus Torvalds t->control &= ~(T_STOP); 35331da177e4SLinus Torvalds } 35341da177e4SLinus Torvalds 35351da177e4SLinus Torvalds if (t->control & T_RUN) { 35361da177e4SLinus Torvalds pktgen_run(t); 35371da177e4SLinus Torvalds t->control &= ~(T_RUN); 35381da177e4SLinus Torvalds } 35391da177e4SLinus Torvalds 354095ed63f7SArthur Kepner if (t->control & T_REMDEVALL) { 35411da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 354295ed63f7SArthur Kepner t->control &= ~(T_REMDEVALL); 354395ed63f7SArthur Kepner } 354495ed63f7SArthur Kepner 354595ed63f7SArthur Kepner if (t->control & T_REMDEV) { 354695ed63f7SArthur Kepner pktgen_rem_one_if(t); 35471da177e4SLinus Torvalds t->control &= ~(T_REMDEV); 35481da177e4SLinus Torvalds } 35491da177e4SLinus Torvalds 355009fe3ef4SAndrew Morton try_to_freeze(); 355109fe3ef4SAndrew Morton 3552ee74baa7SDavid S. Miller set_current_state(TASK_INTERRUPTIBLE); 35531da177e4SLinus Torvalds } 35541da177e4SLinus Torvalds 355525c4e53aSStephen Hemminger pr_debug("pktgen: %s stopping all device\n", t->tsk->comm); 35561da177e4SLinus Torvalds pktgen_stop(t); 35571da177e4SLinus Torvalds 355825c4e53aSStephen Hemminger pr_debug("pktgen: %s removing all device\n", t->tsk->comm); 35591da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 35601da177e4SLinus Torvalds 356125c4e53aSStephen Hemminger pr_debug("pktgen: %s removing thread.\n", t->tsk->comm); 35621da177e4SLinus Torvalds pktgen_rem_thread(t); 3563cdcdbe0bSLuiz Capitulino 3564ee74baa7SDavid S. Miller return 0; 35651da177e4SLinus Torvalds } 35661da177e4SLinus Torvalds 3567222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, 3568222f1806SLuiz Capitulino const char *ifname) 35691da177e4SLinus Torvalds { 3570c26a8016SLuiz Capitulino struct pktgen_dev *p, *pkt_dev = NULL; 35711da177e4SLinus Torvalds if_lock(t); 35721da177e4SLinus Torvalds 3573c26a8016SLuiz Capitulino list_for_each_entry(p, &t->if_list, list) 357439df232fSStephen Hemminger if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) { 3575c26a8016SLuiz Capitulino pkt_dev = p; 35761da177e4SLinus Torvalds break; 35771da177e4SLinus Torvalds } 35781da177e4SLinus Torvalds 35791da177e4SLinus Torvalds if_unlock(t); 358025c4e53aSStephen Hemminger pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev); 35811da177e4SLinus Torvalds return pkt_dev; 35821da177e4SLinus Torvalds } 35831da177e4SLinus Torvalds 35841da177e4SLinus Torvalds /* 35851da177e4SLinus Torvalds * Adds a dev at front of if_list. 35861da177e4SLinus Torvalds */ 35871da177e4SLinus Torvalds 3588222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t, 3589222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 35901da177e4SLinus Torvalds { 35911da177e4SLinus Torvalds int rv = 0; 35921da177e4SLinus Torvalds 35931da177e4SLinus Torvalds if_lock(t); 35941da177e4SLinus Torvalds 35951da177e4SLinus Torvalds if (pkt_dev->pg_thread) { 359625a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: already assigned " 359725a8b254SDavid S. Miller "to a thread.\n"); 35981da177e4SLinus Torvalds rv = -EBUSY; 35991da177e4SLinus Torvalds goto out; 36001da177e4SLinus Torvalds } 3601c26a8016SLuiz Capitulino 3602c26a8016SLuiz Capitulino list_add(&pkt_dev->list, &t->if_list); 36031da177e4SLinus Torvalds pkt_dev->pg_thread = t; 36041da177e4SLinus Torvalds pkt_dev->running = 0; 36051da177e4SLinus Torvalds 36061da177e4SLinus Torvalds out: 36071da177e4SLinus Torvalds if_unlock(t); 36081da177e4SLinus Torvalds return rv; 36091da177e4SLinus Torvalds } 36101da177e4SLinus Torvalds 36111da177e4SLinus Torvalds /* Called under thread lock */ 36121da177e4SLinus Torvalds 36131da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname) 36141da177e4SLinus Torvalds { 36151da177e4SLinus Torvalds struct pktgen_dev *pkt_dev; 361639df232fSStephen Hemminger int err; 36171da177e4SLinus Torvalds 36181da177e4SLinus Torvalds /* We don't allow a device to be on several threads */ 36191da177e4SLinus Torvalds 3620d50a6b56SStephen Hemminger pkt_dev = __pktgen_NN_threads(ifname, FIND); 3621d50a6b56SStephen Hemminger if (pkt_dev) { 362225a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: interface already used.\n"); 3623d50a6b56SStephen Hemminger return -EBUSY; 3624d50a6b56SStephen Hemminger } 36251da177e4SLinus Torvalds 36262845b63bSStephen Hemminger pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL); 36271da177e4SLinus Torvalds if (!pkt_dev) 36281da177e4SLinus Torvalds return -ENOMEM; 36291da177e4SLinus Torvalds 36301da177e4SLinus Torvalds pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state)); 36311da177e4SLinus Torvalds if (pkt_dev->flows == NULL) { 36321da177e4SLinus Torvalds kfree(pkt_dev); 36331da177e4SLinus Torvalds return -ENOMEM; 36341da177e4SLinus Torvalds } 36351da177e4SLinus Torvalds memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state)); 36361da177e4SLinus Torvalds 363795ed63f7SArthur Kepner pkt_dev->removal_mark = 0; 36381da177e4SLinus Torvalds pkt_dev->min_pkt_size = ETH_ZLEN; 36391da177e4SLinus Torvalds pkt_dev->max_pkt_size = ETH_ZLEN; 36401da177e4SLinus Torvalds pkt_dev->nfrags = 0; 36411da177e4SLinus Torvalds pkt_dev->clone_skb = pg_clone_skb_d; 36421da177e4SLinus Torvalds pkt_dev->delay_us = pg_delay_d / 1000; 36431da177e4SLinus Torvalds pkt_dev->delay_ns = pg_delay_d % 1000; 36441da177e4SLinus Torvalds pkt_dev->count = pg_count_d; 36451da177e4SLinus Torvalds pkt_dev->sofar = 0; 36461da177e4SLinus Torvalds pkt_dev->udp_src_min = 9; /* sink port */ 36471da177e4SLinus Torvalds pkt_dev->udp_src_max = 9; 36481da177e4SLinus Torvalds pkt_dev->udp_dst_min = 9; 36491da177e4SLinus Torvalds pkt_dev->udp_dst_max = 9; 36501da177e4SLinus Torvalds 365134954ddcSFrancesco Fondelli pkt_dev->vlan_p = 0; 365234954ddcSFrancesco Fondelli pkt_dev->vlan_cfi = 0; 365334954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; 365434954ddcSFrancesco Fondelli pkt_dev->svlan_p = 0; 365534954ddcSFrancesco Fondelli pkt_dev->svlan_cfi = 0; 365634954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 365734954ddcSFrancesco Fondelli 365839df232fSStephen Hemminger err = pktgen_setup_dev(pkt_dev, ifname); 365939df232fSStephen Hemminger if (err) 366039df232fSStephen Hemminger goto out1; 36611da177e4SLinus Torvalds 36625efdccbcSDenis V. Lunev pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir, 36635efdccbcSDenis V. Lunev &pktgen_if_fops, pkt_dev); 366439df232fSStephen Hemminger if (!pkt_dev->entry) { 366525a8b254SDavid S. Miller printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n", 3666d50a6b56SStephen Hemminger PG_PROC_DIR, ifname); 366739df232fSStephen Hemminger err = -EINVAL; 366839df232fSStephen Hemminger goto out2; 366939df232fSStephen Hemminger } 3670a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 3671a553e4a6SJamal Hadi Salim pkt_dev->ipsmode = XFRM_MODE_TRANSPORT; 3672a553e4a6SJamal Hadi Salim pkt_dev->ipsproto = IPPROTO_ESP; 3673a553e4a6SJamal Hadi Salim #endif 367439df232fSStephen Hemminger 367539df232fSStephen Hemminger return add_dev_to_thread(t, pkt_dev); 367639df232fSStephen Hemminger out2: 367739df232fSStephen Hemminger dev_put(pkt_dev->odev); 367839df232fSStephen Hemminger out1: 3679a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 3680a553e4a6SJamal Hadi Salim free_SAs(pkt_dev); 3681a553e4a6SJamal Hadi Salim #endif 36821da177e4SLinus Torvalds if (pkt_dev->flows) 36831da177e4SLinus Torvalds vfree(pkt_dev->flows); 36841da177e4SLinus Torvalds kfree(pkt_dev); 368539df232fSStephen Hemminger return err; 36861da177e4SLinus Torvalds } 36871da177e4SLinus Torvalds 3688ee74baa7SDavid S. Miller static int __init pktgen_create_thread(int cpu) 36891da177e4SLinus Torvalds { 3690cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 3691d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 3692ee74baa7SDavid S. Miller struct task_struct *p; 36931da177e4SLinus Torvalds 36942845b63bSStephen Hemminger t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL); 36951da177e4SLinus Torvalds if (!t) { 369625a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: out of memory, can't " 369725a8b254SDavid S. Miller "create new thread.\n"); 36981da177e4SLinus Torvalds return -ENOMEM; 36991da177e4SLinus Torvalds } 37001da177e4SLinus Torvalds 37011da177e4SLinus Torvalds spin_lock_init(&t->if_lock); 37021da177e4SLinus Torvalds t->cpu = cpu; 37031da177e4SLinus Torvalds 3704ee74baa7SDavid S. Miller INIT_LIST_HEAD(&t->if_list); 3705ee74baa7SDavid S. Miller 3706ee74baa7SDavid S. Miller list_add_tail(&t->th_list, &pktgen_threads); 3707d3ede327SDenis V. Lunev init_completion(&t->start_done); 3708ee74baa7SDavid S. Miller 3709ee74baa7SDavid S. Miller p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu); 3710ee74baa7SDavid S. Miller if (IS_ERR(p)) { 371125a8b254SDavid S. Miller printk(KERN_ERR "pktgen: kernel_thread() failed " 371225a8b254SDavid S. Miller "for cpu %d\n", t->cpu); 3713ee74baa7SDavid S. Miller list_del(&t->th_list); 3714ee74baa7SDavid S. Miller kfree(t); 3715ee74baa7SDavid S. Miller return PTR_ERR(p); 3716ee74baa7SDavid S. Miller } 3717ee74baa7SDavid S. Miller kthread_bind(p, cpu); 3718ee74baa7SDavid S. Miller t->tsk = p; 3719ee74baa7SDavid S. Miller 37205efdccbcSDenis V. Lunev pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir, 37215efdccbcSDenis V. Lunev &pktgen_thread_fops, t); 3722d50a6b56SStephen Hemminger if (!pe) { 372325a8b254SDavid S. Miller printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n", 3724ee74baa7SDavid S. Miller PG_PROC_DIR, t->tsk->comm); 3725ee74baa7SDavid S. Miller kthread_stop(p); 3726ee74baa7SDavid S. Miller list_del(&t->th_list); 37271da177e4SLinus Torvalds kfree(t); 37281da177e4SLinus Torvalds return -EINVAL; 37291da177e4SLinus Torvalds } 3730d50a6b56SStephen Hemminger 3731ee74baa7SDavid S. Miller wake_up_process(p); 3732d3ede327SDenis V. Lunev wait_for_completion(&t->start_done); 37331da177e4SLinus Torvalds 37341da177e4SLinus Torvalds return 0; 37351da177e4SLinus Torvalds } 37361da177e4SLinus Torvalds 37371da177e4SLinus Torvalds /* 37381da177e4SLinus Torvalds * Removes a device from the thread if_list. 37391da177e4SLinus Torvalds */ 3740222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t, 3741222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 37421da177e4SLinus Torvalds { 3743c26a8016SLuiz Capitulino struct list_head *q, *n; 3744c26a8016SLuiz Capitulino struct pktgen_dev *p; 37451da177e4SLinus Torvalds 3746c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3747c26a8016SLuiz Capitulino p = list_entry(q, struct pktgen_dev, list); 3748c26a8016SLuiz Capitulino if (p == pkt_dev) 3749c26a8016SLuiz Capitulino list_del(&p->list); 37501da177e4SLinus Torvalds } 37511da177e4SLinus Torvalds } 37521da177e4SLinus Torvalds 3753222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t, 3754222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 37551da177e4SLinus Torvalds { 37561da177e4SLinus Torvalds 375725c4e53aSStephen Hemminger pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev); 37581da177e4SLinus Torvalds 37591da177e4SLinus Torvalds if (pkt_dev->running) { 376025a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: WARNING: trying to remove a " 376125a8b254SDavid S. Miller "running interface, stopping it now.\n"); 37621da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 37631da177e4SLinus Torvalds } 37641da177e4SLinus Torvalds 37651da177e4SLinus Torvalds /* Dis-associate from the interface */ 37661da177e4SLinus Torvalds 37671da177e4SLinus Torvalds if (pkt_dev->odev) { 37681da177e4SLinus Torvalds dev_put(pkt_dev->odev); 37691da177e4SLinus Torvalds pkt_dev->odev = NULL; 37701da177e4SLinus Torvalds } 37711da177e4SLinus Torvalds 37721da177e4SLinus Torvalds /* And update the thread if_list */ 37731da177e4SLinus Torvalds 37741da177e4SLinus Torvalds _rem_dev_from_if_list(t, pkt_dev); 37751da177e4SLinus Torvalds 377639df232fSStephen Hemminger if (pkt_dev->entry) 377739df232fSStephen Hemminger remove_proc_entry(pkt_dev->entry->name, pg_proc_dir); 37781da177e4SLinus Torvalds 3779a553e4a6SJamal Hadi Salim #ifdef CONFIG_XFRM 3780a553e4a6SJamal Hadi Salim free_SAs(pkt_dev); 3781a553e4a6SJamal Hadi Salim #endif 37821da177e4SLinus Torvalds if (pkt_dev->flows) 37831da177e4SLinus Torvalds vfree(pkt_dev->flows); 37841da177e4SLinus Torvalds kfree(pkt_dev); 37851da177e4SLinus Torvalds return 0; 37861da177e4SLinus Torvalds } 37871da177e4SLinus Torvalds 37881da177e4SLinus Torvalds static int __init pg_init(void) 37891da177e4SLinus Torvalds { 37901da177e4SLinus Torvalds int cpu; 3791d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 3792d50a6b56SStephen Hemminger 379325a8b254SDavid S. Miller printk(KERN_INFO "%s", version); 37941da177e4SLinus Torvalds 3795457c4cbcSEric W. Biederman pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net); 3796d50a6b56SStephen Hemminger if (!pg_proc_dir) 3797d50a6b56SStephen Hemminger return -ENODEV; 3798d50a6b56SStephen Hemminger pg_proc_dir->owner = THIS_MODULE; 37991da177e4SLinus Torvalds 380025296d59SWang Chen pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops); 3801d50a6b56SStephen Hemminger if (pe == NULL) { 380225a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: cannot create %s " 380325a8b254SDavid S. Miller "procfs entry.\n", PGCTRL); 3804457c4cbcSEric W. Biederman proc_net_remove(&init_net, PG_PROC_DIR); 38051da177e4SLinus Torvalds return -EINVAL; 38061da177e4SLinus Torvalds } 38071da177e4SLinus Torvalds 38081da177e4SLinus Torvalds /* Register us to receive netdevice events */ 38091da177e4SLinus Torvalds register_netdevice_notifier(&pktgen_notifier_block); 38101da177e4SLinus Torvalds 3811670c02c2SJohn Hawkes for_each_online_cpu(cpu) { 38128024bb24SLuiz Capitulino int err; 38131da177e4SLinus Torvalds 3814ee74baa7SDavid S. Miller err = pktgen_create_thread(cpu); 38158024bb24SLuiz Capitulino if (err) 381625a8b254SDavid S. Miller printk(KERN_WARNING "pktgen: WARNING: Cannot create " 381725a8b254SDavid S. Miller "thread for cpu %d (%d)\n", cpu, err); 38181da177e4SLinus Torvalds } 38198024bb24SLuiz Capitulino 38208024bb24SLuiz Capitulino if (list_empty(&pktgen_threads)) { 382125a8b254SDavid S. Miller printk(KERN_ERR "pktgen: ERROR: Initialization failed for " 382225a8b254SDavid S. Miller "all threads\n"); 38238024bb24SLuiz Capitulino unregister_netdevice_notifier(&pktgen_notifier_block); 38248024bb24SLuiz Capitulino remove_proc_entry(PGCTRL, pg_proc_dir); 3825457c4cbcSEric W. Biederman proc_net_remove(&init_net, PG_PROC_DIR); 38268024bb24SLuiz Capitulino return -ENODEV; 38278024bb24SLuiz Capitulino } 38288024bb24SLuiz Capitulino 38291da177e4SLinus Torvalds return 0; 38301da177e4SLinus Torvalds } 38311da177e4SLinus Torvalds 38321da177e4SLinus Torvalds static void __exit pg_cleanup(void) 38331da177e4SLinus Torvalds { 3834cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 3835cdcdbe0bSLuiz Capitulino struct list_head *q, *n; 38361da177e4SLinus Torvalds wait_queue_head_t queue; 38371da177e4SLinus Torvalds init_waitqueue_head(&queue); 38381da177e4SLinus Torvalds 38391da177e4SLinus Torvalds /* Stop all interfaces & threads */ 38401da177e4SLinus Torvalds 3841cdcdbe0bSLuiz Capitulino list_for_each_safe(q, n, &pktgen_threads) { 3842cdcdbe0bSLuiz Capitulino t = list_entry(q, struct pktgen_thread, th_list); 3843ee74baa7SDavid S. Miller kthread_stop(t->tsk); 3844ee74baa7SDavid S. Miller kfree(t); 38451da177e4SLinus Torvalds } 38461da177e4SLinus Torvalds 38471da177e4SLinus Torvalds /* Un-register us from receiving netdevice events */ 38481da177e4SLinus Torvalds unregister_netdevice_notifier(&pktgen_notifier_block); 38491da177e4SLinus Torvalds 38501da177e4SLinus Torvalds /* Clean up proc file system */ 3851d50a6b56SStephen Hemminger remove_proc_entry(PGCTRL, pg_proc_dir); 3852457c4cbcSEric W. Biederman proc_net_remove(&init_net, PG_PROC_DIR); 38531da177e4SLinus Torvalds } 38541da177e4SLinus Torvalds 38551da177e4SLinus Torvalds module_init(pg_init); 38561da177e4SLinus Torvalds module_exit(pg_cleanup); 38571da177e4SLinus Torvalds 38581da177e4SLinus Torvalds MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se"); 38591da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool"); 38601da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 38611da177e4SLinus Torvalds module_param(pg_count_d, int, 0); 38621da177e4SLinus Torvalds module_param(pg_delay_d, int, 0); 38631da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0); 38641da177e4SLinus Torvalds module_param(debug, int, 0); 3865