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> 1161da177e4SLinus Torvalds #include <linux/sched.h> 1171da177e4SLinus Torvalds #include <linux/slab.h> 1181da177e4SLinus Torvalds #include <linux/vmalloc.h> 1191da177e4SLinus Torvalds #include <linux/unistd.h> 1201da177e4SLinus Torvalds #include <linux/string.h> 1211da177e4SLinus Torvalds #include <linux/ptrace.h> 1221da177e4SLinus Torvalds #include <linux/errno.h> 1231da177e4SLinus Torvalds #include <linux/ioport.h> 1241da177e4SLinus Torvalds #include <linux/interrupt.h> 1254fc268d2SRandy Dunlap #include <linux/capability.h> 1261da177e4SLinus Torvalds #include <linux/delay.h> 1271da177e4SLinus Torvalds #include <linux/timer.h> 1281da177e4SLinus Torvalds #include <linux/init.h> 1291da177e4SLinus Torvalds #include <linux/skbuff.h> 1301da177e4SLinus Torvalds #include <linux/netdevice.h> 1311da177e4SLinus Torvalds #include <linux/inet.h> 1321da177e4SLinus Torvalds #include <linux/inetdevice.h> 1331da177e4SLinus Torvalds #include <linux/rtnetlink.h> 1341da177e4SLinus Torvalds #include <linux/if_arp.h> 1351da177e4SLinus Torvalds #include <linux/in.h> 1361da177e4SLinus Torvalds #include <linux/ip.h> 1371da177e4SLinus Torvalds #include <linux/ipv6.h> 1381da177e4SLinus Torvalds #include <linux/udp.h> 1391da177e4SLinus Torvalds #include <linux/proc_fs.h> 140d50a6b56SStephen Hemminger #include <linux/seq_file.h> 1411da177e4SLinus Torvalds #include <linux/wait.h> 142f404e9a6SKris Katterjohn #include <linux/etherdevice.h> 1431da177e4SLinus Torvalds #include <net/checksum.h> 1441da177e4SLinus Torvalds #include <net/ipv6.h> 1451da177e4SLinus Torvalds #include <net/addrconf.h> 1461da177e4SLinus Torvalds #include <asm/byteorder.h> 1471da177e4SLinus Torvalds #include <linux/rcupdate.h> 1481da177e4SLinus Torvalds #include <asm/bitops.h> 1491da177e4SLinus Torvalds #include <asm/io.h> 1501da177e4SLinus Torvalds #include <asm/dma.h> 1511da177e4SLinus Torvalds #include <asm/uaccess.h> 1521da177e4SLinus Torvalds #include <asm/div64.h> /* do_div */ 1531da177e4SLinus Torvalds #include <asm/timex.h> 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds 156d50a6b56SStephen Hemminger #define VERSION "pktgen v2.63: Packet Generator for packet performance testing.\n" 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* #define PG_DEBUG(a) a */ 1591da177e4SLinus Torvalds #define PG_DEBUG(a) 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* The buckets are exponential in 'width' */ 1621da177e4SLinus Torvalds #define LAT_BUCKETS_MAX 32 1631da177e4SLinus Torvalds #define IP_NAME_SZ 32 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds /* Device flag bits */ 1661da177e4SLinus Torvalds #define F_IPSRC_RND (1<<0) /* IP-Src Random */ 1671da177e4SLinus Torvalds #define F_IPDST_RND (1<<1) /* IP-Dst Random */ 1681da177e4SLinus Torvalds #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */ 1691da177e4SLinus Torvalds #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */ 1701da177e4SLinus Torvalds #define F_MACSRC_RND (1<<4) /* MAC-Src Random */ 1711da177e4SLinus Torvalds #define F_MACDST_RND (1<<5) /* MAC-Dst Random */ 1721da177e4SLinus Torvalds #define F_TXSIZE_RND (1<<6) /* Transmit size is random */ 1731da177e4SLinus Torvalds #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */ 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /* Thread control flag bits */ 1761da177e4SLinus Torvalds #define T_TERMINATE (1<<0) 1771da177e4SLinus Torvalds #define T_STOP (1<<1) /* Stop run */ 1781da177e4SLinus Torvalds #define T_RUN (1<<2) /* Start run */ 1791da177e4SLinus Torvalds #define T_REMDEV (1<<3) /* Remove all devs */ 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /* Locks */ 182b7c8921bSStephen Hemminger #define thread_lock() down(&pktgen_sem) 183b7c8921bSStephen Hemminger #define thread_unlock() up(&pktgen_sem) 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* If lock -- can be removed after some work */ 1861da177e4SLinus Torvalds #define if_lock(t) spin_lock(&(t->if_lock)); 1871da177e4SLinus Torvalds #define if_unlock(t) spin_unlock(&(t->if_lock)); 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */ 1901da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955 191d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen" 192d50a6b56SStephen Hemminger #define PGCTRL "pgctrl" 193d50a6b56SStephen Hemminger static struct proc_dir_entry *pg_proc_dir = NULL; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds #define MAX_CFLOWS 65536 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds struct flow_state 1981da177e4SLinus Torvalds { 1991da177e4SLinus Torvalds __u32 cur_daddr; 2001da177e4SLinus Torvalds int count; 2011da177e4SLinus Torvalds }; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds struct pktgen_dev { 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds /* 2061da177e4SLinus Torvalds * Try to keep frequent/infrequent used vars. separated. 2071da177e4SLinus Torvalds */ 2081da177e4SLinus Torvalds 209d50a6b56SStephen Hemminger char ifname[IFNAMSIZ]; 2101da177e4SLinus Torvalds char result[512]; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds struct pktgen_thread* pg_thread; /* the owner */ 2131da177e4SLinus Torvalds struct pktgen_dev *next; /* Used for chaining in the thread's run-queue */ 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds int running; /* if this changes to false, the test will stop */ 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds /* If min != max, then we will either do a linear iteration, or 2181da177e4SLinus Torvalds * we will do a random selection from within the range. 2191da177e4SLinus Torvalds */ 2201da177e4SLinus Torvalds __u32 flags; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds int min_pkt_size; /* = ETH_ZLEN; */ 2231da177e4SLinus Torvalds int max_pkt_size; /* = ETH_ZLEN; */ 2241da177e4SLinus Torvalds int nfrags; 2251da177e4SLinus Torvalds __u32 delay_us; /* Default delay */ 2261da177e4SLinus Torvalds __u32 delay_ns; 2271da177e4SLinus Torvalds __u64 count; /* Default No packets to send */ 2281da177e4SLinus Torvalds __u64 sofar; /* How many pkts we've sent so far */ 2291da177e4SLinus Torvalds __u64 tx_bytes; /* How many bytes we've transmitted */ 2301da177e4SLinus Torvalds __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */ 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* runtime counters relating to clone_skb */ 2331da177e4SLinus Torvalds __u64 next_tx_us; /* timestamp of when to tx next */ 2341da177e4SLinus Torvalds __u32 next_tx_ns; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds __u64 allocated_skbs; 2371da177e4SLinus Torvalds __u32 clone_count; 2381da177e4SLinus Torvalds int last_ok; /* Was last skb sent? 2391da177e4SLinus Torvalds * Or a failed transmit of some sort? This will keep 2401da177e4SLinus Torvalds * sequence numbers in order, for example. 2411da177e4SLinus Torvalds */ 2421da177e4SLinus Torvalds __u64 started_at; /* micro-seconds */ 2431da177e4SLinus Torvalds __u64 stopped_at; /* micro-seconds */ 2441da177e4SLinus Torvalds __u64 idle_acc; /* micro-seconds */ 2451da177e4SLinus Torvalds __u32 seq_num; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds int clone_skb; /* Use multiple SKBs during packet gen. If this number 248b4099fabSStephen Hemminger * is greater than 1, then that many copies of the same 2491da177e4SLinus Torvalds * packet will be sent before a new packet is allocated. 2501da177e4SLinus Torvalds * For instance, if you want to send 1024 identical packets 2511da177e4SLinus Torvalds * before creating a new packet, set clone_skb to 1024. 2521da177e4SLinus Torvalds */ 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2551da177e4SLinus Torvalds char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2561da177e4SLinus Torvalds char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2571da177e4SLinus Torvalds char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds struct in6_addr in6_saddr; 2601da177e4SLinus Torvalds struct in6_addr in6_daddr; 2611da177e4SLinus Torvalds struct in6_addr cur_in6_daddr; 2621da177e4SLinus Torvalds struct in6_addr cur_in6_saddr; 2631da177e4SLinus Torvalds /* For ranges */ 2641da177e4SLinus Torvalds struct in6_addr min_in6_daddr; 2651da177e4SLinus Torvalds struct in6_addr max_in6_daddr; 2661da177e4SLinus Torvalds struct in6_addr min_in6_saddr; 2671da177e4SLinus Torvalds struct in6_addr max_in6_saddr; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* If we're doing ranges, random or incremental, then this 2701da177e4SLinus Torvalds * defines the min/max for those ranges. 2711da177e4SLinus Torvalds */ 2721da177e4SLinus Torvalds __u32 saddr_min; /* inclusive, source IP address */ 2731da177e4SLinus Torvalds __u32 saddr_max; /* exclusive, source IP address */ 2741da177e4SLinus Torvalds __u32 daddr_min; /* inclusive, dest IP address */ 2751da177e4SLinus Torvalds __u32 daddr_max; /* exclusive, dest IP address */ 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds __u16 udp_src_min; /* inclusive, source UDP port */ 2781da177e4SLinus Torvalds __u16 udp_src_max; /* exclusive, source UDP port */ 2791da177e4SLinus Torvalds __u16 udp_dst_min; /* inclusive, dest UDP port */ 2801da177e4SLinus Torvalds __u16 udp_dst_max; /* exclusive, dest UDP port */ 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds __u32 src_mac_count; /* How many MACs to iterate through */ 2831da177e4SLinus Torvalds __u32 dst_mac_count; /* How many MACs to iterate through */ 2841da177e4SLinus Torvalds 285f404e9a6SKris Katterjohn unsigned char dst_mac[ETH_ALEN]; 286f404e9a6SKris Katterjohn unsigned char src_mac[ETH_ALEN]; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds __u32 cur_dst_mac_offset; 2891da177e4SLinus Torvalds __u32 cur_src_mac_offset; 2901da177e4SLinus Torvalds __u32 cur_saddr; 2911da177e4SLinus Torvalds __u32 cur_daddr; 2921da177e4SLinus Torvalds __u16 cur_udp_dst; 2931da177e4SLinus Torvalds __u16 cur_udp_src; 2941da177e4SLinus Torvalds __u32 cur_pkt_size; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds __u8 hh[14]; 2971da177e4SLinus Torvalds /* = { 2981da177e4SLinus Torvalds 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB, 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds We fill in SRC address later 3011da177e4SLinus Torvalds 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 3021da177e4SLinus Torvalds 0x08, 0x00 3031da177e4SLinus Torvalds }; 3041da177e4SLinus Torvalds */ 3051da177e4SLinus Torvalds __u16 pad; /* pad out the hh struct to an even 16 bytes */ 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds struct sk_buff* skb; /* skb we are to transmit next, mainly used for when we 3081da177e4SLinus Torvalds * are transmitting the same one multiple times 3091da177e4SLinus Torvalds */ 3101da177e4SLinus Torvalds struct net_device* odev; /* The out-going device. Note that the device should 3111da177e4SLinus Torvalds * have it's pg_info pointer pointing back to this 3121da177e4SLinus Torvalds * device. This will be set when the user specifies 3131da177e4SLinus Torvalds * the out-going device name (not when the inject is 3141da177e4SLinus Torvalds * started as it used to do.) 3151da177e4SLinus Torvalds */ 3161da177e4SLinus Torvalds struct flow_state *flows; 3171da177e4SLinus Torvalds unsigned cflows; /* Concurrent flows (config) */ 3181da177e4SLinus Torvalds unsigned lflow; /* Flow length (config) */ 3191da177e4SLinus Torvalds unsigned nflows; /* accumulated flows (stats) */ 3201da177e4SLinus Torvalds }; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds struct pktgen_hdr { 3231da177e4SLinus Torvalds __u32 pgh_magic; 3241da177e4SLinus Torvalds __u32 seq_num; 3251da177e4SLinus Torvalds __u32 tv_sec; 3261da177e4SLinus Torvalds __u32 tv_usec; 3271da177e4SLinus Torvalds }; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds struct pktgen_thread { 3301da177e4SLinus Torvalds spinlock_t if_lock; 3311da177e4SLinus Torvalds struct pktgen_dev *if_list; /* All device here */ 3321da177e4SLinus Torvalds struct pktgen_thread* next; 3331da177e4SLinus Torvalds char name[32]; 3341da177e4SLinus Torvalds char result[512]; 3351da177e4SLinus Torvalds u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */ 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds /* Field for thread to receive "posted" events terminate, stop ifs etc.*/ 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds u32 control; 3401da177e4SLinus Torvalds int pid; 3411da177e4SLinus Torvalds int cpu; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds wait_queue_head_t queue; 3441da177e4SLinus Torvalds }; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds #define REMOVE 1 3471da177e4SLinus Torvalds #define FIND 0 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds /* This code works around the fact that do_div cannot handle two 64-bit 3501da177e4SLinus Torvalds numbers, and regular 64-bit division doesn't work on x86 kernels. 3511da177e4SLinus Torvalds --Ben 3521da177e4SLinus Torvalds */ 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds #define PG_DIV 0 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net> 3571da177e4SLinus Torvalds * Function copied/adapted/optimized from: 3581da177e4SLinus Torvalds * 3591da177e4SLinus Torvalds * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html 3601da177e4SLinus Torvalds * 3611da177e4SLinus Torvalds * Copyright 1994, University of Cambridge Computer Laboratory 3621da177e4SLinus Torvalds * All Rights Reserved. 3631da177e4SLinus Torvalds * 3641da177e4SLinus Torvalds */ 36577933d72SJesper Juhl static inline s64 divremdi3(s64 x, s64 y, int type) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds u64 a = (x < 0) ? -x : x; 3681da177e4SLinus Torvalds u64 b = (y < 0) ? -y : y; 3691da177e4SLinus Torvalds u64 res = 0, d = 1; 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds if (b > 0) { 3721da177e4SLinus Torvalds while (b < a) { 3731da177e4SLinus Torvalds b <<= 1; 3741da177e4SLinus Torvalds d <<= 1; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds do { 3791da177e4SLinus Torvalds if ( a >= b ) { 3801da177e4SLinus Torvalds a -= b; 3811da177e4SLinus Torvalds res += d; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds b >>= 1; 3841da177e4SLinus Torvalds d >>= 1; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds while (d); 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds if (PG_DIV == type) { 3891da177e4SLinus Torvalds return (((x ^ y) & (1ll<<63)) == 0) ? res : -(s64)res; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds else { 3921da177e4SLinus Torvalds return ((x & (1ll<<63)) == 0) ? a : -(s64)a; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* End of hacks to deal with 64-bit math on x86 */ 3971da177e4SLinus Torvalds 398b4099fabSStephen Hemminger /** Convert to milliseconds */ 3991da177e4SLinus Torvalds static inline __u64 tv_to_ms(const struct timeval* tv) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds __u64 ms = tv->tv_usec / 1000; 4021da177e4SLinus Torvalds ms += (__u64)tv->tv_sec * (__u64)1000; 4031da177e4SLinus Torvalds return ms; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds /** Convert to micro-seconds */ 4081da177e4SLinus Torvalds static inline __u64 tv_to_us(const struct timeval* tv) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds __u64 us = tv->tv_usec; 4111da177e4SLinus Torvalds us += (__u64)tv->tv_sec * (__u64)1000000; 4121da177e4SLinus Torvalds return us; 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds static inline __u64 pg_div(__u64 n, __u32 base) { 4161da177e4SLinus Torvalds __u64 tmp = n; 4171da177e4SLinus Torvalds do_div(tmp, base); 4181da177e4SLinus Torvalds /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n", 4191da177e4SLinus Torvalds n, base, tmp); */ 4201da177e4SLinus Torvalds return tmp; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds static inline __u64 pg_div64(__u64 n, __u64 base) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds __u64 tmp = n; 4261da177e4SLinus Torvalds /* 427b4099fabSStephen Hemminger * How do we know if the architecture we are running on 4281da177e4SLinus Torvalds * supports division with 64 bit base? 4291da177e4SLinus Torvalds * 4301da177e4SLinus Torvalds */ 4311da177e4SLinus Torvalds #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__) 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds do_div(tmp, base); 4341da177e4SLinus Torvalds #else 4351da177e4SLinus Torvalds tmp = divremdi3(n, base, PG_DIV); 4361da177e4SLinus Torvalds #endif 4371da177e4SLinus Torvalds return tmp; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds static inline u32 pktgen_random(void) 4411da177e4SLinus Torvalds { 4421da177e4SLinus Torvalds #if 0 4431da177e4SLinus Torvalds __u32 n; 4441da177e4SLinus Torvalds get_random_bytes(&n, 4); 4451da177e4SLinus Torvalds return n; 4461da177e4SLinus Torvalds #else 4471da177e4SLinus Torvalds return net_random(); 4481da177e4SLinus Torvalds #endif 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds static inline __u64 getCurMs(void) 4521da177e4SLinus Torvalds { 4531da177e4SLinus Torvalds struct timeval tv; 4541da177e4SLinus Torvalds do_gettimeofday(&tv); 4551da177e4SLinus Torvalds return tv_to_ms(&tv); 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds static inline __u64 getCurUs(void) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds struct timeval tv; 4611da177e4SLinus Torvalds do_gettimeofday(&tv); 4621da177e4SLinus Torvalds return tv_to_us(&tv); 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds static inline __u64 tv_diff(const struct timeval* a, const struct timeval* b) 4661da177e4SLinus Torvalds { 4671da177e4SLinus Torvalds return tv_to_us(a) - tv_to_us(b); 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds /* old include end */ 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds static char version[] __initdata = VERSION; 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread* t, struct pktgen_dev *i); 4761da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread* t, const char* ifname); 4771da177e4SLinus Torvalds static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread* t, const char* ifname); 4781da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *); 4791da177e4SLinus Torvalds static void pktgen_run_all_threads(void); 4801da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void); 4811da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev); 4821da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread* t); 4831da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev); 4841da177e4SLinus Torvalds static struct pktgen_dev *pktgen_NN_threads(const char* dev_name, int remove); 4851da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s,char ip[16]); 4861da177e4SLinus Torvalds static unsigned int fmt_ip6(char *s,const char ip[16]); 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* Module parameters, defaults. */ 4891da177e4SLinus Torvalds static int pg_count_d = 1000; /* 1000 pkts by default */ 490f34fbb97SJaco Kroon static int pg_delay_d; 491f34fbb97SJaco Kroon static int pg_clone_skb_d; 492f34fbb97SJaco Kroon static int debug; 4931da177e4SLinus Torvalds 494b7c8921bSStephen Hemminger static DECLARE_MUTEX(pktgen_sem); 4951da177e4SLinus Torvalds static struct pktgen_thread *pktgen_threads = NULL; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = { 4981da177e4SLinus Torvalds .notifier_call = pktgen_device_event, 4991da177e4SLinus Torvalds }; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds /* 5021da177e4SLinus Torvalds * /proc handling functions 5031da177e4SLinus Torvalds * 5041da177e4SLinus Torvalds */ 5051da177e4SLinus Torvalds 506d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v) 507d50a6b56SStephen Hemminger { 508d50a6b56SStephen Hemminger seq_puts(seq, VERSION); 509d50a6b56SStephen Hemminger return 0; 510d50a6b56SStephen Hemminger } 5111da177e4SLinus Torvalds 512d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file* file,const char __user * buf, 5131da177e4SLinus Torvalds size_t count, loff_t *ppos) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds int err = 0; 516d50a6b56SStephen Hemminger char data[128]; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)){ 5191da177e4SLinus Torvalds err = -EPERM; 5201da177e4SLinus Torvalds goto out; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 523d50a6b56SStephen Hemminger if (count > sizeof(data)) 524d50a6b56SStephen Hemminger count = sizeof(data); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds if (copy_from_user(data, buf, count)) { 5271da177e4SLinus Torvalds err = -EFAULT; 528d50a6b56SStephen Hemminger goto out; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds data[count-1] = 0; /* Make string */ 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds if (!strcmp(data, "stop")) 5331da177e4SLinus Torvalds pktgen_stop_all_threads_ifs(); 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds else if (!strcmp(data, "start")) 5361da177e4SLinus Torvalds pktgen_run_all_threads(); 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds else 5391da177e4SLinus Torvalds printk("pktgen: Unknown command: %s\n", data); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds err = count; 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds out: 5441da177e4SLinus Torvalds return err; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 547d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file) 5481da177e4SLinus Torvalds { 549d50a6b56SStephen Hemminger return single_open(file, pgctrl_show, PDE(inode)->data); 550d50a6b56SStephen Hemminger } 551d50a6b56SStephen Hemminger 552d50a6b56SStephen Hemminger static struct file_operations pktgen_fops = { 553d50a6b56SStephen Hemminger .owner = THIS_MODULE, 554d50a6b56SStephen Hemminger .open = pgctrl_open, 555d50a6b56SStephen Hemminger .read = seq_read, 556d50a6b56SStephen Hemminger .llseek = seq_lseek, 557d50a6b56SStephen Hemminger .write = pgctrl_write, 558d50a6b56SStephen Hemminger .release = single_release, 559d50a6b56SStephen Hemminger }; 560d50a6b56SStephen Hemminger 561d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v) 562d50a6b56SStephen Hemminger { 5631da177e4SLinus Torvalds int i; 564d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 5651da177e4SLinus Torvalds __u64 sa; 5661da177e4SLinus Torvalds __u64 stopped; 5671da177e4SLinus Torvalds __u64 now = getCurUs(); 5681da177e4SLinus Torvalds 569d50a6b56SStephen Hemminger seq_printf(seq, "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n", 5701da177e4SLinus Torvalds (unsigned long long) pkt_dev->count, 5711da177e4SLinus Torvalds pkt_dev->min_pkt_size, pkt_dev->max_pkt_size); 5721da177e4SLinus Torvalds 573d50a6b56SStephen Hemminger seq_printf(seq, " frags: %d delay: %u clone_skb: %d ifname: %s\n", 5741da177e4SLinus Torvalds pkt_dev->nfrags, 1000*pkt_dev->delay_us+pkt_dev->delay_ns, pkt_dev->clone_skb, pkt_dev->ifname); 5751da177e4SLinus Torvalds 576d50a6b56SStephen Hemminger seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows, pkt_dev->lflow); 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds if(pkt_dev->flags & F_IPV6) { 5801da177e4SLinus Torvalds char b1[128], b2[128], b3[128]; 5811da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr); 5821da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr); 5831da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr); 584d50a6b56SStephen Hemminger seq_printf(seq, " saddr: %s min_saddr: %s max_saddr: %s\n", b1, b2, b3); 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr); 5871da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr); 5881da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr); 589d50a6b56SStephen Hemminger seq_printf(seq, " daddr: %s min_daddr: %s max_daddr: %s\n", b1, b2, b3); 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds else 593d50a6b56SStephen Hemminger seq_printf(seq," dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n", 5941da177e4SLinus Torvalds pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min, pkt_dev->src_max); 5951da177e4SLinus Torvalds 596d50a6b56SStephen Hemminger seq_puts(seq, " src_mac: "); 5971da177e4SLinus Torvalds 598f404e9a6SKris Katterjohn if (is_zero_ether_addr(pkt_dev->src_mac)) 5991da177e4SLinus Torvalds for (i = 0; i < 6; i++) 600d50a6b56SStephen Hemminger seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i], i == 5 ? " " : ":"); 6011da177e4SLinus Torvalds else 6021da177e4SLinus Torvalds for (i = 0; i < 6; i++) 603d50a6b56SStephen Hemminger seq_printf(seq, "%02X%s", pkt_dev->src_mac[i], i == 5 ? " " : ":"); 6041da177e4SLinus Torvalds 605d50a6b56SStephen Hemminger seq_printf(seq, "dst_mac: "); 6061da177e4SLinus Torvalds for (i = 0; i < 6; i++) 607d50a6b56SStephen Hemminger seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i], i == 5 ? "\n" : ":"); 6081da177e4SLinus Torvalds 609d50a6b56SStephen Hemminger seq_printf(seq, " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n", 6101da177e4SLinus Torvalds pkt_dev->udp_src_min, pkt_dev->udp_src_max, pkt_dev->udp_dst_min, 6111da177e4SLinus Torvalds pkt_dev->udp_dst_max); 6121da177e4SLinus Torvalds 613d50a6b56SStephen Hemminger seq_printf(seq, " src_mac_count: %d dst_mac_count: %d \n Flags: ", 6141da177e4SLinus Torvalds pkt_dev->src_mac_count, pkt_dev->dst_mac_count); 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) 618d50a6b56SStephen Hemminger seq_printf(seq, "IPV6 "); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 621d50a6b56SStephen Hemminger seq_printf(seq, "IPSRC_RND "); 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) 624d50a6b56SStephen Hemminger seq_printf(seq, "IPDST_RND "); 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) 627d50a6b56SStephen Hemminger seq_printf(seq, "TXSIZE_RND "); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 630d50a6b56SStephen Hemminger seq_printf(seq, "UDPSRC_RND "); 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) 633d50a6b56SStephen Hemminger seq_printf(seq, "UDPDST_RND "); 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 636d50a6b56SStephen Hemminger seq_printf(seq, "MACSRC_RND "); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 639d50a6b56SStephen Hemminger seq_printf(seq, "MACDST_RND "); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds 642d50a6b56SStephen Hemminger seq_puts(seq, "\n"); 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds sa = pkt_dev->started_at; 6451da177e4SLinus Torvalds stopped = pkt_dev->stopped_at; 6461da177e4SLinus Torvalds if (pkt_dev->running) 6471da177e4SLinus Torvalds stopped = now; /* not really stopped, more like last-running-at */ 6481da177e4SLinus Torvalds 649d50a6b56SStephen Hemminger seq_printf(seq, "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n", 6501da177e4SLinus Torvalds (unsigned long long) pkt_dev->sofar, 6511da177e4SLinus Torvalds (unsigned long long) pkt_dev->errors, 6521da177e4SLinus Torvalds (unsigned long long) sa, 6531da177e4SLinus Torvalds (unsigned long long) stopped, 6541da177e4SLinus Torvalds (unsigned long long) pkt_dev->idle_acc); 6551da177e4SLinus Torvalds 656d50a6b56SStephen Hemminger seq_printf(seq, " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n", 657d50a6b56SStephen Hemminger pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset, 658d50a6b56SStephen Hemminger pkt_dev->cur_src_mac_offset); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds if(pkt_dev->flags & F_IPV6) { 6611da177e4SLinus Torvalds char b1[128], b2[128]; 6621da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr); 6631da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr); 664d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1); 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds else 667d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n", 6681da177e4SLinus Torvalds pkt_dev->cur_saddr, pkt_dev->cur_daddr); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds 671d50a6b56SStephen Hemminger seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n", 6721da177e4SLinus Torvalds pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src); 6731da177e4SLinus Torvalds 674d50a6b56SStephen Hemminger seq_printf(seq, " flows: %u\n", pkt_dev->nflows); 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds if (pkt_dev->result[0]) 677d50a6b56SStephen Hemminger seq_printf(seq, "Result: %s\n", pkt_dev->result); 6781da177e4SLinus Torvalds else 679d50a6b56SStephen Hemminger seq_printf(seq, "Result: Idle\n"); 6801da177e4SLinus Torvalds 681d50a6b56SStephen Hemminger return 0; 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds static int count_trail_chars(const char __user *user_buffer, unsigned int maxlen) 6861da177e4SLinus Torvalds { 6871da177e4SLinus Torvalds int i; 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds for (i = 0; i < maxlen; i++) { 6901da177e4SLinus Torvalds char c; 6911da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 6921da177e4SLinus Torvalds return -EFAULT; 6931da177e4SLinus Torvalds switch (c) { 6941da177e4SLinus Torvalds case '\"': 6951da177e4SLinus Torvalds case '\n': 6961da177e4SLinus Torvalds case '\r': 6971da177e4SLinus Torvalds case '\t': 6981da177e4SLinus Torvalds case ' ': 6991da177e4SLinus Torvalds case '=': 7001da177e4SLinus Torvalds break; 7011da177e4SLinus Torvalds default: 7021da177e4SLinus Torvalds goto done; 7031da177e4SLinus Torvalds }; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds done: 7061da177e4SLinus Torvalds return i; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds static unsigned long num_arg(const char __user *user_buffer, unsigned long maxlen, 7101da177e4SLinus Torvalds unsigned long *num) 7111da177e4SLinus Torvalds { 7121da177e4SLinus Torvalds int i = 0; 7131da177e4SLinus Torvalds *num = 0; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds for(; i < maxlen; i++) { 7161da177e4SLinus Torvalds char c; 7171da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 7181da177e4SLinus Torvalds return -EFAULT; 7191da177e4SLinus Torvalds if ((c >= '0') && (c <= '9')) { 7201da177e4SLinus Torvalds *num *= 10; 7211da177e4SLinus Torvalds *num += c -'0'; 7221da177e4SLinus Torvalds } else 7231da177e4SLinus Torvalds break; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds return i; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds static int strn_len(const char __user *user_buffer, unsigned int maxlen) 7291da177e4SLinus Torvalds { 7301da177e4SLinus Torvalds int i = 0; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds for(; i < maxlen; i++) { 7331da177e4SLinus Torvalds char c; 7341da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 7351da177e4SLinus Torvalds return -EFAULT; 7361da177e4SLinus Torvalds switch (c) { 7371da177e4SLinus Torvalds case '\"': 7381da177e4SLinus Torvalds case '\n': 7391da177e4SLinus Torvalds case '\r': 7401da177e4SLinus Torvalds case '\t': 7411da177e4SLinus Torvalds case ' ': 7421da177e4SLinus Torvalds goto done_str; 7431da177e4SLinus Torvalds break; 7441da177e4SLinus Torvalds default: 7451da177e4SLinus Torvalds break; 7461da177e4SLinus Torvalds }; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds done_str: 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds return i; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 753d50a6b56SStephen Hemminger static ssize_t pktgen_if_write(struct file *file, const char __user *user_buffer, 754d50a6b56SStephen Hemminger size_t count, loff_t *offset) 7551da177e4SLinus Torvalds { 756d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *) file->private_data; 757d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 7581da177e4SLinus Torvalds int i = 0, max, len; 7591da177e4SLinus Torvalds char name[16], valstr[32]; 7601da177e4SLinus Torvalds unsigned long value = 0; 7611da177e4SLinus Torvalds char* pg_result = NULL; 7621da177e4SLinus Torvalds int tmp = 0; 7631da177e4SLinus Torvalds char buf[128]; 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds pg_result = &(pkt_dev->result[0]); 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds if (count < 1) { 7681da177e4SLinus Torvalds printk("pktgen: wrong command format\n"); 7691da177e4SLinus Torvalds return -EINVAL; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds max = count - i; 7731da177e4SLinus Torvalds tmp = count_trail_chars(&user_buffer[i], max); 7741da177e4SLinus Torvalds if (tmp < 0) { 7751da177e4SLinus Torvalds printk("pktgen: illegal format\n"); 7761da177e4SLinus Torvalds return tmp; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds i += tmp; 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds /* Read variable name */ 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 7831da177e4SLinus Torvalds if (len < 0) { return len; } 7841da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 7851da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len) ) 7861da177e4SLinus Torvalds return -EFAULT; 7871da177e4SLinus Torvalds i += len; 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds max = count -i; 7901da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 7911da177e4SLinus Torvalds if (len < 0) 7921da177e4SLinus Torvalds return len; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds i += len; 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds if (debug) { 7971da177e4SLinus Torvalds char tb[count + 1]; 7981da177e4SLinus Torvalds if (copy_from_user(tb, user_buffer, count)) 7991da177e4SLinus Torvalds return -EFAULT; 8001da177e4SLinus Torvalds tb[count] = 0; 801d50a6b56SStephen Hemminger printk("pktgen: %s,%lu buffer -:%s:-\n", name, 802d50a6b56SStephen Hemminger (unsigned long) count, tb); 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds if (!strcmp(name, "min_pkt_size")) { 8061da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 8071da177e4SLinus Torvalds if (len < 0) { return len; } 8081da177e4SLinus Torvalds i += len; 8091da177e4SLinus Torvalds if (value < 14+20+8) 8101da177e4SLinus Torvalds value = 14+20+8; 8111da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 8121da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 8131da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds sprintf(pg_result, "OK: min_pkt_size=%u", pkt_dev->min_pkt_size); 8161da177e4SLinus Torvalds return count; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds if (!strcmp(name, "max_pkt_size")) { 8201da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 8211da177e4SLinus Torvalds if (len < 0) { return len; } 8221da177e4SLinus Torvalds i += len; 8231da177e4SLinus Torvalds if (value < 14+20+8) 8241da177e4SLinus Torvalds value = 14+20+8; 8251da177e4SLinus Torvalds if (value != pkt_dev->max_pkt_size) { 8261da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 8271da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds sprintf(pg_result, "OK: max_pkt_size=%u", pkt_dev->max_pkt_size); 8301da177e4SLinus Torvalds return count; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds /* Shortcut for min = max */ 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds if (!strcmp(name, "pkt_size")) { 8361da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 8371da177e4SLinus Torvalds if (len < 0) { return len; } 8381da177e4SLinus Torvalds i += len; 8391da177e4SLinus Torvalds if (value < 14+20+8) 8401da177e4SLinus Torvalds value = 14+20+8; 8411da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 8421da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 8431da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 8441da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size); 8471da177e4SLinus Torvalds return count; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds if (!strcmp(name, "debug")) { 8511da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 8521da177e4SLinus Torvalds if (len < 0) { return len; } 8531da177e4SLinus Torvalds i += len; 8541da177e4SLinus Torvalds debug = value; 8551da177e4SLinus Torvalds sprintf(pg_result, "OK: debug=%u", debug); 8561da177e4SLinus Torvalds return count; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds if (!strcmp(name, "frags")) { 8601da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 8611da177e4SLinus Torvalds if (len < 0) { return len; } 8621da177e4SLinus Torvalds i += len; 8631da177e4SLinus Torvalds pkt_dev->nfrags = value; 8641da177e4SLinus Torvalds sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags); 8651da177e4SLinus Torvalds return count; 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds if (!strcmp(name, "delay")) { 8681da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 8691da177e4SLinus Torvalds if (len < 0) { return len; } 8701da177e4SLinus Torvalds i += len; 8711da177e4SLinus Torvalds if (value == 0x7FFFFFFF) { 8721da177e4SLinus Torvalds pkt_dev->delay_us = 0x7FFFFFFF; 8731da177e4SLinus Torvalds pkt_dev->delay_ns = 0; 8741da177e4SLinus Torvalds } else { 8751da177e4SLinus Torvalds pkt_dev->delay_us = value / 1000; 8761da177e4SLinus Torvalds pkt_dev->delay_ns = value % 1000; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds sprintf(pg_result, "OK: delay=%u", 1000*pkt_dev->delay_us+pkt_dev->delay_ns); 8791da177e4SLinus Torvalds return count; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds if (!strcmp(name, "udp_src_min")) { 8821da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 8831da177e4SLinus Torvalds if (len < 0) { return len; } 8841da177e4SLinus Torvalds i += len; 8851da177e4SLinus Torvalds if (value != pkt_dev->udp_src_min) { 8861da177e4SLinus Torvalds pkt_dev->udp_src_min = value; 8871da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min); 8901da177e4SLinus Torvalds return count; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_min")) { 8931da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 8941da177e4SLinus Torvalds if (len < 0) { return len; } 8951da177e4SLinus Torvalds i += len; 8961da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_min) { 8971da177e4SLinus Torvalds pkt_dev->udp_dst_min = value; 8981da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min); 9011da177e4SLinus Torvalds return count; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds if (!strcmp(name, "udp_src_max")) { 9041da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 9051da177e4SLinus Torvalds if (len < 0) { return len; } 9061da177e4SLinus Torvalds i += len; 9071da177e4SLinus Torvalds if (value != pkt_dev->udp_src_max) { 9081da177e4SLinus Torvalds pkt_dev->udp_src_max = value; 9091da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max); 9121da177e4SLinus Torvalds return count; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_max")) { 9151da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 9161da177e4SLinus Torvalds if (len < 0) { return len; } 9171da177e4SLinus Torvalds i += len; 9181da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_max) { 9191da177e4SLinus Torvalds pkt_dev->udp_dst_max = value; 9201da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max); 9231da177e4SLinus Torvalds return count; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds if (!strcmp(name, "clone_skb")) { 9261da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 9271da177e4SLinus Torvalds if (len < 0) { return len; } 9281da177e4SLinus Torvalds i += len; 9291da177e4SLinus Torvalds pkt_dev->clone_skb = value; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb); 9321da177e4SLinus Torvalds return count; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds if (!strcmp(name, "count")) { 9351da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 9361da177e4SLinus Torvalds if (len < 0) { return len; } 9371da177e4SLinus Torvalds i += len; 9381da177e4SLinus Torvalds pkt_dev->count = value; 9391da177e4SLinus Torvalds sprintf(pg_result, "OK: count=%llu", 9401da177e4SLinus Torvalds (unsigned long long) pkt_dev->count); 9411da177e4SLinus Torvalds return count; 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds if (!strcmp(name, "src_mac_count")) { 9441da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 9451da177e4SLinus Torvalds if (len < 0) { return len; } 9461da177e4SLinus Torvalds i += len; 9471da177e4SLinus Torvalds if (pkt_dev->src_mac_count != value) { 9481da177e4SLinus Torvalds pkt_dev->src_mac_count = value; 9491da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds sprintf(pg_result, "OK: src_mac_count=%d", pkt_dev->src_mac_count); 9521da177e4SLinus Torvalds return count; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds if (!strcmp(name, "dst_mac_count")) { 9551da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 9561da177e4SLinus Torvalds if (len < 0) { return len; } 9571da177e4SLinus Torvalds i += len; 9581da177e4SLinus Torvalds if (pkt_dev->dst_mac_count != value) { 9591da177e4SLinus Torvalds pkt_dev->dst_mac_count = value; 9601da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_mac_count=%d", pkt_dev->dst_mac_count); 9631da177e4SLinus Torvalds return count; 9641da177e4SLinus Torvalds } 9651da177e4SLinus Torvalds if (!strcmp(name, "flag")) { 9661da177e4SLinus Torvalds char f[32]; 9671da177e4SLinus Torvalds memset(f, 0, 32); 9681da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 9691da177e4SLinus Torvalds if (len < 0) { return len; } 9701da177e4SLinus Torvalds if (copy_from_user(f, &user_buffer[i], len)) 9711da177e4SLinus Torvalds return -EFAULT; 9721da177e4SLinus Torvalds i += len; 9731da177e4SLinus Torvalds if (strcmp(f, "IPSRC_RND") == 0) 9741da177e4SLinus Torvalds pkt_dev->flags |= F_IPSRC_RND; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds else if (strcmp(f, "!IPSRC_RND") == 0) 9771da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPSRC_RND; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds else if (strcmp(f, "TXSIZE_RND") == 0) 9801da177e4SLinus Torvalds pkt_dev->flags |= F_TXSIZE_RND; 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds else if (strcmp(f, "!TXSIZE_RND") == 0) 9831da177e4SLinus Torvalds pkt_dev->flags &= ~F_TXSIZE_RND; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds else if (strcmp(f, "IPDST_RND") == 0) 9861da177e4SLinus Torvalds pkt_dev->flags |= F_IPDST_RND; 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds else if (strcmp(f, "!IPDST_RND") == 0) 9891da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPDST_RND; 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds else if (strcmp(f, "UDPSRC_RND") == 0) 9921da177e4SLinus Torvalds pkt_dev->flags |= F_UDPSRC_RND; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds else if (strcmp(f, "!UDPSRC_RND") == 0) 9951da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPSRC_RND; 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds else if (strcmp(f, "UDPDST_RND") == 0) 9981da177e4SLinus Torvalds pkt_dev->flags |= F_UDPDST_RND; 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds else if (strcmp(f, "!UDPDST_RND") == 0) 10011da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPDST_RND; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds else if (strcmp(f, "MACSRC_RND") == 0) 10041da177e4SLinus Torvalds pkt_dev->flags |= F_MACSRC_RND; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds else if (strcmp(f, "!MACSRC_RND") == 0) 10071da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACSRC_RND; 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds else if (strcmp(f, "MACDST_RND") == 0) 10101da177e4SLinus Torvalds pkt_dev->flags |= F_MACDST_RND; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds else if (strcmp(f, "!MACDST_RND") == 0) 10131da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACDST_RND; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds else { 10161da177e4SLinus Torvalds sprintf(pg_result, "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s", 10171da177e4SLinus Torvalds f, 10181da177e4SLinus Torvalds "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n"); 10191da177e4SLinus Torvalds return count; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags); 10221da177e4SLinus Torvalds return count; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) { 10251da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1); 10261da177e4SLinus Torvalds if (len < 0) { return len; } 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 10291da177e4SLinus Torvalds return -EFAULT; 10301da177e4SLinus Torvalds buf[len] = 0; 10311da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_min) != 0) { 10321da177e4SLinus Torvalds memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min)); 10331da177e4SLinus Torvalds strncpy(pkt_dev->dst_min, buf, len); 10341da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 10351da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds if(debug) 10381da177e4SLinus Torvalds printk("pktgen: dst_min set to: %s\n", pkt_dev->dst_min); 10391da177e4SLinus Torvalds i += len; 10401da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min); 10411da177e4SLinus Torvalds return count; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds if (!strcmp(name, "dst_max")) { 10441da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1); 10451da177e4SLinus Torvalds if (len < 0) { return len; } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 10481da177e4SLinus Torvalds return -EFAULT; 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds buf[len] = 0; 10511da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_max) != 0) { 10521da177e4SLinus Torvalds memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max)); 10531da177e4SLinus Torvalds strncpy(pkt_dev->dst_max, buf, len); 10541da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 10551da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_max; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds if(debug) 10581da177e4SLinus Torvalds printk("pktgen: dst_max set to: %s\n", pkt_dev->dst_max); 10591da177e4SLinus Torvalds i += len; 10601da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max); 10611da177e4SLinus Torvalds return count; 10621da177e4SLinus Torvalds } 10631da177e4SLinus Torvalds if (!strcmp(name, "dst6")) { 10641da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 10651da177e4SLinus Torvalds if (len < 0) return len; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 10701da177e4SLinus Torvalds return -EFAULT; 10711da177e4SLinus Torvalds buf[len] = 0; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_daddr.s6_addr); 10741da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr); 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr); 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds if(debug) 10791da177e4SLinus Torvalds printk("pktgen: dst6 set to: %s\n", buf); 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds i += len; 10821da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6=%s", buf); 10831da177e4SLinus Torvalds return count; 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds if (!strcmp(name, "dst6_min")) { 10861da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 10871da177e4SLinus Torvalds if (len < 0) return len; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 10921da177e4SLinus Torvalds return -EFAULT; 10931da177e4SLinus Torvalds buf[len] = 0; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 10961da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->min_in6_daddr); 10991da177e4SLinus Torvalds if(debug) 11001da177e4SLinus Torvalds printk("pktgen: dst6_min set to: %s\n", buf); 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds i += len; 11031da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_min=%s", buf); 11041da177e4SLinus Torvalds return count; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds if (!strcmp(name, "dst6_max")) { 11071da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 11081da177e4SLinus Torvalds if (len < 0) return len; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11131da177e4SLinus Torvalds return -EFAULT; 11141da177e4SLinus Torvalds buf[len] = 0; 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 11171da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds if(debug) 11201da177e4SLinus Torvalds printk("pktgen: dst6_max set to: %s\n", buf); 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds i += len; 11231da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_max=%s", buf); 11241da177e4SLinus Torvalds return count; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds if (!strcmp(name, "src6")) { 11271da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 11281da177e4SLinus Torvalds if (len < 0) return len; 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11331da177e4SLinus Torvalds return -EFAULT; 11341da177e4SLinus Torvalds buf[len] = 0; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_saddr.s6_addr); 11371da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr); 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr); 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds if(debug) 11421da177e4SLinus Torvalds printk("pktgen: src6 set to: %s\n", buf); 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds i += len; 11451da177e4SLinus Torvalds sprintf(pg_result, "OK: src6=%s", buf); 11461da177e4SLinus Torvalds return count; 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds if (!strcmp(name, "src_min")) { 11491da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1); 11501da177e4SLinus Torvalds if (len < 0) { return len; } 11511da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11521da177e4SLinus Torvalds return -EFAULT; 11531da177e4SLinus Torvalds buf[len] = 0; 11541da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_min) != 0) { 11551da177e4SLinus Torvalds memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min)); 11561da177e4SLinus Torvalds strncpy(pkt_dev->src_min, buf, len); 11571da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 11581da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds if(debug) 11611da177e4SLinus Torvalds printk("pktgen: src_min set to: %s\n", pkt_dev->src_min); 11621da177e4SLinus Torvalds i += len; 11631da177e4SLinus Torvalds sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min); 11641da177e4SLinus Torvalds return count; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds if (!strcmp(name, "src_max")) { 11671da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1); 11681da177e4SLinus Torvalds if (len < 0) { return len; } 11691da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 11701da177e4SLinus Torvalds return -EFAULT; 11711da177e4SLinus Torvalds buf[len] = 0; 11721da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_max) != 0) { 11731da177e4SLinus Torvalds memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max)); 11741da177e4SLinus Torvalds strncpy(pkt_dev->src_max, buf, len); 11751da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 11761da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_max; 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds if(debug) 11791da177e4SLinus Torvalds printk("pktgen: src_max set to: %s\n", pkt_dev->src_max); 11801da177e4SLinus Torvalds i += len; 11811da177e4SLinus Torvalds sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max); 11821da177e4SLinus Torvalds return count; 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds if (!strcmp(name, "dst_mac")) { 11851da177e4SLinus Torvalds char *v = valstr; 1186f404e9a6SKris Katterjohn unsigned char old_dmac[ETH_ALEN]; 11871da177e4SLinus Torvalds unsigned char *m = pkt_dev->dst_mac; 1188f404e9a6SKris Katterjohn memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN); 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 11911da177e4SLinus Torvalds if (len < 0) { return len; } 11921da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 11931da177e4SLinus Torvalds if( copy_from_user(valstr, &user_buffer[i], len)) 11941da177e4SLinus Torvalds return -EFAULT; 11951da177e4SLinus Torvalds i += len; 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds for(*m = 0;*v && m < pkt_dev->dst_mac + 6; v++) { 11981da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 11991da177e4SLinus Torvalds *m *= 16; 12001da177e4SLinus Torvalds *m += *v - '0'; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 12031da177e4SLinus Torvalds *m *= 16; 12041da177e4SLinus Torvalds *m += *v - 'A' + 10; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 12071da177e4SLinus Torvalds *m *= 16; 12081da177e4SLinus Torvalds *m += *v - 'a' + 10; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds if (*v == ':') { 12111da177e4SLinus Torvalds m++; 12121da177e4SLinus Torvalds *m = 0; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds /* Set up Dest MAC */ 1217f404e9a6SKris Katterjohn if (compare_ether_addr(old_dmac, pkt_dev->dst_mac)) 1218f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds sprintf(pg_result, "OK: dstmac"); 12211da177e4SLinus Torvalds return count; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds if (!strcmp(name, "src_mac")) { 12241da177e4SLinus Torvalds char *v = valstr; 12251da177e4SLinus Torvalds unsigned char *m = pkt_dev->src_mac; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 12281da177e4SLinus Torvalds if (len < 0) { return len; } 12291da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 12301da177e4SLinus Torvalds if( copy_from_user(valstr, &user_buffer[i], len)) 12311da177e4SLinus Torvalds return -EFAULT; 12321da177e4SLinus Torvalds i += len; 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds for(*m = 0;*v && m < pkt_dev->src_mac + 6; v++) { 12351da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 12361da177e4SLinus Torvalds *m *= 16; 12371da177e4SLinus Torvalds *m += *v - '0'; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 12401da177e4SLinus Torvalds *m *= 16; 12411da177e4SLinus Torvalds *m += *v - 'A' + 10; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 12441da177e4SLinus Torvalds *m *= 16; 12451da177e4SLinus Torvalds *m += *v - 'a' + 10; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds if (*v == ':') { 12481da177e4SLinus Torvalds m++; 12491da177e4SLinus Torvalds *m = 0; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds sprintf(pg_result, "OK: srcmac"); 12541da177e4SLinus Torvalds return count; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds if (!strcmp(name, "clear_counters")) { 12581da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 12591da177e4SLinus Torvalds sprintf(pg_result, "OK: Clearing counters.\n"); 12601da177e4SLinus Torvalds return count; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds if (!strcmp(name, "flows")) { 12641da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 12651da177e4SLinus Torvalds if (len < 0) { return len; } 12661da177e4SLinus Torvalds i += len; 12671da177e4SLinus Torvalds if (value > MAX_CFLOWS) 12681da177e4SLinus Torvalds value = MAX_CFLOWS; 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds pkt_dev->cflows = value; 12711da177e4SLinus Torvalds sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows); 12721da177e4SLinus Torvalds return count; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds if (!strcmp(name, "flowlen")) { 12761da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 12771da177e4SLinus Torvalds if (len < 0) { return len; } 12781da177e4SLinus Torvalds i += len; 12791da177e4SLinus Torvalds pkt_dev->lflow = value; 12801da177e4SLinus Torvalds sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow); 12811da177e4SLinus Torvalds return count; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds sprintf(pkt_dev->result, "No such parameter \"%s\"", name); 12851da177e4SLinus Torvalds return -EINVAL; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 1288d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file) 12891da177e4SLinus Torvalds { 1290d50a6b56SStephen Hemminger return single_open(file, pktgen_if_show, PDE(inode)->data); 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds 1293d50a6b56SStephen Hemminger static struct file_operations pktgen_if_fops = { 1294d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1295d50a6b56SStephen Hemminger .open = pktgen_if_open, 1296d50a6b56SStephen Hemminger .read = seq_read, 1297d50a6b56SStephen Hemminger .llseek = seq_lseek, 1298d50a6b56SStephen Hemminger .write = pktgen_if_write, 1299d50a6b56SStephen Hemminger .release = single_release, 1300d50a6b56SStephen Hemminger }; 1301d50a6b56SStephen Hemminger 1302d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v) 1303d50a6b56SStephen Hemminger { 1304d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 1305d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = NULL; 1306d50a6b56SStephen Hemminger 1307d50a6b56SStephen Hemminger BUG_ON(!t); 1308d50a6b56SStephen Hemminger 1309d50a6b56SStephen Hemminger seq_printf(seq, "Name: %s max_before_softirq: %d\n", 13101da177e4SLinus Torvalds t->name, t->max_before_softirq); 13111da177e4SLinus Torvalds 1312d50a6b56SStephen Hemminger seq_printf(seq, "Running: "); 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds if_lock(t); 13151da177e4SLinus Torvalds for(pkt_dev = t->if_list;pkt_dev; pkt_dev = pkt_dev->next) 13161da177e4SLinus Torvalds if(pkt_dev->running) 1317d50a6b56SStephen Hemminger seq_printf(seq, "%s ", pkt_dev->ifname); 13181da177e4SLinus Torvalds 1319d50a6b56SStephen Hemminger seq_printf(seq, "\nStopped: "); 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds for(pkt_dev = t->if_list;pkt_dev; pkt_dev = pkt_dev->next) 13221da177e4SLinus Torvalds if(!pkt_dev->running) 1323d50a6b56SStephen Hemminger seq_printf(seq, "%s ", pkt_dev->ifname); 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds if (t->result[0]) 1326d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: %s\n", t->result); 13271da177e4SLinus Torvalds else 1328d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: NA\n"); 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds if_unlock(t); 13311da177e4SLinus Torvalds 1332d50a6b56SStephen Hemminger return 0; 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds 1335d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file, 1336d50a6b56SStephen Hemminger const char __user *user_buffer, 1337d50a6b56SStephen Hemminger size_t count, loff_t *offset) 13381da177e4SLinus Torvalds { 1339d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *) file->private_data; 1340d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 13411da177e4SLinus Torvalds int i = 0, max, len, ret; 13421da177e4SLinus Torvalds char name[40]; 13431da177e4SLinus Torvalds char *pg_result; 13441da177e4SLinus Torvalds unsigned long value = 0; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds if (count < 1) { 13471da177e4SLinus Torvalds // sprintf(pg_result, "Wrong command format"); 13481da177e4SLinus Torvalds return -EINVAL; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds max = count - i; 13521da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 13531da177e4SLinus Torvalds if (len < 0) 13541da177e4SLinus Torvalds return len; 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds i += len; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds /* Read variable name */ 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 13611da177e4SLinus Torvalds if (len < 0) 13621da177e4SLinus Torvalds return len; 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 13651da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len)) 13661da177e4SLinus Torvalds return -EFAULT; 13671da177e4SLinus Torvalds i += len; 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds max = count -i; 13701da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 13711da177e4SLinus Torvalds if (len < 0) 13721da177e4SLinus Torvalds return len; 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds i += len; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds if (debug) 1377d50a6b56SStephen Hemminger printk("pktgen: t=%s, count=%lu\n", name, 1378d50a6b56SStephen Hemminger (unsigned long) count); 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds if(!t) { 13811da177e4SLinus Torvalds printk("pktgen: ERROR: No thread\n"); 13821da177e4SLinus Torvalds ret = -EINVAL; 13831da177e4SLinus Torvalds goto out; 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds pg_result = &(t->result[0]); 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds if (!strcmp(name, "add_device")) { 13891da177e4SLinus Torvalds char f[32]; 13901da177e4SLinus Torvalds memset(f, 0, 32); 13911da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 13921da177e4SLinus Torvalds if (len < 0) { 13931da177e4SLinus Torvalds ret = len; 13941da177e4SLinus Torvalds goto out; 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds if( copy_from_user(f, &user_buffer[i], len) ) 13971da177e4SLinus Torvalds return -EFAULT; 13981da177e4SLinus Torvalds i += len; 13991da177e4SLinus Torvalds thread_lock(); 14001da177e4SLinus Torvalds pktgen_add_device(t, f); 14011da177e4SLinus Torvalds thread_unlock(); 14021da177e4SLinus Torvalds ret = count; 14031da177e4SLinus Torvalds sprintf(pg_result, "OK: add_device=%s", f); 14041da177e4SLinus Torvalds goto out; 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds if (!strcmp(name, "rem_device_all")) { 14081da177e4SLinus Torvalds thread_lock(); 14091da177e4SLinus Torvalds t->control |= T_REMDEV; 14101da177e4SLinus Torvalds thread_unlock(); 1411121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 14121da177e4SLinus Torvalds ret = count; 14131da177e4SLinus Torvalds sprintf(pg_result, "OK: rem_device_all"); 14141da177e4SLinus Torvalds goto out; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds if (!strcmp(name, "max_before_softirq")) { 14181da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 14191da177e4SLinus Torvalds thread_lock(); 14201da177e4SLinus Torvalds t->max_before_softirq = value; 14211da177e4SLinus Torvalds thread_unlock(); 14221da177e4SLinus Torvalds ret = count; 14231da177e4SLinus Torvalds sprintf(pg_result, "OK: max_before_softirq=%lu", value); 14241da177e4SLinus Torvalds goto out; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds ret = -EINVAL; 14281da177e4SLinus Torvalds out: 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds return ret; 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds 1433d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file) 14341da177e4SLinus Torvalds { 1435d50a6b56SStephen Hemminger return single_open(file, pktgen_thread_show, PDE(inode)->data); 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 1438d50a6b56SStephen Hemminger static struct file_operations pktgen_thread_fops = { 1439d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1440d50a6b56SStephen Hemminger .open = pktgen_thread_open, 1441d50a6b56SStephen Hemminger .read = seq_read, 1442d50a6b56SStephen Hemminger .llseek = seq_lseek, 1443d50a6b56SStephen Hemminger .write = pktgen_thread_write, 1444d50a6b56SStephen Hemminger .release = single_release, 1445d50a6b56SStephen Hemminger }; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds /* Think find or remove for NN */ 14481da177e4SLinus Torvalds static struct pktgen_dev *__pktgen_NN_threads(const char* ifname, int remove) 14491da177e4SLinus Torvalds { 14501da177e4SLinus Torvalds struct pktgen_thread *t; 14511da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds t = pktgen_threads; 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds while (t) { 14561da177e4SLinus Torvalds pkt_dev = pktgen_find_dev(t, ifname); 14571da177e4SLinus Torvalds if (pkt_dev) { 14581da177e4SLinus Torvalds if(remove) { 14591da177e4SLinus Torvalds if_lock(t); 14601da177e4SLinus Torvalds pktgen_remove_device(t, pkt_dev); 14611da177e4SLinus Torvalds if_unlock(t); 14621da177e4SLinus Torvalds } 14631da177e4SLinus Torvalds break; 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds t = t->next; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds return pkt_dev; 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds static struct pktgen_dev *pktgen_NN_threads(const char* ifname, int remove) 14711da177e4SLinus Torvalds { 14721da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 14731da177e4SLinus Torvalds thread_lock(); 14741da177e4SLinus Torvalds pkt_dev = __pktgen_NN_threads(ifname, remove); 14751da177e4SLinus Torvalds thread_unlock(); 14761da177e4SLinus Torvalds return pkt_dev; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *unused, unsigned long event, void *ptr) 14801da177e4SLinus Torvalds { 14811da177e4SLinus Torvalds struct net_device *dev = (struct net_device *)(ptr); 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds /* It is OK that we do not hold the group lock right now, 14841da177e4SLinus Torvalds * as we run under the RTNL lock. 14851da177e4SLinus Torvalds */ 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds switch (event) { 14881da177e4SLinus Torvalds case NETDEV_CHANGEADDR: 14891da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 14901da177e4SLinus Torvalds case NETDEV_DOWN: 14911da177e4SLinus Torvalds case NETDEV_UP: 14921da177e4SLinus Torvalds /* Ignore for now */ 14931da177e4SLinus Torvalds break; 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds case NETDEV_UNREGISTER: 14961da177e4SLinus Torvalds pktgen_NN_threads(dev->name, REMOVE); 14971da177e4SLinus Torvalds break; 14981da177e4SLinus Torvalds }; 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds return NOTIFY_DONE; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */ 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds static struct net_device* pktgen_setup_dev(struct pktgen_dev *pkt_dev) { 15061da177e4SLinus Torvalds struct net_device *odev; 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds /* Clean old setups */ 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds if (pkt_dev->odev) { 15111da177e4SLinus Torvalds dev_put(pkt_dev->odev); 15121da177e4SLinus Torvalds pkt_dev->odev = NULL; 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds odev = dev_get_by_name(pkt_dev->ifname); 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds if (!odev) { 15181da177e4SLinus Torvalds printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname); 15191da177e4SLinus Torvalds goto out; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds if (odev->type != ARPHRD_ETHER) { 15221da177e4SLinus Torvalds printk("pktgen: not an ethernet device: \"%s\"\n", pkt_dev->ifname); 15231da177e4SLinus Torvalds goto out_put; 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds if (!netif_running(odev)) { 15261da177e4SLinus Torvalds printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname); 15271da177e4SLinus Torvalds goto out_put; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds pkt_dev->odev = odev; 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds return pkt_dev->odev; 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds out_put: 15341da177e4SLinus Torvalds dev_put(odev); 15351da177e4SLinus Torvalds out: 15361da177e4SLinus Torvalds return NULL; 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev 15411da177e4SLinus Torvalds * structure to have the right information to create/send packets 15421da177e4SLinus Torvalds */ 15431da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev) 15441da177e4SLinus Torvalds { 15451da177e4SLinus Torvalds /* Try once more, just in case it works now. */ 15461da177e4SLinus Torvalds if (!pkt_dev->odev) 15471da177e4SLinus Torvalds pktgen_setup_dev(pkt_dev); 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds if (!pkt_dev->odev) { 15501da177e4SLinus Torvalds printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n"); 15511da177e4SLinus Torvalds sprintf(pkt_dev->result, "ERROR: pkt_dev->odev == NULL in setup_inject.\n"); 15521da177e4SLinus Torvalds return; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds /* Default to the interface's mac if not explicitly set. */ 15561da177e4SLinus Torvalds 1557f404e9a6SKris Katterjohn if (is_zero_ether_addr(pkt_dev->src_mac)) 1558f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN); 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds /* Set up Dest MAC */ 1561f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds /* Set up pkt size */ 15641da177e4SLinus Torvalds pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds if(pkt_dev->flags & F_IPV6) { 15671da177e4SLinus Torvalds /* 15681da177e4SLinus Torvalds * Skip this automatic address setting until locks or functions 15691da177e4SLinus Torvalds * gets exported 15701da177e4SLinus Torvalds */ 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds #ifdef NOTNOW 15731da177e4SLinus Torvalds int i, set = 0, err=1; 15741da177e4SLinus Torvalds struct inet6_dev *idev; 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds for(i=0; i< IN6_ADDR_HSIZE; i++) 15771da177e4SLinus Torvalds if(pkt_dev->cur_in6_saddr.s6_addr[i]) { 15781da177e4SLinus Torvalds set = 1; 15791da177e4SLinus Torvalds break; 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds if(!set) { 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds /* 15851da177e4SLinus Torvalds * Use linklevel address if unconfigured. 15861da177e4SLinus Torvalds * 15871da177e4SLinus Torvalds * use ipv6_get_lladdr if/when it's get exported 15881da177e4SLinus Torvalds */ 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds read_lock(&addrconf_lock); 15921da177e4SLinus Torvalds if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) { 15931da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds read_lock_bh(&idev->lock); 15961da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { 15971da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 15981da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &ifp->addr); 15991da177e4SLinus Torvalds err = 0; 16001da177e4SLinus Torvalds break; 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds read_unlock(&addrconf_lock); 16061da177e4SLinus Torvalds if(err) printk("pktgen: ERROR: IPv6 link address not availble.\n"); 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds #endif 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds else { 16111da177e4SLinus Torvalds pkt_dev->saddr_min = 0; 16121da177e4SLinus Torvalds pkt_dev->saddr_max = 0; 16131da177e4SLinus Torvalds if (strlen(pkt_dev->src_min) == 0) { 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds struct in_device *in_dev; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds rcu_read_lock(); 1618e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(pkt_dev->odev); 16191da177e4SLinus Torvalds if (in_dev) { 16201da177e4SLinus Torvalds if (in_dev->ifa_list) { 16211da177e4SLinus Torvalds pkt_dev->saddr_min = in_dev->ifa_list->ifa_address; 16221da177e4SLinus Torvalds pkt_dev->saddr_max = pkt_dev->saddr_min; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds rcu_read_unlock(); 16261da177e4SLinus Torvalds } 16271da177e4SLinus Torvalds else { 16281da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 16291da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 16331da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds /* Initialize current values. */ 16361da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 16371da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 16381da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 16391da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 16401da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 16411da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 16421da177e4SLinus Torvalds pkt_dev->nflows = 0; 16431da177e4SLinus Torvalds } 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us) 16461da177e4SLinus Torvalds { 16471da177e4SLinus Torvalds __u64 start; 16481da177e4SLinus Torvalds __u64 now; 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds start = now = getCurUs(); 16511da177e4SLinus Torvalds printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now)); 16521da177e4SLinus Torvalds while (now < spin_until_us) { 1653b4099fabSStephen Hemminger /* TODO: optimize sleeping behavior */ 1654121caf57SNishanth Aravamudan if (spin_until_us - now > jiffies_to_usecs(1)+1) 1655121caf57SNishanth Aravamudan schedule_timeout_interruptible(1); 1656121caf57SNishanth Aravamudan else if (spin_until_us - now > 100) { 16571da177e4SLinus Torvalds do_softirq(); 16581da177e4SLinus Torvalds if (!pkt_dev->running) 16591da177e4SLinus Torvalds return; 16601da177e4SLinus Torvalds if (need_resched()) 16611da177e4SLinus Torvalds schedule(); 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds now = getCurUs(); 16651da177e4SLinus Torvalds } 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds pkt_dev->idle_acc += now - start; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values 16721da177e4SLinus Torvalds * for IP src/dest, UDP src/dst port, MAC-Addr src/dst 16731da177e4SLinus Torvalds */ 16741da177e4SLinus Torvalds static void mod_cur_headers(struct pktgen_dev *pkt_dev) { 16751da177e4SLinus Torvalds __u32 imn; 16761da177e4SLinus Torvalds __u32 imx; 16771da177e4SLinus Torvalds int flow = 0; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds if(pkt_dev->cflows) { 16801da177e4SLinus Torvalds flow = pktgen_random() % pkt_dev->cflows; 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds if (pkt_dev->flows[flow].count > pkt_dev->lflow) 16831da177e4SLinus Torvalds pkt_dev->flows[flow].count = 0; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds /* Deal with source MAC */ 16881da177e4SLinus Torvalds if (pkt_dev->src_mac_count > 1) { 16891da177e4SLinus Torvalds __u32 mc; 16901da177e4SLinus Torvalds __u32 tmp; 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 16931da177e4SLinus Torvalds mc = pktgen_random() % (pkt_dev->src_mac_count); 16941da177e4SLinus Torvalds else { 16951da177e4SLinus Torvalds mc = pkt_dev->cur_src_mac_offset++; 16961da177e4SLinus Torvalds if (pkt_dev->cur_src_mac_offset > pkt_dev->src_mac_count) 16971da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds tmp = pkt_dev->src_mac[5] + (mc & 0xFF); 17011da177e4SLinus Torvalds pkt_dev->hh[11] = tmp; 17021da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 17031da177e4SLinus Torvalds pkt_dev->hh[10] = tmp; 17041da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 17051da177e4SLinus Torvalds pkt_dev->hh[9] = tmp; 17061da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 17071da177e4SLinus Torvalds pkt_dev->hh[8] = tmp; 17081da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[1] + (tmp >> 8)); 17091da177e4SLinus Torvalds pkt_dev->hh[7] = tmp; 17101da177e4SLinus Torvalds } 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds /* Deal with Destination MAC */ 17131da177e4SLinus Torvalds if (pkt_dev->dst_mac_count > 1) { 17141da177e4SLinus Torvalds __u32 mc; 17151da177e4SLinus Torvalds __u32 tmp; 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 17181da177e4SLinus Torvalds mc = pktgen_random() % (pkt_dev->dst_mac_count); 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds else { 17211da177e4SLinus Torvalds mc = pkt_dev->cur_dst_mac_offset++; 17221da177e4SLinus Torvalds if (pkt_dev->cur_dst_mac_offset > pkt_dev->dst_mac_count) { 17231da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds } 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds tmp = pkt_dev->dst_mac[5] + (mc & 0xFF); 17281da177e4SLinus Torvalds pkt_dev->hh[5] = tmp; 17291da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 17301da177e4SLinus Torvalds pkt_dev->hh[4] = tmp; 17311da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 17321da177e4SLinus Torvalds pkt_dev->hh[3] = tmp; 17331da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 17341da177e4SLinus Torvalds pkt_dev->hh[2] = tmp; 17351da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[1] + (tmp >> 8)); 17361da177e4SLinus Torvalds pkt_dev->hh[1] = tmp; 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) { 17401da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 17411da177e4SLinus Torvalds pkt_dev->cur_udp_src = ((pktgen_random() % (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) + pkt_dev->udp_src_min); 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds else { 17441da177e4SLinus Torvalds pkt_dev->cur_udp_src++; 17451da177e4SLinus Torvalds if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max) 17461da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds } 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) { 17511da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) { 17521da177e4SLinus Torvalds pkt_dev->cur_udp_dst = ((pktgen_random() % (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) + pkt_dev->udp_dst_min); 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds else { 17551da177e4SLinus Torvalds pkt_dev->cur_udp_dst++; 17561da177e4SLinus Torvalds if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max) 17571da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds if (!(pkt_dev->flags & F_IPV6)) { 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds if ((imn = ntohl(pkt_dev->saddr_min)) < (imx = ntohl(pkt_dev->saddr_max))) { 17641da177e4SLinus Torvalds __u32 t; 17651da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 17661da177e4SLinus Torvalds t = ((pktgen_random() % (imx - imn)) + imn); 17671da177e4SLinus Torvalds else { 17681da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_saddr); 17691da177e4SLinus Torvalds t++; 17701da177e4SLinus Torvalds if (t > imx) { 17711da177e4SLinus Torvalds t = imn; 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds pkt_dev->cur_saddr = htonl(t); 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) { 17781da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr; 17791da177e4SLinus Torvalds } else { 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds if ((imn = ntohl(pkt_dev->daddr_min)) < (imx = ntohl(pkt_dev->daddr_max))) { 17821da177e4SLinus Torvalds __u32 t; 17831da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) { 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds t = ((pktgen_random() % (imx - imn)) + imn); 17861da177e4SLinus Torvalds t = htonl(t); 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds while( LOOPBACK(t) || MULTICAST(t) || BADCLASS(t) || ZERONET(t) || LOCAL_MCAST(t) ) { 17891da177e4SLinus Torvalds t = ((pktgen_random() % (imx - imn)) + imn); 17901da177e4SLinus Torvalds t = htonl(t); 17911da177e4SLinus Torvalds } 17921da177e4SLinus Torvalds pkt_dev->cur_daddr = t; 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds else { 17961da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_daddr); 17971da177e4SLinus Torvalds t++; 17981da177e4SLinus Torvalds if (t > imx) { 17991da177e4SLinus Torvalds t = imn; 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds pkt_dev->cur_daddr = htonl(t); 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds } 18041da177e4SLinus Torvalds if(pkt_dev->cflows) { 18051da177e4SLinus Torvalds pkt_dev->flows[flow].cur_daddr = pkt_dev->cur_daddr; 18061da177e4SLinus Torvalds pkt_dev->nflows++; 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds else /* IPV6 * */ 18111da177e4SLinus Torvalds { 18121da177e4SLinus Torvalds if(pkt_dev->min_in6_daddr.s6_addr32[0] == 0 && 18131da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[1] == 0 && 18141da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[2] == 0 && 18151da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[3] == 0); 18161da177e4SLinus Torvalds else { 18171da177e4SLinus Torvalds int i; 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds /* Only random destinations yet */ 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds for(i=0; i < 4; i++) { 18221da177e4SLinus Torvalds pkt_dev->cur_in6_daddr.s6_addr32[i] = 18231da177e4SLinus Torvalds ((pktgen_random() | 18241da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[i]) & 18251da177e4SLinus Torvalds pkt_dev->max_in6_daddr.s6_addr32[i]); 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) { 18311da177e4SLinus Torvalds __u32 t; 18321da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) { 18331da177e4SLinus Torvalds t = ((pktgen_random() % (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)) 18341da177e4SLinus Torvalds + pkt_dev->min_pkt_size); 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds else { 18371da177e4SLinus Torvalds t = pkt_dev->cur_pkt_size + 1; 18381da177e4SLinus Torvalds if (t > pkt_dev->max_pkt_size) 18391da177e4SLinus Torvalds t = pkt_dev->min_pkt_size; 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds pkt_dev->cur_pkt_size = t; 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds pkt_dev->flows[flow].count++; 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev, 18491da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 18501da177e4SLinus Torvalds { 18511da177e4SLinus Torvalds struct sk_buff *skb = NULL; 18521da177e4SLinus Torvalds __u8 *eth; 18531da177e4SLinus Torvalds struct udphdr *udph; 18541da177e4SLinus Torvalds int datalen, iplen; 18551da177e4SLinus Torvalds struct iphdr *iph; 18561da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 18571da177e4SLinus Torvalds 185864053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 185964053beeSRobert Olsson * fields. 186064053beeSRobert Olsson */ 186164053beeSRobert Olsson mod_cur_headers(pkt_dev); 186264053beeSRobert Olsson 1863*7ac5459eSDavid S. Miller datalen = (odev->hard_header_len + 16) & ~0xf; 1864*7ac5459eSDavid S. Miller skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen, GFP_ATOMIC); 18651da177e4SLinus Torvalds if (!skb) { 18661da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 18671da177e4SLinus Torvalds return NULL; 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 1870*7ac5459eSDavid S. Miller skb_reserve(skb, datalen); 18711da177e4SLinus Torvalds 18721da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 18731da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 18741da177e4SLinus Torvalds iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)); 18751da177e4SLinus Torvalds udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr)); 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 18781da177e4SLinus Torvalds *(u16*)ð[12] = __constant_htons(ETH_P_IP); 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8; /* Eth + IPh + UDPh */ 18811da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) 18821da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 18851da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 18861da177e4SLinus Torvalds udph->len = htons(datalen + 8); /* DATA + udphdr */ 18871da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds iph->ihl = 5; 18901da177e4SLinus Torvalds iph->version = 4; 18911da177e4SLinus Torvalds iph->ttl = 32; 18921da177e4SLinus Torvalds iph->tos = 0; 18931da177e4SLinus Torvalds iph->protocol = IPPROTO_UDP; /* UDP */ 18941da177e4SLinus Torvalds iph->saddr = pkt_dev->cur_saddr; 18951da177e4SLinus Torvalds iph->daddr = pkt_dev->cur_daddr; 18961da177e4SLinus Torvalds iph->frag_off = 0; 18971da177e4SLinus Torvalds iplen = 20 + 8 + datalen; 18981da177e4SLinus Torvalds iph->tot_len = htons(iplen); 18991da177e4SLinus Torvalds iph->check = 0; 19001da177e4SLinus Torvalds iph->check = ip_fast_csum((void *) iph, iph->ihl); 19011da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_IP); 19021da177e4SLinus Torvalds skb->mac.raw = ((u8 *)iph) - 14; 19031da177e4SLinus Torvalds skb->dev = odev; 19041da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 19071da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 19081da177e4SLinus Torvalds else { 19091da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 19101da177e4SLinus Torvalds int i; 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8); 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 19151da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 19161da177e4SLinus Torvalds if (datalen > frags*PAGE_SIZE) { 19171da177e4SLinus Torvalds skb_put(skb, datalen-frags*PAGE_SIZE); 19181da177e4SLinus Torvalds datalen = frags*PAGE_SIZE; 19191da177e4SLinus Torvalds } 19201da177e4SLinus Torvalds 19211da177e4SLinus Torvalds i = 0; 19221da177e4SLinus Torvalds while (datalen > 0) { 19231da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 19241da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 19251da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 19261da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 19271da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 19281da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 19291da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 19301da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 19311da177e4SLinus Torvalds i++; 19321da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds while (i < frags) { 19361da177e4SLinus Torvalds int rem; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds if (i == 0) 19391da177e4SLinus Torvalds break; 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 19421da177e4SLinus Torvalds if (rem == 0) 19431da177e4SLinus Torvalds break; 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1]; 19481da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 19491da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page; 19501da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size; 19511da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 19521da177e4SLinus Torvalds i++; 19531da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds if (pgh) { 19601da177e4SLinus Torvalds struct timeval timestamp; 19611da177e4SLinus Torvalds 19621da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 19631da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds do_gettimeofday(×tamp); 19661da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 19671da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds pkt_dev->seq_num++; 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds return skb; 19721da177e4SLinus Torvalds } 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds /* 19751da177e4SLinus Torvalds * scan_ip6, fmt_ip taken from dietlibc-0.21 19761da177e4SLinus Torvalds * Author Felix von Leitner <felix-dietlibc@fefe.de> 19771da177e4SLinus Torvalds * 19781da177e4SLinus Torvalds * Slightly modified for kernel. 19791da177e4SLinus Torvalds * Should be candidate for net/ipv4/utils.c 19801da177e4SLinus Torvalds * --ro 19811da177e4SLinus Torvalds */ 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s,char ip[16]) 19841da177e4SLinus Torvalds { 19851da177e4SLinus Torvalds unsigned int i; 19861da177e4SLinus Torvalds unsigned int len=0; 19871da177e4SLinus Torvalds unsigned long u; 19881da177e4SLinus Torvalds char suffix[16]; 19891da177e4SLinus Torvalds unsigned int prefixlen=0; 19901da177e4SLinus Torvalds unsigned int suffixlen=0; 19911da177e4SLinus Torvalds __u32 tmp; 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds for (i=0; i<16; i++) ip[i]=0; 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds for (;;) { 19961da177e4SLinus Torvalds if (*s == ':') { 19971da177e4SLinus Torvalds len++; 19981da177e4SLinus Torvalds if (s[1] == ':') { /* Found "::", skip to part 2 */ 19991da177e4SLinus Torvalds s+=2; 20001da177e4SLinus Torvalds len++; 20011da177e4SLinus Torvalds break; 20021da177e4SLinus Torvalds } 20031da177e4SLinus Torvalds s++; 20041da177e4SLinus Torvalds } 20051da177e4SLinus Torvalds { 20061da177e4SLinus Torvalds char *tmp; 20071da177e4SLinus Torvalds u=simple_strtoul(s,&tmp,16); 20081da177e4SLinus Torvalds i=tmp-s; 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds if (!i) return 0; 20121da177e4SLinus Torvalds if (prefixlen==12 && s[i]=='.') { 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds /* the last 4 bytes may be written as IPv4 address */ 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds tmp = in_aton(s); 20171da177e4SLinus Torvalds memcpy((struct in_addr*)(ip+12), &tmp, sizeof(tmp)); 20181da177e4SLinus Torvalds return i+len; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds ip[prefixlen++] = (u >> 8); 20211da177e4SLinus Torvalds ip[prefixlen++] = (u & 255); 20221da177e4SLinus Torvalds s += i; len += i; 20231da177e4SLinus Torvalds if (prefixlen==16) 20241da177e4SLinus Torvalds return len; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds /* part 2, after "::" */ 20281da177e4SLinus Torvalds for (;;) { 20291da177e4SLinus Torvalds if (*s == ':') { 20301da177e4SLinus Torvalds if (suffixlen==0) 20311da177e4SLinus Torvalds break; 20321da177e4SLinus Torvalds s++; 20331da177e4SLinus Torvalds len++; 20341da177e4SLinus Torvalds } else if (suffixlen!=0) 20351da177e4SLinus Torvalds break; 20361da177e4SLinus Torvalds { 20371da177e4SLinus Torvalds char *tmp; 20381da177e4SLinus Torvalds u=simple_strtol(s,&tmp,16); 20391da177e4SLinus Torvalds i=tmp-s; 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds if (!i) { 20421da177e4SLinus Torvalds if (*s) len--; 20431da177e4SLinus Torvalds break; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds if (suffixlen+prefixlen<=12 && s[i]=='.') { 20461da177e4SLinus Torvalds tmp = in_aton(s); 20471da177e4SLinus Torvalds memcpy((struct in_addr*)(suffix+suffixlen), &tmp, sizeof(tmp)); 20481da177e4SLinus Torvalds suffixlen+=4; 20491da177e4SLinus Torvalds len+=strlen(s); 20501da177e4SLinus Torvalds break; 20511da177e4SLinus Torvalds } 20521da177e4SLinus Torvalds suffix[suffixlen++] = (u >> 8); 20531da177e4SLinus Torvalds suffix[suffixlen++] = (u & 255); 20541da177e4SLinus Torvalds s += i; len += i; 20551da177e4SLinus Torvalds if (prefixlen+suffixlen==16) 20561da177e4SLinus Torvalds break; 20571da177e4SLinus Torvalds } 20581da177e4SLinus Torvalds for (i=0; i<suffixlen; i++) 20591da177e4SLinus Torvalds ip[16-suffixlen+i] = suffix[i]; 20601da177e4SLinus Torvalds return len; 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds static char tohex(char hexdigit) { 20641da177e4SLinus Torvalds return hexdigit>9?hexdigit+'a'-10:hexdigit+'0'; 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds static int fmt_xlong(char* s,unsigned int i) { 20681da177e4SLinus Torvalds char* bak=s; 20691da177e4SLinus Torvalds *s=tohex((i>>12)&0xf); if (s!=bak || *s!='0') ++s; 20701da177e4SLinus Torvalds *s=tohex((i>>8)&0xf); if (s!=bak || *s!='0') ++s; 20711da177e4SLinus Torvalds *s=tohex((i>>4)&0xf); if (s!=bak || *s!='0') ++s; 20721da177e4SLinus Torvalds *s=tohex(i&0xf); 20731da177e4SLinus Torvalds return s-bak+1; 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds static unsigned int fmt_ip6(char *s,const char ip[16]) { 20771da177e4SLinus Torvalds unsigned int len; 20781da177e4SLinus Torvalds unsigned int i; 20791da177e4SLinus Torvalds unsigned int temp; 20801da177e4SLinus Torvalds unsigned int compressing; 20811da177e4SLinus Torvalds int j; 20821da177e4SLinus Torvalds 20831da177e4SLinus Torvalds len = 0; compressing = 0; 20841da177e4SLinus Torvalds for (j=0; j<16; j+=2) { 20851da177e4SLinus Torvalds 20861da177e4SLinus Torvalds #ifdef V4MAPPEDPREFIX 20871da177e4SLinus Torvalds if (j==12 && !memcmp(ip,V4mappedprefix,12)) { 20881da177e4SLinus Torvalds inet_ntoa_r(*(struct in_addr*)(ip+12),s); 20891da177e4SLinus Torvalds temp=strlen(s); 20901da177e4SLinus Torvalds return len+temp; 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds #endif 20931da177e4SLinus Torvalds temp = ((unsigned long) (unsigned char) ip[j] << 8) + 20941da177e4SLinus Torvalds (unsigned long) (unsigned char) ip[j+1]; 20951da177e4SLinus Torvalds if (temp == 0) { 20961da177e4SLinus Torvalds if (!compressing) { 20971da177e4SLinus Torvalds compressing=1; 20981da177e4SLinus Torvalds if (j==0) { 20991da177e4SLinus Torvalds *s++=':'; ++len; 21001da177e4SLinus Torvalds } 21011da177e4SLinus Torvalds } 21021da177e4SLinus Torvalds } else { 21031da177e4SLinus Torvalds if (compressing) { 21041da177e4SLinus Torvalds compressing=0; 21051da177e4SLinus Torvalds *s++=':'; ++len; 21061da177e4SLinus Torvalds } 21071da177e4SLinus Torvalds i = fmt_xlong(s,temp); len += i; s += i; 21081da177e4SLinus Torvalds if (j<14) { 21091da177e4SLinus Torvalds *s++ = ':'; 21101da177e4SLinus Torvalds ++len; 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds } 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds if (compressing) { 21151da177e4SLinus Torvalds *s++=':'; ++len; 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds *s=0; 21181da177e4SLinus Torvalds return len; 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev, 21221da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 21231da177e4SLinus Torvalds { 21241da177e4SLinus Torvalds struct sk_buff *skb = NULL; 21251da177e4SLinus Torvalds __u8 *eth; 21261da177e4SLinus Torvalds struct udphdr *udph; 21271da177e4SLinus Torvalds int datalen; 21281da177e4SLinus Torvalds struct ipv6hdr *iph; 21291da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 21301da177e4SLinus Torvalds 213164053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 213264053beeSRobert Olsson * fields. 213364053beeSRobert Olsson */ 213464053beeSRobert Olsson mod_cur_headers(pkt_dev); 213564053beeSRobert Olsson 21361da177e4SLinus Torvalds skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC); 21371da177e4SLinus Torvalds if (!skb) { 21381da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 21391da177e4SLinus Torvalds return NULL; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds skb_reserve(skb, 16); 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 21451da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 21461da177e4SLinus Torvalds iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr)); 21471da177e4SLinus Torvalds udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr)); 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 21501da177e4SLinus Torvalds *(u16*)ð[12] = __constant_htons(ETH_P_IPV6); 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds datalen = pkt_dev->cur_pkt_size-14- 21531da177e4SLinus Torvalds sizeof(struct ipv6hdr)-sizeof(struct udphdr); /* Eth + IPh + UDPh */ 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) { 21561da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 21571da177e4SLinus Torvalds if (net_ratelimit()) 21581da177e4SLinus Torvalds printk(KERN_INFO "pktgen: increased datalen to %d\n", datalen); 21591da177e4SLinus Torvalds } 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 21621da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 21631da177e4SLinus Torvalds udph->len = htons(datalen + sizeof(struct udphdr)); 21641da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds *(u32*)iph = __constant_htonl(0x60000000); /* Version + flow */ 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds iph->hop_limit = 32; 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds iph->payload_len = htons(sizeof(struct udphdr) + datalen); 21711da177e4SLinus Torvalds iph->nexthdr = IPPROTO_UDP; 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr); 21741da177e4SLinus Torvalds ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr); 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds skb->mac.raw = ((u8 *)iph) - 14; 21771da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_IPV6); 21781da177e4SLinus Torvalds skb->dev = odev; 21791da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 21821da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 21831da177e4SLinus Torvalds else { 21841da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 21851da177e4SLinus Torvalds int i; 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8); 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 21901da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 21911da177e4SLinus Torvalds if (datalen > frags*PAGE_SIZE) { 21921da177e4SLinus Torvalds skb_put(skb, datalen-frags*PAGE_SIZE); 21931da177e4SLinus Torvalds datalen = frags*PAGE_SIZE; 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds i = 0; 21971da177e4SLinus Torvalds while (datalen > 0) { 21981da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 21991da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 22001da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 22011da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 22021da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 22031da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 22041da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 22051da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 22061da177e4SLinus Torvalds i++; 22071da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds while (i < frags) { 22111da177e4SLinus Torvalds int rem; 22121da177e4SLinus Torvalds 22131da177e4SLinus Torvalds if (i == 0) 22141da177e4SLinus Torvalds break; 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 22171da177e4SLinus Torvalds if (rem == 0) 22181da177e4SLinus Torvalds break; 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 22211da177e4SLinus Torvalds 22221da177e4SLinus Torvalds skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1]; 22231da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 22241da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page; 22251da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size; 22261da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 22271da177e4SLinus Torvalds i++; 22281da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 22291da177e4SLinus Torvalds } 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 22331da177e4SLinus Torvalds /* should we update cloned packets too ? */ 22341da177e4SLinus Torvalds if (pgh) { 22351da177e4SLinus Torvalds struct timeval timestamp; 22361da177e4SLinus Torvalds 22371da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 22381da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds do_gettimeofday(×tamp); 22411da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 22421da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds pkt_dev->seq_num++; 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds return skb; 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 22491da177e4SLinus Torvalds static inline struct sk_buff *fill_packet(struct net_device *odev, 22501da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 22511da177e4SLinus Torvalds { 22521da177e4SLinus Torvalds if(pkt_dev->flags & F_IPV6) 22531da177e4SLinus Torvalds return fill_packet_ipv6(odev, pkt_dev); 22541da177e4SLinus Torvalds else 22551da177e4SLinus Torvalds return fill_packet_ipv4(odev, pkt_dev); 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev) 22591da177e4SLinus Torvalds { 22601da177e4SLinus Torvalds pkt_dev->seq_num = 1; 22611da177e4SLinus Torvalds pkt_dev->idle_acc = 0; 22621da177e4SLinus Torvalds pkt_dev->sofar = 0; 22631da177e4SLinus Torvalds pkt_dev->tx_bytes = 0; 22641da177e4SLinus Torvalds pkt_dev->errors = 0; 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds 22671da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */ 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t) 22701da177e4SLinus Torvalds { 22711da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 22721da177e4SLinus Torvalds int started = 0; 22731da177e4SLinus Torvalds 22741da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t)); 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds if_lock(t); 22771da177e4SLinus Torvalds for (pkt_dev = t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) { 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds /* 22801da177e4SLinus Torvalds * setup odev and create initial packet. 22811da177e4SLinus Torvalds */ 22821da177e4SLinus Torvalds pktgen_setup_inject(pkt_dev); 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds if(pkt_dev->odev) { 22851da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 22861da177e4SLinus Torvalds pkt_dev->running = 1; /* Cranke yeself! */ 22871da177e4SLinus Torvalds pkt_dev->skb = NULL; 22881da177e4SLinus Torvalds pkt_dev->started_at = getCurUs(); 22891da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */ 22901da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 22911da177e4SLinus Torvalds 22921da177e4SLinus Torvalds strcpy(pkt_dev->result, "Starting"); 22931da177e4SLinus Torvalds started++; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds else 22961da177e4SLinus Torvalds strcpy(pkt_dev->result, "Error starting"); 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds if_unlock(t); 22991da177e4SLinus Torvalds if(started) t->control &= ~(T_STOP); 23001da177e4SLinus Torvalds } 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void) 23031da177e4SLinus Torvalds { 23041da177e4SLinus Torvalds struct pktgen_thread *t = pktgen_threads; 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads.\n")); 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds thread_lock(); 23091da177e4SLinus Torvalds while(t) { 23101da177e4SLinus Torvalds pktgen_stop(t); 23111da177e4SLinus Torvalds t = t->next; 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds thread_unlock(); 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds static int thread_is_running(struct pktgen_thread *t ) 23171da177e4SLinus Torvalds { 23181da177e4SLinus Torvalds struct pktgen_dev *next; 23191da177e4SLinus Torvalds int res = 0; 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds for(next=t->if_list; next; next=next->next) { 23221da177e4SLinus Torvalds if(next->running) { 23231da177e4SLinus Torvalds res = 1; 23241da177e4SLinus Torvalds break; 23251da177e4SLinus Torvalds } 23261da177e4SLinus Torvalds } 23271da177e4SLinus Torvalds return res; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t ) 23311da177e4SLinus Torvalds { 23321da177e4SLinus Torvalds if_lock(t); 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds while(thread_is_running(t)) { 23351da177e4SLinus Torvalds 23361da177e4SLinus Torvalds if_unlock(t); 23371da177e4SLinus Torvalds 23381da177e4SLinus Torvalds msleep_interruptible(100); 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds if (signal_pending(current)) 23411da177e4SLinus Torvalds goto signal; 23421da177e4SLinus Torvalds if_lock(t); 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds if_unlock(t); 23451da177e4SLinus Torvalds return 1; 23461da177e4SLinus Torvalds signal: 23471da177e4SLinus Torvalds return 0; 23481da177e4SLinus Torvalds } 23491da177e4SLinus Torvalds 23501da177e4SLinus Torvalds static int pktgen_wait_all_threads_run(void) 23511da177e4SLinus Torvalds { 23521da177e4SLinus Torvalds struct pktgen_thread *t = pktgen_threads; 23531da177e4SLinus Torvalds int sig = 1; 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds while (t) { 23561da177e4SLinus Torvalds sig = pktgen_wait_thread_run(t); 23571da177e4SLinus Torvalds if( sig == 0 ) break; 23581da177e4SLinus Torvalds thread_lock(); 23591da177e4SLinus Torvalds t=t->next; 23601da177e4SLinus Torvalds thread_unlock(); 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds if(sig == 0) { 23631da177e4SLinus Torvalds thread_lock(); 23641da177e4SLinus Torvalds while (t) { 23651da177e4SLinus Torvalds t->control |= (T_STOP); 23661da177e4SLinus Torvalds t=t->next; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds thread_unlock(); 23691da177e4SLinus Torvalds } 23701da177e4SLinus Torvalds return sig; 23711da177e4SLinus Torvalds } 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds static void pktgen_run_all_threads(void) 23741da177e4SLinus Torvalds { 23751da177e4SLinus Torvalds struct pktgen_thread *t = pktgen_threads; 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n")); 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds thread_lock(); 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds while(t) { 23821da177e4SLinus Torvalds t->control |= (T_RUN); 23831da177e4SLinus Torvalds t = t->next; 23841da177e4SLinus Torvalds } 23851da177e4SLinus Torvalds thread_unlock(); 23861da177e4SLinus Torvalds 2387121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds pktgen_wait_all_threads_run(); 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags) 23941da177e4SLinus Torvalds { 23951da177e4SLinus Torvalds __u64 total_us, bps, mbps, pps, idle; 23961da177e4SLinus Torvalds char *p = pkt_dev->result; 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds total_us = pkt_dev->stopped_at - pkt_dev->started_at; 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds idle = pkt_dev->idle_acc; 24011da177e4SLinus Torvalds 24021da177e4SLinus Torvalds p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n", 24031da177e4SLinus Torvalds (unsigned long long) total_us, 24041da177e4SLinus Torvalds (unsigned long long)(total_us - idle), 24051da177e4SLinus Torvalds (unsigned long long) idle, 24061da177e4SLinus Torvalds (unsigned long long) pkt_dev->sofar, 24071da177e4SLinus Torvalds pkt_dev->cur_pkt_size, nr_frags); 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds pps = pkt_dev->sofar * USEC_PER_SEC; 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds while ((total_us >> 32) != 0) { 24121da177e4SLinus Torvalds pps >>= 1; 24131da177e4SLinus Torvalds total_us >>= 1; 24141da177e4SLinus Torvalds } 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds do_div(pps, total_us); 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds bps = pps * 8 * pkt_dev->cur_pkt_size; 24191da177e4SLinus Torvalds 24201da177e4SLinus Torvalds mbps = bps; 24211da177e4SLinus Torvalds do_div(mbps, 1000000); 24221da177e4SLinus Torvalds p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu", 24231da177e4SLinus Torvalds (unsigned long long) pps, 24241da177e4SLinus Torvalds (unsigned long long) mbps, 24251da177e4SLinus Torvalds (unsigned long long) bps, 24261da177e4SLinus Torvalds (unsigned long long) pkt_dev->errors); 24271da177e4SLinus Torvalds } 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */ 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev) 24331da177e4SLinus Torvalds { 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds if (!pkt_dev->running) { 24361da177e4SLinus Torvalds printk("pktgen: interface: %s is already stopped\n", pkt_dev->ifname); 24371da177e4SLinus Torvalds return -EINVAL; 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds pkt_dev->stopped_at = getCurUs(); 24411da177e4SLinus Torvalds pkt_dev->running = 0; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds show_results(pkt_dev, skb_shinfo(pkt_dev->skb)->nr_frags); 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds if (pkt_dev->skb) 24461da177e4SLinus Torvalds kfree_skb(pkt_dev->skb); 24471da177e4SLinus Torvalds 24481da177e4SLinus Torvalds pkt_dev->skb = NULL; 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds return 0; 24511da177e4SLinus Torvalds } 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t ) 24541da177e4SLinus Torvalds { 24551da177e4SLinus Torvalds struct pktgen_dev *next, *best = NULL; 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds if_lock(t); 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds for(next=t->if_list; next ; next=next->next) { 24601da177e4SLinus Torvalds if(!next->running) continue; 24611da177e4SLinus Torvalds if(best == NULL) best=next; 24621da177e4SLinus Torvalds else if ( next->next_tx_us < best->next_tx_us) 24631da177e4SLinus Torvalds best = next; 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds if_unlock(t); 24661da177e4SLinus Torvalds return best; 24671da177e4SLinus Torvalds } 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t) { 24701da177e4SLinus Torvalds struct pktgen_dev *next = NULL; 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: entering pktgen_stop.\n")); 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds if_lock(t); 24751da177e4SLinus Torvalds 24761da177e4SLinus Torvalds for(next=t->if_list; next; next=next->next) 24771da177e4SLinus Torvalds pktgen_stop_device(next); 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds if_unlock(t); 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t) 24831da177e4SLinus Torvalds { 24841da177e4SLinus Torvalds struct pktgen_dev *cur, *next = NULL; 24851da177e4SLinus Torvalds 24861da177e4SLinus Torvalds /* Remove all devices, free mem */ 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds if_lock(t); 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds for(cur=t->if_list; cur; cur=next) { 24911da177e4SLinus Torvalds next = cur->next; 24921da177e4SLinus Torvalds pktgen_remove_device(t, cur); 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds 24951da177e4SLinus Torvalds if_unlock(t); 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t) 24991da177e4SLinus Torvalds { 25001da177e4SLinus Torvalds /* Remove from the thread list */ 25011da177e4SLinus Torvalds 25021da177e4SLinus Torvalds struct pktgen_thread *tmp = pktgen_threads; 25031da177e4SLinus Torvalds 2504d50a6b56SStephen Hemminger remove_proc_entry(t->name, pg_proc_dir); 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds thread_lock(); 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds if (tmp == t) 25091da177e4SLinus Torvalds pktgen_threads = tmp->next; 25101da177e4SLinus Torvalds else { 25111da177e4SLinus Torvalds while (tmp) { 25121da177e4SLinus Torvalds if (tmp->next == t) { 25131da177e4SLinus Torvalds tmp->next = t->next; 25141da177e4SLinus Torvalds t->next = NULL; 25151da177e4SLinus Torvalds break; 25161da177e4SLinus Torvalds } 25171da177e4SLinus Torvalds tmp = tmp->next; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds thread_unlock(); 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev) 25241da177e4SLinus Torvalds { 25251da177e4SLinus Torvalds struct net_device *odev = NULL; 25261da177e4SLinus Torvalds __u64 idle_start = 0; 25271da177e4SLinus Torvalds int ret; 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds odev = pkt_dev->odev; 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds if (pkt_dev->delay_us || pkt_dev->delay_ns) { 25321da177e4SLinus Torvalds u64 now; 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds now = getCurUs(); 25351da177e4SLinus Torvalds if (now < pkt_dev->next_tx_us) 25361da177e4SLinus Torvalds spin(pkt_dev, pkt_dev->next_tx_us); 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds /* This is max DELAY, this has special meaning of 25391da177e4SLinus Torvalds * "never transmit" 25401da177e4SLinus Torvalds */ 25411da177e4SLinus Torvalds if (pkt_dev->delay_us == 0x7FFFFFFF) { 25421da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us; 25431da177e4SLinus Torvalds pkt_dev->next_tx_ns = pkt_dev->delay_ns; 25441da177e4SLinus Torvalds goto out; 25451da177e4SLinus Torvalds } 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds if (netif_queue_stopped(odev) || need_resched()) { 25491da177e4SLinus Torvalds idle_start = getCurUs(); 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds if (!netif_running(odev)) { 25521da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 25531da177e4SLinus Torvalds goto out; 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds if (need_resched()) 25561da177e4SLinus Torvalds schedule(); 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds if (netif_queue_stopped(odev)) { 25611da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 25621da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 25631da177e4SLinus Torvalds goto out; /* Try the next interface */ 25641da177e4SLinus Torvalds } 25651da177e4SLinus Torvalds } 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds if (pkt_dev->last_ok || !pkt_dev->skb) { 25681da177e4SLinus Torvalds if ((++pkt_dev->clone_count >= pkt_dev->clone_skb ) || (!pkt_dev->skb)) { 25691da177e4SLinus Torvalds /* build a new pkt */ 25701da177e4SLinus Torvalds if (pkt_dev->skb) 25711da177e4SLinus Torvalds kfree_skb(pkt_dev->skb); 25721da177e4SLinus Torvalds 25731da177e4SLinus Torvalds pkt_dev->skb = fill_packet(odev, pkt_dev); 25741da177e4SLinus Torvalds if (pkt_dev->skb == NULL) { 25751da177e4SLinus Torvalds printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n"); 25761da177e4SLinus Torvalds schedule(); 25771da177e4SLinus Torvalds pkt_dev->clone_count--; /* back out increment, OOM */ 25781da177e4SLinus Torvalds goto out; 25791da177e4SLinus Torvalds } 25801da177e4SLinus Torvalds pkt_dev->allocated_skbs++; 25811da177e4SLinus Torvalds pkt_dev->clone_count = 0; /* reset counter */ 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds spin_lock_bh(&odev->xmit_lock); 25861da177e4SLinus Torvalds if (!netif_queue_stopped(odev)) { 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds atomic_inc(&(pkt_dev->skb->users)); 25891da177e4SLinus Torvalds retry_now: 25901da177e4SLinus Torvalds ret = odev->hard_start_xmit(pkt_dev->skb, odev); 25911da177e4SLinus Torvalds if (likely(ret == NETDEV_TX_OK)) { 25921da177e4SLinus Torvalds pkt_dev->last_ok = 1; 25931da177e4SLinus Torvalds pkt_dev->sofar++; 25941da177e4SLinus Torvalds pkt_dev->seq_num++; 25951da177e4SLinus Torvalds pkt_dev->tx_bytes += pkt_dev->cur_pkt_size; 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds } else if (ret == NETDEV_TX_LOCKED 25981da177e4SLinus Torvalds && (odev->features & NETIF_F_LLTX)) { 25991da177e4SLinus Torvalds cpu_relax(); 26001da177e4SLinus Torvalds goto retry_now; 26011da177e4SLinus Torvalds } else { /* Retry it next time */ 26021da177e4SLinus Torvalds 26031da177e4SLinus Torvalds atomic_dec(&(pkt_dev->skb->users)); 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds if (debug && net_ratelimit()) 26061da177e4SLinus Torvalds printk(KERN_INFO "pktgen: Hard xmit error\n"); 26071da177e4SLinus Torvalds 26081da177e4SLinus Torvalds pkt_dev->errors++; 26091da177e4SLinus Torvalds pkt_dev->last_ok = 0; 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); 26131da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds pkt_dev->next_tx_us += pkt_dev->delay_us; 26161da177e4SLinus Torvalds pkt_dev->next_tx_ns += pkt_dev->delay_ns; 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds if (pkt_dev->next_tx_ns > 1000) { 26191da177e4SLinus Torvalds pkt_dev->next_tx_us++; 26201da177e4SLinus Torvalds pkt_dev->next_tx_ns -= 1000; 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds } 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds else { /* Retry it next time */ 26251da177e4SLinus Torvalds pkt_dev->last_ok = 0; 26261da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 26271da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 26281da177e4SLinus Torvalds } 26291da177e4SLinus Torvalds 26301da177e4SLinus Torvalds spin_unlock_bh(&odev->xmit_lock); 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds /* If pkt_dev->count is zero, then run forever */ 26331da177e4SLinus Torvalds if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) { 26341da177e4SLinus Torvalds if (atomic_read(&(pkt_dev->skb->users)) != 1) { 26351da177e4SLinus Torvalds idle_start = getCurUs(); 26361da177e4SLinus Torvalds while (atomic_read(&(pkt_dev->skb->users)) != 1) { 26371da177e4SLinus Torvalds if (signal_pending(current)) { 26381da177e4SLinus Torvalds break; 26391da177e4SLinus Torvalds } 26401da177e4SLinus Torvalds schedule(); 26411da177e4SLinus Torvalds } 26421da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 26431da177e4SLinus Torvalds } 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds /* Done with this */ 26461da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds out:; 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds /* 26521da177e4SLinus Torvalds * Main loop of the thread goes here 26531da177e4SLinus Torvalds */ 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds static void pktgen_thread_worker(struct pktgen_thread *t) 26561da177e4SLinus Torvalds { 26571da177e4SLinus Torvalds DEFINE_WAIT(wait); 26581da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 26591da177e4SLinus Torvalds int cpu = t->cpu; 26601da177e4SLinus Torvalds sigset_t tmpsig; 26611da177e4SLinus Torvalds u32 max_before_softirq; 26621da177e4SLinus Torvalds u32 tx_since_softirq = 0; 26631da177e4SLinus Torvalds 26641da177e4SLinus Torvalds daemonize("pktgen/%d", cpu); 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */ 26671da177e4SLinus Torvalds 26681da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 26691da177e4SLinus Torvalds tmpsig = current->blocked; 26701da177e4SLinus Torvalds siginitsetinv(¤t->blocked, 26711da177e4SLinus Torvalds sigmask(SIGKILL) | 26721da177e4SLinus Torvalds sigmask(SIGSTOP)| 26731da177e4SLinus Torvalds sigmask(SIGTERM)); 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds recalc_sigpending(); 26761da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds /* Migrate to the right CPU */ 26791da177e4SLinus Torvalds set_cpus_allowed(current, cpumask_of_cpu(cpu)); 26801da177e4SLinus Torvalds if (smp_processor_id() != cpu) 26811da177e4SLinus Torvalds BUG(); 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds init_waitqueue_head(&t->queue); 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds t->control &= ~(T_TERMINATE); 26861da177e4SLinus Torvalds t->control &= ~(T_RUN); 26871da177e4SLinus Torvalds t->control &= ~(T_STOP); 26881da177e4SLinus Torvalds t->control &= ~(T_REMDEV); 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds t->pid = current->pid; 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid)); 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds max_before_softirq = t->max_before_softirq; 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds __set_current_state(TASK_INTERRUPTIBLE); 26971da177e4SLinus Torvalds mb(); 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds while (1) { 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds /* 27041da177e4SLinus Torvalds * Get next dev to xmit -- if any. 27051da177e4SLinus Torvalds */ 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds pkt_dev = next_to_run(t); 27081da177e4SLinus Torvalds 27091da177e4SLinus Torvalds if (pkt_dev) { 27101da177e4SLinus Torvalds 27111da177e4SLinus Torvalds pktgen_xmit(pkt_dev); 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds /* 27141da177e4SLinus Torvalds * We like to stay RUNNING but must also give 27151da177e4SLinus Torvalds * others fair share. 27161da177e4SLinus Torvalds */ 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds tx_since_softirq += pkt_dev->last_ok; 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds if (tx_since_softirq > max_before_softirq) { 27211da177e4SLinus Torvalds if (local_softirq_pending()) 27221da177e4SLinus Torvalds do_softirq(); 27231da177e4SLinus Torvalds tx_since_softirq = 0; 27241da177e4SLinus Torvalds } 27251da177e4SLinus Torvalds } else { 27261da177e4SLinus Torvalds prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE); 27271da177e4SLinus Torvalds schedule_timeout(HZ/10); 27281da177e4SLinus Torvalds finish_wait(&(t->queue), &wait); 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds /* 27321da177e4SLinus Torvalds * Back from sleep, either due to the timeout or signal. 27331da177e4SLinus Torvalds * We check if we have any "posted" work for us. 27341da177e4SLinus Torvalds */ 27351da177e4SLinus Torvalds 27361da177e4SLinus Torvalds if (t->control & T_TERMINATE || signal_pending(current)) 27371da177e4SLinus Torvalds /* we received a request to terminate ourself */ 27381da177e4SLinus Torvalds break; 27391da177e4SLinus Torvalds 27401da177e4SLinus Torvalds 27411da177e4SLinus Torvalds if(t->control & T_STOP) { 27421da177e4SLinus Torvalds pktgen_stop(t); 27431da177e4SLinus Torvalds t->control &= ~(T_STOP); 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 27461da177e4SLinus Torvalds if(t->control & T_RUN) { 27471da177e4SLinus Torvalds pktgen_run(t); 27481da177e4SLinus Torvalds t->control &= ~(T_RUN); 27491da177e4SLinus Torvalds } 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds if(t->control & T_REMDEV) { 27521da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 27531da177e4SLinus Torvalds t->control &= ~(T_REMDEV); 27541da177e4SLinus Torvalds } 27551da177e4SLinus Torvalds 27561da177e4SLinus Torvalds if (need_resched()) 27571da177e4SLinus Torvalds schedule(); 27581da177e4SLinus Torvalds } 27591da177e4SLinus Torvalds 27601da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name)); 27611da177e4SLinus Torvalds pktgen_stop(t); 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: %s removing all device\n", t->name)); 27641da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 27651da177e4SLinus Torvalds 27661da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name)); 27671da177e4SLinus Torvalds pktgen_rem_thread(t); 27681da177e4SLinus Torvalds } 27691da177e4SLinus Torvalds 27701da177e4SLinus Torvalds static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, const char* ifname) 27711da177e4SLinus Torvalds { 27721da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 27731da177e4SLinus Torvalds if_lock(t); 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds for(pkt_dev=t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) { 2776d50a6b56SStephen Hemminger if (strncmp(pkt_dev->ifname, ifname, IFNAMSIZ) == 0) { 27771da177e4SLinus Torvalds break; 27781da177e4SLinus Torvalds } 27791da177e4SLinus Torvalds } 27801da177e4SLinus Torvalds 27811da177e4SLinus Torvalds if_unlock(t); 27821da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname,pkt_dev)); 27831da177e4SLinus Torvalds return pkt_dev; 27841da177e4SLinus Torvalds } 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds /* 27871da177e4SLinus Torvalds * Adds a dev at front of if_list. 27881da177e4SLinus Torvalds */ 27891da177e4SLinus Torvalds 27901da177e4SLinus Torvalds static int add_dev_to_thread(struct pktgen_thread *t, struct pktgen_dev *pkt_dev) 27911da177e4SLinus Torvalds { 27921da177e4SLinus Torvalds int rv = 0; 27931da177e4SLinus Torvalds 27941da177e4SLinus Torvalds if_lock(t); 27951da177e4SLinus Torvalds 27961da177e4SLinus Torvalds if (pkt_dev->pg_thread) { 27971da177e4SLinus Torvalds printk("pktgen: ERROR: already assigned to a thread.\n"); 27981da177e4SLinus Torvalds rv = -EBUSY; 27991da177e4SLinus Torvalds goto out; 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds pkt_dev->next =t->if_list; t->if_list=pkt_dev; 28021da177e4SLinus Torvalds pkt_dev->pg_thread = t; 28031da177e4SLinus Torvalds pkt_dev->running = 0; 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds out: 28061da177e4SLinus Torvalds if_unlock(t); 28071da177e4SLinus Torvalds return rv; 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds /* Called under thread lock */ 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char* ifname) 28131da177e4SLinus Torvalds { 28141da177e4SLinus Torvalds struct pktgen_dev *pkt_dev; 2815d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 28161da177e4SLinus Torvalds 28171da177e4SLinus Torvalds /* We don't allow a device to be on several threads */ 28181da177e4SLinus Torvalds 2819d50a6b56SStephen Hemminger pkt_dev = __pktgen_NN_threads(ifname, FIND); 2820d50a6b56SStephen Hemminger if (pkt_dev) { 2821d50a6b56SStephen Hemminger printk("pktgen: ERROR: interface already used.\n"); 2822d50a6b56SStephen Hemminger return -EBUSY; 2823d50a6b56SStephen Hemminger } 28241da177e4SLinus Torvalds 28252845b63bSStephen Hemminger pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL); 28261da177e4SLinus Torvalds if (!pkt_dev) 28271da177e4SLinus Torvalds return -ENOMEM; 28281da177e4SLinus Torvalds 28291da177e4SLinus Torvalds pkt_dev->flows = vmalloc(MAX_CFLOWS*sizeof(struct flow_state)); 28301da177e4SLinus Torvalds if (pkt_dev->flows == NULL) { 28311da177e4SLinus Torvalds kfree(pkt_dev); 28321da177e4SLinus Torvalds return -ENOMEM; 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds memset(pkt_dev->flows, 0, MAX_CFLOWS*sizeof(struct flow_state)); 28351da177e4SLinus Torvalds 28361da177e4SLinus Torvalds pkt_dev->min_pkt_size = ETH_ZLEN; 28371da177e4SLinus Torvalds pkt_dev->max_pkt_size = ETH_ZLEN; 28381da177e4SLinus Torvalds pkt_dev->nfrags = 0; 28391da177e4SLinus Torvalds pkt_dev->clone_skb = pg_clone_skb_d; 28401da177e4SLinus Torvalds pkt_dev->delay_us = pg_delay_d / 1000; 28411da177e4SLinus Torvalds pkt_dev->delay_ns = pg_delay_d % 1000; 28421da177e4SLinus Torvalds pkt_dev->count = pg_count_d; 28431da177e4SLinus Torvalds pkt_dev->sofar = 0; 28441da177e4SLinus Torvalds pkt_dev->udp_src_min = 9; /* sink port */ 28451da177e4SLinus Torvalds pkt_dev->udp_src_max = 9; 28461da177e4SLinus Torvalds pkt_dev->udp_dst_min = 9; 28471da177e4SLinus Torvalds pkt_dev->udp_dst_max = 9; 28481da177e4SLinus Torvalds 2849d50a6b56SStephen Hemminger strncpy(pkt_dev->ifname, ifname, IFNAMSIZ); 28501da177e4SLinus Torvalds 28511da177e4SLinus Torvalds if (! pktgen_setup_dev(pkt_dev)) { 28521da177e4SLinus Torvalds printk("pktgen: ERROR: pktgen_setup_dev failed.\n"); 28531da177e4SLinus Torvalds if (pkt_dev->flows) 28541da177e4SLinus Torvalds vfree(pkt_dev->flows); 28551da177e4SLinus Torvalds kfree(pkt_dev); 28561da177e4SLinus Torvalds return -ENODEV; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds 2859d50a6b56SStephen Hemminger pe = create_proc_entry(ifname, 0600, pg_proc_dir); 2860d50a6b56SStephen Hemminger if (!pe) { 2861d50a6b56SStephen Hemminger printk("pktgen: cannot create %s/%s procfs entry.\n", 2862d50a6b56SStephen Hemminger PG_PROC_DIR, ifname); 28631da177e4SLinus Torvalds if (pkt_dev->flows) 28641da177e4SLinus Torvalds vfree(pkt_dev->flows); 28651da177e4SLinus Torvalds kfree(pkt_dev); 28661da177e4SLinus Torvalds return -EINVAL; 28671da177e4SLinus Torvalds } 2868d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_if_fops; 2869d50a6b56SStephen Hemminger pe->data = pkt_dev; 28701da177e4SLinus Torvalds 28711da177e4SLinus Torvalds return add_dev_to_thread(t, pkt_dev); 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds 287469549dddSLuiz Capitulino static struct pktgen_thread * __init pktgen_find_thread(const char* name) 28751da177e4SLinus Torvalds { 28761da177e4SLinus Torvalds struct pktgen_thread *t = NULL; 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds thread_lock(); 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds t = pktgen_threads; 28811da177e4SLinus Torvalds while (t) { 28821da177e4SLinus Torvalds if (strcmp(t->name, name) == 0) 28831da177e4SLinus Torvalds break; 28841da177e4SLinus Torvalds 28851da177e4SLinus Torvalds t = t->next; 28861da177e4SLinus Torvalds } 28871da177e4SLinus Torvalds thread_unlock(); 28881da177e4SLinus Torvalds return t; 28891da177e4SLinus Torvalds } 28901da177e4SLinus Torvalds 289169549dddSLuiz Capitulino static int __init pktgen_create_thread(const char* name, int cpu) 28921da177e4SLinus Torvalds { 28931da177e4SLinus Torvalds struct pktgen_thread *t = NULL; 2894d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds if (strlen(name) > 31) { 28971da177e4SLinus Torvalds printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n"); 28981da177e4SLinus Torvalds return -EINVAL; 28991da177e4SLinus Torvalds } 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds if (pktgen_find_thread(name)) { 29021da177e4SLinus Torvalds printk("pktgen: ERROR: thread: %s already exists\n", name); 29031da177e4SLinus Torvalds return -EINVAL; 29041da177e4SLinus Torvalds } 29051da177e4SLinus Torvalds 29062845b63bSStephen Hemminger t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL); 29071da177e4SLinus Torvalds if (!t) { 29081da177e4SLinus Torvalds printk("pktgen: ERROR: out of memory, can't create new thread.\n"); 29091da177e4SLinus Torvalds return -ENOMEM; 29101da177e4SLinus Torvalds } 29111da177e4SLinus Torvalds 29121da177e4SLinus Torvalds strcpy(t->name, name); 29131da177e4SLinus Torvalds spin_lock_init(&t->if_lock); 29141da177e4SLinus Torvalds t->cpu = cpu; 29151da177e4SLinus Torvalds 2916d50a6b56SStephen Hemminger pe = create_proc_entry(t->name, 0600, pg_proc_dir); 2917d50a6b56SStephen Hemminger if (!pe) { 2918d50a6b56SStephen Hemminger printk("pktgen: cannot create %s/%s procfs entry.\n", 2919d50a6b56SStephen Hemminger PG_PROC_DIR, t->name); 29201da177e4SLinus Torvalds kfree(t); 29211da177e4SLinus Torvalds return -EINVAL; 29221da177e4SLinus Torvalds } 2923d50a6b56SStephen Hemminger 2924d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_thread_fops; 2925d50a6b56SStephen Hemminger pe->data = t; 29261da177e4SLinus Torvalds 29271da177e4SLinus Torvalds t->next = pktgen_threads; 29281da177e4SLinus Torvalds pktgen_threads = t; 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds if (kernel_thread((void *) pktgen_thread_worker, (void *) t, 29311da177e4SLinus Torvalds CLONE_FS | CLONE_FILES | CLONE_SIGHAND) < 0) 29321da177e4SLinus Torvalds printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu); 29331da177e4SLinus Torvalds 29341da177e4SLinus Torvalds return 0; 29351da177e4SLinus Torvalds } 29361da177e4SLinus Torvalds 29371da177e4SLinus Torvalds /* 29381da177e4SLinus Torvalds * Removes a device from the thread if_list. 29391da177e4SLinus Torvalds */ 29401da177e4SLinus Torvalds static void _rem_dev_from_if_list(struct pktgen_thread *t, struct pktgen_dev *pkt_dev) 29411da177e4SLinus Torvalds { 29421da177e4SLinus Torvalds struct pktgen_dev *i, *prev = NULL; 29431da177e4SLinus Torvalds 29441da177e4SLinus Torvalds i = t->if_list; 29451da177e4SLinus Torvalds 29461da177e4SLinus Torvalds while(i) { 29471da177e4SLinus Torvalds if(i == pkt_dev) { 29481da177e4SLinus Torvalds if(prev) prev->next = i->next; 29491da177e4SLinus Torvalds else t->if_list = NULL; 29501da177e4SLinus Torvalds break; 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds prev = i; 29531da177e4SLinus Torvalds i=i->next; 29541da177e4SLinus Torvalds } 29551da177e4SLinus Torvalds } 29561da177e4SLinus Torvalds 29571da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *pkt_dev) 29581da177e4SLinus Torvalds { 29591da177e4SLinus Torvalds 29601da177e4SLinus Torvalds PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev)); 29611da177e4SLinus Torvalds 29621da177e4SLinus Torvalds if (pkt_dev->running) { 29631da177e4SLinus Torvalds printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n"); 29641da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 29651da177e4SLinus Torvalds } 29661da177e4SLinus Torvalds 29671da177e4SLinus Torvalds /* Dis-associate from the interface */ 29681da177e4SLinus Torvalds 29691da177e4SLinus Torvalds if (pkt_dev->odev) { 29701da177e4SLinus Torvalds dev_put(pkt_dev->odev); 29711da177e4SLinus Torvalds pkt_dev->odev = NULL; 29721da177e4SLinus Torvalds } 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds /* And update the thread if_list */ 29751da177e4SLinus Torvalds 29761da177e4SLinus Torvalds _rem_dev_from_if_list(t, pkt_dev); 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds /* Clean up proc file system */ 29791da177e4SLinus Torvalds 2980d50a6b56SStephen Hemminger remove_proc_entry(pkt_dev->ifname, pg_proc_dir); 29811da177e4SLinus Torvalds 29821da177e4SLinus Torvalds if (pkt_dev->flows) 29831da177e4SLinus Torvalds vfree(pkt_dev->flows); 29841da177e4SLinus Torvalds kfree(pkt_dev); 29851da177e4SLinus Torvalds return 0; 29861da177e4SLinus Torvalds } 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds static int __init pg_init(void) 29891da177e4SLinus Torvalds { 29901da177e4SLinus Torvalds int cpu; 2991d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 2992d50a6b56SStephen Hemminger 29931da177e4SLinus Torvalds printk(version); 29941da177e4SLinus Torvalds 2995d50a6b56SStephen Hemminger pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net); 2996d50a6b56SStephen Hemminger if (!pg_proc_dir) 2997d50a6b56SStephen Hemminger return -ENODEV; 2998d50a6b56SStephen Hemminger pg_proc_dir->owner = THIS_MODULE; 29991da177e4SLinus Torvalds 3000d50a6b56SStephen Hemminger pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir); 3001d50a6b56SStephen Hemminger if (pe == NULL) { 3002d50a6b56SStephen Hemminger printk("pktgen: ERROR: cannot create %s procfs entry.\n", PGCTRL); 3003d50a6b56SStephen Hemminger proc_net_remove(PG_PROC_DIR); 30041da177e4SLinus Torvalds return -EINVAL; 30051da177e4SLinus Torvalds } 30061da177e4SLinus Torvalds 3007d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_fops; 3008d50a6b56SStephen Hemminger pe->data = NULL; 30091da177e4SLinus Torvalds 30101da177e4SLinus Torvalds /* Register us to receive netdevice events */ 30111da177e4SLinus Torvalds register_netdevice_notifier(&pktgen_notifier_block); 30121da177e4SLinus Torvalds 3013670c02c2SJohn Hawkes for_each_online_cpu(cpu) { 30141da177e4SLinus Torvalds char buf[30]; 30151da177e4SLinus Torvalds 30161da177e4SLinus Torvalds sprintf(buf, "kpktgend_%i", cpu); 30171da177e4SLinus Torvalds pktgen_create_thread(buf, cpu); 30181da177e4SLinus Torvalds } 30191da177e4SLinus Torvalds return 0; 30201da177e4SLinus Torvalds } 30211da177e4SLinus Torvalds 30221da177e4SLinus Torvalds static void __exit pg_cleanup(void) 30231da177e4SLinus Torvalds { 30241da177e4SLinus Torvalds wait_queue_head_t queue; 30251da177e4SLinus Torvalds init_waitqueue_head(&queue); 30261da177e4SLinus Torvalds 30271da177e4SLinus Torvalds /* Stop all interfaces & threads */ 30281da177e4SLinus Torvalds 30291da177e4SLinus Torvalds while (pktgen_threads) { 30301da177e4SLinus Torvalds struct pktgen_thread *t = pktgen_threads; 30311da177e4SLinus Torvalds pktgen_threads->control |= (T_TERMINATE); 30321da177e4SLinus Torvalds 30331da177e4SLinus Torvalds wait_event_interruptible_timeout(queue, (t != pktgen_threads), HZ); 30341da177e4SLinus Torvalds } 30351da177e4SLinus Torvalds 30361da177e4SLinus Torvalds /* Un-register us from receiving netdevice events */ 30371da177e4SLinus Torvalds unregister_netdevice_notifier(&pktgen_notifier_block); 30381da177e4SLinus Torvalds 30391da177e4SLinus Torvalds /* Clean up proc file system */ 3040d50a6b56SStephen Hemminger remove_proc_entry(PGCTRL, pg_proc_dir); 3041d50a6b56SStephen Hemminger proc_net_remove(PG_PROC_DIR); 30421da177e4SLinus Torvalds } 30431da177e4SLinus Torvalds 30441da177e4SLinus Torvalds 30451da177e4SLinus Torvalds module_init(pg_init); 30461da177e4SLinus Torvalds module_exit(pg_cleanup); 30471da177e4SLinus Torvalds 30481da177e4SLinus Torvalds MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se"); 30491da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool"); 30501da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 30511da177e4SLinus Torvalds module_param(pg_count_d, int, 0); 30521da177e4SLinus Torvalds module_param(pg_delay_d, int, 0); 30531da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0); 30541da177e4SLinus Torvalds module_param(debug, int, 0); 3055