11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Authors: 31da177e4SLinus Torvalds * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se> 41da177e4SLinus Torvalds * Uppsala University and 51da177e4SLinus Torvalds * Swedish University of Agricultural Sciences 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 81da177e4SLinus Torvalds * Ben Greear <greearb@candelatech.com> 91da177e4SLinus Torvalds * Jens L��s <jens.laas@data.slu.se> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 121da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 131da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 141da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * A tool for loading the network with preconfigurated packets. 181da177e4SLinus Torvalds * The tool is implemented as a linux module. Parameters are output 191da177e4SLinus Torvalds * device, delay (to hard_xmit), number of packets, and whether 201da177e4SLinus Torvalds * to use multiple SKBs or just the same one. 211da177e4SLinus Torvalds * pktgen uses the installed interface's output routine. 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Additional hacking by: 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * Jens.Laas@data.slu.se 261da177e4SLinus Torvalds * Improved by ANK. 010120. 271da177e4SLinus Torvalds * Improved by ANK even more. 010212. 281da177e4SLinus Torvalds * MAC address typo fixed. 010417 --ro 291da177e4SLinus Torvalds * Integrated. 020301 --DaveM 301da177e4SLinus Torvalds * Added multiskb option 020301 --DaveM 311da177e4SLinus Torvalds * Scaling of results. 020417--sigurdur@linpro.no 321da177e4SLinus Torvalds * Significant re-work of the module: 331da177e4SLinus Torvalds * * Convert to threaded model to more efficiently be able to transmit 341da177e4SLinus Torvalds * and receive on multiple interfaces at once. 351da177e4SLinus Torvalds * * Converted many counters to __u64 to allow longer runs. 361da177e4SLinus Torvalds * * Allow configuration of ranges, like min/max IP address, MACs, 371da177e4SLinus Torvalds * and UDP-ports, for both source and destination, and can 381da177e4SLinus Torvalds * set to use a random distribution or sequentially walk the range. 391da177e4SLinus Torvalds * * Can now change most values after starting. 401da177e4SLinus Torvalds * * Place 12-byte packet in UDP payload with magic number, 411da177e4SLinus Torvalds * sequence number, and timestamp. 421da177e4SLinus Torvalds * * Add receiver code that detects dropped pkts, re-ordered pkts, and 431da177e4SLinus Torvalds * latencies (with micro-second) precision. 441da177e4SLinus Torvalds * * Add IOCTL interface to easily get counters & configuration. 451da177e4SLinus Torvalds * --Ben Greear <greearb@candelatech.com> 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * Renamed multiskb to clone_skb and cleaned up sending core for two distinct 481da177e4SLinus Torvalds * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0 491da177e4SLinus Torvalds * as a "fastpath" with a configurable number of clones after alloc's. 501da177e4SLinus Torvalds * clone_skb=0 means all packets are allocated this also means ranges time 511da177e4SLinus Torvalds * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100 521da177e4SLinus Torvalds * clones. 531da177e4SLinus Torvalds * 541da177e4SLinus Torvalds * Also moved to /proc/net/pktgen/ 551da177e4SLinus Torvalds * --ro 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever 581da177e4SLinus Torvalds * mistakes. Also merged in DaveM's patch in the -pre6 patch. 591da177e4SLinus Torvalds * --Ben Greear <greearb@candelatech.com> 601da177e4SLinus Torvalds * 611da177e4SLinus Torvalds * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br) 621da177e4SLinus Torvalds * 631da177e4SLinus Torvalds * 641da177e4SLinus Torvalds * 021124 Finished major redesign and rewrite for new functionality. 651da177e4SLinus Torvalds * See Documentation/networking/pktgen.txt for how to use this. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * The new operation: 681da177e4SLinus Torvalds * For each CPU one thread/process is created at start. This process checks 691da177e4SLinus Torvalds * for running devices in the if_list and sends packets until count is 0 it 701da177e4SLinus Torvalds * also the thread checks the thread->control which is used for inter-process 711da177e4SLinus Torvalds * communication. controlling process "posts" operations to the threads this 721da177e4SLinus Torvalds * way. The if_lock should be possible to remove when add/rem_device is merged 731da177e4SLinus Torvalds * into this too. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * By design there should only be *one* "controlling" process. In practice 761da177e4SLinus Torvalds * multiple write accesses gives unpredictable result. Understood by "write" 771da177e4SLinus Torvalds * to /proc gives result code thats should be read be the "writer". 78b4099fabSStephen Hemminger * For practical use this should be no problem. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * Note when adding devices to a specific CPU there good idea to also assign 811da177e4SLinus Torvalds * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU. 821da177e4SLinus Torvalds * --ro 831da177e4SLinus Torvalds * 841da177e4SLinus Torvalds * Fix refcount off by one if first packet fails, potential null deref, 851da177e4SLinus Torvalds * memleak 030710- KJP 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * First "ranges" functionality for ipv6 030726 --ro 881da177e4SLinus Torvalds * 891da177e4SLinus Torvalds * Included flow support. 030802 ANK. 901da177e4SLinus Torvalds * 911da177e4SLinus Torvalds * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org> 921da177e4SLinus Torvalds * 931da177e4SLinus Torvalds * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419 941da177e4SLinus Torvalds * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604 951da177e4SLinus Torvalds * 961da177e4SLinus Torvalds * New xmit() return, do_div and misc clean up by Stephen Hemminger 971da177e4SLinus Torvalds * <shemminger@osdl.org> 040923 981da177e4SLinus Torvalds * 99b4099fabSStephen Hemminger * Randy Dunlap fixed u64 printk compiler waring 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org> 1021da177e4SLinus Torvalds * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * Corrections from Nikolai Malykh (nmalykh@bilim.com) 1051da177e4SLinus Torvalds * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230 1061da177e4SLinus Torvalds * 1071da177e4SLinus Torvalds * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com> 1081da177e4SLinus Torvalds * 050103 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds #include <linux/sys.h> 1111da177e4SLinus Torvalds #include <linux/types.h> 1121da177e4SLinus Torvalds #include <linux/module.h> 1131da177e4SLinus Torvalds #include <linux/moduleparam.h> 1141da177e4SLinus Torvalds #include <linux/kernel.h> 1151da177e4SLinus Torvalds #include <linux/smp_lock.h> 116*222fa076SLuiz Capitulino #include <linux/mutex.h> 1171da177e4SLinus Torvalds #include <linux/sched.h> 1181da177e4SLinus Torvalds #include <linux/slab.h> 1191da177e4SLinus Torvalds #include <linux/vmalloc.h> 1201da177e4SLinus Torvalds #include <linux/unistd.h> 1211da177e4SLinus Torvalds #include <linux/string.h> 1221da177e4SLinus Torvalds #include <linux/ptrace.h> 1231da177e4SLinus Torvalds #include <linux/errno.h> 1241da177e4SLinus Torvalds #include <linux/ioport.h> 1251da177e4SLinus Torvalds #include <linux/interrupt.h> 1264fc268d2SRandy Dunlap #include <linux/capability.h> 1271da177e4SLinus Torvalds #include <linux/delay.h> 1281da177e4SLinus Torvalds #include <linux/timer.h> 129cdcdbe0bSLuiz Capitulino #include <linux/list.h> 1301da177e4SLinus Torvalds #include <linux/init.h> 1311da177e4SLinus Torvalds #include <linux/skbuff.h> 1321da177e4SLinus Torvalds #include <linux/netdevice.h> 1331da177e4SLinus Torvalds #include <linux/inet.h> 1341da177e4SLinus Torvalds #include <linux/inetdevice.h> 1351da177e4SLinus Torvalds #include <linux/rtnetlink.h> 1361da177e4SLinus Torvalds #include <linux/if_arp.h> 1371da177e4SLinus Torvalds #include <linux/in.h> 1381da177e4SLinus Torvalds #include <linux/ip.h> 1391da177e4SLinus Torvalds #include <linux/ipv6.h> 1401da177e4SLinus Torvalds #include <linux/udp.h> 1411da177e4SLinus Torvalds #include <linux/proc_fs.h> 142d50a6b56SStephen Hemminger #include <linux/seq_file.h> 1431da177e4SLinus Torvalds #include <linux/wait.h> 144f404e9a6SKris Katterjohn #include <linux/etherdevice.h> 1451da177e4SLinus Torvalds #include <net/checksum.h> 1461da177e4SLinus Torvalds #include <net/ipv6.h> 1471da177e4SLinus Torvalds #include <net/addrconf.h> 1481da177e4SLinus Torvalds #include <asm/byteorder.h> 1491da177e4SLinus Torvalds #include <linux/rcupdate.h> 1501da177e4SLinus Torvalds #include <asm/bitops.h> 1511da177e4SLinus Torvalds #include <asm/io.h> 1521da177e4SLinus Torvalds #include <asm/dma.h> 1531da177e4SLinus Torvalds #include <asm/uaccess.h> 1541da177e4SLinus Torvalds #include <asm/div64.h> /* do_div */ 1551da177e4SLinus Torvalds #include <asm/timex.h> 1561da177e4SLinus Torvalds 15765a3980eSLuiz Capitulino #define VERSION "pktgen v2.65: Packet Generator for packet performance testing.\n" 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds /* #define PG_DEBUG(a) a */ 1601da177e4SLinus Torvalds #define PG_DEBUG(a) 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* The buckets are exponential in 'width' */ 1631da177e4SLinus Torvalds #define LAT_BUCKETS_MAX 32 1641da177e4SLinus Torvalds #define IP_NAME_SZ 32 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* Device flag bits */ 1671da177e4SLinus Torvalds #define F_IPSRC_RND (1<<0) /* IP-Src Random */ 1681da177e4SLinus Torvalds #define F_IPDST_RND (1<<1) /* IP-Dst Random */ 1691da177e4SLinus Torvalds #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */ 1701da177e4SLinus Torvalds #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */ 1711da177e4SLinus Torvalds #define F_MACSRC_RND (1<<4) /* MAC-Src Random */ 1721da177e4SLinus Torvalds #define F_MACDST_RND (1<<5) /* MAC-Dst Random */ 1731da177e4SLinus Torvalds #define F_TXSIZE_RND (1<<6) /* Transmit size is random */ 1741da177e4SLinus Torvalds #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */ 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds /* Thread control flag bits */ 1771da177e4SLinus Torvalds #define T_TERMINATE (1<<0) 1781da177e4SLinus Torvalds #define T_STOP (1<<1) /* Stop run */ 1791da177e4SLinus Torvalds #define T_RUN (1<<2) /* Start run */ 18095ed63f7SArthur Kepner #define T_REMDEVALL (1<<3) /* Remove all devs */ 18195ed63f7SArthur Kepner #define T_REMDEV (1<<4) /* Remove one dev */ 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds /* Locks */ 184*222fa076SLuiz Capitulino #define thread_lock() mutex_lock(&pktgen_thread_lock) 185*222fa076SLuiz Capitulino #define thread_unlock() mutex_unlock(&pktgen_thread_lock) 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* If lock -- can be removed after some work */ 1881da177e4SLinus Torvalds #define if_lock(t) spin_lock(&(t->if_lock)); 1891da177e4SLinus Torvalds #define if_unlock(t) spin_unlock(&(t->if_lock)); 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */ 1921da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955 193d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen" 194d50a6b56SStephen Hemminger #define PGCTRL "pgctrl" 195d50a6b56SStephen Hemminger static struct proc_dir_entry *pg_proc_dir = NULL; 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds #define MAX_CFLOWS 65536 1981da177e4SLinus Torvalds 199222f1806SLuiz Capitulino struct flow_state { 2001da177e4SLinus Torvalds __u32 cur_daddr; 2011da177e4SLinus Torvalds int count; 2021da177e4SLinus Torvalds }; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds struct pktgen_dev { 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /* 2071da177e4SLinus Torvalds * Try to keep frequent/infrequent used vars. separated. 2081da177e4SLinus Torvalds */ 2091da177e4SLinus Torvalds 210d50a6b56SStephen Hemminger char ifname[IFNAMSIZ]; 2111da177e4SLinus Torvalds char result[512]; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds struct pktgen_thread *pg_thread; /* the owner */ 214c26a8016SLuiz Capitulino struct list_head list; /* Used for chaining in the thread's run-queue */ 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds int running; /* if this changes to false, the test will stop */ 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds /* If min != max, then we will either do a linear iteration, or 2191da177e4SLinus Torvalds * we will do a random selection from within the range. 2201da177e4SLinus Torvalds */ 2211da177e4SLinus Torvalds __u32 flags; 22295ed63f7SArthur Kepner int removal_mark; /* non-zero => the device is marked for 22395ed63f7SArthur Kepner * removal by worker thread */ 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds int min_pkt_size; /* = ETH_ZLEN; */ 2261da177e4SLinus Torvalds int max_pkt_size; /* = ETH_ZLEN; */ 2271da177e4SLinus Torvalds int nfrags; 2281da177e4SLinus Torvalds __u32 delay_us; /* Default delay */ 2291da177e4SLinus Torvalds __u32 delay_ns; 2301da177e4SLinus Torvalds __u64 count; /* Default No packets to send */ 2311da177e4SLinus Torvalds __u64 sofar; /* How many pkts we've sent so far */ 2321da177e4SLinus Torvalds __u64 tx_bytes; /* How many bytes we've transmitted */ 2331da177e4SLinus Torvalds __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */ 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds /* runtime counters relating to clone_skb */ 2361da177e4SLinus Torvalds __u64 next_tx_us; /* timestamp of when to tx next */ 2371da177e4SLinus Torvalds __u32 next_tx_ns; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds __u64 allocated_skbs; 2401da177e4SLinus Torvalds __u32 clone_count; 2411da177e4SLinus Torvalds int last_ok; /* Was last skb sent? 2421da177e4SLinus Torvalds * Or a failed transmit of some sort? This will keep 2431da177e4SLinus Torvalds * sequence numbers in order, for example. 2441da177e4SLinus Torvalds */ 2451da177e4SLinus Torvalds __u64 started_at; /* micro-seconds */ 2461da177e4SLinus Torvalds __u64 stopped_at; /* micro-seconds */ 2471da177e4SLinus Torvalds __u64 idle_acc; /* micro-seconds */ 2481da177e4SLinus Torvalds __u32 seq_num; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds int clone_skb; /* Use multiple SKBs during packet gen. If this number 251b4099fabSStephen Hemminger * is greater than 1, then that many copies of the same 2521da177e4SLinus Torvalds * packet will be sent before a new packet is allocated. 2531da177e4SLinus Torvalds * For instance, if you want to send 1024 identical packets 2541da177e4SLinus Torvalds * before creating a new packet, set clone_skb to 1024. 2551da177e4SLinus Torvalds */ 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2581da177e4SLinus Torvalds char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2591da177e4SLinus Torvalds char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2601da177e4SLinus Torvalds char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds struct in6_addr in6_saddr; 2631da177e4SLinus Torvalds struct in6_addr in6_daddr; 2641da177e4SLinus Torvalds struct in6_addr cur_in6_daddr; 2651da177e4SLinus Torvalds struct in6_addr cur_in6_saddr; 2661da177e4SLinus Torvalds /* For ranges */ 2671da177e4SLinus Torvalds struct in6_addr min_in6_daddr; 2681da177e4SLinus Torvalds struct in6_addr max_in6_daddr; 2691da177e4SLinus Torvalds struct in6_addr min_in6_saddr; 2701da177e4SLinus Torvalds struct in6_addr max_in6_saddr; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds /* If we're doing ranges, random or incremental, then this 2731da177e4SLinus Torvalds * defines the min/max for those ranges. 2741da177e4SLinus Torvalds */ 2751da177e4SLinus Torvalds __u32 saddr_min; /* inclusive, source IP address */ 2761da177e4SLinus Torvalds __u32 saddr_max; /* exclusive, source IP address */ 2771da177e4SLinus Torvalds __u32 daddr_min; /* inclusive, dest IP address */ 2781da177e4SLinus Torvalds __u32 daddr_max; /* exclusive, dest IP address */ 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds __u16 udp_src_min; /* inclusive, source UDP port */ 2811da177e4SLinus Torvalds __u16 udp_src_max; /* exclusive, source UDP port */ 2821da177e4SLinus Torvalds __u16 udp_dst_min; /* inclusive, dest UDP port */ 2831da177e4SLinus Torvalds __u16 udp_dst_max; /* exclusive, dest UDP port */ 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds __u32 src_mac_count; /* How many MACs to iterate through */ 2861da177e4SLinus Torvalds __u32 dst_mac_count; /* How many MACs to iterate through */ 2871da177e4SLinus Torvalds 288f404e9a6SKris Katterjohn unsigned char dst_mac[ETH_ALEN]; 289f404e9a6SKris Katterjohn unsigned char src_mac[ETH_ALEN]; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds __u32 cur_dst_mac_offset; 2921da177e4SLinus Torvalds __u32 cur_src_mac_offset; 2931da177e4SLinus Torvalds __u32 cur_saddr; 2941da177e4SLinus Torvalds __u32 cur_daddr; 2951da177e4SLinus Torvalds __u16 cur_udp_dst; 2961da177e4SLinus Torvalds __u16 cur_udp_src; 2971da177e4SLinus Torvalds __u32 cur_pkt_size; 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds __u8 hh[14]; 3001da177e4SLinus Torvalds /* = { 3011da177e4SLinus Torvalds 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB, 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds We fill in SRC address later 3041da177e4SLinus Torvalds 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 3051da177e4SLinus Torvalds 0x08, 0x00 3061da177e4SLinus Torvalds }; 3071da177e4SLinus Torvalds */ 3081da177e4SLinus Torvalds __u16 pad; /* pad out the hh struct to an even 16 bytes */ 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we 3111da177e4SLinus Torvalds * are transmitting the same one multiple times 3121da177e4SLinus Torvalds */ 3131da177e4SLinus Torvalds struct net_device *odev; /* The out-going device. Note that the device should 3141da177e4SLinus Torvalds * have it's pg_info pointer pointing back to this 3151da177e4SLinus Torvalds * device. This will be set when the user specifies 3161da177e4SLinus Torvalds * the out-going device name (not when the inject is 3171da177e4SLinus Torvalds * started as it used to do.) 3181da177e4SLinus Torvalds */ 3191da177e4SLinus Torvalds struct flow_state *flows; 3201da177e4SLinus Torvalds unsigned cflows; /* Concurrent flows (config) */ 3211da177e4SLinus Torvalds unsigned lflow; /* Flow length (config) */ 3221da177e4SLinus Torvalds unsigned nflows; /* accumulated flows (stats) */ 3231da177e4SLinus Torvalds }; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds struct pktgen_hdr { 3261da177e4SLinus Torvalds __u32 pgh_magic; 3271da177e4SLinus Torvalds __u32 seq_num; 3281da177e4SLinus Torvalds __u32 tv_sec; 3291da177e4SLinus Torvalds __u32 tv_usec; 3301da177e4SLinus Torvalds }; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds struct pktgen_thread { 3331da177e4SLinus Torvalds spinlock_t if_lock; 334c26a8016SLuiz Capitulino struct list_head if_list; /* All device here */ 335cdcdbe0bSLuiz Capitulino struct list_head th_list; 336cdcdbe0bSLuiz Capitulino int removed; 3371da177e4SLinus Torvalds char name[32]; 3381da177e4SLinus Torvalds char result[512]; 3391da177e4SLinus Torvalds u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */ 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* Field for thread to receive "posted" events terminate, stop ifs etc. */ 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds u32 control; 3441da177e4SLinus Torvalds int pid; 3451da177e4SLinus Torvalds int cpu; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds wait_queue_head_t queue; 3481da177e4SLinus Torvalds }; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds #define REMOVE 1 3511da177e4SLinus Torvalds #define FIND 0 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds /* This code works around the fact that do_div cannot handle two 64-bit 3541da177e4SLinus Torvalds numbers, and regular 64-bit division doesn't work on x86 kernels. 3551da177e4SLinus Torvalds --Ben 3561da177e4SLinus Torvalds */ 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds #define PG_DIV 0 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net> 3611da177e4SLinus Torvalds * Function copied/adapted/optimized from: 3621da177e4SLinus Torvalds * 3631da177e4SLinus Torvalds * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html 3641da177e4SLinus Torvalds * 3651da177e4SLinus Torvalds * Copyright 1994, University of Cambridge Computer Laboratory 3661da177e4SLinus Torvalds * All Rights Reserved. 3671da177e4SLinus Torvalds * 3681da177e4SLinus Torvalds */ 36977933d72SJesper Juhl static inline s64 divremdi3(s64 x, s64 y, int type) 3701da177e4SLinus Torvalds { 3711da177e4SLinus Torvalds u64 a = (x < 0) ? -x : x; 3721da177e4SLinus Torvalds u64 b = (y < 0) ? -y : y; 3731da177e4SLinus Torvalds u64 res = 0, d = 1; 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds if (b > 0) { 3761da177e4SLinus Torvalds while (b < a) { 3771da177e4SLinus Torvalds b <<= 1; 3781da177e4SLinus Torvalds d <<= 1; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds do { 3831da177e4SLinus Torvalds if (a >= b) { 3841da177e4SLinus Torvalds a -= b; 3851da177e4SLinus Torvalds res += d; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds b >>= 1; 3881da177e4SLinus Torvalds d >>= 1; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds while (d); 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds if (PG_DIV == type) { 3931da177e4SLinus Torvalds return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res; 394222f1806SLuiz Capitulino } else { 3951da177e4SLinus Torvalds return ((x & (1ll << 63)) == 0) ? a : -(s64) a; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* End of hacks to deal with 64-bit math on x86 */ 4001da177e4SLinus Torvalds 401b4099fabSStephen Hemminger /** Convert to milliseconds */ 4021da177e4SLinus Torvalds static inline __u64 tv_to_ms(const struct timeval *tv) 4031da177e4SLinus Torvalds { 4041da177e4SLinus Torvalds __u64 ms = tv->tv_usec / 1000; 4051da177e4SLinus Torvalds ms += (__u64) tv->tv_sec * (__u64) 1000; 4061da177e4SLinus Torvalds return ms; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /** Convert to micro-seconds */ 4101da177e4SLinus Torvalds static inline __u64 tv_to_us(const struct timeval *tv) 4111da177e4SLinus Torvalds { 4121da177e4SLinus Torvalds __u64 us = tv->tv_usec; 4131da177e4SLinus Torvalds us += (__u64) tv->tv_sec * (__u64) 1000000; 4141da177e4SLinus Torvalds return us; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 417222f1806SLuiz Capitulino static inline __u64 pg_div(__u64 n, __u32 base) 418222f1806SLuiz Capitulino { 4191da177e4SLinus Torvalds __u64 tmp = n; 4201da177e4SLinus Torvalds do_div(tmp, base); 4211da177e4SLinus Torvalds /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n", 4221da177e4SLinus Torvalds n, base, tmp); */ 4231da177e4SLinus Torvalds return tmp; 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds static inline __u64 pg_div64(__u64 n, __u64 base) 4271da177e4SLinus Torvalds { 4281da177e4SLinus Torvalds __u64 tmp = n; 4291da177e4SLinus Torvalds /* 430b4099fabSStephen Hemminger * How do we know if the architecture we are running on 4311da177e4SLinus Torvalds * supports division with 64 bit base? 4321da177e4SLinus Torvalds * 4331da177e4SLinus Torvalds */ 4341da177e4SLinus Torvalds #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__) 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds do_div(tmp, base); 4371da177e4SLinus Torvalds #else 4381da177e4SLinus Torvalds tmp = divremdi3(n, base, PG_DIV); 4391da177e4SLinus Torvalds #endif 4401da177e4SLinus Torvalds return tmp; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds static inline u32 pktgen_random(void) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds #if 0 4461da177e4SLinus Torvalds __u32 n; 4471da177e4SLinus Torvalds get_random_bytes(&n, 4); 4481da177e4SLinus Torvalds return n; 4491da177e4SLinus Torvalds #else 4501da177e4SLinus Torvalds return net_random(); 4511da177e4SLinus Torvalds #endif 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds static inline __u64 getCurMs(void) 4551da177e4SLinus Torvalds { 4561da177e4SLinus Torvalds struct timeval tv; 4571da177e4SLinus Torvalds do_gettimeofday(&tv); 4581da177e4SLinus Torvalds return tv_to_ms(&tv); 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds static inline __u64 getCurUs(void) 4621da177e4SLinus Torvalds { 4631da177e4SLinus Torvalds struct timeval tv; 4641da177e4SLinus Torvalds do_gettimeofday(&tv); 4651da177e4SLinus Torvalds return tv_to_us(&tv); 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b) 4691da177e4SLinus Torvalds { 4701da177e4SLinus Torvalds return tv_to_us(a) - tv_to_us(b); 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds /* old include end */ 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds static char version[] __initdata = VERSION; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i); 4781da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname); 479222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, 480222f1806SLuiz Capitulino const char *ifname); 4811da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *); 4821da177e4SLinus Torvalds static void pktgen_run_all_threads(void); 4831da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void); 4841da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev); 4851da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t); 4861da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev); 48795ed63f7SArthur Kepner static int pktgen_mark_device(const char *ifname); 4881da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]); 4891da177e4SLinus Torvalds static unsigned int fmt_ip6(char *s, const char ip[16]); 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* Module parameters, defaults. */ 4921da177e4SLinus Torvalds static int pg_count_d = 1000; /* 1000 pkts by default */ 493f34fbb97SJaco Kroon static int pg_delay_d; 494f34fbb97SJaco Kroon static int pg_clone_skb_d; 495f34fbb97SJaco Kroon static int debug; 4961da177e4SLinus Torvalds 497*222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock); 498cdcdbe0bSLuiz Capitulino static LIST_HEAD(pktgen_threads); 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = { 5011da177e4SLinus Torvalds .notifier_call = pktgen_device_event, 5021da177e4SLinus Torvalds }; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds /* 5051da177e4SLinus Torvalds * /proc handling functions 5061da177e4SLinus Torvalds * 5071da177e4SLinus Torvalds */ 5081da177e4SLinus Torvalds 509d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v) 510d50a6b56SStephen Hemminger { 511d50a6b56SStephen Hemminger seq_puts(seq, VERSION); 512d50a6b56SStephen Hemminger return 0; 513d50a6b56SStephen Hemminger } 5141da177e4SLinus Torvalds 515d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user * buf, 5161da177e4SLinus Torvalds size_t count, loff_t * ppos) 5171da177e4SLinus Torvalds { 5181da177e4SLinus Torvalds int err = 0; 519d50a6b56SStephen Hemminger char data[128]; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) { 5221da177e4SLinus Torvalds err = -EPERM; 5231da177e4SLinus Torvalds goto out; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 526d50a6b56SStephen Hemminger if (count > sizeof(data)) 527d50a6b56SStephen Hemminger count = sizeof(data); 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds if (copy_from_user(data, buf, count)) { 5301da177e4SLinus Torvalds err = -EFAULT; 531d50a6b56SStephen Hemminger goto out; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds data[count - 1] = 0; /* Make string */ 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds if (!strcmp(data, "stop")) 5361da177e4SLinus Torvalds pktgen_stop_all_threads_ifs(); 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds else if (!strcmp(data, "start")) 5391da177e4SLinus Torvalds pktgen_run_all_threads(); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds else 5421da177e4SLinus Torvalds printk("pktgen: Unknown command: %s\n", data); 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds err = count; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds out: 5471da177e4SLinus Torvalds return err; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 550d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file) 5511da177e4SLinus Torvalds { 552d50a6b56SStephen Hemminger return single_open(file, pgctrl_show, PDE(inode)->data); 553d50a6b56SStephen Hemminger } 554d50a6b56SStephen Hemminger 555d50a6b56SStephen Hemminger static struct file_operations pktgen_fops = { 556d50a6b56SStephen Hemminger .owner = THIS_MODULE, 557d50a6b56SStephen Hemminger .open = pgctrl_open, 558d50a6b56SStephen Hemminger .read = seq_read, 559d50a6b56SStephen Hemminger .llseek = seq_lseek, 560d50a6b56SStephen Hemminger .write = pgctrl_write, 561d50a6b56SStephen Hemminger .release = single_release, 562d50a6b56SStephen Hemminger }; 563d50a6b56SStephen Hemminger 564d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v) 565d50a6b56SStephen Hemminger { 5661da177e4SLinus Torvalds int i; 567d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 5681da177e4SLinus Torvalds __u64 sa; 5691da177e4SLinus Torvalds __u64 stopped; 5701da177e4SLinus Torvalds __u64 now = getCurUs(); 5711da177e4SLinus Torvalds 572222f1806SLuiz Capitulino seq_printf(seq, 573222f1806SLuiz Capitulino "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n", 574222f1806SLuiz Capitulino (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size, 575222f1806SLuiz Capitulino pkt_dev->max_pkt_size); 5761da177e4SLinus Torvalds 577222f1806SLuiz Capitulino seq_printf(seq, 578222f1806SLuiz Capitulino " frags: %d delay: %u clone_skb: %d ifname: %s\n", 579222f1806SLuiz Capitulino pkt_dev->nfrags, 580222f1806SLuiz Capitulino 1000 * pkt_dev->delay_us + pkt_dev->delay_ns, 581222f1806SLuiz Capitulino pkt_dev->clone_skb, pkt_dev->ifname); 5821da177e4SLinus Torvalds 583222f1806SLuiz Capitulino seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows, 584222f1806SLuiz Capitulino pkt_dev->lflow); 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 5871da177e4SLinus Torvalds char b1[128], b2[128], b3[128]; 5881da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr); 5891da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr); 5901da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr); 591222f1806SLuiz Capitulino seq_printf(seq, 592222f1806SLuiz Capitulino " saddr: %s min_saddr: %s max_saddr: %s\n", b1, 593222f1806SLuiz Capitulino b2, b3); 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr); 5961da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr); 5971da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr); 598222f1806SLuiz Capitulino seq_printf(seq, 599222f1806SLuiz Capitulino " daddr: %s min_daddr: %s max_daddr: %s\n", b1, 600222f1806SLuiz Capitulino b2, b3); 6011da177e4SLinus Torvalds 602222f1806SLuiz Capitulino } else 603222f1806SLuiz Capitulino seq_printf(seq, 604222f1806SLuiz Capitulino " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n", 605222f1806SLuiz Capitulino pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min, 606222f1806SLuiz Capitulino pkt_dev->src_max); 6071da177e4SLinus Torvalds 608d50a6b56SStephen Hemminger seq_puts(seq, " src_mac: "); 6091da177e4SLinus Torvalds 610f404e9a6SKris Katterjohn if (is_zero_ether_addr(pkt_dev->src_mac)) 6111da177e4SLinus Torvalds for (i = 0; i < 6; i++) 612222f1806SLuiz Capitulino seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i], 613222f1806SLuiz Capitulino i == 5 ? " " : ":"); 6141da177e4SLinus Torvalds else 6151da177e4SLinus Torvalds for (i = 0; i < 6; i++) 616222f1806SLuiz Capitulino seq_printf(seq, "%02X%s", pkt_dev->src_mac[i], 617222f1806SLuiz Capitulino i == 5 ? " " : ":"); 6181da177e4SLinus Torvalds 619d50a6b56SStephen Hemminger seq_printf(seq, "dst_mac: "); 6201da177e4SLinus Torvalds for (i = 0; i < 6; i++) 621222f1806SLuiz Capitulino seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i], 622222f1806SLuiz Capitulino i == 5 ? "\n" : ":"); 6231da177e4SLinus Torvalds 624222f1806SLuiz Capitulino seq_printf(seq, 625222f1806SLuiz Capitulino " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n", 626222f1806SLuiz Capitulino pkt_dev->udp_src_min, pkt_dev->udp_src_max, 627222f1806SLuiz Capitulino pkt_dev->udp_dst_min, pkt_dev->udp_dst_max); 6281da177e4SLinus Torvalds 629222f1806SLuiz Capitulino seq_printf(seq, 630222f1806SLuiz Capitulino " src_mac_count: %d dst_mac_count: %d \n Flags: ", 6311da177e4SLinus Torvalds pkt_dev->src_mac_count, pkt_dev->dst_mac_count); 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) 634d50a6b56SStephen Hemminger seq_printf(seq, "IPV6 "); 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 637d50a6b56SStephen Hemminger seq_printf(seq, "IPSRC_RND "); 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) 640d50a6b56SStephen Hemminger seq_printf(seq, "IPDST_RND "); 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) 643d50a6b56SStephen Hemminger seq_printf(seq, "TXSIZE_RND "); 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 646d50a6b56SStephen Hemminger seq_printf(seq, "UDPSRC_RND "); 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) 649d50a6b56SStephen Hemminger seq_printf(seq, "UDPDST_RND "); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 652d50a6b56SStephen Hemminger seq_printf(seq, "MACSRC_RND "); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 655d50a6b56SStephen Hemminger seq_printf(seq, "MACDST_RND "); 6561da177e4SLinus Torvalds 657d50a6b56SStephen Hemminger seq_puts(seq, "\n"); 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds sa = pkt_dev->started_at; 6601da177e4SLinus Torvalds stopped = pkt_dev->stopped_at; 6611da177e4SLinus Torvalds if (pkt_dev->running) 6621da177e4SLinus Torvalds stopped = now; /* not really stopped, more like last-running-at */ 6631da177e4SLinus Torvalds 664222f1806SLuiz Capitulino seq_printf(seq, 665222f1806SLuiz Capitulino "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n", 6661da177e4SLinus Torvalds (unsigned long long)pkt_dev->sofar, 667222f1806SLuiz Capitulino (unsigned long long)pkt_dev->errors, (unsigned long long)sa, 6681da177e4SLinus Torvalds (unsigned long long)stopped, 6691da177e4SLinus Torvalds (unsigned long long)pkt_dev->idle_acc); 6701da177e4SLinus Torvalds 671222f1806SLuiz Capitulino seq_printf(seq, 672222f1806SLuiz Capitulino " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n", 673d50a6b56SStephen Hemminger pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset, 674d50a6b56SStephen Hemminger pkt_dev->cur_src_mac_offset); 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 6771da177e4SLinus Torvalds char b1[128], b2[128]; 6781da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr); 6791da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr); 680d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1); 681222f1806SLuiz Capitulino } else 682d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n", 6831da177e4SLinus Torvalds pkt_dev->cur_saddr, pkt_dev->cur_daddr); 6841da177e4SLinus Torvalds 685d50a6b56SStephen Hemminger seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n", 6861da177e4SLinus Torvalds pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src); 6871da177e4SLinus Torvalds 688d50a6b56SStephen Hemminger seq_printf(seq, " flows: %u\n", pkt_dev->nflows); 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds if (pkt_dev->result[0]) 691d50a6b56SStephen Hemminger seq_printf(seq, "Result: %s\n", pkt_dev->result); 6921da177e4SLinus Torvalds else 693d50a6b56SStephen Hemminger seq_printf(seq, "Result: Idle\n"); 6941da177e4SLinus Torvalds 695d50a6b56SStephen Hemminger return 0; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds 698222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer, 699222f1806SLuiz Capitulino unsigned int maxlen) 7001da177e4SLinus Torvalds { 7011da177e4SLinus Torvalds int i; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds for (i = 0; i < maxlen; i++) { 7041da177e4SLinus Torvalds char c; 7051da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 7061da177e4SLinus Torvalds return -EFAULT; 7071da177e4SLinus Torvalds switch (c) { 7081da177e4SLinus Torvalds case '\"': 7091da177e4SLinus Torvalds case '\n': 7101da177e4SLinus Torvalds case '\r': 7111da177e4SLinus Torvalds case '\t': 7121da177e4SLinus Torvalds case ' ': 7131da177e4SLinus Torvalds case '=': 7141da177e4SLinus Torvalds break; 7151da177e4SLinus Torvalds default: 7161da177e4SLinus Torvalds goto done; 7171da177e4SLinus Torvalds }; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds done: 7201da177e4SLinus Torvalds return i; 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 723222f1806SLuiz Capitulino static unsigned long num_arg(const char __user * user_buffer, 724222f1806SLuiz Capitulino unsigned long maxlen, unsigned long *num) 7251da177e4SLinus Torvalds { 7261da177e4SLinus Torvalds int i = 0; 7271da177e4SLinus Torvalds *num = 0; 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds for (; i < maxlen; i++) { 7301da177e4SLinus Torvalds char c; 7311da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 7321da177e4SLinus Torvalds return -EFAULT; 7331da177e4SLinus Torvalds if ((c >= '0') && (c <= '9')) { 7341da177e4SLinus Torvalds *num *= 10; 7351da177e4SLinus Torvalds *num += c - '0'; 7361da177e4SLinus Torvalds } else 7371da177e4SLinus Torvalds break; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds return i; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds int i = 0; 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds for (; i < maxlen; i++) { 7471da177e4SLinus Torvalds char c; 7481da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 7491da177e4SLinus Torvalds return -EFAULT; 7501da177e4SLinus Torvalds switch (c) { 7511da177e4SLinus Torvalds case '\"': 7521da177e4SLinus Torvalds case '\n': 7531da177e4SLinus Torvalds case '\r': 7541da177e4SLinus Torvalds case '\t': 7551da177e4SLinus Torvalds case ' ': 7561da177e4SLinus Torvalds goto done_str; 7571da177e4SLinus Torvalds break; 7581da177e4SLinus Torvalds default: 7591da177e4SLinus Torvalds break; 7601da177e4SLinus Torvalds }; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds done_str: 7631da177e4SLinus Torvalds return i; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 766222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file, 767222f1806SLuiz Capitulino const char __user * user_buffer, size_t count, 768222f1806SLuiz Capitulino loff_t * offset) 7691da177e4SLinus Torvalds { 770d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *)file->private_data; 771d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 7721da177e4SLinus Torvalds int i = 0, max, len; 7731da177e4SLinus Torvalds char name[16], valstr[32]; 7741da177e4SLinus Torvalds unsigned long value = 0; 7751da177e4SLinus Torvalds char *pg_result = NULL; 7761da177e4SLinus Torvalds int tmp = 0; 7771da177e4SLinus Torvalds char buf[128]; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds pg_result = &(pkt_dev->result[0]); 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds if (count < 1) { 7821da177e4SLinus Torvalds printk("pktgen: wrong command format\n"); 7831da177e4SLinus Torvalds return -EINVAL; 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds max = count - i; 7871da177e4SLinus Torvalds tmp = count_trail_chars(&user_buffer[i], max); 7881da177e4SLinus Torvalds if (tmp < 0) { 7891da177e4SLinus Torvalds printk("pktgen: illegal format\n"); 7901da177e4SLinus Torvalds return tmp; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds i += tmp; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* Read variable name */ 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 797222f1806SLuiz Capitulino if (len < 0) { 798222f1806SLuiz Capitulino return len; 799222f1806SLuiz Capitulino } 8001da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 8011da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len)) 8021da177e4SLinus Torvalds return -EFAULT; 8031da177e4SLinus Torvalds i += len; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds max = count - i; 8061da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 8071da177e4SLinus Torvalds if (len < 0) 8081da177e4SLinus Torvalds return len; 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds i += len; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds if (debug) { 8131da177e4SLinus Torvalds char tb[count + 1]; 8141da177e4SLinus Torvalds if (copy_from_user(tb, user_buffer, count)) 8151da177e4SLinus Torvalds return -EFAULT; 8161da177e4SLinus Torvalds tb[count] = 0; 817d50a6b56SStephen Hemminger printk("pktgen: %s,%lu buffer -:%s:-\n", name, 818d50a6b56SStephen Hemminger (unsigned long)count, tb); 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds if (!strcmp(name, "min_pkt_size")) { 8221da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 823222f1806SLuiz Capitulino if (len < 0) { 824222f1806SLuiz Capitulino return len; 825222f1806SLuiz Capitulino } 8261da177e4SLinus Torvalds i += len; 8271da177e4SLinus Torvalds if (value < 14 + 20 + 8) 8281da177e4SLinus Torvalds value = 14 + 20 + 8; 8291da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 8301da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 8311da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 8321da177e4SLinus Torvalds } 833222f1806SLuiz Capitulino sprintf(pg_result, "OK: min_pkt_size=%u", 834222f1806SLuiz Capitulino pkt_dev->min_pkt_size); 8351da177e4SLinus Torvalds return count; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds if (!strcmp(name, "max_pkt_size")) { 8391da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 840222f1806SLuiz Capitulino if (len < 0) { 841222f1806SLuiz Capitulino return len; 842222f1806SLuiz Capitulino } 8431da177e4SLinus Torvalds i += len; 8441da177e4SLinus Torvalds if (value < 14 + 20 + 8) 8451da177e4SLinus Torvalds value = 14 + 20 + 8; 8461da177e4SLinus Torvalds if (value != pkt_dev->max_pkt_size) { 8471da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 8481da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 8491da177e4SLinus Torvalds } 850222f1806SLuiz Capitulino sprintf(pg_result, "OK: max_pkt_size=%u", 851222f1806SLuiz Capitulino pkt_dev->max_pkt_size); 8521da177e4SLinus Torvalds return count; 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds /* Shortcut for min = max */ 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds if (!strcmp(name, "pkt_size")) { 8581da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 859222f1806SLuiz Capitulino if (len < 0) { 860222f1806SLuiz Capitulino return len; 861222f1806SLuiz Capitulino } 8621da177e4SLinus Torvalds i += len; 8631da177e4SLinus Torvalds if (value < 14 + 20 + 8) 8641da177e4SLinus Torvalds value = 14 + 20 + 8; 8651da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 8661da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 8671da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 8681da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size); 8711da177e4SLinus Torvalds return count; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds if (!strcmp(name, "debug")) { 8751da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 876222f1806SLuiz Capitulino if (len < 0) { 877222f1806SLuiz Capitulino return len; 878222f1806SLuiz Capitulino } 8791da177e4SLinus Torvalds i += len; 8801da177e4SLinus Torvalds debug = value; 8811da177e4SLinus Torvalds sprintf(pg_result, "OK: debug=%u", debug); 8821da177e4SLinus Torvalds return count; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds if (!strcmp(name, "frags")) { 8861da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 887222f1806SLuiz Capitulino if (len < 0) { 888222f1806SLuiz Capitulino return len; 889222f1806SLuiz Capitulino } 8901da177e4SLinus Torvalds i += len; 8911da177e4SLinus Torvalds pkt_dev->nfrags = value; 8921da177e4SLinus Torvalds sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags); 8931da177e4SLinus Torvalds return count; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds if (!strcmp(name, "delay")) { 8961da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 897222f1806SLuiz Capitulino if (len < 0) { 898222f1806SLuiz Capitulino return len; 899222f1806SLuiz Capitulino } 9001da177e4SLinus Torvalds i += len; 9011da177e4SLinus Torvalds if (value == 0x7FFFFFFF) { 9021da177e4SLinus Torvalds pkt_dev->delay_us = 0x7FFFFFFF; 9031da177e4SLinus Torvalds pkt_dev->delay_ns = 0; 9041da177e4SLinus Torvalds } else { 9051da177e4SLinus Torvalds pkt_dev->delay_us = value / 1000; 9061da177e4SLinus Torvalds pkt_dev->delay_ns = value % 1000; 9071da177e4SLinus Torvalds } 908222f1806SLuiz Capitulino sprintf(pg_result, "OK: delay=%u", 909222f1806SLuiz Capitulino 1000 * pkt_dev->delay_us + pkt_dev->delay_ns); 9101da177e4SLinus Torvalds return count; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds if (!strcmp(name, "udp_src_min")) { 9131da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 914222f1806SLuiz Capitulino if (len < 0) { 915222f1806SLuiz Capitulino return len; 916222f1806SLuiz Capitulino } 9171da177e4SLinus Torvalds i += len; 9181da177e4SLinus Torvalds if (value != pkt_dev->udp_src_min) { 9191da177e4SLinus Torvalds pkt_dev->udp_src_min = value; 9201da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min); 9231da177e4SLinus Torvalds return count; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_min")) { 9261da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 927222f1806SLuiz Capitulino if (len < 0) { 928222f1806SLuiz Capitulino return len; 929222f1806SLuiz Capitulino } 9301da177e4SLinus Torvalds i += len; 9311da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_min) { 9321da177e4SLinus Torvalds pkt_dev->udp_dst_min = value; 9331da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min); 9361da177e4SLinus Torvalds return count; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds if (!strcmp(name, "udp_src_max")) { 9391da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 940222f1806SLuiz Capitulino if (len < 0) { 941222f1806SLuiz Capitulino return len; 942222f1806SLuiz Capitulino } 9431da177e4SLinus Torvalds i += len; 9441da177e4SLinus Torvalds if (value != pkt_dev->udp_src_max) { 9451da177e4SLinus Torvalds pkt_dev->udp_src_max = value; 9461da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max); 9491da177e4SLinus Torvalds return count; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_max")) { 9521da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 953222f1806SLuiz Capitulino if (len < 0) { 954222f1806SLuiz Capitulino return len; 955222f1806SLuiz Capitulino } 9561da177e4SLinus Torvalds i += len; 9571da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_max) { 9581da177e4SLinus Torvalds pkt_dev->udp_dst_max = value; 9591da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max); 9621da177e4SLinus Torvalds return count; 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds if (!strcmp(name, "clone_skb")) { 9651da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 966222f1806SLuiz Capitulino if (len < 0) { 967222f1806SLuiz Capitulino return len; 968222f1806SLuiz Capitulino } 9691da177e4SLinus Torvalds i += len; 9701da177e4SLinus Torvalds pkt_dev->clone_skb = value; 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb); 9731da177e4SLinus Torvalds return count; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds if (!strcmp(name, "count")) { 9761da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 977222f1806SLuiz Capitulino if (len < 0) { 978222f1806SLuiz Capitulino return len; 979222f1806SLuiz Capitulino } 9801da177e4SLinus Torvalds i += len; 9811da177e4SLinus Torvalds pkt_dev->count = value; 9821da177e4SLinus Torvalds sprintf(pg_result, "OK: count=%llu", 9831da177e4SLinus Torvalds (unsigned long long)pkt_dev->count); 9841da177e4SLinus Torvalds return count; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds if (!strcmp(name, "src_mac_count")) { 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 (pkt_dev->src_mac_count != value) { 9931da177e4SLinus Torvalds pkt_dev->src_mac_count = value; 9941da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 9951da177e4SLinus Torvalds } 996222f1806SLuiz Capitulino sprintf(pg_result, "OK: src_mac_count=%d", 997222f1806SLuiz Capitulino pkt_dev->src_mac_count); 9981da177e4SLinus Torvalds return count; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds if (!strcmp(name, "dst_mac_count")) { 10011da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1002222f1806SLuiz Capitulino if (len < 0) { 1003222f1806SLuiz Capitulino return len; 1004222f1806SLuiz Capitulino } 10051da177e4SLinus Torvalds i += len; 10061da177e4SLinus Torvalds if (pkt_dev->dst_mac_count != value) { 10071da177e4SLinus Torvalds pkt_dev->dst_mac_count = value; 10081da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 10091da177e4SLinus Torvalds } 1010222f1806SLuiz Capitulino sprintf(pg_result, "OK: dst_mac_count=%d", 1011222f1806SLuiz Capitulino pkt_dev->dst_mac_count); 10121da177e4SLinus Torvalds return count; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds if (!strcmp(name, "flag")) { 10151da177e4SLinus Torvalds char f[32]; 10161da177e4SLinus Torvalds memset(f, 0, 32); 10171da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 1018222f1806SLuiz Capitulino if (len < 0) { 1019222f1806SLuiz Capitulino return len; 1020222f1806SLuiz Capitulino } 10211da177e4SLinus Torvalds if (copy_from_user(f, &user_buffer[i], len)) 10221da177e4SLinus Torvalds return -EFAULT; 10231da177e4SLinus Torvalds i += len; 10241da177e4SLinus Torvalds if (strcmp(f, "IPSRC_RND") == 0) 10251da177e4SLinus Torvalds pkt_dev->flags |= F_IPSRC_RND; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds else if (strcmp(f, "!IPSRC_RND") == 0) 10281da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPSRC_RND; 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds else if (strcmp(f, "TXSIZE_RND") == 0) 10311da177e4SLinus Torvalds pkt_dev->flags |= F_TXSIZE_RND; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds else if (strcmp(f, "!TXSIZE_RND") == 0) 10341da177e4SLinus Torvalds pkt_dev->flags &= ~F_TXSIZE_RND; 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds else if (strcmp(f, "IPDST_RND") == 0) 10371da177e4SLinus Torvalds pkt_dev->flags |= F_IPDST_RND; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds else if (strcmp(f, "!IPDST_RND") == 0) 10401da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPDST_RND; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds else if (strcmp(f, "UDPSRC_RND") == 0) 10431da177e4SLinus Torvalds pkt_dev->flags |= F_UDPSRC_RND; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds else if (strcmp(f, "!UDPSRC_RND") == 0) 10461da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPSRC_RND; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds else if (strcmp(f, "UDPDST_RND") == 0) 10491da177e4SLinus Torvalds pkt_dev->flags |= F_UDPDST_RND; 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds else if (strcmp(f, "!UDPDST_RND") == 0) 10521da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPDST_RND; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds else if (strcmp(f, "MACSRC_RND") == 0) 10551da177e4SLinus Torvalds pkt_dev->flags |= F_MACSRC_RND; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds else if (strcmp(f, "!MACSRC_RND") == 0) 10581da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACSRC_RND; 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds else if (strcmp(f, "MACDST_RND") == 0) 10611da177e4SLinus Torvalds pkt_dev->flags |= F_MACDST_RND; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds else if (strcmp(f, "!MACDST_RND") == 0) 10641da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACDST_RND; 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds else { 1067222f1806SLuiz Capitulino sprintf(pg_result, 1068222f1806SLuiz Capitulino "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s", 10691da177e4SLinus Torvalds f, 10701da177e4SLinus Torvalds "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n"); 10711da177e4SLinus Torvalds return count; 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags); 10741da177e4SLinus Torvalds return count; 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) { 10771da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1); 1078222f1806SLuiz Capitulino if (len < 0) { 1079222f1806SLuiz Capitulino return len; 1080222f1806SLuiz Capitulino } 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 10831da177e4SLinus Torvalds return -EFAULT; 10841da177e4SLinus Torvalds buf[len] = 0; 10851da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_min) != 0) { 10861da177e4SLinus Torvalds memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min)); 10871da177e4SLinus Torvalds strncpy(pkt_dev->dst_min, buf, len); 10881da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 10891da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds if (debug) 1092222f1806SLuiz Capitulino printk("pktgen: dst_min set to: %s\n", 1093222f1806SLuiz Capitulino pkt_dev->dst_min); 10941da177e4SLinus Torvalds i += len; 10951da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min); 10961da177e4SLinus Torvalds return count; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds if (!strcmp(name, "dst_max")) { 10991da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1); 1100222f1806SLuiz Capitulino if (len < 0) { 1101222f1806SLuiz Capitulino return len; 1102222f1806SLuiz Capitulino } 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11051da177e4SLinus Torvalds return -EFAULT; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds buf[len] = 0; 11081da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_max) != 0) { 11091da177e4SLinus Torvalds memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max)); 11101da177e4SLinus Torvalds strncpy(pkt_dev->dst_max, buf, len); 11111da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 11121da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_max; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds if (debug) 1115222f1806SLuiz Capitulino printk("pktgen: dst_max set to: %s\n", 1116222f1806SLuiz Capitulino pkt_dev->dst_max); 11171da177e4SLinus Torvalds i += len; 11181da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max); 11191da177e4SLinus Torvalds return count; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds if (!strcmp(name, "dst6")) { 11221da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1123222f1806SLuiz Capitulino if (len < 0) 1124222f1806SLuiz Capitulino return len; 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11291da177e4SLinus Torvalds return -EFAULT; 11301da177e4SLinus Torvalds buf[len] = 0; 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_daddr.s6_addr); 11331da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr); 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr); 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds if (debug) 11381da177e4SLinus Torvalds printk("pktgen: dst6 set to: %s\n", buf); 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds i += len; 11411da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6=%s", buf); 11421da177e4SLinus Torvalds return count; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds if (!strcmp(name, "dst6_min")) { 11451da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1146222f1806SLuiz Capitulino if (len < 0) 1147222f1806SLuiz Capitulino return len; 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11521da177e4SLinus Torvalds return -EFAULT; 11531da177e4SLinus Torvalds buf[len] = 0; 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 11561da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 11571da177e4SLinus Torvalds 1158222f1806SLuiz Capitulino ipv6_addr_copy(&pkt_dev->cur_in6_daddr, 1159222f1806SLuiz Capitulino &pkt_dev->min_in6_daddr); 11601da177e4SLinus Torvalds if (debug) 11611da177e4SLinus Torvalds printk("pktgen: dst6_min set to: %s\n", buf); 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds i += len; 11641da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_min=%s", buf); 11651da177e4SLinus Torvalds return count; 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds if (!strcmp(name, "dst6_max")) { 11681da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1169222f1806SLuiz Capitulino if (len < 0) 1170222f1806SLuiz Capitulino return len; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11751da177e4SLinus Torvalds return -EFAULT; 11761da177e4SLinus Torvalds buf[len] = 0; 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 11791da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds if (debug) 11821da177e4SLinus Torvalds printk("pktgen: dst6_max set to: %s\n", buf); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds i += len; 11851da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_max=%s", buf); 11861da177e4SLinus Torvalds return count; 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds if (!strcmp(name, "src6")) { 11891da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1190222f1806SLuiz Capitulino if (len < 0) 1191222f1806SLuiz Capitulino return len; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11961da177e4SLinus Torvalds return -EFAULT; 11971da177e4SLinus Torvalds buf[len] = 0; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_saddr.s6_addr); 12001da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr); 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr); 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds if (debug) 12051da177e4SLinus Torvalds printk("pktgen: src6 set to: %s\n", buf); 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds i += len; 12081da177e4SLinus Torvalds sprintf(pg_result, "OK: src6=%s", buf); 12091da177e4SLinus Torvalds return count; 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds if (!strcmp(name, "src_min")) { 12121da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1); 1213222f1806SLuiz Capitulino if (len < 0) { 1214222f1806SLuiz Capitulino return len; 1215222f1806SLuiz Capitulino } 12161da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12171da177e4SLinus Torvalds return -EFAULT; 12181da177e4SLinus Torvalds buf[len] = 0; 12191da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_min) != 0) { 12201da177e4SLinus Torvalds memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min)); 12211da177e4SLinus Torvalds strncpy(pkt_dev->src_min, buf, len); 12221da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 12231da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds if (debug) 1226222f1806SLuiz Capitulino printk("pktgen: src_min set to: %s\n", 1227222f1806SLuiz Capitulino pkt_dev->src_min); 12281da177e4SLinus Torvalds i += len; 12291da177e4SLinus Torvalds sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min); 12301da177e4SLinus Torvalds return count; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds if (!strcmp(name, "src_max")) { 12331da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1); 1234222f1806SLuiz Capitulino if (len < 0) { 1235222f1806SLuiz Capitulino return len; 1236222f1806SLuiz Capitulino } 12371da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12381da177e4SLinus Torvalds return -EFAULT; 12391da177e4SLinus Torvalds buf[len] = 0; 12401da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_max) != 0) { 12411da177e4SLinus Torvalds memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max)); 12421da177e4SLinus Torvalds strncpy(pkt_dev->src_max, buf, len); 12431da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 12441da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_max; 12451da177e4SLinus Torvalds } 12461da177e4SLinus Torvalds if (debug) 1247222f1806SLuiz Capitulino printk("pktgen: src_max set to: %s\n", 1248222f1806SLuiz Capitulino pkt_dev->src_max); 12491da177e4SLinus Torvalds i += len; 12501da177e4SLinus Torvalds sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max); 12511da177e4SLinus Torvalds return count; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds if (!strcmp(name, "dst_mac")) { 12541da177e4SLinus Torvalds char *v = valstr; 1255f404e9a6SKris Katterjohn unsigned char old_dmac[ETH_ALEN]; 12561da177e4SLinus Torvalds unsigned char *m = pkt_dev->dst_mac; 1257f404e9a6SKris Katterjohn memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN); 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 1260222f1806SLuiz Capitulino if (len < 0) { 1261222f1806SLuiz Capitulino return len; 1262222f1806SLuiz Capitulino } 12631da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 12641da177e4SLinus Torvalds if (copy_from_user(valstr, &user_buffer[i], len)) 12651da177e4SLinus Torvalds return -EFAULT; 12661da177e4SLinus Torvalds i += len; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) { 12691da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 12701da177e4SLinus Torvalds *m *= 16; 12711da177e4SLinus Torvalds *m += *v - '0'; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 12741da177e4SLinus Torvalds *m *= 16; 12751da177e4SLinus Torvalds *m += *v - 'A' + 10; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 12781da177e4SLinus Torvalds *m *= 16; 12791da177e4SLinus Torvalds *m += *v - 'a' + 10; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds if (*v == ':') { 12821da177e4SLinus Torvalds m++; 12831da177e4SLinus Torvalds *m = 0; 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds /* Set up Dest MAC */ 1288f404e9a6SKris Katterjohn if (compare_ether_addr(old_dmac, pkt_dev->dst_mac)) 1289f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds sprintf(pg_result, "OK: dstmac"); 12921da177e4SLinus Torvalds return count; 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds if (!strcmp(name, "src_mac")) { 12951da177e4SLinus Torvalds char *v = valstr; 12961da177e4SLinus Torvalds unsigned char *m = pkt_dev->src_mac; 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 1299222f1806SLuiz Capitulino if (len < 0) { 1300222f1806SLuiz Capitulino return len; 1301222f1806SLuiz Capitulino } 13021da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 13031da177e4SLinus Torvalds if (copy_from_user(valstr, &user_buffer[i], len)) 13041da177e4SLinus Torvalds return -EFAULT; 13051da177e4SLinus Torvalds i += len; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) { 13081da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 13091da177e4SLinus Torvalds *m *= 16; 13101da177e4SLinus Torvalds *m += *v - '0'; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 13131da177e4SLinus Torvalds *m *= 16; 13141da177e4SLinus Torvalds *m += *v - 'A' + 10; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 13171da177e4SLinus Torvalds *m *= 16; 13181da177e4SLinus Torvalds *m += *v - 'a' + 10; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds if (*v == ':') { 13211da177e4SLinus Torvalds m++; 13221da177e4SLinus Torvalds *m = 0; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds sprintf(pg_result, "OK: srcmac"); 13271da177e4SLinus Torvalds return count; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds if (!strcmp(name, "clear_counters")) { 13311da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 13321da177e4SLinus Torvalds sprintf(pg_result, "OK: Clearing counters.\n"); 13331da177e4SLinus Torvalds return count; 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds if (!strcmp(name, "flows")) { 13371da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1338222f1806SLuiz Capitulino if (len < 0) { 1339222f1806SLuiz Capitulino return len; 1340222f1806SLuiz Capitulino } 13411da177e4SLinus Torvalds i += len; 13421da177e4SLinus Torvalds if (value > MAX_CFLOWS) 13431da177e4SLinus Torvalds value = MAX_CFLOWS; 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds pkt_dev->cflows = value; 13461da177e4SLinus Torvalds sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows); 13471da177e4SLinus Torvalds return count; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds if (!strcmp(name, "flowlen")) { 13511da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1352222f1806SLuiz Capitulino if (len < 0) { 1353222f1806SLuiz Capitulino return len; 1354222f1806SLuiz Capitulino } 13551da177e4SLinus Torvalds i += len; 13561da177e4SLinus Torvalds pkt_dev->lflow = value; 13571da177e4SLinus Torvalds sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow); 13581da177e4SLinus Torvalds return count; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds sprintf(pkt_dev->result, "No such parameter \"%s\"", name); 13621da177e4SLinus Torvalds return -EINVAL; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 1365d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file) 13661da177e4SLinus Torvalds { 1367d50a6b56SStephen Hemminger return single_open(file, pktgen_if_show, PDE(inode)->data); 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 1370d50a6b56SStephen Hemminger static struct file_operations pktgen_if_fops = { 1371d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1372d50a6b56SStephen Hemminger .open = pktgen_if_open, 1373d50a6b56SStephen Hemminger .read = seq_read, 1374d50a6b56SStephen Hemminger .llseek = seq_lseek, 1375d50a6b56SStephen Hemminger .write = pktgen_if_write, 1376d50a6b56SStephen Hemminger .release = single_release, 1377d50a6b56SStephen Hemminger }; 1378d50a6b56SStephen Hemminger 1379d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v) 1380d50a6b56SStephen Hemminger { 1381d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 1382c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 1383d50a6b56SStephen Hemminger 1384d50a6b56SStephen Hemminger BUG_ON(!t); 1385d50a6b56SStephen Hemminger 1386d50a6b56SStephen Hemminger seq_printf(seq, "Name: %s max_before_softirq: %d\n", 13871da177e4SLinus Torvalds t->name, t->max_before_softirq); 13881da177e4SLinus Torvalds 1389d50a6b56SStephen Hemminger seq_printf(seq, "Running: "); 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds if_lock(t); 1392c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 13931da177e4SLinus Torvalds if (pkt_dev->running) 1394d50a6b56SStephen Hemminger seq_printf(seq, "%s ", pkt_dev->ifname); 13951da177e4SLinus Torvalds 1396d50a6b56SStephen Hemminger seq_printf(seq, "\nStopped: "); 13971da177e4SLinus Torvalds 1398c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 13991da177e4SLinus Torvalds if (!pkt_dev->running) 1400d50a6b56SStephen Hemminger seq_printf(seq, "%s ", pkt_dev->ifname); 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds if (t->result[0]) 1403d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: %s\n", t->result); 14041da177e4SLinus Torvalds else 1405d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: NA\n"); 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds if_unlock(t); 14081da177e4SLinus Torvalds 1409d50a6b56SStephen Hemminger return 0; 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 1412d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file, 1413d50a6b56SStephen Hemminger const char __user * user_buffer, 1414d50a6b56SStephen Hemminger size_t count, loff_t * offset) 14151da177e4SLinus Torvalds { 1416d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *)file->private_data; 1417d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 14181da177e4SLinus Torvalds int i = 0, max, len, ret; 14191da177e4SLinus Torvalds char name[40]; 14201da177e4SLinus Torvalds char *pg_result; 14211da177e4SLinus Torvalds unsigned long value = 0; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds if (count < 1) { 14241da177e4SLinus Torvalds // sprintf(pg_result, "Wrong command format"); 14251da177e4SLinus Torvalds return -EINVAL; 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds max = count - i; 14291da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 14301da177e4SLinus Torvalds if (len < 0) 14311da177e4SLinus Torvalds return len; 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds i += len; 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds /* Read variable name */ 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 14381da177e4SLinus Torvalds if (len < 0) 14391da177e4SLinus Torvalds return len; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 14421da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len)) 14431da177e4SLinus Torvalds return -EFAULT; 14441da177e4SLinus Torvalds i += len; 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds max = count - i; 14471da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 14481da177e4SLinus Torvalds if (len < 0) 14491da177e4SLinus Torvalds return len; 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds i += len; 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds if (debug) 1454222f1806SLuiz Capitulino printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count); 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds if (!t) { 14571da177e4SLinus Torvalds printk("pktgen: ERROR: No thread\n"); 14581da177e4SLinus Torvalds ret = -EINVAL; 14591da177e4SLinus Torvalds goto out; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds pg_result = &(t->result[0]); 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds if (!strcmp(name, "add_device")) { 14651da177e4SLinus Torvalds char f[32]; 14661da177e4SLinus Torvalds memset(f, 0, 32); 14671da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 14681da177e4SLinus Torvalds if (len < 0) { 14691da177e4SLinus Torvalds ret = len; 14701da177e4SLinus Torvalds goto out; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds if (copy_from_user(f, &user_buffer[i], len)) 14731da177e4SLinus Torvalds return -EFAULT; 14741da177e4SLinus Torvalds i += len; 14751da177e4SLinus Torvalds thread_lock(); 14761da177e4SLinus Torvalds pktgen_add_device(t, f); 14771da177e4SLinus Torvalds thread_unlock(); 14781da177e4SLinus Torvalds ret = count; 14791da177e4SLinus Torvalds sprintf(pg_result, "OK: add_device=%s", f); 14801da177e4SLinus Torvalds goto out; 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds if (!strcmp(name, "rem_device_all")) { 14841da177e4SLinus Torvalds thread_lock(); 148595ed63f7SArthur Kepner t->control |= T_REMDEVALL; 14861da177e4SLinus Torvalds thread_unlock(); 1487121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 14881da177e4SLinus Torvalds ret = count; 14891da177e4SLinus Torvalds sprintf(pg_result, "OK: rem_device_all"); 14901da177e4SLinus Torvalds goto out; 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds if (!strcmp(name, "max_before_softirq")) { 14941da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 14951da177e4SLinus Torvalds thread_lock(); 14961da177e4SLinus Torvalds t->max_before_softirq = value; 14971da177e4SLinus Torvalds thread_unlock(); 14981da177e4SLinus Torvalds ret = count; 14991da177e4SLinus Torvalds sprintf(pg_result, "OK: max_before_softirq=%lu", value); 15001da177e4SLinus Torvalds goto out; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds ret = -EINVAL; 15041da177e4SLinus Torvalds out: 15051da177e4SLinus Torvalds return ret; 15061da177e4SLinus Torvalds } 15071da177e4SLinus Torvalds 1508d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file) 15091da177e4SLinus Torvalds { 1510d50a6b56SStephen Hemminger return single_open(file, pktgen_thread_show, PDE(inode)->data); 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds 1513d50a6b56SStephen Hemminger static struct file_operations pktgen_thread_fops = { 1514d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1515d50a6b56SStephen Hemminger .open = pktgen_thread_open, 1516d50a6b56SStephen Hemminger .read = seq_read, 1517d50a6b56SStephen Hemminger .llseek = seq_lseek, 1518d50a6b56SStephen Hemminger .write = pktgen_thread_write, 1519d50a6b56SStephen Hemminger .release = single_release, 1520d50a6b56SStephen Hemminger }; 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds /* Think find or remove for NN */ 15231da177e4SLinus Torvalds static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove) 15241da177e4SLinus Torvalds { 15251da177e4SLinus Torvalds struct pktgen_thread *t; 15261da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 15271da177e4SLinus Torvalds 1528cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) { 15291da177e4SLinus Torvalds pkt_dev = pktgen_find_dev(t, ifname); 15301da177e4SLinus Torvalds if (pkt_dev) { 15311da177e4SLinus Torvalds if (remove) { 15321da177e4SLinus Torvalds if_lock(t); 153395ed63f7SArthur Kepner pkt_dev->removal_mark = 1; 153495ed63f7SArthur Kepner t->control |= T_REMDEV; 15351da177e4SLinus Torvalds if_unlock(t); 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds break; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds return pkt_dev; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 154395ed63f7SArthur Kepner /* 154495ed63f7SArthur Kepner * mark a device for removal 154595ed63f7SArthur Kepner */ 154695ed63f7SArthur Kepner static int pktgen_mark_device(const char *ifname) 15471da177e4SLinus Torvalds { 15481da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 154995ed63f7SArthur Kepner const int max_tries = 10, msec_per_try = 125; 155095ed63f7SArthur Kepner int i = 0; 155195ed63f7SArthur Kepner int ret = 0; 155295ed63f7SArthur Kepner 15531da177e4SLinus Torvalds thread_lock(); 155495ed63f7SArthur Kepner PG_DEBUG(printk("pktgen: pktgen_mark_device marking %s for removal\n", 155595ed63f7SArthur Kepner ifname)); 155695ed63f7SArthur Kepner 155795ed63f7SArthur Kepner while (1) { 155895ed63f7SArthur Kepner 155995ed63f7SArthur Kepner pkt_dev = __pktgen_NN_threads(ifname, REMOVE); 1560222f1806SLuiz Capitulino if (pkt_dev == NULL) 1561222f1806SLuiz Capitulino break; /* success */ 156295ed63f7SArthur Kepner 15631da177e4SLinus Torvalds thread_unlock(); 156495ed63f7SArthur Kepner PG_DEBUG(printk("pktgen: pktgen_mark_device waiting for %s " 156595ed63f7SArthur Kepner "to disappear....\n", ifname)); 156695ed63f7SArthur Kepner schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try)); 156795ed63f7SArthur Kepner thread_lock(); 156895ed63f7SArthur Kepner 156995ed63f7SArthur Kepner if (++i >= max_tries) { 157095ed63f7SArthur Kepner printk("pktgen_mark_device: timed out after waiting " 157195ed63f7SArthur Kepner "%d msec for device %s to be removed\n", 157295ed63f7SArthur Kepner msec_per_try * i, ifname); 157395ed63f7SArthur Kepner ret = 1; 157495ed63f7SArthur Kepner break; 157595ed63f7SArthur Kepner } 157695ed63f7SArthur Kepner 157795ed63f7SArthur Kepner } 157895ed63f7SArthur Kepner 157995ed63f7SArthur Kepner thread_unlock(); 158095ed63f7SArthur Kepner 158195ed63f7SArthur Kepner return ret; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds 1584222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused, 1585222f1806SLuiz Capitulino unsigned long event, void *ptr) 15861da177e4SLinus Torvalds { 15871da177e4SLinus Torvalds struct net_device *dev = (struct net_device *)(ptr); 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds /* It is OK that we do not hold the group lock right now, 15901da177e4SLinus Torvalds * as we run under the RTNL lock. 15911da177e4SLinus Torvalds */ 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds switch (event) { 15941da177e4SLinus Torvalds case NETDEV_CHANGEADDR: 15951da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 15961da177e4SLinus Torvalds case NETDEV_DOWN: 15971da177e4SLinus Torvalds case NETDEV_UP: 15981da177e4SLinus Torvalds /* Ignore for now */ 15991da177e4SLinus Torvalds break; 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds case NETDEV_UNREGISTER: 160295ed63f7SArthur Kepner pktgen_mark_device(dev->name); 16031da177e4SLinus Torvalds break; 16041da177e4SLinus Torvalds }; 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds return NOTIFY_DONE; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */ 16101da177e4SLinus Torvalds 1611222f1806SLuiz Capitulino static struct net_device *pktgen_setup_dev(struct pktgen_dev *pkt_dev) 1612222f1806SLuiz Capitulino { 16131da177e4SLinus Torvalds struct net_device *odev; 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds /* Clean old setups */ 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds if (pkt_dev->odev) { 16181da177e4SLinus Torvalds dev_put(pkt_dev->odev); 16191da177e4SLinus Torvalds pkt_dev->odev = NULL; 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds odev = dev_get_by_name(pkt_dev->ifname); 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds if (!odev) { 16251da177e4SLinus Torvalds printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname); 16261da177e4SLinus Torvalds goto out; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds if (odev->type != ARPHRD_ETHER) { 1629222f1806SLuiz Capitulino printk("pktgen: not an ethernet device: \"%s\"\n", 1630222f1806SLuiz Capitulino pkt_dev->ifname); 16311da177e4SLinus Torvalds goto out_put; 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds if (!netif_running(odev)) { 16341da177e4SLinus Torvalds printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname); 16351da177e4SLinus Torvalds goto out_put; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds pkt_dev->odev = odev; 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds return pkt_dev->odev; 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds out_put: 16421da177e4SLinus Torvalds dev_put(odev); 16431da177e4SLinus Torvalds out: 16441da177e4SLinus Torvalds return NULL; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev 16491da177e4SLinus Torvalds * structure to have the right information to create/send packets 16501da177e4SLinus Torvalds */ 16511da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev) 16521da177e4SLinus Torvalds { 16531da177e4SLinus Torvalds /* Try once more, just in case it works now. */ 16541da177e4SLinus Torvalds if (!pkt_dev->odev) 16551da177e4SLinus Torvalds pktgen_setup_dev(pkt_dev); 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds if (!pkt_dev->odev) { 16581da177e4SLinus Torvalds printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n"); 1659222f1806SLuiz Capitulino sprintf(pkt_dev->result, 1660222f1806SLuiz Capitulino "ERROR: pkt_dev->odev == NULL in setup_inject.\n"); 16611da177e4SLinus Torvalds return; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds /* Default to the interface's mac if not explicitly set. */ 16651da177e4SLinus Torvalds 1666f404e9a6SKris Katterjohn if (is_zero_ether_addr(pkt_dev->src_mac)) 1667f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN); 16681da177e4SLinus Torvalds 16691da177e4SLinus Torvalds /* Set up Dest MAC */ 1670f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds /* Set up pkt size */ 16731da177e4SLinus Torvalds pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 16761da177e4SLinus Torvalds /* 16771da177e4SLinus Torvalds * Skip this automatic address setting until locks or functions 16781da177e4SLinus Torvalds * gets exported 16791da177e4SLinus Torvalds */ 16801da177e4SLinus Torvalds 16811da177e4SLinus Torvalds #ifdef NOTNOW 16821da177e4SLinus Torvalds int i, set = 0, err = 1; 16831da177e4SLinus Torvalds struct inet6_dev *idev; 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) 16861da177e4SLinus Torvalds if (pkt_dev->cur_in6_saddr.s6_addr[i]) { 16871da177e4SLinus Torvalds set = 1; 16881da177e4SLinus Torvalds break; 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds if (!set) { 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds /* 16941da177e4SLinus Torvalds * Use linklevel address if unconfigured. 16951da177e4SLinus Torvalds * 16961da177e4SLinus Torvalds * use ipv6_get_lladdr if/when it's get exported 16971da177e4SLinus Torvalds */ 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds read_lock(&addrconf_lock); 17001da177e4SLinus Torvalds if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) { 17011da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1704222f1806SLuiz Capitulino for (ifp = idev->addr_list; ifp; 1705222f1806SLuiz Capitulino ifp = ifp->if_next) { 1706222f1806SLuiz Capitulino if (ifp->scope == IFA_LINK 1707222f1806SLuiz Capitulino && !(ifp-> 1708222f1806SLuiz Capitulino flags & IFA_F_TENTATIVE)) { 1709222f1806SLuiz Capitulino ipv6_addr_copy(&pkt_dev-> 1710222f1806SLuiz Capitulino cur_in6_saddr, 1711222f1806SLuiz Capitulino &ifp->addr); 17121da177e4SLinus Torvalds err = 0; 17131da177e4SLinus Torvalds break; 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 17171da177e4SLinus Torvalds } 17181da177e4SLinus Torvalds read_unlock(&addrconf_lock); 1719222f1806SLuiz Capitulino if (err) 1720222f1806SLuiz Capitulino printk("pktgen: ERROR: IPv6 link address not availble.\n"); 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds #endif 1723222f1806SLuiz Capitulino } else { 17241da177e4SLinus Torvalds pkt_dev->saddr_min = 0; 17251da177e4SLinus Torvalds pkt_dev->saddr_max = 0; 17261da177e4SLinus Torvalds if (strlen(pkt_dev->src_min) == 0) { 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds struct in_device *in_dev; 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds rcu_read_lock(); 1731e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(pkt_dev->odev); 17321da177e4SLinus Torvalds if (in_dev) { 17331da177e4SLinus Torvalds if (in_dev->ifa_list) { 1734222f1806SLuiz Capitulino pkt_dev->saddr_min = 1735222f1806SLuiz Capitulino in_dev->ifa_list->ifa_address; 17361da177e4SLinus Torvalds pkt_dev->saddr_max = pkt_dev->saddr_min; 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds } 17391da177e4SLinus Torvalds rcu_read_unlock(); 1740222f1806SLuiz Capitulino } else { 17411da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 17421da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 17461da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds /* Initialize current values. */ 17491da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 17501da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 17511da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 17521da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 17531da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 17541da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 17551da177e4SLinus Torvalds pkt_dev->nflows = 0; 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us) 17591da177e4SLinus Torvalds { 17601da177e4SLinus Torvalds __u64 start; 17611da177e4SLinus Torvalds __u64 now; 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds start = now = getCurUs(); 17641da177e4SLinus Torvalds printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now)); 17651da177e4SLinus Torvalds while (now < spin_until_us) { 1766b4099fabSStephen Hemminger /* TODO: optimize sleeping behavior */ 1767121caf57SNishanth Aravamudan if (spin_until_us - now > jiffies_to_usecs(1) + 1) 1768121caf57SNishanth Aravamudan schedule_timeout_interruptible(1); 1769121caf57SNishanth Aravamudan else if (spin_until_us - now > 100) { 17701da177e4SLinus Torvalds do_softirq(); 17711da177e4SLinus Torvalds if (!pkt_dev->running) 17721da177e4SLinus Torvalds return; 17731da177e4SLinus Torvalds if (need_resched()) 17741da177e4SLinus Torvalds schedule(); 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds now = getCurUs(); 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds pkt_dev->idle_acc += now - start; 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values 17841da177e4SLinus Torvalds * for IP src/dest, UDP src/dst port, MAC-Addr src/dst 17851da177e4SLinus Torvalds */ 1786222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev) 1787222f1806SLuiz Capitulino { 17881da177e4SLinus Torvalds __u32 imn; 17891da177e4SLinus Torvalds __u32 imx; 17901da177e4SLinus Torvalds int flow = 0; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds if (pkt_dev->cflows) { 17931da177e4SLinus Torvalds flow = pktgen_random() % pkt_dev->cflows; 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds if (pkt_dev->flows[flow].count > pkt_dev->lflow) 17961da177e4SLinus Torvalds pkt_dev->flows[flow].count = 0; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds /* Deal with source MAC */ 18001da177e4SLinus Torvalds if (pkt_dev->src_mac_count > 1) { 18011da177e4SLinus Torvalds __u32 mc; 18021da177e4SLinus Torvalds __u32 tmp; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 18051da177e4SLinus Torvalds mc = pktgen_random() % (pkt_dev->src_mac_count); 18061da177e4SLinus Torvalds else { 18071da177e4SLinus Torvalds mc = pkt_dev->cur_src_mac_offset++; 1808222f1806SLuiz Capitulino if (pkt_dev->cur_src_mac_offset > 1809222f1806SLuiz Capitulino pkt_dev->src_mac_count) 18101da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds tmp = pkt_dev->src_mac[5] + (mc & 0xFF); 18141da177e4SLinus Torvalds pkt_dev->hh[11] = tmp; 18151da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 18161da177e4SLinus Torvalds pkt_dev->hh[10] = tmp; 18171da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 18181da177e4SLinus Torvalds pkt_dev->hh[9] = tmp; 18191da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 18201da177e4SLinus Torvalds pkt_dev->hh[8] = tmp; 18211da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[1] + (tmp >> 8)); 18221da177e4SLinus Torvalds pkt_dev->hh[7] = tmp; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds /* Deal with Destination MAC */ 18261da177e4SLinus Torvalds if (pkt_dev->dst_mac_count > 1) { 18271da177e4SLinus Torvalds __u32 mc; 18281da177e4SLinus Torvalds __u32 tmp; 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 18311da177e4SLinus Torvalds mc = pktgen_random() % (pkt_dev->dst_mac_count); 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds else { 18341da177e4SLinus Torvalds mc = pkt_dev->cur_dst_mac_offset++; 1835222f1806SLuiz Capitulino if (pkt_dev->cur_dst_mac_offset > 1836222f1806SLuiz Capitulino pkt_dev->dst_mac_count) { 18371da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds tmp = pkt_dev->dst_mac[5] + (mc & 0xFF); 18421da177e4SLinus Torvalds pkt_dev->hh[5] = tmp; 18431da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 18441da177e4SLinus Torvalds pkt_dev->hh[4] = tmp; 18451da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 18461da177e4SLinus Torvalds pkt_dev->hh[3] = tmp; 18471da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 18481da177e4SLinus Torvalds pkt_dev->hh[2] = tmp; 18491da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[1] + (tmp >> 8)); 18501da177e4SLinus Torvalds pkt_dev->hh[1] = tmp; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) { 18541da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 1855222f1806SLuiz Capitulino pkt_dev->cur_udp_src = 1856222f1806SLuiz Capitulino ((pktgen_random() % 1857222f1806SLuiz Capitulino (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) + 1858222f1806SLuiz Capitulino pkt_dev->udp_src_min); 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds else { 18611da177e4SLinus Torvalds pkt_dev->cur_udp_src++; 18621da177e4SLinus Torvalds if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max) 18631da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 18641da177e4SLinus Torvalds } 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) { 18681da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) { 1869222f1806SLuiz Capitulino pkt_dev->cur_udp_dst = 1870222f1806SLuiz Capitulino ((pktgen_random() % 1871222f1806SLuiz Capitulino (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) + 1872222f1806SLuiz Capitulino pkt_dev->udp_dst_min); 1873222f1806SLuiz Capitulino } else { 18741da177e4SLinus Torvalds pkt_dev->cur_udp_dst++; 18751da177e4SLinus Torvalds if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max) 18761da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds if (!(pkt_dev->flags & F_IPV6)) { 18811da177e4SLinus Torvalds 1882222f1806SLuiz Capitulino if ((imn = ntohl(pkt_dev->saddr_min)) < (imx = 1883222f1806SLuiz Capitulino ntohl(pkt_dev-> 1884222f1806SLuiz Capitulino saddr_max))) { 18851da177e4SLinus Torvalds __u32 t; 18861da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 18871da177e4SLinus Torvalds t = ((pktgen_random() % (imx - imn)) + imn); 18881da177e4SLinus Torvalds else { 18891da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_saddr); 18901da177e4SLinus Torvalds t++; 18911da177e4SLinus Torvalds if (t > imx) { 18921da177e4SLinus Torvalds t = imn; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds pkt_dev->cur_saddr = htonl(t); 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) { 18991da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr; 19001da177e4SLinus Torvalds } else { 19011da177e4SLinus Torvalds 1902222f1806SLuiz Capitulino if ((imn = ntohl(pkt_dev->daddr_min)) < (imx = 1903222f1806SLuiz Capitulino ntohl(pkt_dev-> 1904222f1806SLuiz Capitulino daddr_max))) 1905222f1806SLuiz Capitulino { 19061da177e4SLinus Torvalds __u32 t; 19071da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) { 19081da177e4SLinus Torvalds 1909222f1806SLuiz Capitulino t = ((pktgen_random() % (imx - imn)) + 1910222f1806SLuiz Capitulino imn); 19111da177e4SLinus Torvalds t = htonl(t); 19121da177e4SLinus Torvalds 1913222f1806SLuiz Capitulino while (LOOPBACK(t) || MULTICAST(t) 1914222f1806SLuiz Capitulino || BADCLASS(t) || ZERONET(t) 1915222f1806SLuiz Capitulino || LOCAL_MCAST(t)) { 1916222f1806SLuiz Capitulino t = ((pktgen_random() % 1917222f1806SLuiz Capitulino (imx - imn)) + imn); 19181da177e4SLinus Torvalds t = htonl(t); 19191da177e4SLinus Torvalds } 19201da177e4SLinus Torvalds pkt_dev->cur_daddr = t; 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds else { 19241da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_daddr); 19251da177e4SLinus Torvalds t++; 19261da177e4SLinus Torvalds if (t > imx) { 19271da177e4SLinus Torvalds t = imn; 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds pkt_dev->cur_daddr = htonl(t); 19301da177e4SLinus Torvalds } 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds if (pkt_dev->cflows) { 1933222f1806SLuiz Capitulino pkt_dev->flows[flow].cur_daddr = 1934222f1806SLuiz Capitulino pkt_dev->cur_daddr; 19351da177e4SLinus Torvalds pkt_dev->nflows++; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds } 1938222f1806SLuiz Capitulino } else { /* IPV6 * */ 1939222f1806SLuiz Capitulino 19401da177e4SLinus Torvalds if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 && 19411da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[1] == 0 && 19421da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[2] == 0 && 19431da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ; 19441da177e4SLinus Torvalds else { 19451da177e4SLinus Torvalds int i; 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* Only random destinations yet */ 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds for (i = 0; i < 4; i++) { 19501da177e4SLinus Torvalds pkt_dev->cur_in6_daddr.s6_addr32[i] = 19511da177e4SLinus Torvalds ((pktgen_random() | 19521da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[i]) & 19531da177e4SLinus Torvalds pkt_dev->max_in6_daddr.s6_addr32[i]); 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds } 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) { 19591da177e4SLinus Torvalds __u32 t; 19601da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) { 1961222f1806SLuiz Capitulino t = ((pktgen_random() % 1962222f1806SLuiz Capitulino (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)) 19631da177e4SLinus Torvalds + pkt_dev->min_pkt_size); 1964222f1806SLuiz Capitulino } else { 19651da177e4SLinus Torvalds t = pkt_dev->cur_pkt_size + 1; 19661da177e4SLinus Torvalds if (t > pkt_dev->max_pkt_size) 19671da177e4SLinus Torvalds t = pkt_dev->min_pkt_size; 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds pkt_dev->cur_pkt_size = t; 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds pkt_dev->flows[flow].count++; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev, 19761da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 19771da177e4SLinus Torvalds { 19781da177e4SLinus Torvalds struct sk_buff *skb = NULL; 19791da177e4SLinus Torvalds __u8 *eth; 19801da177e4SLinus Torvalds struct udphdr *udph; 19811da177e4SLinus Torvalds int datalen, iplen; 19821da177e4SLinus Torvalds struct iphdr *iph; 19831da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 19841da177e4SLinus Torvalds 198564053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 198664053beeSRobert Olsson * fields. 198764053beeSRobert Olsson */ 198864053beeSRobert Olsson mod_cur_headers(pkt_dev); 198964053beeSRobert Olsson 19907ac5459eSDavid S. Miller datalen = (odev->hard_header_len + 16) & ~0xf; 19917ac5459eSDavid S. Miller skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen, GFP_ATOMIC); 19921da177e4SLinus Torvalds if (!skb) { 19931da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 19941da177e4SLinus Torvalds return NULL; 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 19977ac5459eSDavid S. Miller skb_reserve(skb, datalen); 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 20001da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 20011da177e4SLinus Torvalds iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)); 20021da177e4SLinus Torvalds udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr)); 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 20051da177e4SLinus Torvalds *(u16 *) & eth[12] = __constant_htons(ETH_P_IP); 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8; /* Eth + IPh + UDPh */ 20081da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) 20091da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 20121da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 20131da177e4SLinus Torvalds udph->len = htons(datalen + 8); /* DATA + udphdr */ 20141da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds iph->ihl = 5; 20171da177e4SLinus Torvalds iph->version = 4; 20181da177e4SLinus Torvalds iph->ttl = 32; 20191da177e4SLinus Torvalds iph->tos = 0; 20201da177e4SLinus Torvalds iph->protocol = IPPROTO_UDP; /* UDP */ 20211da177e4SLinus Torvalds iph->saddr = pkt_dev->cur_saddr; 20221da177e4SLinus Torvalds iph->daddr = pkt_dev->cur_daddr; 20231da177e4SLinus Torvalds iph->frag_off = 0; 20241da177e4SLinus Torvalds iplen = 20 + 8 + datalen; 20251da177e4SLinus Torvalds iph->tot_len = htons(iplen); 20261da177e4SLinus Torvalds iph->check = 0; 20271da177e4SLinus Torvalds iph->check = ip_fast_csum((void *)iph, iph->ihl); 20281da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_IP); 20291da177e4SLinus Torvalds skb->mac.raw = ((u8 *) iph) - 14; 20301da177e4SLinus Torvalds skb->dev = odev; 20311da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 20341da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 20351da177e4SLinus Torvalds else { 20361da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 20371da177e4SLinus Torvalds int i; 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 20421da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 20431da177e4SLinus Torvalds if (datalen > frags * PAGE_SIZE) { 20441da177e4SLinus Torvalds skb_put(skb, datalen - frags * PAGE_SIZE); 20451da177e4SLinus Torvalds datalen = frags * PAGE_SIZE; 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds i = 0; 20491da177e4SLinus Torvalds while (datalen > 0) { 20501da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 20511da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 20521da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 20531da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 20541da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 20551da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 20561da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 20571da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 20581da177e4SLinus Torvalds i++; 20591da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds while (i < frags) { 20631da177e4SLinus Torvalds int rem; 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds if (i == 0) 20661da177e4SLinus Torvalds break; 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 20691da177e4SLinus Torvalds if (rem == 0) 20701da177e4SLinus Torvalds break; 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 20731da177e4SLinus Torvalds 2074222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i] = 2075222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1]; 20761da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 2077222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page = 2078222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].page; 2079222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page_offset += 2080222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].size; 20811da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 20821da177e4SLinus Torvalds i++; 20831da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 20841da177e4SLinus Torvalds } 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds if (pgh) { 20901da177e4SLinus Torvalds struct timeval timestamp; 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 20931da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds do_gettimeofday(×tamp); 20961da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 20971da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 20981da177e4SLinus Torvalds } 20991da177e4SLinus Torvalds pkt_dev->seq_num++; 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds return skb; 21021da177e4SLinus Torvalds } 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds /* 21051da177e4SLinus Torvalds * scan_ip6, fmt_ip taken from dietlibc-0.21 21061da177e4SLinus Torvalds * Author Felix von Leitner <felix-dietlibc@fefe.de> 21071da177e4SLinus Torvalds * 21081da177e4SLinus Torvalds * Slightly modified for kernel. 21091da177e4SLinus Torvalds * Should be candidate for net/ipv4/utils.c 21101da177e4SLinus Torvalds * --ro 21111da177e4SLinus Torvalds */ 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]) 21141da177e4SLinus Torvalds { 21151da177e4SLinus Torvalds unsigned int i; 21161da177e4SLinus Torvalds unsigned int len = 0; 21171da177e4SLinus Torvalds unsigned long u; 21181da177e4SLinus Torvalds char suffix[16]; 21191da177e4SLinus Torvalds unsigned int prefixlen = 0; 21201da177e4SLinus Torvalds unsigned int suffixlen = 0; 21211da177e4SLinus Torvalds __u32 tmp; 21221da177e4SLinus Torvalds 2123222f1806SLuiz Capitulino for (i = 0; i < 16; i++) 2124222f1806SLuiz Capitulino ip[i] = 0; 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds for (;;) { 21271da177e4SLinus Torvalds if (*s == ':') { 21281da177e4SLinus Torvalds len++; 21291da177e4SLinus Torvalds if (s[1] == ':') { /* Found "::", skip to part 2 */ 21301da177e4SLinus Torvalds s += 2; 21311da177e4SLinus Torvalds len++; 21321da177e4SLinus Torvalds break; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds s++; 21351da177e4SLinus Torvalds } 21361da177e4SLinus Torvalds { 21371da177e4SLinus Torvalds char *tmp; 21381da177e4SLinus Torvalds u = simple_strtoul(s, &tmp, 16); 21391da177e4SLinus Torvalds i = tmp - s; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 2142222f1806SLuiz Capitulino if (!i) 2143222f1806SLuiz Capitulino return 0; 21441da177e4SLinus Torvalds if (prefixlen == 12 && s[i] == '.') { 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds /* the last 4 bytes may be written as IPv4 address */ 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds tmp = in_aton(s); 21491da177e4SLinus Torvalds memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp)); 21501da177e4SLinus Torvalds return i + len; 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds ip[prefixlen++] = (u >> 8); 21531da177e4SLinus Torvalds ip[prefixlen++] = (u & 255); 2154222f1806SLuiz Capitulino s += i; 2155222f1806SLuiz Capitulino len += i; 21561da177e4SLinus Torvalds if (prefixlen == 16) 21571da177e4SLinus Torvalds return len; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds /* part 2, after "::" */ 21611da177e4SLinus Torvalds for (;;) { 21621da177e4SLinus Torvalds if (*s == ':') { 21631da177e4SLinus Torvalds if (suffixlen == 0) 21641da177e4SLinus Torvalds break; 21651da177e4SLinus Torvalds s++; 21661da177e4SLinus Torvalds len++; 21671da177e4SLinus Torvalds } else if (suffixlen != 0) 21681da177e4SLinus Torvalds break; 21691da177e4SLinus Torvalds { 21701da177e4SLinus Torvalds char *tmp; 21711da177e4SLinus Torvalds u = simple_strtol(s, &tmp, 16); 21721da177e4SLinus Torvalds i = tmp - s; 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds if (!i) { 2175222f1806SLuiz Capitulino if (*s) 2176222f1806SLuiz Capitulino len--; 21771da177e4SLinus Torvalds break; 21781da177e4SLinus Torvalds } 21791da177e4SLinus Torvalds if (suffixlen + prefixlen <= 12 && s[i] == '.') { 21801da177e4SLinus Torvalds tmp = in_aton(s); 2181222f1806SLuiz Capitulino memcpy((struct in_addr *)(suffix + suffixlen), &tmp, 2182222f1806SLuiz Capitulino sizeof(tmp)); 21831da177e4SLinus Torvalds suffixlen += 4; 21841da177e4SLinus Torvalds len += strlen(s); 21851da177e4SLinus Torvalds break; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds suffix[suffixlen++] = (u >> 8); 21881da177e4SLinus Torvalds suffix[suffixlen++] = (u & 255); 2189222f1806SLuiz Capitulino s += i; 2190222f1806SLuiz Capitulino len += i; 21911da177e4SLinus Torvalds if (prefixlen + suffixlen == 16) 21921da177e4SLinus Torvalds break; 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds for (i = 0; i < suffixlen; i++) 21951da177e4SLinus Torvalds ip[16 - suffixlen + i] = suffix[i]; 21961da177e4SLinus Torvalds return len; 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 2199222f1806SLuiz Capitulino static char tohex(char hexdigit) 2200222f1806SLuiz Capitulino { 22011da177e4SLinus Torvalds return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0'; 22021da177e4SLinus Torvalds } 22031da177e4SLinus Torvalds 2204222f1806SLuiz Capitulino static int fmt_xlong(char *s, unsigned int i) 2205222f1806SLuiz Capitulino { 22061da177e4SLinus Torvalds char *bak = s; 2207222f1806SLuiz Capitulino *s = tohex((i >> 12) & 0xf); 2208222f1806SLuiz Capitulino if (s != bak || *s != '0') 2209222f1806SLuiz Capitulino ++s; 2210222f1806SLuiz Capitulino *s = tohex((i >> 8) & 0xf); 2211222f1806SLuiz Capitulino if (s != bak || *s != '0') 2212222f1806SLuiz Capitulino ++s; 2213222f1806SLuiz Capitulino *s = tohex((i >> 4) & 0xf); 2214222f1806SLuiz Capitulino if (s != bak || *s != '0') 2215222f1806SLuiz Capitulino ++s; 22161da177e4SLinus Torvalds *s = tohex(i & 0xf); 22171da177e4SLinus Torvalds return s - bak + 1; 22181da177e4SLinus Torvalds } 22191da177e4SLinus Torvalds 2220222f1806SLuiz Capitulino static unsigned int fmt_ip6(char *s, const char ip[16]) 2221222f1806SLuiz Capitulino { 22221da177e4SLinus Torvalds unsigned int len; 22231da177e4SLinus Torvalds unsigned int i; 22241da177e4SLinus Torvalds unsigned int temp; 22251da177e4SLinus Torvalds unsigned int compressing; 22261da177e4SLinus Torvalds int j; 22271da177e4SLinus Torvalds 2228222f1806SLuiz Capitulino len = 0; 2229222f1806SLuiz Capitulino compressing = 0; 22301da177e4SLinus Torvalds for (j = 0; j < 16; j += 2) { 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds #ifdef V4MAPPEDPREFIX 22331da177e4SLinus Torvalds if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) { 22341da177e4SLinus Torvalds inet_ntoa_r(*(struct in_addr *)(ip + 12), s); 22351da177e4SLinus Torvalds temp = strlen(s); 22361da177e4SLinus Torvalds return len + temp; 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds #endif 22391da177e4SLinus Torvalds temp = ((unsigned long)(unsigned char)ip[j] << 8) + 22401da177e4SLinus Torvalds (unsigned long)(unsigned char)ip[j + 1]; 22411da177e4SLinus Torvalds if (temp == 0) { 22421da177e4SLinus Torvalds if (!compressing) { 22431da177e4SLinus Torvalds compressing = 1; 22441da177e4SLinus Torvalds if (j == 0) { 2245222f1806SLuiz Capitulino *s++ = ':'; 2246222f1806SLuiz Capitulino ++len; 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds } else { 22501da177e4SLinus Torvalds if (compressing) { 22511da177e4SLinus Torvalds compressing = 0; 2252222f1806SLuiz Capitulino *s++ = ':'; 2253222f1806SLuiz Capitulino ++len; 22541da177e4SLinus Torvalds } 2255222f1806SLuiz Capitulino i = fmt_xlong(s, temp); 2256222f1806SLuiz Capitulino len += i; 2257222f1806SLuiz Capitulino s += i; 22581da177e4SLinus Torvalds if (j < 14) { 22591da177e4SLinus Torvalds *s++ = ':'; 22601da177e4SLinus Torvalds ++len; 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds } 22631da177e4SLinus Torvalds } 22641da177e4SLinus Torvalds if (compressing) { 2265222f1806SLuiz Capitulino *s++ = ':'; 2266222f1806SLuiz Capitulino ++len; 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds *s = 0; 22691da177e4SLinus Torvalds return len; 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev, 22731da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 22741da177e4SLinus Torvalds { 22751da177e4SLinus Torvalds struct sk_buff *skb = NULL; 22761da177e4SLinus Torvalds __u8 *eth; 22771da177e4SLinus Torvalds struct udphdr *udph; 22781da177e4SLinus Torvalds int datalen; 22791da177e4SLinus Torvalds struct ipv6hdr *iph; 22801da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 22811da177e4SLinus Torvalds 228264053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 228364053beeSRobert Olsson * fields. 228464053beeSRobert Olsson */ 228564053beeSRobert Olsson mod_cur_headers(pkt_dev); 228664053beeSRobert Olsson 22871da177e4SLinus Torvalds skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC); 22881da177e4SLinus Torvalds if (!skb) { 22891da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 22901da177e4SLinus Torvalds return NULL; 22911da177e4SLinus Torvalds } 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds skb_reserve(skb, 16); 22941da177e4SLinus Torvalds 22951da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 22961da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 22971da177e4SLinus Torvalds iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr)); 22981da177e4SLinus Torvalds udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr)); 22991da177e4SLinus Torvalds 23001da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 23011da177e4SLinus Torvalds *(u16 *) & eth[12] = __constant_htons(ETH_P_IPV6); 23021da177e4SLinus Torvalds 2303222f1806SLuiz Capitulino datalen = pkt_dev->cur_pkt_size - 14 - sizeof(struct ipv6hdr) - sizeof(struct udphdr); /* Eth + IPh + UDPh */ 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) { 23061da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 23071da177e4SLinus Torvalds if (net_ratelimit()) 2308222f1806SLuiz Capitulino printk(KERN_INFO "pktgen: increased datalen to %d\n", 2309222f1806SLuiz Capitulino datalen); 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 23131da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 23141da177e4SLinus Torvalds udph->len = htons(datalen + sizeof(struct udphdr)); 23151da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds *(u32 *) iph = __constant_htonl(0x60000000); /* Version + flow */ 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds iph->hop_limit = 32; 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds iph->payload_len = htons(sizeof(struct udphdr) + datalen); 23221da177e4SLinus Torvalds iph->nexthdr = IPPROTO_UDP; 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr); 23251da177e4SLinus Torvalds ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr); 23261da177e4SLinus Torvalds 23271da177e4SLinus Torvalds skb->mac.raw = ((u8 *) iph) - 14; 23281da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_IPV6); 23291da177e4SLinus Torvalds skb->dev = odev; 23301da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 23331da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 23341da177e4SLinus Torvalds else { 23351da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 23361da177e4SLinus Torvalds int i; 23371da177e4SLinus Torvalds 23381da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 23411da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 23421da177e4SLinus Torvalds if (datalen > frags * PAGE_SIZE) { 23431da177e4SLinus Torvalds skb_put(skb, datalen - frags * PAGE_SIZE); 23441da177e4SLinus Torvalds datalen = frags * PAGE_SIZE; 23451da177e4SLinus Torvalds } 23461da177e4SLinus Torvalds 23471da177e4SLinus Torvalds i = 0; 23481da177e4SLinus Torvalds while (datalen > 0) { 23491da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 23501da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 23511da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 23521da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 23531da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 23541da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 23551da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 23561da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 23571da177e4SLinus Torvalds i++; 23581da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 23591da177e4SLinus Torvalds } 23601da177e4SLinus Torvalds 23611da177e4SLinus Torvalds while (i < frags) { 23621da177e4SLinus Torvalds int rem; 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds if (i == 0) 23651da177e4SLinus Torvalds break; 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 23681da177e4SLinus Torvalds if (rem == 0) 23691da177e4SLinus Torvalds break; 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 23721da177e4SLinus Torvalds 2373222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i] = 2374222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1]; 23751da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 2376222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page = 2377222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].page; 2378222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page_offset += 2379222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].size; 23801da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 23811da177e4SLinus Torvalds i++; 23821da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 23831da177e4SLinus Torvalds } 23841da177e4SLinus Torvalds } 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 23871da177e4SLinus Torvalds /* should we update cloned packets too ? */ 23881da177e4SLinus Torvalds if (pgh) { 23891da177e4SLinus Torvalds struct timeval timestamp; 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 23921da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 23931da177e4SLinus Torvalds 23941da177e4SLinus Torvalds do_gettimeofday(×tamp); 23951da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 23961da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds pkt_dev->seq_num++; 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds return skb; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds static inline struct sk_buff *fill_packet(struct net_device *odev, 24041da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 24051da177e4SLinus Torvalds { 24061da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) 24071da177e4SLinus Torvalds return fill_packet_ipv6(odev, pkt_dev); 24081da177e4SLinus Torvalds else 24091da177e4SLinus Torvalds return fill_packet_ipv4(odev, pkt_dev); 24101da177e4SLinus Torvalds } 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev) 24131da177e4SLinus Torvalds { 24141da177e4SLinus Torvalds pkt_dev->seq_num = 1; 24151da177e4SLinus Torvalds pkt_dev->idle_acc = 0; 24161da177e4SLinus Torvalds pkt_dev->sofar = 0; 24171da177e4SLinus Torvalds pkt_dev->tx_bytes = 0; 24181da177e4SLinus Torvalds pkt_dev->errors = 0; 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */ 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t) 24241da177e4SLinus Torvalds { 2425c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 24261da177e4SLinus Torvalds int started = 0; 24271da177e4SLinus Torvalds 24281da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t)); 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds if_lock(t); 2431c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds /* 24341da177e4SLinus Torvalds * setup odev and create initial packet. 24351da177e4SLinus Torvalds */ 24361da177e4SLinus Torvalds pktgen_setup_inject(pkt_dev); 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds if (pkt_dev->odev) { 24391da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 24401da177e4SLinus Torvalds pkt_dev->running = 1; /* Cranke yeself! */ 24411da177e4SLinus Torvalds pkt_dev->skb = NULL; 24421da177e4SLinus Torvalds pkt_dev->started_at = getCurUs(); 24431da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */ 24441da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds strcpy(pkt_dev->result, "Starting"); 24471da177e4SLinus Torvalds started++; 2448222f1806SLuiz Capitulino } else 24491da177e4SLinus Torvalds strcpy(pkt_dev->result, "Error starting"); 24501da177e4SLinus Torvalds } 24511da177e4SLinus Torvalds if_unlock(t); 2452222f1806SLuiz Capitulino if (started) 2453222f1806SLuiz Capitulino t->control &= ~(T_STOP); 24541da177e4SLinus Torvalds } 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void) 24571da177e4SLinus Torvalds { 2458cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 24591da177e4SLinus Torvalds 246095ed63f7SArthur Kepner PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads_ifs.\n")); 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds thread_lock(); 2463cdcdbe0bSLuiz Capitulino 2464cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 246595ed63f7SArthur Kepner t->control |= T_STOP; 2466cdcdbe0bSLuiz Capitulino 24671da177e4SLinus Torvalds thread_unlock(); 24681da177e4SLinus Torvalds } 24691da177e4SLinus Torvalds 24701da177e4SLinus Torvalds static int thread_is_running(struct pktgen_thread *t) 24711da177e4SLinus Torvalds { 2472c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 24731da177e4SLinus Torvalds int res = 0; 24741da177e4SLinus Torvalds 2475c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 2476c26a8016SLuiz Capitulino if (pkt_dev->running) { 24771da177e4SLinus Torvalds res = 1; 24781da177e4SLinus Torvalds break; 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds return res; 24811da177e4SLinus Torvalds } 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t) 24841da177e4SLinus Torvalds { 24851da177e4SLinus Torvalds if_lock(t); 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds while (thread_is_running(t)) { 24881da177e4SLinus Torvalds 24891da177e4SLinus Torvalds if_unlock(t); 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds msleep_interruptible(100); 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds if (signal_pending(current)) 24941da177e4SLinus Torvalds goto signal; 24951da177e4SLinus Torvalds if_lock(t); 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds if_unlock(t); 24981da177e4SLinus Torvalds return 1; 24991da177e4SLinus Torvalds signal: 25001da177e4SLinus Torvalds return 0; 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds static int pktgen_wait_all_threads_run(void) 25041da177e4SLinus Torvalds { 2505cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 25061da177e4SLinus Torvalds int sig = 1; 25071da177e4SLinus Torvalds 2508cdcdbe0bSLuiz Capitulino thread_lock(); 2509cdcdbe0bSLuiz Capitulino 2510cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) { 25111da177e4SLinus Torvalds sig = pktgen_wait_thread_run(t); 2512222f1806SLuiz Capitulino if (sig == 0) 2513222f1806SLuiz Capitulino break; 25141da177e4SLinus Torvalds } 2515cdcdbe0bSLuiz Capitulino 2516cdcdbe0bSLuiz Capitulino if (sig == 0) 2517cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 25181da177e4SLinus Torvalds t->control |= (T_STOP); 2519cdcdbe0bSLuiz Capitulino 25201da177e4SLinus Torvalds thread_unlock(); 25211da177e4SLinus Torvalds return sig; 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds static void pktgen_run_all_threads(void) 25251da177e4SLinus Torvalds { 2526cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n")); 25291da177e4SLinus Torvalds 25301da177e4SLinus Torvalds thread_lock(); 25311da177e4SLinus Torvalds 2532cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 25331da177e4SLinus Torvalds t->control |= (T_RUN); 2534cdcdbe0bSLuiz Capitulino 25351da177e4SLinus Torvalds thread_unlock(); 25361da177e4SLinus Torvalds 2537121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds pktgen_wait_all_threads_run(); 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags) 25431da177e4SLinus Torvalds { 25441da177e4SLinus Torvalds __u64 total_us, bps, mbps, pps, idle; 25451da177e4SLinus Torvalds char *p = pkt_dev->result; 25461da177e4SLinus Torvalds 25471da177e4SLinus Torvalds total_us = pkt_dev->stopped_at - pkt_dev->started_at; 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds idle = pkt_dev->idle_acc; 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n", 25521da177e4SLinus Torvalds (unsigned long long)total_us, 25531da177e4SLinus Torvalds (unsigned long long)(total_us - idle), 25541da177e4SLinus Torvalds (unsigned long long)idle, 25551da177e4SLinus Torvalds (unsigned long long)pkt_dev->sofar, 25561da177e4SLinus Torvalds pkt_dev->cur_pkt_size, nr_frags); 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds pps = pkt_dev->sofar * USEC_PER_SEC; 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds while ((total_us >> 32) != 0) { 25611da177e4SLinus Torvalds pps >>= 1; 25621da177e4SLinus Torvalds total_us >>= 1; 25631da177e4SLinus Torvalds } 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds do_div(pps, total_us); 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds bps = pps * 8 * pkt_dev->cur_pkt_size; 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds mbps = bps; 25701da177e4SLinus Torvalds do_div(mbps, 1000000); 25711da177e4SLinus Torvalds p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu", 25721da177e4SLinus Torvalds (unsigned long long)pps, 25731da177e4SLinus Torvalds (unsigned long long)mbps, 25741da177e4SLinus Torvalds (unsigned long long)bps, 25751da177e4SLinus Torvalds (unsigned long long)pkt_dev->errors); 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */ 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev) 25811da177e4SLinus Torvalds { 2582222f1806SLuiz Capitulino int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1; 25831da177e4SLinus Torvalds 25841da177e4SLinus Torvalds if (!pkt_dev->running) { 2585222f1806SLuiz Capitulino printk("pktgen: interface: %s is already stopped\n", 2586222f1806SLuiz Capitulino pkt_dev->ifname); 25871da177e4SLinus Torvalds return -EINVAL; 25881da177e4SLinus Torvalds } 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds pkt_dev->stopped_at = getCurUs(); 25911da177e4SLinus Torvalds pkt_dev->running = 0; 25921da177e4SLinus Torvalds 259395ed63f7SArthur Kepner show_results(pkt_dev, nr_frags); 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds return 0; 25961da177e4SLinus Torvalds } 25971da177e4SLinus Torvalds 25981da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t) 25991da177e4SLinus Torvalds { 2600c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev, *best = NULL; 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds if_lock(t); 26031da177e4SLinus Torvalds 2604c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 2605c26a8016SLuiz Capitulino if (!pkt_dev->running) 2606222f1806SLuiz Capitulino continue; 2607222f1806SLuiz Capitulino if (best == NULL) 2608c26a8016SLuiz Capitulino best = pkt_dev; 2609c26a8016SLuiz Capitulino else if (pkt_dev->next_tx_us < best->next_tx_us) 2610c26a8016SLuiz Capitulino best = pkt_dev; 26111da177e4SLinus Torvalds } 26121da177e4SLinus Torvalds if_unlock(t); 26131da177e4SLinus Torvalds return best; 26141da177e4SLinus Torvalds } 26151da177e4SLinus Torvalds 2616222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t) 2617222f1806SLuiz Capitulino { 2618c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 26191da177e4SLinus Torvalds 262095ed63f7SArthur Kepner PG_DEBUG(printk("pktgen: entering pktgen_stop\n")); 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds if_lock(t); 26231da177e4SLinus Torvalds 2624c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 2625c26a8016SLuiz Capitulino pktgen_stop_device(pkt_dev); 2626c26a8016SLuiz Capitulino if (pkt_dev->skb) 2627c26a8016SLuiz Capitulino kfree_skb(pkt_dev->skb); 262895ed63f7SArthur Kepner 2629c26a8016SLuiz Capitulino pkt_dev->skb = NULL; 263095ed63f7SArthur Kepner } 263195ed63f7SArthur Kepner 263295ed63f7SArthur Kepner if_unlock(t); 263395ed63f7SArthur Kepner } 263495ed63f7SArthur Kepner 263595ed63f7SArthur Kepner /* 263695ed63f7SArthur Kepner * one of our devices needs to be removed - find it 263795ed63f7SArthur Kepner * and remove it 263895ed63f7SArthur Kepner */ 263995ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t) 264095ed63f7SArthur Kepner { 2641c26a8016SLuiz Capitulino struct list_head *q, *n; 2642c26a8016SLuiz Capitulino struct pktgen_dev *cur; 264395ed63f7SArthur Kepner 264495ed63f7SArthur Kepner PG_DEBUG(printk("pktgen: entering pktgen_rem_one_if\n")); 264595ed63f7SArthur Kepner 264695ed63f7SArthur Kepner if_lock(t); 264795ed63f7SArthur Kepner 2648c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 2649c26a8016SLuiz Capitulino cur = list_entry(q, struct pktgen_dev, list); 265095ed63f7SArthur Kepner 2651222f1806SLuiz Capitulino if (!cur->removal_mark) 2652222f1806SLuiz Capitulino continue; 265395ed63f7SArthur Kepner 265495ed63f7SArthur Kepner if (cur->skb) 265595ed63f7SArthur Kepner kfree_skb(cur->skb); 265695ed63f7SArthur Kepner cur->skb = NULL; 265795ed63f7SArthur Kepner 265895ed63f7SArthur Kepner pktgen_remove_device(t, cur); 265995ed63f7SArthur Kepner 266095ed63f7SArthur Kepner break; 266195ed63f7SArthur Kepner } 26621da177e4SLinus Torvalds 26631da177e4SLinus Torvalds if_unlock(t); 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t) 26671da177e4SLinus Torvalds { 2668c26a8016SLuiz Capitulino struct list_head *q, *n; 2669c26a8016SLuiz Capitulino struct pktgen_dev *cur; 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds /* Remove all devices, free mem */ 26721da177e4SLinus Torvalds 267395ed63f7SArthur Kepner PG_DEBUG(printk("pktgen: entering pktgen_rem_all_ifs\n")); 26741da177e4SLinus Torvalds if_lock(t); 26751da177e4SLinus Torvalds 2676c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 2677c26a8016SLuiz Capitulino cur = list_entry(q, struct pktgen_dev, list); 267895ed63f7SArthur Kepner 267995ed63f7SArthur Kepner if (cur->skb) 268095ed63f7SArthur Kepner kfree_skb(cur->skb); 268195ed63f7SArthur Kepner cur->skb = NULL; 268295ed63f7SArthur Kepner 26831da177e4SLinus Torvalds pktgen_remove_device(t, cur); 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds if_unlock(t); 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t) 26901da177e4SLinus Torvalds { 26911da177e4SLinus Torvalds /* Remove from the thread list */ 26921da177e4SLinus Torvalds 2693d50a6b56SStephen Hemminger remove_proc_entry(t->name, pg_proc_dir); 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds thread_lock(); 26961da177e4SLinus Torvalds 2697cdcdbe0bSLuiz Capitulino list_del(&t->th_list); 2698cdcdbe0bSLuiz Capitulino 26991da177e4SLinus Torvalds thread_unlock(); 27001da177e4SLinus Torvalds } 27011da177e4SLinus Torvalds 27021da177e4SLinus Torvalds static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev) 27031da177e4SLinus Torvalds { 27041da177e4SLinus Torvalds struct net_device *odev = NULL; 27051da177e4SLinus Torvalds __u64 idle_start = 0; 27061da177e4SLinus Torvalds int ret; 27071da177e4SLinus Torvalds 27081da177e4SLinus Torvalds odev = pkt_dev->odev; 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds if (pkt_dev->delay_us || pkt_dev->delay_ns) { 27111da177e4SLinus Torvalds u64 now; 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds now = getCurUs(); 27141da177e4SLinus Torvalds if (now < pkt_dev->next_tx_us) 27151da177e4SLinus Torvalds spin(pkt_dev, pkt_dev->next_tx_us); 27161da177e4SLinus Torvalds 27171da177e4SLinus Torvalds /* This is max DELAY, this has special meaning of 27181da177e4SLinus Torvalds * "never transmit" 27191da177e4SLinus Torvalds */ 27201da177e4SLinus Torvalds if (pkt_dev->delay_us == 0x7FFFFFFF) { 27211da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us; 27221da177e4SLinus Torvalds pkt_dev->next_tx_ns = pkt_dev->delay_ns; 27231da177e4SLinus Torvalds goto out; 27241da177e4SLinus Torvalds } 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds 27271da177e4SLinus Torvalds if (netif_queue_stopped(odev) || need_resched()) { 27281da177e4SLinus Torvalds idle_start = getCurUs(); 27291da177e4SLinus Torvalds 27301da177e4SLinus Torvalds if (!netif_running(odev)) { 27311da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 273295ed63f7SArthur Kepner if (pkt_dev->skb) 273395ed63f7SArthur Kepner kfree_skb(pkt_dev->skb); 273495ed63f7SArthur Kepner pkt_dev->skb = NULL; 27351da177e4SLinus Torvalds goto out; 27361da177e4SLinus Torvalds } 27371da177e4SLinus Torvalds if (need_resched()) 27381da177e4SLinus Torvalds schedule(); 27391da177e4SLinus Torvalds 27401da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 27411da177e4SLinus Torvalds 27421da177e4SLinus Torvalds if (netif_queue_stopped(odev)) { 27431da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 27441da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 27451da177e4SLinus Torvalds goto out; /* Try the next interface */ 27461da177e4SLinus Torvalds } 27471da177e4SLinus Torvalds } 27481da177e4SLinus Torvalds 27491da177e4SLinus Torvalds if (pkt_dev->last_ok || !pkt_dev->skb) { 2750222f1806SLuiz Capitulino if ((++pkt_dev->clone_count >= pkt_dev->clone_skb) 2751222f1806SLuiz Capitulino || (!pkt_dev->skb)) { 27521da177e4SLinus Torvalds /* build a new pkt */ 27531da177e4SLinus Torvalds if (pkt_dev->skb) 27541da177e4SLinus Torvalds kfree_skb(pkt_dev->skb); 27551da177e4SLinus Torvalds 27561da177e4SLinus Torvalds pkt_dev->skb = fill_packet(odev, pkt_dev); 27571da177e4SLinus Torvalds if (pkt_dev->skb == NULL) { 27581da177e4SLinus Torvalds printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n"); 27591da177e4SLinus Torvalds schedule(); 27601da177e4SLinus Torvalds pkt_dev->clone_count--; /* back out increment, OOM */ 27611da177e4SLinus Torvalds goto out; 27621da177e4SLinus Torvalds } 27631da177e4SLinus Torvalds pkt_dev->allocated_skbs++; 27641da177e4SLinus Torvalds pkt_dev->clone_count = 0; /* reset counter */ 27651da177e4SLinus Torvalds } 27661da177e4SLinus Torvalds } 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds spin_lock_bh(&odev->xmit_lock); 27691da177e4SLinus Torvalds if (!netif_queue_stopped(odev)) { 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds atomic_inc(&(pkt_dev->skb->users)); 27721da177e4SLinus Torvalds retry_now: 27731da177e4SLinus Torvalds ret = odev->hard_start_xmit(pkt_dev->skb, odev); 27741da177e4SLinus Torvalds if (likely(ret == NETDEV_TX_OK)) { 27751da177e4SLinus Torvalds pkt_dev->last_ok = 1; 27761da177e4SLinus Torvalds pkt_dev->sofar++; 27771da177e4SLinus Torvalds pkt_dev->seq_num++; 27781da177e4SLinus Torvalds pkt_dev->tx_bytes += pkt_dev->cur_pkt_size; 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds } else if (ret == NETDEV_TX_LOCKED 27811da177e4SLinus Torvalds && (odev->features & NETIF_F_LLTX)) { 27821da177e4SLinus Torvalds cpu_relax(); 27831da177e4SLinus Torvalds goto retry_now; 27841da177e4SLinus Torvalds } else { /* Retry it next time */ 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds atomic_dec(&(pkt_dev->skb->users)); 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds if (debug && net_ratelimit()) 27891da177e4SLinus Torvalds printk(KERN_INFO "pktgen: Hard xmit error\n"); 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds pkt_dev->errors++; 27921da177e4SLinus Torvalds pkt_dev->last_ok = 0; 27931da177e4SLinus Torvalds } 27941da177e4SLinus Torvalds 27951da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); 27961da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 27971da177e4SLinus Torvalds 27981da177e4SLinus Torvalds pkt_dev->next_tx_us += pkt_dev->delay_us; 27991da177e4SLinus Torvalds pkt_dev->next_tx_ns += pkt_dev->delay_ns; 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds if (pkt_dev->next_tx_ns > 1000) { 28021da177e4SLinus Torvalds pkt_dev->next_tx_us++; 28031da177e4SLinus Torvalds pkt_dev->next_tx_ns -= 1000; 28041da177e4SLinus Torvalds } 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds else { /* Retry it next time */ 28081da177e4SLinus Torvalds pkt_dev->last_ok = 0; 28091da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 28101da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 28111da177e4SLinus Torvalds } 28121da177e4SLinus Torvalds 28131da177e4SLinus Torvalds spin_unlock_bh(&odev->xmit_lock); 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds /* If pkt_dev->count is zero, then run forever */ 28161da177e4SLinus Torvalds if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) { 28171da177e4SLinus Torvalds if (atomic_read(&(pkt_dev->skb->users)) != 1) { 28181da177e4SLinus Torvalds idle_start = getCurUs(); 28191da177e4SLinus Torvalds while (atomic_read(&(pkt_dev->skb->users)) != 1) { 28201da177e4SLinus Torvalds if (signal_pending(current)) { 28211da177e4SLinus Torvalds break; 28221da177e4SLinus Torvalds } 28231da177e4SLinus Torvalds schedule(); 28241da177e4SLinus Torvalds } 28251da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 28261da177e4SLinus Torvalds } 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds /* Done with this */ 28291da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 283095ed63f7SArthur Kepner if (pkt_dev->skb) 283195ed63f7SArthur Kepner kfree_skb(pkt_dev->skb); 283295ed63f7SArthur Kepner pkt_dev->skb = NULL; 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds out:; 28351da177e4SLinus Torvalds } 28361da177e4SLinus Torvalds 28371da177e4SLinus Torvalds /* 28381da177e4SLinus Torvalds * Main loop of the thread goes here 28391da177e4SLinus Torvalds */ 28401da177e4SLinus Torvalds 28411da177e4SLinus Torvalds static void pktgen_thread_worker(struct pktgen_thread *t) 28421da177e4SLinus Torvalds { 28431da177e4SLinus Torvalds DEFINE_WAIT(wait); 28441da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 28451da177e4SLinus Torvalds int cpu = t->cpu; 28461da177e4SLinus Torvalds sigset_t tmpsig; 28471da177e4SLinus Torvalds u32 max_before_softirq; 28481da177e4SLinus Torvalds u32 tx_since_softirq = 0; 28491da177e4SLinus Torvalds 28501da177e4SLinus Torvalds daemonize("pktgen/%d", cpu); 28511da177e4SLinus Torvalds 28521da177e4SLinus Torvalds /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */ 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 28551da177e4SLinus Torvalds tmpsig = current->blocked; 28561da177e4SLinus Torvalds siginitsetinv(¤t->blocked, 2857222f1806SLuiz Capitulino sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGTERM)); 28581da177e4SLinus Torvalds 28591da177e4SLinus Torvalds recalc_sigpending(); 28601da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 28611da177e4SLinus Torvalds 28621da177e4SLinus Torvalds /* Migrate to the right CPU */ 28631da177e4SLinus Torvalds set_cpus_allowed(current, cpumask_of_cpu(cpu)); 28641da177e4SLinus Torvalds if (smp_processor_id() != cpu) 28651da177e4SLinus Torvalds BUG(); 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds init_waitqueue_head(&t->queue); 28681da177e4SLinus Torvalds 28691da177e4SLinus Torvalds t->control &= ~(T_TERMINATE); 28701da177e4SLinus Torvalds t->control &= ~(T_RUN); 28711da177e4SLinus Torvalds t->control &= ~(T_STOP); 287295ed63f7SArthur Kepner t->control &= ~(T_REMDEVALL); 28731da177e4SLinus Torvalds t->control &= ~(T_REMDEV); 28741da177e4SLinus Torvalds 28751da177e4SLinus Torvalds t->pid = current->pid; 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid)); 28781da177e4SLinus Torvalds 28791da177e4SLinus Torvalds max_before_softirq = t->max_before_softirq; 28801da177e4SLinus Torvalds 28811da177e4SLinus Torvalds __set_current_state(TASK_INTERRUPTIBLE); 28821da177e4SLinus Torvalds mb(); 28831da177e4SLinus Torvalds 28841da177e4SLinus Torvalds while (1) { 28851da177e4SLinus Torvalds 28861da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 28871da177e4SLinus Torvalds 28881da177e4SLinus Torvalds /* 28891da177e4SLinus Torvalds * Get next dev to xmit -- if any. 28901da177e4SLinus Torvalds */ 28911da177e4SLinus Torvalds 28921da177e4SLinus Torvalds pkt_dev = next_to_run(t); 28931da177e4SLinus Torvalds 28941da177e4SLinus Torvalds if (pkt_dev) { 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds pktgen_xmit(pkt_dev); 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds /* 28991da177e4SLinus Torvalds * We like to stay RUNNING but must also give 29001da177e4SLinus Torvalds * others fair share. 29011da177e4SLinus Torvalds */ 29021da177e4SLinus Torvalds 29031da177e4SLinus Torvalds tx_since_softirq += pkt_dev->last_ok; 29041da177e4SLinus Torvalds 29051da177e4SLinus Torvalds if (tx_since_softirq > max_before_softirq) { 29061da177e4SLinus Torvalds if (local_softirq_pending()) 29071da177e4SLinus Torvalds do_softirq(); 29081da177e4SLinus Torvalds tx_since_softirq = 0; 29091da177e4SLinus Torvalds } 29101da177e4SLinus Torvalds } else { 29111da177e4SLinus Torvalds prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE); 29121da177e4SLinus Torvalds schedule_timeout(HZ / 10); 29131da177e4SLinus Torvalds finish_wait(&(t->queue), &wait); 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds /* 29171da177e4SLinus Torvalds * Back from sleep, either due to the timeout or signal. 29181da177e4SLinus Torvalds * We check if we have any "posted" work for us. 29191da177e4SLinus Torvalds */ 29201da177e4SLinus Torvalds 29211da177e4SLinus Torvalds if (t->control & T_TERMINATE || signal_pending(current)) 29221da177e4SLinus Torvalds /* we received a request to terminate ourself */ 29231da177e4SLinus Torvalds break; 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds if (t->control & T_STOP) { 29261da177e4SLinus Torvalds pktgen_stop(t); 29271da177e4SLinus Torvalds t->control &= ~(T_STOP); 29281da177e4SLinus Torvalds } 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds if (t->control & T_RUN) { 29311da177e4SLinus Torvalds pktgen_run(t); 29321da177e4SLinus Torvalds t->control &= ~(T_RUN); 29331da177e4SLinus Torvalds } 29341da177e4SLinus Torvalds 293595ed63f7SArthur Kepner if (t->control & T_REMDEVALL) { 29361da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 293795ed63f7SArthur Kepner t->control &= ~(T_REMDEVALL); 293895ed63f7SArthur Kepner } 293995ed63f7SArthur Kepner 294095ed63f7SArthur Kepner if (t->control & T_REMDEV) { 294195ed63f7SArthur Kepner pktgen_rem_one_if(t); 29421da177e4SLinus Torvalds t->control &= ~(T_REMDEV); 29431da177e4SLinus Torvalds } 29441da177e4SLinus Torvalds 29451da177e4SLinus Torvalds if (need_resched()) 29461da177e4SLinus Torvalds schedule(); 29471da177e4SLinus Torvalds } 29481da177e4SLinus Torvalds 29491da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name)); 29501da177e4SLinus Torvalds pktgen_stop(t); 29511da177e4SLinus Torvalds 29521da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: %s removing all device\n", t->name)); 29531da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 29541da177e4SLinus Torvalds 29551da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name)); 29561da177e4SLinus Torvalds pktgen_rem_thread(t); 2957cdcdbe0bSLuiz Capitulino 2958cdcdbe0bSLuiz Capitulino t->removed = 1; 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds 2961222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, 2962222f1806SLuiz Capitulino const char *ifname) 29631da177e4SLinus Torvalds { 2964c26a8016SLuiz Capitulino struct pktgen_dev *p, *pkt_dev = NULL; 29651da177e4SLinus Torvalds if_lock(t); 29661da177e4SLinus Torvalds 2967c26a8016SLuiz Capitulino list_for_each_entry(p, &t->if_list, list) 2968c26a8016SLuiz Capitulino if (strncmp(p->ifname, ifname, IFNAMSIZ) == 0) { 2969c26a8016SLuiz Capitulino pkt_dev = p; 29701da177e4SLinus Torvalds break; 29711da177e4SLinus Torvalds } 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds if_unlock(t); 29741da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev)); 29751da177e4SLinus Torvalds return pkt_dev; 29761da177e4SLinus Torvalds } 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds /* 29791da177e4SLinus Torvalds * Adds a dev at front of if_list. 29801da177e4SLinus Torvalds */ 29811da177e4SLinus Torvalds 2982222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t, 2983222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 29841da177e4SLinus Torvalds { 29851da177e4SLinus Torvalds int rv = 0; 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds if_lock(t); 29881da177e4SLinus Torvalds 29891da177e4SLinus Torvalds if (pkt_dev->pg_thread) { 29901da177e4SLinus Torvalds printk("pktgen: ERROR: already assigned to a thread.\n"); 29911da177e4SLinus Torvalds rv = -EBUSY; 29921da177e4SLinus Torvalds goto out; 29931da177e4SLinus Torvalds } 2994c26a8016SLuiz Capitulino 2995c26a8016SLuiz Capitulino list_add(&pkt_dev->list, &t->if_list); 29961da177e4SLinus Torvalds pkt_dev->pg_thread = t; 29971da177e4SLinus Torvalds pkt_dev->running = 0; 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds out: 30001da177e4SLinus Torvalds if_unlock(t); 30011da177e4SLinus Torvalds return rv; 30021da177e4SLinus Torvalds } 30031da177e4SLinus Torvalds 30041da177e4SLinus Torvalds /* Called under thread lock */ 30051da177e4SLinus Torvalds 30061da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname) 30071da177e4SLinus Torvalds { 30081da177e4SLinus Torvalds struct pktgen_dev *pkt_dev; 3009d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds /* We don't allow a device to be on several threads */ 30121da177e4SLinus Torvalds 3013d50a6b56SStephen Hemminger pkt_dev = __pktgen_NN_threads(ifname, FIND); 3014d50a6b56SStephen Hemminger if (pkt_dev) { 3015d50a6b56SStephen Hemminger printk("pktgen: ERROR: interface already used.\n"); 3016d50a6b56SStephen Hemminger return -EBUSY; 3017d50a6b56SStephen Hemminger } 30181da177e4SLinus Torvalds 30192845b63bSStephen Hemminger pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL); 30201da177e4SLinus Torvalds if (!pkt_dev) 30211da177e4SLinus Torvalds return -ENOMEM; 30221da177e4SLinus Torvalds 30231da177e4SLinus Torvalds pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state)); 30241da177e4SLinus Torvalds if (pkt_dev->flows == NULL) { 30251da177e4SLinus Torvalds kfree(pkt_dev); 30261da177e4SLinus Torvalds return -ENOMEM; 30271da177e4SLinus Torvalds } 30281da177e4SLinus Torvalds memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state)); 30291da177e4SLinus Torvalds 303095ed63f7SArthur Kepner pkt_dev->removal_mark = 0; 30311da177e4SLinus Torvalds pkt_dev->min_pkt_size = ETH_ZLEN; 30321da177e4SLinus Torvalds pkt_dev->max_pkt_size = ETH_ZLEN; 30331da177e4SLinus Torvalds pkt_dev->nfrags = 0; 30341da177e4SLinus Torvalds pkt_dev->clone_skb = pg_clone_skb_d; 30351da177e4SLinus Torvalds pkt_dev->delay_us = pg_delay_d / 1000; 30361da177e4SLinus Torvalds pkt_dev->delay_ns = pg_delay_d % 1000; 30371da177e4SLinus Torvalds pkt_dev->count = pg_count_d; 30381da177e4SLinus Torvalds pkt_dev->sofar = 0; 30391da177e4SLinus Torvalds pkt_dev->udp_src_min = 9; /* sink port */ 30401da177e4SLinus Torvalds pkt_dev->udp_src_max = 9; 30411da177e4SLinus Torvalds pkt_dev->udp_dst_min = 9; 30421da177e4SLinus Torvalds pkt_dev->udp_dst_max = 9; 30431da177e4SLinus Torvalds 3044d50a6b56SStephen Hemminger strncpy(pkt_dev->ifname, ifname, IFNAMSIZ); 30451da177e4SLinus Torvalds 30461da177e4SLinus Torvalds if (!pktgen_setup_dev(pkt_dev)) { 30471da177e4SLinus Torvalds printk("pktgen: ERROR: pktgen_setup_dev failed.\n"); 30481da177e4SLinus Torvalds if (pkt_dev->flows) 30491da177e4SLinus Torvalds vfree(pkt_dev->flows); 30501da177e4SLinus Torvalds kfree(pkt_dev); 30511da177e4SLinus Torvalds return -ENODEV; 30521da177e4SLinus Torvalds } 30531da177e4SLinus Torvalds 3054d50a6b56SStephen Hemminger pe = create_proc_entry(ifname, 0600, pg_proc_dir); 3055d50a6b56SStephen Hemminger if (!pe) { 3056d50a6b56SStephen Hemminger printk("pktgen: cannot create %s/%s procfs entry.\n", 3057d50a6b56SStephen Hemminger PG_PROC_DIR, ifname); 30581da177e4SLinus Torvalds if (pkt_dev->flows) 30591da177e4SLinus Torvalds vfree(pkt_dev->flows); 30601da177e4SLinus Torvalds kfree(pkt_dev); 30611da177e4SLinus Torvalds return -EINVAL; 30621da177e4SLinus Torvalds } 3063d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_if_fops; 3064d50a6b56SStephen Hemminger pe->data = pkt_dev; 30651da177e4SLinus Torvalds 30661da177e4SLinus Torvalds return add_dev_to_thread(t, pkt_dev); 30671da177e4SLinus Torvalds } 30681da177e4SLinus Torvalds 306969549dddSLuiz Capitulino static struct pktgen_thread *__init pktgen_find_thread(const char *name) 30701da177e4SLinus Torvalds { 3071cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 30721da177e4SLinus Torvalds 30731da177e4SLinus Torvalds thread_lock(); 30741da177e4SLinus Torvalds 3075cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 3076cdcdbe0bSLuiz Capitulino if (strcmp(t->name, name) == 0) { 30771da177e4SLinus Torvalds thread_unlock(); 30781da177e4SLinus Torvalds return t; 30791da177e4SLinus Torvalds } 30801da177e4SLinus Torvalds 3081cdcdbe0bSLuiz Capitulino thread_unlock(); 3082cdcdbe0bSLuiz Capitulino return NULL; 3083cdcdbe0bSLuiz Capitulino } 3084cdcdbe0bSLuiz Capitulino 308569549dddSLuiz Capitulino static int __init pktgen_create_thread(const char *name, int cpu) 30861da177e4SLinus Torvalds { 308712e18723SLuiz Capitulino int err; 30881da177e4SLinus Torvalds struct pktgen_thread *t = NULL; 3089d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 30901da177e4SLinus Torvalds 30911da177e4SLinus Torvalds if (strlen(name) > 31) { 30921da177e4SLinus Torvalds printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n"); 30931da177e4SLinus Torvalds return -EINVAL; 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds 30961da177e4SLinus Torvalds if (pktgen_find_thread(name)) { 30971da177e4SLinus Torvalds printk("pktgen: ERROR: thread: %s already exists\n", name); 30981da177e4SLinus Torvalds return -EINVAL; 30991da177e4SLinus Torvalds } 31001da177e4SLinus Torvalds 31012845b63bSStephen Hemminger t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL); 31021da177e4SLinus Torvalds if (!t) { 31031da177e4SLinus Torvalds printk("pktgen: ERROR: out of memory, can't create new thread.\n"); 31041da177e4SLinus Torvalds return -ENOMEM; 31051da177e4SLinus Torvalds } 31061da177e4SLinus Torvalds 31071da177e4SLinus Torvalds strcpy(t->name, name); 31081da177e4SLinus Torvalds spin_lock_init(&t->if_lock); 31091da177e4SLinus Torvalds t->cpu = cpu; 31101da177e4SLinus Torvalds 3111d50a6b56SStephen Hemminger pe = create_proc_entry(t->name, 0600, pg_proc_dir); 3112d50a6b56SStephen Hemminger if (!pe) { 3113d50a6b56SStephen Hemminger printk("pktgen: cannot create %s/%s procfs entry.\n", 3114d50a6b56SStephen Hemminger PG_PROC_DIR, t->name); 31151da177e4SLinus Torvalds kfree(t); 31161da177e4SLinus Torvalds return -EINVAL; 31171da177e4SLinus Torvalds } 3118d50a6b56SStephen Hemminger 3119d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_thread_fops; 3120d50a6b56SStephen Hemminger pe->data = t; 31211da177e4SLinus Torvalds 3122c26a8016SLuiz Capitulino INIT_LIST_HEAD(&t->if_list); 3123c26a8016SLuiz Capitulino 3124cdcdbe0bSLuiz Capitulino list_add_tail(&t->th_list, &pktgen_threads); 3125cdcdbe0bSLuiz Capitulino 3126cdcdbe0bSLuiz Capitulino t->removed = 0; 31271da177e4SLinus Torvalds 312812e18723SLuiz Capitulino err = kernel_thread((void *)pktgen_thread_worker, (void *)t, 312912e18723SLuiz Capitulino CLONE_FS | CLONE_FILES | CLONE_SIGHAND); 313012e18723SLuiz Capitulino if (err < 0) { 31311da177e4SLinus Torvalds printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu); 313212e18723SLuiz Capitulino remove_proc_entry(t->name, pg_proc_dir); 313312e18723SLuiz Capitulino list_del(&t->th_list); 313412e18723SLuiz Capitulino kfree(t); 313512e18723SLuiz Capitulino return err; 313612e18723SLuiz Capitulino } 31371da177e4SLinus Torvalds 31381da177e4SLinus Torvalds return 0; 31391da177e4SLinus Torvalds } 31401da177e4SLinus Torvalds 31411da177e4SLinus Torvalds /* 31421da177e4SLinus Torvalds * Removes a device from the thread if_list. 31431da177e4SLinus Torvalds */ 3144222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t, 3145222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 31461da177e4SLinus Torvalds { 3147c26a8016SLuiz Capitulino struct list_head *q, *n; 3148c26a8016SLuiz Capitulino struct pktgen_dev *p; 31491da177e4SLinus Torvalds 3150c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3151c26a8016SLuiz Capitulino p = list_entry(q, struct pktgen_dev, list); 3152c26a8016SLuiz Capitulino if (p == pkt_dev) 3153c26a8016SLuiz Capitulino list_del(&p->list); 31541da177e4SLinus Torvalds } 31551da177e4SLinus Torvalds } 31561da177e4SLinus Torvalds 3157222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t, 3158222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 31591da177e4SLinus Torvalds { 31601da177e4SLinus Torvalds 31611da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev)); 31621da177e4SLinus Torvalds 31631da177e4SLinus Torvalds if (pkt_dev->running) { 31641da177e4SLinus Torvalds printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n"); 31651da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 31661da177e4SLinus Torvalds } 31671da177e4SLinus Torvalds 31681da177e4SLinus Torvalds /* Dis-associate from the interface */ 31691da177e4SLinus Torvalds 31701da177e4SLinus Torvalds if (pkt_dev->odev) { 31711da177e4SLinus Torvalds dev_put(pkt_dev->odev); 31721da177e4SLinus Torvalds pkt_dev->odev = NULL; 31731da177e4SLinus Torvalds } 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds /* And update the thread if_list */ 31761da177e4SLinus Torvalds 31771da177e4SLinus Torvalds _rem_dev_from_if_list(t, pkt_dev); 31781da177e4SLinus Torvalds 31791da177e4SLinus Torvalds /* Clean up proc file system */ 31801da177e4SLinus Torvalds 3181d50a6b56SStephen Hemminger remove_proc_entry(pkt_dev->ifname, pg_proc_dir); 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds if (pkt_dev->flows) 31841da177e4SLinus Torvalds vfree(pkt_dev->flows); 31851da177e4SLinus Torvalds kfree(pkt_dev); 31861da177e4SLinus Torvalds return 0; 31871da177e4SLinus Torvalds } 31881da177e4SLinus Torvalds 31891da177e4SLinus Torvalds static int __init pg_init(void) 31901da177e4SLinus Torvalds { 31911da177e4SLinus Torvalds int cpu; 3192d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 3193d50a6b56SStephen Hemminger 31941da177e4SLinus Torvalds printk(version); 31951da177e4SLinus Torvalds 3196d50a6b56SStephen Hemminger pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net); 3197d50a6b56SStephen Hemminger if (!pg_proc_dir) 3198d50a6b56SStephen Hemminger return -ENODEV; 3199d50a6b56SStephen Hemminger pg_proc_dir->owner = THIS_MODULE; 32001da177e4SLinus Torvalds 3201d50a6b56SStephen Hemminger pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir); 3202d50a6b56SStephen Hemminger if (pe == NULL) { 3203222f1806SLuiz Capitulino printk("pktgen: ERROR: cannot create %s procfs entry.\n", 3204222f1806SLuiz Capitulino PGCTRL); 3205d50a6b56SStephen Hemminger proc_net_remove(PG_PROC_DIR); 32061da177e4SLinus Torvalds return -EINVAL; 32071da177e4SLinus Torvalds } 32081da177e4SLinus Torvalds 3209d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_fops; 3210d50a6b56SStephen Hemminger pe->data = NULL; 32111da177e4SLinus Torvalds 32121da177e4SLinus Torvalds /* Register us to receive netdevice events */ 32131da177e4SLinus Torvalds register_netdevice_notifier(&pktgen_notifier_block); 32141da177e4SLinus Torvalds 3215670c02c2SJohn Hawkes for_each_online_cpu(cpu) { 32168024bb24SLuiz Capitulino int err; 32171da177e4SLinus Torvalds char buf[30]; 32181da177e4SLinus Torvalds 32191da177e4SLinus Torvalds sprintf(buf, "kpktgend_%i", cpu); 32208024bb24SLuiz Capitulino err = pktgen_create_thread(buf, cpu); 32218024bb24SLuiz Capitulino if (err) 32228024bb24SLuiz Capitulino printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n", 32238024bb24SLuiz Capitulino cpu, err); 32241da177e4SLinus Torvalds } 32258024bb24SLuiz Capitulino 32268024bb24SLuiz Capitulino if (list_empty(&pktgen_threads)) { 32278024bb24SLuiz Capitulino printk("pktgen: ERROR: Initialization failed for all threads\n"); 32288024bb24SLuiz Capitulino unregister_netdevice_notifier(&pktgen_notifier_block); 32298024bb24SLuiz Capitulino remove_proc_entry(PGCTRL, pg_proc_dir); 32308024bb24SLuiz Capitulino proc_net_remove(PG_PROC_DIR); 32318024bb24SLuiz Capitulino return -ENODEV; 32328024bb24SLuiz Capitulino } 32338024bb24SLuiz Capitulino 32341da177e4SLinus Torvalds return 0; 32351da177e4SLinus Torvalds } 32361da177e4SLinus Torvalds 32371da177e4SLinus Torvalds static void __exit pg_cleanup(void) 32381da177e4SLinus Torvalds { 3239cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 3240cdcdbe0bSLuiz Capitulino struct list_head *q, *n; 32411da177e4SLinus Torvalds wait_queue_head_t queue; 32421da177e4SLinus Torvalds init_waitqueue_head(&queue); 32431da177e4SLinus Torvalds 32441da177e4SLinus Torvalds /* Stop all interfaces & threads */ 32451da177e4SLinus Torvalds 3246cdcdbe0bSLuiz Capitulino list_for_each_safe(q, n, &pktgen_threads) { 3247cdcdbe0bSLuiz Capitulino t = list_entry(q, struct pktgen_thread, th_list); 3248cdcdbe0bSLuiz Capitulino t->control |= (T_TERMINATE); 32491da177e4SLinus Torvalds 3250cdcdbe0bSLuiz Capitulino wait_event_interruptible_timeout(queue, (t->removed == 1), HZ); 32511da177e4SLinus Torvalds } 32521da177e4SLinus Torvalds 32531da177e4SLinus Torvalds /* Un-register us from receiving netdevice events */ 32541da177e4SLinus Torvalds unregister_netdevice_notifier(&pktgen_notifier_block); 32551da177e4SLinus Torvalds 32561da177e4SLinus Torvalds /* Clean up proc file system */ 3257d50a6b56SStephen Hemminger remove_proc_entry(PGCTRL, pg_proc_dir); 3258d50a6b56SStephen Hemminger proc_net_remove(PG_PROC_DIR); 32591da177e4SLinus Torvalds } 32601da177e4SLinus Torvalds 32611da177e4SLinus Torvalds module_init(pg_init); 32621da177e4SLinus Torvalds module_exit(pg_cleanup); 32631da177e4SLinus Torvalds 32641da177e4SLinus Torvalds MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se"); 32651da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool"); 32661da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 32671da177e4SLinus Torvalds module_param(pg_count_d, int, 0); 32681da177e4SLinus Torvalds module_param(pg_delay_d, int, 0); 32691da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0); 32701da177e4SLinus Torvalds module_param(debug, int, 0); 3271