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